1 package org.paneris.bibliomania.fti;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.melati.util.UnexpectedExceptionException;
7
8 import com.sleepycat.db.Database;
9 import com.sleepycat.db.DatabaseConfig;
10 import com.sleepycat.db.DatabaseException;
11 import com.sleepycat.db.DatabaseType;
12 import com.sleepycat.db.MemoryException;
13 import com.sleepycat.db.Cursor;
14 import com.sleepycat.db.DatabaseEntry;
15 import com.sleepycat.db.OperationStatus;
16 import com.sleepycat.db.Transaction;
17
18 public class DbUtils {
19
20 public static void setUserMem(DatabaseEntry databaseEntry, int size) {
21 databaseEntry.setData(new byte[size]);
22 databaseEntry.setSize(size);
23 databaseEntry.setUserBuffer(size, true);
24 }
25
26 public static void setUserMem(DatabaseEntry databaseEntry, byte[] data) {
27 databaseEntry.setData(data);
28 databaseEntry.setSize(data.length);
29 databaseEntry.setUserBuffer(data.length, true);
30 }
31
32 public static DatabaseEntry userMemDatabaseEntry(int size) {
33 DatabaseEntry databaseEntry = new DatabaseEntry();
34 setUserMem(databaseEntry, size);
35 return databaseEntry;
36 }
37
38 public static DatabaseEntry userMemDatabaseEntry(byte[] data) {
39 DatabaseEntry DatabaseEntry = new DatabaseEntry();
40 setUserMem(DatabaseEntry, data);
41 return DatabaseEntry;
42 }
43
44 public static class UnexpectedDatabaseExceptionException
45 extends UnexpectedExceptionException {
46
47 private static final long serialVersionUID = -3258846592503363130L;
48 public String path;
49
50 public UnexpectedDatabaseExceptionException(Exception exception, String path) {
51 super(exception);
52 this.path = path == null ? "<unknown>" : path;
53 }
54
55 public String getMessage() {
56 return "An unexpected exception arose using the Database `"
57 + path
58 + "':\n"
59 + subException;
60 }
61 }
62
63 public static OperationStatus get(Database Database, DatabaseEntry key, DatabaseEntry value, int estimate) {
64 byte[] buf = value.getData();
65 if (buf == null || buf.length == 0)
66 buf = new byte[estimate];
67
68 for (;;) {
69 setUserMem(value, buf);
70 try {
71 return Database.get((Transaction)null, key, value, null);
72 } catch (MemoryException e) {
73 buf = new byte[buf.length * 2];
74 } catch (DatabaseException e) {
75 throw new UnexpectedDatabaseExceptionException(e, null);
76 }
77 }
78 }
79
80 public static OperationStatus get(Cursor cursor, DatabaseEntry key, DatabaseEntry value, int flags, int estimate) {
81 byte[] buf = value.getData();
82 if (buf == null || buf.length == 0)
83 buf = new byte[estimate];
84
85 for (;;) {
86 setUserMem(value, buf);
87 try {
88
89 return cursor.getFirst(key, value, null);
90 } catch (MemoryException e) {
91 buf = new byte[buf.length * 2];
92 } catch (DatabaseException e) {
93 throw new UnexpectedDatabaseExceptionException(e, null);
94 }
95 }
96 }
97
98 public static Database openOrCreateBTreeDb(File dir, String name, int cacheSize)
99 throws IOException, DatabaseException {
100 DatabaseConfig dc = new DatabaseConfig();
101 if (cacheSize != 0)
102 dc.setCacheSize(cacheSize);
103 dc.setAllowCreate(true);
104 dc.setType(DatabaseType.BTREE);
105
106 if (new File(dir, name + ".db").exists())
107 Database.upgrade(new File(dir, name + ".db").getPath(), dc);
108
109 Database it = new Database(new File(dir, name + ".db").getPath(),
110 null, dc);
111
112 return it;
113 }
114
115 public static void main(final String[] args) throws Exception {
116 String dbHome = "/usr/local/share/bibliomania/workspace";
117 openOrCreateBTreeDb(new File(dbHome), "idOfWord", 0);
118 openOrCreateBTreeDb(new File(dbHome), "occurrencesOfWordInText",0);
119 openOrCreateBTreeDb(new File(dbHome), "anchorOfIndex", 0);
120 openOrCreateBTreeDb(new File(dbHome), "blockmarkOfIndex", 0);
121 openOrCreateBTreeDb(new File(dbHome), "wordsInText", 0);
122
123 }
124 }