vector-index 0.0.1 → 0.1.0

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.
Files changed (46) hide show
  1. package/LICENSE +201 -21
  2. package/README.md +1447 -12
  3. package/build-browser.js +67 -0
  4. package/index.js +38 -6
  5. package/package.json +70 -6
  6. package/src/async.js +458 -0
  7. package/src/clustering.js +226 -0
  8. package/src/embeddings/als.js +211 -0
  9. package/src/embeddings/bpr.js +149 -0
  10. package/src/embeddings/embeddings.js +222 -0
  11. package/src/embeddings/evaluate.js +193 -0
  12. package/src/embeddings/graph.js +376 -0
  13. package/src/embeddings/idmap.js +120 -0
  14. package/src/embeddings/interactions.js +123 -0
  15. package/src/embeddings/memory.js +52 -0
  16. package/src/embeddings/node2vec.js +142 -0
  17. package/src/embeddings/sgns.js +191 -0
  18. package/src/embeddings/skipgram.js +128 -0
  19. package/src/embeddings/trainer.js +147 -0
  20. package/src/embeddings/walker.js +208 -0
  21. package/src/errors.js +41 -0
  22. package/src/heap.js +82 -0
  23. package/src/index/block-codec.js +191 -0
  24. package/src/index/diskann-block-engine.js +891 -0
  25. package/src/index/diskann.js +331 -0
  26. package/src/index/engine-common.js +159 -0
  27. package/src/index/flat.js +49 -0
  28. package/src/index/hnsw.js +282 -0
  29. package/src/index/ivf.js +232 -0
  30. package/src/index/prune.js +70 -0
  31. package/src/index/vector-index.js +391 -0
  32. package/src/lru.js +32 -0
  33. package/src/math.js +666 -0
  34. package/src/metric.js +62 -0
  35. package/src/quant/bq.js +172 -0
  36. package/src/quant/pq.js +418 -0
  37. package/src/quant/scan.js +52 -0
  38. package/src/quant/sq.js +274 -0
  39. package/src/quant/turboquant.js +527 -0
  40. package/src/reduce/pca.js +253 -0
  41. package/src/results.js +86 -0
  42. package/src/search.js +52 -0
  43. package/src/sparse/sparse-index.js +349 -0
  44. package/src/store.js +476 -0
  45. package/src/typed.js +23 -0
  46. package/src/validate.js +130 -0
@@ -0,0 +1,331 @@
1
+ /*
2
+ * vector-index — Apache-2.0 — https://github.com/colocohen/vector-index
3
+ */
4
+
5
+ // ===========================
6
+ // DiskANN engine — Vamana graph
7
+ // single-layer graph, robust pruning with alpha parameter
8
+ //
9
+ // this is the in-memory version — diskann-block-engine.js is the variant
10
+ // whose nodes live in external storage
11
+ // ===========================
12
+
13
+ import { pruneNeighbors } from './prune.js';
14
+ import { liveStats, makeStore, toResults, filteredScan, filterPlan } from './engine-common.js';
15
+
16
+ function createDiskANNEngine(config) {
17
+ const d = config.dimensions;
18
+ const R = config.R; // max neighbors per node
19
+ const L = config.L; // candidate list size during build/search
20
+ const alpha = config.alpha; // robust pruning parameter (typically 1.2)
21
+ const emit = config.emit || (() => {});
22
+
23
+ const store = makeStore(config);
24
+
25
+ // a neighbor list may grow to RMAX = 1.3·R before it is pruned back to R
26
+ // (DiskANN's GRAPH_SLACK_FACTOR). pruning on every overflow re-runs an
27
+ // O(R²) robust prune for each back edge — ~90% of build time; with slack,
28
+ // the next ~0.3·R back edges after a prune are plain appends.
29
+ const RMAX = Math.ceil(R * 1.3);
30
+
31
+ // out-edges: neighbors of slot s are nbr[s*RMAX .. s*RMAX + deg[s])
32
+ let nbr = new Int32Array(store.capacity * RMAX);
33
+ let deg = new Int32Array(store.capacity);
34
+ store.onGrow((cap) => {
35
+ const n = new Int32Array(cap * RMAX); n.set(nbr); nbr = n;
36
+ const g = new Int32Array(cap); g.set(deg); deg = g;
37
+ });
38
+
39
+ let entry = -1; // entry slot (the medoid, approximately)
40
+ const liveSum = new Float64Array(d); // Σ live vectors (as stored) → centroid
41
+ let insertsSinceRefresh = 0;
42
+
43
+ // ===========================
44
+ // candidate list for greedy search — sorted by distance, at most ef long
45
+ //
46
+ // the classic DiskANN "neighbor priority queue": insertion keeps the list
47
+ // sorted (binary search + shift), and a cursor tracks the first entry not
48
+ // expanded yet, so finding the next node to expand is O(1) amortized
49
+ // instead of a scan + sort of the whole list at every step.
50
+ // ===========================
51
+ let listCap = 0;
52
+ let lSlot = null, lDist = null, lDone = null;
53
+ let listSize = 0;
54
+
55
+ function ensureList(ef) {
56
+ if (listCap >= ef + 1) return;
57
+ listCap = ef + 1;
58
+ lSlot = new Int32Array(listCap);
59
+ lDist = new Float64Array(listCap);
60
+ lDone = new Uint8Array(listCap);
61
+ }
62
+
63
+ // insert (dist, slot) keeping order; returns the position, or -1 if it
64
+ // didn't make the cut
65
+ function listInsert(dist, slot, ef) {
66
+ if (listSize === ef && dist >= lDist[listSize - 1]) return -1;
67
+ let lo = 0, hi = listSize;
68
+ while (lo < hi) {
69
+ const mid = (lo + hi) >> 1;
70
+ if (lDist[mid] <= dist) lo = mid + 1;
71
+ else hi = mid;
72
+ }
73
+ const last = listSize < ef ? listSize : ef - 1; // the last one falls off when full
74
+ for (let i = last; i > lo; i--) {
75
+ lSlot[i] = lSlot[i - 1];
76
+ lDist[i] = lDist[i - 1];
77
+ lDone[i] = lDone[i - 1];
78
+ }
79
+ lSlot[lo] = slot;
80
+ lDist[lo] = dist;
81
+ lDone[lo] = 0;
82
+ if (listSize < ef) listSize++;
83
+ return lo;
84
+ }
85
+
86
+ // ===========================
87
+ // Greedy Search — Algorithm 1 from the DiskANN paper
88
+ //
89
+ // after it returns: lSlot/lDist[0 .. listSize) hold the closest ef found
90
+ // (closest first); the returned array holds every expanded slot.
91
+ // soft-deleted nodes are traversed (they keep the graph connected) and
92
+ // filtered out by callers.
93
+ // ===========================
94
+ function greedySearch(h, start, ef) {
95
+ ensureList(ef);
96
+ store.newTraversal();
97
+ listSize = 0;
98
+ store.mark(start);
99
+ listInsert(1 - store.sim(h, start), start, ef);
100
+
101
+ const expanded = [];
102
+ let cursor = 0;
103
+ while (cursor < listSize) {
104
+ const s = lSlot[cursor];
105
+ lDone[cursor] = 1;
106
+ expanded.push(s);
107
+
108
+ let firstInsert = listSize;
109
+ const base = s * RMAX;
110
+ const n = deg[s];
111
+ for (let i = 0; i < n; i++) {
112
+ const t = nbr[base + i];
113
+ if (!store.mark(t)) continue;
114
+ const pos = listInsert(1 - store.sim(h, t), t, ef);
115
+ if (pos !== -1 && pos < firstInsert) firstInsert = pos;
116
+ }
117
+
118
+ // next node to expand: first not-done entry. everything before
119
+ // min(cursor, firstInsert) is already expanded.
120
+ let c = cursor < firstInsert ? cursor : firstInsert;
121
+ while (c < listSize && lDone[c]) c++;
122
+ cursor = c;
123
+ }
124
+ return expanded;
125
+ }
126
+
127
+ // ===========================
128
+ // Robust Prune — Algorithm 2 from the DiskANN paper
129
+ // picks at most R live, diverse neighbors for slot p from candidate slots
130
+ // ===========================
131
+ const slotDist = (a, b) => 1 - store.simSlots(a, b);
132
+
133
+ function robustPrune(p, candidates, alphaParam) {
134
+ store.newTraversal();
135
+ store.mark(p);
136
+ const cs = [];
137
+ for (let i = 0; i < candidates.length; i++) {
138
+ const c = candidates[i];
139
+ if (store.isDeleted(c) || !store.mark(c)) continue;
140
+ cs.push(c);
141
+ }
142
+ const n = cs.length;
143
+ const dist = new Float64Array(n);
144
+ for (let i = 0; i < n; i++) dist[i] = 1 - store.simSlots(p, cs[i]);
145
+ return pruneNeighbors(cs, dist, slotDist, R, alphaParam, false, false, false);
146
+ }
147
+
148
+ function setNeighbors(s, slots) {
149
+ const base = s * RMAX;
150
+ for (let i = 0; i < slots.length; i++) nbr[base + i] = slots[i];
151
+ deg[s] = slots.length;
152
+ }
153
+
154
+ function hasEdge(s, t) {
155
+ const base = s * RMAX;
156
+ for (let i = 0; i < deg[s]; i++) if (nbr[base + i] === t) return true;
157
+ return false;
158
+ }
159
+
160
+ // add edge n → s; once n holds RMAX edges, prune back to R
161
+ // (tombstones drop out of the list here)
162
+ function addBackEdge(n, s, alphaParam) {
163
+ if (deg[n] < RMAX) {
164
+ nbr[n * RMAX + deg[n]++] = s;
165
+ return;
166
+ }
167
+ const cands = new Array(deg[n] + 1);
168
+ for (let i = 0; i < deg[n]; i++) cands[i] = nbr[n * RMAX + i];
169
+ cands[deg[n]] = s;
170
+ setNeighbors(n, robustPrune(n, cands, alphaParam));
171
+ }
172
+
173
+ // candidates for a node = everything expanded + the final list
174
+ function candidatesFrom(expanded, extra) {
175
+ const out = expanded.slice();
176
+ for (let i = 0; i < listSize; i++) out.push(lSlot[i]);
177
+ if (extra) for (let i = 0; i < extra.length; i++) out.push(extra[i]);
178
+ return out;
179
+ }
180
+
181
+ function insertSlot(slot, h) {
182
+ if (entry === -1) {
183
+ entry = slot;
184
+ return;
185
+ }
186
+ const expanded = greedySearch(h, entry, L);
187
+ const selected = robustPrune(slot, candidatesFrom(expanded), alpha);
188
+
189
+ // no live node reachable (only tombstones left): become the entry point
190
+ // so later inserts attach to this node instead of ending up isolated
191
+ if (selected.length === 0) {
192
+ entry = slot;
193
+ return;
194
+ }
195
+ setNeighbors(slot, selected);
196
+ for (const n of selected) addBackEdge(n, slot, alpha);
197
+ }
198
+
199
+ // the medoid of live vectors, approximately: the node a greedy search for
200
+ // the centroid lands on. the centroid comes from a running sum, so this is
201
+ // one ordinary search instead of a full scan.
202
+ function refreshEntry() {
203
+ if (store.live === 0 || entry === -1) return;
204
+ const c = new Float32Array(d);
205
+ for (let i = 0; i < d; i++) c[i] = liveSum[i] / store.live; // the mean (for l2 the scale matters)
206
+ greedySearch(store.query(c), entry, L);
207
+ for (let i = 0; i < listSize; i++) {
208
+ if (!store.isDeleted(lSlot[i])) {
209
+ entry = lSlot[i];
210
+ break;
211
+ }
212
+ }
213
+ insertsSinceRefresh = 0;
214
+ }
215
+
216
+ function add(id, vector, bytes) {
217
+ const slot = store.add(id, vector, bytes);
218
+ const v = store.input(vector);
219
+ for (let i = 0; i < d; i++) liveSum[i] += v[i];
220
+ insertSlot(slot, store.nodeQuery(slot, vector));
221
+ // refresh on doubling: after 10, 20, 40, 80, ... inserts
222
+ if (++insertsSinceRefresh >= Math.max(10, store.live / 4)) refreshEntry();
223
+ }
224
+
225
+ function del(id) {
226
+ const slot = store.remove(id);
227
+ const v = store.vectorOf(slot);
228
+ for (let i = 0; i < d; i++) liveSum[i] -= v[i];
229
+ }
230
+
231
+ // Two-pass rebuild — the DiskANN paper's Algorithm 3
232
+ // pass 1: alpha=1.0 (greedy nearest neighbors)
233
+ // pass 2: alpha=alpha (add long-range edges via robust pruning)
234
+ //
235
+ // a generator (time-sliced by the wrapper): yields after each node. a
236
+ // node's list is replaced in one step, so the graph is valid at every
237
+ // yield — searches running between slices just see it improve.
238
+ function* buildJob() {
239
+ if (store.live < 2) return;
240
+ refreshEntry();
241
+ yield;
242
+
243
+ const live = [];
244
+ for (let s = 0; s < store.count; s++) if (!store.isDeleted(s)) live.push(s);
245
+ const reportEvery = Math.max(1, Math.floor(live.length / 20));
246
+ const alphas = [1.0, alpha];
247
+
248
+ for (let pass = 0; pass < 2; pass++) {
249
+ for (let i = 0; i < live.length; i++) {
250
+ const s = live[i];
251
+ const current = Array.from(nbr.subarray(s * RMAX, s * RMAX + deg[s]));
252
+ const expanded = greedySearch(store.nodeQuery(s, store.vectorOf(s)), entry, L);
253
+ const selected = robustPrune(s, candidatesFrom(expanded, current), alphas[pass]);
254
+ setNeighbors(s, selected);
255
+ for (const n of selected) if (!hasEdge(n, s)) addBackEdge(n, s, alphas[pass]);
256
+
257
+ if ((i + 1) % reportEvery === 0 || i === live.length - 1) {
258
+ emit('progress', {
259
+ phase: 'build',
260
+ pass: pass + 1,
261
+ processed: i + 1,
262
+ total: live.length,
263
+ percent: Math.round((pass * live.length + i + 1) / (2 * live.length) * 100)
264
+ });
265
+ }
266
+ yield;
267
+ }
268
+ }
269
+ }
270
+
271
+ function search(query, k, accept) {
272
+ if (store.live === 0 || entry === -1) return [];
273
+ const h = store.query(query);
274
+ let widen = 1;
275
+ if (accept) {
276
+ const plan = filterPlan(store, accept);
277
+ if (plan.scan) return filteredScan(store, h, k, accept);
278
+ widen = plan.widen;
279
+ }
280
+ // widen the beam by the deleted ratio (and the filter's) so k live,
281
+ // matching results survive
282
+ const base = Math.max(L, k);
283
+ const ef = Math.min(store.count,
284
+ Math.ceil((base + base * (store.count - store.live) / store.count) * widen));
285
+ // fewer than k although more live (matching) nodes exist: the beam saw
286
+ // mostly tombstones — a deleted region. widen it (×4, up to 3 times),
287
+ // then scan
288
+ let out;
289
+ for (let tries = 0, e = ef; ; tries++, e = Math.min(store.count, e * 4)) {
290
+ greedySearch(h, entry, e);
291
+ out = toResults(store, h, lDist, lSlot, listSize, k, accept);
292
+ if (out.length >= k || out.length >= store.live || tries === 3 || e === store.count) break;
293
+ }
294
+ if (out.length < k && out.length < store.live) return filteredScan(store, h, k, accept || (() => true));
295
+ return out;
296
+ }
297
+
298
+ function stats() {
299
+ let totalDegree = 0, maxDegree = 0, minDegree = Infinity;
300
+ for (let s = 0; s < store.count; s++) {
301
+ if (store.isDeleted(s)) continue;
302
+ const g = deg[s];
303
+ totalDegree += g;
304
+ if (g > maxDegree) maxDegree = g;
305
+ if (g < minDegree) minDegree = g;
306
+ }
307
+ return {
308
+ ...liveStats(store),
309
+ R,
310
+ L,
311
+ alpha,
312
+ avgDegree: store.live === 0 ? 0 : parseFloat((totalDegree / store.live).toFixed(1)),
313
+ maxDegree,
314
+ minDegree: store.live === 0 ? 0 : minDegree,
315
+ entryPoint: entry === -1 ? null : store.idOf(entry)
316
+ };
317
+ }
318
+
319
+ return {
320
+ add,
321
+ delete: del,
322
+ search,
323
+ stats,
324
+ buildJob,
325
+ liveEntries: store.liveEntries,
326
+ cloneEmpty: () => createDiskANNEngine(config),
327
+ size: () => store.live
328
+ };
329
+ }
330
+
331
+ export { createDiskANNEngine };
@@ -0,0 +1,159 @@
1
+ /*
2
+ * vector-index — Apache-2.0 — https://github.com/colocohen/vector-index
3
+ */
4
+
5
+ // ===========================
6
+ // shared by the in-memory engines (flat.js, ivf.js, diskann.js, hnsw.js)
7
+ //
8
+ // all four have the same internal shape (store.js):
9
+ // - vectors live in one contiguous Float32Array (normalized for cosine),
10
+ // addressed by a dense integer "slot"
11
+ // - the caller's id is translated to a slot once, at the boundary
12
+ // - graphs store neighbor slots in Int32Arrays, visited marks are epochs,
13
+ // heaps are typed — a search allocates almost nothing
14
+ //
15
+ // engine interface (used by vector-index.js):
16
+ // add(id, vector[, bytes]) delete(id) search(query, k) → [{ id, score | distance }]
17
+ // stats() liveEntries() cloneEmpty() [trainJob(vectors)] [buildJob()]
18
+ //
19
+ // with option `vectors` the engine holds a code store (store.js) built from
20
+ // the caller's functions; `bytes` is only passed by compact, to move stored
21
+ // codes without re-encoding. engines never look inside the bytes — they
22
+ // only call store.query / store.sim / store.simSlots.
23
+ // engines compare by a similarity, higher = closer, and use 1 − similarity
24
+ // as their distance (store.js): 1 − cos for 'cosine', ‖q − v‖² for 'l2'.
25
+ // results: { id, score } (the cosine similarity) for 'cosine',
26
+ // { id, distance } (the euclidean distance) for 'l2',
27
+ // { id, score } (the inner product ⟨q, v⟩) for 'dot'.
28
+ // ===========================
29
+
30
+ import { createVectorStore, createCodeStore } from '../store.js';
31
+ import { createHeap } from '../heap.js';
32
+ import { resultOf as metricResult } from '../metric.js';
33
+
34
+ function makeStore(config) {
35
+ return config.format
36
+ ? createCodeStore(config.dimensions, config.format, undefined, config.metric)
37
+ : createVectorStore(config.dimensions, undefined, config.metric);
38
+ }
39
+
40
+ // one result from a similarity (h: the query handle — 'dot' reports the
41
+ // exact inner product, not the augmented distance it ranked by)
42
+ function resultOf(store, h, slot, sim) {
43
+ return store.metric === 'dot'
44
+ ? { id: store.idOf(slot), score: store.ip(h, slot) }
45
+ : metricResult(store.metric, store.idOf(slot), 1 - sim);
46
+ }
47
+
48
+ // turn closest-first (distance, slot) arrays into results — live nodes only
49
+ function toResults(store, h, dist, slots, n, k, accept) {
50
+ const out = [];
51
+ for (let i = 0; i < n && out.length < k; i++) {
52
+ const s = slots[i];
53
+ if (store.isDeleted(s)) continue;
54
+ if (accept && !accept(store.idOf(s))) continue;
55
+ out.push(resultOf(store, h, s, 1 - dist[i]));
56
+ }
57
+ return out;
58
+ }
59
+
60
+ // bounded top-k by similarity over a list of candidate slots
61
+ const ABANDON_PROBE = 1024;
62
+
63
+ function topKBySimilarity(store, h, k, forEachCandidate) {
64
+ // min-heap on similarity: the top is the worst of the current best k
65
+ const heap = createHeap(false, k + 1);
66
+ // l2 with full vectors: early abandoning (math.js l2sqAtBounded). it saves
67
+ // a lot when most vectors are clearly farther than the k-th (clusters),
68
+ // and nothing when every distance is about the same (uniform data), where
69
+ // the checks only cost. so it's measured: after ABANDON_PROBE candidates,
70
+ // if under 25% of the dimensions were skipped, this query goes on without.
71
+ let bounded = store.simBounded;
72
+ const work = bounded ? new Float64Array(1) : null;
73
+ let probed = 0;
74
+ forEachCandidate((s) => {
75
+ let sim;
76
+ if (bounded && heap.size() === k) {
77
+ sim = bounded(h, s, heap.topDist(), work);
78
+ if (++probed === ABANDON_PROBE && work[0] > 0.75 * probed * store.dimensions) bounded = null;
79
+ } else {
80
+ sim = store.sim(h, s);
81
+ }
82
+ if (heap.size() < k) heap.push(sim, s);
83
+ else if (sim > heap.topDist()) {
84
+ heap.pop();
85
+ heap.push(sim, s);
86
+ }
87
+ });
88
+ const { dist, slot } = heap.drainSorted(); // ascending similarity
89
+ const out = new Array(dist.length);
90
+ for (let i = 0; i < dist.length; i++) {
91
+ out[dist.length - 1 - i] = resultOf(store, h, slot[i], dist[i]);
92
+ }
93
+ return out;
94
+ }
95
+
96
+ // ===========================
97
+ // filtered search (option filter) — shared by the graph engines
98
+ //
99
+ // a node that fails the filter is treated like a tombstone: the traversal
100
+ // still passes through it, it just never takes a result slot. that works
101
+ // while enough nodes pass; with a very selective filter the graph search
102
+ // would wander through most of the index to find k matches, and an exact
103
+ // scan of the matching nodes is cheaper. so:
104
+ // - estimate the pass rate on a sample of live nodes
105
+ // - small index, or under FILTER_SCAN_RATE passing → exact scan
106
+ // - otherwise the graph search, beam widened by 1 / pass rate; if it
107
+ // still finds fewer than k, the exact scan
108
+ // ===========================
109
+ const FILTER_SAMPLE = 200;
110
+ const FILTER_SCAN_RATE = 0.05;
111
+ const FILTER_SCAN_SIZE = 2000;
112
+
113
+ // fraction of live nodes that pass, from an evenly spread sample
114
+ function filterPassRate(store, accept) {
115
+ const n = store.count;
116
+ const deleted = store.deletedFlags;
117
+ const step = Math.max(1, Math.floor(n / FILTER_SAMPLE));
118
+ let seen = 0, pass = 0;
119
+ for (let s = 0; s < n && seen < FILTER_SAMPLE; s += step) {
120
+ if (deleted[s]) continue;
121
+ seen++;
122
+ if (accept(store.idOf(s))) pass++;
123
+ }
124
+ return seen === 0 ? 0 : pass / seen;
125
+ }
126
+
127
+ // exact top-k over the live nodes that pass
128
+ function filteredScan(store, h, k, accept) {
129
+ const n = store.count;
130
+ const deleted = store.deletedFlags;
131
+ return topKBySimilarity(store, h, k, (visit) => {
132
+ for (let s = 0; s < n; s++) if (!deleted[s] && accept(store.idOf(s))) visit(s);
133
+ });
134
+ }
135
+
136
+ // the plan for one filtered graph search: { scan: true } or { widen }
137
+ function filterPlan(store, accept) {
138
+ if (store.live <= FILTER_SCAN_SIZE) return { scan: true };
139
+ const rate = filterPassRate(store, accept);
140
+ if (rate < FILTER_SCAN_RATE) return { scan: true };
141
+ return { widen: 1 / rate };
142
+ }
143
+
144
+ // the counts every engine's stats() starts with
145
+ function liveStats(store) {
146
+ const total = store.count;
147
+ const deleted = total - store.live;
148
+ return { total, alive: store.live, deleted, ratio: total === 0 ? 0 : deleted / total };
149
+ }
150
+
151
+ export {
152
+ liveStats,
153
+ makeStore,
154
+ resultOf,
155
+ toResults,
156
+ topKBySimilarity,
157
+ filteredScan,
158
+ filterPlan
159
+ };
@@ -0,0 +1,49 @@
1
+ /*
2
+ * vector-index — Apache-2.0 — https://github.com/colocohen/vector-index
3
+ */
4
+
5
+ // ===========================
6
+ // Flat engine — brute force
7
+ // scans everything, 100% recall, no tricks
8
+ // good for: small datasets, baseline for other algorithms
9
+ // ===========================
10
+
11
+ import { liveStats, makeStore, topKBySimilarity, filteredScan } from './engine-common.js';
12
+
13
+ function createFlatEngine(config) {
14
+ const store = makeStore(config);
15
+
16
+ function add(id, vector, bytes) {
17
+ store.add(id, vector, bytes);
18
+ }
19
+
20
+ function del(id) {
21
+ store.remove(id);
22
+ }
23
+
24
+ function search(query, k, accept) {
25
+ const h = store.query(query);
26
+ if (accept) return filteredScan(store, h, k, accept);
27
+ const n = store.count;
28
+ const deleted = store.deletedFlags;
29
+ return topKBySimilarity(store, h, k, (visit) => {
30
+ for (let s = 0; s < n; s++) if (!deleted[s]) visit(s);
31
+ });
32
+ }
33
+
34
+ function stats() {
35
+ return liveStats(store);
36
+ }
37
+
38
+ return {
39
+ add,
40
+ delete: del,
41
+ search,
42
+ stats,
43
+ liveEntries: store.liveEntries,
44
+ cloneEmpty: () => createFlatEngine(config),
45
+ size: () => store.live
46
+ };
47
+ }
48
+
49
+ export { createFlatEngine };