Struct async_mwmr::WriteTransaction
source · 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,
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,
sourcepub async fn insert(
&mut self,
key: D::Key,
value: D::Value
) -> Result<(), Error<D, W>>
pub async fn insert( &mut self, key: D::Key, value: D::Value ) -> Result<(), Error<D, W>>
Insert a key-value pair to the database.
sourcepub async fn remove(&mut self, key: D::Key) -> Result<(), Error<D, W>>
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.
sourcepub 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>>
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.
sourcepub async fn iter(
&self,
opts: IteratorOptions
) -> Result<D::Iterator<'_>, Error<D, W>>
pub async fn iter( &self, opts: IteratorOptions ) -> Result<D::Iterator<'_>, Error<D, W>>
Returns an iterator.
sourcepub async fn keys(&self, opts: KeysOptions) -> Result<D::Keys<'_>, Error<D, W>>
pub async fn keys(&self, opts: KeysOptions) -> Result<D::Keys<'_>, Error<D, W>>
Returns an iterator over keys.
sourcepub async fn commit(&mut self) -> Result<(), Error<D, W>>
pub async fn commit(&mut self) -> Result<(), Error<D, W>>
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, 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,
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,
sourcepub 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,
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:
-
If there are no writes, return immediately, a new task will be spawned, and future 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 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>
impl<D, W, S, H> WriteTransaction<D, W, S, H>
sourcepub async fn discard(&mut self)
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.