@delali/narsil 0.3.1-next.84 → 0.3.1-next.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-RZXNWQSM.mjs → chunk-B3F7LZXM.mjs} +610 -205
- package/dist/{chunk-XIQZKKTK.mjs → chunk-IDLP54FH.mjs} +39 -10
- package/dist/{chunk-B3UOQJD3.mjs → chunk-NFF5TDRG.mjs} +1 -1
- package/dist/{chunk-XZF4LZ3X.mjs → chunk-RM6FRYEC.mjs} +31 -6
- package/dist/{chunk-IA67SOJO.mjs → chunk-XPK3GB2P.mjs} +271 -93
- package/dist/{chunk-POWOTUKJ.mjs → chunk-YRX6XMOQ.mjs} +1 -0
- package/dist/{direct-executor-6WWFY5EI.mjs → direct-executor-SI2QV4YI.mjs} +3 -3
- package/dist/distribution.mjs +5 -5
- package/dist/index.browser.mjs +782 -173
- package/dist/index.mjs +5 -5
- package/dist/persistence/durability/checkpoint-worker.mjs +5 -5
- package/dist/server.mjs +31 -16
- package/dist/{thread-33M5AOEI.mjs → thread-EUUNJNKU.mjs} +226 -41
- package/dist/vector/search-worker.d.ts +8 -0
- package/dist/vector/search-worker.mjs +2 -2
- package/dist/workers/entry.mjs +4 -4
- package/package.json +7 -7
package/dist/index.browser.mjs
CHANGED
|
@@ -4875,10 +4875,10 @@ function firstAtOrAfter2(sorted, key) {
|
|
|
4875
4875
|
}
|
|
4876
4876
|
return lo;
|
|
4877
4877
|
}
|
|
4878
|
-
function settledColumn(column,
|
|
4878
|
+
function settledColumn(column, holds2) {
|
|
4879
4879
|
const { keys, values } = column;
|
|
4880
4880
|
const order = [];
|
|
4881
|
-
for (let at = 0; at < keys.length; at++) if (
|
|
4881
|
+
for (let at = 0; at < keys.length; at++) if (holds2(values[at])) order.push(at);
|
|
4882
4882
|
order.sort((left, right) => compareUnits(keys[left], keys[right]) || compareUnits(values[left], values[right]));
|
|
4883
4883
|
const settled2 = { keys: [], values: [] };
|
|
4884
4884
|
for (const at of order) {
|
|
@@ -4890,8 +4890,8 @@ function settledColumn(column, holds) {
|
|
|
4890
4890
|
return settled2;
|
|
4891
4891
|
}
|
|
4892
4892
|
var ValueAffixes = class {
|
|
4893
|
-
constructor(
|
|
4894
|
-
this.holds =
|
|
4893
|
+
constructor(holds2, values) {
|
|
4894
|
+
this.holds = holds2;
|
|
4895
4895
|
for (const value of values) this.add(value);
|
|
4896
4896
|
}
|
|
4897
4897
|
forward = { keys: [], values: [] };
|
|
@@ -6792,7 +6792,7 @@ function withinBounds(value, bounds) {
|
|
|
6792
6792
|
}
|
|
6793
6793
|
return true;
|
|
6794
6794
|
}
|
|
6795
|
-
function selectTextRange(column, requested, capacity,
|
|
6795
|
+
function selectTextRange(column, requested, capacity, isLive3) {
|
|
6796
6796
|
const bounds = {
|
|
6797
6797
|
...requested,
|
|
6798
6798
|
lower: requested.lower === null ? null : truncateSortString(requested.lower),
|
|
@@ -6813,7 +6813,7 @@ function selectTextRange(column, requested, capacity, isLive2) {
|
|
|
6813
6813
|
}
|
|
6814
6814
|
for (let position = start; position < end; position++) {
|
|
6815
6815
|
const internalId = ordered[position];
|
|
6816
|
-
if (!column.isDirty(internalId) &&
|
|
6816
|
+
if (!column.isDirty(internalId) && isLive3(internalId)) bitsetSet(result, internalId);
|
|
6817
6817
|
}
|
|
6818
6818
|
for (const internalId of column.dirtyStream().present) {
|
|
6819
6819
|
if (withinBounds(column.valueOf(internalId), bounds)) bitsetSet(result, internalId);
|
|
@@ -11753,6 +11753,7 @@ var VECTOR_STORE_INITIAL_CAPACITY = 16;
|
|
|
11753
11753
|
var VECTOR_BLOCK_INITIAL_SLOTS = 256;
|
|
11754
11754
|
var DISK_STORAGE_BLOCK_BYTES = 67108864;
|
|
11755
11755
|
var MAX_VECTOR_ORDINALS = 16777216;
|
|
11756
|
+
var UNPLACED_FLAG_CHUNK_ORDINALS = 1024;
|
|
11756
11757
|
var VECTOR_SCRATCH_SLOTS = 64;
|
|
11757
11758
|
var SCRATCH_SLOTS_PER_THREAD_POOL = 31;
|
|
11758
11759
|
var MAX_DOC_ID_TABLE_BYTES = 1073741824;
|
|
@@ -12145,9 +12146,16 @@ function openVectorBlock(handle, threadSlot) {
|
|
|
12145
12146
|
const simd = storage.kind === "shared-memory" ? createSharedArenaSimd(storage.memory) : storage.kind === "private-memory" ? storage.simd : null;
|
|
12146
12147
|
const hasScratch = simd !== null && threadSlot >= 0 && threadSlot < VECTOR_SCRATCH_SLOTS;
|
|
12147
12148
|
const scratchSlot = hasScratch ? threadSlot : 0;
|
|
12148
|
-
let float32View
|
|
12149
|
-
let byteView
|
|
12150
|
-
let dataView
|
|
12149
|
+
let float32View;
|
|
12150
|
+
let byteView;
|
|
12151
|
+
let dataView;
|
|
12152
|
+
function rebindViews() {
|
|
12153
|
+
const buffer = blockBuffer(handle);
|
|
12154
|
+
float32View = fixedView(buffer, Float32Array);
|
|
12155
|
+
byteView = fixedView(buffer, Uint8Array);
|
|
12156
|
+
dataView = new DataView(buffer, 0, byteView.length);
|
|
12157
|
+
}
|
|
12158
|
+
rebindViews();
|
|
12151
12159
|
return {
|
|
12152
12160
|
handle,
|
|
12153
12161
|
simd,
|
|
@@ -12155,15 +12163,15 @@ function openVectorBlock(handle, threadSlot) {
|
|
|
12155
12163
|
scratchByteOffset: scratchSlot * layout.scratchStride,
|
|
12156
12164
|
stagedOrdinal: NOTHING_STAGED,
|
|
12157
12165
|
float32(neededLength) {
|
|
12158
|
-
if (neededLength > float32View.length)
|
|
12166
|
+
if (neededLength > float32View.length) rebindViews();
|
|
12159
12167
|
return float32View;
|
|
12160
12168
|
},
|
|
12161
12169
|
bytes(neededLength) {
|
|
12162
|
-
if (neededLength > byteView.length)
|
|
12170
|
+
if (neededLength > byteView.length) rebindViews();
|
|
12163
12171
|
return byteView;
|
|
12164
12172
|
},
|
|
12165
12173
|
data(neededLength) {
|
|
12166
|
-
if (neededLength >
|
|
12174
|
+
if (neededLength > byteView.length) rebindViews();
|
|
12167
12175
|
return dataView;
|
|
12168
12176
|
}
|
|
12169
12177
|
};
|
|
@@ -12298,6 +12306,8 @@ var STORE_DOC_ID_BYTES = 5;
|
|
|
12298
12306
|
var STORE_CODE_BLOCK_COUNT = 6;
|
|
12299
12307
|
var STORE_CALIBRATION_GENERATION = 7;
|
|
12300
12308
|
var STORE_RELEASED_VECTORS_TO_DISK = 8;
|
|
12309
|
+
var STORE_UNPLACED_COUNT = 9;
|
|
12310
|
+
var STORE_REMOVED_COUNT = 10;
|
|
12301
12311
|
var STORE_HEADER_WORDS = 16;
|
|
12302
12312
|
var IN_MEMORY = -1;
|
|
12303
12313
|
function fixedInt32(words) {
|
|
@@ -12306,6 +12316,9 @@ function fixedInt32(words) {
|
|
|
12306
12316
|
function fixedFloat32(length) {
|
|
12307
12317
|
return new Float32Array(createFixedBuffer(length * 4), 0, length);
|
|
12308
12318
|
}
|
|
12319
|
+
function unplacedChunkCount(ordinals) {
|
|
12320
|
+
return Math.floor(ordinals / UNPLACED_FLAG_CHUNK_ORDINALS) + 1;
|
|
12321
|
+
}
|
|
12309
12322
|
function perOrdinal(bytesPerOrdinal) {
|
|
12310
12323
|
return createGrowableBuffer(
|
|
12311
12324
|
VECTOR_STORE_INITIAL_CAPACITY * bytesPerOrdinal,
|
|
@@ -12330,6 +12343,9 @@ function createSharedVectorStoreHandles(dimension, codeBits, blockBytes2 = BLOCK
|
|
|
12330
12343
|
vectorFiles: [],
|
|
12331
12344
|
magnitudes: perOrdinal(8),
|
|
12332
12345
|
present: perOrdinal(1),
|
|
12346
|
+
unplaced: perOrdinal(1),
|
|
12347
|
+
unplacedChunks: createGrowableBuffer(4, unplacedChunkCount(MAX_VECTOR_ORDINALS) * 4),
|
|
12348
|
+
removed: perOrdinal(1),
|
|
12333
12349
|
partitions: perOrdinal(4),
|
|
12334
12350
|
docIdBytes: createGrowableBuffer(VECTOR_STORE_INITIAL_CAPACITY * 16, MAX_DOC_ID_TABLE_BYTES),
|
|
12335
12351
|
docIdOffsets: perOrdinal(4),
|
|
@@ -12347,6 +12363,9 @@ function sharedVectorStoreBytes(handles) {
|
|
|
12347
12363
|
for (const block of handles.codeBlocks) bytes += blockBuffer(block).byteLength;
|
|
12348
12364
|
bytes += handles.magnitudes.byteLength;
|
|
12349
12365
|
bytes += handles.present.byteLength;
|
|
12366
|
+
bytes += handles.unplaced.byteLength;
|
|
12367
|
+
bytes += handles.unplacedChunks.byteLength;
|
|
12368
|
+
bytes += handles.removed.byteLength;
|
|
12350
12369
|
bytes += handles.partitions.byteLength;
|
|
12351
12370
|
bytes += handles.docIdBytes.byteLength;
|
|
12352
12371
|
bytes += handles.docIdOffsets.byteLength;
|
|
@@ -12600,7 +12619,7 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12600
12619
|
let centroidGeneration = -1;
|
|
12601
12620
|
let currentQuery = null;
|
|
12602
12621
|
const singleOrdinal = new Int32Array(1);
|
|
12603
|
-
function
|
|
12622
|
+
function reach2(ordinal) {
|
|
12604
12623
|
if (ordinal < codePresent.length) return true;
|
|
12605
12624
|
if (ordinal >= store.handles.codePresent.byteLength) return false;
|
|
12606
12625
|
codePresent = fixedView(store.handles.codePresent, Uint8Array);
|
|
@@ -12621,8 +12640,8 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12621
12640
|
if (generation !== centroidGeneration) recomputeCentroidDot(generation);
|
|
12622
12641
|
return centroidDot;
|
|
12623
12642
|
}
|
|
12624
|
-
function
|
|
12625
|
-
return ordinal >= 0 &&
|
|
12643
|
+
function holds2(ordinal) {
|
|
12644
|
+
return ordinal >= 0 && reach2(ordinal) && codePresent[ordinal] === 1;
|
|
12626
12645
|
}
|
|
12627
12646
|
function recordOffset(ordinal) {
|
|
12628
12647
|
return slotByteOffset(layout, ordinal % capacity);
|
|
@@ -12643,7 +12662,7 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12643
12662
|
}
|
|
12644
12663
|
}
|
|
12645
12664
|
function writeRecordOf(ordinal) {
|
|
12646
|
-
if (!
|
|
12665
|
+
if (!reach2(ordinal) || !store.holdsOrdinal(ordinal)) return;
|
|
12647
12666
|
const code = osqQuantize(store.vectorAt(ordinal), store.handles.centroid, bits, metric, scratch);
|
|
12648
12667
|
const block = store.codeBlockOf(ordinal);
|
|
12649
12668
|
const offset = recordOffset(ordinal);
|
|
@@ -12713,7 +12732,7 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12713
12732
|
return currentQuery;
|
|
12714
12733
|
}
|
|
12715
12734
|
function distanceBetween(ordA, ordB) {
|
|
12716
|
-
if (!
|
|
12735
|
+
if (!holds2(ordA) || !holds2(ordB)) return Number.POSITIVE_INFINITY;
|
|
12717
12736
|
const blockA = store.codeBlockOf(ordA);
|
|
12718
12737
|
const blockB = store.codeBlockOf(ordB);
|
|
12719
12738
|
const offsetA = recordOffset(ordA);
|
|
@@ -12741,7 +12760,7 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12741
12760
|
return isCalibrated() ? store.handles.centroid : null;
|
|
12742
12761
|
},
|
|
12743
12762
|
isCalibrated,
|
|
12744
|
-
holdsOrdinal:
|
|
12763
|
+
holdsOrdinal: holds2,
|
|
12745
12764
|
writeCodes(ordinal) {
|
|
12746
12765
|
singleOrdinal[0] = ordinal;
|
|
12747
12766
|
writeCodesOf(singleOrdinal);
|
|
@@ -12755,7 +12774,7 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12755
12774
|
return true;
|
|
12756
12775
|
},
|
|
12757
12776
|
restoreRecord(ordinal, record) {
|
|
12758
|
-
if (!
|
|
12777
|
+
if (!reach2(ordinal) || record.length !== layout.slotStride) return;
|
|
12759
12778
|
const block = store.codeBlockOf(ordinal);
|
|
12760
12779
|
const offset = recordOffset(ordinal);
|
|
12761
12780
|
block.bytes(offset + layout.slotStride).set(record, offset);
|
|
@@ -12767,7 +12786,7 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12767
12786
|
return block.bytes(offset + layout.slotStride).subarray(offset, offset + layout.slotStride);
|
|
12768
12787
|
},
|
|
12769
12788
|
clearCodes(ordinal) {
|
|
12770
|
-
if (ordinal < 0 || !
|
|
12789
|
+
if (ordinal < 0 || !reach2(ordinal)) return;
|
|
12771
12790
|
if (Atomics.exchange(codePresent, ordinal, 0) === 1) Atomics.sub(store.handles.header, STORE_CODE_COUNT, 1);
|
|
12772
12791
|
},
|
|
12773
12792
|
writeCentroid,
|
|
@@ -12783,21 +12802,21 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12783
12802
|
},
|
|
12784
12803
|
prepareQuery,
|
|
12785
12804
|
distanceFromPreparedByOrdinal(prepared, ordinal) {
|
|
12786
|
-
if (!
|
|
12805
|
+
if (!holds2(ordinal)) return Number.POSITIVE_INFINITY;
|
|
12787
12806
|
return estimateDistance(ordinal, prepared);
|
|
12788
12807
|
},
|
|
12789
12808
|
preparedDistance(prepared) {
|
|
12790
|
-
const generic = (ordinal) =>
|
|
12809
|
+
const generic = (ordinal) => holds2(ordinal) ? estimateDistance(ordinal, prepared) : Number.POSITIVE_INFINITY;
|
|
12791
12810
|
if (store.handles.codeBlocks.length === 0) return generic;
|
|
12792
12811
|
const block = store.codeBlockOf(0);
|
|
12793
12812
|
const simd = block.simd;
|
|
12794
12813
|
if (simd === null || !block.hasScratch) return generic;
|
|
12795
12814
|
const presentView = codePresent;
|
|
12796
12815
|
const limit = Math.min(presentView.length, capacity);
|
|
12797
|
-
const
|
|
12816
|
+
const { slotsOffset, slotStride } = layout;
|
|
12817
|
+
const data = block.data(slotsOffset + slotStride);
|
|
12798
12818
|
const dataLength = data.byteLength;
|
|
12799
12819
|
const queryOffset = block.scratchByteOffset;
|
|
12800
|
-
const { slotsOffset, slotStride } = layout;
|
|
12801
12820
|
return (ordinal) => {
|
|
12802
12821
|
if (ordinal < 0 || ordinal >= limit || block.stagedOrdinal !== QUERY_STAGED) return generic(ordinal);
|
|
12803
12822
|
if (presentView[ordinal] !== 1) return Number.POSITIVE_INFINITY;
|
|
@@ -12826,9 +12845,9 @@ function openSharedQuantizer(store, layout, metric) {
|
|
|
12826
12845
|
if (simd === null) return distanceBetween;
|
|
12827
12846
|
const presentView = codePresent;
|
|
12828
12847
|
const limit = Math.min(presentView.length, capacity);
|
|
12829
|
-
const data = block.data(0);
|
|
12830
|
-
const dataLength = data.byteLength;
|
|
12831
12848
|
const { slotsOffset, slotStride } = layout;
|
|
12849
|
+
const data = block.data(slotsOffset + slotStride);
|
|
12850
|
+
const dataLength = data.byteLength;
|
|
12832
12851
|
return (ordA, ordB) => {
|
|
12833
12852
|
if (ordA < 0 || ordB < 0 || ordA >= limit || ordB >= limit) return distanceBetween(ordA, ordB);
|
|
12834
12853
|
if (presentView[ordA] !== 1 || presentView[ordB] !== 1) return Number.POSITIVE_INFINITY;
|
|
@@ -13261,7 +13280,7 @@ function openSharedVectorStore(initial, threadSlot) {
|
|
|
13261
13280
|
const index = Math.floor(ordinal / capacity);
|
|
13262
13281
|
return index < handles.blocks.length && handles.blocks[index] !== null ? HELD_HOT : NOT_HELD;
|
|
13263
13282
|
}
|
|
13264
|
-
function
|
|
13283
|
+
function holds2(ordinal) {
|
|
13265
13284
|
return holdState(ordinal) !== NOT_HELD;
|
|
13266
13285
|
}
|
|
13267
13286
|
function readFromFile(file, offset, target) {
|
|
@@ -13292,7 +13311,7 @@ function openSharedVectorStore(initial, threadSlot) {
|
|
|
13292
13311
|
if (block.stagedOrdinal === ordinal) return true;
|
|
13293
13312
|
const source = vectorFor(ordinal, coldScratchA);
|
|
13294
13313
|
if (source === null) return false;
|
|
13295
|
-
block.float32(
|
|
13314
|
+
block.float32(block.scratchByteOffset / 4 + dimension).set(source, block.scratchByteOffset / 4);
|
|
13296
13315
|
block.stagedOrdinal = ordinal;
|
|
13297
13316
|
return true;
|
|
13298
13317
|
}
|
|
@@ -13306,7 +13325,7 @@ function openSharedVectorStore(initial, threadSlot) {
|
|
|
13306
13325
|
function stageQuery(block) {
|
|
13307
13326
|
if (currentQuery === null) return false;
|
|
13308
13327
|
if (block.stagedOrdinal !== QUERY_STAGED) {
|
|
13309
|
-
block.float32(
|
|
13328
|
+
block.float32(block.scratchByteOffset / 4 + dimension).set(currentQuery, block.scratchByteOffset / 4);
|
|
13310
13329
|
block.stagedOrdinal = QUERY_STAGED;
|
|
13311
13330
|
}
|
|
13312
13331
|
return true;
|
|
@@ -13341,7 +13360,7 @@ function openSharedVectorStore(initial, threadSlot) {
|
|
|
13341
13360
|
reader?.retainOnly(new Set(next.vectorFiles.filter((path) => path.length > 0)));
|
|
13342
13361
|
},
|
|
13343
13362
|
rebind,
|
|
13344
|
-
holdsOrdinal:
|
|
13363
|
+
holdsOrdinal: holds2,
|
|
13345
13364
|
isCold,
|
|
13346
13365
|
readColdInto,
|
|
13347
13366
|
readFromFile,
|
|
@@ -13382,7 +13401,7 @@ function openSharedVectorStore(initial, threadSlot) {
|
|
|
13382
13401
|
return magnitudes[ordinal];
|
|
13383
13402
|
},
|
|
13384
13403
|
entryForOrdinal(ordinal) {
|
|
13385
|
-
if (!
|
|
13404
|
+
if (!holds2(ordinal)) return void 0;
|
|
13386
13405
|
return { vector: view.vectorAt(ordinal), magnitude: magnitudes[ordinal] };
|
|
13387
13406
|
},
|
|
13388
13407
|
docIdAt(ordinal) {
|
|
@@ -13404,7 +13423,7 @@ function openSharedVectorStore(initial, threadSlot) {
|
|
|
13404
13423
|
return partition < 0 ? void 0 : partition;
|
|
13405
13424
|
},
|
|
13406
13425
|
distanceByOrdinal(ordA, ordB, metric) {
|
|
13407
|
-
if (!
|
|
13426
|
+
if (!holds2(ordA) || !holds2(ordB)) return Number.POSITIVE_INFINITY;
|
|
13408
13427
|
if (!isCold(ordB)) {
|
|
13409
13428
|
const indexA = ordA / capacity | 0;
|
|
13410
13429
|
const indexB = ordB / capacity | 0;
|
|
@@ -13541,6 +13560,9 @@ function ensureOrdinal(store, ordinal, cold) {
|
|
|
13541
13560
|
const { handles } = store;
|
|
13542
13561
|
growBufferTo(handles.magnitudes, (ordinal + 1) * 8);
|
|
13543
13562
|
growBufferTo(handles.present, ordinal + 1);
|
|
13563
|
+
growBufferTo(handles.unplaced, ordinal + 1);
|
|
13564
|
+
growBufferTo(handles.unplacedChunks, unplacedChunkCount(ordinal) * 4);
|
|
13565
|
+
growBufferTo(handles.removed, ordinal + 1);
|
|
13544
13566
|
growBufferTo(handles.partitions, (ordinal + 1) * 4);
|
|
13545
13567
|
growBufferTo(handles.codePresent, ordinal + 1);
|
|
13546
13568
|
const diskFileBytes = handles.diskFile.byteLength;
|
|
@@ -13917,8 +13939,11 @@ var GRAPH_M = 2;
|
|
|
13917
13939
|
var GRAPH_MMAX0 = 3;
|
|
13918
13940
|
var GRAPH_EF_CONSTRUCTION = 4;
|
|
13919
13941
|
var GRAPH_METRIC = 5;
|
|
13942
|
+
var GRAPH_RETIRED = 6;
|
|
13920
13943
|
var GRAPH_NODE_COUNT = 32;
|
|
13921
13944
|
var GRAPH_TOMBSTONE_COUNT = 33;
|
|
13945
|
+
var GRAPH_UNLINKED_NODES = 34;
|
|
13946
|
+
var GRAPH_INCOMING_UNCOUNTED = 35;
|
|
13922
13947
|
var GRAPH_UPPER_USED = 64;
|
|
13923
13948
|
var GRAPH_SLOTS = 65;
|
|
13924
13949
|
var GRAPH_UPPER_CAPACITY = 66;
|
|
@@ -13956,6 +13981,7 @@ function createSharedGraphHandles(shape) {
|
|
|
13956
13981
|
upper: perOrdinal2(upperStride * 4, INITIAL_UPPER_SLOTS),
|
|
13957
13982
|
locks: perOrdinal2(4, VECTOR_STORE_INITIAL_CAPACITY),
|
|
13958
13983
|
tombstones: perOrdinal2(1, VECTOR_STORE_INITIAL_CAPACITY),
|
|
13984
|
+
incoming: perOrdinal2(4, VECTOR_STORE_INITIAL_CAPACITY),
|
|
13959
13985
|
heldLocks: new Int32Array(createFixedBuffer(heldWords * 4), 0, heldWords)
|
|
13960
13986
|
};
|
|
13961
13987
|
publishGraphCapacity(handles);
|
|
@@ -13975,7 +14001,8 @@ function publishGraphCapacity(handles) {
|
|
|
13975
14001
|
Math.floor(handles.level0.byteLength / (level0Stride * 4)),
|
|
13976
14002
|
Math.floor(handles.upperBase.byteLength / 4),
|
|
13977
14003
|
Math.floor(handles.locks.byteLength / 4),
|
|
13978
|
-
handles.tombstones.byteLength
|
|
14004
|
+
handles.tombstones.byteLength,
|
|
14005
|
+
Math.floor(handles.incoming.byteLength / 4)
|
|
13979
14006
|
);
|
|
13980
14007
|
raiseHeaderWord(handles.header, GRAPH_SLOT_CAPACITY, slotCapacity);
|
|
13981
14008
|
raiseHeaderWord(handles.header, GRAPH_UPPER_CAPACITY, Math.floor(handles.upper.byteLength / 4));
|
|
@@ -14002,11 +14029,13 @@ function openAdjacency(handles, m, mMax0) {
|
|
|
14002
14029
|
nodeLevels: fixedView(handles.nodeLevels, Uint8Array),
|
|
14003
14030
|
level0: fixedView(handles.level0, Int32Array),
|
|
14004
14031
|
upperBase: fixedView(handles.upperBase, Int32Array),
|
|
14005
|
-
upper: fixedView(handles.upper, Int32Array)
|
|
14032
|
+
upper: fixedView(handles.upper, Int32Array),
|
|
14033
|
+
incoming: fixedView(handles.incoming, Int32Array)
|
|
14006
14034
|
};
|
|
14007
14035
|
}
|
|
14008
14036
|
function rebindNodes(adj) {
|
|
14009
14037
|
adj.nodeLevels = fixedView(adj.handles.nodeLevels, Uint8Array);
|
|
14038
|
+
adj.incoming = fixedView(adj.handles.incoming, Int32Array);
|
|
14010
14039
|
adj.level0 = fixedView(adj.handles.level0, Int32Array);
|
|
14011
14040
|
adj.upperBase = fixedView(adj.handles.upperBase, Int32Array);
|
|
14012
14041
|
}
|
|
@@ -14043,6 +14072,7 @@ function ensureAdjacencyCapacity(adj, needed) {
|
|
|
14043
14072
|
growBufferToExactly(handles.upperBase, capacity * 4);
|
|
14044
14073
|
growBufferToExactly(handles.locks, capacity * 4);
|
|
14045
14074
|
growBufferToExactly(handles.tombstones, capacity);
|
|
14075
|
+
growBufferToExactly(handles.incoming, capacity * 4);
|
|
14046
14076
|
growBufferToExactly(handles.nodeLevels, capacity);
|
|
14047
14077
|
publishGraphCapacity(handles);
|
|
14048
14078
|
rebindNodes(adj);
|
|
@@ -14089,10 +14119,12 @@ function deleteNode(adj, ord) {
|
|
|
14089
14119
|
Atomics.store(adj.nodeLevels, ord, 0);
|
|
14090
14120
|
adj.upperBase[ord] = 0;
|
|
14091
14121
|
adj.level0[ord * adj.level0Stride] = 0;
|
|
14122
|
+
Atomics.store(adj.incoming, ord, 0);
|
|
14092
14123
|
}
|
|
14093
14124
|
function resetAdjacency(adj) {
|
|
14094
14125
|
rebindNodes(adj);
|
|
14095
14126
|
rebindUpper(adj);
|
|
14127
|
+
adj.incoming.fill(0);
|
|
14096
14128
|
adj.nodeLevels.fill(0);
|
|
14097
14129
|
adj.level0.fill(0);
|
|
14098
14130
|
adj.upperBase.fill(0);
|
|
@@ -14109,7 +14141,51 @@ function hasNode(adj, ord) {
|
|
|
14109
14141
|
}
|
|
14110
14142
|
function graphBytes(adj) {
|
|
14111
14143
|
const handles = adj.handles;
|
|
14112
|
-
return handles.header.byteLength + handles.nodeLevels.byteLength + handles.level0.byteLength + handles.upperBase.byteLength + handles.upper.byteLength + handles.locks.byteLength + handles.tombstones.byteLength + handles.heldLocks.byteLength;
|
|
14144
|
+
return handles.header.byteLength + handles.nodeLevels.byteLength + handles.level0.byteLength + handles.upperBase.byteLength + handles.upper.byteLength + handles.locks.byteLength + handles.tombstones.byteLength + handles.incoming.byteLength + handles.heldLocks.byteLength;
|
|
14145
|
+
}
|
|
14146
|
+
|
|
14147
|
+
// src/vector/hnsw/adjacency/incoming.ts
|
|
14148
|
+
function incomingCount(adj, ord) {
|
|
14149
|
+
if (ord < 0 || !reachNode(adj, ord)) return 0;
|
|
14150
|
+
return Atomics.load(adj.incoming, ord);
|
|
14151
|
+
}
|
|
14152
|
+
function noteLinked(adj, ord) {
|
|
14153
|
+
if (ord < 0 || !reachNode(adj, ord)) return;
|
|
14154
|
+
Atomics.add(adj.incoming, ord, 1);
|
|
14155
|
+
}
|
|
14156
|
+
function noteUnlinked(adj, ord) {
|
|
14157
|
+
if (ord < 0 || !reachNode(adj, ord)) return 0;
|
|
14158
|
+
for (; ; ) {
|
|
14159
|
+
const seen = Atomics.load(adj.incoming, ord);
|
|
14160
|
+
if (seen <= 0) return 0;
|
|
14161
|
+
if (Atomics.compareExchange(adj.incoming, ord, seen, seen - 1) === seen) return seen - 1;
|
|
14162
|
+
}
|
|
14163
|
+
}
|
|
14164
|
+
function releaseUnlessLast(adj, ord) {
|
|
14165
|
+
if (ord < 0 || !reachNode(adj, ord)) return true;
|
|
14166
|
+
for (; ; ) {
|
|
14167
|
+
const seen = Atomics.load(adj.incoming, ord);
|
|
14168
|
+
if (seen <= 1) return false;
|
|
14169
|
+
if (Atomics.compareExchange(adj.incoming, ord, seen, seen - 1) === seen) return true;
|
|
14170
|
+
}
|
|
14171
|
+
}
|
|
14172
|
+
function holdsNodeAt(adj, ord) {
|
|
14173
|
+
return ord >= 0 && ord < adj.nodeLevels.length && adj.nodeLevels[ord] !== 0;
|
|
14174
|
+
}
|
|
14175
|
+
function countLinksOutOf(adj, ord, step) {
|
|
14176
|
+
if (!holdsNodeAt(adj, ord)) return;
|
|
14177
|
+
const incoming = adj.incoming;
|
|
14178
|
+
const level0 = adj.level0;
|
|
14179
|
+
const base = ord * adj.level0Stride;
|
|
14180
|
+
const count = level0[base];
|
|
14181
|
+
for (let i = 1; i <= count; i++) {
|
|
14182
|
+
const neighbour = level0[base + i];
|
|
14183
|
+
if (neighbour === ord || !holdsNodeAt(adj, neighbour)) continue;
|
|
14184
|
+
if (incoming[neighbour] + step >= 0) incoming[neighbour] += step;
|
|
14185
|
+
}
|
|
14186
|
+
}
|
|
14187
|
+
function countEveryLink(adj, slots) {
|
|
14188
|
+
for (let ord = 0; ord < slots; ord++) countLinksOutOf(adj, ord, 1);
|
|
14113
14189
|
}
|
|
14114
14190
|
|
|
14115
14191
|
// src/vector/hnsw/adjacency/neighbors.ts
|
|
@@ -14159,6 +14235,7 @@ function addNeighbor(adj, ord, layer, neighborOrd) {
|
|
|
14159
14235
|
if (count + 1 > stride - 1) return;
|
|
14160
14236
|
array[base + count + 1] = neighborOrd;
|
|
14161
14237
|
array[base] = count + 1;
|
|
14238
|
+
if (layer === 0) noteLinked(adj, neighborOrd);
|
|
14162
14239
|
}
|
|
14163
14240
|
function removeNeighbor(adj, ord, layer, neighborOrd) {
|
|
14164
14241
|
const base = layerBase(adj, ord, layer);
|
|
@@ -14171,10 +14248,11 @@ function removeNeighbor(adj, ord, layer, neighborOrd) {
|
|
|
14171
14248
|
array[base + j] = array[base + j + 1];
|
|
14172
14249
|
}
|
|
14173
14250
|
array[base] = count - 1;
|
|
14251
|
+
if (layer === 0) noteUnlinked(adj, neighborOrd);
|
|
14174
14252
|
return;
|
|
14175
14253
|
}
|
|
14176
14254
|
}
|
|
14177
|
-
function
|
|
14255
|
+
function writeNeighbors(adj, ord, layer, neighbors, count) {
|
|
14178
14256
|
const base = layerBase(adj, ord, layer);
|
|
14179
14257
|
if (base === ABSENT) return;
|
|
14180
14258
|
const array = layerArray(adj, layer);
|
|
@@ -14185,6 +14263,24 @@ function replaceNeighbors(adj, ord, layer, neighbors, count) {
|
|
|
14185
14263
|
}
|
|
14186
14264
|
array[base] = limit;
|
|
14187
14265
|
}
|
|
14266
|
+
function holds(neighbors, count, ord) {
|
|
14267
|
+
for (let i = 0; i < count; i++) if (neighbors[i] === ord) return true;
|
|
14268
|
+
return false;
|
|
14269
|
+
}
|
|
14270
|
+
function replaceNeighbors(adj, ord, layer, neighbors, count) {
|
|
14271
|
+
const base = layerBase(adj, ord, layer);
|
|
14272
|
+
if (base === ABSENT) return;
|
|
14273
|
+
if (layer > 0) {
|
|
14274
|
+
writeNeighbors(adj, ord, layer, neighbors, count);
|
|
14275
|
+
return;
|
|
14276
|
+
}
|
|
14277
|
+
const array = adj.level0;
|
|
14278
|
+
const held = array.slice(base + 1, base + 1 + array[base]);
|
|
14279
|
+
const limit = Math.min(count, adj.level0Stride - 1);
|
|
14280
|
+
for (const former of held) if (!holds(neighbors, limit, former)) noteUnlinked(adj, former);
|
|
14281
|
+
for (let i = 0; i < limit; i++) if (!holds(held, held.length, neighbors[i])) noteLinked(adj, neighbors[i]);
|
|
14282
|
+
writeNeighbors(adj, ord, 0, neighbors, count);
|
|
14283
|
+
}
|
|
14188
14284
|
|
|
14189
14285
|
// src/vector/hnsw/locks.ts
|
|
14190
14286
|
var FREE = 0;
|
|
@@ -14415,6 +14511,17 @@ function nativeCompact(state, field, metric) {
|
|
|
14415
14511
|
const compact2 = () => field.core.compact(field.graph, field.store.handle, nativeMetricCode(metric), state.locks.threadSlot, field.wakes);
|
|
14416
14512
|
return writeToTheGraph(state, field, compact2) === NATIVE_OK;
|
|
14417
14513
|
}
|
|
14514
|
+
function nativeConnect(state, field, metric, nearby) {
|
|
14515
|
+
const connect = () => field.core.connectUnreached(
|
|
14516
|
+
field.graph,
|
|
14517
|
+
field.store.handle,
|
|
14518
|
+
nativeMetricCode(metric),
|
|
14519
|
+
nearby,
|
|
14520
|
+
state.locks.threadSlot,
|
|
14521
|
+
field.wakes
|
|
14522
|
+
);
|
|
14523
|
+
return writeToTheGraph(state, field, connect) === NATIVE_OK;
|
|
14524
|
+
}
|
|
14418
14525
|
|
|
14419
14526
|
// src/vector/hnsw/shared.ts
|
|
14420
14527
|
function entryPointOf(state) {
|
|
@@ -14554,7 +14661,8 @@ function createHNSWWorkspace() {
|
|
|
14554
14661
|
pruneCandidates: createList(INITIAL_LIST_CAPACITY),
|
|
14555
14662
|
pruneSelection: createList(INITIAL_LIST_CAPACITY),
|
|
14556
14663
|
repairCandidates: createList(INITIAL_LIST_CAPACITY),
|
|
14557
|
-
repairSelection: createList(INITIAL_LIST_CAPACITY)
|
|
14664
|
+
repairSelection: createList(INITIAL_LIST_CAPACITY),
|
|
14665
|
+
droppedLinks: createList(INITIAL_LIST_CAPACITY)
|
|
14558
14666
|
};
|
|
14559
14667
|
}
|
|
14560
14668
|
function linkSelectionsFor(workspace, layers) {
|
|
@@ -14712,6 +14820,260 @@ function setEntryPointsFromList(workspace, list) {
|
|
|
14712
14820
|
workspace.entryPointCount = list.size;
|
|
14713
14821
|
}
|
|
14714
14822
|
|
|
14823
|
+
// src/vector/hnsw/last-links.ts
|
|
14824
|
+
function isLive(state, ord) {
|
|
14825
|
+
return nodeExists(state, ord) && !isTombstoned(state, ord);
|
|
14826
|
+
}
|
|
14827
|
+
function noteNodeLeftUnlinked(state) {
|
|
14828
|
+
Atomics.store(state.header, GRAPH_UNLINKED_NODES, 1);
|
|
14829
|
+
}
|
|
14830
|
+
function listHolds(list, ord) {
|
|
14831
|
+
for (let i = 0; i < list.size; i++) if (list.ords[i] === ord) return true;
|
|
14832
|
+
return false;
|
|
14833
|
+
}
|
|
14834
|
+
function collectDropped(sorted, kept, dropped) {
|
|
14835
|
+
dropped.size = 0;
|
|
14836
|
+
let next = 0;
|
|
14837
|
+
for (let i = 0; i < sorted.size; i++) {
|
|
14838
|
+
const ord = sorted.ords[i];
|
|
14839
|
+
if (next < kept.size && kept.ords[next] === ord) {
|
|
14840
|
+
next += 1;
|
|
14841
|
+
continue;
|
|
14842
|
+
}
|
|
14843
|
+
if (listHolds(kept, ord)) continue;
|
|
14844
|
+
appendToList(dropped, ord, sorted.distances[i]);
|
|
14845
|
+
}
|
|
14846
|
+
}
|
|
14847
|
+
function replaceFarthestShared(state, kept, held, ord, distance) {
|
|
14848
|
+
const tried = /* @__PURE__ */ new Set();
|
|
14849
|
+
for (; ; ) {
|
|
14850
|
+
let farthest = -1;
|
|
14851
|
+
for (let k = 0; k < kept.size; k++) {
|
|
14852
|
+
if (tried.has(k) || incomingCount(state.adjacency, kept.ords[k]) < 2) continue;
|
|
14853
|
+
if (farthest === -1 || kept.distances[k] > kept.distances[farthest]) farthest = k;
|
|
14854
|
+
}
|
|
14855
|
+
if (farthest === -1) return false;
|
|
14856
|
+
tried.add(farthest);
|
|
14857
|
+
const replaced = kept.ords[farthest];
|
|
14858
|
+
if (held(replaced) && !releaseUnlessLast(state.adjacency, replaced)) continue;
|
|
14859
|
+
kept.ords[farthest] = ord;
|
|
14860
|
+
kept.distances[farthest] = distance;
|
|
14861
|
+
return true;
|
|
14862
|
+
}
|
|
14863
|
+
}
|
|
14864
|
+
function writeKeepingLastLinks(state, ord, sorted, kept, held) {
|
|
14865
|
+
const adjacency = state.adjacency;
|
|
14866
|
+
const limit = state.Mmax0;
|
|
14867
|
+
const dropped = state.workspace.droppedLinks;
|
|
14868
|
+
collectDropped(sorted, kept, dropped);
|
|
14869
|
+
ensureListCapacity(kept, limit);
|
|
14870
|
+
for (let i = 0; i < dropped.size; i++) {
|
|
14871
|
+
const candidate = dropped.ords[i];
|
|
14872
|
+
if (!held(candidate)) continue;
|
|
14873
|
+
if (!isLive(state, candidate)) {
|
|
14874
|
+
noteUnlinked(adjacency, candidate);
|
|
14875
|
+
continue;
|
|
14876
|
+
}
|
|
14877
|
+
if (releaseUnlessLast(adjacency, candidate)) continue;
|
|
14878
|
+
if (kept.size < limit) {
|
|
14879
|
+
appendToList(kept, candidate, dropped.distances[i]);
|
|
14880
|
+
continue;
|
|
14881
|
+
}
|
|
14882
|
+
if (replaceFarthestShared(state, kept, held, candidate, dropped.distances[i])) continue;
|
|
14883
|
+
noteUnlinked(adjacency, candidate);
|
|
14884
|
+
noteNodeLeftUnlinked(state);
|
|
14885
|
+
}
|
|
14886
|
+
for (let k = 0; k < kept.size; k++) if (!held(kept.ords[k])) noteLinked(adjacency, kept.ords[k]);
|
|
14887
|
+
writeNeighbors(adjacency, ord, 0, kept.ords, kept.size);
|
|
14888
|
+
}
|
|
14889
|
+
|
|
14890
|
+
// src/vector/hnsw/connect-unreached.ts
|
|
14891
|
+
var NOT_REACHED_THROUGH_A_NODE = -1;
|
|
14892
|
+
function reach(walk, node, through) {
|
|
14893
|
+
walk.reached[node] = 1;
|
|
14894
|
+
walk.firstReachedThrough[node] = through;
|
|
14895
|
+
walk.queue[walk.tail++] = node;
|
|
14896
|
+
}
|
|
14897
|
+
function walkFrom(state, walk, start, through) {
|
|
14898
|
+
const adjacency = state.adjacency;
|
|
14899
|
+
if (walk.reached[start] === 1) return;
|
|
14900
|
+
reach(walk, start, through);
|
|
14901
|
+
while (walk.head < walk.tail) {
|
|
14902
|
+
const ord = walk.queue[walk.head++];
|
|
14903
|
+
const base = layerBase(adjacency, ord, 0);
|
|
14904
|
+
if (base === ABSENT) continue;
|
|
14905
|
+
const count = adjacency.level0[base];
|
|
14906
|
+
for (let i = 1; i <= count; i++) {
|
|
14907
|
+
const next = adjacency.level0[base + i];
|
|
14908
|
+
if (next < 0 || next >= walk.reached.length || walk.reached[next] === 1 || !nodeExists(state, next)) continue;
|
|
14909
|
+
reach(walk, next, ord);
|
|
14910
|
+
}
|
|
14911
|
+
}
|
|
14912
|
+
}
|
|
14913
|
+
function hasRoom(state, _walk, host) {
|
|
14914
|
+
const base = layerBase(state.adjacency, host, 0);
|
|
14915
|
+
return base !== ABSENT && state.adjacency.level0[base] < state.Mmax0;
|
|
14916
|
+
}
|
|
14917
|
+
function reachedThroughAnotherNode(walk, host, neighbour) {
|
|
14918
|
+
if (neighbour < 0 || neighbour >= walk.reached.length || walk.reached[neighbour] !== 1) return false;
|
|
14919
|
+
const through = walk.firstReachedThrough[neighbour];
|
|
14920
|
+
return through !== NOT_REACHED_THROUGH_A_NODE && through !== host;
|
|
14921
|
+
}
|
|
14922
|
+
function holdsLinkOutsideTheWalk(state, walk, host) {
|
|
14923
|
+
const adjacency = state.adjacency;
|
|
14924
|
+
const base = layerBase(adjacency, host, 0);
|
|
14925
|
+
if (base === ABSENT) return false;
|
|
14926
|
+
const count = adjacency.level0[base];
|
|
14927
|
+
for (let i = 1; i <= count; i++) if (reachedThroughAnotherNode(walk, host, adjacency.level0[base + i])) return true;
|
|
14928
|
+
return false;
|
|
14929
|
+
}
|
|
14930
|
+
function listOf(state, ord) {
|
|
14931
|
+
const adjacency = state.adjacency;
|
|
14932
|
+
const base = layerBase(adjacency, ord, 0);
|
|
14933
|
+
if (base === ABSENT) return new Int32Array(0);
|
|
14934
|
+
return adjacency.level0.slice(base + 1, base + 1 + adjacency.level0[base]);
|
|
14935
|
+
}
|
|
14936
|
+
function nearestHost(state, walk, orphan, nearby, test) {
|
|
14937
|
+
const distance = nodeDistanceFunction(state, state.buildMetric);
|
|
14938
|
+
let best = -1;
|
|
14939
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
14940
|
+
const consider = (host) => {
|
|
14941
|
+
if (host === orphan || walk.reached[host] !== 1 || !isLive(state, host) || !test(state, walk, host)) return;
|
|
14942
|
+
const away = distance(orphan, host);
|
|
14943
|
+
if (best === -1 || away < bestDistance) {
|
|
14944
|
+
best = host;
|
|
14945
|
+
bestDistance = away;
|
|
14946
|
+
}
|
|
14947
|
+
};
|
|
14948
|
+
for (const host of nearby.list) consider(host);
|
|
14949
|
+
for (const host of listOf(state, orphan))
|
|
14950
|
+
if (host >= 0 && host < walk.reached.length && nearby.holds[host] !== 1) consider(host);
|
|
14951
|
+
if (best !== -1) return best;
|
|
14952
|
+
for (let host = 0; host < walk.reached.length; host++) consider(host);
|
|
14953
|
+
return best;
|
|
14954
|
+
}
|
|
14955
|
+
function takeOutFarthestLinkOutsideTheWalk(state, walk, host) {
|
|
14956
|
+
const adjacency = state.adjacency;
|
|
14957
|
+
const base = layerBase(adjacency, host, 0);
|
|
14958
|
+
if (base === ABSENT) return;
|
|
14959
|
+
const distance = nodeDistanceFunction(state, state.buildMetric);
|
|
14960
|
+
const count = adjacency.level0[base];
|
|
14961
|
+
let farthest = -1;
|
|
14962
|
+
let farthestDistance = Number.NEGATIVE_INFINITY;
|
|
14963
|
+
for (let i = 1; i <= count; i++) {
|
|
14964
|
+
const neighbour = adjacency.level0[base + i];
|
|
14965
|
+
if (!reachedThroughAnotherNode(walk, host, neighbour)) continue;
|
|
14966
|
+
const away = distance(host, neighbour);
|
|
14967
|
+
if (farthest === -1 || away > farthestDistance) {
|
|
14968
|
+
farthest = i;
|
|
14969
|
+
farthestDistance = away;
|
|
14970
|
+
}
|
|
14971
|
+
}
|
|
14972
|
+
if (farthest === -1) return;
|
|
14973
|
+
const taken = adjacency.level0[base + farthest];
|
|
14974
|
+
for (let i = farthest; i < count; i++) adjacency.level0[base + i] = adjacency.level0[base + i + 1];
|
|
14975
|
+
adjacency.level0[base] = count - 1;
|
|
14976
|
+
noteUnlinked(adjacency, taken);
|
|
14977
|
+
}
|
|
14978
|
+
function appendTo(state, host, orphan) {
|
|
14979
|
+
const adjacency = state.adjacency;
|
|
14980
|
+
const base = layerBase(adjacency, host, 0);
|
|
14981
|
+
if (base === ABSENT) return;
|
|
14982
|
+
const count = adjacency.level0[base];
|
|
14983
|
+
adjacency.level0[base + count + 1] = orphan;
|
|
14984
|
+
adjacency.level0[base] = count + 1;
|
|
14985
|
+
noteLinked(adjacency, orphan);
|
|
14986
|
+
}
|
|
14987
|
+
function connectUnreached(state, nearby) {
|
|
14988
|
+
const slots = adjacencySlots(state.adjacency);
|
|
14989
|
+
const entry = entryPointOf(state);
|
|
14990
|
+
if (entry < 0 || entry >= slots || !nodeExists(state, entry)) return;
|
|
14991
|
+
const walk = {
|
|
14992
|
+
reached: new Uint8Array(slots),
|
|
14993
|
+
firstReachedThrough: new Int32Array(slots),
|
|
14994
|
+
queue: new Int32Array(slots),
|
|
14995
|
+
head: 0,
|
|
14996
|
+
tail: 0
|
|
14997
|
+
};
|
|
14998
|
+
walkFrom(state, walk, entry, NOT_REACHED_THROUGH_A_NODE);
|
|
14999
|
+
const held = { list: [], holds: new Uint8Array(slots) };
|
|
15000
|
+
for (let i = 0; i < nearby.length; i++) {
|
|
15001
|
+
const ord = nearby[i];
|
|
15002
|
+
if (ord < 0 || ord >= slots || held.holds[ord] === 1) continue;
|
|
15003
|
+
held.holds[ord] = 1;
|
|
15004
|
+
held.list.push(ord);
|
|
15005
|
+
}
|
|
15006
|
+
for (let orphan = 0; orphan < slots; orphan++) {
|
|
15007
|
+
if (walk.reached[orphan] === 1 || !isLive(state, orphan)) continue;
|
|
15008
|
+
let host = nearestHost(state, walk, orphan, held, hasRoom);
|
|
15009
|
+
if (host === -1) {
|
|
15010
|
+
host = nearestHost(state, walk, orphan, held, holdsLinkOutsideTheWalk);
|
|
15011
|
+
if (host === -1) continue;
|
|
15012
|
+
takeOutFarthestLinkOutsideTheWalk(state, walk, host);
|
|
15013
|
+
}
|
|
15014
|
+
appendTo(state, host, orphan);
|
|
15015
|
+
walkFrom(state, walk, orphan, host);
|
|
15016
|
+
}
|
|
15017
|
+
}
|
|
15018
|
+
function holdsUnlinkedNode(state) {
|
|
15019
|
+
const slots = adjacencySlots(state.adjacency);
|
|
15020
|
+
const entry = entryPointOf(state);
|
|
15021
|
+
const incoming = state.adjacency.incoming;
|
|
15022
|
+
for (let ord = 0; ord < slots; ord++) {
|
|
15023
|
+
if (incoming[ord] === 0 && ord !== entry && isLive(state, ord)) return true;
|
|
15024
|
+
}
|
|
15025
|
+
return false;
|
|
15026
|
+
}
|
|
15027
|
+
function connectThroughEitherCore(state, nearby) {
|
|
15028
|
+
const field = nativeFieldFor();
|
|
15029
|
+
if (field !== null && nativeConnect(state, field, state.buildMetric, nearby)) return;
|
|
15030
|
+
connectUnreached(state, nearby);
|
|
15031
|
+
}
|
|
15032
|
+
function connectAfterRemoval(state, nearby) {
|
|
15033
|
+
connectThroughEitherCore(state, nearby);
|
|
15034
|
+
Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
|
|
15035
|
+
}
|
|
15036
|
+
function connectAfterPlacement(state, holdsGraphLock) {
|
|
15037
|
+
if (Atomics.load(state.header, GRAPH_UNLINKED_NODES) === 0) return;
|
|
15038
|
+
if (!holdsGraphLock) lockGraphExclusive(state.locks);
|
|
15039
|
+
try {
|
|
15040
|
+
if (Atomics.load(state.header, GRAPH_UNLINKED_NODES) === 0) return;
|
|
15041
|
+
connectThroughEitherCore(state, new Int32Array(0));
|
|
15042
|
+
Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
|
|
15043
|
+
} finally {
|
|
15044
|
+
if (!holdsGraphLock) unlockGraphExclusive(state.locks);
|
|
15045
|
+
}
|
|
15046
|
+
}
|
|
15047
|
+
function baseNeighboursOfRemoved(state, removed) {
|
|
15048
|
+
const leaving = new Set(removed);
|
|
15049
|
+
const nearby = [];
|
|
15050
|
+
for (const ord of removed) {
|
|
15051
|
+
for (const neighbour of listOf(state, ord)) if (!leaving.has(neighbour)) nearby.push(neighbour);
|
|
15052
|
+
}
|
|
15053
|
+
return Int32Array.from(nearby);
|
|
15054
|
+
}
|
|
15055
|
+
function leaveIncomingUncounted(state) {
|
|
15056
|
+
Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
|
|
15057
|
+
Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 1);
|
|
15058
|
+
}
|
|
15059
|
+
function countIncomingBeforeWriting(state, holdsGraphLock) {
|
|
15060
|
+
if (Atomics.load(state.header, GRAPH_INCOMING_UNCOUNTED) === 0) return;
|
|
15061
|
+
if (!holdsGraphLock) lockGraphExclusive(state.locks);
|
|
15062
|
+
try {
|
|
15063
|
+
if (Atomics.load(state.header, GRAPH_INCOMING_UNCOUNTED) === 0) return;
|
|
15064
|
+
const adjacency = state.adjacency;
|
|
15065
|
+
const slots = adjacencySlots(adjacency);
|
|
15066
|
+
if (slots > 0) reachNode(adjacency, slots - 1);
|
|
15067
|
+
adjacency.incoming.fill(0);
|
|
15068
|
+
countEveryLink(adjacency, slots);
|
|
15069
|
+
if (holdsUnlinkedNode(state)) connectThroughEitherCore(state, new Int32Array(0));
|
|
15070
|
+
Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
|
|
15071
|
+
Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 0);
|
|
15072
|
+
} finally {
|
|
15073
|
+
if (!holdsGraphLock) unlockGraphExclusive(state.locks);
|
|
15074
|
+
}
|
|
15075
|
+
}
|
|
15076
|
+
|
|
14715
15077
|
// src/vector/hnsw/graph-ops.ts
|
|
14716
15078
|
function readNeighborsLocked(state, ord, layer) {
|
|
14717
15079
|
for (; ; ) {
|
|
@@ -14808,14 +15170,24 @@ function pruneConnections(state, ord, layer, distance) {
|
|
|
14808
15170
|
for (let i = 1; i <= count; i++) {
|
|
14809
15171
|
const connOrd = neighbors[base + i];
|
|
14810
15172
|
const dist = distance(ord, connOrd);
|
|
14811
|
-
if (dist === Number.POSITIVE_INFINITY)
|
|
15173
|
+
if (dist === Number.POSITIVE_INFINITY) {
|
|
15174
|
+
if (layer === 0) noteUnlinked(adjacency, connOrd);
|
|
15175
|
+
continue;
|
|
15176
|
+
}
|
|
14812
15177
|
candidates.ords[candidates.size] = connOrd;
|
|
14813
15178
|
candidates.distances[candidates.size] = dist;
|
|
14814
15179
|
candidates.size += 1;
|
|
14815
15180
|
}
|
|
14816
15181
|
const kept = state.workspace.pruneSelection;
|
|
14817
15182
|
selectNeighborsHeuristic(state, candidates, mc, distance, kept);
|
|
14818
|
-
|
|
15183
|
+
if (layer === 0) {
|
|
15184
|
+
writeKeepingLastLinks(state, ord, state.workspace.working, kept, everyCandidateWasHeld);
|
|
15185
|
+
return;
|
|
15186
|
+
}
|
|
15187
|
+
writeNeighbors(adjacency, ord, layer, kept.ords, kept.size);
|
|
15188
|
+
}
|
|
15189
|
+
function everyCandidateWasHeld() {
|
|
15190
|
+
return true;
|
|
14819
15191
|
}
|
|
14820
15192
|
|
|
14821
15193
|
// src/vector/hnsw/mutation.ts
|
|
@@ -14929,11 +15301,15 @@ function linkNeighborsBack(state, ord, linkTop) {
|
|
|
14929
15301
|
}
|
|
14930
15302
|
function linkNode(state, ord, level2) {
|
|
14931
15303
|
const topLayer = topLayerOf(state);
|
|
15304
|
+
const formerEntry = entryPointOf(state);
|
|
14932
15305
|
const linkTop = selectNeighborsPerLayer(state, ord, level2);
|
|
14933
15306
|
if (linkTop < 0) return;
|
|
14934
15307
|
writeOwnLists(state, ord, linkTop);
|
|
14935
15308
|
linkNeighborsBack(state, ord, linkTop);
|
|
14936
|
-
if (level2
|
|
15309
|
+
if (level2 <= topLayer) return;
|
|
15310
|
+
raiseEntry(state, ord, level2);
|
|
15311
|
+
const demoted = entryPointOf(state) !== formerEntry && isLive(state, formerEntry);
|
|
15312
|
+
if (demoted && incomingCount(state.adjacency, formerEntry) === 0) noteNodeLeftUnlinked(state);
|
|
14937
15313
|
}
|
|
14938
15314
|
function writeRecordBeforePlacement(state, ord) {
|
|
14939
15315
|
const quantizer = state.quantizer;
|
|
@@ -14959,8 +15335,12 @@ function insertNode(state, ord, holdsGraphLock = false) {
|
|
|
14959
15335
|
if (!state.store.holdsOrdinal(ord)) return false;
|
|
14960
15336
|
ensureCapacity(state, ord + 1);
|
|
14961
15337
|
if (nodeExists(state, ord)) return false;
|
|
15338
|
+
countIncomingBeforeWriting(state, holdsGraphLock);
|
|
14962
15339
|
writeRecordBeforePlacement(state, ord);
|
|
14963
|
-
if (placeThroughTheCore(state, ord, holdsGraphLock))
|
|
15340
|
+
if (placeThroughTheCore(state, ord, holdsGraphLock)) {
|
|
15341
|
+
connectAfterPlacement(state, holdsGraphLock);
|
|
15342
|
+
return true;
|
|
15343
|
+
}
|
|
14964
15344
|
if (nodeExists(state, ord)) return false;
|
|
14965
15345
|
if (!holdsGraphLock) lockGraphShared(state.locks);
|
|
14966
15346
|
try {
|
|
@@ -14968,11 +15348,11 @@ function insertNode(state, ord, holdsGraphLock = false) {
|
|
|
14968
15348
|
const level2 = randomLevel(state.mL);
|
|
14969
15349
|
createNode(state.adjacency, ord, level2);
|
|
14970
15350
|
Atomics.add(state.header, GRAPH_NODE_COUNT, 1);
|
|
14971
|
-
if (claimEntryIfEmpty(state, ord, level2))
|
|
14972
|
-
linkNode(state, ord, level2);
|
|
15351
|
+
if (!claimEntryIfEmpty(state, ord, level2)) linkNode(state, ord, level2);
|
|
14973
15352
|
} finally {
|
|
14974
15353
|
if (!holdsGraphLock) unlockGraphShared(state.locks);
|
|
14975
15354
|
}
|
|
15355
|
+
connectAfterPlacement(state, holdsGraphLock);
|
|
14976
15356
|
return true;
|
|
14977
15357
|
}
|
|
14978
15358
|
function removeNodeEager(state, ord, excludeOrds) {
|
|
@@ -14990,7 +15370,8 @@ function removeNodeEager(state, ord, excludeOrds) {
|
|
|
14990
15370
|
if (layer > nodeMaxLayer(state, neighborOrd)) continue;
|
|
14991
15371
|
const mc = maxConns(state, layer);
|
|
14992
15372
|
if (neighborCount(state.adjacency, neighborOrd, layer) >= mc) continue;
|
|
14993
|
-
const
|
|
15373
|
+
const heldNow = new Set(collectNeighbors(state.adjacency, neighborOrd, layer));
|
|
15374
|
+
const candidateOrds = new Set(heldNow);
|
|
14994
15375
|
for (const otherOrd of formerNeighbors) {
|
|
14995
15376
|
if (otherOrd !== neighborOrd && otherOrd !== ord) {
|
|
14996
15377
|
if (excludeOrds?.has(otherOrd)) continue;
|
|
@@ -15001,12 +15382,19 @@ function removeNodeEager(state, ord, excludeOrds) {
|
|
|
15001
15382
|
candidates.size = 0;
|
|
15002
15383
|
for (const candOrd of candidateOrds) {
|
|
15003
15384
|
const dist = distance(neighborOrd, candOrd);
|
|
15004
|
-
if (dist === Number.POSITIVE_INFINITY)
|
|
15385
|
+
if (dist === Number.POSITIVE_INFINITY) {
|
|
15386
|
+
if (layer === 0 && heldNow.has(candOrd)) noteUnlinked(state.adjacency, candOrd);
|
|
15387
|
+
continue;
|
|
15388
|
+
}
|
|
15005
15389
|
appendToList(candidates, candOrd, dist);
|
|
15006
15390
|
}
|
|
15007
15391
|
const selected = state.workspace.repairSelection;
|
|
15008
15392
|
selectNeighborsHeuristic(state, candidates, mc, distance, selected);
|
|
15009
|
-
|
|
15393
|
+
if (layer === 0) {
|
|
15394
|
+
writeKeepingLastLinks(state, neighborOrd, state.workspace.working, selected, (held) => heldNow.has(held));
|
|
15395
|
+
} else {
|
|
15396
|
+
replaceNeighbors(state.adjacency, neighborOrd, layer, selected.ords, selected.size);
|
|
15397
|
+
}
|
|
15010
15398
|
for (let i = 0; i < selected.size; i++) {
|
|
15011
15399
|
const newConnOrd = selected.ords[i];
|
|
15012
15400
|
if (layer <= nodeMaxLayer(state, newConnOrd)) {
|
|
@@ -15016,6 +15404,8 @@ function removeNodeEager(state, ord, excludeOrds) {
|
|
|
15016
15404
|
}
|
|
15017
15405
|
}
|
|
15018
15406
|
}
|
|
15407
|
+
const formerBaseNeighbors = collectNeighbors(state.adjacency, ord, 0);
|
|
15408
|
+
for (const neighborOrd of formerBaseNeighbors) noteUnlinked(state.adjacency, neighborOrd);
|
|
15019
15409
|
deleteNode(state.adjacency, ord);
|
|
15020
15410
|
Atomics.sub(state.header, GRAPH_NODE_COUNT, 1);
|
|
15021
15411
|
clearTombstone(state, ord);
|
|
@@ -15038,8 +15428,7 @@ function markTombstone(state, ord) {
|
|
|
15038
15428
|
}
|
|
15039
15429
|
function compactTombstones(state) {
|
|
15040
15430
|
if (Atomics.load(state.header, GRAPH_TOMBSTONE_COUNT) === 0) return;
|
|
15041
|
-
|
|
15042
|
-
if (field !== null && nativeCompact(state, field, state.buildMetric)) return;
|
|
15431
|
+
countIncomingBeforeWriting(state, true);
|
|
15043
15432
|
const tombstonedOrds = [];
|
|
15044
15433
|
const slots = adjacencySlots(state.adjacency);
|
|
15045
15434
|
for (let ord = 0; ord < slots; ord++) {
|
|
@@ -15047,10 +15436,15 @@ function compactTombstones(state) {
|
|
|
15047
15436
|
tombstonedOrds.push(ord);
|
|
15048
15437
|
}
|
|
15049
15438
|
}
|
|
15050
|
-
const
|
|
15051
|
-
|
|
15052
|
-
|
|
15439
|
+
const nearby = baseNeighboursOfRemoved(state, tombstonedOrds);
|
|
15440
|
+
const field = nativeFieldFor();
|
|
15441
|
+
if (field === null || !nativeCompact(state, field, state.buildMetric)) {
|
|
15442
|
+
const excl = new Set(tombstonedOrds);
|
|
15443
|
+
for (const ord of tombstonedOrds) {
|
|
15444
|
+
removeNodeEager(state, ord, excl);
|
|
15445
|
+
}
|
|
15053
15446
|
}
|
|
15447
|
+
connectAfterRemoval(state, nearby);
|
|
15054
15448
|
}
|
|
15055
15449
|
function resetGraph(state) {
|
|
15056
15450
|
resetAdjacency(state.adjacency);
|
|
@@ -15060,6 +15454,7 @@ function resetGraph(state) {
|
|
|
15060
15454
|
Atomics.store(state.header, GRAPH_NODE_COUNT, 0);
|
|
15061
15455
|
Atomics.store(state.header, GRAPH_ENTRY_POINT, -1);
|
|
15062
15456
|
Atomics.store(state.header, GRAPH_TOP_LAYER, -1);
|
|
15457
|
+
Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 0);
|
|
15063
15458
|
}
|
|
15064
15459
|
function rebuild(state) {
|
|
15065
15460
|
const nodeCount = Atomics.load(state.header, GRAPH_NODE_COUNT);
|
|
@@ -15269,7 +15664,7 @@ function deserializeNumberedGraph(state, graph, ordinalOfNumber) {
|
|
|
15269
15664
|
}
|
|
15270
15665
|
cursor += count + 1;
|
|
15271
15666
|
if (ordinal !== NO_ORDINAL && layer <= keptTop) {
|
|
15272
|
-
|
|
15667
|
+
writeNeighbors(state.adjacency, ordinal, layer, kept, kept.length);
|
|
15273
15668
|
}
|
|
15274
15669
|
}
|
|
15275
15670
|
}
|
|
@@ -15281,6 +15676,7 @@ function deserializeNumberedGraph(state, graph, ordinalOfNumber) {
|
|
|
15281
15676
|
const entryIsUsable = entryOrdinal !== NO_ORDINAL && entryTop >= 0;
|
|
15282
15677
|
Atomics.store(state.header, GRAPH_ENTRY_POINT, entryIsUsable ? entryOrdinal : nodeOnHighestLayer);
|
|
15283
15678
|
Atomics.store(state.header, GRAPH_TOP_LAYER, entryIsUsable ? entryTop : highestLayer);
|
|
15679
|
+
leaveIncomingUncounted(state);
|
|
15284
15680
|
}
|
|
15285
15681
|
|
|
15286
15682
|
// src/vector/hnsw/persistence.ts
|
|
@@ -15319,7 +15715,7 @@ function deserializeGraph(state, store, data) {
|
|
|
15319
15715
|
for (const node of nodes) {
|
|
15320
15716
|
createNode(state.adjacency, node.ord, node.maxLayer);
|
|
15321
15717
|
for (const [layer, neighbors] of node.layers) {
|
|
15322
|
-
|
|
15718
|
+
writeNeighbors(state.adjacency, node.ord, layer, neighbors, neighbors.length);
|
|
15323
15719
|
}
|
|
15324
15720
|
Atomics.add(state.header, GRAPH_NODE_COUNT, 1);
|
|
15325
15721
|
}
|
|
@@ -15345,6 +15741,7 @@ function deserializeGraph(state, store, data) {
|
|
|
15345
15741
|
Atomics.store(state.header, GRAPH_ENTRY_POINT, bestOrd);
|
|
15346
15742
|
Atomics.store(state.header, GRAPH_TOP_LAYER, bestLayer);
|
|
15347
15743
|
}
|
|
15744
|
+
leaveIncomingUncounted(state);
|
|
15348
15745
|
}
|
|
15349
15746
|
|
|
15350
15747
|
// src/vector/hnsw/scratch-bytes.ts
|
|
@@ -15644,6 +16041,7 @@ function restoreSnapshot(state, snapshot) {
|
|
|
15644
16041
|
Atomics.store(state.header, GRAPH_NODE_COUNT, snapshot.nodeCount);
|
|
15645
16042
|
Atomics.store(state.header, GRAPH_ENTRY_POINT, snapshot.entryPointOrd);
|
|
15646
16043
|
Atomics.store(state.header, GRAPH_TOP_LAYER, snapshot.topLayer);
|
|
16044
|
+
leaveIncomingUncounted(state);
|
|
15647
16045
|
}
|
|
15648
16046
|
|
|
15649
16047
|
// src/vector/hnsw/state.ts
|
|
@@ -15779,6 +16177,7 @@ function createHNSWIndex(dimension, store, config, quantizer, handles) {
|
|
|
15779
16177
|
},
|
|
15780
16178
|
search: (query, k, searchMetric, minSimilarity, options) => search(state, docIdOf, query, k, searchMetric, minSimilarity, options),
|
|
15781
16179
|
clear: () => exclusively(() => resetGraph(state)),
|
|
16180
|
+
retire: () => Atomics.store(shared2.header, GRAPH_RETIRED, 1),
|
|
15782
16181
|
entries: entriesIterator,
|
|
15783
16182
|
compactionNeeded,
|
|
15784
16183
|
compactTombstones: () => exclusively(() => compactTombstones(state)),
|
|
@@ -15842,6 +16241,7 @@ function releaseReplacedVectors(state) {
|
|
|
15842
16241
|
for (const ordinal of state.store.releaseReplacedVectors()) state.osq?.removeOrdinal(ordinal);
|
|
15843
16242
|
}
|
|
15844
16243
|
function adoptGraph(state, graph) {
|
|
16244
|
+
if (state.hnsw !== null && state.hnsw !== graph) state.hnsw.retire();
|
|
15845
16245
|
if (graph === null && state.hnsw !== null) {
|
|
15846
16246
|
state.hnsw.clear();
|
|
15847
16247
|
}
|
|
@@ -16607,7 +17007,7 @@ async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fa
|
|
|
16607
17007
|
continue;
|
|
16608
17008
|
}
|
|
16609
17009
|
if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
|
|
16610
|
-
state.buffer.delete(docId);
|
|
17010
|
+
if (graph === state.hnsw) state.buffer.delete(docId);
|
|
16611
17011
|
}
|
|
16612
17012
|
if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
|
|
16613
17013
|
fallback.shortAnswerWarned = true;
|
|
@@ -16624,7 +17024,7 @@ async function insertLocally(state, graph, docIds, admit) {
|
|
|
16624
17024
|
const ordinal = state.store.getOrdinal(docId);
|
|
16625
17025
|
if (ordinal === void 0) continue;
|
|
16626
17026
|
graph.insertOrdinal(ordinal);
|
|
16627
|
-
state.buffer.delete(docId);
|
|
17027
|
+
if (graph === state.hnsw) state.buffer.delete(docId);
|
|
16628
17028
|
count += 1;
|
|
16629
17029
|
if (count % BUILD_CHUNK_SIZE === 0) {
|
|
16630
17030
|
noteWrite(state);
|
|
@@ -16741,6 +17141,10 @@ async function buildGraphFromStore(state) {
|
|
|
16741
17141
|
}
|
|
16742
17142
|
const previous = state.hnsw;
|
|
16743
17143
|
adoptGraph(state, graph);
|
|
17144
|
+
for (const docId of [...state.buffer]) {
|
|
17145
|
+
const ordinal = state.store.getOrdinal(docId);
|
|
17146
|
+
if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) state.buffer.delete(docId);
|
|
17147
|
+
}
|
|
16744
17148
|
if (previous !== null && previous !== graph) dropSharedGraph(state, previous);
|
|
16745
17149
|
await releasePendingLocations(state);
|
|
16746
17150
|
}
|
|
@@ -17249,7 +17653,7 @@ function registryMatches(state, listedKeys) {
|
|
|
17249
17653
|
if (listed.length !== state.savedFiles.length) return false;
|
|
17250
17654
|
return state.savedFiles.every((file, index) => file.key === listed[index]);
|
|
17251
17655
|
}
|
|
17252
|
-
function
|
|
17656
|
+
function isLive2(state, ordinal) {
|
|
17253
17657
|
if (ordinal === NO_LIVE_VECTOR_AT_POSITION) return false;
|
|
17254
17658
|
const docId = state.store.docIdForOrdinal(ordinal);
|
|
17255
17659
|
return docId !== void 0 && !state.tombstones.has(docId);
|
|
@@ -17258,7 +17662,7 @@ function isGraphTombstone(state, ordinal) {
|
|
|
17258
17662
|
return ordinal !== NO_LIVE_VECTOR_AT_POSITION && state.hnsw !== null && state.hnsw.holdsTombstone(ordinal);
|
|
17259
17663
|
}
|
|
17260
17664
|
function isCarried(state, ordinal) {
|
|
17261
|
-
return
|
|
17665
|
+
return isLive2(state, ordinal) || isGraphTombstone(state, ordinal);
|
|
17262
17666
|
}
|
|
17263
17667
|
function carriedCountOf(state, file) {
|
|
17264
17668
|
let carried = 0;
|
|
@@ -17266,7 +17670,7 @@ function carriedCountOf(state, file) {
|
|
|
17266
17670
|
return carried;
|
|
17267
17671
|
}
|
|
17268
17672
|
function deadBitsOf(state, file) {
|
|
17269
|
-
return deadBitsWhere(file.ordinals.length, (position) => !
|
|
17673
|
+
return deadBitsWhere(file.ordinals.length, (position) => !isLive2(state, file.ordinals[position]));
|
|
17270
17674
|
}
|
|
17271
17675
|
function filesWorthKeeping(state, listed, calibration, carriedCounts) {
|
|
17272
17676
|
const kept = [];
|
|
@@ -17348,7 +17752,7 @@ function graphPartsWithoutNodes(parts, numbers) {
|
|
|
17348
17752
|
}
|
|
17349
17753
|
function plannedVectorOf(state, ordinal) {
|
|
17350
17754
|
const docId = state.store.docIdForOrdinal(ordinal) ?? state.store.view.docIdAt(ordinal) ?? "";
|
|
17351
|
-
return { ordinal, docId, tombstoned: !
|
|
17755
|
+
return { ordinal, docId, tombstoned: !isLive2(state, ordinal) };
|
|
17352
17756
|
}
|
|
17353
17757
|
function readFile(state, planned, centroid) {
|
|
17354
17758
|
const dimension = state.dimension;
|
|
@@ -17590,6 +17994,87 @@ function beginCheckpointRestore(state, shape) {
|
|
|
17590
17994
|
return { addFile, finish };
|
|
17591
17995
|
}
|
|
17592
17996
|
|
|
17997
|
+
// src/vector/vector-index/flagged-documents.ts
|
|
17998
|
+
var FlaggedDocuments = class extends Set {
|
|
17999
|
+
constructor(store, layout) {
|
|
18000
|
+
super();
|
|
18001
|
+
this.store = store;
|
|
18002
|
+
this.layout = layout;
|
|
18003
|
+
}
|
|
18004
|
+
ordinals = /* @__PURE__ */ new Map();
|
|
18005
|
+
handles = null;
|
|
18006
|
+
flags = new Uint8Array(0);
|
|
18007
|
+
chunks = null;
|
|
18008
|
+
add(docId) {
|
|
18009
|
+
super.add(docId);
|
|
18010
|
+
const handles = this.openHandles();
|
|
18011
|
+
if (handles === null) return this;
|
|
18012
|
+
this.flagCurrentOrdinal(docId);
|
|
18013
|
+
Atomics.store(handles.header, this.layout.countWord, this.size);
|
|
18014
|
+
return this;
|
|
18015
|
+
}
|
|
18016
|
+
delete(docId) {
|
|
18017
|
+
const held = super.delete(docId);
|
|
18018
|
+
const handles = this.openHandles();
|
|
18019
|
+
if (handles === null) return held;
|
|
18020
|
+
const ordinal = this.ordinals.get(docId);
|
|
18021
|
+
if (ordinal !== void 0) {
|
|
18022
|
+
this.ordinals.delete(docId);
|
|
18023
|
+
this.unflag(ordinal);
|
|
18024
|
+
}
|
|
18025
|
+
Atomics.store(handles.header, this.layout.countWord, this.size);
|
|
18026
|
+
return held;
|
|
18027
|
+
}
|
|
18028
|
+
clear() {
|
|
18029
|
+
super.clear();
|
|
18030
|
+
const handles = this.openHandles();
|
|
18031
|
+
if (handles !== null) {
|
|
18032
|
+
for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
|
|
18033
|
+
Atomics.store(handles.header, this.layout.countWord, 0);
|
|
18034
|
+
}
|
|
18035
|
+
this.ordinals.clear();
|
|
18036
|
+
}
|
|
18037
|
+
flagCurrentOrdinal(docId) {
|
|
18038
|
+
const ordinal = this.store.getOrdinal(docId);
|
|
18039
|
+
const previous = this.ordinals.get(docId);
|
|
18040
|
+
if (ordinal === void 0 || ordinal === previous) return;
|
|
18041
|
+
if (this.chunks !== null) Atomics.add(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
|
|
18042
|
+
Atomics.store(this.flags, ordinal, 1);
|
|
18043
|
+
this.ordinals.set(docId, ordinal);
|
|
18044
|
+
if (previous !== void 0) this.unflag(previous);
|
|
18045
|
+
}
|
|
18046
|
+
unflag(ordinal) {
|
|
18047
|
+
Atomics.store(this.flags, ordinal, 0);
|
|
18048
|
+
if (this.chunks !== null) Atomics.sub(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
|
|
18049
|
+
}
|
|
18050
|
+
openHandles() {
|
|
18051
|
+
if (this.store.dimension === 0) return null;
|
|
18052
|
+
const handles = this.store.handles;
|
|
18053
|
+
if (handles === this.handles) return handles;
|
|
18054
|
+
this.handles = handles;
|
|
18055
|
+
this.flags = new Uint8Array(this.layout.flagsOf(handles));
|
|
18056
|
+
const chunksOf2 = this.layout.chunksOf;
|
|
18057
|
+
this.chunks = chunksOf2 === null ? null : new Int32Array(chunksOf2(handles));
|
|
18058
|
+
this.ordinals.clear();
|
|
18059
|
+
for (const docId of this) this.flagCurrentOrdinal(docId);
|
|
18060
|
+
return handles;
|
|
18061
|
+
}
|
|
18062
|
+
};
|
|
18063
|
+
function createUnplacedVectors(store) {
|
|
18064
|
+
return new FlaggedDocuments(store, {
|
|
18065
|
+
flagsOf: (handles) => handles.unplaced,
|
|
18066
|
+
chunksOf: (handles) => handles.unplacedChunks,
|
|
18067
|
+
countWord: STORE_UNPLACED_COUNT
|
|
18068
|
+
});
|
|
18069
|
+
}
|
|
18070
|
+
function createRemovedDocuments(store) {
|
|
18071
|
+
return new FlaggedDocuments(store, {
|
|
18072
|
+
flagsOf: (handles) => handles.removed,
|
|
18073
|
+
chunksOf: null,
|
|
18074
|
+
countWord: STORE_REMOVED_COUNT
|
|
18075
|
+
});
|
|
18076
|
+
}
|
|
18077
|
+
|
|
17593
18078
|
// src/vector/vector-index/maintenance.ts
|
|
17594
18079
|
function compact(state) {
|
|
17595
18080
|
if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
|
|
@@ -17787,6 +18272,43 @@ function createHeap2(compare) {
|
|
|
17787
18272
|
};
|
|
17788
18273
|
}
|
|
17789
18274
|
|
|
18275
|
+
// src/vector/vector-index/merge-results.ts
|
|
18276
|
+
function highScoreFirst(a, b) {
|
|
18277
|
+
return b.score - a.score || compareCodePoints(a.docId, b.docId);
|
|
18278
|
+
}
|
|
18279
|
+
function mergeResults(hnswResults, bufferResults, k) {
|
|
18280
|
+
const seen = /* @__PURE__ */ new Set();
|
|
18281
|
+
const merged = [];
|
|
18282
|
+
let hi = 0;
|
|
18283
|
+
let bi = 0;
|
|
18284
|
+
while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
|
|
18285
|
+
const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
|
|
18286
|
+
const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
|
|
18287
|
+
let pick;
|
|
18288
|
+
if (h && b) {
|
|
18289
|
+
if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
|
|
18290
|
+
pick = h;
|
|
18291
|
+
hi++;
|
|
18292
|
+
} else {
|
|
18293
|
+
pick = b;
|
|
18294
|
+
bi++;
|
|
18295
|
+
}
|
|
18296
|
+
} else if (h) {
|
|
18297
|
+
pick = h;
|
|
18298
|
+
hi++;
|
|
18299
|
+
} else if (b) {
|
|
18300
|
+
pick = b;
|
|
18301
|
+
bi++;
|
|
18302
|
+
} else {
|
|
18303
|
+
break;
|
|
18304
|
+
}
|
|
18305
|
+
if (seen.has(pick.docId)) continue;
|
|
18306
|
+
seen.add(pick.docId);
|
|
18307
|
+
merged.push(pick);
|
|
18308
|
+
}
|
|
18309
|
+
return merged;
|
|
18310
|
+
}
|
|
18311
|
+
|
|
17790
18312
|
// src/vector/vector-index/search.ts
|
|
17791
18313
|
function* bufferCandidates(state, filter) {
|
|
17792
18314
|
for (const docId of state.buffer) {
|
|
@@ -17816,9 +18338,6 @@ function fetchedOutcome(state, results, minSimilarity, filter) {
|
|
|
17816
18338
|
function scannedOutcome(scan2) {
|
|
17817
18339
|
return { results: scan2.results, matched: scan2.matched, matchedExact: true };
|
|
17818
18340
|
}
|
|
17819
|
-
function highScoreFirst(a, b) {
|
|
17820
|
-
return b.score - a.score || compareCodePoints(a.docId, b.docId);
|
|
17821
|
-
}
|
|
17822
18341
|
function liveCandidates(state, candidates) {
|
|
17823
18342
|
const docIds = [];
|
|
17824
18343
|
const ordinals = [];
|
|
@@ -17883,38 +18402,6 @@ function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
|
|
|
17883
18402
|
}
|
|
17884
18403
|
return { results: heap.toSortedArray().reverse(), matched };
|
|
17885
18404
|
}
|
|
17886
|
-
function mergeResults(hnswResults, bufferResults, k) {
|
|
17887
|
-
const seen = /* @__PURE__ */ new Set();
|
|
17888
|
-
const merged = [];
|
|
17889
|
-
let hi = 0;
|
|
17890
|
-
let bi = 0;
|
|
17891
|
-
while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
|
|
17892
|
-
const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
|
|
17893
|
-
const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
|
|
17894
|
-
let pick;
|
|
17895
|
-
if (h && b) {
|
|
17896
|
-
if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
|
|
17897
|
-
pick = h;
|
|
17898
|
-
hi++;
|
|
17899
|
-
} else {
|
|
17900
|
-
pick = b;
|
|
17901
|
-
bi++;
|
|
17902
|
-
}
|
|
17903
|
-
} else if (h) {
|
|
17904
|
-
pick = h;
|
|
17905
|
-
hi++;
|
|
17906
|
-
} else if (b) {
|
|
17907
|
-
pick = b;
|
|
17908
|
-
bi++;
|
|
17909
|
-
} else {
|
|
17910
|
-
break;
|
|
17911
|
-
}
|
|
17912
|
-
if (seen.has(pick.docId)) continue;
|
|
17913
|
-
seen.add(pick.docId);
|
|
17914
|
-
merged.push(pick);
|
|
17915
|
-
}
|
|
17916
|
-
return merged;
|
|
17917
|
-
}
|
|
17918
18405
|
function search2(state, query, k, options) {
|
|
17919
18406
|
return searchWithFilter(state, query, k, options, filterForOptions(state, options));
|
|
17920
18407
|
}
|
|
@@ -18000,8 +18487,8 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
|
|
|
18000
18487
|
hnswConfig,
|
|
18001
18488
|
workerCopies,
|
|
18002
18489
|
store,
|
|
18003
|
-
tombstones:
|
|
18004
|
-
buffer:
|
|
18490
|
+
tombstones: createRemovedDocuments(store),
|
|
18491
|
+
buffer: createUnplacedVectors(store),
|
|
18005
18492
|
pendingLocations: /* @__PURE__ */ new Map(),
|
|
18006
18493
|
savedFiles: [],
|
|
18007
18494
|
savedSignature: null,
|
|
@@ -20477,55 +20964,93 @@ function runSegmentAction(entry, action) {
|
|
|
20477
20964
|
}
|
|
20478
20965
|
}
|
|
20479
20966
|
|
|
20480
|
-
// src/vector/shared-field/
|
|
20481
|
-
function
|
|
20482
|
-
|
|
20967
|
+
// src/vector/shared-field/store-flags.ts
|
|
20968
|
+
function bytesOf(flags, source) {
|
|
20969
|
+
if (flags.source !== source) {
|
|
20970
|
+
flags.source = source;
|
|
20971
|
+
flags.view = new Uint8Array(source);
|
|
20972
|
+
}
|
|
20973
|
+
return flags.view;
|
|
20483
20974
|
}
|
|
20484
|
-
function
|
|
20485
|
-
|
|
20975
|
+
function countsOf(chunks, source) {
|
|
20976
|
+
if (chunks.source !== source) {
|
|
20977
|
+
chunks.source = source;
|
|
20978
|
+
chunks.view = new Int32Array(source);
|
|
20979
|
+
}
|
|
20980
|
+
return chunks.view;
|
|
20981
|
+
}
|
|
20982
|
+
function readStoreFlags(store) {
|
|
20983
|
+
const unplaced = { source: null, view: new Uint8Array(0) };
|
|
20984
|
+
const unplacedChunks = { source: null, view: new Int32Array(0) };
|
|
20985
|
+
const removed = { source: null, view: new Uint8Array(0) };
|
|
20986
|
+
return {
|
|
20987
|
+
liveDocuments() {
|
|
20988
|
+
const header2 = store.handles.header;
|
|
20989
|
+
return Atomics.load(header2, STORE_LIVE_COUNT) - Atomics.load(header2, STORE_REMOVED_COUNT);
|
|
20990
|
+
},
|
|
20991
|
+
isRemoved(ordinal) {
|
|
20992
|
+
const bytes = bytesOf(removed, store.handles.removed);
|
|
20993
|
+
return ordinal < bytes.length && Atomics.load(bytes, ordinal) === 1;
|
|
20994
|
+
},
|
|
20995
|
+
unplacedOrdinals() {
|
|
20996
|
+
const handles = store.handles;
|
|
20997
|
+
if (Atomics.load(handles.header, STORE_UNPLACED_COUNT) === 0) return [];
|
|
20998
|
+
const bytes = bytesOf(unplaced, handles.unplaced);
|
|
20999
|
+
const counts = countsOf(unplacedChunks, handles.unplacedChunks);
|
|
21000
|
+
const end = Math.min(store.slots, bytes.length);
|
|
21001
|
+
const ordinals = [];
|
|
21002
|
+
for (let chunk = 0; chunk < counts.length; chunk++) {
|
|
21003
|
+
const from = chunk * UNPLACED_FLAG_CHUNK_ORDINALS;
|
|
21004
|
+
if (from >= end) break;
|
|
21005
|
+
if (Atomics.load(counts, chunk) === 0) continue;
|
|
21006
|
+
const to = Math.min(end, from + UNPLACED_FLAG_CHUNK_ORDINALS);
|
|
21007
|
+
for (let ordinal = from; ordinal < to; ordinal++) {
|
|
21008
|
+
if (Atomics.load(bytes, ordinal) === 1) ordinals.push(ordinal);
|
|
21009
|
+
}
|
|
21010
|
+
}
|
|
21011
|
+
return ordinals;
|
|
21012
|
+
}
|
|
21013
|
+
};
|
|
21014
|
+
}
|
|
21015
|
+
|
|
21016
|
+
// src/vector/shared-field/searcher.ts
|
|
21017
|
+
function distancesInTypeScript(store, query, metric, ordinals) {
|
|
21018
|
+
const arenaQuery = store.prepareQueryArena(query);
|
|
20486
21019
|
const queryMagnitude = arenaQuery ? arenaQuery.magnitude : magnitude(query);
|
|
20487
21020
|
const distances = new Float64Array(ordinals.length).fill(Number.POSITIVE_INFINITY);
|
|
20488
21021
|
for (let i = 0; i < ordinals.length; i++) {
|
|
20489
21022
|
if (arenaQuery) {
|
|
20490
|
-
distances[i] =
|
|
21023
|
+
distances[i] = store.distanceFromArena(arenaQuery, ordinals[i], metric);
|
|
20491
21024
|
continue;
|
|
20492
21025
|
}
|
|
20493
|
-
const entry =
|
|
21026
|
+
const entry = store.entryForOrdinal(ordinals[i]);
|
|
20494
21027
|
if (entry) distances[i] = toDistance(query, entry.vector, queryMagnitude, entry.magnitude, metric);
|
|
20495
21028
|
}
|
|
20496
21029
|
return distances;
|
|
20497
21030
|
}
|
|
20498
|
-
function
|
|
20499
|
-
const ordinals =
|
|
20500
|
-
|
|
20501
|
-
|
|
20502
|
-
|
|
20503
|
-
const docId = docIdOf(ord);
|
|
20504
|
-
if (docId === void 0) continue;
|
|
20505
|
-
ordinals.push(ord);
|
|
20506
|
-
docIds.push(docId);
|
|
20507
|
-
}
|
|
20508
|
-
const distances = nativeScoresOf(state.store.handles, query, metric, ordinals) ?? distancesInTypeScript(state, query, metric, ordinals);
|
|
20509
|
-
const heap = createBoundedMaxHeap(bestFirst2, k);
|
|
21031
|
+
function scoreCandidates(store, candidates, query, k, metric, minSimilarity) {
|
|
21032
|
+
const { ordinals, docIds } = candidates;
|
|
21033
|
+
if (ordinals.length === 0) return { results: [], matched: 0 };
|
|
21034
|
+
const distances = nativeScoresOf(store.handles, query, metric, ordinals) ?? distancesInTypeScript(store, query, metric, ordinals);
|
|
21035
|
+
const heap = createBoundedMaxHeap(highScoreFirst, k);
|
|
20510
21036
|
let matched = 0;
|
|
20511
21037
|
for (let i = 0; i < ordinals.length; i++) {
|
|
20512
21038
|
if (distances[i] === Number.POSITIVE_INFINITY) continue;
|
|
20513
21039
|
const score = toScore(distances[i], metric);
|
|
20514
21040
|
if (score < minSimilarity) continue;
|
|
20515
21041
|
matched++;
|
|
20516
|
-
heap.push({
|
|
21042
|
+
heap.push({ docId: docIds[i], score });
|
|
20517
21043
|
}
|
|
20518
|
-
return {
|
|
21044
|
+
return { results: heap.toSortedArray().reverse(), matched };
|
|
20519
21045
|
}
|
|
20520
21046
|
function createSharedVectorSearcher(options) {
|
|
20521
21047
|
const { fieldName, view, holdsDocument } = options;
|
|
21048
|
+
const flags = readStoreFlags(view.store);
|
|
20522
21049
|
function filterForDocIds(allowed) {
|
|
20523
21050
|
const filter = createOrdinalFilter(view.store.slots);
|
|
20524
|
-
const graph = view.graph;
|
|
20525
21051
|
for (const docId of allowed) {
|
|
20526
21052
|
const ordinal = view.ordinalOf(docId);
|
|
20527
|
-
if (ordinal === void 0) continue;
|
|
20528
|
-
if (graph !== null && isTombstoned(graph, ordinal)) continue;
|
|
21053
|
+
if (ordinal === void 0 || flags.isRemoved(ordinal)) continue;
|
|
20529
21054
|
addToOrdinalFilter(filter, ordinal);
|
|
20530
21055
|
}
|
|
20531
21056
|
return filter;
|
|
@@ -20535,9 +21060,9 @@ function createSharedVectorSearcher(options) {
|
|
|
20535
21060
|
const filter = createOrdinalFilter(slots);
|
|
20536
21061
|
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
20537
21062
|
const partition = view.store.partitionAt(ordinal);
|
|
20538
|
-
if (partition
|
|
20539
|
-
|
|
20540
|
-
|
|
21063
|
+
if (partition === void 0 || !allowed.has(partition)) continue;
|
|
21064
|
+
if (!view.store.holdsOrdinal(ordinal) || flags.isRemoved(ordinal)) continue;
|
|
21065
|
+
addToOrdinalFilter(filter, ordinal);
|
|
20541
21066
|
}
|
|
20542
21067
|
return filter;
|
|
20543
21068
|
}
|
|
@@ -20546,18 +21071,62 @@ function createSharedVectorSearcher(options) {
|
|
|
20546
21071
|
if (searchOptions.filterPartitions !== void 0) return filterForPartitions(searchOptions.filterPartitions);
|
|
20547
21072
|
return void 0;
|
|
20548
21073
|
}
|
|
20549
|
-
function
|
|
21074
|
+
function candidatesOf(ordinals, filter) {
|
|
21075
|
+
const candidates = { ordinals: [], docIds: [] };
|
|
21076
|
+
for (const ordinal of ordinals) {
|
|
21077
|
+
if (filter !== void 0 && !ordinalFilterHas(filter, ordinal)) continue;
|
|
21078
|
+
if (flags.isRemoved(ordinal)) continue;
|
|
21079
|
+
const docId = view.docIdOf(ordinal);
|
|
21080
|
+
if (docId === void 0 || view.ordinalOf(docId) !== ordinal) continue;
|
|
21081
|
+
candidates.ordinals.push(ordinal);
|
|
21082
|
+
candidates.docIds.push(docId);
|
|
21083
|
+
}
|
|
21084
|
+
return candidates;
|
|
21085
|
+
}
|
|
21086
|
+
function scanned(scan2) {
|
|
21087
|
+
return { results: scan2.results, matched: scan2.matched, matchedExact: true, resolvedEveryHit: true };
|
|
21088
|
+
}
|
|
21089
|
+
function search3(query, k, searchOptions, filter) {
|
|
20550
21090
|
const { metric, minSimilarity, efSearch, oversample } = searchOptions;
|
|
20551
|
-
const
|
|
21091
|
+
const empty = { results: [], matched: 0, matchedExact: true, resolvedEveryHit: true };
|
|
21092
|
+
const liveDocuments = flags.liveDocuments();
|
|
21093
|
+
if (liveDocuments <= 0) return empty;
|
|
21094
|
+
if (filter !== void 0 && filter.count === 0) return empty;
|
|
21095
|
+
function fetched(results, resolvedEveryHit2) {
|
|
21096
|
+
if (minSimilarity === Number.NEGATIVE_INFINITY) {
|
|
21097
|
+
const matched = filter === void 0 ? liveDocuments : filter.count;
|
|
21098
|
+
return { results, matched, matchedExact: true, resolvedEveryHit: resolvedEveryHit2 };
|
|
21099
|
+
}
|
|
21100
|
+
return { results, matched: results.length, matchedExact: false, resolvedEveryHit: resolvedEveryHit2 };
|
|
21101
|
+
}
|
|
21102
|
+
if (k <= 0) return fetched([], true);
|
|
20552
21103
|
const graph = view.graph;
|
|
20553
|
-
if (graph
|
|
20554
|
-
|
|
21104
|
+
if (graph === null) {
|
|
21105
|
+
const ordinals = filter === void 0 ? flags.unplacedOrdinals() : ordinalFilterValues(filter);
|
|
21106
|
+
return scanned(scoreCandidates(view.store, candidatesOf(ordinals, filter), query, k, metric, minSimilarity));
|
|
20555
21107
|
}
|
|
20556
|
-
|
|
20557
|
-
|
|
20558
|
-
|
|
21108
|
+
if (filter !== void 0) {
|
|
21109
|
+
const graphLiveSize = view.liveSize;
|
|
21110
|
+
const selectivity = graphLiveSize > 0 ? filter.count / graphLiveSize : 1;
|
|
21111
|
+
if (selectivity < view.handles.filterThreshold) {
|
|
21112
|
+
const candidates = candidatesOf(ordinalFilterValues(filter), filter);
|
|
21113
|
+
return scanned(scoreCandidates(view.store, candidates, query, k, metric, minSimilarity));
|
|
21114
|
+
}
|
|
21115
|
+
}
|
|
21116
|
+
let resolvedEveryHit = true;
|
|
21117
|
+
const graphResults = [];
|
|
21118
|
+
for (const hit of view.searchOrdinals(query, k, metric, minSimilarity, { filter, efSearch, oversample })) {
|
|
21119
|
+
const docId = view.docIdOf(hit.ord);
|
|
21120
|
+
if (docId === void 0) {
|
|
21121
|
+
resolvedEveryHit = false;
|
|
21122
|
+
continue;
|
|
21123
|
+
}
|
|
21124
|
+
graphResults.push({ docId, score: hit.score });
|
|
20559
21125
|
}
|
|
20560
|
-
|
|
21126
|
+
const unplaced = flags.unplacedOrdinals();
|
|
21127
|
+
if (unplaced.length === 0) return fetched(graphResults, resolvedEveryHit);
|
|
21128
|
+
const scan2 = scoreCandidates(view.store, candidatesOf(unplaced, filter), query, k, metric, minSimilarity);
|
|
21129
|
+
return fetched(mergeResults(graphResults, scan2.results, k), resolvedEveryHit);
|
|
20561
21130
|
}
|
|
20562
21131
|
return {
|
|
20563
21132
|
fieldName,
|
|
@@ -20572,22 +21141,20 @@ function createSharedVectorSearcher(options) {
|
|
|
20572
21141
|
{ expected: view.handles.dimension, received: query.length }
|
|
20573
21142
|
);
|
|
20574
21143
|
}
|
|
20575
|
-
const
|
|
20576
|
-
if (
|
|
20577
|
-
|
|
20578
|
-
|
|
20579
|
-
|
|
20580
|
-
|
|
20581
|
-
|
|
20582
|
-
const docId = view.docIdOf(hit.ord);
|
|
20583
|
-
if (docId === void 0 || !holdsDocument(docId)) continue;
|
|
20584
|
-
results.push({ docId, score: hit.score });
|
|
21144
|
+
const outcome = search3(query, k, searchOptions, filterFor(searchOptions));
|
|
21145
|
+
if (view.graphRetired) {
|
|
21146
|
+
throw new NarsilError(
|
|
21147
|
+
ErrorCodes.INDEX_NOT_FOUND,
|
|
21148
|
+
`The main thread replaced the graph of vector field "${fieldName}" while this thread searched it`,
|
|
21149
|
+
{ fieldName }
|
|
21150
|
+
);
|
|
20585
21151
|
}
|
|
20586
|
-
const
|
|
21152
|
+
const results = outcome.results.filter((result) => holdsDocument(result.docId));
|
|
21153
|
+
const holdsEveryHit = outcome.resolvedEveryHit && results.length === outcome.results.length;
|
|
20587
21154
|
return Promise.resolve({
|
|
20588
21155
|
results,
|
|
20589
|
-
matched:
|
|
20590
|
-
matchedExact:
|
|
21156
|
+
matched: outcome.matched,
|
|
21157
|
+
matchedExact: outcome.matchedExact && holdsEveryHit
|
|
20591
21158
|
});
|
|
20592
21159
|
}
|
|
20593
21160
|
};
|
|
@@ -20636,6 +21203,9 @@ function openSharedVectorField(initial, threadSlot) {
|
|
|
20636
21203
|
get readsEveryVector() {
|
|
20637
21204
|
return store.holdsEveryBlock;
|
|
20638
21205
|
},
|
|
21206
|
+
get graphRetired() {
|
|
21207
|
+
return graph !== null && Atomics.load(graph.header, GRAPH_RETIRED) === 1;
|
|
21208
|
+
},
|
|
20639
21209
|
docIdOf,
|
|
20640
21210
|
ordinalOf,
|
|
20641
21211
|
vectorOf(docId) {
|
|
@@ -20697,7 +21267,8 @@ function loadHeldVectorCopy(held, manager, fieldName, handle, handles, threadSlo
|
|
|
20697
21267
|
);
|
|
20698
21268
|
}
|
|
20699
21269
|
function holdsVectorField(held, fieldName) {
|
|
20700
|
-
|
|
21270
|
+
const view = held.fields.get(fieldName)?.view;
|
|
21271
|
+
return view?.readsEveryVector === true && !view.graphRetired;
|
|
20701
21272
|
}
|
|
20702
21273
|
function dropHeldVectorCopy(held, fieldName, handle) {
|
|
20703
21274
|
held.builds.get(handle)?.view.close();
|
|
@@ -20768,6 +21339,9 @@ function createDirectExecutor(options) {
|
|
|
20768
21339
|
return vectorIndexes;
|
|
20769
21340
|
}
|
|
20770
21341
|
function createIndex(indexName, config, language, analysisStale = false) {
|
|
21342
|
+
addIndex(indexName, config, language, analysisStale, null);
|
|
21343
|
+
}
|
|
21344
|
+
function addIndex(indexName, config, language, analysisStale, awaitedCopyLoad) {
|
|
20771
21345
|
if (indexes.has(indexName)) {
|
|
20772
21346
|
throw new NarsilError(ErrorCodes.INDEX_ALREADY_EXISTS, `Index "${indexName}" already exists`, {
|
|
20773
21347
|
indexName
|
|
@@ -20788,7 +21362,8 @@ function createDirectExecutor(options) {
|
|
|
20788
21362
|
},
|
|
20789
21363
|
vectorIndexes,
|
|
20790
21364
|
vectorCopies: createHeldVectorCopies(),
|
|
20791
|
-
analysisStale
|
|
21365
|
+
analysisStale,
|
|
21366
|
+
awaitedCopyLoad
|
|
20792
21367
|
});
|
|
20793
21368
|
}
|
|
20794
21369
|
function dropIndex(indexName) {
|
|
@@ -20810,7 +21385,7 @@ function createDirectExecutor(options) {
|
|
|
20810
21385
|
}
|
|
20811
21386
|
function queryContextOf(indexName) {
|
|
20812
21387
|
const entry = indexes.get(indexName);
|
|
20813
|
-
if (entry === void 0) return void 0;
|
|
21388
|
+
if (entry === void 0 || entry.awaitedCopyLoad !== null) return void 0;
|
|
20814
21389
|
return {
|
|
20815
21390
|
manager: entry.manager,
|
|
20816
21391
|
config: entry.config,
|
|
@@ -20840,7 +21415,12 @@ function createDirectExecutor(options) {
|
|
|
20840
21415
|
}
|
|
20841
21416
|
case "createIndex": {
|
|
20842
21417
|
const language = getLanguage(action.config.language ?? "english");
|
|
20843
|
-
|
|
21418
|
+
addIndex(action.indexName, action.config, language, action.analysisStale ?? false, action.copyLoad ?? null);
|
|
21419
|
+
return void 0;
|
|
21420
|
+
}
|
|
21421
|
+
case "copyLoaded": {
|
|
21422
|
+
const entry = indexes.get(action.indexName);
|
|
21423
|
+
if (entry?.awaitedCopyLoad === action.copyLoad) entry.awaitedCopyLoad = null;
|
|
20844
21424
|
return void 0;
|
|
20845
21425
|
}
|
|
20846
21426
|
case "dropIndex": {
|
|
@@ -21488,12 +22068,12 @@ async function lockDirectory(root) {
|
|
|
21488
22068
|
const resolvedRoot = pathMod.resolve(root);
|
|
21489
22069
|
const lockPath = pathMod.join(resolvedRoot, LOCK_FILE_NAME);
|
|
21490
22070
|
const self = await describeSelf();
|
|
21491
|
-
async function
|
|
22071
|
+
async function holds2() {
|
|
21492
22072
|
const holder = await readHolder(fs, lockPath);
|
|
21493
22073
|
return typeof holder === "object" && holder.nonce === self.nonce;
|
|
21494
22074
|
}
|
|
21495
22075
|
async function removeOwnLockFile() {
|
|
21496
|
-
if (await
|
|
22076
|
+
if (await holds2().catch(() => false)) await fs.unlink(lockPath).catch(() => void 0);
|
|
21497
22077
|
}
|
|
21498
22078
|
let directoryKey = null;
|
|
21499
22079
|
let closeDirectory = () => void 0;
|
|
@@ -21519,7 +22099,7 @@ async function lockDirectory(root) {
|
|
|
21519
22099
|
const heldKey = directoryKey;
|
|
21520
22100
|
let released = false;
|
|
21521
22101
|
return {
|
|
21522
|
-
holds,
|
|
22102
|
+
holds: holds2,
|
|
21523
22103
|
async release() {
|
|
21524
22104
|
if (released) return;
|
|
21525
22105
|
released = true;
|
|
@@ -29320,6 +29900,7 @@ function shareFrozenSegment(segment, analysis) {
|
|
|
29320
29900
|
}
|
|
29321
29901
|
|
|
29322
29902
|
// src/engine/worker-resync.ts
|
|
29903
|
+
var copyLoadSequence = 0;
|
|
29323
29904
|
function captureComposite(manager, partitionId) {
|
|
29324
29905
|
const partition = manager.getPartition(partitionId);
|
|
29325
29906
|
if (!isCompositePartition(partition)) return null;
|
|
@@ -29380,8 +29961,8 @@ async function sendPartition(indexName, executors, captured) {
|
|
|
29380
29961
|
)
|
|
29381
29962
|
);
|
|
29382
29963
|
}
|
|
29383
|
-
|
|
29384
|
-
|
|
29964
|
+
function transferIndexToPool(indexName, pool, config, manager, analysisStale = false) {
|
|
29965
|
+
return loadIndexOntoWorkers(
|
|
29385
29966
|
indexName,
|
|
29386
29967
|
pool.getAllExecutors(),
|
|
29387
29968
|
config,
|
|
@@ -29391,6 +29972,8 @@ async function transferIndexToPool(indexName, pool, config, manager, analysisSta
|
|
|
29391
29972
|
);
|
|
29392
29973
|
}
|
|
29393
29974
|
async function loadIndexOntoWorkers(indexName, executors, config, manager, onDropped, analysisStale = false) {
|
|
29975
|
+
copyLoadSequence += 1;
|
|
29976
|
+
const copyLoad = `${indexName}#${copyLoadSequence}`;
|
|
29394
29977
|
const captured = capturePartitions(manager);
|
|
29395
29978
|
await Promise.allSettled(
|
|
29396
29979
|
executors.map(
|
|
@@ -29405,13 +29988,15 @@ async function loadIndexOntoWorkers(indexName, executors, config, manager, onDro
|
|
|
29405
29988
|
indexName,
|
|
29406
29989
|
config,
|
|
29407
29990
|
requestId: `resync-create-${indexName}`,
|
|
29408
|
-
analysisStale
|
|
29991
|
+
analysisStale,
|
|
29992
|
+
copyLoad
|
|
29409
29993
|
})
|
|
29410
29994
|
)
|
|
29411
29995
|
);
|
|
29412
29996
|
for (const partition of captured) {
|
|
29413
29997
|
await sendPartition(indexName, executors, partition);
|
|
29414
29998
|
}
|
|
29999
|
+
return copyLoad;
|
|
29415
30000
|
}
|
|
29416
30001
|
|
|
29417
30002
|
// src/workers/protocol.ts
|
|
@@ -29659,6 +30244,14 @@ function enqueueReplication(state, indexName, action) {
|
|
|
29659
30244
|
queue.inFlight.push(run);
|
|
29660
30245
|
queue.tail = run;
|
|
29661
30246
|
}
|
|
30247
|
+
function markCopiesLoaded(state, indexName, copyLoad) {
|
|
30248
|
+
enqueueReplication(state, indexName, {
|
|
30249
|
+
type: "copyLoaded",
|
|
30250
|
+
indexName,
|
|
30251
|
+
copyLoad,
|
|
30252
|
+
requestId: `copy-loaded-${copyLoad}`
|
|
30253
|
+
});
|
|
30254
|
+
}
|
|
29662
30255
|
async function replicateToWorkers(state, action) {
|
|
29663
30256
|
if (!("indexName" in action)) {
|
|
29664
30257
|
if (state.workerPool === null) return;
|
|
@@ -30608,8 +31201,9 @@ async function loadOntoReplacement(state, replacement, indexName) {
|
|
|
30608
31201
|
if (entry === void 0 || manager === void 0 || !state.scaledOutIndexes.has(indexName)) return;
|
|
30609
31202
|
const buffered = [];
|
|
30610
31203
|
state.copyLoadBuffers.set(indexName, buffered);
|
|
31204
|
+
let copyLoad;
|
|
30611
31205
|
try {
|
|
30612
|
-
await loadIndexOntoWorkers(
|
|
31206
|
+
copyLoad = await loadIndexOntoWorkers(
|
|
30613
31207
|
indexName,
|
|
30614
31208
|
[replacement.executor],
|
|
30615
31209
|
entry.config,
|
|
@@ -30622,6 +31216,7 @@ async function loadOntoReplacement(state, replacement, indexName) {
|
|
|
30622
31216
|
state.copyLoadBuffers.delete(indexName);
|
|
30623
31217
|
for (const action of buffered) enqueueReplication(state, indexName, action);
|
|
30624
31218
|
}
|
|
31219
|
+
markCopiesLoaded(state, indexName, copyLoad);
|
|
30625
31220
|
refreshVectorCopies(state, indexName);
|
|
30626
31221
|
}
|
|
30627
31222
|
async function replaceWorker(state, pool, workerId) {
|
|
@@ -30751,21 +31346,21 @@ async function ensurePool(state) {
|
|
|
30751
31346
|
function documentCountOf(state, indexName) {
|
|
30752
31347
|
return state.executor.getManager(indexName)?.countDocuments() ?? 0;
|
|
30753
31348
|
}
|
|
30754
|
-
function copyThresholdReason(state, indexName,
|
|
31349
|
+
function copyThresholdReason(state, indexName, incomingCount2 = 0) {
|
|
30755
31350
|
const held = documentCountOf(state, indexName);
|
|
30756
|
-
if (
|
|
31351
|
+
if (incomingCount2 <= 0) {
|
|
30757
31352
|
return `Index "${indexName}" holds ${held} documents, reaching the copy threshold of ${state.copyThreshold}`;
|
|
30758
31353
|
}
|
|
30759
|
-
return `Index "${indexName}" holds ${held} documents, and the batch that the engine is writing adds ${
|
|
31354
|
+
return `Index "${indexName}" holds ${held} documents, and the batch that the engine is writing adds ${incomingCount2}, for ${held + incomingCount2} against the copy threshold of ${state.copyThreshold}`;
|
|
30760
31355
|
}
|
|
30761
|
-
function indexReadyForCopies(state, indexName,
|
|
31356
|
+
function indexReadyForCopies(state, indexName, incomingCount2 = 0) {
|
|
30762
31357
|
if (!copiesAllowed(state)) return false;
|
|
30763
31358
|
if (state.scaledOutIndexes.has(indexName) || state.desyncedIndexes.has(indexName)) return false;
|
|
30764
31359
|
if (state.copyTransitions.has(indexName)) return false;
|
|
30765
31360
|
if (state.callbacks?.shouldDeferCopies?.()) return false;
|
|
30766
31361
|
const entry = state.indexRegistry.get(indexName);
|
|
30767
31362
|
if (entry === void 0 || state.executor.getManager(indexName) === void 0) return false;
|
|
30768
|
-
if (documentCountOf(state, indexName) +
|
|
31363
|
+
if (documentCountOf(state, indexName) + incomingCount2 < state.copyThreshold) return false;
|
|
30769
31364
|
const ineligibility = workerIneligibility(indexName, entry.config, state.bootstrapModule);
|
|
30770
31365
|
if (ineligibility !== null) {
|
|
30771
31366
|
reportIneligible(state, indexName, ineligibility);
|
|
@@ -30798,7 +31393,13 @@ async function loadCopies(state, indexName, reason) {
|
|
|
30798
31393
|
state.copyLoadBuffers.set(indexName, buffered);
|
|
30799
31394
|
try {
|
|
30800
31395
|
freezeLiveTailsBeforeCopiesLoad(manager);
|
|
30801
|
-
|
|
31396
|
+
const copyLoad = await transferIndexToPool(
|
|
31397
|
+
indexName,
|
|
31398
|
+
pool,
|
|
31399
|
+
entry.config,
|
|
31400
|
+
manager,
|
|
31401
|
+
state.callbacks?.isAnalysisStale?.(indexName)
|
|
31402
|
+
);
|
|
30802
31403
|
if (state.desyncedIndexes.has(indexName)) {
|
|
30803
31404
|
discardLoadedCopies(state, pool, indexName);
|
|
30804
31405
|
return;
|
|
@@ -30807,6 +31408,7 @@ async function loadCopies(state, indexName, reason) {
|
|
|
30807
31408
|
state.lastAccessAt.set(indexName, Date.now());
|
|
30808
31409
|
state.poolRetryDelayMs = POOL_RESTART_DELAY_MS;
|
|
30809
31410
|
for (const action of buffered) enqueueReplication(state, indexName, action);
|
|
31411
|
+
markCopiesLoaded(state, indexName, copyLoad);
|
|
30810
31412
|
} catch (err) {
|
|
30811
31413
|
if (dropReason !== void 0) state.droppedCopies.set(indexName, dropReason);
|
|
30812
31414
|
throw err;
|
|
@@ -30849,15 +31451,15 @@ async function scaleOutReadyIndexes(state) {
|
|
|
30849
31451
|
}, 0);
|
|
30850
31452
|
}
|
|
30851
31453
|
}
|
|
30852
|
-
async function scaleOutBeforeBatch(state, indexName,
|
|
30853
|
-
if (
|
|
31454
|
+
async function scaleOutBeforeBatch(state, indexName, incomingCount2) {
|
|
31455
|
+
if (incomingCount2 <= 0) return;
|
|
30854
31456
|
const pending = state.copyTransitions.get(indexName);
|
|
30855
31457
|
if (pending !== void 0) {
|
|
30856
31458
|
if (pending.kind === "load") await pending.done;
|
|
30857
31459
|
return;
|
|
30858
31460
|
}
|
|
30859
|
-
if (!indexReadyForCopies(state, indexName,
|
|
30860
|
-
await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName,
|
|
31461
|
+
if (!indexReadyForCopies(state, indexName, incomingCount2)) return;
|
|
31462
|
+
await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName, incomingCount2));
|
|
30861
31463
|
}
|
|
30862
31464
|
|
|
30863
31465
|
// src/engine/orchestration/idle.ts
|
|
@@ -31214,8 +31816,15 @@ function createWorkerOrchestrator(config, executor, indexRegistry, callbacks, ve
|
|
|
31214
31816
|
const manager = executor.getManager(indexName);
|
|
31215
31817
|
if (pool !== null && entry !== void 0 && manager !== void 0) {
|
|
31216
31818
|
try {
|
|
31217
|
-
|
|
31819
|
+
const copyLoad = await transferIndexToPool(
|
|
31820
|
+
indexName,
|
|
31821
|
+
pool,
|
|
31822
|
+
entry.config,
|
|
31823
|
+
manager,
|
|
31824
|
+
callbacks?.isAnalysisStale?.(indexName)
|
|
31825
|
+
);
|
|
31218
31826
|
state.scaledOutIndexes.add(indexName);
|
|
31827
|
+
markCopiesLoaded(state, indexName, copyLoad);
|
|
31219
31828
|
refreshVectorCopies(state, indexName);
|
|
31220
31829
|
} catch (repairError) {
|
|
31221
31830
|
throw new NarsilError(
|
|
@@ -31280,7 +31889,7 @@ function createWorkerOrchestrator(config, executor, indexRegistry, callbacks, ve
|
|
|
31280
31889
|
}
|
|
31281
31890
|
return {
|
|
31282
31891
|
scaleOutReadyIndexes: () => scaleOutReadyIndexes(state),
|
|
31283
|
-
scaleOutBeforeBatch: (indexName,
|
|
31892
|
+
scaleOutBeforeBatch: (indexName, incomingCount2) => scaleOutBeforeBatch(state, indexName, incomingCount2),
|
|
31284
31893
|
replicateToWorkers: replicate,
|
|
31285
31894
|
awaitReplication: (indexName) => awaitReplicationIdle(state, indexName),
|
|
31286
31895
|
awaitWrites: (indexName) => awaitWritesApplied(state, indexName),
|
|
@@ -34229,7 +34838,7 @@ function dateOf(value, path) {
|
|
|
34229
34838
|
refuse(path, `holds a "${DATE_TAG}" tag outside the range of a date`);
|
|
34230
34839
|
return new Date(milliseconds);
|
|
34231
34840
|
}
|
|
34232
|
-
function
|
|
34841
|
+
function bytesOf2(value, path) {
|
|
34233
34842
|
const bytes = typeof value === "string" ? decodeExactBase64(value) : null;
|
|
34234
34843
|
if (bytes === null) refuse(path, `holds a "${BYTES_TAG}" tag whose text is not padded base64`);
|
|
34235
34844
|
return bytes;
|
|
@@ -34259,7 +34868,7 @@ function untagTag(tag, value, path, depth) {
|
|
|
34259
34868
|
case DATE_TAG:
|
|
34260
34869
|
return dateOf(value, path);
|
|
34261
34870
|
case BYTES_TAG:
|
|
34262
|
-
return
|
|
34871
|
+
return bytesOf2(value, path);
|
|
34263
34872
|
case NUMBER_TAG:
|
|
34264
34873
|
return numberOf(value, path);
|
|
34265
34874
|
case OBJECT_TAG:
|
|
@@ -36602,15 +37211,15 @@ function oversampleOf(vectorConfig) {
|
|
|
36602
37211
|
return oversample;
|
|
36603
37212
|
}
|
|
36604
37213
|
function vectorFetchDepth(limit, offset, cursorDepth) {
|
|
36605
|
-
const
|
|
36606
|
-
if (
|
|
37214
|
+
const reach2 = cursorDepth + offset + limit;
|
|
37215
|
+
if (reach2 > RESULT_WINDOW) {
|
|
36607
37216
|
throw new NarsilError(
|
|
36608
37217
|
ErrorCodes.SEARCH_RESULT_WINDOW_EXCEEDED,
|
|
36609
|
-
`The engine pages a vector search through the first ${RESULT_WINDOW} results, and this page ends at ${
|
|
37218
|
+
`The engine pages a vector search through the first ${RESULT_WINDOW} results, and this page ends at ${reach2}. Narrow the search with a filter or a similarity floor`,
|
|
36610
37219
|
{ limit, offset, cursorDepth, window: RESULT_WINDOW }
|
|
36611
37220
|
);
|
|
36612
37221
|
}
|
|
36613
|
-
return Math.min(
|
|
37222
|
+
return Math.min(reach2 + 1, RESULT_WINDOW);
|
|
36614
37223
|
}
|
|
36615
37224
|
async function executeVectorSearch(params, context, limit, offset, cursorDepth = 0) {
|
|
36616
37225
|
const { manager, config, partitionIds } = context;
|