deno.land / x / replicache@v10.0.0-beta.0 / db / commit.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import type * as dag from '../dag/mod';import type {ReadonlyJSONValue} from '../json';import {assertJSONValue} from '../json';import { assert, assertArray, assertNumber, assertObject, assertString,} from '../asserts';import type {Value} from '../kv/store';import {assertHash, Hash} from '../hash';import {skipCommitDataAsserts} from '../config.js';
export const DEFAULT_HEAD_NAME = 'main';
export const enum MetaTyped { NONE = 0, IndexChange = 1, Local = 2, Snapshot = 3,}
export class Commit<M extends Meta> { readonly chunk: dag.Chunk<CommitData<M>>;
constructor(chunk: dag.Chunk<CommitData<M>>) { this.chunk = chunk; }
get meta(): M { return this.chunk.data.meta; }
isLocal(): this is Commit<LocalMeta> { return this.meta.type === MetaTyped.Local; }
isSnapshot(): this is Commit<SnapshotMeta> { return this.meta.type === MetaTyped.Snapshot; }
isIndexChange(): this is Commit<IndexChangeMeta> { return this.meta.type === MetaTyped.IndexChange; }
get valueHash(): Hash { // Already validated! return this.chunk.data.valueHash; }
get mutationID(): number { const {meta} = this; switch (meta.type) { case MetaTyped.IndexChange: case MetaTyped.Snapshot: return meta.lastMutationID; case MetaTyped.Local: return meta.mutationID; } }
get nextMutationID(): number { return this.mutationID + 1; }
get indexes(): readonly IndexRecord[] { // Already validated! return this.chunk.data.indexes; }}
/** * Returns the set of local commits from the given from_commit_hash back to but not * including its base snapshot. If from_commit_hash is a snapshot, the returned vector * will be empty. When, as typical, from_commit_hash is the head of the default chain * then the returned commits are the set of pending commits, ie the set of local commits * that have not yet been pushed to the data layer. * * The vector of commits is returned in reverse chain order, that is, starting * with the commit with hash from_commit_hash and walking backwards. */export async function localMutations( fromCommitHash: Hash, dagRead: dag.Read,): Promise<Commit<LocalMeta>[]> { const commits = await chain(fromCommitHash, dagRead); // Filter does not deal with type narrowing. return commits.filter(c => c.isLocal()) as Commit<LocalMeta>[];}
export async function baseSnapshot( hash: Hash, dagRead: dag.Read,): Promise<Commit<SnapshotMeta>> { let commit = await fromHash(hash, dagRead); while (!commit.isSnapshot()) { const {meta} = commit; const {basisHash} = meta; if (basisHash === null) { throw new Error(`Commit ${commit.chunk.hash} has no basis`); } commit = await fromHash(basisHash, dagRead); } return commit;}
export function snapshotMetaParts( c: Commit<SnapshotMeta>,): [lastMutationID: number, cookie: ReadonlyJSONValue] { const m = c.meta; return [m.lastMutationID, m.cookieJSON];}
/** * Returns all commits from the commit with from_commit_hash to its base * snapshot, inclusive of both. Resulting vector is in chain-head-first order * (so snapshot comes last). */export async function chain( fromCommitHash: Hash, dagRead: dag.Read,): Promise<Commit<Meta>[]> { let commit = await fromHash(fromCommitHash, dagRead); const commits = []; while (!commit.isSnapshot()) { const {meta} = commit; const {basisHash} = meta; if (basisHash === null) { throw new Error(`Commit ${commit.chunk.hash} has no basis`); } commits.push(commit); commit = await fromHash(basisHash, dagRead); } commits.push(commit); return commits;}
export async function fromHash( hash: Hash, dagRead: dag.Read,): Promise<Commit<Meta>> { const chunk = await dagRead.mustGetChunk(hash); return fromChunk(chunk);}
export async function fromHead( name: string, dagRead: dag.Read,): Promise<Commit<Meta>> { const hash = await dagRead.getHead(name); assert(hash, `Missing head ${name}`); return fromHash(hash, dagRead);}
type BasisHash = { readonly basisHash: Hash | null;};
export type IndexChangeMeta = BasisHash & { readonly type: MetaTyped.IndexChange; readonly lastMutationID: number;};
function assertIndexChangeMeta( v: Record<string, unknown>,): asserts v is IndexChangeMeta { // type already asserted assertNumber(v.lastMutationID);
// Note: indexes are already validated for all commit types. Only additional // things to validate are: // - last_mutation_id is equal to the basis // - value_hash has not been changed // However we don't have a write transaction this deep, so these validated at // commit time.}
export type LocalMeta = BasisHash & { readonly type: MetaTyped.Local; readonly mutationID: number; readonly mutatorName: string; readonly mutatorArgsJSON: ReadonlyJSONValue; readonly originalHash: Hash | null; readonly timestamp: number;};
function assertLocalMeta(v: Record<string, unknown>): asserts v is LocalMeta { // type already asserted assertNumber(v.mutationID); assertString(v.mutatorName); if (!v.mutatorName) { throw new Error('Missing mutator name'); } assertJSONValue(v.mutatorArgsJSON); if (v.originalHash !== null) { assertHash(v.originalHash); } assertNumber(v.timestamp);}
export type SnapshotMeta = BasisHash & { readonly type: MetaTyped.Snapshot; readonly lastMutationID: number; readonly cookieJSON: ReadonlyJSONValue;};
function assertSnapshotMeta( v: Record<string, unknown>,): asserts v is SnapshotMeta { // type already asserted assertNumber(v.lastMutationID); assertJSONValue(v.cookieJSON);}
export type Meta = IndexChangeMeta | LocalMeta | SnapshotMeta;
function assertMeta(v: unknown): asserts v is Meta { assertObject(v); if (v.basisHash !== null) { assertString(v.basisHash); }
assertNumber(v.type); switch (v.type) { case MetaTyped.IndexChange: assertIndexChangeMeta(v); break; case MetaTyped.Local: assertLocalMeta(v); break; case MetaTyped.Snapshot: assertSnapshotMeta(v); break; default: throw new Error(`Invalid enum value ${v.type}`); }}
export type IndexDefinition = { readonly name: string; // keyPrefix describes a subset of the primary key to index readonly keyPrefix: string; // jsonPointer describes the (sub-)value to index (secondary index) readonly jsonPointer: string;};
function assertIndexDefinition(v: unknown): asserts v is IndexDefinition { assertObject(v); assertString(v.name); assertString(v.keyPrefix); assertString(v.jsonPointer);}
export type IndexRecord = { readonly definition: IndexDefinition; readonly valueHash: Hash;};
function assertIndexRecord(v: unknown): asserts v is IndexRecord { assertObject(v); assertIndexDefinition(v.definition); assertString(v.valueHash);}
export function newLocal( createChunk: dag.CreateChunk, basisHash: Hash | null, mutationID: number, mutatorName: string, mutatorArgsJSON: ReadonlyJSONValue, originalHash: Hash | null, valueHash: Hash, indexes: readonly IndexRecord[], timestamp: number,): Commit<LocalMeta> { const meta: LocalMeta = { type: MetaTyped.Local, basisHash, mutationID, mutatorName, mutatorArgsJSON, originalHash, timestamp, }; return commitFromCommitData(createChunk, {meta, valueHash, indexes});}
export function newSnapshot( createChunk: dag.CreateChunk, basisHash: Hash | null, lastMutationID: number, cookieJSON: ReadonlyJSONValue, valueHash: Hash, indexes: readonly IndexRecord[],): Commit<SnapshotMeta> { return commitFromCommitData( createChunk, newSnapshotCommitData( basisHash, lastMutationID, cookieJSON, valueHash, indexes, ), );}
export function newSnapshotCommitData( basisHash: Hash | null, lastMutationID: number, cookieJSON: ReadonlyJSONValue, valueHash: Hash, indexes: readonly IndexRecord[],): CommitData<SnapshotMeta> { const meta: SnapshotMeta = { type: MetaTyped.Snapshot, basisHash, lastMutationID, cookieJSON, }; return {meta, valueHash, indexes};}
export function newIndexChange( createChunk: dag.CreateChunk, basisHash: Hash | null, lastMutationID: number, valueHash: Hash, indexes: readonly IndexRecord[],): Commit<IndexChangeMeta> { const meta: IndexChangeMeta = { type: MetaTyped.IndexChange, basisHash, lastMutationID, }; return commitFromCommitData(createChunk, {meta, valueHash, indexes});}
export function fromChunk(chunk: dag.Chunk): Commit<Meta> { validateChunk(chunk); return new Commit(chunk);}
function commitFromCommitData<M extends Meta>( createChunk: dag.CreateChunk, data: CommitData<M>,): Commit<M> { return new Commit(createChunk(data, getRefs(data)));}
export function getRefs(data: CommitData<Meta>): Hash[] { const refs: Hash[] = [data.valueHash]; const {meta} = data; switch (meta.type) { case MetaTyped.IndexChange: meta.basisHash && refs.push(meta.basisHash); break; case MetaTyped.Local: meta.basisHash && refs.push(meta.basisHash); // Local has weak originalHash break; case MetaTyped.Snapshot: // Snapshot has weak basisHash break; }
for (const index of data.indexes) { refs.push(index.valueHash); }
return refs;}
export type CommitData<M extends Meta> = { readonly meta: M; readonly valueHash: Hash; readonly indexes: readonly IndexRecord[];};
export function assertCommitData(v: unknown): asserts v is CommitData<Meta> { if (skipCommitDataAsserts) { return; }
assertObject(v); assertMeta(v.meta); assertString(v.valueHash); assertArray(v.indexes); for (const index of v.indexes) { assertIndexRecord(index); }}
function validateChunk( chunk: dag.Chunk<Value>,): asserts chunk is dag.Chunk<CommitData<Meta>> { const {data} = chunk; assertCommitData(data);
// Indexes is optional const seen = new Set(); for (const index of data.indexes) { const {name} = index.definition; if (seen.has(name)) { throw new Error(`Duplicate index ${name}`); } seen.add(name); }}
replicache

Version Info

Tagged at
2 years ago