Skip to content
Closed
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
13 changes: 12 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);
}
// Memoized subject kinds, resolved lazily as kind terms are evaluated:
// `subject_kind` is called at most once per subject across the whole call,
// however many kind terms the term set carries (including duplicates).
let kind_cache: RefCell<HashMap<[u8; 32], Option<String>>> = RefCell::new(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,14 @@ pub fn search_terms(
.keys()
.copied()
.filter(|subject| {
subject_kind(&Hash::from_bytes(*subject)).as_deref() == Some(*kind)
let resolved = {
let mut cache = kind_cache.borrow_mut();
cache
.entry(*subject)
.or_insert_with(|| subject_kind(&Hash::from_bytes(*subject)))
.clone()
};
resolved.as_deref() == Some(*kind)
})
.collect(),
_ => claims_by_subject.keys().copied().collect(),
Expand Down
62 changes: 62 additions & 0 deletions pkgs/id/tests/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,68 @@ 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() {
// Three subjects, one live claim each. The counting lookup maps one to
// kind "alpha", one to "beta", and one to no kind at all.
let alpha = Hash::digest(b"alpha-subject");
let beta = Hash::digest(b"beta-subject");
let unknown = Hash::digest(b"unknown-subject");
let mut a = Writer::new(1);
let on_alpha = a.set(alpha, "city", Some("Seattle"), vec![]);
let on_beta = a.set(beta, "city", Some("Seattle"), vec![on_alpha.id]);
let on_unknown = a.set(unknown, "city", Some("Seattle"), vec![on_beta.id]);

let (_dir, mut store) = open();
for event in [&on_alpha, &on_beta, &on_unknown] {
deliver(&mut store, event);
}
let live = store.tag_claims().unwrap();

let calls = std::cell::Cell::new(0usize);
let lookup = |subject: &Hash| {
calls.set(calls.get() + 1);
match *subject {
s if s == alpha => Some("alpha".to_owned()),
s if s == beta => Some("beta".to_owned()),
_ => None,
}
};

// K=2 kind terms, the same term twice: the alpha subject matches, and the
// lookup runs at most once per subject (N=3), not once per term (2*N=6).
let hits = search_terms(
&live,
&[Term::SubjectKind("alpha"), Term::SubjectKind("alpha")],
&lookup,
);
assert_eq!(shape(&hits), vec![(alpha, vec![on_alpha.id])]);
assert!(
calls.get() <= 3,
"subject_kind called {} times for 3 subjects x 2 kind terms",
calls.get()
);

// A second, distinct kind term in the AND set still adds no new lookups:
// no subject is both kinds, so the answer is empty, at <= N calls again.
calls.set(0);
let hits = search_terms(
&live,
&[
Term::SubjectKind("alpha"),
Term::SubjectKind("beta"),
Term::SubjectKind("alpha"),
],
&lookup,
);
assert!(hits.is_empty());
assert!(
calls.get() <= 3,
"subject_kind called {} times for 3 subjects x 3 kind terms",
calls.get()
);
}

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