-
Notifications
You must be signed in to change notification settings - Fork 36
/
test-store.ts
57 lines (51 loc) · 1.52 KB
/
test-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {StoreImpl} from './store-impl';
import {
assertNotTempHash,
makeNewFakeHashFunction,
parse as parseHash,
} from '../hash';
import {Chunk, ChunkHasher, createChunkWithHash} from './chunk';
import type {Hash} from '../hash';
import {chunkMetaKey, parse as parseKey} from './key';
import {KeyType} from './key';
import {TestMemStore} from '../kv/test-mem-store';
import {assertArray, assertString} from '../asserts';
import {stringCompare} from '../string-compare';
export class TestStore extends StoreImpl {
readonly kvStore: TestMemStore;
constructor(
kvStore = new TestMemStore(),
chunkHasher: ChunkHasher = makeNewFakeHashFunction('fakehash'),
assertValidHash = assertNotTempHash,
) {
super(kvStore, chunkHasher, assertValidHash);
this.kvStore = kvStore;
}
chunks(): Chunk[] {
const rv: Chunk[] = [];
for (const [key, value] of this.kvStore.entries()) {
const pk = parseKey(key);
if (pk.type === KeyType.ChunkData) {
const refsValue = this.kvStore.map().get(chunkMetaKey(pk.hash));
rv.push(createChunkWithHash(pk.hash, value, toRefs(refsValue)));
}
}
return sortByHash(rv);
}
clear(): void {
this.kvStore.clear();
}
}
export function sortByHash(arr: Iterable<Chunk>): Chunk[] {
return [...arr].sort((a, b) => stringCompare(String(a.hash), String(b.hash)));
}
function toRefs(refs: unknown): Hash[] {
if (refs === undefined) {
return [];
}
assertArray(refs);
return refs.map(h => {
assertString(h);
return parseHash(h);
});
}