日期:2014-05-16  浏览次数:20413 次

berkeleydb的使用

在napoli的代码中发现这个berkeleydb,版本为3.2.43,使用代码如下

接口定义如下:

/**
 * Project: napoli.client
 * 
 * File Created at Sep 15, 2009
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.alibaba.napoli.client.inner.persistencestore;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 持久化仓库的接口。
 * 
 * @author ding.lid
 * @author guolin.zhuanggl
 */
public interface PersistenceStore<T extends Serializable> {
    /**
     * 写入一条数据。会自动生成一个与之对应的key。
     * 
     * @param data
     */
    void write(T data);

    /**
     * 写入多条数据。
     * 
     * @param dataList
     */
    void batchWrite(List<T> dataList);

    /**
     * 读一条数据。<br>
     * 如果读多条数据的情况下,尽量使用{@link #batchRead(int)}方法,有更好的效率。
     * 
     * @return 读到的数据。如果store中没有数据,则返回<code>null</code>。
     */
    Map.Entry<String, T> read();

    /**
     * 读多条数据。
     * 
     * @param count 要读数据的条数
     * @return 读到的数据。如果store中没有数据,则返回空的Map(即size ==0)。
     */
    Map<String, T> batchRead(int count);

    /**
     * 删除一条数据。
     * 
     * @param key
     */
    void delete(String key);

    /**
     * 删除多条数据。
     * 
     * @param keys 要删除数据的Key
     */
    void delete(List<String> keys);
}
?

?

采用berkeleydb的实现如下:

/**
 * Project: napoli.client
 * 
 * File Created at Sep 15, 2009
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.alibaba.napoli.client.inner.persistencestore.impl;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.napoli.client.inner.persistencestore.PersistenceStore;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.EnvironmentMutableConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.TransactionConfig;

/**
 * @author guolin.zhuanggl
 * @author ding.lid
 */
public class BdbPersistenceStore<T extends Serializable> implements PersistenceStore<T> {
    private static final Log    log                  = LogFactory.getLog(BdbPersistenceStore.class);

    private final static String MESSAGE_DBNAME       = "MESSAGE_DB";
    private final static String MESSAGE_CLASS_DBNAME = "MESSAGE_CLASS_DB";

    private String              bdbStorePath;
    // 默认值 10M
    private long                bdbCheckpointBytes   = 10 * 1024 * 1024;
    // 默认值 5M
    private long                bdbCacheSize         = 5 * 1024 * 1024;

    private Environment         bdbEnvironment;
    private Database            bdb;
    private StoredClassCatalog  bdbClassCatalog;

    private final Class<