diff options
Diffstat (limited to 'src/document/passing.rs')
| -rw-r--r-- | src/document/passing.rs | 95 |
1 files changed, 94 insertions, 1 deletions
diff --git a/src/document/passing.rs b/src/document/passing.rs index 10b888d..0b9a3dd 100644 --- a/src/document/passing.rs +++ b/src/document/passing.rs @@ -11,7 +11,7 @@ use super::replica::Replica; use super::signatures; use super::sync::{Received, SyncSession}; use crate::swarm::throughput::SyncBytes; -use crate::transport::{DeltaSignatures, DeltaSpec, SyncBody, SyncPayload}; +use crate::transport::{DeltaSignatures, DeltaSpec, HeadsSummary, SyncBody, SyncPayload}; /// What goes to a peer next. pub(crate) struct Outgoing { @@ -454,3 +454,96 @@ pub(crate) fn take_bulk( }), ) } + +/// Where a document stood, kept to tell later what came since (`mark`): its heads then. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub(crate) struct Mark(Vec<ChangeHash>); + +impl Mark { + /// Standing at `heads`. + pub(super) fn at(heads: &[ChangeHash]) -> Mark { + Mark(heads.to_vec()) + } +} + +/// Where the document `replica` holds stands now. +pub(crate) fn mark(replica: &Replica) -> Mark { + Mark(replica.heads().to_vec()) +} + +/// What changed in the document `replica` holds since `since`: where it stands now, the +/// paths that changed, and those of them where writes at once made a conflict. After +/// another document took its place (an epoch switch, a rejoin, a drop), the root alone, +/// which everything is within. +pub(crate) fn changed_since( + replica: &Replica, + since: &Mark, +) -> (Mark, Vec<Vec<String>>, Vec<Vec<String>>) { + let now = mark(replica); + if now == *since { + return (now, Vec::new(), Vec::new()); + } + if !super::changes::missing(&replica.doc, &since.0).is_empty() { + return (now, vec![Vec::new()], Vec::new()); + } + let mut paths = Vec::new(); + let mut conflicts = Vec::new(); + for changed in replica.changed_since(&since.0, super::store::InLists::Index) { + if changed.conflict { + conflicts.push(changed.path.clone()); + } + paths.push(changed.path); + } + (now, paths, conflicts) +} + +/// The units the node `node` made itself since `seen` (which becomes where the document +/// stands now), if what came since is a write of its own; None if none of it is (a +/// change fetched as a blob), or another document took its place meanwhile. +pub(crate) fn own_since( + replica: &Replica, + seen: &mut Mark, + node: &str, +) -> Option<Vec<super::UnitId>> { + let before = std::mem::replace(seen, mark(replica)); + if !super::changes::holds_all(&replica.doc, &before.0) { + return None; + } + super::changes::since(&replica.doc, &before.0) + .authors + .into_iter() + .find(|(author, _)| author == node) + .map(|(_, hashes)| hashes.into_iter().map(Into::into).collect()) +} + +/// Of where a peer says it stands (`theirs`), what the document lacks: with a digest, +/// of the sample it lists. +pub(crate) fn lacking(doc: &super::Document, theirs: &HeadsSummary) -> Vec<super::UnitId> { + super::changes::missing(doc, theirs.listed()) + .into_iter() + .map(Into::into) + .collect() +} + +/// Whether a peer standing at `theirs` holds all the document held at `ours`. +pub(crate) fn peer_holds(doc: &super::Document, theirs: &HeadsSummary, ours: &Mark) -> bool { + super::changes::covers(doc, theirs.listed(), &ours.0) +} + +/// Has the session ask the peer for what the document lacks of where it stands +/// (`theirs`, `lacking` of it): all of it, where the peer listed all; else what is +/// known to lack. +pub(crate) fn pull(session: &mut SyncSession, theirs: &HeadsSummary, lacking: Vec<super::UnitId>) { + session.pull_from(match theirs.heads() { + Some(heads) => heads.to_vec(), + None => lacking.into_iter().map(Into::into).collect(), + }); +} + +/// A peer said where it stands, outside a session's messages: what it has counts as +/// sent to it no more (`SyncSession::heard_heads`). Nothing where it gave a digest. +pub(crate) fn heard(session: &mut SyncSession, doc: &super::Document, theirs: &HeadsSummary) { + if let Some(heads) = theirs.heads() { + session.heard_heads(doc, heads); + } +} |
