package dao;/** * Berkeley DB interface of DAO * */public interface BerkeleydbDao{ /** * open database * */ public void openConnection(String filePath, String databaseName); /** * 关闭数据库 * */ public void closeConnection(); /** * insert * */ public void save(String name, T t); /** * delete * */ public void delete(String name); /** * update * */ public void update(String name, T t); /** * select * */ public T get(String name); }
package dao.impl;import java.io.File;import com.sleepycat.bind.EntryBinding;import com.sleepycat.bind.serial.SerialBinding;import com.sleepycat.bind.serial.StoredClassCatalog;import com.sleepycat.collections.StoredMap;import com.sleepycat.je.Database;import com.sleepycat.je.DatabaseConfig;import com.sleepycat.je.Environment;import com.sleepycat.je.EnvironmentConfig;import dao.BerkeleydbDao;/** * 使用StoredMap实现 * */public class BerkeleydbDaoSortedMapImplimplements BerkeleydbDao { Environment env = null; private Database database = null; private StoredMap storedMap = null; private Class persistentClass = null; EntryBinding keyBinding = null; SerialBinding valueBinding = null; public BerkeleydbDaoSortedMapImpl(Class persistentClass){ this.persistentClass = persistentClass; } @Override public void openConnection(String filePath, String databaseName) { File file = new File(filePath); EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); envConfig.setTransactional(true); env = new Environment(file, envConfig); DatabaseConfig databaseConfig = new DatabaseConfig(); databaseConfig.setAllowCreate(true); databaseConfig.setTransactional(true); database = env.openDatabase(null, databaseName, databaseConfig); StoredClassCatalog catalog = new StoredClassCatalog(database); keyBinding = new SerialBinding (catalog, String.class); valueBinding = new SerialBinding (catalog, persistentClass); storedMap = new StoredMap (database, keyBinding, valueBinding, true); } @Override public void closeConnection() { if(database != null){ database.close(); if(env != null){ env.cleanLog(); env.close(); } } } @Override public void delete(String name) { storedMap.remove(name); } @Override public T get(String name) { return storedMap.get(name); } @Override public void save(String name, T t) { storedMap.put(name, t); } @Override public void update(String name, T t) { save(name, t); } }
package dao.impl;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import com.sleepycat.je.Database;import com.sleepycat.je.DatabaseConfig;import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.Environment;import com.sleepycat.je.EnvironmentConfig;import com.sleepycat.je.LockMode;import com.sleepycat.je.OperationStatus;import dao.BerkeleydbDao;/** * 通过database对象直接操作 * */public class BerkeleydbDatabaseObjectImplimplements BerkeleydbDao { Environment env = null; private Database database = null; @Override public void openConnection(String filePath, String databaseName) { File file = new File(filePath); EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); envConfig.setTransactional(true); env = new Environment(file, envConfig); DatabaseConfig databaseConfig = new DatabaseConfig(); databaseConfig.setAllowCreate(true); databaseConfig.setTransactional(true); database = env.openDatabase(null, databaseName, databaseConfig); } @Override public void closeConnection() { if(database != null){ database.close(); if(env != null){ env.cleanLog(); env.close(); } } } @Override public void delete(String name) { DatabaseEntry keyEntry = new DatabaseEntry(); keyEntry.setData(name.getBytes()); database.delete(null, keyEntry); } @SuppressWarnings("unchecked") @Override public T get(String name) { T t = null; DatabaseEntry keyEntry = new DatabaseEntry(); DatabaseEntry valueEntry = new DatabaseEntry(); keyEntry.setData(name.getBytes()); if(database.get(null, keyEntry, valueEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS){ ByteArrayInputStream bais = new ByteArrayInputStream(valueEntry.getData()); try { ObjectInputStream ois = new ObjectInputStream(bais); t = (T) ois.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return t; } @Override public void save(String name, T t) { DatabaseEntry keyEntry = new DatabaseEntry(); DatabaseEntry valueEntry = new DatabaseEntry(); keyEntry.setData(name.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(t); } catch (IOException e) { e.printStackTrace(); } valueEntry.setData(baos.toByteArray()); database.put(null, keyEntry, valueEntry); } @Override public void update(String name, T t) { save(name, t); }}
实验证明两种方式存储的数据结构是不一样的,具体是什么样的目前还不清楚,但是如果是第一种方法存储的数据,用第二种是无法读取的,也就是说两者是相互独立的,推荐使用第一种。