001 | package db; |
002 | |
003 | import java.io.File; |
004 | import com.sleepycat.je.Database; |
005 | import com.sleepycat.je.DatabaseConfig; |
006 | import com.sleepycat.je.DatabaseEntry; |
007 | import com.sleepycat.je.DatabaseException; |
008 | import com.sleepycat.je.Environment; |
009 | import com.sleepycat.je.EnvironmentConfig; |
010 | import com.sleepycat.je.LockMode; |
011 | import com.sleepycat.je.OperationStatus; |
012 | |
013 | public 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 | } |