diff options
Diffstat (limited to 'src/federation/store')
| -rw-r--r-- | src/federation/store/memory.rs | 31 | ||||
| -rw-r--r-- | src/federation/store/mod.rs | 3 | ||||
| -rw-r--r-- | src/federation/store/testfold.rs | 2 |
3 files changed, 34 insertions, 2 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs index 8493735..05479e3 100644 --- a/src/federation/store/memory.rs +++ b/src/federation/store/memory.rs @@ -263,8 +263,8 @@ impl<F: Fold> Store<F> { ordered } - /// What a new event builds on: the events nothing else held builds on, or the - /// checkpoint's heads where nothing came after it. + /// The heads of what the store holds: the events nothing else held builds on, in the + /// order taken, or the checkpoint's heads where nothing came after it. pub(crate) fn heads(&self) -> Vec<Id> { let built_on: HashSet<&Id> = self .events @@ -284,6 +284,15 @@ impl<F: Fold> Store<F> { } } + /// What a new event builds on: its heads, as many as an event may name + /// (`Fold::DEPENDENCIES`), the earliest taken first, so that what is written merges + /// what stayed apart the longest. + pub(crate) fn building_on(&self) -> Vec<Id> { + let mut heads = self.heads(); + heads.truncate(F::DEPENDENCIES); + heads + } + /// Offers an event: it is taken once the store holds its dependencies, where the fold /// takes it at its history. pub(crate) fn offer(&mut self, bytes: Vec<u8>) -> Offered { @@ -1139,6 +1148,24 @@ mod tests { assert!(store.drop_taken(&[lock]).is_err()); } + /// A new event builds on as many heads as an event may name, the earliest taken first: + /// a later one merges the rest. + #[test] + fn a_new_event_builds_on_the_earliest_heads_an_event_may_name() { + let mut store = store(); + let root = store.id(); + let apart: Vec<Id> = (0..=TestFold::DEPENDENCIES) + .map(|n| taken(store.offer(testfold::put(&[root], &format!("k{n}"), "1")))) + .collect(); + assert_eq!(store.heads(), apart); + assert_eq!(store.building_on(), apart[..TestFold::DEPENDENCIES]); + let merged = taken(store.offer(testfold::put(&store.building_on(), "m", "1"))); + assert_eq!( + store.building_on(), + vec![apart[TestFold::DEPENDENCIES], merged] + ); + } + /// Heads leave out what is neither among them, in their history, nor above them. #[test] fn heads_leave_out_what_is_beside_them() { diff --git a/src/federation/store/mod.rs b/src/federation/store/mod.rs index c80f5b8..d2028da 100644 --- a/src/federation/store/mod.rs +++ b/src/federation/store/mod.rs @@ -79,6 +79,9 @@ pub(crate) trait Fold { /// The most bytes an event may have: larger ones are none. const LARGEST: usize; + /// The most events an event may build on: what a new event builds on is no more. + const DEPENDENCIES: usize = usize::MAX; + /// What the event builds on; `None` where its bytes are none of this fold's events. fn dependencies(event: &[u8]) -> Option<Vec<Id>>; diff --git a/src/federation/store/testfold.rs b/src/federation/store/testfold.rs index 17e039b..6dd1460 100644 --- a/src/federation/store/testfold.rs +++ b/src/federation/store/testfold.rs @@ -295,6 +295,8 @@ impl Fold for TestFold { const LARGEST: usize = 64 * 1024; + const DEPENDENCIES: usize = 8; + fn dependencies(event: &[u8]) -> Option<Vec<Id>> { decode(event).map(|(deps, _)| deps) } |
