feat: Update KeyValueStorage interface with entries function

This commit is contained in:
Joachim Van Herwegen
2021-02-25 16:59:14 +01:00
parent 52551ac773
commit 0f00a8dffd
8 changed files with 33 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
import { BasicRepresentation } from '../../ldp/representation/BasicRepresentation';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { readableToString } from '../../util/StreamUtil';
import type { ResourceStore } from '../ResourceStore';
import type { KeyValueStorage } from './KeyValueStorage';
@@ -61,4 +62,9 @@ export class JsonResourceStorage implements KeyValueStorage<ResourceIdentifier,
return false;
}
}
public entries(): never {
// We don't know which resources are in the store
throw new NotImplementedHttpError();
}
}

View File

@@ -33,4 +33,9 @@ export interface KeyValueStorage<TKey, TValue> {
* @returns If there was a value to delete.
*/
delete: (key: TKey) => Promise<boolean>;
/**
* An iterable of entries in the storage.
*/
entries: () => AsyncIterableIterator<[TKey, TValue]>;
}

View File

@@ -28,4 +28,10 @@ export class MemoryMapStorage<TKey, TValue> implements KeyValueStorage<TKey, TVa
public async delete(key: TKey): Promise<boolean> {
return this.data.delete(key);
}
public async* entries(): AsyncIterableIterator<[TKey, TValue]> {
for (const entry of this.data.entries()) {
yield entry;
}
}
}

View File

@@ -30,4 +30,10 @@ export class ResourceIdentifierStorage<T> implements KeyValueStorage<ResourceIde
public async delete(key: ResourceIdentifier): Promise<boolean> {
return this.source.delete(key.path);
}
public async* entries(): AsyncIterableIterator<[ResourceIdentifier, T]> {
for await (const [ path, value ] of this.source.entries()) {
yield [{ path }, value ];
}
}
}