Add Memorelay storages module
This commit is contained in:
parent
792e47f0f7
commit
24c405b9b9
|
@ -0,0 +1,63 @@
|
|||
import { Debug, type Event, type Filter, LRUCache } from '@/deps.ts';
|
||||
import { getFilterId, getMicroFilters, isMicrofilter } from '@/filter.ts';
|
||||
import { type EventStore, type GetEventsOpts } from '@/store.ts';
|
||||
|
||||
/** In-memory data store for events using microfilters. */
|
||||
class Memorelay implements EventStore {
|
||||
#debug = Debug('ditto:memorelay');
|
||||
#events: LRUCache<string, Event>;
|
||||
|
||||
constructor(...args: ConstructorParameters<typeof LRUCache<string, Event>>) {
|
||||
this.#events = new LRUCache<string, Event>(...args);
|
||||
}
|
||||
|
||||
/** Get events from memory. */
|
||||
getEvents<K extends number>(filters: Filter<K>[], opts: GetEventsOpts = {}): Promise<Event<K>[]> {
|
||||
if (opts.signal?.aborted) return Promise.resolve([]);
|
||||
if (!filters.length) return Promise.resolve([]);
|
||||
this.#debug('REQ', JSON.stringify(filters));
|
||||
|
||||
const results: Event<K>[] = [];
|
||||
|
||||
for (const filter of filters) {
|
||||
if (isMicrofilter(filter)) {
|
||||
const event = this.#events.get(getFilterId(filter));
|
||||
if (event) {
|
||||
results.push(event as Event<K>);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.resolve(results);
|
||||
}
|
||||
|
||||
/** Insert an event into memory. */
|
||||
storeEvent(event: Event): Promise<void> {
|
||||
for (const microfilter of getMicroFilters(event)) {
|
||||
const filterId = getFilterId(microfilter);
|
||||
const existing = this.#events.get(filterId);
|
||||
if (!existing || event.created_at > existing.created_at) {
|
||||
this.#events.set(filterId, event);
|
||||
}
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
/** Count events in memory for the filters. */
|
||||
async countEvents(filters: Filter[]): Promise<number> {
|
||||
const events = await this.getEvents(filters);
|
||||
return events.length;
|
||||
}
|
||||
|
||||
/** Delete events from memory. */
|
||||
deleteEvents(filters: Filter[]): Promise<void> {
|
||||
for (const filter of filters) {
|
||||
if (isMicrofilter(filter)) {
|
||||
this.#events.delete(getFilterId(filter));
|
||||
}
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
export { Memorelay };
|
Loading…
Reference in New Issue