@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
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { IvfIndex } from "./ivf.js";
|
|
2
|
+
/**
|
|
3
|
+
* A persistent vector database: an adaptive PARTITIONED (IVF) index over
|
|
4
|
+
* 1-bit RaBitQ codes (cosine), stored entirely in SQLite at `dbPath`. See
|
|
5
|
+
* ivf.ts for the index design. The original float vectors are never
|
|
6
|
+
* retained — only the sign codes — so a 1024-d vector costs 128 bytes.
|
|
7
|
+
*
|
|
8
|
+
* Inserting is route-and-append: cost is essentially FLAT in collection size
|
|
9
|
+
* (one RAM scan of the pivot table + one chunk append — no per-insert graph
|
|
10
|
+
* walk). Query cost is bounded by nprobe × cluster size — constant in N
|
|
11
|
+
* once the collection is past its first splits.
|
|
12
|
+
*/
|
|
13
|
+
export class VectorDatabase {
|
|
14
|
+
index;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
if (!options || typeof options.dbPath !== "string" ||
|
|
17
|
+
options.dbPath.length === 0) {
|
|
18
|
+
throw new Error("DatabaseOptions.dbPath (string) is required");
|
|
19
|
+
}
|
|
20
|
+
this.index = new IvfIndex(options.dbPath, options);
|
|
21
|
+
}
|
|
22
|
+
get dim() {
|
|
23
|
+
return this.index.dim;
|
|
24
|
+
}
|
|
25
|
+
/** Number of live (non-deleted) vectors. */
|
|
26
|
+
get size() {
|
|
27
|
+
return this.index.size;
|
|
28
|
+
}
|
|
29
|
+
/** Physical slot count including tombstones from deletes/updates. */
|
|
30
|
+
get physicalSize() {
|
|
31
|
+
return this.index.physicalSize;
|
|
32
|
+
}
|
|
33
|
+
/** Number of clusters (partitions) currently in the index. */
|
|
34
|
+
get clusterCount() {
|
|
35
|
+
return this.index.clusterCount;
|
|
36
|
+
}
|
|
37
|
+
get efSearch() {
|
|
38
|
+
return this.index.efSearch;
|
|
39
|
+
}
|
|
40
|
+
set efSearch(value) {
|
|
41
|
+
this.index.efSearch = value;
|
|
42
|
+
}
|
|
43
|
+
/** Distance computations performed during the most recent query. */
|
|
44
|
+
get lastQueryDistanceComputations() {
|
|
45
|
+
return this.index.lastQueryDistComps;
|
|
46
|
+
}
|
|
47
|
+
/** Storage row reads issued by the most recent query — the honest,
|
|
48
|
+
* cache-independent scalability metric. */
|
|
49
|
+
get lastQueryStorageReads() {
|
|
50
|
+
return this.index.lastQueryStorageReads;
|
|
51
|
+
}
|
|
52
|
+
get storage() {
|
|
53
|
+
const f32 = this.dim * 4;
|
|
54
|
+
const bpv = this.index.bytesPerVector;
|
|
55
|
+
return {
|
|
56
|
+
float32BytesPerVector: f32,
|
|
57
|
+
codeBytesPerVector: bpv,
|
|
58
|
+
bytesPerVector: bpv,
|
|
59
|
+
compressionRatio: f32 / bpv,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
checkId(id) {
|
|
63
|
+
// 32-bit signed range: chunk blobs store exts as int32 — a wider id
|
|
64
|
+
// would silently truncate, so it is rejected at the door instead.
|
|
65
|
+
if (!Number.isInteger(id) || id > 0x7fffffff || id < -0x80000000) {
|
|
66
|
+
throw new Error(`External id must be a 32-bit integer, got ${id}`);
|
|
67
|
+
}
|
|
68
|
+
return id;
|
|
69
|
+
}
|
|
70
|
+
/** Convert a value to code bytes, selecting by length: `codeWords`
|
|
71
|
+
* elements → an existing 1-bit code; otherwise a raw `dim`-vector. */
|
|
72
|
+
toCodeBytes(value) {
|
|
73
|
+
if (value.length === this.index.codeWords) {
|
|
74
|
+
return this.index.codeToBytes(value);
|
|
75
|
+
}
|
|
76
|
+
if (value.length !== this.dim) {
|
|
77
|
+
throw new Error(`Vector dimension mismatch: expected ${this.dim}, got ${value.length}`);
|
|
78
|
+
}
|
|
79
|
+
return this.index.encodeToBytes(value);
|
|
80
|
+
}
|
|
81
|
+
has(id) {
|
|
82
|
+
return this.index.has(this.checkId(id));
|
|
83
|
+
}
|
|
84
|
+
/** Stream every live external id (bounded memory). */
|
|
85
|
+
*keys() {
|
|
86
|
+
yield* this.index.keys();
|
|
87
|
+
}
|
|
88
|
+
/** Stream live entries whose INTERNAL id is > `after` — a durable
|
|
89
|
+
* incremental watermark (internal ids are monotone at insert and preserved
|
|
90
|
+
* by update and compact). */
|
|
91
|
+
*keysSince(after) {
|
|
92
|
+
yield* this.index.keysSince(after);
|
|
93
|
+
}
|
|
94
|
+
/** Read the stored 1-bit code for an id (a copy as a Uint32Array), or null. */
|
|
95
|
+
get(id) {
|
|
96
|
+
const bytes = this.index.codeOf(this.checkId(id));
|
|
97
|
+
return bytes ? this.index.bytesToCode(bytes) : null;
|
|
98
|
+
}
|
|
99
|
+
insert(id, value) {
|
|
100
|
+
this.index.begin();
|
|
101
|
+
try {
|
|
102
|
+
this.index.insert(this.checkId(id), this.toCodeBytes(value));
|
|
103
|
+
this.index.commit();
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
this.index.rollback();
|
|
107
|
+
throw e;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
update(id, value) {
|
|
111
|
+
this.index.begin();
|
|
112
|
+
try {
|
|
113
|
+
this.index.update(this.checkId(id), this.toCodeBytes(value));
|
|
114
|
+
this.index.commit();
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
this.index.rollback();
|
|
118
|
+
throw e;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
upsert(id, value) {
|
|
122
|
+
this.upsertMany([{ id, vector: value }]);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Upsert many vectors under ONE transaction — one WAL commit for the whole
|
|
126
|
+
* batch instead of one per vector, with chunk appends to the same cluster
|
|
127
|
+
* coalesced in the index's write-back buffer.
|
|
128
|
+
*/
|
|
129
|
+
upsertMany(entries) {
|
|
130
|
+
if (entries.length === 0)
|
|
131
|
+
return;
|
|
132
|
+
this.index.begin();
|
|
133
|
+
try {
|
|
134
|
+
for (const e of entries) {
|
|
135
|
+
// One shared vmap probe decides insert vs update — the per-row point
|
|
136
|
+
// query is the dominant bulk-load cost, so it is never duplicated.
|
|
137
|
+
this.index.upsert(this.checkId(e.id), this.toCodeBytes(e.vector));
|
|
138
|
+
}
|
|
139
|
+
this.index.commit();
|
|
140
|
+
}
|
|
141
|
+
catch (e) {
|
|
142
|
+
this.index.rollback();
|
|
143
|
+
throw e;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** Delete many ids under ONE transaction. Absent ids are skipped.
|
|
147
|
+
* Returns the number of vectors actually removed. */
|
|
148
|
+
deleteMany(ids) {
|
|
149
|
+
if (ids.length === 0)
|
|
150
|
+
return 0;
|
|
151
|
+
let removed = 0;
|
|
152
|
+
this.index.begin();
|
|
153
|
+
try {
|
|
154
|
+
for (const id of ids) {
|
|
155
|
+
if (this.index.remove(this.checkId(id)))
|
|
156
|
+
removed++;
|
|
157
|
+
}
|
|
158
|
+
this.index.commit();
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
this.index.rollback();
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
164
|
+
return removed;
|
|
165
|
+
}
|
|
166
|
+
/** Delete the vector bound to an id. Returns false if absent. */
|
|
167
|
+
delete(id) {
|
|
168
|
+
return this.deleteMany([id]) === 1;
|
|
169
|
+
}
|
|
170
|
+
/** Heat the SQLite page cache with sequential scans (latency only). */
|
|
171
|
+
warmCache() {
|
|
172
|
+
return this.index.warmCache();
|
|
173
|
+
}
|
|
174
|
+
/** Reclaim tombstoned slots by rewriting the clusters that carry any, then
|
|
175
|
+
* VACUUM. Internal ids and cluster assignment are preserved. */
|
|
176
|
+
compact() {
|
|
177
|
+
this.index.compact();
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* k-NN search. The argument's length selects the mode:
|
|
181
|
+
* - `dim` elements → a raw vector (accurate estimator)
|
|
182
|
+
* - `codeWords` elements → a 1-bit code, by sign-bit Hamming
|
|
183
|
+
*/
|
|
184
|
+
query(query, k = 10, opts) {
|
|
185
|
+
let hits;
|
|
186
|
+
if (query.length === this.index.codeWords) {
|
|
187
|
+
hits = this.index.searchKnnByCode(this.index.codeToBytes(query), k, opts?.ef);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
if (query.length !== this.dim) {
|
|
191
|
+
throw new Error(`Vector dimension mismatch: expected ${this.dim}, got ${query.length}`);
|
|
192
|
+
}
|
|
193
|
+
hits = this.index.searchKnn(query, k, opts?.ef);
|
|
194
|
+
}
|
|
195
|
+
return hits;
|
|
196
|
+
}
|
|
197
|
+
/** Close the underlying database. The instance must not be used afterwards. */
|
|
198
|
+
close() {
|
|
199
|
+
this.index.close();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { VectorDatabase } from "./database.js";
|
|
2
|
+
export type { DatabaseOptions, ExternalId, QueryResult, StorageStats, } from "./database.js";
|
|
3
|
+
export { IvfIndex } from "./ivf.js";
|
|
4
|
+
export type { IvfConfig, IvfHit } from "./ivf.js";
|
|
5
|
+
export { RaBitQuantizer } from "./rabitq.js";
|
|
6
|
+
export type { QueryContext, RaBitQOptions } from "./rabitq.js";
|
|
7
|
+
export { Prng } from "./prng.js";
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
export { VectorDatabase } from "./database.js";
|
|
2
|
-
export {
|
|
2
|
+
export { IvfIndex } from "./ivf.js";
|
|
3
3
|
export { RaBitQuantizer } from "./rabitq.js";
|
|
4
|
-
export { Store } from "./store.js";
|
|
5
|
-
export { Heap } from "./heap.js";
|
|
6
4
|
export { Prng } from "./prng.js";
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
export interface IvfConfig {
|
|
3
|
+
dim: number;
|
|
4
|
+
efSearch: number;
|
|
5
|
+
queryBits: number;
|
|
6
|
+
rotationRounds: number;
|
|
7
|
+
seed: number;
|
|
8
|
+
centroid: Float64Array | null;
|
|
9
|
+
codeWords: number;
|
|
10
|
+
paddedDim: number;
|
|
11
|
+
}
|
|
12
|
+
export interface IvfHit {
|
|
13
|
+
/** external (user) id */
|
|
14
|
+
id: number;
|
|
15
|
+
/** estimated cosine distance (1 - cosine) */
|
|
16
|
+
distance: number;
|
|
17
|
+
}
|
|
18
|
+
export declare class IvfIndex {
|
|
19
|
+
readonly db: DatabaseSync;
|
|
20
|
+
private readonly quantizer;
|
|
21
|
+
private readonly codeBytes;
|
|
22
|
+
private readonly chunkBytes;
|
|
23
|
+
private _efSearch;
|
|
24
|
+
/** Pivot codes, K × codeBytes, contiguous — the insert/query routing scan
|
|
25
|
+
* is one linear pass over this array. */
|
|
26
|
+
private centCodes;
|
|
27
|
+
/** Aligned 32-bit word view of {@link centCodes} — the routing scan's
|
|
28
|
+
* operand (4 bytes per XOR+popcount instead of byte re-composition). */
|
|
29
|
+
private centWords;
|
|
30
|
+
/** Per-cluster entry count (live + dead) — the split trigger. */
|
|
31
|
+
private centEntries;
|
|
32
|
+
/** Per-cluster chunk count (tail seq = count − 1). */
|
|
33
|
+
private centChunks;
|
|
34
|
+
private K;
|
|
35
|
+
private live;
|
|
36
|
+
private totalSlots;
|
|
37
|
+
private nextId;
|
|
38
|
+
/** Storage row reads (chunk blob fetches) — the cache-independent
|
|
39
|
+
* scalability witness, same discipline as the graph index it replaces. */
|
|
40
|
+
reads: number;
|
|
41
|
+
lastQueryDistComps: number;
|
|
42
|
+
lastQueryStorageReads: number;
|
|
43
|
+
private readonly chunkCache;
|
|
44
|
+
private readonly dirtyChunks;
|
|
45
|
+
private readonly dirtyCents;
|
|
46
|
+
private metaDirty;
|
|
47
|
+
private readonly cacheEnabled;
|
|
48
|
+
private readonly chunkCacheMax;
|
|
49
|
+
private sSelChunk;
|
|
50
|
+
private sUpsChunk;
|
|
51
|
+
private sDelChunk;
|
|
52
|
+
private sSelVmap;
|
|
53
|
+
private sInsVmap;
|
|
54
|
+
private sUpdVmap;
|
|
55
|
+
private sDelVmap;
|
|
56
|
+
private sUpsCent;
|
|
57
|
+
private sMeta;
|
|
58
|
+
constructor(dbPath: string, options: {
|
|
59
|
+
dim?: number;
|
|
60
|
+
efSearch?: number;
|
|
61
|
+
queryBits?: number;
|
|
62
|
+
rotationRounds?: number;
|
|
63
|
+
seed?: number;
|
|
64
|
+
centroid?: ArrayLike<number>;
|
|
65
|
+
cacheSizeMb?: number;
|
|
66
|
+
});
|
|
67
|
+
readonly dim: number;
|
|
68
|
+
private prepareAll;
|
|
69
|
+
/** Load the pivot table into RAM — K rows, ~(codeBytes + 8) bytes each. */
|
|
70
|
+
private loadCents;
|
|
71
|
+
private growCents;
|
|
72
|
+
get size(): number;
|
|
73
|
+
get physicalSize(): number;
|
|
74
|
+
get clusterCount(): number;
|
|
75
|
+
get bytesPerVector(): number;
|
|
76
|
+
get efSearch(): number;
|
|
77
|
+
set efSearch(v: number);
|
|
78
|
+
resetReads(): void;
|
|
79
|
+
/** Fetch a chunk, serving dirty/cached copies first. `retain` controls
|
|
80
|
+
* whether a clean fetch enters the cache — read-only consumers pass the
|
|
81
|
+
* cacheEnabled flag so the `cacheSizeMb: 0` mode stays honestly uncached;
|
|
82
|
+
* writers always retain (the copy is about to become dirty). */
|
|
83
|
+
private chunkAt;
|
|
84
|
+
private cacheChunk;
|
|
85
|
+
private markDirty;
|
|
86
|
+
/** Write every dirty chunk row; drop the clean cache only when retention
|
|
87
|
+
* is off (no budget) — under a budget the warm set survives the flush.
|
|
88
|
+
* Runs inside the caller's transaction. */
|
|
89
|
+
private flushChunks;
|
|
90
|
+
private flushCents;
|
|
91
|
+
/** Persist all buffered state. MUST be called before the enclosing
|
|
92
|
+
* transaction commits (upsertMany/deleteMany do; single-op paths too). */
|
|
93
|
+
private flushAll;
|
|
94
|
+
private static readonly SUPER_G;
|
|
95
|
+
private static readonly SUPER_TOP;
|
|
96
|
+
/** Majority super-pivot codes, one per group of SUPER_G clusters. */
|
|
97
|
+
private superWords;
|
|
98
|
+
/** Group ids whose super-pivot is stale (member pivot changed). */
|
|
99
|
+
private readonly superStale;
|
|
100
|
+
/** Scratch word view of the code being routed (avoids per-call copies). */
|
|
101
|
+
private routeScratch;
|
|
102
|
+
private markSuperStale;
|
|
103
|
+
/** Recompute stale super-pivots (majority bit over member pivots). */
|
|
104
|
+
private freshenSupers;
|
|
105
|
+
/** Word-popcount Hamming between the routing scratch and a words-row. */
|
|
106
|
+
private hamAt;
|
|
107
|
+
/** Nearest cluster to a code — exact over member pivots of the SUPER_TOP
|
|
108
|
+
* nearest super-groups (or over everything while K is small). Pure RAM,
|
|
109
|
+
* no allocation, no storage reads. */
|
|
110
|
+
private nearestCid;
|
|
111
|
+
/** The nprobe nearest clusters to a QUERY (accurate estimator on pivots),
|
|
112
|
+
* as cids ordered nearest-first. */
|
|
113
|
+
private probeOrder;
|
|
114
|
+
private probeOrderByCode;
|
|
115
|
+
/** Whether an external id is present (live). */
|
|
116
|
+
has(ext: number): boolean;
|
|
117
|
+
/** The stored code for an ext (a copy), or null. */
|
|
118
|
+
codeOf(ext: number): Uint8Array | null;
|
|
119
|
+
/** Insert a code under `ext`. Caller owns the transaction; buffered rows
|
|
120
|
+
* are flushed by {@link commitFlush}. Throws if ext is already live. */
|
|
121
|
+
insert(ext: number, code: Uint8Array): void;
|
|
122
|
+
/** Insert-or-update with ONE vmap probe. The point probe is a real cost
|
|
123
|
+
* at bulk-load rates (statement dispatch + a B-tree descent per row —
|
|
124
|
+
* profiled as the single largest insert-path term), so the presence check
|
|
125
|
+
* and the update lookup share it. */
|
|
126
|
+
upsert(ext: number, code: Uint8Array): void;
|
|
127
|
+
/** The insert core — presence already established by the caller. */
|
|
128
|
+
private insertNew;
|
|
129
|
+
private newCluster;
|
|
130
|
+
private appendToCluster;
|
|
131
|
+
/** Tombstone a live ext. Returns false when absent. */
|
|
132
|
+
remove(ext: number): boolean;
|
|
133
|
+
/** Update the code bound to a live ext. A byte-identical code is a no-op
|
|
134
|
+
* (content-addressed callers re-upsert unchanged vectors wholesale after a
|
|
135
|
+
* restart; each no-op otherwise costs a tombstone + reinsert). */
|
|
136
|
+
update(ext: number, code: Uint8Array): void;
|
|
137
|
+
private updateAt;
|
|
138
|
+
/** Flush buffered chunk/cent/meta rows. Call before COMMIT. */
|
|
139
|
+
commitFlush(): void;
|
|
140
|
+
/** Split a full cluster in two, deterministically. Dead slots are dropped
|
|
141
|
+
* (splits double as incremental compaction). Both halves get fresh
|
|
142
|
+
* majority-bit pivots, so routing sharpens as the corpus grows. */
|
|
143
|
+
private split;
|
|
144
|
+
/** Bit-majority sample size per split side. A pivot is a routing aid, not
|
|
145
|
+
* a stored value: the majority bit of a 512-member deterministic sample
|
|
146
|
+
* agrees with the full-population majority except on near-tied bits,
|
|
147
|
+
* where either choice routes equally well — and counting every member of
|
|
148
|
+
* a 4096-entry cluster was 25% of bulk-load CPU (profiled). */
|
|
149
|
+
private static readonly MAJORITY_SAMPLE;
|
|
150
|
+
/** Majority-bit code of the side's members (the binary centroid), or null
|
|
151
|
+
* when the side is empty. Members are sampled on a deterministic stride
|
|
152
|
+
* when the side exceeds {@link MAJORITY_SAMPLE}. */
|
|
153
|
+
private majorityCode;
|
|
154
|
+
/** Rewrite a cluster's chunks from a member list (one side of a split or a
|
|
155
|
+
* compaction survivor set), updating vmap rows. `dropFrom` deletes any
|
|
156
|
+
* leftover chunk rows beyond the new count. */
|
|
157
|
+
private rebuildCluster;
|
|
158
|
+
/** Multi-row vmap statements, prepared per row count (powers of a fixed
|
|
159
|
+
* batch width) — REPLACE semantics on the ext primary key. */
|
|
160
|
+
private readonly vmapBatchStmts;
|
|
161
|
+
private static readonly VMAP_BATCH;
|
|
162
|
+
private vmapBatchStmt;
|
|
163
|
+
/** Write flat (ext, id, cid, seq, slot) tuples in wide batches. */
|
|
164
|
+
private flushVmapRows;
|
|
165
|
+
/** ef → clusters probed. ef is the familiar "candidate breadth" knob; a
|
|
166
|
+
* probe scans one whole cluster, so nprobe = ef/4 keeps the default
|
|
167
|
+
* (ef 64 → 16 probes) both accurate on trained stores and bounded. */
|
|
168
|
+
private nprobeOf;
|
|
169
|
+
/** k-NN with a full-precision query (accurate RaBitQ estimator). */
|
|
170
|
+
searchKnn(vec: ArrayLike<number>, k: number, ef?: number): IvfHit[];
|
|
171
|
+
/** k-NN with an already-quantized code (sign-bit Hamming distance). */
|
|
172
|
+
searchKnnByCode(code: Uint8Array, k: number, ef?: number): IvfHit[];
|
|
173
|
+
private scanClusters;
|
|
174
|
+
/** Stream live entries whose internal id is > `after`, in id order.
|
|
175
|
+
* Internal ids are monotone at insert and PRESERVED by update/compact, so
|
|
176
|
+
* the largest id a caller has seen is a durable incremental watermark. */
|
|
177
|
+
keysSince(after: number, batch?: number): IterableIterator<{
|
|
178
|
+
ext: number;
|
|
179
|
+
internal: number;
|
|
180
|
+
}>;
|
|
181
|
+
/** Stream every live external id. */
|
|
182
|
+
keys(): IterableIterator<number>;
|
|
183
|
+
/** Drop tombstoned slots by rewriting each cluster that carries any, then
|
|
184
|
+
* VACUUM. Internal ids and cluster assignment are preserved — routing
|
|
185
|
+
* quality is untouched, only dead space is reclaimed. */
|
|
186
|
+
compact(): void;
|
|
187
|
+
/** Heat SQLite's page cache with sequential scans of the chunk and vmap
|
|
188
|
+
* tables — a cold session otherwise warms it through random point reads.
|
|
189
|
+
* Purely a latency optimisation; returns rows touched. */
|
|
190
|
+
warmCache(): number;
|
|
191
|
+
begin(): void;
|
|
192
|
+
commit(): void;
|
|
193
|
+
rollback(): void;
|
|
194
|
+
/** Encode a raw vector to its 1-bit code bytes. */
|
|
195
|
+
encodeToBytes(vec: ArrayLike<number>): Uint8Array;
|
|
196
|
+
codeToBytes(code: ArrayLike<number>): Uint8Array;
|
|
197
|
+
bytesToCode(bytes: Uint8Array): Uint32Array;
|
|
198
|
+
get codeWords(): number;
|
|
199
|
+
close(): void;
|
|
200
|
+
}
|