欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java操作BerkeleyDB封装类,javaberkeleydb封装,[Java]代码 jav

来源: javaer 分享于  点击 24317 次 点评:87

java操作BerkeleyDB封装类,javaberkeleydb封装,[Java]代码 jav


[Java]代码 java操作BerkeleyDB封装类

package cn.outofmemory.db;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.LockMode;import com.sleepycat.je.OperationStatus;public class BerkeleyDB {    private Environment env;    private Database db;    public BerkeleyDB(String path, String dbName) {        setUp(path, 1000000);        open(dbName);    }    public String get(String key) throws Exception {        byte[] theKey = key.getBytes();        DatabaseEntry queryKey = new DatabaseEntry(theKey);        DatabaseEntry value = new DatabaseEntry();        OperationStatus status = db                .get(null, queryKey, value, LockMode.DEFAULT);        if (status == OperationStatus.SUCCESS) {            return new String(value.getData());        }        return null;    }    public boolean put(String key, String value) throws Exception {        byte[] theKey = key.getBytes();        byte[] theValue = value.getBytes();        OperationStatus status = db.putNoOverwrite(null, new DatabaseEntry(                theKey), new DatabaseEntry(theValue));        if (status == OperationStatus.SUCCESS) {            return true;        }        return false;    }    public boolean delete(String key) throws Exception {        byte[] theKey = key.getBytes();        OperationStatus status = db.delete(null, new DatabaseEntry(theKey));        if (status == OperationStatus.SUCCESS) {            return true;        }        return false;    }    public boolean update(String key, String value) throws Exception {        byte[] updateKey = key.getBytes();        byte[] updateValue = value.getBytes();        OperationStatus status = db.put(null, new DatabaseEntry(updateKey),                new DatabaseEntry(updateValue));        if (status == OperationStatus.SUCCESS) {            return true;        }        return false;    }    public void close() {        try {            if (db != null) {                db.close();            }            if (env != null) {                env.close();            }        } catch (DatabaseException e) {            e.printStackTrace();        }    }    private void setUp(String path, long cacheSize) {        EnvironmentConfig envConfig = new EnvironmentConfig();        envConfig.setAllowCreate(true);        envConfig.setCacheSize(cacheSize);        try {            env = new Environment(new File(path), envConfig);        } catch (DatabaseException e) {            e.printStackTrace();        }    }    private void open(String dbName) {        DatabaseConfig dbConfig = new DatabaseConfig();        dbConfig.setAllowCreate(true);        try {            db = env.openDatabase(null, dbName, dbConfig);        } catch (DatabaseException e) {            e.printStackTrace();        }    }}
相关栏目:

用户点评