diff options
| author | tv <tv@krebsco.de> | 2026-11-05 01:23:40 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-05 01:23:40 +0100 |
| commit | 889e23ee1eb8a209efe7c1eae86aa39091560149 (patch) | |
| tree | 8da04d6d62f9a1647f90b6ea459eba62b91f5a12 /src/document/changes.rs | |
| parent | 0d16147e95211b0829fddaefb0d014fe0c48dbc8 (diff) | |
covered neighbours have it
Diffstat (limited to 'src/document/changes.rs')
| -rw-r--r-- | src/document/changes.rs | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/src/document/changes.rs b/src/document/changes.rs index 0409c50..19d4db6 100644 --- a/src/document/changes.rs +++ b/src/document/changes.rs @@ -28,10 +28,13 @@ pub(crate) fn commit_times(doc: &Document, since: &[ChangeHash]) -> Vec<i64> { } /// Who wrote the changes after `since`: the authors their writers set (a node's ID, -/// `replica::authored`), each once; and whether any change has none (a snapshot, or a -/// change of a node that set none). -pub(crate) fn authors_since(doc: &Document, since: &[ChangeHash]) -> (Vec<String>, bool) { - let mut authors: Vec<String> = Vec::new(); +/// `replica::authored`), each once, with the hashes of their changes; and whether any +/// change has none (a snapshot, or a change of a node that set none). +pub(crate) fn authors_since( + doc: &Document, + since: &[ChangeHash], +) -> (Vec<(String, Vec<ChangeHash>)>, bool) { + let mut authors: Vec<(String, Vec<ChangeHash>)> = Vec::new(); let mut unknown = false; for change in doc.get_changes_meta(since) { match change @@ -39,10 +42,10 @@ pub(crate) fn authors_since(doc: &Document, since: &[ChangeHash]) -> (Vec<String .as_ref() .and_then(|author| std::str::from_utf8(author.as_bytes()).ok()) { - Some(author) if !authors.iter().any(|known| known == author) => { - authors.push(author.to_owned()); - } - Some(_) => {} + Some(author) => match authors.iter_mut().find(|(known, _)| known == author) { + Some((_, hashes)) => hashes.push(change.hash), + None => authors.push((author.to_owned(), vec![change.hash])), + }, None => unknown = true, } } @@ -221,7 +224,11 @@ mod tests { merged.merge(&mut b).unwrap(); let (mut authors, unknown) = authors_since(&merged, &since); authors.sort(); - assert_eq!(authors, ["node-a", "node-b"]); + let counts: Vec<(&str, usize)> = authors + .iter() + .map(|(author, hashes)| (author.as_str(), hashes.len())) + .collect(); + assert_eq!(counts, [("node-a", 2), ("node-b", 1)]); assert!(!unknown); // A change no node authored (a snapshot's, say). |
