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;
use typed_store::metrics::DBMetrics;
use prometheus::Registry;
use core::fmt::Error;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Error> {
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);
}
Ok(())
}Implementations§
Source§impl DBBatch
impl DBBatch
Sourcepub fn new(
dbref: &Arc<DBWithThreadMode<MultiThreaded>>,
db_metrics: &Arc<DBMetrics>,
write_sample_interval: &SamplingInterval,
) -> Self
pub fn new( dbref: &Arc<DBWithThreadMode<MultiThreaded>>, db_metrics: &Arc<DBMetrics>, write_sample_interval: &SamplingInterval, ) -> 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
Source§impl 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 Freeze for DBBatch
impl !RefUnwindSafe for DBBatch
impl Send for DBBatch
impl !Sync for DBBatch
impl Unpin for DBBatch
impl !UnwindSafe for DBBatch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.