View Javadoc

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