Skip to content

edwardstock/leveldb-multiplatform

Repository files navigation

[Android Release]
[Kotlin Release]

LevelDB for Android and Kotlin

This Repository based on https://github.com/hf/leveldb-android.

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

Usage

Add this to your build.gradle:

repositories {
    mavenCentral()
}

And then this as a dependency:\n For Android\n

dependencies {
    implementation 'com.edwardstock:leveldb-android:1.0.0'
}

For Kotlin\n

dependencies {
    implementation 'com.edwardstock:leveldb-kt:1.0.0'
}

Example

Opening, Closing, Putting, Deleting

val levelDb = LevelDB.open("path/to/leveldb", LevelDB.configure().createIfMissing(true))
// or u can just

levelDb.put("leveldb".getBytes(), "Is awesome!")
val result: String? = levelDb.getString("leveldb")
val resultBytes: ByteArray? = levelDb.get("leveldb")

levelDb.put("magic", byteArrayOf(0, 1, 2, 3, 4))
val magic: ByteArray? = levelDB.get("magic")

// !IMPORTANT! you must close it
levelDb.close()

For Android almost the same, but with other class

import com.edwardstock.leveldb.*

// context can be used for place db file: context.filesDir.toString() + File.separator + (dbName ?: LevelDB.DEFAULT_DBNAME)
val levelDb = AndroidLevelDB.open(context, LevelDB.configure().createIfMissing(true))

// or also you can use default instance
val levelDb = LevelDB.open(context, LevelDB.configure().createIfMissing(true))

The same, but using try-with-resource

class Main {
    public static myHandler() {

        try (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());
        }
    }
}
val levelDb = LevelDB.open("path/to/leveldb") {
    createIfMissing(true)
}

levelDb.use {
    levelDb.put("leveldb", "Is awesome!")
    val result: String? = levelDb.getString("leveldb")
    val resultBytes: ByteArray? = levelDb["leveldb"]

    levelDb.put("magic", byteArrayOf(0, 1, 2, 3, 4))
    val magic: ByteArray? = levelDB["magic"]
}

Open using android context

val context: context

// it writes db file to: `context.filesDir.toString() + File.separator + DEFAULT_DBNAME`
val levelDb = LevelDB.open(context) {
    createIfMissing(true)
}

levelDb.use {
    levelDb.put("leveldb", "Is awesome!")
    val result: String? = levelDb.getString("leveldb")
    val resultBytes: ByteArray? = levelDb["leveldb"]

    levelDb.put("magic", byteArrayOf(0, 1, 2, 3, 4))
    val magic: ByteArray? = levelDB["magic"]
}

WriteBatch (a.k.a. Transactions)

class My {
    public static void myFun() {
        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

Until Google (or someone else) fixes the Android Gradle build tools to properly support NDK, this is the way to build this project.

  1. Install the NDK
  2. Build with Gradle (leveldb::assembleRelease)

Or u can build to local maven repository:

cd /path/to/project
sh publish_local.sh

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.

About

LevelDB for kotlin and android

Resources

License

Stars

Watchers

Forks

Packages

No packages published