@delali/narsil 0.3.1-next.86 → 0.3.1-next.87
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/dist/browser/chunk-3BXQPIXK.mjs +602 -0
- package/dist/browser/chunk-3ZRMZIH4.mjs +3959 -0
- package/dist/browser/chunk-BBDAYUCO.mjs +2703 -0
- package/dist/browser/chunk-FHFFWLEK.mjs +669 -0
- package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
- package/dist/browser/chunk-NQW2YXEX.mjs +346 -0
- package/dist/browser/chunk-TBMNPGFO.mjs +22910 -0
- package/dist/browser/durability-wiring-7RUN7LNS.mjs +3250 -0
- package/dist/browser/module-E25UDADM.mjs +2766 -0
- package/dist/browser/snapshot-A42DRERF.mjs +888 -0
- package/dist/browser/write-4U6Q26TJ.mjs +206 -0
- package/dist/{capabilities-CAdkp2P-.d.ts → capabilities-SJPttcgQ.d.ts} +2 -2
- package/dist/chunk-37CD3OKS.mjs +87 -0
- package/dist/chunk-AQU2QHSY.mjs +7247 -0
- package/dist/{chunk-OLXM65YH.mjs → chunk-AV4IJKD2.mjs} +1 -18
- package/dist/{chunk-4D6OH4EF.mjs → chunk-AXDAS2VA.mjs} +7 -5
- package/dist/{chunk-LS5IRM23.mjs → chunk-BVOJIYRV.mjs} +2 -25
- package/dist/chunk-C7SLA6CM.mjs +19 -0
- package/dist/{chunk-KJ7LB3TR.mjs → chunk-CVQWQVFX.mjs} +3 -22
- package/dist/chunk-DJHHF2L5.mjs +17 -0
- package/dist/chunk-FGMGUDHX.mjs +21 -0
- package/dist/chunk-IKST36MA.mjs +614 -0
- package/dist/{chunk-2453RJYK.mjs → chunk-J2BPXTD7.mjs} +167 -176
- package/dist/chunk-JFJBA3PD.mjs +522 -0
- package/dist/chunk-K4OWQ2M3.mjs +462 -0
- package/dist/chunk-L5ORED7V.mjs +25 -0
- package/dist/chunk-MKK4OZ5W.mjs +703 -0
- package/dist/chunk-MXS5HWIH.mjs +515 -0
- package/dist/chunk-P36MUFEJ.mjs +10 -0
- package/dist/chunk-PR4O7CAP.mjs +18 -0
- package/dist/{chunk-4VBG6PEH.mjs → chunk-PSUJCOSE.mjs} +3 -2
- package/dist/{chunk-ZCKPRCYI.mjs → chunk-Q5DC6WAB.mjs} +1 -1
- package/dist/{chunk-NFF5TDRG.mjs → chunk-QZN3MIJ3.mjs} +1 -16
- package/dist/chunk-R7OSDAFK.mjs +44 -0
- package/dist/chunk-RVM7AODE.mjs +73 -0
- package/dist/{chunk-XYDP7CCJ.mjs → chunk-SYRFSF6V.mjs} +6 -4
- package/dist/{chunk-SREPGC6O.mjs → chunk-T6CNO72J.mjs} +1 -1
- package/dist/chunk-T7DMUVTL.mjs +1714 -0
- package/dist/chunk-UKSUENHK.mjs +997 -0
- package/dist/{chunk-B3F7LZXM.mjs → chunk-ULRPNYAG.mjs} +1220 -2318
- package/dist/{chunk-GNYXCLJS.mjs → chunk-URGLSNKR.mjs} +4 -3
- package/dist/chunk-WQSHDZ7Z.mjs +631 -0
- package/dist/{chunk-XPK3GB2P.mjs → chunk-XBXKPWMR.mjs} +20 -2664
- package/dist/{chunk-RM6FRYEC.mjs → chunk-XSZVS4S3.mjs} +3 -2
- package/dist/chunk-Y2F66IQJ.mjs +74 -0
- package/dist/chunk-YCJLVNWR.mjs +262 -0
- package/dist/{client-CKUtkLz3.d.ts → client-BMK53IR2.d.ts} +3 -3
- package/dist/client.d.ts +8 -8
- package/dist/{config-CSDEPraV.d.ts → config-BUR-7eVQ.d.ts} +2 -2
- package/dist/{config-BuIkeM5C.d.ts → config-UJx1dyX1.d.ts} +2 -2
- package/dist/direct-executor-LD6JW57R.mjs +18 -0
- package/dist/distribution/coordinator/etcd.d.ts +2 -2
- package/dist/distribution/coordinator/in-memory.d.ts +2 -2
- package/dist/distribution.d.ts +8 -8
- package/dist/distribution.mjs +30 -15
- package/dist/durability-wiring-5S32KDDC.mjs +2797 -0
- package/dist/{engine-DMNZJAUq.d.ts → engine-9X6W5naj.d.ts} +2 -2
- package/dist/{index-DzONZUKE.d.ts → index-vCMu8OeQ.d.ts} +1 -1
- package/dist/index.browser.mjs +2 -38128
- package/dist/index.d.ts +23 -9
- package/dist/index.mjs +18 -14
- package/dist/{memory-BPKNx9Tl.d.ts → memory-CV9NAxLY.d.ts} +1 -1
- package/dist/module-WIDYIJLY.mjs +1819 -0
- package/dist/partial-words.mjs +5 -3
- package/dist/pattern.mjs +3 -2
- package/dist/persistence/durability/checkpoint-worker.d.ts +1 -1
- package/dist/persistence/durability/checkpoint-worker.mjs +23 -13
- package/dist/react.d.ts +7 -7
- package/dist/{schema-DgyDijhf.d.ts → schema-DtsgmNfU.d.ts} +1 -1
- package/dist/server.d.ts +10 -10
- package/dist/server.mjs +14 -9
- package/dist/snapshot-6BYSUPWL.mjs +469 -0
- package/dist/{tasks-DGyz586z.d.ts → tasks-BGaZJyoY.d.ts} +1 -1
- package/dist/{thread-EUUNJNKU.mjs → thread-GS65E6RM.mjs} +12 -9
- package/dist/vector/search-worker.d.ts +30 -3
- package/dist/vector/search-worker.mjs +6 -2
- package/dist/workers/entry.mjs +5 -3
- package/dist/write-HLEZTL2L.mjs +233 -0
- package/package.json +11 -7
- package/dist/chunk-IDLP54FH.mjs +0 -15198
- package/dist/direct-executor-SI2QV4YI.mjs +0 -19
- package/dist/simd-DW5AtJ6B.d.ts +0 -41
|
@@ -0,0 +1,1819 @@
|
|
|
1
|
+
import { VECTOR_WORKER_COPIES_ALLOWED, DEFAULT_PROMOTION_THRESHOLD, DEFAULT_FILTER_THRESHOLD, recordCheckpoint, planCheckpoint, deserialize, assignStorePartitions, liveSize, threadsHoldCurrentLayout, filterForOptions, OSQ4_MIN_DIMENSION, emptyFieldBeforeRestore, ESTIMATED_MS_PER_TOMBSTONE, graphNeedsRebuild, ESTIMATED_MS_PER_VECTOR_REBUILD, releaseReplacedVectors, WORKER_COPY_MIN_VECTORS, allLiveDocIds, recalibrateFromStore, signatureOf, adoptGraph, calibrateQuantizer, fieldHandlesOf, BUILD_CHUNK_SIZE, yieldToEventLoop, DISK_ADOPTION_YIELD_INTERVAL } from './chunk-MXS5HWIH.mjs';
|
|
2
|
+
import { isDead, NO_LIVE_VECTOR_AT_POSITION, bytesToVectors } from './chunk-YCJLVNWR.mjs';
|
|
3
|
+
import { createVectorStore } from './chunk-UKSUENHK.mjs';
|
|
4
|
+
export { openSharedVectorField } from './chunk-UKSUENHK.mjs';
|
|
5
|
+
import { osqBitsOf, createOsqQuantizer, ordinalFilterValues, STORE_UNPLACED_COUNT, STORE_LIVE_COUNT, STORE_REMOVED_COUNT, createOrdinalFilter, addToOrdinalFilter, nativeScoresOf, toScore, ordinalFilterHas, magnitude, nativeStoreScratchBytes, toDistance, euclideanDistance, dotProduct, cosineSimilarityWithMagnitudes, createHNSWIndex, topLayerInParts, sharedMemoryAvailable, requireWholeNeighbourLists, releaseLocksHeldBy } from './chunk-ULRPNYAG.mjs';
|
|
6
|
+
export { releaseLocksHeldBy } from './chunk-ULRPNYAG.mjs';
|
|
7
|
+
import './chunk-37CD3OKS.mjs';
|
|
8
|
+
import { noteFallback } from './chunk-DJHHF2L5.mjs';
|
|
9
|
+
import { spawnNodeWorker } from './chunk-C7SLA6CM.mjs';
|
|
10
|
+
import { resolveWorkerCount, resolveWorkerEntry, workerResourceLimits } from './chunk-RVM7AODE.mjs';
|
|
11
|
+
import { detectRuntime } from './chunk-AV4IJKD2.mjs';
|
|
12
|
+
import './chunk-Q5DC6WAB.mjs';
|
|
13
|
+
import { DISK_STORAGE_BLOCK_BYTES, UNPLACED_FLAG_CHUNK_ORDINALS, GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER, GRAPH_BUILD_DISPATCH_CHUNK, VECTOR_SEARCH_TIMEOUT_MS, VECTOR_SEARCH_LOAD_TIMEOUT_MS, VECTOR_INSERT_TIMEOUT_MS, VECTOR_SCRATCH_SLOTS } from './chunk-Y2F66IQJ.mjs';
|
|
14
|
+
import './chunk-ZJ4CSAB2.mjs';
|
|
15
|
+
import './chunk-QZN3MIJ3.mjs';
|
|
16
|
+
import './chunk-FGMGUDHX.mjs';
|
|
17
|
+
import './chunk-KEHK2O6P.mjs';
|
|
18
|
+
import './chunk-R6A773WK.mjs';
|
|
19
|
+
import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
|
|
20
|
+
import { NarsilError, ErrorCodes } from './chunk-TPJU7NDJ.mjs';
|
|
21
|
+
|
|
22
|
+
// src/core/heap.ts
|
|
23
|
+
function createMaxHeap(compare) {
|
|
24
|
+
return createHeap((a, b) => compare(b, a));
|
|
25
|
+
}
|
|
26
|
+
function createBoundedMaxHeap(compare, capacity) {
|
|
27
|
+
const heap = createMaxHeap(compare);
|
|
28
|
+
const innerPush = heap.push;
|
|
29
|
+
return {
|
|
30
|
+
push(item) {
|
|
31
|
+
if (heap.size < capacity) {
|
|
32
|
+
innerPush(item);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const top = heap.peek();
|
|
36
|
+
if (top !== void 0 && compare(item, top) < 0) {
|
|
37
|
+
heap.pop();
|
|
38
|
+
innerPush(item);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
pop: heap.pop,
|
|
42
|
+
peek: heap.peek,
|
|
43
|
+
get size() {
|
|
44
|
+
return heap.size;
|
|
45
|
+
},
|
|
46
|
+
toSortedArray: heap.toSortedArray
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function createHeap(compare) {
|
|
50
|
+
const items = [];
|
|
51
|
+
function siftUp(idx) {
|
|
52
|
+
while (idx > 0) {
|
|
53
|
+
const parent = idx - 1 >> 1;
|
|
54
|
+
if (compare(items[idx], items[parent]) >= 0) break;
|
|
55
|
+
const tmp = items[idx];
|
|
56
|
+
items[idx] = items[parent];
|
|
57
|
+
items[parent] = tmp;
|
|
58
|
+
idx = parent;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function siftDown(idx) {
|
|
62
|
+
const len = items.length;
|
|
63
|
+
while (true) {
|
|
64
|
+
let smallest = idx;
|
|
65
|
+
const left = 2 * idx + 1;
|
|
66
|
+
const right = 2 * idx + 2;
|
|
67
|
+
if (left < len && compare(items[left], items[smallest]) < 0) smallest = left;
|
|
68
|
+
if (right < len && compare(items[right], items[smallest]) < 0) smallest = right;
|
|
69
|
+
if (smallest === idx) break;
|
|
70
|
+
const tmp = items[idx];
|
|
71
|
+
items[idx] = items[smallest];
|
|
72
|
+
items[smallest] = tmp;
|
|
73
|
+
idx = smallest;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
push(item) {
|
|
78
|
+
items.push(item);
|
|
79
|
+
siftUp(items.length - 1);
|
|
80
|
+
},
|
|
81
|
+
pop() {
|
|
82
|
+
if (items.length === 0) return void 0;
|
|
83
|
+
const top = items[0];
|
|
84
|
+
const last = items.pop();
|
|
85
|
+
if (items.length > 0) {
|
|
86
|
+
items[0] = last;
|
|
87
|
+
siftDown(0);
|
|
88
|
+
}
|
|
89
|
+
return top;
|
|
90
|
+
},
|
|
91
|
+
peek() {
|
|
92
|
+
return items[0];
|
|
93
|
+
},
|
|
94
|
+
get size() {
|
|
95
|
+
return items.length;
|
|
96
|
+
},
|
|
97
|
+
toSortedArray() {
|
|
98
|
+
return [...items].sort(compare);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/vector/vector-index/merge-results.ts
|
|
104
|
+
function highScoreFirst(a, b) {
|
|
105
|
+
return b.score - a.score || compareCodePoints(a.docId, b.docId);
|
|
106
|
+
}
|
|
107
|
+
function mergeResults(hnswResults, bufferResults, k) {
|
|
108
|
+
const seen = /* @__PURE__ */ new Set();
|
|
109
|
+
const merged = [];
|
|
110
|
+
let hi = 0;
|
|
111
|
+
let bi = 0;
|
|
112
|
+
while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
|
|
113
|
+
const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
|
|
114
|
+
const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
|
|
115
|
+
let pick;
|
|
116
|
+
if (h && b) {
|
|
117
|
+
if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
|
|
118
|
+
pick = h;
|
|
119
|
+
hi++;
|
|
120
|
+
} else {
|
|
121
|
+
pick = b;
|
|
122
|
+
bi++;
|
|
123
|
+
}
|
|
124
|
+
} else if (h) {
|
|
125
|
+
pick = h;
|
|
126
|
+
hi++;
|
|
127
|
+
} else if (b) {
|
|
128
|
+
pick = b;
|
|
129
|
+
bi++;
|
|
130
|
+
} else {
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
if (seen.has(pick.docId)) continue;
|
|
134
|
+
seen.add(pick.docId);
|
|
135
|
+
merged.push(pick);
|
|
136
|
+
}
|
|
137
|
+
return merged;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/vector/shared-field/store-flags.ts
|
|
141
|
+
function bytesOf(flags, source) {
|
|
142
|
+
if (flags.source !== source) {
|
|
143
|
+
flags.source = source;
|
|
144
|
+
flags.view = new Uint8Array(source);
|
|
145
|
+
}
|
|
146
|
+
return flags.view;
|
|
147
|
+
}
|
|
148
|
+
function countsOf(chunks, source) {
|
|
149
|
+
if (chunks.source !== source) {
|
|
150
|
+
chunks.source = source;
|
|
151
|
+
chunks.view = new Int32Array(source);
|
|
152
|
+
}
|
|
153
|
+
return chunks.view;
|
|
154
|
+
}
|
|
155
|
+
function readStoreFlags(store) {
|
|
156
|
+
const unplaced = { source: null, view: new Uint8Array(0) };
|
|
157
|
+
const unplacedChunks = { source: null, view: new Int32Array(0) };
|
|
158
|
+
const removed = { source: null, view: new Uint8Array(0) };
|
|
159
|
+
return {
|
|
160
|
+
liveDocuments() {
|
|
161
|
+
const header = store.handles.header;
|
|
162
|
+
return Atomics.load(header, STORE_LIVE_COUNT) - Atomics.load(header, STORE_REMOVED_COUNT);
|
|
163
|
+
},
|
|
164
|
+
isRemoved(ordinal) {
|
|
165
|
+
const bytes = bytesOf(removed, store.handles.removed);
|
|
166
|
+
return ordinal < bytes.length && Atomics.load(bytes, ordinal) === 1;
|
|
167
|
+
},
|
|
168
|
+
unplacedOrdinals() {
|
|
169
|
+
const handles = store.handles;
|
|
170
|
+
if (Atomics.load(handles.header, STORE_UNPLACED_COUNT) === 0) return [];
|
|
171
|
+
const bytes = bytesOf(unplaced, handles.unplaced);
|
|
172
|
+
const counts = countsOf(unplacedChunks, handles.unplacedChunks);
|
|
173
|
+
const end = Math.min(store.slots, bytes.length);
|
|
174
|
+
const ordinals = [];
|
|
175
|
+
for (let chunk = 0; chunk < counts.length; chunk++) {
|
|
176
|
+
const from = chunk * UNPLACED_FLAG_CHUNK_ORDINALS;
|
|
177
|
+
if (from >= end) break;
|
|
178
|
+
if (Atomics.load(counts, chunk) === 0) continue;
|
|
179
|
+
const to = Math.min(end, from + UNPLACED_FLAG_CHUNK_ORDINALS);
|
|
180
|
+
for (let ordinal = from; ordinal < to; ordinal++) {
|
|
181
|
+
if (Atomics.load(bytes, ordinal) === 1) ordinals.push(ordinal);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return ordinals;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/vector/shared-field/searcher.ts
|
|
190
|
+
function distancesInTypeScript(store, query, metric, ordinals) {
|
|
191
|
+
noteFallback("score");
|
|
192
|
+
const arenaQuery = store.prepareQueryArena(query);
|
|
193
|
+
const queryMagnitude = arenaQuery ? arenaQuery.magnitude : magnitude(query);
|
|
194
|
+
const distances = new Float64Array(ordinals.length).fill(Number.POSITIVE_INFINITY);
|
|
195
|
+
for (let i = 0; i < ordinals.length; i++) {
|
|
196
|
+
if (arenaQuery) {
|
|
197
|
+
distances[i] = store.distanceFromArena(arenaQuery, ordinals[i], metric);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const entry = store.entryForOrdinal(ordinals[i]);
|
|
201
|
+
if (entry) distances[i] = toDistance(query, entry.vector, queryMagnitude, entry.magnitude, metric);
|
|
202
|
+
}
|
|
203
|
+
return distances;
|
|
204
|
+
}
|
|
205
|
+
function scoreCandidates(store, candidates, query, k, metric, minSimilarity) {
|
|
206
|
+
const { ordinals, docIds } = candidates;
|
|
207
|
+
if (ordinals.length === 0) return { results: [], matched: 0 };
|
|
208
|
+
const distances = nativeScoresOf(store.handles, query, metric, ordinals) ?? distancesInTypeScript(store, query, metric, ordinals);
|
|
209
|
+
const heap = createBoundedMaxHeap(highScoreFirst, k);
|
|
210
|
+
let matched = 0;
|
|
211
|
+
for (let i = 0; i < ordinals.length; i++) {
|
|
212
|
+
if (distances[i] === Number.POSITIVE_INFINITY) continue;
|
|
213
|
+
const score = toScore(distances[i], metric);
|
|
214
|
+
if (score < minSimilarity) continue;
|
|
215
|
+
matched++;
|
|
216
|
+
heap.push({ docId: docIds[i], score });
|
|
217
|
+
}
|
|
218
|
+
return { results: heap.toSortedArray().reverse(), matched };
|
|
219
|
+
}
|
|
220
|
+
function createSharedVectorSearcher(options) {
|
|
221
|
+
const { fieldName, view, holdsDocument } = options;
|
|
222
|
+
const flags = readStoreFlags(view.store);
|
|
223
|
+
function filterForDocIds(allowed) {
|
|
224
|
+
const filter = createOrdinalFilter(view.store.slots);
|
|
225
|
+
for (const docId of allowed) {
|
|
226
|
+
const ordinal = view.ordinalOf(docId);
|
|
227
|
+
if (ordinal === void 0 || flags.isRemoved(ordinal)) continue;
|
|
228
|
+
addToOrdinalFilter(filter, ordinal);
|
|
229
|
+
}
|
|
230
|
+
return filter;
|
|
231
|
+
}
|
|
232
|
+
function filterForPartitions(allowed) {
|
|
233
|
+
const slots = view.store.slots;
|
|
234
|
+
const filter = createOrdinalFilter(slots);
|
|
235
|
+
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
236
|
+
const partition = view.store.partitionAt(ordinal);
|
|
237
|
+
if (partition === void 0 || !allowed.has(partition)) continue;
|
|
238
|
+
if (!view.store.holdsOrdinal(ordinal) || flags.isRemoved(ordinal)) continue;
|
|
239
|
+
addToOrdinalFilter(filter, ordinal);
|
|
240
|
+
}
|
|
241
|
+
return filter;
|
|
242
|
+
}
|
|
243
|
+
function filterFor(searchOptions) {
|
|
244
|
+
if (searchOptions.filterDocIds !== void 0) return filterForDocIds(searchOptions.filterDocIds);
|
|
245
|
+
if (searchOptions.filterPartitions !== void 0) return filterForPartitions(searchOptions.filterPartitions);
|
|
246
|
+
return void 0;
|
|
247
|
+
}
|
|
248
|
+
function candidatesOf(ordinals, filter) {
|
|
249
|
+
const candidates = { ordinals: [], docIds: [] };
|
|
250
|
+
for (const ordinal of ordinals) {
|
|
251
|
+
if (filter !== void 0 && !ordinalFilterHas(filter, ordinal)) continue;
|
|
252
|
+
if (flags.isRemoved(ordinal)) continue;
|
|
253
|
+
const docId = view.docIdOf(ordinal);
|
|
254
|
+
if (docId === void 0 || view.ordinalOf(docId) !== ordinal) continue;
|
|
255
|
+
candidates.ordinals.push(ordinal);
|
|
256
|
+
candidates.docIds.push(docId);
|
|
257
|
+
}
|
|
258
|
+
return candidates;
|
|
259
|
+
}
|
|
260
|
+
function scanned(scan) {
|
|
261
|
+
return { results: scan.results, matched: scan.matched, matchedExact: true, resolvedEveryHit: true };
|
|
262
|
+
}
|
|
263
|
+
function search2(query, k, searchOptions, filter) {
|
|
264
|
+
const { metric, minSimilarity, efSearch, oversample } = searchOptions;
|
|
265
|
+
const empty = { results: [], matched: 0, matchedExact: true, resolvedEveryHit: true };
|
|
266
|
+
const liveDocuments = flags.liveDocuments();
|
|
267
|
+
if (liveDocuments <= 0) return empty;
|
|
268
|
+
if (filter !== void 0 && filter.count === 0) return empty;
|
|
269
|
+
function fetched(results, resolvedEveryHit2) {
|
|
270
|
+
if (minSimilarity === Number.NEGATIVE_INFINITY) {
|
|
271
|
+
const matched = filter === void 0 ? liveDocuments : filter.count;
|
|
272
|
+
return { results, matched, matchedExact: true, resolvedEveryHit: resolvedEveryHit2 };
|
|
273
|
+
}
|
|
274
|
+
return { results, matched: results.length, matchedExact: false, resolvedEveryHit: resolvedEveryHit2 };
|
|
275
|
+
}
|
|
276
|
+
if (k <= 0) return fetched([], true);
|
|
277
|
+
const graph = view.graph;
|
|
278
|
+
if (graph === null) {
|
|
279
|
+
const ordinals = filter === void 0 ? flags.unplacedOrdinals() : ordinalFilterValues(filter);
|
|
280
|
+
return scanned(scoreCandidates(view.store, candidatesOf(ordinals, filter), query, k, metric, minSimilarity));
|
|
281
|
+
}
|
|
282
|
+
if (filter !== void 0) {
|
|
283
|
+
const graphLiveSize = view.liveSize;
|
|
284
|
+
const selectivity = graphLiveSize > 0 ? filter.count / graphLiveSize : 1;
|
|
285
|
+
if (selectivity < view.handles.filterThreshold) {
|
|
286
|
+
const candidates = candidatesOf(ordinalFilterValues(filter), filter);
|
|
287
|
+
return scanned(scoreCandidates(view.store, candidates, query, k, metric, minSimilarity));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
let resolvedEveryHit = true;
|
|
291
|
+
const graphResults = [];
|
|
292
|
+
for (const hit of view.searchOrdinals(query, k, metric, minSimilarity, { filter, efSearch, oversample })) {
|
|
293
|
+
const docId = view.docIdOf(hit.ord);
|
|
294
|
+
if (docId === void 0) {
|
|
295
|
+
resolvedEveryHit = false;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
graphResults.push({ docId, score: hit.score });
|
|
299
|
+
}
|
|
300
|
+
const unplaced = flags.unplacedOrdinals();
|
|
301
|
+
if (unplaced.length === 0) return fetched(graphResults, resolvedEveryHit);
|
|
302
|
+
const scan = scoreCandidates(view.store, candidatesOf(unplaced, filter), query, k, metric, minSimilarity);
|
|
303
|
+
return fetched(mergeResults(graphResults, scan.results, k), resolvedEveryHit);
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
fieldName,
|
|
307
|
+
dimension: view.handles.dimension,
|
|
308
|
+
partitionsKnown: () => true,
|
|
309
|
+
assignPartitions: () => void 0,
|
|
310
|
+
searchParallel(query, k, searchOptions) {
|
|
311
|
+
if (query.length !== view.handles.dimension) {
|
|
312
|
+
throw new NarsilError(
|
|
313
|
+
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
314
|
+
`Vector dimension mismatch: expected ${view.handles.dimension}, got ${query.length}`,
|
|
315
|
+
{ expected: view.handles.dimension, received: query.length }
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
const outcome = search2(query, k, searchOptions, filterFor(searchOptions));
|
|
319
|
+
if (view.graphRetired) {
|
|
320
|
+
throw new NarsilError(
|
|
321
|
+
ErrorCodes.INDEX_NOT_FOUND,
|
|
322
|
+
`The main thread replaced the graph of vector field "${fieldName}" while this thread searched it`,
|
|
323
|
+
{ fieldName }
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
const results = outcome.results.filter((result) => holdsDocument(result.docId));
|
|
327
|
+
const holdsEveryHit = outcome.resolvedEveryHit && results.length === outcome.results.length;
|
|
328
|
+
return Promise.resolve({
|
|
329
|
+
results,
|
|
330
|
+
matched: outcome.matched,
|
|
331
|
+
matchedExact: outcome.matchedExact && holdsEveryHit
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/vector/search-pool/spawn.ts
|
|
338
|
+
function resolveWorkerEntryPoint() {
|
|
339
|
+
return resolveWorkerEntry(import.meta.url, /\/src\/vector\/search-pool\/[^/]+$/, "vector/search-worker.mjs");
|
|
340
|
+
}
|
|
341
|
+
async function spawnWorker(entryPoint) {
|
|
342
|
+
if (entryPoint === null) return null;
|
|
343
|
+
const runtime = detectRuntime();
|
|
344
|
+
if (runtime.supportsWorkerThreads) {
|
|
345
|
+
try {
|
|
346
|
+
return await spawnNodeWorker(new URL(entryPoint), workerResourceLimits());
|
|
347
|
+
} catch {
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (runtime.supportsWebWorkers) {
|
|
352
|
+
try {
|
|
353
|
+
const WorkerCtor = globalThis.Worker;
|
|
354
|
+
if (typeof WorkerCtor !== "function") return null;
|
|
355
|
+
return new WorkerCtor(entryPoint, { type: "module" });
|
|
356
|
+
} catch {
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
function listen(worker, handler) {
|
|
363
|
+
if (typeof worker.on === "function") {
|
|
364
|
+
worker.on("message", handler);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
worker.addEventListener?.("message", (event) => {
|
|
368
|
+
handler(event.data);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
function listenForFailure(worker, handler) {
|
|
372
|
+
if (typeof worker.on === "function") {
|
|
373
|
+
worker.on("error", (err) => handler(err instanceof Error ? err : new Error(String(err))));
|
|
374
|
+
worker.on("exit", () => handler(new Error("Vector search worker exited")));
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
worker.addEventListener?.("error", () => handler(new Error("Vector search worker failed")));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/vector/search-pool/index.ts
|
|
381
|
+
function searchPoolScratchSlot(index) {
|
|
382
|
+
return VECTOR_SCRATCH_SLOTS - 1 - index;
|
|
383
|
+
}
|
|
384
|
+
function graphSearchOptionsOf(options) {
|
|
385
|
+
return {
|
|
386
|
+
...options.filter !== void 0 ? { filter: options.filter } : {},
|
|
387
|
+
...options.efSearch !== void 0 ? { efSearch: options.efSearch } : {},
|
|
388
|
+
...options.oversample !== void 0 ? { oversample: options.oversample } : {}
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
async function createVectorSearchPool(requestedCount) {
|
|
392
|
+
const entryPoint = resolveWorkerEntryPoint();
|
|
393
|
+
const count = resolveWorkerCount(requestedCount);
|
|
394
|
+
const slots = [];
|
|
395
|
+
const sharedFields = /* @__PURE__ */ new Map();
|
|
396
|
+
for (let i = 0; i < count; i++) {
|
|
397
|
+
const worker = await spawnWorker(entryPoint);
|
|
398
|
+
if (worker === null) break;
|
|
399
|
+
const slot = {
|
|
400
|
+
worker,
|
|
401
|
+
scratchSlot: searchPoolScratchSlot(i),
|
|
402
|
+
pending: /* @__PURE__ */ new Map(),
|
|
403
|
+
alive: true,
|
|
404
|
+
outstanding: 0
|
|
405
|
+
};
|
|
406
|
+
listen(worker, (raw) => {
|
|
407
|
+
const message = raw;
|
|
408
|
+
const key = message.requestId;
|
|
409
|
+
if (typeof key !== "string") return;
|
|
410
|
+
const waiting = slot.pending.get(key);
|
|
411
|
+
if (!waiting) return;
|
|
412
|
+
slot.pending.delete(key);
|
|
413
|
+
clearTimeout(waiting.timer);
|
|
414
|
+
waiting.resolve(message);
|
|
415
|
+
});
|
|
416
|
+
listenForFailure(worker, (err) => {
|
|
417
|
+
slot.alive = false;
|
|
418
|
+
for (const handles of sharedFields.values()) {
|
|
419
|
+
if (handles.graph !== null) releaseLocksHeldBy(handles.graph, slot.scratchSlot);
|
|
420
|
+
}
|
|
421
|
+
for (const [, waiting] of slot.pending) {
|
|
422
|
+
clearTimeout(waiting.timer);
|
|
423
|
+
waiting.reject(err);
|
|
424
|
+
}
|
|
425
|
+
slot.pending.clear();
|
|
426
|
+
});
|
|
427
|
+
slots.push(slot);
|
|
428
|
+
}
|
|
429
|
+
if (slots.length === 0) return null;
|
|
430
|
+
let requestCounter = 0;
|
|
431
|
+
function send(slot, key, message, timeoutMs) {
|
|
432
|
+
return new Promise((resolve, reject) => {
|
|
433
|
+
if (!slot.alive) {
|
|
434
|
+
reject(new Error("Vector search worker is not running"));
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const timer = setTimeout(() => {
|
|
438
|
+
slot.pending.delete(key);
|
|
439
|
+
reject(new Error(`Vector search worker did not answer within ${timeoutMs}ms`));
|
|
440
|
+
}, timeoutMs);
|
|
441
|
+
slot.pending.set(key, { resolve, reject, timer });
|
|
442
|
+
try {
|
|
443
|
+
slot.worker.postMessage(message);
|
|
444
|
+
} catch (err) {
|
|
445
|
+
slot.pending.delete(key);
|
|
446
|
+
clearTimeout(timer);
|
|
447
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
function refusalOf(message) {
|
|
452
|
+
return message.type === "error" ? message.message : `the worker answered "${message.type}"`;
|
|
453
|
+
}
|
|
454
|
+
function pickSlot() {
|
|
455
|
+
let slot = null;
|
|
456
|
+
for (const candidate of slots) {
|
|
457
|
+
if (!candidate.alive) continue;
|
|
458
|
+
if (slot === null || candidate.outstanding < slot.outstanding) slot = candidate;
|
|
459
|
+
if (slot.outstanding === 0) break;
|
|
460
|
+
}
|
|
461
|
+
return slot;
|
|
462
|
+
}
|
|
463
|
+
async function sendBusy(slot, request, timeoutMs) {
|
|
464
|
+
slot.outstanding += 1;
|
|
465
|
+
try {
|
|
466
|
+
return await send(slot, request.requestId, request, timeoutMs);
|
|
467
|
+
} finally {
|
|
468
|
+
slot.outstanding -= 1;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
async function broadcast(build, timeoutMs) {
|
|
472
|
+
const outcomes = await Promise.allSettled(
|
|
473
|
+
slots.map((slot) => {
|
|
474
|
+
requestCounter += 1;
|
|
475
|
+
const requestId = `${requestCounter}`;
|
|
476
|
+
return send(slot, requestId, build(slot, requestId), timeoutMs);
|
|
477
|
+
})
|
|
478
|
+
);
|
|
479
|
+
return outcomes.every(
|
|
480
|
+
(outcome) => outcome.status === "fulfilled" && outcome.value.type === "ack"
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
return {
|
|
484
|
+
get workerCount() {
|
|
485
|
+
return slots.filter((slot) => slot.alive).length;
|
|
486
|
+
},
|
|
487
|
+
load(handle, snapshot) {
|
|
488
|
+
return broadcast(
|
|
489
|
+
(_slot, requestId) => ({ type: "load", requestId, handle, snapshot }),
|
|
490
|
+
VECTOR_SEARCH_LOAD_TIMEOUT_MS
|
|
491
|
+
);
|
|
492
|
+
},
|
|
493
|
+
async loadShared(handle, handles) {
|
|
494
|
+
sharedFields.set(handle, handles);
|
|
495
|
+
const loaded = await broadcast(
|
|
496
|
+
(slot, requestId) => ({ type: "loadShared", requestId, handle, scratchSlot: slot.scratchSlot, handles }),
|
|
497
|
+
VECTOR_SEARCH_LOAD_TIMEOUT_MS
|
|
498
|
+
);
|
|
499
|
+
if (!loaded) sharedFields.delete(handle);
|
|
500
|
+
return loaded;
|
|
501
|
+
},
|
|
502
|
+
async drop(handle) {
|
|
503
|
+
sharedFields.delete(handle);
|
|
504
|
+
await broadcast((_slot, requestId) => ({ type: "drop", requestId, handle }), VECTOR_SEARCH_TIMEOUT_MS);
|
|
505
|
+
},
|
|
506
|
+
async insertOrdinals(handle, ordinals) {
|
|
507
|
+
const slot = pickSlot();
|
|
508
|
+
if (slot === null) return null;
|
|
509
|
+
requestCounter += 1;
|
|
510
|
+
const request = { type: "insertOrdinals", requestId: `${requestCounter}`, handle, ordinals };
|
|
511
|
+
const message = await sendBusy(slot, request, VECTOR_INSERT_TIMEOUT_MS);
|
|
512
|
+
if (message.type !== "inserted") {
|
|
513
|
+
console.warn("A vector worker placed no vectors in the shared graph:", refusalOf(message));
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
return message.outcome;
|
|
517
|
+
},
|
|
518
|
+
async search(handle, query, k, metric, minSimilarity, options = {}) {
|
|
519
|
+
const slot = pickSlot();
|
|
520
|
+
if (slot === null) throw new Error("No vector search worker is running");
|
|
521
|
+
requestCounter += 1;
|
|
522
|
+
const request = {
|
|
523
|
+
type: "search",
|
|
524
|
+
requestId: `${requestCounter}`,
|
|
525
|
+
handle,
|
|
526
|
+
query,
|
|
527
|
+
k,
|
|
528
|
+
metric,
|
|
529
|
+
minSimilarity,
|
|
530
|
+
...graphSearchOptionsOf(options)
|
|
531
|
+
};
|
|
532
|
+
const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
|
|
533
|
+
if (message.type === "error") throw new Error(message.message);
|
|
534
|
+
if (message.type !== "result") throw new Error("Vector search worker returned an unexpected message");
|
|
535
|
+
const results = new Array(message.docIds.length);
|
|
536
|
+
for (let i = 0; i < message.docIds.length; i++) {
|
|
537
|
+
results[i] = { docId: message.docIds[i], score: message.scores[i] };
|
|
538
|
+
}
|
|
539
|
+
return results;
|
|
540
|
+
},
|
|
541
|
+
async searchOrdinals(handle, query, k, metric, minSimilarity, options = {}) {
|
|
542
|
+
const slot = pickSlot();
|
|
543
|
+
if (slot === null) throw new Error("No vector search worker is running");
|
|
544
|
+
requestCounter += 1;
|
|
545
|
+
const request = {
|
|
546
|
+
type: "searchOrdinals",
|
|
547
|
+
requestId: `${requestCounter}`,
|
|
548
|
+
handle,
|
|
549
|
+
query,
|
|
550
|
+
k,
|
|
551
|
+
metric,
|
|
552
|
+
minSimilarity,
|
|
553
|
+
...graphSearchOptionsOf(options)
|
|
554
|
+
};
|
|
555
|
+
const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
|
|
556
|
+
if (message.type === "error") throw new Error(message.message);
|
|
557
|
+
if (message.type !== "ordinalResult") throw new Error("Vector search worker returned an unexpected message");
|
|
558
|
+
return { ordinals: message.ordinals, scores: message.scores };
|
|
559
|
+
},
|
|
560
|
+
async shutdown() {
|
|
561
|
+
for (const slot of slots) {
|
|
562
|
+
slot.alive = false;
|
|
563
|
+
for (const [, waiting] of slot.pending) {
|
|
564
|
+
clearTimeout(waiting.timer);
|
|
565
|
+
waiting.reject(new Error("Vector search pool has shut down"));
|
|
566
|
+
}
|
|
567
|
+
slot.pending.clear();
|
|
568
|
+
try {
|
|
569
|
+
await slot.worker.terminate();
|
|
570
|
+
} catch {
|
|
571
|
+
slot.alive = false;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
var sharedPool = null;
|
|
578
|
+
var poolHolders = 0;
|
|
579
|
+
var workersUnavailable = false;
|
|
580
|
+
function acquireVectorSearchPool(requestedCount) {
|
|
581
|
+
poolHolders += 1;
|
|
582
|
+
if (workersUnavailable) return Promise.resolve(null);
|
|
583
|
+
if (sharedPool === null) {
|
|
584
|
+
sharedPool = createVectorSearchPool(requestedCount).then(
|
|
585
|
+
(pool) => {
|
|
586
|
+
if (pool === null) workersUnavailable = true;
|
|
587
|
+
return pool;
|
|
588
|
+
},
|
|
589
|
+
() => {
|
|
590
|
+
workersUnavailable = true;
|
|
591
|
+
return null;
|
|
592
|
+
}
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
return sharedPool;
|
|
596
|
+
}
|
|
597
|
+
async function releaseVectorSearchPool() {
|
|
598
|
+
if (poolHolders === 0) return;
|
|
599
|
+
poolHolders -= 1;
|
|
600
|
+
if (poolHolders > 0 || sharedPool === null) return;
|
|
601
|
+
const pending = sharedPool;
|
|
602
|
+
sharedPool = null;
|
|
603
|
+
const pool = await pending;
|
|
604
|
+
await pool?.shutdown();
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// src/vector/vector-index/worker-copies.ts
|
|
608
|
+
var handleCounter = 0;
|
|
609
|
+
function nextHandle(state) {
|
|
610
|
+
handleCounter += 1;
|
|
611
|
+
return `${state.indexName}/${state.fieldName}#${handleCounter}`;
|
|
612
|
+
}
|
|
613
|
+
async function dropHandle(state, handle) {
|
|
614
|
+
const host = state.workerCopies.host;
|
|
615
|
+
if (host !== void 0) {
|
|
616
|
+
await host.drop(state.indexName, state.fieldName, handle).catch(() => void 0);
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const pool = state.workerCopyPool;
|
|
620
|
+
if (pool !== null) {
|
|
621
|
+
await pool.drop(handle).catch(() => void 0);
|
|
622
|
+
await releaseVectorSearchPool().catch(() => void 0);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
function dropSharedGraph(state, graph) {
|
|
626
|
+
const shared = state.sharedHandles.get(graph);
|
|
627
|
+
if (shared === void 0) return;
|
|
628
|
+
state.sharedHandles.delete(graph);
|
|
629
|
+
if (state.workerCopyHandle === shared.handle) {
|
|
630
|
+
state.workerCopyHandle = null;
|
|
631
|
+
state.workerCopyRevision = -1;
|
|
632
|
+
state.workerCopyMode = null;
|
|
633
|
+
}
|
|
634
|
+
void dropHandle(state, shared.handle);
|
|
635
|
+
}
|
|
636
|
+
function invalidateWorkerCopies(state) {
|
|
637
|
+
state.revision += 1;
|
|
638
|
+
for (const graph of [...state.sharedHandles.keys()]) dropSharedGraph(state, graph);
|
|
639
|
+
if (state.workerCopyHandle !== null) {
|
|
640
|
+
const handle = state.workerCopyHandle;
|
|
641
|
+
state.workerCopyHandle = null;
|
|
642
|
+
state.workerCopyRevision = -1;
|
|
643
|
+
state.workerCopyMode = null;
|
|
644
|
+
void dropHandle(state, handle);
|
|
645
|
+
}
|
|
646
|
+
if (state.workerCopies.host === void 0 && state.workerCopyPool !== null && state.sharedHandles.size === 0) {
|
|
647
|
+
state.workerCopyPool = null;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
async function withdrawWorkerCopies(state) {
|
|
651
|
+
state.revision += 1;
|
|
652
|
+
const handles = /* @__PURE__ */ new Set();
|
|
653
|
+
for (const shared of state.sharedHandles.values()) handles.add(shared.handle);
|
|
654
|
+
if (state.workerCopyHandle !== null) handles.add(state.workerCopyHandle);
|
|
655
|
+
state.sharedHandles.clear();
|
|
656
|
+
state.workerCopyHandle = null;
|
|
657
|
+
state.workerCopyRevision = -1;
|
|
658
|
+
state.workerCopyMode = null;
|
|
659
|
+
await Promise.all([...handles].map((handle) => dropHandle(state, handle)));
|
|
660
|
+
if (state.workerCopies.host === void 0 && state.sharedHandles.size === 0) state.workerCopyPool = null;
|
|
661
|
+
}
|
|
662
|
+
function noteWrite(state) {
|
|
663
|
+
state.revision += 1;
|
|
664
|
+
if (state.workerCopyMode === "clone") invalidateWorkerCopies(state);
|
|
665
|
+
}
|
|
666
|
+
function refreshWorkerCopies(state) {
|
|
667
|
+
invalidateWorkerCopies(state);
|
|
668
|
+
scheduleWorkerCopyLoad(state);
|
|
669
|
+
}
|
|
670
|
+
function recordSharedField(state, record) {
|
|
671
|
+
const { graph, handle, searchable, mode, revision, layoutRevision } = record;
|
|
672
|
+
state.sharedHandles.set(graph, { handle, searchable, mode });
|
|
673
|
+
state.sharedLayoutRevision = layoutRevision;
|
|
674
|
+
if (!searchable) return;
|
|
675
|
+
state.workerCopyHandle = handle;
|
|
676
|
+
state.workerCopyRevision = revision;
|
|
677
|
+
state.workerCopyMode = mode;
|
|
678
|
+
}
|
|
679
|
+
async function loadOnHost(state, host, graph, searchable) {
|
|
680
|
+
if (!host.holdsIndex(state.indexName)) return null;
|
|
681
|
+
if (!state.store.partitionsKnown) {
|
|
682
|
+
assignStorePartitions(state, (docId) => host.resolvePartition(state.indexName, docId));
|
|
683
|
+
}
|
|
684
|
+
const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
|
|
685
|
+
const layoutRevision = state.store.handles.layoutRevision;
|
|
686
|
+
let loaded = false;
|
|
687
|
+
try {
|
|
688
|
+
loaded = await host.loadShared(state.indexName, state.fieldName, handle, fieldHandlesOf(state, graph, searchable));
|
|
689
|
+
} catch {
|
|
690
|
+
loaded = false;
|
|
691
|
+
}
|
|
692
|
+
if (!loaded || state.disposed) return null;
|
|
693
|
+
recordSharedField(state, { graph, handle, searchable, mode: "hosted", revision: state.revision, layoutRevision });
|
|
694
|
+
return handle;
|
|
695
|
+
}
|
|
696
|
+
function captureCloneCodes(state) {
|
|
697
|
+
const quantizer = state.osq;
|
|
698
|
+
const centroid = quantizer?.centroid ?? null;
|
|
699
|
+
const layout = state.store.handles.codeLayout;
|
|
700
|
+
if (quantizer === null || centroid === null || layout === null) return null;
|
|
701
|
+
const slots = state.store.slots;
|
|
702
|
+
const records = new Uint8Array(slots * layout.slotStride);
|
|
703
|
+
const present = new Uint8Array(slots);
|
|
704
|
+
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
705
|
+
const record = quantizer.recordAtOrdinal(ordinal);
|
|
706
|
+
if (record === void 0) continue;
|
|
707
|
+
records.set(record, ordinal * layout.slotStride);
|
|
708
|
+
present[ordinal] = 1;
|
|
709
|
+
}
|
|
710
|
+
return { centroid: Float32Array.from(centroid), records, present };
|
|
711
|
+
}
|
|
712
|
+
function captureCloneSnapshot(state, graph) {
|
|
713
|
+
return {
|
|
714
|
+
dimension: state.dimension,
|
|
715
|
+
quantization: state.quantizationMode,
|
|
716
|
+
metric: state.metric,
|
|
717
|
+
codes: captureCloneCodes(state),
|
|
718
|
+
store: state.store.exportSnapshot(),
|
|
719
|
+
graph: graph.exportSnapshot(),
|
|
720
|
+
tombstones: Array.from(state.tombstones)
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
async function loadOnPool(state, graph, searchable) {
|
|
724
|
+
if (graph === null) return null;
|
|
725
|
+
const revision = state.revision;
|
|
726
|
+
const pool = state.workerCopyPool ?? await acquireVectorSearchPool(state.workerCopies.count);
|
|
727
|
+
if (pool === null) {
|
|
728
|
+
await releaseVectorSearchPool();
|
|
729
|
+
return null;
|
|
730
|
+
}
|
|
731
|
+
if (state.disposed) {
|
|
732
|
+
await releaseVectorSearchPool();
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
state.workerCopyPool = pool;
|
|
736
|
+
const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
|
|
737
|
+
const layoutRevision = state.store.handles.layoutRevision;
|
|
738
|
+
let mode = "shared";
|
|
739
|
+
let loaded = false;
|
|
740
|
+
try {
|
|
741
|
+
if (sharedMemoryAvailable()) {
|
|
742
|
+
loaded = await pool.loadShared(handle, fieldHandlesOf(state, graph, searchable));
|
|
743
|
+
}
|
|
744
|
+
if (!loaded && searchable) {
|
|
745
|
+
mode = "clone";
|
|
746
|
+
loaded = await pool.load(handle, captureCloneSnapshot(state, graph));
|
|
747
|
+
}
|
|
748
|
+
} catch {
|
|
749
|
+
loaded = false;
|
|
750
|
+
}
|
|
751
|
+
if (!loaded || state.disposed || mode === "clone" && state.revision !== revision) {
|
|
752
|
+
await pool.drop(handle).catch(() => void 0);
|
|
753
|
+
return null;
|
|
754
|
+
}
|
|
755
|
+
recordSharedField(state, { graph, handle, searchable, mode, revision, layoutRevision });
|
|
756
|
+
return handle;
|
|
757
|
+
}
|
|
758
|
+
function shareGraph(state, graph, searchable) {
|
|
759
|
+
const run = state.sharing.then(async () => {
|
|
760
|
+
if (!state.workerCopies.enabled || state.disposed) return null;
|
|
761
|
+
const existing = state.sharedHandles.get(graph);
|
|
762
|
+
const layoutUnchanged = threadsHoldCurrentLayout(state);
|
|
763
|
+
if (existing !== void 0 && layoutUnchanged && (existing.searchable || !searchable)) return existing.handle;
|
|
764
|
+
const host = state.workerCopies.host;
|
|
765
|
+
return host !== void 0 ? loadOnHost(state, host, graph, searchable) : loadOnPool(state, graph, searchable);
|
|
766
|
+
});
|
|
767
|
+
state.sharing = run.then(
|
|
768
|
+
() => void 0,
|
|
769
|
+
() => void 0
|
|
770
|
+
);
|
|
771
|
+
return run;
|
|
772
|
+
}
|
|
773
|
+
function resendSharedHandles(state) {
|
|
774
|
+
const run = state.sharing.then(async () => {
|
|
775
|
+
if (!state.workerCopies.enabled || state.disposed) return;
|
|
776
|
+
const host = state.workerCopies.host;
|
|
777
|
+
for (const [graph, shared] of [...state.sharedHandles]) {
|
|
778
|
+
if (host !== void 0) await loadOnHost(state, host, graph, shared.searchable);
|
|
779
|
+
else await loadOnPool(state, graph, shared.searchable);
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
state.sharing = run.then(
|
|
783
|
+
() => void 0,
|
|
784
|
+
() => void 0
|
|
785
|
+
);
|
|
786
|
+
return run;
|
|
787
|
+
}
|
|
788
|
+
function scheduleWorkerCopyLoad(state) {
|
|
789
|
+
if (!state.workerCopies.enabled) return;
|
|
790
|
+
if (state.disposed || state.workerCopyLoading || state.building) return;
|
|
791
|
+
const graph = state.hnsw;
|
|
792
|
+
const host = state.workerCopies.host;
|
|
793
|
+
if (graph === null && host === void 0) return;
|
|
794
|
+
const existing = state.sharedHandles.get(graph);
|
|
795
|
+
if (existing?.searchable && threadsHoldCurrentLayout(state)) return;
|
|
796
|
+
if (host === void 0 && liveSize(state) < WORKER_COPY_MIN_VECTORS) return;
|
|
797
|
+
state.workerCopyLoading = true;
|
|
798
|
+
void shareGraph(state, graph, true).then(
|
|
799
|
+
() => {
|
|
800
|
+
state.workerCopyLoading = false;
|
|
801
|
+
},
|
|
802
|
+
() => {
|
|
803
|
+
state.workerCopyLoading = false;
|
|
804
|
+
}
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
async function searchViaWorkerCopies(state, query, k, metric, minSimilarity, options) {
|
|
808
|
+
const pool = state.workerCopyPool;
|
|
809
|
+
const handle = state.workerCopyHandle;
|
|
810
|
+
if (pool === null || handle === null || state.workerCopyMode === "hosted") return null;
|
|
811
|
+
if (state.workerCopyMode === "clone" && state.workerCopyRevision !== state.revision) return null;
|
|
812
|
+
try {
|
|
813
|
+
if (state.workerCopyMode === "shared") {
|
|
814
|
+
const outcome = await pool.searchOrdinals(handle, query, k, metric, minSimilarity, options);
|
|
815
|
+
const results = [];
|
|
816
|
+
for (let i = 0; i < outcome.ordinals.length; i++) {
|
|
817
|
+
const docId = state.store.docIdForOrdinal(outcome.ordinals[i]);
|
|
818
|
+
if (docId === void 0) continue;
|
|
819
|
+
results.push({ docId, score: outcome.scores[i] });
|
|
820
|
+
}
|
|
821
|
+
return results;
|
|
822
|
+
}
|
|
823
|
+
return await pool.search(handle, query, k, metric, minSimilarity, options);
|
|
824
|
+
} catch {
|
|
825
|
+
return null;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
// src/vector/vector-index/build-host.ts
|
|
830
|
+
async function dispatcherFor(state, graph) {
|
|
831
|
+
if (!state.workerCopies.enabled || state.disposed) return null;
|
|
832
|
+
const host = state.workerCopies.host;
|
|
833
|
+
if (host !== void 0) {
|
|
834
|
+
if (!host.holdsIndex(state.indexName)) return null;
|
|
835
|
+
const handle2 = await shareGraph(state, graph, graph === state.hnsw);
|
|
836
|
+
if (handle2 === null) return null;
|
|
837
|
+
return {
|
|
838
|
+
send: (ordinals) => host.insertOrdinals(state.indexName, state.fieldName, handle2, ordinals),
|
|
839
|
+
workerCount: Math.max(1, host.workerCount)
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
if (liveSize(state) < WORKER_COPY_MIN_VECTORS || !sharedMemoryAvailable()) return null;
|
|
843
|
+
const handle = await shareGraph(state, graph, graph === state.hnsw);
|
|
844
|
+
const pool = state.workerCopyPool;
|
|
845
|
+
if (handle === null || pool === null || state.sharedHandles.get(graph)?.mode !== "shared") return null;
|
|
846
|
+
return {
|
|
847
|
+
send: (ordinals) => pool.insertOrdinals(handle, ordinals),
|
|
848
|
+
workerCount: Math.max(1, pool.workerCount)
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
async function insertIntoGraph(state, graph, docIds, admit) {
|
|
852
|
+
const found = await dispatcherFor(state, graph);
|
|
853
|
+
if (found === null) return insertLocally(state, graph, docIds, admit);
|
|
854
|
+
const dispatch = found;
|
|
855
|
+
const pending = /* @__PURE__ */ new Set();
|
|
856
|
+
const limit = dispatch.workerCount * GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER;
|
|
857
|
+
const fallback = { noWorkerWarned: false, shortAnswerWarned: false };
|
|
858
|
+
let batchDocIds = [];
|
|
859
|
+
let batchOrdinals = [];
|
|
860
|
+
async function flush() {
|
|
861
|
+
if (batchOrdinals.length === 0) return;
|
|
862
|
+
const chunkDocIds = batchDocIds;
|
|
863
|
+
const chunkOrdinals = Int32Array.from(batchOrdinals);
|
|
864
|
+
batchDocIds = [];
|
|
865
|
+
batchOrdinals = [];
|
|
866
|
+
if (!threadsHoldCurrentLayout(state)) await shareGraph(state, graph, graph === state.hnsw);
|
|
867
|
+
const run = placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback).finally(() => {
|
|
868
|
+
pending.delete(run);
|
|
869
|
+
});
|
|
870
|
+
pending.add(run);
|
|
871
|
+
if (pending.size >= limit) await Promise.race(pending);
|
|
872
|
+
}
|
|
873
|
+
for (const docId of docIds) {
|
|
874
|
+
if (state.disposed) break;
|
|
875
|
+
if (!admit(docId)) continue;
|
|
876
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
877
|
+
if (ordinal === void 0) continue;
|
|
878
|
+
batchDocIds.push(docId);
|
|
879
|
+
batchOrdinals.push(ordinal);
|
|
880
|
+
if (batchOrdinals.length >= GRAPH_BUILD_DISPATCH_CHUNK) await flush();
|
|
881
|
+
}
|
|
882
|
+
await flush();
|
|
883
|
+
await Promise.all(pending);
|
|
884
|
+
return !state.disposed;
|
|
885
|
+
}
|
|
886
|
+
async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback) {
|
|
887
|
+
let outcome = null;
|
|
888
|
+
try {
|
|
889
|
+
outcome = await dispatch.send(chunkOrdinals);
|
|
890
|
+
} catch {
|
|
891
|
+
outcome = null;
|
|
892
|
+
}
|
|
893
|
+
if (state.disposed) return;
|
|
894
|
+
if (outcome === null) {
|
|
895
|
+
if (!fallback.noWorkerWarned) {
|
|
896
|
+
fallback.noWorkerWarned = true;
|
|
897
|
+
console.warn(
|
|
898
|
+
`No worker thread placed the vectors of "${state.indexName}/${state.fieldName}", so this thread is building its graph itself`
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
for (const ordinal of chunkOrdinals) graph.insertOrdinal(ordinal);
|
|
902
|
+
}
|
|
903
|
+
let placedHere = 0;
|
|
904
|
+
for (let i = 0; i < chunkDocIds.length; i++) {
|
|
905
|
+
const docId = chunkDocIds[i];
|
|
906
|
+
const ordinal = chunkOrdinals[i];
|
|
907
|
+
if (state.store.getOrdinal(docId) !== ordinal) {
|
|
908
|
+
graph.markTombstoneOrdinal(ordinal);
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
|
|
912
|
+
if (graph === state.hnsw) state.buffer.delete(docId);
|
|
913
|
+
}
|
|
914
|
+
if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
|
|
915
|
+
fallback.shortAnswerWarned = true;
|
|
916
|
+
console.warn(
|
|
917
|
+
`A worker thread answered a chunk of "${state.indexName}/${state.fieldName}" without placing ${placedHere} of its vectors, so this thread placed them itself`
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
async function insertLocally(state, graph, docIds, admit) {
|
|
922
|
+
let count = 0;
|
|
923
|
+
for (const docId of docIds) {
|
|
924
|
+
if (state.disposed) return false;
|
|
925
|
+
if (!admit(docId)) continue;
|
|
926
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
927
|
+
if (ordinal === void 0) continue;
|
|
928
|
+
graph.insertOrdinal(ordinal);
|
|
929
|
+
if (graph === state.hnsw) state.buffer.delete(docId);
|
|
930
|
+
count += 1;
|
|
931
|
+
if (count % BUILD_CHUNK_SIZE === 0) {
|
|
932
|
+
noteWrite(state);
|
|
933
|
+
await yieldToEventLoop();
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
if (count > 0) noteWrite(state);
|
|
937
|
+
return !state.disposed;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// src/vector/vector-index/disk.ts
|
|
941
|
+
function readsFromDisk(state) {
|
|
942
|
+
return state.storage === "disk";
|
|
943
|
+
}
|
|
944
|
+
function holdsVectorsOnDisk(state) {
|
|
945
|
+
return readsFromDisk(state) && state.hnsw !== null;
|
|
946
|
+
}
|
|
947
|
+
function* locationsOf(state, layout) {
|
|
948
|
+
const stride = state.dimension * 4;
|
|
949
|
+
for (let i = 0; i < layout.docIds.length; i++) {
|
|
950
|
+
if (isDead(layout.dead, i)) continue;
|
|
951
|
+
yield [layout.docIds[i], { path: layout.path, offset: layout.vectorsOffset + i * stride }];
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
async function shareFilesWithEveryThread(state, paths) {
|
|
955
|
+
const fileIndexes = /* @__PURE__ */ new Map();
|
|
956
|
+
for (const path of paths) fileIndexes.set(path, state.store.addVectorFile(path));
|
|
957
|
+
await resendSharedHandles(state);
|
|
958
|
+
return fileIndexes;
|
|
959
|
+
}
|
|
960
|
+
async function releaseLocations(state, paths, locations) {
|
|
961
|
+
const fileIndexes = await shareFilesWithEveryThread(state, paths);
|
|
962
|
+
if (state.disposed) return;
|
|
963
|
+
let count = 0;
|
|
964
|
+
for (const [docId, location] of locations) {
|
|
965
|
+
if (count > 0 && count % DISK_ADOPTION_YIELD_INTERVAL === 0) {
|
|
966
|
+
await yieldToEventLoop();
|
|
967
|
+
if (state.disposed) return;
|
|
968
|
+
}
|
|
969
|
+
count += 1;
|
|
970
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
971
|
+
const fileIndex = fileIndexes.get(location.path);
|
|
972
|
+
if (ordinal === void 0 || fileIndex === void 0) continue;
|
|
973
|
+
state.store.releaseToFile(ordinal, { fileIndex, offset: location.offset });
|
|
974
|
+
}
|
|
975
|
+
state.store.releaseColdBlocks();
|
|
976
|
+
await resendSharedHandles(state);
|
|
977
|
+
}
|
|
978
|
+
function releaseInTurn(state, release) {
|
|
979
|
+
const run = state.releaseToFilesInFlight.then(release);
|
|
980
|
+
state.releaseToFilesInFlight = run.then(
|
|
981
|
+
() => void 0,
|
|
982
|
+
() => void 0
|
|
983
|
+
);
|
|
984
|
+
return run;
|
|
985
|
+
}
|
|
986
|
+
async function adoptDiskLayout(state, layout) {
|
|
987
|
+
if (!readsFromDisk(state) || state.disposed || layout.docIds.length === 0) return;
|
|
988
|
+
if (!holdsVectorsOnDisk(state)) {
|
|
989
|
+
for (const [docId, location] of locationsOf(state, layout)) {
|
|
990
|
+
if (state.store.has(docId)) state.pendingLocations.set(docId, location);
|
|
991
|
+
}
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
await releaseInTurn(state, () => releaseLocations(state, [layout.path], locationsOf(state, layout)));
|
|
995
|
+
}
|
|
996
|
+
async function releasePendingLocations(state) {
|
|
997
|
+
if (state.pendingLocations.size === 0 || !holdsVectorsOnDisk(state) || state.disposed) return;
|
|
998
|
+
const pending = [...state.pendingLocations];
|
|
999
|
+
state.pendingLocations.clear();
|
|
1000
|
+
const paths = new Set(pending.map(([, location]) => location.path));
|
|
1001
|
+
await releaseInTurn(state, () => releaseLocations(state, paths, pending));
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
// src/vector/vector-index/build.ts
|
|
1005
|
+
async function buildGraphFromStore(state) {
|
|
1006
|
+
const graph = createHNSWIndex(state.dimension, state.store, state.hnswConfig, state.osq ?? void 0);
|
|
1007
|
+
state.freshGraph = graph;
|
|
1008
|
+
let completed = false;
|
|
1009
|
+
try {
|
|
1010
|
+
completed = await insertIntoGraph(state, graph, [...allLiveDocIds(state)], () => true);
|
|
1011
|
+
} finally {
|
|
1012
|
+
state.freshGraph = null;
|
|
1013
|
+
}
|
|
1014
|
+
if (!completed) {
|
|
1015
|
+
dropSharedGraph(state, graph);
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
const previous = state.hnsw;
|
|
1019
|
+
adoptGraph(state, graph);
|
|
1020
|
+
for (const docId of [...state.buffer]) {
|
|
1021
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
1022
|
+
if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) state.buffer.delete(docId);
|
|
1023
|
+
}
|
|
1024
|
+
if (previous !== null && previous !== graph) dropSharedGraph(state, previous);
|
|
1025
|
+
await releasePendingLocations(state);
|
|
1026
|
+
}
|
|
1027
|
+
async function promoteToGraph(state) {
|
|
1028
|
+
if (liveSize(state) === 0) return;
|
|
1029
|
+
calibrateQuantizer(state);
|
|
1030
|
+
await buildGraphFromStore(state);
|
|
1031
|
+
}
|
|
1032
|
+
async function growGraph(state, graph, bufferSnapshot) {
|
|
1033
|
+
const admit = (docId) => {
|
|
1034
|
+
if (state.hnsw !== graph) return false;
|
|
1035
|
+
if (state.tombstones.has(docId) || !state.store.has(docId)) {
|
|
1036
|
+
state.buffer.delete(docId);
|
|
1037
|
+
return false;
|
|
1038
|
+
}
|
|
1039
|
+
return true;
|
|
1040
|
+
};
|
|
1041
|
+
if (state.osq && !state.osq.isCalibrated()) calibrateQuantizer(state);
|
|
1042
|
+
await insertIntoGraph(state, graph, bufferSnapshot, admit);
|
|
1043
|
+
}
|
|
1044
|
+
function triggerBuild(state) {
|
|
1045
|
+
if (state.building) return;
|
|
1046
|
+
state.building = true;
|
|
1047
|
+
const bufferSnapshot = new Set(state.buffer);
|
|
1048
|
+
const existingGraph = state.hnsw;
|
|
1049
|
+
const buildPromise = (async () => {
|
|
1050
|
+
try {
|
|
1051
|
+
if (state.disposed) return;
|
|
1052
|
+
if (existingGraph !== null) {
|
|
1053
|
+
await growGraph(state, existingGraph, bufferSnapshot);
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
await promoteToGraph(state);
|
|
1057
|
+
} finally {
|
|
1058
|
+
state.building = false;
|
|
1059
|
+
state.pendingBuild = null;
|
|
1060
|
+
if (state.buffer.size > 0) {
|
|
1061
|
+
scheduleBuild(state);
|
|
1062
|
+
} else {
|
|
1063
|
+
scheduleWorkerCopyLoad(state);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
})();
|
|
1067
|
+
state.pendingBuild = buildPromise;
|
|
1068
|
+
}
|
|
1069
|
+
function buildDue(state) {
|
|
1070
|
+
if (state.hnsw === null) return liveSize(state) >= state.promotionThreshold;
|
|
1071
|
+
if (state.buffer.size === 0) return false;
|
|
1072
|
+
if (state.workerCopies.enabled && state.workerCopies.host !== void 0) return true;
|
|
1073
|
+
return state.buffer.size >= state.promotionThreshold;
|
|
1074
|
+
}
|
|
1075
|
+
function scheduleBuild(state) {
|
|
1076
|
+
if (state.building || state.buildScheduled || state.disposed) return;
|
|
1077
|
+
if (!buildDue(state)) return;
|
|
1078
|
+
state.buildScheduled = true;
|
|
1079
|
+
setTimeout(() => {
|
|
1080
|
+
state.buildScheduled = false;
|
|
1081
|
+
triggerBuild(state);
|
|
1082
|
+
}, 0);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
// src/vector/vector-index/checkpoint-restore.ts
|
|
1086
|
+
var CALIBRATION_NEVER_CURRENT = -1;
|
|
1087
|
+
function sameCentroid(a, b) {
|
|
1088
|
+
if (a.length !== b.length) return false;
|
|
1089
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
1090
|
+
return true;
|
|
1091
|
+
}
|
|
1092
|
+
function bufferEveryVectorOutsideTheGraph(state) {
|
|
1093
|
+
for (const docId of state.store.docIds()) {
|
|
1094
|
+
if (state.tombstones.has(docId)) continue;
|
|
1095
|
+
if (state.hnsw === null || !state.hnsw.has(docId)) state.buffer.add(docId);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
function insertNodesOfLaterGraph(state, graph, later, ordinals) {
|
|
1099
|
+
for (const part of later.parts) requireWholeNeighbourLists(part);
|
|
1100
|
+
for (let number = 0; number < ordinals.length; number++) {
|
|
1101
|
+
const ordinal = ordinals[number];
|
|
1102
|
+
if (ordinal === NO_LIVE_VECTOR_AT_POSITION || topLayerInParts(later, number) < 0) continue;
|
|
1103
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
1104
|
+
if (docId !== void 0 && !graph.has(docId)) graph.insertOrdinal(ordinal);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
function restoreGraph(state, payload, ordinalOfNumber, tombstoned) {
|
|
1108
|
+
const first = payload?.graphs[0]?.parts[0];
|
|
1109
|
+
if (payload === null || first === void 0) {
|
|
1110
|
+
releaseReplacedVectors(state);
|
|
1111
|
+
bufferEveryVectorOutsideTheGraph(state);
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
const graph = createHNSWIndex(
|
|
1115
|
+
state.dimension,
|
|
1116
|
+
state.store,
|
|
1117
|
+
{ m: first.m, efConstruction: first.efConstruction, metric: first.metric },
|
|
1118
|
+
state.osq ?? void 0
|
|
1119
|
+
);
|
|
1120
|
+
graph.deserializeNumbered(payload.graphs[0], ordinalOfNumber);
|
|
1121
|
+
for (const ordinal of tombstoned) graph.markTombstoneOrdinal(ordinal);
|
|
1122
|
+
adoptGraph(state, graph);
|
|
1123
|
+
for (const later of payload.graphs.slice(1)) insertNodesOfLaterGraph(state, graph, later, ordinalOfNumber);
|
|
1124
|
+
bufferEveryVectorOutsideTheGraph(state);
|
|
1125
|
+
if (state.buffer.size <= graph.size) return;
|
|
1126
|
+
adoptGraph(state, null);
|
|
1127
|
+
releaseReplacedVectors(state);
|
|
1128
|
+
state.buffer.clear();
|
|
1129
|
+
bufferEveryVectorOutsideTheGraph(state);
|
|
1130
|
+
}
|
|
1131
|
+
function beginCheckpointRestore(state, shape) {
|
|
1132
|
+
emptyFieldBeforeRestore(state);
|
|
1133
|
+
const cold = readsFromDisk(state) && (shape.holdsGraph || shape.liveVectors >= state.promotionThreshold);
|
|
1134
|
+
const restoresCodes = state.osq !== null && osqBitsOf(state.quantizationMode) !== null && shape.holdsGraph;
|
|
1135
|
+
const files = [];
|
|
1136
|
+
const graphNodes = shape.graph?.graphs[0] ?? null;
|
|
1137
|
+
const tombstoned = /* @__PURE__ */ new Set();
|
|
1138
|
+
let nextNumber = 0;
|
|
1139
|
+
let sharedCodes = null;
|
|
1140
|
+
let codesAgree = true;
|
|
1141
|
+
function restoreRecords(payload, ordinals) {
|
|
1142
|
+
const quantizer = state.osq;
|
|
1143
|
+
const layout = state.store.handles.codeLayout;
|
|
1144
|
+
if (!restoresCodes || quantizer === null || layout === null || !codesAgree) return;
|
|
1145
|
+
const codes = payload.codes;
|
|
1146
|
+
if (codes === null || codes.bits !== quantizer.bits) {
|
|
1147
|
+
codesAgree = false;
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
if (sharedCodes === null) {
|
|
1151
|
+
sharedCodes = codes;
|
|
1152
|
+
quantizer.restoreCentroid(Float32Array.from(codes.centroid));
|
|
1153
|
+
} else if (!sameCentroid(sharedCodes.centroid, codes.centroid)) {
|
|
1154
|
+
codesAgree = false;
|
|
1155
|
+
return;
|
|
1156
|
+
}
|
|
1157
|
+
const stride = layout.slotStride;
|
|
1158
|
+
for (let i = 0; i < ordinals.length; i++) {
|
|
1159
|
+
if (ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) continue;
|
|
1160
|
+
const record = codes.records.subarray(i * stride, (i + 1) * stride);
|
|
1161
|
+
if (tombstoned.has(ordinals[i])) quantizer.restoreRecordAtOrdinal(ordinals[i], record);
|
|
1162
|
+
else quantizer.restoreRecord(payload.docIds[i], record);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
function graphHoldsNodeFor(number) {
|
|
1166
|
+
return graphNodes !== null && topLayerInParts(graphNodes, number) >= 0;
|
|
1167
|
+
}
|
|
1168
|
+
function addFile(file) {
|
|
1169
|
+
const { payload, location } = file;
|
|
1170
|
+
if (payload.dimension !== state.dimension) {
|
|
1171
|
+
throw new NarsilError(
|
|
1172
|
+
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
1173
|
+
`Payload dimension ${payload.dimension} does not match index dimension ${state.dimension}`,
|
|
1174
|
+
{ expected: state.dimension, received: payload.dimension }
|
|
1175
|
+
);
|
|
1176
|
+
}
|
|
1177
|
+
const dimension = state.dimension;
|
|
1178
|
+
const onDisk = readsFromDisk(state) && location !== null;
|
|
1179
|
+
const vectors = bytesToVectors(payload.vectors);
|
|
1180
|
+
const fileIndex = onDisk && cold ? state.store.addVectorFile(location.path) : null;
|
|
1181
|
+
const ordinals = new Int32Array(payload.docIds.length).fill(NO_LIVE_VECTOR_AT_POSITION);
|
|
1182
|
+
const firstNumber = nextNumber;
|
|
1183
|
+
nextNumber += payload.docIds.length;
|
|
1184
|
+
for (let i = 0; i < payload.docIds.length; i++) {
|
|
1185
|
+
const dead = isDead(file.dead, i);
|
|
1186
|
+
if (dead && !graphHoldsNodeFor(firstNumber + i)) continue;
|
|
1187
|
+
const docId = payload.docIds[i];
|
|
1188
|
+
const vector = vectors.subarray(i * dimension, (i + 1) * dimension);
|
|
1189
|
+
const offset = onDisk ? location.vectorsOffset + i * dimension * 4 : 0;
|
|
1190
|
+
if (dead) {
|
|
1191
|
+
ordinals[i] = state.store.insertReplaced(docId, vector, fileIndex === null ? void 0 : { fileIndex, offset });
|
|
1192
|
+
tombstoned.add(ordinals[i]);
|
|
1193
|
+
continue;
|
|
1194
|
+
}
|
|
1195
|
+
const replaced = state.store.getOrdinal(docId);
|
|
1196
|
+
if (replaced !== void 0) state.osq?.removeOrdinal(replaced);
|
|
1197
|
+
if (fileIndex !== null) {
|
|
1198
|
+
ordinals[i] = state.store.insertCold(docId, magnitude(vector), { fileIndex, offset });
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
ordinals[i] = state.store.insert(docId, vector);
|
|
1202
|
+
if (onDisk) state.pendingLocations.set(docId, { path: location.path, offset });
|
|
1203
|
+
}
|
|
1204
|
+
restoreRecords(payload, ordinals);
|
|
1205
|
+
files.push({ id: file.id, key: file.key, ordinals, coded: payload.codes !== null });
|
|
1206
|
+
}
|
|
1207
|
+
function finish() {
|
|
1208
|
+
const codesRestored = restoresCodes && codesAgree && sharedCodes !== null;
|
|
1209
|
+
if (restoresCodes && !codesRestored) {
|
|
1210
|
+
recalibrateFromStore(state);
|
|
1211
|
+
for (const ordinal of tombstoned) state.osq?.writeCodes(ordinal);
|
|
1212
|
+
}
|
|
1213
|
+
let total = 0;
|
|
1214
|
+
for (const file of files) total += file.ordinals.length;
|
|
1215
|
+
const ordinalOfNumber = new Int32Array(total);
|
|
1216
|
+
let number = 0;
|
|
1217
|
+
for (const file of files) {
|
|
1218
|
+
for (let i = 0; i < file.ordinals.length; i++) {
|
|
1219
|
+
const ordinal = file.ordinals[i];
|
|
1220
|
+
if (!state.store.holdsOrdinal(ordinal) && !tombstoned.has(ordinal)) {
|
|
1221
|
+
file.ordinals[i] = NO_LIVE_VECTOR_AT_POSITION;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
ordinalOfNumber.set(file.ordinals, number);
|
|
1225
|
+
number += file.ordinals.length;
|
|
1226
|
+
}
|
|
1227
|
+
restoreGraph(state, shape.graph, ordinalOfNumber, tombstoned);
|
|
1228
|
+
const current = signatureOf(state).calibration;
|
|
1229
|
+
const codedUnder = codesRestored && current !== null ? current : CALIBRATION_NEVER_CURRENT;
|
|
1230
|
+
const saved = files.map((file) => ({
|
|
1231
|
+
id: file.id,
|
|
1232
|
+
key: file.key,
|
|
1233
|
+
ordinals: file.ordinals,
|
|
1234
|
+
calibration: file.coded ? codedUnder : null
|
|
1235
|
+
}));
|
|
1236
|
+
state.savedFiles = saved;
|
|
1237
|
+
state.savedSignature = signatureOf(state);
|
|
1238
|
+
}
|
|
1239
|
+
return { addFile, finish };
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
// src/vector/vector-index/flagged-documents.ts
|
|
1243
|
+
var FlaggedDocuments = class extends Set {
|
|
1244
|
+
constructor(store, layout) {
|
|
1245
|
+
super();
|
|
1246
|
+
this.store = store;
|
|
1247
|
+
this.layout = layout;
|
|
1248
|
+
}
|
|
1249
|
+
ordinals = /* @__PURE__ */ new Map();
|
|
1250
|
+
handles = null;
|
|
1251
|
+
flags = new Uint8Array(0);
|
|
1252
|
+
chunks = null;
|
|
1253
|
+
add(docId) {
|
|
1254
|
+
super.add(docId);
|
|
1255
|
+
const handles = this.openHandles();
|
|
1256
|
+
if (handles === null) return this;
|
|
1257
|
+
this.flagCurrentOrdinal(docId);
|
|
1258
|
+
Atomics.store(handles.header, this.layout.countWord, this.size);
|
|
1259
|
+
return this;
|
|
1260
|
+
}
|
|
1261
|
+
delete(docId) {
|
|
1262
|
+
const held = super.delete(docId);
|
|
1263
|
+
const handles = this.openHandles();
|
|
1264
|
+
if (handles === null) return held;
|
|
1265
|
+
const ordinal = this.ordinals.get(docId);
|
|
1266
|
+
if (ordinal !== void 0) {
|
|
1267
|
+
this.ordinals.delete(docId);
|
|
1268
|
+
this.unflag(ordinal);
|
|
1269
|
+
}
|
|
1270
|
+
Atomics.store(handles.header, this.layout.countWord, this.size);
|
|
1271
|
+
return held;
|
|
1272
|
+
}
|
|
1273
|
+
clear() {
|
|
1274
|
+
super.clear();
|
|
1275
|
+
const handles = this.openHandles();
|
|
1276
|
+
if (handles !== null) {
|
|
1277
|
+
for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
|
|
1278
|
+
Atomics.store(handles.header, this.layout.countWord, 0);
|
|
1279
|
+
}
|
|
1280
|
+
this.ordinals.clear();
|
|
1281
|
+
}
|
|
1282
|
+
flagCurrentOrdinal(docId) {
|
|
1283
|
+
const ordinal = this.store.getOrdinal(docId);
|
|
1284
|
+
const previous = this.ordinals.get(docId);
|
|
1285
|
+
if (ordinal === void 0 || ordinal === previous) return;
|
|
1286
|
+
if (this.chunks !== null) Atomics.add(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
|
|
1287
|
+
Atomics.store(this.flags, ordinal, 1);
|
|
1288
|
+
this.ordinals.set(docId, ordinal);
|
|
1289
|
+
if (previous !== void 0) this.unflag(previous);
|
|
1290
|
+
}
|
|
1291
|
+
unflag(ordinal) {
|
|
1292
|
+
Atomics.store(this.flags, ordinal, 0);
|
|
1293
|
+
if (this.chunks !== null) Atomics.sub(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
|
|
1294
|
+
}
|
|
1295
|
+
openHandles() {
|
|
1296
|
+
if (this.store.dimension === 0) return null;
|
|
1297
|
+
const handles = this.store.handles;
|
|
1298
|
+
if (handles === this.handles) return handles;
|
|
1299
|
+
this.handles = handles;
|
|
1300
|
+
this.flags = new Uint8Array(this.layout.flagsOf(handles));
|
|
1301
|
+
const chunksOf = this.layout.chunksOf;
|
|
1302
|
+
this.chunks = chunksOf === null ? null : new Int32Array(chunksOf(handles));
|
|
1303
|
+
this.ordinals.clear();
|
|
1304
|
+
for (const docId of this) this.flagCurrentOrdinal(docId);
|
|
1305
|
+
return handles;
|
|
1306
|
+
}
|
|
1307
|
+
};
|
|
1308
|
+
function createUnplacedVectors(store) {
|
|
1309
|
+
return new FlaggedDocuments(store, {
|
|
1310
|
+
flagsOf: (handles) => handles.unplaced,
|
|
1311
|
+
chunksOf: (handles) => handles.unplacedChunks,
|
|
1312
|
+
countWord: STORE_UNPLACED_COUNT
|
|
1313
|
+
});
|
|
1314
|
+
}
|
|
1315
|
+
function createRemovedDocuments(store) {
|
|
1316
|
+
return new FlaggedDocuments(store, {
|
|
1317
|
+
flagsOf: (handles) => handles.removed,
|
|
1318
|
+
chunksOf: null,
|
|
1319
|
+
countWord: STORE_REMOVED_COUNT
|
|
1320
|
+
});
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
// src/vector/vector-index/maintenance.ts
|
|
1324
|
+
function compact(state) {
|
|
1325
|
+
if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
|
|
1326
|
+
if (state.hnsw) {
|
|
1327
|
+
state.compactedNodeCount += state.hnsw.tombstoneCount;
|
|
1328
|
+
state.hnsw.compactTombstones();
|
|
1329
|
+
}
|
|
1330
|
+
state.freshGraph?.compactTombstones();
|
|
1331
|
+
for (const docId of state.tombstones) {
|
|
1332
|
+
state.store.remove(docId);
|
|
1333
|
+
state.buffer.delete(docId);
|
|
1334
|
+
if (state.osq) {
|
|
1335
|
+
state.osq.remove(docId);
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
releaseReplacedVectors(state);
|
|
1339
|
+
state.tombstones.clear();
|
|
1340
|
+
}
|
|
1341
|
+
function recalibrateWhileNoThreadSearches(state, graph) {
|
|
1342
|
+
if (graph === null) {
|
|
1343
|
+
recalibrateFromStore(state);
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
graph.exclusively(() => recalibrateFromStore(state));
|
|
1347
|
+
}
|
|
1348
|
+
async function insertMissing(state, graph) {
|
|
1349
|
+
const missingOrReplaced = (docId) => !graph.has(docId) || state.buffer.has(docId);
|
|
1350
|
+
const heldWhenTheCallStarted = [...allLiveDocIds(state)];
|
|
1351
|
+
const stillLiveAndUnplaced = (docId) => !state.tombstones.has(docId) && missingOrReplaced(docId);
|
|
1352
|
+
await insertIntoGraph(state, graph, heldWhenTheCallStarted, stillLiveAndUnplaced);
|
|
1353
|
+
}
|
|
1354
|
+
async function foldIntoGraph(state) {
|
|
1355
|
+
const rebuildNeeded = graphNeedsRebuild(state);
|
|
1356
|
+
compact(state);
|
|
1357
|
+
if (liveSize(state) === 0) {
|
|
1358
|
+
const previous = state.hnsw;
|
|
1359
|
+
adoptGraph(state, null);
|
|
1360
|
+
if (previous !== null) dropSharedGraph(state, previous);
|
|
1361
|
+
state.buffer.clear();
|
|
1362
|
+
if (state.osq) {
|
|
1363
|
+
state.osq.clear();
|
|
1364
|
+
}
|
|
1365
|
+
return;
|
|
1366
|
+
}
|
|
1367
|
+
const graph = state.hnsw;
|
|
1368
|
+
if (graph === null || rebuildNeeded) {
|
|
1369
|
+
if (state.osq?.isCalibrated()) recalibrateWhileNoThreadSearches(state, graph);
|
|
1370
|
+
else calibrateQuantizer(state);
|
|
1371
|
+
await buildGraphFromStore(state);
|
|
1372
|
+
} else {
|
|
1373
|
+
await insertMissing(state, graph);
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
async function buildExclusively(state, work) {
|
|
1377
|
+
while (state.pendingBuild) {
|
|
1378
|
+
await state.pendingBuild;
|
|
1379
|
+
}
|
|
1380
|
+
if (state.disposed) return;
|
|
1381
|
+
state.building = true;
|
|
1382
|
+
const run = work();
|
|
1383
|
+
state.pendingBuild = run;
|
|
1384
|
+
try {
|
|
1385
|
+
await run;
|
|
1386
|
+
} finally {
|
|
1387
|
+
state.building = false;
|
|
1388
|
+
state.pendingBuild = null;
|
|
1389
|
+
if (state.buffer.size > 0) {
|
|
1390
|
+
scheduleBuild(state);
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
function optimize(state) {
|
|
1395
|
+
return buildExclusively(state, () => foldIntoGraph(state));
|
|
1396
|
+
}
|
|
1397
|
+
async function fillGraph(state) {
|
|
1398
|
+
const graph = state.hnsw;
|
|
1399
|
+
if (graph === null) {
|
|
1400
|
+
if (liveSize(state) >= state.promotionThreshold) await promoteToGraph(state);
|
|
1401
|
+
return;
|
|
1402
|
+
}
|
|
1403
|
+
if (state.osq && !state.osq.isCalibrated()) calibrateQuantizer(state);
|
|
1404
|
+
if (graphNeedsRebuild(state)) {
|
|
1405
|
+
await buildGraphFromStore(state);
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
await insertMissing(state, graph);
|
|
1409
|
+
}
|
|
1410
|
+
function completeGraph(state) {
|
|
1411
|
+
return buildExclusively(state, () => fillGraph(state));
|
|
1412
|
+
}
|
|
1413
|
+
function maintenanceStatus(state) {
|
|
1414
|
+
const storeSize = state.store.size;
|
|
1415
|
+
const graphCount = state.hnsw ? 1 : 0;
|
|
1416
|
+
const graphNodes = state.hnsw === null ? storeSize : state.hnsw.size + state.hnsw.tombstoneCount;
|
|
1417
|
+
const tombstoned = state.tombstones.size + state.store.replacedVectorCount;
|
|
1418
|
+
const tombstoneRatio = graphNodes > 0 ? tombstoned / graphNodes : 0;
|
|
1419
|
+
const estimatedCompactMs = Math.round(tombstoned * ESTIMATED_MS_PER_TOMBSTONE * state.dimensionScale);
|
|
1420
|
+
const optimizeVectorCount = state.hnsw === null || graphNeedsRebuild(state) ? storeSize : state.buffer.size;
|
|
1421
|
+
const estimatedOptimizeMs = Math.round(optimizeVectorCount * ESTIMATED_MS_PER_VECTOR_REBUILD * state.dimensionScale);
|
|
1422
|
+
return {
|
|
1423
|
+
tombstoneRatio,
|
|
1424
|
+
graphCount,
|
|
1425
|
+
bufferSize: state.buffer.size,
|
|
1426
|
+
building: state.building || state.buildScheduled,
|
|
1427
|
+
estimatedCompactMs,
|
|
1428
|
+
estimatedOptimizeMs
|
|
1429
|
+
};
|
|
1430
|
+
}
|
|
1431
|
+
function flatScanScratchBytes(state) {
|
|
1432
|
+
return state.store.dimension === 0 ? 0 : nativeStoreScratchBytes(state.store.handles);
|
|
1433
|
+
}
|
|
1434
|
+
function estimateMemoryBytes(state) {
|
|
1435
|
+
if (state.hnsw === null) return state.store.memoryBytes() + flatScanScratchBytes(state);
|
|
1436
|
+
return state.store.memoryBytes() + state.hnsw.graphBytes + state.hnsw.searchScratchBytes;
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
// src/vector/vector-index/search.ts
|
|
1440
|
+
function* bufferCandidates(state, filter) {
|
|
1441
|
+
for (const docId of state.buffer) {
|
|
1442
|
+
if (state.tombstones.has(docId)) continue;
|
|
1443
|
+
if (filter) {
|
|
1444
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
1445
|
+
if (ordinal === void 0 || !ordinalFilterHas(filter, ordinal)) continue;
|
|
1446
|
+
}
|
|
1447
|
+
yield docId;
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
function* filteredDocIds(state, filter) {
|
|
1451
|
+
for (const ordinal of ordinalFilterValues(filter)) {
|
|
1452
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
1453
|
+
if (docId !== void 0) yield docId;
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
function admittedTotal(state, filter) {
|
|
1457
|
+
return filter === void 0 ? liveSize(state) : filter.count;
|
|
1458
|
+
}
|
|
1459
|
+
function fetchedOutcome(state, results, minSimilarity, filter) {
|
|
1460
|
+
if (minSimilarity === Number.NEGATIVE_INFINITY) {
|
|
1461
|
+
return { results, matched: admittedTotal(state, filter), matchedExact: true };
|
|
1462
|
+
}
|
|
1463
|
+
return { results, matched: results.length, matchedExact: false };
|
|
1464
|
+
}
|
|
1465
|
+
function scannedOutcome(scan) {
|
|
1466
|
+
return { results: scan.results, matched: scan.matched, matchedExact: true };
|
|
1467
|
+
}
|
|
1468
|
+
function liveCandidates(state, candidates) {
|
|
1469
|
+
const docIds = [];
|
|
1470
|
+
const ordinals = [];
|
|
1471
|
+
for (const docId of candidates) {
|
|
1472
|
+
if (state.tombstones.has(docId)) continue;
|
|
1473
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
1474
|
+
if (ordinal === void 0) continue;
|
|
1475
|
+
docIds.push(docId);
|
|
1476
|
+
ordinals.push(ordinal);
|
|
1477
|
+
}
|
|
1478
|
+
return { docIds, ordinals };
|
|
1479
|
+
}
|
|
1480
|
+
function scanThroughTheCore(state, query, k, metric, minSimilarity, scanned) {
|
|
1481
|
+
const distances = nativeScoresOf(state.store.handles, query, metric, scanned.ordinals);
|
|
1482
|
+
if (distances === null) return null;
|
|
1483
|
+
const heap = createBoundedMaxHeap(highScoreFirst, k);
|
|
1484
|
+
let matched = 0;
|
|
1485
|
+
for (let i = 0; i < scanned.ordinals.length; i++) {
|
|
1486
|
+
if (distances[i] === Number.POSITIVE_INFINITY) continue;
|
|
1487
|
+
const score = toScore(distances[i], metric);
|
|
1488
|
+
if (score < minSimilarity) continue;
|
|
1489
|
+
matched++;
|
|
1490
|
+
heap.push({ docId: scanned.docIds[i], score });
|
|
1491
|
+
}
|
|
1492
|
+
return { results: heap.toSortedArray().reverse(), matched };
|
|
1493
|
+
}
|
|
1494
|
+
function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
|
|
1495
|
+
const scanned = liveCandidates(state, candidates);
|
|
1496
|
+
const fromTheCore = scanThroughTheCore(state, query, k, metric, minSimilarity, scanned);
|
|
1497
|
+
if (fromTheCore !== null) return fromTheCore;
|
|
1498
|
+
noteFallback("score");
|
|
1499
|
+
const arenaQuery = state.store.prepareQueryArena(query);
|
|
1500
|
+
const queryMag = arenaQuery ? arenaQuery.magnitude : magnitude(query);
|
|
1501
|
+
const heap = createBoundedMaxHeap(highScoreFirst, k);
|
|
1502
|
+
let matched = 0;
|
|
1503
|
+
for (let i = 0; i < scanned.docIds.length; i++) {
|
|
1504
|
+
const docId = scanned.docIds[i];
|
|
1505
|
+
let score;
|
|
1506
|
+
if (arenaQuery) {
|
|
1507
|
+
const distance = state.store.distanceFromArena(arenaQuery, scanned.ordinals[i], metric);
|
|
1508
|
+
if (distance === Number.POSITIVE_INFINITY) continue;
|
|
1509
|
+
score = toScore(distance, metric);
|
|
1510
|
+
} else {
|
|
1511
|
+
const entry = state.store.get(docId);
|
|
1512
|
+
if (!entry) continue;
|
|
1513
|
+
switch (metric) {
|
|
1514
|
+
case "cosine":
|
|
1515
|
+
score = cosineSimilarityWithMagnitudes(query, entry.vector, queryMag, entry.magnitude);
|
|
1516
|
+
break;
|
|
1517
|
+
case "dotProduct":
|
|
1518
|
+
score = dotProduct(query, entry.vector);
|
|
1519
|
+
break;
|
|
1520
|
+
case "euclidean": {
|
|
1521
|
+
const dist = euclideanDistance(query, entry.vector);
|
|
1522
|
+
score = 1 / (1 + dist);
|
|
1523
|
+
break;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
if (score < minSimilarity) continue;
|
|
1528
|
+
matched++;
|
|
1529
|
+
heap.push({ docId, score });
|
|
1530
|
+
}
|
|
1531
|
+
return { results: heap.toSortedArray().reverse(), matched };
|
|
1532
|
+
}
|
|
1533
|
+
function search(state, query, k, options) {
|
|
1534
|
+
return searchWithFilter(state, query, k, options, filterForOptions(state, options));
|
|
1535
|
+
}
|
|
1536
|
+
function searchWithFilter(state, query, k, options, filter) {
|
|
1537
|
+
if (query.length !== state.dimension) {
|
|
1538
|
+
throw new NarsilError(
|
|
1539
|
+
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
1540
|
+
`Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
|
|
1541
|
+
{ expected: state.dimension, received: query.length }
|
|
1542
|
+
);
|
|
1543
|
+
}
|
|
1544
|
+
const currentLiveSize = liveSize(state);
|
|
1545
|
+
const { metric, minSimilarity, efSearch, oversample } = options;
|
|
1546
|
+
if (currentLiveSize === 0) return { results: [], matched: 0, matchedExact: true };
|
|
1547
|
+
if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
|
|
1548
|
+
if (k <= 0) return fetchedOutcome(state, [], minSimilarity, filter);
|
|
1549
|
+
if (state.buffer.size > 0 && !state.building && !state.buildScheduled) {
|
|
1550
|
+
scheduleBuild(state);
|
|
1551
|
+
}
|
|
1552
|
+
if (!state.hnsw) {
|
|
1553
|
+
const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
|
|
1554
|
+
return scannedOutcome(bruteForceSearch(state, query, k, metric, minSimilarity, candidates));
|
|
1555
|
+
}
|
|
1556
|
+
if (filter) {
|
|
1557
|
+
const hnswLiveSize = state.hnsw.size;
|
|
1558
|
+
const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
|
|
1559
|
+
if (selectivity < state.filterThreshold) {
|
|
1560
|
+
const scan = bruteForceSearch(state, query, k, metric, minSimilarity, filteredDocIds(state, filter));
|
|
1561
|
+
return scannedOutcome(scan);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
const hnswResults = state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample }).map((r) => ({ docId: r.docId, score: r.score }));
|
|
1565
|
+
return fetchedOutcome(
|
|
1566
|
+
state,
|
|
1567
|
+
withBufferedVectors(state, query, k, options, filter, hnswResults),
|
|
1568
|
+
minSimilarity,
|
|
1569
|
+
filter
|
|
1570
|
+
);
|
|
1571
|
+
}
|
|
1572
|
+
function withBufferedVectors(state, query, k, options, filter, graphResults) {
|
|
1573
|
+
if (state.buffer.size === 0) return graphResults;
|
|
1574
|
+
const candidates = bufferCandidates(state, filter);
|
|
1575
|
+
const buffered = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates);
|
|
1576
|
+
return mergeResults(graphResults, buffered.results, k);
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
// src/vector/vector-index/index.ts
|
|
1580
|
+
function defaultQuantizationFor(dimension) {
|
|
1581
|
+
if (dimension >= OSQ4_MIN_DIMENSION) return "osq4";
|
|
1582
|
+
return "osq8";
|
|
1583
|
+
}
|
|
1584
|
+
function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_WORKER_COPIES_ALLOWED, indexName = "", storage = "memory") {
|
|
1585
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
1586
|
+
throw new NarsilError(
|
|
1587
|
+
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
1588
|
+
`Vector dimension must be a positive integer, got ${dimension}`,
|
|
1589
|
+
{ dimension }
|
|
1590
|
+
);
|
|
1591
|
+
}
|
|
1592
|
+
const promotionThreshold = config?.threshold ?? DEFAULT_PROMOTION_THRESHOLD;
|
|
1593
|
+
const filterThreshold = Math.max(0, Math.min(1, config?.filterThreshold ?? DEFAULT_FILTER_THRESHOLD));
|
|
1594
|
+
const quantizationMode = config?.quantization ?? defaultQuantizationFor(dimension);
|
|
1595
|
+
const codeBits = osqBitsOf(quantizationMode);
|
|
1596
|
+
const metric = config?.hnswConfig?.metric ?? "cosine";
|
|
1597
|
+
const rawHnswM = config?.hnswConfig?.m;
|
|
1598
|
+
const hnswConfig = config?.hnswConfig ? { ...config.hnswConfig, m: rawHnswM !== void 0 ? Math.max(rawHnswM, 2) : void 0 } : void 0;
|
|
1599
|
+
const dimensionScale = dimension / 256;
|
|
1600
|
+
const store = createVectorStore({
|
|
1601
|
+
dimension,
|
|
1602
|
+
codeBits,
|
|
1603
|
+
...storage === "disk" ? { blockBytes: DISK_STORAGE_BLOCK_BYTES } : {}
|
|
1604
|
+
});
|
|
1605
|
+
const state = {
|
|
1606
|
+
indexName,
|
|
1607
|
+
fieldName,
|
|
1608
|
+
dimension,
|
|
1609
|
+
dimensionScale,
|
|
1610
|
+
promotionThreshold,
|
|
1611
|
+
filterThreshold,
|
|
1612
|
+
quantizationMode,
|
|
1613
|
+
storage,
|
|
1614
|
+
metric,
|
|
1615
|
+
hnswConfig,
|
|
1616
|
+
workerCopies,
|
|
1617
|
+
store,
|
|
1618
|
+
tombstones: createRemovedDocuments(store),
|
|
1619
|
+
buffer: createUnplacedVectors(store),
|
|
1620
|
+
pendingLocations: /* @__PURE__ */ new Map(),
|
|
1621
|
+
savedFiles: [],
|
|
1622
|
+
savedSignature: null,
|
|
1623
|
+
osq: codeBits === null ? null : createOsqQuantizer(dimension, codeBits, metric, store),
|
|
1624
|
+
hnsw: null,
|
|
1625
|
+
freshGraph: null,
|
|
1626
|
+
compactedNodeCount: 0,
|
|
1627
|
+
building: false,
|
|
1628
|
+
buildScheduled: false,
|
|
1629
|
+
pendingBuild: null,
|
|
1630
|
+
disposed: false,
|
|
1631
|
+
revision: 0,
|
|
1632
|
+
workerCopyPool: null,
|
|
1633
|
+
workerCopyHandle: null,
|
|
1634
|
+
workerCopyRevision: -1,
|
|
1635
|
+
workerCopyMode: null,
|
|
1636
|
+
workerCopyLoading: false,
|
|
1637
|
+
sharedHandles: /* @__PURE__ */ new Map(),
|
|
1638
|
+
sharedLayoutRevision: 0,
|
|
1639
|
+
sharing: Promise.resolve(),
|
|
1640
|
+
releaseToFilesInFlight: Promise.resolve()
|
|
1641
|
+
};
|
|
1642
|
+
function validateDimension(vector) {
|
|
1643
|
+
if (vector.length !== state.dimension) {
|
|
1644
|
+
throw new NarsilError(
|
|
1645
|
+
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
1646
|
+
`Vector dimension mismatch: expected ${state.dimension}, got ${vector.length}`,
|
|
1647
|
+
{ expected: state.dimension, received: vector.length }
|
|
1648
|
+
);
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
function heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal) {
|
|
1652
|
+
try {
|
|
1653
|
+
return state.store.entryForOrdinal(ordinal)?.vector;
|
|
1654
|
+
} catch {
|
|
1655
|
+
return void 0;
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
function holdsThisVectorAlready(docId, ordinal, vector) {
|
|
1659
|
+
if (state.tombstones.has(docId)) return false;
|
|
1660
|
+
const held = heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal);
|
|
1661
|
+
if (held === void 0) return false;
|
|
1662
|
+
for (let i = 0; i < vector.length; i++) if (held[i] !== vector[i]) return false;
|
|
1663
|
+
return true;
|
|
1664
|
+
}
|
|
1665
|
+
function insert(docId, vector, partitionId) {
|
|
1666
|
+
validateDimension(vector);
|
|
1667
|
+
const previous = state.store.getOrdinal(docId);
|
|
1668
|
+
if (previous !== void 0 && holdsThisVectorAlready(docId, previous, vector)) {
|
|
1669
|
+
if (partitionId !== void 0 && state.store.partitionOfOrdinal(previous) !== partitionId) {
|
|
1670
|
+
noteWrite(state);
|
|
1671
|
+
state.store.setPartition(docId, partitionId);
|
|
1672
|
+
}
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
noteWrite(state);
|
|
1676
|
+
state.tombstones.delete(docId);
|
|
1677
|
+
const graphHoldsPrevious = previous !== void 0 && ((state.hnsw?.holdsNode(previous) ?? false) || (state.freshGraph?.holdsNode(previous) ?? false));
|
|
1678
|
+
state.store.insert(docId, vector, partitionId, graphHoldsPrevious);
|
|
1679
|
+
if (previous !== void 0) {
|
|
1680
|
+
state.hnsw?.markTombstoneOrdinal(previous);
|
|
1681
|
+
state.freshGraph?.markTombstoneOrdinal(previous);
|
|
1682
|
+
if (!graphHoldsPrevious) state.osq?.removeOrdinal(previous);
|
|
1683
|
+
}
|
|
1684
|
+
state.buffer.add(docId);
|
|
1685
|
+
state.pendingLocations.delete(docId);
|
|
1686
|
+
if (!threadsHoldCurrentLayout(state)) scheduleWorkerCopyLoad(state);
|
|
1687
|
+
}
|
|
1688
|
+
function remove(docId) {
|
|
1689
|
+
if (!state.store.has(docId)) return;
|
|
1690
|
+
noteWrite(state);
|
|
1691
|
+
state.tombstones.add(docId);
|
|
1692
|
+
state.buffer.delete(docId);
|
|
1693
|
+
state.pendingLocations.delete(docId);
|
|
1694
|
+
if (state.hnsw) {
|
|
1695
|
+
state.hnsw.markTombstone(docId);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
function getVector(docId) {
|
|
1699
|
+
if (state.tombstones.has(docId)) return null;
|
|
1700
|
+
const entry = state.store.get(docId);
|
|
1701
|
+
if (!entry) return null;
|
|
1702
|
+
return new Float32Array(entry.vector);
|
|
1703
|
+
}
|
|
1704
|
+
function has(docId) {
|
|
1705
|
+
return state.store.has(docId) && !state.tombstones.has(docId);
|
|
1706
|
+
}
|
|
1707
|
+
async function awaitPendingBuild() {
|
|
1708
|
+
while (state.pendingBuild) {
|
|
1709
|
+
await state.pendingBuild;
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
function releaseHeldMemory() {
|
|
1713
|
+
state.hnsw = null;
|
|
1714
|
+
state.freshGraph = null;
|
|
1715
|
+
state.osq = null;
|
|
1716
|
+
state.tombstones.clear();
|
|
1717
|
+
state.buffer.clear();
|
|
1718
|
+
state.pendingLocations.clear();
|
|
1719
|
+
state.savedFiles = [];
|
|
1720
|
+
state.savedSignature = null;
|
|
1721
|
+
state.store.release();
|
|
1722
|
+
}
|
|
1723
|
+
function dispose() {
|
|
1724
|
+
state.disposed = true;
|
|
1725
|
+
invalidateWorkerCopies(state);
|
|
1726
|
+
const pending = state.pendingBuild;
|
|
1727
|
+
if (pending !== null) {
|
|
1728
|
+
void pending.then(releaseHeldMemory, releaseHeldMemory);
|
|
1729
|
+
return;
|
|
1730
|
+
}
|
|
1731
|
+
releaseHeldMemory();
|
|
1732
|
+
}
|
|
1733
|
+
async function searchParallel(query, k, options) {
|
|
1734
|
+
const confined = options.filterDocIds !== void 0 || options.filterPartitions !== void 0;
|
|
1735
|
+
let filter = filterForOptions(state, options);
|
|
1736
|
+
if (filter !== void 0) {
|
|
1737
|
+
if (state.hnsw) {
|
|
1738
|
+
const hnswLiveSize = state.hnsw.size;
|
|
1739
|
+
const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
|
|
1740
|
+
if (selectivity < state.filterThreshold) {
|
|
1741
|
+
return searchWithFilter(state, query, k, options, filter);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
const filterRevision = state.revision;
|
|
1746
|
+
scheduleWorkerCopyLoad(state);
|
|
1747
|
+
const viaWorkerCopy = await searchViaWorkerCopies(state, query, k, options.metric, options.minSimilarity, {
|
|
1748
|
+
filter,
|
|
1749
|
+
efSearch: options.efSearch,
|
|
1750
|
+
oversample: options.oversample
|
|
1751
|
+
});
|
|
1752
|
+
if (confined && state.revision !== filterRevision) {
|
|
1753
|
+
filter = filterForOptions(state, options);
|
|
1754
|
+
}
|
|
1755
|
+
if (viaWorkerCopy !== null) {
|
|
1756
|
+
const results = withBufferedVectors(state, query, k, options, filter, viaWorkerCopy);
|
|
1757
|
+
return fetchedOutcome(state, results, options.minSimilarity, filter);
|
|
1758
|
+
}
|
|
1759
|
+
return searchWithFilter(state, query, k, options, filter);
|
|
1760
|
+
}
|
|
1761
|
+
return {
|
|
1762
|
+
get size() {
|
|
1763
|
+
return liveSize(state);
|
|
1764
|
+
},
|
|
1765
|
+
get dimension() {
|
|
1766
|
+
return state.dimension;
|
|
1767
|
+
},
|
|
1768
|
+
get fieldName() {
|
|
1769
|
+
return state.fieldName;
|
|
1770
|
+
},
|
|
1771
|
+
get quantization() {
|
|
1772
|
+
return state.quantizationMode;
|
|
1773
|
+
},
|
|
1774
|
+
get storage() {
|
|
1775
|
+
return state.storage;
|
|
1776
|
+
},
|
|
1777
|
+
insert,
|
|
1778
|
+
remove,
|
|
1779
|
+
partitionsKnown: () => state.store.partitionsKnown,
|
|
1780
|
+
assignPartitions: (resolve) => assignStorePartitions(state, resolve),
|
|
1781
|
+
scheduleBuild: () => scheduleBuild(state),
|
|
1782
|
+
awaitPendingBuild,
|
|
1783
|
+
completeGraph: () => completeGraph(state),
|
|
1784
|
+
dispose,
|
|
1785
|
+
search: (query, k, options) => search(state, query, k, options),
|
|
1786
|
+
searchParallel,
|
|
1787
|
+
refreshWorkerCopies: () => refreshWorkerCopies(state),
|
|
1788
|
+
getVector,
|
|
1789
|
+
has,
|
|
1790
|
+
compact: () => {
|
|
1791
|
+
noteWrite(state);
|
|
1792
|
+
compact(state);
|
|
1793
|
+
},
|
|
1794
|
+
optimize: async () => {
|
|
1795
|
+
noteWrite(state);
|
|
1796
|
+
await optimize(state);
|
|
1797
|
+
},
|
|
1798
|
+
maintenanceStatus: () => maintenanceStatus(state),
|
|
1799
|
+
estimateMemoryBytes: () => estimateMemoryBytes(state),
|
|
1800
|
+
deserialize: (parts) => {
|
|
1801
|
+
invalidateWorkerCopies(state);
|
|
1802
|
+
deserialize(state, parts);
|
|
1803
|
+
},
|
|
1804
|
+
planCheckpoint: (listedKeys, rewritesGraph) => planCheckpoint(state, listedKeys, rewritesGraph),
|
|
1805
|
+
recordCheckpoint: (plan, written) => recordCheckpoint(state, plan, written),
|
|
1806
|
+
restoreCheckpoint: (shape) => {
|
|
1807
|
+
invalidateWorkerCopies(state);
|
|
1808
|
+
return beginCheckpointRestore(state, shape);
|
|
1809
|
+
},
|
|
1810
|
+
vectorFilesInUse: () => state.store.dimension === 0 ? [] : state.store.handles.vectorFiles.filter((path) => path !== ""),
|
|
1811
|
+
adoptDiskLayout: (layout) => adoptDiskLayout(state, layout),
|
|
1812
|
+
releaseVectorFiles: async () => {
|
|
1813
|
+
await withdrawWorkerCopies(state);
|
|
1814
|
+
state.store.closeFiles();
|
|
1815
|
+
}
|
|
1816
|
+
};
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
export { createSharedVectorSearcher, createVectorIndex };
|