pub struct WriteTransaction<D: AsyncDatabase, W: AsyncPendingManager, S: AsyncSpawner, H = RandomState> { /* private fields */ }
Expand description

WriteTransaction is used to perform writes to the database. It is created by calling TransactionDB::write.

Implementations§

source§

impl<D, W, S, H> WriteTransaction<D, W, S, H>
where D: AsyncDatabase, W: AsyncPendingManager<Key = D::Key, Value = D::Value>, S: AsyncSpawner, H: BuildHasher + Default,

source

pub async fn insert( &mut self, key: D::Key, value: D::Value ) -> Result<(), Error<D, W>>

Insert a key-value pair to the database.

source

pub async fn remove(&mut self, key: D::Key) -> Result<(), Error<D, W>>

Removes a key.

This is done by adding a delete marker for the key at commit timestamp. Any reads happening before this timestamp would be unaffected. Any reads after this commit would see the deletion.

source

pub async fn get<'a, 'b: 'a>( &'a mut self, key: &'b D::Key ) -> Result<Option<Item<'a, D::Key, D::Value, D::ItemRef<'a>, D::Item>>, Error<D, W>>

Looks for key and returns corresponding Item.

source

pub async fn iter( &self, opts: IteratorOptions ) -> Result<D::Iterator<'_>, Error<D, W>>

Returns an iterator.

source

pub async fn keys(&self, opts: KeysOptions) -> Result<D::Keys<'_>, Error<D, W>>

Returns an iterator over keys.

source

pub async fn commit(&mut self) -> Result<(), Error<D, W>>

Commits the transaction, following these steps:

  1. If there are no writes, return immediately.

  2. Check if read rows were updated since txn started. If so, return TransactionError::Conflict.

  3. If no conflict, generate a commit timestamp and update written rows’ commit ts.

  4. Batch up all writes, write them to database.

  5. If callback is provided, Badger will return immediately after checking for conflicts. Writes to the database will happen in the background. If there is a conflict, an error will be returned and the callback will not run. If there are no conflicts, the callback will be called in the background upon successful completion of writes or any error during write.

If error is nil, the transaction is successfully committed. In case of a non-nil error, the LSM tree won’t be updated, so there’s no need for any rollback.

source§

impl<D, W, S, H> WriteTransaction<D, W, S, H>
where D: AsyncDatabase + Send + Sync, D::Key: Send, D::Value: Send, W: AsyncPendingManager<Key = D::Key, Value = D::Value> + Send, S: AsyncSpawner, H: BuildHasher + Default + Send + Sync + 'static,

source

pub async fn commit_with_task<R>( &mut self, fut: impl FnOnce(Result<(), D::Error>) -> R + Send + 'static ) -> Result<S::JoinHandle<R>, Error<D, W>>
where R: Send + 'static,

Acts like commit, but takes a future and a spawner, which gets run via a task to avoid blocking this function. Following these steps:

  1. If there are no writes, return immediately, a new task will be spawned, and future will be invoked.

  2. Check if read rows were updated since txn started. If so, return TransactionError::Conflict.

  3. If no conflict, generate a commit timestamp and update written rows’ commit ts.

  4. Batch up all writes, write them to database.

  5. Return immediately after checking for conflicts. If there is a conflict, an error will be returned immediately and the no task will be spawned run. If there are no conflicts, a task will be spawned and the future will be called in the background upon successful completion of writes or any error during write.

If error does not occur, the transaction is successfully committed. In case of an error, the DB should not be updated (The implementors of AsyncDatabase must promise this), so there’s no need for any rollback.

source§

impl<D, W, S, H> WriteTransaction<D, W, S, H>

source

pub async fn discard(&mut self)

Discards a created transaction. This method is very important and must be called. commit* methods calls this internally, however, calling this multiple times doesn’t cause any issues. So, this can safely be called via a defer right when transaction is created.

NOTE: If any operations are run on a discarded transaction, TransactionError::Discard is returned.

Trait Implementations§

source§

impl<D, W, S, H> Drop for WriteTransaction<D, W, S, H>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<D, W, S, H> Freeze for WriteTransaction<D, W, S, H>
where H: Freeze, W: Freeze, <D as AsyncDatabase>::Key: Freeze, <D as AsyncDatabase>::Value: Freeze,

§

impl<D, W, S, H = RandomState> !RefUnwindSafe for WriteTransaction<D, W, S, H>

§

impl<D, W, S, H> Send for WriteTransaction<D, W, S, H>
where H: Sync + Send,

§

impl<D, W, S, H> Sync for WriteTransaction<D, W, S, H>
where H: Sync + Send,

§

impl<D, W, S, H> Unpin for WriteTransaction<D, W, S, H>
where H: Unpin, W: Unpin, <D as AsyncDatabase>::Key: Unpin, <D as AsyncDatabase>::Value: Unpin,

§

impl<D, W, S, H = RandomState> !UnwindSafe for WriteTransaction<D, W, S, H>

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>,

§

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>,

§

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.