deno.land / x / replicache@v10.0.0-beta.0 / db / write.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import {LogContext} from '@rocicorp/logger';import {expect} from '@esm-bundle/chai';import {assertNotUndefined} from '../asserts';import * as dag from '../dag/mod';import {DEFAULT_HEAD_NAME} from './commit';import { readCommit, readCommitForBTreeRead, readIndexesForRead, whenceHead,} from './read';import {initDB, Write} from './write';import {encodeIndexKey} from './index';import {asyncIterableToArray} from '../async-iterable-to-array';import {BTreeRead} from '../btree/mod';
test('basics', async () => { const ds = new dag.TestStore(); const lc = new LogContext(); await initDB(await ds.write(), DEFAULT_HEAD_NAME);
// Put. await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify([]), null, dagWrite, 42, ); await w.put(lc, 'foo', 'bar'); // Assert we can read the same value from within this transaction.; const val = await w.get('foo'); expect(val).to.deep.equal('bar'); await w.commit(DEFAULT_HEAD_NAME); });
// As well as after it has committed. await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify(null), null, dagWrite, 42, ); const val = await w.get('foo'); expect(val).to.deep.equal('bar'); });
// Del. await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify([]), null, dagWrite, 42, ); await w.del(lc, 'foo'); // Assert it is gone while still within this transaction. const val = await w.get('foo'); expect(val).to.be.undefined; await w.commit(DEFAULT_HEAD_NAME); });
// As well as after it has committed. await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify(null), null, dagWrite, 42, ); const val = await w.get(`foo`); expect(val).to.be.undefined; });});
test('index commit type constraints', async () => { const ds = new dag.TestStore(); const lc = new LogContext(); await initDB(await ds.write(), DEFAULT_HEAD_NAME);
// Test that local changes cannot create or drop an index. const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify([]), null, await ds.write(), 42, );
let err; try { await w.createIndex(lc, 'foo', '', ''); } catch (e) { err = e; } expect(err).to.be.an.instanceof(Error); expect(err).to.have.property('message', 'Not allowed');
err = undefined; try { await w.dropIndex('foo'); } catch (e) { err = e; } expect(err).to.be.an.instanceof(Error); expect(err).to.have.property('message', 'Not allowed');});
test('clear', async () => { const ds = new dag.TestStore(); const lc = new LogContext(); await ds.withWrite(dagWrite => initDB(dagWrite, DEFAULT_HEAD_NAME)); await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify([]), null, dagWrite, 42, ); await w.put(lc, 'foo', 'bar'); await w.commit(DEFAULT_HEAD_NAME); });
await ds.withWrite(async dagWrite => { const w = await Write.newIndexChange( whenceHead(DEFAULT_HEAD_NAME), dagWrite, ); await w.createIndex(lc, 'idx', '', ''); await w.commit(DEFAULT_HEAD_NAME); });
await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify([]), null, dagWrite, 42, ); await w.put(lc, 'hot', 'dog');
const keys = await asyncIterableToArray(w.map.keys()); expect(keys).to.have.lengthOf(2); let index = w.indexes.get('idx'); assertNotUndefined(index); await index.withMap(dagWrite, async map => { const keys = await asyncIterableToArray(map.keys()); expect(keys).to.have.lengthOf(2); });
await w.clear(); const keys2 = await asyncIterableToArray(w.map.keys()); expect(keys2).to.have.lengthOf(0); index = w.indexes.get('idx'); assertNotUndefined(index); await index.withMap(dagWrite, async map => { const keys = await asyncIterableToArray(map.keys()); expect(keys).to.have.lengthOf(0); });
await w.commit(DEFAULT_HEAD_NAME); });
await ds.withRead(async dagRead => { const [, c, r] = await readCommitForBTreeRead( whenceHead(DEFAULT_HEAD_NAME), dagRead, ); const indexes = readIndexesForRead(c); const keys = await asyncIterableToArray(r.keys()); expect(keys).to.have.lengthOf(0); const index = indexes.get('idx'); assertNotUndefined(index); await index.withMap(dagRead, async map => { const keys = await asyncIterableToArray(map.keys()); expect(keys).to.have.lengthOf(0); }); });});
test('create and drop index', async () => { const t = async (writeBeforeIndexing: boolean) => { const ds = new dag.TestStore(); const lc = new LogContext(); await ds.withWrite(dagWrite => initDB(dagWrite, DEFAULT_HEAD_NAME));
if (writeBeforeIndexing) { await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify([]), null, dagWrite, 42, ); for (let i = 0; i < 3; i++) { await w.put(lc, `k${i}`, {s: `s${i}`}); } await w.commit(DEFAULT_HEAD_NAME); }); }
const indexName = 'i1'; await ds.withWrite(async dagWrite => { const w = await Write.newIndexChange( whenceHead(DEFAULT_HEAD_NAME), dagWrite, ); await w.createIndex(lc, indexName, '', '/s'); await w.commit(DEFAULT_HEAD_NAME); });
if (!writeBeforeIndexing) { await ds.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), 'mutator_name', JSON.stringify([]), null, dagWrite, 42, ); for (let i = 0; i < 3; i++) { await w.put(lc, `k${i}`, {s: `s${i}`}); } await w.commit(DEFAULT_HEAD_NAME); }); }
await ds.withRead(async dagRead => { const [, c] = await readCommit(whenceHead(DEFAULT_HEAD_NAME), dagRead); const {indexes} = c; expect(indexes).to.have.lengthOf(1); const idx = indexes[0]; expect(idx.definition.name).to.equal(indexName); expect(idx.definition.keyPrefix).to.be.empty; expect(idx.definition.jsonPointer).to.equal('/s'); const indexMap = new BTreeRead(dagRead, idx.valueHash);
const entries = await asyncIterableToArray(indexMap); expect(entries).to.have.lengthOf(3); for (let i = 0; i < 3; i++) { expect(entries[i][0]).to.deep.equal(encodeIndexKey([`s${i}`, `k${i}`])); } });
// Ensure drop works. await ds.withWrite(async dagWrite => { const w = await Write.newIndexChange( whenceHead(DEFAULT_HEAD_NAME), dagWrite, ); await w.dropIndex(indexName); await w.commit(DEFAULT_HEAD_NAME); const [, c] = await readCommit(whenceHead(DEFAULT_HEAD_NAME), dagWrite); const {indexes} = c; expect(indexes).to.be.empty; }); };
await t(true); await t(false);});
replicache

Version Info

Tagged at
2 years ago