1 25 29 package org.jresearch.gossip.dao.file.berkeleydb; 30 31 import java.io.UnsupportedEncodingException ; 32 33 import org.jresearch.gossip.dao.file.IFileProcessor; 34 import org.jresearch.gossip.exception.SystemException; 35 36 import com.sleepycat.je.Database; 37 import com.sleepycat.je.DatabaseEntry; 38 import com.sleepycat.je.DatabaseException; 39 import com.sleepycat.je.LockMode; 40 import com.sleepycat.je.OperationStatus; 41 42 46 public class BerkeleyDBFileProcessor implements IFileProcessor { 47 48 private String dbname; 49 50 private FileDbEnv env; 51 52 56 public BerkeleyDBFileProcessor() throws SystemException { 57 env = FileDbEnv.getInstance(); 58 } 59 60 private Database getFileDB() throws SystemException { 61 return env.getFileDb(dbname); 62 } 63 64 67 public String getDbname() { 68 return dbname; 69 } 70 71 76 public void setStoreName(String dbname) { 77 this.dbname = dbname; 78 } 79 80 86 public void saveFileData(byte[] data, String key) throws SystemException { 87 Database db = null; 88 try { 89 db = getFileDB(); 90 DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8")); 91 DatabaseEntry theData = new DatabaseEntry(data); 92 db.put(null, theKey, theData); 93 } catch (UnsupportedEncodingException e) { 94 throw new SystemException(e); 95 } catch (DatabaseException e) { 96 throw new SystemException(e); 97 } finally { 98 if (db != null) { 99 try { 100 db.close(); 101 } catch (DatabaseException e) { 102 throw new SystemException(e); 103 } 104 } 105 } 106 } 107 108 113 public byte[] getFileData(String key) throws SystemException { 114 Database db = null; 115 try { 116 db = getFileDB(); 117 DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8")); 118 DatabaseEntry theData = new DatabaseEntry(); 119 if (db.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { 120 return theData.getData(); 121 } 122 return null; 123 } catch (UnsupportedEncodingException e) { 124 throw new SystemException(e); 125 } catch (DatabaseException e) { 126 throw new SystemException(e); 127 } finally { 128 if (db != null) { 129 try { 130 db.close(); 131 } catch (DatabaseException e) { 132 throw new SystemException(e); 133 } 134 } 135 } 136 } 137 138 143 public void removeFileData(String key) throws SystemException { 144 Database db = null; 145 try { 146 db = getFileDB(); 147 DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8")); 148 db.delete(null, theKey); 149 } catch (UnsupportedEncodingException e) { 150 throw new SystemException(e); 151 } catch (DatabaseException e) { 152 throw new SystemException(e); 153 } finally { 154 if (db != null) { 155 try { 156 db.close(); 157 } catch (DatabaseException e) { 158 throw new SystemException(e); 159 } 160 } 161 } 162 163 } 164 165 } | Popular Tags |