diff options
| author | tv <tv@krebsco.de> | 2026-11-29 12:50:10 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-29 12:50:10 +0000 |
| commit | a7ead170e08a316711601ec859163a6205ede6b8 (patch) | |
| tree | 87192b2582636bbdcc68a26e5ba16e54987def09 /src/federation/store | |
| parent | d7e3a6e4d2ce26dc4abb1be2e3417eb5415257a3 (diff) | |
federation: a store tells what it lets go of, where asked
With the bytes, and why (Store::telling, let_go, Gone): what a move to a
checkpoint forgot, the heads it holds by name alone among it; what its
history takes and the node doesn't, by what it took before or by its
operator's drop; what builds on something refused, unjudged. For what
carries the store, to pass on or keep a while; a store that isn't asked
keeps none of it, as before.
Diffstat (limited to 'src/federation/store')
| -rw-r--r-- | src/federation/store/memory.rs | 89 |
1 files changed, 83 insertions, 6 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs index d49fca0..289e5b4 100644 --- a/src/federation/store/memory.rs +++ b/src/federation/store/memory.rs @@ -39,6 +39,19 @@ pub(crate) enum Offered { Stopped, } +/// Why a store let go of an event whose bytes it had. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Gone { + /// A move to a checkpoint let it go: it came before the heads or is one of them, and + /// isn't kept. + Forgotten, + /// Its history takes it and the node doesn't: by what it took before, or as its + /// operator had it dropped. + Own, + /// It builds on one the store refuses, and wasn't judged. + BuiltOn(Id), +} + /// A checkpoint built: its bytes, and the events it keeps, by ID. pub(crate) type Built = (Vec<u8>, BTreeMap<Id, Vec<u8>>); @@ -83,6 +96,9 @@ pub(crate) struct Store<F: Fold> { stopped: Option<(Id, String)>, held_back: Vec<(Id, Event)>, holding_back: HashSet<Id>, + /// What the store let go of since it was last asked, with the bytes, where it tells + /// (`telling`): for what carries the store to pass on, or to keep a while. + let_go: Option<Vec<(Id, Vec<u8>, Gone)>>, } impl<F: Fold> Store<F> { @@ -129,6 +145,7 @@ impl<F: Fold> Store<F> { stopped: None, held_back: Vec::new(), holding_back: HashSet::new(), + let_go: None, }) } @@ -166,6 +183,24 @@ impl<F: Fold> Store<F> { self.fork(heads, spec).is_ok_and(|(built, _)| built == root) } + /// The store telling what it lets go of (`let_go`). + pub(crate) fn telling(mut self) -> Self { + self.let_go = Some(Vec::new()); + self + } + + /// What the store let go of since last asked, each with its bytes and why: nothing, + /// where it doesn't tell. + pub(crate) fn let_go(&mut self) -> Vec<(Id, Vec<u8>, Gone)> { + self.let_go.as_mut().map(std::mem::take).unwrap_or_default() + } + + fn tell(&mut self, id: Id, bytes: Vec<u8>, gone: Gone) { + if let Some(let_go) = self.let_go.as_mut() { + let_go.push((id, bytes, gone)); + } + } + /// Whether the store holds an event: taken after its checkpoint, or one of its heads. pub(crate) fn holds(&self, id: &Id) -> bool { self.events.contains_key(id) || self.heads.contains(id) @@ -512,9 +547,26 @@ impl<F: Fold> Store<F> { .copied(), ); self.checkpoint = checkpoint; - self.kept = kept; + let unkept = std::mem::replace(&mut self.kept, kept); + for (id, bytes) in unkept { + if !self.kept.contains_key(&id) { + self.tell(id, bytes, Gone::Forgotten); + } + } self.order.retain(|id| above.contains(id)); - self.events.retain(|id, _| above.contains(id)); + let below: Vec<Id> = self + .events + .keys() + .filter(|id| !above.contains(id)) + .copied() + .collect(); + for id in below { + if let Some(event) = self.events.remove(&id) + && before.contains(&id) + { + self.tell(id, event.bytes, Gone::Forgotten); + } + } let mut state = start.clone(); for id in &self.order { F::add(&mut state, id, &self.events[id].bytes); @@ -584,6 +636,7 @@ impl<F: Fold> Store<F> { && self.writes.values().any(|dropped| *dropped == writes) { self.writes.insert(id, writes); + self.tell(id, event.bytes, Gone::Own); return self.refuse(id, Refusal::Dropped); } if let Some(refused) = event @@ -592,6 +645,7 @@ impl<F: Fold> Store<F> { .find(|dependency| self.refused.contains_key(dependency)) { let refused = *refused; + self.tell(id, event.bytes, Gone::BuiltOn(refused)); return self.refuse(id, Refusal::BuiltOn(refused)); } let before = |dependency: &Id| { @@ -636,6 +690,7 @@ impl<F: Fold> Store<F> { return self.hold_back(id, event); } if !F::keeps(&self.state, &id, &event.bytes) { + self.tell(id, event.bytes, Gone::Own); return self.refuse(id, Refusal::Own); } F::add(&mut self.state, &id, &event.bytes); @@ -713,10 +768,15 @@ impl<F: Fold> Store<F> { if !order.is_empty() { self.order.retain(|id| !dropped.contains_key(id)); for id in &order { - if let Some(event) = self.events.remove(id) - && let Some(writes) = F::writes(&event.bytes) - { - self.writes.insert(*id, writes); + if let Some(event) = self.events.remove(id) { + if let Some(writes) = F::writes(&event.bytes) { + self.writes.insert(*id, writes); + } + let gone = match &dropped[id] { + Refusal::BuiltOn(on) => Gone::BuiltOn(*on), + _ => Gone::Own, + }; + self.tell(*id, event.bytes, gone); } } self.refused.extend(dropped); @@ -742,6 +802,7 @@ impl<F: Fold> Store<F> { if let Some(writes) = F::writes(&event.bytes) { self.writes.insert(stopped, writes); } + self.tell(stopped, event.bytes, Gone::Own); self.refuse(stopped, Refusal::Dropped); order.push(stopped); } @@ -840,6 +901,7 @@ impl<F: Fold> Store<F> { } } self.refused.insert(waiter, Refusal::BuiltOn(refused)); + self.tell(waiter, event.bytes, Gone::BuiltOn(refused)); todo.push(waiter); } } @@ -1416,4 +1478,19 @@ mod tests { assert!(matches!(store.offer(two), Offered::Taken(ids) if ids.len() == 2)); assert_eq!(store.heads(), vec![Id::of(&both)]); } + + #[test] + fn a_store_tells_what_it_lets_go_of_only_where_asked() { + let mut quiet = store(); + let root = quiet.id(); + taken(quiet.offer(testfold::era(&[root], 1, "one"))); + quiet.offer(testfold::era(&[root], 1, "other")); + assert!(quiet.let_go().is_empty()); + let mut telling = store().telling(); + taken(telling.offer(testfold::era(&[root], 1, "one"))); + let other = testfold::era(&[root], 1, "other"); + telling.offer(other.clone()); + assert_eq!(telling.let_go(), vec![(Id::of(&other), other, Gone::Own)]); + assert!(telling.let_go().is_empty()); + } } |
