22 releases (11 breaking)

new 0.21.1 Aug 9, 2020
0.20.2 Mar 6, 2020
0.9.3 Oct 26, 2019
0.9.1 Jul 4, 2019
0.4.0 Mar 6, 2018

#92 in Database interfaces

Download history 71/week @ 2020-04-24 62/week @ 2020-05-01 90/week @ 2020-05-08 84/week @ 2020-05-15 73/week @ 2020-05-22 115/week @ 2020-05-29 104/week @ 2020-06-05 137/week @ 2020-06-12 126/week @ 2020-06-19 170/week @ 2020-06-26 148/week @ 2020-07-03 110/week @ 2020-07-10 91/week @ 2020-07-17 35/week @ 2020-07-24 48/week @ 2020-07-31 144/week @ 2020-08-07

439 downloads per month
Used in 5 crates (4 directly)

ISC license

36KB
907 lines

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"))?;

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

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

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

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

        let x: Json<SomeType> = bucket.get("example")?.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(())
}
#
# fn main() {
#     run().unwrap();
# }

Dependencies

~2.3–3.5MB
~68K SLoC