Home > JLevelDB

JLevelDB

JLevelDB is a project mainly written in JAVA and C++, it's free.

Java access to LevelDB Google Project Database

JavaLevelDB is java bridge to C++ library leveldb.

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. That project provides tiny access to library making transparent access to C++ classes through Java facade.

How to build

Checkout the project and run in command line : 'make' - checkout leveldb project from svn, compile it and build ready to use jar.

Build is only tested under Linux. Possible problems :

  • No internet connection to checkout leveldb from googlecode
  • leveldb can not be built under certain environment (under Cygwin in Windows)
  • JAVA_HOME variable is not defined
  • swig is not installed

How to use

// Loads library to corresponding OS if it is not loaded exception is thrown
// Before library is loaded you can not create any object!
// Library can be loaded in class initializator :  `static { boolean loaded = LevelDBAccess.load(); }`
DBAccessor dbAccessor = LevelDBAccess.getDBAcessor();

Options options = new Options();
options.setCreateIfMissing(true);
Status status = dbAccessor.open(options, "filepath");

if (status.ok()) {
    WriteOptions opts = new WriteOptions();
    ReadOptions ro = new ReadOptions();
    dbAccessor.put(opts, "key", "value");
    assert "value".equals(dbAccessor.get(ro, "key"));
}

What is to be verified and implemented

There are some operations not linked with C++ API :

  • Comparator - important enough. But require callback from C to Java which is difficult to do in SWIG.
  • TableBuilder and Table - should be properly tested. There is no default API to read Table from middle of file but it can be implemented.

Is it really needed ?

  • Cache - own implementation of cache
  • Environment - own implementation of RandomAccessFile, SequentialFile,...
  • Cleanup Function of Iterator
  • WriteBatch Iterator - iterate over entities to be writen

Contribution

Please feel free to make forks and provide your pull requests :)

Additional

That project provides very fast ArraySerializer that allows to store array of strings into string. The stream parser processes String in place and not allocate new memory. With that helper you can serialize/deserialize every data structure like as array of arrays or map (as doubled array). The format of serialization is pretty simple : [Value, Value2, [ Value3 ]]. It also supports quotation of important for deserialization characters.

int next;
EntityValueTokenizer tokenizer = new EntityValueTokenizer();
tokenizer.tokenize(value);

while ((next = tokenizer.next()) != END) {
    if (next == ELEMENT) {
        // TODO process element 
        String value = tokenizer.value();
    } else if (next == START_ARRAY) {
        // TODO process start of inner array
    } else if (next == END_ARRAY) {
        // TODO process end of array
    }
}
Previous:ImageViewer