@hviana/sema 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +6 -5
- package/CITATION.cff +49 -0
- package/HOW_IT_WORKS.md +11 -12
- package/README.md +7 -5
- package/dist/example/demo.js +25 -25
- package/dist/example/train_base.d.ts +19 -21
- package/dist/example/train_base.js +1700 -1952
- package/dist/src/alphabet.d.ts +3 -3
- package/dist/src/alphabet.js +24 -27
- package/dist/src/alu/src/alu.d.ts +169 -194
- package/dist/src/alu/src/alu.js +374 -414
- package/dist/src/alu/src/expr.d.ts +36 -43
- package/dist/src/alu/src/expr.js +252 -278
- package/dist/src/alu/src/index.d.ts +9 -98
- package/dist/src/alu/src/index.js +10 -64
- package/dist/src/alu/src/kernel-arith.d.ts +1 -5
- package/dist/src/alu/src/kernel-arith.js +195 -285
- package/dist/src/alu/src/kernel-bits.d.ts +1 -5
- package/dist/src/alu/src/kernel-bits.js +81 -115
- package/dist/src/alu/src/kernel-logic.js +33 -63
- package/dist/src/alu/src/kernel-nd.js +155 -195
- package/dist/src/alu/src/kernel-numeric.d.ts +16 -80
- package/dist/src/alu/src/kernel-numeric.js +259 -331
- package/dist/src/alu/src/operation.d.ts +112 -132
- package/dist/src/alu/src/operation.js +149 -163
- package/dist/src/alu/src/parser.d.ts +168 -175
- package/dist/src/alu/src/parser.js +382 -421
- package/dist/src/alu/src/resonance.d.ts +16 -25
- package/dist/src/alu/src/resonance.js +49 -56
- package/dist/src/alu/src/text.d.ts +3 -7
- package/dist/src/alu/src/text.js +31 -37
- package/dist/src/alu/src/value.d.ts +14 -14
- package/dist/src/alu/src/value.js +150 -160
- package/dist/src/alu/test/alu.test.js +543 -656
- package/dist/src/bytes.d.ts +1 -5
- package/dist/src/bytes.js +33 -40
- package/dist/src/config.d.ts +99 -102
- package/dist/src/config.js +78 -83
- package/dist/src/derive/src/deduction.d.ts +60 -62
- package/dist/src/derive/src/deduction.js +103 -109
- package/dist/src/derive/src/index.d.ts +1 -7
- package/dist/src/derive/src/priority-queue.d.ts +8 -8
- package/dist/src/derive/src/priority-queue.js +57 -61
- package/dist/src/derive/src/rewrite.d.ts +15 -18
- package/dist/src/derive/src/rewrite.js +59 -67
- package/dist/src/derive/src/trie.d.ts +56 -56
- package/dist/src/derive/src/trie.js +175 -179
- package/dist/src/derive/test/derive.test.js +96 -100
- package/dist/src/extension.d.ts +13 -16
- package/dist/src/geometry.d.ts +29 -43
- package/dist/src/geometry.js +324 -268
- package/dist/src/index.d.ts +2 -17
- package/dist/src/index.js +2 -12
- package/dist/src/ingest-cache.d.ts +25 -30
- package/dist/src/ingest-cache.js +108 -127
- package/dist/src/mind/articulation.d.ts +1 -5
- package/dist/src/mind/articulation.js +77 -112
- package/dist/src/mind/attention.d.ts +40 -108
- package/dist/src/mind/attention.js +754 -872
- package/dist/src/mind/canonical.d.ts +4 -19
- package/dist/src/mind/canonical.js +27 -31
- package/dist/src/mind/graph-search.d.ts +201 -225
- package/dist/src/mind/graph-search.js +742 -821
- package/dist/src/mind/index.d.ts +2 -10
- package/dist/src/mind/junction.d.ts +31 -58
- package/dist/src/mind/junction.js +172 -237
- package/dist/src/mind/learning.d.ts +16 -52
- package/dist/src/mind/learning.js +143 -165
- package/dist/src/mind/match.d.ts +20 -69
- package/dist/src/mind/match.js +259 -318
- package/dist/src/mind/mechanisms/alu.js +16 -16
- package/dist/src/mind/mechanisms/cast.d.ts +9 -13
- package/dist/src/mind/mechanisms/cast.js +363 -456
- package/dist/src/mind/mechanisms/confluence.d.ts +8 -12
- package/dist/src/mind/mechanisms/confluence.js +152 -183
- package/dist/src/mind/mechanisms/cover.d.ts +2 -8
- package/dist/src/mind/mechanisms/cover.js +148 -210
- package/dist/src/mind/mechanisms/extraction.d.ts +8 -34
- package/dist/src/mind/mechanisms/extraction.js +234 -288
- package/dist/src/mind/mechanisms/recall.d.ts +6 -10
- package/dist/src/mind/mechanisms/recall.js +141 -185
- package/dist/src/mind/mind.d.ts +194 -155
- package/dist/src/mind/mind.js +392 -289
- package/dist/src/mind/pipeline-mechanism.d.ts +112 -124
- package/dist/src/mind/pipeline-mechanism.js +161 -172
- package/dist/src/mind/pipeline.d.ts +4 -14
- package/dist/src/mind/pipeline.js +125 -189
- package/dist/src/mind/primitives.d.ts +20 -33
- package/dist/src/mind/primitives.js +124 -120
- package/dist/src/mind/rationale.d.ts +86 -98
- package/dist/src/mind/rationale.js +113 -121
- package/dist/src/mind/reasoning.d.ts +2 -13
- package/dist/src/mind/reasoning.js +129 -166
- package/dist/src/mind/recognition.d.ts +1 -4
- package/dist/src/mind/recognition.js +198 -210
- package/dist/src/mind/resonance.d.ts +5 -22
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +6 -25
- package/dist/src/mind/trace.js +50 -58
- package/dist/src/mind/traverse.d.ts +29 -58
- package/dist/src/mind/traverse.js +344 -356
- package/dist/src/mind/types.d.ts +122 -127
- package/dist/src/mind/types.js +60 -69
- package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
- package/dist/src/rabitq-ivf/src/database.js +201 -0
- package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
- package/dist/src/{rabitq-hnsw → rabitq-ivf}/src/index.js +1 -3
- package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
- package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
- package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
- package/dist/src/rabitq-ivf/src/prng.js +36 -0
- package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
- package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
- package/dist/src/sema.d.ts +9 -13
- package/dist/src/sema.js +26 -40
- package/dist/src/store-sqlite.d.ts +171 -166
- package/dist/src/store-sqlite.js +768 -662
- package/dist/src/store.d.ts +597 -630
- package/dist/src/store.js +1434 -1581
- package/dist/src/vec.d.ts +5 -9
- package/dist/src/vec.js +61 -73
- package/example/train_base.ts +33 -15
- package/index.html +65 -0
- package/package.json +1 -1
- package/src/alu/README.md +1 -1
- package/src/alu/src/index.ts +1 -1
- package/src/config.ts +19 -27
- package/src/geometry.ts +93 -1
- package/src/index.ts +6 -11
- package/src/mind/attention.ts +11 -6
- package/src/mind/mechanisms/recall.ts +29 -13
- package/src/mind/mind.ts +250 -19
- package/src/mind/primitives.ts +43 -6
- package/src/mind/recognition.ts +26 -8
- package/src/mind/traverse.ts +47 -0
- package/src/mind/types.ts +20 -21
- package/src/rabitq-ivf/README.md +56 -0
- package/src/rabitq-ivf/src/database.ts +276 -0
- package/src/{rabitq-hnsw → rabitq-ivf}/src/index.ts +2 -5
- package/src/rabitq-ivf/src/ivf.ts +1330 -0
- package/src/{rabitq-hnsw → rabitq-ivf}/src/prng.ts +1 -1
- package/src/store-sqlite.ts +204 -9
- package/src/store.ts +27 -34
- package/test/08-storage.test.mjs +3 -3
- package/test/13-conversation.test.mjs +190 -20
- package/test/14-scaling.test.mjs +2 -2
- package/test/35-ivf.test.mjs +263 -0
- package/test/36-bloom.test.mjs +123 -0
- package/dist/src/rabitq-hnsw/src/database.d.ts +0 -202
- package/dist/src/rabitq-hnsw/src/database.js +0 -405
- package/dist/src/rabitq-hnsw/src/heap.d.ts +0 -22
- package/dist/src/rabitq-hnsw/src/heap.js +0 -94
- package/dist/src/rabitq-hnsw/src/hnsw.d.ts +0 -125
- package/dist/src/rabitq-hnsw/src/hnsw.js +0 -500
- package/dist/src/rabitq-hnsw/src/index.d.ts +0 -15
- package/dist/src/rabitq-hnsw/src/prng.d.ts +0 -19
- package/dist/src/rabitq-hnsw/src/prng.js +0 -38
- package/dist/src/rabitq-hnsw/src/rabitq.d.ts +0 -95
- package/dist/src/rabitq-hnsw/src/rabitq.js +0 -299
- package/dist/src/rabitq-hnsw/src/store.d.ts +0 -162
- package/dist/src/rabitq-hnsw/src/store.js +0 -916
- package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +0 -1
- package/dist/src/rabitq-hnsw/test/hnsw.test.js +0 -1197
- package/src/rabitq-hnsw/README.md +0 -303
- package/src/rabitq-hnsw/src/database.ts +0 -492
- package/src/rabitq-hnsw/src/heap.ts +0 -90
- package/src/rabitq-hnsw/src/hnsw.ts +0 -514
- package/src/rabitq-hnsw/src/store.ts +0 -994
- package/src/rabitq-hnsw/test/hnsw.test.ts +0 -1213
- /package/src/{rabitq-hnsw → rabitq-ivf}/src/rabitq.ts +0 -0
package/dist/src/store-sqlite.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// store-sqlite.ts — SQLite + rabitq-
|
|
1
|
+
// store-sqlite.ts — SQLite + rabitq-ivf persistence adapter.
|
|
2
2
|
//
|
|
3
3
|
// SQliteStore extends AbstractStore and implements only the bare essentials
|
|
4
4
|
// required for database communication: SQL schema, prepared statements, and
|
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
// vectors) is an independent file. The vector databases persist to their
|
|
28
28
|
// own files on every write — no separate serialisation step is needed.
|
|
29
29
|
import { DatabaseSync } from "node:sqlite";
|
|
30
|
-
import { AbstractStore, flatBytesKids, packKids, unpackKids } from "./store.js";
|
|
30
|
+
import { AbstractStore, flatBytesKids, packKids, unpackKids, } from "./store.js";
|
|
31
31
|
import { DEFAULT_CONFIG } from "./config.js";
|
|
32
|
-
import { VectorDatabase } from "./rabitq-
|
|
32
|
+
import { VectorDatabase } from "./rabitq-ivf/src/index.js";
|
|
33
33
|
const SCHEMA = `
|
|
34
34
|
-- Content-addressed lookup goes through ONE small integer index: h is the
|
|
35
35
|
-- FNV-1a hash of the row's content key (leaf bytes, or the packed kid ids of a
|
|
@@ -44,6 +44,21 @@ CREATE TABLE IF NOT EXISTS node (
|
|
|
44
44
|
h INTEGER NOT NULL
|
|
45
45
|
);
|
|
46
46
|
CREATE INDEX IF NOT EXISTS idx_node_h ON node(h);
|
|
47
|
+
-- Negative-lookup BLOOM FILTER over node.h — the RAM structure that lets the
|
|
48
|
+
-- content-addressed dedup probes (findLeaf/findBranch, the training hot path)
|
|
49
|
+
-- answer "definitely new content" without an idx_node_h descent. Persisted
|
|
50
|
+
-- here at checkpoint/close with upto = the next node id NOT covered; on
|
|
51
|
+
-- open, rows with id >= upto (a crash's tail) are re-added by an incremental
|
|
52
|
+
-- rowid-range scan, so the filter can NEVER be missing an inserted hash — a
|
|
53
|
+
-- false negative would silently break hash-consing (duplicate mints), while
|
|
54
|
+
-- a false positive only costs the SQLite probe that was paid on every call
|
|
55
|
+
-- before the filter existed.
|
|
56
|
+
CREATE TABLE IF NOT EXISTS bloom (
|
|
57
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
58
|
+
bits BLOB NOT NULL,
|
|
59
|
+
n INTEGER NOT NULL,
|
|
60
|
+
upto INTEGER NOT NULL
|
|
61
|
+
);
|
|
47
62
|
-- The reverse structural edge child→parent. A WITHOUT ROWID table clustered on
|
|
48
63
|
-- (child, parent) IS the lookup index: parents(child) is one B-tree descent to a
|
|
49
64
|
-- contiguous run, with no separate rowid heap and no secondary index to maintain
|
|
@@ -106,277 +121,413 @@ CREATE TABLE IF NOT EXISTS meta (
|
|
|
106
121
|
val TEXT NOT NULL
|
|
107
122
|
);
|
|
108
123
|
`;
|
|
124
|
+
/** Blocked Bloom filter over 32-bit content hashes. k=4 bit positions per
|
|
125
|
+
* key via double hashing; at the maintained ≥12 bits/key the false-positive
|
|
126
|
+
* rate stays under ~1%, so ≥99% of miss-probes skip SQLite entirely.
|
|
127
|
+
* No deletions (nodes are never deleted), so growth is a rebuild. */
|
|
128
|
+
class NodeBloom {
|
|
129
|
+
bits;
|
|
130
|
+
mask;
|
|
131
|
+
n = 0;
|
|
132
|
+
constructor(log2bits) {
|
|
133
|
+
this.bits = new Uint8Array(1 << (log2bits - 3));
|
|
134
|
+
this.mask = (1 << log2bits) - 1 >>> 0;
|
|
135
|
+
}
|
|
136
|
+
add(h) {
|
|
137
|
+
const h2 = (Math.imul(h, 0x9e3779b1) | 1) >>> 0;
|
|
138
|
+
let x = h >>> 0;
|
|
139
|
+
const bits = this.bits;
|
|
140
|
+
const mask = this.mask;
|
|
141
|
+
for (let i = 0; i < 4; i++) {
|
|
142
|
+
const b = x & mask;
|
|
143
|
+
bits[b >> 3] |= 1 << (b & 7);
|
|
144
|
+
x = (x + h2) >>> 0;
|
|
145
|
+
}
|
|
146
|
+
this.n++;
|
|
147
|
+
}
|
|
148
|
+
mightContain(h) {
|
|
149
|
+
const h2 = (Math.imul(h, 0x9e3779b1) | 1) >>> 0;
|
|
150
|
+
let x = h >>> 0;
|
|
151
|
+
const bits = this.bits;
|
|
152
|
+
const mask = this.mask;
|
|
153
|
+
for (let i = 0; i < 4; i++) {
|
|
154
|
+
const b = x & mask;
|
|
155
|
+
if ((bits[b >> 3] & (1 << (b & 7))) === 0)
|
|
156
|
+
return false;
|
|
157
|
+
x = (x + h2) >>> 0;
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
/** Whether the filter is due for a bigger rebuild (< 12 bits/key). */
|
|
162
|
+
get saturated() {
|
|
163
|
+
return this.n * 12 > this.bits.length * 8;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** log2 of the bloom bit count sized for `n` keys at 16 bits/key, clamped to
|
|
167
|
+
* [17, 30] — 16 KiB floor, 128 MiB ceiling (past ~90M nodes the FPR is
|
|
168
|
+
* allowed to drift up rather than RAM). */
|
|
169
|
+
function bloomLog2For(n) {
|
|
170
|
+
let log2 = 17;
|
|
171
|
+
while (log2 < 30 && (1 << log2) < n * 16)
|
|
172
|
+
log2++;
|
|
173
|
+
return log2;
|
|
174
|
+
}
|
|
175
|
+
/** Below this node count the filter is NOT persisted: a rebuild scan at open
|
|
176
|
+
* is instant at this size, and the blob would dominate a small store's
|
|
177
|
+
* on-disk footprint. */
|
|
178
|
+
const BLOOM_PERSIST_MIN_NODES = 65_536;
|
|
109
179
|
export class SQliteStore extends AbstractStore {
|
|
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
|
-
|
|
180
|
+
opts;
|
|
181
|
+
vectorCacheMb;
|
|
182
|
+
sqliteCacheMb;
|
|
183
|
+
vectorSeed;
|
|
184
|
+
// ── the two SEPARATE vector indices ───────────────────────────────────
|
|
185
|
+
content = null; // STORAGE: node gists
|
|
186
|
+
halos = null; // CACHE: halo gists
|
|
187
|
+
sqlite = null;
|
|
188
|
+
// Deferred write transaction guard.
|
|
189
|
+
_inTx = false;
|
|
190
|
+
// ── negative dedup filter ──────────────────────────────────────────────
|
|
191
|
+
/** Bloom filter over every node row's content hash `h`. The dedup probes
|
|
192
|
+
* consult it first: "definitely absent" answers the training hot path's
|
|
193
|
+
* dominant miss case in RAM (profiled: the SQLite probes were 38.7% of
|
|
194
|
+
* ingest wall at 2.9M nodes). INVARIANT: every inserted h is added
|
|
195
|
+
* before the probe that could see it — a false negative would mint a
|
|
196
|
+
* duplicate node and silently break hash-consing. */
|
|
197
|
+
_bloom = null;
|
|
198
|
+
/** Dedup probes answered by the filter alone this session (observability). */
|
|
199
|
+
bloomSkips = 0;
|
|
200
|
+
_insertNode = null;
|
|
201
|
+
_insertKid = null;
|
|
202
|
+
_selContain = null;
|
|
203
|
+
_selParents = null;
|
|
204
|
+
_selHalo = null;
|
|
205
|
+
_upHalo = null;
|
|
206
|
+
_selLeaf = null;
|
|
207
|
+
_selFlat = null;
|
|
208
|
+
_selKids = null;
|
|
209
|
+
_selNode = null;
|
|
210
|
+
_insertEdge = null;
|
|
211
|
+
_selNext = null;
|
|
212
|
+
_selPrev = null;
|
|
213
|
+
_setMeta = null;
|
|
214
|
+
_getMeta = null;
|
|
215
|
+
_delMeta = null;
|
|
216
|
+
_insSnapshot = null;
|
|
217
|
+
_selSnapshot = null;
|
|
218
|
+
constructor(opts = {}) {
|
|
219
|
+
const d = DEFAULT_CONFIG.store;
|
|
220
|
+
// Resolve config from opts, falling back to defaults.
|
|
221
|
+
const config = {
|
|
222
|
+
minHaloMass: opts.minHaloMass ?? d.minHaloMass,
|
|
223
|
+
efSearch: opts.efSearch ?? d.efSearch,
|
|
224
|
+
compactEveryNWrites: opts.compactEveryNWrites ?? d.compactEveryNWrites,
|
|
225
|
+
overfetch: opts.overfetch ?? d.overfetch,
|
|
226
|
+
batchSize: opts.batchSize ?? d.batchSize,
|
|
227
|
+
dedupCacheMax: opts.dedupCacheMax ?? d.dedupCacheMax,
|
|
228
|
+
bytesCacheMax: opts.bytesCacheMax ?? d.bytesCacheMax,
|
|
229
|
+
recCacheBytes: opts.recCacheBytes ?? d.recCacheBytes,
|
|
230
|
+
ingestCacheBytes: opts.ingestCacheBytes ?? d.ingestCacheBytes,
|
|
231
|
+
pendingGistBytes: opts.pendingGistBytes ?? d.pendingGistBytes,
|
|
232
|
+
haloCacheBytes: opts.haloCacheBytes ?? d.haloCacheBytes,
|
|
233
|
+
vectorCacheMb: opts.vectorCacheMb ?? d.vectorCacheMb,
|
|
234
|
+
sqliteCacheMb: opts.sqliteCacheMb ?? d.sqliteCacheMb,
|
|
235
|
+
coveredIdsMax: opts.coveredIdsMax ?? d.coveredIdsMax,
|
|
236
|
+
chainCacheBytes: opts.chainCacheBytes ?? d.chainCacheBytes,
|
|
237
|
+
};
|
|
238
|
+
const D = opts.D ?? 1024;
|
|
239
|
+
const maxGroup = opts.maxGroup ?? DEFAULT_CONFIG.geometry.maxGroup;
|
|
240
|
+
super(config, D, maxGroup);
|
|
241
|
+
this.opts = opts;
|
|
242
|
+
this.vectorCacheMb = opts.vectorCacheMb ?? d.vectorCacheMb;
|
|
243
|
+
this.sqliteCacheMb = opts.sqliteCacheMb ?? d.sqliteCacheMb;
|
|
244
|
+
this.vectorSeed = (0x51f15e ^ 0x9e3779b9) >>> 0;
|
|
245
|
+
this._ready = this._dbOpen();
|
|
246
|
+
}
|
|
247
|
+
// ── Vector-DB paths ───────────────────────────────────────────────────
|
|
248
|
+
vectorDbPath(name) {
|
|
249
|
+
const stem = this.opts.path ?? ":memory:";
|
|
250
|
+
if (stem === ":memory:")
|
|
251
|
+
return ":memory:";
|
|
252
|
+
return `${stem}.${name}.vec`;
|
|
253
|
+
}
|
|
254
|
+
openVectorDB(name) {
|
|
255
|
+
return new VectorDatabase({
|
|
256
|
+
dbPath: this.vectorDbPath(name),
|
|
257
|
+
dim: this.D,
|
|
258
|
+
efSearch: this.efSearch,
|
|
259
|
+
// Query-side estimator precision. 8 bits: 4 bits measurably misranks
|
|
260
|
+
// tight gist clusters (mixture recall@10 37.5% vs 39.0%, self-recall
|
|
261
|
+
// 69% vs 76%; rabitq test 2a), and the codes on disk are independent of
|
|
262
|
+
// it, so existing stores adopt the sharper setting on reopen. The one
|
|
263
|
+
// decision the sharper ranking exposed — a saturated hub winning a
|
|
264
|
+
// sub-noise rank tie and silencing its region — is handled by the
|
|
265
|
+
// tie-band saturation fallback in attention.ts, which is derived from
|
|
266
|
+
// the estimator's own margin noise rather than tuned to a bit width.
|
|
267
|
+
queryBits: this.opts.queryBits ?? 8,
|
|
268
|
+
seed: this.vectorSeed,
|
|
269
|
+
cacheSizeMb: this.vectorCacheMb,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
// ── Abstract method implementations ───────────────────────────────────
|
|
273
|
+
// -- Lifecycle --
|
|
274
|
+
async _dbOpen() {
|
|
275
|
+
const stem = this.opts.path ?? ":memory:";
|
|
276
|
+
const sp = stem === ":memory:" ? ":memory:" : `${stem}.sqlite`;
|
|
277
|
+
this.sqlite = new DatabaseSync(sp);
|
|
278
|
+
// Page size matched to the store's row grain BEFORE any table exists (a
|
|
279
|
+
// no-op on a non-empty database). Every table here holds small rows —
|
|
280
|
+
// packed kid/contain pairs, byte-packed flat branches, quantized halos —
|
|
281
|
+
// so SQLite's default 4 KiB pages carry mostly slack; 1 KiB pages keep
|
|
282
|
+
// leaf-page fill high at a negligible extra tree depth.
|
|
283
|
+
this.sqlite.exec("PRAGMA page_size = 1024;");
|
|
284
|
+
// WAL + NORMAL sync: crash-safe (WAL commits are atomic) without a full
|
|
285
|
+
// fsync per commit — the same discipline the vector databases use.
|
|
286
|
+
this.sqlite.exec("PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL;");
|
|
287
|
+
// SQLite's autocheckpoint default is 1000 PAGES — 1 MiB at this store's
|
|
288
|
+
// 1 KiB page size — so at training write rates nearly every batch commit
|
|
289
|
+
// crossed the threshold and paid a synchronous checkpoint: random writes
|
|
290
|
+
// into a GB-scale main file, with the hottest B-tree pages copied out on
|
|
291
|
+
// every commit instead of being coalesced across commits in the WAL.
|
|
292
|
+
// Profiled on a 7.5M-node store: ~114 ms PER COMMIT, ~19% of ingest
|
|
293
|
+
// wall-clock. 16 MiB of WAL between checkpoints coalesces hot-page
|
|
294
|
+
// rewrites and amortizes the checkpoint's fixed cost; crash-safety is
|
|
295
|
+
// unchanged (WAL recovery replays it), the only trade is a bounded,
|
|
296
|
+
// fixed-size WAL file.
|
|
297
|
+
this.sqlite.exec("PRAGMA wal_autocheckpoint = 65536;");
|
|
298
|
+
// Page cache for the DAG tables. Content-addressed dedup (findLeaf /
|
|
299
|
+
// findBranch), parent probes and contain appends are millions of point
|
|
300
|
+
// queries per session against a file that outgrows SQLite's ~2 MiB
|
|
301
|
+
// default cache almost immediately; every miss is a file read on the
|
|
302
|
+
// ingest hot path. Negative = KiB budget (size-based, page-size
|
|
303
|
+
// independent). Latency only — results identical at any value.
|
|
304
|
+
const cacheKb = Math.max(0, Math.floor(this.sqliteCacheMb * 1024));
|
|
305
|
+
if (cacheKb > 0)
|
|
306
|
+
this.sqlite.exec(`PRAGMA cache_size = -${cacheKb};`);
|
|
307
|
+
this.sqlite.exec(SCHEMA);
|
|
308
|
+
// Recover D from a previous run so loadFromStore() can bootstrap with any
|
|
309
|
+
// default — the real value is read from meta before vector indices load.
|
|
310
|
+
// The meta table always exists here (SCHEMA above creates it), so these
|
|
311
|
+
// reads run bare: a throw is a genuine storage fault, and swallowing it
|
|
312
|
+
// would let the store proceed with a default D/maxGroup that silently
|
|
313
|
+
// disagrees with what training persisted.
|
|
314
|
+
{
|
|
315
|
+
const row = this.sqlite.prepare("SELECT val FROM meta WHERE key = 'train.D'").get();
|
|
316
|
+
if (row) {
|
|
317
|
+
const d = Number(row.val);
|
|
318
|
+
if (Number.isInteger(d) && d > 0)
|
|
319
|
+
this._D = d;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
// Recover maxGroup from meta so indexSubtree uses the training-time value.
|
|
323
|
+
{
|
|
324
|
+
const row = this.sqlite.prepare("SELECT val FROM meta WHERE key = 'geometry.maxGroup'").get();
|
|
325
|
+
if (row) {
|
|
326
|
+
const g = Number(row.val);
|
|
327
|
+
if (Number.isInteger(g) && g > 0)
|
|
328
|
+
this._maxGroup = g;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// Persist maxGroup to meta when opening a FRESH store (no rows yet) so
|
|
332
|
+
// indexSubtree always sees the training-time value even when the store is
|
|
333
|
+
// accessed without a Mind / full snapshot.
|
|
334
|
+
if (this._nextId === 0) {
|
|
335
|
+
this.sqlite.prepare("INSERT OR IGNORE INTO meta (key, val) VALUES ('geometry.maxGroup', ?)").run(String(this._maxGroup));
|
|
336
|
+
}
|
|
337
|
+
this.content = this.openVectorDB("content");
|
|
338
|
+
this.halos = this.openVectorDB("halo");
|
|
339
|
+
// Ids are dense (0,1,2,…) and never deleted, so MAX(id)+1 IS the next id
|
|
340
|
+
// — one rightmost B-tree descent on the rowid, O(log n). (COUNT(*) gives
|
|
341
|
+
// the same value but scans every leaf page, so open time grew linearly
|
|
342
|
+
// with the store; a per-row scan into a Set was worse still.)
|
|
343
|
+
this._nextId = (this.sqlite.prepare("SELECT MAX(id) AS m FROM node").get().m ?? -1) + 1;
|
|
344
|
+
this._bloomLoad();
|
|
345
|
+
}
|
|
346
|
+
// ── negative dedup filter: load / rebuild / persist ────────────────────
|
|
347
|
+
/** Restore the persisted filter and top up the rows a crash left uncovered
|
|
348
|
+
* (id ≥ stored `upto`); a missing/undersized blob forces a full rebuild.
|
|
349
|
+
* Clean-close sessions restart with a 0-row top-up. */
|
|
350
|
+
_bloomLoad() {
|
|
351
|
+
const row = this.sqlite.prepare("SELECT bits, n, upto FROM bloom WHERE id = 1").get();
|
|
352
|
+
const wantLog2 = bloomLog2For(this._nextId);
|
|
353
|
+
if (row && row.bits.length * 8 >= (1 << wantLog2)) {
|
|
354
|
+
const log2 = 31 - Math.clz32(row.bits.length * 8);
|
|
355
|
+
const b = new NodeBloom(log2);
|
|
356
|
+
b.bits.set(row.bits);
|
|
357
|
+
b.n = row.n;
|
|
358
|
+
this._bloom = b;
|
|
359
|
+
this._bloomTopUp(row.upto);
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
this._bloomRebuild();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/** Re-add the hashes of rows with id ≥ `from` (the tail a crash cut off).
|
|
366
|
+
* Re-adding a hash twice only re-sets its bits — never a correctness
|
|
367
|
+
* issue — so overlap with the persisted coverage is harmless. */
|
|
368
|
+
_bloomTopUp(from) {
|
|
369
|
+
if (from >= this._nextId)
|
|
370
|
+
return;
|
|
371
|
+
const b = this._bloom;
|
|
372
|
+
const scan = this.sqlite.prepare("SELECT h FROM node WHERE id >= ?");
|
|
373
|
+
scan.setReturnArrays(true);
|
|
374
|
+
for (const r of scan.iterate(from)) {
|
|
375
|
+
b.add(r[0]);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
/** Build a fresh filter sized for the current node count from one
|
|
379
|
+
* sequential scan of the h column. Called at open when no usable blob is
|
|
380
|
+
* stored, and again whenever growth saturates the bits-per-key budget. */
|
|
381
|
+
_bloomRebuild() {
|
|
382
|
+
const b = new NodeBloom(bloomLog2For(this._nextId));
|
|
383
|
+
b.n = 0;
|
|
384
|
+
if (this._nextId > 0) {
|
|
385
|
+
const scan = this.sqlite.prepare("SELECT h FROM node");
|
|
386
|
+
scan.setReturnArrays(true);
|
|
387
|
+
for (const r of scan.iterate()) {
|
|
388
|
+
// add() counts n itself.
|
|
389
|
+
b.add(r[0]);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
this._bloom = b;
|
|
393
|
+
}
|
|
394
|
+
/** Persist the filter with its coverage watermark. Runs inside the
|
|
395
|
+
* caller's transaction when one is open (checkpoint/close both flush
|
|
396
|
+
* first). */
|
|
397
|
+
_bloomPersist() {
|
|
398
|
+
if (!this._bloom || !this.sqlite)
|
|
399
|
+
return;
|
|
400
|
+
if (this._nextId < BLOOM_PERSIST_MIN_NODES)
|
|
401
|
+
return;
|
|
402
|
+
this.sqlite.prepare("INSERT INTO bloom (id, bits, n, upto) VALUES (1, ?, ?, ?) " +
|
|
403
|
+
"ON CONFLICT(id) DO UPDATE SET bits = excluded.bits, " +
|
|
404
|
+
"n = excluded.n, upto = excluded.upto").run(this._bloom.bits, this._bloom.n, this._nextId);
|
|
405
|
+
}
|
|
406
|
+
_dbClose() {
|
|
407
|
+
if (this.sqlite)
|
|
408
|
+
this._bloomPersist();
|
|
409
|
+
if (this.content) {
|
|
410
|
+
this.content.close();
|
|
411
|
+
this.content = null;
|
|
412
|
+
}
|
|
413
|
+
if (this.halos) {
|
|
414
|
+
this.halos.close();
|
|
415
|
+
this.halos = null;
|
|
416
|
+
}
|
|
417
|
+
if (this.sqlite) {
|
|
418
|
+
this.sqlite.close();
|
|
419
|
+
this.sqlite = null;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
// -- Transaction --
|
|
423
|
+
_dbBeginTx() {
|
|
424
|
+
if (this._inTx || !this.sqlite)
|
|
425
|
+
return;
|
|
426
|
+
this.sqlite.exec("BEGIN");
|
|
427
|
+
this._inTx = true;
|
|
428
|
+
}
|
|
429
|
+
_dbCommitTx() {
|
|
430
|
+
if (!this._inTx || !this.sqlite)
|
|
431
|
+
return;
|
|
432
|
+
this._inTx = false; // clear first so a throw can't wedge us mid-commit
|
|
433
|
+
this.sqlite.exec("COMMIT");
|
|
434
|
+
}
|
|
435
|
+
// -- Node CRUD --
|
|
436
|
+
_dbInsertNode(id, leaf, kids, h) {
|
|
437
|
+
if (!this._insertNode) {
|
|
438
|
+
this._insertNode = this.sqlite.prepare("INSERT INTO node (id, leaf, kids, h) VALUES (?, ?, ?, ?)");
|
|
439
|
+
}
|
|
440
|
+
this._insertNode.run(id, leaf, kids, h);
|
|
441
|
+
// The filter must see every inserted hash BEFORE any probe could — the
|
|
442
|
+
// add sits in the same synchronous call as the row insert.
|
|
443
|
+
const b = this._bloom;
|
|
444
|
+
b.add(h);
|
|
445
|
+
if (b.saturated)
|
|
446
|
+
this._bloomRebuild();
|
|
447
|
+
}
|
|
448
|
+
_dbGetNode(id) {
|
|
449
|
+
if (!this._selNode) {
|
|
450
|
+
this._selNode = this.sqlite.prepare("SELECT id, leaf, kids FROM node WHERE id = ?");
|
|
451
|
+
}
|
|
452
|
+
const r = this._selNode.get(id);
|
|
453
|
+
if (!r)
|
|
454
|
+
return null;
|
|
455
|
+
// A zero-length kids blob marks a FLAT branch: the leaf column holds its
|
|
456
|
+
// bytes and the kid list is derived, one implicit leaf per byte.
|
|
457
|
+
const flat = r.kids !== null && r.kids.byteLength === 0;
|
|
458
|
+
return {
|
|
459
|
+
id: r.id,
|
|
460
|
+
leaf: r.leaf && !flat ? new Uint8Array(r.leaf) : null,
|
|
461
|
+
kids: flat
|
|
462
|
+
? flatBytesKids(r.leaf)
|
|
463
|
+
: (r.kids ? unpackKids(r.kids) : null),
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
_dbFindLeaf(h, bytes) {
|
|
467
|
+
if (this._bloom && !this._bloom.mightContain(h)) {
|
|
468
|
+
this.bloomSkips++;
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
if (!this._selLeaf) {
|
|
472
|
+
this._selLeaf = this.sqlite.prepare("SELECT id FROM node WHERE h = ? AND leaf = ? AND kids IS NULL LIMIT 1");
|
|
473
|
+
}
|
|
474
|
+
const row = this._selLeaf.get(h, bytes);
|
|
475
|
+
return row ? row.id : null;
|
|
476
|
+
}
|
|
477
|
+
_dbFindBranchByLeaf(h, bytes) {
|
|
478
|
+
if (this._bloom && !this._bloom.mightContain(h)) {
|
|
479
|
+
this.bloomSkips++;
|
|
480
|
+
return null;
|
|
481
|
+
}
|
|
482
|
+
if (!this._selFlat) {
|
|
483
|
+
this._selFlat = this.sqlite.prepare("SELECT id FROM node WHERE h = ? AND leaf = ? AND kids IS NOT NULL LIMIT 1");
|
|
484
|
+
}
|
|
485
|
+
const row = this._selFlat.get(h, bytes);
|
|
486
|
+
return row ? row.id : null;
|
|
487
|
+
}
|
|
488
|
+
_dbFindBranchByKids(h, packed) {
|
|
489
|
+
if (this._bloom && !this._bloom.mightContain(h)) {
|
|
490
|
+
this.bloomSkips++;
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
493
|
+
if (!this._selKids) {
|
|
494
|
+
this._selKids = this.sqlite.prepare("SELECT id FROM node WHERE h = ? AND kids = ? LIMIT 1");
|
|
495
|
+
}
|
|
496
|
+
const row = this._selKids.get(h, packed);
|
|
497
|
+
return row ? row.id : null;
|
|
498
|
+
}
|
|
499
|
+
// -- Kid (structural parent) edges --
|
|
500
|
+
_dbInsertKid(child, parent) {
|
|
501
|
+
if (!this._insertKid) {
|
|
502
|
+
this._insertKid = this.sqlite.prepare("INSERT OR IGNORE INTO kid (child, parent) VALUES (?, ?)");
|
|
503
|
+
}
|
|
504
|
+
this._insertKid.run(child, parent);
|
|
505
|
+
}
|
|
506
|
+
_dbGetParents(id) {
|
|
507
|
+
if (!this._selParents) {
|
|
508
|
+
this._selParents = this.sqlite.prepare("SELECT parent FROM kid WHERE child = ?");
|
|
509
|
+
}
|
|
510
|
+
return this._selParents.all(id).map((r) => r.parent);
|
|
511
|
+
}
|
|
512
|
+
// -- Containment --
|
|
513
|
+
_selParentsFirst = null;
|
|
514
|
+
_dbGetParentsFirst(id, limit) {
|
|
515
|
+
if (!this._selParentsFirst) {
|
|
516
|
+
this._selParentsFirst = this.sqlite.prepare("SELECT parent FROM kid WHERE child = ? LIMIT ?");
|
|
517
|
+
}
|
|
518
|
+
return this._selParentsFirst.all(id, limit)
|
|
519
|
+
.map((r) => r.parent);
|
|
520
|
+
}
|
|
521
|
+
_selChain = null;
|
|
522
|
+
/** {@link Store.chainRun}'s walk as ONE recursive CTE: the whole
|
|
523
|
+
* transparent chain (no edge in or out, exactly one parent) is descended
|
|
524
|
+
* inside SQLite — per-node work is the same three indexed probes as the
|
|
525
|
+
* base class's loop, but without a JS↔SQLite round trip per node, which
|
|
526
|
+
* dominates on the deep single-structure scaffolding this read exists
|
|
527
|
+
* for. */
|
|
528
|
+
_chainWalk(id, cap) {
|
|
529
|
+
if (!this._selChain) {
|
|
530
|
+
this._selChain = this.sqlite.prepare(`WITH RECURSIVE chain(n, d) AS (
|
|
380
531
|
SELECT ?, 0
|
|
381
532
|
UNION ALL
|
|
382
533
|
SELECT (SELECT parent FROM kid WHERE child = chain.n LIMIT 1),
|
|
@@ -390,395 +541,350 @@ export class SQliteStore extends AbstractStore {
|
|
|
390
541
|
) = 1
|
|
391
542
|
)
|
|
392
543
|
SELECT n FROM chain ORDER BY d`);
|
|
544
|
+
}
|
|
545
|
+
return this._selChain.all(id, cap - 1)
|
|
546
|
+
.map((r) => r.n);
|
|
547
|
+
}
|
|
548
|
+
/** Width of the seq band inside the packed contain rowid: rowid =
|
|
549
|
+
* child·SEQ_SPAN + seq. Page sizes are geometric, so a chain of k live
|
|
550
|
+
* pages needs a base of ≥ 2^k · 4 bytes — seq stays ≤ ~50 for any
|
|
551
|
+
* physically possible list; the append path guards the band anyway. */
|
|
552
|
+
static SEQ_SPAN = 1 << 8;
|
|
553
|
+
_selContainExists = null;
|
|
554
|
+
_dbContainExists(child) {
|
|
555
|
+
if (!this._selContainExists) {
|
|
556
|
+
// A probe of the child's rowid range — one descent.
|
|
557
|
+
this._selContainExists = this.sqlite.prepare("SELECT 1 AS one FROM contain WHERE id >= ? AND id < ? LIMIT 1");
|
|
558
|
+
}
|
|
559
|
+
const base = child * SQliteStore.SEQ_SPAN;
|
|
560
|
+
return this._selContainExists.get(base, base + SQliteStore.SEQ_SPAN) !==
|
|
561
|
+
undefined;
|
|
562
|
+
}
|
|
563
|
+
_selContainPages = null;
|
|
564
|
+
/** The child's page directory — (seq, byte length) per page, O(log fan-in)
|
|
565
|
+
* rows, each read from the cell header without loading the blob. */
|
|
566
|
+
containPages(child) {
|
|
567
|
+
if (!this._selContainPages) {
|
|
568
|
+
this._selContainPages = this.sqlite.prepare("SELECT id, length(parents) AS len FROM contain " +
|
|
569
|
+
"WHERE id >= ? AND id < ? ORDER BY id");
|
|
570
|
+
}
|
|
571
|
+
const base = child * SQliteStore.SEQ_SPAN;
|
|
572
|
+
return this._selContainPages.all(base, base + SQliteStore.SEQ_SPAN)
|
|
573
|
+
.map((r) => ({ seq: r.id - base, len: r.len }));
|
|
574
|
+
}
|
|
575
|
+
_selContainPageSub = null;
|
|
576
|
+
_selContainPage = null;
|
|
577
|
+
containPage(child, seq) {
|
|
578
|
+
if (!this._selContainPage) {
|
|
579
|
+
this._selContainPage = this.sqlite.prepare("SELECT parents FROM contain WHERE id = ?");
|
|
580
|
+
}
|
|
581
|
+
return this._selContainPage.get(child * SQliteStore.SEQ_SPAN + seq).parents;
|
|
582
|
+
}
|
|
583
|
+
_dbGetContainParentsSlice(child, offset, limit) {
|
|
584
|
+
if (!this._selContainPageSub) {
|
|
585
|
+
// substr on a BLOB returns bytes: unpack only the requested span.
|
|
586
|
+
this._selContainPageSub = this.sqlite.prepare("SELECT substr(parents, 1 + ? * 4, ? * 4) AS page " +
|
|
587
|
+
"FROM contain WHERE id = ?");
|
|
588
|
+
}
|
|
589
|
+
const out = [];
|
|
590
|
+
let skip = offset;
|
|
591
|
+
for (const seg of this.containPages(child)) {
|
|
592
|
+
const n = seg.len >>> 2;
|
|
593
|
+
if (skip >= n) {
|
|
594
|
+
skip -= n;
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
const take = Math.min(limit - out.length, n - skip);
|
|
598
|
+
const row = this._selContainPageSub.get(skip, take, child * SQliteStore.SEQ_SPAN + seg.seq);
|
|
599
|
+
if (row && row.page.length > 0)
|
|
600
|
+
out.push(...unpackKids(row.page));
|
|
601
|
+
skip = 0;
|
|
602
|
+
if (out.length >= limit)
|
|
603
|
+
break;
|
|
604
|
+
}
|
|
605
|
+
return out;
|
|
606
|
+
}
|
|
607
|
+
_selContainCount = null;
|
|
608
|
+
_dbGetContainCount(child) {
|
|
609
|
+
if (!this._selContainCount) {
|
|
610
|
+
// Stored ENTRY count (transient duplicates included) — consistent with
|
|
611
|
+
// what the slice streams, which is all the seam math needs.
|
|
612
|
+
this._selContainCount = this.sqlite.prepare("SELECT COALESCE(SUM(length(parents)), 0) / 4 AS n " +
|
|
613
|
+
"FROM contain WHERE id >= ? AND id < ?");
|
|
614
|
+
}
|
|
615
|
+
const base = child * SQliteStore.SEQ_SPAN;
|
|
616
|
+
return this._selContainCount.get(base, base + SQliteStore.SEQ_SPAN).n;
|
|
393
617
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
if (!this._selContainExists) {
|
|
405
|
-
// A probe of the child's rowid range — one descent.
|
|
406
|
-
this._selContainExists = this.sqlite.prepare(
|
|
407
|
-
"SELECT 1 AS one FROM contain WHERE id >= ? AND id < ? LIMIT 1",
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
const base = child * SQliteStore.SEQ_SPAN;
|
|
411
|
-
return this._selContainExists.get(base, base + SQliteStore.SEQ_SPAN) !==
|
|
412
|
-
undefined;
|
|
413
|
-
}
|
|
414
|
-
_selContainPages = null;
|
|
415
|
-
/** The child's page directory — (seq, byte length) per page, O(log fan-in)
|
|
416
|
-
* rows, each read from the cell header without loading the blob. */
|
|
417
|
-
containPages(child) {
|
|
418
|
-
if (!this._selContainPages) {
|
|
419
|
-
this._selContainPages = this.sqlite.prepare(
|
|
420
|
-
"SELECT id, length(parents) AS len FROM contain " +
|
|
421
|
-
"WHERE id >= ? AND id < ? ORDER BY id",
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
const base = child * SQliteStore.SEQ_SPAN;
|
|
425
|
-
return this._selContainPages.all(base, base + SQliteStore.SEQ_SPAN)
|
|
426
|
-
.map((r) => ({ seq: r.id - base, len: r.len }));
|
|
427
|
-
}
|
|
428
|
-
_selContainPageSub = null;
|
|
429
|
-
_selContainPage = null;
|
|
430
|
-
containPage(child, seq) {
|
|
431
|
-
if (!this._selContainPage) {
|
|
432
|
-
this._selContainPage = this.sqlite.prepare(
|
|
433
|
-
"SELECT parents FROM contain WHERE id = ?",
|
|
434
|
-
);
|
|
435
|
-
}
|
|
436
|
-
return this._selContainPage.get(child * SQliteStore.SEQ_SPAN + seq).parents;
|
|
437
|
-
}
|
|
438
|
-
_dbGetContainParentsSlice(child, offset, limit) {
|
|
439
|
-
if (!this._selContainPageSub) {
|
|
440
|
-
// substr on a BLOB returns bytes: unpack only the requested span.
|
|
441
|
-
this._selContainPageSub = this.sqlite.prepare(
|
|
442
|
-
"SELECT substr(parents, 1 + ? * 4, ? * 4) AS page " +
|
|
443
|
-
"FROM contain WHERE id = ?",
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
const out = [];
|
|
447
|
-
let skip = offset;
|
|
448
|
-
for (const seg of this.containPages(child)) {
|
|
449
|
-
const n = seg.len >>> 2;
|
|
450
|
-
if (skip >= n) {
|
|
451
|
-
skip -= n;
|
|
452
|
-
continue;
|
|
453
|
-
}
|
|
454
|
-
const take = Math.min(limit - out.length, n - skip);
|
|
455
|
-
const row = this._selContainPageSub.get(
|
|
456
|
-
skip,
|
|
457
|
-
take,
|
|
458
|
-
child * SQliteStore.SEQ_SPAN + seg.seq,
|
|
459
|
-
);
|
|
460
|
-
if (row && row.page.length > 0) {
|
|
461
|
-
out.push(...unpackKids(row.page));
|
|
462
|
-
}
|
|
463
|
-
skip = 0;
|
|
464
|
-
if (out.length >= limit) {
|
|
465
|
-
break;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
return out;
|
|
469
|
-
}
|
|
470
|
-
_selContainCount = null;
|
|
471
|
-
_dbGetContainCount(child) {
|
|
472
|
-
if (!this._selContainCount) {
|
|
473
|
-
// Stored ENTRY count (transient duplicates included) — consistent with
|
|
474
|
-
// what the slice streams, which is all the seam math needs.
|
|
475
|
-
this._selContainCount = this.sqlite.prepare(
|
|
476
|
-
"SELECT COALESCE(SUM(length(parents)), 0) / 4 AS n " +
|
|
477
|
-
"FROM contain WHERE id >= ? AND id < ?",
|
|
478
|
-
);
|
|
479
|
-
}
|
|
480
|
-
const base = child * SQliteStore.SEQ_SPAN;
|
|
481
|
-
return this._selContainCount.get(base, base + SQliteStore.SEQ_SPAN).n;
|
|
482
|
-
}
|
|
483
|
-
_dbGetContainParents(child) {
|
|
484
|
-
if (!this._selContain) {
|
|
485
|
-
this._selContain = this.sqlite.prepare(
|
|
486
|
-
"SELECT parents FROM contain WHERE id >= ? AND id < ? ORDER BY id",
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
const base = child * SQliteStore.SEQ_SPAN;
|
|
490
|
-
const rows = this._selContain.all(base, base + SQliteStore.SEQ_SPAN);
|
|
491
|
-
if (rows.length === 0) {
|
|
492
|
-
return [];
|
|
493
|
-
}
|
|
494
|
-
// Dedup across pages (a pair re-added after its page merged into the base
|
|
495
|
-
// may repeat in the tail until the next base merge), preserving order.
|
|
496
|
-
const seen = new Set();
|
|
497
|
-
const out = [];
|
|
498
|
-
for (const r of rows) {
|
|
499
|
-
for (const p of unpackKids(r.parents)) {
|
|
500
|
-
if (!seen.has(p)) {
|
|
501
|
-
seen.add(p);
|
|
502
|
-
out.push(p);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
return out;
|
|
507
|
-
}
|
|
508
|
-
_upsContainPage = null;
|
|
509
|
-
_delContainPage = null;
|
|
510
|
-
_dbAppendContain(child, parents) {
|
|
511
|
-
if (parents.length === 0) {
|
|
512
|
-
return;
|
|
513
|
-
}
|
|
514
|
-
if (!this._upsContainPage) {
|
|
515
|
-
this._upsContainPage = this.sqlite.prepare(
|
|
516
|
-
"INSERT INTO contain (id, parents) VALUES (?, ?) " +
|
|
517
|
-
"ON CONFLICT(id) DO UPDATE SET parents = excluded.parents",
|
|
518
|
-
);
|
|
519
|
-
this._delContainPage = this.sqlite.prepare(
|
|
520
|
-
"DELETE FROM contain WHERE id = ?",
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
|
-
// Fold stored tail pages into the new page IN MEMORY, then write ONCE.
|
|
524
|
-
// Merge rule per fold: below a 256-byte floor always merge (most children
|
|
525
|
-
// have a handful of parents — two tiny rows would double their per-row
|
|
526
|
-
// overhead for no amortization benefit); above it, merge only while the
|
|
527
|
-
// stored page is ≤ 2× the accumulated one, which bounds total re-writing
|
|
528
|
-
// to O(fan-in · log fan-in) bytes and keeps the page directory
|
|
529
|
-
// logarithmic. A fold that reaches the seq-0 base page dedups, squeezing
|
|
530
|
-
// out duplicates on the same geometric schedule. Writing the fold's
|
|
531
|
-
// result as one upsert (an in-place row replace in the common
|
|
532
|
-
// small-child case) rather than delete+insert churn keeps B-tree leaf
|
|
533
|
-
// fill at the packed single-row format's level — measured: churn alone
|
|
534
|
-
// cost ~26% extra pages on the same payload.
|
|
535
|
-
const segs = this.containPages(child);
|
|
536
|
-
let cur = packKids(parents);
|
|
537
|
-
let seq = segs.length > 0 ? segs[segs.length - 1].seq + 1 : 0;
|
|
538
|
-
if (seq >= SQliteStore.SEQ_SPAN) {
|
|
539
|
-
throw new Error(`contain page seq overflow for child ${child}`);
|
|
540
|
-
}
|
|
541
|
-
const folded = [];
|
|
542
|
-
while (segs.length > 0) {
|
|
543
|
-
const a = segs[segs.length - 1];
|
|
544
|
-
if (a.len + cur.byteLength > 256 && a.len > 2 * cur.byteLength) {
|
|
545
|
-
break;
|
|
546
|
-
}
|
|
547
|
-
const pa = this.containPage(child, a.seq);
|
|
548
|
-
if (a.seq === 0) {
|
|
618
|
+
_dbGetContainParents(child) {
|
|
619
|
+
if (!this._selContain) {
|
|
620
|
+
this._selContain = this.sqlite.prepare("SELECT parents FROM contain WHERE id >= ? AND id < ? ORDER BY id");
|
|
621
|
+
}
|
|
622
|
+
const base = child * SQliteStore.SEQ_SPAN;
|
|
623
|
+
const rows = this._selContain.all(base, base + SQliteStore.SEQ_SPAN);
|
|
624
|
+
if (rows.length === 0)
|
|
625
|
+
return [];
|
|
626
|
+
// Dedup across pages (a pair re-added after its page merged into the base
|
|
627
|
+
// may repeat in the tail until the next base merge), preserving order.
|
|
549
628
|
const seen = new Set();
|
|
550
|
-
const
|
|
551
|
-
for (const
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
629
|
+
const out = [];
|
|
630
|
+
for (const r of rows) {
|
|
631
|
+
for (const p of unpackKids(r.parents)) {
|
|
632
|
+
if (!seen.has(p)) {
|
|
633
|
+
seen.add(p);
|
|
634
|
+
out.push(p);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return out;
|
|
639
|
+
}
|
|
640
|
+
_upsContainPage = null;
|
|
641
|
+
_delContainPage = null;
|
|
642
|
+
_dbAppendContain(child, parents) {
|
|
643
|
+
if (parents.length === 0)
|
|
644
|
+
return;
|
|
645
|
+
if (!this._upsContainPage) {
|
|
646
|
+
this._upsContainPage = this.sqlite.prepare("INSERT INTO contain (id, parents) VALUES (?, ?) " +
|
|
647
|
+
"ON CONFLICT(id) DO UPDATE SET parents = excluded.parents");
|
|
648
|
+
this._delContainPage = this.sqlite.prepare("DELETE FROM contain WHERE id = ?");
|
|
649
|
+
}
|
|
650
|
+
// Fold stored tail pages into the new page IN MEMORY, then write ONCE.
|
|
651
|
+
// Merge rule per fold: below a 256-byte floor always merge (most children
|
|
652
|
+
// have a handful of parents — two tiny rows would double their per-row
|
|
653
|
+
// overhead for no amortization benefit); above it, merge only while the
|
|
654
|
+
// stored page is ≤ 2× the accumulated one, which bounds total re-writing
|
|
655
|
+
// to O(fan-in · log fan-in) bytes and keeps the page directory
|
|
656
|
+
// logarithmic. A fold that reaches the seq-0 base page dedups, squeezing
|
|
657
|
+
// out duplicates on the same geometric schedule. Writing the fold's
|
|
658
|
+
// result as one upsert (an in-place row replace in the common
|
|
659
|
+
// small-child case) rather than delete+insert churn keeps B-tree leaf
|
|
660
|
+
// fill at the packed single-row format's level — measured: churn alone
|
|
661
|
+
// cost ~26% extra pages on the same payload.
|
|
662
|
+
const segs = this.containPages(child);
|
|
663
|
+
let cur = packKids(parents);
|
|
664
|
+
let seq = segs.length > 0 ? segs[segs.length - 1].seq + 1 : 0;
|
|
665
|
+
if (seq >= SQliteStore.SEQ_SPAN) {
|
|
666
|
+
throw new Error(`contain page seq overflow for child ${child}`);
|
|
667
|
+
}
|
|
668
|
+
const folded = [];
|
|
669
|
+
while (segs.length > 0) {
|
|
670
|
+
const a = segs[segs.length - 1];
|
|
671
|
+
if (a.len + cur.byteLength > 256 && a.len > 2 * cur.byteLength)
|
|
672
|
+
break;
|
|
673
|
+
const pa = this.containPage(child, a.seq);
|
|
674
|
+
if (a.seq === 0) {
|
|
675
|
+
const seen = new Set();
|
|
676
|
+
const ids = [];
|
|
677
|
+
for (const p of unpackKids(pa)) {
|
|
678
|
+
if (!seen.has(p)) {
|
|
679
|
+
seen.add(p);
|
|
680
|
+
ids.push(p);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
for (const p of unpackKids(cur)) {
|
|
684
|
+
if (!seen.has(p)) {
|
|
685
|
+
seen.add(p);
|
|
686
|
+
ids.push(p);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
cur = packKids(ids);
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
const m = new Uint8Array(pa.byteLength + cur.byteLength);
|
|
693
|
+
m.set(pa, 0);
|
|
694
|
+
m.set(cur, pa.byteLength);
|
|
695
|
+
cur = m;
|
|
696
|
+
}
|
|
697
|
+
folded.push(a.seq);
|
|
698
|
+
seq = a.seq;
|
|
699
|
+
segs.pop();
|
|
700
|
+
}
|
|
701
|
+
const base = child * SQliteStore.SEQ_SPAN;
|
|
702
|
+
for (const s of folded) {
|
|
703
|
+
if (s !== seq)
|
|
704
|
+
this._delContainPage.run(base + s);
|
|
705
|
+
}
|
|
706
|
+
this._upsContainPage.run(base + seq, cur);
|
|
707
|
+
}
|
|
708
|
+
// -- Continuation edges --
|
|
709
|
+
_dbInsertEdge(src, dst) {
|
|
710
|
+
if (!this._insertEdge) {
|
|
711
|
+
this._insertEdge = this.sqlite.prepare("INSERT OR IGNORE INTO edge (src, dst) VALUES (?, ?)");
|
|
712
|
+
}
|
|
713
|
+
this._insertEdge.run(src, dst);
|
|
714
|
+
}
|
|
715
|
+
_dbGetNextEdges(id) {
|
|
716
|
+
if (!this._selNext) {
|
|
717
|
+
this._selNext = this.sqlite.prepare("SELECT dst FROM edge WHERE src = ? ORDER BY seq ASC");
|
|
718
|
+
}
|
|
719
|
+
return this._selNext.all(id).map((r) => r.dst);
|
|
720
|
+
}
|
|
721
|
+
_dbGetPrevEdges(id) {
|
|
722
|
+
if (!this._selPrev) {
|
|
723
|
+
this._selPrev = this.sqlite.prepare("SELECT src FROM edge WHERE dst = ? ORDER BY seq DESC");
|
|
724
|
+
}
|
|
725
|
+
return this._selPrev.all(id).map((r) => r.src);
|
|
726
|
+
}
|
|
727
|
+
_selNextFirst = null;
|
|
728
|
+
_dbGetNextEdgesFirst(id, limit) {
|
|
729
|
+
if (!this._selNextFirst) {
|
|
730
|
+
this._selNextFirst = this.sqlite.prepare("SELECT dst FROM edge WHERE src = ? ORDER BY seq ASC LIMIT ?");
|
|
731
|
+
}
|
|
732
|
+
return this._selNextFirst.all(id, limit)
|
|
733
|
+
.map((r) => r.dst);
|
|
734
|
+
}
|
|
735
|
+
_selPrevFirst = null;
|
|
736
|
+
_dbGetPrevEdgesFirst(id, limit) {
|
|
737
|
+
if (!this._selPrevFirst) {
|
|
738
|
+
this._selPrevFirst = this.sqlite.prepare("SELECT src FROM edge WHERE dst = ? ORDER BY seq DESC LIMIT ?");
|
|
739
|
+
}
|
|
740
|
+
return this._selPrevFirst.all(id, limit)
|
|
741
|
+
.map((r) => r.src);
|
|
742
|
+
}
|
|
743
|
+
_selPrevCount = null;
|
|
744
|
+
/** {@link Store.prevCount} — one indexed COUNT over idx_edge_dst, never a
|
|
745
|
+
* row materialisation (a common continuation's reverse fan-in is
|
|
746
|
+
* corpus-sized). */
|
|
747
|
+
prevCount(id) {
|
|
748
|
+
if (!this._selPrevCount) {
|
|
749
|
+
this._selPrevCount = this.sqlite.prepare("SELECT COUNT(*) AS n FROM edge WHERE dst = ?");
|
|
750
|
+
}
|
|
751
|
+
return this._selPrevCount.get(id).n;
|
|
752
|
+
}
|
|
753
|
+
_selSrcExists = null;
|
|
754
|
+
_dbEdgeSrcExists(src) {
|
|
755
|
+
if (!this._selSrcExists) {
|
|
756
|
+
// A prefix probe of idx_edge(src, dst) — one B-tree descent.
|
|
757
|
+
this._selSrcExists = this.sqlite.prepare("SELECT 1 AS one FROM edge WHERE src = ? LIMIT 1");
|
|
758
|
+
}
|
|
759
|
+
return this._selSrcExists.get(src) !== undefined;
|
|
760
|
+
}
|
|
761
|
+
_dbEdgeDistinctSrcCount() {
|
|
762
|
+
return this.sqlite.prepare("SELECT COUNT(*) AS n FROM (SELECT DISTINCT src FROM edge)").get().n;
|
|
763
|
+
}
|
|
764
|
+
// -- Halos (durable 2-bit quantized rows) --
|
|
765
|
+
_dbGetHalo(id) {
|
|
766
|
+
if (!this._selHalo) {
|
|
767
|
+
this._selHalo = this.sqlite.prepare("SELECT vec, mass FROM halo WHERE id = ?");
|
|
768
|
+
}
|
|
769
|
+
const r = this._selHalo.get(id);
|
|
770
|
+
return r ?? null;
|
|
771
|
+
}
|
|
772
|
+
_dbUpsertHalo(id, encodedVec, mass) {
|
|
773
|
+
if (!this._upHalo) {
|
|
774
|
+
this._upHalo = this.sqlite.prepare("INSERT INTO halo (id, vec, mass) VALUES (?, ?, ?) " +
|
|
775
|
+
"ON CONFLICT(id) DO UPDATE SET vec = excluded.vec, mass = excluded.mass");
|
|
776
|
+
}
|
|
777
|
+
this._upHalo.run(id, encodedVec, mass);
|
|
778
|
+
}
|
|
779
|
+
// -- Meta --
|
|
780
|
+
_dbGetMeta(key) {
|
|
781
|
+
if (!this._getMeta) {
|
|
782
|
+
this._getMeta = this.sqlite.prepare("SELECT val FROM meta WHERE key = ?");
|
|
783
|
+
}
|
|
784
|
+
const row = this._getMeta.get(key);
|
|
785
|
+
return row ? row.val : null;
|
|
786
|
+
}
|
|
787
|
+
_dbSetMeta(key, val) {
|
|
788
|
+
if (!this._setMeta) {
|
|
789
|
+
this._setMeta = this.sqlite.prepare("INSERT INTO meta (key, val) VALUES (?, ?) " +
|
|
790
|
+
"ON CONFLICT(key) DO UPDATE SET val = excluded.val");
|
|
791
|
+
}
|
|
792
|
+
this._setMeta.run(key, val);
|
|
793
|
+
}
|
|
794
|
+
_dbDeleteMeta(key) {
|
|
795
|
+
if (!this._delMeta) {
|
|
796
|
+
this._delMeta = this.sqlite.prepare("DELETE FROM meta WHERE key = ?");
|
|
797
|
+
}
|
|
798
|
+
this._delMeta.run(key);
|
|
799
|
+
}
|
|
800
|
+
// -- Snapshot --
|
|
801
|
+
_dbSaveSnapshot(bytes) {
|
|
802
|
+
if (!this._insSnapshot) {
|
|
803
|
+
this._insSnapshot = this.sqlite.prepare("INSERT INTO snapshot (id, data) VALUES (1, ?) " +
|
|
804
|
+
"ON CONFLICT(id) DO UPDATE SET data = excluded.data");
|
|
805
|
+
}
|
|
806
|
+
this._insSnapshot.run(bytes);
|
|
807
|
+
// Checkpoint cadence: persisting the dedup filter here keeps the crash
|
|
808
|
+
// top-up (the id ≥ upto rescan at next open) bounded by one checkpoint
|
|
809
|
+
// interval instead of the whole session.
|
|
810
|
+
this._bloomPersist();
|
|
811
|
+
}
|
|
812
|
+
_dbLoadSnapshot() {
|
|
813
|
+
if (!this._selSnapshot) {
|
|
814
|
+
this._selSnapshot = this.sqlite.prepare("SELECT data FROM snapshot WHERE id = 1");
|
|
815
|
+
}
|
|
816
|
+
const r = this._selSnapshot.get();
|
|
817
|
+
return r ? new Uint8Array(r.data) : null;
|
|
818
|
+
}
|
|
819
|
+
// -- Vector DB: content (gist) index --
|
|
820
|
+
_vecContentUpsert(entries) {
|
|
821
|
+
this.content.upsertMany(entries);
|
|
822
|
+
}
|
|
823
|
+
_vecContentQuery(v, k, ef) {
|
|
824
|
+
return this.content.query(v, k, { ef });
|
|
825
|
+
}
|
|
826
|
+
_vecContentHas(id) {
|
|
827
|
+
return this.content ? this.content.has(id) : false;
|
|
828
|
+
}
|
|
829
|
+
_vecContentSize() {
|
|
830
|
+
return this.content ? this.content.size : 0;
|
|
831
|
+
}
|
|
832
|
+
_vecContentLastReads() {
|
|
833
|
+
return this.content ? this.content.lastQueryStorageReads : 0;
|
|
834
|
+
}
|
|
835
|
+
_vecContentPhysicalSize() {
|
|
836
|
+
return this.content ? this.content.physicalSize : 0;
|
|
837
|
+
}
|
|
838
|
+
_vecContentClusterCount() {
|
|
839
|
+
return this.content ? this.content.clusterCount : 0;
|
|
840
|
+
}
|
|
841
|
+
_vecContentCompact() {
|
|
842
|
+
this.content.compact();
|
|
843
|
+
}
|
|
844
|
+
_vecContentDeleteMany(ids) {
|
|
845
|
+
this.content.deleteMany(ids);
|
|
846
|
+
}
|
|
847
|
+
*_vecContentEntriesSince(after) {
|
|
848
|
+
if (this.content)
|
|
849
|
+
yield* this.content.keysSince(after);
|
|
850
|
+
}
|
|
851
|
+
/** {@link AbstractStore._dbEdgeOrHaloIds} — one C-side scan; UNION dedups
|
|
852
|
+
* and (with the ORDER BY) sorts, so the result is ready for the binary-
|
|
853
|
+
* search membership probes and the ascending-id maintenance walks. */
|
|
854
|
+
_dbEdgeOrHaloIds() {
|
|
855
|
+
const rows = this.sqlite.prepare("SELECT src AS id FROM edge UNION SELECT dst FROM edge " +
|
|
856
|
+
"UNION SELECT id FROM halo ORDER BY 1").all();
|
|
857
|
+
const out = new Array(rows.length);
|
|
858
|
+
for (let i = 0; i < rows.length; i++)
|
|
859
|
+
out[i] = rows[i].id;
|
|
860
|
+
return out;
|
|
861
|
+
}
|
|
862
|
+
// -- Vector DB: halo index --
|
|
863
|
+
_vecHaloUpsert(entries) {
|
|
864
|
+
this.halos.upsertMany(entries);
|
|
865
|
+
}
|
|
866
|
+
_vecHaloQuery(v, k, ef) {
|
|
867
|
+
return this.halos.query(v, k, { ef });
|
|
868
|
+
}
|
|
869
|
+
_vecHaloSize() {
|
|
870
|
+
return this.halos ? this.halos.size : 0;
|
|
871
|
+
}
|
|
872
|
+
_vecHaloPhysicalSize() {
|
|
873
|
+
return this.halos ? this.halos.physicalSize : 0;
|
|
874
|
+
}
|
|
875
|
+
_vecHaloClusterCount() {
|
|
876
|
+
return this.halos ? this.halos.clusterCount : 0;
|
|
877
|
+
}
|
|
878
|
+
_vecHaloCompact() {
|
|
879
|
+
this.halos.compact();
|
|
880
|
+
}
|
|
881
|
+
/** Pre-fill both vector indices' RAM caches with sequential scans (up to
|
|
882
|
+
* their budget caps) — seconds of streaming instead of the minutes of
|
|
883
|
+
* random point reads a cold training/query session otherwise pays while
|
|
884
|
+
* warming. Optional; call once after open before sustained work.
|
|
885
|
+
* Returns rows warmed. */
|
|
886
|
+
async warmVectorCaches() {
|
|
887
|
+
await this._ensureReady();
|
|
888
|
+
return (this.content?.warmCache() ?? 0) + (this.halos?.warmCache() ?? 0);
|
|
889
|
+
}
|
|
784
890
|
}
|