-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nostrdb-net: algo for unifying similar subscriptions #31
Comments
I have something like this in |
On Wed, Jan 22, 2025 at 01:08:19PM GMT, Kieran wrote:
I have something like this in ***@***.***/system-wasm` but its not very well written: https://github.com/v0l/snort/blob/main/packages/system-wasm/src/merge.rs
interesting I'll take a look
|
The general algo works like this: 1. Split a filter into "FlatFilters" by Cartesian productTake the example filter {"kinds":0,"authors":"a"}
{"kinds":0,"authors":"b"}
{"kinds":1,"authors":"a"}
{"kinds":1,"authors":"b"}
{"kinds":2,"authors":"a"}
{"kinds":2,"authors":"b"} 2. Merge similarTo find "similar" filters you simply measure how "far" one filter is from another by comparing their properties: For each property of the filter you add up the total distance as computed by the following function: #[inline(always)]
fn prop_dist<T: Eq>(a: &Option<T>, b: &Option<T>) -> u32 {
if (a.is_some() && b.is_none()) || (a.is_none() && b.is_some()) {
return 10;
} else if a.is_some() && a != b {
return 1;
}
0
} Then you check if the value is {"kinds":0,"authors":"a"}
{"kinds":0,"authors":"b"} But not: {"#e": "ccc","authors":"a"}
{"kinds":0,"authors":"a"} There is also never a case when filters can be merged when using limit/since/until, as code this looks like: impl CanMerge for ReqFilter {
fn can_merge(&self, other: &Self) -> bool {
if self.since != other.since
|| self.until != other.until
|| self.limit != other.limit
|| self.search != other.search
{
return false;
}
self.distance(other) <= 1
}
} Then you fold your list of "FlatFilters" by checking |
This will be useful for our subscription manager
Example:
subA:
{"authors": ["a"], "since": 100}
subB:
{"authors": ["b"], "since": 200}
Should optimize to:
subAB:
{"authors": ["a", "b"], "since": 100}
The text was updated successfully, but these errors were encountered: