Crate sled

source
Expand description

sled is a high-performance embedded database with an API that is similar to a BTreeMap<[u8], [u8]>, but with several additional capabilities for assisting creators of stateful systems.

It is fully thread-safe, and all operations are atomic. Multiple Trees with isolated keyspaces are supported with the Db::open_tree method.

sled is built by experienced database engineers who think users should spend less time tuning and working against high-friction APIs. Expect significant ergonomic and performance improvements over time. Most surprises are bugs, so please let us know if something is high friction.

§Examples

let db: sled::Db = sled::open("my_db").unwrap();

// insert and get
db.insert(b"yo!", b"v1");
assert_eq!(&db.get(b"yo!").unwrap().unwrap(), b"v1");

// Atomic compare-and-swap.
db.compare_and_swap(
    b"yo!",      // key
    Some(b"v1"), // old value, None for not present
    Some(b"v2"), // new value, None for delete
)
.unwrap();

// Iterates over key-value pairs, starting at the given key.
let scan_key: &[u8] = b"a non-present key before yo!";
let mut iter = db.range(scan_key..);
assert_eq!(&iter.next().unwrap().unwrap().0, b"yo!");
assert!(iter.next().is_none());

db.remove(b"yo!");
assert!(db.get(b"yo!").unwrap().is_none());

let other_tree: sled::Tree = db.open_tree(b"cool db facts").unwrap();
other_tree.insert(
    b"k1",
    &b"a Db acts like a Tree due to implementing Deref<Target = Tree>"[..]
).unwrap();

Structs§

Functions§

  • 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.

Type Aliases§