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_opts(tempfile::tempdir().unwrap(), None, MetricConf::default(), &[("First_CF", rocksdb::Options::default()), ("Second_CF", rocksdb::Options::default())]).unwrap();
let db_cf_1 = DBMap::reopen(&rocks, Some("First_CF"), &ReadWriteOptions::default(), false)
.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"), &ReadWriteOptions::default(), false)
.expect("Failed to open storage");
let keys_vals_2 = (1000..1100).map(|i| (i, i.to_string()));
let mut batch = db_cf_1.batch();
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<Database>,
batch: StorageWriteBatch,
db_metrics: &Arc<DBMetrics>,
write_sample_interval: &SamplingInterval,
) -> Self
pub fn new( dbref: &Arc<Database>, batch: StorageWriteBatch, 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
pub fn size_in_bytes(&self) -> usize
pub fn delete_batch<J: Borrow<K>, K: Serialize, V>( &mut self, db: &DBMap<K, V>, purged_vals: impl IntoIterator<Item = J>, ) -> Result<(), TypedStoreError>
Sourcepub fn schedule_delete_range<K: Serialize, V>(
&mut self,
db: &DBMap<K, V>,
from: &K,
to: &K,
) -> Result<(), TypedStoreError>
pub fn schedule_delete_range<K: Serialize, V>( &mut self, db: &DBMap<K, V>, from: &K, to: &K, ) -> Result<(), TypedStoreError>
Deletes a range of keys between from
(inclusive) and to
(non-inclusive)
by writing a range delete tombstone in the db map
If the DBMap is configured with ignore_range_deletions set to false,
the effect of this write will be visible immediately i.e. you won’t
see old values when you do a lookup or scan. But if it is configured
with ignore_range_deletions set to true, the old value are visible until
compaction actually deletes them which will happen sometime after. By
default ignore_range_deletions is set to true on a DBMap (unless it is
overridden in the config), so please use this function with caution
Sourcepub fn insert_batch<J: Borrow<K>, K: Serialize, U: Borrow<V>, V: Serialize>(
&mut self,
db: &DBMap<K, V>,
new_vals: impl IntoIterator<Item = (J, U)>,
) -> Result<&mut Self, TypedStoreError>
pub fn insert_batch<J: Borrow<K>, K: Serialize, U: Borrow<V>, V: Serialize>( &mut self, db: &DBMap<K, V>, new_vals: impl IntoIterator<Item = (J, U)>, ) -> Result<&mut Self, TypedStoreError>
inserts a range of (key, value) pairs given as an iterator
pub fn partial_merge_batch<J: Borrow<K>, K: Serialize, V: Serialize, B: AsRef<[u8]>>( &mut self, db: &DBMap<K, V>, new_vals: impl IntoIterator<Item = (J, B)>, ) -> Result<&mut Self, TypedStoreError>
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
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§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 more§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 more§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
§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
§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.§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.§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.§impl<T> Tap for T
impl<T> Tap for T
§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 more§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 more§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 more§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 more§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 more§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 more§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.§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.§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.§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.§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.§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.§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.