Skip to main content

WriteBatchWithIndex

Struct WriteBatchWithIndex 

Source
pub struct WriteBatchWithIndex { /* private fields */ }
Expand description

A write batch that can also be read from, and that can be layered on top of a database iterator.

Values read out of the batch are copied, but iterators and pinned slices borrow, so the borrow checker is what keeps them from outliving their owner.

An iterator built with Self::iterator_with_base reads directly out of the batch’s internal skip-list, so it cannot outlive the batch:

use rust_rocksdb::{DB, WriteBatchWithIndex};

let db = DB::open_default("foo").unwrap();
let mut iter = {
    let mut wbwi = WriteBatchWithIndex::new(0, true);
    wbwi.put(b"k", b"v");
    wbwi.iterator_with_base(db.raw_iterator())
};
iter.seek_to_first();

A slice from Self::get_pinned_from_batch_and_db pins a block in the database’s block cache, so it cannot outlive the database:

use rust_rocksdb::{DB, ReadOptions, WriteBatchWithIndex};

let wbwi = WriteBatchWithIndex::new(0, true);
let readopts = ReadOptions::default();
let _value = {
    let db = DB::open_default("foo").unwrap();
    wbwi.get_pinned_from_batch_and_db(&db, b"k", &readopts).unwrap()
};

Implementations§

Source§

impl WriteBatchWithIndex

Source

pub fn new(reserved_bytes: usize, overwrite_key: bool) -> Self

Source

pub fn len(&self) -> usize

Source

pub fn size_in_bytes(&self) -> usize

Return WriteBatch serialized size (in bytes).

Source

pub fn data(&self) -> &[u8]

Return a reference to a byte array which represents a serialized version of the batch.

Source

pub fn is_empty(&self) -> bool

Source

pub fn get_from_batch<K>( &self, key: K, options: &Options, ) -> Result<Option<Vec<u8>>, Error>
where K: AsRef<[u8]>,

Source

pub fn get_from_batch_cf<K>( &self, cf: &impl AsColumnFamilyRef, key: K, options: &Options, ) -> Result<Option<Vec<u8>>, Error>
where K: AsRef<[u8]>,

Source

pub fn get_from_batch_and_db<T, I, K>( &self, db: &DBCommon<T, I>, key: K, readopts: &ReadOptions, ) -> Result<Option<Vec<u8>>, Error>
where T: ThreadMode, I: DBInner, K: AsRef<[u8]>,

Source

pub fn get_pinned_from_batch_and_db<'db, T, I, K>( &self, db: &'db DBCommon<T, I>, key: K, readopts: &ReadOptions, ) -> Result<Option<DBPinnableSlice<'db>>, Error>
where T: ThreadMode, I: DBInner, K: AsRef<[u8]>,

The returned slice pins a block inside db’s block cache, so its lifetime is tied to db rather than to self. Letting lifetime elision pick &self here would allow the slice to outlive the database and release a cache handle into a destroyed cache.

Source

pub fn get_from_batch_and_db_cf<T, I, K>( &self, db: &DBCommon<T, I>, cf: &impl AsColumnFamilyRef, key: K, readopts: &ReadOptions, ) -> Result<Option<Vec<u8>>, Error>
where T: ThreadMode, I: DBInner, K: AsRef<[u8]>,

Source

pub fn get_pinned_from_batch_and_db_cf<'db, T, I, K>( &self, db: &'db DBCommon<T, I>, cf: &impl AsColumnFamilyRef, key: K, readopts: &ReadOptions, ) -> Result<Option<DBPinnableSlice<'db>>, Error>
where T: ThreadMode, I: DBInner, K: AsRef<[u8]>,

The returned slice pins a block inside db’s block cache, so its lifetime is tied to db rather than to self. See Self::get_pinned_from_batch_and_db.

Source

pub fn put<K, V>(&mut self, key: K, value: V)
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Insert a value into the database under the given key.

Source

pub fn put_cf<K, V>(&mut self, cf: &impl AsColumnFamilyRef, key: K, value: V)
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Source

pub fn merge<K, V>(&mut self, key: K, value: V)
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Source

pub fn merge_cf<K, V>(&mut self, cf: &impl AsColumnFamilyRef, key: K, value: V)
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Source

pub fn delete<K: AsRef<[u8]>>(&mut self, key: K)

Removes the database entry for key. Does nothing if the key was not found.

Source

pub fn delete_cf<K: AsRef<[u8]>>(&mut self, cf: &impl AsColumnFamilyRef, key: K)

Source

pub fn clear(&mut self)

Clear all updates buffered in this batch.

Source

pub fn iterator_with_base<'a, D>( &'a self, base_iterator: DBRawIteratorWithThreadMode<'a, D>, ) -> DBRawIteratorWithThreadMode<'a, D>
where D: DBAccess,

The returned iterator reads directly out of this batch’s internal skip-list and write buffer, so it must not outlive the batch. Binding &self to the same lifetime as the base iterator is what enforces that; with an independent lifetime on &self the iterator could outlive the batch and read freed memory.

Source

pub fn iterator_with_base_cf<'a, D>( &'a self, base_iterator: DBRawIteratorWithThreadMode<'a, D>, cf: &impl AsColumnFamilyRef, ) -> DBRawIteratorWithThreadMode<'a, D>
where D: DBAccess,

The returned iterator reads directly out of this batch, so it must not outlive the batch. See Self::iterator_with_base.

Trait Implementations§

Source§

impl Drop for WriteBatchWithIndex

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for WriteBatchWithIndex

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.