BerkeleyDB使用的一个简单封装

001package db;
002  
003import java.io.File;
004import com.sleepycat.je.Database;
005import com.sleepycat.je.DatabaseConfig;
006import com.sleepycat.je.DatabaseEntry;
007import com.sleepycat.je.DatabaseException;
008import com.sleepycat.je.Environment;
009import com.sleepycat.je.EnvironmentConfig;
010import com.sleepycat.je.LockMode;
011import com.sleepycat.je.OperationStatus;
012  
013public class BerkeleyDB {
014    private Environment env;
015    private Database db;
016  
017    public BerkeleyDB(String path, String dbName) {
018        setUp(path, 1000000);
019        open(dbName);
020    }
021  
022    public String get(String key) throws Exception {
023        byte[] theKey = key.getBytes();
024        DatabaseEntry queryKey = new DatabaseEntry(theKey);
025        DatabaseEntry value = new DatabaseEntry();
026  
027        OperationStatus status = db
028                .get(null, queryKey, value, LockMode.DEFAULT);
029        if (status == OperationStatus.SUCCESS) {
030            return new String(value.getData());
031        }
032        return null;
033    }
034  
035    public boolean put(String key, String value) throws Exception {
036        byte[] theKey = key.getBytes();
037        byte[] theValue = value.getBytes();
038        OperationStatus status = db.putNoOverwrite(null, new DatabaseEntry(
039                theKey), new DatabaseEntry(theValue));
040        if (status == OperationStatus.SUCCESS) {
041            return true;
042        }
043        return false;
044    }
045  
046    public boolean delete(String key) throws Exception {
047        byte[] theKey = key.getBytes();
048        OperationStatus status = db.delete(null, new DatabaseEntry(theKey));
049        if (status == OperationStatus.SUCCESS) {
050            return true;
051        }
052        return false;
053    }
054  
055    public boolean update(String key, String value) throws Exception {
056        byte[] updateKey = key.getBytes();
057        byte[] updateValue = value.getBytes();
058  
059        OperationStatus status = db.put(null, new DatabaseEntry(updateKey),
060                new DatabaseEntry(updateValue));
061        if (status == OperationStatus.SUCCESS) {
062            return true;
063        }
064        return false;
065    }
066  
067    public void close() {
068        try {
069            if (db != null) {
070                db.close();
071            }
072            if (env != null) {
073                env.close();
074            }
075        } catch (DatabaseException e) {
076            e.printStackTrace();
077        }
078    }
079  
080    private void setUp(String path, long cacheSize) {
081        EnvironmentConfig envConfig = new EnvironmentConfig();
082        envConfig.setAllowCreate(true);
083        envConfig.setCacheSize(cacheSize);
084        try {
085            env = new Environment(new File(path), envConfig);
086        } catch (DatabaseException e) {
087            e.printStackTrace();
088        }
089    }
090  
091    private void open(String dbName) {
092        DatabaseConfig dbConfig = new DatabaseConfig();
093        dbConfig.setAllowCreate(true);
094        try {
095            db = env.openDatabase(null, dbName, dbConfig);
096        } catch (DatabaseException e) {
097            e.printStackTrace();
098        }
099    }
100}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值