leveldb

Port of LevelDB to Java

License

License

GroupId

GroupId

org.iq80.leveldb
ArtifactId

ArtifactId

leveldb
Last Version

Last Version

0.12
Release Date

Release Date

Type

Type

jar
Description

Description

leveldb
Port of LevelDB to Java
Source Code Management

Source Code Management

http://github.com/dain/leveldb/tree/master/leveldb

Download leveldb

How to add to project

<!-- https://jarcasting.com/artifacts/org.iq80.leveldb/leveldb/ -->
<dependency>
    <groupId>org.iq80.leveldb</groupId>
    <artifactId>leveldb</artifactId>
    <version>0.12</version>
</dependency>
// https://jarcasting.com/artifacts/org.iq80.leveldb/leveldb/
implementation 'org.iq80.leveldb:leveldb:0.12'
// https://jarcasting.com/artifacts/org.iq80.leveldb/leveldb/
implementation ("org.iq80.leveldb:leveldb:0.12")
'org.iq80.leveldb:leveldb:jar:0.12'
<dependency org="org.iq80.leveldb" name="leveldb" rev="0.12">
  <artifact name="leveldb" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.iq80.leveldb', module='leveldb', version='0.12')
)
libraryDependencies += "org.iq80.leveldb" % "leveldb" % "0.12"
[org.iq80.leveldb/leveldb "0.12"]

Dependencies

compile (4)

Group / Artifact Type Version
org.iq80.leveldb : leveldb-api jar 0.12
org.xerial.snappy : snappy-java Optional jar 1.1.2.6
org.iq80.snappy : snappy Optional jar 0.4
com.google.guava : guava jar 21.0

test (3)

Group / Artifact Type Version
joda-time : joda-time jar 2.9.9
org.testng : testng jar 6.9.6
org.fusesource.leveldbjni : leveldbjni jar 1.1

Project Modules

There are no modules declared in this project.

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:

import org.iq80.leveldb.*;
import static org.iq80.leveldb.impl.Iq80DBFactory.*;
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(bytes("Tampa"), wo);

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.peekNext().getKey());
    String value = 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.

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);

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.

Versions

Version
0.12
0.11
0.10
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1