Git Product home page Git Product logo

leveldb-android's Introduction

Build Status codecov Download

LevelDB for Android

LOOKING FOR A MAINTAINER. PLEASE WRITE HERE IF INTERESTED.

This is a Java wrapper for the amazing LevelDB by Google.

Currently it does not use Snappy for data compression. (There is really no need for this in Android, i.e. it's unnecessary overhead.)

LevelDB's native log output is tagged: com.github.hf.leveldb:N

Usage

Add this to your build.gradle:

repositories {
  maven {
    url "https://dl.bintray.com/stojan/android"
  }
}

And then this as a dependency:

dependencies {
  compile 'com.github.hf:leveldb:1.19.0@aar'
}

ProGuard rules:

-keep class com.github.hf.leveldb.** { *; }

If you really want to obfuscate LevelDB, then make sure that the exceptions are not obfuscated since those are used within JNI code and will not be resolved properly at runtime.

Example

Opening, Closing, Putting, Deleting

LevelDB levelDB = LevelDB.open("path/to/leveldb", LevelDB.configure().createIfMissing(true));

levelDB.put("leveldb".getBytes(), "Is awesome!".getBytes());
String value = levelDB.get("leveldb".getBytes());

leveldb.put("magic".getBytes(), new byte[] { 0, 1, 2, 3, 4 });
byte[] magic = levelDB.getBytes("magic".getBytes());

levelDB.close(); // closing is a must!

WriteBatch (a.k.a. Transactions)

LevelDB levelDB = LevelDB.open("path/to/leveldb"); // createIfMissing == true

levelDB.put("sql".getBytes(), "is lovely!".getBytes());

levelDB.writeBatch()
  .put("leveldb".getBytes(), "Is awesome!".getBytes())
  .put("magic".getBytes(), new byte[] { 0, 1, 2, 3, 4 })
  .del("sql".getBytes())
  .write(); // commit transaction

levelDB.close(); // closing is a must!

Iteration Over Key-Value Pairs

LevelDB is a key-value store, but it has some nice iteration features.

Every key-value pair inside LevelDB is ordered. Until the comparator wrapper API is finished you can iterate over your LevelDB in the key's lexicographical order.

LevelDB levelDB = LevelDB.open("path/to/leveldb");

Iterator iterator = levelDB.iterator();

for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
  byte[] key   = iterator.key();
  byte[] value = iterator.value();
}

iterator.close(); // closing is a must!

Reverse Iteration

It is somewhat slower than forward iteration.

LevelDB levelDB = LevelDB.open("path/to/leveldb");

Iterator iterator = levelDB.iterator();

for (iterator.seekToLast(); iterator.isValid(); iterator.previous()) {
  String key   = iterator.key();
  String value = iterator.value();
}

iterator.close(); // closing is a must!

Iterate from a Starting Position

LevelDB levelDB = LevelDB.open("path/to/leveldb");

Iterator iterator = levelDB.iterator();

for (iterator.seek("leveldb".getBytes()); iterator.isValid(); iterator.next()) {
  String key   = iterator.key();
  String value = iterator.value();
}

iterator.close(); // closing is a must!

This will start from the key leveldb if it exists, or from the one that follows (eg. sql, i.e. l < s).

Snapshots

Snapshots give you a consistent view of the data in the database at a given time.

Here's a simple example demonstrating their use:

LevelDB levelDB = LevelDB.open("path/to/leveldb");

levelDB.put("hello".getBytes(), "world".getBytes());

Snapshot helloWorld = levelDB.obtainSnapshot();

levelDB.put("hello".getBytes(), "brave-new-world".getBytes());

levelDB.get("hello".getBytes(), helloWorld); // == "world"

levelDB.get("hello".getBytes()); // == "brave-new-world"

levelDB.releaseSnapshot(helloWorld); // release the snapshot

levelDB.close(); // snapshots will automatically be released after this

Mock LevelDB

The implementation also supplies a mock LevelDB implementation that is an in-memory equivalent of the native LevelDB. It is meant to be used in testing environments, especially non-Android ones like Robolectric.

There are a few of differences from the native implementation:

  • it is not configurable
  • it does not support properties (as in LevelDB#getProperty())
  • it does not support paths, i.e. always returns :MOCK:

Use it like so:

LevelDB.mock();

Building

Project can be build with ndk-bundle and cmake installed from Android Studio SDK Manager.

License

This wrapper library is licensed under the BSD 3-Clause License, same as the code from Google.

See LICENSE.txt for the full Copyright.

leveldb-android's People

Contributors

hf avatar giacomoran avatar whoeverest avatar proxain avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.