Struct OptimisticTransaction

Source
pub struct OptimisticTransaction<K, V, S: AsyncSpawner> { /* private fields */ }
Expand description

A optimistic concurrency control transaction over the SerializableDb.

Implementations§

Source§

impl<K, V, S> OptimisticTransaction<K, V, S>
where K: CheapClone + Ord + Send + Sync + 'static, V: Send + Sync + 'static, S: AsyncSpawner,

Source

pub async fn commit( &mut self, ) -> Result<(), WtmError<Infallible, Infallible, Infallible>>

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

Source§

impl<K, V, S> OptimisticTransaction<K, V, S>
where K: CheapClone + Ord + Send + Sync + 'static, V: Send + Sync + 'static, S: AsyncSpawner,

Source

pub async fn commit_with_task<Fut, E, R>( &mut self, callback: impl FnOnce(Result<(), E>) -> Fut + Send + 'static, ) -> Result<S::JoinHandle<R>, WtmError<Infallible, Infallible, E>>
where E: Error + Send, Fut: Future<Output = R> + Send + 'static, R: Send + 'static,

Acts like commit, but takes a callback, which gets run via a thread to avoid blocking this function. Following these steps:

  1. If there are no writes, return immediately, callback 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 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.

Source§

impl<K, V, S> OptimisticTransaction<K, V, S>
where K: CheapClone + Ord, S: AsyncSpawner,

Source

pub fn version(&self) -> u64

Returns the read version of the transaction.

Source

pub fn rollback( &mut self, ) -> Result<(), TransactionError<Infallible, Infallible>>

Rollback the transaction.

Source

pub fn contains_key( &mut self, key: &K, ) -> Result<bool, TransactionError<Infallible, Infallible>>

Returns true if the given key exists in the database.

Source

pub fn get<'a, 'b: 'a>( &'a mut self, key: &'b K, ) -> Result<Option<Ref<'a, K, V>>, TransactionError<Infallible, Infallible>>

Get a value from the database.

Source

pub fn insert( &mut self, key: K, value: V, ) -> Result<(), TransactionError<Infallible, Infallible>>

Insert a new key-value pair.

Source

pub fn remove( &mut self, key: K, ) -> Result<(), TransactionError<Infallible, Infallible>>

Remove a key.

Source

pub fn iter( &mut self, ) -> Result<TransactionIter<'_, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>

Iterate over the entries of the write transaction.

Source

pub fn iter_rev( &mut self, ) -> Result<WriteTransactionRevIter<'_, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>

Iterate over the entries of the write transaction in reverse order.

Source

pub fn range<'a, Q, R>( &'a mut self, range: R, ) -> Result<TransactionRange<'a, Q, R, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
where K: Borrow<Q>, R: RangeBounds<Q> + 'a, Q: Ord + ?Sized,

Returns an iterator over the subset of entries of the database.

Source

pub fn range_rev<'a, Q, R>( &'a mut self, range: R, ) -> Result<WriteTransactionRevRange<'a, Q, R, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
where K: Borrow<Q>, R: RangeBounds<Q> + 'a, Q: Ord + ?Sized,

Returns an iterator over the subset of entries of the database in reverse order.

Auto Trait Implementations§

§

impl<K, V, S> Freeze for OptimisticTransaction<K, V, S>
where K: Freeze, V: Freeze,

§

impl<K, V, S> !RefUnwindSafe for OptimisticTransaction<K, V, S>

§

impl<K, V, S> Send for OptimisticTransaction<K, V, S>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V, S> Sync for OptimisticTransaction<K, V, S>
where K: Sync + Send, V: Sync + Send,

§

impl<K, V, S> Unpin for OptimisticTransaction<K, V, S>
where K: Unpin, V: Unpin,

§

impl<K, V, S> !UnwindSafe for OptimisticTransaction<K, V, S>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more