16 releases (9 breaking)

✓ Uses Rust 2018 edition

0.11.0 Jan 14, 2020
0.9.3 Oct 26, 2019
0.9.1 Jul 4, 2019
0.8.1 Dec 30, 2018
0.4.0 Mar 6, 2018

#89 in Database interfaces

Download history 10/week @ 2019-10-06 26/week @ 2019-10-13 39/week @ 2019-10-20 64/week @ 2019-10-27 34/week @ 2019-11-03 32/week @ 2019-11-10 35/week @ 2019-11-17 25/week @ 2019-11-24 28/week @ 2019-12-01 468/week @ 2019-12-08 7/week @ 2019-12-15 17/week @ 2019-12-22 2/week @ 2019-12-29 44/week @ 2020-01-05 71/week @ 2020-01-12

285 downloads per month
Used in 3 crates (2 directly)

ISC license

54KB
1.5K SLoC

kv

An embedded key/value store for Rust built on LMDB

  • Easy configuration
  • Integer keys
  • Serde integration (see the Encoding trait)

Optional features

  • cbor-value
    • CBOR value encoding using serde
  • msgpack-value
    • MessagePack encoding using serde
  • json-value
    • JSON value encoding using serde
  • bincode-value
    • bincode value encoding using serde

Some examples of implementing your own encodings using serde can be found here

Documentation

See https://docs.rs/kv


lib.rs:

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

Getting started

use kv::{Config, Error, Manager, ValueRef};

fn run() -> Result<(), Error> {
    // First step create a manager, this ensured that each LMDB environment will only be
    // accessed once per process
    let mut mgr = Manager::new();

    // Next configure a database
    let mut cfg = Config::default("/tmp/rust-kv");

    // Add a bucket named `test`
    cfg.bucket("test", None);

    // Get a Store handle
    let handle = mgr.open(cfg)?;

    // Get read-write access to the underlying store
    let store = handle.write()?;

    // A Bucket provides typed access to an LMDB database
    let bucket = store.bucket::<&str, &str>(Some("test"))?;

    {
        // Finally, a transaction is needed, they can be read-write or readonly, here we will use a
        // write transaction to add data
        let mut txn = store.write_txn()?;

        // To set a value
        let () = txn.set(&bucket, "testing", "abc123")?;

        // Make sure to commit the transaction. There is also an `abort` function to abandon
        // the transaction
        txn.commit()?;
    }

    {
        // This time a readonly transaction
        let txn = store.read_txn()?;

        // Getting a value is easy once everything is set up
        let val = txn.get(&bucket, "testing")?;
        println!("testing => {}", val);
    }

    {
        let txn = store.read_txn()?;
        let mut curs = txn.read_cursor(&bucket)?;
        let all: Vec<(String, String)> =
           curs.map(|(k, v)| (k.to_string(), v.to_string())).collect();
        println!("{:?}", all);
    }

    Ok(())
}
#
# fn main() {
#     run().unwrap();
# }

Dependencies

~1.5–2.4MB
~54K SLoC