diff options
Diffstat (limited to 'src/federation/held.rs')
| -rw-r--r-- | src/federation/held.rs | 99 |
1 files changed, 80 insertions, 19 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index 5595a08..ce97788 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -134,8 +134,10 @@ struct Inner { /// What a document holds that its store wasn't offered from it. pub(crate) struct Unread { epoch_seq: u32, - /// The root, where the store isn't started yet. + /// The root, and the checkpoint a remake's snapshot holds, if any, where the store + /// isn't started yet. root: Option<Vec<u8>>, + checkpoint: Option<Vec<u8>>, events: BTreeMap<Id, Vec<u8>>, } @@ -204,19 +206,22 @@ impl Holding { .filter(|id| !same || !inner.read.contains(id)) .filter_map(|id| Some((id, events::bytes(doc, &id)?))) .collect(); - let root = match &inner.store { - None => Some(events::bytes(doc, &swarm)?), - Some(_) => None, + let (root, checkpoint) = match &inner.store { + None => (Some(events::bytes(doc, &swarm)?), events::checkpoint(doc)), + Some(_) => (None, None), }; Some(Unread { epoch_seq, root, + checkpoint, events, }) } - /// Offers the store what was `unread`: started from the root first, by what the node - /// kept of its own. What it refuses of its own from then on, it keeps. + /// Offers the store what was `unread`: started first, by what the node kept of its + /// own, from the checkpoint its operator pinned, found in the document; else from the + /// document's checkpoint, where a remake's snapshot holds one; else from the root. + /// What it refuses of its own from then on, it keeps. pub(crate) fn read(&self, unread: Unread) { let mut inner = self.inner.lock_or_recover(); let inner = &mut *inner; @@ -230,20 +235,38 @@ impl Holding { }; let id = Id::of(&root); let (own, dropped) = inner.kept.ids().unwrap_or_default(); + // What a checkpoint keeps follows it in the document: a fork's root's too. + let kept = |checkpoint: &[u8]| -> Option<Vec<Vec<u8>>> { + Federation::parts(checkpoint) + .map_or_else(Vec::new, |(_, keeps)| keeps) + .iter() + .map(|keep| unread.events.get(keep).cloned()) + .collect() + }; let started = match self.pin { - // A fork's root keeps events, which follow it in the document. - None => { - let keeps = Federation::parts(&root).map_or_else(Vec::new, |(_, keeps)| keeps); - let kept: Option<Vec<Vec<u8>>> = keeps - .iter() - .map(|keep| unread.events.get(keep).cloned()) - .collect(); - let Some(kept) = kept else { - return; - }; - Store::new(root, kept) - } - Some(pin) => match found(&pin, &root, &unread.events) { + None => match &unread.checkpoint { + Some(checkpoint) => { + let Some(kept) = kept(checkpoint) else { + return; + }; + Store::pinned(id, checkpoint.clone(), kept) + } + None => { + let Some(kept) = kept(&root) else { + return; + }; + Store::new(root, kept) + } + }, + Some(pin) => match found(&pin, &root, &unread.events).or_else(|| { + let checkpoint = unread + .checkpoint + .clone() + .filter(|checkpoint| Id::of(checkpoint) == pin)?; + let kept = kept(&checkpoint)?; + let kept = kept.into_iter().map(|bytes| (Id::of(&bytes), bytes)); + Some((checkpoint, kept.collect())) + }) { Some((checkpoint, kept)) => { info!(store = %id, checkpoint = %pin, "Found the pinned checkpoint in the document"); Store::pinned(id, checkpoint, kept.into_values().collect()) @@ -918,6 +941,44 @@ mod tests { let elsewhere = Holding::open(None, files, Some(Id::of(b"elsewhere")), None).unwrap(); read(&elsewhere, &doc, 0); assert!(elsewhere.with(|_| ()).is_none()); + + // A document holding that checkpoint, as a remake's snapshot does, with what it + // keeps and what came after it, and none of what came before: a node starts there, + // and so does one pinning it. + let (checkpoint, after) = from_root + .with(|store| { + let mut after: BTreeMap<Id, Vec<u8>> = store + .kept() + .into_iter() + .map(|bytes| (Id::of(&bytes), bytes)) + .collect(); + for id in store.taken() { + after.insert(*id, store.get(id).unwrap().to_vec()); + } + (store.checkpoint().to_vec(), after) + }) + .unwrap(); + let mut snapshot = events::started(Federation::NAME, &root, &[]).unwrap(); + let mut write = snapshot.transaction(); + store::write_val_path( + &mut write, + &meta::path(&[meta::CHECKPOINT]), + &crate::document::typed::Val::Scalar(ScalarValue::Bytes(checkpoint)), + store::Writing::sticky(Default::default()), + ) + .unwrap(); + write.commit(); + for event in after.values() { + put(&mut snapshot, event); + } + for holding in [ + Holding::default(), + Holding::open(None, Arc::new(Files::default()), Some(pin), None).unwrap(), + ] { + read(&holding, &snapshot, 0); + assert_eq!(holding.with(|store| Id::of(store.checkpoint())), Some(pin)); + assert_eq!(taken(&holding), taken(&from_root)); + } } /// An event put at the API is offered to the store; what the operator drops is |
