pub struct Transaction { /* private fields */ }Expand description
Represents a single, atomic transaction for modifying a Database.
An Transaction encapsulates a mutable EntryBuilder being constructed. Users interact with
specific Store instances obtained via Transaction::get_store to stage changes.
All staged changes across different subtrees within the transaction are recorded
in the internal EntryBuilder.
When commit() is called, the transaction:
- Finalizes the
EntryBuilderby building an immutableEntry - Calculates the entry’s content-addressable ID
- Ensures the correct parent links are set based on the tree’s state
- Removes any empty subtrees that didn’t have data staged
- Signs the entry if authentication is configured
- Persists the resulting immutable
Entryto the backend
Transaction instances are typically created via Database::new_transaction().
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn get_settings(&self) -> Result<SettingsStore>
pub fn get_settings(&self) -> Result<SettingsStore>
Get a SettingsStore handle for the settings subtree within this transaction.
This method returns a SettingsStore that provides specialized access to the _settings subtree,
allowing you to read and modify settings data within this atomic transaction.
The DocStore automatically merges historical settings from the database with any
staged changes in this transaction.
§Returns
Returns a Result<SettingsStore> that can be used to:
- Read current settings values (including both historical and staged data)
- Stage new settings changes within this transaction
- Access nested settings structures
§Example
let txn = database.new_transaction().await?;
let settings = txn.get_settings()?;
// Read a setting
if let Ok(name) = settings.get_name().await {
println!("Database name: {}", name);
}
// Modify a setting
settings.set_name("Updated Database Name").await?;§Errors
Returns an error if:
- Unable to create the SettingsStore for the settings subtree
- Operation has already been committed
Sourcepub async fn get_index(&self) -> Result<Registry>
pub async fn get_index(&self) -> Result<Registry>
Gets a handle to the Index for managing subtree registry and metadata.
The Index provides access to the _index subtree, which stores metadata
about all subtrees in the database including their type identifiers and configurations.
§Returns
A Result<Registry> containing the handle for managing the index.
§Errors
Returns an error if:
- Unable to create the Registry for the _index subtree
- Operation has already been committed
Sourcepub async fn get_store<T>(
&self,
subtree_name: impl Into<String> + Send,
) -> Result<T>
pub async fn get_store<T>( &self, subtree_name: impl Into<String> + Send, ) -> Result<T>
Gets a handle to a specific Store for modification within this transaction.
This method creates and returns an instance of the specified Store type T,
associated with this Transaction. The returned Store handle can be used to
stage changes (e.g., using DocStore::set).
These changes are recorded within this Transaction.
If this is the first time this subtree is accessed within the transaction, its parent tips will be fetched and stored.
§Type Parameters
T- The concreteStoreimplementation type to create.
§Arguments
subtree_name- The name of the subtree to get a modification handle for.
§Returns
A Result<T> containing the Store handle.
Sourcepub fn get_local_data<T>(
&self,
subtree_name: impl AsRef<str>,
) -> Result<Option<T>>where
T: Data,
pub fn get_local_data<T>(
&self,
subtree_name: impl AsRef<str>,
) -> Result<Option<T>>where
T: Data,
Gets the currently staged data for a specific subtree within this transaction.
This is intended for use by Store implementations to retrieve the data
they have staged locally within the Transaction before potentially merging
it with historical data.
§Type Parameters
T- The data type (expected to be a CRDT) to deserialize the staged data into.
§Arguments
subtree_name- The name of the subtree whose staged data is needed.
§Returns
A Result<Option<T>>:
§Behavior
- If the subtree doesn’t exist or has no data, returns
Ok(None) - If the subtree exists but has empty data (empty string or whitespace), returns
Ok(None) - Otherwise deserializes the JSON data to type
Tand returnsOk(Some(T))
§Errors
Returns an error if the transaction has already been committed or if the
subtree data exists but cannot be deserialized to type T.
Sourcepub async fn commit(self) -> Result<ID>
pub async fn commit(self) -> Result<ID>
Commits the transaction, finalizing and persisting the entry to the backend.
This method:
- Takes ownership of the
EntryBuilderfrom the internalOption - Removes any empty subtrees
- Adds metadata if appropriate
- Sets authentication if configured
- Builds the immutable
EntryusingEntryBuilder::build() - Signs the entry if authentication is configured
- Validates authentication if present
- Calculates the entry’s content-addressable ID
- Persists the entry to the backend
- Returns the ID of the newly created entry
After commit, the transaction cannot be used again, as the internal
EntryBuilder has been consumed.
§Returns
A Result<ID> containing the ID of the committed entry.
Trait Implementations§
Source§impl Clone for Transaction
impl Clone for Transaction
Source§fn clone(&self) -> Transaction
fn clone(&self) -> Transaction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for Transaction
impl !RefUnwindSafe for Transaction
impl Send for Transaction
impl Sync for Transaction
impl Unpin for Transaction
impl !UnwindSafe for Transaction
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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