@delali/narsil 0.3.1-next.82 → 0.3.1-next.83

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.
@@ -12920,9 +12920,17 @@ function createOsqQuantizer(dimension, bits, metric, store) {
12920
12920
  if (view === null || ordinal === void 0 || !view.holdsOrdinal(ordinal)) return void 0;
12921
12921
  return view.recordAt(ordinal);
12922
12922
  },
12923
+ recordAtOrdinal(ordinal) {
12924
+ const view = shared2();
12925
+ if (view === null || !view.holdsOrdinal(ordinal)) return void 0;
12926
+ return view.recordAt(ordinal);
12927
+ },
12923
12928
  restoreRecord(docId, record) {
12924
12929
  requireShared().restoreRecord(ordinalOf(docId), record);
12925
12930
  },
12931
+ restoreRecordAtOrdinal(ordinal, record) {
12932
+ requireShared().restoreRecord(ordinal, record);
12933
+ },
12926
12934
  restoreCentroid(centroid) {
12927
12935
  requireShared().writeCentroid(centroid);
12928
12936
  },
@@ -12955,6 +12963,43 @@ function createOsqQuantizer(dimension, bits, metric, store) {
12955
12963
  };
12956
12964
  }
12957
12965
 
12966
+ // src/vector/similarity.ts
12967
+ function magnitude(v) {
12968
+ const result = simdMagnitude(v);
12969
+ if (result !== null) return result;
12970
+ let sum = 0;
12971
+ for (let i = 0; i < v.length; i++) {
12972
+ sum += v[i] * v[i];
12973
+ }
12974
+ return Math.sqrt(sum);
12975
+ }
12976
+ function cosineSimilarityWithMagnitudes(a, b, magA, magB) {
12977
+ if (magA === 0 || magB === 0) return 0;
12978
+ return dotProduct(a, b) / (magA * magB);
12979
+ }
12980
+ function dotProduct(a, b) {
12981
+ const result = simdDotProduct(a, b);
12982
+ if (result !== null) return result;
12983
+ let sum = 0;
12984
+ for (let i = 0; i < a.length; i++) {
12985
+ sum += a[i] * b[i];
12986
+ }
12987
+ return sum;
12988
+ }
12989
+ function euclideanDistance(a, b) {
12990
+ return Math.sqrt(squaredEuclideanDistance(a, b));
12991
+ }
12992
+ function squaredEuclideanDistance(a, b) {
12993
+ const result = simdSquaredEuclideanDistance(a, b);
12994
+ if (result !== null) return result;
12995
+ let sum = 0;
12996
+ for (let i = 0; i < a.length; i++) {
12997
+ const diff = a[i] - b[i];
12998
+ sum += diff * diff;
12999
+ }
13000
+ return sum;
13001
+ }
13002
+
12958
13003
  // src/vector/vector-store/doc-ids.ts
12959
13004
  var encoder2 = new TextEncoder();
12960
13005
  function appendDocId(handles, ordinal, docId) {
@@ -12974,6 +13019,78 @@ function resetDocIds(handles) {
12974
13019
  Atomics.store(handles.header, STORE_DOC_ID_BYTES, 0);
12975
13020
  }
12976
13021
 
13022
+ // src/vector/vector-store/ordinal-reads.ts
13023
+ function entryAt(store, ordinal) {
13024
+ return { vector: store.view.vectorAt(ordinal), magnitude: store.magnitudes[ordinal] };
13025
+ }
13026
+ function createOrdinalReads(openStore, readable) {
13027
+ function pairDistance(metric) {
13028
+ const open = openStore();
13029
+ if (open === null) return () => Number.POSITIVE_INFINITY;
13030
+ const inner = open.view.pairDistance(metric);
13031
+ return (ordA, ordB) => readable(ordA) && readable(ordB) ? inner(ordA, ordB) : Number.POSITIVE_INFINITY;
13032
+ }
13033
+ return {
13034
+ entryForOrdinal(ordinal) {
13035
+ const open = openStore();
13036
+ if (open === null || !readable(ordinal)) return void 0;
13037
+ return entryAt(open, ordinal);
13038
+ },
13039
+ distanceByOrdinal(ordA, ordB, metric) {
13040
+ const open = openStore();
13041
+ if (open === null || !readable(ordA) || !readable(ordB)) return Number.POSITIVE_INFINITY;
13042
+ return open.view.distanceByOrdinal(ordA, ordB, metric);
13043
+ },
13044
+ distanceFromArena(prepared, ordinal, metric) {
13045
+ const open = openStore();
13046
+ if (open === null || !readable(ordinal)) return Number.POSITIVE_INFINITY;
13047
+ return open.view.distanceFromArena(prepared, ordinal, metric);
13048
+ },
13049
+ queryDistance(prepared, metric) {
13050
+ const open = openStore();
13051
+ if (open === null) return () => Number.POSITIVE_INFINITY;
13052
+ const inner = open.view.queryDistance(prepared, metric);
13053
+ return (ordinal) => readable(ordinal) ? inner(ordinal) : Number.POSITIVE_INFINITY;
13054
+ },
13055
+ ordinalDistance(from, metric) {
13056
+ const pair = pairDistance(metric);
13057
+ return (ordinal) => pair(from, ordinal);
13058
+ },
13059
+ pairDistance
13060
+ };
13061
+ }
13062
+
13063
+ // src/vector/vector-store/listing.ts
13064
+ function* entriesOf(open, documents) {
13065
+ if (open === null) return;
13066
+ for (let ordinal = 0; ordinal < documents.length; ordinal++) {
13067
+ const docId = documents[ordinal];
13068
+ if (docId === void 0) continue;
13069
+ yield [docId, entryAt(open, ordinal)];
13070
+ }
13071
+ }
13072
+ function* docIdsOf(open, documents) {
13073
+ if (open === null) return;
13074
+ for (let ordinal = 0; ordinal < documents.length; ordinal++) {
13075
+ const docId = documents[ordinal];
13076
+ if (docId !== void 0) yield docId;
13077
+ }
13078
+ }
13079
+ function snapshotOf(open, documents, replacedOrdinals) {
13080
+ const slots = documents.length;
13081
+ const dimension = open === null ? 0 : open.handles.dimension;
13082
+ const vectors = new Float32Array(slots * dimension);
13083
+ const magnitudes = new Float64Array(slots);
13084
+ const docIds = new Array(slots);
13085
+ for (let ordinal = 0; ordinal < slots; ordinal++) {
13086
+ docIds[ordinal] = documents[ordinal] ?? null;
13087
+ if (open === null || documents[ordinal] === void 0 && !replacedOrdinals.has(ordinal)) continue;
13088
+ vectors.set(open.view.vectorAt(ordinal), ordinal * dimension);
13089
+ magnitudes[ordinal] = open.magnitudes[ordinal];
13090
+ }
13091
+ return { dimension, slots, vectors, magnitudes, docIds, replacedOrdinals: [...replacedOrdinals] };
13092
+ }
13093
+
12977
13094
  // src/vector/ordinal-filter.ts
12978
13095
  function createOrdinalFilter(slots) {
12979
13096
  return { bits: new Uint8Array(slots + 7 >>> 3), count: 0 };
@@ -13010,43 +13127,6 @@ function* ordinalFilterValues(filter) {
13010
13127
  }
13011
13128
  }
13012
13129
 
13013
- // src/vector/similarity.ts
13014
- function magnitude(v) {
13015
- const result = simdMagnitude(v);
13016
- if (result !== null) return result;
13017
- let sum = 0;
13018
- for (let i = 0; i < v.length; i++) {
13019
- sum += v[i] * v[i];
13020
- }
13021
- return Math.sqrt(sum);
13022
- }
13023
- function cosineSimilarityWithMagnitudes(a, b, magA, magB) {
13024
- if (magA === 0 || magB === 0) return 0;
13025
- return dotProduct(a, b) / (magA * magB);
13026
- }
13027
- function dotProduct(a, b) {
13028
- const result = simdDotProduct(a, b);
13029
- if (result !== null) return result;
13030
- let sum = 0;
13031
- for (let i = 0; i < a.length; i++) {
13032
- sum += a[i] * b[i];
13033
- }
13034
- return sum;
13035
- }
13036
- function euclideanDistance(a, b) {
13037
- return Math.sqrt(squaredEuclideanDistance(a, b));
13038
- }
13039
- function squaredEuclideanDistance(a, b) {
13040
- const result = simdSquaredEuclideanDistance(a, b);
13041
- if (result !== null) return result;
13042
- let sum = 0;
13043
- for (let i = 0; i < a.length; i++) {
13044
- const diff = a[i] - b[i];
13045
- sum += diff * diff;
13046
- }
13047
- return sum;
13048
- }
13049
-
13050
13130
  // src/vector/vector-store/vector-file.browser.ts
13051
13131
  function filesystemUnavailable() {
13052
13132
  throw new NarsilError(
@@ -13480,13 +13560,14 @@ function writeVector(store, ordinal, vector) {
13480
13560
  block.float32(base + vector.length).set(vector, base);
13481
13561
  store.magnitudes[ordinal] = magnitude(vector);
13482
13562
  }
13483
- function blockFullyCold(store, blockIndex, documents) {
13563
+ function blockFullyCold(store, blockIndex, documents, replacedOrdinals) {
13484
13564
  const capacity = store.handles.layout.capacity;
13485
13565
  const slots = Atomics.load(store.handles.header, STORE_SLOTS);
13486
13566
  const end = Math.min(slots, (blockIndex + 1) * capacity);
13487
13567
  if (end < (blockIndex + 1) * capacity) return false;
13488
13568
  for (let ordinal = blockIndex * capacity; ordinal < end; ordinal++) {
13489
- if (documents[ordinal] !== void 0 && store.diskFile[ordinal] === IN_MEMORY) return false;
13569
+ const held = documents[ordinal] !== void 0 || replacedOrdinals.has(ordinal);
13570
+ if (held && store.diskFile[ordinal] === IN_MEMORY) return false;
13490
13571
  }
13491
13572
  return true;
13492
13573
  }
@@ -13499,10 +13580,11 @@ function fileHoldsSameBytes(store, ordinal, location) {
13499
13580
  for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
13500
13581
  return true;
13501
13582
  }
13502
- function forgetUnreferencedFiles(store, documents) {
13583
+ function forgetUnreferencedFiles(store, documents, replacedOrdinals) {
13503
13584
  const referenced = /* @__PURE__ */ new Set();
13504
13585
  for (let ordinal = 0; ordinal < documents.length; ordinal++) {
13505
- if (documents[ordinal] !== void 0 && store.diskFile[ordinal] !== IN_MEMORY) {
13586
+ const held = documents[ordinal] !== void 0 || replacedOrdinals.has(ordinal);
13587
+ if (held && store.diskFile[ordinal] !== IN_MEMORY) {
13506
13588
  referenced.add(store.diskFile[ordinal]);
13507
13589
  }
13508
13590
  }
@@ -13543,15 +13625,20 @@ function createVectorStore(options) {
13543
13625
  const blockBytes2 = options?.blockBytes ?? BLOCK_MAX_BYTES;
13544
13626
  const docToOrd = /* @__PURE__ */ new Map();
13545
13627
  const ordToDoc = [];
13628
+ const replacedOrdinals = /* @__PURE__ */ new Set();
13546
13629
  let unknownPartitions = 0;
13547
13630
  let liveCount = 0;
13548
13631
  let open = options?.dimension === void 0 ? null : openHandles(options.dimension, codeBits, blockBytes2);
13549
13632
  function forgetDocuments() {
13550
13633
  docToOrd.clear();
13551
13634
  ordToDoc.length = 0;
13635
+ replacedOrdinals.clear();
13552
13636
  unknownPartitions = 0;
13553
13637
  liveCount = 0;
13554
13638
  }
13639
+ function readable(ordinal) {
13640
+ return ordToDoc[ordinal] !== void 0 || replacedOrdinals.has(ordinal);
13641
+ }
13555
13642
  function requireOpen() {
13556
13643
  if (open === null) {
13557
13644
  throw new NarsilError(ErrorCodes.VECTOR_DIMENSION_MISMATCH, "The vector store holds no vector yet");
@@ -13568,11 +13655,27 @@ function createVectorStore(options) {
13568
13655
  Atomics.store(store.present, ordinal, 0);
13569
13656
  if (store.partitions[ordinal] === UNKNOWN_PARTITION) unknownPartitions -= 1;
13570
13657
  }
13571
- function claimOrdinal(store, docId, cold, partitionId) {
13658
+ function keepReplacedOrdinal(store, ordinal) {
13659
+ ordToDoc[ordinal] = void 0;
13660
+ if (store.partitions[ordinal] === UNKNOWN_PARTITION) unknownPartitions -= 1;
13661
+ store.partitions[ordinal] = NO_PARTITION;
13662
+ replacedOrdinals.add(ordinal);
13663
+ }
13664
+ function restoreReplacedVector(store, ordinal, vector) {
13665
+ store.diskFile[ordinal] = IN_MEMORY;
13666
+ writeVector(store, ordinal, vector);
13667
+ Atomics.store(store.present, ordinal, 1);
13668
+ replacedOrdinals.add(ordinal);
13669
+ }
13670
+ function claimSlot(store, docId, cold) {
13572
13671
  const ordinal = Atomics.load(store.handles.header, STORE_SLOTS);
13573
13672
  ensureOrdinal(store, ordinal, cold);
13574
13673
  store.diskFile[ordinal] = IN_MEMORY;
13575
13674
  appendDocId(store.handles, ordinal, docId);
13675
+ return ordinal;
13676
+ }
13677
+ function claimOrdinal(store, docId, cold, partitionId) {
13678
+ const ordinal = claimSlot(store, docId, cold);
13576
13679
  recordPartition(store, ordinal, partitionId);
13577
13680
  ordToDoc[ordinal] = docId;
13578
13681
  docToOrd.set(docId, ordinal);
@@ -13582,25 +13685,30 @@ function createVectorStore(options) {
13582
13685
  Atomics.store(store.handles.header, STORE_SLOTS, ordinal + 1);
13583
13686
  Atomics.store(store.present, ordinal, 1);
13584
13687
  }
13688
+ function publishOnDisk(store, ordinal, vectorMagnitude, location) {
13689
+ store.magnitudes[ordinal] = vectorMagnitude;
13690
+ store.diskOffset[ordinal] = location.offset;
13691
+ store.diskFile[ordinal] = location.fileIndex;
13692
+ publishOrdinal(store, ordinal);
13693
+ }
13585
13694
  function appendOrdinal(store, docId, vector, partitionId) {
13586
13695
  const ordinal = claimOrdinal(store, docId, false, partitionId);
13587
13696
  writeVector(store, ordinal, vector);
13588
13697
  publishOrdinal(store, ordinal);
13589
13698
  return ordinal;
13590
13699
  }
13591
- function settleReplacement(store, ordinal, previous) {
13700
+ function settleReplacement(store, ordinal, previous, keepsReplacedVector) {
13592
13701
  if (previous !== void 0) {
13593
- retireOrdinal(store, previous);
13702
+ if (keepsReplacedVector) keepReplacedOrdinal(store, previous);
13703
+ else retireOrdinal(store, previous);
13594
13704
  return ordinal;
13595
13705
  }
13596
13706
  liveCount++;
13597
13707
  Atomics.add(store.handles.header, STORE_LIVE_COUNT, 1);
13598
13708
  return ordinal;
13599
13709
  }
13600
- function entryAt(store, ordinal) {
13601
- return { vector: store.view.vectorAt(ordinal), magnitude: store.magnitudes[ordinal] };
13602
- }
13603
13710
  return {
13711
+ ...createOrdinalReads(() => open, readable),
13604
13712
  get size() {
13605
13713
  return liveCount;
13606
13714
  },
@@ -13619,7 +13727,10 @@ function createVectorStore(options) {
13619
13727
  get view() {
13620
13728
  return requireOpen().view;
13621
13729
  },
13622
- insert(docId, vector, partitionId) {
13730
+ get replacedVectorCount() {
13731
+ return replacedOrdinals.size;
13732
+ },
13733
+ insert(docId, vector, partitionId, keepsReplacedVector = false) {
13623
13734
  if (open === null) open = openHandles(vector.length, codeBits, blockBytes2);
13624
13735
  const store = open;
13625
13736
  if (vector.length !== store.handles.dimension) {
@@ -13631,18 +13742,37 @@ function createVectorStore(options) {
13631
13742
  }
13632
13743
  const previous = docToOrd.get(docId);
13633
13744
  const ordinal = appendOrdinal(store, docId, vector, partitionId);
13634
- return settleReplacement(store, ordinal, previous);
13745
+ return settleReplacement(store, ordinal, previous, keepsReplacedVector);
13746
+ },
13747
+ releaseReplacedVectors() {
13748
+ const released = [...replacedOrdinals];
13749
+ replacedOrdinals.clear();
13750
+ if (open !== null) for (const ordinal of released) Atomics.store(open.present, ordinal, 0);
13751
+ return released;
13635
13752
  },
13636
13753
  insertCold(docId, vectorMagnitude, location, partitionId) {
13637
13754
  const store = requireOpen();
13638
13755
  const previous = docToOrd.get(docId);
13639
13756
  Atomics.store(store.handles.header, STORE_RELEASED_VECTORS_TO_DISK, 1);
13640
13757
  const ordinal = claimOrdinal(store, docId, true, partitionId);
13641
- store.magnitudes[ordinal] = vectorMagnitude;
13642
- store.diskOffset[ordinal] = location.offset;
13643
- store.diskFile[ordinal] = location.fileIndex;
13758
+ publishOnDisk(store, ordinal, vectorMagnitude, location);
13759
+ return settleReplacement(store, ordinal, previous, false);
13760
+ },
13761
+ insertReplaced(docId, vector, location) {
13762
+ if (open === null) open = openHandles(vector.length, codeBits, blockBytes2);
13763
+ const store = open;
13764
+ if (location !== void 0) Atomics.store(store.handles.header, STORE_RELEASED_VECTORS_TO_DISK, 1);
13765
+ const ordinal = claimSlot(store, docId, location !== void 0);
13766
+ store.partitions[ordinal] = NO_PARTITION;
13767
+ ordToDoc[ordinal] = void 0;
13768
+ replacedOrdinals.add(ordinal);
13769
+ if (location !== void 0) {
13770
+ publishOnDisk(store, ordinal, magnitude(vector), location);
13771
+ return ordinal;
13772
+ }
13773
+ writeVector(store, ordinal, vector);
13644
13774
  publishOrdinal(store, ordinal);
13645
- return settleReplacement(store, ordinal, previous);
13775
+ return ordinal;
13646
13776
  },
13647
13777
  addVectorFile(path) {
13648
13778
  const store = requireOpen();
@@ -13669,11 +13799,11 @@ function createVectorStore(options) {
13669
13799
  const store = requireOpen();
13670
13800
  let released = 0;
13671
13801
  for (let index = 0; index < store.handles.blocks.length; index++) {
13672
- if (store.handles.blocks[index] === null || !blockFullyCold(store, index, ordToDoc)) continue;
13802
+ if (store.handles.blocks[index] === null || !blockFullyCold(store, index, ordToDoc, replacedOrdinals)) continue;
13673
13803
  store.handles.blocks[index] = null;
13674
13804
  released += 1;
13675
13805
  }
13676
- if (released + forgetUnreferencedFiles(store, ordToDoc) > 0) relayout(store);
13806
+ if (released + forgetUnreferencedFiles(store, ordToDoc, replacedOrdinals) > 0) relayout(store);
13677
13807
  return released;
13678
13808
  },
13679
13809
  setPartition(docId, partitionId) {
@@ -13717,21 +13847,8 @@ function createVectorStore(options) {
13717
13847
  holdsOrdinal(ordinal) {
13718
13848
  return ordinal >= 0 && ordinal < ordToDoc.length && ordToDoc[ordinal] !== void 0;
13719
13849
  },
13720
- *entries() {
13721
- if (open === null) return;
13722
- for (let ordinal = 0; ordinal < ordToDoc.length; ordinal++) {
13723
- const docId = ordToDoc[ordinal];
13724
- if (docId === void 0) continue;
13725
- yield [docId, entryAt(open, ordinal)];
13726
- }
13727
- },
13728
- *docIds() {
13729
- if (open === null) return;
13730
- for (let ordinal = 0; ordinal < ordToDoc.length; ordinal++) {
13731
- const docId = ordToDoc[ordinal];
13732
- if (docId !== void 0) yield docId;
13733
- }
13734
- },
13850
+ entries: () => entriesOf(open, ordToDoc),
13851
+ docIds: () => docIdsOf(open, ordToDoc),
13735
13852
  clear() {
13736
13853
  forgetDocuments();
13737
13854
  if (open === null) return;
@@ -13763,59 +13880,16 @@ function createVectorStore(options) {
13763
13880
  docIdForOrdinal(ordinal) {
13764
13881
  return ordToDoc[ordinal];
13765
13882
  },
13766
- entryForOrdinal(ordinal) {
13767
- if (open === null || ordinal < 0 || ordinal >= ordToDoc.length || ordToDoc[ordinal] === void 0) {
13768
- return void 0;
13769
- }
13770
- return entryAt(open, ordinal);
13771
- },
13772
- distanceByOrdinal(ordA, ordB, metric) {
13773
- if (open === null || ordToDoc[ordA] === void 0 || ordToDoc[ordB] === void 0) {
13774
- return Number.POSITIVE_INFINITY;
13775
- }
13776
- return open.view.distanceByOrdinal(ordA, ordB, metric);
13777
- },
13778
13883
  prepareQueryArena(query) {
13779
13884
  return open === null ? null : open.view.prepareQueryArena(query);
13780
13885
  },
13781
- distanceFromArena(prepared, ordinal, metric) {
13782
- if (open === null || ordToDoc[ordinal] === void 0) return Number.POSITIVE_INFINITY;
13783
- return open.view.distanceFromArena(prepared, ordinal, metric);
13784
- },
13785
- queryDistance(prepared, metric) {
13786
- if (open === null) return () => Number.POSITIVE_INFINITY;
13787
- const inner = open.view.queryDistance(prepared, metric);
13788
- return (ordinal) => ordToDoc[ordinal] === void 0 ? Number.POSITIVE_INFINITY : inner(ordinal);
13789
- },
13790
- ordinalDistance(from, metric) {
13791
- const pair = this.pairDistance(metric);
13792
- return (ordinal) => pair(from, ordinal);
13793
- },
13794
- pairDistance(metric) {
13795
- if (open === null) return () => Number.POSITIVE_INFINITY;
13796
- const inner = open.view.pairDistance(metric);
13797
- return (ordA, ordB) => ordToDoc[ordA] === void 0 || ordToDoc[ordB] === void 0 ? Number.POSITIVE_INFINITY : inner(ordA, ordB);
13798
- },
13799
- exportSnapshot() {
13800
- const slots = ordToDoc.length;
13801
- const dimension = open === null ? 0 : open.handles.dimension;
13802
- const vectors = new Float32Array(slots * dimension);
13803
- const magnitudes = new Float64Array(slots);
13804
- const docIds = new Array(slots);
13805
- for (let ordinal = 0; ordinal < slots; ordinal++) {
13806
- const docId = ordToDoc[ordinal];
13807
- docIds[ordinal] = docId ?? null;
13808
- if (docId === void 0 || open === null) continue;
13809
- vectors.set(open.view.vectorAt(ordinal), ordinal * dimension);
13810
- magnitudes[ordinal] = open.magnitudes[ordinal];
13811
- }
13812
- return { dimension, slots, vectors, magnitudes, docIds };
13813
- },
13886
+ exportSnapshot: () => snapshotOf(open, ordToDoc, replacedOrdinals),
13814
13887
  restoreSnapshot(snapshot) {
13815
13888
  this.clear();
13816
13889
  if (snapshot.dimension === 0 || snapshot.slots === 0) return;
13817
13890
  if (open === null) open = openHandles(snapshot.dimension, codeBits, blockBytes2);
13818
13891
  const store = open;
13892
+ const replaced = new Set(snapshot.replacedOrdinals);
13819
13893
  for (let ordinal = 0; ordinal < snapshot.slots; ordinal++) {
13820
13894
  const docId = snapshot.docIds[ordinal];
13821
13895
  const vector = snapshot.vectors.subarray(ordinal * snapshot.dimension, (ordinal + 1) * snapshot.dimension);
@@ -13823,6 +13897,7 @@ function createVectorStore(options) {
13823
13897
  ensureOrdinal(store, ordinal, false);
13824
13898
  ordToDoc[ordinal] = void 0;
13825
13899
  store.partitions[ordinal] = NO_PARTITION;
13900
+ if (replaced.has(ordinal)) restoreReplacedVector(store, ordinal, vector);
13826
13901
  Atomics.store(store.handles.header, STORE_SLOTS, ordinal + 1);
13827
13902
  continue;
13828
13903
  }
@@ -14647,7 +14722,7 @@ function readNeighborsLocked(state, ord, layer) {
14647
14722
  if (nodeReadHeld(state.locks, ord, version)) return count;
14648
14723
  }
14649
14724
  }
14650
- function searchLayer(state, qVec, qMag, ef, layer, metric, skipTombstones, distFn, results) {
14725
+ function searchLayer(state, qVec, qMag, ef, layer, metric, excludeTombstones, distFn, results) {
14651
14726
  const getDistance = distFn ?? ((ord) => queryDistanceByOrd(state, qVec, qMag, ord, metric));
14652
14727
  const workspace = state.workspace;
14653
14728
  const frontier = workspace.frontier;
@@ -14663,10 +14738,10 @@ function searchLayer(state, qVec, qMag, ef, layer, metric, skipTombstones, distF
14663
14738
  if (state.visited[epOrd] === stamp) continue;
14664
14739
  state.visited[epOrd] = stamp;
14665
14740
  if (!nodeExists(state, epOrd)) continue;
14666
- if (skipTombstones && isTombstoned(state, epOrd)) continue;
14667
14741
  const dist = getDistance(epOrd);
14668
14742
  if (dist === Number.POSITIVE_INFINITY) continue;
14669
14743
  pushHeap(frontier, epOrd, dist);
14744
+ if (excludeTombstones && isTombstoned(state, epOrd)) continue;
14670
14745
  pushHeap(found, epOrd, dist);
14671
14746
  if (found.size > ef) {
14672
14747
  popHeap(found);
@@ -14674,19 +14749,19 @@ function searchLayer(state, qVec, qMag, ef, layer, metric, skipTombstones, distF
14674
14749
  }
14675
14750
  if (found.size > 0) furthestDist = found.distances[0];
14676
14751
  while (popHeap(frontier)) {
14677
- if (frontier.topDistance > furthestDist) break;
14752
+ if (frontier.topDistance > furthestDist && found.size >= ef) break;
14678
14753
  const count = readNeighborsLocked(state, frontier.topOrd, layer);
14679
14754
  for (let i = 0; i < count; i++) {
14680
14755
  const neighborOrd = scratch[i];
14681
14756
  ensureVisited(state, neighborOrd + 1);
14682
14757
  if (state.visited[neighborOrd] === stamp) continue;
14683
14758
  state.visited[neighborOrd] = stamp;
14684
- if (skipTombstones && isTombstoned(state, neighborOrd)) continue;
14685
14759
  if (!nodeExists(state, neighborOrd)) continue;
14686
14760
  const dist = getDistance(neighborOrd);
14687
14761
  if (dist === Number.POSITIVE_INFINITY) continue;
14688
14762
  if (dist < furthestDist || found.size < ef) {
14689
14763
  pushHeap(frontier, neighborOrd, dist);
14764
+ if (excludeTombstones && isTombstoned(state, neighborOrd)) continue;
14690
14765
  pushHeap(found, neighborOrd, dist);
14691
14766
  if (found.size > ef) {
14692
14767
  popHeap(found);
@@ -14962,13 +15037,6 @@ function markTombstone(state, ord) {
14962
15037
  if (Atomics.compareExchange(state.tombstones, ord, 0, 1) === 0) {
14963
15038
  Atomics.add(state.header, GRAPH_TOMBSTONE_COUNT, 1);
14964
15039
  }
14965
- if (entryPointOf(state) !== ord) return;
14966
- lockEntry(state.locks);
14967
- try {
14968
- if (entryPointOf(state) === ord) setEntryPoint(state, highestNode(state, false));
14969
- } finally {
14970
- unlockEntry(state.locks);
14971
- }
14972
15040
  }
14973
15041
  function compactTombstones(state) {
14974
15042
  if (Atomics.load(state.header, GRAPH_TOMBSTONE_COUNT) === 0) return;
@@ -15042,7 +15110,7 @@ function valuesOf(bytes) {
15042
15110
  return new Uint32Array(copy.buffer, 0, copy.byteLength / BYTES_PER_VALUE);
15043
15111
  }
15044
15112
  function savedTopLayer(state, ordinal) {
15045
- if (ordinal === NO_ORDINAL || isTombstoned(state, ordinal)) return NO_LAYER;
15113
+ if (ordinal === NO_ORDINAL) return NO_LAYER;
15046
15114
  return Math.min(nodeMaxLayer(state, ordinal), MAX_LAYER_CAP);
15047
15115
  }
15048
15116
  function neighbourValueCount(state, topLayers, start, end) {
@@ -15108,6 +15176,25 @@ function serializeNumberedGraphParts(state, numberOfOrdinal, ordinalOfNumber, pa
15108
15176
  }
15109
15177
  return parts;
15110
15178
  }
15179
+ function graphWithoutNodes(part, firstNumber, numbers) {
15180
+ const values = valuesOf(part.neighbours);
15181
+ const levels = part.levels.slice();
15182
+ const kept = new Uint32Array(values.length);
15183
+ let cursor = 0;
15184
+ let length = 0;
15185
+ for (let at = 0; at < part.levels.length; at++) {
15186
+ const start = cursor;
15187
+ for (let layer = 0; layer < part.levels[at]; layer++) cursor += values[cursor] + 1;
15188
+ if (numbers.has(firstNumber + at)) {
15189
+ levels[at] = 0;
15190
+ continue;
15191
+ }
15192
+ kept.set(values.subarray(start, cursor), length);
15193
+ length += cursor - start;
15194
+ }
15195
+ const entryPoint = part.entryPoint !== null && numbers.has(part.entryPoint) ? null : part.entryPoint;
15196
+ return { ...part, entryPoint, levels, neighbours: littleEndianBytesOf(kept.subarray(0, length)) };
15197
+ }
15111
15198
  function endsInsideAList() {
15112
15199
  throw new NarsilError(
15113
15200
  ErrorCodes.PERSISTENCE_LOAD_FAILED,
@@ -15374,7 +15461,7 @@ function traverse(state, query, k, searchMetric, options) {
15374
15461
  if (entryPoint === -1) return { candidates, depth: useQuantized ? depth : 0, arenaQuery, qMag, native: null };
15375
15462
  setSingleEntryPoint(workspace, entryPoint);
15376
15463
  for (let layer = topLayerOf(state); layer >= 1; layer--) {
15377
- searchLayer(state, query, qMag, 1, layer, searchMetric, true, distFn, candidates);
15464
+ searchLayer(state, query, qMag, 1, layer, searchMetric, false, distFn, candidates);
15378
15465
  if (candidates.size > 0) {
15379
15466
  setSingleEntryPoint(workspace, candidates.ords[0]);
15380
15467
  }
@@ -15682,6 +15769,8 @@ function createHNSWIndex(dimension, store, config, quantizer, handles) {
15682
15769
  if (ord !== void 0) markTombstone(state, ord);
15683
15770
  },
15684
15771
  markTombstoneOrdinal: (ordinal) => markTombstone(state, ordinal),
15772
+ holdsNode: (ordinal) => hasNode(state.adjacency, ordinal),
15773
+ holdsTombstone: (ordinal) => hasNode(state.adjacency, ordinal) && isTombstoned(state, ordinal),
15685
15774
  has: (docId) => {
15686
15775
  const ord = store.getOrdinal(docId);
15687
15776
  return ord !== void 0 && hasNode(state.adjacency, ord) && !isTombstoned(state, ord);
@@ -15751,6 +15840,9 @@ function emptyFieldBeforeRestore(state) {
15751
15840
  state.savedFiles = [];
15752
15841
  state.savedSignature = null;
15753
15842
  }
15843
+ function releaseReplacedVectors(state) {
15844
+ for (const ordinal of state.store.releaseReplacedVectors()) state.osq?.removeOrdinal(ordinal);
15845
+ }
15754
15846
  function adoptGraph(state, graph) {
15755
15847
  if (graph === null && state.hnsw !== null) {
15756
15848
  state.hnsw.clear();
@@ -16310,9 +16402,7 @@ function captureCloneCodes(state) {
16310
16402
  const records = new Uint8Array(slots * layout.slotStride);
16311
16403
  const present = new Uint8Array(slots);
16312
16404
  for (let ordinal = 0; ordinal < slots; ordinal++) {
16313
- const docId = state.store.docIdForOrdinal(ordinal);
16314
- if (docId === void 0) continue;
16315
- const record = quantizer.recordOf(docId);
16405
+ const record = quantizer.recordAtOrdinal(ordinal);
16316
16406
  if (record === void 0) continue;
16317
16407
  records.set(record, ordinal * layout.slotStride);
16318
16408
  present[ordinal] = 1;
@@ -16449,7 +16539,7 @@ async function dispatcherFor(state, graph) {
16449
16539
  workerCount: Math.max(1, host.workerCount)
16450
16540
  };
16451
16541
  }
16452
- if (liveSize(state) < WORKER_COPY_MIN_VECTORS) return null;
16542
+ if (liveSize(state) < WORKER_COPY_MIN_VECTORS || !sharedMemoryAvailable()) return null;
16453
16543
  const handle = await shareGraph(state, graph, graph === state.hnsw);
16454
16544
  const pool = state.workerCopyPool;
16455
16545
  if (handle === null || pool === null || state.sharedHandles.get(graph)?.mode !== "shared") return null;
@@ -16539,12 +16629,40 @@ async function insertLocally(state, graph, docIds, admit) {
16539
16629
  state.buffer.delete(docId);
16540
16630
  count += 1;
16541
16631
  if (count % BUILD_CHUNK_SIZE === 0) {
16632
+ noteWrite(state);
16542
16633
  await yieldToEventLoop2();
16543
16634
  }
16544
16635
  }
16636
+ if (count > 0) noteWrite(state);
16545
16637
  return !state.disposed;
16546
16638
  }
16547
16639
 
16640
+ // src/vector/vector-index/dead-bits.ts
16641
+ var NO_LIVE_VECTOR_AT_POSITION = -1;
16642
+ function deadBytesFor(count) {
16643
+ return Math.ceil(count / 8);
16644
+ }
16645
+ function isDead(dead, position) {
16646
+ return dead !== null && (dead[position >> 3] >> (position & 7) & 1) === 1;
16647
+ }
16648
+ function countDead(dead, count) {
16649
+ if (dead === null) return 0;
16650
+ let found = 0;
16651
+ for (let position = 0; position < count; position++) {
16652
+ if (isDead(dead, position)) found += 1;
16653
+ }
16654
+ return found;
16655
+ }
16656
+ function deadBitsWhere(count, isDeadAt) {
16657
+ let dead = null;
16658
+ for (let position = 0; position < count; position++) {
16659
+ if (!isDeadAt(position)) continue;
16660
+ dead ??= new Uint8Array(deadBytesFor(count));
16661
+ dead[position >> 3] |= 1 << (position & 7);
16662
+ }
16663
+ return dead;
16664
+ }
16665
+
16548
16666
  // src/vector/vector-index/disk.ts
16549
16667
  function readsFromDisk(state) {
16550
16668
  return state.storage === "disk";
@@ -16555,6 +16673,7 @@ function holdsVectorsOnDisk(state) {
16555
16673
  function* locationsOf(state, layout) {
16556
16674
  const stride = state.dimension * 4;
16557
16675
  for (let i = 0; i < layout.docIds.length; i++) {
16676
+ if (isDead(layout.dead, i)) continue;
16558
16677
  yield [layout.docIds[i], { path: layout.path, offset: layout.vectorsOffset + i * stride }];
16559
16678
  }
16560
16679
  }
@@ -16945,32 +17064,6 @@ function graphPartsOfEarlierPayload(payload) {
16945
17064
  };
16946
17065
  }
16947
17066
 
16948
- // src/vector/vector-index/dead-bits.ts
16949
- var NO_LIVE_VECTOR_AT_POSITION = -1;
16950
- function deadBytesFor(count) {
16951
- return Math.ceil(count / 8);
16952
- }
16953
- function isDead(dead, position) {
16954
- return dead !== null && (dead[position >> 3] >> (position & 7) & 1) === 1;
16955
- }
16956
- function countDead(dead, count) {
16957
- if (dead === null) return 0;
16958
- let found = 0;
16959
- for (let position = 0; position < count; position++) {
16960
- if (isDead(dead, position)) found += 1;
16961
- }
16962
- return found;
16963
- }
16964
- function deadBitsWhere(count, isDeadAt) {
16965
- let dead = null;
16966
- for (let position = 0; position < count; position++) {
16967
- if (!isDeadAt(position)) continue;
16968
- dead ??= new Uint8Array(deadBytesFor(count));
16969
- dead[position >> 3] |= 1 << (position & 7);
16970
- }
16971
- return dead;
16972
- }
16973
-
16974
17067
  // src/vector/vector-index/persistence.ts
16975
17068
  function recordFor(state, quantizer, docId) {
16976
17069
  const record = quantizer.recordOf(docId);
@@ -16992,7 +17085,7 @@ function sameCentroid(state, planned) {
16992
17085
  }
16993
17086
  return true;
16994
17087
  }
16995
- function partCodes(state, docIds, centroid) {
17088
+ function partCodes(state, docIds, centroid, tombstonedAt) {
16996
17089
  const quantizer = state.osq;
16997
17090
  const layout = state.store.handles.codeLayout;
16998
17091
  if (centroid === null || quantizer === null || layout === null) return null;
@@ -17005,7 +17098,8 @@ function partCodes(state, docIds, centroid) {
17005
17098
  }
17006
17099
  const records = new Uint8Array(docIds.length * layout.slotStride);
17007
17100
  for (let i = 0; i < docIds.length; i++) {
17008
- const record = recordFor(state, quantizer, docIds[i]);
17101
+ const tombstoned = tombstonedAt.get(i);
17102
+ const record = tombstoned === void 0 ? recordFor(state, quantizer, docIds[i]) : quantizer.recordAtOrdinal(tombstoned);
17009
17103
  if (record !== void 0) records.set(record, i * layout.slotStride);
17010
17104
  }
17011
17105
  return { bits: quantizer.bits, centroid, records };
@@ -17162,33 +17256,39 @@ function isLive(state, ordinal) {
17162
17256
  const docId = state.store.docIdForOrdinal(ordinal);
17163
17257
  return docId !== void 0 && !state.tombstones.has(docId);
17164
17258
  }
17165
- function liveCountOf(state, file) {
17166
- let live = 0;
17167
- for (let i = 0; i < file.ordinals.length; i++) if (isLive(state, file.ordinals[i])) live += 1;
17168
- return live;
17259
+ function isGraphTombstone(state, ordinal) {
17260
+ return ordinal !== NO_LIVE_VECTOR_AT_POSITION && state.hnsw !== null && state.hnsw.holdsTombstone(ordinal);
17261
+ }
17262
+ function isCarried(state, ordinal) {
17263
+ return isLive(state, ordinal) || isGraphTombstone(state, ordinal);
17264
+ }
17265
+ function carriedCountOf(state, file) {
17266
+ let carried = 0;
17267
+ for (let i = 0; i < file.ordinals.length; i++) if (isCarried(state, file.ordinals[i])) carried += 1;
17268
+ return carried;
17169
17269
  }
17170
17270
  function deadBitsOf(state, file) {
17171
17271
  return deadBitsWhere(file.ordinals.length, (position) => !isLive(state, file.ordinals[position]));
17172
17272
  }
17173
- function filesWorthKeeping(state, listed, calibration, liveCounts) {
17273
+ function filesWorthKeeping(state, listed, calibration, carriedCounts) {
17174
17274
  const kept = [];
17175
17275
  for (const file of listed) {
17176
- const live = liveCountOf(state, file);
17177
- const deadShare = file.ordinals.length === 0 ? 1 : 1 - live / file.ordinals.length;
17178
- if (file.calibration !== calibration || deadShare > VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE) continue;
17276
+ const carried = carriedCountOf(state, file);
17277
+ const deadWithoutNodeShare = file.ordinals.length === 0 ? 1 : 1 - carried / file.ordinals.length;
17278
+ if (file.calibration !== calibration || deadWithoutNodeShare > VECTOR_FILE_REPLACED_ABOVE_DEAD_SHARE) continue;
17179
17279
  kept.push(file);
17180
- liveCounts.push(live);
17280
+ carriedCounts.push(carried);
17181
17281
  }
17182
17282
  return kept;
17183
17283
  }
17184
- function absorbSmallerPartialFiles(kept, liveCounts, vectorsToWrite, capacity) {
17284
+ function absorbSmallerPartialFiles(kept, carriedCounts, vectorsToWrite, capacity) {
17185
17285
  let pending = vectorsToWrite;
17186
17286
  for (let index = kept.length - 1; index >= 0 && pending > 0; index--) {
17187
17287
  if (kept[index].ordinals.length >= capacity) continue;
17188
- if (liveCounts[index] > pending) return;
17189
- pending += liveCounts[index];
17288
+ if (carriedCounts[index] > pending) return;
17289
+ pending += carriedCounts[index];
17190
17290
  kept.splice(index, 1);
17191
- liveCounts.splice(index, 1);
17291
+ carriedCounts.splice(index, 1);
17192
17292
  }
17193
17293
  }
17194
17294
  function ordinalsOutsideOf(state, kept) {
@@ -17202,14 +17302,15 @@ function ordinalsOutsideOf(state, kept) {
17202
17302
  }
17203
17303
  const outside = [];
17204
17304
  for (let ordinal = 0; ordinal < slots; ordinal++) {
17205
- if (saved[ordinal] === 0 && isLive(state, ordinal)) outside.push(ordinal);
17305
+ if (saved[ordinal] === 0 && isCarried(state, ordinal)) outside.push(ordinal);
17206
17306
  }
17207
17307
  return outside;
17208
17308
  }
17209
- function countOutside(state, liveCounts) {
17309
+ function countOutside(state, carriedCounts) {
17210
17310
  let held = 0;
17211
- for (const live of liveCounts) held += live;
17212
- return Math.max(0, state.store.size - state.tombstones.size - held);
17311
+ for (const carried of carriedCounts) held += carried;
17312
+ const tombstonedNodes = state.hnsw?.tombstoneCount ?? 0;
17313
+ return Math.max(0, state.store.size - state.tombstones.size + tombstonedNodes - held);
17213
17314
  }
17214
17315
  function numberEveryVector(state, kept, written) {
17215
17316
  let total = written.length;
@@ -17220,7 +17321,7 @@ function numberEveryVector(state, kept, written) {
17220
17321
  for (const file of kept) {
17221
17322
  for (let i = 0; i < file.ordinals.length; i++, number++) {
17222
17323
  const ordinal = file.ordinals[i];
17223
- if (!isLive(state, ordinal)) continue;
17324
+ if (!isCarried(state, ordinal)) continue;
17224
17325
  numberOfOrdinal[ordinal] = number;
17225
17326
  ordinalOfNumber[number] = ordinal;
17226
17327
  }
@@ -17241,16 +17342,30 @@ function graphPartsOf(state, numbers) {
17241
17342
  );
17242
17343
  return parts.map((graph) => ({ v: VECTOR_GRAPH_PART_PAYLOAD_VERSION, fieldName: state.fieldName, graphs: [graph] }));
17243
17344
  }
17244
- function readFile(state, planned, docIds, centroid) {
17345
+ function graphPartsWithoutNodes(parts, numbers) {
17346
+ return parts.map((part, index) => ({
17347
+ ...part,
17348
+ graphs: part.graphs.map((graph) => graphWithoutNodes(graph, index * VECTOR_GRAPH_PART_VECTORS, numbers))
17349
+ }));
17350
+ }
17351
+ function plannedVectorOf(state, ordinal) {
17352
+ const docId = state.store.docIdForOrdinal(ordinal) ?? state.store.view.docIdAt(ordinal) ?? "";
17353
+ return { ordinal, docId, tombstoned: !isLive(state, ordinal) };
17354
+ }
17355
+ function readFile(state, planned, centroid) {
17245
17356
  const dimension = state.dimension;
17246
17357
  const vectors = new Float32Array(planned.length * dimension);
17247
17358
  const ordinals = new Int32Array(planned.length).fill(NO_LIVE_VECTOR_AT_POSITION);
17359
+ const tombstonedAt = /* @__PURE__ */ new Map();
17360
+ const docIds = planned.map((vector) => vector.docId);
17248
17361
  for (let i = 0; i < planned.length; i++) {
17249
- const held = state.store.holdsOrdinal(planned[i]) ? planned[i] : state.store.getOrdinal(docIds[i]);
17362
+ const { ordinal, docId, tombstoned } = planned[i];
17363
+ const held = tombstoned || state.store.holdsOrdinal(ordinal) ? ordinal : state.store.getOrdinal(docId);
17250
17364
  const entry = held === void 0 ? void 0 : state.store.entryForOrdinal(held);
17251
17365
  if (held === void 0 || entry === void 0) continue;
17252
17366
  vectors.set(entry.vector, i * dimension);
17253
17367
  ordinals[i] = held;
17368
+ if (tombstoned) tombstonedAt.set(i, held);
17254
17369
  }
17255
17370
  return {
17256
17371
  payload: {
@@ -17258,10 +17373,11 @@ function readFile(state, planned, docIds, centroid) {
17258
17373
  fieldName: state.fieldName,
17259
17374
  dimension,
17260
17375
  docIds,
17261
- codes: partCodes(state, docIds, centroid),
17376
+ codes: partCodes(state, docIds, centroid, tombstonedAt),
17262
17377
  vectors: vectorsToBytes(vectors)
17263
17378
  },
17264
- ordinals
17379
+ ordinals,
17380
+ dead: deadBitsWhere(planned.length, (i) => planned[i].tombstoned || ordinals[i] === NO_LIVE_VECTOR_AT_POSITION)
17265
17381
  };
17266
17382
  }
17267
17383
  function planCheckpoint(state, listedKeys, rewritesGraph = false) {
@@ -17269,14 +17385,20 @@ function planCheckpoint(state, listedKeys, rewritesGraph = false) {
17269
17385
  const calibration = signature.calibration;
17270
17386
  const trusted = registryMatches(state, listedKeys);
17271
17387
  const capacity = fileCapacityOf(state);
17272
- const liveCounts = [];
17273
- const keptFiles = filesWorthKeeping(state, trusted ? state.savedFiles : [], calibration, liveCounts);
17274
- absorbSmallerPartialFiles(keptFiles, liveCounts, countOutside(state, liveCounts), capacity);
17388
+ const carriedCounts = [];
17389
+ const keptFiles = filesWorthKeeping(state, trusted ? state.savedFiles : [], calibration, carriedCounts);
17390
+ absorbSmallerPartialFiles(keptFiles, carriedCounts, countOutside(state, carriedCounts), capacity);
17275
17391
  const written = ordinalsOutsideOf(state, keptFiles);
17276
- const writtenDocIds = written.map((ordinal) => state.store.docIdForOrdinal(ordinal) ?? "");
17392
+ const planned = written.map((ordinal) => plannedVectorOf(state, ordinal));
17277
17393
  const centroid = codedCentroid(state);
17278
17394
  const listedBefore = trusted && listedKeys !== null;
17279
17395
  const unchanged = !rewritesGraph && listedBefore && sameSignature(state.savedSignature, signature) && written.length === 0;
17396
+ let firstWrittenNumber = 0;
17397
+ for (const file of keptFiles) firstWrittenNumber += file.ordinals.length;
17398
+ const graphParts = unchanged ? null : graphPartsOf(state, numberEveryVector(state, keptFiles, written));
17399
+ const vanished = /* @__PURE__ */ new Set();
17400
+ let vanishedWhenStripped = 0;
17401
+ let graphPartsToWrite = graphParts;
17280
17402
  return {
17281
17403
  unchanged,
17282
17404
  keptFiles,
@@ -17287,12 +17409,22 @@ function planCheckpoint(state, listedKeys, rewritesGraph = false) {
17287
17409
  dead: deadBitsOf(state, file)
17288
17410
  })),
17289
17411
  newFiles: Math.ceil(written.length / capacity),
17290
- graphParts: unchanged ? null : graphPartsOf(state, numberEveryVector(state, keptFiles, written)),
17412
+ get graphParts() {
17413
+ if (graphParts !== null && vanished.size !== vanishedWhenStripped) {
17414
+ graphPartsToWrite = graphPartsWithoutNodes(graphParts, vanished);
17415
+ vanishedWhenStripped = vanished.size;
17416
+ }
17417
+ return graphPartsToWrite;
17418
+ },
17291
17419
  signature,
17292
17420
  readNewFile(index) {
17293
17421
  const start = index * capacity;
17294
17422
  const end = start + capacity;
17295
- return readFile(state, written.slice(start, end), writtenDocIds.slice(start, end), centroid);
17423
+ const file = readFile(state, planned.slice(start, end), centroid);
17424
+ for (let i = 0; i < file.ordinals.length; i++) {
17425
+ if (file.ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) vanished.add(firstWrittenNumber + start + i);
17426
+ }
17427
+ return file;
17296
17428
  }
17297
17429
  };
17298
17430
  }
@@ -17325,9 +17457,10 @@ function insertNodesOfLaterGraph(state, graph, later, ordinals) {
17325
17457
  if (docId !== void 0 && !graph.has(docId)) graph.insertOrdinal(ordinal);
17326
17458
  }
17327
17459
  }
17328
- function restoreGraph(state, payload, ordinalOfNumber) {
17460
+ function restoreGraph(state, payload, ordinalOfNumber, tombstoned) {
17329
17461
  const first = payload?.graphs[0]?.parts[0];
17330
17462
  if (payload === null || first === void 0) {
17463
+ releaseReplacedVectors(state);
17331
17464
  bufferEveryVectorOutsideTheGraph(state);
17332
17465
  return;
17333
17466
  }
@@ -17338,11 +17471,13 @@ function restoreGraph(state, payload, ordinalOfNumber) {
17338
17471
  state.osq ?? void 0
17339
17472
  );
17340
17473
  graph.deserializeNumbered(payload.graphs[0], ordinalOfNumber);
17474
+ for (const ordinal of tombstoned) graph.markTombstoneOrdinal(ordinal);
17341
17475
  adoptGraph(state, graph);
17342
17476
  for (const later of payload.graphs.slice(1)) insertNodesOfLaterGraph(state, graph, later, ordinalOfNumber);
17343
17477
  bufferEveryVectorOutsideTheGraph(state);
17344
17478
  if (state.buffer.size <= graph.size) return;
17345
17479
  adoptGraph(state, null);
17480
+ releaseReplacedVectors(state);
17346
17481
  state.buffer.clear();
17347
17482
  bufferEveryVectorOutsideTheGraph(state);
17348
17483
  }
@@ -17351,6 +17486,9 @@ function beginCheckpointRestore(state, shape) {
17351
17486
  const cold = readsFromDisk(state) && (shape.holdsGraph || shape.liveVectors >= state.promotionThreshold);
17352
17487
  const restoresCodes = state.osq !== null && osqBitsOf(state.quantizationMode) !== null && shape.holdsGraph;
17353
17488
  const files = [];
17489
+ const graphNodes = shape.graph?.graphs[0] ?? null;
17490
+ const tombstoned = /* @__PURE__ */ new Set();
17491
+ let nextNumber = 0;
17354
17492
  let sharedCodes = null;
17355
17493
  let codesAgree = true;
17356
17494
  function restoreRecords(payload, ordinals) {
@@ -17372,9 +17510,14 @@ function beginCheckpointRestore(state, shape) {
17372
17510
  const stride = layout.slotStride;
17373
17511
  for (let i = 0; i < ordinals.length; i++) {
17374
17512
  if (ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) continue;
17375
- quantizer.restoreRecord(payload.docIds[i], codes.records.subarray(i * stride, (i + 1) * stride));
17513
+ const record = codes.records.subarray(i * stride, (i + 1) * stride);
17514
+ if (tombstoned.has(ordinals[i])) quantizer.restoreRecordAtOrdinal(ordinals[i], record);
17515
+ else quantizer.restoreRecord(payload.docIds[i], record);
17376
17516
  }
17377
17517
  }
17518
+ function graphHoldsNodeFor(number) {
17519
+ return graphNodes !== null && topLayerInParts(graphNodes, number) >= 0;
17520
+ }
17378
17521
  function addFile(file) {
17379
17522
  const { payload, location } = file;
17380
17523
  if (payload.dimension !== state.dimension) {
@@ -17389,13 +17532,21 @@ function beginCheckpointRestore(state, shape) {
17389
17532
  const vectors = bytesToVectors(payload.vectors);
17390
17533
  const fileIndex = onDisk && cold ? state.store.addVectorFile(location.path) : null;
17391
17534
  const ordinals = new Int32Array(payload.docIds.length).fill(NO_LIVE_VECTOR_AT_POSITION);
17535
+ const firstNumber = nextNumber;
17536
+ nextNumber += payload.docIds.length;
17392
17537
  for (let i = 0; i < payload.docIds.length; i++) {
17393
- if (isDead(file.dead, i)) continue;
17538
+ const dead = isDead(file.dead, i);
17539
+ if (dead && !graphHoldsNodeFor(firstNumber + i)) continue;
17394
17540
  const docId = payload.docIds[i];
17395
- const replaced = state.store.getOrdinal(docId);
17396
- if (replaced !== void 0) state.osq?.removeOrdinal(replaced);
17397
17541
  const vector = vectors.subarray(i * dimension, (i + 1) * dimension);
17398
17542
  const offset = onDisk ? location.vectorsOffset + i * dimension * 4 : 0;
17543
+ if (dead) {
17544
+ ordinals[i] = state.store.insertReplaced(docId, vector, fileIndex === null ? void 0 : { fileIndex, offset });
17545
+ tombstoned.add(ordinals[i]);
17546
+ continue;
17547
+ }
17548
+ const replaced = state.store.getOrdinal(docId);
17549
+ if (replaced !== void 0) state.osq?.removeOrdinal(replaced);
17399
17550
  if (fileIndex !== null) {
17400
17551
  ordinals[i] = state.store.insertCold(docId, magnitude(vector), { fileIndex, offset });
17401
17552
  continue;
@@ -17406,21 +17557,27 @@ function beginCheckpointRestore(state, shape) {
17406
17557
  restoreRecords(payload, ordinals);
17407
17558
  files.push({ id: file.id, key: file.key, ordinals, coded: payload.codes !== null });
17408
17559
  }
17409
- function finish(graph) {
17560
+ function finish() {
17410
17561
  const codesRestored = restoresCodes && codesAgree && sharedCodes !== null;
17411
- if (restoresCodes && !codesRestored) recalibrateFromStore(state);
17562
+ if (restoresCodes && !codesRestored) {
17563
+ recalibrateFromStore(state);
17564
+ for (const ordinal of tombstoned) state.osq?.writeCodes(ordinal);
17565
+ }
17412
17566
  let total = 0;
17413
17567
  for (const file of files) total += file.ordinals.length;
17414
17568
  const ordinalOfNumber = new Int32Array(total);
17415
17569
  let number = 0;
17416
17570
  for (const file of files) {
17417
17571
  for (let i = 0; i < file.ordinals.length; i++) {
17418
- if (!state.store.holdsOrdinal(file.ordinals[i])) file.ordinals[i] = NO_LIVE_VECTOR_AT_POSITION;
17572
+ const ordinal = file.ordinals[i];
17573
+ if (!state.store.holdsOrdinal(ordinal) && !tombstoned.has(ordinal)) {
17574
+ file.ordinals[i] = NO_LIVE_VECTOR_AT_POSITION;
17575
+ }
17419
17576
  }
17420
17577
  ordinalOfNumber.set(file.ordinals, number);
17421
17578
  number += file.ordinals.length;
17422
17579
  }
17423
- restoreGraph(state, graph, ordinalOfNumber);
17580
+ restoreGraph(state, shape.graph, ordinalOfNumber, tombstoned);
17424
17581
  const current = signatureOf(state).calibration;
17425
17582
  const codedUnder = codesRestored && current !== null ? current : CALIBRATION_NEVER_CURRENT;
17426
17583
  const saved = files.map((file) => ({
@@ -17437,11 +17594,12 @@ function beginCheckpointRestore(state, shape) {
17437
17594
 
17438
17595
  // src/vector/vector-index/maintenance.ts
17439
17596
  function compact(state) {
17440
- if (state.tombstones.size === 0) return;
17597
+ if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
17441
17598
  if (state.hnsw) {
17442
17599
  state.compactedNodeCount += state.hnsw.tombstoneCount;
17443
17600
  state.hnsw.compactTombstones();
17444
17601
  }
17602
+ state.freshGraph?.compactTombstones();
17445
17603
  for (const docId of state.tombstones) {
17446
17604
  state.store.remove(docId);
17447
17605
  state.buffer.delete(docId);
@@ -17449,6 +17607,7 @@ function compact(state) {
17449
17607
  state.osq.remove(docId);
17450
17608
  }
17451
17609
  }
17610
+ releaseReplacedVectors(state);
17452
17611
  state.tombstones.clear();
17453
17612
  }
17454
17613
  function recalibrateWhileNoThreadSearches(state, graph) {
@@ -17480,6 +17639,7 @@ async function foldIntoGraph(state) {
17480
17639
  const graph = state.hnsw;
17481
17640
  if (graph === null || rebuildNeeded) {
17482
17641
  if (state.osq?.isCalibrated()) recalibrateWhileNoThreadSearches(state, graph);
17642
+ else calibrateQuantizer(state);
17483
17643
  await buildGraphFromStore(state);
17484
17644
  } else {
17485
17645
  await insertMissing(state, graph);
@@ -17526,8 +17686,9 @@ function maintenanceStatus(state) {
17526
17686
  const storeSize = state.store.size;
17527
17687
  const graphCount = state.hnsw ? 1 : 0;
17528
17688
  const graphNodes = state.hnsw === null ? storeSize : state.hnsw.size + state.hnsw.tombstoneCount;
17529
- const tombstoneRatio = graphNodes > 0 ? state.tombstones.size / graphNodes : 0;
17530
- const estimatedCompactMs = Math.round(state.tombstones.size * ESTIMATED_MS_PER_TOMBSTONE * state.dimensionScale);
17689
+ const tombstoned = state.tombstones.size + state.store.replacedVectorCount;
17690
+ const tombstoneRatio = graphNodes > 0 ? tombstoned / graphNodes : 0;
17691
+ const estimatedCompactMs = Math.round(tombstoned * ESTIMATED_MS_PER_TOMBSTONE * state.dimensionScale);
17531
17692
  const optimizeVectorCount = state.hnsw === null || graphNeedsRebuild(state) ? storeSize : state.buffer.size;
17532
17693
  const estimatedOptimizeMs = Math.round(optimizeVectorCount * ESTIMATED_MS_PER_VECTOR_REBUILD * state.dimensionScale);
17533
17694
  return {
@@ -17787,19 +17948,19 @@ function searchWithFilter(state, query, k, options, filter) {
17787
17948
  return scannedOutcome(scan2);
17788
17949
  }
17789
17950
  }
17790
- const graphOptions = { filter, efSearch, oversample };
17791
- if (state.buffer.size === 0) {
17792
- const hnswResults2 = state.hnsw.search(query, k, metric, minSimilarity, graphOptions);
17793
- return fetchedOutcome(
17794
- state,
17795
- hnswResults2.map((r) => ({ docId: r.docId, score: r.score })),
17796
- minSimilarity,
17797
- filter
17798
- );
17799
- }
17800
- const hnswResults = state.hnsw.search(query, k, metric, minSimilarity, graphOptions).map((r) => ({ docId: r.docId, score: r.score }));
17801
- const buffered = bruteForceSearch(state, query, k, metric, minSimilarity, bufferCandidates(state, filter));
17802
- return fetchedOutcome(state, mergeResults(hnswResults, buffered.results, k), minSimilarity, filter);
17951
+ const hnswResults = state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample }).map((r) => ({ docId: r.docId, score: r.score }));
17952
+ return fetchedOutcome(
17953
+ state,
17954
+ withBufferedVectors(state, query, k, options, filter, hnswResults),
17955
+ minSimilarity,
17956
+ filter
17957
+ );
17958
+ }
17959
+ function withBufferedVectors(state, query, k, options, filter, graphResults) {
17960
+ if (state.buffer.size === 0) return graphResults;
17961
+ const candidates = bufferCandidates(state, filter);
17962
+ const buffered = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates);
17963
+ return mergeResults(graphResults, buffered.results, k);
17803
17964
  }
17804
17965
 
17805
17966
  // src/vector/vector-index/index.ts
@@ -17900,11 +18061,12 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
17900
18061
  }
17901
18062
  noteWrite(state);
17902
18063
  state.tombstones.delete(docId);
17903
- state.store.insert(docId, vector, partitionId);
18064
+ const graphHoldsPrevious = previous !== void 0 && ((state.hnsw?.holdsNode(previous) ?? false) || (state.freshGraph?.holdsNode(previous) ?? false));
18065
+ state.store.insert(docId, vector, partitionId, graphHoldsPrevious);
17904
18066
  if (previous !== void 0) {
17905
18067
  state.hnsw?.markTombstoneOrdinal(previous);
17906
18068
  state.freshGraph?.markTombstoneOrdinal(previous);
17907
- state.osq?.removeOrdinal(previous);
18069
+ if (!graphHoldsPrevious) state.osq?.removeOrdinal(previous);
17908
18070
  }
17909
18071
  state.buffer.add(docId);
17910
18072
  state.pendingLocations.delete(docId);
@@ -17974,10 +18136,13 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
17974
18136
  efSearch: options.efSearch,
17975
18137
  oversample: options.oversample
17976
18138
  });
17977
- if (viaWorkerCopy !== null) return fetchedOutcome(state, viaWorkerCopy, options.minSimilarity, filter);
17978
18139
  if (confined && state.revision !== filterRevision) {
17979
18140
  filter = filterForOptions(state, options);
17980
18141
  }
18142
+ if (viaWorkerCopy !== null) {
18143
+ const results = withBufferedVectors(state, query, k, options, filter, viaWorkerCopy);
18144
+ return fetchedOutcome(state, results, options.minSimilarity, filter);
18145
+ }
17981
18146
  return searchWithFilter(state, query, k, options, filter);
17982
18147
  }
17983
18148
  return {
@@ -19380,10 +19545,10 @@ function indexByFieldPath(entries) {
19380
19545
  for (const entry of entries) byFieldPath.set(entry.fieldPath, entry);
19381
19546
  return byFieldPath;
19382
19547
  }
19383
- function rowsByFieldPath(inputs, entriesOf, valuesOf2) {
19548
+ function rowsByFieldPath(inputs, entriesOf2, valuesOf2) {
19384
19549
  const byFieldPath = /* @__PURE__ */ new Map();
19385
19550
  for (const input of inputs) {
19386
- for (const entry of entriesOf(input)) {
19551
+ for (const entry of entriesOf2(input)) {
19387
19552
  let rows = byFieldPath.get(entry.fieldPath);
19388
19553
  if (rows === void 0) {
19389
19554
  rows = [];
@@ -23782,7 +23947,8 @@ async function readVectorFile(store, file, fieldPath) {
23782
23947
  async function loadVectorField(store, ref, vectorIndex) {
23783
23948
  const restore = vectorIndex.restoreCheckpoint({
23784
23949
  liveVectors: liveVectorsOf(ref),
23785
- holdsGraph: ref.graphParts.length > 0
23950
+ holdsGraph: ref.graphParts.length > 0,
23951
+ graph: await readGraphParts(store, ref)
23786
23952
  });
23787
23953
  for (const file of ref.files) {
23788
23954
  const { payload, payloadLength } = await readVectorFile(store, file, ref.fieldPath);
@@ -23797,7 +23963,7 @@ async function loadVectorField(store, ref, vectorIndex) {
23797
23963
  }
23798
23964
  });
23799
23965
  }
23800
- restore.finish(await readGraphParts(store, ref));
23966
+ restore.finish();
23801
23967
  }
23802
23968
  var EMPTY_BIN = new Uint8Array(0);
23803
23969
  var EMPTY_BIN_BYTES = 2;
@@ -23861,7 +24027,7 @@ async function writeField(input, fieldPath, plan, outcome) {
23861
24027
  const files = plan.kept.map((kept) => keptFileRef(input, prior, kept));
23862
24028
  const written = [];
23863
24029
  for (let index = 0; index < plan.newFiles; index++) {
23864
- const { payload, ordinals } = plan.readNewFile(index);
24030
+ const { payload, ordinals, dead } = plan.readNewFile(index);
23865
24031
  const id = nextFileId;
23866
24032
  nextFileId += 1;
23867
24033
  const key = vectorFileKey(input.indexName, fieldPath, id);
@@ -23871,13 +24037,13 @@ async function writeField(input, fieldPath, plan, outcome) {
23871
24037
  `vector file "${key}"`
23872
24038
  );
23873
24039
  await input.store.write(key, packed.chunks);
23874
- const dead = deadBitsWhere(ordinals.length, (position) => ordinals[position] === NO_LIVE_VECTOR_AT_POSITION);
23875
24040
  files.push({ id, key, count: payload.docIds.length, dead, bytes: packed.byteLength, checksum: packed.checksum });
23876
24041
  written.push({ id, key, ordinals });
23877
24042
  outcome.layouts.push({
23878
24043
  fieldPath,
23879
24044
  key,
23880
24045
  docIds: payload.docIds,
24046
+ dead,
23881
24047
  vectorsOffset: packed.byteLength - payload.vectors.byteLength
23882
24048
  });
23883
24049
  }
@@ -25748,7 +25914,12 @@ async function adoptVectorLayouts(store, vectorIndexes, layouts) {
25748
25914
  const vecIndex = vectorIndexes.get(layout.fieldPath);
25749
25915
  if (vecIndex === void 0 || vecIndex.storage !== "disk") continue;
25750
25916
  const path = await pathOf.call(store, layout.key);
25751
- await vecIndex.adoptDiskLayout({ path, docIds: layout.docIds, vectorsOffset: layout.vectorsOffset });
25917
+ await vecIndex.adoptDiskLayout({
25918
+ path,
25919
+ docIds: layout.docIds,
25920
+ dead: layout.dead,
25921
+ vectorsOffset: layout.vectorsOffset
25922
+ });
25752
25923
  }
25753
25924
  }
25754
25925
  async function garbageThatNoFieldStillReads(store, vectorIndexes, keys) {
@@ -26703,7 +26874,11 @@ async function readGraph(store, ref) {
26703
26874
  async function loadVectorField2(store, ref, vectorIndex, fileRefs) {
26704
26875
  let liveVectors = 0;
26705
26876
  for (const file of ref.files) liveVectors += file.count - countDead(file.dead, file.count);
26706
- const restore = vectorIndex.restoreCheckpoint({ liveVectors, holdsGraph: ref.graphKey !== null });
26877
+ const restore = vectorIndex.restoreCheckpoint({
26878
+ liveVectors,
26879
+ holdsGraph: ref.graphKey !== null,
26880
+ graph: await readGraph(store, ref)
26881
+ });
26707
26882
  for (const file of ref.files) {
26708
26883
  const bytes = await store.read(file.key);
26709
26884
  if (bytes === null) {
@@ -26736,7 +26911,7 @@ async function loadVectorField2(store, ref, vectorIndex, fileRefs) {
26736
26911
  }
26737
26912
  });
26738
26913
  }
26739
- restore.finish(await readGraph(store, ref));
26914
+ restore.finish();
26740
26915
  }
26741
26916
  async function loadEarlierLayout(store, indexName, manifest, deps) {
26742
26917
  let highestPartitionId = manifest.partitions.length - 1;
@@ -34397,7 +34572,7 @@ function partitionVectorPlan(vectorIndex, docIds) {
34397
34572
  codes: null,
34398
34573
  vectors: vectorsToBytes(packed)
34399
34574
  };
34400
- files.push({ payload, ordinals: new Int32Array(slice.length) });
34575
+ files.push({ payload, ordinals: new Int32Array(slice.length), dead: null });
34401
34576
  }
34402
34577
  return {
34403
34578
  unchanged: false,