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.1 $
    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          EnvironmentConfig envConfig = new EnvironmentConfig();
    42          envConfig.setAllowCreate(true);
    43          envConfig.setTransactional(true);
    44          return new Environment(dir, envConfig);
    45      }
    46
    47      public static DatabaseConfig createDatabaseConfig() {
    48          DatabaseConfig config = new DatabaseConfig();
    49          config.setTransactional(true);
    50          config.setAllowCreate(true);
    51          return config;
    52      }
    53
    54      /***
    55       * @return the current thread local transaction that is in progress or null if there is no
    56       *         transaction in progress
    57       */
    58      public static Transaction getTransaction() {
    59          LinkedList list = (LinkedList) threadLocalTxn.get();
    60          if (list != null && !list.isEmpty()) {
    61              return (Transaction) list.getFirst();
    62          }
    63          return null;
    64      }
    65
    66      /***
    67       * Pops off the current transaction from the stack
    68       */
    69      public static Transaction popTransaction() {
    70          LinkedList list = (LinkedList) threadLocalTxn.get();
    71          if (list == null || list.isEmpty()) {
    72              log.warn("Attempt to pop transaction when no transaction in progress");
    73              return null;
    74          }
    75          else {
    76              return (Transaction) list.removeFirst();
    77          }
    78      }
    79
    80      /***
    81       * Sets the current transaction, possibly including nesting
    82       */
    83      public static void pushTransaction(Transaction transaction) {
    84          LinkedList list = (LinkedList) threadLocalTxn.get();
    85          if (list == null) {
    86              list = new LinkedList();
    87              threadLocalTxn.set(list);
    88          }
    89          list.addLast(transaction);
    90      }
    91
    92      public static int getTransactionCount() {
    93          LinkedList list = (LinkedList) threadLocalTxn.get();
    94          if (list != null) {
    95              return list.size();
    96          }
    97          return 0;
    98      }
    99
    100     public static byte[] asBytes(long v) {
    101         byte[] data = new byte[8];
    102         data[0] = (byte) (v >>> 56);
    103         data[1] = (byte) (v >>> 48);
    104         data[2] = (byte) (v >>> 40);
    105         data[3] = (byte) (v >>> 32);
    106         data[4] = (byte) (v >>> 24);
    107         data[5] = (byte) (v >>> 16);
    108         data[6] = (byte) (v >>> 8);
    109         data[7] = (byte) (v >>> 0);
    110         return data;
    111     }
    112
    113     public static byte[] asBytes(Long key) {
    114         long v = key.longValue();
    115         return asBytes(v);
    116     }
    117
    118     public static long longFromBytes(byte[] data) {
    119         return (((long) data[0] << 56) +
    120                 ((long) (data[1] & 255) << 48) +
    121                 ((long) (data[2] & 255) << 40) +
    122                 ((long) (data[3] & 255) << 32) +
    123                 ((long) (data[4] & 255) << 24) +
    124                 ((data[5] & 255) << 16) +
    125                 ((data[6] & 255) << 8) +
    126                 ((data[7] & 255) << 0));
    127     }
    128
    129 }