LevelDB client for Kotlin and/or Java 8+

Related tags

Utility levelkt
Overview

levelkt

Build Status Maven Central ktlint

LevelDB client for Kotlin and/or Java 8+.

Initially project aimed to provide an alternative API for fusesource/leveldbjni's JNI layer fixing some of the issues around thread-safety, inefficient backward traversal, absent key-only iteration, Java 8 support. Later on, leveldbjni was replaced with protonail/leveldb-jna.

Compared to pure leveldb-jna, levelkt

  • hides the complexity associated with managing LevelDB Read/Write options
    (while optimizing number of JNA calls),
  • provides convenient API for range/prefix queries, existence checks and
  • makes it easier not to leak resources (you only need to close LevelDB, LevelDBSnapshot and LevelDBCursor instances (it will also warn you if you don't)).

Usage

<dependency>
    <groupId>com.github.shyiko.levelkt</groupId>
    <artifactId>levelkt</artifactId>
    <version>0.1.0</version>
    <!-- omit classifier below if you plan to use this library in koltin -->
    <classifier>kalvanized</classifier>
</dependency>

(example) Kotlin

import com.github.shyiko.levelkt.jna.JNALevelDB

JNALevelDB("/tmp/0.leveldb").use { levelDB ->

    levelDB.put("key1".toByteArray(), "value1".toByteArray())
    levelDB.put("key2.1".toByteArray(), "value2.1".toByteArray())
    levelDB.put("key2.2".toByteArray(), "value2.2".toByteArray())
    levelDB.put("key3".toByteArray(), "value3".toByteArray())

    assert(levelDB.get("key1".toByteArray())?.let { String(it) } == "value1")

    assert(levelDB.keySlice("key".toByteArray(), offset = 1, limit = 2).map { String(it) } == 
        listOf("key2.1", "key2.2"))
    assert(levelDB.keySlice("key2".toByteArray(), "key3".toByteArray()).map { String(it) } == 
        listOf("key2.1", "key2.2"))
    assert(levelDB.keySliceByPrefix("key2".toByteArray()).map { String(it) } == 
        listOf("key2.1", "key2.2"))

    levelDB.keyCursor().use { cursor ->
        assert(cursor.seek("key2".toByteArray())?.let { String(it) } == "key2.1")
        assert(cursor.next()?.let { String(it) } == "key2.2")
    }

    // slice/sliceByPrefix/cursor look exactly the same as keySlice/keySliceByPrefix/keyCursor
    // the only difference - they operate with LevelDBRecord instead of ByteArray

    levelDB.batch {
        put("key1".toByteArray(), "value1.updated".toByteArray())
        del("key3".toByteArray())
    }

    levelDB.del("key1".toByteArray())
    
}

(example) Java 8+

import com.github.shyiko.levelkt.LevelDB;
import com.github.shyiko.levelkt.jna.JNALevelDB;

try (LevelDB levelDB = new JNALevelDB("/tmp/0.levedb")) {

    levelDB.batch(batch -> batch
        .put("key1".getBytes(), "value1".getBytes())
        .put("key2.1".getBytes(), "value2.1".getBytes())
        .put("key2.2".getBytes(), "value2.2".getBytes())
        .put("key3".getBytes(), "value3".getBytes())
    );

    assert levelDB
        .sliceByPrefix("key2".getBytes())
        .stream()
        .map(record -> new String(record.getKey()) + "=" + new String(record.getValue()))
        .collect(Collectors.toList())
        .equals(Arrays.asList("key2.1=value2.1", "key2.2=value2.2"));

}

Development

git clone https://github.com/shyiko/levelkt && cd levelkt
./mvnw # shows how to build, test, etc. project

Legal

All code, unless specified otherwise, is licensed under the MIT license.
Copyright (c) 2017 Stanley Shyiko.

You might also like...
Error handling library for Android and Java

ErrorHandler Error handling library for Android and Java Encapsulate error handling logic into objects that adhere to configurable defaults. Then pass

BinGait is a tool to disassemble and view java class files, developed by BinClub.

BinGait Tool to diassemble java class files created by x4e. Usage To run BinGait, run java -jar target/bingait-shadow.jar and BinGait will launch. If

Android tiny device flow client

OAuth 2.0 Device Flow Example Setup Create an Auth0 application Enable Device Code Grants in Advanced settings Disable Client Credentials Enable Devic

POC Simulate Backend Biometric Authentication with AIDL (client app/server app)

poc-simulate-bio-auth-aidl POC Simulate Backend Biometric Authentication with AIDL (client app/server app) #How to use Install server app and run Inst

Java implementation of a Disk-based LRU cache which specifically targets Android compatibility.

Disk LRU Cache A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key and a fixed number of values. Each key m

Runtime code generation for the Java virtual machine.
Runtime code generation for the Java virtual machine.

Byte Buddy runtime code generation for the Java virtual machine Byte Buddy is a code generation and manipulation library for creating and modifying Ja

Apk parser for java

APK parser lib, for decoding binary XML files, getting APK meta info. Table of Contents Features Get APK-parser Usage 1. APK Info 2. Get Binary XML an

Apk parser for java

APK parser lib, for decoding binary XML files, getting APK meta info. Table of Contents Features Get APK-parser Usage 1. APK Info 2. Get Binary XML an

java.io.File compatible SAF library

DocumentFileX java.io.File compatible SAF implementation Tired of SAF bullshits? Implement SAF with ease! This library is in alpha stage. Most feature

Releases(0.1.0)
Owner
Stanley Shyiko
Stanley Shyiko
WebSocket & WAMP in Java for Android and Java 8

Autobahn|Java Client library providing WAMP on Java 8 (Netty) and Android, plus (secure) WebSocket for Android. Autobahn|Java is a subproject of the A

Crossbar.io 1.5k Dec 9, 2022
WebSocket & WAMP in Java for Android and Java 8

Autobahn|Java Client library providing WAMP on Java 8 (Netty) and Android, plus (secure) WebSocket for Android. Autobahn|Java is a subproject of the A

Crossbar.io 1.5k Dec 9, 2022
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 31, 2022
Multiplaform kotlin library for calculating text differences. Based on java-diff-utils, supports JVM, JS and native targets.

kotlin-multiplatform-diff This is a port of java-diff-utils to kotlin with multiplatform support. All credit for the implementation goes to original a

Peter Trifanov 51 Jan 3, 2023
Bar Service Kotlin Client

A simple starter service client written in Kotlin against generated models (protos)A simple starter service client written in Kotlin against generated models (protos)

Logesh Dinakaran 0 Nov 9, 2021
DEMOMovieDB - Client App using movieDB with Kotlin

DEMOMovieDB DEMOMovieDB is a gorgeous client application for TMDb on Android, bu

null 0 Feb 15, 2022
Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platform the code is running.

Trail Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platfor

Mauricio Togneri 13 Aug 29, 2022
a simple cache for android and java

ASimpleCache ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。 1、它可以缓存什么东西? 普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 b

Michael Yang 3.7k Dec 14, 2022
A lightning fast, transactional, file-based FIFO for Android and Java.

Tape by Square, Inc. Tape is a collection of queue-related classes for Android and Java. QueueFile is a lightning-fast, transactional, file-based FIFO

Square 2.4k Dec 30, 2022
UPnP/DLNA library for Java and Android

Cling EOL: This project is no longer actively maintained, code may be outdated. If you are interested in maintaining and developing this project, comm

4th Line 1.6k Jan 4, 2023