aetherlight: Docs loses Deref; reads are inherent methods and every row an iterator yields is counted
No behaviour change. Reads of a partition go through get, contains_key, len, is_empty, iter, keys, values, range, Index and IntoIterator, so no reader can bypass the test-build row counter by coercing to BTreeMap. Resolver and is_genesis parameters take &Docs. golden.digests untouched. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
576605c7b0
commit
436c6d59ca
4 changed files with 127 additions and 19 deletions
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<u64> = 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>(I);
|
||||
|
||||
impl<I: Iterator> Iterator for Counted<I> {
|
||||
type Item = I::Item;
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
let item = self.0.next();
|
||||
#[cfg(test)]
|
||||
if item.is_some() {
|
||||
count_rows(1);
|
||||
}
|
||||
item
|
||||
}
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.0.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: DoubleEndedIterator> DoubleEndedIterator for Counted<I> {
|
||||
fn next_back(&mut self) -> Option<I::Item> {
|
||||
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, V> = (K, Option<V>);
|
||||
|
|
@ -42,18 +91,11 @@ impl<K: Ord + Clone, V: Clone + PartialEq> PartialEq for Docs<K, V> {
|
|||
|
||||
impl<K: Ord + Clone, V: Clone + PartialEq + Eq> Eq for Docs<K, V> {}
|
||||
|
||||
impl<K: Ord + Clone, V: Clone + PartialEq> Deref for Docs<K, V> {
|
||||
type Target = BTreeMap<K, V>;
|
||||
fn deref(&self) -> &BTreeMap<K, V> {
|
||||
&self.map
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K: Ord + Clone, V: Clone + PartialEq> IntoIterator for &'a Docs<K, V> {
|
||||
type Item = (&'a K, &'a V);
|
||||
type IntoIter = std::collections::btree_map::Iter<'a, K, V>;
|
||||
type IntoIter = Counted<std::collections::btree_map::Iter<'a, K, V>>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.map.iter()
|
||||
self.iter()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +107,55 @@ impl<K: Ord + Clone + Serialize, V: Clone + PartialEq + Serialize> Serialize for
|
|||
}
|
||||
}
|
||||
|
||||
impl<K: Ord + Clone, V: Clone + PartialEq> std::ops::Index<&K> for Docs<K, V> {
|
||||
type Output = V;
|
||||
fn index(&self, key: &K) -> &V {
|
||||
&self.map[key]
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Ord + Clone, V: Clone + PartialEq> Docs<K, V> {
|
||||
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<std::collections::btree_map::Iter<'_, K, V>> {
|
||||
Counted(self.map.iter())
|
||||
}
|
||||
|
||||
pub(crate) fn keys(&self) -> Counted<std::collections::btree_map::Keys<'_, K, V>> {
|
||||
Counted(self.map.keys())
|
||||
}
|
||||
|
||||
pub(crate) fn values(&self) -> Counted<std::collections::btree_map::Values<'_, K, V>> {
|
||||
Counted(self.map.values())
|
||||
}
|
||||
|
||||
pub(crate) fn range<R: std::ops::RangeBounds<K>>(
|
||||
&self,
|
||||
range: R,
|
||||
) -> Counted<std::collections::btree_map::Range<'_, K, V>> {
|
||||
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<K, V> {
|
||||
&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)]);
|
||||
|
|
|
|||
|
|
@ -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<SubjectId, SubjectState>) -> bool {
|
||||
fn is_genesis(revision: u64, subjects: &Docs<SubjectId, SubjectState>) -> bool {
|
||||
revision == 0 && subjects.is_empty()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<AffordanceId>,
|
||||
index: &BTreeMap<DraftHandle, RefKind>,
|
||||
catalog: &BTreeMap<AffordanceId, Affordance>,
|
||||
catalog: &Docs<AffordanceId, Affordance>,
|
||||
mismatches: &mut Vec<Mismatch>,
|
||||
) -> Option<Key<AffordanceId>> {
|
||||
match reference {
|
||||
|
|
@ -2676,7 +2677,7 @@ fn resolve_entity(
|
|||
expected_kind: EntityKind,
|
||||
reference: &Ref<EntityId>,
|
||||
index: &BTreeMap<DraftHandle, RefKind>,
|
||||
entities: &BTreeMap<EntityId, EntityRecord>,
|
||||
entities: &Docs<EntityId, EntityRecord>,
|
||||
mismatches: &mut Vec<Mismatch>,
|
||||
) -> Option<Key<EntityId>> {
|
||||
let expected = RefKind::Entity(expected_kind);
|
||||
|
|
@ -2724,7 +2725,7 @@ fn resolve_subject(
|
|||
site: Site,
|
||||
reference: &Ref<SubjectId>,
|
||||
index: &BTreeMap<DraftHandle, RefKind>,
|
||||
subjects: &BTreeMap<SubjectId, SubjectState>,
|
||||
subjects: &Docs<SubjectId, SubjectState>,
|
||||
mismatches: &mut Vec<Mismatch>,
|
||||
) -> Option<Key<SubjectId>> {
|
||||
match reference {
|
||||
|
|
@ -2766,7 +2767,7 @@ fn resolve_route(
|
|||
site: Site,
|
||||
reference: &Ref<EdgeId>,
|
||||
index: &BTreeMap<DraftHandle, RefKind>,
|
||||
edges: &BTreeMap<EdgeId, EdgeRecord>,
|
||||
edges: &Docs<EdgeId, EdgeRecord>,
|
||||
mismatches: &mut Vec<Mismatch>,
|
||||
) -> Option<Key<EdgeId>> {
|
||||
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<EntityId, EntityRecord>,
|
||||
entities: &Docs<EntityId, EntityRecord>,
|
||||
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<EntityId, EntityRecord>,
|
||||
entities: &Docs<EntityId, EntityRecord>,
|
||||
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<EntityId, EntityRecord>,
|
||||
entities: &Docs<EntityId, EntityRecord>,
|
||||
) -> bool {
|
||||
let mut current = entities.get(&start).and_then(|record| record.container);
|
||||
for _ in 0..=entities.len() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue