All Packages This Package Class Hierarchy Class Search Index
Class grendel.storage.intertwingle.SimpleDB
java.lang.Object | +----grendel.storage.intertwingle.BaseDB | +----grendel.storage.intertwingle.SimpleDB
Simplistic implementation of a RDF-ish database. This is basically a massive hashtable hack. Almost certainly we shouldn't ship this, but it ought to be good enough to play with for a while.
Here's how it works:
There are five files. One is the "store", and it stores the data. All data is accessed via a hash table, so each entry in the store contains one or two pointers to other entries with the same hash. ("two" is in the case where there are two hash tables into the same data.)
The other four files are head pointers into the hashtable, for various hashes.
Random strings are stored in the store. They get hashed using String.hashCode(), the hash value is masked off to HASHMASK, and the first pointer to the hashtable appear in the "strhead" file. Each entry consists of a pointer to the next string with the same hashcode, and then a refcount, and then the UTF version of the string itself.
Assertions are stored in the store. Each assertion is three strings: name, slot, and value. The strings are first put into the store, and then the assertion is recorded by writing down the location of the strings. Each assertion is hashed twice: once by combining the name and slot, and once by combining the value and slot. So, each assertion records the next value for both hash tables, followed by the three string locations. So, each assertion costs 20 bytes on disk, not counting the string storage. The "head[]" files are the first pointers for each hashtable.
Finally, a free list of unused chunks in the store are kept. The "freehead" file points to the first free entry of a given size; each free entry points to the next free entry of the same size.
public final class SimpleDB extends grendel.storage.intertwingle.BaseDB { // Fields 8 private static final int HASHMASK; private static final int MAXALLOC; private static final int MINALLOC; private RandomAccessFile freehead; private RandomAccessFile[] head; private RandomAccessFile store; private File storename; private RandomAccessFile strhead; // Constructors 1 public SimpleDB(File) throws IOException; // Methods 11 private int LookupString(String, boolean) throws IOException; private void addStringRef(int) throws IOException; private int alloc(int) throws IOException; public synchronized void assert(String, String, String) throws IOException; public synchronized void dump(PrintStream) throws IOException; private void dumpString(int) throws IOException; public synchronized Enumeration findAll(String, String, boolean) throws IOException; public synchronized String findFirst(String, String, boolean) throws IOException; private void free(int, int) throws IOException; private void removeStringRef(int) throws IOException; public synchronized void unassert(String, String, String) throws IOException; // Inner Classes 1 class AllEnumerator }
Fields
storename
private File storename
store
private RandomAccessFile store
head
private RandomAccessFile[] head
strhead
private RandomAccessFile strhead
freehead
private RandomAccessFile freehead
MINALLOC
private static final int MINALLOC
MAXALLOC
private static final int MAXALLOC
HASHMASK
private static final int HASHMASK
Constructors
SimpleDB
public SimpleDB(File f) throws IOException
Methods
alloc
private int alloc(int length) throws IOException
free
private void free(int ptr, int length) throws IOException
LookupString
private int LookupString(String str, boolean create) throws IOException
addStringRef
private void addStringRef(int loc) throws IOException
removeStringRef
private void removeStringRef(int loc) throws IOException
assert
public synchronized void assert(String name, String slot, String value) throws IOException
unassert
public synchronized void unassert(String name, String slot, String value) throws IOException
findFirst
public synchronized String findFirst(String name, String slot, boolean reverse) throws IOException
findAll
public synchronized Enumeration findAll(String name, String slot, boolean reverse) throws IOException
dumpString
private void dumpString(int loc) throws IOException
dump
public synchronized void dump(PrintStream out) throws IOException
All Packages This Package Class Hierarchy Class Search IndexFreshly brewed Java API Documentation automatically generated with polardoc Version 1.0.4