I built a small Nostr in-memory datastore based on what I'm calling "microfilters". A microfilter is a filter to get a single note or author, eg `{ ids: ['123'] }` or `{ kinds: [0], authors: ['456'] }`
When an event is added to the store, it generates all possible microfilters for that event (there are just a couple supported so far), then uses stringify-stable on the filters to use as keys. It sets the event on each key.
To do lookups, you have to pass in microfilters. They get converted to keys with stringify-stable then looked up by key. The result is a key-value store that only works for filters with specific criteria. I know I'm not the first to do this. But it improves performance and reliability a lot for the most basic and common queries.
https://gitlab.com/soapbox-pub/ditto/-/blob/main/src/db/memorelay.ts
When an event is added to the store, it generates all possible microfilters for that event (there are just a couple supported so far), then uses stringify-stable on the filters to use as keys. It sets the event on each key.
To do lookups, you have to pass in microfilters. They get converted to keys with stringify-stable then looked up by key. The result is a key-value store that only works for filters with specific criteria. I know I'm not the first to do this. But it improves performance and reliability a lot for the most basic and common queries.
https://gitlab.com/soapbox-pub/ditto/-/blob/main/src/db/memorelay.ts