Memorelay: use EventSet data structure
This commit is contained in:
parent
384bb729b4
commit
8ab0fefbf2
|
@ -1,6 +1,7 @@
|
||||||
import { Debug, type Event, type Filter, LRUCache, matchFilter } from '@/deps.ts';
|
import { Debug, type Event, type Filter, LRUCache, matchFilter } from '@/deps.ts';
|
||||||
import { normalizeFilters } from '@/filter.ts';
|
import { normalizeFilters } from '@/filter.ts';
|
||||||
import { type EventStore, type GetEventsOpts } from '@/store.ts';
|
import { type EventStore, type GetEventsOpts } from '@/store.ts';
|
||||||
|
import { EventSet } from '@/utils/event-set.ts';
|
||||||
|
|
||||||
/** In-memory data store for events. */
|
/** In-memory data store for events. */
|
||||||
class Memorelay implements EventStore {
|
class Memorelay implements EventStore {
|
||||||
|
@ -35,14 +36,14 @@ class Memorelay implements EventStore {
|
||||||
this.#debug('REQ', JSON.stringify(filters));
|
this.#debug('REQ', JSON.stringify(filters));
|
||||||
|
|
||||||
/** Event results to return. */
|
/** Event results to return. */
|
||||||
const results: Event<K>[] = [];
|
const results = new EventSet<Event<K>>();
|
||||||
|
|
||||||
/** Number of times an event has been added to results for each filter. */
|
/** Number of times an event has been added to results for each filter. */
|
||||||
const filterUsages: number[] = [];
|
const filterUsages: number[] = [];
|
||||||
|
|
||||||
/** Check if all filters have been satisfied. */
|
/** Check if all filters have been satisfied. */
|
||||||
function checkSatisfied() {
|
function checkSatisfied() {
|
||||||
return results.length >= (opts.limit ?? Infinity) ||
|
return results.size >= (opts.limit ?? Infinity) ||
|
||||||
filters.every((filter, index) => filter.limit && (filterUsages[index] >= filter.limit));
|
filters.every((filter, index) => filter.limit && (filterUsages[index] >= filter.limit));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +53,7 @@ class Memorelay implements EventStore {
|
||||||
for (const id of filter.ids) {
|
for (const id of filter.ids) {
|
||||||
const event = this.#cache.get(id);
|
const event = this.#cache.get(id);
|
||||||
if (event && matchFilter(filter, event)) {
|
if (event && matchFilter(filter, event)) {
|
||||||
results.push(event as Event<K>);
|
results.add(event as Event<K>);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
filterUsages[index] = Infinity;
|
filterUsages[index] = Infinity;
|
||||||
|
@ -61,7 +62,7 @@ class Memorelay implements EventStore {
|
||||||
|
|
||||||
// Return early if all filters are satisfied.
|
// Return early if all filters are satisfied.
|
||||||
if (checkSatisfied()) {
|
if (checkSatisfied()) {
|
||||||
return Promise.resolve(results);
|
return Promise.resolve([...results]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seek through all events in memory.
|
// Seek through all events in memory.
|
||||||
|
@ -73,7 +74,7 @@ class Memorelay implements EventStore {
|
||||||
if (usage >= limit) {
|
if (usage >= limit) {
|
||||||
return;
|
return;
|
||||||
} else if (matchFilter(filter, event)) {
|
} else if (matchFilter(filter, event)) {
|
||||||
results.push(event as Event<K>);
|
results.add(event as Event<K>);
|
||||||
this.#cache.get(event.id);
|
this.#cache.get(event.id);
|
||||||
filterUsages[index] = usage + 1;
|
filterUsages[index] = usage + 1;
|
||||||
}
|
}
|
||||||
|
@ -87,7 +88,7 @@ class Memorelay implements EventStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve(results);
|
return Promise.resolve([...results]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Insert an event into memory. */
|
/** Insert an event into memory. */
|
||||||
|
|
Loading…
Reference in New Issue