Skip to content

Commit

Permalink
bump to automerge 0.5.0, re-add save decision
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Jul 30, 2023
1 parent 3fcf21d commit 60e7095
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
39 changes: 28 additions & 11 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChangeHash>,
}

impl DocumentInfo {
Expand All @@ -566,13 +568,18 @@ impl DocumentInfo {
document: Arc<RwLock<SharedDocument>>,
handle_count: Arc<AtomicUsize>,
) -> Self {
let last_heads = {
let doc = document.read();
doc.automerge.get_heads()
};
DocumentInfo {
state,
document,
handle_count,
sync_states: Default::default(),
change_observers: Default::default(),
patches_since_last_save: 0,
last_heads,
}
}

Expand Down Expand Up @@ -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>) {
Expand All @@ -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) => {
Expand All @@ -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,
Expand Down

0 comments on commit 60e7095

Please sign in to comment.