#key-value-store #thread-safe #disk #pair #data-store #index #lookup

skv

A simple thread-safe disk-backed key-value store

3 releases

0.1.2 Jul 16, 2023
0.1.1 Jun 19, 2023
0.1.0 Jun 18, 2023

#629 in Filesystem

33 downloads per month

MIT license

14KB
211 lines

skv

Crates.io Apache 2.0 licensed

A simple key-value store written in Rust.

skv implements a basic thread-safe key-value store that persists data to disk. It maintains an index to quickly lookup data offsets in the storage file. Key-value pairs can be inserted into the store with the insert method and retrieved with the get method. The store's state can be loaded from disk using the load method, and a new store can be created with the new method.

Most basic usage

use skv::KeyValueStore;

fn main() -> Result<(), Box<dyn std::error::Error>> {

    // Create a new key-value store
    let kv_store = KeyValueStore::new("kv_store.db", "kv_index.db")?;

    // Insert some key-value pairs
    kv_store.insert("key1".to_string(), "value1".to_string())?;
    kv_store.insert("key2".to_string(), "value2".to_string())?;

    // Retrieve some key-value pairs
    println!("{:?}", kv_store.get("key1"));
    println!("{:?}", kv_store.get("key2"));

    Ok(())
    
}

Generic input

use skv::KeyValueStore;

fn main() -> Result<(), Box<dyn std::error::Error>> {

    // Create a new key-value store with a type that can be serialized
    // as a byte array. In this case, we use a fixed size array.
    let kv_store = KeyValueStore::<[u8;2]>::new("kv_store.db", "kv_index.db")?;

    // Insert some key-value pairs
    kv_store.insert("key1".to_string(), [0, 1])?;
    kv_store.insert("key2".to_string(), [2, 3])?;

    // Retrieve some key-value pairs
    println!("{:?}", kv_store.get("key1"));
    println!("{:?}", kv_store.get("key2"));

    Ok(())

}

Benchmarks

TLDR; skv is pretty fast. Read take ~18µs and writes take ~750µs 🏎️

Please run cargo bench to run the benchmarks on your machine to get a better idea of how skv performs. The following benchmarks were run on a 2019 MBP, they may not be representative of your machine.

Benchmark Lower Estimate Upper
random reads 17.544 µs 17.774 µs 18.035 µs
random writes 710.12 µs 756.05 µs 804.26 µs
sequential reads 17.864 µs 18.160 µs 18.497 µs
sequential writes 698.29 µs 746.16 µs 800.71 µs

Limitations

Below are some of the limitations of skv. These limitations may be addressed in future versions, or may require a more complex implementation. skv is intended stay exremely simple and easy to use and may not fit your use case.

  • benches are test are on relatively small data sets <1M key-value pairs
  • only supports String key-value pairs
  • index is not optimized for memory usage and lookup is O(n)
  • persisted data is not compressed or optimized for storage

Dependencies

~0.7–1.4MB
~31K SLoC