pub struct DBBatch { /* private fields */ }
Expand description

Provides a mutable struct to form a collection of database write operations, and execute them.

Batching write and delete operations is faster than performing them one by one and ensures their atomicity, ie. they are all written or none is. This is also true of operations across column families in the same database.

Serializations / Deserialization, and naming of column families is performed by passing a DBMap<K,V> with each operation.

use typed_store::rocks::*;
use tempfile::tempdir;
use typed_store::Map;
let rocks = open_cf(tempfile::tempdir().unwrap(), None, &["First_CF", "Second_CF"]).unwrap();

let db_cf_1 = DBMap::reopen(&rocks, Some("First_CF"))
    .expect("Failed to open storage");
let keys_vals_1 = (1..100).map(|i| (i, i.to_string()));

let db_cf_2 = DBMap::reopen(&rocks, Some("Second_CF"))
    .expect("Failed to open storage");
let keys_vals_2 = (1000..1100).map(|i| (i, i.to_string()));

let batch = db_cf_1
    .batch()
    .insert_batch(&db_cf_1, keys_vals_1.clone())
    .expect("Failed to batch insert")
    .insert_batch(&db_cf_2, keys_vals_2.clone())
    .expect("Failed to batch insert");

let _ = batch.write().expect("Failed to execute batch");
for (k, v) in keys_vals_1 {
    let val = db_cf_1.get(&k).expect("Failed to get inserted key");
    assert_eq!(Some(v), val);
}

for (k, v) in keys_vals_2 {
    let val = db_cf_2.get(&k).expect("Failed to get inserted key");
    assert_eq!(Some(v), val);
}

Implementations

Create a new batch associated with a DB reference.

Use open_cf to get the DB reference or an existing open database.

Consume the batch and write its operations to the database

Deletes a set of keys given as an iterator

Deletes a range of keys between from (inclusive) and to (non-inclusive)

inserts a range of (key, value) pairs given as an iterator

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.