Memorelay: normalize filters
This commit is contained in:
parent
5bffffe07b
commit
c235fa6123
|
@ -104,6 +104,17 @@ function canFilter(filter: Filter): boolean {
|
|||
return getFilterLimit(filter) > 0;
|
||||
}
|
||||
|
||||
/** Normalize the `limit` of each filter, and remove filters that can't produce any events. */
|
||||
function normalizeFilters<F extends Filter>(filters: F[]): F[] {
|
||||
return filters.reduce<F[]>((acc, filter) => {
|
||||
const limit = getFilterLimit(filter);
|
||||
if (limit > 0) {
|
||||
acc.push(limit === Infinity ? filter : { ...filter, limit });
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
export {
|
||||
type AuthorMicrofilter,
|
||||
canFilter,
|
||||
|
@ -116,5 +127,6 @@ export {
|
|||
isMicrofilter,
|
||||
matchDittoFilters,
|
||||
type MicroFilter,
|
||||
normalizeFilters,
|
||||
type Relation,
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Debug, type Event, type Filter, LRUCache, matchFilter, matchFilters } from '@/deps.ts';
|
||||
import { canFilter, getFilterLimit } from '@/filter.ts';
|
||||
import { normalizeFilters } from '@/filter.ts';
|
||||
import { type EventStore, type GetEventsOpts } from '@/store.ts';
|
||||
|
||||
/** In-memory data store for events. */
|
||||
|
@ -28,7 +28,7 @@ class Memorelay implements EventStore {
|
|||
/** Get events from memory. */
|
||||
getEvents<K extends number>(filters: Filter<K>[], opts: GetEventsOpts = {}): Promise<Event<K>[]> {
|
||||
if (opts.signal?.aborted) return Promise.resolve([]);
|
||||
filters = filters.filter(canFilter);
|
||||
filters = normalizeFilters(filters);
|
||||
if (!filters.length) return Promise.resolve([]);
|
||||
this.#debug('REQ', JSON.stringify(filters));
|
||||
|
||||
|
@ -39,7 +39,7 @@ class Memorelay implements EventStore {
|
|||
let index = 0;
|
||||
|
||||
for (const filter of filters) {
|
||||
const limit = getFilterLimit(filter);
|
||||
const limit = filter.limit ?? Infinity;
|
||||
const usage = usages[index] ?? 0;
|
||||
|
||||
if (usage >= limit) {
|
||||
|
@ -52,7 +52,7 @@ class Memorelay implements EventStore {
|
|||
index++;
|
||||
}
|
||||
|
||||
if (filters.every((filter, index) => usages[index] >= getFilterLimit(filter))) {
|
||||
if (filters.every((filter, index) => filter.limit && (usages[index] >= filter.limit))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue