26 releases (14 breaking)

0.24.0 Aug 25, 2022
0.23.1 Feb 9, 2022
0.22.0 Sep 8, 2020
0.20.2 Mar 6, 2020
0.4.0 Mar 6, 2018

#2222 in Database interfaces

Download history 1317/week @ 2024-07-22 1062/week @ 2024-07-29 900/week @ 2024-08-05 787/week @ 2024-08-12 948/week @ 2024-08-19 1453/week @ 2024-08-26 1770/week @ 2024-09-02 1413/week @ 2024-09-09 882/week @ 2024-09-16 1362/week @ 2024-09-23 1047/week @ 2024-09-30 887/week @ 2024-10-07 672/week @ 2024-10-14 666/week @ 2024-10-21 829/week @ 2024-10-28 940/week @ 2024-11-04

3,161 downloads per month
Used in 15 crates (9 directly)

ISC license

43KB
1K SLoC

kv

An embedded key/value store for Rust built on sled

  • Easy configuration
  • Integer keys
  • Serde integration

Note: kv 0.20 and greater have been completely re-written to use sled instead of LMDB. In the process the entire API has been redesigned and simplified significantly. If you still need to use LMDB or don't like the new interface then you might want to check out rkv.

Optional features

  • msgpack-value
    • MessagePack encoding using rmp-serde
  • json-value
    • JSON encoding using serde_json
  • bincode-value
    • bincode encoding using bincode
  • lexpr-value
    • S-expression encoding using serde-lexpr

Documentation

See https://docs.rs/kv


lib.rs:

kv is a simple way to embed a key/value store in Rust applications. It is built using sled and aims to be as lightweight as possible while still providing a nice high level interface.

Getting started

use kv::*;

#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
struct SomeType {
    a: i32,
    b: i32
}

fn run() -> Result<(), Error> {
    // Configure the database
    let mut cfg = Config::new("./test/example1");

    // Open the key/value store
    let store = Store::new(cfg)?;

    // A Bucket provides typed access to a section of the key/value store
    let test = store.bucket::<Raw, Raw>(Some("test"))?;

    let key = Raw::from(b"test");
    let value = Raw::from(b"123");

    // Set test = 123
    test.set(&key, &value)?;
    assert!(test.get(&key).unwrap().unwrap() == value);
    assert!(test.get(&b"something else".into()).unwrap() == None);

    // Integer keys
    let aaa = store.bucket::<Integer, String>(Some("aaa"))?;
    let key = Integer::from(1);
    let value = String::from("Testing");
    aaa.set(&key, &value);

    #[cfg(feature = "json-value")]
    {
        // Using a Json encoded type is easy, thanks to Serde
        let bucket = store.bucket::<&str, Json<SomeType>>(None)?;

        let k = "example";
        let x = Json(SomeType {a: 1, b: 2});
        bucket.set(&k, &x)?;

        let x: Json<SomeType> = bucket.get(&k)?.unwrap();

        for item in bucket.iter() {
            let item = item?;
            let key: String = item.key()?;
            let value = item.value::<Json<SomeType>>()?;
            println!("key: {}, value: {}", key, value);
        }

        // A transaction
        bucket.transaction(|txn| {
            txn.set(&"x", &Json(SomeType {a: 1, b: 2}))?;
            txn.set(&"y", &Json(SomeType {a: 3, b: 4}))?;
            txn.set(&"z", &Json(SomeType {a: 5, b: 6}))?;

            Ok(())
        })?;
    }
    Ok(())
}
#

Dependencies

~1.8–3.5MB
~66K SLoC