deno.land / x / pg_mem@2.8.1 / schema / btree-index.ts

btree-index.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
import { IValue, _IIndex, _ITable, getId, IndexKey, CreateIndexColDef, _Transaction, _Explainer, _IndexExplanation, IndexExpression, IndexOp, Stats, _INamedIndex, Reg, _ISchema } from '../interfaces-private.ts';// @ts-ignoreimport createTree from 'https://deno.land/x/functional_red_black_tree@1.0.1-deno/mod.ts';import { QueryError, NotSupported, nil } from '../interfaces.ts';import { Set as ImSet, Map as ImMap } from 'https://deno.land/x/immutable@4.0.0-rc.12-deno.1/mod.ts';import { deepCloneSimple, nullIsh, hasNullish } from '../utils.ts';

// https://www.npmjs.com/package/functional-red-black-treeinterface BTree<T> { readonly keys: Iterable<IndexKey>; readonly values: Iterable<IndexKey>; readonly length: number; get(key: IndexKey): T; insert(key: IndexKey, value: T): BTree<T>; remove(key: IndexKey): BTree<T>; find(key: IndexKey): BIterator<T>; /** Find the first item in the tree whose key is >= key */ ge(key: IndexKey): BIterator<T>; /** Finds the first item in the tree whose key is > key */ gt(key: IndexKey): BIterator<T>; /** Finds the last item in the tree whose key is < key */ lt(key: IndexKey): BIterator<T>; /** Finds the last item in the tree whose key is <= key */ le(key: IndexKey): BIterator<T>;
at(pos: number): BIterator<T>;
readonly begin: BIterator<T>; readonly end: BIterator<T>; /** If a truthy value is returned from the visitor, then iteration is stopped. */ forEach(fn: (key: IndexKey, value: T) => boolean, low?: number, high?: number): void; // root;}
interface BIterator<T> { readonly key: IndexKey; readonly value: T; // tree; readonly index: any; readonly valid: boolean; clone(): BIterator<T>; remove(): BTree<T>; update(value: T): BTree<T>; next(): void; prev(): void; readonly hasNext: boolean; readonly hasPrev: boolean;}
type RawTree<T> = BTree<ImMap<string, T>>;
export class BIndex<T = any> implements _INamedIndex<T> {
get type(): 'index' { return 'index'; }
readonly reg: Reg;
// private asBinary: RawTree; expressions: (IndexExpression & IValue)[]; private treeBinId = Symbol(); private treeCountId = Symbol();

get ownerSchema(): _ISchema { return this.onTable.ownerSchema; }
constructor(t: _Transaction , readonly name: string , readonly cols: readonly CreateIndexColDef[] , readonly onTable: _ITable<T> , readonly hash: string , readonly unique: boolean , readonly notNull: boolean , readonly predicate: IValue | nil) { this.reg = onTable.ownerSchema._reg_register(this); this.truncate(t); this.expressions = cols.map(x => x.value); }
drop(t: _Transaction): void { this.onTable.dropIndex(t, this.name); }
compare(_a: any, _b: any) { for (let i = 0; i < this.expressions.length; i++) { const k = this.cols[i]; const a = _a[i]; const b = _b[i]; const an = nullIsh(a); const bn = nullIsh(b); if (an || bn) { if (an === bn) { continue; } return (an ? -1 : 1) * (k.nullsLast ? 1 : -1); } if (k.value.type.equals(a, b)) { continue; } return (k.value.type.gt(a, b) ? 1 : -1)// * (k.desc ? -1 : 1); } return 0; }
buildKey(raw: any, t: _Transaction): any[] { return this.expressions.map(k => k.get(raw, t)); }
truncate(t: _Transaction) { const asBinary = createTree((a: any, b: any) => { return this.compare(a, b); }); this.setBin(t, asBinary); }
dropFromData(t: _Transaction) { t.delete(this.treeBinId); }
private bin(t: _Transaction) { return t.get<RawTree<T>>(this.treeBinId); } private setBin(t: _Transaction, val: RawTree<T>) { return t.set(this.treeBinId, val); }
private setCount(t: _Transaction, val: number) { return t.set(this.treeCountId, val); } private getCount(t: _Transaction): number { return t.get<number>(this.treeCountId) ?? 0; }
hasKey(key: IndexKey[], t: _Transaction): boolean { const it = this.bin(t).find(key); return it.valid; }
add(raw: T, t: _Transaction) { // check that predicate is OK if (this.predicate) { const val = this.predicate.get(raw, t); if (nullIsh(val) || val === false) { return; } }
// build key and object id const id = getId(raw); const key = this.buildKey(raw, t); const hasNil = hasNullish(...key); if (this.notNull && hasNil) { throw new QueryError('Cannot add a null record in index ' + this.name); } if (this.unique && !hasNil && this.hasKey(key, t)) { const idCols = this.cols.map(it => it.value.id); throw new QueryError({ error: `insert into "${this.onTable.name}" (${Object.keys(raw as any).join(', ')}) ` + `values (${Object.keys(raw as any).map((_, i) => `$${i + 1}`).join(', ')}) returning "${idCols}" ` + `- duplicate key value violates unique constraint "${this.onTable.name}_pkey"`, details: `Key (${idCols})=(${key}) already exists.`, code: '23505' }); } // get tree let tree = this.bin(t); // get key in tree let keyValues = tree.find(key); if (keyValues.valid) { if (keyValues.value.has(id)) { return; // already exists } tree = keyValues.update(keyValues.value.set(id, raw)); } else { tree = tree.insert(key, ImMap<string, T>().set(id, raw)); } this.setBin(t, tree); this.setCount(t, this.getCount(t) + 1);
}
delete(raw: any, t: _Transaction) { const key = this.buildKey(raw, t); let tree = this.bin(t); let keyValues = tree.find(key); if (!keyValues.valid) { return; // key does not exists } const id = getId(raw); if (!keyValues.value.has(id)) { return; // element does not exists } const newKeyValues = keyValues.value.delete(id); if (!newKeyValues.size) { tree = keyValues.remove(); } else { tree = keyValues.update(newKeyValues); } this.setBin(t, tree); this.setCount(t, this.getCount(t) - 1); }
eqFirst(rawKey: IndexKey, t: _Transaction): T | null { for (const r of this.eq(rawKey, t, false)) { return deepCloneSimple(r); } return null; }

*nin(rawKey: IndexKey[], t: _Transaction): Iterable<T> { rawKey.sort((a, b) => this.compare(a, b)); const kit = rawKey[Symbol.iterator](); let cur = kit.next(); const bin = this.bin(t); let it = bin.begin; while (!cur.done) { // yield previous while (it.valid && this.compare(it.key, cur.value) < 0) { yield* it.value.values(); it.next(); } // skip equals if (this.compare(it.key, cur.value) === 0) { it = bin.gt(cur.value); } cur = kit.next(); }
// finish while (it.valid) { yield* it.value.values(); it.next(); } }

entropy(op: IndexOp) { const bin = this.bin(op.t); if (!bin.length) { return 0; } const all = op.t.get<number>(this.treeCountId) ?? 0; // evaluate number of keys included in this operation const e = this._keyCount(op); // multiply by average values per key return e * all / bin.length; }
stats(t: _Transaction, key?: IndexKey): Stats { if (!key) { return { count: t.get<number>(this.treeCountId) ?? 0, }; } const found = this.bin(t).get(key); return { count: found?.size ?? 0, }; }
iterateKeys(t: _Transaction): Iterable<IndexKey> { const bin = this.bin(t); return bin.keys; }

private _keyCount(op: IndexOp) { const bin = this.bin(op.t); switch (op.type) { case 'eq': { const begin = bin.find(op.key); if (!begin.valid) { return 0; } const end = bin.gt(op.key); if (!end.valid) { return bin.length - begin.index; } return end.index - begin.index + 1; } case 'neq': { let cnt = 0; const first = bin.find(op.key); if (!first.valid) { return bin.length; } cnt += first.valid ? first.index : 0; const end = bin.gt(op.key); cnt += end.valid ? (bin.length - end.index) : 0; return cnt; } case 'ge': { const found = bin.ge(op.key); return found.valid ? (bin.length - found.index) : 0; } case 'gt': { const found = bin.gt(op.key); return found.valid ? (bin.length - found.index) : 0; } case 'le': { const found = bin.gt(op.key); return found.valid ? found.index : bin.length; } case 'lt': { const found = bin.ge(op.key); return found.valid ? found.index : bin.length; } case 'inside': { const begin = bin.ge(op.lo); if (!begin.valid) { return 0; } const end = bin.gt(op.hi); if (!end.valid) { return bin.length - begin.index; } return end.index - begin.index; } case 'outside': { let cnt = 0; const first = bin.lt(op.lo); cnt += first.valid ? first.index + 1 : 0; const end = bin.gt(op.hi); cnt += end.valid ? (bin.length - end.index) : 0; return cnt; } case 'nin': { let cnt = bin.length; for (const e of op.keys) { const f = bin.find(e); if (f.valid) { cnt--; } } return cnt; } default: throw NotSupported.never(op['type']); } }
*enumerate(op: IndexOp): Iterable<T> { for (const x of this._enumerate(op)) { yield deepCloneSimple(x); } } private _enumerate(op: IndexOp): Iterable<T> { switch (op.type) { case 'eq': return this.eq(op.key, op.t, op.matchNull!); case 'neq': return this.neq(op.key, op.t, op.matchNull!); case 'ge': return this.ge(op.key, op.t); case 'le': return this.le(op.key, op.t); case 'gt': return this.gt(op.key, op.t); case 'lt': return this.lt(op.key, op.t); case 'outside': return this.outside(op.lo, op.hi, op.t); case 'inside': return this.inside(op.lo, op.hi, op.t); case 'nin': return this.nin(op.keys, op.t); default: throw NotSupported.never(op['type']); } }
*eq(key: IndexKey, t: _Transaction, matchNull: boolean): Iterable<T> { if (!matchNull && key.some(nullIsh)) { return; } const it = this.bin(t).find(key); while (it.valid && this.compare(it.key, key) === 0) { yield* it.value.values(); it.next(); } }


*neq(key: IndexKey, t: _Transaction, matchNull: boolean): Iterable<T> { if (!matchNull && key.some(nullIsh)) { return; } // yield before const bin = this.bin(t); let it = bin.begin; while (it.valid && this.compare(it.key, key) < 0) { yield* it.value.values(); it.next(); } // yield after it = bin.gt(key); while (it.valid) { yield* it.value.values(); it.next(); } }
*gt(key: IndexKey, t: _Transaction): Iterable<T> { const it = this.bin(t).gt(key); while (it.valid) { yield* it.value.values(); it.next(); } }
*ge(key: IndexKey, t: _Transaction): Iterable<T> { const it = this.bin(t).ge(key); while (it.valid) { yield* it.value.values(); it.next(); } }
*lt(key: IndexKey, t: _Transaction): Iterable<T> { const bin = this.bin(t); const limit = bin.lt(key); const it = bin.begin; if (!limit.valid) { // yield all while (it.valid) { yield* it.value.values(); it.next(); } return; } while (it.valid && limit.index >= it.index) { yield* it.value.values(); it.next(); } // const it = this.asBinary.lt(key); // while (it.valid) { // yield* it.value.values(); // it.prev(); // } }
*le(key: IndexKey, t: _Transaction): Iterable<T> { const bin = this.bin(t); const limit = bin.le(key); const it = bin.begin; if (!limit.valid) { // yield all while (it.valid) { yield* it.value.values(); it.next(); } return; } while (it.valid && limit.index >= it.index) { yield* it.value.values(); it.next(); } // const it = this.asBinary.le(key); // while (it.valid) { // yield* it.value.values(); // it.prev(); // } }
*outside(lo: IndexKey, hi: IndexKey, t: _Transaction): Iterable<T> { yield* this.lt(lo, t); yield* this.gt(hi, t); }
*inside(lo: IndexKey, hi: IndexKey, t: _Transaction): Iterable<T> { const it = this.bin(t).ge(lo); while (it.valid && this.compare(it.key, hi) <= 0) { yield* it.value.values(); it.next(); } }
explain(e: _Explainer): _IndexExplanation { return { _: 'btree', onTable: this.onTable.name, btree: this.expressions.map(x => x.id!), } }}
pg_mem

Version Info

Tagged at
4 months ago