diff options
| author | tv <tv@krebsco.de> | 2026-11-05 16:12:30 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-05 16:12:30 +0100 |
| commit | 250f99172058f733de3215703173115695297878 (patch) | |
| tree | 85279d13cdedf924ed8ec563c9fc906922758f3a /src/document/changes.rs | |
| parent | 63acf8447f7702a6d083b8ed602eafdf2ff7f6d9 (diff) | |
one walk since
Diffstat (limited to 'src/document/changes.rs')
| -rw-r--r-- | src/document/changes.rs | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/src/document/changes.rs b/src/document/changes.rs index 9a07fc0..7ee1a4b 100644 --- a/src/document/changes.rs +++ b/src/document/changes.rs @@ -43,39 +43,58 @@ pub(crate) fn missing(doc: &impl ReadDoc, heads: &[ChangeHash]) -> Vec<ChangeHas .collect() } -/// When the changes after `since` were committed, in Unix milliseconds by their -/// writers' clocks (`Replica::transact`); changes without a time (snapshots) left out. -pub(crate) fn commit_times(doc: &Document, since: &[ChangeHash]) -> Vec<i64> { - doc.get_changes(since) - .iter() - .map(|change| change.timestamp()) - .filter(|&time| time > 0) - .collect() +/// The changes after `since`, walked once: their hashes; who wrote them (the authors +/// their writers set, a node's ID, `replica::authored`), each once, with the hashes of +/// their changes, and whether any has none (a snapshot, or a change of a node that set +/// none); and when they were committed, in Unix milliseconds by their writers' clocks +/// (`Replica::transact`), changes without a time (snapshots) left out. +pub(crate) struct Since { + pub(crate) hashes: Vec<ChangeHash>, + pub(crate) authors: Vec<(String, Vec<ChangeHash>)>, + pub(crate) unauthored: bool, + pub(crate) times: Vec<i64>, } -/// Who wrote the changes after `since`: the authors their writers set (a node's ID, -/// `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; +pub(crate) fn since(doc: &Document, since: &[ChangeHash]) -> Since { + let mut found = Since { + hashes: Vec::new(), + authors: Vec::new(), + unauthored: false, + times: Vec::new(), + }; for change in doc.get_changes_meta(since) { + found.hashes.push(change.hash); + if change.timestamp > 0 { + found.times.push(change.timestamp); + } match change .author .as_ref() .and_then(|author| std::str::from_utf8(author.as_bytes()).ok()) { - Some(author) => match authors.iter_mut().find(|(known, _)| known == author) { + Some(author) => match found.authors.iter_mut().find(|(known, _)| known == author) { Some((_, hashes)) => hashes.push(change.hash), - None => authors.push((author.to_owned(), vec![change.hash])), + None => found.authors.push((author.to_owned(), vec![change.hash])), }, - None => unknown = true, + None => found.unauthored = true, } } - (authors, unknown) + found +} + +/// When the changes after `since` were committed (`Since::times`). +pub(crate) fn commit_times(doc: &Document, since: &[ChangeHash]) -> Vec<i64> { + self::since(doc, since).times +} + +/// Who wrote the changes after `since` (`Since::authors`, `Since::unauthored`). +#[cfg(test)] +pub(crate) fn authors_since( + doc: &Document, + since: &[ChangeHash], +) -> (Vec<(String, Vec<ChangeHash>)>, bool) { + let found = self::since(doc, since); + (found.authors, found.unauthored) } /// The change `hash` as the document encodes it; None if `doc` doesn't have it. |
