| 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 | 0 | public class DbUtils { |
| 19 | |
|
| 20 | |
public static void setUserMem(DatabaseEntry databaseEntry, int size) { |
| 21 | 105 | databaseEntry.setData(new byte[size]); |
| 22 | 105 | databaseEntry.setSize(size); |
| 23 | 105 | databaseEntry.setUserBuffer(size, true); |
| 24 | 105 | } |
| 25 | |
|
| 26 | |
public static void setUserMem(DatabaseEntry databaseEntry, byte[] data) { |
| 27 | 4 | databaseEntry.setData(data); |
| 28 | 4 | databaseEntry.setSize(data.length); |
| 29 | 4 | databaseEntry.setUserBuffer(data.length, true); |
| 30 | 4 | } |
| 31 | |
|
| 32 | |
public static DatabaseEntry userMemDatabaseEntry(int size) { |
| 33 | 105 | DatabaseEntry databaseEntry = new DatabaseEntry(); |
| 34 | 105 | setUserMem(databaseEntry, size); |
| 35 | 105 | return databaseEntry; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public static DatabaseEntry userMemDatabaseEntry(byte[] data) { |
| 39 | 2 | DatabaseEntry DatabaseEntry = new DatabaseEntry(); |
| 40 | 2 | setUserMem(DatabaseEntry, data); |
| 41 | 2 | return DatabaseEntry; |
| 42 | |
} |
| 43 | |
|
| 44 | 0 | 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 | 2 | byte[] buf = value.getData(); |
| 65 | 2 | if (buf == null || buf.length == 0) |
| 66 | 0 | buf = new byte[estimate]; |
| 67 | |
|
| 68 | |
for (;;) { |
| 69 | 2 | setUserMem(value, buf); |
| 70 | |
try { |
| 71 | 2 | return Database.get((Transaction)null, key, value, null); |
| 72 | 0 | } catch (MemoryException e) { |
| 73 | 0 | buf = new byte[buf.length * 2]; |
| 74 | 0 | } catch (DatabaseException e) { |
| 75 | 0 | throw new UnexpectedDatabaseExceptionException(e, null); |
| 76 | 0 | } |
| 77 | |
} |
| 78 | |
} |
| 79 | |
|
| 80 | |
public static OperationStatus get(Cursor cursor, DatabaseEntry key, DatabaseEntry value, int flags, int estimate) { |
| 81 | 0 | byte[] buf = value.getData(); |
| 82 | 0 | if (buf == null || buf.length == 0) |
| 83 | 0 | buf = new byte[estimate]; |
| 84 | |
|
| 85 | |
for (;;) { |
| 86 | 0 | setUserMem(value, buf); |
| 87 | |
try { |
| 88 | |
|
| 89 | 0 | return cursor.getFirst(key, value, null); |
| 90 | 0 | } catch (MemoryException e) { |
| 91 | 0 | buf = new byte[buf.length * 2]; |
| 92 | 0 | } catch (DatabaseException e) { |
| 93 | 0 | throw new UnexpectedDatabaseExceptionException(e, null); |
| 94 | 0 | } |
| 95 | |
} |
| 96 | |
} |
| 97 | |
|
| 98 | |
public static Database openOrCreateBTreeDb(File dir, String name, int cacheSize) |
| 99 | |
throws IOException, DatabaseException { |
| 100 | 87 | DatabaseConfig dc = new DatabaseConfig(); |
| 101 | 87 | if (cacheSize != 0) |
| 102 | 68 | dc.setCacheSize(cacheSize); |
| 103 | 87 | dc.setAllowCreate(true); |
| 104 | 87 | dc.setType(DatabaseType.BTREE); |
| 105 | |
|
| 106 | 87 | if (new File(dir, name + ".db").exists()) |
| 107 | 87 | Database.upgrade(new File(dir, name + ".db").getPath(), dc); |
| 108 | |
|
| 109 | 87 | Database it = new Database(new File(dir, name + ".db").getPath(), |
| 110 | |
null, dc); |
| 111 | |
|
| 112 | 87 | return it; |
| 113 | |
} |
| 114 | |
|
| 115 | |
public static void main(final String[] args) throws Exception { |
| 116 | 0 | String dbHome = "/usr/local/share/bibliomania/workspace"; |
| 117 | 0 | openOrCreateBTreeDb(new File(dbHome), "idOfWord", 0); |
| 118 | 0 | openOrCreateBTreeDb(new File(dbHome), "occurrencesOfWordInText",0); |
| 119 | 0 | openOrCreateBTreeDb(new File(dbHome), "anchorOfIndex", 0); |
| 120 | 0 | openOrCreateBTreeDb(new File(dbHome), "blockmarkOfIndex", 0); |
| 121 | 0 | openOrCreateBTreeDb(new File(dbHome), "wordsInText", 0); |
| 122 | |
|
| 123 | 0 | } |
| 124 | |
} |