博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Berkeley DB的增删改查的两种实现方式
阅读量:5820 次
发布时间:2019-06-18

本文共 5229 字,大约阅读时间需要 17 分钟。

hot3.png

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 BerkeleydbDaoSortedMapImpl
implements 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 BerkeleydbDatabaseObjectImpl
implements 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); }}

实验证明两种方式存储的数据结构是不一样的,具体是什么样的目前还不清楚,但是如果是第一种方法存储的数据,用第二种是无法读取的,也就是说两者是相互独立的,推荐使用第一种。

转载于:https://my.oschina.net/sourcecoding/blog/57019

你可能感兴趣的文章
快速傅里叶变换FFT
查看>>
大数据常用基本算法
查看>>
JavaScript学习笔记(十三)——生成器(generator)
查看>>
hibernate保存失败
查看>>
MySQL增量订阅&消费组件Canal POC
查看>>
Sqlite多线程
查看>>
数据结构-时间复杂度
查看>>
对象与字符串相互转换
查看>>
[NOIp2017提高组]小凯的疑惑
查看>>
《C程序设计语言》练习1-5
查看>>
$\frac{dy}{dx}$ 是什么意思?
查看>>
Go开发之路(目录)
查看>>
RHEL6.5安装成功ORACLE11GR2之后,编写PROC程序出错解决方法
查看>>
(50)与magento集成
查看>>
Ubuntu设置python3为默认版本
查看>>
日期Calendar/Date的用法
查看>>
JsonCpp 的使用
查看>>
问题账户需求分析
查看>>
JavaSE-代码块
查看>>
爬取所有校园新闻
查看>>