Struct automerge::transaction::Transaction

source ·
pub struct Transaction<'a> { /* private fields */ }
Expand description

A transaction on a document. Transactions group operations into a single change so that no other operations can happen in-between.

Created from Automerge::transaction().

§Drop

This transaction should be manually committed or rolled back. If not done manually then it will be rolled back when it is dropped. This is to prevent the document being in an unsafe intermediate state. This is consistent with ? error handling.

Implementations§

source§

impl<'a> Transaction<'a>

source

pub fn hash_for_opid(&self, opid: &ExId) -> Option<ChangeHash>

Get the hash of the change that contains the given opid.

Returns none if the opid:

  • is the root object id
  • does not exist in this document
  • is for an operation in this transaction
source§

impl<'a> Transaction<'a>

source

pub fn get_heads(&self) -> Vec<ChangeHash>

Get the heads of the document before this transaction was started.

source

pub fn commit(self) -> (Option<ChangeHash>, PatchLog)

Commit the operations performed in this transaction, returning the hashes corresponding to the new heads.

source

pub fn commit_with( self, options: CommitOptions ) -> (Option<ChangeHash>, PatchLog)

Commit the operations in this transaction with some options.

let mut doc = Automerge::new();
let mut tx = doc.transaction();
tx.put_object(ROOT, "todos", ObjType::List).unwrap();
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as
i64;
tx.commit_with(CommitOptions::default().with_message("Create todos list").with_time(now));
source

pub fn rollback(self) -> usize

Undo the operations added in this transaction, returning the number of cancelled operations.

Trait Implementations§

source§

impl<'a> Debug for Transaction<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> Drop for Transaction<'a>

source§

fn drop(&mut self)

If a transaction is not commited or rolled back manually then it can leave the document in an intermediate state. This defaults to rolling back the transaction to be compatible with ? error returning before reaching a call to Self::commit().

source§

impl<'a> ReadDoc for Transaction<'a>

source§

fn keys<O: AsRef<ExId>>(&self, obj: O) -> Keys<'_>

Get the keys of the object obj. Read more
source§

fn keys_at<O: AsRef<ExId>>(&self, obj: O, heads: &[ChangeHash]) -> Keys<'_>

Get the keys of the object obj as at heads Read more
source§

fn map_range<'b, O: AsRef<ExId>, R: RangeBounds<String> + 'b>( &'b self, obj: O, range: R ) -> MapRange<'b, R>

Iterate over the keys and values of the map obj in the given range. Read more
source§

fn map_range_at<'b, O: AsRef<ExId>, R: RangeBounds<String> + 'b>( &'b self, obj: O, range: R, heads: &[ChangeHash] ) -> MapRange<'b, R>

Iterate over the keys and values of the map obj in the given range as at heads Read more
source§

fn list_range<O: AsRef<ExId>, R: RangeBounds<usize>>( &self, obj: O, range: R ) -> ListRange<'_, R>

Iterate over the indexes and values of the list or text obj in the given range. Read more
source§

fn list_range_at<O: AsRef<ExId>, R: RangeBounds<usize>>( &self, obj: O, range: R, heads: &[ChangeHash] ) -> ListRange<'_, R>

Iterate over the indexes and values of the list or text obj in the given range as at heads Read more
source§

fn values<O: AsRef<ExId>>(&self, obj: O) -> Values<'_>

Iterate over the values in a map, list, or text object Read more
source§

fn values_at<O: AsRef<ExId>>(&self, obj: O, heads: &[ChangeHash]) -> Values<'_>

Iterate over the values in a map, list, or text object as at heads Read more
source§

fn length<O: AsRef<ExId>>(&self, obj: O) -> usize

Get the length of the given object. Read more
source§

fn length_at<O: AsRef<ExId>>(&self, obj: O, heads: &[ChangeHash]) -> usize

Get the length of the given object as at heads Read more
source§

fn object_type<O: AsRef<ExId>>(&self, obj: O) -> Result<ObjType, AutomergeError>

Get the type of this object, if it is an object.
source§

fn text<O: AsRef<ExId>>(&self, obj: O) -> Result<String, AutomergeError>

Get the string represented by the given text object.
source§

fn text_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<String, AutomergeError>

Get the string represented by the given text object as at heads, see Self::text()
source§

fn spans<O: AsRef<ExId>>(&self, obj: O) -> Result<Spans<'_>, AutomergeError>

Return the sequence of text and block markers in the text object obj
source§

fn spans_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<Spans<'_>, AutomergeError>

Return the sequence of text and block markers in the text object obj as at heads
source§

fn get_cursor<O: AsRef<ExId>>( &self, obj: O, position: usize, at: Option<&[ChangeHash]> ) -> Result<Cursor, AutomergeError>

Obtain the stable address (Cursor) for a usize position in a Sequence (either ObjType::List or ObjType::Text). Read more
source§

fn get_cursor_position<O: AsRef<ExId>>( &self, obj: O, address: &Cursor, at: Option<&[ChangeHash]> ) -> Result<usize, AutomergeError>

Translate Cursor in a Sequence into an absolute position of type usize. Read more
source§

fn marks<O: AsRef<ExId>>(&self, obj: O) -> Result<Vec<Mark<'_>>, AutomergeError>

Get all marks on a current sequence
source§

fn marks_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<Vec<Mark<'_>>, AutomergeError>

Get all marks on a sequence at a given heads
source§

fn hydrate<O: AsRef<ExId>>( &self, obj: O, heads: Option<&[ChangeHash]> ) -> Result<Value, AutomergeError>

source§

fn get_marks<O: AsRef<ExId>>( &self, obj: O, index: usize, heads: Option<&[ChangeHash]> ) -> Result<MarkSet, AutomergeError>

source§

fn get<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P ) -> Result<Option<(Value<'_>, ExId)>, AutomergeError>

Get a value out of the document. Read more
source§

fn get_at<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P, heads: &[ChangeHash] ) -> Result<Option<(Value<'_>, ExId)>, AutomergeError>

Get the value of the given key as at heads, see Self::get()
source§

fn get_all<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P ) -> Result<Vec<(Value<'_>, ExId)>, AutomergeError>

Get all conflicting values out of the document at this prop that conflict. Read more
source§

fn get_all_at<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P, heads: &[ChangeHash] ) -> Result<Vec<(Value<'_>, ExId)>, AutomergeError>

Get all possibly conflicting values for a key as at heads Read more
source§

fn parents<O: AsRef<ExId>>(&self, obj: O) -> Result<Parents<'_>, AutomergeError>

Get the parents of an object in the document tree. Read more
source§

fn parents_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<Parents<'_>, AutomergeError>

Get the parents of the object obj as at heads Read more
source§

fn get_missing_deps(&self, heads: &[ChangeHash]) -> Vec<ChangeHash>

Get the hashes of the changes in this document that aren’t transitive dependencies of the given heads.
source§

fn get_change_by_hash(&self, hash: &ChangeHash) -> Option<&Change>

Get a change by its hash.
source§

impl<'a> Transactable for Transaction<'a>

source§

fn pending_ops(&self) -> usize

Get the number of pending operations in this transaction.

source§

fn put<O: AsRef<ExId>, P: Into<Prop>, V: Into<ScalarValue>>( &mut self, obj: O, prop: P, value: V ) -> Result<(), AutomergeError>

Set the value of property P to value V in object obj.

§Errors

This will return an error if

  • The object does not exist
  • The key is the wrong type for the object
  • The key does not exist in the object
source§

fn splice<O: AsRef<ExId>, V: IntoIterator<Item = ScalarValue>>( &mut self, obj: O, pos: usize, del: isize, vals: V ) -> Result<(), AutomergeError>

Splice new elements into the given sequence. Returns a vector of the OpIds used to insert the new elements

source§

fn put_object<O: AsRef<ExId>, P: Into<Prop>>( &mut self, obj: O, prop: P, value: ObjType ) -> Result<ExId, AutomergeError>

Set the value of property P to the new object V in object obj. Read more
source§

fn insert<O: AsRef<ExId>, V: Into<ScalarValue>>( &mut self, obj: O, index: usize, value: V ) -> Result<(), AutomergeError>

Insert a value into a list at the given index.
source§

fn insert_object<O: AsRef<ExId>>( &mut self, obj: O, index: usize, value: ObjType ) -> Result<ExId, AutomergeError>

Insert an object into a list at the given index.
source§

fn increment<O: AsRef<ExId>, P: Into<Prop>>( &mut self, obj: O, prop: P, value: i64 ) -> Result<(), AutomergeError>

Increment the counter at the prop in the object by value.
source§

fn delete<O: AsRef<ExId>, P: Into<Prop>>( &mut self, obj: O, prop: P ) -> Result<(), AutomergeError>

Delete the value at prop in the object.
source§

fn splice_text<O: AsRef<ExId>>( &mut self, obj: O, pos: usize, del: isize, text: &str ) -> Result<(), AutomergeError>

Like Self::splice but for text.
source§

fn mark<O: AsRef<ExId>>( &mut self, obj: O, mark: Mark<'_>, expand: ExpandMark ) -> Result<(), AutomergeError>

Mark a sequence
source§

fn unmark<O: AsRef<ExId>>( &mut self, obj: O, name: &str, start: usize, end: usize, expand: ExpandMark ) -> Result<(), AutomergeError>

Remove a Mark from a sequence
source§

fn split_block<'p, O>( &mut self, obj: O, index: usize ) -> Result<ExId, AutomergeError>
where O: AsRef<ExId>,

Insert a block marker into the text object obj at the given index. Read more
source§

fn join_block<O>(&mut self, text: O, index: usize) -> Result<(), AutomergeError>
where O: AsRef<ExId>,

Delete a block marker at index from the text object obj.
source§

fn replace_block<'p, O>( &mut self, text: O, index: usize ) -> Result<ExId, AutomergeError>
where O: AsRef<ExId>,

Replace a block marker at index in obj with a new marker and return the ID of the new marker
source§

fn base_heads(&self) -> Vec<ChangeHash>

The heads this transaction will be based on
source§

fn update_text<S: AsRef<str>>( &mut self, obj: &ExId, new_text: S ) -> Result<(), AutomergeError>

Update the value of a string Read more
source§

fn update_spans<'b, O: AsRef<ExId>, I: IntoIterator<Item = BlockOrText<'b>>>( &mut self, text: O, new_text: I ) -> Result<(), AutomergeError>

Update the blocks and text in a text object Read more
source§

fn update_object<O: AsRef<ExId>>( &mut self, obj: O, new_value: &Value ) -> Result<(), UpdateObjectError>

Auto Trait Implementations§

§

impl<'a> Freeze for Transaction<'a>

§

impl<'a> RefUnwindSafe for Transaction<'a>

§

impl<'a> Send for Transaction<'a>

§

impl<'a> Sync for Transaction<'a>

§

impl<'a> Unpin for Transaction<'a>

§

impl<'a> !UnwindSafe for Transaction<'a>

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

§

type Output = T

Should always be Self
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.
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