aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/passing.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 04:05:41 +0000
committertv <tv@krebsco.de>2026-11-29 04:08:47 +0000
commit2a475153b715c954935ae46d3773ffbf3d007f03 (patch)
tree8ebe8a5d6cdaf9869822f95fe2ac7eca0c10cf7f /src/document/passing.rs
parent2ae7562083f5f96490f90e9398762cf6a024d51e (diff)
document: where a document stands, and a peer, is the document's to compare
document::passing: a Mark, where a document stood, by which what changed since and the node's own units since are told (changed_since, own_since: the change publisher, the sync worker); and of where a peer says it stands, what the document lacks, whether the peer holds what the document held, a pull of what is lacking, and what it has counting as sent no more (lacking, peer_holds, pull, heard: the relay, keepalives). The published stamp gives its summary and its mark.
Diffstat (limited to 'src/document/passing.rs')
-rw-r--r--src/document/passing.rs95
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);
+ }
+}