Struct typed_store::rocks::DBBatch
source · [−]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
sourceimpl DBBatch
impl DBBatch
sourcepub fn new(dbref: &Arc<DBWithThreadMode<MultiThreaded>>) -> Self
pub fn new(dbref: &Arc<DBWithThreadMode<MultiThreaded>>) -> Self
Create a new batch associated with a DB reference.
Use open_cf
to get the DB reference or an existing open database.
sourcepub fn write(self) -> Result<(), TypedStoreError>
pub fn write(self) -> Result<(), TypedStoreError>
Consume the batch and write its operations to the database
sourceimpl DBBatch
impl DBBatch
sourcepub fn delete_batch<J: Borrow<K>, K: Serialize, V>(
self,
db: &DBMap<K, V>,
purged_vals: impl IntoIterator<Item = J>
) -> Result<Self, TypedStoreError>
pub fn delete_batch<J: Borrow<K>, K: Serialize, V>(
self,
db: &DBMap<K, V>,
purged_vals: impl IntoIterator<Item = J>
) -> Result<Self, TypedStoreError>
Deletes a set of keys given as an iterator
sourcepub fn delete_range<'a, K: Serialize, V>(
self,
db: &'a DBMap<K, V>,
from: &K,
to: &K
) -> Result<Self, TypedStoreError>
pub fn delete_range<'a, K: Serialize, V>(
self,
db: &'a DBMap<K, V>,
from: &K,
to: &K
) -> Result<Self, TypedStoreError>
Deletes a range of keys between from
(inclusive) and to
(non-inclusive)
sourcepub fn insert_batch<J: Borrow<K>, K: Serialize, U: Borrow<V>, V: Serialize>(
self,
db: &DBMap<K, V>,
new_vals: impl IntoIterator<Item = (J, U)>
) -> Result<Self, TypedStoreError>
pub fn insert_batch<J: Borrow<K>, K: Serialize, U: Borrow<V>, V: Serialize>(
self,
db: &DBMap<K, V>,
new_vals: impl IntoIterator<Item = (J, U)>
) -> Result<Self, TypedStoreError>
inserts a range of (key, value) pairs given as an iterator
Auto Trait Implementations
impl RefUnwindSafe for DBBatch
impl Send for DBBatch
impl !Sync for DBBatch
impl Unpin for DBBatch
impl UnwindSafe for DBBatch
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more