Crate typed_sled

source ·
Expand description

typed-sled - a database build on top of sled.

sled is a high-performance embedded database with an API that is similar to a BTreeMap<[u8], [u8]>.
typed-sled builds on top of sled and offers an API that is similar to a BTreeMap<K, V>, where K and V are user defined types which implement Deserialize and Serialize.

features

Multiple features for common use cases are also available:

  • [search]: SearchEngine on top of a Tree.
  • [key_generating]: Create Trees with automatically generated keys.
  • [convert]: Convert any Tree into another Tree with different key and value types.
  • custom_serde: Create Trees with custom (de)serialization. This for example makes lazy or zero-copy (de)serialization possible.

Example

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct SomeValue(u32);

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Creating a temporary sled database.
    // If you want to persist the data use sled::open instead.
    let db = sled::Config::new().temporary(true).open().unwrap();

    // The id is used by sled to identify which Tree in the database (db) to open
    let tree = typed_sled::Tree::<String, SomeValue>::open(&db, "unique_id");

    tree.insert(&"some_key".to_owned(), &SomeValue(10))?;

    assert_eq!(tree.get(&"some_key".to_owned())?, Some(SomeValue(10)));
    Ok(())
}

Modules

Support for custom (de)serialization.

Structs

Compare and swap error.
Top-level configuration for the system.
A flash-sympathetic persistent lock-free B+ tree.

Enums

Traits

Trait alias for bounds required on keys and values. For now only types that implement DeserializeOwned are supported.

Functions

The function which is used to deserialize all keys and values.
Opens a Db with a default configuration at the specified path. This will create a new storage directory at the specified path if it does not already exist. You can use the Db::was_recovered method to determine if your database was recovered from a previous instance. You can use Config::create_new if you want to increase the chances that the database will be freshly created.
The function which is used to serialize all keys and values.