BDB

package com.yihaodian.testBDB;

import java.io.File;

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.EnvironmentStats;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.StatsConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;

public class MyBerkeleyDB {

private Environment env;
private Database db;

public MyBerkeleyDB() {

}

/** 构建Environment: 指定存储的文件(一个Environment可以有多个数据库) */
public void setUp(String path, long cacheSize) {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setCacheSize(cacheSize);// The memory available to the
// database system, in bytes.
File dir = new File(path);
if (!dir.exists()) {// 如果指定的目录不存在,则自动创建
dir.mkdir();
System.out.println("创建目录:" + path);
}
try {
env = new Environment(dir, envConfig);
} catch (DatabaseException e) {
e.printStackTrace();
}
}

/** 构建Database: 指定数据库名字,如果指定名字的数据库不存在,则自动创建。 */
public void open(String dbName) {
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
try {
db = env.openDatabase(null, dbName, dbConfig);
} catch (DatabaseException e) {
e.printStackTrace();
}
}

/** 关闭数据库和环境 */
public void close() {
try {
if (db != null) {
db.close();
}
if (env != null) {
env.close();
}
} catch (DatabaseException e) {
e.printStackTrace();
}
}

/**
* 数据操作:写 BDB存储的数据是无格式的,都是二进制的数据,无论是KEY,还是VALUE。
* 如果我们要存取JAVA对象,需要程序员先序列化成二进制的。
* */
public boolean put(String key, String value) throws Exception {
byte[] theKey = key.getBytes("UTF-8");
byte[] theValue = value.getBytes("UTF-8");
OperationStatus status = db.put(null, new DatabaseEntry(theKey),
new DatabaseEntry(theValue));
if (status == OperationStatus.SUCCESS) {
return true;
}
return false;
}

/** 数据操作:读 */
public String get(String key) throws Exception {
DatabaseEntry queryKey = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
queryKey.setData(key.getBytes("UTF-8"));

OperationStatus status = db
.get(null, queryKey, value, LockMode.DEFAULT);
if (status == OperationStatus.SUCCESS) {
return new String(value.getData());
}
return null;
}

/** 数据操作:修改 (覆盖写就是修改) */
public boolean update(String key, String value) throws Exception {
byte[] updateKey = key.getBytes("UTF-8");
byte[] updateValue = value.getBytes("UTF-8");

OperationStatus status = db.put(null, new DatabaseEntry(updateKey),
new DatabaseEntry(updateValue));
if (status == OperationStatus.SUCCESS) {
return true;
}
return false;
}

/** 数据操作:删除 */
public boolean delete(String key) throws Exception {
byte[] theKey = key.getBytes("UTF-8");
OperationStatus status = db.delete(null, new DatabaseEntry(theKey));
if (status == OperationStatus.SUCCESS) {
return true;
}
return false;
}

public static void main(String[] args) throws Exception {
// MyBerkeleyDB mbdb = new MyBerkeleyDB();
// mbdb.setUp("E:\\bdb", 1000000);
// mbdb.open("myDB");
// System.out.println("开始向Berkeley DB中存入数据...");
// for (int i = 0; i < 20; i++) {
// try {
// String key = "myKey" + i;
// String value = "myValue" + i;
// System.out.println("[" + key + ":" + value + "]");
// mbdb.put(key, value);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// mbdb.put("liwei", "20");
// String value = mbdb.get("liwei");
// System.out.println(value);
//
// mbdb.close();

File envHome = new File("E:\\bdb");
EnvironmentConfig configuration = new EnvironmentConfig();
configuration.setAllowCreate(true);
configuration.setTransactional(false);
configuration.setCachePercent(10);
configuration.setReadOnly(false);

Environment env = new Environment(envHome, configuration);

StoreConfig config = new StoreConfig();
config.setAllowCreate(true);
config.setTransactional(false);
config.setReadOnly(false);

String storeName = "store";
EntityStore store = new EntityStore(env, storeName, config);

PrimaryIndex<Long, ProductEntity> bdb_result = store.getPrimaryIndex(Long.class, ProductEntity.class);

// bdb_result.put(new ProductEntity(11L, "product1"));
// bdb_result.put(new ProductEntity(12L, "product2"));
// bdb_result.put(new ProductEntity(13L, "product3"));
// bdb_result.put(new ProductEntity(14L, "product4"));
// bdb_result.put(new ProductEntity(15L, "product5"));
//
// store.close();
// env.checkpoint(null);
// env.close();

EntityCursor<Long> key_cursor = bdb_result.keys();
for(Long key:key_cursor){
System.out.println(key);
}
key_cursor.close();

EntityCursor<ProductEntity> productCursor = bdb_result.entities();
for(ProductEntity entity:productCursor){
System.out.println(entity.productName);
}
productCursor.close();

// printStats(env);
}

@Entity
static class ProductEntity {

@PrimaryKey
Long productId;

String productName;

ProductEntity(Long productId, String productName) {
this.productId = productId;
this.productName = productName;
}

private ProductEntity() {};
}

public static void printStats(Environment env){
StatsConfig statsConfig = new StatsConfig();
statsConfig.setClear(true);
EnvironmentStats stats = env.getStats(statsConfig);
System.out.println(stats.toString());
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值