| 1 | |
package org.paneris.bibliomania.fti; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileNotFoundException; |
| 5 | |
|
| 6 | |
import com.sleepycat.db.Database; |
| 7 | |
import com.sleepycat.db.DatabaseConfig; |
| 8 | |
import com.sleepycat.db.DatabaseType; |
| 9 | |
import com.sleepycat.db.Environment; |
| 10 | |
import com.sleepycat.db.DatabaseException; |
| 11 | |
import com.sleepycat.db.LockMode; |
| 12 | |
import com.sleepycat.db.MemoryException; |
| 13 | |
import com.sleepycat.db.DatabaseEntry; |
| 14 | |
import com.sleepycat.db.OperationStatus; |
| 15 | |
|
| 16 | |
public class StringDbHash { |
| 17 | |
private Database db; |
| 18 | |
private DatabaseConfig dbConfig; |
| 19 | 0 | private DatabaseEntry keyDatabaseEntry = new DatabaseEntry(), valueDatabaseEntry = new DatabaseEntry(); |
| 20 | |
private String path; |
| 21 | |
|
| 22 | |
public StringDbHash(Environment env, String path, int cacheSize, int mode) |
| 23 | 0 | throws DatabaseException, FileNotFoundException { |
| 24 | 0 | this.path = path; |
| 25 | 0 | dbConfig = new DatabaseConfig(); |
| 26 | 0 | dbConfig.setCacheSize(cacheSize); |
| 27 | 0 | dbConfig.setAllowCreate(true); |
| 28 | 0 | dbConfig.setType(DatabaseType.HASH); |
| 29 | 0 | db = new Database(path, null, dbConfig); |
| 30 | |
|
| 31 | 0 | } |
| 32 | |
|
| 33 | |
public StringDbHash(String path, int cacheSize, int mode) |
| 34 | |
throws DatabaseException, FileNotFoundException { |
| 35 | 0 | this(null, path, cacheSize, mode); |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
public StringDbHash(Environment env, String path, int cacheSize) |
| 39 | |
throws DatabaseException, FileNotFoundException { |
| 40 | 0 | this(env, path, cacheSize, 0644); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public StringDbHash(String path, int cacheSize) |
| 44 | |
throws DatabaseException, FileNotFoundException { |
| 45 | 0 | this(null, path, cacheSize); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
public synchronized void put(String key, String value) { |
| 49 | 0 | DbUtils.setUserMem(keyDatabaseEntry, key.getBytes()); |
| 50 | 0 | DbUtils.setUserMem(valueDatabaseEntry, value.getBytes()); |
| 51 | |
|
| 52 | |
try { |
| 53 | 0 | db.put(null, keyDatabaseEntry, valueDatabaseEntry); |
| 54 | |
} |
| 55 | 0 | catch (DatabaseException e) { |
| 56 | 0 | throw new DbUtils.UnexpectedDatabaseExceptionException(e, path); |
| 57 | 0 | } |
| 58 | 0 | } |
| 59 | |
|
| 60 | 0 | private byte[] buf = new byte[100]; |
| 61 | |
|
| 62 | |
public synchronized String get(String key) { |
| 63 | 0 | DbUtils.setUserMem(keyDatabaseEntry, key.getBytes()); |
| 64 | |
|
| 65 | |
OperationStatus result; |
| 66 | |
for (;;) { |
| 67 | 0 | DbUtils.setUserMem(valueDatabaseEntry, buf); |
| 68 | |
try { |
| 69 | 0 | result = db.get(null, keyDatabaseEntry, valueDatabaseEntry,LockMode.DEFAULT); |
| 70 | 0 | break; |
| 71 | 0 | } catch (MemoryException e) { |
| 72 | 0 | buf = new byte[buf.length * 2]; |
| 73 | |
} |
| 74 | 0 | catch (DatabaseException e) { |
| 75 | 0 | throw new DbUtils.UnexpectedDatabaseExceptionException(e, path); |
| 76 | 0 | } |
| 77 | |
} |
| 78 | |
|
| 79 | 0 | if (buf.length > 10000) |
| 80 | 0 | buf = new byte[10000]; |
| 81 | |
|
| 82 | 0 | return result == OperationStatus.SUCCESS ? |
| 83 | |
new String(valueDatabaseEntry.getData(), 0, valueDatabaseEntry.getSize()) : null; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public void flush() { |
| 87 | |
try { |
| 88 | 0 | db.sync(); |
| 89 | |
} |
| 90 | 0 | catch (DatabaseException e) { |
| 91 | 0 | throw new DbUtils.UnexpectedDatabaseExceptionException(e, path); |
| 92 | 0 | } |
| 93 | 0 | } |
| 94 | |
|
| 95 | |
protected void finalize() throws Throwable { |
| 96 | 0 | db.close(); |
| 97 | 0 | } |
| 98 | |
public static void main(String[] args) throws Exception { |
| 99 | 0 | StringDbHash h = new StringDbHash(null, "/tmp/test.db", 0); |
| 100 | 0 | int e = args[0].indexOf('='); |
| 101 | 0 | if (e == -1) |
| 102 | 0 | System.out.println(h.get(args[0])); |
| 103 | |
else |
| 104 | 0 | h.put(args[0].substring(0, e), args[0].substring(e + 1)); |
| 105 | 0 | h.flush(); |
| 106 | 0 | } |
| 107 | |
} |