View Javadoc

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