From 6105507200231e67ba544c12aa10b988e1c23826 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 19:53:27 +0100 Subject: use Automerge as Document instead of AutoCommit This allows a lot of operations to drop mut. --- src/remake/switch.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/remake/switch.rs') diff --git a/src/remake/switch.rs b/src/remake/switch.rs index 9aae748..e60c942 100644 --- a/src/remake/switch.rs +++ b/src/remake/switch.rs @@ -219,10 +219,10 @@ async fn switch( // 2. Build (or reuse), verify and replay without holding any lock. let (new_doc, copy_heads, mut stats) = tokio::task::spawn_blocking( move || -> Result<(Document, Vec, ReplayStats), String> { - let mut copy = copy; + let copy = copy; let snapshot = match prebuilt { Some(snapshot) => snapshot, - None => snapshot::build(&mut copy, &request.params()) + None => snapshot::build(©, &request.params()) .map_err(|error| format!("snapshot build failed: {error}"))?, }; if snapshot.epoch != expected { @@ -235,13 +235,20 @@ async fn switch( // A fresh actor: this node's writes in the new epoch. let mut doc = snapshot.doc.with_actor(actor); + // The carried record and the replayed writes go into one change. + let heads = copy.get_heads(); + let paths = delta::changed_paths(©, &base, &heads); + let mut transaction = doc.transaction(); + // Keep this remake's record visible in the new epoch, for its history. - if carry_record && let Err(error) = store::copy_path(©, &mut doc, &record_path) { + if carry_record + && let Err(error) = store::copy_path(©, &mut transaction, &record_path) + { warn!(error = %error, "Could not carry the remake record into the new epoch"); } - let heads = copy.get_heads(); - let stats = delta::replay(&mut copy, &base, &heads, &mut doc, &translator); + let stats = delta::replay_paths(©, &mut transaction, &paths, &translator); + transaction.commit(); Ok((doc, heads, stats)) }, ) -- cgit v1.3.1