Skip to content

Based on Dain's LevelDB port to Java with the support of Zlib compression.

License

Notifications You must be signed in to change notification settings

NukkitR/nukkit-leveldb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nukkit-LevelDB

Based on Dain's LevelDB port to Java with the support of Zlib compression and *.ldb format table.

API Usage:

Recommended Package imports:

import org.iq80.leveldb.*;
import org.nukkit.leveldb.*;
import static org.nukkit.leveldb.BedrockDBFactory.*;
import java.io.*;

Opening and closing the database.

// Use ExtendedOptions instead of Options
ExtendedOptions options = new ExtendedOptions();
options.createIfMissing(true);
// Add support for ZLIB and ZLIB_RAW
options.extendedCompressionType(ExtendedCompressionType.ZLIB_RAW);
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;
    }
};

ExtendedOptions options = new ExtendedOptions();
options.comparator(comparator);
DB db = factory.open(new File("example"), options);

Disabling Compression

ExtendedOptions options = new ExtendedOptions();
options.extendedCompressionType(ExtendedCompressionType.NONE);
DB db = factory.open(new File("example"),options);

Configuring the Cache

ExtendedOptions options = new ExtendedOptions();
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);
    }
};
ExtendedOptions options = new ExtendedOptions();
options.logger(logger);
DB db = factory.open(new File("example"), options);

Destroying a database.

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

About

Based on Dain's LevelDB port to Java with the support of Zlib compression.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages