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>
impl<K, V, S> OptimisticTransaction<K, V, S>
Sourcepub async fn commit(
&mut self,
) -> Result<(), WtmError<Infallible, Infallible, Infallible>>
pub async fn commit( &mut self, ) -> Result<(), WtmError<Infallible, Infallible, Infallible>>
Commits the transaction, following these steps:
-
If there are no writes, return immediately.
-
Check if read rows were updated since txn started. If so, return
TransactionError::Conflict
. -
If no conflict, generate a commit timestamp and update written rows’ commit ts.
-
Batch up all writes, write them to database.
-
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>
impl<K, V, S> OptimisticTransaction<K, V, S>
Sourcepub 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>>
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>>
Acts like commit
, but takes a callback, which gets run via a
thread to avoid blocking this function. Following these steps:
-
If there are no writes, return immediately, callback will be invoked.
-
Check if read rows were updated since txn started. If so, return
TransactionError::Conflict
. -
If no conflict, generate a commit timestamp and update written rows’ commit ts.
-
Batch up all writes, write them to database.
-
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>
impl<K, V, S> OptimisticTransaction<K, V, S>
Sourcepub fn rollback(
&mut self,
) -> Result<(), TransactionError<Infallible, Infallible>>
pub fn rollback( &mut self, ) -> Result<(), TransactionError<Infallible, Infallible>>
Rollback the transaction.
Sourcepub fn contains_key(
&mut self,
key: &K,
) -> Result<bool, TransactionError<Infallible, Infallible>>
pub fn contains_key( &mut self, key: &K, ) -> Result<bool, TransactionError<Infallible, Infallible>>
Returns true if the given key exists in the database.
Sourcepub fn get<'a, 'b: 'a>(
&'a mut self,
key: &'b K,
) -> Result<Option<Ref<'a, K, V>>, TransactionError<Infallible, Infallible>>
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.
Sourcepub fn insert(
&mut self,
key: K,
value: V,
) -> Result<(), TransactionError<Infallible, Infallible>>
pub fn insert( &mut self, key: K, value: V, ) -> Result<(), TransactionError<Infallible, Infallible>>
Insert a new key-value pair.
Sourcepub fn remove(
&mut self,
key: K,
) -> Result<(), TransactionError<Infallible, Infallible>>
pub fn remove( &mut self, key: K, ) -> Result<(), TransactionError<Infallible, Infallible>>
Remove a key.
Sourcepub fn iter(
&mut self,
) -> Result<TransactionIter<'_, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
pub fn iter( &mut self, ) -> Result<TransactionIter<'_, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
Iterate over the entries of the write transaction.
Sourcepub fn iter_rev(
&mut self,
) -> Result<WriteTransactionRevIter<'_, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
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.
Sourcepub fn range<'a, Q, R>(
&'a mut self,
range: R,
) -> Result<TransactionRange<'a, Q, R, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
pub fn range<'a, Q, R>( &'a mut self, range: R, ) -> Result<TransactionRange<'a, Q, R, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
Returns an iterator over the subset of entries of the database.
Sourcepub fn range_rev<'a, Q, R>(
&'a mut self,
range: R,
) -> Result<WriteTransactionRevRange<'a, Q, R, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
pub fn range_rev<'a, Q, R>( &'a mut self, range: R, ) -> Result<WriteTransactionRevRange<'a, Q, R, K, V, BTreeCm<K>>, TransactionError<Infallible, Infallible>>
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>
impl<K, V, S> !RefUnwindSafe for OptimisticTransaction<K, V, S>
impl<K, V, S> Send for OptimisticTransaction<K, V, S>
impl<K, V, S> Sync for OptimisticTransaction<K, V, S>
impl<K, V, S> Unpin for OptimisticTransaction<K, V, S>
impl<K, V, S> !UnwindSafe for OptimisticTransaction<K, V, S>
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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