deno.land / x / replicache@v10.0.0-beta.0 / db / scan.test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {expect} from '@esm-bundle/chai';import type {ScanItem} from './scan';import * as dag from '../dag/mod';import {BTreeWrite} from '../btree/mod';import {decodeIndexKey} from './index.js';import {fromKeyForIndexScanInternal} from '../scan-iterator.js';
test('scan', async () => { const t = async (fromKey: string, expected: string[]) => { const dagStore = new dag.TestStore();
await dagStore.withWrite(async dagWrite => { const map = new BTreeWrite(dagWrite); await map.put('foo', 'foo'); await map.put('bar', 'bar'); await map.put('baz', 'baz'); await map.flush();
const actual = []; for await (const entry of map.scan(fromKey)) { actual.push(entry[0]); } const expected2 = expected; expect(actual).to.deep.equal(expected2); }); };
await t('', ['bar', 'baz', 'foo']); await t('ba', ['bar', 'baz', 'foo']); await t('bar', ['bar', 'baz', 'foo']); await t('bas', ['baz', 'foo']); await t('baz', ['baz', 'foo']); await t('baza', ['foo']); await t('fop', []);});
async function makeBTreeWrite( dagWrite: dag.Write, entries: Iterable<[string, string]>,): Promise<BTreeWrite> { const map = new BTreeWrite(dagWrite); for (const [k, v] of entries) { await map.put(k, v); } return map;}
test('scan index startKey', async () => { const t = async ( entries: Iterable<[string, string]>, { startSecondaryKey, startPrimaryKey, }: { startSecondaryKey: string; startPrimaryKey?: string; }, expected: ScanItem[], ) => { const dagStore = new dag.TestStore();
await dagStore.withWrite(async dagWrite => { const map = await makeBTreeWrite(dagWrite, entries); await map.flush();
const fromKey = fromKeyForIndexScanInternal({ start: {key: [startSecondaryKey, startPrimaryKey]}, indexName: 'dummy', }); const actual: ScanItem[] = []; for await (const entry of map.scan(fromKey)) { const [secondaryKey, primaryKey] = decodeIndexKey(entry[0]); actual.push({primaryKey, secondaryKey, val: entry[1]}); }
expect(actual).to.deep.equal(expected); }); };
await t( [ ['\u{0000}as\u{0000}ap', '1'], ['\u{0000}bs\u{0000}bp', '2'], ['\u{0000}cs\u{0000}cp', '3'], ], { startSecondaryKey: 'bs', startPrimaryKey: undefined, }, [ { primaryKey: 'bp', secondaryKey: 'bs', val: '2', }, { primaryKey: 'cp', secondaryKey: 'cs', val: '3', }, ], );
await t( [ ['\u{0000}as\u{0000}ap', '1'], ['\u{0000}bs\u{0000}bp', '2'], ['\u{0000}cs\u{0000}cp', '3'], ], { startSecondaryKey: 'bs', startPrimaryKey: undefined, }, [ { primaryKey: 'bp', secondaryKey: 'bs', val: '2', }, { primaryKey: 'cp', secondaryKey: 'cs', val: '3', }, ], );
await t( [ ['\u{0000}as\u{0000}ap', '1'], ['\u{0000}bs\u{0000}bp1', '2'], ['\u{0000}bs\u{0000}bp2', '3'], ['\u{0000}cs\u{0000}cp', '4'], ], { startSecondaryKey: 'bs', startPrimaryKey: 'bp2', }, [ { primaryKey: 'bp2', secondaryKey: 'bs', val: '3', }, { primaryKey: 'cp', secondaryKey: 'cs', val: '4', }, ], );
await t( [ ['\u{0000}as\u{0000}ap', '1'], ['\u{0000}bs\u{0000}bp1', '2'], ['\u{0000}bs\u{0000}bp2', '3'], ['\u{0000}cs\u{0000}cp', '4'], ], { startSecondaryKey: 'bs', startPrimaryKey: 'bp2', }, [ { primaryKey: 'bp2', secondaryKey: 'bs', val: '3', }, { primaryKey: 'cp', secondaryKey: 'cs', val: '4', }, ], );});
replicache

Version Info

Tagged at
2 years ago