Struct automerge::AutoCommit

source ·
pub struct AutoCommit { /* private fields */ }
Expand description

An automerge document that automatically manages transactions.

§Creating, loading, merging and forking documents

A new document can be created with Self::new(), which will create a document with a random ActorId. Existing documents can be loaded with Self::load().

If you have two documents and you want to merge the changes from one into the other you can use Self::merge().

If you have a document you want to split into two concurrent threads of execution you can use Self::fork(). If you want to split a document from ealier in its history you can use Self::fork_at().

§Reading values

Self implements ReadDoc, which provides methods for reading values from the document.

§Modifying a document

This type implements Transactable directly, so you can modify it using methods from Transactable.

§Synchronization

To synchronise call Self::sync() which returns an implementation of SyncDoc

§Patches, maintaining materialized views

AutoCommit allows you to generate Patches representing changes to the current state of the document which you can use to maintain a materialized view of the current state. There are several ways to use this. See the documentation on Self::diff() for more details, but the key point to remember is that AutoCommit manages an internal “diff cursor” for you. This is a representation of the heads of the document last time you called Self::diff_incremental() but you can also manage it directly using Self::update_diff_cursor() and Self::reset_diff_cursor().

Implementations§

source§

impl AutoCommit

source

pub fn new() -> AutoCommit

source

pub fn load(data: &[u8]) -> Result<Self, AutomergeError>

source

pub fn load_unverified_heads(data: &[u8]) -> Result<Self, AutomergeError>

source

pub fn load_with( data: &[u8], on_error: OnPartialLoad, mode: VerificationMode ) -> Result<Self, AutomergeError>

👎Deprecated since 0.5.2: use load_with_options instead
source

pub fn load_with_options( data: &[u8], options: LoadOptions<'_> ) -> Result<Self, AutomergeError>

source

pub fn reset_diff_cursor(&mut self)

Erases the diff cursor created by Self::update_diff_cursor() and no longer indexes changes to the document.

source

pub fn update_diff_cursor(&mut self)

Sets the Self::diff_cursor() to current heads of the document and will begin building an index with every change moving forward.

If Self::diff() is called with Self::diff_cursor() as before and Self::get_heads() as after - the index will be used

If the cursor is no longer needed it can be reset with Self::reset_diff_cursor()

source

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

Returns the cursor set by Self::update_diff_cursor()

source

pub fn make_patches(&self, patch_log: &mut PatchLog) -> Vec<Patch>

Generate the patches recorded in patch_log

source

pub fn diff( &mut self, before: &[ChangeHash], after: &[ChangeHash] ) -> Vec<Patch>

Generates a diff from before to after

By default the diff requires a sequental scan of all the ops in the doc.

To do a fast indexed diff before must equal Self::diff_cursor() and after must equal Self::get_heads(). The diff cursor is managed with Self::update_diff_cursor() and Self::reset_diff_cursor()

Managing the diff index has a small but non-zero overhead. It should be disabled if no longer needed. If a signifigantly large change is applied to the document it may be faster to reset the index before applying it, doing an unindxed diff afterwards and then reenable the index.

§Arguments

Note: before and after do not have to be chronological. Document state can move backward. Normal use might look like:

§Example
use automerge::{ AutoCommit };

let mut doc = AutoCommit::new(); // or AutoCommit::load(data)
// make some changes - use and update the index
let heads = doc.get_heads();
let diff_cursor = doc.diff_cursor();
let patches = doc.diff(&diff_cursor, &heads);
doc.update_diff_cursor();

See Self::diff_incremental() for encapsulating this pattern.

source

pub fn diff_incremental(&mut self) -> Vec<Patch>

This is a convience function that encapsulates the following common pattern

use automerge::AutoCommit;
let mut doc = AutoCommit::new();
// make some changes
let heads = doc.get_heads();
let diff_cursor = doc.diff_cursor();
let patches = doc.diff(&diff_cursor, &heads);
doc.update_diff_cursor();
source

pub fn fork(&mut self) -> Self

source

pub fn fork_at(&mut self, heads: &[ChangeHash]) -> Result<Self, AutomergeError>

source

pub fn with_actor(self, actor: ActorId) -> Self

source

pub fn set_actor(&mut self, actor: ActorId) -> &mut Self

source

pub fn get_actor(&self) -> &ActorId

source

pub fn isolate(&mut self, heads: &[ChangeHash])

source

pub fn integrate(&mut self)

source

pub fn load_incremental(&mut self, data: &[u8]) -> Result<usize, AutomergeError>

Load an incremental save of a document.

Unlike Self::load() this imports changes into an existing document. It will work with both the output of Self::save() and Self::save_incremental()

The return value is the number of ops which were applied, this is not useful and will change in future.

source

pub fn apply_changes( &mut self, changes: impl IntoIterator<Item = Change> ) -> Result<(), AutomergeError>

source

pub fn merge( &mut self, other: &mut AutoCommit ) -> Result<Vec<ChangeHash>, AutomergeError>

Takes all the changes in other which are not in self and applies them

source

pub fn save(&mut self) -> Vec<u8>

Save the entirety of this document in a compact form.

source

pub fn save_with_options(&mut self, options: SaveOptions) -> Vec<u8>

source

pub fn save_and_verify(&mut self) -> Result<Vec<u8>, AutomergeError>

Save the document and attempt to load it before returning - slow!

source

pub fn save_nocompress(&mut self) -> Vec<u8>

Save this document, but don’t run it through DEFLATE afterwards

source

pub fn save_incremental(&mut self) -> Vec<u8>

Save the changes since the last call to Self::save()

The output of this will not be a compressed document format, but a series of individual changes. This is useful if you know you have only made a small change since the last Self::save() and you want to immediately send it somewhere (e.g. you’ve inserted a single character in a text object).

source

pub fn save_after(&mut self, heads: &[ChangeHash]) -> Vec<u8>

Save everything which is not a (transitive) dependency of heads

source

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

source

pub fn get_last_local_change(&mut self) -> Option<&Change>

Get the last change made by this documents actor ID

source

pub fn get_changes(&mut self, have_deps: &[ChangeHash]) -> Vec<&Change>

source

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

source

pub fn get_changes_added<'a>(&mut self, other: &'a mut Self) -> Vec<&'a Change>

Get changes in other that are not in self

source

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

Get the current heads of the document.

This closes the transaction first, if one is in progress.

source

pub fn set_text_rep(&mut self, text_rep: TextRepresentation)

source

pub fn get_text_rep(&mut self) -> TextRepresentation

source

pub fn with_text_rep(self, text_rep: TextRepresentation) -> Self

source

pub fn commit(&mut self) -> Option<ChangeHash>

Commit any uncommitted changes

Returns None if there were no operations to commit

source

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

Commit the current operations with some options.

Returns None if there were no operations to commit

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

pub fn rollback(&mut self) -> usize

Remove any changes that have been made in the current transaction from the document

source

pub fn empty_change(&mut self, options: CommitOptions) -> ChangeHash

Generate an empty change

The main reason to do this is if you wish to create a “merge commit” which has all the current heads of the documents as dependencies but you have no new operations to create.

Because this structure is an “autocommit” there may actually be outstanding operations to submit. If this is the case this function will create two changes, one with the outstanding operations and a new one with no operations. The returned ChangeHash will always be the hash of the empty change.

source

pub fn sync(&mut self) -> impl SyncDoc + '_

An implementation of crate::sync::SyncDoc for this autocommit

This ensures that any outstanding transactions for this document are committed before taking part in the sync protocol

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 a transaction

Trait Implementations§

source§

impl Clone for AutoCommit

source§

fn clone(&self) -> AutoCommit

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AutoCommit

source§

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

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

impl Default for AutoCommit

An autocommit document with an inactive PatchLog

See AutoCommit

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl ReadDoc for AutoCommit

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 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<'a, O: AsRef<ExId>, R: RangeBounds<String> + 'a>( &'a self, obj: O, range: R ) -> MapRange<'a, R>

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

fn map_range_at<'a, O: AsRef<ExId>, R: RangeBounds<String> + 'a>( &'a self, obj: O, range: R, heads: &[ChangeHash] ) -> MapRange<'a, 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 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 get_marks<O: AsRef<ExId>>( &self, obj: O, index: usize, heads: Option<&[ChangeHash]> ) -> Result<MarkSet, AutomergeError>

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 hydrate<O: AsRef<ExId>>( &self, obj: O, heads: Option<&[ChangeHash]> ) -> Result<Value, 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 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 Transactable for AutoCommit

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 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. Read more
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, key: &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: AsRef<ExId>>( &mut self, text: O, index: usize ) -> Result<(), AutomergeError>

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<'a, O: AsRef<ExId>, I: IntoIterator<Item = BlockOrText<'a>>>( &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§

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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