View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.store.bdb;
    19
    20  import com.sleepycat.je.DatabaseConfig;
    21  import com.sleepycat.je.DatabaseException;
    22  import com.sleepycat.je.Environment;
    23  import com.sleepycat.je.EnvironmentConfig;
    24  import com.sleepycat.je.Transaction;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27
    28  import java.io.File;
    29  import java.util.LinkedList;
    30
    31  /***
    32   * Some helper factory methods for creating default configured Berkeley DB objects
    33   *
    34   * @version $Revision: 1.2 $
    35   */
    36  public class BDbHelper {
    37      private static final Log log = LogFactory.getLog(BDbHelper.class);
    38      private static ThreadLocal threadLocalTxn = new ThreadLocal();
    39
    40      public static Environment createEnvironment(File dir) throws DatabaseException {
    41          dir.mkdirs();
    42          EnvironmentConfig envConfig = new EnvironmentConfig();
    43          envConfig.setAllowCreate(true);
    44          envConfig.setTransactional(true);
    45          return new Environment(dir, envConfig);
    46      }
    47
    48      public static DatabaseConfig createDatabaseConfig() {
    49          DatabaseConfig config = new DatabaseConfig();
    50          config.setTransactional(true);
    51          config.setAllowCreate(true);
    52          return config;
    53      }
    54
    55      /***
    56       * @return the current thread local transaction that is in progress or null if there is no
    57       *         transaction in progress
    58       */
    59      public static Transaction getTransaction() {
    60          LinkedList list = (LinkedList) threadLocalTxn.get();
    61          if (list != null && !list.isEmpty()) {
    62              return (Transaction) list.getFirst();
    63          }
    64          return null;
    65      }
    66
    67      /***
    68       * Pops off the current transaction from the stack
    69       */
    70      public static Transaction popTransaction() {
    71          LinkedList list = (LinkedList) threadLocalTxn.get();
    72          if (list == null || list.isEmpty()) {
    73              log.warn("Attempt to pop transaction when no transaction in progress");
    74              return null;
    75          }
    76          else {
    77              return (Transaction) list.removeFirst();
    78          }
    79      }
    80
    81      /***
    82       * Sets the current transaction, possibly including nesting
    83       */
    84      public static void pushTransaction(Transaction transaction) {
    85          LinkedList list = (LinkedList) threadLocalTxn.get();
    86          if (list == null) {
    87              list = new LinkedList();
    88              threadLocalTxn.set(list);
    89          }
    90          list.addLast(transaction);
    91      }
    92
    93      public static int getTransactionCount() {
    94          LinkedList list = (LinkedList) threadLocalTxn.get();
    95          if (list != null) {
    96              return list.size();
    97          }
    98          return 0;
    99      }
    100
    101     public static byte[] asBytes(long v) {
    102         byte[] data = new byte[8];
    103         data[0] = (byte) (v >>> 56);
    104         data[1] = (byte) (v >>> 48);
    105         data[2] = (byte) (v >>> 40);
    106         data[3] = (byte) (v >>> 32);
    107         data[4] = (byte) (v >>> 24);
    108         data[5] = (byte) (v >>> 16);
    109         data[6] = (byte) (v >>> 8);
    110         data[7] = (byte) (v >>> 0);
    111         return data;
    112     }
    113
    114     public static byte[] asBytes(Long key) {
    115         long v = key.longValue();
    116         return asBytes(v);
    117     }
    118
    119     public static long longFromBytes(byte[] data) {
    120         return (((long) data[0] << 56) +
    121                 ((long) (data[1] & 255) << 48) +
    122                 ((long) (data[2] & 255) << 40) +
    123                 ((long) (data[3] & 255) << 32) +
    124                 ((long) (data[4] & 255) << 24) +
    125                 ((data[5] & 255) << 16) +
    126                 ((data[6] & 255) << 8) +
    127                 ((data[7] & 255) << 0));
    128     }
    129
    130 }