@delali/narsil 0.3.1-next.85 → 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.
Files changed (82) hide show
  1. package/dist/browser/chunk-3BXQPIXK.mjs +602 -0
  2. package/dist/browser/chunk-3ZRMZIH4.mjs +3959 -0
  3. package/dist/browser/chunk-BBDAYUCO.mjs +2703 -0
  4. package/dist/browser/chunk-FHFFWLEK.mjs +669 -0
  5. package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
  6. package/dist/browser/chunk-NQW2YXEX.mjs +346 -0
  7. package/dist/browser/chunk-TBMNPGFO.mjs +22910 -0
  8. package/dist/browser/durability-wiring-7RUN7LNS.mjs +3250 -0
  9. package/dist/browser/module-E25UDADM.mjs +2766 -0
  10. package/dist/browser/snapshot-A42DRERF.mjs +888 -0
  11. package/dist/browser/write-4U6Q26TJ.mjs +206 -0
  12. package/dist/{capabilities-CAdkp2P-.d.ts → capabilities-SJPttcgQ.d.ts} +2 -2
  13. package/dist/chunk-37CD3OKS.mjs +87 -0
  14. package/dist/chunk-AQU2QHSY.mjs +7247 -0
  15. package/dist/{chunk-OLXM65YH.mjs → chunk-AV4IJKD2.mjs} +1 -18
  16. package/dist/{chunk-4D6OH4EF.mjs → chunk-AXDAS2VA.mjs} +7 -5
  17. package/dist/{chunk-LS5IRM23.mjs → chunk-BVOJIYRV.mjs} +2 -25
  18. package/dist/chunk-C7SLA6CM.mjs +19 -0
  19. package/dist/{chunk-KJ7LB3TR.mjs → chunk-CVQWQVFX.mjs} +3 -22
  20. package/dist/chunk-DJHHF2L5.mjs +17 -0
  21. package/dist/chunk-FGMGUDHX.mjs +21 -0
  22. package/dist/chunk-IKST36MA.mjs +614 -0
  23. package/dist/{chunk-2453RJYK.mjs → chunk-J2BPXTD7.mjs} +167 -176
  24. package/dist/chunk-JFJBA3PD.mjs +522 -0
  25. package/dist/chunk-K4OWQ2M3.mjs +462 -0
  26. package/dist/chunk-L5ORED7V.mjs +25 -0
  27. package/dist/chunk-MKK4OZ5W.mjs +703 -0
  28. package/dist/chunk-MXS5HWIH.mjs +515 -0
  29. package/dist/chunk-P36MUFEJ.mjs +10 -0
  30. package/dist/chunk-PR4O7CAP.mjs +18 -0
  31. package/dist/{chunk-4VBG6PEH.mjs → chunk-PSUJCOSE.mjs} +3 -2
  32. package/dist/{chunk-ZCKPRCYI.mjs → chunk-Q5DC6WAB.mjs} +1 -1
  33. package/dist/{chunk-NFF5TDRG.mjs → chunk-QZN3MIJ3.mjs} +1 -16
  34. package/dist/chunk-R7OSDAFK.mjs +44 -0
  35. package/dist/chunk-RVM7AODE.mjs +73 -0
  36. package/dist/{chunk-XYDP7CCJ.mjs → chunk-SYRFSF6V.mjs} +6 -4
  37. package/dist/{chunk-SREPGC6O.mjs → chunk-T6CNO72J.mjs} +1 -1
  38. package/dist/chunk-T7DMUVTL.mjs +1714 -0
  39. package/dist/chunk-UKSUENHK.mjs +997 -0
  40. package/dist/{chunk-NIWDQTZB.mjs → chunk-ULRPNYAG.mjs} +1220 -2311
  41. package/dist/{chunk-GNYXCLJS.mjs → chunk-URGLSNKR.mjs} +4 -3
  42. package/dist/chunk-WQSHDZ7Z.mjs +631 -0
  43. package/dist/{chunk-RCSXJCI5.mjs → chunk-XBXKPWMR.mjs} +20 -2664
  44. package/dist/{chunk-RM6FRYEC.mjs → chunk-XSZVS4S3.mjs} +3 -2
  45. package/dist/chunk-Y2F66IQJ.mjs +74 -0
  46. package/dist/chunk-YCJLVNWR.mjs +262 -0
  47. package/dist/{client-CKUtkLz3.d.ts → client-BMK53IR2.d.ts} +3 -3
  48. package/dist/client.d.ts +8 -8
  49. package/dist/{config-CSDEPraV.d.ts → config-BUR-7eVQ.d.ts} +2 -2
  50. package/dist/{config-BuIkeM5C.d.ts → config-UJx1dyX1.d.ts} +2 -2
  51. package/dist/direct-executor-LD6JW57R.mjs +18 -0
  52. package/dist/distribution/coordinator/etcd.d.ts +2 -2
  53. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  54. package/dist/distribution.d.ts +8 -8
  55. package/dist/distribution.mjs +30 -15
  56. package/dist/durability-wiring-5S32KDDC.mjs +2797 -0
  57. package/dist/{engine-DMNZJAUq.d.ts → engine-9X6W5naj.d.ts} +2 -2
  58. package/dist/{index-DzONZUKE.d.ts → index-vCMu8OeQ.d.ts} +1 -1
  59. package/dist/index.browser.mjs +2 -38121
  60. package/dist/index.d.ts +23 -9
  61. package/dist/index.mjs +18 -14
  62. package/dist/{memory-BPKNx9Tl.d.ts → memory-CV9NAxLY.d.ts} +1 -1
  63. package/dist/module-WIDYIJLY.mjs +1819 -0
  64. package/dist/partial-words.mjs +5 -3
  65. package/dist/pattern.mjs +3 -2
  66. package/dist/persistence/durability/checkpoint-worker.d.ts +1 -1
  67. package/dist/persistence/durability/checkpoint-worker.mjs +23 -13
  68. package/dist/react.d.ts +7 -7
  69. package/dist/{schema-DgyDijhf.d.ts → schema-DtsgmNfU.d.ts} +1 -1
  70. package/dist/server.d.ts +10 -10
  71. package/dist/server.mjs +14 -9
  72. package/dist/snapshot-6BYSUPWL.mjs +469 -0
  73. package/dist/{tasks-DGyz586z.d.ts → tasks-BGaZJyoY.d.ts} +1 -1
  74. package/dist/{thread-EUUNJNKU.mjs → thread-GS65E6RM.mjs} +12 -9
  75. package/dist/vector/search-worker.d.ts +30 -3
  76. package/dist/vector/search-worker.mjs +6 -2
  77. package/dist/workers/entry.mjs +5 -3
  78. package/dist/write-HLEZTL2L.mjs +233 -0
  79. package/package.json +11 -7
  80. package/dist/chunk-67MQF5CV.mjs +0 -15198
  81. package/dist/direct-executor-NY7ZTZIH.mjs +0 -19
  82. package/dist/simd-DW5AtJ6B.d.ts +0 -41
@@ -1,17 +1,14 @@
1
- import { createGrowingPartialWords, requirePatternSearchFor, requirePartialWordSearchFor, buildPartialWordTableFrom, patternIndexOf, encodePartialSpellingsOf, countPartialSpelling, joinPatternWorkMeter, countsWithoutScores, fanOutMatchCount, fanOutQuery, sanitizeGlobalStats, expandPartialWords, computeFacets, cloneProjected, projectionKeepsField, setNestedValue, fnv1a, partialSpellingsOfDocuments, createFrozenPartialWords, requirePatternSearch, PATTERN_HOST, validatedFilterOf, everyValueFacetConfig, mergeFacets, kWayMerge, polygonContainment, polygonBoxes, radiusBoxes, locationCode, undeclaredFilterField, coveringRanges } from './chunk-XYDP7CCJ.mjs';
2
- import { getLanguage, detectRuntime } from './chunk-OLXM65YH.mjs';
3
- import { MAX_DOC_ID_LENGTH } from './chunk-ZCKPRCYI.mjs';
4
- import { SCRATCH_SLOTS_PER_THREAD_POOL, osqRecordBytes, openSharedVectorField, osqBitsOf, createVectorStore, DISK_STORAGE_BLOCK_BYTES, createOsqQuantizer, STORE_UNPLACED_COUNT, STORE_REMOVED_COUNT, STORE_LIVE_COUNT, ordinalFilterValues, UNPLACED_FLAG_CHUNK_ORDINALS, magnitude, graphWithoutNodes, createHNSWIndex, nativeStoreScratchBytes, nativeScoresOf, toScore, ordinalFilterHas, createOrdinalFilter, addToOrdinalFilter, removeFromOrdinalFilter, euclideanDistance, dotProduct, cosineSimilarityWithMagnitudes, topLayerInParts, STORE_CALIBRATION_GENERATION, toDistance, sharedMemoryAvailable, requireWholeNeighbourLists, GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER, GRAPH_BUILD_DISPATCH_CHUNK, releaseLocksHeldBy, VECTOR_SEARCH_TIMEOUT_MS, VECTOR_SEARCH_LOAD_TIMEOUT_MS, VECTOR_INSERT_TIMEOUT_MS, VECTOR_SCRATCH_SLOTS } from './chunk-NIWDQTZB.mjs';
5
- import { noteFallback } from './chunk-NFF5TDRG.mjs';
6
- import { GrowableUint32, bitsetHas, createBitSet, bitsetSet, bitsetOrInto, bitsetAndNotInto, bitsetAndInto, bitsetFromSet, bitsetIsEmpty } from './chunk-SREPGC6O.mjs';
1
+ import { createGrowingPartialWords, requirePatternSearchFor, requirePartialWordSearchFor, buildPartialWordTableFrom, patternIndexOf, encodePartialSpellingsOf, countPartialSpelling, joinPatternWorkMeter, countsWithoutScores, fanOutMatchCount, fanOutQuery, sanitizeGlobalStats, expandPartialWords, computeFacets, cloneProjected, projectionKeepsField, setNestedValue, fnv1a, partialSpellingsOfDocuments, createFrozenPartialWords, requirePatternSearch, PATTERN_HOST, validatedFilterOf, everyValueFacetConfig, mergeFacets, kWayMerge, polygonContainment, polygonBoxes, radiusBoxes, locationCode, undeclaredFilterField, coveringRanges } from './chunk-SYRFSF6V.mjs';
2
+ import { getLanguage } from './chunk-PR4O7CAP.mjs';
3
+ import { GrowableUint32, bitsetHas, createBitSet, bitsetSet, bitsetOrInto, bitsetAndNotInto, bitsetAndInto, bitsetFromSet, bitsetIsEmpty } from './chunk-T6CNO72J.mjs';
7
4
  import { GEO_UNIT_METRES } from './chunk-WQBCBKY3.mjs';
8
- import { tokenizeOptions, getNestedValue, visitTokens, expandTermPrefix, suggestDisplayTerms, textFieldsChanged, sameFields, codePointOrder, encodeStringBlob, encodeLookupKey, SEGMENT_TOKEN_TABLE_START, SEGMENT_POSTING_LOG_START, SEGMENT_POSITION_POOL_START, SEGMENT_FIELD_SCRATCH_START, getFlatSchema, tokenize, SCORE_BUFFER_INITIAL_TOUCHED_CAPACITY, PRUNING_REJECTION_SLACK, getOrCreateFieldNameIndex, getFieldValueByInternalId, SORT_COLUMN_MINIMUM_REBUILD_THRESHOLD, TARGET_BLOCK_ENTRIES, SORT_COLUMN_REBUILD_FRACTION_SHIFT, partitionDocumentFrequency } from './chunk-4VBG6PEH.mjs';
9
- import { schemaFieldsOf, partialFieldsOf, validateDocument, validateDocumentStrict, encodeDocument, extractVectorFieldsFromSchema, VINCENTY_CONVERGENCE_THRESHOLD, EARTH_RADIUS_METERS, VINCENTY_MAX_ITERATIONS, DEG_TO_RAD } from './chunk-KJ7LB3TR.mjs';
10
- import { MAX_M, EARLIER_RELEASE_MIN_M, MIN_M } from './chunk-KEHK2O6P.mjs';
5
+ import { tokenizeOptions, getNestedValue, visitTokens, expandTermPrefix, suggestDisplayTerms, textFieldsChanged, sameFields, codePointOrder, encodeStringBlob, encodeLookupKey, SEGMENT_TOKEN_TABLE_START, SEGMENT_POSTING_LOG_START, SEGMENT_POSITION_POOL_START, SEGMENT_FIELD_SCRATCH_START, getFlatSchema, tokenize, SCORE_BUFFER_INITIAL_TOUCHED_CAPACITY, PRUNING_REJECTION_SLACK, getOrCreateFieldNameIndex, getFieldValueByInternalId, SORT_COLUMN_MINIMUM_REBUILD_THRESHOLD, TARGET_BLOCK_ENTRIES, SORT_COLUMN_REBUILD_FRACTION_SHIFT, partitionDocumentFrequency } from './chunk-PSUJCOSE.mjs';
6
+ import { schemaFieldsOf, partialFieldsOf, validateDocument, validateDocumentStrict, encodeDocument, extractVectorFieldsFromSchema, VINCENTY_CONVERGENCE_THRESHOLD, EARTH_RADIUS_METERS, VINCENTY_MAX_ITERATIONS, DEG_TO_RAD } from './chunk-CVQWQVFX.mjs';
11
7
  import { fuzzyPrefixOf, boundedLevenshtein } from './chunk-GES7U2PM.mjs';
12
- import { isWordIndexedBase, parsedTypeOf, isListBase } from './chunk-LS5IRM23.mjs';
8
+ import { isWordIndexedBase, parsedTypeOf, isListBase } from './chunk-BVOJIYRV.mjs';
13
9
  import { toReducedSortValue, readSortField, defaultSortMode, compareComparableValues, truncateSortString, SORT_VALUE_MAX_CODE_POINTS } from './chunk-R6A773WK.mjs';
14
10
  import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
11
+ import { loadedVectorCode, loadVectorCode, loadVectorCodeFor, requireLoadedVectorCode } from './chunk-R7OSDAFK.mjs';
15
12
  import { NarsilError, ErrorCodes } from './chunk-TPJU7NDJ.mjs';
16
13
  import { totalmem } from 'os';
17
14
  import { getHeapStatistics } from 'v8';
@@ -2574,7 +2571,7 @@ function withinBounds(value, bounds) {
2574
2571
  }
2575
2572
  return true;
2576
2573
  }
2577
- function selectTextRange(column, requested, capacity, isLive2) {
2574
+ function selectTextRange(column, requested, capacity, isLive) {
2578
2575
  const bounds = {
2579
2576
  ...requested,
2580
2577
  lower: requested.lower === null ? null : truncateSortString(requested.lower),
@@ -2595,7 +2592,7 @@ function selectTextRange(column, requested, capacity, isLive2) {
2595
2592
  }
2596
2593
  for (let position = start; position < end; position++) {
2597
2594
  const internalId = ordered[position];
2598
- if (!column.isDirty(internalId) && isLive2(internalId)) bitsetSet(result, internalId);
2595
+ if (!column.isDirty(internalId) && isLive(internalId)) bitsetSet(result, internalId);
2599
2596
  }
2600
2597
  for (const internalId of column.dirtyStream().present) {
2601
2598
  if (withinBounds(column.valueOf(internalId), bounds)) bitsetSet(result, internalId);
@@ -6016,2457 +6013,6 @@ function createPartitionRouter() {
6016
6013
  return { route, routeBatch };
6017
6014
  }
6018
6015
 
6019
- // src/vector/vector-index/constants.ts
6020
- var DEFAULT_PROMOTION_THRESHOLD = 1024;
6021
- var DEFAULT_FILTER_THRESHOLD = 0.03;
6022
- var ESTIMATED_MS_PER_TOMBSTONE = 0.05;
6023
- var ESTIMATED_MS_PER_VECTOR_REBUILD = 0.15;
6024
- var WORKER_COPY_MIN_VECTORS = 1024;
6025
- var BUILD_CHUNK_SIZE = 100;
6026
- var REBUILD_REMOVED_RATIO = 0.2;
6027
- var VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE = 0.2;
6028
- var VECTOR_FILE_FIXED_OVERHEAD_BYTES = 65536;
6029
- var VECTOR_FILE_STRING_HEADER_BYTES = 5;
6030
- var VECTOR_FILE_CENTROID_BYTES_PER_COMPONENT = 9;
6031
- var OSQ4_MIN_DIMENSION = 384;
6032
- var DISK_ADOPTION_YIELD_INTERVAL = 4096;
6033
-
6034
- // src/vector/vector-index/shared.ts
6035
- var VECTOR_WORKER_COPIES_ALLOWED = { enabled: true };
6036
- function liveSize(state) {
6037
- return state.store.size - state.tombstones.size;
6038
- }
6039
- function threadsHoldCurrentLayout(state) {
6040
- return state.sharedLayoutRevision === state.store.handles.layoutRevision;
6041
- }
6042
- function assignStorePartitions(state, resolve) {
6043
- for (let ordinal = 0; ordinal < state.store.slots; ordinal += 1) {
6044
- if (state.store.partitionOfOrdinal(ordinal) !== void 0) continue;
6045
- const docId = state.store.docIdForOrdinal(ordinal);
6046
- if (docId === void 0) continue;
6047
- const partitionId = resolve(docId);
6048
- if (partitionId === void 0) {
6049
- state.store.forgetPartition(docId);
6050
- continue;
6051
- }
6052
- state.store.setPartition(docId, partitionId);
6053
- }
6054
- }
6055
- function emptyFieldBeforeRestore(state) {
6056
- state.store.clear();
6057
- state.tombstones.clear();
6058
- state.buffer.clear();
6059
- state.pendingLocations.clear();
6060
- adoptGraph(state, null);
6061
- state.osq?.clear();
6062
- state.savedFiles = [];
6063
- state.savedSignature = null;
6064
- }
6065
- function releaseReplacedVectors(state) {
6066
- for (const ordinal of state.store.releaseReplacedVectors()) state.osq?.removeOrdinal(ordinal);
6067
- }
6068
- function adoptGraph(state, graph) {
6069
- if (state.hnsw !== null && state.hnsw !== graph) state.hnsw.retire();
6070
- if (graph === null && state.hnsw !== null) {
6071
- state.hnsw.clear();
6072
- }
6073
- state.hnsw = graph;
6074
- state.compactedNodeCount = 0;
6075
- if (graph === null) return;
6076
- for (const docId of state.tombstones) {
6077
- if (graph.has(docId)) {
6078
- graph.markTombstone(docId);
6079
- }
6080
- }
6081
- }
6082
- function graphNeedsRebuild(state) {
6083
- const graph = state.hnsw;
6084
- if (graph === null) return false;
6085
- const removed = graph.tombstoneCount + state.compactedNodeCount;
6086
- const held = graph.size + removed;
6087
- if (held === 0) return false;
6088
- return removed / held > REBUILD_REMOVED_RATIO;
6089
- }
6090
- function yieldToEventLoop() {
6091
- return new Promise((resolve) => setTimeout(resolve, 0));
6092
- }
6093
- function* allLiveDocIds(state) {
6094
- for (const docId of state.store.docIds()) {
6095
- if (state.tombstones.has(docId)) continue;
6096
- yield docId;
6097
- }
6098
- }
6099
- function ordinalFilterForPartitions(state, partitionIds) {
6100
- const filter = state.store.partitionFilter(partitionIds);
6101
- for (const docId of state.tombstones) {
6102
- const ordinal = state.store.getOrdinal(docId);
6103
- if (ordinal === void 0) continue;
6104
- removeFromOrdinalFilter(filter, ordinal);
6105
- }
6106
- return filter;
6107
- }
6108
- function filterForOptions(state, options) {
6109
- if (options.filterDocIds !== void 0) {
6110
- return ordinalFilterForDocIds(state, options.filterDocIds);
6111
- }
6112
- if (options.filterPartitions !== void 0) {
6113
- return ordinalFilterForPartitions(state, options.filterPartitions);
6114
- }
6115
- return void 0;
6116
- }
6117
- function ordinalFilterForDocIds(state, docIds) {
6118
- const filter = createOrdinalFilter(state.store.slots);
6119
- for (const docId of docIds) {
6120
- if (state.tombstones.has(docId)) continue;
6121
- const ordinal = state.store.getOrdinal(docId);
6122
- if (ordinal === void 0) continue;
6123
- addToOrdinalFilter(filter, ordinal);
6124
- }
6125
- return filter;
6126
- }
6127
- function liveOrdinals(state) {
6128
- const ordinals = [];
6129
- for (let ordinal = 0; ordinal < state.store.slots; ordinal++) {
6130
- const docId = state.store.docIdForOrdinal(ordinal);
6131
- if (docId !== void 0 && !state.tombstones.has(docId)) ordinals.push(ordinal);
6132
- }
6133
- return Int32Array.from(ordinals);
6134
- }
6135
- function calibrateQuantizer(state) {
6136
- if (state.osq === null || state.store.size === 0) return;
6137
- state.osq.calibrate(liveOrdinals(state));
6138
- }
6139
- function recalibrateFromStore(state) {
6140
- if (state.osq === null) return;
6141
- state.osq.recalibrate(liveOrdinals(state));
6142
- }
6143
- function fieldHandlesOf(state, graph, searchable) {
6144
- return {
6145
- dimension: state.dimension,
6146
- quantization: state.osq === null ? "none" : state.quantizationMode,
6147
- metric: state.metric,
6148
- store: state.store.handles,
6149
- graph: graph === null ? null : graph.handles,
6150
- filterThreshold: state.filterThreshold,
6151
- searchable
6152
- };
6153
- }
6154
-
6155
- // src/workers/constants.ts
6156
- var WORKER_YOUNG_GENERATION_MB = 24;
6157
- var MIN_WORKER_COUNT = 2;
6158
- var MAX_WORKER_COUNT = 8;
6159
- var FALLBACK_CPU_COUNT = 4;
6160
- var MIN_CORES_FOR_SEVERAL_REQUEST_THREADS = 3;
6161
- var DEFAULT_BACKPRESSURE_LIMIT = 100;
6162
- var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
6163
- var SHUTDOWN_TIMEOUT_MS = 5e3;
6164
- var CONSECUTIVE_TIMEOUTS_BEFORE_DEATH = 3;
6165
-
6166
- // src/workers/worker-count.ts
6167
- function detectCpuCount() {
6168
- try {
6169
- if (navigator?.hardwareConcurrency) {
6170
- return navigator.hardwareConcurrency;
6171
- }
6172
- if (typeof process !== "undefined") {
6173
- const ap = process.availableParallelism;
6174
- if (typeof ap === "function") {
6175
- return ap();
6176
- }
6177
- }
6178
- } catch {
6179
- return FALLBACK_CPU_COUNT;
6180
- }
6181
- return FALLBACK_CPU_COUNT;
6182
- }
6183
- function resolveWorkerCount(requested) {
6184
- if (requested !== void 0 && requested > 0) {
6185
- return Math.min(requested, SCRATCH_SLOTS_PER_THREAD_POOL);
6186
- }
6187
- return Math.max(MIN_WORKER_COUNT, Math.min(MAX_WORKER_COUNT, detectCpuCount() - 1));
6188
- }
6189
- function resolveRequestThreadCount(requested) {
6190
- if (detectCpuCount() < MIN_CORES_FOR_SEVERAL_REQUEST_THREADS) return 1;
6191
- return resolveWorkerCount(requested);
6192
- }
6193
- function splitWorkerBudget(total) {
6194
- const keyword = Math.ceil(total / 2);
6195
- return { keyword, vector: total - keyword };
6196
- }
6197
-
6198
- // src/workers/node-worker.ts
6199
- async function spawnNodeWorker(entryPoint, resourceLimits) {
6200
- try {
6201
- const workerThreads = await import('worker_threads');
6202
- return new workerThreads.Worker(entryPoint, { resourceLimits });
6203
- } catch {
6204
- return null;
6205
- }
6206
- }
6207
- async function nodeThreadId() {
6208
- try {
6209
- const workerThreads = await import('worker_threads');
6210
- return workerThreads.threadId;
6211
- } catch {
6212
- return 0;
6213
- }
6214
- }
6215
-
6216
- // src/workers/entry-point.ts
6217
- var DIST_SEGMENT = "/dist/";
6218
- function resolveWorkerEntry(moduleUrl, sourceDirectory, distEntry) {
6219
- const distIndex = moduleUrl.lastIndexOf(DIST_SEGMENT);
6220
- const entry = distIndex !== -1 ? new URL(distEntry, moduleUrl.slice(0, distIndex + DIST_SEGMENT.length)).href : sourceDirectory.test(moduleUrl) ? moduleUrl.replace(sourceDirectory, `${DIST_SEGMENT}${distEntry}`) : null;
6221
- return entry === moduleUrl ? null : entry;
6222
- }
6223
- function missingWorkerEntry(moduleUrl) {
6224
- return new NarsilError(
6225
- ErrorCodes.CONFIG_INVALID,
6226
- `The engine finds no worker entry beside its own module at "${moduleUrl}", which happens where a bundler folds @delali/narsil into an application bundle. Keep @delali/narsil outside the bundle, or set workers.enabled to false`,
6227
- { moduleUrl }
6228
- );
6229
- }
6230
-
6231
- // src/workers/resource-limits.ts
6232
- function workerResourceLimits() {
6233
- return { maxYoungGenerationSizeMb: WORKER_YOUNG_GENERATION_MB };
6234
- }
6235
-
6236
- // src/vector/search-pool/spawn.ts
6237
- function resolveWorkerEntryPoint() {
6238
- return resolveWorkerEntry(import.meta.url, /\/src\/vector\/search-pool\/[^/]+$/, "vector/search-worker.mjs");
6239
- }
6240
- async function spawnWorker(entryPoint) {
6241
- if (entryPoint === null) return null;
6242
- const runtime = detectRuntime();
6243
- if (runtime.supportsWorkerThreads) {
6244
- try {
6245
- return await spawnNodeWorker(new URL(entryPoint), workerResourceLimits());
6246
- } catch {
6247
- return null;
6248
- }
6249
- }
6250
- if (runtime.supportsWebWorkers) {
6251
- try {
6252
- const WorkerCtor = globalThis.Worker;
6253
- if (typeof WorkerCtor !== "function") return null;
6254
- return new WorkerCtor(entryPoint, { type: "module" });
6255
- } catch {
6256
- return null;
6257
- }
6258
- }
6259
- return null;
6260
- }
6261
- function listen(worker, handler) {
6262
- if (typeof worker.on === "function") {
6263
- worker.on("message", handler);
6264
- return;
6265
- }
6266
- worker.addEventListener?.("message", (event) => {
6267
- handler(event.data);
6268
- });
6269
- }
6270
- function listenForFailure(worker, handler) {
6271
- if (typeof worker.on === "function") {
6272
- worker.on("error", (err) => handler(err instanceof Error ? err : new Error(String(err))));
6273
- worker.on("exit", () => handler(new Error("Vector search worker exited")));
6274
- return;
6275
- }
6276
- worker.addEventListener?.("error", () => handler(new Error("Vector search worker failed")));
6277
- }
6278
-
6279
- // src/vector/search-pool/index.ts
6280
- function searchPoolScratchSlot(index) {
6281
- return VECTOR_SCRATCH_SLOTS - 1 - index;
6282
- }
6283
- function graphSearchOptionsOf(options) {
6284
- return {
6285
- ...options.filter !== void 0 ? { filter: options.filter } : {},
6286
- ...options.efSearch !== void 0 ? { efSearch: options.efSearch } : {},
6287
- ...options.oversample !== void 0 ? { oversample: options.oversample } : {}
6288
- };
6289
- }
6290
- async function createVectorSearchPool(requestedCount) {
6291
- const entryPoint = resolveWorkerEntryPoint();
6292
- const count = resolveWorkerCount(requestedCount);
6293
- const slots = [];
6294
- const sharedFields = /* @__PURE__ */ new Map();
6295
- for (let i = 0; i < count; i++) {
6296
- const worker = await spawnWorker(entryPoint);
6297
- if (worker === null) break;
6298
- const slot = {
6299
- worker,
6300
- scratchSlot: searchPoolScratchSlot(i),
6301
- pending: /* @__PURE__ */ new Map(),
6302
- alive: true,
6303
- outstanding: 0
6304
- };
6305
- listen(worker, (raw) => {
6306
- const message = raw;
6307
- const key = message.requestId;
6308
- if (typeof key !== "string") return;
6309
- const waiting = slot.pending.get(key);
6310
- if (!waiting) return;
6311
- slot.pending.delete(key);
6312
- clearTimeout(waiting.timer);
6313
- waiting.resolve(message);
6314
- });
6315
- listenForFailure(worker, (err) => {
6316
- slot.alive = false;
6317
- for (const handles of sharedFields.values()) {
6318
- if (handles.graph !== null) releaseLocksHeldBy(handles.graph, slot.scratchSlot);
6319
- }
6320
- for (const [, waiting] of slot.pending) {
6321
- clearTimeout(waiting.timer);
6322
- waiting.reject(err);
6323
- }
6324
- slot.pending.clear();
6325
- });
6326
- slots.push(slot);
6327
- }
6328
- if (slots.length === 0) return null;
6329
- let requestCounter = 0;
6330
- function send(slot, key, message, timeoutMs) {
6331
- return new Promise((resolve, reject) => {
6332
- if (!slot.alive) {
6333
- reject(new Error("Vector search worker is not running"));
6334
- return;
6335
- }
6336
- const timer = setTimeout(() => {
6337
- slot.pending.delete(key);
6338
- reject(new Error(`Vector search worker did not answer within ${timeoutMs}ms`));
6339
- }, timeoutMs);
6340
- slot.pending.set(key, { resolve, reject, timer });
6341
- try {
6342
- slot.worker.postMessage(message);
6343
- } catch (err) {
6344
- slot.pending.delete(key);
6345
- clearTimeout(timer);
6346
- reject(err instanceof Error ? err : new Error(String(err)));
6347
- }
6348
- });
6349
- }
6350
- function refusalOf(message) {
6351
- return message.type === "error" ? message.message : `the worker answered "${message.type}"`;
6352
- }
6353
- function pickSlot() {
6354
- let slot = null;
6355
- for (const candidate of slots) {
6356
- if (!candidate.alive) continue;
6357
- if (slot === null || candidate.outstanding < slot.outstanding) slot = candidate;
6358
- if (slot.outstanding === 0) break;
6359
- }
6360
- return slot;
6361
- }
6362
- async function sendBusy(slot, request, timeoutMs) {
6363
- slot.outstanding += 1;
6364
- try {
6365
- return await send(slot, request.requestId, request, timeoutMs);
6366
- } finally {
6367
- slot.outstanding -= 1;
6368
- }
6369
- }
6370
- async function broadcast(build, timeoutMs) {
6371
- const outcomes = await Promise.allSettled(
6372
- slots.map((slot) => {
6373
- requestCounter += 1;
6374
- const requestId = `${requestCounter}`;
6375
- return send(slot, requestId, build(slot, requestId), timeoutMs);
6376
- })
6377
- );
6378
- return outcomes.every(
6379
- (outcome) => outcome.status === "fulfilled" && outcome.value.type === "ack"
6380
- );
6381
- }
6382
- return {
6383
- get workerCount() {
6384
- return slots.filter((slot) => slot.alive).length;
6385
- },
6386
- load(handle, snapshot) {
6387
- return broadcast(
6388
- (_slot, requestId) => ({ type: "load", requestId, handle, snapshot }),
6389
- VECTOR_SEARCH_LOAD_TIMEOUT_MS
6390
- );
6391
- },
6392
- async loadShared(handle, handles) {
6393
- sharedFields.set(handle, handles);
6394
- const loaded = await broadcast(
6395
- (slot, requestId) => ({ type: "loadShared", requestId, handle, scratchSlot: slot.scratchSlot, handles }),
6396
- VECTOR_SEARCH_LOAD_TIMEOUT_MS
6397
- );
6398
- if (!loaded) sharedFields.delete(handle);
6399
- return loaded;
6400
- },
6401
- async drop(handle) {
6402
- sharedFields.delete(handle);
6403
- await broadcast((_slot, requestId) => ({ type: "drop", requestId, handle }), VECTOR_SEARCH_TIMEOUT_MS);
6404
- },
6405
- async insertOrdinals(handle, ordinals) {
6406
- const slot = pickSlot();
6407
- if (slot === null) return null;
6408
- requestCounter += 1;
6409
- const request = { type: "insertOrdinals", requestId: `${requestCounter}`, handle, ordinals };
6410
- const message = await sendBusy(slot, request, VECTOR_INSERT_TIMEOUT_MS);
6411
- if (message.type !== "inserted") {
6412
- console.warn("A vector worker placed no vectors in the shared graph:", refusalOf(message));
6413
- return null;
6414
- }
6415
- return message.outcome;
6416
- },
6417
- async search(handle, query, k, metric, minSimilarity, options = {}) {
6418
- const slot = pickSlot();
6419
- if (slot === null) throw new Error("No vector search worker is running");
6420
- requestCounter += 1;
6421
- const request = {
6422
- type: "search",
6423
- requestId: `${requestCounter}`,
6424
- handle,
6425
- query,
6426
- k,
6427
- metric,
6428
- minSimilarity,
6429
- ...graphSearchOptionsOf(options)
6430
- };
6431
- const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
6432
- if (message.type === "error") throw new Error(message.message);
6433
- if (message.type !== "result") throw new Error("Vector search worker returned an unexpected message");
6434
- const results = new Array(message.docIds.length);
6435
- for (let i = 0; i < message.docIds.length; i++) {
6436
- results[i] = { docId: message.docIds[i], score: message.scores[i] };
6437
- }
6438
- return results;
6439
- },
6440
- async searchOrdinals(handle, query, k, metric, minSimilarity, options = {}) {
6441
- const slot = pickSlot();
6442
- if (slot === null) throw new Error("No vector search worker is running");
6443
- requestCounter += 1;
6444
- const request = {
6445
- type: "searchOrdinals",
6446
- requestId: `${requestCounter}`,
6447
- handle,
6448
- query,
6449
- k,
6450
- metric,
6451
- minSimilarity,
6452
- ...graphSearchOptionsOf(options)
6453
- };
6454
- const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
6455
- if (message.type === "error") throw new Error(message.message);
6456
- if (message.type !== "ordinalResult") throw new Error("Vector search worker returned an unexpected message");
6457
- return { ordinals: message.ordinals, scores: message.scores };
6458
- },
6459
- async shutdown() {
6460
- for (const slot of slots) {
6461
- slot.alive = false;
6462
- for (const [, waiting] of slot.pending) {
6463
- clearTimeout(waiting.timer);
6464
- waiting.reject(new Error("Vector search pool has shut down"));
6465
- }
6466
- slot.pending.clear();
6467
- try {
6468
- await slot.worker.terminate();
6469
- } catch {
6470
- slot.alive = false;
6471
- }
6472
- }
6473
- }
6474
- };
6475
- }
6476
- var sharedPool = null;
6477
- var poolHolders = 0;
6478
- var workersUnavailable = false;
6479
- function acquireVectorSearchPool(requestedCount) {
6480
- poolHolders += 1;
6481
- if (workersUnavailable) return Promise.resolve(null);
6482
- if (sharedPool === null) {
6483
- sharedPool = createVectorSearchPool(requestedCount).then(
6484
- (pool) => {
6485
- if (pool === null) workersUnavailable = true;
6486
- return pool;
6487
- },
6488
- () => {
6489
- workersUnavailable = true;
6490
- return null;
6491
- }
6492
- );
6493
- }
6494
- return sharedPool;
6495
- }
6496
- async function releaseVectorSearchPool() {
6497
- if (poolHolders === 0) return;
6498
- poolHolders -= 1;
6499
- if (poolHolders > 0 || sharedPool === null) return;
6500
- const pending = sharedPool;
6501
- sharedPool = null;
6502
- const pool = await pending;
6503
- await pool?.shutdown();
6504
- }
6505
-
6506
- // src/vector/vector-index/worker-copies.ts
6507
- var handleCounter = 0;
6508
- function nextHandle(state) {
6509
- handleCounter += 1;
6510
- return `${state.indexName}/${state.fieldName}#${handleCounter}`;
6511
- }
6512
- async function dropHandle(state, handle) {
6513
- const host = state.workerCopies.host;
6514
- if (host !== void 0) {
6515
- await host.drop(state.indexName, state.fieldName, handle).catch(() => void 0);
6516
- return;
6517
- }
6518
- const pool = state.workerCopyPool;
6519
- if (pool !== null) {
6520
- await pool.drop(handle).catch(() => void 0);
6521
- await releaseVectorSearchPool().catch(() => void 0);
6522
- }
6523
- }
6524
- function dropSharedGraph(state, graph) {
6525
- const shared = state.sharedHandles.get(graph);
6526
- if (shared === void 0) return;
6527
- state.sharedHandles.delete(graph);
6528
- if (state.workerCopyHandle === shared.handle) {
6529
- state.workerCopyHandle = null;
6530
- state.workerCopyRevision = -1;
6531
- state.workerCopyMode = null;
6532
- }
6533
- void dropHandle(state, shared.handle);
6534
- }
6535
- function invalidateWorkerCopies(state) {
6536
- state.revision += 1;
6537
- for (const graph of [...state.sharedHandles.keys()]) dropSharedGraph(state, graph);
6538
- if (state.workerCopyHandle !== null) {
6539
- const handle = state.workerCopyHandle;
6540
- state.workerCopyHandle = null;
6541
- state.workerCopyRevision = -1;
6542
- state.workerCopyMode = null;
6543
- void dropHandle(state, handle);
6544
- }
6545
- if (state.workerCopies.host === void 0 && state.workerCopyPool !== null && state.sharedHandles.size === 0) {
6546
- state.workerCopyPool = null;
6547
- }
6548
- }
6549
- async function withdrawWorkerCopies(state) {
6550
- state.revision += 1;
6551
- const handles = /* @__PURE__ */ new Set();
6552
- for (const shared of state.sharedHandles.values()) handles.add(shared.handle);
6553
- if (state.workerCopyHandle !== null) handles.add(state.workerCopyHandle);
6554
- state.sharedHandles.clear();
6555
- state.workerCopyHandle = null;
6556
- state.workerCopyRevision = -1;
6557
- state.workerCopyMode = null;
6558
- await Promise.all([...handles].map((handle) => dropHandle(state, handle)));
6559
- if (state.workerCopies.host === void 0 && state.sharedHandles.size === 0) state.workerCopyPool = null;
6560
- }
6561
- function noteWrite(state) {
6562
- state.revision += 1;
6563
- if (state.workerCopyMode === "clone") invalidateWorkerCopies(state);
6564
- }
6565
- function refreshWorkerCopies(state) {
6566
- invalidateWorkerCopies(state);
6567
- scheduleWorkerCopyLoad(state);
6568
- }
6569
- function recordSharedField(state, record) {
6570
- const { graph, handle, searchable, mode, revision, layoutRevision } = record;
6571
- state.sharedHandles.set(graph, { handle, searchable, mode });
6572
- state.sharedLayoutRevision = layoutRevision;
6573
- if (!searchable) return;
6574
- state.workerCopyHandle = handle;
6575
- state.workerCopyRevision = revision;
6576
- state.workerCopyMode = mode;
6577
- }
6578
- async function loadOnHost(state, host, graph, searchable) {
6579
- if (!host.holdsIndex(state.indexName)) return null;
6580
- if (!state.store.partitionsKnown) {
6581
- assignStorePartitions(state, (docId) => host.resolvePartition(state.indexName, docId));
6582
- }
6583
- const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
6584
- const layoutRevision = state.store.handles.layoutRevision;
6585
- let loaded = false;
6586
- try {
6587
- loaded = await host.loadShared(state.indexName, state.fieldName, handle, fieldHandlesOf(state, graph, searchable));
6588
- } catch {
6589
- loaded = false;
6590
- }
6591
- if (!loaded || state.disposed) return null;
6592
- recordSharedField(state, { graph, handle, searchable, mode: "hosted", revision: state.revision, layoutRevision });
6593
- return handle;
6594
- }
6595
- function captureCloneCodes(state) {
6596
- const quantizer = state.osq;
6597
- const centroid = quantizer?.centroid ?? null;
6598
- const layout = state.store.handles.codeLayout;
6599
- if (quantizer === null || centroid === null || layout === null) return null;
6600
- const slots = state.store.slots;
6601
- const records = new Uint8Array(slots * layout.slotStride);
6602
- const present = new Uint8Array(slots);
6603
- for (let ordinal = 0; ordinal < slots; ordinal++) {
6604
- const record = quantizer.recordAtOrdinal(ordinal);
6605
- if (record === void 0) continue;
6606
- records.set(record, ordinal * layout.slotStride);
6607
- present[ordinal] = 1;
6608
- }
6609
- return { centroid: Float32Array.from(centroid), records, present };
6610
- }
6611
- function captureCloneSnapshot(state, graph) {
6612
- return {
6613
- dimension: state.dimension,
6614
- quantization: state.quantizationMode,
6615
- metric: state.metric,
6616
- codes: captureCloneCodes(state),
6617
- store: state.store.exportSnapshot(),
6618
- graph: graph.exportSnapshot(),
6619
- tombstones: Array.from(state.tombstones)
6620
- };
6621
- }
6622
- async function loadOnPool(state, graph, searchable) {
6623
- if (graph === null) return null;
6624
- const revision = state.revision;
6625
- const pool = state.workerCopyPool ?? await acquireVectorSearchPool(state.workerCopies.count);
6626
- if (pool === null) {
6627
- await releaseVectorSearchPool();
6628
- return null;
6629
- }
6630
- if (state.disposed) {
6631
- await releaseVectorSearchPool();
6632
- return null;
6633
- }
6634
- state.workerCopyPool = pool;
6635
- const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
6636
- const layoutRevision = state.store.handles.layoutRevision;
6637
- let mode = "shared";
6638
- let loaded = false;
6639
- try {
6640
- if (sharedMemoryAvailable()) {
6641
- loaded = await pool.loadShared(handle, fieldHandlesOf(state, graph, searchable));
6642
- }
6643
- if (!loaded && searchable) {
6644
- mode = "clone";
6645
- loaded = await pool.load(handle, captureCloneSnapshot(state, graph));
6646
- }
6647
- } catch {
6648
- loaded = false;
6649
- }
6650
- if (!loaded || state.disposed || mode === "clone" && state.revision !== revision) {
6651
- await pool.drop(handle).catch(() => void 0);
6652
- return null;
6653
- }
6654
- recordSharedField(state, { graph, handle, searchable, mode, revision, layoutRevision });
6655
- return handle;
6656
- }
6657
- function shareGraph(state, graph, searchable) {
6658
- const run = state.sharing.then(async () => {
6659
- if (!state.workerCopies.enabled || state.disposed) return null;
6660
- const existing = state.sharedHandles.get(graph);
6661
- const layoutUnchanged = threadsHoldCurrentLayout(state);
6662
- if (existing !== void 0 && layoutUnchanged && (existing.searchable || !searchable)) return existing.handle;
6663
- const host = state.workerCopies.host;
6664
- return host !== void 0 ? loadOnHost(state, host, graph, searchable) : loadOnPool(state, graph, searchable);
6665
- });
6666
- state.sharing = run.then(
6667
- () => void 0,
6668
- () => void 0
6669
- );
6670
- return run;
6671
- }
6672
- function resendSharedHandles(state) {
6673
- const run = state.sharing.then(async () => {
6674
- if (!state.workerCopies.enabled || state.disposed) return;
6675
- const host = state.workerCopies.host;
6676
- for (const [graph, shared] of [...state.sharedHandles]) {
6677
- if (host !== void 0) await loadOnHost(state, host, graph, shared.searchable);
6678
- else await loadOnPool(state, graph, shared.searchable);
6679
- }
6680
- });
6681
- state.sharing = run.then(
6682
- () => void 0,
6683
- () => void 0
6684
- );
6685
- return run;
6686
- }
6687
- function scheduleWorkerCopyLoad(state) {
6688
- if (!state.workerCopies.enabled) return;
6689
- if (state.disposed || state.workerCopyLoading || state.building) return;
6690
- const graph = state.hnsw;
6691
- const host = state.workerCopies.host;
6692
- if (graph === null && host === void 0) return;
6693
- const existing = state.sharedHandles.get(graph);
6694
- if (existing?.searchable && threadsHoldCurrentLayout(state)) return;
6695
- if (host === void 0 && liveSize(state) < WORKER_COPY_MIN_VECTORS) return;
6696
- state.workerCopyLoading = true;
6697
- void shareGraph(state, graph, true).then(
6698
- () => {
6699
- state.workerCopyLoading = false;
6700
- },
6701
- () => {
6702
- state.workerCopyLoading = false;
6703
- }
6704
- );
6705
- }
6706
- async function searchViaWorkerCopies(state, query, k, metric, minSimilarity, options) {
6707
- const pool = state.workerCopyPool;
6708
- const handle = state.workerCopyHandle;
6709
- if (pool === null || handle === null || state.workerCopyMode === "hosted") return null;
6710
- if (state.workerCopyMode === "clone" && state.workerCopyRevision !== state.revision) return null;
6711
- try {
6712
- if (state.workerCopyMode === "shared") {
6713
- const outcome = await pool.searchOrdinals(handle, query, k, metric, minSimilarity, options);
6714
- const results = [];
6715
- for (let i = 0; i < outcome.ordinals.length; i++) {
6716
- const docId = state.store.docIdForOrdinal(outcome.ordinals[i]);
6717
- if (docId === void 0) continue;
6718
- results.push({ docId, score: outcome.scores[i] });
6719
- }
6720
- return results;
6721
- }
6722
- return await pool.search(handle, query, k, metric, minSimilarity, options);
6723
- } catch {
6724
- return null;
6725
- }
6726
- }
6727
-
6728
- // src/vector/vector-index/build-host.ts
6729
- async function dispatcherFor(state, graph) {
6730
- if (!state.workerCopies.enabled || state.disposed) return null;
6731
- const host = state.workerCopies.host;
6732
- if (host !== void 0) {
6733
- if (!host.holdsIndex(state.indexName)) return null;
6734
- const handle2 = await shareGraph(state, graph, graph === state.hnsw);
6735
- if (handle2 === null) return null;
6736
- return {
6737
- send: (ordinals) => host.insertOrdinals(state.indexName, state.fieldName, handle2, ordinals),
6738
- workerCount: Math.max(1, host.workerCount)
6739
- };
6740
- }
6741
- if (liveSize(state) < WORKER_COPY_MIN_VECTORS || !sharedMemoryAvailable()) return null;
6742
- const handle = await shareGraph(state, graph, graph === state.hnsw);
6743
- const pool = state.workerCopyPool;
6744
- if (handle === null || pool === null || state.sharedHandles.get(graph)?.mode !== "shared") return null;
6745
- return {
6746
- send: (ordinals) => pool.insertOrdinals(handle, ordinals),
6747
- workerCount: Math.max(1, pool.workerCount)
6748
- };
6749
- }
6750
- async function insertIntoGraph(state, graph, docIds, admit) {
6751
- const found = await dispatcherFor(state, graph);
6752
- if (found === null) return insertLocally(state, graph, docIds, admit);
6753
- const dispatch = found;
6754
- const pending = /* @__PURE__ */ new Set();
6755
- const limit = dispatch.workerCount * GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER;
6756
- const fallback = { noWorkerWarned: false, shortAnswerWarned: false };
6757
- let batchDocIds = [];
6758
- let batchOrdinals = [];
6759
- async function flush() {
6760
- if (batchOrdinals.length === 0) return;
6761
- const chunkDocIds = batchDocIds;
6762
- const chunkOrdinals = Int32Array.from(batchOrdinals);
6763
- batchDocIds = [];
6764
- batchOrdinals = [];
6765
- if (!threadsHoldCurrentLayout(state)) await shareGraph(state, graph, graph === state.hnsw);
6766
- const run = placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback).finally(() => {
6767
- pending.delete(run);
6768
- });
6769
- pending.add(run);
6770
- if (pending.size >= limit) await Promise.race(pending);
6771
- }
6772
- for (const docId of docIds) {
6773
- if (state.disposed) break;
6774
- if (!admit(docId)) continue;
6775
- const ordinal = state.store.getOrdinal(docId);
6776
- if (ordinal === void 0) continue;
6777
- batchDocIds.push(docId);
6778
- batchOrdinals.push(ordinal);
6779
- if (batchOrdinals.length >= GRAPH_BUILD_DISPATCH_CHUNK) await flush();
6780
- }
6781
- await flush();
6782
- await Promise.all(pending);
6783
- return !state.disposed;
6784
- }
6785
- async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback) {
6786
- let outcome = null;
6787
- try {
6788
- outcome = await dispatch.send(chunkOrdinals);
6789
- } catch {
6790
- outcome = null;
6791
- }
6792
- if (state.disposed) return;
6793
- if (outcome === null) {
6794
- if (!fallback.noWorkerWarned) {
6795
- fallback.noWorkerWarned = true;
6796
- console.warn(
6797
- `No worker thread placed the vectors of "${state.indexName}/${state.fieldName}", so this thread is building its graph itself`
6798
- );
6799
- }
6800
- for (const ordinal of chunkOrdinals) graph.insertOrdinal(ordinal);
6801
- }
6802
- let placedHere = 0;
6803
- for (let i = 0; i < chunkDocIds.length; i++) {
6804
- const docId = chunkDocIds[i];
6805
- const ordinal = chunkOrdinals[i];
6806
- if (state.store.getOrdinal(docId) !== ordinal) {
6807
- graph.markTombstoneOrdinal(ordinal);
6808
- continue;
6809
- }
6810
- if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
6811
- if (graph === state.hnsw) state.buffer.delete(docId);
6812
- }
6813
- if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
6814
- fallback.shortAnswerWarned = true;
6815
- console.warn(
6816
- `A worker thread answered a chunk of "${state.indexName}/${state.fieldName}" without placing ${placedHere} of its vectors, so this thread placed them itself`
6817
- );
6818
- }
6819
- }
6820
- async function insertLocally(state, graph, docIds, admit) {
6821
- let count = 0;
6822
- for (const docId of docIds) {
6823
- if (state.disposed) return false;
6824
- if (!admit(docId)) continue;
6825
- const ordinal = state.store.getOrdinal(docId);
6826
- if (ordinal === void 0) continue;
6827
- graph.insertOrdinal(ordinal);
6828
- if (graph === state.hnsw) state.buffer.delete(docId);
6829
- count += 1;
6830
- if (count % BUILD_CHUNK_SIZE === 0) {
6831
- noteWrite(state);
6832
- await yieldToEventLoop();
6833
- }
6834
- }
6835
- if (count > 0) noteWrite(state);
6836
- return !state.disposed;
6837
- }
6838
-
6839
- // src/vector/vector-index/dead-bits.ts
6840
- var NO_LIVE_VECTOR_AT_POSITION = -1;
6841
- function deadBytesFor(count) {
6842
- return Math.ceil(count / 8);
6843
- }
6844
- function isDead(dead, position) {
6845
- return dead !== null && (dead[position >> 3] >> (position & 7) & 1) === 1;
6846
- }
6847
- function countDead(dead, count) {
6848
- if (dead === null) return 0;
6849
- let found = 0;
6850
- for (let position = 0; position < count; position++) {
6851
- if (isDead(dead, position)) found += 1;
6852
- }
6853
- return found;
6854
- }
6855
- function deadBitsWhere(count, isDeadAt) {
6856
- let dead = null;
6857
- for (let position = 0; position < count; position++) {
6858
- if (!isDeadAt(position)) continue;
6859
- dead ??= new Uint8Array(deadBytesFor(count));
6860
- dead[position >> 3] |= 1 << (position & 7);
6861
- }
6862
- return dead;
6863
- }
6864
-
6865
- // src/vector/vector-index/disk.ts
6866
- function readsFromDisk(state) {
6867
- return state.storage === "disk";
6868
- }
6869
- function holdsVectorsOnDisk(state) {
6870
- return readsFromDisk(state) && state.hnsw !== null;
6871
- }
6872
- function* locationsOf(state, layout) {
6873
- const stride = state.dimension * 4;
6874
- for (let i = 0; i < layout.docIds.length; i++) {
6875
- if (isDead(layout.dead, i)) continue;
6876
- yield [layout.docIds[i], { path: layout.path, offset: layout.vectorsOffset + i * stride }];
6877
- }
6878
- }
6879
- async function shareFilesWithEveryThread(state, paths) {
6880
- const fileIndexes = /* @__PURE__ */ new Map();
6881
- for (const path of paths) fileIndexes.set(path, state.store.addVectorFile(path));
6882
- await resendSharedHandles(state);
6883
- return fileIndexes;
6884
- }
6885
- async function releaseLocations(state, paths, locations) {
6886
- const fileIndexes = await shareFilesWithEveryThread(state, paths);
6887
- if (state.disposed) return;
6888
- let count = 0;
6889
- for (const [docId, location] of locations) {
6890
- if (count > 0 && count % DISK_ADOPTION_YIELD_INTERVAL === 0) {
6891
- await yieldToEventLoop();
6892
- if (state.disposed) return;
6893
- }
6894
- count += 1;
6895
- const ordinal = state.store.getOrdinal(docId);
6896
- const fileIndex = fileIndexes.get(location.path);
6897
- if (ordinal === void 0 || fileIndex === void 0) continue;
6898
- state.store.releaseToFile(ordinal, { fileIndex, offset: location.offset });
6899
- }
6900
- state.store.releaseColdBlocks();
6901
- await resendSharedHandles(state);
6902
- }
6903
- function releaseInTurn(state, release) {
6904
- const run = state.releaseToFilesInFlight.then(release);
6905
- state.releaseToFilesInFlight = run.then(
6906
- () => void 0,
6907
- () => void 0
6908
- );
6909
- return run;
6910
- }
6911
- async function adoptDiskLayout(state, layout) {
6912
- if (!readsFromDisk(state) || state.disposed || layout.docIds.length === 0) return;
6913
- if (!holdsVectorsOnDisk(state)) {
6914
- for (const [docId, location] of locationsOf(state, layout)) {
6915
- if (state.store.has(docId)) state.pendingLocations.set(docId, location);
6916
- }
6917
- return;
6918
- }
6919
- await releaseInTurn(state, () => releaseLocations(state, [layout.path], locationsOf(state, layout)));
6920
- }
6921
- async function releasePendingLocations(state) {
6922
- if (state.pendingLocations.size === 0 || !holdsVectorsOnDisk(state) || state.disposed) return;
6923
- const pending = [...state.pendingLocations];
6924
- state.pendingLocations.clear();
6925
- const paths = new Set(pending.map(([, location]) => location.path));
6926
- await releaseInTurn(state, () => releaseLocations(state, paths, pending));
6927
- }
6928
-
6929
- // src/vector/vector-index/build.ts
6930
- async function buildGraphFromStore(state) {
6931
- const graph = createHNSWIndex(state.dimension, state.store, state.hnswConfig, state.osq ?? void 0);
6932
- state.freshGraph = graph;
6933
- let completed = false;
6934
- try {
6935
- completed = await insertIntoGraph(state, graph, [...allLiveDocIds(state)], () => true);
6936
- } finally {
6937
- state.freshGraph = null;
6938
- }
6939
- if (!completed) {
6940
- dropSharedGraph(state, graph);
6941
- return;
6942
- }
6943
- const previous = state.hnsw;
6944
- adoptGraph(state, graph);
6945
- for (const docId of [...state.buffer]) {
6946
- const ordinal = state.store.getOrdinal(docId);
6947
- if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) state.buffer.delete(docId);
6948
- }
6949
- if (previous !== null && previous !== graph) dropSharedGraph(state, previous);
6950
- await releasePendingLocations(state);
6951
- }
6952
- async function promoteToGraph(state) {
6953
- if (liveSize(state) === 0) return;
6954
- calibrateQuantizer(state);
6955
- await buildGraphFromStore(state);
6956
- }
6957
- async function growGraph(state, graph, bufferSnapshot) {
6958
- const admit = (docId) => {
6959
- if (state.hnsw !== graph) return false;
6960
- if (state.tombstones.has(docId) || !state.store.has(docId)) {
6961
- state.buffer.delete(docId);
6962
- return false;
6963
- }
6964
- return true;
6965
- };
6966
- if (state.osq && !state.osq.isCalibrated()) calibrateQuantizer(state);
6967
- await insertIntoGraph(state, graph, bufferSnapshot, admit);
6968
- }
6969
- function triggerBuild(state) {
6970
- if (state.building) return;
6971
- state.building = true;
6972
- const bufferSnapshot = new Set(state.buffer);
6973
- const existingGraph = state.hnsw;
6974
- const buildPromise = (async () => {
6975
- try {
6976
- if (state.disposed) return;
6977
- if (existingGraph !== null) {
6978
- await growGraph(state, existingGraph, bufferSnapshot);
6979
- return;
6980
- }
6981
- await promoteToGraph(state);
6982
- } finally {
6983
- state.building = false;
6984
- state.pendingBuild = null;
6985
- if (state.buffer.size > 0) {
6986
- scheduleBuild(state);
6987
- } else {
6988
- scheduleWorkerCopyLoad(state);
6989
- }
6990
- }
6991
- })();
6992
- state.pendingBuild = buildPromise;
6993
- }
6994
- function buildDue(state) {
6995
- if (state.hnsw === null) return liveSize(state) >= state.promotionThreshold;
6996
- if (state.buffer.size === 0) return false;
6997
- if (state.workerCopies.enabled && state.workerCopies.host !== void 0) return true;
6998
- return state.buffer.size >= state.promotionThreshold;
6999
- }
7000
- function scheduleBuild(state) {
7001
- if (state.building || state.buildScheduled || state.disposed) return;
7002
- if (!buildDue(state)) return;
7003
- state.buildScheduled = true;
7004
- setTimeout(() => {
7005
- state.buildScheduled = false;
7006
- triggerBuild(state);
7007
- }, 0);
7008
- }
7009
-
7010
- // src/serialization/constants.ts
7011
- var CHECKSUM_YIELD_CHUNK_BYTES = 4 * 1024 * 1024;
7012
- var NATIVE_CRC32_MIN_BYTES = 16;
7013
- var MAX_FILE_BYTES = 2147483647;
7014
- var VALID_HNSW_METRICS = /* @__PURE__ */ new Set(["cosine", "dotProduct", "euclidean"]);
7015
-
7016
- // src/vector/vector-index/payload.ts
7017
- var VECTOR_INDEX_PAYLOAD_VERSION = 3;
7018
- var VECTOR_INDEX_PART_VECTORS = 65536;
7019
- var LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
7020
- function vectorsToBytes(vectors) {
7021
- if (LITTLE_ENDIAN) return new Uint8Array(vectors.buffer, vectors.byteOffset, vectors.byteLength);
7022
- const bytes = new Uint8Array(vectors.byteLength);
7023
- const view = new DataView(bytes.buffer);
7024
- for (let i = 0; i < vectors.length; i++) view.setFloat32(i * 4, vectors[i], true);
7025
- return bytes;
7026
- }
7027
- function bytesToVectors(bytes) {
7028
- if (LITTLE_ENDIAN && bytes.byteOffset % 4 === 0) {
7029
- return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
7030
- }
7031
- const vectors = new Float32Array(bytes.byteLength / 4);
7032
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
7033
- for (let i = 0; i < vectors.length; i++) vectors[i] = view.getFloat32(i * 4, true);
7034
- return vectors;
7035
- }
7036
- function invalid(message, details) {
7037
- throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Invalid vector index payload: ${message}`, details);
7038
- }
7039
- function isRecord(value) {
7040
- return typeof value === "object" && value !== null && !Array.isArray(value);
7041
- }
7042
- function isNonNegativeInteger(value) {
7043
- return typeof value === "number" && Number.isInteger(value) && value >= 0;
7044
- }
7045
- function isSerializedGraph(value) {
7046
- return isRecord(value) && Array.isArray(value.nodes);
7047
- }
7048
- function codesOf(raw, dimension, count) {
7049
- if (raw === null || raw === void 0) return null;
7050
- if (!isRecord(raw)) invalid("codes must be a map or nil");
7051
- const bits = raw.bits;
7052
- if (bits !== 1 && bits !== 2 && bits !== 4 && bits !== 8) invalid("codes.bits must be 8, 4, 2, or 1", { bits });
7053
- if (!Array.isArray(raw.centroid) || raw.centroid.length !== dimension) {
7054
- invalid("codes.centroid must hold one number per dimension");
7055
- }
7056
- if (!(raw.records instanceof Uint8Array)) invalid("codes.records must be bytes");
7057
- if (raw.records.byteLength !== count * osqRecordBytes(dimension, bits)) {
7058
- invalid("codes.records must hold one record per vector", { count, bytes: raw.records.byteLength });
7059
- }
7060
- return { bits, centroid: raw.centroid.map(Number), records: raw.records };
7061
- }
7062
- function decodeVectorIndexPart(raw) {
7063
- if (!isRecord(raw)) invalid("payload must be a map");
7064
- if (raw.v !== VECTOR_INDEX_PAYLOAD_VERSION) {
7065
- throw new NarsilError(
7066
- ErrorCodes.ENVELOPE_VERSION_MISMATCH,
7067
- `This vector index payload was written in layout ${String(raw.v ?? 1)}, and this build reads layout ${VECTOR_INDEX_PAYLOAD_VERSION} alone`,
7068
- { version: raw.v ?? 1 }
7069
- );
7070
- }
7071
- if (typeof raw.fieldName !== "string") invalid("fieldName must be a string");
7072
- if (!isNonNegativeInteger(raw.dimension) || raw.dimension === 0) invalid("dimension must be a positive integer");
7073
- if (!isNonNegativeInteger(raw.part)) invalid("part must be a non-negative integer");
7074
- if (!isNonNegativeInteger(raw.parts) || raw.parts === 0 || raw.part >= raw.parts) {
7075
- invalid("parts must exceed part", { part: raw.part, parts: raw.parts });
7076
- }
7077
- if (!Array.isArray(raw.docIds) || !raw.docIds.every((docId) => typeof docId === "string")) {
7078
- invalid("docIds must be a list of strings");
7079
- }
7080
- if (raw.docIds.length > VECTOR_INDEX_PART_VECTORS) {
7081
- invalid(`a part holds at most ${VECTOR_INDEX_PART_VECTORS} vectors`, { count: raw.docIds.length });
7082
- }
7083
- if (!Array.isArray(raw.graphs) || !raw.graphs.every(isSerializedGraph)) {
7084
- invalid("graphs must be a list of graphs, each holding a list of nodes");
7085
- }
7086
- if (!(raw.vectors instanceof Uint8Array)) invalid("vectors must be bytes");
7087
- if (raw.vectors.byteLength !== raw.docIds.length * raw.dimension * 4) {
7088
- invalid("vectors must hold dimension float32 components per document", {
7089
- count: raw.docIds.length,
7090
- bytes: raw.vectors.byteLength
7091
- });
7092
- }
7093
- return {
7094
- v: VECTOR_INDEX_PAYLOAD_VERSION,
7095
- fieldName: raw.fieldName,
7096
- dimension: raw.dimension,
7097
- part: raw.part,
7098
- parts: raw.parts,
7099
- docIds: raw.docIds,
7100
- graphs: raw.graphs,
7101
- codes: codesOf(raw.codes, raw.dimension, raw.docIds.length),
7102
- vectors: raw.vectors
7103
- };
7104
- }
7105
- function decodeVectorIndexParts(raw) {
7106
- if (!Array.isArray(raw)) invalid("a field must carry a list of parts");
7107
- return raw.map(decodeVectorIndexPart);
7108
- }
7109
-
7110
- // src/vector/vector-index/checkpoint-payload.ts
7111
- var VECTOR_FILE_PAYLOAD_VERSION = 1;
7112
- var VECTOR_GRAPH_PAYLOAD_VERSION = 1;
7113
- var VECTOR_GRAPH_PART_PAYLOAD_VERSION = 2;
7114
- var VECTOR_GRAPH_PART_VECTORS = 16384;
7115
- var VECTOR_FILE_MAX_VECTORS = VECTOR_INDEX_PART_VECTORS;
7116
- var METRICS = ["cosine", "dotProduct", "euclidean"];
7117
- function invalid2(payload, message, details) {
7118
- throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Invalid ${payload} payload: ${message}`, details);
7119
- }
7120
- function isRecord2(value) {
7121
- return typeof value === "object" && value !== null && !Array.isArray(value);
7122
- }
7123
- function isIntegerUpTo(value, highest) {
7124
- return typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= highest;
7125
- }
7126
- function requireVersion(payload, found, expected) {
7127
- if (found === expected) return;
7128
- throw new NarsilError(
7129
- ErrorCodes.ENVELOPE_VERSION_MISMATCH,
7130
- `This ${payload} payload was written in layout ${String(found)}, and this build reads layout ${expected} alone`,
7131
- { version: found }
7132
- );
7133
- }
7134
- function decodeVectorFilePayload(raw) {
7135
- const name = "vector file";
7136
- if (!isRecord2(raw)) invalid2(name, "payload must be a map");
7137
- requireVersion(name, raw.v, VECTOR_FILE_PAYLOAD_VERSION);
7138
- if (typeof raw.fieldName !== "string") invalid2(name, "fieldName must be a string");
7139
- if (!isIntegerUpTo(raw.dimension, 65535) || raw.dimension === 0) invalid2(name, "dimension must be a positive uint16");
7140
- if (!Array.isArray(raw.docIds) || !raw.docIds.every((docId) => typeof docId === "string")) {
7141
- invalid2(name, "docIds must be a list of strings");
7142
- }
7143
- if (raw.docIds.length > VECTOR_FILE_MAX_VECTORS) {
7144
- invalid2(name, `a file holds at most ${VECTOR_FILE_MAX_VECTORS} vectors`, { count: raw.docIds.length });
7145
- }
7146
- if (!(raw.vectors instanceof Uint8Array)) invalid2(name, "vectors must be bytes");
7147
- if (raw.vectors.byteLength !== raw.docIds.length * raw.dimension * 4) {
7148
- invalid2(name, "vectors must hold dimension float32 components per document", {
7149
- count: raw.docIds.length,
7150
- bytes: raw.vectors.byteLength
7151
- });
7152
- }
7153
- return {
7154
- v: VECTOR_FILE_PAYLOAD_VERSION,
7155
- fieldName: raw.fieldName,
7156
- dimension: raw.dimension,
7157
- docIds: raw.docIds,
7158
- codes: codesOf(raw.codes, raw.dimension, raw.docIds.length),
7159
- vectors: raw.vectors
7160
- };
7161
- }
7162
- function isMetric(value) {
7163
- return METRICS.some((metric) => metric === value);
7164
- }
7165
- function decodeNumberedGraph(raw, minimumM) {
7166
- const name = "vector graph";
7167
- if (!isRecord2(raw)) invalid2(name, "each graph must be a map");
7168
- const entryPoint = raw.entryPoint ?? null;
7169
- if (entryPoint !== null && !isIntegerUpTo(entryPoint, 4294967295))
7170
- invalid2(name, "entryPoint must be a uint32 or nil");
7171
- if (!isIntegerUpTo(raw.maxLayer, 255)) invalid2(name, "maxLayer must be a uint8");
7172
- if (!isIntegerUpTo(raw.m, MAX_M) || raw.m < minimumM) {
7173
- invalid2(name, `m must be a whole number from ${minimumM} to ${MAX_M}`);
7174
- }
7175
- if (!isIntegerUpTo(raw.efConstruction, 65535)) invalid2(name, "efConstruction must be a uint16");
7176
- if (!isMetric(raw.metric)) invalid2(name, "metric must name a supported metric", { metric: raw.metric });
7177
- if (!(raw.levels instanceof Uint8Array)) invalid2(name, "levels must be bytes");
7178
- if (!(raw.neighbours instanceof Uint8Array)) invalid2(name, "neighbours must be bytes");
7179
- return {
7180
- entryPoint,
7181
- maxLayer: raw.maxLayer,
7182
- m: raw.m,
7183
- efConstruction: raw.efConstruction,
7184
- metric: raw.metric,
7185
- levels: raw.levels,
7186
- neighbours: raw.neighbours
7187
- };
7188
- }
7189
- function decodeVectorGraphPayload(raw) {
7190
- const name = "vector graph";
7191
- if (!isRecord2(raw)) invalid2(name, "payload must be a map");
7192
- requireVersion(name, raw.v, VECTOR_GRAPH_PAYLOAD_VERSION);
7193
- if (typeof raw.fieldName !== "string") invalid2(name, "fieldName must be a string");
7194
- if (!Array.isArray(raw.graphs)) invalid2(name, "graphs must be a list");
7195
- return {
7196
- v: VECTOR_GRAPH_PAYLOAD_VERSION,
7197
- fieldName: raw.fieldName,
7198
- graphs: raw.graphs.map((graph) => decodeNumberedGraph(graph, EARLIER_RELEASE_MIN_M))
7199
- };
7200
- }
7201
- function decodeVectorGraphPartPayload(raw) {
7202
- const name = "vector graph part";
7203
- if (!isRecord2(raw)) invalid2(name, "payload must be a map");
7204
- requireVersion(name, raw.v, VECTOR_GRAPH_PART_PAYLOAD_VERSION);
7205
- if (typeof raw.fieldName !== "string") invalid2(name, "fieldName must be a string");
7206
- if (!Array.isArray(raw.graphs)) invalid2(name, "graphs must be a list");
7207
- return {
7208
- v: VECTOR_GRAPH_PART_PAYLOAD_VERSION,
7209
- fieldName: raw.fieldName,
7210
- graphs: raw.graphs.map((graph) => decodeNumberedGraph(graph, MIN_M))
7211
- };
7212
- }
7213
- function sameGraphHeader(a, b) {
7214
- return a.entryPoint === b.entryPoint && a.maxLayer === b.maxLayer && a.m === b.m && a.efConstruction === b.efConstruction && a.metric === b.metric;
7215
- }
7216
- function assembleGraphParts(parts) {
7217
- const name = "vector graph part";
7218
- const first = parts[0];
7219
- if (first === void 0) invalid2(name, "a field that holds a graph must have at least one part");
7220
- const partLists = first.graphs.map(() => []);
7221
- for (let p = 0; p < parts.length; p++) {
7222
- const part = parts[p];
7223
- const isLast = p === parts.length - 1;
7224
- if (part.fieldName !== first.fieldName || part.graphs.length !== first.graphs.length) {
7225
- invalid2(name, `part ${p} lists other graphs than part 0`, { part: p });
7226
- }
7227
- for (let g = 0; g < part.graphs.length; g++) {
7228
- const graph = part.graphs[g];
7229
- if (!sameGraphHeader(graph, first.graphs[g]))
7230
- invalid2(name, `part ${p} gives graph ${g} other headers`, { part: p });
7231
- const levels = graph.levels.length;
7232
- const fits = isLast ? levels >= 1 && levels <= VECTOR_GRAPH_PART_VECTORS : levels === VECTOR_GRAPH_PART_VECTORS;
7233
- if (!fits) invalid2(name, `part ${p} gives graph ${g} ${levels} bytes of levels`, { part: p, levels });
7234
- partLists[g].push(graph);
7235
- }
7236
- }
7237
- const graphs = partLists.map((list) => ({ partSize: VECTOR_GRAPH_PART_VECTORS, parts: list }));
7238
- return { fieldName: first.fieldName, graphs };
7239
- }
7240
- function graphPartsOfEarlierPayload(payload) {
7241
- return {
7242
- fieldName: payload.fieldName,
7243
- graphs: payload.graphs.map((graph) => ({ partSize: Math.max(graph.levels.length, 1), parts: [graph] }))
7244
- };
7245
- }
7246
-
7247
- // src/vector/vector-index/persistence.ts
7248
- function recordFor(state, quantizer, docId) {
7249
- const record = quantizer.recordOf(docId);
7250
- if (record !== void 0) return record;
7251
- if (!state.store.has(docId)) return void 0;
7252
- quantizer.quantize(docId);
7253
- return quantizer.recordOf(docId);
7254
- }
7255
- function codedCentroid(state) {
7256
- const centroid = state.osq?.centroid ?? null;
7257
- if (centroid === null || state.store.handles.codeLayout === null || state.hnsw === null) return null;
7258
- return Array.from(centroid);
7259
- }
7260
- function sameCentroid(state, planned) {
7261
- const current = state.osq?.centroid ?? null;
7262
- if (current === null || current.length !== planned.length) return false;
7263
- for (let i = 0; i < planned.length; i++) {
7264
- if (current[i] !== planned[i]) return false;
7265
- }
7266
- return true;
7267
- }
7268
- function partCodes(state, docIds, centroid, tombstonedAt) {
7269
- const quantizer = state.osq;
7270
- const layout = state.store.handles.codeLayout;
7271
- if (centroid === null || quantizer === null || layout === null) return null;
7272
- if (!sameCentroid(state, centroid)) {
7273
- throw new NarsilError(
7274
- ErrorCodes.PERSISTENCE_SAVE_FAILED,
7275
- `The vector field "${state.fieldName}" was recalibrated while a save was writing its vectors`,
7276
- { fieldName: state.fieldName }
7277
- );
7278
- }
7279
- const records = new Uint8Array(docIds.length * layout.slotStride);
7280
- for (let i = 0; i < docIds.length; i++) {
7281
- const tombstoned = tombstonedAt.get(i);
7282
- const record = tombstoned === void 0 ? recordFor(state, quantizer, docIds[i]) : quantizer.recordAtOrdinal(tombstoned);
7283
- if (record !== void 0) records.set(record, i * layout.slotStride);
7284
- }
7285
- return { bits: quantizer.bits, centroid, records };
7286
- }
7287
- function splitSequences(parts) {
7288
- const sequences = [];
7289
- let current = null;
7290
- for (const part of parts) {
7291
- if (current === null || part.part === 0) {
7292
- current = { docIds: [], graphs: [], codes: null, codesComplete: true };
7293
- sequences.push(current);
7294
- }
7295
- current.docIds.push(...part.docIds);
7296
- for (let index = 0; index < part.graphs.length; index++) {
7297
- const graph = part.graphs[index];
7298
- const held = current.graphs[index];
7299
- if (held === void 0) current.graphs[index] = { ...graph, nodes: [...graph.nodes] };
7300
- else held.nodes.push(...graph.nodes);
7301
- }
7302
- if (part.codes === null) {
7303
- if (part.docIds.length > 0) current.codesComplete = false;
7304
- continue;
7305
- }
7306
- if (current.codes === null) {
7307
- current.codes = { bits: part.codes.bits, centroid: part.codes.centroid, records: part.codes.records };
7308
- continue;
7309
- }
7310
- if (current.codes.bits !== part.codes.bits) {
7311
- current.codesComplete = false;
7312
- continue;
7313
- }
7314
- const joined = new Uint8Array(current.codes.records.length + part.codes.records.length);
7315
- joined.set(current.codes.records, 0);
7316
- joined.set(part.codes.records, current.codes.records.length);
7317
- current.codes = { ...current.codes, records: joined };
7318
- }
7319
- return sequences;
7320
- }
7321
- function restoreCodes(state, sequences) {
7322
- const quantizer = state.osq;
7323
- const layout = state.store.handles.codeLayout;
7324
- if (quantizer === null || layout === null) return;
7325
- const usable = sequences.filter((sequence) => sequence.docIds.length > 0);
7326
- const single = usable.length === 1 ? usable[0] : null;
7327
- if (single === null || single.codes === null || !single.codesComplete || single.codes.bits !== quantizer.bits) {
7328
- recalibrateFromStore(state);
7329
- return;
7330
- }
7331
- quantizer.restoreCentroid(Float32Array.from(single.codes.centroid));
7332
- const recordBytes = layout.slotStride;
7333
- for (let i = 0; i < single.docIds.length; i++) {
7334
- quantizer.restoreRecord(single.docIds[i], single.codes.records.subarray(i * recordBytes, (i + 1) * recordBytes));
7335
- }
7336
- }
7337
- function restoreGraphs(state, graphs) {
7338
- if (graphs.length === 0) {
7339
- for (const docId of state.store.docIds()) {
7340
- if (!state.tombstones.has(docId)) state.buffer.add(docId);
7341
- }
7342
- return;
7343
- }
7344
- const graphData = graphs[0];
7345
- const restoredHnsw = createHNSWIndex(
7346
- state.dimension,
7347
- state.store,
7348
- {
7349
- m: graphData.m ?? state.hnswConfig?.m,
7350
- efConstruction: graphData.efConstruction ?? state.hnswConfig?.efConstruction,
7351
- metric: graphData.metric ?? state.hnswConfig?.metric
7352
- },
7353
- state.osq ?? void 0
7354
- );
7355
- restoredHnsw.deserialize(graphData);
7356
- adoptGraph(state, restoredHnsw);
7357
- for (let i = 1; i < graphs.length; i++) {
7358
- for (const [nodeDocId] of graphs[i].nodes) {
7359
- if (!restoredHnsw.has(nodeDocId) && state.store.has(nodeDocId)) restoredHnsw.insertNode(nodeDocId);
7360
- }
7361
- }
7362
- for (const docId of state.store.docIds()) {
7363
- if (state.tombstones.has(docId)) continue;
7364
- if (!restoredHnsw.has(docId)) state.buffer.add(docId);
7365
- }
7366
- if (state.buffer.size > restoredHnsw.size) {
7367
- adoptGraph(state, null);
7368
- state.buffer.clear();
7369
- for (const docId of state.store.docIds()) {
7370
- if (!state.tombstones.has(docId)) state.buffer.add(docId);
7371
- }
7372
- }
7373
- }
7374
- function insertPart(state, part) {
7375
- const dimension = state.dimension;
7376
- const vectors = bytesToVectors(part.vectors);
7377
- for (let i = 0; i < part.docIds.length; i++) {
7378
- state.store.insert(part.docIds[i], vectors.subarray(i * dimension, (i + 1) * dimension));
7379
- }
7380
- }
7381
- function deserialize(state, parts) {
7382
- for (const part of parts) {
7383
- if (part.dimension !== state.dimension) {
7384
- throw new NarsilError(
7385
- ErrorCodes.VECTOR_DIMENSION_MISMATCH,
7386
- `Payload dimension ${part.dimension} does not match index dimension ${state.dimension}`,
7387
- { expected: state.dimension, received: part.dimension }
7388
- );
7389
- }
7390
- }
7391
- const sequences = splitSequences(parts);
7392
- const graphs = [];
7393
- for (const sequence of sequences) {
7394
- for (const graph of sequence.graphs) {
7395
- if (graph.nodes.length > 0) graphs.push(graph);
7396
- }
7397
- }
7398
- emptyFieldBeforeRestore(state);
7399
- for (const part of parts) insertPart(state, part);
7400
- if (state.osq !== null && osqBitsOf(state.quantizationMode) !== null && graphs.length > 0) {
7401
- restoreCodes(state, sequences);
7402
- }
7403
- restoreGraphs(state, graphs);
7404
- }
7405
-
7406
- // src/vector/vector-index/checkpoint-plan.ts
7407
- var ORDINAL_HOLDS_NO_NUMBER = -1;
7408
- function vectorsPerFile(dimension, codeBits) {
7409
- const recordBytes = codeBits === 1 || codeBits === 2 || codeBits === 4 || codeBits === 8 ? osqRecordBytes(dimension, codeBits) : 0;
7410
- const docIdBytes = VECTOR_FILE_STRING_HEADER_BYTES + MAX_DOC_ID_LENGTH * 3;
7411
- const perVector = dimension * 4 + recordBytes + docIdBytes;
7412
- const overhead = VECTOR_FILE_FIXED_OVERHEAD_BYTES + dimension * VECTOR_FILE_CENTROID_BYTES_PER_COMPONENT;
7413
- const fitting = Math.floor((MAX_FILE_BYTES - overhead) / perVector);
7414
- return Math.max(1, Math.min(VECTOR_FILE_MAX_VECTORS, fitting));
7415
- }
7416
- function fileCapacityOf(state) {
7417
- return vectorsPerFile(state.dimension, osqBitsOf(state.quantizationMode));
7418
- }
7419
- function calibrationOf(state) {
7420
- if (codedCentroid(state) === null) return null;
7421
- return Atomics.load(state.store.handles.header, STORE_CALIBRATION_GENERATION);
7422
- }
7423
- function signatureOf(state) {
7424
- return { revision: state.revision, graphSize: state.hnsw?.size ?? -1, calibration: calibrationOf(state) };
7425
- }
7426
- function sameSignature(a, b) {
7427
- return a !== null && a.revision === b.revision && a.graphSize === b.graphSize && a.calibration === b.calibration;
7428
- }
7429
- function registryMatches(state, listedKeys) {
7430
- const listed = listedKeys ?? [];
7431
- if (listed.length !== state.savedFiles.length) return false;
7432
- return state.savedFiles.every((file, index) => file.key === listed[index]);
7433
- }
7434
- function isLive(state, ordinal) {
7435
- if (ordinal === NO_LIVE_VECTOR_AT_POSITION) return false;
7436
- const docId = state.store.docIdForOrdinal(ordinal);
7437
- return docId !== void 0 && !state.tombstones.has(docId);
7438
- }
7439
- function isGraphTombstone(state, ordinal) {
7440
- return ordinal !== NO_LIVE_VECTOR_AT_POSITION && state.hnsw !== null && state.hnsw.holdsTombstone(ordinal);
7441
- }
7442
- function isCarried(state, ordinal) {
7443
- return isLive(state, ordinal) || isGraphTombstone(state, ordinal);
7444
- }
7445
- function carriedCountOf(state, file) {
7446
- let carried = 0;
7447
- for (let i = 0; i < file.ordinals.length; i++) if (isCarried(state, file.ordinals[i])) carried += 1;
7448
- return carried;
7449
- }
7450
- function deadBitsOf(state, file) {
7451
- return deadBitsWhere(file.ordinals.length, (position) => !isLive(state, file.ordinals[position]));
7452
- }
7453
- function filesWorthKeeping(state, listed, calibration, carriedCounts) {
7454
- const kept = [];
7455
- for (const file of listed) {
7456
- const carried = carriedCountOf(state, file);
7457
- const deadWithoutNodeShare = file.ordinals.length === 0 ? 1 : 1 - carried / file.ordinals.length;
7458
- if (file.calibration !== calibration || deadWithoutNodeShare > VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE) continue;
7459
- kept.push(file);
7460
- carriedCounts.push(carried);
7461
- }
7462
- return kept;
7463
- }
7464
- function absorbSmallerPartialFiles(kept, carriedCounts, vectorsToWrite, capacity) {
7465
- let pending = vectorsToWrite;
7466
- for (let index = kept.length - 1; index >= 0 && pending > 0; index--) {
7467
- if (kept[index].ordinals.length >= capacity) continue;
7468
- if (carriedCounts[index] > pending) return;
7469
- pending += carriedCounts[index];
7470
- kept.splice(index, 1);
7471
- carriedCounts.splice(index, 1);
7472
- }
7473
- }
7474
- function ordinalsOutsideOf(state, kept) {
7475
- const slots = state.store.slots;
7476
- const saved = new Uint8Array(slots);
7477
- for (const file of kept) {
7478
- for (let i = 0; i < file.ordinals.length; i++) {
7479
- const ordinal = file.ordinals[i];
7480
- if (ordinal !== NO_LIVE_VECTOR_AT_POSITION && ordinal < slots) saved[ordinal] = 1;
7481
- }
7482
- }
7483
- const outside = [];
7484
- for (let ordinal = 0; ordinal < slots; ordinal++) {
7485
- if (saved[ordinal] === 0 && isCarried(state, ordinal)) outside.push(ordinal);
7486
- }
7487
- return outside;
7488
- }
7489
- function countOutside(state, carriedCounts) {
7490
- let held = 0;
7491
- for (const carried of carriedCounts) held += carried;
7492
- const tombstonedNodes = state.hnsw?.tombstoneCount ?? 0;
7493
- return Math.max(0, state.store.size - state.tombstones.size + tombstonedNodes - held);
7494
- }
7495
- function numberEveryVector(state, kept, written) {
7496
- let total = written.length;
7497
- for (const file of kept) total += file.ordinals.length;
7498
- const numberOfOrdinal = new Int32Array(state.store.slots).fill(ORDINAL_HOLDS_NO_NUMBER);
7499
- const ordinalOfNumber = new Int32Array(total).fill(NO_LIVE_VECTOR_AT_POSITION);
7500
- let number = 0;
7501
- for (const file of kept) {
7502
- for (let i = 0; i < file.ordinals.length; i++, number++) {
7503
- const ordinal = file.ordinals[i];
7504
- if (!isCarried(state, ordinal)) continue;
7505
- numberOfOrdinal[ordinal] = number;
7506
- ordinalOfNumber[number] = ordinal;
7507
- }
7508
- }
7509
- for (const ordinal of written) {
7510
- numberOfOrdinal[ordinal] = number;
7511
- ordinalOfNumber[number] = ordinal;
7512
- number += 1;
7513
- }
7514
- return { numberOfOrdinal, ordinalOfNumber };
7515
- }
7516
- function graphPartsOf(state, numbers) {
7517
- if (state.hnsw === null || numbers.ordinalOfNumber.length === 0) return null;
7518
- const parts = state.hnsw.serializeNumbered(
7519
- numbers.numberOfOrdinal,
7520
- numbers.ordinalOfNumber,
7521
- VECTOR_GRAPH_PART_VECTORS
7522
- );
7523
- return parts.map((graph) => ({ v: VECTOR_GRAPH_PART_PAYLOAD_VERSION, fieldName: state.fieldName, graphs: [graph] }));
7524
- }
7525
- function graphPartsWithoutNodes(parts, numbers) {
7526
- return parts.map((part, index) => ({
7527
- ...part,
7528
- graphs: part.graphs.map((graph) => graphWithoutNodes(graph, index * VECTOR_GRAPH_PART_VECTORS, numbers))
7529
- }));
7530
- }
7531
- function plannedVectorOf(state, ordinal) {
7532
- const docId = state.store.docIdForOrdinal(ordinal) ?? state.store.view.docIdAt(ordinal) ?? "";
7533
- return { ordinal, docId, tombstoned: !isLive(state, ordinal) };
7534
- }
7535
- function readFile(state, planned, centroid) {
7536
- const dimension = state.dimension;
7537
- const vectors = new Float32Array(planned.length * dimension);
7538
- const ordinals = new Int32Array(planned.length).fill(NO_LIVE_VECTOR_AT_POSITION);
7539
- const tombstonedAt = /* @__PURE__ */ new Map();
7540
- const docIds = planned.map((vector) => vector.docId);
7541
- for (let i = 0; i < planned.length; i++) {
7542
- const { ordinal, docId, tombstoned } = planned[i];
7543
- const held = tombstoned || state.store.holdsOrdinal(ordinal) ? ordinal : state.store.getOrdinal(docId);
7544
- const entry = held === void 0 ? void 0 : state.store.entryForOrdinal(held);
7545
- if (held === void 0 || entry === void 0) continue;
7546
- vectors.set(entry.vector, i * dimension);
7547
- ordinals[i] = held;
7548
- if (tombstoned) tombstonedAt.set(i, held);
7549
- }
7550
- return {
7551
- payload: {
7552
- v: VECTOR_FILE_PAYLOAD_VERSION,
7553
- fieldName: state.fieldName,
7554
- dimension,
7555
- docIds,
7556
- codes: partCodes(state, docIds, centroid, tombstonedAt),
7557
- vectors: vectorsToBytes(vectors)
7558
- },
7559
- ordinals,
7560
- dead: deadBitsWhere(planned.length, (i) => planned[i].tombstoned || ordinals[i] === NO_LIVE_VECTOR_AT_POSITION)
7561
- };
7562
- }
7563
- function planCheckpoint(state, listedKeys, rewritesGraph = false) {
7564
- const signature = signatureOf(state);
7565
- const calibration = signature.calibration;
7566
- const trusted = registryMatches(state, listedKeys);
7567
- const capacity = fileCapacityOf(state);
7568
- const carriedCounts = [];
7569
- const keptFiles = filesWorthKeeping(state, trusted ? state.savedFiles : [], calibration, carriedCounts);
7570
- absorbSmallerPartialFiles(keptFiles, carriedCounts, countOutside(state, carriedCounts), capacity);
7571
- const written = ordinalsOutsideOf(state, keptFiles);
7572
- const planned = written.map((ordinal) => plannedVectorOf(state, ordinal));
7573
- const centroid = codedCentroid(state);
7574
- const listedBefore = trusted && listedKeys !== null;
7575
- const unchanged = !rewritesGraph && listedBefore && sameSignature(state.savedSignature, signature) && written.length === 0;
7576
- let firstWrittenNumber = 0;
7577
- for (const file of keptFiles) firstWrittenNumber += file.ordinals.length;
7578
- const graphParts = unchanged ? null : graphPartsOf(state, numberEveryVector(state, keptFiles, written));
7579
- const vanished = /* @__PURE__ */ new Set();
7580
- let vanishedWhenStripped = 0;
7581
- let graphPartsToWrite = graphParts;
7582
- return {
7583
- unchanged,
7584
- keptFiles,
7585
- kept: keptFiles.map((file) => ({
7586
- id: file.id,
7587
- key: file.key,
7588
- count: file.ordinals.length,
7589
- dead: deadBitsOf(state, file)
7590
- })),
7591
- newFiles: Math.ceil(written.length / capacity),
7592
- get graphParts() {
7593
- if (graphParts !== null && vanished.size !== vanishedWhenStripped) {
7594
- graphPartsToWrite = graphPartsWithoutNodes(graphParts, vanished);
7595
- vanishedWhenStripped = vanished.size;
7596
- }
7597
- return graphPartsToWrite;
7598
- },
7599
- signature,
7600
- readNewFile(index) {
7601
- const start = index * capacity;
7602
- const end = start + capacity;
7603
- const file = readFile(state, planned.slice(start, end), centroid);
7604
- for (let i = 0; i < file.ordinals.length; i++) {
7605
- if (file.ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) vanished.add(firstWrittenNumber + start + i);
7606
- }
7607
- return file;
7608
- }
7609
- };
7610
- }
7611
- function recordCheckpoint(state, plan, written) {
7612
- if (plan.unchanged) return;
7613
- const calibration = plan.signature.calibration;
7614
- state.savedFiles = [...plan.keptFiles, ...written.map((file) => ({ ...file, calibration }))];
7615
- state.savedSignature = plan.signature;
7616
- }
7617
-
7618
- // src/vector/vector-index/checkpoint-restore.ts
7619
- var CALIBRATION_NEVER_CURRENT = -1;
7620
- function sameCentroid2(a, b) {
7621
- if (a.length !== b.length) return false;
7622
- for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
7623
- return true;
7624
- }
7625
- function bufferEveryVectorOutsideTheGraph(state) {
7626
- for (const docId of state.store.docIds()) {
7627
- if (state.tombstones.has(docId)) continue;
7628
- if (state.hnsw === null || !state.hnsw.has(docId)) state.buffer.add(docId);
7629
- }
7630
- }
7631
- function insertNodesOfLaterGraph(state, graph, later, ordinals) {
7632
- for (const part of later.parts) requireWholeNeighbourLists(part);
7633
- for (let number = 0; number < ordinals.length; number++) {
7634
- const ordinal = ordinals[number];
7635
- if (ordinal === NO_LIVE_VECTOR_AT_POSITION || topLayerInParts(later, number) < 0) continue;
7636
- const docId = state.store.docIdForOrdinal(ordinal);
7637
- if (docId !== void 0 && !graph.has(docId)) graph.insertOrdinal(ordinal);
7638
- }
7639
- }
7640
- function restoreGraph(state, payload, ordinalOfNumber, tombstoned) {
7641
- const first = payload?.graphs[0]?.parts[0];
7642
- if (payload === null || first === void 0) {
7643
- releaseReplacedVectors(state);
7644
- bufferEveryVectorOutsideTheGraph(state);
7645
- return;
7646
- }
7647
- const graph = createHNSWIndex(
7648
- state.dimension,
7649
- state.store,
7650
- { m: first.m, efConstruction: first.efConstruction, metric: first.metric },
7651
- state.osq ?? void 0
7652
- );
7653
- graph.deserializeNumbered(payload.graphs[0], ordinalOfNumber);
7654
- for (const ordinal of tombstoned) graph.markTombstoneOrdinal(ordinal);
7655
- adoptGraph(state, graph);
7656
- for (const later of payload.graphs.slice(1)) insertNodesOfLaterGraph(state, graph, later, ordinalOfNumber);
7657
- bufferEveryVectorOutsideTheGraph(state);
7658
- if (state.buffer.size <= graph.size) return;
7659
- adoptGraph(state, null);
7660
- releaseReplacedVectors(state);
7661
- state.buffer.clear();
7662
- bufferEveryVectorOutsideTheGraph(state);
7663
- }
7664
- function beginCheckpointRestore(state, shape) {
7665
- emptyFieldBeforeRestore(state);
7666
- const cold = readsFromDisk(state) && (shape.holdsGraph || shape.liveVectors >= state.promotionThreshold);
7667
- const restoresCodes = state.osq !== null && osqBitsOf(state.quantizationMode) !== null && shape.holdsGraph;
7668
- const files = [];
7669
- const graphNodes = shape.graph?.graphs[0] ?? null;
7670
- const tombstoned = /* @__PURE__ */ new Set();
7671
- let nextNumber = 0;
7672
- let sharedCodes = null;
7673
- let codesAgree = true;
7674
- function restoreRecords(payload, ordinals) {
7675
- const quantizer = state.osq;
7676
- const layout = state.store.handles.codeLayout;
7677
- if (!restoresCodes || quantizer === null || layout === null || !codesAgree) return;
7678
- const codes = payload.codes;
7679
- if (codes === null || codes.bits !== quantizer.bits) {
7680
- codesAgree = false;
7681
- return;
7682
- }
7683
- if (sharedCodes === null) {
7684
- sharedCodes = codes;
7685
- quantizer.restoreCentroid(Float32Array.from(codes.centroid));
7686
- } else if (!sameCentroid2(sharedCodes.centroid, codes.centroid)) {
7687
- codesAgree = false;
7688
- return;
7689
- }
7690
- const stride = layout.slotStride;
7691
- for (let i = 0; i < ordinals.length; i++) {
7692
- if (ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) continue;
7693
- const record = codes.records.subarray(i * stride, (i + 1) * stride);
7694
- if (tombstoned.has(ordinals[i])) quantizer.restoreRecordAtOrdinal(ordinals[i], record);
7695
- else quantizer.restoreRecord(payload.docIds[i], record);
7696
- }
7697
- }
7698
- function graphHoldsNodeFor(number) {
7699
- return graphNodes !== null && topLayerInParts(graphNodes, number) >= 0;
7700
- }
7701
- function addFile(file) {
7702
- const { payload, location } = file;
7703
- if (payload.dimension !== state.dimension) {
7704
- throw new NarsilError(
7705
- ErrorCodes.VECTOR_DIMENSION_MISMATCH,
7706
- `Payload dimension ${payload.dimension} does not match index dimension ${state.dimension}`,
7707
- { expected: state.dimension, received: payload.dimension }
7708
- );
7709
- }
7710
- const dimension = state.dimension;
7711
- const onDisk = readsFromDisk(state) && location !== null;
7712
- const vectors = bytesToVectors(payload.vectors);
7713
- const fileIndex = onDisk && cold ? state.store.addVectorFile(location.path) : null;
7714
- const ordinals = new Int32Array(payload.docIds.length).fill(NO_LIVE_VECTOR_AT_POSITION);
7715
- const firstNumber = nextNumber;
7716
- nextNumber += payload.docIds.length;
7717
- for (let i = 0; i < payload.docIds.length; i++) {
7718
- const dead = isDead(file.dead, i);
7719
- if (dead && !graphHoldsNodeFor(firstNumber + i)) continue;
7720
- const docId = payload.docIds[i];
7721
- const vector = vectors.subarray(i * dimension, (i + 1) * dimension);
7722
- const offset = onDisk ? location.vectorsOffset + i * dimension * 4 : 0;
7723
- if (dead) {
7724
- ordinals[i] = state.store.insertReplaced(docId, vector, fileIndex === null ? void 0 : { fileIndex, offset });
7725
- tombstoned.add(ordinals[i]);
7726
- continue;
7727
- }
7728
- const replaced = state.store.getOrdinal(docId);
7729
- if (replaced !== void 0) state.osq?.removeOrdinal(replaced);
7730
- if (fileIndex !== null) {
7731
- ordinals[i] = state.store.insertCold(docId, magnitude(vector), { fileIndex, offset });
7732
- continue;
7733
- }
7734
- ordinals[i] = state.store.insert(docId, vector);
7735
- if (onDisk) state.pendingLocations.set(docId, { path: location.path, offset });
7736
- }
7737
- restoreRecords(payload, ordinals);
7738
- files.push({ id: file.id, key: file.key, ordinals, coded: payload.codes !== null });
7739
- }
7740
- function finish() {
7741
- const codesRestored = restoresCodes && codesAgree && sharedCodes !== null;
7742
- if (restoresCodes && !codesRestored) {
7743
- recalibrateFromStore(state);
7744
- for (const ordinal of tombstoned) state.osq?.writeCodes(ordinal);
7745
- }
7746
- let total = 0;
7747
- for (const file of files) total += file.ordinals.length;
7748
- const ordinalOfNumber = new Int32Array(total);
7749
- let number = 0;
7750
- for (const file of files) {
7751
- for (let i = 0; i < file.ordinals.length; i++) {
7752
- const ordinal = file.ordinals[i];
7753
- if (!state.store.holdsOrdinal(ordinal) && !tombstoned.has(ordinal)) {
7754
- file.ordinals[i] = NO_LIVE_VECTOR_AT_POSITION;
7755
- }
7756
- }
7757
- ordinalOfNumber.set(file.ordinals, number);
7758
- number += file.ordinals.length;
7759
- }
7760
- restoreGraph(state, shape.graph, ordinalOfNumber, tombstoned);
7761
- const current = signatureOf(state).calibration;
7762
- const codedUnder = codesRestored && current !== null ? current : CALIBRATION_NEVER_CURRENT;
7763
- const saved = files.map((file) => ({
7764
- id: file.id,
7765
- key: file.key,
7766
- ordinals: file.ordinals,
7767
- calibration: file.coded ? codedUnder : null
7768
- }));
7769
- state.savedFiles = saved;
7770
- state.savedSignature = signatureOf(state);
7771
- }
7772
- return { addFile, finish };
7773
- }
7774
-
7775
- // src/vector/vector-index/flagged-documents.ts
7776
- var FlaggedDocuments = class extends Set {
7777
- constructor(store, layout) {
7778
- super();
7779
- this.store = store;
7780
- this.layout = layout;
7781
- }
7782
- ordinals = /* @__PURE__ */ new Map();
7783
- handles = null;
7784
- flags = new Uint8Array(0);
7785
- chunks = null;
7786
- add(docId) {
7787
- super.add(docId);
7788
- const handles = this.openHandles();
7789
- if (handles === null) return this;
7790
- this.flagCurrentOrdinal(docId);
7791
- Atomics.store(handles.header, this.layout.countWord, this.size);
7792
- return this;
7793
- }
7794
- delete(docId) {
7795
- const held = super.delete(docId);
7796
- const handles = this.openHandles();
7797
- if (handles === null) return held;
7798
- const ordinal = this.ordinals.get(docId);
7799
- if (ordinal !== void 0) {
7800
- this.ordinals.delete(docId);
7801
- this.unflag(ordinal);
7802
- }
7803
- Atomics.store(handles.header, this.layout.countWord, this.size);
7804
- return held;
7805
- }
7806
- clear() {
7807
- super.clear();
7808
- const handles = this.openHandles();
7809
- if (handles !== null) {
7810
- for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
7811
- Atomics.store(handles.header, this.layout.countWord, 0);
7812
- }
7813
- this.ordinals.clear();
7814
- }
7815
- flagCurrentOrdinal(docId) {
7816
- const ordinal = this.store.getOrdinal(docId);
7817
- const previous = this.ordinals.get(docId);
7818
- if (ordinal === void 0 || ordinal === previous) return;
7819
- if (this.chunks !== null) Atomics.add(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
7820
- Atomics.store(this.flags, ordinal, 1);
7821
- this.ordinals.set(docId, ordinal);
7822
- if (previous !== void 0) this.unflag(previous);
7823
- }
7824
- unflag(ordinal) {
7825
- Atomics.store(this.flags, ordinal, 0);
7826
- if (this.chunks !== null) Atomics.sub(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
7827
- }
7828
- openHandles() {
7829
- if (this.store.dimension === 0) return null;
7830
- const handles = this.store.handles;
7831
- if (handles === this.handles) return handles;
7832
- this.handles = handles;
7833
- this.flags = new Uint8Array(this.layout.flagsOf(handles));
7834
- const chunksOf = this.layout.chunksOf;
7835
- this.chunks = chunksOf === null ? null : new Int32Array(chunksOf(handles));
7836
- this.ordinals.clear();
7837
- for (const docId of this) this.flagCurrentOrdinal(docId);
7838
- return handles;
7839
- }
7840
- };
7841
- function createUnplacedVectors(store) {
7842
- return new FlaggedDocuments(store, {
7843
- flagsOf: (handles) => handles.unplaced,
7844
- chunksOf: (handles) => handles.unplacedChunks,
7845
- countWord: STORE_UNPLACED_COUNT
7846
- });
7847
- }
7848
- function createRemovedDocuments(store) {
7849
- return new FlaggedDocuments(store, {
7850
- flagsOf: (handles) => handles.removed,
7851
- chunksOf: null,
7852
- countWord: STORE_REMOVED_COUNT
7853
- });
7854
- }
7855
-
7856
- // src/vector/vector-index/maintenance.ts
7857
- function compact(state) {
7858
- if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
7859
- if (state.hnsw) {
7860
- state.compactedNodeCount += state.hnsw.tombstoneCount;
7861
- state.hnsw.compactTombstones();
7862
- }
7863
- state.freshGraph?.compactTombstones();
7864
- for (const docId of state.tombstones) {
7865
- state.store.remove(docId);
7866
- state.buffer.delete(docId);
7867
- if (state.osq) {
7868
- state.osq.remove(docId);
7869
- }
7870
- }
7871
- releaseReplacedVectors(state);
7872
- state.tombstones.clear();
7873
- }
7874
- function recalibrateWhileNoThreadSearches(state, graph) {
7875
- if (graph === null) {
7876
- recalibrateFromStore(state);
7877
- return;
7878
- }
7879
- graph.exclusively(() => recalibrateFromStore(state));
7880
- }
7881
- async function insertMissing(state, graph) {
7882
- const missingOrReplaced = (docId) => !graph.has(docId) || state.buffer.has(docId);
7883
- const heldWhenTheCallStarted = [...allLiveDocIds(state)];
7884
- const stillLiveAndUnplaced = (docId) => !state.tombstones.has(docId) && missingOrReplaced(docId);
7885
- await insertIntoGraph(state, graph, heldWhenTheCallStarted, stillLiveAndUnplaced);
7886
- }
7887
- async function foldIntoGraph(state) {
7888
- const rebuildNeeded = graphNeedsRebuild(state);
7889
- compact(state);
7890
- if (liveSize(state) === 0) {
7891
- const previous = state.hnsw;
7892
- adoptGraph(state, null);
7893
- if (previous !== null) dropSharedGraph(state, previous);
7894
- state.buffer.clear();
7895
- if (state.osq) {
7896
- state.osq.clear();
7897
- }
7898
- return;
7899
- }
7900
- const graph = state.hnsw;
7901
- if (graph === null || rebuildNeeded) {
7902
- if (state.osq?.isCalibrated()) recalibrateWhileNoThreadSearches(state, graph);
7903
- else calibrateQuantizer(state);
7904
- await buildGraphFromStore(state);
7905
- } else {
7906
- await insertMissing(state, graph);
7907
- }
7908
- }
7909
- async function buildExclusively(state, work) {
7910
- while (state.pendingBuild) {
7911
- await state.pendingBuild;
7912
- }
7913
- if (state.disposed) return;
7914
- state.building = true;
7915
- const run = work();
7916
- state.pendingBuild = run;
7917
- try {
7918
- await run;
7919
- } finally {
7920
- state.building = false;
7921
- state.pendingBuild = null;
7922
- if (state.buffer.size > 0) {
7923
- scheduleBuild(state);
7924
- }
7925
- }
7926
- }
7927
- function optimize(state) {
7928
- return buildExclusively(state, () => foldIntoGraph(state));
7929
- }
7930
- async function fillGraph(state) {
7931
- const graph = state.hnsw;
7932
- if (graph === null) {
7933
- if (liveSize(state) >= state.promotionThreshold) await promoteToGraph(state);
7934
- return;
7935
- }
7936
- if (state.osq && !state.osq.isCalibrated()) calibrateQuantizer(state);
7937
- if (graphNeedsRebuild(state)) {
7938
- await buildGraphFromStore(state);
7939
- return;
7940
- }
7941
- await insertMissing(state, graph);
7942
- }
7943
- function completeGraph(state) {
7944
- return buildExclusively(state, () => fillGraph(state));
7945
- }
7946
- function maintenanceStatus(state) {
7947
- const storeSize = state.store.size;
7948
- const graphCount = state.hnsw ? 1 : 0;
7949
- const graphNodes = state.hnsw === null ? storeSize : state.hnsw.size + state.hnsw.tombstoneCount;
7950
- const tombstoned = state.tombstones.size + state.store.replacedVectorCount;
7951
- const tombstoneRatio = graphNodes > 0 ? tombstoned / graphNodes : 0;
7952
- const estimatedCompactMs = Math.round(tombstoned * ESTIMATED_MS_PER_TOMBSTONE * state.dimensionScale);
7953
- const optimizeVectorCount = state.hnsw === null || graphNeedsRebuild(state) ? storeSize : state.buffer.size;
7954
- const estimatedOptimizeMs = Math.round(optimizeVectorCount * ESTIMATED_MS_PER_VECTOR_REBUILD * state.dimensionScale);
7955
- return {
7956
- tombstoneRatio,
7957
- graphCount,
7958
- bufferSize: state.buffer.size,
7959
- building: state.building || state.buildScheduled,
7960
- estimatedCompactMs,
7961
- estimatedOptimizeMs
7962
- };
7963
- }
7964
- function flatScanScratchBytes(state) {
7965
- return state.store.dimension === 0 ? 0 : nativeStoreScratchBytes(state.store.handles);
7966
- }
7967
- function estimateMemoryBytes(state) {
7968
- if (state.hnsw === null) return state.store.memoryBytes() + flatScanScratchBytes(state);
7969
- return state.store.memoryBytes() + state.hnsw.graphBytes + state.hnsw.searchScratchBytes;
7970
- }
7971
-
7972
- // src/core/heap.ts
7973
- function createMaxHeap(compare) {
7974
- return createHeap((a, b) => compare(b, a));
7975
- }
7976
- function createBoundedMaxHeap(compare, capacity) {
7977
- const heap = createMaxHeap(compare);
7978
- const innerPush = heap.push;
7979
- return {
7980
- push(item) {
7981
- if (heap.size < capacity) {
7982
- innerPush(item);
7983
- return;
7984
- }
7985
- const top = heap.peek();
7986
- if (top !== void 0 && compare(item, top) < 0) {
7987
- heap.pop();
7988
- innerPush(item);
7989
- }
7990
- },
7991
- pop: heap.pop,
7992
- peek: heap.peek,
7993
- get size() {
7994
- return heap.size;
7995
- },
7996
- toSortedArray: heap.toSortedArray
7997
- };
7998
- }
7999
- function createHeap(compare) {
8000
- const items = [];
8001
- function siftUp(idx) {
8002
- while (idx > 0) {
8003
- const parent = idx - 1 >> 1;
8004
- if (compare(items[idx], items[parent]) >= 0) break;
8005
- const tmp = items[idx];
8006
- items[idx] = items[parent];
8007
- items[parent] = tmp;
8008
- idx = parent;
8009
- }
8010
- }
8011
- function siftDown2(idx) {
8012
- const len = items.length;
8013
- while (true) {
8014
- let smallest = idx;
8015
- const left = 2 * idx + 1;
8016
- const right = 2 * idx + 2;
8017
- if (left < len && compare(items[left], items[smallest]) < 0) smallest = left;
8018
- if (right < len && compare(items[right], items[smallest]) < 0) smallest = right;
8019
- if (smallest === idx) break;
8020
- const tmp = items[idx];
8021
- items[idx] = items[smallest];
8022
- items[smallest] = tmp;
8023
- idx = smallest;
8024
- }
8025
- }
8026
- return {
8027
- push(item) {
8028
- items.push(item);
8029
- siftUp(items.length - 1);
8030
- },
8031
- pop() {
8032
- if (items.length === 0) return void 0;
8033
- const top = items[0];
8034
- const last = items.pop();
8035
- if (items.length > 0) {
8036
- items[0] = last;
8037
- siftDown2(0);
8038
- }
8039
- return top;
8040
- },
8041
- peek() {
8042
- return items[0];
8043
- },
8044
- get size() {
8045
- return items.length;
8046
- },
8047
- toSortedArray() {
8048
- return [...items].sort(compare);
8049
- }
8050
- };
8051
- }
8052
-
8053
- // src/vector/vector-index/merge-results.ts
8054
- function highScoreFirst(a, b) {
8055
- return b.score - a.score || compareCodePoints(a.docId, b.docId);
8056
- }
8057
- function mergeResults(hnswResults, bufferResults, k) {
8058
- const seen = /* @__PURE__ */ new Set();
8059
- const merged = [];
8060
- let hi = 0;
8061
- let bi = 0;
8062
- while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
8063
- const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
8064
- const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
8065
- let pick;
8066
- if (h && b) {
8067
- if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
8068
- pick = h;
8069
- hi++;
8070
- } else {
8071
- pick = b;
8072
- bi++;
8073
- }
8074
- } else if (h) {
8075
- pick = h;
8076
- hi++;
8077
- } else if (b) {
8078
- pick = b;
8079
- bi++;
8080
- } else {
8081
- break;
8082
- }
8083
- if (seen.has(pick.docId)) continue;
8084
- seen.add(pick.docId);
8085
- merged.push(pick);
8086
- }
8087
- return merged;
8088
- }
8089
-
8090
- // src/vector/vector-index/search.ts
8091
- function* bufferCandidates(state, filter) {
8092
- for (const docId of state.buffer) {
8093
- if (state.tombstones.has(docId)) continue;
8094
- if (filter) {
8095
- const ordinal = state.store.getOrdinal(docId);
8096
- if (ordinal === void 0 || !ordinalFilterHas(filter, ordinal)) continue;
8097
- }
8098
- yield docId;
8099
- }
8100
- }
8101
- function* filteredDocIds(state, filter) {
8102
- for (const ordinal of ordinalFilterValues(filter)) {
8103
- const docId = state.store.docIdForOrdinal(ordinal);
8104
- if (docId !== void 0) yield docId;
8105
- }
8106
- }
8107
- function admittedTotal(state, filter) {
8108
- return filter === void 0 ? liveSize(state) : filter.count;
8109
- }
8110
- function fetchedOutcome(state, results, minSimilarity, filter) {
8111
- if (minSimilarity === Number.NEGATIVE_INFINITY) {
8112
- return { results, matched: admittedTotal(state, filter), matchedExact: true };
8113
- }
8114
- return { results, matched: results.length, matchedExact: false };
8115
- }
8116
- function scannedOutcome(scan2) {
8117
- return { results: scan2.results, matched: scan2.matched, matchedExact: true };
8118
- }
8119
- function liveCandidates(state, candidates) {
8120
- const docIds = [];
8121
- const ordinals = [];
8122
- for (const docId of candidates) {
8123
- if (state.tombstones.has(docId)) continue;
8124
- const ordinal = state.store.getOrdinal(docId);
8125
- if (ordinal === void 0) continue;
8126
- docIds.push(docId);
8127
- ordinals.push(ordinal);
8128
- }
8129
- return { docIds, ordinals };
8130
- }
8131
- function scanThroughTheCore(state, query, k, metric, minSimilarity, scanned) {
8132
- const distances = nativeScoresOf(state.store.handles, query, metric, scanned.ordinals);
8133
- if (distances === null) return null;
8134
- const heap = createBoundedMaxHeap(highScoreFirst, k);
8135
- let matched = 0;
8136
- for (let i = 0; i < scanned.ordinals.length; i++) {
8137
- if (distances[i] === Number.POSITIVE_INFINITY) continue;
8138
- const score = toScore(distances[i], metric);
8139
- if (score < minSimilarity) continue;
8140
- matched++;
8141
- heap.push({ docId: scanned.docIds[i], score });
8142
- }
8143
- return { results: heap.toSortedArray().reverse(), matched };
8144
- }
8145
- function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
8146
- const scanned = liveCandidates(state, candidates);
8147
- const fromTheCore = scanThroughTheCore(state, query, k, metric, minSimilarity, scanned);
8148
- if (fromTheCore !== null) return fromTheCore;
8149
- noteFallback("score");
8150
- const arenaQuery = state.store.prepareQueryArena(query);
8151
- const queryMag = arenaQuery ? arenaQuery.magnitude : magnitude(query);
8152
- const heap = createBoundedMaxHeap(highScoreFirst, k);
8153
- let matched = 0;
8154
- for (let i = 0; i < scanned.docIds.length; i++) {
8155
- const docId = scanned.docIds[i];
8156
- let score;
8157
- if (arenaQuery) {
8158
- const distance = state.store.distanceFromArena(arenaQuery, scanned.ordinals[i], metric);
8159
- if (distance === Number.POSITIVE_INFINITY) continue;
8160
- score = toScore(distance, metric);
8161
- } else {
8162
- const entry = state.store.get(docId);
8163
- if (!entry) continue;
8164
- switch (metric) {
8165
- case "cosine":
8166
- score = cosineSimilarityWithMagnitudes(query, entry.vector, queryMag, entry.magnitude);
8167
- break;
8168
- case "dotProduct":
8169
- score = dotProduct(query, entry.vector);
8170
- break;
8171
- case "euclidean": {
8172
- const dist = euclideanDistance(query, entry.vector);
8173
- score = 1 / (1 + dist);
8174
- break;
8175
- }
8176
- }
8177
- }
8178
- if (score < minSimilarity) continue;
8179
- matched++;
8180
- heap.push({ docId, score });
8181
- }
8182
- return { results: heap.toSortedArray().reverse(), matched };
8183
- }
8184
- function search(state, query, k, options) {
8185
- return searchWithFilter(state, query, k, options, filterForOptions(state, options));
8186
- }
8187
- function searchWithFilter(state, query, k, options, filter) {
8188
- if (query.length !== state.dimension) {
8189
- throw new NarsilError(
8190
- ErrorCodes.VECTOR_DIMENSION_MISMATCH,
8191
- `Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
8192
- { expected: state.dimension, received: query.length }
8193
- );
8194
- }
8195
- const currentLiveSize = liveSize(state);
8196
- const { metric, minSimilarity, efSearch, oversample } = options;
8197
- if (currentLiveSize === 0) return { results: [], matched: 0, matchedExact: true };
8198
- if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
8199
- if (k <= 0) return fetchedOutcome(state, [], minSimilarity, filter);
8200
- if (state.buffer.size > 0 && !state.building && !state.buildScheduled) {
8201
- scheduleBuild(state);
8202
- }
8203
- if (!state.hnsw) {
8204
- const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
8205
- return scannedOutcome(bruteForceSearch(state, query, k, metric, minSimilarity, candidates));
8206
- }
8207
- if (filter) {
8208
- const hnswLiveSize = state.hnsw.size;
8209
- const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
8210
- if (selectivity < state.filterThreshold) {
8211
- const scan2 = bruteForceSearch(state, query, k, metric, minSimilarity, filteredDocIds(state, filter));
8212
- return scannedOutcome(scan2);
8213
- }
8214
- }
8215
- const hnswResults = state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample }).map((r) => ({ docId: r.docId, score: r.score }));
8216
- return fetchedOutcome(
8217
- state,
8218
- withBufferedVectors(state, query, k, options, filter, hnswResults),
8219
- minSimilarity,
8220
- filter
8221
- );
8222
- }
8223
- function withBufferedVectors(state, query, k, options, filter, graphResults) {
8224
- if (state.buffer.size === 0) return graphResults;
8225
- const candidates = bufferCandidates(state, filter);
8226
- const buffered = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates);
8227
- return mergeResults(graphResults, buffered.results, k);
8228
- }
8229
-
8230
- // src/vector/vector-index/index.ts
8231
- function defaultQuantizationFor(dimension) {
8232
- if (dimension >= OSQ4_MIN_DIMENSION) return "osq4";
8233
- return "osq8";
8234
- }
8235
- function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_WORKER_COPIES_ALLOWED, indexName = "", storage = "memory") {
8236
- if (!Number.isInteger(dimension) || dimension <= 0) {
8237
- throw new NarsilError(
8238
- ErrorCodes.VECTOR_DIMENSION_MISMATCH,
8239
- `Vector dimension must be a positive integer, got ${dimension}`,
8240
- { dimension }
8241
- );
8242
- }
8243
- const promotionThreshold = config?.threshold ?? DEFAULT_PROMOTION_THRESHOLD;
8244
- const filterThreshold = Math.max(0, Math.min(1, config?.filterThreshold ?? DEFAULT_FILTER_THRESHOLD));
8245
- const quantizationMode = config?.quantization ?? defaultQuantizationFor(dimension);
8246
- const codeBits = osqBitsOf(quantizationMode);
8247
- const metric = config?.hnswConfig?.metric ?? "cosine";
8248
- const rawHnswM = config?.hnswConfig?.m;
8249
- const hnswConfig = config?.hnswConfig ? { ...config.hnswConfig, m: rawHnswM !== void 0 ? Math.max(rawHnswM, 2) : void 0 } : void 0;
8250
- const dimensionScale = dimension / 256;
8251
- const store = createVectorStore({
8252
- dimension,
8253
- codeBits,
8254
- ...storage === "disk" ? { blockBytes: DISK_STORAGE_BLOCK_BYTES } : {}
8255
- });
8256
- const state = {
8257
- indexName,
8258
- fieldName,
8259
- dimension,
8260
- dimensionScale,
8261
- promotionThreshold,
8262
- filterThreshold,
8263
- quantizationMode,
8264
- storage,
8265
- metric,
8266
- hnswConfig,
8267
- workerCopies,
8268
- store,
8269
- tombstones: createRemovedDocuments(store),
8270
- buffer: createUnplacedVectors(store),
8271
- pendingLocations: /* @__PURE__ */ new Map(),
8272
- savedFiles: [],
8273
- savedSignature: null,
8274
- osq: codeBits === null ? null : createOsqQuantizer(dimension, codeBits, metric, store),
8275
- hnsw: null,
8276
- freshGraph: null,
8277
- compactedNodeCount: 0,
8278
- building: false,
8279
- buildScheduled: false,
8280
- pendingBuild: null,
8281
- disposed: false,
8282
- revision: 0,
8283
- workerCopyPool: null,
8284
- workerCopyHandle: null,
8285
- workerCopyRevision: -1,
8286
- workerCopyMode: null,
8287
- workerCopyLoading: false,
8288
- sharedHandles: /* @__PURE__ */ new Map(),
8289
- sharedLayoutRevision: 0,
8290
- sharing: Promise.resolve(),
8291
- releaseToFilesInFlight: Promise.resolve()
8292
- };
8293
- function validateDimension(vector) {
8294
- if (vector.length !== state.dimension) {
8295
- throw new NarsilError(
8296
- ErrorCodes.VECTOR_DIMENSION_MISMATCH,
8297
- `Vector dimension mismatch: expected ${state.dimension}, got ${vector.length}`,
8298
- { expected: state.dimension, received: vector.length }
8299
- );
8300
- }
8301
- }
8302
- function heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal) {
8303
- try {
8304
- return state.store.entryForOrdinal(ordinal)?.vector;
8305
- } catch {
8306
- return void 0;
8307
- }
8308
- }
8309
- function holdsThisVectorAlready(docId, ordinal, vector) {
8310
- if (state.tombstones.has(docId)) return false;
8311
- const held = heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal);
8312
- if (held === void 0) return false;
8313
- for (let i = 0; i < vector.length; i++) if (held[i] !== vector[i]) return false;
8314
- return true;
8315
- }
8316
- function insert(docId, vector, partitionId) {
8317
- validateDimension(vector);
8318
- const previous = state.store.getOrdinal(docId);
8319
- if (previous !== void 0 && holdsThisVectorAlready(docId, previous, vector)) {
8320
- if (partitionId !== void 0 && state.store.partitionOfOrdinal(previous) !== partitionId) {
8321
- noteWrite(state);
8322
- state.store.setPartition(docId, partitionId);
8323
- }
8324
- return;
8325
- }
8326
- noteWrite(state);
8327
- state.tombstones.delete(docId);
8328
- const graphHoldsPrevious = previous !== void 0 && ((state.hnsw?.holdsNode(previous) ?? false) || (state.freshGraph?.holdsNode(previous) ?? false));
8329
- state.store.insert(docId, vector, partitionId, graphHoldsPrevious);
8330
- if (previous !== void 0) {
8331
- state.hnsw?.markTombstoneOrdinal(previous);
8332
- state.freshGraph?.markTombstoneOrdinal(previous);
8333
- if (!graphHoldsPrevious) state.osq?.removeOrdinal(previous);
8334
- }
8335
- state.buffer.add(docId);
8336
- state.pendingLocations.delete(docId);
8337
- if (!threadsHoldCurrentLayout(state)) scheduleWorkerCopyLoad(state);
8338
- }
8339
- function remove(docId) {
8340
- if (!state.store.has(docId)) return;
8341
- noteWrite(state);
8342
- state.tombstones.add(docId);
8343
- state.buffer.delete(docId);
8344
- state.pendingLocations.delete(docId);
8345
- if (state.hnsw) {
8346
- state.hnsw.markTombstone(docId);
8347
- }
8348
- }
8349
- function getVector(docId) {
8350
- if (state.tombstones.has(docId)) return null;
8351
- const entry = state.store.get(docId);
8352
- if (!entry) return null;
8353
- return new Float32Array(entry.vector);
8354
- }
8355
- function has(docId) {
8356
- return state.store.has(docId) && !state.tombstones.has(docId);
8357
- }
8358
- async function awaitPendingBuild() {
8359
- while (state.pendingBuild) {
8360
- await state.pendingBuild;
8361
- }
8362
- }
8363
- function releaseHeldMemory() {
8364
- state.hnsw = null;
8365
- state.freshGraph = null;
8366
- state.osq = null;
8367
- state.tombstones.clear();
8368
- state.buffer.clear();
8369
- state.pendingLocations.clear();
8370
- state.savedFiles = [];
8371
- state.savedSignature = null;
8372
- state.store.release();
8373
- }
8374
- function dispose() {
8375
- state.disposed = true;
8376
- invalidateWorkerCopies(state);
8377
- const pending = state.pendingBuild;
8378
- if (pending !== null) {
8379
- void pending.then(releaseHeldMemory, releaseHeldMemory);
8380
- return;
8381
- }
8382
- releaseHeldMemory();
8383
- }
8384
- async function searchParallel(query, k, options) {
8385
- const confined = options.filterDocIds !== void 0 || options.filterPartitions !== void 0;
8386
- let filter = filterForOptions(state, options);
8387
- if (filter !== void 0) {
8388
- if (state.hnsw) {
8389
- const hnswLiveSize = state.hnsw.size;
8390
- const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
8391
- if (selectivity < state.filterThreshold) {
8392
- return searchWithFilter(state, query, k, options, filter);
8393
- }
8394
- }
8395
- }
8396
- const filterRevision = state.revision;
8397
- scheduleWorkerCopyLoad(state);
8398
- const viaWorkerCopy = await searchViaWorkerCopies(state, query, k, options.metric, options.minSimilarity, {
8399
- filter,
8400
- efSearch: options.efSearch,
8401
- oversample: options.oversample
8402
- });
8403
- if (confined && state.revision !== filterRevision) {
8404
- filter = filterForOptions(state, options);
8405
- }
8406
- if (viaWorkerCopy !== null) {
8407
- const results = withBufferedVectors(state, query, k, options, filter, viaWorkerCopy);
8408
- return fetchedOutcome(state, results, options.minSimilarity, filter);
8409
- }
8410
- return searchWithFilter(state, query, k, options, filter);
8411
- }
8412
- return {
8413
- get size() {
8414
- return liveSize(state);
8415
- },
8416
- get dimension() {
8417
- return state.dimension;
8418
- },
8419
- get fieldName() {
8420
- return state.fieldName;
8421
- },
8422
- get quantization() {
8423
- return state.quantizationMode;
8424
- },
8425
- get storage() {
8426
- return state.storage;
8427
- },
8428
- insert,
8429
- remove,
8430
- partitionsKnown: () => state.store.partitionsKnown,
8431
- assignPartitions: (resolve) => assignStorePartitions(state, resolve),
8432
- scheduleBuild: () => scheduleBuild(state),
8433
- awaitPendingBuild,
8434
- completeGraph: () => completeGraph(state),
8435
- dispose,
8436
- search: (query, k, options) => search(state, query, k, options),
8437
- searchParallel,
8438
- refreshWorkerCopies: () => refreshWorkerCopies(state),
8439
- getVector,
8440
- has,
8441
- compact: () => {
8442
- noteWrite(state);
8443
- compact(state);
8444
- },
8445
- optimize: async () => {
8446
- noteWrite(state);
8447
- await optimize(state);
8448
- },
8449
- maintenanceStatus: () => maintenanceStatus(state),
8450
- estimateMemoryBytes: () => estimateMemoryBytes(state),
8451
- deserialize: (parts) => {
8452
- invalidateWorkerCopies(state);
8453
- deserialize(state, parts);
8454
- },
8455
- planCheckpoint: (listedKeys, rewritesGraph) => planCheckpoint(state, listedKeys, rewritesGraph),
8456
- recordCheckpoint: (plan, written) => recordCheckpoint(state, plan, written),
8457
- restoreCheckpoint: (shape) => {
8458
- invalidateWorkerCopies(state);
8459
- return beginCheckpointRestore(state, shape);
8460
- },
8461
- vectorFilesInUse: () => state.store.dimension === 0 ? [] : state.store.handles.vectorFiles.filter((path) => path !== ""),
8462
- adoptDiskLayout: (layout) => adoptDiskLayout(state, layout),
8463
- releaseVectorFiles: async () => {
8464
- await withdrawWorkerCopies(state);
8465
- state.store.closeFiles();
8466
- }
8467
- };
8468
- }
8469
-
8470
6016
  // src/core/id-generator.ts
8471
6017
  function generateId() {
8472
6018
  const bytes = new Uint8Array(16);
@@ -10651,203 +8197,6 @@ function runSegmentAction(entry, action) {
10651
8197
  }
10652
8198
  }
10653
8199
 
10654
- // src/vector/shared-field/store-flags.ts
10655
- function bytesOf(flags, source) {
10656
- if (flags.source !== source) {
10657
- flags.source = source;
10658
- flags.view = new Uint8Array(source);
10659
- }
10660
- return flags.view;
10661
- }
10662
- function countsOf(chunks, source) {
10663
- if (chunks.source !== source) {
10664
- chunks.source = source;
10665
- chunks.view = new Int32Array(source);
10666
- }
10667
- return chunks.view;
10668
- }
10669
- function readStoreFlags(store) {
10670
- const unplaced = { source: null, view: new Uint8Array(0) };
10671
- const unplacedChunks = { source: null, view: new Int32Array(0) };
10672
- const removed = { source: null, view: new Uint8Array(0) };
10673
- return {
10674
- liveDocuments() {
10675
- const header = store.handles.header;
10676
- return Atomics.load(header, STORE_LIVE_COUNT) - Atomics.load(header, STORE_REMOVED_COUNT);
10677
- },
10678
- isRemoved(ordinal) {
10679
- const bytes = bytesOf(removed, store.handles.removed);
10680
- return ordinal < bytes.length && Atomics.load(bytes, ordinal) === 1;
10681
- },
10682
- unplacedOrdinals() {
10683
- const handles = store.handles;
10684
- if (Atomics.load(handles.header, STORE_UNPLACED_COUNT) === 0) return [];
10685
- const bytes = bytesOf(unplaced, handles.unplaced);
10686
- const counts = countsOf(unplacedChunks, handles.unplacedChunks);
10687
- const end = Math.min(store.slots, bytes.length);
10688
- const ordinals = [];
10689
- for (let chunk = 0; chunk < counts.length; chunk++) {
10690
- const from = chunk * UNPLACED_FLAG_CHUNK_ORDINALS;
10691
- if (from >= end) break;
10692
- if (Atomics.load(counts, chunk) === 0) continue;
10693
- const to = Math.min(end, from + UNPLACED_FLAG_CHUNK_ORDINALS);
10694
- for (let ordinal = from; ordinal < to; ordinal++) {
10695
- if (Atomics.load(bytes, ordinal) === 1) ordinals.push(ordinal);
10696
- }
10697
- }
10698
- return ordinals;
10699
- }
10700
- };
10701
- }
10702
-
10703
- // src/vector/shared-field/searcher.ts
10704
- function distancesInTypeScript(store, query, metric, ordinals) {
10705
- noteFallback("score");
10706
- const arenaQuery = store.prepareQueryArena(query);
10707
- const queryMagnitude = arenaQuery ? arenaQuery.magnitude : magnitude(query);
10708
- const distances = new Float64Array(ordinals.length).fill(Number.POSITIVE_INFINITY);
10709
- for (let i = 0; i < ordinals.length; i++) {
10710
- if (arenaQuery) {
10711
- distances[i] = store.distanceFromArena(arenaQuery, ordinals[i], metric);
10712
- continue;
10713
- }
10714
- const entry = store.entryForOrdinal(ordinals[i]);
10715
- if (entry) distances[i] = toDistance(query, entry.vector, queryMagnitude, entry.magnitude, metric);
10716
- }
10717
- return distances;
10718
- }
10719
- function scoreCandidates(store, candidates, query, k, metric, minSimilarity) {
10720
- const { ordinals, docIds } = candidates;
10721
- if (ordinals.length === 0) return { results: [], matched: 0 };
10722
- const distances = nativeScoresOf(store.handles, query, metric, ordinals) ?? distancesInTypeScript(store, query, metric, ordinals);
10723
- const heap = createBoundedMaxHeap(highScoreFirst, k);
10724
- let matched = 0;
10725
- for (let i = 0; i < ordinals.length; i++) {
10726
- if (distances[i] === Number.POSITIVE_INFINITY) continue;
10727
- const score = toScore(distances[i], metric);
10728
- if (score < minSimilarity) continue;
10729
- matched++;
10730
- heap.push({ docId: docIds[i], score });
10731
- }
10732
- return { results: heap.toSortedArray().reverse(), matched };
10733
- }
10734
- function createSharedVectorSearcher(options) {
10735
- const { fieldName, view, holdsDocument } = options;
10736
- const flags = readStoreFlags(view.store);
10737
- function filterForDocIds(allowed) {
10738
- const filter = createOrdinalFilter(view.store.slots);
10739
- for (const docId of allowed) {
10740
- const ordinal = view.ordinalOf(docId);
10741
- if (ordinal === void 0 || flags.isRemoved(ordinal)) continue;
10742
- addToOrdinalFilter(filter, ordinal);
10743
- }
10744
- return filter;
10745
- }
10746
- function filterForPartitions(allowed) {
10747
- const slots = view.store.slots;
10748
- const filter = createOrdinalFilter(slots);
10749
- for (let ordinal = 0; ordinal < slots; ordinal++) {
10750
- const partition = view.store.partitionAt(ordinal);
10751
- if (partition === void 0 || !allowed.has(partition)) continue;
10752
- if (!view.store.holdsOrdinal(ordinal) || flags.isRemoved(ordinal)) continue;
10753
- addToOrdinalFilter(filter, ordinal);
10754
- }
10755
- return filter;
10756
- }
10757
- function filterFor(searchOptions) {
10758
- if (searchOptions.filterDocIds !== void 0) return filterForDocIds(searchOptions.filterDocIds);
10759
- if (searchOptions.filterPartitions !== void 0) return filterForPartitions(searchOptions.filterPartitions);
10760
- return void 0;
10761
- }
10762
- function candidatesOf(ordinals, filter) {
10763
- const candidates = { ordinals: [], docIds: [] };
10764
- for (const ordinal of ordinals) {
10765
- if (filter !== void 0 && !ordinalFilterHas(filter, ordinal)) continue;
10766
- if (flags.isRemoved(ordinal)) continue;
10767
- const docId = view.docIdOf(ordinal);
10768
- if (docId === void 0 || view.ordinalOf(docId) !== ordinal) continue;
10769
- candidates.ordinals.push(ordinal);
10770
- candidates.docIds.push(docId);
10771
- }
10772
- return candidates;
10773
- }
10774
- function scanned(scan2) {
10775
- return { results: scan2.results, matched: scan2.matched, matchedExact: true, resolvedEveryHit: true };
10776
- }
10777
- function search2(query, k, searchOptions, filter) {
10778
- const { metric, minSimilarity, efSearch, oversample } = searchOptions;
10779
- const empty = { results: [], matched: 0, matchedExact: true, resolvedEveryHit: true };
10780
- const liveDocuments = flags.liveDocuments();
10781
- if (liveDocuments <= 0) return empty;
10782
- if (filter !== void 0 && filter.count === 0) return empty;
10783
- function fetched(results, resolvedEveryHit2) {
10784
- if (minSimilarity === Number.NEGATIVE_INFINITY) {
10785
- const matched = filter === void 0 ? liveDocuments : filter.count;
10786
- return { results, matched, matchedExact: true, resolvedEveryHit: resolvedEveryHit2 };
10787
- }
10788
- return { results, matched: results.length, matchedExact: false, resolvedEveryHit: resolvedEveryHit2 };
10789
- }
10790
- if (k <= 0) return fetched([], true);
10791
- const graph = view.graph;
10792
- if (graph === null) {
10793
- const ordinals = filter === void 0 ? flags.unplacedOrdinals() : ordinalFilterValues(filter);
10794
- return scanned(scoreCandidates(view.store, candidatesOf(ordinals, filter), query, k, metric, minSimilarity));
10795
- }
10796
- if (filter !== void 0) {
10797
- const graphLiveSize = view.liveSize;
10798
- const selectivity = graphLiveSize > 0 ? filter.count / graphLiveSize : 1;
10799
- if (selectivity < view.handles.filterThreshold) {
10800
- const candidates = candidatesOf(ordinalFilterValues(filter), filter);
10801
- return scanned(scoreCandidates(view.store, candidates, query, k, metric, minSimilarity));
10802
- }
10803
- }
10804
- let resolvedEveryHit = true;
10805
- const graphResults = [];
10806
- for (const hit of view.searchOrdinals(query, k, metric, minSimilarity, { filter, efSearch, oversample })) {
10807
- const docId = view.docIdOf(hit.ord);
10808
- if (docId === void 0) {
10809
- resolvedEveryHit = false;
10810
- continue;
10811
- }
10812
- graphResults.push({ docId, score: hit.score });
10813
- }
10814
- const unplaced = flags.unplacedOrdinals();
10815
- if (unplaced.length === 0) return fetched(graphResults, resolvedEveryHit);
10816
- const scan2 = scoreCandidates(view.store, candidatesOf(unplaced, filter), query, k, metric, minSimilarity);
10817
- return fetched(mergeResults(graphResults, scan2.results, k), resolvedEveryHit);
10818
- }
10819
- return {
10820
- fieldName,
10821
- dimension: view.handles.dimension,
10822
- partitionsKnown: () => true,
10823
- assignPartitions: () => void 0,
10824
- searchParallel(query, k, searchOptions) {
10825
- if (query.length !== view.handles.dimension) {
10826
- throw new NarsilError(
10827
- ErrorCodes.VECTOR_DIMENSION_MISMATCH,
10828
- `Vector dimension mismatch: expected ${view.handles.dimension}, got ${query.length}`,
10829
- { expected: view.handles.dimension, received: query.length }
10830
- );
10831
- }
10832
- const outcome = search2(query, k, searchOptions, filterFor(searchOptions));
10833
- if (view.graphRetired) {
10834
- throw new NarsilError(
10835
- ErrorCodes.INDEX_NOT_FOUND,
10836
- `The main thread replaced the graph of vector field "${fieldName}" while this thread searched it`,
10837
- { fieldName }
10838
- );
10839
- }
10840
- const results = outcome.results.filter((result) => holdsDocument(result.docId));
10841
- const holdsEveryHit = outcome.resolvedEveryHit && results.length === outcome.results.length;
10842
- return Promise.resolve({
10843
- results,
10844
- matched: outcome.matched,
10845
- matchedExact: outcome.matchedExact && holdsEveryHit
10846
- });
10847
- }
10848
- };
10849
- }
10850
-
10851
8200
  // src/workers/vector-copies.ts
10852
8201
  function createHeldVectorCopies() {
10853
8202
  return { searchers: /* @__PURE__ */ new Map(), fields: /* @__PURE__ */ new Map(), builds: /* @__PURE__ */ new Map() };
@@ -10858,6 +8207,7 @@ function fieldUnderHandle(held, fieldName, handle) {
10858
8207
  return held.builds.get(handle);
10859
8208
  }
10860
8209
  function loadHeldVectorCopy(held, manager, fieldName, handle, handles, threadSlot) {
8210
+ const { createSharedVectorSearcher, openSharedVectorField } = requireLoadedVectorCode();
10861
8211
  const existing = fieldUnderHandle(held, fieldName, handle);
10862
8212
  const view = existing?.view ?? openSharedVectorField(handles, threadSlot);
10863
8213
  if (existing !== void 0) view.adopt(handles);
@@ -10896,7 +8246,7 @@ function closeHeldVectorCopies(held) {
10896
8246
  held.fields.clear();
10897
8247
  held.searchers.clear();
10898
8248
  }
10899
- function yieldToEventLoop2() {
8249
+ function yieldToEventLoop() {
10900
8250
  return new Promise((resolve) => {
10901
8251
  if (typeof setImmediate === "function") setImmediate(resolve);
10902
8252
  else setTimeout(resolve, 0);
@@ -10907,7 +8257,7 @@ async function insertIntoHeldGraph(held, fieldName, handle, ordinals) {
10907
8257
  if (field === void 0) return null;
10908
8258
  for (const ordinal of ordinals) {
10909
8259
  field.view.insertOrdinal(ordinal);
10910
- await yieldToEventLoop2();
8260
+ await yieldToEventLoop();
10911
8261
  }
10912
8262
  return field.view.takeOutcome();
10913
8263
  }
@@ -10933,7 +8283,10 @@ function createDirectExecutor(options) {
10933
8283
  }
10934
8284
  function vectorIndexesFor(indexName, config) {
10935
8285
  const vectorIndexes = /* @__PURE__ */ new Map();
10936
- for (const [fieldPath, dim] of extractVectorFieldsFromSchema(config.schema)) {
8286
+ const vectorFields = extractVectorFieldsFromSchema(config.schema);
8287
+ if (vectorFields.size === 0) return vectorIndexes;
8288
+ const { createVectorIndex } = requireLoadedVectorCode();
8289
+ for (const [fieldPath, dim] of vectorFields) {
10937
8290
  vectorIndexes.set(
10938
8291
  fieldPath,
10939
8292
  createVectorIndex(
@@ -11024,6 +8377,8 @@ function createDirectExecutor(options) {
11024
8377
  );
11025
8378
  }
11026
8379
  case "createIndex": {
8380
+ const loading = loadVectorCodeFor(action.config.schema);
8381
+ if (loading !== null) await loading;
11027
8382
  const language = getLanguage(action.config.language ?? "english");
11028
8383
  addIndex(action.indexName, action.config, language, action.analysisStale ?? false, action.copyLoad ?? null);
11029
8384
  return void 0;
@@ -11038,6 +8393,7 @@ function createDirectExecutor(options) {
11038
8393
  return void 0;
11039
8394
  }
11040
8395
  case "loadVectorCopy": {
8396
+ if (loadedVectorCode() === null) await loadVectorCode();
11041
8397
  const entry = requireIndex(action.indexName);
11042
8398
  loadHeldVectorCopy(
11043
8399
  entry.vectorCopies,
@@ -11192,4 +8548,4 @@ function createDirectExecutor(options) {
11192
8548
  };
11193
8549
  }
11194
8550
 
11195
- export { CHECKSUM_YIELD_CHUNK_BYTES, CONSECUTIVE_TIMEOUTS_BEFORE_DEATH, DEFAULT_BACKPRESSURE_LIMIT, DEFAULT_REQUEST_TIMEOUT_MS, MAX_FILE_BYTES, NATIVE_CRC32_MIN_BYTES, SHUTDOWN_TIMEOUT_MS, VALID_HNSW_METRICS, VECTOR_FILE_MAX_VECTORS, VECTOR_FILE_PAYLOAD_VERSION, assembleGraphParts, buildSegmentPayload, bytesToVectors, countDead, createDirectExecutor, createFrozenSegment, createPartitionIndex, createPartitionManager, createPartitionRouter, createSharedFrozenSegment, deadBytesFor, decodeEveryStoredDocument, decodeMapWithoutFields, decodeStoredDocument, decodeVectorFilePayload, decodeVectorGraphPartPayload, decodeVectorGraphPayload, decodeVectorIndexParts, encodeSegmentState, freezeSegmentShared, generateId, getStopWords, getTokenizer, graphPartsOfEarlierPayload, hasStopWords, hasTokenizer, isCompositePartition, isDead, mergeFrozenSegments, mergeSegmentContents, missingWorkerEntry, nodeThreadId, partitionStateOf, readHeapStatistics, registerStopWords, registerTokenizer, resolveIndexAnalysis, resolvePartitionInsertOptions, resolveRequestThreadCount, resolveWorkerCount, resolveWorkerEntry, segmentArraysOf, selectionKeeps, shareSegmentArrays, spawnNodeWorker, splitWorkerBudget, survivorDocumentsOf, vectorsPerFile, vectorsToBytes, workerResourceLimits };
8551
+ export { buildSegmentPayload, createDirectExecutor, createFrozenSegment, createPartitionIndex, createPartitionManager, createPartitionRouter, createSharedFrozenSegment, decodeEveryStoredDocument, decodeMapWithoutFields, decodeStoredDocument, encodeSegmentState, freezeSegmentShared, generateId, getStopWords, getTokenizer, hasStopWords, hasTokenizer, isCompositePartition, mergeFrozenSegments, mergeSegmentContents, partitionStateOf, readHeapStatistics, registerStopWords, registerTokenizer, resolveIndexAnalysis, resolvePartitionInsertOptions, segmentArraysOf, selectionKeeps, shareSegmentArrays, survivorDocumentsOf };