A Java Native Interface to LevelDB
Switch branches/tags
Nothing to show
Pull request Compare This branch is 147 commits behind fusesource:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
license.txt
pom.xml
readme.md

readme.md

LevelDB JNI

Description

LevelDB JNI gives you a Java interface to the LevelDB C++ library which is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values..

Building Prerequisites

Building

The following worked for me on a OS X Lion system with X Code 4 Installed.

First checkout the leveldb and leveldbjni project source code:

svn checkout http://leveldb.googlecode.com/svn/trunk/ leveldb
git clone git://github.com/fusesource/leveldbjni.git

Compile the leveldb project. This produces a static library.

cd leveldb
make

Now use maven to build the leveldbjni project.

cd ../leveldbjni
mvn install -P download -Dleveldb=`cd ../leveldb; pwd`

The above will produce:

  • target/leveldbjni-${version}.jar : The java class file to the library.
  • target/leveldbjni-${version}-native-src.zip : A GNU style source project which you can use to build the native library on other systems.
  • target/leveldbjni-${version}-${platform}.jar : A jar file containing the built native library using your currently platform.

Using as a Maven Dependency

You just nee to add the following repositories and dependencies to your Maven pom.

<repositories>
  <repository>
    <id>fusesource.nexus.snapshot</id>
    <name>FuseSource Community Snapshot Repository</name>
    <url>http://repo.fusesource.com/nexus/content/groups/public-snapshots</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>org.fusesource.leveldbjni</groupId>
    <artifactId>leveldbjni</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency>
    <groupId>org.fusesource.leveldbjni</groupId>
    <artifactId>leveldbjni</artifactId>
    <version>1.0-SNAPSHOT</version>
    <classifier>osx</classifier>
  </dependency>
</dependencies>

API Usage:

Recommended Package imports:

import org.fusesource.leveldbjni.*;
import static org.fusesource.leveldbjni.DB.*;
import java.io.*;

Opening and closing the database.

Options options = new Options();
options.setCreateIfMissing(true);
DB db = DB.open(options, new File("example"));
try {
  // Use the db in here....
} finally {
  // Make sure you delete the db and options to avoid resource leaks.
  db.delete();
  options.delete();
}

Putting, Getting, and Deleting key/values.

WriteOptions wo = new WriteOptions();
ReadOptions ro = new ReadOptions();

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

Performing Batch/Bulk/Atomic Updates.

WriteBatch batch = new WriteBatch();
try {
  batch.delete(bytes("Denver"));
  batch.put(bytes("Tampa"), bytes("green"));
  batch.put(bytes("London"), bytes("red"));

  WriteOptions wo = new WriteOptions();
  db.write(wo, batch);
} finally {
  // Make sure you delete the batch to avoid resource leaks.
  batch.delete();
}

Iterating key/values.

ReadOptions ro = new ReadOptions();
Iterator iterator = db.iterator(ro);
try {
  for(iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
    String key = asString(iterator.key());
    String value = asString(iterator.value());
    System.out.println(key+" = "+value);
  }
} finally {
  // Make sure you delete the iterator to avoid resource leaks.
  iterator.delete();
}

Working against a Snapshot view of the Database.

ReadOptions ro = new ReadOptions();
ro.setSnapshot(db.getSnapshot());
try {
  
  // All read operations will now use the same 
  // consistent view of the data.
  ... = db.iterator(ro);
  ... = db.get(ro, bytes("Tampa"));

} finally {
  // Make sure you release the snapshot to avoid resource leaks.
  db.releaseSnapshot(ro.getSnapshot());
  ro.setSnapshot(null);
}