diff options
Diffstat (limited to 'src/federation/held.rs')
| -rw-r--r-- | src/federation/held.rs | 71 |
1 files changed, 64 insertions, 7 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index bb34407..51d8ef0 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -130,6 +130,13 @@ pub(crate) struct Voter { pub(crate) rules: Rules, } +/// A set of events as a node starts with it: held, from its file or begun from the +/// root; or still to be gathered of its peers, where the node was given neither. +pub(crate) enum Begun { + Set(Box<Set<Federation>>), + Start(super::store::passing::Start<Federation>), +} + /// What carries the store: a document it is read from, or a set of events, which is /// the store's own document (`store::set`). enum Carried { @@ -175,6 +182,9 @@ impl Carried { struct Inner { /// None until a document holding a store holds its root. store: Option<Carried>, + /// A set of events to be, where the node started with nothing of its store and + /// gathers what its set begins from of its peers (`store::passing::Start`). + starting: Option<super::store::passing::Start<Federation>>, /// The document read from (`Replica::epoch_seq`), and its events read. epoch_seq: Option<u32>, read: HashSet<Id>, @@ -1040,18 +1050,65 @@ impl Holding { self } - /// Itself, its store carried by `set` and kept in the file at `path`, where the node - /// is given a set. + /// Itself, its store to be carried by a set of events it has nothing of yet: what + /// `start` gathers of its peers (`swarm::set`). + pub(crate) fn awaiting(mut self, start: super::store::passing::Start<Federation>) -> Self { + self.of_a_set = true; + self.inner.lock_or_recover().starting = Some(start); + self + } + + /// The names a node that started with nothing asks a peer for, to begin its set + /// from; none, once it holds one. + pub(crate) fn start_wants(&self) -> Vec<crate::document::ranges::Name> { + self.inner + .lock_or_recover() + .starting + .as_ref() + .map(|start| start.wants()) + .unwrap_or_default() + } + + /// Takes what a peer sent a node that started with nothing. Whether it holds its + /// set by it. + pub(crate) fn start_take(&self, units: Vec<super::store::passing::Unit>) -> bool { + let mut inner = self.inner.lock_or_recover(); + let Some(set) = inner.starting.as_mut().and_then(|start| start.take(units)) else { + return false; + }; + info!(store = %set.store().id(), "Begun the store's set from what peers gave by name"); + inner.starting = None; + inner.store = Some(Carried::Set(Box::new(set))); + self.settle(&mut inner); + true + } + + /// The store the node holds, or is to hold once it has gathered its set: by its ID. + pub(crate) fn store_id(&self) -> Option<Id> { + let inner = self.inner.lock_or_recover(); + inner + .store + .as_ref() + .map(|store| store.id()) + .or_else(|| inner.starting.as_ref().map(|start| start.store())) + } + + /// Itself, its store carried by the set `begun`, held or to be, and kept in the + /// file at `path`; itself, where the node is given no set. pub(crate) fn carrying_if( self, - set: Option<Set<Federation>>, + begun: Option<Begun>, path: Option<PathBuf>, files: Arc<Files>, ) -> Self { - match (set, path) { - (Some(set), Some(path)) => self.carrying(set).kept_in(path, files), - (Some(set), None) => self.carrying(set), - (None, _) => self, + let holding = match begun { + Some(Begun::Set(set)) => self.carrying(*set), + Some(Begun::Start(start)) => self.awaiting(start), + None => return self, + }; + match path { + Some(path) => holding.kept_in(path, files), + None => holding, } } |
