[][src]Module sled::transaction

Fully serializable (ACID) multi-Tree transactions

Examples

let config = Config::new().temporary(true);
let db1 = config.open().unwrap();
let db = db1.open_tree(b"a").unwrap();

// Use write-only transactions as a writebatch:
db.transaction(|db| {
    db.insert(b"k1", b"cats")?;
    db.insert(b"k2", b"dogs")?;
    Ok(())
})?;

// Atomically swap two items:
db.transaction(|db| {
    let v1_option = db.remove(b"k1")?;
    let v1 = v1_option.unwrap();
    let v2_option = db.remove(b"k2")?;
    let v2 = v2_option.unwrap();

    db.insert(b"k1", v2)?;
    db.insert(b"k2", v1)?;

    Ok(())
})?;

assert_eq!(&db.get(b"k1")?.unwrap(), b"dogs");
assert_eq!(&db.get(b"k2")?.unwrap(), b"cats");

Transactions also work on tuples of Trees, preserving serializable ACID semantics! In this example, we treat two trees like a work queue, atomically apply updates to data and move them from the unprocessed Tree to the processed Tree.

let config = Config::new().temporary(true);
let db = config.open().unwrap();

let unprocessed = db.open_tree(b"unprocessed items").unwrap();
let processed = db.open_tree(b"processed items").unwrap();

// An update somehow gets into the tree, which we
// later trigger the atomic processing of.
unprocessed.insert(b"k3", b"ligers").unwrap();

// Atomically process the new item and move it
// between `Tree`s.
(&unprocessed, &processed)
    .transaction(|(unprocessed, processed)| {
        let unprocessed_item = unprocessed.remove(b"k3")?.unwrap();
        let mut processed_item = b"yappin' ".to_vec();
        processed_item.extend_from_slice(&unprocessed_item);
        processed.insert(b"k3", processed_item)?;
        Ok(())
    })?;

assert_eq!(unprocessed.get(b"k3").unwrap(), None);
assert_eq!(&processed.get(b"k3").unwrap().unwrap(), b"yappin' ligers");

Structs

TransactionalTree

A transaction that will be applied atomically to the Tree.

TransactionalTrees

A type which allows for pluggable transactional capabilities

Enums

ConflictableTransactionError

An error type that is returned from the closure passed to the transaction method.

TransactionError

An error type that is returned from the closure passed to the transaction method.

UnabortableTransactionError

An error type that is returned from the closure passed to the transaction method.

Traits

Transactional

A type that may be transacted on in sled transactions.

Functions

abort

A simple constructor for Err(TransactionError::Abort(_))

Type Definitions

ConflictableTransactionResult

A transaction-related Result which is used for transparently handling concurrency-related conflicts when running transaction closures.

TransactionResult

A transaction-related Result which is used for returning the final result of a transaction after potentially running the provided closure several times due to underlying conflicts.