deno.land / x / replicache@v10.0.0-beta.0 / btree / node.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import {assertJSONValue, JSONValue, ReadonlyJSONValue} from '../json';import {assert, assertArray, assertNumber, assertString} from '../asserts';import {Hash, emptyHash, newTempHash} from '../hash';import type {BTreeRead} from './read';import type {BTreeWrite} from './write';import {skipBTreeNodeAsserts} from '../config.js';
export type Entry<V> = [key: string, value: V];export type ReadonlyEntry<V> = readonly [key: string, value: V];
export const NODE_LEVEL = 0;export const NODE_ENTRIES = 1;
/** * The type of B+Tree node chunk data */type BaseNode<V> = readonly [level: number, entries: ReadonlyArray<Entry<V>>];
export type InternalNode = BaseNode<Hash>;
export type DataNode = BaseNode<ReadonlyJSONValue>;
export type Node = DataNode | InternalNode;
export function getRefs(node: Node): ReadonlyArray<Hash> { return isInternalNode(node) ? node[NODE_ENTRIES].map(e => e[1]) : [];}
export const enum DiffResultOp { Add, Delete, Change,}
export type DiffResult<V> = | { op: DiffResultOp.Add; key: string; newValue: V; } | { op: DiffResultOp.Delete; key: string; oldValue: V; } | { op: DiffResultOp.Change; key: string; oldValue: V; newValue: V; };
/** * Finds the leaf where a key is (if present) or where it should go if not * present. */export async function findLeaf( key: string, hash: Hash, source: BTreeRead,): Promise<DataNodeImpl> { const node = await source.getNode(hash); if (isDataNodeImpl(node)) { return node; } const {entries} = node; let i = binarySearch(key, entries); if (i < 0) { // not found i = ~i; } if (i === entries.length) { i--; } const entry = entries[i]; return findLeaf(key, entry[1], source);}
/** * Does a binary search over entries * * If the key found then the return value is the index it was found at. * * If the key was *not* found then the return value is the index where it should * be inserted at bitwise or'ed (`~index`). This is the same as `-index -1`. For * example if not found and needs to be inserted at `0` then we return `-1`. */export function binarySearch<V>( key: string, entries: ReadonlyArray<ReadonlyEntry<V>>,): number { let {length} = entries; if (length === 0) { return ~0; } let base = 0;
while (length > 1) { const half = length >> 1; const mid = base + half; // mid is always in [0, size), that means mid is >= 0 and < size. // mid >= 0: by definition // mid < size: mid = size / 2 + size / 4 + size / 8 ... const midKey = entries[mid][0]; if (midKey <= key) { base = mid; } length -= half; }
// base is always in [0, size) because base <= mid. const baseKey = entries[base][0]; if (baseKey === key) { return base; } const index = base + (baseKey < key ? 1 : 0); return ~index;}
export function assertBTreeNode( v: unknown,): asserts v is InternalNode | DataNode { if (skipBTreeNodeAsserts) { return; } assertArray(v);
function assertEntry( v: unknown, f: | ((v: unknown) => asserts v is Hash) | ((v: unknown) => asserts v is JSONValue), ): asserts v is Entry<Hash | JSONValue> { assertArray(v); assertString(v[0]); f(v[1]); }
assert(v.length >= 2); const [level, entries] = v;
assertNumber(level); assertArray(entries); if (level > 0) { entries.forEach(e => assertEntry(e, assertString)); } else { entries.forEach(e => assertEntry(e, assertJSONValue)); }}
export function isInternalNode(node: Node): node is InternalNode { return node[NODE_LEVEL] > 0;}
abstract class NodeImpl<Value extends Hash | ReadonlyJSONValue> { entries: Array<Entry<Value>>; hash: Hash; abstract readonly level: number; readonly isMutable: boolean;
constructor(entries: Array<Entry<Value>>, hash: Hash, isMutable: boolean) { this.entries = entries; this.hash = hash; this.isMutable = isMutable; }
abstract set( key: string, value: Value, tree: BTreeWrite, ): Promise<NodeImpl<Value>>;
abstract del( key: string, tree: BTreeWrite, ): Promise<NodeImpl<Value> | DataNodeImpl>;
maxKey(): string { return this.entries[this.entries.length - 1][0]; }
toChunkData(): Node { return [this.level, this.entries]; }}
export class DataNodeImpl extends NodeImpl<ReadonlyJSONValue> { readonly level = 0;
async set( key: string, value: ReadonlyJSONValue, tree: BTreeWrite, ): Promise<DataNodeImpl> { let deleteCount: number; let i = binarySearch(key, this.entries); if (i < 0) { // Not found, insert. i = ~i; deleteCount = 0; } else { deleteCount = 1; }
return this._splice(tree, i, deleteCount, [key, value]); }
private _splice( tree: BTreeWrite, start: number, deleteCount: number, ...items: Entry<ReadonlyJSONValue>[] ): DataNodeImpl { if (this.isMutable) { this.entries.splice(start, deleteCount, ...items); tree.updateNode(this); return this; }
const entries = readonlySplice(this.entries, start, deleteCount, ...items); return tree.newDataNodeImpl(entries); }
async del(key: string, tree: BTreeWrite): Promise<DataNodeImpl> { const i = binarySearch(key, this.entries); if (i < 0) { // Not found. Return this without changes. return this; }
// Found. Create new node or mutate existing one. return this._splice(tree, i, 1); }
async *keys(_tree: BTreeRead): AsyncGenerator<string, void> { for (const entry of this.entries) { yield entry[0]; } }
async *entriesIter( _tree: BTreeRead, ): AsyncGenerator<ReadonlyEntry<ReadonlyJSONValue>, void> { for (const entry of this.entries) { yield entry; } }}
function readonlySplice<T>( array: ReadonlyArray<T>, start: number, deleteCount: number, ...items: T[]): T[] { const arr = array.slice(0, start); for (let i = 0; i < items.length; i++) { arr.push(items[i]); } for (let i = start + deleteCount; i < array.length; i++) { arr.push(array[i]); } return arr;}
function* joinIterables<T>(...iters: Iterable<T>[]) { for (const iter of iters) { yield* iter; }}
export class InternalNodeImpl extends NodeImpl<Hash> { readonly level: number;
constructor( entries: Array<Entry<Hash>>, hash: Hash, level: number, isMutable: boolean, ) { super(entries, hash, isMutable); this.level = level; }
async set( key: string, value: ReadonlyJSONValue, tree: BTreeWrite, ): Promise<InternalNodeImpl> { let i = binarySearch(key, this.entries); if (i < 0) { i = ~i; if (i >= this.entries.length) { // We are going to insert into last (right most) leaf. i = this.entries.length - 1; } }
const childHash = this.entries[i][1]; const oldChildNode = await tree.getNode(childHash);
const childNode = await oldChildNode.set(key, value, tree);
const childNodeSize = tree.childNodeSize(childNode); if (childNodeSize > tree.maxSize || childNodeSize < tree.minSize) { return this._mergeAndPartition(tree, i, childNode); }
return this._replaceChild(tree, i, [childNode.maxKey(), childNode.hash]); }
/** * This merges the child node entries with previous or next sibling and then * partitions the merged entries. */ private async _mergeAndPartition( tree: BTreeWrite, i: number, childNode: DataNodeImpl | InternalNodeImpl, ): Promise<InternalNodeImpl> { const level = this.level - 1; const thisEntries = this.entries;
let values: Iterable<Entry<Hash> | Entry<ReadonlyJSONValue>>; let startIndex: number; let removeCount: number; if (i > 0) { const hash = thisEntries[i - 1][1]; const previousSibling = await tree.getNode(hash); values = joinIterables(previousSibling.entries, childNode.entries); startIndex = i - 1; removeCount = 2; } else if (i < thisEntries.length - 1) { const hash = thisEntries[i + 1][1]; const nextSibling = await tree.getNode(hash); values = joinIterables(childNode.entries, nextSibling.entries); startIndex = i; removeCount = 2; } else { values = childNode.entries; startIndex = i; removeCount = 1; }
const partitions = partition( values, tree.getEntrySize, tree.minSize - tree.chunkHeaderSize, tree.maxSize - tree.chunkHeaderSize, );
// TODO: There are cases where we can reuse the old nodes. Creating new ones // means more memory churn but also more writes to the underlying KV store. const newEntries: Entry<Hash>[] = []; for (const entries of partitions) { const node = tree.newNodeImpl(entries, level); newEntries.push([node.maxKey(), node.hash]); }
if (this.isMutable) { this.entries.splice(startIndex, removeCount, ...newEntries); tree.updateNode(this); return this; }
const entries = readonlySplice( thisEntries, startIndex, removeCount, ...newEntries, );
return tree.newInternalNodeImpl(entries, this.level); }
private _replaceChild( tree: BTreeWrite, index: number, newEntry: Entry<Hash>, ): InternalNodeImpl { if (this.isMutable) { this.entries.splice(index, 1, newEntry); tree.updateNode(this); return this; } const entries = readonlySplice(this.entries, index, 1, newEntry); return tree.newInternalNodeImpl(entries, this.level); }
async del( key: string, tree: BTreeWrite, ): Promise<InternalNodeImpl | DataNodeImpl> { let i = binarySearch(key, this.entries); if (i < 0) { i = ~i; if (i >= this.entries.length) { // Key is larger than maxKey of rightmost entry so it is not present. return this; } }
const childHash = this.entries[i][1]; const oldChildNode = await tree.getNode(childHash); const oldHash = oldChildNode.hash;
const childNode = await oldChildNode.del(key, tree); if (childNode.hash === oldHash) { return this; }
if (childNode.entries.length === 0) { const entries = readonlySplice(this.entries, i, 1); return tree.newInternalNodeImpl(entries, this.level); }
if (i === 0 && this.entries.length === 1) { return childNode; }
// The child node is still a good size. if (tree.childNodeSize(childNode) > tree.minSize) { // No merging needed. return this._replaceChild(tree, i, [childNode.maxKey(), childNode.hash]); }
// Child node size is too small. return this._mergeAndPartition(tree, i, childNode); }
async *keys(tree: BTreeRead): AsyncGenerator<string, void> { for (const entry of this.entries) { const childNode = await tree.getNode(entry[1]); yield* childNode.keys(tree); } }
async *entriesIter( tree: BTreeRead, ): AsyncGenerator<ReadonlyEntry<ReadonlyJSONValue>, void> { for (const entry of this.entries) { const childNode = await tree.getNode(entry[1]); yield* childNode.entriesIter(tree); } }
async getChildren( start: number, length: number, tree: BTreeRead, ): Promise<Array<InternalNodeImpl | DataNodeImpl>> { const ps: Promise<DataNodeImpl | InternalNodeImpl>[] = []; for (let i = start; i < length && i < this.entries.length; i++) { ps.push(tree.getNode(this.entries[i][1])); } return Promise.all(ps); }
async getCompositeChildren( start: number, length: number, tree: BTreeRead, ): Promise<InternalNodeImpl | DataNodeImpl> { const {level} = this;
if (length === 0) { return new InternalNodeImpl([], newTempHash(), level - 1, true); }
const output = await this.getChildren(start, start + length, tree);
if (level > 1) { const entries: Entry<Hash>[] = []; for (const child of output as InternalNodeImpl[]) { entries.push(...child.entries); } return new InternalNodeImpl(entries, newTempHash(), level - 1, true); }
assert(level === 1); const entries: Entry<ReadonlyJSONValue>[] = []; for (const child of output as DataNodeImpl[]) { entries.push(...child.entries); } return new DataNodeImpl(entries, newTempHash(), true); }}
export function newNodeImpl( entries: Array<Entry<ReadonlyJSONValue>>, hash: Hash, level: number, isMutable: boolean,): DataNodeImpl;export function newNodeImpl( entries: Array<Entry<Hash>>, hash: Hash, level: number, isMutable: boolean,): InternalNodeImpl;export function newNodeImpl( entries: Array<Entry<ReadonlyJSONValue>> | Array<Entry<Hash>>, hash: Hash, level: number, isMutable: boolean,): DataNodeImpl | InternalNodeImpl;export function newNodeImpl( entries: Array<Entry<ReadonlyJSONValue>> | Array<Entry<Hash>>, hash: Hash, level: number, isMutable: boolean,): DataNodeImpl | InternalNodeImpl { if (level === 0) { return new DataNodeImpl(entries, hash, isMutable); } return new InternalNodeImpl(entries as Entry<Hash>[], hash, level, isMutable);}
export function isDataNodeImpl( node: DataNodeImpl | InternalNodeImpl,): node is DataNodeImpl { return node.level === 0;}
export function partition<T>( values: Iterable<T>, getValueSize: (v: T) => number, min: number, max: number,): T[][] { const partitions: T[][] = []; const sizes: number[] = []; let sum = 0; let accum: T[] = []; for (const value of values) { // for (let i = 0; i < values.length; i++) { const size = getValueSize(value); if (size >= max) { if (accum.length > 0) { partitions.push(accum); sizes.push(sum); } partitions.push([value]); sizes.push(size); sum = 0; accum = []; } else if (sum + size >= min) { accum.push(value); partitions.push(accum); sizes.push(sum + size); sum = 0; accum = []; } else { sum += size; accum.push(value); } }
if (sum > 0) { if (sizes.length > 0 && sum + sizes[sizes.length - 1] <= max) { partitions[partitions.length - 1].push(...accum); } else { partitions.push(accum); } }
return partitions;}
export const emptyDataNode: DataNode = [0, []];export const emptyDataNodeImpl = new DataNodeImpl([], emptyHash, false);
replicache

Version Info

Tagged at
2 years ago