diff options
| author | tv <tv@krebsco.de> | 2026-11-29 03:00:19 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-29 03:00:19 +0000 |
| commit | 2b4e99e1ed273da0f921eaec3e52300480acdf0a (patch) | |
| tree | 53e2e5e1882eb6ff3853c2f2e8fa9d91a8c16655 /src/document | |
| parent | 438b0ee5d1fe5d40f18e16d94904fc9bdd89783a (diff) | |
document: what a dropped change takes with it is the document's to say
document::dropping: a change a document holds and all that builds on it
(Tainted), and the document left without them, with what this node's
own writes among them touched and the others' changes (Dropped). The
walk of the history, the fork and the fresh actor were the node's, in
AppState::drop_taken, which keeps what is its own: whether a drop waits
for a second member's word, the quarantine, the refused.
Diffstat (limited to 'src/document')
| -rw-r--r-- | src/document/dropping.rs | 143 | ||||
| -rw-r--r-- | src/document/mod.rs | 1 |
2 files changed, 144 insertions, 0 deletions
diff --git a/src/document/dropping.rs b/src/document/dropping.rs new file mode 100644 index 0000000..56775fd --- /dev/null +++ b/src/document/dropping.rs @@ -0,0 +1,143 @@ +//! A document without a change it holds and every change built on it: what goes +//! (`Tainted`), and the document that is left (`Tainted::dropped`), forked where none of +//! them is, written by a fresh actor, as the old one's later changes are among those +//! dropped. + +use std::collections::HashSet; + +use super::{ActorId, Change, ChangeHash, Document, ExpandedChange}; + +/// Why a change isn't dropped. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Kept { + /// It is the epoch's root: what the document is. + Root, + /// Nothing would be left. + Nothing, +} + +/// A change a document holds and all that builds on it. +pub(crate) struct Tainted { + root: ChangeHash, + changes: Vec<Change>, + tainted: HashSet<ChangeHash>, +} + +/// The document left once what a `Tainted` names is dropped, and what went. +pub(crate) struct Dropped { + pub(crate) doc: Document, + /// The fresh actor `doc` is written by. + pub(crate) actor: ActorId, + /// What this node's own writes among those dropped touched. + pub(crate) own_paths: Vec<Vec<String>>, + /// The others' changes dropped. + pub(crate) theirs: Vec<Theirs>, +} + +/// Another node's change dropped: who made it, for whom, and whether it is the one +/// dropped or builds on it. +pub(crate) struct Theirs { + pub(crate) change: Change, + pub(crate) committer: Option<String>, + pub(crate) user: Option<String>, + pub(crate) root: bool, +} + +impl Tainted { + /// `root` and what builds on it in `doc`; none if `doc` doesn't hold it; why it is + /// kept, where it is. + pub(crate) fn of(doc: &Document, root: ChangeHash) -> Option<Result<Tainted, Kept>> { + let changes: Vec<Change> = doc.get_changes(&[]).into_iter().collect(); + let dropped = changes.iter().find(|change| change.hash() == root)?; + if dropped.deps().is_empty() { + return Some(Err(Kept::Root)); + } + // In causal order: a change after those it builds on. + let mut tainted: HashSet<ChangeHash> = [root].into(); + for change in &changes { + if change.deps().iter().any(|dep| tainted.contains(dep)) { + tainted.insert(change.hash()); + } + } + if tainted.len() == changes.len() { + return Some(Err(Kept::Nothing)); + } + Some(Ok(Tainted { + root, + changes, + tainted, + })) + } + + /// How many changes go. + pub(crate) fn len(&self) -> usize { + self.tainted.len() + } + + /// How many changes the document holds, those that go among them. + pub(crate) fn of_all(&self) -> usize { + self.changes.len() + } + + /// `doc` without them: this node's own writes among them by the actors it wrote + /// under (`own`, and the one `doc` writes with). + pub(crate) fn dropped(self, doc: &Document, own: Vec<ActorId>) -> anyhow::Result<Dropped> { + let kept = || { + self.changes + .iter() + .filter(|change| !self.tainted.contains(&change.hash())) + }; + let built_on: HashSet<ChangeHash> = kept() + .flat_map(|change| change.deps().iter().copied()) + .collect(); + let heads: Vec<ChangeHash> = kept() + .map(|change| change.hash()) + .filter(|hash| !built_on.contains(hash)) + .collect(); + let mut actors = own; + actors.push(doc.get_actor().clone()); + let gone = || { + self.changes + .iter() + .filter(|change| self.tainted.contains(&change.hash())) + }; + let ours: Vec<ExpandedChange> = gone() + .filter(|change| actors.contains(change.actor_id())) + .map(|change| change.decode()) + .collect(); + let mut own_paths: Vec<Vec<String>> = super::inspect::Inspector::new(doc) + .ops(&ours) + .into_iter() + .flatten() + .map(|op| op.path) + .collect(); + own_paths.sort(); + own_paths.dedup(); + let actor = ActorId::random(); + let fresh = doc.fork_at(&heads)?.with_actor(actor.clone()); + let theirs = gone() + .filter(|change| !actors.contains(change.actor_id())) + .map(|change| Theirs { + change: change.clone(), + committer: doc + .get_author_for_actor(change.actor_id()) + .and_then(|author| { + std::str::from_utf8(author.as_bytes()) + .ok() + .map(str::to_owned) + }), + user: change + .message() + .and_then(super::replica::user_of) + .map(str::to_owned), + root: change.hash() == self.root, + }) + .collect(); + Ok(Dropped { + doc: fresh, + actor, + own_paths, + theirs, + }) + } +} diff --git a/src/document/mod.rs b/src/document/mod.rs index fd44331..1c39de3 100644 --- a/src/document/mod.rs +++ b/src/document/mod.rs @@ -57,6 +57,7 @@ pub(crate) mod at; #[cfg(test)] pub(crate) mod bench; pub(crate) mod changes; +pub(crate) mod dropping; #[cfg(feature = "federation")] pub(crate) mod events; pub(crate) mod file; |
