Skip to content

Repository files navigation

LevelDB in Java

This is a rewrite (port) of LevelDB in Java. This goal is to have a feature complete implementation that is within 10% of the performance of the C++ original and produces byte-for-byte exact copies of the C++ code.

Current status

Currently the code base is basically functional, but only trivially tested. In some places, this code is a literal conversion of the C++ code and in others it has been converted to a more natural Java style. The plan is to leave the code closer to the C++ original until the baseline performance has been established.

API Usage:

Recommended Package imports:

importorg.iq80.leveldb.*;
importstaticorg.iq80.leveldb.impl.Iq80DBFactory.*;
importjava.io.*;

Opening and closing the database.

Optionsoptions = newOptions();
options.createIfMissing(true);
DBdb = factory.open(newFile("example"), options);
try {
// Use the db in here....
} finally {
// Make sure you close the db to shutdown the // database and avoid resource leaks.db.close();
}

Putting, Getting, and Deleting key/values.

db.put(bytes("Tampa"), bytes("rocks"));
Stringvalue = asString(db.get(bytes("Tampa")));
db.delete(bytes("Tampa"), wo);

Performing Batch/Bulk/Atomic Updates.

WriteBatchbatch = db.createWriteBatch();
try {
batch.delete(bytes("Denver"));
batch.put(bytes("Tampa"), bytes("green"));
batch.put(bytes("London"), bytes("red"));
db.write(batch);
} finally {
// Make sure you close the batch to avoid resource leaks.batch.close();
}

Iterating key/values.

DBIteratoriterator = db.iterator();
try {
for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
Stringkey = asString(iterator.peekNext().getKey());
Stringvalue = asString(iterator.peekNext().getValue());
System.out.println(key+" = "+value);
}
} finally {
// Make sure you close the iterator to avoid resource leaks.iterator.close();
}

Working against a Snapshot view of the Database.

ReadOptionsro = newReadOptions();
ro.snapshot(db.getSnapshot());
try {
// All read operations will now use the same // consistent view of the data.
... = db.iterator(ro);
... = db.get(bytes("Tampa"), ro);
} finally {
// Make sure you close the snapshot to avoid resource leaks.ro.snapshot().close();
}

Using a custom Comparator.

DBComparatorcomparator = newDBComparator(){
publicintcompare(byte[] key1, byte[] key2) {
returnnewString(key1).compareTo(newString(key2));
}
publicStringname() {
return"simple";
}
publicbyte[] findShortestSeparator(byte[] start, byte[] limit) {
returnstart;
}
publicbyte[] findShortSuccessor(byte[] key) {
returnkey;
}
};
Optionsoptions = newOptions();
options.comparator(comparator);
DBdb = factory.open(newFile("example"), options);

Disabling Compression

Optionsoptions = newOptions();
options.compressionType(CompressionType.NONE);
DBdb = factory.open(newFile("example"), options);

Configuring the Cache

Optionsoptions = newOptions();
options.cacheSize(100 * 1048576); // 100MB cacheDBdb = factory.open(newFile("example"), options);

Getting approximate sizes.

long[] sizes = db.getApproximateSizes(newRange(bytes("a"), bytes("k")), newRange(bytes("k"), bytes("z")));
System.out.println("Size: "+sizes[0]+", "+sizes[1]);

Getting database status.

Stringstats = db.getProperty("leveldb.stats");
System.out.println(stats);

Getting informational log messages.

Loggerlogger = newLogger() {
publicvoidlog(Stringmessage) {
System.out.println(message);
}
};
Optionsoptions = newOptions();
options.logger(logger);
DBdb = factory.open(newFile("example"), options);

Destroying a database.

Optionsoptions = newOptions();
factory.destroy(newFile("example"), options);

Projects using this port of LevelDB

  • ActiveMQ Apollo: Defaults to using leveldbjni, but falls back to this port if the jni port is not available on your platform.

About

Port of LevelDB to Java

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages