Skip to content

Commit c43a2c3

Browse files
committed
bump to automerge 0.5.0, re-add save decision
1 parent 2312e9e commit c43a2c3

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ path = "examples/tcp_example.rs"
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14-
automerge = { version = "0.4.1" }
14+
automerge = { version = "0.5.0" }
1515
uuid = { version = "1.2.2"}
1616
crossbeam-channel = { version = "0.5.8" }
1717
parking_lot = { version = "0.12.1" }

src/repo.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::dochandle::{DocHandle, SharedDocument};
22
use crate::interfaces::{DocumentId, RepoId};
33
use crate::interfaces::{NetworkError, RepoMessage, Storage, StorageError};
44
use automerge::sync::{Message as SyncMessage, State as SyncState, SyncDoc};
5-
use automerge::Automerge;
5+
use automerge::{Automerge, ChangeHash};
66
use core::pin::Pin;
77
use crossbeam_channel::{select, unbounded, Receiver, Sender};
88
use futures::future::Future;
@@ -527,6 +527,8 @@ pub(crate) struct DocumentInfo {
527527
/// Counter of patches since last save,
528528
/// used to make decisions about full or incemental saves.
529529
patches_since_last_save: usize,
530+
/// Last heads obtained from the automerge doc.
531+
last_heads: Vec<ChangeHash>,
530532
}
531533

532534
impl DocumentInfo {
@@ -535,13 +537,18 @@ impl DocumentInfo {
535537
document: Arc<RwLock<SharedDocument>>,
536538
handle_count: Arc<AtomicUsize>,
537539
) -> Self {
540+
let last_heads = {
541+
let doc = document.read();
542+
doc.automerge.get_heads()
543+
};
538544
DocumentInfo {
539545
state,
540546
document,
541547
handle_count,
542548
sync_states: Default::default(),
543549
change_observers: Default::default(),
544550
patches_since_last_save: 0,
551+
last_heads,
545552
}
546553
}
547554

@@ -663,8 +670,14 @@ impl DocumentInfo {
663670
/// Count patches since last save,
664671
/// returns whether there were any.
665672
fn note_changes(&mut self) -> bool {
666-
// TODO: count patches somehow.
667-
true
673+
let count = {
674+
let doc = self.document.read();
675+
let changes = doc.automerge.get_changes(&self.last_heads);
676+
changes.len()
677+
};
678+
let has_patches = count > 0;
679+
self.patches_since_last_save = self.patches_since_last_save.checked_add(count).unwrap_or(0);
680+
has_patches
668681
}
669682

670683
fn resolve_change_observers(&mut self, result: Result<(), RepoError>) {
@@ -683,18 +696,21 @@ impl DocumentInfo {
683696
return;
684697
}
685698
let should_compact = self.patches_since_last_save > 10;
686-
let storage_fut = if should_compact {
687-
let to_save = {
688-
let mut doc = self.document.write();
689-
doc.automerge.save()
699+
let (storage_fut, new_heads) = if should_compact {
700+
let (to_save, new_heads) = {
701+
let doc = self.document.read();
702+
(doc.automerge.save(), doc.automerge.get_heads())
690703
};
691-
storage.compact(document_id.clone(), to_save)
704+
(storage.compact(document_id.clone(), to_save), new_heads)
692705
} else {
693-
let to_save = {
694-
let mut doc = self.document.write();
695-
doc.automerge.save_incremental()
706+
let (to_save, new_heads) = {
707+
let doc = self.document.read();
708+
(
709+
doc.automerge.save_after(&self.last_heads),
710+
doc.automerge.get_heads(),
711+
)
696712
};
697-
storage.append(document_id.clone(), to_save)
713+
(storage.append(document_id.clone(), to_save), new_heads)
698714
};
699715
self.state = match self.state {
700716
DocState::Sync(None) => DocState::Sync(Some(storage_fut)),
@@ -710,6 +726,7 @@ impl DocumentInfo {
710726
let waker = Arc::new(RepoWaker::Storage(wake_sender.clone(), document_id));
711727
self.state.poll_pending_save(waker);
712728
self.patches_since_last_save = 0;
729+
self.last_heads = new_heads;
713730
}
714731

715732
/// Apply incoming sync messages.

0 commit comments

Comments
 (0)