diff --git a/native/aetherlight/src/action.rs b/native/aetherlight/src/action.rs index 582de0623..fb6fb9347 100644 --- a/native/aetherlight/src/action.rs +++ b/native/aetherlight/src/action.rs @@ -1638,7 +1638,7 @@ mod tests { .expect("the affordance document encodes"); let decoded: std::collections::BTreeMap<_, crate::Affordance> = rmp_serde::from_slice(&catalog).expect("the affordance document decodes"); - assert_eq!(decoded, *bench.kernel.state.affordance_catalog); + assert_eq!(decoded, *bench.kernel.state.affordance_catalog.as_map()); assert!(bench.kernel.state.full_state_digest().is_ok()); } diff --git a/native/aetherlight/src/docs.rs b/native/aetherlight/src/docs.rs index fff61f099..7ef189406 100644 --- a/native/aetherlight/src/docs.rs +++ b/native/aetherlight/src/docs.rs @@ -5,7 +5,7 @@ //! key's value as it stood before the transaction (first touch wins). `finish` //! turns that record into the keys whose value really changed, with their old //! values; `rollback` puts every touched key back. Reads see the plain map -//! through `Deref`, and there is no `DerefMut`, so a write cannot skip the log. +//! through inherent methods, and no method hands out the map, so a write cannot skip the log. //! The fields are private to this module for the same reason. use serde::{Serialize, Serializer}; @@ -13,6 +13,55 @@ use std::collections::BTreeMap; use std::collections::btree_map::Entry; use std::ops::Deref; +#[cfg(test)] +thread_local! { + /// Rows any `Docs` iterator has yielded on this thread, and entries any + /// `indexed` read has returned: the work a read did, counted where the + /// reads are owned so no reader can skip it. + static ROWS_READ: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// The rows read on this thread so far (test builds only). +#[cfg(test)] +pub(crate) fn rows_read() -> u64 { + ROWS_READ.with(|rows| rows.get()) +} + +#[cfg(test)] +fn count_rows(rows: u64) { + ROWS_READ.with(|count| count.set(count.get() + rows)); +} + +/// A partition iterator that counts every row it yields (test builds); in a +/// shipped build it is the map's own iterator. +pub(crate) struct Counted(I); + +impl Iterator for Counted { + type Item = I::Item; + fn next(&mut self) -> Option { + let item = self.0.next(); + #[cfg(test)] + if item.is_some() { + count_rows(1); + } + item + } + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } +} + +impl DoubleEndedIterator for Counted { + fn next_back(&mut self) -> Option { + let item = self.0.next_back(); + #[cfg(test)] + if item.is_some() { + count_rows(1); + } + item + } +} + /// A key whose value changed in a transaction, with the value it had before /// (`None` when the key was absent). The new value is whatever the map holds. pub(crate) type Changed = (K, Option); @@ -42,18 +91,11 @@ impl PartialEq for Docs { impl Eq for Docs {} -impl Deref for Docs { - type Target = BTreeMap; - fn deref(&self) -> &BTreeMap { - &self.map - } -} - impl<'a, K: Ord + Clone, V: Clone + PartialEq> IntoIterator for &'a Docs { type Item = (&'a K, &'a V); - type IntoIter = std::collections::btree_map::Iter<'a, K, V>; + type IntoIter = Counted>; fn into_iter(self) -> Self::IntoIter { - self.map.iter() + self.iter() } } @@ -65,7 +107,55 @@ impl Serialize for } } +impl std::ops::Index<&K> for Docs { + type Output = V; + fn index(&self, key: &K) -> &V { + &self.map[key] + } +} + impl Docs { + pub(crate) fn get(&self, key: &K) -> Option<&V> { + self.map.get(key) + } + + pub(crate) fn contains_key(&self, key: &K) -> bool { + self.map.contains_key(key) + } + + pub(crate) fn len(&self) -> usize { + self.map.len() + } + + pub(crate) fn is_empty(&self) -> bool { + self.map.is_empty() + } + + pub(crate) fn iter(&self) -> Counted> { + Counted(self.map.iter()) + } + + pub(crate) fn keys(&self) -> Counted> { + Counted(self.map.keys()) + } + + pub(crate) fn values(&self) -> Counted> { + Counted(self.map.values()) + } + + pub(crate) fn range>( + &self, + range: R, + ) -> Counted> { + Counted(self.map.range(range)) + } + + /// The plain map, for a test that compares a decoded partition to it. + #[cfg(test)] + pub(crate) fn as_map(&self) -> &BTreeMap { + &self.map + } + fn touch(&mut self, key: &K) { if !self.touched.contains_key(key) { self.touched.insert(key.clone(), self.map.get(key).cloned()); @@ -290,6 +380,23 @@ mod tests { assert!(docs.is_clean()); } + #[test] + fn every_row_an_iterator_yields_is_counted_and_point_reads_are_not() { + let docs = rows(&[(1, 10), (2, 20), (3, 30), (4, 40)]); + let before = rows_read(); + assert_eq!(docs.iter().count(), 4); + assert_eq!(rows_read() - before, 4); + assert_eq!(docs.keys().count() + docs.values().count(), 8); + assert_eq!(docs.range(2..4).count(), 2); + assert_eq!((&docs).into_iter().rev().take(1).count(), 1); + assert_eq!(rows_read() - before, 4 + 8 + 2 + 1); + let before = rows_read(); + assert_eq!(docs.get(&2), Some(&20)); + assert!(docs.contains_key(&3) && docs[&4] == 40); + assert_eq!(docs.len(), 4); + assert_eq!(rows_read(), before); + } + #[test] fn equality_is_the_documents_and_not_the_log() { let mut open = rows(&[(1, 10)]); diff --git a/native/aetherlight/src/lib.rs b/native/aetherlight/src/lib.rs index cde56ab16..cbc5b94f9 100644 --- a/native/aetherlight/src/lib.rs +++ b/native/aetherlight/src/lib.rs @@ -1169,7 +1169,7 @@ fn require_play(caller: &CallerId) -> Result<(), KernelError> { /// `WorldState` so the genesis lane, which resolves declarations before any /// `WorldState` exists, can call the same owner instead of hand-copying the /// literal it would otherwise always evaluate to. -fn is_genesis(revision: u64, subjects: &BTreeMap) -> bool { +fn is_genesis(revision: u64, subjects: &Docs) -> bool { revision == 0 && subjects.is_empty() } diff --git a/native/aetherlight/src/patch.rs b/native/aetherlight/src/patch.rs index 884590815..21dcd0374 100644 --- a/native/aetherlight/src/patch.rs +++ b/native/aetherlight/src/patch.rs @@ -13,6 +13,7 @@ use super::clock::{FictionalMinutes, TickMinutes}; use super::grammar::GrammarRefusal; +use super::docs::Docs; use super::{ AffordanceId, CommandId, EdgeId, EntityId, SubjectId, SubjectKind, SubjectState, WorldId, }; @@ -2412,7 +2413,7 @@ fn resolve_affordance( site: Site, reference: &Ref, index: &BTreeMap, - catalog: &BTreeMap, + catalog: &Docs, mismatches: &mut Vec, ) -> Option> { match reference { @@ -2676,7 +2677,7 @@ fn resolve_entity( expected_kind: EntityKind, reference: &Ref, index: &BTreeMap, - entities: &BTreeMap, + entities: &Docs, mismatches: &mut Vec, ) -> Option> { let expected = RefKind::Entity(expected_kind); @@ -2724,7 +2725,7 @@ fn resolve_subject( site: Site, reference: &Ref, index: &BTreeMap, - subjects: &BTreeMap, + subjects: &Docs, mismatches: &mut Vec, ) -> Option> { match reference { @@ -2766,7 +2767,7 @@ fn resolve_route( site: Site, reference: &Ref, index: &BTreeMap, - edges: &BTreeMap, + edges: &Docs, mismatches: &mut Vec, ) -> Option> { match reference { @@ -5606,7 +5607,7 @@ fn contains_itself( /// downward enumeration and no child index: every jurisdictional question this /// kernel asks is "is this target inside my ground". pub(super) fn covers_place( - entities: &BTreeMap, + entities: &Docs, root: EntityId, place: EntityId, ) -> bool { @@ -5624,7 +5625,7 @@ pub(super) fn covers_place( /// The canonical twin of the candidate graph's `candidate_targets_overlap`: whether two /// jurisdictions of one kind cover common ground. pub(super) fn targets_overlap( - entities: &BTreeMap, + entities: &Docs, left: AuthorityTarget, right: AuthorityTarget, ) -> bool { @@ -5663,7 +5664,7 @@ pub(super) fn route_admits( /// chain that never terminates dies before the partition keeps it. pub(super) fn containment_terminates( start: EntityId, - entities: &BTreeMap, + entities: &Docs, ) -> bool { let mut current = entities.get(&start).and_then(|record| record.container); for _ in 0..=entities.len() {