Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkgs/id/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ pub fn search_terms(
.or_default()
.push(claim.id);
}
// `subject_kind` is resolved at most once per subject, however many kind terms the
// search carries: the resolved kind (including an unresolvable `None`) is memoised
// here on first use, and every kind term reads the memo.
let mut kind_memo: HashMap<[u8; 32], Option<String>> = HashMap::new();
// For each term, the subjects it matches and, for claim terms, the claims that match it.
let mut matched: Vec<(BTreeSet<[u8; 32]>, BTreeMap<[u8; 32], BTreeSet<[u8; 32]>>)> =
Vec::with_capacity(terms.len());
Expand Down Expand Up @@ -137,7 +141,11 @@ pub fn search_terms(
.keys()
.copied()
.filter(|subject| {
subject_kind(&Hash::from_bytes(*subject)).as_deref() == Some(*kind)
kind_memo
.entry(*subject)
.or_insert_with(|| subject_kind(&Hash::from_bytes(*subject)))
.as_deref()
== Some(*kind)
})
.collect(),
_ => claims_by_subject.keys().copied().collect(),
Expand Down
59 changes: 59 additions & 0 deletions pkgs/id/tests/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,65 @@ fn test_search_by_subject_kind_matches_only_events_of_that_kind() {
assert!(hits.is_empty());
}

#[test]
fn test_search_terms_resolves_subject_kind_at_most_once_per_subject() {
use std::cell::Cell;
use std::collections::HashMap as Map;

// Three subjects, each with one live claim.
let mut author = Writer::new(1);
let one = Hash::digest(b"counting-one");
let two = Hash::digest(b"counting-two");
let three = Hash::digest(b"counting-three");
let on_one = author.set(one, "city", Some("Seattle"), vec![]);
let on_two = author.set(two, "city", Some("Seattle"), vec![on_one.id]);
let on_three = author.set(three, "city", Some("Seattle"), vec![on_two.id]);

let (_dir, mut store) = open();
for event in [&on_one, &on_two, &on_three] {
deliver(&mut store, event);
}
let live = store.tag_claims().unwrap();

// Subjects one and two resolve to a kind; three resolves to none.
let kinds: Map<Hash, Option<String>> = [
(one, Some("kind-a".to_string())),
(two, Some("kind-a".to_string())),
(three, None),
]
.into_iter()
.collect();
let calls = Cell::new(0usize);
let lookup = |subject: &Hash| {
calls.set(calls.get() + 1);
kinds.get(subject).cloned().unwrap_or(None)
};

// Three kind terms (one duplicated) over three subjects: without the memo
// that is nine lookups; the documented contract is at most one per subject.
let hits = search_terms(
&live,
&[
Term::SubjectKind("kind-a"),
Term::SubjectKind("kind-a"),
Term::SubjectKind("kind-b"),
],
&lookup,
);
assert_eq!(calls.get(), 3);
// AND semantics are unchanged: no subject is both kind-a and kind-b.
assert!(hits.is_empty());

// A single kind term gives the same subjects as before, still one call each.
calls.set(0);
let hits = search_terms(&live, &[Term::SubjectKind("kind-a")], &lookup);
assert_eq!(calls.get(), 3);
assert_eq!(
shape(&hits),
vec![(one, vec![on_one.id]), (two, vec![on_two.id])]
);
}

#[test]
fn test_search_terms_without_kind_succeeds_even_when_subject_event_is_missing() {
let mut author = Writer::new(1);
Expand Down