Skip to content

jsherman1/leveldbjni

This branch is 97 commits behind fusesource/leveldbjni:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

9ca765e · Feb 16, 2012

History

57 Commits
Feb 16, 2012
Feb 16, 2012
Feb 16, 2012
Feb 16, 2012
Feb 16, 2012
Jul 31, 2011
Aug 8, 2011
Feb 15, 2012
Nov 22, 2011
Feb 16, 2012
Feb 16, 2012
Nov 30, 2011

Repository files navigation

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..

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-all</artifactId>
    <version>1.1</version>
  </dependency>
</dependencies>

API Usage:

Recommended Package imports:

import org.iq80.leveldb.*;
import static org.fusesource.leveldbjni.JniDBFactory.*;
import java.io.*;

Opening and closing the database.

Options options = new Options();
options.createIfMissing(true);
DB db = factory.open(new File("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"));
String value = asString(db.get(bytes("Tampa")));
db.delete(wo, bytes("Tampa"));

Performing Batch/Bulk/Atomic Updates.

WriteBatch batch = 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.

DBIterator iterator = db.iterator();
try {
  for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
    String key = asString(iterator.peakNext().getKey());
    String value = asString(iterator.peakNext().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.

ReadOptions ro = new ReadOptions();
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.

DBComparator comparator = new DBComparator(){
    public int compare(byte[] key1, byte[] key2) {
        return new String(key1).compareTo(new String(key2));
    }
    public String name() {
        return "simple";
    }
    public byte[] findShortestSeparator(byte[] start, byte[] limit) {
        return start;
    }
    public byte[] findShortSuccessor(byte[] key) {
        return key;
    }
};
Options options = new Options();
options.comparator(comparator);
DB db = factory.open(new File("example"), options);

Disabling Compression

Options options = new Options();
options.compressionType(CompressionType.NONE);
DB db = factory.open(new File("example"), options);

Configuring the Cache

Options options = new Options();
options.cacheSize(100 * 1048576); // 100MB cache
DB db = factory.open(new File("example"), options);

Getting approximate sizes.

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

Getting database status.

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

Getting informational log messages.

Logger logger = new Logger() {
  public void log(String message) {
    System.out.println(message);
  }
};
Options options = new Options();
options.logger(logger);
DB db = factory.open(new File("example"), options);

Destroying a database.

Options options = new Options();
factory.destroy(new File("example"), options);

Repairing a database.

Options options = new Options();
factory.repair(new File("example"), options);

Building

Prerequisites

Supported Platforms

The following worked for me on:

  • OS X Lion with X Code 4
  • CentOS 5.6 (32 and 64 bit)
  • Ubuntu 10.04 (32 and 64 bit)

Build Procedure

Then download the snappy, leveldb, and leveldbjni project source code:

wget http://snappy.googlecode.com/files/snappy-1.0.3.tar.gz
tar -zxvf snappy-1.0.3.tar.gz
git clone https://code.google.com/p/leveldb
cd leveldb; git checkout 239ac9d2dea0ac1708b7d903a3d80d3883e0781b ; cd -
git clone git://github.com/fusesource/leveldbjni.git

Compile the snappy project. This produces a static library.

cd snappy-1.0.3 
./configure --disable-shared --with-pic
make

Patch and Compile the leveldb project. This produces a static library.

cd ../leveldb
export LIBRARY_PATH=`cd ../snappy-1.0.3; pwd`
export C_INCLUDE_PATH=${LIBRARY_PATH}
export CPLUS_INCLUDE_PATH=${LIBRARY_PATH}
git apply ../leveldbjni/leveldb.patch
make

Now use maven to build the leveldbjni project.

cd ../leveldbjni
mvn clean install -Dleveldb=`cd ../leveldb; pwd` -Dsnappy=`cd ../snappy-1.0.3; pwd` -P download -P ${platform}

Replace ${platform} with one of the following platform identifiers (depending on the platform your building on):

  • linux32
  • linux64
  • mac

Build Results

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

About

A Java Native Interface to LevelDB

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 95.2%
  • C 4.8%