MCPcopy Index your code
hub / github.com/dain/leveldb

github.com/dain/leveldb @0.12

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.12 ↗ · + Follow
1,090 symbols 3,342 edges 105 files 118 documented · 11% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.

Extension points exported contracts — how you extend this code

DBComparator (Interface)
@author Hiram Chirino [5 implementers]
leveldb-api/src/main/java/org/iq80/leveldb/DBComparator.java
InternalIterator (Interface)
A common interface for internal iterators. @author Hiram Chirino [5 implementers]
leveldb/src/main/java/org/iq80/leveldb/util/InternalIterator.java
WriteBatch (Interface)
@author Hiram Chirino [5 implementers]
leveldb-api/src/main/java/org/iq80/leveldb/WriteBatch.java
UserComparator (Interface)
(no doc) [7 implementers]
leveldb/src/main/java/org/iq80/leveldb/table/UserComparator.java
DBFactory (Interface)
@author Hiram Chirino [2 implementers]
leveldb-api/src/main/java/org/iq80/leveldb/DBFactory.java
SeekingIterable (Interface)
(no doc) [9 implementers]
leveldb/src/main/java/org/iq80/leveldb/impl/SeekingIterable.java
DBIterator (Interface)
@author Hiram Chirino [2 implementers]
leveldb-api/src/main/java/org/iq80/leveldb/DBIterator.java
Handler (Interface)
(no doc) [5 implementers]
leveldb/src/main/java/org/iq80/leveldb/impl/WriteBatchImpl.java

Core symbols most depended-on inside this repo

length
called by 96
leveldb/src/main/java/org/iq80/leveldb/util/Slice.java
get
called by 92
leveldb-api/src/main/java/org/iq80/leveldb/DB.java
getBytes
called by 38
leveldb/src/main/java/org/iq80/leveldb/util/Slice.java
getKey
called by 37
leveldb/src/main/java/org/iq80/leveldb/table/BlockEntry.java
add
called by 36
leveldb/src/main/java/org/iq80/leveldb/impl/MemTable.java
size
called by 35
leveldb/src/main/java/org/iq80/leveldb/impl/WriteBatchImpl.java
getUserKey
called by 33
leveldb/src/main/java/org/iq80/leveldb/impl/LookupKey.java
close
called by 31
leveldb/src/main/java/org/iq80/leveldb/impl/LogWriter.java

Shape

Method 951
Class 115
Interface 16
Enum 8

Languages

Java100%

Modules by API surface

leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java83 symbols
leveldb/src/main/java/org/iq80/leveldb/impl/DbImpl.java55 symbols
leveldb/src/main/java/org/iq80/leveldb/impl/VersionSet.java47 symbols
leveldb-benchmark/src/main/java/org/iq80/leveldb/benchmark/DbBenchmark.java37 symbols
leveldb/src/main/java/org/iq80/leveldb/util/SliceInput.java28 symbols
leveldb/src/main/java/org/iq80/leveldb/util/Slice.java26 symbols
leveldb/src/main/java/org/iq80/leveldb/impl/Version.java26 symbols
leveldb/src/main/java/org/iq80/leveldb/impl/Filename.java25 symbols
leveldb/src/main/java/org/iq80/leveldb/impl/SeekingIteratorAdapter.java24 symbols
leveldb/src/main/java/org/iq80/leveldb/impl/VersionEdit.java22 symbols
leveldb/src/main/java/org/iq80/leveldb/util/SliceOutput.java21 symbols
leveldb/src/main/java/org/iq80/leveldb/util/Snappy.java18 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add leveldb \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact