@delali/narsil 0.2.4-next.59 → 0.3.1-next.101
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/README.md +9 -8
- package/dist/adapters/filesystem.d.ts +1 -1
- package/dist/adapters/filesystem.mjs +1 -1
- package/dist/adapters/indexeddb.d.ts +1 -1
- package/dist/adapters/indexeddb.mjs +1 -1
- package/dist/adapters/memory.d.ts +1 -1
- package/dist/{adapters-79OIa4xm.d.ts → adapters-BQFQ4PRz.d.ts} +27 -1
- package/dist/browser/chunk-BDOSMEEP.mjs +493 -0
- package/dist/browser/chunk-EJDUFWSO.mjs +602 -0
- package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
- package/dist/browser/chunk-JOVZ6HMT.mjs +4358 -0
- package/dist/browser/chunk-JPTQZWJQ.mjs +10 -0
- package/dist/browser/chunk-KPJMDPHG.mjs +24558 -0
- package/dist/browser/chunk-PGIM3HIM.mjs +2743 -0
- package/dist/browser/chunk-RHG4O77A.mjs +206 -0
- package/dist/browser/chunk-RIJSLY2I.mjs +360 -0
- package/dist/browser/durability-wiring-SRUYT3VV.mjs +3875 -0
- package/dist/browser/module-CBB2ETYP.mjs +3111 -0
- package/dist/browser/release-76MXRPGV.mjs +33 -0
- package/dist/browser/replace-RURXTGVG.mjs +57 -0
- package/dist/browser/restore-queue-EEZQO6RF.mjs +35 -0
- package/dist/browser/runtime-7ZAHCHWR.mjs +557 -0
- package/dist/browser/snapshot-OAAA5UJ7.mjs +1210 -0
- package/dist/browser/write-7D6VIK6O.mjs +208 -0
- package/dist/{capabilities-DSsOGz6F.d.ts → capabilities-DiGlB6Sr.d.ts} +68 -7
- package/dist/chunk-2RIRGOUL.mjs +113 -0
- package/dist/chunk-37CD3OKS.mjs +87 -0
- package/dist/chunk-44W3CBNG.mjs +7 -0
- package/dist/chunk-46D6JQIG.mjs +426 -0
- package/dist/{chunk-VMRR3DQU.mjs → chunk-5JNA5SKS.mjs} +546 -35
- package/dist/chunk-747OHOUA.mjs +11 -0
- package/dist/chunk-75XIEF6H.mjs +126 -0
- package/dist/chunk-7X5JST5L.mjs +114 -0
- package/dist/chunk-BOFBLJFZ.mjs +209 -0
- package/dist/{chunk-USKFU7J5.mjs → chunk-BWJVNZPR.mjs} +8 -1
- package/dist/chunk-C7SLA6CM.mjs +19 -0
- package/dist/{chunk-POWOTUKJ.mjs → chunk-CQUMNIIG.mjs} +3 -6
- package/dist/chunk-CVXLLEQ3.mjs +1011 -0
- package/dist/chunk-DJHHF2L5.mjs +17 -0
- package/dist/chunk-E74YLAIG.mjs +24 -0
- package/dist/{chunk-UXNWQZST.mjs → chunk-EMRMGYRE.mjs} +236 -18
- package/dist/chunk-EWZLIYHV.mjs +57 -0
- package/dist/chunk-GBEEG5AN.mjs +35 -0
- package/dist/chunk-GES7U2PM.mjs +68 -0
- package/dist/chunk-HJSN7UYG.mjs +10 -0
- package/dist/chunk-HLPRHOSO.mjs +17 -0
- package/dist/chunk-HMFNFVP7.mjs +14 -0
- package/dist/chunk-J5QP52HA.mjs +1751 -0
- package/dist/{chunk-YNVIUGF5.mjs → chunk-JZVARCRZ.mjs} +3 -28
- package/dist/chunk-KOKM4PQN.mjs +52 -0
- package/dist/{chunk-B2TFROFX.mjs → chunk-KWTW6VTG.mjs} +465 -250
- package/dist/chunk-KZ3NX2P7.mjs +21 -0
- package/dist/chunk-L5ORED7V.mjs +25 -0
- package/dist/{chunk-THYRHHO2.mjs → chunk-LGAMKHA2.mjs} +2009 -2351
- package/dist/chunk-LKOB5COA.mjs +117 -0
- package/dist/chunk-LPQOEU5M.mjs +96 -0
- package/dist/{chunk-3LHGNRYE.mjs → chunk-LXWDQLTW.mjs} +7 -2
- package/dist/{chunk-L6TNPLAR.mjs → chunk-OEYI3KRB.mjs} +270 -108
- package/dist/chunk-OM362BRO.mjs +948 -0
- package/dist/chunk-OZDFA4WC.mjs +67 -0
- package/dist/chunk-P36MUFEJ.mjs +10 -0
- package/dist/chunk-PC2G76VW.mjs +2540 -0
- package/dist/{chunk-GCM3JFVA.mjs → chunk-PNEYIHA4.mjs} +11 -2
- package/dist/chunk-PNFKUCG3.mjs +1130 -0
- package/dist/chunk-Q6XWVUZX.mjs +44 -0
- package/dist/chunk-R27EMH5M.mjs +617 -0
- package/dist/{chunk-YMZCMH5M.mjs → chunk-R6A773WK.mjs} +19 -24
- package/dist/chunk-RF7PLG43.mjs +106 -0
- package/dist/chunk-RFBLQLLZ.mjs +16 -0
- package/dist/{chunk-Y3L5MDRV.mjs → chunk-RFVUDM5E.mjs} +17 -2
- package/dist/chunk-S7FL6UAH.mjs +521 -0
- package/dist/chunk-S7GNDRPS.mjs +651 -0
- package/dist/chunk-SP3NM3ZG.mjs +3537 -0
- package/dist/chunk-U476IXKQ.mjs +10 -0
- package/dist/chunk-UA3AUGNV.mjs +462 -0
- package/dist/chunk-UTG5YNXV.mjs +507 -0
- package/dist/chunk-VI4R6W25.mjs +8484 -0
- package/dist/chunk-WQBCBKY3.mjs +8 -0
- package/dist/{chunk-24RXHMEL.mjs → chunk-WVCOCKBU.mjs} +3794 -7570
- package/dist/chunk-X5CV2E2B.mjs +1 -0
- package/dist/chunk-XADZPPZI.mjs +614 -0
- package/dist/chunk-YEEQHT4V.mjs +212 -0
- package/dist/chunk-YTJCGKM4.mjs +56 -0
- package/dist/chunk-YYQIODMF.mjs +275 -0
- package/dist/chunk-ZI34TVOA.mjs +144 -0
- package/dist/{chunk-OOSIDHA3.mjs → chunk-ZKWYWW4Y.mjs} +1 -1
- package/dist/chunk-ZM7KU4YW.mjs +35 -0
- package/dist/{client-BQK6Tf6A.d.ts → client-BOStAkc5.d.ts} +160 -37
- package/dist/client.d.ts +11 -10
- package/dist/client.mjs +209 -45
- package/dist/{config-DmXULPa-.d.ts → config-Cjvzbs_j.d.ts} +51 -3
- package/dist/config-DffQlmRq.d.ts +421 -0
- package/dist/direct-executor-ZD64PPEZ.mjs +24 -0
- package/dist/distribution/coordinator/etcd.d.ts +79 -18
- package/dist/distribution/coordinator/etcd.mjs +553 -109
- package/dist/distribution/coordinator/in-memory.d.ts +4 -3
- package/dist/distribution/coordinator/in-memory.mjs +22 -3
- package/dist/distribution/transport/grpc.d.ts +2 -2
- package/dist/distribution/transport/grpc.mjs +79 -14
- package/dist/distribution/transport/in-memory.d.ts +2 -2
- package/dist/distribution/transport/in-memory.mjs +12 -7
- package/dist/distribution/transport/tcp.d.ts +2 -2
- package/dist/distribution/transport/tcp.mjs +50 -28
- package/dist/distribution.d.ts +144 -23
- package/dist/distribution.mjs +14479 -8173
- package/dist/durability-wiring-FEUBR5BT.mjs +3330 -0
- package/dist/embeddings/openai.d.ts +2 -2
- package/dist/embeddings/openai.mjs +22 -133
- package/dist/{engine-CIOyFozK.d.ts → engine-rC0vjjY-.d.ts} +165 -141
- package/dist/{memory-yFaVn2xZ.d.ts → events-DqOIg5xa.d.ts} +645 -136
- package/dist/{types-DAK7mnUZ.d.ts → index-Bkdt8nDw.d.ts} +182 -45
- package/dist/index.browser.mjs +4 -32180
- package/dist/index.d.ts +91 -20
- package/dist/index.mjs +33 -16
- package/dist/invalidation/broadcast-channel.d.ts +1 -1
- package/dist/invalidation/filesystem.d.ts +1 -1
- package/dist/invalidation/filesystem.mjs +2 -1
- package/dist/invalidation/noop.d.ts +1 -1
- package/dist/module-3S65MVCM.mjs +2151 -0
- package/dist/partial-words-DTIHO8G4.d.ts +19 -0
- package/dist/partial-words.d.ts +22 -0
- package/dist/partial-words.mjs +17 -0
- package/dist/pattern-Cledccbl.d.ts +18 -0
- package/dist/pattern.d.ts +22 -0
- package/dist/pattern.mjs +12 -0
- package/dist/persistence/durability/checkpoint-worker.d.ts +119 -72
- package/dist/persistence/durability/checkpoint-worker.mjs +51 -18
- package/dist/react.d.ts +279 -183
- package/dist/react.mjs +201 -48
- package/dist/release-BH3DKW3T.mjs +33 -0
- package/dist/replace-CEO5KJJ3.mjs +57 -0
- package/dist/rerank-K7C4WZFG.mjs +60 -0
- package/dist/rerankers/http.d.ts +50 -0
- package/dist/rerankers/http.mjs +104 -0
- package/dist/restore-queue-KQHA7ZV2.mjs +35 -0
- package/dist/runtime-6Y4ZYXOG.mjs +177 -0
- package/dist/{schema-d5Afu03A.d.ts → schema-4W_7tk1f.d.ts} +81 -11
- package/dist/server.d.ts +13 -12
- package/dist/server.mjs +611 -80
- package/dist/snapshot-QGZPODM5.mjs +799 -0
- package/dist/synonyms-CpOAHPa-.d.ts +52 -0
- package/dist/synonyms.d.ts +22 -0
- package/dist/synonyms.mjs +12 -0
- package/dist/{tasks-CH2Dod8w.d.ts → tasks-CwPTz-tP.d.ts} +10 -3
- package/dist/thread-N3IUGUS3.mjs +697 -0
- package/dist/{types-BQDlIvfS.d.ts → types-2qoitTXg.d.ts} +1 -1
- package/dist/{types-D5OwhtsQ.d.ts → types-Cz5okLml.d.ts} +2 -2
- package/dist/vector/search-worker.d.ts +46 -4
- package/dist/vector/search-worker.mjs +21 -11
- package/dist/workers/entry.mjs +9 -6
- package/dist/write-U5RI7MAX.mjs +241 -0
- package/package.json +55 -9
- package/dist/chunk-3L4U6EJQ.mjs +0 -49
- package/dist/chunk-IFBKLBGB.mjs +0 -7
- package/dist/chunk-ORBLD3EL.mjs +0 -6
- package/dist/chunk-U7KYW4BA.mjs +0 -135
- package/dist/chunk-ZJ4CSAB2.mjs +0 -13
- package/dist/chunk-ZNZBXMP5.mjs +0 -2117
- package/dist/chunk-ZZT3EAUC.mjs +0 -11168
- package/dist/config-BhldZgaK.d.ts +0 -194
- package/dist/direct-executor-ALAVE7FC.mjs +0 -12
- package/dist/serialization/crc32-worker.d.ts +0 -20
- package/dist/serialization/crc32-worker.mjs +0 -62
- package/dist/simd-DW5AtJ6B.d.ts +0 -41
- package/dist/thread-DYHEQRVY.mjs +0 -402
|
@@ -0,0 +1,651 @@
|
|
|
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-YYQIODMF.mjs';
|
|
2
|
+
import { createOrdinalFilter, addToOrdinalFilter, osqBitsOf, STORE_ORDINAL_LAYOUT_GENERATION, removeFromOrdinalFilter, createHNSWIndex, STORE_CALIBRATION_GENERATION, graphWithoutNodes, createOsqScratch, osqQuantize } from './chunk-LGAMKHA2.mjs';
|
|
3
|
+
import { osqRecordBytes, writeRecord } from './chunk-37CD3OKS.mjs';
|
|
4
|
+
import { MAX_DOC_ID_LENGTH } from './chunk-LXWDQLTW.mjs';
|
|
5
|
+
import { MAX_FILE_BYTES } from './chunk-U476IXKQ.mjs';
|
|
6
|
+
import { NarsilError, ErrorCodes } from './chunk-BWJVNZPR.mjs';
|
|
7
|
+
|
|
8
|
+
// src/vector/filter-ordinals.ts
|
|
9
|
+
var UNRESOLVED_ORDINAL = -1;
|
|
10
|
+
var NO_VECTOR_MARK = -2;
|
|
11
|
+
function freshOrdinals(length) {
|
|
12
|
+
return new Int32Array(length).fill(UNRESOLVED_ORDINAL);
|
|
13
|
+
}
|
|
14
|
+
function mapsOf(owner) {
|
|
15
|
+
owner.vectorOrdinalMaps ??= { bytes: 0, byField: /* @__PURE__ */ new Map() };
|
|
16
|
+
return owner.vectorOrdinalMaps;
|
|
17
|
+
}
|
|
18
|
+
function mapFor(maps, docStore, space, numberingGeneration) {
|
|
19
|
+
const held = maps.byField.get(space.fieldName);
|
|
20
|
+
if (held !== void 0 && held.header === space.header && held.layoutGeneration === space.layoutGeneration && held.numberingGeneration === numberingGeneration) {
|
|
21
|
+
return held;
|
|
22
|
+
}
|
|
23
|
+
const map = {
|
|
24
|
+
header: space.header,
|
|
25
|
+
layoutGeneration: space.layoutGeneration,
|
|
26
|
+
numberingGeneration,
|
|
27
|
+
ordinals: freshOrdinals(docStore.internalIdCapacity())
|
|
28
|
+
};
|
|
29
|
+
maps.bytes += map.ordinals.byteLength - (held?.ordinals.byteLength ?? 0);
|
|
30
|
+
maps.byField.set(space.fieldName, map);
|
|
31
|
+
return map;
|
|
32
|
+
}
|
|
33
|
+
function growTo(maps, map, length) {
|
|
34
|
+
const grown = freshOrdinals(Math.max(length, map.ordinals.length * 2));
|
|
35
|
+
grown.set(map.ordinals);
|
|
36
|
+
maps.bytes += grown.byteLength - map.ordinals.byteLength;
|
|
37
|
+
map.ordinals = grown;
|
|
38
|
+
}
|
|
39
|
+
function ordinalAt(maps, map, internalId, docStore, space) {
|
|
40
|
+
if (internalId >= map.ordinals.length) growTo(maps, map, internalId + 1);
|
|
41
|
+
const held = map.ordinals[internalId];
|
|
42
|
+
if (held >= 0 && space.holdsCurrentOrdinal(held)) return held;
|
|
43
|
+
if (held <= NO_VECTOR_MARK && NO_VECTOR_MARK - held === space.slots) return UNRESOLVED_ORDINAL;
|
|
44
|
+
const docId = docStore.getExternalId(internalId);
|
|
45
|
+
const resolved = docId === void 0 ? void 0 : space.ordinalOf(docId);
|
|
46
|
+
map.ordinals[internalId] = resolved ?? NO_VECTOR_MARK - space.slots;
|
|
47
|
+
return resolved ?? UNRESOLVED_ORDINAL;
|
|
48
|
+
}
|
|
49
|
+
function ordinalFilterForSubsets(subsets, space) {
|
|
50
|
+
const filter = createOrdinalFilter(space.slots);
|
|
51
|
+
for (const subset of subsets) {
|
|
52
|
+
const docStore = subset.owner.docStore;
|
|
53
|
+
if (docStore.numberingGeneration() !== subset.numberingGeneration) continue;
|
|
54
|
+
const resolver = docStore.resolver();
|
|
55
|
+
const maps = mapsOf(subset.owner);
|
|
56
|
+
const map = mapFor(maps, docStore, space, subset.numberingGeneration);
|
|
57
|
+
const bits = subset.bits;
|
|
58
|
+
for (let wordIndex = 0; wordIndex < bits.length; wordIndex++) {
|
|
59
|
+
let word = bits[wordIndex];
|
|
60
|
+
if (word === 0) continue;
|
|
61
|
+
const base = wordIndex << 5;
|
|
62
|
+
while (word !== 0) {
|
|
63
|
+
const internalId = base + (Math.clz32(word & -word) ^ 31);
|
|
64
|
+
word &= word - 1;
|
|
65
|
+
if (!resolver.isLive(internalId)) continue;
|
|
66
|
+
const ordinal = ordinalAt(maps, map, internalId, docStore, space);
|
|
67
|
+
if (ordinal < 0 || space.isRemoved(ordinal)) continue;
|
|
68
|
+
addToOrdinalFilter(filter, ordinal);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return filter;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/vector/vector-index/constants.ts
|
|
76
|
+
var DEFAULT_PROMOTION_THRESHOLD = 1024;
|
|
77
|
+
var DEFAULT_FILTER_THRESHOLD = 0.03;
|
|
78
|
+
var ESTIMATED_MS_PER_TOMBSTONE = 0.05;
|
|
79
|
+
var ESTIMATED_MS_PER_VECTOR_REBUILD = 0.15;
|
|
80
|
+
var WORKER_COPY_MIN_VECTORS = 1024;
|
|
81
|
+
var BUILD_CHUNK_SIZE = 100;
|
|
82
|
+
var BUILD_TURN_HANDOVER_AFTER_MS = 50;
|
|
83
|
+
var BUILD_RETRY_FIRST_DELAY_MS = 250;
|
|
84
|
+
var BUILD_RETRY_MAX_DELAY_MS = 3e4;
|
|
85
|
+
var REBUILD_REMOVED_RATIO = 0.2;
|
|
86
|
+
var DRIFT_CHECK_GROWTH_FACTOR = 2;
|
|
87
|
+
var RECALIBRATION_DRIFT_RATIO = 2.5;
|
|
88
|
+
var RECALIBRATION_CHUNK_VECTORS = 512;
|
|
89
|
+
var VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE = 0.2;
|
|
90
|
+
var VECTOR_FILE_FIXED_OVERHEAD_BYTES = 65536;
|
|
91
|
+
var VECTOR_FILE_STRING_HEADER_BYTES = 5;
|
|
92
|
+
var VECTOR_FILE_CENTROID_BYTES_PER_COMPONENT = 9;
|
|
93
|
+
var OSQ4_MIN_DIMENSION = 384;
|
|
94
|
+
var DISK_ADOPTION_YIELD_INTERVAL = 4096;
|
|
95
|
+
|
|
96
|
+
// src/vector/vector-index/shared.ts
|
|
97
|
+
var VECTOR_WORKER_COPIES_ALLOWED = { enabled: true };
|
|
98
|
+
function liveSize(state) {
|
|
99
|
+
return state.store.size - state.tombstones.size;
|
|
100
|
+
}
|
|
101
|
+
function threadsHoldCurrentLayout(state) {
|
|
102
|
+
return state.sharedLayoutRevision === state.store.handles.layoutRevision;
|
|
103
|
+
}
|
|
104
|
+
function assignStorePartitions(state, resolve) {
|
|
105
|
+
for (let ordinal = 0; ordinal < state.store.slots; ordinal += 1) {
|
|
106
|
+
if (state.store.partitionOfOrdinal(ordinal) !== void 0) continue;
|
|
107
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
108
|
+
if (docId === void 0) continue;
|
|
109
|
+
const partitionId = resolve(docId);
|
|
110
|
+
if (partitionId === void 0) {
|
|
111
|
+
state.store.forgetPartition(docId);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
state.store.setPartition(docId, partitionId);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function emptyFieldBeforeRestore(state) {
|
|
118
|
+
state.store.clear();
|
|
119
|
+
state.tombstones.clear();
|
|
120
|
+
state.buffer.clear();
|
|
121
|
+
state.pendingLocations.clear();
|
|
122
|
+
adoptGraph(state, null);
|
|
123
|
+
state.osq?.clear();
|
|
124
|
+
forgetCalibration(state);
|
|
125
|
+
state.savedFiles = [];
|
|
126
|
+
state.savedSignature = null;
|
|
127
|
+
}
|
|
128
|
+
function releaseReplacedVectors(state) {
|
|
129
|
+
for (const ordinal of state.store.releaseReplacedVectors()) state.osq?.removeOrdinal(ordinal);
|
|
130
|
+
}
|
|
131
|
+
function adoptGraph(state, graph) {
|
|
132
|
+
if (state.hnsw !== null && state.hnsw !== graph) state.hnsw.retire();
|
|
133
|
+
if (graph === null && state.hnsw !== null) {
|
|
134
|
+
state.hnsw.clear();
|
|
135
|
+
}
|
|
136
|
+
state.hnsw = graph;
|
|
137
|
+
state.shuffledBuildCount = null;
|
|
138
|
+
state.compactedNodeCount = 0;
|
|
139
|
+
if (graph === null) return;
|
|
140
|
+
for (const docId of state.tombstones) {
|
|
141
|
+
if (graph.has(docId)) {
|
|
142
|
+
graph.markTombstone(docId);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function graphNeedsRebuild(state) {
|
|
147
|
+
const graph = state.hnsw;
|
|
148
|
+
if (graph === null) return false;
|
|
149
|
+
const removed = graph.tombstoneCount + state.compactedNodeCount;
|
|
150
|
+
const held = graph.size + removed;
|
|
151
|
+
if (held === 0) return false;
|
|
152
|
+
return removed / held > REBUILD_REMOVED_RATIO;
|
|
153
|
+
}
|
|
154
|
+
function yieldBuildTurn(state) {
|
|
155
|
+
return new Promise((resolve) => {
|
|
156
|
+
const takeTurn = () => {
|
|
157
|
+
clearTimeout(timer);
|
|
158
|
+
state.buildTurnWaiters.delete(takeTurn);
|
|
159
|
+
resolve();
|
|
160
|
+
};
|
|
161
|
+
const timer = setTimeout(takeTurn, 0);
|
|
162
|
+
state.buildTurnWaiters.set(takeTurn, performance.now());
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
function noBuildHistory() {
|
|
166
|
+
return {
|
|
167
|
+
shuffledBuildCount: null,
|
|
168
|
+
calibratedCount: null,
|
|
169
|
+
calibratedMeanLength: null,
|
|
170
|
+
nextDriftCheckAt: Number.POSITIVE_INFINITY,
|
|
171
|
+
shuffledRebuild: null
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function handOverBuildTurns(state) {
|
|
175
|
+
if (state.buildTurnWaiters.size === 0) return;
|
|
176
|
+
const [longestWaitingSince] = state.buildTurnWaiters.values();
|
|
177
|
+
if (longestWaitingSince === void 0 || performance.now() - longestWaitingSince < BUILD_TURN_HANDOVER_AFTER_MS)
|
|
178
|
+
return;
|
|
179
|
+
for (const takeTurn of [...state.buildTurnWaiters.keys()]) takeTurn();
|
|
180
|
+
}
|
|
181
|
+
function* allLiveDocIds(state) {
|
|
182
|
+
for (const docId of state.store.docIds()) {
|
|
183
|
+
if (state.tombstones.has(docId)) continue;
|
|
184
|
+
yield docId;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function ordinalFilterForPartitions(state, partitionIds) {
|
|
188
|
+
const filter = state.store.partitionFilter(partitionIds);
|
|
189
|
+
for (const docId of state.tombstones) {
|
|
190
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
191
|
+
if (ordinal === void 0) continue;
|
|
192
|
+
removeFromOrdinalFilter(filter, ordinal);
|
|
193
|
+
}
|
|
194
|
+
return filter;
|
|
195
|
+
}
|
|
196
|
+
function ordinalSpaceOf(state) {
|
|
197
|
+
const store = state.store;
|
|
198
|
+
const handles = store.handles;
|
|
199
|
+
const removed = new Uint8Array(handles.removed);
|
|
200
|
+
return {
|
|
201
|
+
fieldName: state.fieldName,
|
|
202
|
+
slots: store.slots,
|
|
203
|
+
header: handles.header,
|
|
204
|
+
layoutGeneration: Atomics.load(handles.header, STORE_ORDINAL_LAYOUT_GENERATION),
|
|
205
|
+
ordinalOf: (docId) => store.getOrdinal(docId),
|
|
206
|
+
holdsCurrentOrdinal: (ordinal) => store.holdsOrdinal(ordinal),
|
|
207
|
+
isRemoved: (ordinal) => ordinal < removed.length && removed[ordinal] === 1
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function filterForOptions(state, options) {
|
|
211
|
+
if (options.filterSubsets !== void 0) {
|
|
212
|
+
if (state.store.dimension === 0) return createOrdinalFilter(0);
|
|
213
|
+
return ordinalFilterForSubsets(options.filterSubsets, ordinalSpaceOf(state));
|
|
214
|
+
}
|
|
215
|
+
if (options.filterPartitions !== void 0) {
|
|
216
|
+
return ordinalFilterForPartitions(state, options.filterPartitions);
|
|
217
|
+
}
|
|
218
|
+
return void 0;
|
|
219
|
+
}
|
|
220
|
+
function liveOrdinals(state) {
|
|
221
|
+
const ordinals = [];
|
|
222
|
+
for (let ordinal = 0; ordinal < state.store.slots; ordinal++) {
|
|
223
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
224
|
+
if (docId !== void 0 && !state.tombstones.has(docId)) ordinals.push(ordinal);
|
|
225
|
+
}
|
|
226
|
+
return Int32Array.from(ordinals);
|
|
227
|
+
}
|
|
228
|
+
function forgetCalibration(state) {
|
|
229
|
+
state.calibratedCount = null;
|
|
230
|
+
state.calibratedMeanLength = null;
|
|
231
|
+
state.nextDriftCheckAt = Number.POSITIVE_INFINITY;
|
|
232
|
+
}
|
|
233
|
+
function adoptLoadedCalibration(state, calibratedCount, calibratedMeanLength) {
|
|
234
|
+
if (state.osq === null || !state.osq.isCalibrated()) return;
|
|
235
|
+
state.calibratedCount = calibratedCount;
|
|
236
|
+
state.calibratedMeanLength = calibratedMeanLength;
|
|
237
|
+
state.nextDriftCheckAt = Math.max(1, calibratedCount) * DRIFT_CHECK_GROWTH_FACTOR;
|
|
238
|
+
}
|
|
239
|
+
function recalibrateFromStore(state) {
|
|
240
|
+
if (state.osq === null) return;
|
|
241
|
+
const ordinals = liveOrdinals(state);
|
|
242
|
+
state.osq.recalibrate(ordinals);
|
|
243
|
+
state.calibratedCount = ordinals.length;
|
|
244
|
+
state.calibratedMeanLength = null;
|
|
245
|
+
state.nextDriftCheckAt = Math.max(1, ordinals.length) * DRIFT_CHECK_GROWTH_FACTOR;
|
|
246
|
+
}
|
|
247
|
+
function fieldHandlesOf(state, graph, searchable) {
|
|
248
|
+
return {
|
|
249
|
+
dimension: state.dimension,
|
|
250
|
+
quantization: state.osq === null ? "none" : state.quantizationMode,
|
|
251
|
+
metric: state.metric,
|
|
252
|
+
store: state.store.handles,
|
|
253
|
+
graph: graph === null ? null : graph.handles,
|
|
254
|
+
filterThreshold: state.filterThreshold,
|
|
255
|
+
searchable
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/vector/vector-index/persistence.ts
|
|
260
|
+
function recordFor(state, quantizer, docId) {
|
|
261
|
+
const record = quantizer.recordOf(docId);
|
|
262
|
+
if (record !== void 0) return record;
|
|
263
|
+
if (!state.store.has(docId)) return void 0;
|
|
264
|
+
quantizer.quantize(docId);
|
|
265
|
+
return quantizer.recordOf(docId);
|
|
266
|
+
}
|
|
267
|
+
function codedCentroid(state) {
|
|
268
|
+
const centroid = state.osq?.centroid ?? null;
|
|
269
|
+
if (centroid === null || state.store.handles.codeLayout === null || state.hnsw === null) return null;
|
|
270
|
+
return Array.from(centroid);
|
|
271
|
+
}
|
|
272
|
+
function sameCentroid(state, planned) {
|
|
273
|
+
const current = state.osq?.centroid ?? null;
|
|
274
|
+
if (current === null || current.length !== planned.length) return false;
|
|
275
|
+
for (let i = 0; i < planned.length; i++) {
|
|
276
|
+
if (current[i] !== planned[i]) return false;
|
|
277
|
+
}
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
function codesAgainstPlannedCentroid(quantizer, slotStride, centroid, vectors, ordinals) {
|
|
281
|
+
const planned = Float32Array.from(centroid);
|
|
282
|
+
const dimension = planned.length;
|
|
283
|
+
const scratch = createOsqScratch(dimension);
|
|
284
|
+
const records = new Uint8Array(ordinals.length * slotStride);
|
|
285
|
+
const trailer = new DataView(records.buffer, records.byteOffset, records.byteLength);
|
|
286
|
+
for (let i = 0; i < ordinals.length; i++) {
|
|
287
|
+
if (ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) continue;
|
|
288
|
+
const vector = vectors.subarray(i * dimension, (i + 1) * dimension);
|
|
289
|
+
const code = osqQuantize(vector, planned, quantizer.bits, quantizer.metric, scratch);
|
|
290
|
+
writeRecord(records, i * slotStride, code, quantizer.bits, trailer);
|
|
291
|
+
}
|
|
292
|
+
return { bits: quantizer.bits, centroid, records };
|
|
293
|
+
}
|
|
294
|
+
function partCodes(state, docIds, centroid, tombstonedAt, vectors, ordinals) {
|
|
295
|
+
const quantizer = state.osq;
|
|
296
|
+
const layout = state.store.handles.codeLayout;
|
|
297
|
+
if (centroid === null || quantizer === null || layout === null) return null;
|
|
298
|
+
if (!sameCentroid(state, centroid)) {
|
|
299
|
+
return codesAgainstPlannedCentroid(quantizer, layout.slotStride, centroid, vectors, ordinals);
|
|
300
|
+
}
|
|
301
|
+
const records = new Uint8Array(docIds.length * layout.slotStride);
|
|
302
|
+
for (let i = 0; i < docIds.length; i++) {
|
|
303
|
+
const tombstoned = tombstonedAt.get(i);
|
|
304
|
+
const record = tombstoned === void 0 ? recordFor(state, quantizer, docIds[i]) : quantizer.recordAtOrdinal(tombstoned);
|
|
305
|
+
if (record !== void 0) records.set(record, i * layout.slotStride);
|
|
306
|
+
}
|
|
307
|
+
return { bits: quantizer.bits, centroid, records };
|
|
308
|
+
}
|
|
309
|
+
function splitSequences(parts) {
|
|
310
|
+
const sequences = [];
|
|
311
|
+
let current = null;
|
|
312
|
+
for (const part of parts) {
|
|
313
|
+
if (current === null || part.part === 0) {
|
|
314
|
+
current = { docIds: [], graphs: [], codes: null, codesComplete: true };
|
|
315
|
+
sequences.push(current);
|
|
316
|
+
}
|
|
317
|
+
current.docIds.push(...part.docIds);
|
|
318
|
+
for (let index = 0; index < part.graphs.length; index++) {
|
|
319
|
+
const graph = part.graphs[index];
|
|
320
|
+
const held = current.graphs[index];
|
|
321
|
+
if (held === void 0) current.graphs[index] = { ...graph, nodes: [...graph.nodes] };
|
|
322
|
+
else held.nodes.push(...graph.nodes);
|
|
323
|
+
}
|
|
324
|
+
if (part.codes === null) {
|
|
325
|
+
if (part.docIds.length > 0) current.codesComplete = false;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (current.codes === null) {
|
|
329
|
+
current.codes = { bits: part.codes.bits, centroid: part.codes.centroid, records: part.codes.records };
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (current.codes.bits !== part.codes.bits) {
|
|
333
|
+
current.codesComplete = false;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
const joined = new Uint8Array(current.codes.records.length + part.codes.records.length);
|
|
337
|
+
joined.set(current.codes.records, 0);
|
|
338
|
+
joined.set(part.codes.records, current.codes.records.length);
|
|
339
|
+
current.codes = { ...current.codes, records: joined };
|
|
340
|
+
}
|
|
341
|
+
return sequences;
|
|
342
|
+
}
|
|
343
|
+
function restoreCodes(state, sequences) {
|
|
344
|
+
const quantizer = state.osq;
|
|
345
|
+
const layout = state.store.handles.codeLayout;
|
|
346
|
+
if (quantizer === null || layout === null) return;
|
|
347
|
+
const usable = sequences.filter((sequence) => sequence.docIds.length > 0);
|
|
348
|
+
const single = usable.length === 1 ? usable[0] : null;
|
|
349
|
+
if (single === null || single.codes === null || !single.codesComplete || single.codes.bits !== quantizer.bits) {
|
|
350
|
+
recalibrateFromStore(state);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
quantizer.restoreCentroid(Float32Array.from(single.codes.centroid));
|
|
354
|
+
const recordBytes = layout.slotStride;
|
|
355
|
+
for (let i = 0; i < single.docIds.length; i++) {
|
|
356
|
+
quantizer.restoreRecord(single.docIds[i], single.codes.records.subarray(i * recordBytes, (i + 1) * recordBytes));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function restoreGraphs(state, graphs) {
|
|
360
|
+
if (graphs.length === 0) {
|
|
361
|
+
for (const docId of state.store.docIds()) {
|
|
362
|
+
if (!state.tombstones.has(docId)) state.buffer.add(docId);
|
|
363
|
+
}
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const graphData = graphs[0];
|
|
367
|
+
const restoredHnsw = createHNSWIndex(
|
|
368
|
+
state.dimension,
|
|
369
|
+
state.store,
|
|
370
|
+
{
|
|
371
|
+
m: graphData.m ?? state.hnswConfig?.m,
|
|
372
|
+
efConstruction: graphData.efConstruction ?? state.hnswConfig?.efConstruction,
|
|
373
|
+
metric: graphData.metric ?? state.hnswConfig?.metric
|
|
374
|
+
},
|
|
375
|
+
state.osq ?? void 0
|
|
376
|
+
);
|
|
377
|
+
restoredHnsw.deserialize(graphData);
|
|
378
|
+
adoptGraph(state, restoredHnsw);
|
|
379
|
+
for (let i = 1; i < graphs.length; i++) {
|
|
380
|
+
for (const [nodeDocId] of graphs[i].nodes) {
|
|
381
|
+
if (!restoredHnsw.has(nodeDocId) && state.store.has(nodeDocId)) restoredHnsw.insertNode(nodeDocId);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
for (const docId of state.store.docIds()) {
|
|
385
|
+
if (state.tombstones.has(docId)) continue;
|
|
386
|
+
if (!restoredHnsw.has(docId)) state.buffer.add(docId);
|
|
387
|
+
}
|
|
388
|
+
if (state.buffer.size > restoredHnsw.size) {
|
|
389
|
+
adoptGraph(state, null);
|
|
390
|
+
state.buffer.clear();
|
|
391
|
+
for (const docId of state.store.docIds()) {
|
|
392
|
+
if (!state.tombstones.has(docId)) state.buffer.add(docId);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function insertPart(state, part) {
|
|
397
|
+
const dimension = state.dimension;
|
|
398
|
+
const vectors = bytesToVectors(part.vectors);
|
|
399
|
+
for (let i = 0; i < part.docIds.length; i++) {
|
|
400
|
+
state.store.insert(part.docIds[i], vectors.subarray(i * dimension, (i + 1) * dimension));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function deserialize(state, parts) {
|
|
404
|
+
for (const part of parts) {
|
|
405
|
+
if (part.dimension !== state.dimension) {
|
|
406
|
+
throw new NarsilError(
|
|
407
|
+
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
408
|
+
`Payload dimension ${part.dimension} does not match index dimension ${state.dimension}`,
|
|
409
|
+
{ expected: state.dimension, received: part.dimension }
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const sequences = splitSequences(parts);
|
|
414
|
+
const graphs = [];
|
|
415
|
+
for (const sequence of sequences) {
|
|
416
|
+
for (const graph of sequence.graphs) {
|
|
417
|
+
if (graph.nodes.length > 0) graphs.push(graph);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
emptyFieldBeforeRestore(state);
|
|
421
|
+
for (const part of parts) insertPart(state, part);
|
|
422
|
+
if (state.osq !== null && osqBitsOf(state.quantizationMode) !== null && graphs.length > 0) {
|
|
423
|
+
restoreCodes(state, sequences);
|
|
424
|
+
}
|
|
425
|
+
restoreGraphs(state, graphs);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// src/vector/vector-index/checkpoint-plan.ts
|
|
429
|
+
var ORDINAL_HOLDS_NO_NUMBER = -1;
|
|
430
|
+
function vectorsPerFile(dimension, codeBits) {
|
|
431
|
+
const recordBytes = codeBits === 1 || codeBits === 2 || codeBits === 4 || codeBits === 8 ? osqRecordBytes(dimension, codeBits) : 0;
|
|
432
|
+
const docIdBytes = VECTOR_FILE_STRING_HEADER_BYTES + MAX_DOC_ID_LENGTH * 3;
|
|
433
|
+
const perVector = dimension * 4 + recordBytes + docIdBytes;
|
|
434
|
+
const overhead = VECTOR_FILE_FIXED_OVERHEAD_BYTES + dimension * VECTOR_FILE_CENTROID_BYTES_PER_COMPONENT;
|
|
435
|
+
const fitting = Math.floor((MAX_FILE_BYTES - overhead) / perVector);
|
|
436
|
+
return Math.max(1, Math.min(VECTOR_FILE_MAX_VECTORS, fitting));
|
|
437
|
+
}
|
|
438
|
+
function fileCapacityOf(state) {
|
|
439
|
+
return vectorsPerFile(state.dimension, osqBitsOf(state.quantizationMode));
|
|
440
|
+
}
|
|
441
|
+
function calibrationOf(state) {
|
|
442
|
+
if (codedCentroid(state) === null) return null;
|
|
443
|
+
return Atomics.load(state.store.handles.header, STORE_CALIBRATION_GENERATION);
|
|
444
|
+
}
|
|
445
|
+
function signatureOf(state) {
|
|
446
|
+
return { revision: state.revision, graphSize: state.hnsw?.size ?? -1, calibration: calibrationOf(state) };
|
|
447
|
+
}
|
|
448
|
+
function sameSignature(a, b) {
|
|
449
|
+
return a !== null && a.revision === b.revision && a.graphSize === b.graphSize && a.calibration === b.calibration;
|
|
450
|
+
}
|
|
451
|
+
function registryMatches(state, listedKeys) {
|
|
452
|
+
const listed = listedKeys ?? [];
|
|
453
|
+
if (listed.length !== state.savedFiles.length) return false;
|
|
454
|
+
return state.savedFiles.every((file, index) => file.key === listed[index]);
|
|
455
|
+
}
|
|
456
|
+
function isLive(state, ordinal) {
|
|
457
|
+
if (ordinal === NO_LIVE_VECTOR_AT_POSITION) return false;
|
|
458
|
+
const docId = state.store.docIdForOrdinal(ordinal);
|
|
459
|
+
return docId !== void 0 && !state.tombstones.has(docId);
|
|
460
|
+
}
|
|
461
|
+
function isGraphTombstone(state, ordinal) {
|
|
462
|
+
return ordinal !== NO_LIVE_VECTOR_AT_POSITION && state.hnsw !== null && state.hnsw.holdsTombstone(ordinal);
|
|
463
|
+
}
|
|
464
|
+
function isCarried(state, ordinal) {
|
|
465
|
+
return isLive(state, ordinal) || isGraphTombstone(state, ordinal);
|
|
466
|
+
}
|
|
467
|
+
function carriedCountOf(state, file) {
|
|
468
|
+
let carried = 0;
|
|
469
|
+
for (let i = 0; i < file.ordinals.length; i++) if (isCarried(state, file.ordinals[i])) carried += 1;
|
|
470
|
+
return carried;
|
|
471
|
+
}
|
|
472
|
+
function deadBitsOf(state, file) {
|
|
473
|
+
return deadBitsWhere(file.ordinals.length, (position) => !isLive(state, file.ordinals[position]));
|
|
474
|
+
}
|
|
475
|
+
function filesWorthKeeping(state, listed, calibration, carriedCounts) {
|
|
476
|
+
const kept = [];
|
|
477
|
+
for (const file of listed) {
|
|
478
|
+
const carried = carriedCountOf(state, file);
|
|
479
|
+
const deadWithoutNodeShare = file.ordinals.length === 0 ? 1 : 1 - carried / file.ordinals.length;
|
|
480
|
+
if (file.calibration !== calibration || deadWithoutNodeShare > VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE) continue;
|
|
481
|
+
kept.push(file);
|
|
482
|
+
carriedCounts.push(carried);
|
|
483
|
+
}
|
|
484
|
+
return kept;
|
|
485
|
+
}
|
|
486
|
+
function absorbSmallerPartialFiles(kept, carriedCounts, vectorsToWrite, capacity) {
|
|
487
|
+
let pending = vectorsToWrite;
|
|
488
|
+
for (let index = kept.length - 1; index >= 0 && pending > 0; index--) {
|
|
489
|
+
if (kept[index].ordinals.length >= capacity) continue;
|
|
490
|
+
if (carriedCounts[index] > pending) return;
|
|
491
|
+
pending += carriedCounts[index];
|
|
492
|
+
kept.splice(index, 1);
|
|
493
|
+
carriedCounts.splice(index, 1);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
function ordinalsOutsideOf(state, kept) {
|
|
497
|
+
const slots = state.store.slots;
|
|
498
|
+
const saved = new Uint8Array(slots);
|
|
499
|
+
for (const file of kept) {
|
|
500
|
+
for (let i = 0; i < file.ordinals.length; i++) {
|
|
501
|
+
const ordinal = file.ordinals[i];
|
|
502
|
+
if (ordinal !== NO_LIVE_VECTOR_AT_POSITION && ordinal < slots) saved[ordinal] = 1;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
const outside = [];
|
|
506
|
+
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
507
|
+
if (saved[ordinal] === 0 && isCarried(state, ordinal)) outside.push(ordinal);
|
|
508
|
+
}
|
|
509
|
+
return outside;
|
|
510
|
+
}
|
|
511
|
+
function countOutside(state, carriedCounts) {
|
|
512
|
+
let held = 0;
|
|
513
|
+
for (const carried of carriedCounts) held += carried;
|
|
514
|
+
const tombstonedNodes = state.hnsw?.tombstoneCount ?? 0;
|
|
515
|
+
return Math.max(0, state.store.size - state.tombstones.size + tombstonedNodes - held);
|
|
516
|
+
}
|
|
517
|
+
function numberEveryVector(state, kept, written) {
|
|
518
|
+
let total = written.length;
|
|
519
|
+
for (const file of kept) total += file.ordinals.length;
|
|
520
|
+
const numberOfOrdinal = new Int32Array(state.store.slots).fill(ORDINAL_HOLDS_NO_NUMBER);
|
|
521
|
+
const ordinalOfNumber = new Int32Array(total).fill(NO_LIVE_VECTOR_AT_POSITION);
|
|
522
|
+
let number = 0;
|
|
523
|
+
for (const file of kept) {
|
|
524
|
+
for (let i = 0; i < file.ordinals.length; i++, number++) {
|
|
525
|
+
const ordinal = file.ordinals[i];
|
|
526
|
+
if (!isCarried(state, ordinal)) continue;
|
|
527
|
+
numberOfOrdinal[ordinal] = number;
|
|
528
|
+
ordinalOfNumber[number] = ordinal;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
for (const ordinal of written) {
|
|
532
|
+
numberOfOrdinal[ordinal] = number;
|
|
533
|
+
ordinalOfNumber[number] = ordinal;
|
|
534
|
+
number += 1;
|
|
535
|
+
}
|
|
536
|
+
return { numberOfOrdinal, ordinalOfNumber };
|
|
537
|
+
}
|
|
538
|
+
function withBuildHistory(state, graph) {
|
|
539
|
+
const shuffledBuildCount = state.shuffledBuildCount;
|
|
540
|
+
if (shuffledBuildCount === null) return graph;
|
|
541
|
+
const calibratedMeanLength = state.calibratedMeanLength;
|
|
542
|
+
if (calibratedMeanLength === null) return { ...graph, shuffledBuildCount };
|
|
543
|
+
return { ...graph, shuffledBuildCount, calibratedMeanLength };
|
|
544
|
+
}
|
|
545
|
+
function graphPartsOf(state, numbers) {
|
|
546
|
+
if (state.hnsw === null || numbers.ordinalOfNumber.length === 0) return null;
|
|
547
|
+
const parts = state.hnsw.serializeNumbered(
|
|
548
|
+
numbers.numberOfOrdinal,
|
|
549
|
+
numbers.ordinalOfNumber,
|
|
550
|
+
VECTOR_GRAPH_PART_VECTORS
|
|
551
|
+
);
|
|
552
|
+
return parts.map((graph) => ({
|
|
553
|
+
v: VECTOR_GRAPH_PART_PAYLOAD_VERSION,
|
|
554
|
+
fieldName: state.fieldName,
|
|
555
|
+
graphs: [withBuildHistory(state, graph)]
|
|
556
|
+
}));
|
|
557
|
+
}
|
|
558
|
+
function graphPartsWithoutNodes(parts, numbers) {
|
|
559
|
+
return parts.map((part, index) => ({
|
|
560
|
+
...part,
|
|
561
|
+
graphs: part.graphs.map((graph) => graphWithoutNodes(graph, index * VECTOR_GRAPH_PART_VECTORS, numbers))
|
|
562
|
+
}));
|
|
563
|
+
}
|
|
564
|
+
function plannedVectorOf(state, ordinal) {
|
|
565
|
+
const docId = state.store.docIdForOrdinal(ordinal) ?? state.store.view.docIdAt(ordinal) ?? "";
|
|
566
|
+
return { ordinal, docId, tombstoned: !isLive(state, ordinal) };
|
|
567
|
+
}
|
|
568
|
+
function readFile(state, planned, centroid) {
|
|
569
|
+
const dimension = state.dimension;
|
|
570
|
+
const vectors = new Float32Array(planned.length * dimension);
|
|
571
|
+
const ordinals = new Int32Array(planned.length).fill(NO_LIVE_VECTOR_AT_POSITION);
|
|
572
|
+
const tombstonedAt = /* @__PURE__ */ new Map();
|
|
573
|
+
const docIds = planned.map((vector) => vector.docId);
|
|
574
|
+
for (let i = 0; i < planned.length; i++) {
|
|
575
|
+
const { ordinal, docId, tombstoned } = planned[i];
|
|
576
|
+
const held = tombstoned || state.store.holdsOrdinal(ordinal) ? ordinal : state.store.getOrdinal(docId);
|
|
577
|
+
const entry = held === void 0 ? void 0 : state.store.entryForOrdinal(held);
|
|
578
|
+
if (held === void 0 || entry === void 0) continue;
|
|
579
|
+
vectors.set(entry.vector, i * dimension);
|
|
580
|
+
ordinals[i] = held;
|
|
581
|
+
if (tombstoned) tombstonedAt.set(i, held);
|
|
582
|
+
}
|
|
583
|
+
return {
|
|
584
|
+
payload: {
|
|
585
|
+
v: VECTOR_FILE_PAYLOAD_VERSION,
|
|
586
|
+
fieldName: state.fieldName,
|
|
587
|
+
dimension,
|
|
588
|
+
docIds,
|
|
589
|
+
codes: partCodes(state, docIds, centroid, tombstonedAt, vectors, ordinals),
|
|
590
|
+
vectors: vectorsToBytes(vectors)
|
|
591
|
+
},
|
|
592
|
+
ordinals,
|
|
593
|
+
dead: deadBitsWhere(planned.length, (i) => planned[i].tombstoned || ordinals[i] === NO_LIVE_VECTOR_AT_POSITION)
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
function planCheckpoint(state, listedKeys, rewritesGraph = false) {
|
|
597
|
+
const signature = signatureOf(state);
|
|
598
|
+
const calibration = signature.calibration;
|
|
599
|
+
const trusted = registryMatches(state, listedKeys);
|
|
600
|
+
const capacity = fileCapacityOf(state);
|
|
601
|
+
const carriedCounts = [];
|
|
602
|
+
const keptFiles = filesWorthKeeping(state, trusted ? state.savedFiles : [], calibration, carriedCounts);
|
|
603
|
+
absorbSmallerPartialFiles(keptFiles, carriedCounts, countOutside(state, carriedCounts), capacity);
|
|
604
|
+
const written = ordinalsOutsideOf(state, keptFiles);
|
|
605
|
+
const planned = written.map((ordinal) => plannedVectorOf(state, ordinal));
|
|
606
|
+
const centroid = codedCentroid(state);
|
|
607
|
+
const listedBefore = trusted && listedKeys !== null;
|
|
608
|
+
const unchanged = !rewritesGraph && listedBefore && sameSignature(state.savedSignature, signature) && written.length === 0;
|
|
609
|
+
let firstWrittenNumber = 0;
|
|
610
|
+
for (const file of keptFiles) firstWrittenNumber += file.ordinals.length;
|
|
611
|
+
const graphParts = unchanged ? null : graphPartsOf(state, numberEveryVector(state, keptFiles, written));
|
|
612
|
+
const vanished = /* @__PURE__ */ new Set();
|
|
613
|
+
let vanishedWhenStripped = 0;
|
|
614
|
+
let graphPartsToWrite = graphParts;
|
|
615
|
+
return {
|
|
616
|
+
unchanged,
|
|
617
|
+
keptFiles,
|
|
618
|
+
kept: keptFiles.map((file) => ({
|
|
619
|
+
id: file.id,
|
|
620
|
+
key: file.key,
|
|
621
|
+
count: file.ordinals.length,
|
|
622
|
+
dead: deadBitsOf(state, file)
|
|
623
|
+
})),
|
|
624
|
+
newFiles: Math.ceil(written.length / capacity),
|
|
625
|
+
get graphParts() {
|
|
626
|
+
if (graphParts !== null && vanished.size !== vanishedWhenStripped) {
|
|
627
|
+
graphPartsToWrite = graphPartsWithoutNodes(graphParts, vanished);
|
|
628
|
+
vanishedWhenStripped = vanished.size;
|
|
629
|
+
}
|
|
630
|
+
return graphPartsToWrite;
|
|
631
|
+
},
|
|
632
|
+
signature,
|
|
633
|
+
readNewFile(index) {
|
|
634
|
+
const start = index * capacity;
|
|
635
|
+
const end = start + capacity;
|
|
636
|
+
const file = readFile(state, planned.slice(start, end), centroid);
|
|
637
|
+
for (let i = 0; i < file.ordinals.length; i++) {
|
|
638
|
+
if (file.ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) vanished.add(firstWrittenNumber + start + i);
|
|
639
|
+
}
|
|
640
|
+
return file;
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
function recordCheckpoint(state, plan, written) {
|
|
645
|
+
if (plan.unchanged) return;
|
|
646
|
+
const calibration = plan.signature.calibration;
|
|
647
|
+
state.savedFiles = [...plan.keptFiles, ...written.map((file) => ({ ...file, calibration }))];
|
|
648
|
+
state.savedSignature = plan.signature;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
export { BUILD_CHUNK_SIZE, BUILD_RETRY_FIRST_DELAY_MS, BUILD_RETRY_MAX_DELAY_MS, DEFAULT_FILTER_THRESHOLD, DEFAULT_PROMOTION_THRESHOLD, DISK_ADOPTION_YIELD_INTERVAL, DRIFT_CHECK_GROWTH_FACTOR, ESTIMATED_MS_PER_TOMBSTONE, ESTIMATED_MS_PER_VECTOR_REBUILD, OSQ4_MIN_DIMENSION, RECALIBRATION_CHUNK_VECTORS, RECALIBRATION_DRIFT_RATIO, VECTOR_WORKER_COPIES_ALLOWED, WORKER_COPY_MIN_VECTORS, adoptGraph, adoptLoadedCalibration, allLiveDocIds, assignStorePartitions, deserialize, emptyFieldBeforeRestore, fieldHandlesOf, filterForOptions, graphNeedsRebuild, handOverBuildTurns, liveOrdinals, liveSize, noBuildHistory, ordinalFilterForSubsets, planCheckpoint, recalibrateFromStore, recordCheckpoint, releaseReplacedVectors, signatureOf, threadsHoldCurrentLayout, vectorsPerFile, yieldBuildTurn };
|