Berkeley-DB类操作

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() {

	}

	public 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();
		}
	}

	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();
		}
	}

	public synchronized String get(String key)  {
		DatabaseEntry queryKey = new DatabaseEntry();
		DatabaseEntry value = new DatabaseEntry();
		try{
			queryKey.setData(key.getBytes("UTF-8"));	
			OperationStatus status = db.get(null, queryKey, value, LockMode.DEFAULT);
			if (status == OperationStatus.SUCCESS) {
				return new String(value.getData());
			}
		}catch(Exception ex) {
			//ex.printStackTrace();
			return null;
		}
		return null;
	}

	public synchronized boolean put(String key, String value)  {
		try{
			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;
			}
		}catch(Exception ex) {
			//ex.printStackTrace();
			return false;
		}
		return false;
	}

	public synchronized 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 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 static void main(String[] args) {
		BerkeleyDB bdb = new BerkeleyDB();
		bdb.setUp("d:/news/data"/**HttpContext.getConfig().getString(AppConfig.BDB_PATH)**/, 1000*1000);
		bdb.open("searchDB");
		try {
			boolean isExists = bdb.get("你好啊")!=null;
			System.out.println("--isExists-->"+isExists);
			if (!isExists) {
				System.out.println("--->不存在了啊!sss");
			}
			if (bdb.get("你好啊")!=null) {
				System.out.println("--->已经存在了啊!");
			}
						System.out.println("-----------"+bdb.get("你好啊")+"------"+bdb.get("你好啊1"));
		} catch (Exception e) {			
			e.printStackTrace();
		}
		bdb.close();
	}
}

 b-db在项目中也有应用,写了个基本的类:

 

 

你可能感兴趣的:(Berkeley)