phelps.io

Class FileHash

public class FileHash extends Object

Under development Disk-based hash, like dbm/ndbm/gdbm, which is useful for simple key-value databases that do not require SQL queries or crash recovery. Keys are arbitrary strings. Data is an arbitrary byte array, of length up to (2^31)-1 bytes (2GB), which is the largest byte array Java will allocate. Disk hashes are the same across machine architectures.

Performance is biased toward an access pattern of many reads, some writes, few deletions.

To do: data space recycling, database dump, separate constructors for create and open new, automatic compression (java.util.zip.Deflater) and encryption (RC4) with flags to constructor.

Version: $Revision: 1.11 $ $Date: 2003/06/01 08:09:25 $

See Also: jdbm

UNKNOWN: Sleepycat Software for a commercial, supported version of gdbm, with Java bindings and ACID properties

Field Summary
static longNO_RECORD
Constructor Summary
FileHash(String filename, int keyblocksize)
Open disk hash on passed file.
FileHash(String filename)
Method Summary
voidclose()
Hash must be closed.
booleanexists(String key)
voidflush()
Write pending key and data changes to disk.
byte[]get(String key)
Return data at given key, or null if no such key.
byte[]get(String key, byte[] b, int offset, int length)
Iterator<String>iterator()
Returns Iterator over keys, in sorted order.
Iterator<String>iterator(String key1, String key2)
Returns Iterator over keys range from key1 to key2, inclusive.
voidoptimize()
Optimize for space by squeezing out holes in the data stream which were created by overriding data with shorter data.
voidoptimizeAtomic(KeyStore newkey, DataStore keydata)
voidput(String key, byte[] data, int offset, int length)
Save data at given key.
voidput(String key, byte[] data)
voidremove(String key)
Remove/delete key and associated data.

Field Detail

NO_RECORD

public static final long NO_RECORD

Constructor Detail

FileHash

public FileHash(String filename, int keyblocksize)
Open disk hash on passed file. If no such file exists, create a new disk hash.

Parameters: keyblocksize indirectly determines the number of keys per node, and thus the depth of tree and number of disk accesses it takes to locate the key's value.

Throws: IOException

FileHash

public FileHash(String filename)

Method Detail

close

public void close()
Hash must be closed. Hash instance cannot be used again after close.

exists

public boolean exists(String key)

flush

public void flush()
Write pending key and data changes to disk.

get

public byte[] get(String key)
Return data at given key, or null if no such key.

get

public byte[] get(String key, byte[] b, int offset, int length)

iterator

public Iterator<String> iterator()
Returns Iterator over keys, in sorted order.

iterator

public Iterator<String> iterator(String key1, String key2)
Returns Iterator over keys range from key1 to key2, inclusive. If key1 is null, the first key in the store is used; if key2 is null, the last key in the store is used.

optimize

public void optimize()
Optimize for space by squeezing out holes in the data stream which were created by overriding data with shorter data. Since this generally copies all the data, it can be slow.

optimizeAtomic

public void optimizeAtomic(KeyStore newkey, DataStore keydata)

put

public void put(String key, byte[] data, int offset, int length)
Save data at given key. Any existing under this key is lost. If new data replaces old, then it if the new data is shorter or the same size as the old it overwrites the old, and if the new is large than the old the new is appended. Zero-length data is acceptable, but negative length is not.

put

public void put(String key, byte[] data)

remove

public void remove(String key)
Remove/delete key and associated data.