@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,515 @@
|
|
|
1
|
+
import { VECTOR_FILE_MAX_VECTORS, NO_LIVE_VECTOR_AT_POSITION, bytesToVectors, VECTOR_GRAPH_PART_VECTORS, VECTOR_GRAPH_PART_PAYLOAD_VERSION, VECTOR_FILE_PAYLOAD_VERSION, deadBitsWhere, vectorsToBytes } from './chunk-YCJLVNWR.mjs';
|
|
2
|
+
import { createOrdinalFilter, addToOrdinalFilter, osqBitsOf, removeFromOrdinalFilter, createHNSWIndex, STORE_CALIBRATION_GENERATION, graphWithoutNodes } from './chunk-ULRPNYAG.mjs';
|
|
3
|
+
import { osqRecordBytes } from './chunk-37CD3OKS.mjs';
|
|
4
|
+
import { MAX_FILE_BYTES } from './chunk-RVM7AODE.mjs';
|
|
5
|
+
import { MAX_DOC_ID_LENGTH } from './chunk-Q5DC6WAB.mjs';
|
|
6
|
+
import { NarsilError, ErrorCodes } from './chunk-TPJU7NDJ.mjs';
|
|
7
|
+
|
|
8
|
+
// src/vector/vector-index/constants.ts
|
|
9
|
+
var DEFAULT_PROMOTION_THRESHOLD = 1024;
|
|
10
|
+
var DEFAULT_FILTER_THRESHOLD = 0.03;
|
|
11
|
+
var ESTIMATED_MS_PER_TOMBSTONE = 0.05;
|
|
12
|
+
var ESTIMATED_MS_PER_VECTOR_REBUILD = 0.15;
|
|
13
|
+
var WORKER_COPY_MIN_VECTORS = 1024;
|
|
14
|
+
var BUILD_CHUNK_SIZE = 100;
|
|
15
|
+
var REBUILD_REMOVED_RATIO = 0.2;
|
|
16
|
+
var VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE = 0.2;
|
|
17
|
+
var VECTOR_FILE_FIXED_OVERHEAD_BYTES = 65536;
|
|
18
|
+
var VECTOR_FILE_STRING_HEADER_BYTES = 5;
|
|
19
|
+
var VECTOR_FILE_CENTROID_BYTES_PER_COMPONENT = 9;
|
|
20
|
+
var OSQ4_MIN_DIMENSION = 384;
|
|
21
|
+
var DISK_ADOPTION_YIELD_INTERVAL = 4096;
|
|
22
|
+
|
|
23
|
+
// src/vector/vector-index/shared.ts
|
|
24
|
+
var VECTOR_WORKER_COPIES_ALLOWED = { enabled: true };
|
|
25
|
+
function liveSize(state) {
|
|
26
|
+
return state.store.size - state.tombstones.size;
|
|
27
|
+
}
|
|
28
|
+
function threadsHoldCurrentLayout(state) {
|
|
29
|
+
return state.sharedLayoutRevision === state.store.handles.layoutRevision;
|
|
30
|
+
}
|
|
31
|
+
function assignStorePartitions(state, resolve) {
|
|
32
|
+
for (let ordinal = 0; ordinal < state.store.slots; ordinal += 1) {
|
|
33
|
+
if (state.store.partitionOfOrdinal(ordinal) !== void 0) continue;
|
|
34
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
35
|
+
if (docId === void 0) continue;
|
|
36
|
+
const partitionId = resolve(docId);
|
|
37
|
+
if (partitionId === void 0) {
|
|
38
|
+
state.store.forgetPartition(docId);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
state.store.setPartition(docId, partitionId);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function emptyFieldBeforeRestore(state) {
|
|
45
|
+
state.store.clear();
|
|
46
|
+
state.tombstones.clear();
|
|
47
|
+
state.buffer.clear();
|
|
48
|
+
state.pendingLocations.clear();
|
|
49
|
+
adoptGraph(state, null);
|
|
50
|
+
state.osq?.clear();
|
|
51
|
+
state.savedFiles = [];
|
|
52
|
+
state.savedSignature = null;
|
|
53
|
+
}
|
|
54
|
+
function releaseReplacedVectors(state) {
|
|
55
|
+
for (const ordinal of state.store.releaseReplacedVectors()) state.osq?.removeOrdinal(ordinal);
|
|
56
|
+
}
|
|
57
|
+
function adoptGraph(state, graph) {
|
|
58
|
+
if (state.hnsw !== null && state.hnsw !== graph) state.hnsw.retire();
|
|
59
|
+
if (graph === null && state.hnsw !== null) {
|
|
60
|
+
state.hnsw.clear();
|
|
61
|
+
}
|
|
62
|
+
state.hnsw = graph;
|
|
63
|
+
state.compactedNodeCount = 0;
|
|
64
|
+
if (graph === null) return;
|
|
65
|
+
for (const docId of state.tombstones) {
|
|
66
|
+
if (graph.has(docId)) {
|
|
67
|
+
graph.markTombstone(docId);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function graphNeedsRebuild(state) {
|
|
72
|
+
const graph = state.hnsw;
|
|
73
|
+
if (graph === null) return false;
|
|
74
|
+
const removed = graph.tombstoneCount + state.compactedNodeCount;
|
|
75
|
+
const held = graph.size + removed;
|
|
76
|
+
if (held === 0) return false;
|
|
77
|
+
return removed / held > REBUILD_REMOVED_RATIO;
|
|
78
|
+
}
|
|
79
|
+
function yieldToEventLoop() {
|
|
80
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
81
|
+
}
|
|
82
|
+
function* allLiveDocIds(state) {
|
|
83
|
+
for (const docId of state.store.docIds()) {
|
|
84
|
+
if (state.tombstones.has(docId)) continue;
|
|
85
|
+
yield docId;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function ordinalFilterForPartitions(state, partitionIds) {
|
|
89
|
+
const filter = state.store.partitionFilter(partitionIds);
|
|
90
|
+
for (const docId of state.tombstones) {
|
|
91
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
92
|
+
if (ordinal === void 0) continue;
|
|
93
|
+
removeFromOrdinalFilter(filter, ordinal);
|
|
94
|
+
}
|
|
95
|
+
return filter;
|
|
96
|
+
}
|
|
97
|
+
function filterForOptions(state, options) {
|
|
98
|
+
if (options.filterDocIds !== void 0) {
|
|
99
|
+
return ordinalFilterForDocIds(state, options.filterDocIds);
|
|
100
|
+
}
|
|
101
|
+
if (options.filterPartitions !== void 0) {
|
|
102
|
+
return ordinalFilterForPartitions(state, options.filterPartitions);
|
|
103
|
+
}
|
|
104
|
+
return void 0;
|
|
105
|
+
}
|
|
106
|
+
function ordinalFilterForDocIds(state, docIds) {
|
|
107
|
+
const filter = createOrdinalFilter(state.store.slots);
|
|
108
|
+
for (const docId of docIds) {
|
|
109
|
+
if (state.tombstones.has(docId)) continue;
|
|
110
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
111
|
+
if (ordinal === void 0) continue;
|
|
112
|
+
addToOrdinalFilter(filter, ordinal);
|
|
113
|
+
}
|
|
114
|
+
return filter;
|
|
115
|
+
}
|
|
116
|
+
function liveOrdinals(state) {
|
|
117
|
+
const ordinals = [];
|
|
118
|
+
for (let ordinal = 0; ordinal < state.store.slots; ordinal++) {
|
|
119
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
120
|
+
if (docId !== void 0 && !state.tombstones.has(docId)) ordinals.push(ordinal);
|
|
121
|
+
}
|
|
122
|
+
return Int32Array.from(ordinals);
|
|
123
|
+
}
|
|
124
|
+
function calibrateQuantizer(state) {
|
|
125
|
+
if (state.osq === null || state.store.size === 0) return;
|
|
126
|
+
state.osq.calibrate(liveOrdinals(state));
|
|
127
|
+
}
|
|
128
|
+
function recalibrateFromStore(state) {
|
|
129
|
+
if (state.osq === null) return;
|
|
130
|
+
state.osq.recalibrate(liveOrdinals(state));
|
|
131
|
+
}
|
|
132
|
+
function fieldHandlesOf(state, graph, searchable) {
|
|
133
|
+
return {
|
|
134
|
+
dimension: state.dimension,
|
|
135
|
+
quantization: state.osq === null ? "none" : state.quantizationMode,
|
|
136
|
+
metric: state.metric,
|
|
137
|
+
store: state.store.handles,
|
|
138
|
+
graph: graph === null ? null : graph.handles,
|
|
139
|
+
filterThreshold: state.filterThreshold,
|
|
140
|
+
searchable
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/vector/vector-index/persistence.ts
|
|
145
|
+
function recordFor(state, quantizer, docId) {
|
|
146
|
+
const record = quantizer.recordOf(docId);
|
|
147
|
+
if (record !== void 0) return record;
|
|
148
|
+
if (!state.store.has(docId)) return void 0;
|
|
149
|
+
quantizer.quantize(docId);
|
|
150
|
+
return quantizer.recordOf(docId);
|
|
151
|
+
}
|
|
152
|
+
function codedCentroid(state) {
|
|
153
|
+
const centroid = state.osq?.centroid ?? null;
|
|
154
|
+
if (centroid === null || state.store.handles.codeLayout === null || state.hnsw === null) return null;
|
|
155
|
+
return Array.from(centroid);
|
|
156
|
+
}
|
|
157
|
+
function sameCentroid(state, planned) {
|
|
158
|
+
const current = state.osq?.centroid ?? null;
|
|
159
|
+
if (current === null || current.length !== planned.length) return false;
|
|
160
|
+
for (let i = 0; i < planned.length; i++) {
|
|
161
|
+
if (current[i] !== planned[i]) return false;
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
function partCodes(state, docIds, centroid, tombstonedAt) {
|
|
166
|
+
const quantizer = state.osq;
|
|
167
|
+
const layout = state.store.handles.codeLayout;
|
|
168
|
+
if (centroid === null || quantizer === null || layout === null) return null;
|
|
169
|
+
if (!sameCentroid(state, centroid)) {
|
|
170
|
+
throw new NarsilError(
|
|
171
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
172
|
+
`The vector field "${state.fieldName}" was recalibrated while a save was writing its vectors`,
|
|
173
|
+
{ fieldName: state.fieldName }
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
const records = new Uint8Array(docIds.length * layout.slotStride);
|
|
177
|
+
for (let i = 0; i < docIds.length; i++) {
|
|
178
|
+
const tombstoned = tombstonedAt.get(i);
|
|
179
|
+
const record = tombstoned === void 0 ? recordFor(state, quantizer, docIds[i]) : quantizer.recordAtOrdinal(tombstoned);
|
|
180
|
+
if (record !== void 0) records.set(record, i * layout.slotStride);
|
|
181
|
+
}
|
|
182
|
+
return { bits: quantizer.bits, centroid, records };
|
|
183
|
+
}
|
|
184
|
+
function splitSequences(parts) {
|
|
185
|
+
const sequences = [];
|
|
186
|
+
let current = null;
|
|
187
|
+
for (const part of parts) {
|
|
188
|
+
if (current === null || part.part === 0) {
|
|
189
|
+
current = { docIds: [], graphs: [], codes: null, codesComplete: true };
|
|
190
|
+
sequences.push(current);
|
|
191
|
+
}
|
|
192
|
+
current.docIds.push(...part.docIds);
|
|
193
|
+
for (let index = 0; index < part.graphs.length; index++) {
|
|
194
|
+
const graph = part.graphs[index];
|
|
195
|
+
const held = current.graphs[index];
|
|
196
|
+
if (held === void 0) current.graphs[index] = { ...graph, nodes: [...graph.nodes] };
|
|
197
|
+
else held.nodes.push(...graph.nodes);
|
|
198
|
+
}
|
|
199
|
+
if (part.codes === null) {
|
|
200
|
+
if (part.docIds.length > 0) current.codesComplete = false;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (current.codes === null) {
|
|
204
|
+
current.codes = { bits: part.codes.bits, centroid: part.codes.centroid, records: part.codes.records };
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (current.codes.bits !== part.codes.bits) {
|
|
208
|
+
current.codesComplete = false;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const joined = new Uint8Array(current.codes.records.length + part.codes.records.length);
|
|
212
|
+
joined.set(current.codes.records, 0);
|
|
213
|
+
joined.set(part.codes.records, current.codes.records.length);
|
|
214
|
+
current.codes = { ...current.codes, records: joined };
|
|
215
|
+
}
|
|
216
|
+
return sequences;
|
|
217
|
+
}
|
|
218
|
+
function restoreCodes(state, sequences) {
|
|
219
|
+
const quantizer = state.osq;
|
|
220
|
+
const layout = state.store.handles.codeLayout;
|
|
221
|
+
if (quantizer === null || layout === null) return;
|
|
222
|
+
const usable = sequences.filter((sequence) => sequence.docIds.length > 0);
|
|
223
|
+
const single = usable.length === 1 ? usable[0] : null;
|
|
224
|
+
if (single === null || single.codes === null || !single.codesComplete || single.codes.bits !== quantizer.bits) {
|
|
225
|
+
recalibrateFromStore(state);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
quantizer.restoreCentroid(Float32Array.from(single.codes.centroid));
|
|
229
|
+
const recordBytes = layout.slotStride;
|
|
230
|
+
for (let i = 0; i < single.docIds.length; i++) {
|
|
231
|
+
quantizer.restoreRecord(single.docIds[i], single.codes.records.subarray(i * recordBytes, (i + 1) * recordBytes));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function restoreGraphs(state, graphs) {
|
|
235
|
+
if (graphs.length === 0) {
|
|
236
|
+
for (const docId of state.store.docIds()) {
|
|
237
|
+
if (!state.tombstones.has(docId)) state.buffer.add(docId);
|
|
238
|
+
}
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const graphData = graphs[0];
|
|
242
|
+
const restoredHnsw = createHNSWIndex(
|
|
243
|
+
state.dimension,
|
|
244
|
+
state.store,
|
|
245
|
+
{
|
|
246
|
+
m: graphData.m ?? state.hnswConfig?.m,
|
|
247
|
+
efConstruction: graphData.efConstruction ?? state.hnswConfig?.efConstruction,
|
|
248
|
+
metric: graphData.metric ?? state.hnswConfig?.metric
|
|
249
|
+
},
|
|
250
|
+
state.osq ?? void 0
|
|
251
|
+
);
|
|
252
|
+
restoredHnsw.deserialize(graphData);
|
|
253
|
+
adoptGraph(state, restoredHnsw);
|
|
254
|
+
for (let i = 1; i < graphs.length; i++) {
|
|
255
|
+
for (const [nodeDocId] of graphs[i].nodes) {
|
|
256
|
+
if (!restoredHnsw.has(nodeDocId) && state.store.has(nodeDocId)) restoredHnsw.insertNode(nodeDocId);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const docId of state.store.docIds()) {
|
|
260
|
+
if (state.tombstones.has(docId)) continue;
|
|
261
|
+
if (!restoredHnsw.has(docId)) state.buffer.add(docId);
|
|
262
|
+
}
|
|
263
|
+
if (state.buffer.size > restoredHnsw.size) {
|
|
264
|
+
adoptGraph(state, null);
|
|
265
|
+
state.buffer.clear();
|
|
266
|
+
for (const docId of state.store.docIds()) {
|
|
267
|
+
if (!state.tombstones.has(docId)) state.buffer.add(docId);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function insertPart(state, part) {
|
|
272
|
+
const dimension = state.dimension;
|
|
273
|
+
const vectors = bytesToVectors(part.vectors);
|
|
274
|
+
for (let i = 0; i < part.docIds.length; i++) {
|
|
275
|
+
state.store.insert(part.docIds[i], vectors.subarray(i * dimension, (i + 1) * dimension));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function deserialize(state, parts) {
|
|
279
|
+
for (const part of parts) {
|
|
280
|
+
if (part.dimension !== state.dimension) {
|
|
281
|
+
throw new NarsilError(
|
|
282
|
+
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
283
|
+
`Payload dimension ${part.dimension} does not match index dimension ${state.dimension}`,
|
|
284
|
+
{ expected: state.dimension, received: part.dimension }
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const sequences = splitSequences(parts);
|
|
289
|
+
const graphs = [];
|
|
290
|
+
for (const sequence of sequences) {
|
|
291
|
+
for (const graph of sequence.graphs) {
|
|
292
|
+
if (graph.nodes.length > 0) graphs.push(graph);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
emptyFieldBeforeRestore(state);
|
|
296
|
+
for (const part of parts) insertPart(state, part);
|
|
297
|
+
if (state.osq !== null && osqBitsOf(state.quantizationMode) !== null && graphs.length > 0) {
|
|
298
|
+
restoreCodes(state, sequences);
|
|
299
|
+
}
|
|
300
|
+
restoreGraphs(state, graphs);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// src/vector/vector-index/checkpoint-plan.ts
|
|
304
|
+
var ORDINAL_HOLDS_NO_NUMBER = -1;
|
|
305
|
+
function vectorsPerFile(dimension, codeBits) {
|
|
306
|
+
const recordBytes = codeBits === 1 || codeBits === 2 || codeBits === 4 || codeBits === 8 ? osqRecordBytes(dimension, codeBits) : 0;
|
|
307
|
+
const docIdBytes = VECTOR_FILE_STRING_HEADER_BYTES + MAX_DOC_ID_LENGTH * 3;
|
|
308
|
+
const perVector = dimension * 4 + recordBytes + docIdBytes;
|
|
309
|
+
const overhead = VECTOR_FILE_FIXED_OVERHEAD_BYTES + dimension * VECTOR_FILE_CENTROID_BYTES_PER_COMPONENT;
|
|
310
|
+
const fitting = Math.floor((MAX_FILE_BYTES - overhead) / perVector);
|
|
311
|
+
return Math.max(1, Math.min(VECTOR_FILE_MAX_VECTORS, fitting));
|
|
312
|
+
}
|
|
313
|
+
function fileCapacityOf(state) {
|
|
314
|
+
return vectorsPerFile(state.dimension, osqBitsOf(state.quantizationMode));
|
|
315
|
+
}
|
|
316
|
+
function calibrationOf(state) {
|
|
317
|
+
if (codedCentroid(state) === null) return null;
|
|
318
|
+
return Atomics.load(state.store.handles.header, STORE_CALIBRATION_GENERATION);
|
|
319
|
+
}
|
|
320
|
+
function signatureOf(state) {
|
|
321
|
+
return { revision: state.revision, graphSize: state.hnsw?.size ?? -1, calibration: calibrationOf(state) };
|
|
322
|
+
}
|
|
323
|
+
function sameSignature(a, b) {
|
|
324
|
+
return a !== null && a.revision === b.revision && a.graphSize === b.graphSize && a.calibration === b.calibration;
|
|
325
|
+
}
|
|
326
|
+
function registryMatches(state, listedKeys) {
|
|
327
|
+
const listed = listedKeys ?? [];
|
|
328
|
+
if (listed.length !== state.savedFiles.length) return false;
|
|
329
|
+
return state.savedFiles.every((file, index) => file.key === listed[index]);
|
|
330
|
+
}
|
|
331
|
+
function isLive(state, ordinal) {
|
|
332
|
+
if (ordinal === NO_LIVE_VECTOR_AT_POSITION) return false;
|
|
333
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
334
|
+
return docId !== void 0 && !state.tombstones.has(docId);
|
|
335
|
+
}
|
|
336
|
+
function isGraphTombstone(state, ordinal) {
|
|
337
|
+
return ordinal !== NO_LIVE_VECTOR_AT_POSITION && state.hnsw !== null && state.hnsw.holdsTombstone(ordinal);
|
|
338
|
+
}
|
|
339
|
+
function isCarried(state, ordinal) {
|
|
340
|
+
return isLive(state, ordinal) || isGraphTombstone(state, ordinal);
|
|
341
|
+
}
|
|
342
|
+
function carriedCountOf(state, file) {
|
|
343
|
+
let carried = 0;
|
|
344
|
+
for (let i = 0; i < file.ordinals.length; i++) if (isCarried(state, file.ordinals[i])) carried += 1;
|
|
345
|
+
return carried;
|
|
346
|
+
}
|
|
347
|
+
function deadBitsOf(state, file) {
|
|
348
|
+
return deadBitsWhere(file.ordinals.length, (position) => !isLive(state, file.ordinals[position]));
|
|
349
|
+
}
|
|
350
|
+
function filesWorthKeeping(state, listed, calibration, carriedCounts) {
|
|
351
|
+
const kept = [];
|
|
352
|
+
for (const file of listed) {
|
|
353
|
+
const carried = carriedCountOf(state, file);
|
|
354
|
+
const deadWithoutNodeShare = file.ordinals.length === 0 ? 1 : 1 - carried / file.ordinals.length;
|
|
355
|
+
if (file.calibration !== calibration || deadWithoutNodeShare > VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE) continue;
|
|
356
|
+
kept.push(file);
|
|
357
|
+
carriedCounts.push(carried);
|
|
358
|
+
}
|
|
359
|
+
return kept;
|
|
360
|
+
}
|
|
361
|
+
function absorbSmallerPartialFiles(kept, carriedCounts, vectorsToWrite, capacity) {
|
|
362
|
+
let pending = vectorsToWrite;
|
|
363
|
+
for (let index = kept.length - 1; index >= 0 && pending > 0; index--) {
|
|
364
|
+
if (kept[index].ordinals.length >= capacity) continue;
|
|
365
|
+
if (carriedCounts[index] > pending) return;
|
|
366
|
+
pending += carriedCounts[index];
|
|
367
|
+
kept.splice(index, 1);
|
|
368
|
+
carriedCounts.splice(index, 1);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function ordinalsOutsideOf(state, kept) {
|
|
372
|
+
const slots = state.store.slots;
|
|
373
|
+
const saved = new Uint8Array(slots);
|
|
374
|
+
for (const file of kept) {
|
|
375
|
+
for (let i = 0; i < file.ordinals.length; i++) {
|
|
376
|
+
const ordinal = file.ordinals[i];
|
|
377
|
+
if (ordinal !== NO_LIVE_VECTOR_AT_POSITION && ordinal < slots) saved[ordinal] = 1;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const outside = [];
|
|
381
|
+
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
382
|
+
if (saved[ordinal] === 0 && isCarried(state, ordinal)) outside.push(ordinal);
|
|
383
|
+
}
|
|
384
|
+
return outside;
|
|
385
|
+
}
|
|
386
|
+
function countOutside(state, carriedCounts) {
|
|
387
|
+
let held = 0;
|
|
388
|
+
for (const carried of carriedCounts) held += carried;
|
|
389
|
+
const tombstonedNodes = state.hnsw?.tombstoneCount ?? 0;
|
|
390
|
+
return Math.max(0, state.store.size - state.tombstones.size + tombstonedNodes - held);
|
|
391
|
+
}
|
|
392
|
+
function numberEveryVector(state, kept, written) {
|
|
393
|
+
let total = written.length;
|
|
394
|
+
for (const file of kept) total += file.ordinals.length;
|
|
395
|
+
const numberOfOrdinal = new Int32Array(state.store.slots).fill(ORDINAL_HOLDS_NO_NUMBER);
|
|
396
|
+
const ordinalOfNumber = new Int32Array(total).fill(NO_LIVE_VECTOR_AT_POSITION);
|
|
397
|
+
let number = 0;
|
|
398
|
+
for (const file of kept) {
|
|
399
|
+
for (let i = 0; i < file.ordinals.length; i++, number++) {
|
|
400
|
+
const ordinal = file.ordinals[i];
|
|
401
|
+
if (!isCarried(state, ordinal)) continue;
|
|
402
|
+
numberOfOrdinal[ordinal] = number;
|
|
403
|
+
ordinalOfNumber[number] = ordinal;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
for (const ordinal of written) {
|
|
407
|
+
numberOfOrdinal[ordinal] = number;
|
|
408
|
+
ordinalOfNumber[number] = ordinal;
|
|
409
|
+
number += 1;
|
|
410
|
+
}
|
|
411
|
+
return { numberOfOrdinal, ordinalOfNumber };
|
|
412
|
+
}
|
|
413
|
+
function graphPartsOf(state, numbers) {
|
|
414
|
+
if (state.hnsw === null || numbers.ordinalOfNumber.length === 0) return null;
|
|
415
|
+
const parts = state.hnsw.serializeNumbered(
|
|
416
|
+
numbers.numberOfOrdinal,
|
|
417
|
+
numbers.ordinalOfNumber,
|
|
418
|
+
VECTOR_GRAPH_PART_VECTORS
|
|
419
|
+
);
|
|
420
|
+
return parts.map((graph) => ({ v: VECTOR_GRAPH_PART_PAYLOAD_VERSION, fieldName: state.fieldName, graphs: [graph] }));
|
|
421
|
+
}
|
|
422
|
+
function graphPartsWithoutNodes(parts, numbers) {
|
|
423
|
+
return parts.map((part, index) => ({
|
|
424
|
+
...part,
|
|
425
|
+
graphs: part.graphs.map((graph) => graphWithoutNodes(graph, index * VECTOR_GRAPH_PART_VECTORS, numbers))
|
|
426
|
+
}));
|
|
427
|
+
}
|
|
428
|
+
function plannedVectorOf(state, ordinal) {
|
|
429
|
+
const docId = state.store.docIdForOrdinal(ordinal) ?? state.store.view.docIdAt(ordinal) ?? "";
|
|
430
|
+
return { ordinal, docId, tombstoned: !isLive(state, ordinal) };
|
|
431
|
+
}
|
|
432
|
+
function readFile(state, planned, centroid) {
|
|
433
|
+
const dimension = state.dimension;
|
|
434
|
+
const vectors = new Float32Array(planned.length * dimension);
|
|
435
|
+
const ordinals = new Int32Array(planned.length).fill(NO_LIVE_VECTOR_AT_POSITION);
|
|
436
|
+
const tombstonedAt = /* @__PURE__ */ new Map();
|
|
437
|
+
const docIds = planned.map((vector) => vector.docId);
|
|
438
|
+
for (let i = 0; i < planned.length; i++) {
|
|
439
|
+
const { ordinal, docId, tombstoned } = planned[i];
|
|
440
|
+
const held = tombstoned || state.store.holdsOrdinal(ordinal) ? ordinal : state.store.getOrdinal(docId);
|
|
441
|
+
const entry = held === void 0 ? void 0 : state.store.entryForOrdinal(held);
|
|
442
|
+
if (held === void 0 || entry === void 0) continue;
|
|
443
|
+
vectors.set(entry.vector, i * dimension);
|
|
444
|
+
ordinals[i] = held;
|
|
445
|
+
if (tombstoned) tombstonedAt.set(i, held);
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
payload: {
|
|
449
|
+
v: VECTOR_FILE_PAYLOAD_VERSION,
|
|
450
|
+
fieldName: state.fieldName,
|
|
451
|
+
dimension,
|
|
452
|
+
docIds,
|
|
453
|
+
codes: partCodes(state, docIds, centroid, tombstonedAt),
|
|
454
|
+
vectors: vectorsToBytes(vectors)
|
|
455
|
+
},
|
|
456
|
+
ordinals,
|
|
457
|
+
dead: deadBitsWhere(planned.length, (i) => planned[i].tombstoned || ordinals[i] === NO_LIVE_VECTOR_AT_POSITION)
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
function planCheckpoint(state, listedKeys, rewritesGraph = false) {
|
|
461
|
+
const signature = signatureOf(state);
|
|
462
|
+
const calibration = signature.calibration;
|
|
463
|
+
const trusted = registryMatches(state, listedKeys);
|
|
464
|
+
const capacity = fileCapacityOf(state);
|
|
465
|
+
const carriedCounts = [];
|
|
466
|
+
const keptFiles = filesWorthKeeping(state, trusted ? state.savedFiles : [], calibration, carriedCounts);
|
|
467
|
+
absorbSmallerPartialFiles(keptFiles, carriedCounts, countOutside(state, carriedCounts), capacity);
|
|
468
|
+
const written = ordinalsOutsideOf(state, keptFiles);
|
|
469
|
+
const planned = written.map((ordinal) => plannedVectorOf(state, ordinal));
|
|
470
|
+
const centroid = codedCentroid(state);
|
|
471
|
+
const listedBefore = trusted && listedKeys !== null;
|
|
472
|
+
const unchanged = !rewritesGraph && listedBefore && sameSignature(state.savedSignature, signature) && written.length === 0;
|
|
473
|
+
let firstWrittenNumber = 0;
|
|
474
|
+
for (const file of keptFiles) firstWrittenNumber += file.ordinals.length;
|
|
475
|
+
const graphParts = unchanged ? null : graphPartsOf(state, numberEveryVector(state, keptFiles, written));
|
|
476
|
+
const vanished = /* @__PURE__ */ new Set();
|
|
477
|
+
let vanishedWhenStripped = 0;
|
|
478
|
+
let graphPartsToWrite = graphParts;
|
|
479
|
+
return {
|
|
480
|
+
unchanged,
|
|
481
|
+
keptFiles,
|
|
482
|
+
kept: keptFiles.map((file) => ({
|
|
483
|
+
id: file.id,
|
|
484
|
+
key: file.key,
|
|
485
|
+
count: file.ordinals.length,
|
|
486
|
+
dead: deadBitsOf(state, file)
|
|
487
|
+
})),
|
|
488
|
+
newFiles: Math.ceil(written.length / capacity),
|
|
489
|
+
get graphParts() {
|
|
490
|
+
if (graphParts !== null && vanished.size !== vanishedWhenStripped) {
|
|
491
|
+
graphPartsToWrite = graphPartsWithoutNodes(graphParts, vanished);
|
|
492
|
+
vanishedWhenStripped = vanished.size;
|
|
493
|
+
}
|
|
494
|
+
return graphPartsToWrite;
|
|
495
|
+
},
|
|
496
|
+
signature,
|
|
497
|
+
readNewFile(index) {
|
|
498
|
+
const start = index * capacity;
|
|
499
|
+
const end = start + capacity;
|
|
500
|
+
const file = readFile(state, planned.slice(start, end), centroid);
|
|
501
|
+
for (let i = 0; i < file.ordinals.length; i++) {
|
|
502
|
+
if (file.ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) vanished.add(firstWrittenNumber + start + i);
|
|
503
|
+
}
|
|
504
|
+
return file;
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
function recordCheckpoint(state, plan, written) {
|
|
509
|
+
if (plan.unchanged) return;
|
|
510
|
+
const calibration = plan.signature.calibration;
|
|
511
|
+
state.savedFiles = [...plan.keptFiles, ...written.map((file) => ({ ...file, calibration }))];
|
|
512
|
+
state.savedSignature = plan.signature;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export { BUILD_CHUNK_SIZE, DEFAULT_FILTER_THRESHOLD, DEFAULT_PROMOTION_THRESHOLD, DISK_ADOPTION_YIELD_INTERVAL, ESTIMATED_MS_PER_TOMBSTONE, ESTIMATED_MS_PER_VECTOR_REBUILD, OSQ4_MIN_DIMENSION, VECTOR_WORKER_COPIES_ALLOWED, WORKER_COPY_MIN_VECTORS, adoptGraph, allLiveDocIds, assignStorePartitions, calibrateQuantizer, deserialize, emptyFieldBeforeRestore, fieldHandlesOf, filterForOptions, graphNeedsRebuild, liveSize, planCheckpoint, recalibrateFromStore, recordCheckpoint, releaseReplacedVectors, signatureOf, threadsHoldCurrentLayout, vectorsPerFile, yieldToEventLoop };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// src/distribution/constants.ts
|
|
2
|
+
var WIRE_BATCH_BUDGET = {
|
|
3
|
+
maxCount: 1e3,
|
|
4
|
+
maxBytes: 8388608,
|
|
5
|
+
overheadBytes: 256
|
|
6
|
+
};
|
|
7
|
+
var MAX_PARTITION_COUNT = 65536;
|
|
8
|
+
var MAX_REPLICATION_FACTOR = 255;
|
|
9
|
+
|
|
10
|
+
export { MAX_PARTITION_COUNT, MAX_REPLICATION_FACTOR, WIRE_BATCH_BUDGET };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { english } from './chunk-TGP5M5R7.mjs';
|
|
2
|
+
import { NarsilError, ErrorCodes } from './chunk-TPJU7NDJ.mjs';
|
|
3
|
+
|
|
4
|
+
// src/languages/registry.ts
|
|
5
|
+
var languages = /* @__PURE__ */ new Map();
|
|
6
|
+
languages.set("english", english);
|
|
7
|
+
function registerLanguage(module) {
|
|
8
|
+
languages.set(module.name, module);
|
|
9
|
+
}
|
|
10
|
+
function getLanguage(name) {
|
|
11
|
+
const lang = languages.get(name);
|
|
12
|
+
if (!lang) {
|
|
13
|
+
throw new NarsilError(ErrorCodes.LANGUAGE_NOT_SUPPORTED, `Language "${name}" is not registered`, { language: name });
|
|
14
|
+
}
|
|
15
|
+
return lang;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { getLanguage, registerLanguage };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { flattenSchema
|
|
1
|
+
import { flattenSchema } from './chunk-CVQWQVFX.mjs';
|
|
2
|
+
import { PARTIAL_WORD_MIN_CODE_POINTS, PARTIAL_WORDS_ADDED_PER_QUERY, DEFAULT_FUZZY_PREFIX_LENGTH } from './chunk-FGMGUDHX.mjs';
|
|
2
3
|
import { fuzzyTermMatches } from './chunk-GES7U2PM.mjs';
|
|
3
|
-
import { isWordIndexedBase } from './chunk-
|
|
4
|
+
import { isWordIndexedBase } from './chunk-BVOJIYRV.mjs';
|
|
4
5
|
import { compareCodePoints, codePointCount } from './chunk-YTJCGKM4.mjs';
|
|
5
6
|
import { createNarsilError, ErrorCodes } from './chunk-TPJU7NDJ.mjs';
|
|
6
7
|
|
|
@@ -3,21 +3,6 @@ import { existsSync } from 'fs';
|
|
|
3
3
|
import { createRequire } from 'module';
|
|
4
4
|
import { join, dirname } from 'path';
|
|
5
5
|
|
|
6
|
-
// src/vector/native/backend.ts
|
|
7
|
-
var nativeSearchCoreEnabled = true;
|
|
8
|
-
function nativeSearchCoreIsEnabled() {
|
|
9
|
-
return nativeSearchCoreEnabled;
|
|
10
|
-
}
|
|
11
|
-
function stopUsingTheNativeSearchCore(error) {
|
|
12
|
-
if (!nativeSearchCoreEnabled) return;
|
|
13
|
-
nativeSearchCoreEnabled = false;
|
|
14
|
-
console.warn(
|
|
15
|
-
"The native search core raised an error, so this thread searches through WebAssembly from now on:",
|
|
16
|
-
error instanceof Error ? error.message : String(error)
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
function noteFallback(operation) {
|
|
20
|
-
}
|
|
21
6
|
var NATIVE_CORE_ABI_VERSION = 3;
|
|
22
7
|
var BINARY_FILE_NAME = "narsil-core.node";
|
|
23
8
|
var MUSL_DIRECTORY = "musl";
|
|
@@ -126,4 +111,4 @@ function loadNativeCore() {
|
|
|
126
111
|
return loaded;
|
|
127
112
|
}
|
|
128
113
|
|
|
129
|
-
export { loadNativeCore,
|
|
114
|
+
export { loadNativeCore, requireSearchBackendSettings };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isPlainObject, MAX_NESTING_DEPTH } from './chunk-L5ORED7V.mjs';
|
|
2
|
+
|
|
3
|
+
// src/workers/vector-code.ts
|
|
4
|
+
var VECTOR_TYPE_NAME = "vector";
|
|
5
|
+
var loaded = null;
|
|
6
|
+
var loading = null;
|
|
7
|
+
function loadedVectorCode() {
|
|
8
|
+
return loaded;
|
|
9
|
+
}
|
|
10
|
+
function requireLoadedVectorCode() {
|
|
11
|
+
if (loaded === null) throw new Error("The engine reached vector code before it loaded that code");
|
|
12
|
+
return loaded;
|
|
13
|
+
}
|
|
14
|
+
function loadVectorCode() {
|
|
15
|
+
if (loaded !== null) return Promise.resolve(loaded);
|
|
16
|
+
if (loading === null) {
|
|
17
|
+
loading = import('./module-WIDYIJLY.mjs').then(
|
|
18
|
+
(code) => {
|
|
19
|
+
loaded = code;
|
|
20
|
+
return code;
|
|
21
|
+
},
|
|
22
|
+
(err) => {
|
|
23
|
+
loading = null;
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return loading;
|
|
29
|
+
}
|
|
30
|
+
function mayDeclareVectorField(schema, depth) {
|
|
31
|
+
if (!isPlainObject(schema) || depth > MAX_NESTING_DEPTH + 1) return false;
|
|
32
|
+
for (const type of Object.values(schema)) {
|
|
33
|
+
if (typeof type === "string" ? type.includes(VECTOR_TYPE_NAME) : mayDeclareVectorField(type, depth + 1)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
function loadVectorCodeFor(schema) {
|
|
40
|
+
if (loaded !== null || !mayDeclareVectorField(schema, 1)) return null;
|
|
41
|
+
return loadVectorCode();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { loadVectorCode, loadVectorCodeFor, loadedVectorCode, requireLoadedVectorCode };
|