import { Key } from 'interface-datastore/key' import { NotFoundError } from 'interface-store' import { BaseDatastore } from './base.ts' import type { AbortOptions } from 'abort-error' import type { KeyQuery, Pair, Query } from 'interface-datastore' export class MemoryDatastore extends BaseDatastore { private readonly data: Map constructor () { super() this.data = new Map() } put (key: Key, val: Uint8Array, options?: AbortOptions): Key | Promise { options?.signal?.throwIfAborted() this.data.set(key.toString(), val) return key } get (key: Key, options?: AbortOptions): Uint8Array | Promise { options?.signal?.throwIfAborted() const result = this.data.get(key.toString()) if (result == null) { throw new NotFoundError() } return result } has (key: Key, options?: AbortOptions): boolean | Promise { options?.signal?.throwIfAborted() return this.data.has(key.toString()) } delete (key: Key, options?: AbortOptions): void | Promise { options?.signal?.throwIfAborted() this.data.delete(key.toString()) } * _all (q: Query, options?: AbortOptions): Generator | AsyncGenerator { options?.signal?.throwIfAborted() for (const [key, value] of this.data.entries()) { yield { key: new Key(key), value } options?.signal?.throwIfAborted() } } * _allKeys (q: KeyQuery, options?: AbortOptions): Generator | AsyncGenerator { options?.signal?.throwIfAborted() for (const key of this.data.keys()) { yield new Key(key) options?.signal?.throwIfAborted() } } }