diff options
| author | tv <tv@krebsco.de> | 2026-11-01 19:35:20 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-01 19:35:20 +0100 |
| commit | 1f3be9d059876f7f3994056b1ac8ce307628a52a (patch) | |
| tree | 8f1ca7e7d4b5c1b317d6191b44f0c580aa90acbc /src/document | |
| parent | a9838a756fa7730e887598592a2f1b5b294e68a7 (diff) | |
document: allow writes during rejoin
Diffstat (limited to 'src/document')
| -rw-r--r-- | src/document/file.rs | 5 | ||||
| -rw-r--r-- | src/document/replica.rs | 36 |
2 files changed, 28 insertions, 13 deletions
diff --git a/src/document/file.rs b/src/document/file.rs index 9efae42..3ea3afd 100644 --- a/src/document/file.rs +++ b/src/document/file.rs @@ -197,6 +197,11 @@ impl DocumentFile { /// What is due for a document of `epoch` at `heads`, written by this node's `actors` /// (see the module docs). + /// How many of this node's actors the file holds; None before it was written. + pub(crate) fn actors(&self) -> Option<usize> { + self.lock().as_ref().map(|written| written.actors) + } + pub(crate) fn due(&self, epoch: Epoch, actors: usize, heads: &[ChangeHash]) -> Due { let written = self.lock().clone(); let reason = match &written { diff --git a/src/document/replica.rs b/src/document/replica.rs index 61f2b3b..df2fafd 100644 --- a/src/document/replica.rs +++ b/src/document/replica.rs @@ -182,19 +182,7 @@ impl Replica { at: crate::clock::UnixMillis, f: impl FnOnce(&mut Transaction<'_>) -> Result<T, E>, ) -> Result<T, E> { - let mut transaction = self.doc.transaction(); - let out = f(&mut transaction); - match &out { - Ok(_) => { - transaction.commit_with( - automerge::transaction::CommitOptions::default().with_time(at.as_u64() as i64), - ); - } - Err(_) => { - transaction.rollback(); - } - } - out + transact(&mut self.doc, at, f) } } @@ -272,3 +260,25 @@ mod tests { assert_eq!(epoch(2, 0xab).to_string(), "2:abababababab"); } } + +/// The writes `f` makes to `doc`, as one change stamped `at` if it succeeds, dropped if +/// not ([`Replica::transact`], and writes into the document before a rejoin). +pub(crate) fn transact<T, E>( + doc: &mut Document, + at: crate::clock::UnixMillis, + f: impl FnOnce(&mut Transaction<'_>) -> Result<T, E>, +) -> Result<T, E> { + let mut transaction = doc.transaction(); + let out = f(&mut transaction); + match &out { + Ok(_) => { + transaction.commit_with( + automerge::transaction::CommitOptions::default().with_time(at.as_u64() as i64), + ); + } + Err(_) => { + transaction.rollback(); + } + } + out +} |
