Crate kv

source ·
Expand description

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);
    }

    Ok(())
}

Structs

A Bucket represents a single database, or section of the Store
Config is used to create a new store
Database options.
Integer key type
A process is only permitted to have one open handle to each database. This manager exists to enforce that constraint: don’t open databases directly.
A Store is used to keep data on disk using LMDB
A Value can be used to dynamically build values
A mutable reference to an existing value slice
A reference to an existing value slice

Enums

Iterable access to the database
CursorOp provides the ability to specify the position of a cursor
Error type
Access to the database

Traits

Encoded values
A Key can be used as a key to a database
A trait for types wrapping Serde values
A Value can be stored in a database