[][src]Crate kv

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

Macros

codec

Define a codec type and implement the Codec trait

Structs

Batch

Batch update

Bincode

Codec implementation

Bucket

Provides typed access to the key/value store

Config

Config is used to create a new store

Integer

Integer key type

Item

Key/value pair

Iter

Iterator over Bucket keys and values

Json

Codec implementation

Lexpr

Codec implementation

Msgpack

Codec implementation

Store

Store is used to read/write data to disk using sled

Transaction

Transaction

Watch

Subscribe to key updated

Enums

Error

Error type

Event

Event is used to describe the type of update

Traits

Codec

Base trait for values that can be encoded using serde

Key

A Key can be used as a key to a database

Value

A trait used to convert between types and Raw

Functions

abort

Abort a transaction

Type Definitions

Raw

Raw is an alias for sled::IVec

TransactionError

Transaction error