All Projects → BillyWei01 → FastKV

BillyWei01 / FastKV

Licence: MIT license
FastKV is an efficient key-value storage library.

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to FastKV

Lucid
High performance and distributed KV store w/ REST API. 🦀
Stars: ✭ 171 (-37.82%)
Mutual labels:  key-value
Filebase
A Simple but Powerful Flat File Database Storage.
Stars: ✭ 235 (-14.55%)
Mutual labels:  key-value
SwiftStore
Key-Value store for Swift backed by LevelDB
Stars: ✭ 119 (-56.73%)
Mutual labels:  key-value
Ansible Role Redis
Ansible Role - Redis
Stars: ✭ 176 (-36%)
Mutual labels:  key-value
Endb
Key-value storage for multiple databases. Supports MongoDB, MySQL, Postgres, Redis, and SQLite.
Stars: ✭ 208 (-24.36%)
Mutual labels:  key-value
raft-badger
Badger-based backend for Hashicorp's raft package
Stars: ✭ 27 (-90.18%)
Mutual labels:  key-value
Fastkv
FastKV is a real-time and high-performance persistent key-value store implemented by mmap. FastKV是由mmap实现的一个高实时性、高性能key-value持久化存储组件。
Stars: ✭ 163 (-40.73%)
Mutual labels:  key-value
DataTanker
Embedded persistent key-value store for .NET. Pure C# code.
Stars: ✭ 53 (-80.73%)
Mutual labels:  key-value
Libdict
C library of key-value data structures.
Stars: ✭ 234 (-14.91%)
Mutual labels:  key-value
keyv
Simple key-value storage with support for multiple backends.
Stars: ✭ 202 (-26.55%)
Mutual labels:  key-value
Hive
Lightweight and blazing fast key-value database written in pure Dart.
Stars: ✭ 2,681 (+874.91%)
Mutual labels:  key-value
Iowow
The skiplist based persistent key/value storage engine
Stars: ✭ 206 (-25.09%)
Mutual labels:  key-value
Dictionary
A dictionary data type with a fast b-tree based search
Stars: ✭ 39 (-85.82%)
Mutual labels:  key-value
Dvid
Distributed, Versioned, Image-oriented Dataservice
Stars: ✭ 174 (-36.73%)
Mutual labels:  key-value
okdbc
A fast, light-weight key/value store with http & memcache(TCP/UDP) interface.
Stars: ✭ 28 (-89.82%)
Mutual labels:  key-value
Mmkv
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
Stars: ✭ 13,900 (+4954.55%)
Mutual labels:  key-value
Cubdb
Elixir embedded key/value database
Stars: ✭ 235 (-14.55%)
Mutual labels:  key-value
redis-multi-programming-language-practice
🖖 Learn how to use Redis, from beginner basics to advanced techniques | 最新 Redis 底层原理分析与多语言应用实践
Stars: ✭ 28 (-89.82%)
Mutual labels:  key-value
docs
Source code of the ArangoDB online documentation
Stars: ✭ 18 (-93.45%)
Mutual labels:  key-value
mysqly
Full-featured opensource small-overhead PHP data framework for Mysql built for fast and efficient development
Stars: ✭ 18 (-93.45%)
Mutual labels:  key-value

FastKV

Maven Central中文文档

FastKV is an efficient key-value storage library 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 text coding such as XML;
    • Incremental update: FastKV records the offset of each key-value relative to the file head, 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 accessing of other key-value .
  2. Support different writing modes

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

    • Support primitive types such as Boolean / int / float / long / double / string;
    • Support ByteArray (byte []);
    • Support storage objects.
    • Built in StringSet encoder(for compatibility with SharePreferences).
  4. Support multi-process

    • The project supply an implement to support multi-process (MPFastKV).
    • Support listener for changed values, one process write, all processes known.
  5. 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.
  6. 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 integrate at any time;
    • FastKV checks the integrity of the files when loading, if one file is not integrate, it will be restored with another file which is integrated.
    • If mmap API fails, it will be degraded to the blocking I/O; and it will try to restore to mmap mode when reloading.
  7. Simple code

    • FastKV is implemented in pure Java and size of jar is less then 100KB.

2. Getting Start

2.1 Import

FastKV had been published to Maven Central:

For Android (include SharePreferences API, support multi-process):

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

For Java (Pure Java API, not support multi-process):

dependencies {
    implementation 'io.github.billywei01:fastkv-java:1.1.3'
}

2.2 Initialization

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

Initialization is optional.
You could set log callback and executor.
It is recommended to set 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);
    }
    
    int count = kv.getInt("count");
    if(count < 10){
        kv.putInt("count" , count + 1);
    }
  • Sava custom object
    FastKV.Encoder<?>[] encoders = new FastKV.Encoder[]{LongListEncoder.INSTANCE};
    FastKV kv = new FastKV.Builder(path, name).encoder(encoders).build();
        
    List<Long> list = new ArrayList<>();
    list.add(100L);
    list.add(200L);
    list.add(300L);
    kv.putObject("long_list", 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) {
        return PackDecoder.newInstance(bytes, offset, length).getLongList(0); 
    }
}

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) , hundreds of 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.

  • Test Code:Benchmark

  • Comparison component: SharePreferences/DataStore/MMKV

  • Device: Huawei Honor 20s

Result:

Write(ms) Read(ms)
SharePreferences 1182 2
DataStore 33277 2
MMKV 29 10
FastKV 19 1
  • SharePreferences use the apply mode. When use commit mode, it will be much slower.
  • DataStore writes data very slow.
  • MMKV read slower than SharePreferences/DataStore,but much faster in writing.
  • FastKV is the fastest both in writing or reading.

The test above writes hundreds of key-values on one file, so the results have a big difference. Normally one file may only save several or tens of key-values, the result may be close.

License

See the LICENSE file for license rights and limitations.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].