//! 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, tainted: HashSet, } /// 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>, /// The others' changes dropped. pub(crate) theirs: Vec, } /// 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, pub(crate) user: Option, 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> { let changes: Vec = 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 = [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) -> anyhow::Result { let kept = || { self.changes .iter() .filter(|change| !self.tainted.contains(&change.hash())) }; let built_on: HashSet = kept() .flat_map(|change| change.deps().iter().copied()) .collect(); let heads: Vec = 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 = gone() .filter(|change| actors.contains(change.actor_id())) .map(|change| change.decode()) .collect(); let mut own_paths: Vec> = 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, }) } }