FastKV is an efficient and reliable key-value storage component written with Java.

Overview

FastKV

中文文档

FastKV is an efficient and reliable key-value storage component written with Java.
It can be used on platforms with JVM environment, such as Android.

1. Features

  1. Efficient

    • Binary coding: the size after coding is much smaller than that of text coding such as XML;
    • Incremental update: FastKV records the offset of each key-value relative to the file, updating can be written directly at the right location.
    • By default, data is recorded with mmap . When updating data, it can be written directly to memory without IO blocking.
    • For a value which length is larger than the threshold, it will be written to another file separately, only it's file name will be cached. In that way, it will not slow down other key-value's accessing.
  2. Support multiple writing mode

    • In addition to the non-blocking writing mode (with mmap), FastKV also supports synchronous blocking and asynchronous blocking (similar to commit and apply of SharePreferences).
  3. Support multiple types

    • Support primitive types such as Boolean / int / float / long / double / string;
    • Support ByteArray (byte []);
    • Support storage objects.
    • Built in encoder with Set (for compatibility with SharePreferences).
  4. Easy to use

    • FastKV provides rich API interfaces, including getAll() and putAll() methods, it is convenient to migrate the data of frameworks such as sharepreferences to FastKV.
  5. Stable and reliable

    • When FastKV writes data in non-blocking way (mmap), it writes two files one by one, to ensure that at least one file is complete at any time;
    • FastKV checks the integrity of the files when loading, if one file is incomplete, it will be restored with another file which is complte.
    • If mmap API fails, it will be degraded to the normal I/O writing mode; and it will try to restore to mmap mode when reloading.
  6. Simple code

    • FastKV is implemented in pure Java and the jar package is less than 40K.

2. Getting Start

2.1 Import

FastKV had been publish to Maven Central:

dependencies {
    implementation 'io.github.billywei01:fastkv:1.0.2'
}

2.2 Initialization

    FastKVConfig.setLogger(FastKVLogger)
    FastKVConfig.setExecutor(ChannelExecutorService(4))

Initialization is optional.
You could set log callback and executor as needed.
It is recommended to pass in your own thread pool to reuse threads.

The log interface provides three levels of callbacks.

    public interface Logger {
        void i(String name, String message);

        void w(String name, Exception e);

        void e(String name, Exception e);
    }

2.3 Read/Write

  • Basic case
    FastKV kv = new FastKV.Builder(path, name).build();
    if(!kv.getBoolean("flag")){
        kv.putBoolean("flag" , true);
    }
  • Sava custom object
    FastKV.Encoder<?>[] encoders = new FastKV.Encoder[]{LongListEncoder.INSTANCE};
    FastKV kv = new FastKV.Builder(path, name).encoder(encoders).build();
        
    String objectKey = "long_list";
    List<Long> list = new ArrayList<>();
    list.add(100L);
    list.add(200L);
    list.add(300L);
    kv.putObject(objectKey, list, LongListEncoder.INSTANCE);

    List<Long> list2 = kv.getObject("long_list");

In addition to supporting basic types, FastKV also supports writing objects. You only need to pass in the encoder of the object when building FastKV instances.
The encoder is an object that implements FastKV.Encoder.
For example, the implementation of LongListEncoder like this:

public class LongListEncoder implements FastKV.Encoder<List<Long>> {
    public static final LongListEncoder INSTANCE = new LongListEncoder();

    @Override
    public String tag() {
        return "LongList";
    }

    @Override
    public byte[] encode(List<Long> obj) {
        return new PackEncoder().putLongList(0, obj).getBytes();
    }

    @Override
    public List<Long> decode(byte[] bytes, int offset, int length) {
        PackDecoder decoder = PackDecoder.newInstance(bytes, offset, length);
        List<Long> list = decoder.getLongList(0);
        decoder.recycle();
        return (list != null) ? list : new ArrayList<>();
    }
}

Encoding objects needs serialization/deserialization.
Here recommend my serialization project: https://github.com/BillyWei01/Packable

2.4 For Android

Comparing with common usage, Android platform has SharePreferences API and support Kotlin.
See: Android Case

3. Benchmark

  • Data source: Collecting part of the key-value data of SharePreferences in the app (with confusion) , 400+ key-values.
    Because some key values are accessed more and others accessed less in normally, I make a normally distributed sequence to test the accessing.

  • Comparison component: Sharepreferences/DataStore/MMKV

  • Device: Huawei Horor 20s

Result:

Write(ms) Read(ms)
SharePreferences 1258 3
DataStore 16650 3
MMKV 25 9
FastKV 16 1
  • SharePreferences use the apply mode. When use commit mode, used more than 5s, much slower.
  • DataStore writes data very slow.
  • MMKV read slower then SharePreferences/DataStore,but much faster in writing.
  • FastKV is fastest both in writing or reading.

License

See the LICENSE file for license rights and limitations.

You might also like...

Java JWT: JSON Web Token for Java and Android

Java JWT: JSON Web Token for Java and Android JJWT aims to be the easiest to use and understand library for creating and verifying JSON Web Tokens (JW

Dec 30, 2022

Java Project based on Java and Encryption using Cryptography algorithms

Symmetric-Encryption-Cryptography-in-Java Java Project based on Java and Encryption using Cryptography algorithms Project Aim Develop Java program to

Feb 3, 2022

A mitigation for CVE-2021-44228 (log4shell) that works by patching the vulnerability at runtime. (Works with any vulnerable java software, tested with java 6 and newer)

Log4jPatcher A Java Agent based mitigation for Log4j2 JNDI exploits. This agent employs 2 patches: Disabling all Lookup conversions (on supported Log4

Dec 16, 2022

Open Source Identity and Access Management For Modern Applications and Services

Keycloak Keycloak is an Open Source Identity and Access Management solution for modern Applications and Services. This repository contains the source

Jan 5, 2023

This application can recognize the sign language alphabets and help people who do not understand sign language to communicate with the speech and hearing impaired.

This application can recognize the sign language alphabets and help people who do not understand sign language to communicate with the speech and hearing impaired.

Sign Language Recognition App This application can recognize the sign language alphabets and help people who do not understand sign language to commun

Oct 7, 2021

JAP is an open source authentication middleware, it is highly decoupled from business code and has good modularity and flexiblity. Developers could integrate JAP into web applications effortlessly.

JAP is an open source authentication middleware, it is highly decoupled from business code and has good modularity and flexiblity. Developers could integrate JAP into web applications effortlessly.

🎨 JAP 是什么? JAP 是一款开源的登录中间件,基于模块化设计,并且与业务高度解耦,使用起来非常灵活,开发者可以毫不费力地将 JAP 集

Dec 1, 2022

Make a customized list of exercises, create and save workouts, and be led through your routine. This application is currently under development.

HIIT Workout Builder ABOUT This application allows you to create and be led through customized high-intensity interval training (HIIT) sessions. The a

Nov 28, 2022

Toloka has a powerful open API, it allows you to integrate an on-demand workforce directly into your processes, and to build scalable and fully automated human-in-the-loop ML pipelines.

Toloka has a powerful open API, it allows you to integrate an on-demand workforce directly into your processes, and to build scalable and fully automated human-in-the-loop ML pipelines.

Toloka Java SDK Documentation Website | API Documentation | Platform Designed by engineers for engineers, Toloka lets you integrate an on-demand workf

Apr 27, 2022

Time-Based One-Time Password (RFC 6238) and HMAC-Based One-Time Password (RFC 4226) reference implementations and more.

Crypto Time-Based One-Time Password (RFC 6238) and HMAC-Based One-Time Password (RFC 4226) reference implementations and more. Getting Started TOTP ge

May 12, 2022
Comments
Releases(v1.1.2)
Owner
Billy Wei
The journey of a thousand miles begins with a single step.
Billy Wei
Jwks RSA - JSON Web Key Set parser.

jwks-rsa Install Maven <dependency> <groupId>com.auth0</groupId> <artifactId>jwks-rsa</artifactId> <version>0.17.0</version> </dependency>

Auth0 158 Dec 30, 2022
Library to easily configure API Key authentication in (parts of) your Spring Boot Application

42 API Key Authentication A library to easily configure API Key authentication in (parts of) your Spring Boot Application. Features Easily configure A

null 2 Dec 8, 2021
A program that presses a key when you move your mouse to another monitor.

FollowMouse A program that presses a key when you move your mouse to another monitor. (useful for automatically changing scenes while livestreaming) F

Brime Live 7 Jul 31, 2022
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

Cossack Labs 1.6k Dec 29, 2022
OAUTHScan is a Burp Suite Extension written in Java with the aim to provide some automatic security checks

OAUTHScan is a Burp Suite Extension written in Java with the aim to provide some automatic security checks, which could be useful during penetration testing on applications implementing OAUTHv2 and OpenID standards.

Maurizio S 163 Nov 29, 2022
An API wrapper for BotiCord API written in Java

An API wrapper for BotiCord API written in Java

BotiCord 3 Nov 8, 2022
A small and easy-to-use one-time password generator library for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP).

OTP-Java A small and easy-to-use one-time password generator for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP). Table of Contents Features Ins

Bastiaan Jansen 106 Dec 30, 2022
Examples and HowTos for BouncyCastle and Java Cryptography Extension (JCE)

CryptographicUtilities Examples and HowTos for BouncyCastle and Java Cryptography Extension (JCE) See class "/src/main/java/de/soderer/utilities/crypt

null 1 Dec 19, 2021
A small and easy-to-use one-time password generator library for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP).

OTP-Java A small and easy-to-use one-time password generator for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP). Table of Contents Features Ins

Bastiaan Jansen 106 Dec 30, 2022