Skip to content

Commit 1750e8f

Browse files
TheSentinel454Elrondclaude
committed
refactor(relay): choose each filter's selector in one pass
Each filter is now keyed by its `#p` values when it names them, else by its authors, so one pass builds the selector keys. This drops the second, authors-only pass, and a set that mixes `#p` filters with author filters now routes through the selector index instead of the kind or wildcard index. Fan-out already looks up both key types for every event. The event-side `p` tag read now points at `filters_match`, whose rule it must match. Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent e35e178 commit 1750e8f

1 file changed

Lines changed: 61 additions & 27 deletions

File tree

‎crates/buzz-relay/src/subscription.rs‎

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -704,37 +704,15 @@ fn p_tag() -> SingleLetterTag {
704704
const MAX_GLOBAL_SELECTOR_KEYS: usize = 4096;
705705

706706
/// Index keys for a global subscription whose every filter is constrained to
707-
/// `#p` recipients, or else to authors, with or without kinds, within
708-
/// [`MAX_GLOBAL_SELECTOR_KEYS`]. `None` means it needs the broader
709-
/// kind/wildcard indexes. `#p` wins when a filter names both.
707+
/// `#p` recipients or to authors, with or without kinds, within
708+
/// [`MAX_GLOBAL_SELECTOR_KEYS`]. Each filter is keyed by its `#p` values when
709+
/// it names them, else by its authors. `None` means it needs the broader
710+
/// kind/wildcard indexes.
710711
fn extract_global_selector_index_keys(
711712
community_id: CommunityId,
712713
filters: &[Filter],
713714
) -> Option<Vec<GlobalSelectorIndexKey>> {
714715
let p_tag = p_tag();
715-
extract_selector_keys(community_id, filters, |filter| {
716-
let values = filter.generic_tags.get(&p_tag)?;
717-
Some(values.iter().cloned().map(GlobalSelector::P).collect())
718-
})
719-
.or_else(|| {
720-
extract_selector_keys(community_id, filters, |filter| {
721-
let authors = filter.authors.as_ref()?;
722-
Some(
723-
authors
724-
.iter()
725-
.copied()
726-
.map(GlobalSelector::Author)
727-
.collect(),
728-
)
729-
})
730-
})
731-
}
732-
733-
fn extract_selector_keys(
734-
community_id: CommunityId,
735-
filters: &[Filter],
736-
selectors_of: impl Fn(&Filter) -> Option<Vec<GlobalSelector>>,
737-
) -> Option<Vec<GlobalSelectorIndexKey>> {
738716
let mut seen = HashSet::new();
739717
let mut keys = Vec::new();
740718
let mut budget = MAX_GLOBAL_SELECTOR_KEYS;
@@ -746,7 +724,16 @@ fn extract_selector_keys(
746724
None => vec![None],
747725
};
748726

749-
let selectors = selectors_of(filter)?;
727+
let selectors: Vec<GlobalSelector> =
728+
match (filter.generic_tags.get(&p_tag), &filter.authors) {
729+
(Some(values), _) => values.iter().cloned().map(GlobalSelector::P).collect(),
730+
(None, Some(authors)) => authors
731+
.iter()
732+
.copied()
733+
.map(GlobalSelector::Author)
734+
.collect(),
735+
(None, None) => return None,
736+
};
750737
if selectors.is_empty() {
751738
return None;
752739
}
@@ -774,6 +761,8 @@ fn extract_selector_keys(
774761
fn event_global_selectors(event: &StoredEvent) -> Vec<GlobalSelector> {
775762
let mut seen = HashSet::new();
776763
let mut selectors = Vec::new();
764+
// Must read `p` tags exactly as `buzz_core::filter::filters_match` does, or
765+
// fan-out silently skips subscriptions that match.
777766
for tag in event.event.tags.iter() {
778767
if tag.kind().to_string() != "p" {
779768
continue;
@@ -2009,6 +1998,51 @@ mod tests {
20091998
.is_empty());
20101999
}
20112000

2001+
/// A set whose filters each name `#p` or authors routes by selector, each
2002+
/// filter by its own; a `kinds: []` filter beside them matches nothing
2003+
/// and needs no key.
2004+
#[test]
2005+
fn test_global_selector_index_routes_mixed_p_and_author_filters() {
2006+
let registry = SubscriptionRegistry::new();
2007+
let presence = Kind::Custom(buzz_core::kind::KIND_PRESENCE_UPDATE as u16);
2008+
let alice = Keys::generate();
2009+
let p = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
2010+
let conn_id = Uuid::new_v4();
2011+
2012+
registry.register(
2013+
conn_id,
2014+
"mixed".to_string(),
2015+
vec![
2016+
Filter::new().custom_tags(p_tag(), [p]),
2017+
Filter::new().kind(presence).author(alice.public_key()),
2018+
Filter::new().kinds(Vec::<Kind>::new()),
2019+
],
2020+
None,
2021+
);
2022+
assert!(registry.global_kind_index.is_empty());
2023+
assert!(registry
2024+
.global_wildcard_index
2025+
.get(&test_community())
2026+
.is_none());
2027+
assert_eq!(registry.global_selector_index.len(), 2);
2028+
2029+
let delivered = vec![(conn_id, "mixed".to_string())];
2030+
assert_eq!(
2031+
registry.fan_out(&make_stored_event_with_p(Kind::TextNote, p, None)),
2032+
delivered
2033+
);
2034+
assert_eq!(
2035+
registry.fan_out(&make_stored_event_by(&alice, presence)),
2036+
delivered
2037+
);
2038+
assert!(registry
2039+
.fan_out(&make_stored_event_by(&alice, Kind::TextNote))
2040+
.is_empty());
2041+
2042+
registry.remove_subscription(conn_id, "mixed");
2043+
assert!(registry.global_selector_index.is_empty());
2044+
}
2045+
20122046
#[test]
20132047
fn test_global_selector_index_kindless_filters_skip_wildcard_scan() {
20142048
let registry = SubscriptionRegistry::new();

0 commit comments

Comments
 (0)