BerkeleyDB示例

/**
002.* @author zhen.chen
003.* @createTime 2010-2-20 上午10:43:31
004.*/
005. package com.renren.nuclear.storage;
006. import java.io.File;
007. import java.util.ArrayList;
008. import java.util.List;
009. import com.sleepycat.je.Cursor;
010. import com.sleepycat.je.Database;
011. import com.sleepycat.je.DatabaseConfig;
012. import com.sleepycat.je.DatabaseEntry;
013. import com.sleepycat.je.DatabaseException;
014. import com.sleepycat.je.Environment;
015. import com.sleepycat.je.EnvironmentConfig;
016. import com.sleepycat.je.LockMode;
017. import com.sleepycat.je.OperationStatus;
018. import com.sleepycat.je.Transaction;
019.
020. public class Test {
021. private static Environment env;
022. private static Database db;
023. private static long cacheSize = 1000 * 1024 * 1024;
024. private static String path = “./bdb/”;
025. public static void main(String[] args) {
026.EnvironmentConfig envConfig = new EnvironmentConfig();
027.envConfig.setTransactional( true);
028.envConfig.setAllowCreate( true);
029.envConfig.setCacheSize(cacheSize);
030. try {
031.env = new Environment( new File(path), envConfig);
032.} catch (DatabaseException e) {
033.}
034.open(”nuclear”);
035. //写入
036.put(”aa”,”aaa”);
037. //取出
038.get(”aa”);
039.}
040.
041. /**
042.* 写入操作
043.* @author [url=http://www.54chen.com/]www.54chen.com[/url]
044.* @createTime 2010-2-20 上午11:04:43
045.*/
046. public static void put(String key, String value){
047. // open(”nuclear”);
048. byte[] theKey = key.getBytes();
049. byte[] theValue = value.getBytes();
050.OperationStatus status = null;
051.Transaction transaction = null;
052.Cursor cursor = null;
053. boolean succeeded = false;
054. try {
055.transaction = env.beginTransaction( null, null);
056.cursor = db.openCursor(transaction, null);
057.status = cursor.put( new DatabaseEntry(theKey), new DatabaseEntry(theValue));
058. if (status != OperationStatus.SUCCESS){
059. // TODO
060.}
061.succeeded = true;
062.} catch (DatabaseException e) {
063.} finally {
064.attemptClose(cursor);
065. if (succeeded)
066.attemptCommit(transaction);
067. else
068.attemptAbort(transaction);
069.}
070.}
071.
072. /**
073.* 取出操作
074.* @author [url=http://www.54chen.com/]www.54chen.com[/url]
075.* @createTime 2010-2-20 上午11:04:51
076.*/
077. public static List<String> get(String key){
078.ArrayList<String> nuclearStorageValue = new ArrayList<String>();
079.DatabaseEntry queryKey = new DatabaseEntry();
080.DatabaseEntry value = new DatabaseEntry();
081.queryKey.setData(key.getBytes());
082.Cursor cursor = null;
083. try {
084.cursor = db.openCursor( null, null);
085. for(OperationStatus status = cursor.getSearchKey(queryKey, value, LockMode.READ_UNCOMMITTED); status == OperationStatus.SUCCESS; status = cursor.getNextDup(queryKey,value,LockMode.RMW)) {
086.nuclearStorageValue.add(value.getData().toString());
087.}
088.} catch (DatabaseException e) {
089.} finally {
090.attemptClose(cursor);
091.}
092. return nuclearStorageValue;
093.}
094.
095. /**
096.* 打开DB
097.* @author [url=http://www.54chen.com/]www.54chen.com[/url]
098.* @createTime 2010-2-20 上午11:01:48
099.*/
100.
101. private static void open(String dbName) {
102.DatabaseConfig dbConfig = new DatabaseConfig();
103.dbConfig.setAllowCreate( true);
104.dbConfig.setTransactional( true);
105. try {
106.db = env.openDatabase( null, dbName, dbConfig);
107.} catch (DatabaseException e) {
108.e.printStackTrace();
109.}
110.}
111.
112. private static void attemptClose(Cursor cursor) {
113. try {
114. if (cursor != null)
115.cursor.close();
116.} catch (DatabaseException e) {
117. // TODO logger
118.}
119.}
120.
121. private static void attemptCommit(Transaction transaction) {
122. try {
123.transaction.commit();
124.} catch (DatabaseException e) {
125.attemptAbort(transaction);
126. // TODO logger
127.}
128.}
129.
130. private static void attemptAbort(Transaction transaction) {
131. try {
132.transaction.abort();
133.} catch (Exception e) {
134. // TODO logger
135.}
136.}
137.}

你可能感兴趣的:(职场,示例,休闲,BerkeleyDB)