Skip to content

krisskross/lmdbjni

 
 

Repository files navigation

LMDB JNI

Build Status Coverage Status Coverity Scan Build Status

LMDB JNI provide a Java API to LMDB which is an ultra-fast, ultra-compact key-value embedded data store developed by Symas for the OpenLDAP Project. It uses memory-mapped files, so it has the read performance of a pure in-memory database while still offering the persistence of standard disk-based databases. Transactional with full ACID semantics and crash-proof by design. No corruption. No startup time. Zero-config cache tuning. No dependencies.

LMDB JNI is available for 64 bit Linux, OSX, Windows and Android.

Documentation

Google groups

Presentations

Benchmarks

  • [In-Memory Microbenchmark] (http://symas.com/mdb/inmem), June 2014

    Multithreaded read performance for a purely in-memory database.

  • In-Memory Microbenchmark (Scaling/NUMA), September 2014

    Same as above showing performance improvements with numactl --interleave=all enabled.

  • On-Disk Microbenchmark, November 2014

    Multithreaded read performance for a database that is over 5 times larger than the size of RAM.

  • [RxLMDB benchmarks] (https://github.com/deephacks/RxLMDB), July 2015

    Benchmarks using RxJava and LMDB comparing zero copy, various serialization mechanisms, parallel and skip scans.

  • LMDB JNI Microbenchmark, February 2015 (source)

    Row scanning speed per second compared with the Java ports of RocksDB, LevelDB and MapDB. Mongodb is difficult to setup in JMH but de.flapdoodle.embed.mongo indicate that it is around 50x slower than lmdb_zero_copy.

    Benchmark                    Mode  Cnt         Score         Error  Units
    Iteration.leveldb           thrpt   10   6965637.351 ±  784589.894  ops/s
    Iteration.lmdb_buffer_copy  thrpt   10   3157796.643 ±  265830.424  ops/s
    Iteration.lmdb_zero_copy    thrpt   10  16372428.882 ± 1812316.504  ops/s
    Iteration.mapdb             thrpt   10   1358748.670 ±   87502.413  ops/s
    Iteration.rocksdb           thrpt   10   1311441.804 ±  176129.883  ops/s

Maven

<!-- required java classes -->

<dependency>
  <groupId>org.deephacks.lmdbjni</groupId>
  <artifactId>lmdbjni</artifactId>
  <version>${lmdbjni.version}</version>
</dependency>

<!-- prebuilt liblmdb platform packages -->

<dependency>
  <groupId>org.deephacks.lmdbjni</groupId>
  <artifactId>lmdbjni-linux64</artifactId>
  <version>${lmdbjni.version}</version>
</dependency>

<dependency>
  <groupId>org.deephacks.lmdbjni</groupId>
  <artifactId>lmdbjni-osx64</artifactId>
  <version>${lmdbjni.version}</version>
</dependency>

<dependency>
  <groupId>org.deephacks.lmdbjni</groupId>
  <artifactId>lmdbjni-win64</artifactId>
  <version>${lmdbjni.version}</version>
</dependency>

<dependency>
  <groupId>org.deephacks.lmdbjni</groupId>
  <artifactId>lmdbjni-android</artifactId>
  <version>${lmdbjni.version}</version>
</dependency>

Build from source

See building from source on wiki.

Usage

Recommended package imports.

 import org.fusesource.lmdbjni.*;
 import static org.fusesource.lmdbjni.Constants.*;

Opening and closing the database.

 try (Env env = new Env("/tmp/mydb")) {
   try (Database db = env.openDatabase()) {
     ... // use the db
   }
 }

Putting, getting, and deleting key/values.

 db.put(bytes("Tampa"), bytes("rocks"));
 String value = string(db.get(bytes("Tampa")));
 db.delete(bytes("Tampa"));

Iterating and seeking key/values forward and backward.

Transaction tx = env.createReadTransaction();
try (EntryIterator it = db.iterate(tx)) {
  for (Entry next : it.iterable()) {
  }
}

try (EntryIterator it = db.iterateBackward(tx)) {
  for (Entry next : it.iterable()) {
  }
}

byte[] key = bytes("London");
try (EntryIterator it = db.seek(tx, key)) {
  for (Entry next : it.iterable()) {
  }
}

try (EntryIterator it = db.seekBackward(tx, key))) {
  for (Entry next : it.iterable()) {
  }
}
tx.abort();

Performing transactional updates.

 try (Transaction tx = env.createWriteTransaction()) {
   db.delete(tx, bytes("Denver"));
   db.put(tx, bytes("Tampa"), bytes("green"));
   db.put(tx, bytes("London"), bytes("red"));
   tx.commit();  // if commit is not called, the transaction is aborted
 }

Working against a snapshot view of the database using cursors.

 // create a read-only transaction...
 try (Transaction tx = env.createReadTransaction()) {
   
   // All read operations will now use the same 
   // consistent view of the data.
   ... = db.openCursor(tx);
   ... = db.get(tx, bytes("Tampa"));
 }

A cursor in a write-transaction can be closed before its transaction ends, and will otherwise be closed when its transaction ends. A cursor must not be used after its transaction is closed. Both these try blocks are unsafe and may SIGSEGV.

 try (Transaction tx = env.createWriteTransaction();
      Cursor cursor = db.openCursor(tx)) {
   ...
   tx.commit();
 }

 try (Transaction tx = env.createWriteTransaction();
      EntryIterator it = db.iterate(tx)) {
   ...
   tx.commit();
 }

A cursor in a read-only transaction must be closed explicitly, before or after its transaction ends. Both these try blocks are safe.

 try (Transaction tx = env.createReadTransaction();
      Cursor cursor = db.openCursor(tx)) {
 }

 try (Transaction tx = env.createReadTransaction();
      EntryIterator it = db.iterate(tx)) {
 }

Set a custom key comparison function for a database.

 db.setComparator(tx, new Comparator<byte[]>() {
      @Override
      public int compare(byte[] key1, byte[] key2) {
        // do compare
      }
    });

Atomic hot backup.

 env.copy(backupPath);

Using a memory pool to make native memory allocations more efficient:

 Env.pushMemoryPool(1024 * 512);
 try {
     // .. work with the DB in here, 
 } finally {
     Env.popMemoryPool();
 }

Zero copy usage

The safest (and least efficient) approach for interacting with LMDB JNI is using buffer copy as shown above. BufferCursor is an advanced, more efficient, zero copy mode. This mode is not available on Android.

There are also methods that give access to DirectBuffer which are even more advanced but users should avoid interacting directly with these and use the BufferCursor API instead. Otherwise take extra care of buffer memory address+size and byte ordering. Mistakes may lead to SIGSEGV or unpredictable key ordering etc.

 // read only
 try (Transaction tx = env.createReadTransaction(); 
      BufferCursor cursor = db.bufferCursor(tx)) {
   // iterate from first item and forwards
   cursor.first();
   while(cursor.next()) {
     // read a position in buffer
     cursor.keyByte(0);
     cursor.valByte(0);
   }

   // iterate from last item and backwards
   cursor.last();
   while(cursor.prev()) {
     // copy entire buffer
     cursor.keyBytes();
     cursor.valBytes();
   }

   // find first key greater than or equal to specified key.
   cursor.seek(bytes("London"));
   // read utf-8 string from position until NULL byte
   cursor.keyUtf8(0);
   cursor.valUtf8(0);
 }
 
 // open for write
 try (Transaction tx = env.createWriteTransaction()) {
   // cursors must close before write transactions!
   try (BufferCursor cursor = db.bufferCursor(tx)) {
     cursor.first();
     // write utf-8 ending with NULL byte
     cursor.keyWriteUtf8("England");
     cursor.valWriteUtf8("London");
     // overwrite existing item if any. Data is not written
     // into database before this operation is called and
     // no updates are visible outside this transaction until
     // the transaction is committed
     cursor.overwrite();
     cursor.first();
     // delete current cursor position
     cursor.delete();
   }
   // commit changes or try-with-resources will auto-abort
   tx.commit();
 } 

License

This project is licensed under the Apache License, Version 2.0 but the binary jar it produces also includes liblmdb library version 0.9.14 of the OpenLDAP project which is licensed under the The OpenLDAP Public License.

Packages

No packages published

Languages

  • C 43.3%
  • Shell 33.4%
  • Java 23.3%