From dde49ea3920d5a14f42706addffeb3f2c8b44e6f Mon Sep 17 00:00:00 2001 From: gterzian <2792687+gterzian@users.noreply.github.com> Date: Fri, 30 Jun 2023 16:30:27 +0800 Subject: [PATCH 1/2] bump to automerge 0.5.0, re-add save decision --- Cargo.toml | 2 +- src/repo.rs | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7ef45bd6..a36734e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ tokio = ["dep:tokio", "dep:tokio-util"] tokio-tungstenite = ["tokio", "dep:tokio-tungstenite", "dep:tungstenite"] [dependencies] -automerge = { version = "0.4.1" } +automerge = { version = "0.5.0" } uuid = { version = "1.2.2"} crossbeam-channel = { version = "0.5.8" } parking_lot = { version = "0.12.1" } diff --git a/src/repo.rs b/src/repo.rs index 8626465f..5ebf5a53 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -558,6 +558,8 @@ pub(crate) struct DocumentInfo { /// Counter of patches since last save, /// used to make decisions about full or incemental saves. patches_since_last_save: usize, + /// Last heads obtained from the automerge doc. + last_heads: Vec, } impl DocumentInfo { @@ -566,6 +568,10 @@ impl DocumentInfo { document: Arc>, handle_count: Arc, ) -> Self { + let last_heads = { + let doc = document.read(); + doc.automerge.get_heads() + }; DocumentInfo { state, document, @@ -573,6 +579,7 @@ impl DocumentInfo { sync_states: Default::default(), change_observers: Default::default(), patches_since_last_save: 0, + last_heads, } } @@ -688,8 +695,14 @@ impl DocumentInfo { /// Count patches since last save, /// returns whether there were any. fn note_changes(&mut self) -> bool { - // TODO: count patches somehow. - true + let count = { + let doc = self.document.read(); + let changes = doc.automerge.get_changes(&self.last_heads); + changes.len() + }; + let has_patches = count > 0; + self.patches_since_last_save = self.patches_since_last_save.checked_add(count).unwrap_or(0); + has_patches } fn resolve_change_observers(&mut self, result: Result<(), RepoError>) { @@ -708,18 +721,21 @@ impl DocumentInfo { return; } let should_compact = self.patches_since_last_save > 10; - let storage_fut = if should_compact { - let to_save = { - let mut doc = self.document.write(); - doc.automerge.save() + let (storage_fut, new_heads) = if should_compact { + let (to_save, new_heads) = { + let doc = self.document.read(); + (doc.automerge.save(), doc.automerge.get_heads()) }; - storage.compact(document_id.clone(), to_save) + (storage.compact(document_id.clone(), to_save), new_heads) } else { - let to_save = { - let mut doc = self.document.write(); - doc.automerge.save_incremental() + let (to_save, new_heads) = { + let doc = self.document.read(); + ( + doc.automerge.save_after(&self.last_heads), + doc.automerge.get_heads(), + ) }; - storage.append(document_id.clone(), to_save) + (storage.append(document_id.clone(), to_save), new_heads) }; match self.state { DocState::Sync(ref mut futs) => { @@ -733,6 +749,7 @@ impl DocumentInfo { let waker = Arc::new(RepoWaker::Storage(wake_sender.clone(), document_id)); self.state.poll_pending_save(waker); self.patches_since_last_save = 0; + self.last_heads = new_heads; } /// Apply incoming sync messages, From bd1814558f61d4e688488c25ec3379583745001b Mon Sep 17 00:00:00 2001 From: gterzian <2792687+gterzian@users.noreply.github.com> Date: Sun, 30 Jul 2023 17:06:01 +0800 Subject: [PATCH 2/2] update autosurgeon --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a36734e0..8381ba47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,4 +53,4 @@ test-log = "0.2.12" env_logger = "0.10.0" tracing-subscriber = "0.3.17" itertools = "0.11.0" -autosurgeon = "0.7.1" +autosurgeon = "0.8.0"