@delali/narsil 0.3.1-next.99 → 0.4.1-next.211
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/NOTICE +45 -0
- package/README.md +30 -11
- package/dist/adapters/filesystem.d.ts +1 -1
- package/dist/adapters/filesystem.mjs +101 -13
- package/dist/adapters/gcs.d.ts +49 -0
- package/dist/adapters/gcs.mjs +213 -0
- package/dist/adapters/indexeddb.d.ts +1 -1
- package/dist/adapters/indexeddb.mjs +2 -7
- package/dist/adapters/memory.d.ts +1 -1
- package/dist/adapters/memory.mjs +5 -0
- package/dist/adapters/s3.d.ts +53 -0
- package/dist/adapters/s3.mjs +263 -0
- package/dist/{adapters-BQFQ4PRz.d.ts → adapters-pljAqmrC.d.ts} +33 -0
- package/dist/browser/chunk-4E5APHTM.mjs +1459 -0
- package/dist/browser/chunk-NDYADQ4Y.mjs +23426 -0
- package/dist/browser/chunk-QAOVAYAG.mjs +564 -0
- package/dist/browser/chunk-WC2DEXWU.mjs +29 -0
- package/dist/browser/chunk-YINZFPLT.mjs +1239 -0
- package/dist/browser/chunk-YJHGOJAC.mjs +15 -0
- package/dist/browser/{chunk-Z7QH2XHB.mjs → core-leaf-HWAXCEWX.mjs} +3987 -2103
- package/dist/browser/custom-token-results-R5BIX7SL.mjs +59 -0
- package/dist/browser/digest-leaf-64IPRPZD.mjs +151 -0
- package/dist/browser/durability-config-75NSFCQN.mjs +97 -0
- package/dist/browser/embed-GG6PS3EA.mjs +264 -0
- package/dist/browser/geo-index-GNHY4PUB.mjs +1484 -0
- package/dist/browser/gzip-JF3S6PPT.mjs +31 -0
- package/dist/browser/module-ZZTNG5JZ.mjs +5762 -0
- package/dist/browser/msgpack-leaf-44XNWSSY.mjs +302 -0
- package/dist/browser/optional-reads-XMVPG6HG.mjs +3230 -0
- package/dist/browser/optional-writes-76DWMRQH.mjs +3603 -0
- package/dist/browser/phrase-module-6UGFVW5J.mjs +617 -0
- package/dist/browser/resolve-vector-text-XRR2OVHE.mjs +85 -0
- package/dist/browser/restore-queue-SG5O735W.mjs +196 -0
- package/dist/browser/{runtime-NYE5TB6O.mjs → runtime-OMKE7DF4.mjs} +83 -25
- package/dist/browser/segment-codec-leaf-A427QJUX.mjs +554 -0
- package/dist/browser/segment-merge-leaf-7BZ54JPI.mjs +1636 -0
- package/dist/browser/storage-leaf-5UTR5K7J.mjs +19829 -0
- package/dist/browser/store-leaf-3L33V35P.mjs +2070 -0
- package/dist/browser/suggestions-leaf-UBNDVKBD.mjs +1430 -0
- package/dist/browser/worker-copies-JXSGYR5X.mjs +2951 -0
- package/dist/{capabilities-BTTIs96E.d.ts → capabilities-DMte5K9_.d.ts} +162 -18
- package/dist/chunk-2AFZE7Y3.mjs +72 -0
- package/dist/chunk-2CKDCC65.mjs +47 -0
- package/dist/chunk-3ECIRFS5.mjs +61 -0
- package/dist/chunk-44W3CBNG.mjs +7 -0
- package/dist/chunk-4H4RG2FR.mjs +13 -0
- package/dist/chunk-4PY4JYMY.mjs +11 -0
- package/dist/chunk-4ZPVAHWF.mjs +1289 -0
- package/dist/chunk-5EI5CXO6.mjs +197 -0
- package/dist/chunk-5F5MXWS5.mjs +283 -0
- package/dist/{chunk-OHP7QR33.mjs → chunk-5FLRXF73.mjs} +25 -4
- package/dist/chunk-6FOENO4E.mjs +273 -0
- package/dist/chunk-6GHX35ZM.mjs +108 -0
- package/dist/chunk-6MSZ543Y.mjs +32 -0
- package/dist/chunk-6N6HHBCQ.mjs +4596 -0
- package/dist/{chunk-LXPIXDUJ.mjs → chunk-7P5YWDRQ.mjs} +403 -169
- package/dist/{chunk-TGP5M5R7.mjs → chunk-7T4LAYT4.mjs} +1 -1
- package/dist/{chunk-GGSH6KBQ.mjs → chunk-ASZLNVWW.mjs} +23 -3
- package/dist/chunk-AVBPB3MI.mjs +602 -0
- package/dist/{chunk-UCDF35PU.mjs → chunk-AVGUEEQ3.mjs} +494 -245
- package/dist/chunk-BUMREPLU.mjs +15 -0
- package/dist/chunk-BY6NALRK.mjs +31921 -0
- package/dist/chunk-C4ZMXXTL.mjs +92 -0
- package/dist/{chunk-2WYMG3OZ.mjs → chunk-C74KPRR7.mjs} +274 -19
- package/dist/{chunk-RSRVGAIQ.mjs → chunk-D456ZFPQ.mjs} +3 -2
- package/dist/chunk-DXZ6JDWG.mjs +797 -0
- package/dist/chunk-FJXJZUWF.mjs +209 -0
- package/dist/chunk-FS3HPPSC.mjs +212 -0
- package/dist/chunk-FT4KLZXW.mjs +1164 -0
- package/dist/chunk-GR5GMFJJ.mjs +104 -0
- package/dist/chunk-GXCDWLQI.mjs +193 -0
- package/dist/chunk-GXED6R3Q.mjs +271 -0
- package/dist/chunk-HLPRHOSO.mjs +17 -0
- package/dist/chunk-IL5DRCNU.mjs +3041 -0
- package/dist/{chunk-P36MUFEJ.mjs → chunk-JDDXV6UF.mjs} +2 -1
- package/dist/chunk-K5HSUPYS.mjs +11 -0
- package/dist/chunk-KYIRXTCA.mjs +100 -0
- package/dist/chunk-LFXS6OF7.mjs +289 -0
- package/dist/chunk-LNGE7SLY.mjs +151 -0
- package/dist/chunk-M2Z5WVWW.mjs +194 -0
- package/dist/{chunk-45AVKHMJ.mjs → chunk-M3B7SGJB.mjs} +63 -50
- package/dist/chunk-M3OW2XSP.mjs +40 -0
- package/dist/chunk-MFPSQZ6P.mjs +127 -0
- package/dist/chunk-MRCJBWJZ.mjs +1292 -0
- package/dist/chunk-MSIN4NM3.mjs +25 -0
- package/dist/chunk-MW3CJVGP.mjs +219 -0
- package/dist/chunk-MYM6CAVK.mjs +76 -0
- package/dist/chunk-NJJN4FMM.mjs +84 -0
- package/dist/chunk-NM7I3BM7.mjs +59 -0
- package/dist/chunk-OIJ64LGT.mjs +19 -0
- package/dist/{chunk-K7D4ZTSW.mjs → chunk-P4F4UWLC.mjs} +1 -1
- package/dist/chunk-Q2AP2FSN.mjs +15 -0
- package/dist/chunk-R2PJKSNL.mjs +54 -0
- package/dist/{chunk-HXJBSHXF.mjs → chunk-R724DJSG.mjs} +5056 -2619
- package/dist/chunk-S2POFTLN.mjs +10 -0
- package/dist/chunk-S5QMWECL.mjs +7 -0
- package/dist/chunk-SLAC5PAB.mjs +5008 -0
- package/dist/chunk-T4HMSZCX.mjs +5 -0
- package/dist/chunk-TACP6VK6.mjs +111 -0
- package/dist/chunk-TGPKS2UM.mjs +125 -0
- package/dist/chunk-TKKXPKEO.mjs +979 -0
- package/dist/chunk-V6EUBAIT.mjs +19 -0
- package/dist/chunk-XL56L4IR.mjs +228 -0
- package/dist/chunk-XQIAMDAG.mjs +3076 -0
- package/dist/chunk-YTUZ4R22.mjs +1063 -0
- package/dist/{chunk-KV3AZFT3.mjs → chunk-ZKOQR7EA.mjs} +45 -1
- package/dist/chunk-ZPJFGGGI.mjs +638 -0
- package/dist/chunk-ZXX5GZEX.mjs +17 -0
- package/dist/{client-i9eLJrw5.d.ts → client-BKqtl0_V.d.ts} +442 -22
- package/dist/client.d.ts +11 -10
- package/dist/client.mjs +8 -898
- package/dist/config-Bnl7L-0H.d.ts +608 -0
- package/dist/{config-P__Diiy4.d.ts → config-CJ-jfI0a.d.ts} +119 -9
- package/dist/core-leaf-6JVHEVFB.mjs +8 -0
- package/dist/custom-token-results-NXHBVY7U.mjs +59 -0
- package/dist/digest-leaf-KXBGKYHZ.mjs +1 -0
- package/dist/direct-executor-V3WOYU2Y.mjs +17 -0
- package/dist/distribution/coordinator/etcd.d.ts +63 -7
- package/dist/distribution/coordinator/etcd.mjs +725 -81
- package/dist/distribution/coordinator/in-memory.d.ts +3 -3
- package/dist/distribution/coordinator/in-memory.mjs +65 -10
- package/dist/distribution/transport/grpc.d.ts +2 -2
- package/dist/distribution/transport/grpc.mjs +39 -10
- package/dist/distribution/transport/in-memory.d.ts +2 -2
- package/dist/distribution/transport/in-memory.mjs +9 -6
- package/dist/distribution/transport/tcp.d.ts +2 -2
- package/dist/distribution/transport/tcp.mjs +20 -15
- package/dist/distribution.d.ts +267 -56
- package/dist/distribution.mjs +23156 -11362
- package/dist/durability-config-VWS2K24E.mjs +96 -0
- package/dist/embed-4PHHKGBK.mjs +263 -0
- package/dist/embeddings/openai.d.ts +1 -1
- package/dist/embeddings/openai.mjs +2 -2
- package/dist/{memory-CUEDwr-F.d.ts → engine-qjAxJSKk.d.ts} +2254 -1123
- package/dist/geo-index-2ZMMWWSK.mjs +1484 -0
- package/dist/gzip-2UCZW2MM.mjs +31 -0
- package/dist/{index-lcdGLyJH.d.ts → index-B_vmxpgW.d.ts} +260 -102
- package/dist/index.browser.mjs +4 -4
- package/dist/index.d.ts +2133 -57
- package/dist/index.mjs +16 -32
- package/dist/index.react-native.mjs +12 -0
- package/dist/invalidation/broadcast-channel.d.ts +1 -1
- package/dist/invalidation/filesystem.d.ts +1 -1
- package/dist/invalidation/filesystem.mjs +1 -2
- package/dist/invalidation/noop.d.ts +1 -1
- package/dist/languages/albanian.mjs +1 -1
- package/dist/languages/amharic.mjs +1 -1
- package/dist/languages/arabic.mjs +1 -1
- package/dist/languages/armenian.mjs +1 -1
- package/dist/languages/azerbaijani.mjs +1 -1
- package/dist/languages/bambara.mjs +1 -1
- package/dist/languages/basque.mjs +1 -1
- package/dist/languages/belarusian.mjs +1 -1
- package/dist/languages/bengali.mjs +1 -1
- package/dist/languages/bosnian.mjs +1 -1
- package/dist/languages/breton.mjs +1 -1
- package/dist/languages/bulgarian.mjs +1 -1
- package/dist/languages/burmese.mjs +1 -1
- package/dist/languages/catalan.mjs +1 -1
- package/dist/languages/chinese.mjs +1 -1
- package/dist/languages/croatian.mjs +1 -1
- package/dist/languages/czech.mjs +1 -1
- package/dist/languages/dagbani.mjs +1 -1
- package/dist/languages/danish.mjs +1 -1
- package/dist/languages/dutch.mjs +1 -1
- package/dist/languages/english.mjs +1 -1
- package/dist/languages/esperanto.mjs +1 -1
- package/dist/languages/estonian.mjs +1 -1
- package/dist/languages/ewe.mjs +1 -1
- package/dist/languages/faroese.mjs +1 -1
- package/dist/languages/fijian.mjs +1 -1
- package/dist/languages/finnish.mjs +1 -1
- package/dist/languages/french.mjs +1 -1
- package/dist/languages/ga.mjs +1 -1
- package/dist/languages/galician.mjs +1 -1
- package/dist/languages/georgian.mjs +1 -1
- package/dist/languages/german.mjs +1 -1
- package/dist/languages/greek.mjs +1 -1
- package/dist/languages/guarani.mjs +1 -1
- package/dist/languages/gujarati.mjs +1 -1
- package/dist/languages/haitian-creole.mjs +1 -1
- package/dist/languages/hausa.mjs +1 -1
- package/dist/languages/hawaiian.mjs +1 -1
- package/dist/languages/hebrew.mjs +1 -1
- package/dist/languages/hindi.mjs +1 -1
- package/dist/languages/hungarian.mjs +1 -1
- package/dist/languages/icelandic.mjs +1 -1
- package/dist/languages/igbo.mjs +1 -1
- package/dist/languages/indonesian.mjs +1 -1
- package/dist/languages/irish.mjs +1 -1
- package/dist/languages/italian.mjs +1 -1
- package/dist/languages/japanese.mjs +1 -1
- package/dist/languages/kannada.mjs +1 -1
- package/dist/languages/kazakh.mjs +1 -1
- package/dist/languages/khmer.mjs +1 -1
- package/dist/languages/kinyarwanda.mjs +1 -1
- package/dist/languages/kirundi.mjs +1 -1
- package/dist/languages/korean.mjs +1 -1
- package/dist/languages/kurmanji.mjs +1 -1
- package/dist/languages/kyrgyz.mjs +1 -1
- package/dist/languages/lao.mjs +1 -1
- package/dist/languages/latin.mjs +1 -1
- package/dist/languages/latvian.mjs +1 -1
- package/dist/languages/lingala.mjs +1 -1
- package/dist/languages/lithuanian.mjs +1 -1
- package/dist/languages/luxembourgish.mjs +1 -1
- package/dist/languages/macedonian.mjs +1 -1
- package/dist/languages/malagasy.mjs +1 -1
- package/dist/languages/malay.mjs +1 -1
- package/dist/languages/malayalam.mjs +1 -1
- package/dist/languages/maltese.mjs +1 -1
- package/dist/languages/maori.mjs +1 -1
- package/dist/languages/marathi.mjs +1 -1
- package/dist/languages/nepali.mjs +1 -1
- package/dist/languages/norwegian.mjs +1 -1
- package/dist/languages/oromo.mjs +1 -1
- package/dist/languages/persian.mjs +1 -1
- package/dist/languages/polish.mjs +1 -1
- package/dist/languages/portuguese.mjs +1 -1
- package/dist/languages/punjabi.mjs +1 -1
- package/dist/languages/romanian.mjs +1 -1
- package/dist/languages/russian.mjs +1 -1
- package/dist/languages/samoan.mjs +1 -1
- package/dist/languages/sanskrit.mjs +1 -1
- package/dist/languages/scottish-gaelic.mjs +1 -1
- package/dist/languages/serbian.mjs +1 -1
- package/dist/languages/shona.mjs +1 -1
- package/dist/languages/sinhala.mjs +1 -1
- package/dist/languages/slovak.mjs +1 -1
- package/dist/languages/slovenian.mjs +1 -1
- package/dist/languages/sorani.mjs +1 -1
- package/dist/languages/spanish.mjs +1 -1
- package/dist/languages/swahili.mjs +1 -1
- package/dist/languages/swedish.mjs +1 -1
- package/dist/languages/tagalog.mjs +1 -1
- package/dist/languages/tamil.mjs +1 -1
- package/dist/languages/tatar.mjs +1 -1
- package/dist/languages/telugu.mjs +1 -1
- package/dist/languages/thai.mjs +1 -1
- package/dist/languages/tibetan.mjs +1 -1
- package/dist/languages/tigrinya.mjs +1 -1
- package/dist/languages/tongan.mjs +1 -1
- package/dist/languages/turkish.mjs +1 -1
- package/dist/languages/twi.mjs +1 -1
- package/dist/languages/ukrainian.mjs +1 -1
- package/dist/languages/urdu.mjs +1 -1
- package/dist/languages/vietnamese.mjs +1 -1
- package/dist/languages/welsh.mjs +1 -1
- package/dist/languages/wolof.mjs +1 -1
- package/dist/languages/xhosa.mjs +1 -1
- package/dist/languages/yoruba.mjs +1 -1
- package/dist/languages/zulu.mjs +1 -1
- package/dist/long-query-thread-UCRK7MSI.mjs +154 -0
- package/dist/module-5ZSLUQII.mjs +3729 -0
- package/dist/msgpack-leaf-H5ZYW7S7.mjs +9 -0
- package/dist/optional-reads-5HFMF2QZ.mjs +3373 -0
- package/dist/optional-writes-4UOQKETU.mjs +247 -0
- package/dist/paged-partition-archive-BBRLZCIF.mjs +253 -0
- package/dist/partial-words.browser.mjs +1650 -0
- package/dist/partial-words.mjs +17 -11
- package/dist/{chunk-ASY5PHYI.mjs → pattern.browser.mjs} +253 -311
- package/dist/pattern.mjs +6 -7
- package/dist/persistence/durability/checkpoint-worker.d.ts +93 -25
- package/dist/persistence/durability/checkpoint-worker.mjs +51 -41
- package/dist/phrase-module-EFESWOCR.mjs +635 -0
- package/dist/query-actions-HMJUVWGT.mjs +119 -0
- package/dist/query-suggestions/merge-worker.d.ts +55 -0
- package/dist/query-suggestions/merge-worker.mjs +53 -0
- package/dist/query-suggestions-CBYkrQ5T.d.ts +311 -0
- package/dist/query-suggestions.browser.mjs +47 -0
- package/dist/query-suggestions.d.ts +20 -0
- package/dist/query-suggestions.mjs +19 -0
- package/dist/react-native/chunks/chunk-3OEFSHVV.mjs +970 -0
- package/dist/{browser/chunk-RELZRJNR.mjs → react-native/chunks/chunk-43R4GCM7.mjs} +49 -50
- package/dist/react-native/chunks/chunk-AKRYHAVG.mjs +6729 -0
- package/dist/react-native/chunks/chunk-CW2ZFXSE.mjs +14 -0
- package/dist/react-native/chunks/chunk-G3WCDTOF.mjs +29640 -0
- package/dist/react-native/chunks/chunk-GXXHHA2T.mjs +212 -0
- package/dist/react-native/chunks/chunk-H3MDVPBO.mjs +151 -0
- package/dist/react-native/chunks/chunk-HQEVQEDJ.mjs +52 -0
- package/dist/react-native/chunks/chunk-HRDMIQ63.mjs +131 -0
- package/dist/react-native/chunks/chunk-HTBY4GON.mjs +565 -0
- package/dist/react-native/chunks/chunk-LX62Y4B5.mjs +61 -0
- package/dist/react-native/chunks/chunk-MRH36RN3.mjs +902 -0
- package/dist/react-native/chunks/chunk-Q32DDFKX.mjs +19 -0
- package/dist/react-native/chunks/chunk-UE4ANHSX.mjs +127 -0
- package/dist/react-native/chunks/chunk-UNZPWLVL.mjs +1063 -0
- package/dist/react-native/chunks/chunk-UO44MA5M.mjs +232 -0
- package/dist/react-native/chunks/chunk-UYKIV2KT.mjs +42 -0
- package/dist/react-native/chunks/chunk-VUJCSQG5.mjs +11 -0
- package/dist/react-native/chunks/chunk-WVHF4ZAK.mjs +802 -0
- package/dist/react-native/chunks/chunk-ZUJCUPVS.mjs +54 -0
- package/dist/react-native/chunks/core-leaf-S2KNBWDY.mjs +7 -0
- package/dist/react-native/chunks/custom-token-results-3OZEMRE7.mjs +59 -0
- package/dist/react-native/chunks/digest-leaf-X6OIFDIU.mjs +1 -0
- package/dist/react-native/chunks/durability-config-FCSD4MXY.mjs +96 -0
- package/dist/react-native/chunks/embed-HA2I7RCF.mjs +263 -0
- package/dist/react-native/chunks/geo-index-QEYKEVPH.mjs +1484 -0
- package/dist/react-native/chunks/gzip.react-native-CVM4NQG2.mjs +12 -0
- package/dist/react-native/chunks/module-FMBX75GP.mjs +5209 -0
- package/dist/react-native/chunks/msgpack-leaf-JMNGSTRW.mjs +7 -0
- package/dist/react-native/chunks/optional-reads-AFPO2FEB.mjs +3364 -0
- package/dist/react-native/chunks/optional-writes-XOH25XZI.mjs +296 -0
- package/dist/react-native/chunks/phrase-module-IGYBK7XH.mjs +636 -0
- package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
- package/dist/react-native/chunks/resolve-vector-text-GE2CRJKI.mjs +84 -0
- package/dist/react-native/chunks/restore-queue-LLNMYB4Z.mjs +187 -0
- package/dist/react-native/chunks/runtime-UOHD2XUJ.mjs +615 -0
- package/dist/react-native/chunks/segment-codec-leaf-VR6O5GLC.mjs +13 -0
- package/dist/react-native/chunks/segment-merge-leaf-LLMWU4EW.mjs +14 -0
- package/dist/react-native/chunks/storage-leaf-JD7PIDO4.mjs +19633 -0
- package/dist/react-native/chunks/store-leaf-4UVCMYGN.mjs +15 -0
- package/dist/react-native/chunks/suggestions-leaf-J5VT5RHY.mjs +1321 -0
- package/dist/react-native/chunks/worker-copies-ZYHY7FKJ.mjs +2831 -0
- package/dist/react-native/partial-words.mjs +1658 -0
- package/dist/react-native/pattern.mjs +2570 -0
- package/dist/react-native/query-suggestions.mjs +55 -0
- package/dist/react-native/sync.mjs +2759 -0
- package/dist/react-native/synonyms.mjs +676 -0
- package/dist/react.d.ts +12 -8
- package/dist/react.mjs +45 -1
- package/dist/{rerank-NWTKK636.mjs → rerank-RI2PMNOD.mjs} +22 -12
- package/dist/rerankers/http.d.ts +1 -1
- package/dist/rerankers/http.mjs +2 -2
- package/dist/resolve-vector-text-OD4J3WHF.mjs +3 -0
- package/dist/restore-queue-PCKWDAKQ.mjs +192 -0
- package/dist/runtime-USQYKN3I.mjs +615 -0
- package/dist/{schema-4W_7tk1f.d.ts → schema-CzG9OUw5.d.ts} +70 -30
- package/dist/segment-codec-leaf-QJM7I6QG.mjs +18 -0
- package/dist/segment-merge-leaf-4BJGIJHY.mjs +18 -0
- package/dist/server.d.ts +348 -22
- package/dist/server.mjs +2211 -327
- package/dist/storage-leaf-GRRRI7UV.mjs +5962 -0
- package/dist/store-leaf-6VMMSVBE.mjs +20 -0
- package/dist/stored-values-BWIMpiT_.d.ts +25 -0
- package/dist/suggestions-leaf-4KPTMPTL.mjs +54 -0
- package/dist/sync/filesystem.d.ts +30 -0
- package/dist/sync/filesystem.mjs +288 -0
- package/dist/sync/indexeddb.browser.mjs +287 -0
- package/dist/sync/indexeddb.d.ts +30 -0
- package/dist/sync/indexeddb.mjs +242 -0
- package/dist/sync.browser.mjs +1271 -0
- package/dist/sync.d.ts +23 -0
- package/dist/sync.mjs +2712 -0
- package/dist/synonyms.browser.mjs +658 -0
- package/dist/synonyms.mjs +17 -7
- package/dist/tasks-65b6t6lk.d.ts +782 -0
- package/dist/thread-UJ3W5LVI.mjs +31 -0
- package/dist/{types-B3b68nfF.d.ts → types-Ck97nB3D.d.ts} +1 -1
- package/dist/types-CpCg-Gq-.d.ts +227 -0
- package/dist/{types-Cb9kiY5P.d.ts → types-DvncmvuQ.d.ts} +12 -1
- package/dist/vector/search-worker.d.ts +10 -2
- package/dist/vector/search-worker.mjs +11 -11
- package/dist/worker-copies-YSXZXIGC.mjs +2232 -0
- package/dist/workers/entry.mjs +75 -15
- package/openapi.json +6840 -0
- package/package.json +404 -19
- package/dist/browser/chunk-CN4I4FZU.mjs +0 -602
- package/dist/browser/chunk-HOZW3PKP.mjs +0 -9
- package/dist/browser/chunk-JPTQZWJQ.mjs +0 -10
- package/dist/browser/chunk-LD55ZGU3.mjs +0 -347
- package/dist/browser/chunk-NAFT3K6X.mjs +0 -24546
- package/dist/browser/chunk-NNG3EEB6.mjs +0 -2728
- package/dist/browser/chunk-QWES44AW.mjs +0 -473
- package/dist/browser/durability-wiring-PY3L7DES.mjs +0 -3679
- package/dist/browser/module-PDXYHT75.mjs +0 -2822
- package/dist/browser/release-76MXRPGV.mjs +0 -33
- package/dist/browser/restore-queue-DVIYJ7RU.mjs +0 -35
- package/dist/browser/snapshot-J57MZK6L.mjs +0 -1207
- package/dist/browser/write-L7FAQA47.mjs +0 -208
- package/dist/chunk-37CD3OKS.mjs +0 -87
- package/dist/chunk-5XNIRLFC.mjs +0 -96
- package/dist/chunk-7X5JST5L.mjs +0 -114
- package/dist/chunk-AV4IJKD2.mjs +0 -35
- package/dist/chunk-AY3AQ3UH.mjs +0 -73
- package/dist/chunk-BKB3XTMI.mjs +0 -707
- package/dist/chunk-C7SLA6CM.mjs +0 -19
- package/dist/chunk-COVCQ73F.mjs +0 -587
- package/dist/chunk-DJHHF2L5.mjs +0 -17
- package/dist/chunk-E74YLAIG.mjs +0 -24
- package/dist/chunk-FMWRCP2C.mjs +0 -57
- package/dist/chunk-GES7U2PM.mjs +0 -68
- package/dist/chunk-HJSN7UYG.mjs +0 -10
- package/dist/chunk-JFRNNBOM.mjs +0 -35
- package/dist/chunk-K25LZA54.mjs +0 -8484
- package/dist/chunk-KEHK2O6P.mjs +0 -13
- package/dist/chunk-KOKM4PQN.mjs +0 -52
- package/dist/chunk-KZ3NX2P7.mjs +0 -21
- package/dist/chunk-L3L5YFXC.mjs +0 -614
- package/dist/chunk-L5ORED7V.mjs +0 -25
- package/dist/chunk-LH4NZNJJ.mjs +0 -1731
- package/dist/chunk-LU2CE5AE.mjs +0 -507
- package/dist/chunk-LXWDQLTW.mjs +0 -30
- package/dist/chunk-MHOSCQCQ.mjs +0 -948
- package/dist/chunk-NKRBEGJZ.mjs +0 -1705
- package/dist/chunk-NTRMFARG.mjs +0 -462
- package/dist/chunk-NW4XDSP2.mjs +0 -212
- package/dist/chunk-OOEXLUGR.mjs +0 -117
- package/dist/chunk-ORBLD3EL.mjs +0 -6
- package/dist/chunk-OWNZYMRE.mjs +0 -3591
- package/dist/chunk-PPY2GSIB.mjs +0 -44
- package/dist/chunk-QY7GC2MW.mjs +0 -33
- package/dist/chunk-R6A773WK.mjs +0 -226
- package/dist/chunk-R6PR4WYZ.mjs +0 -68
- package/dist/chunk-R7EEKAJE.mjs +0 -593
- package/dist/chunk-RONEOEON.mjs +0 -426
- package/dist/chunk-VWULPWRJ.mjs +0 -7469
- package/dist/chunk-WCJKUGHB.mjs +0 -262
- package/dist/chunk-WQBCBKY3.mjs +0 -8
- package/dist/chunk-X5CV2E2B.mjs +0 -1
- package/dist/chunk-YA7XLE2R.mjs +0 -1256
- package/dist/chunk-YTJCGKM4.mjs +0 -56
- package/dist/chunk-ZG477IPV.mjs +0 -526
- package/dist/chunk-ZYNAMIYX.mjs +0 -113
- package/dist/config-BGB_fDtq.d.ts +0 -237
- package/dist/direct-executor-CXZ66ACP.mjs +0 -22
- package/dist/durability-wiring-QTHOGFMZ.mjs +0 -3229
- package/dist/engine-BGCEm9E_.d.ts +0 -577
- package/dist/module-3NONKBZX.mjs +0 -1860
- package/dist/release-BH3DKW3T.mjs +0 -33
- package/dist/restore-queue-JPRBUYII.mjs +0 -35
- package/dist/runtime-ZKYZFJJC.mjs +0 -177
- package/dist/snapshot-HOGXNO5W.mjs +0 -793
- package/dist/tasks-HG3rlxx6.d.ts +0 -302
- package/dist/thread-B657LQXK.mjs +0 -692
- package/dist/write-XLIGTXBU.mjs +0 -238
|
@@ -1,2822 +0,0 @@
|
|
|
1
|
-
import { spawnNodeWorker } from './chunk-HOZW3PKP.mjs';
|
|
2
|
-
import { VECTOR_WORKER_COPIES_ALLOWED, openSharedQuantizer, STORE_ORDINAL_LAYOUT_GENERATION, searchOrdinals, insertNode, GRAPH_RETIRED, nodeCountOf, tombstoneCountOf, DEFAULT_PROMOTION_THRESHOLD, DEFAULT_FILTER_THRESHOLD, osqBitsOf, createOsqQuantizer, recordCheckpoint, planCheckpoint, deserialize, assignStorePartitions, liveSize, ordinalFilterForSubsets, ordinalFilterValues, threadsHoldCurrentLayout, filterForOptions, openGraphState, STORE_UNPLACED_COUNT, STORE_LIVE_COUNT, STORE_REMOVED_COUNT, fixedView, OSQ4_MIN_DIMENSION, sharedVectorStoreBytes, STORE_SLOTS, IN_MEMORY, resetStoreHeader, STORE_RELEASED_VECTORS_TO_DISK, magnitude, emptyFieldBeforeRestore, ESTIMATED_MS_PER_TOMBSTONE, graphNeedsRebuild, ESTIMATED_MS_PER_VECTOR_REBUILD, releaseReplacedVectors, createOrdinalFilter, addToOrdinalFilter, nativeScoresOf, toScore, ordinalFilterHas, WORKER_COPY_MIN_VECTORS, allLiveDocIds, openVectorBlock, NOTHING_STAGED, arenaFloat32Distance, slotByteOffset, detachNativeStore, STORE_BLOCK_COUNT, STORE_CODE_BLOCK_COUNT, createSharedVectorStoreHandles, BLOCK_MAX_BYTES, growBufferTo, unplacedChunkCount, STORE_DOC_ID_BYTES, recalibrateFromStore, signatureOf, nativeStoreScratchBytes, noteFallback, toDistance, euclideanDistance, dotProduct, cosineSimilarityWithMagnitudes, QUERY_STAGED, createVectorBlock, ensureBlockSlots, createHNSWIndex, adoptGraph, topLayerInParts, calibrateQuantizer, fieldHandlesOf, sharedMemoryAvailable, requireWholeNeighbourLists, BUILD_CHUNK_SIZE, yieldToEventLoop, releaseLocksHeldBy, DISK_ADOPTION_YIELD_INTERVAL } from './chunk-Z7QH2XHB.mjs';
|
|
3
|
-
export { releaseLocksHeldBy } from './chunk-Z7QH2XHB.mjs';
|
|
4
|
-
import { isDead, NO_LIVE_VECTOR_AT_POSITION, bytesToVectors } from './chunk-LD55ZGU3.mjs';
|
|
5
|
-
import { MAX_RENUMBERED_SEARCH_ATTEMPTS, DISK_STORAGE_BLOCK_BYTES, compareCodePoints, UNPLACED_FLAG_CHUNK_ORDINALS, MAX_VECTOR_ORDINALS, GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER, GRAPH_BUILD_DISPATCH_CHUNK, resolveWorkerCount, resolveWorkerEntry, detectRuntime, WORKER_YOUNG_GENERATION_MB, VECTOR_SEARCH_TIMEOUT_MS, VECTOR_SEARCH_LOAD_TIMEOUT_MS, VECTOR_INSERT_TIMEOUT_MS, VECTOR_SCRATCH_SLOTS } from './chunk-QWES44AW.mjs';
|
|
6
|
-
import { NarsilError, ErrorCodes } from './chunk-RELZRJNR.mjs';
|
|
7
|
-
|
|
8
|
-
// src/core/heap.ts
|
|
9
|
-
function createMaxHeap(compare) {
|
|
10
|
-
return createHeap((a, b) => compare(b, a));
|
|
11
|
-
}
|
|
12
|
-
function createBoundedMaxHeap(compare, capacity) {
|
|
13
|
-
const heap = createMaxHeap(compare);
|
|
14
|
-
const innerPush = heap.push;
|
|
15
|
-
return {
|
|
16
|
-
push(item) {
|
|
17
|
-
if (heap.size < capacity) {
|
|
18
|
-
innerPush(item);
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const top = heap.peek();
|
|
22
|
-
if (top !== void 0 && compare(item, top) < 0) {
|
|
23
|
-
heap.pop();
|
|
24
|
-
innerPush(item);
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
pop: heap.pop,
|
|
28
|
-
peek: heap.peek,
|
|
29
|
-
get size() {
|
|
30
|
-
return heap.size;
|
|
31
|
-
},
|
|
32
|
-
toSortedArray: heap.toSortedArray
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
function createHeap(compare) {
|
|
36
|
-
const items = [];
|
|
37
|
-
function siftUp(idx) {
|
|
38
|
-
while (idx > 0) {
|
|
39
|
-
const parent = idx - 1 >> 1;
|
|
40
|
-
if (compare(items[idx], items[parent]) >= 0) break;
|
|
41
|
-
const tmp = items[idx];
|
|
42
|
-
items[idx] = items[parent];
|
|
43
|
-
items[parent] = tmp;
|
|
44
|
-
idx = parent;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function siftDown(idx) {
|
|
48
|
-
const len = items.length;
|
|
49
|
-
while (true) {
|
|
50
|
-
let smallest = idx;
|
|
51
|
-
const left = 2 * idx + 1;
|
|
52
|
-
const right = 2 * idx + 2;
|
|
53
|
-
if (left < len && compare(items[left], items[smallest]) < 0) smallest = left;
|
|
54
|
-
if (right < len && compare(items[right], items[smallest]) < 0) smallest = right;
|
|
55
|
-
if (smallest === idx) break;
|
|
56
|
-
const tmp = items[idx];
|
|
57
|
-
items[idx] = items[smallest];
|
|
58
|
-
items[smallest] = tmp;
|
|
59
|
-
idx = smallest;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
push(item) {
|
|
64
|
-
items.push(item);
|
|
65
|
-
siftUp(items.length - 1);
|
|
66
|
-
},
|
|
67
|
-
pop() {
|
|
68
|
-
if (items.length === 0) return void 0;
|
|
69
|
-
const top = items[0];
|
|
70
|
-
const last = items.pop();
|
|
71
|
-
if (items.length > 0) {
|
|
72
|
-
items[0] = last;
|
|
73
|
-
siftDown(0);
|
|
74
|
-
}
|
|
75
|
-
return top;
|
|
76
|
-
},
|
|
77
|
-
peek() {
|
|
78
|
-
return items[0];
|
|
79
|
-
},
|
|
80
|
-
get size() {
|
|
81
|
-
return items.length;
|
|
82
|
-
},
|
|
83
|
-
toSortedArray() {
|
|
84
|
-
return [...items].sort(compare);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// src/vector/vector-index/merge-results.ts
|
|
90
|
-
function highScoreFirst(a, b) {
|
|
91
|
-
return b.score - a.score || compareCodePoints(a.docId, b.docId);
|
|
92
|
-
}
|
|
93
|
-
function mergeResults(hnswResults, bufferResults, k) {
|
|
94
|
-
const seen = /* @__PURE__ */ new Set();
|
|
95
|
-
const merged = [];
|
|
96
|
-
let hi = 0;
|
|
97
|
-
let bi = 0;
|
|
98
|
-
while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
|
|
99
|
-
const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
|
|
100
|
-
const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
|
|
101
|
-
let pick;
|
|
102
|
-
if (h && b) {
|
|
103
|
-
if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
|
|
104
|
-
pick = h;
|
|
105
|
-
hi++;
|
|
106
|
-
} else {
|
|
107
|
-
pick = b;
|
|
108
|
-
bi++;
|
|
109
|
-
}
|
|
110
|
-
} else if (h) {
|
|
111
|
-
pick = h;
|
|
112
|
-
hi++;
|
|
113
|
-
} else if (b) {
|
|
114
|
-
pick = b;
|
|
115
|
-
bi++;
|
|
116
|
-
} else {
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
if (seen.has(pick.docId)) continue;
|
|
120
|
-
seen.add(pick.docId);
|
|
121
|
-
merged.push(pick);
|
|
122
|
-
}
|
|
123
|
-
return merged;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// src/vector/shared-field/store-flags.ts
|
|
127
|
-
function bytesOf(flags, source) {
|
|
128
|
-
if (flags.source !== source) {
|
|
129
|
-
flags.source = source;
|
|
130
|
-
flags.view = new Uint8Array(source);
|
|
131
|
-
}
|
|
132
|
-
return flags.view;
|
|
133
|
-
}
|
|
134
|
-
function countsOf(chunks, source) {
|
|
135
|
-
if (chunks.source !== source) {
|
|
136
|
-
chunks.source = source;
|
|
137
|
-
chunks.view = new Int32Array(source);
|
|
138
|
-
}
|
|
139
|
-
return chunks.view;
|
|
140
|
-
}
|
|
141
|
-
function readStoreFlags(store) {
|
|
142
|
-
const unplaced = { source: null, view: new Uint8Array(0) };
|
|
143
|
-
const unplacedChunks = { source: null, view: new Int32Array(0) };
|
|
144
|
-
const removed = { source: null, view: new Uint8Array(0) };
|
|
145
|
-
return {
|
|
146
|
-
liveDocuments() {
|
|
147
|
-
const header = store.handles.header;
|
|
148
|
-
return Atomics.load(header, STORE_LIVE_COUNT) - Atomics.load(header, STORE_REMOVED_COUNT);
|
|
149
|
-
},
|
|
150
|
-
isRemoved(ordinal) {
|
|
151
|
-
const bytes = bytesOf(removed, store.handles.removed);
|
|
152
|
-
return ordinal < bytes.length && Atomics.load(bytes, ordinal) === 1;
|
|
153
|
-
},
|
|
154
|
-
unplacedOrdinals() {
|
|
155
|
-
const handles = store.handles;
|
|
156
|
-
if (Atomics.load(handles.header, STORE_UNPLACED_COUNT) === 0) return [];
|
|
157
|
-
const bytes = bytesOf(unplaced, handles.unplaced);
|
|
158
|
-
const counts = countsOf(unplacedChunks, handles.unplacedChunks);
|
|
159
|
-
const end = Math.min(store.slots, bytes.length);
|
|
160
|
-
const ordinals = [];
|
|
161
|
-
for (let chunk = 0; chunk < counts.length; chunk++) {
|
|
162
|
-
const from = chunk * UNPLACED_FLAG_CHUNK_ORDINALS;
|
|
163
|
-
if (from >= end) break;
|
|
164
|
-
if (Atomics.load(counts, chunk) === 0) continue;
|
|
165
|
-
const to = Math.min(end, from + UNPLACED_FLAG_CHUNK_ORDINALS);
|
|
166
|
-
for (let ordinal = from; ordinal < to; ordinal++) {
|
|
167
|
-
if (Atomics.load(bytes, ordinal) === 1) ordinals.push(ordinal);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
return ordinals;
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// src/vector/shared-field/searcher.ts
|
|
176
|
-
function distancesInTypeScript(store, query, metric, ordinals) {
|
|
177
|
-
noteFallback("score");
|
|
178
|
-
const arenaQuery = store.prepareQueryArena(query);
|
|
179
|
-
const queryMagnitude = arenaQuery ? arenaQuery.magnitude : magnitude(query);
|
|
180
|
-
const distances = new Float64Array(ordinals.length).fill(Number.POSITIVE_INFINITY);
|
|
181
|
-
for (let i = 0; i < ordinals.length; i++) {
|
|
182
|
-
if (arenaQuery) {
|
|
183
|
-
distances[i] = store.distanceFromArena(arenaQuery, ordinals[i], metric);
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
const entry = store.entryForOrdinal(ordinals[i]);
|
|
187
|
-
if (entry) distances[i] = toDistance(query, entry.vector, queryMagnitude, entry.magnitude, metric);
|
|
188
|
-
}
|
|
189
|
-
return distances;
|
|
190
|
-
}
|
|
191
|
-
function scoreCandidates(store, candidates, query, k, metric, minSimilarity) {
|
|
192
|
-
const { ordinals, docIds } = candidates;
|
|
193
|
-
if (ordinals.length === 0) return { results: [], matched: 0 };
|
|
194
|
-
const distances = nativeScoresOf(store.handles, query, metric, ordinals) ?? distancesInTypeScript(store, query, metric, ordinals);
|
|
195
|
-
const heap = createBoundedMaxHeap(highScoreFirst, k);
|
|
196
|
-
let matched = 0;
|
|
197
|
-
for (let i = 0; i < ordinals.length; i++) {
|
|
198
|
-
if (distances[i] === Number.POSITIVE_INFINITY) continue;
|
|
199
|
-
const score = toScore(distances[i], metric);
|
|
200
|
-
if (score < minSimilarity) continue;
|
|
201
|
-
matched++;
|
|
202
|
-
heap.push({ docId: docIds[i], score });
|
|
203
|
-
}
|
|
204
|
-
return { results: heap.toSortedArray().reverse(), matched };
|
|
205
|
-
}
|
|
206
|
-
function createSharedVectorSearcher(options) {
|
|
207
|
-
const { fieldName, view, holdsDocument } = options;
|
|
208
|
-
const flags = readStoreFlags(view.store);
|
|
209
|
-
function ordinalLayoutGeneration() {
|
|
210
|
-
return Atomics.load(view.store.handles.header, STORE_ORDINAL_LAYOUT_GENERATION);
|
|
211
|
-
}
|
|
212
|
-
function ordinalSpace() {
|
|
213
|
-
const store = view.store;
|
|
214
|
-
const layoutGeneration = ordinalLayoutGeneration();
|
|
215
|
-
return {
|
|
216
|
-
fieldName,
|
|
217
|
-
layoutGeneration,
|
|
218
|
-
slots: store.slots,
|
|
219
|
-
header: store.handles.header,
|
|
220
|
-
ordinalOf: (docId) => view.ordinalOf(docId),
|
|
221
|
-
holdsCurrentOrdinal: (ordinal) => store.holdsOrdinal(ordinal) && store.partitionAt(ordinal) !== void 0,
|
|
222
|
-
isRemoved: (ordinal) => flags.isRemoved(ordinal)
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
function filterForPartitions(allowed) {
|
|
226
|
-
const slots = view.store.slots;
|
|
227
|
-
const filter = createOrdinalFilter(slots);
|
|
228
|
-
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
229
|
-
const partition = view.store.partitionAt(ordinal);
|
|
230
|
-
if (partition === void 0 || !allowed.has(partition)) continue;
|
|
231
|
-
if (!view.store.holdsOrdinal(ordinal) || flags.isRemoved(ordinal)) continue;
|
|
232
|
-
addToOrdinalFilter(filter, ordinal);
|
|
233
|
-
}
|
|
234
|
-
return filter;
|
|
235
|
-
}
|
|
236
|
-
function filterFor(searchOptions) {
|
|
237
|
-
if (searchOptions.filterSubsets !== void 0) {
|
|
238
|
-
return ordinalFilterForSubsets(searchOptions.filterSubsets, ordinalSpace());
|
|
239
|
-
}
|
|
240
|
-
if (searchOptions.filterPartitions !== void 0) return filterForPartitions(searchOptions.filterPartitions);
|
|
241
|
-
return void 0;
|
|
242
|
-
}
|
|
243
|
-
function candidatesOf(ordinals, filter) {
|
|
244
|
-
const candidates = { ordinals: [], docIds: [] };
|
|
245
|
-
for (const ordinal of ordinals) {
|
|
246
|
-
if (filter !== void 0 && !ordinalFilterHas(filter, ordinal)) continue;
|
|
247
|
-
if (flags.isRemoved(ordinal)) continue;
|
|
248
|
-
const docId = view.docIdOf(ordinal);
|
|
249
|
-
if (docId === void 0 || view.ordinalOf(docId) !== ordinal) continue;
|
|
250
|
-
candidates.ordinals.push(ordinal);
|
|
251
|
-
candidates.docIds.push(docId);
|
|
252
|
-
}
|
|
253
|
-
return candidates;
|
|
254
|
-
}
|
|
255
|
-
function answerOf(outcome) {
|
|
256
|
-
const results = outcome.results.filter((result) => holdsDocument(result.docId));
|
|
257
|
-
const holdsEveryHit = outcome.resolvedEveryHit && results.length === outcome.results.length;
|
|
258
|
-
return { results, matched: outcome.matched, matchedExact: outcome.matchedExact && holdsEveryHit };
|
|
259
|
-
}
|
|
260
|
-
function scanned(scan) {
|
|
261
|
-
return { results: scan.results, matched: scan.matched, matchedExact: true, resolvedEveryHit: true };
|
|
262
|
-
}
|
|
263
|
-
function search2(query, k, searchOptions, filter) {
|
|
264
|
-
const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = searchOptions;
|
|
265
|
-
const empty = { results: [], matched: 0, matchedExact: true, resolvedEveryHit: true };
|
|
266
|
-
const liveDocuments = flags.liveDocuments();
|
|
267
|
-
if (liveDocuments <= 0) return empty;
|
|
268
|
-
if (filter !== void 0 && filter.count === 0) return empty;
|
|
269
|
-
function fetched(results, resolvedEveryHit2) {
|
|
270
|
-
if (minSimilarity === Number.NEGATIVE_INFINITY) {
|
|
271
|
-
const matched = filter === void 0 ? liveDocuments : filter.count;
|
|
272
|
-
return { results, matched, matchedExact: true, resolvedEveryHit: resolvedEveryHit2 };
|
|
273
|
-
}
|
|
274
|
-
return { results, matched: results.length, matchedExact: false, resolvedEveryHit: resolvedEveryHit2 };
|
|
275
|
-
}
|
|
276
|
-
if (k <= 0) return fetched([], true);
|
|
277
|
-
const graph = view.graph;
|
|
278
|
-
if (graph === null) {
|
|
279
|
-
const ordinals = filter === void 0 ? flags.unplacedOrdinals() : ordinalFilterValues(filter);
|
|
280
|
-
return scanned(scoreCandidates(view.store, candidatesOf(ordinals, filter), query, k, metric, minSimilarity));
|
|
281
|
-
}
|
|
282
|
-
if (filter !== void 0) {
|
|
283
|
-
const graphLiveSize = view.liveSize;
|
|
284
|
-
const selectivity = graphLiveSize > 0 ? filter.count / graphLiveSize : 1;
|
|
285
|
-
if (selectivity < view.handles.filterThreshold) {
|
|
286
|
-
const candidates = candidatesOf(ordinalFilterValues(filter), filter);
|
|
287
|
-
return scanned(scoreCandidates(view.store, candidates, query, k, metric, minSimilarity));
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
let resolvedEveryHit = true;
|
|
291
|
-
const graphResults = [];
|
|
292
|
-
for (const hit of view.searchOrdinals(query, k, metric, minSimilarity, {
|
|
293
|
-
filter,
|
|
294
|
-
efSearch,
|
|
295
|
-
oversample,
|
|
296
|
-
rescoreWindow
|
|
297
|
-
})) {
|
|
298
|
-
const docId = view.docIdOf(hit.ord);
|
|
299
|
-
if (docId === void 0) {
|
|
300
|
-
resolvedEveryHit = false;
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
graphResults.push({ docId, score: hit.score });
|
|
304
|
-
}
|
|
305
|
-
const unplaced = flags.unplacedOrdinals();
|
|
306
|
-
if (unplaced.length === 0) return fetched(graphResults, resolvedEveryHit);
|
|
307
|
-
const scan = scoreCandidates(view.store, candidatesOf(unplaced, filter), query, k, metric, minSimilarity);
|
|
308
|
-
return fetched(mergeResults(graphResults, scan.results, k), resolvedEveryHit);
|
|
309
|
-
}
|
|
310
|
-
return {
|
|
311
|
-
fieldName,
|
|
312
|
-
dimension: view.handles.dimension,
|
|
313
|
-
partitionsKnown: () => true,
|
|
314
|
-
assignPartitions: () => void 0,
|
|
315
|
-
searchParallel(query, k, searchOptions) {
|
|
316
|
-
if (query.length !== view.handles.dimension) {
|
|
317
|
-
throw new NarsilError(
|
|
318
|
-
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
319
|
-
`Vector dimension mismatch: expected ${view.handles.dimension}, got ${query.length}`,
|
|
320
|
-
{ expected: view.handles.dimension, received: query.length }
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
for (let attempt = 1; ; attempt++) {
|
|
324
|
-
const layoutGeneration = ordinalLayoutGeneration();
|
|
325
|
-
const outcome = search2(query, k, searchOptions, filterFor(searchOptions));
|
|
326
|
-
if (view.graphRetired) {
|
|
327
|
-
throw new NarsilError(
|
|
328
|
-
ErrorCodes.WORKER_BUSY,
|
|
329
|
-
`The main thread replaced the graph of vector field "${fieldName}" while this thread searched it`,
|
|
330
|
-
{ fieldName }
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
|
-
if (ordinalLayoutGeneration() === layoutGeneration) return Promise.resolve(answerOf(outcome));
|
|
334
|
-
if (attempt >= MAX_RENUMBERED_SEARCH_ATTEMPTS) {
|
|
335
|
-
throw new NarsilError(
|
|
336
|
-
ErrorCodes.WORKER_BUSY,
|
|
337
|
-
`The main thread renumbered the vectors of field "${fieldName}" during each of ${attempt} searches on this thread`,
|
|
338
|
-
{ fieldName, attempts: attempt }
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
// src/vector/vector-store/vector-file.browser.ts
|
|
347
|
-
function filesystemUnavailable() {
|
|
348
|
-
throw new NarsilError(
|
|
349
|
-
ErrorCodes.CONFIG_INVALID,
|
|
350
|
-
"Reading vectors from a checkpoint file requires a Node.js runtime; keep the field in memory in the browser"
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
function createVectorFileReader() {
|
|
354
|
-
return {
|
|
355
|
-
readInto: () => filesystemUnavailable(),
|
|
356
|
-
retainOnly: () => void 0,
|
|
357
|
-
close: () => void 0
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
// src/vector/vector-store/hot-block-distance.ts
|
|
362
|
-
function jsDistance(a, b, magA, magB, metric) {
|
|
363
|
-
switch (metric) {
|
|
364
|
-
case "cosine":
|
|
365
|
-
return 1 - cosineSimilarityWithMagnitudes(a, b, magA, magB);
|
|
366
|
-
case "dotProduct":
|
|
367
|
-
return -dotProduct(a, b);
|
|
368
|
-
case "euclidean":
|
|
369
|
-
return euclideanDistance(a, b);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
function inMemory(reads, ordinal) {
|
|
373
|
-
return ordinal >= 0 && ordinal < reads.limit && reads.diskFile[ordinal] === IN_MEMORY;
|
|
374
|
-
}
|
|
375
|
-
function queryDistanceOf(source, prepared, metric) {
|
|
376
|
-
const generic = (ordinal) => source.distanceFromArena(prepared, ordinal, metric);
|
|
377
|
-
const reads = source.hotBlockReads();
|
|
378
|
-
const simd = reads?.block.simd ?? null;
|
|
379
|
-
if (reads === null || simd === null || !reads.block.hasScratch) return generic;
|
|
380
|
-
const { block, present, magnitudes, slotsOffset, slotStride, dimension } = reads;
|
|
381
|
-
const scratchOffset = block.scratchByteOffset;
|
|
382
|
-
const queryMagnitude = prepared.magnitude;
|
|
383
|
-
return (ordinal) => {
|
|
384
|
-
if (!inMemory(reads, ordinal) || block.stagedOrdinal !== QUERY_STAGED) return generic(ordinal);
|
|
385
|
-
if (present[ordinal] !== 1) return Number.POSITIVE_INFINITY;
|
|
386
|
-
const slotOffset = slotsOffset + ordinal * slotStride;
|
|
387
|
-
return arenaFloat32Distance(simd, scratchOffset, slotOffset, dimension, metric, queryMagnitude, magnitudes[ordinal]);
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
function pairDistanceOf(source, metric) {
|
|
391
|
-
const generic = (ordA, ordB) => source.distanceByOrdinal(ordA, ordB, metric);
|
|
392
|
-
const reads = source.hotBlockReads();
|
|
393
|
-
const simd = reads?.block.simd ?? null;
|
|
394
|
-
if (reads === null || simd === null) return generic;
|
|
395
|
-
const { present, magnitudes, slotsOffset, slotStride, dimension } = reads;
|
|
396
|
-
return (ordA, ordB) => {
|
|
397
|
-
if (!inMemory(reads, ordA) || !inMemory(reads, ordB) || present[ordA] !== 1) return generic(ordA, ordB);
|
|
398
|
-
if (present[ordB] !== 1) return Number.POSITIVE_INFINITY;
|
|
399
|
-
const offsetA = slotsOffset + ordA * slotStride;
|
|
400
|
-
const offsetB = slotsOffset + ordB * slotStride;
|
|
401
|
-
return arenaFloat32Distance(simd, offsetA, offsetB, dimension, metric, magnitudes[ordA], magnitudes[ordB]);
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
function ordinalDistanceOf(source, from, metric) {
|
|
405
|
-
const pair = pairDistanceOf(source, metric);
|
|
406
|
-
return (ordinal) => pair(from, ordinal);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
// src/vector/vector-store/view.ts
|
|
410
|
-
var decoder = new TextDecoder();
|
|
411
|
-
var NOT_HELD = 0;
|
|
412
|
-
var HELD_COLD = 1;
|
|
413
|
-
var HELD_HOT = 2;
|
|
414
|
-
function openSharedVectorStore(initial, threadSlot) {
|
|
415
|
-
let handles = initial;
|
|
416
|
-
const { dimension, layout } = initial;
|
|
417
|
-
const capacity = layout.capacity;
|
|
418
|
-
const codeCapacity = initial.codeLayout?.capacity ?? 1;
|
|
419
|
-
const blocks = [];
|
|
420
|
-
const codeBlocks = [];
|
|
421
|
-
let present = fixedView(handles.present, Uint8Array);
|
|
422
|
-
let magnitudes = fixedView(handles.magnitudes, Float64Array);
|
|
423
|
-
let partitions = fixedView(handles.partitions, Int32Array);
|
|
424
|
-
let docIdBytes = fixedView(handles.docIdBytes, Uint8Array);
|
|
425
|
-
let docIdOffsets = fixedView(handles.docIdOffsets, Uint32Array);
|
|
426
|
-
let diskFile = fixedView(handles.diskFile, Int32Array);
|
|
427
|
-
let diskOffset = fixedView(handles.diskOffset, Uint32Array);
|
|
428
|
-
let currentQuery = null;
|
|
429
|
-
let reader = null;
|
|
430
|
-
const coldScratchA = new Float32Array(dimension);
|
|
431
|
-
const coldScratchB = new Float32Array(dimension);
|
|
432
|
-
function rebind() {
|
|
433
|
-
present = fixedView(handles.present, Uint8Array);
|
|
434
|
-
magnitudes = fixedView(handles.magnitudes, Float64Array);
|
|
435
|
-
partitions = fixedView(handles.partitions, Int32Array);
|
|
436
|
-
docIdBytes = fixedView(handles.docIdBytes, Uint8Array);
|
|
437
|
-
docIdOffsets = fixedView(handles.docIdOffsets, Uint32Array);
|
|
438
|
-
diskFile = fixedView(handles.diskFile, Int32Array);
|
|
439
|
-
diskOffset = fixedView(handles.diskOffset, Uint32Array);
|
|
440
|
-
}
|
|
441
|
-
function blockAt(index) {
|
|
442
|
-
let block = blocks[index];
|
|
443
|
-
if (block === void 0) {
|
|
444
|
-
const handle = handles.blocks[index];
|
|
445
|
-
if (handle === null || handle === void 0) {
|
|
446
|
-
throw new Error(`Vector block ${index} holds no vectors on this thread`);
|
|
447
|
-
}
|
|
448
|
-
block = openVectorBlock(handle, threadSlot);
|
|
449
|
-
blocks[index] = block;
|
|
450
|
-
}
|
|
451
|
-
return block;
|
|
452
|
-
}
|
|
453
|
-
function codeBlockAt(index) {
|
|
454
|
-
let block = codeBlocks[index];
|
|
455
|
-
if (block === void 0) {
|
|
456
|
-
block = openVectorBlock(handles.codeBlocks[index], threadSlot);
|
|
457
|
-
codeBlocks[index] = block;
|
|
458
|
-
}
|
|
459
|
-
return block;
|
|
460
|
-
}
|
|
461
|
-
function fileOf(ordinal) {
|
|
462
|
-
if (ordinal >= diskFile.length) {
|
|
463
|
-
if (ordinal * 4 >= handles.diskFile.byteLength) return IN_MEMORY;
|
|
464
|
-
rebind();
|
|
465
|
-
}
|
|
466
|
-
return diskFile[ordinal];
|
|
467
|
-
}
|
|
468
|
-
function isCold(ordinal) {
|
|
469
|
-
return fileOf(ordinal) !== IN_MEMORY;
|
|
470
|
-
}
|
|
471
|
-
function holdState(ordinal) {
|
|
472
|
-
if (ordinal < 0) return NOT_HELD;
|
|
473
|
-
if (ordinal >= present.length) {
|
|
474
|
-
if (ordinal >= handles.present.byteLength) return NOT_HELD;
|
|
475
|
-
rebind();
|
|
476
|
-
}
|
|
477
|
-
if (present[ordinal] !== 1) return NOT_HELD;
|
|
478
|
-
if (isCold(ordinal)) return HELD_COLD;
|
|
479
|
-
const index = Math.floor(ordinal / capacity);
|
|
480
|
-
return index < handles.blocks.length && handles.blocks[index] !== null ? HELD_HOT : NOT_HELD;
|
|
481
|
-
}
|
|
482
|
-
function holds(ordinal) {
|
|
483
|
-
return holdState(ordinal) !== NOT_HELD;
|
|
484
|
-
}
|
|
485
|
-
function readFromFile(file, offset, target) {
|
|
486
|
-
const path = handles.vectorFiles[file];
|
|
487
|
-
if (path === void 0 || path.length === 0) return false;
|
|
488
|
-
if (reader === null) reader = createVectorFileReader();
|
|
489
|
-
const bytes = new Uint8Array(target.buffer, target.byteOffset, target.byteLength);
|
|
490
|
-
return reader.readInto(path, offset, bytes);
|
|
491
|
-
}
|
|
492
|
-
function readColdInto(ordinal, target) {
|
|
493
|
-
const file = fileOf(ordinal);
|
|
494
|
-
if (file === IN_MEMORY) return false;
|
|
495
|
-
if (ordinal >= diskOffset.length) rebind();
|
|
496
|
-
return readFromFile(file, diskOffset[ordinal], target);
|
|
497
|
-
}
|
|
498
|
-
function vectorIn(block, local) {
|
|
499
|
-
const base = slotByteOffset(layout, local) / 4;
|
|
500
|
-
return block.float32(base + dimension).subarray(base, base + dimension);
|
|
501
|
-
}
|
|
502
|
-
function hotVector(ordinal) {
|
|
503
|
-
return vectorIn(blockAt(ordinal / capacity | 0), ordinal % capacity);
|
|
504
|
-
}
|
|
505
|
-
function vectorFor(ordinal, scratch) {
|
|
506
|
-
if (!isCold(ordinal)) return hotVector(ordinal);
|
|
507
|
-
return readColdInto(ordinal, scratch) ? scratch : null;
|
|
508
|
-
}
|
|
509
|
-
function stageVector(block, ordinal) {
|
|
510
|
-
if (block.stagedOrdinal === ordinal) return true;
|
|
511
|
-
const source = vectorFor(ordinal, coldScratchA);
|
|
512
|
-
if (source === null) return false;
|
|
513
|
-
block.float32(block.scratchByteOffset / 4 + dimension).set(source, block.scratchByteOffset / 4);
|
|
514
|
-
block.stagedOrdinal = ordinal;
|
|
515
|
-
return true;
|
|
516
|
-
}
|
|
517
|
-
function hotBlockReads() {
|
|
518
|
-
const handle = handles.blocks[0];
|
|
519
|
-
if (handle === null || handle === void 0) return null;
|
|
520
|
-
const { slotsOffset, slotStride } = layout;
|
|
521
|
-
const limit = Math.min(present.length, diskFile.length, magnitudes.length, capacity);
|
|
522
|
-
return { block: blockAt(0), present, diskFile, magnitudes, limit, slotsOffset, slotStride, dimension };
|
|
523
|
-
}
|
|
524
|
-
function stageQuery(block) {
|
|
525
|
-
if (currentQuery === null) return false;
|
|
526
|
-
if (block.stagedOrdinal !== QUERY_STAGED) {
|
|
527
|
-
block.float32(block.scratchByteOffset / 4 + dimension).set(currentQuery, block.scratchByteOffset / 4);
|
|
528
|
-
block.stagedOrdinal = QUERY_STAGED;
|
|
529
|
-
}
|
|
530
|
-
return true;
|
|
531
|
-
}
|
|
532
|
-
const view = {
|
|
533
|
-
dimension,
|
|
534
|
-
threadSlot,
|
|
535
|
-
get handles() {
|
|
536
|
-
return handles;
|
|
537
|
-
},
|
|
538
|
-
get slots() {
|
|
539
|
-
return Atomics.load(handles.header, STORE_SLOTS);
|
|
540
|
-
},
|
|
541
|
-
get simdAvailable() {
|
|
542
|
-
const first = handles.blocks.find((block2) => block2 !== null);
|
|
543
|
-
if (first === void 0) return false;
|
|
544
|
-
const block = blockAt(handles.blocks.indexOf(first));
|
|
545
|
-
return block.simd !== null && block.hasScratch;
|
|
546
|
-
},
|
|
547
|
-
get holdsEveryBlock() {
|
|
548
|
-
return Atomics.load(handles.header, STORE_BLOCK_COUNT) <= handles.blocks.length && Atomics.load(handles.header, STORE_CODE_BLOCK_COUNT) <= handles.codeBlocks.length;
|
|
549
|
-
},
|
|
550
|
-
adoptHandles(next) {
|
|
551
|
-
detachNativeStore(handles);
|
|
552
|
-
handles = next;
|
|
553
|
-
rebind();
|
|
554
|
-
for (let index = 0; index < next.blocks.length; index++) {
|
|
555
|
-
if (next.blocks[index] === null) blocks[index] = void 0;
|
|
556
|
-
else if (blocks[index] === void 0) blockAt(index);
|
|
557
|
-
}
|
|
558
|
-
for (let index = codeBlocks.length; index < next.codeBlocks.length; index++) codeBlockAt(index);
|
|
559
|
-
reader?.retainOnly(new Set(next.vectorFiles.filter((path) => path.length > 0)));
|
|
560
|
-
},
|
|
561
|
-
rebind,
|
|
562
|
-
holdsOrdinal: holds,
|
|
563
|
-
isCold,
|
|
564
|
-
readColdInto,
|
|
565
|
-
readFromFile,
|
|
566
|
-
close() {
|
|
567
|
-
detachNativeStore(handles);
|
|
568
|
-
reader?.close();
|
|
569
|
-
reader = null;
|
|
570
|
-
},
|
|
571
|
-
blockOf(ordinal) {
|
|
572
|
-
return blockAt(ordinal / capacity | 0);
|
|
573
|
-
},
|
|
574
|
-
localOrdinal(ordinal) {
|
|
575
|
-
return ordinal % capacity;
|
|
576
|
-
},
|
|
577
|
-
codeBlockOf(ordinal) {
|
|
578
|
-
return codeBlockAt(ordinal / codeCapacity | 0);
|
|
579
|
-
},
|
|
580
|
-
codeLocalOrdinal(ordinal) {
|
|
581
|
-
return ordinal % codeCapacity;
|
|
582
|
-
},
|
|
583
|
-
openCodeBlocks() {
|
|
584
|
-
return codeBlocks;
|
|
585
|
-
},
|
|
586
|
-
vectorAt(ordinal) {
|
|
587
|
-
if (!isCold(ordinal)) return hotVector(ordinal);
|
|
588
|
-
const copy = new Float32Array(dimension);
|
|
589
|
-
if (!readColdInto(ordinal, copy)) {
|
|
590
|
-
throw new NarsilError(
|
|
591
|
-
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
592
|
-
`The vector at ordinal ${ordinal} is missing from its checkpoint file`,
|
|
593
|
-
{ ordinal, path: handles.vectorFiles[fileOf(ordinal)] }
|
|
594
|
-
);
|
|
595
|
-
}
|
|
596
|
-
return copy;
|
|
597
|
-
},
|
|
598
|
-
magnitudeAt(ordinal) {
|
|
599
|
-
if (ordinal >= magnitudes.length) rebind();
|
|
600
|
-
return magnitudes[ordinal];
|
|
601
|
-
},
|
|
602
|
-
entryForOrdinal(ordinal) {
|
|
603
|
-
if (!holds(ordinal)) return void 0;
|
|
604
|
-
return { vector: view.vectorAt(ordinal), magnitude: magnitudes[ordinal] };
|
|
605
|
-
},
|
|
606
|
-
docIdAt(ordinal) {
|
|
607
|
-
if (ordinal < 0 || ordinal >= view.slots) return void 0;
|
|
608
|
-
if (ordinal + 1 >= docIdOffsets.length) rebind();
|
|
609
|
-
const start = docIdOffsets[ordinal];
|
|
610
|
-
const end = docIdOffsets[ordinal + 1];
|
|
611
|
-
if (end <= start) return void 0;
|
|
612
|
-
if (end > docIdBytes.length) rebind();
|
|
613
|
-
return decoder.decode(docIdBytes.subarray(start, end));
|
|
614
|
-
},
|
|
615
|
-
partitionAt(ordinal) {
|
|
616
|
-
if (ordinal < 0) return void 0;
|
|
617
|
-
if (ordinal >= partitions.length) {
|
|
618
|
-
if (ordinal * 4 >= handles.partitions.byteLength) return void 0;
|
|
619
|
-
rebind();
|
|
620
|
-
}
|
|
621
|
-
const partition = partitions[ordinal];
|
|
622
|
-
return partition < 0 ? void 0 : partition;
|
|
623
|
-
},
|
|
624
|
-
distanceByOrdinal(ordA, ordB, metric) {
|
|
625
|
-
if (!holds(ordA) || !holds(ordB)) return Number.POSITIVE_INFINITY;
|
|
626
|
-
if (!isCold(ordB)) {
|
|
627
|
-
const indexA = ordA / capacity | 0;
|
|
628
|
-
const indexB = ordB / capacity | 0;
|
|
629
|
-
const blockB = blockAt(indexB);
|
|
630
|
-
const localB = ordB % capacity;
|
|
631
|
-
if (blockB.simd !== null) {
|
|
632
|
-
if (indexA === indexB && !isCold(ordA)) {
|
|
633
|
-
return arenaFloat32Distance(
|
|
634
|
-
blockB.simd,
|
|
635
|
-
slotByteOffset(layout, ordA % capacity),
|
|
636
|
-
slotByteOffset(layout, localB),
|
|
637
|
-
dimension,
|
|
638
|
-
metric,
|
|
639
|
-
magnitudes[ordA],
|
|
640
|
-
magnitudes[ordB]
|
|
641
|
-
);
|
|
642
|
-
}
|
|
643
|
-
if (blockB.hasScratch && stageVector(blockB, ordA)) {
|
|
644
|
-
return arenaFloat32Distance(
|
|
645
|
-
blockB.simd,
|
|
646
|
-
blockB.scratchByteOffset,
|
|
647
|
-
slotByteOffset(layout, localB),
|
|
648
|
-
dimension,
|
|
649
|
-
metric,
|
|
650
|
-
magnitudes[ordA],
|
|
651
|
-
magnitudes[ordB]
|
|
652
|
-
);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
const a = vectorFor(ordA, coldScratchA);
|
|
657
|
-
const b = vectorFor(ordB, coldScratchB);
|
|
658
|
-
if (a === null || b === null) return Number.POSITIVE_INFINITY;
|
|
659
|
-
return jsDistance(a, b, magnitudes[ordA], magnitudes[ordB], metric);
|
|
660
|
-
},
|
|
661
|
-
prepareQueryArena(query) {
|
|
662
|
-
if (query.length !== dimension || !view.simdAvailable) return null;
|
|
663
|
-
currentQuery = query;
|
|
664
|
-
for (const block of blocks) {
|
|
665
|
-
if (block !== void 0) block.stagedOrdinal = NOTHING_STAGED;
|
|
666
|
-
}
|
|
667
|
-
return { magnitude: magnitude(query) };
|
|
668
|
-
},
|
|
669
|
-
distanceFromArena(prepared, ordinal, metric) {
|
|
670
|
-
const state = holdState(ordinal);
|
|
671
|
-
if (state === NOT_HELD) return Number.POSITIVE_INFINITY;
|
|
672
|
-
if (currentQuery === null) return Number.POSITIVE_INFINITY;
|
|
673
|
-
if (state === HELD_COLD) {
|
|
674
|
-
if (!readColdInto(ordinal, coldScratchA)) return Number.POSITIVE_INFINITY;
|
|
675
|
-
return jsDistance(currentQuery, coldScratchA, prepared.magnitude, magnitudes[ordinal], metric);
|
|
676
|
-
}
|
|
677
|
-
const block = blockAt(ordinal / capacity | 0);
|
|
678
|
-
const local = ordinal % capacity;
|
|
679
|
-
if (block.simd !== null && block.hasScratch && stageQuery(block)) {
|
|
680
|
-
return arenaFloat32Distance(
|
|
681
|
-
block.simd,
|
|
682
|
-
block.scratchByteOffset,
|
|
683
|
-
slotByteOffset(layout, local),
|
|
684
|
-
dimension,
|
|
685
|
-
metric,
|
|
686
|
-
prepared.magnitude,
|
|
687
|
-
magnitudes[ordinal]
|
|
688
|
-
);
|
|
689
|
-
}
|
|
690
|
-
return jsDistance(currentQuery, vectorIn(block, local), prepared.magnitude, magnitudes[ordinal], metric);
|
|
691
|
-
},
|
|
692
|
-
hotBlockReads,
|
|
693
|
-
queryDistance: (prepared, metric) => queryDistanceOf(view, prepared, metric),
|
|
694
|
-
ordinalDistance: (from, metric) => ordinalDistanceOf(view, from, metric),
|
|
695
|
-
pairDistance: (metric) => pairDistanceOf(view, metric)
|
|
696
|
-
};
|
|
697
|
-
for (let index = 0; index < handles.blocks.length; index++) {
|
|
698
|
-
if (handles.blocks[index] !== null) blockAt(index);
|
|
699
|
-
}
|
|
700
|
-
for (let index = 0; index < handles.codeBlocks.length; index++) codeBlockAt(index);
|
|
701
|
-
return view;
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
// src/vector/shared-field/view.ts
|
|
705
|
-
function openSharedVectorField(initial, threadSlot) {
|
|
706
|
-
let handles = initial;
|
|
707
|
-
const store = openSharedVectorStore(initial.store, threadSlot);
|
|
708
|
-
const codeLayout = initial.store.codeLayout;
|
|
709
|
-
const quantizer = initial.quantization !== "none" && codeLayout !== null ? openSharedQuantizer(store, codeLayout, initial.metric) : void 0;
|
|
710
|
-
let graph = null;
|
|
711
|
-
let ordinals = null;
|
|
712
|
-
let scanned = 0;
|
|
713
|
-
let scannedLayoutGeneration = Atomics.load(store.handles.header, STORE_ORDINAL_LAYOUT_GENERATION);
|
|
714
|
-
let placed = 0;
|
|
715
|
-
function openGraph(next) {
|
|
716
|
-
graph = next.graph === null ? null : openGraphState(next.graph, next.dimension, store, quantizer, threadSlot);
|
|
717
|
-
}
|
|
718
|
-
openGraph(initial);
|
|
719
|
-
function docIdOf(ordinal) {
|
|
720
|
-
if (!store.holdsOrdinal(ordinal)) return void 0;
|
|
721
|
-
return store.docIdAt(ordinal);
|
|
722
|
-
}
|
|
723
|
-
function ordinalOf(docId) {
|
|
724
|
-
const layoutGeneration = Atomics.load(store.handles.header, STORE_ORDINAL_LAYOUT_GENERATION);
|
|
725
|
-
if (layoutGeneration !== scannedLayoutGeneration) {
|
|
726
|
-
ordinals = null;
|
|
727
|
-
scanned = 0;
|
|
728
|
-
scannedLayoutGeneration = layoutGeneration;
|
|
729
|
-
}
|
|
730
|
-
const slots = store.slots;
|
|
731
|
-
if (ordinals === null) ordinals = /* @__PURE__ */ new Map();
|
|
732
|
-
for (; scanned < slots; scanned++) {
|
|
733
|
-
const id = store.docIdAt(scanned);
|
|
734
|
-
if (id !== void 0) ordinals.set(id, scanned);
|
|
735
|
-
}
|
|
736
|
-
const ordinal = ordinals.get(docId);
|
|
737
|
-
return ordinal !== void 0 && store.holdsOrdinal(ordinal) ? ordinal : void 0;
|
|
738
|
-
}
|
|
739
|
-
return {
|
|
740
|
-
get handles() {
|
|
741
|
-
return handles;
|
|
742
|
-
},
|
|
743
|
-
store,
|
|
744
|
-
quantizer,
|
|
745
|
-
get graph() {
|
|
746
|
-
return graph;
|
|
747
|
-
},
|
|
748
|
-
get liveSize() {
|
|
749
|
-
return graph === null ? 0 : nodeCountOf(graph) - tombstoneCountOf(graph);
|
|
750
|
-
},
|
|
751
|
-
get readsEveryVector() {
|
|
752
|
-
return store.holdsEveryBlock;
|
|
753
|
-
},
|
|
754
|
-
get graphRetired() {
|
|
755
|
-
return graph !== null && Atomics.load(graph.header, GRAPH_RETIRED) === 1;
|
|
756
|
-
},
|
|
757
|
-
docIdOf,
|
|
758
|
-
ordinalOf,
|
|
759
|
-
vectorOf(docId) {
|
|
760
|
-
const ordinal = ordinalOf(docId);
|
|
761
|
-
return ordinal === void 0 ? void 0 : store.vectorAt(ordinal);
|
|
762
|
-
},
|
|
763
|
-
adopt(next) {
|
|
764
|
-
store.adoptHandles(next.store);
|
|
765
|
-
if (next.graph?.header !== handles.graph?.header) openGraph(next);
|
|
766
|
-
handles = next;
|
|
767
|
-
},
|
|
768
|
-
close() {
|
|
769
|
-
store.close();
|
|
770
|
-
},
|
|
771
|
-
insertOrdinal(ordinal) {
|
|
772
|
-
if (graph === null) return false;
|
|
773
|
-
const fresh = insertNode(graph, ordinal);
|
|
774
|
-
if (fresh) placed += 1;
|
|
775
|
-
return fresh;
|
|
776
|
-
},
|
|
777
|
-
takeOutcome() {
|
|
778
|
-
const outcome = { placed };
|
|
779
|
-
placed = 0;
|
|
780
|
-
return outcome;
|
|
781
|
-
},
|
|
782
|
-
searchOrdinals(query, k, metric, minSimilarity, options) {
|
|
783
|
-
if (graph === null) return [];
|
|
784
|
-
return searchOrdinals(graph, docIdOf, query, k, metric, minSimilarity, options);
|
|
785
|
-
}
|
|
786
|
-
};
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
// src/vector/vector-store/doc-ids.ts
|
|
790
|
-
var encoder = new TextEncoder();
|
|
791
|
-
function appendDocId(handles, ordinal, docId) {
|
|
792
|
-
const bytes = encoder.encode(docId);
|
|
793
|
-
const start = Atomics.load(handles.header, STORE_DOC_ID_BYTES);
|
|
794
|
-
const end = start + bytes.length;
|
|
795
|
-
growBufferTo(handles.docIdBytes, end);
|
|
796
|
-
growBufferTo(handles.docIdOffsets, (ordinal + 2) * 4);
|
|
797
|
-
new Uint8Array(handles.docIdBytes).set(bytes, start);
|
|
798
|
-
const offsets = new Uint32Array(handles.docIdOffsets);
|
|
799
|
-
offsets[ordinal] = start;
|
|
800
|
-
offsets[ordinal + 1] = end;
|
|
801
|
-
Atomics.store(handles.header, STORE_DOC_ID_BYTES, end);
|
|
802
|
-
}
|
|
803
|
-
function resetDocIds(handles) {
|
|
804
|
-
new Uint32Array(handles.docIdOffsets).fill(0);
|
|
805
|
-
Atomics.store(handles.header, STORE_DOC_ID_BYTES, 0);
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
// src/vector/vector-store/ordinal-reads.ts
|
|
809
|
-
function entryAt(store, ordinal) {
|
|
810
|
-
return { vector: store.view.vectorAt(ordinal), magnitude: store.magnitudes[ordinal] };
|
|
811
|
-
}
|
|
812
|
-
function createOrdinalReads(openStore, readable) {
|
|
813
|
-
function pairDistance(metric) {
|
|
814
|
-
const open = openStore();
|
|
815
|
-
if (open === null) return () => Number.POSITIVE_INFINITY;
|
|
816
|
-
const inner = open.view.pairDistance(metric);
|
|
817
|
-
return (ordA, ordB) => readable(ordA) && readable(ordB) ? inner(ordA, ordB) : Number.POSITIVE_INFINITY;
|
|
818
|
-
}
|
|
819
|
-
return {
|
|
820
|
-
entryForOrdinal(ordinal) {
|
|
821
|
-
const open = openStore();
|
|
822
|
-
if (open === null || !readable(ordinal)) return void 0;
|
|
823
|
-
return entryAt(open, ordinal);
|
|
824
|
-
},
|
|
825
|
-
distanceByOrdinal(ordA, ordB, metric) {
|
|
826
|
-
const open = openStore();
|
|
827
|
-
if (open === null || !readable(ordA) || !readable(ordB)) return Number.POSITIVE_INFINITY;
|
|
828
|
-
return open.view.distanceByOrdinal(ordA, ordB, metric);
|
|
829
|
-
},
|
|
830
|
-
distanceFromArena(prepared, ordinal, metric) {
|
|
831
|
-
const open = openStore();
|
|
832
|
-
if (open === null || !readable(ordinal)) return Number.POSITIVE_INFINITY;
|
|
833
|
-
return open.view.distanceFromArena(prepared, ordinal, metric);
|
|
834
|
-
},
|
|
835
|
-
queryDistance(prepared, metric) {
|
|
836
|
-
const open = openStore();
|
|
837
|
-
if (open === null) return () => Number.POSITIVE_INFINITY;
|
|
838
|
-
const inner = open.view.queryDistance(prepared, metric);
|
|
839
|
-
return (ordinal) => readable(ordinal) ? inner(ordinal) : Number.POSITIVE_INFINITY;
|
|
840
|
-
},
|
|
841
|
-
ordinalDistance(from, metric) {
|
|
842
|
-
const pair = pairDistance(metric);
|
|
843
|
-
return (ordinal) => pair(from, ordinal);
|
|
844
|
-
},
|
|
845
|
-
pairDistance
|
|
846
|
-
};
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
// src/vector/vector-store/listing.ts
|
|
850
|
-
function* entriesOf(open, documents) {
|
|
851
|
-
if (open === null) return;
|
|
852
|
-
for (let ordinal = 0; ordinal < documents.length; ordinal++) {
|
|
853
|
-
const docId = documents[ordinal];
|
|
854
|
-
if (docId === void 0) continue;
|
|
855
|
-
yield [docId, entryAt(open, ordinal)];
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
function* docIdsOf(open, documents) {
|
|
859
|
-
if (open === null) return;
|
|
860
|
-
for (let ordinal = 0; ordinal < documents.length; ordinal++) {
|
|
861
|
-
const docId = documents[ordinal];
|
|
862
|
-
if (docId !== void 0) yield docId;
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
function snapshotOf(open, documents, replacedOrdinals) {
|
|
866
|
-
const slots = documents.length;
|
|
867
|
-
const dimension = open === null ? 0 : open.handles.dimension;
|
|
868
|
-
const vectors = new Float32Array(slots * dimension);
|
|
869
|
-
const magnitudes = new Float64Array(slots);
|
|
870
|
-
const docIds = new Array(slots);
|
|
871
|
-
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
872
|
-
docIds[ordinal] = documents[ordinal] ?? null;
|
|
873
|
-
if (open === null || documents[ordinal] === void 0 && !replacedOrdinals.has(ordinal)) continue;
|
|
874
|
-
vectors.set(open.view.vectorAt(ordinal), ordinal * dimension);
|
|
875
|
-
magnitudes[ordinal] = open.magnitudes[ordinal];
|
|
876
|
-
}
|
|
877
|
-
return { dimension, slots, vectors, magnitudes, docIds, replacedOrdinals: [...replacedOrdinals] };
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
// src/vector/vector-store/open-store.ts
|
|
881
|
-
function openHandles(dimension, codeBits, blockBytes) {
|
|
882
|
-
const handles = createSharedVectorStoreHandles(dimension, codeBits, blockBytes);
|
|
883
|
-
return {
|
|
884
|
-
handles,
|
|
885
|
-
view: openSharedVectorStore(handles, 0),
|
|
886
|
-
magnitudes: new Float64Array(handles.magnitudes),
|
|
887
|
-
present: new Uint8Array(handles.present),
|
|
888
|
-
partitions: new Int32Array(handles.partitions),
|
|
889
|
-
diskFile: new Int32Array(handles.diskFile),
|
|
890
|
-
diskOffset: new Uint32Array(handles.diskOffset)
|
|
891
|
-
};
|
|
892
|
-
}
|
|
893
|
-
function relayout(store) {
|
|
894
|
-
store.handles.layoutRevision += 1;
|
|
895
|
-
store.view.adoptHandles(store.handles);
|
|
896
|
-
}
|
|
897
|
-
function claimSlot(store, docId, cold) {
|
|
898
|
-
const ordinal = Atomics.load(store.handles.header, STORE_SLOTS);
|
|
899
|
-
ensureOrdinal(store, ordinal, cold);
|
|
900
|
-
store.diskFile[ordinal] = IN_MEMORY;
|
|
901
|
-
appendDocId(store.handles, ordinal, docId);
|
|
902
|
-
return ordinal;
|
|
903
|
-
}
|
|
904
|
-
function publishOrdinal(store, ordinal) {
|
|
905
|
-
Atomics.store(store.handles.header, STORE_SLOTS, ordinal + 1);
|
|
906
|
-
Atomics.store(store.present, ordinal, 1);
|
|
907
|
-
}
|
|
908
|
-
function publishOnDisk(store, ordinal, vectorMagnitude, location) {
|
|
909
|
-
store.magnitudes[ordinal] = vectorMagnitude;
|
|
910
|
-
store.diskOffset[ordinal] = location.offset;
|
|
911
|
-
store.diskFile[ordinal] = location.fileIndex;
|
|
912
|
-
publishOrdinal(store, ordinal);
|
|
913
|
-
}
|
|
914
|
-
function relocateFiles(store, rewrite) {
|
|
915
|
-
const files = store.handles.vectorFiles;
|
|
916
|
-
if (files.length === 0) return;
|
|
917
|
-
for (let file = 0; file < files.length; file++) {
|
|
918
|
-
if (files[file].length > 0) files[file] = rewrite(files[file]);
|
|
919
|
-
}
|
|
920
|
-
relayout(store);
|
|
921
|
-
}
|
|
922
|
-
function ensureCodeSlot(store, ordinal) {
|
|
923
|
-
const { handles } = store;
|
|
924
|
-
if (handles.codeLayout === null) return;
|
|
925
|
-
const blockIndex = Math.floor(ordinal / handles.codeLayout.capacity);
|
|
926
|
-
while (handles.codeBlocks.length <= blockIndex) {
|
|
927
|
-
handles.codeBlocks.push(createVectorBlock(handles.codeLayout));
|
|
928
|
-
Atomics.store(handles.header, STORE_CODE_BLOCK_COUNT, handles.codeBlocks.length);
|
|
929
|
-
relayout(store);
|
|
930
|
-
}
|
|
931
|
-
ensureBlockSlots(handles.codeBlocks[blockIndex], ordinal % handles.codeLayout.capacity + 1);
|
|
932
|
-
}
|
|
933
|
-
function ensureFloatSlot(store, ordinal) {
|
|
934
|
-
const { handles } = store;
|
|
935
|
-
const blockIndex = Math.floor(ordinal / handles.layout.capacity);
|
|
936
|
-
let changed = false;
|
|
937
|
-
while (handles.blocks.length <= blockIndex) {
|
|
938
|
-
handles.blocks.push(null);
|
|
939
|
-
changed = true;
|
|
940
|
-
}
|
|
941
|
-
if (handles.blocks[blockIndex] === null) {
|
|
942
|
-
handles.blocks[blockIndex] = createVectorBlock(handles.layout);
|
|
943
|
-
changed = true;
|
|
944
|
-
}
|
|
945
|
-
if (changed) {
|
|
946
|
-
Atomics.store(handles.header, STORE_BLOCK_COUNT, handles.blocks.length);
|
|
947
|
-
relayout(store);
|
|
948
|
-
}
|
|
949
|
-
const block = handles.blocks[blockIndex];
|
|
950
|
-
if (block !== null) ensureBlockSlots(block, ordinal % handles.layout.capacity + 1);
|
|
951
|
-
}
|
|
952
|
-
function ensureOrdinal(store, ordinal, cold) {
|
|
953
|
-
if (ordinal >= MAX_VECTOR_ORDINALS) {
|
|
954
|
-
throw new NarsilError(
|
|
955
|
-
ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
|
|
956
|
-
`A vector field holds at most ${MAX_VECTOR_ORDINALS} vectors, deleted ones included, until it is rebuilt`,
|
|
957
|
-
{ ordinal, maxOrdinals: MAX_VECTOR_ORDINALS }
|
|
958
|
-
);
|
|
959
|
-
}
|
|
960
|
-
const { handles } = store;
|
|
961
|
-
growBufferTo(handles.magnitudes, (ordinal + 1) * 8);
|
|
962
|
-
growBufferTo(handles.present, ordinal + 1);
|
|
963
|
-
growBufferTo(handles.unplaced, ordinal + 1);
|
|
964
|
-
growBufferTo(handles.unplacedChunks, unplacedChunkCount(ordinal) * 4);
|
|
965
|
-
growBufferTo(handles.removed, ordinal + 1);
|
|
966
|
-
growBufferTo(handles.partitions, (ordinal + 1) * 4);
|
|
967
|
-
growBufferTo(handles.codePresent, ordinal + 1);
|
|
968
|
-
const diskFileBytes = handles.diskFile.byteLength;
|
|
969
|
-
growBufferTo(handles.diskFile, (ordinal + 1) * 4);
|
|
970
|
-
if (handles.diskFile.byteLength > diskFileBytes) {
|
|
971
|
-
new Int32Array(handles.diskFile).fill(IN_MEMORY, diskFileBytes / 4);
|
|
972
|
-
}
|
|
973
|
-
growBufferTo(handles.diskOffset, (ordinal + 1) * 4);
|
|
974
|
-
if (!cold) ensureFloatSlot(store, ordinal);
|
|
975
|
-
ensureCodeSlot(store, ordinal);
|
|
976
|
-
}
|
|
977
|
-
function writeVector(store, ordinal, vector) {
|
|
978
|
-
const block = store.view.blockOf(ordinal);
|
|
979
|
-
const base = slotByteOffset(store.handles.layout, store.view.localOrdinal(ordinal)) / 4;
|
|
980
|
-
block.float32(base + vector.length).set(vector, base);
|
|
981
|
-
store.magnitudes[ordinal] = magnitude(vector);
|
|
982
|
-
}
|
|
983
|
-
function blockFullyCold(store, blockIndex, documents, replacedOrdinals) {
|
|
984
|
-
const capacity = store.handles.layout.capacity;
|
|
985
|
-
const slots = Atomics.load(store.handles.header, STORE_SLOTS);
|
|
986
|
-
const end = Math.min(slots, (blockIndex + 1) * capacity);
|
|
987
|
-
if (end < (blockIndex + 1) * capacity) return false;
|
|
988
|
-
for (let ordinal = blockIndex * capacity; ordinal < end; ordinal++) {
|
|
989
|
-
const held = documents[ordinal] !== void 0 || replacedOrdinals.has(ordinal);
|
|
990
|
-
if (held && store.diskFile[ordinal] === IN_MEMORY) return false;
|
|
991
|
-
}
|
|
992
|
-
return true;
|
|
993
|
-
}
|
|
994
|
-
function fileHoldsSameBytes(store, ordinal, location) {
|
|
995
|
-
const fromFile = new Float32Array(store.handles.dimension);
|
|
996
|
-
if (!store.view.readFromFile(location.fileIndex, location.offset, fromFile)) return false;
|
|
997
|
-
const held = store.view.vectorAt(ordinal);
|
|
998
|
-
const a = new Uint8Array(fromFile.buffer);
|
|
999
|
-
const b = new Uint8Array(held.buffer, held.byteOffset, held.byteLength);
|
|
1000
|
-
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
1001
|
-
return true;
|
|
1002
|
-
}
|
|
1003
|
-
function forgetUnreferencedFiles(store, documents, replacedOrdinals) {
|
|
1004
|
-
const referenced = /* @__PURE__ */ new Set();
|
|
1005
|
-
for (let ordinal = 0; ordinal < documents.length; ordinal++) {
|
|
1006
|
-
const held = documents[ordinal] !== void 0 || replacedOrdinals.has(ordinal);
|
|
1007
|
-
if (held && store.diskFile[ordinal] !== IN_MEMORY) {
|
|
1008
|
-
referenced.add(store.diskFile[ordinal]);
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
let forgotten = 0;
|
|
1012
|
-
for (let file = 0; file < store.handles.vectorFiles.length; file++) {
|
|
1013
|
-
if (referenced.has(file) || store.handles.vectorFiles[file] === "") continue;
|
|
1014
|
-
store.handles.vectorFiles[file] = "";
|
|
1015
|
-
forgotten += 1;
|
|
1016
|
-
}
|
|
1017
|
-
return forgotten;
|
|
1018
|
-
}
|
|
1019
|
-
function partitionFilterOf(store, documents, partitionIds) {
|
|
1020
|
-
const filter = createOrdinalFilter(documents.length);
|
|
1021
|
-
if (store === null) return filter;
|
|
1022
|
-
let highest = -1;
|
|
1023
|
-
for (const partitionId of partitionIds) {
|
|
1024
|
-
if (partitionId > highest) highest = partitionId;
|
|
1025
|
-
}
|
|
1026
|
-
if (highest < 0) return filter;
|
|
1027
|
-
const wanted = new Uint8Array(highest + 1);
|
|
1028
|
-
for (const partitionId of partitionIds) {
|
|
1029
|
-
if (partitionId >= 0) wanted[partitionId] = 1;
|
|
1030
|
-
}
|
|
1031
|
-
for (let ordinal = 0; ordinal < documents.length; ordinal++) {
|
|
1032
|
-
if (documents[ordinal] === void 0) continue;
|
|
1033
|
-
const partitionId = store.partitions[ordinal];
|
|
1034
|
-
if (partitionId < 0 || partitionId > highest || wanted[partitionId] === 0) continue;
|
|
1035
|
-
addToOrdinalFilter(filter, ordinal);
|
|
1036
|
-
}
|
|
1037
|
-
return filter;
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
|
-
// src/vector/vector-store/index.ts
|
|
1041
|
-
var UNKNOWN_PARTITION = -1;
|
|
1042
|
-
var NO_PARTITION = -2;
|
|
1043
|
-
function createVectorStore(options) {
|
|
1044
|
-
const codeBits = options?.codeBits ?? null;
|
|
1045
|
-
const blockBytes = options?.blockBytes ?? BLOCK_MAX_BYTES;
|
|
1046
|
-
const docToOrd = /* @__PURE__ */ new Map();
|
|
1047
|
-
const ordToDoc = [];
|
|
1048
|
-
const replacedOrdinals = /* @__PURE__ */ new Set();
|
|
1049
|
-
let unknownPartitions = 0;
|
|
1050
|
-
let liveCount = 0;
|
|
1051
|
-
let open = options?.dimension === void 0 ? null : openHandles(options.dimension, codeBits, blockBytes);
|
|
1052
|
-
function forgetDocuments() {
|
|
1053
|
-
docToOrd.clear();
|
|
1054
|
-
ordToDoc.length = 0;
|
|
1055
|
-
replacedOrdinals.clear();
|
|
1056
|
-
unknownPartitions = 0;
|
|
1057
|
-
liveCount = 0;
|
|
1058
|
-
}
|
|
1059
|
-
function readable(ordinal) {
|
|
1060
|
-
return ordToDoc[ordinal] !== void 0 || replacedOrdinals.has(ordinal);
|
|
1061
|
-
}
|
|
1062
|
-
function requireOpen() {
|
|
1063
|
-
if (open === null) {
|
|
1064
|
-
throw new NarsilError(ErrorCodes.VECTOR_DIMENSION_MISMATCH, "The vector store holds no vector yet");
|
|
1065
|
-
}
|
|
1066
|
-
return open;
|
|
1067
|
-
}
|
|
1068
|
-
function recordPartition(store, ordinal, partitionId) {
|
|
1069
|
-
const given = partitionId !== void 0 && partitionId >= 0 ? partitionId : UNKNOWN_PARTITION;
|
|
1070
|
-
store.partitions[ordinal] = given;
|
|
1071
|
-
if (given === UNKNOWN_PARTITION) unknownPartitions += 1;
|
|
1072
|
-
}
|
|
1073
|
-
function retireOrdinal(store, ordinal) {
|
|
1074
|
-
ordToDoc[ordinal] = void 0;
|
|
1075
|
-
Atomics.store(store.present, ordinal, 0);
|
|
1076
|
-
if (store.partitions[ordinal] === UNKNOWN_PARTITION) unknownPartitions -= 1;
|
|
1077
|
-
}
|
|
1078
|
-
function keepReplacedOrdinal(store, ordinal) {
|
|
1079
|
-
ordToDoc[ordinal] = void 0;
|
|
1080
|
-
if (store.partitions[ordinal] === UNKNOWN_PARTITION) unknownPartitions -= 1;
|
|
1081
|
-
store.partitions[ordinal] = NO_PARTITION;
|
|
1082
|
-
replacedOrdinals.add(ordinal);
|
|
1083
|
-
}
|
|
1084
|
-
function restoreReplacedVector(store, ordinal, vector) {
|
|
1085
|
-
store.diskFile[ordinal] = IN_MEMORY;
|
|
1086
|
-
writeVector(store, ordinal, vector);
|
|
1087
|
-
Atomics.store(store.present, ordinal, 1);
|
|
1088
|
-
replacedOrdinals.add(ordinal);
|
|
1089
|
-
}
|
|
1090
|
-
function claimOrdinal(store, docId, cold, partitionId) {
|
|
1091
|
-
const ordinal = claimSlot(store, docId, cold);
|
|
1092
|
-
recordPartition(store, ordinal, partitionId);
|
|
1093
|
-
ordToDoc[ordinal] = docId;
|
|
1094
|
-
docToOrd.set(docId, ordinal);
|
|
1095
|
-
return ordinal;
|
|
1096
|
-
}
|
|
1097
|
-
function appendOrdinal(store, docId, vector, partitionId) {
|
|
1098
|
-
const ordinal = claimOrdinal(store, docId, false, partitionId);
|
|
1099
|
-
writeVector(store, ordinal, vector);
|
|
1100
|
-
publishOrdinal(store, ordinal);
|
|
1101
|
-
return ordinal;
|
|
1102
|
-
}
|
|
1103
|
-
function settleReplacement(store, ordinal, previous, keepsReplacedVector) {
|
|
1104
|
-
if (previous !== void 0) {
|
|
1105
|
-
if (keepsReplacedVector) keepReplacedOrdinal(store, previous);
|
|
1106
|
-
else retireOrdinal(store, previous);
|
|
1107
|
-
return ordinal;
|
|
1108
|
-
}
|
|
1109
|
-
liveCount++;
|
|
1110
|
-
Atomics.add(store.handles.header, STORE_LIVE_COUNT, 1);
|
|
1111
|
-
return ordinal;
|
|
1112
|
-
}
|
|
1113
|
-
return {
|
|
1114
|
-
...createOrdinalReads(() => open, readable),
|
|
1115
|
-
get size() {
|
|
1116
|
-
return liveCount;
|
|
1117
|
-
},
|
|
1118
|
-
get slots() {
|
|
1119
|
-
return ordToDoc.length;
|
|
1120
|
-
},
|
|
1121
|
-
get dimension() {
|
|
1122
|
-
return open === null ? 0 : open.handles.dimension;
|
|
1123
|
-
},
|
|
1124
|
-
get codeBits() {
|
|
1125
|
-
return codeBits;
|
|
1126
|
-
},
|
|
1127
|
-
get handles() {
|
|
1128
|
-
return requireOpen().handles;
|
|
1129
|
-
},
|
|
1130
|
-
get view() {
|
|
1131
|
-
return requireOpen().view;
|
|
1132
|
-
},
|
|
1133
|
-
get replacedVectorCount() {
|
|
1134
|
-
return replacedOrdinals.size;
|
|
1135
|
-
},
|
|
1136
|
-
insert(docId, vector, partitionId, keepsReplacedVector = false) {
|
|
1137
|
-
if (open === null) open = openHandles(vector.length, codeBits, blockBytes);
|
|
1138
|
-
const store = open;
|
|
1139
|
-
if (vector.length !== store.handles.dimension) {
|
|
1140
|
-
throw new NarsilError(
|
|
1141
|
-
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
1142
|
-
`Vector dimension mismatch: expected ${store.handles.dimension}, got ${vector.length}`,
|
|
1143
|
-
{ expected: store.handles.dimension, received: vector.length }
|
|
1144
|
-
);
|
|
1145
|
-
}
|
|
1146
|
-
const previous = docToOrd.get(docId);
|
|
1147
|
-
const ordinal = appendOrdinal(store, docId, vector, partitionId);
|
|
1148
|
-
return settleReplacement(store, ordinal, previous, keepsReplacedVector);
|
|
1149
|
-
},
|
|
1150
|
-
releaseReplacedVectors() {
|
|
1151
|
-
const released = [...replacedOrdinals];
|
|
1152
|
-
replacedOrdinals.clear();
|
|
1153
|
-
if (open !== null) for (const ordinal of released) Atomics.store(open.present, ordinal, 0);
|
|
1154
|
-
return released;
|
|
1155
|
-
},
|
|
1156
|
-
insertCold(docId, vectorMagnitude, location, partitionId) {
|
|
1157
|
-
const store = requireOpen();
|
|
1158
|
-
const previous = docToOrd.get(docId);
|
|
1159
|
-
Atomics.store(store.handles.header, STORE_RELEASED_VECTORS_TO_DISK, 1);
|
|
1160
|
-
const ordinal = claimOrdinal(store, docId, true, partitionId);
|
|
1161
|
-
publishOnDisk(store, ordinal, vectorMagnitude, location);
|
|
1162
|
-
return settleReplacement(store, ordinal, previous, false);
|
|
1163
|
-
},
|
|
1164
|
-
insertReplaced(docId, vector, location) {
|
|
1165
|
-
if (open === null) open = openHandles(vector.length, codeBits, blockBytes);
|
|
1166
|
-
const store = open;
|
|
1167
|
-
if (location !== void 0) Atomics.store(store.handles.header, STORE_RELEASED_VECTORS_TO_DISK, 1);
|
|
1168
|
-
const ordinal = claimSlot(store, docId, location !== void 0);
|
|
1169
|
-
store.partitions[ordinal] = NO_PARTITION;
|
|
1170
|
-
ordToDoc[ordinal] = void 0;
|
|
1171
|
-
replacedOrdinals.add(ordinal);
|
|
1172
|
-
if (location !== void 0) {
|
|
1173
|
-
publishOnDisk(store, ordinal, magnitude(vector), location);
|
|
1174
|
-
return ordinal;
|
|
1175
|
-
}
|
|
1176
|
-
writeVector(store, ordinal, vector);
|
|
1177
|
-
publishOrdinal(store, ordinal);
|
|
1178
|
-
return ordinal;
|
|
1179
|
-
},
|
|
1180
|
-
addVectorFile(path) {
|
|
1181
|
-
const store = requireOpen();
|
|
1182
|
-
const existing = store.handles.vectorFiles.indexOf(path);
|
|
1183
|
-
if (existing !== -1) return existing;
|
|
1184
|
-
store.handles.vectorFiles.push(path);
|
|
1185
|
-
relayout(store);
|
|
1186
|
-
return store.handles.vectorFiles.length - 1;
|
|
1187
|
-
},
|
|
1188
|
-
isCold(ordinal) {
|
|
1189
|
-
if (open === null) return false;
|
|
1190
|
-
return open.view.isCold(ordinal);
|
|
1191
|
-
},
|
|
1192
|
-
releaseToFile(ordinal, location) {
|
|
1193
|
-
const store = requireOpen();
|
|
1194
|
-
if (ordToDoc[ordinal] === void 0) return false;
|
|
1195
|
-
if (store.diskFile[ordinal] === IN_MEMORY && !fileHoldsSameBytes(store, ordinal, location)) return false;
|
|
1196
|
-
Atomics.store(store.handles.header, STORE_RELEASED_VECTORS_TO_DISK, 1);
|
|
1197
|
-
store.diskOffset[ordinal] = location.offset;
|
|
1198
|
-
store.diskFile[ordinal] = location.fileIndex;
|
|
1199
|
-
return true;
|
|
1200
|
-
},
|
|
1201
|
-
releaseColdBlocks() {
|
|
1202
|
-
const store = requireOpen();
|
|
1203
|
-
let released = 0;
|
|
1204
|
-
for (let index = 0; index < store.handles.blocks.length; index++) {
|
|
1205
|
-
if (store.handles.blocks[index] === null || !blockFullyCold(store, index, ordToDoc, replacedOrdinals)) continue;
|
|
1206
|
-
store.handles.blocks[index] = null;
|
|
1207
|
-
released += 1;
|
|
1208
|
-
}
|
|
1209
|
-
if (released + forgetUnreferencedFiles(store, ordToDoc, replacedOrdinals) > 0) relayout(store);
|
|
1210
|
-
return released;
|
|
1211
|
-
},
|
|
1212
|
-
setPartition(docId, partitionId) {
|
|
1213
|
-
const ordinal = docToOrd.get(docId);
|
|
1214
|
-
if (ordinal === void 0 || open === null || partitionId < 0) return;
|
|
1215
|
-
if (open.partitions[ordinal] === UNKNOWN_PARTITION) unknownPartitions -= 1;
|
|
1216
|
-
open.partitions[ordinal] = partitionId;
|
|
1217
|
-
},
|
|
1218
|
-
forgetPartition(docId) {
|
|
1219
|
-
const ordinal = docToOrd.get(docId);
|
|
1220
|
-
if (ordinal === void 0 || open === null) return;
|
|
1221
|
-
if (open.partitions[ordinal] === UNKNOWN_PARTITION) unknownPartitions -= 1;
|
|
1222
|
-
open.partitions[ordinal] = NO_PARTITION;
|
|
1223
|
-
},
|
|
1224
|
-
partitionOfOrdinal(ordinal) {
|
|
1225
|
-
if (open === null || ordinal < 0 || ordinal >= ordToDoc.length) return void 0;
|
|
1226
|
-
const partitionId = open.partitions[ordinal];
|
|
1227
|
-
return partitionId < 0 ? void 0 : partitionId;
|
|
1228
|
-
},
|
|
1229
|
-
get partitionsKnown() {
|
|
1230
|
-
return unknownPartitions === 0;
|
|
1231
|
-
},
|
|
1232
|
-
partitionFilter(partitionIds) {
|
|
1233
|
-
return partitionFilterOf(open, ordToDoc, partitionIds);
|
|
1234
|
-
},
|
|
1235
|
-
remove(docId) {
|
|
1236
|
-
const ordinal = docToOrd.get(docId);
|
|
1237
|
-
if (ordinal === void 0 || open === null) return;
|
|
1238
|
-
docToOrd.delete(docId);
|
|
1239
|
-
retireOrdinal(open, ordinal);
|
|
1240
|
-
liveCount--;
|
|
1241
|
-
Atomics.sub(open.handles.header, STORE_LIVE_COUNT, 1);
|
|
1242
|
-
},
|
|
1243
|
-
get(docId) {
|
|
1244
|
-
const ordinal = docToOrd.get(docId);
|
|
1245
|
-
return ordinal === void 0 || open === null ? void 0 : entryAt(open, ordinal);
|
|
1246
|
-
},
|
|
1247
|
-
has(docId) {
|
|
1248
|
-
return docToOrd.has(docId);
|
|
1249
|
-
},
|
|
1250
|
-
holdsOrdinal(ordinal) {
|
|
1251
|
-
return ordinal >= 0 && ordinal < ordToDoc.length && ordToDoc[ordinal] !== void 0;
|
|
1252
|
-
},
|
|
1253
|
-
entries: () => entriesOf(open, ordToDoc),
|
|
1254
|
-
docIds: () => docIdsOf(open, ordToDoc),
|
|
1255
|
-
clear() {
|
|
1256
|
-
forgetDocuments();
|
|
1257
|
-
if (open === null) return;
|
|
1258
|
-
open.present.fill(0);
|
|
1259
|
-
new Uint8Array(open.handles.codePresent).fill(0);
|
|
1260
|
-
new Int32Array(open.handles.diskFile).fill(IN_MEMORY);
|
|
1261
|
-
open.handles.centroid.fill(0);
|
|
1262
|
-
if (open.handles.vectorFiles.length > 0) {
|
|
1263
|
-
open.handles.vectorFiles.length = 0;
|
|
1264
|
-
relayout(open);
|
|
1265
|
-
}
|
|
1266
|
-
resetDocIds(open.handles);
|
|
1267
|
-
resetStoreHeader(open.handles.header);
|
|
1268
|
-
},
|
|
1269
|
-
release() {
|
|
1270
|
-
forgetDocuments();
|
|
1271
|
-
if (open !== null) Atomics.add(open.handles.header, STORE_ORDINAL_LAYOUT_GENERATION, 1);
|
|
1272
|
-
open?.view.close();
|
|
1273
|
-
open = null;
|
|
1274
|
-
},
|
|
1275
|
-
closeFiles: () => open?.view.close(),
|
|
1276
|
-
relocateVectorFiles(rewrite) {
|
|
1277
|
-
if (open !== null) relocateFiles(open, rewrite);
|
|
1278
|
-
},
|
|
1279
|
-
getOrdinal(docId) {
|
|
1280
|
-
return docToOrd.get(docId);
|
|
1281
|
-
},
|
|
1282
|
-
docIdForOrdinal(ordinal) {
|
|
1283
|
-
return ordToDoc[ordinal];
|
|
1284
|
-
},
|
|
1285
|
-
prepareQueryArena(query) {
|
|
1286
|
-
return open === null ? null : open.view.prepareQueryArena(query);
|
|
1287
|
-
},
|
|
1288
|
-
exportSnapshot: () => snapshotOf(open, ordToDoc, replacedOrdinals),
|
|
1289
|
-
restoreSnapshot(snapshot) {
|
|
1290
|
-
this.clear();
|
|
1291
|
-
if (snapshot.dimension === 0 || snapshot.slots === 0) return;
|
|
1292
|
-
if (open === null) open = openHandles(snapshot.dimension, codeBits, blockBytes);
|
|
1293
|
-
const store = open;
|
|
1294
|
-
const replaced = new Set(snapshot.replacedOrdinals);
|
|
1295
|
-
for (let ordinal = 0; ordinal < snapshot.slots; ordinal++) {
|
|
1296
|
-
const docId = snapshot.docIds[ordinal];
|
|
1297
|
-
const vector = snapshot.vectors.subarray(ordinal * snapshot.dimension, (ordinal + 1) * snapshot.dimension);
|
|
1298
|
-
if (docId === null || docId === void 0) {
|
|
1299
|
-
ensureOrdinal(store, ordinal, false);
|
|
1300
|
-
ordToDoc[ordinal] = void 0;
|
|
1301
|
-
store.partitions[ordinal] = NO_PARTITION;
|
|
1302
|
-
if (replaced.has(ordinal)) restoreReplacedVector(store, ordinal, vector);
|
|
1303
|
-
Atomics.store(store.handles.header, STORE_SLOTS, ordinal + 1);
|
|
1304
|
-
continue;
|
|
1305
|
-
}
|
|
1306
|
-
appendOrdinal(store, docId, vector);
|
|
1307
|
-
liveCount++;
|
|
1308
|
-
}
|
|
1309
|
-
Atomics.store(store.handles.header, STORE_LIVE_COUNT, liveCount);
|
|
1310
|
-
},
|
|
1311
|
-
memoryBytes() {
|
|
1312
|
-
return open === null ? 0 : sharedVectorStoreBytes(open.handles);
|
|
1313
|
-
}
|
|
1314
|
-
};
|
|
1315
|
-
}
|
|
1316
|
-
|
|
1317
|
-
// src/workers/resource-limits.ts
|
|
1318
|
-
function workerResourceLimits() {
|
|
1319
|
-
return { maxYoungGenerationSizeMb: WORKER_YOUNG_GENERATION_MB };
|
|
1320
|
-
}
|
|
1321
|
-
|
|
1322
|
-
// src/vector/search-pool/spawn.ts
|
|
1323
|
-
function resolveWorkerEntryPoint() {
|
|
1324
|
-
return resolveWorkerEntry(import.meta.url, /\/src\/vector\/search-pool\/[^/]+$/, "vector/search-worker.mjs");
|
|
1325
|
-
}
|
|
1326
|
-
async function spawnWorker(entryPoint) {
|
|
1327
|
-
if (entryPoint === null) return null;
|
|
1328
|
-
const runtime = detectRuntime();
|
|
1329
|
-
if (runtime.supportsWorkerThreads) {
|
|
1330
|
-
try {
|
|
1331
|
-
return await spawnNodeWorker(new URL(entryPoint), workerResourceLimits());
|
|
1332
|
-
} catch {
|
|
1333
|
-
return null;
|
|
1334
|
-
}
|
|
1335
|
-
}
|
|
1336
|
-
if (runtime.supportsWebWorkers) {
|
|
1337
|
-
try {
|
|
1338
|
-
const WorkerCtor = globalThis.Worker;
|
|
1339
|
-
if (typeof WorkerCtor !== "function") return null;
|
|
1340
|
-
return new WorkerCtor(entryPoint, { type: "module" });
|
|
1341
|
-
} catch {
|
|
1342
|
-
return null;
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1345
|
-
return null;
|
|
1346
|
-
}
|
|
1347
|
-
function listen(worker, handler) {
|
|
1348
|
-
if (typeof worker.on === "function") {
|
|
1349
|
-
worker.on("message", handler);
|
|
1350
|
-
return;
|
|
1351
|
-
}
|
|
1352
|
-
worker.addEventListener?.("message", (event) => {
|
|
1353
|
-
handler(event.data);
|
|
1354
|
-
});
|
|
1355
|
-
}
|
|
1356
|
-
function listenForFailure(worker, handler) {
|
|
1357
|
-
if (typeof worker.on === "function") {
|
|
1358
|
-
worker.on("error", (err) => handler(err instanceof Error ? err : new Error(String(err))));
|
|
1359
|
-
worker.on("exit", () => handler(new Error("Vector search worker exited")));
|
|
1360
|
-
return;
|
|
1361
|
-
}
|
|
1362
|
-
worker.addEventListener?.("error", () => handler(new Error("Vector search worker failed")));
|
|
1363
|
-
}
|
|
1364
|
-
|
|
1365
|
-
// src/vector/search-pool/index.ts
|
|
1366
|
-
function searchPoolScratchSlot(index) {
|
|
1367
|
-
return VECTOR_SCRATCH_SLOTS - 1 - index;
|
|
1368
|
-
}
|
|
1369
|
-
function graphSearchOptionsOf(options) {
|
|
1370
|
-
return {
|
|
1371
|
-
...options.filter !== void 0 ? { filter: options.filter } : {},
|
|
1372
|
-
...options.efSearch !== void 0 ? { efSearch: options.efSearch } : {},
|
|
1373
|
-
...options.oversample !== void 0 ? { oversample: options.oversample } : {},
|
|
1374
|
-
...options.rescoreWindow !== void 0 ? { rescoreWindow: options.rescoreWindow } : {}
|
|
1375
|
-
};
|
|
1376
|
-
}
|
|
1377
|
-
async function createVectorSearchPool(requestedCount) {
|
|
1378
|
-
const entryPoint = resolveWorkerEntryPoint();
|
|
1379
|
-
const count = resolveWorkerCount(requestedCount);
|
|
1380
|
-
const slots = [];
|
|
1381
|
-
const sharedFields = /* @__PURE__ */ new Map();
|
|
1382
|
-
for (let i = 0; i < count; i++) {
|
|
1383
|
-
const worker = await spawnWorker(entryPoint);
|
|
1384
|
-
if (worker === null) break;
|
|
1385
|
-
const slot = {
|
|
1386
|
-
worker,
|
|
1387
|
-
scratchSlot: searchPoolScratchSlot(i),
|
|
1388
|
-
pending: /* @__PURE__ */ new Map(),
|
|
1389
|
-
alive: true,
|
|
1390
|
-
outstanding: 0
|
|
1391
|
-
};
|
|
1392
|
-
listen(worker, (raw) => {
|
|
1393
|
-
const message = raw;
|
|
1394
|
-
const key = message.requestId;
|
|
1395
|
-
if (typeof key !== "string") return;
|
|
1396
|
-
const waiting = slot.pending.get(key);
|
|
1397
|
-
if (!waiting) return;
|
|
1398
|
-
slot.pending.delete(key);
|
|
1399
|
-
clearTimeout(waiting.timer);
|
|
1400
|
-
waiting.resolve(message);
|
|
1401
|
-
});
|
|
1402
|
-
listenForFailure(worker, (err) => {
|
|
1403
|
-
slot.alive = false;
|
|
1404
|
-
for (const handles of sharedFields.values()) {
|
|
1405
|
-
if (handles.graph !== null) releaseLocksHeldBy(handles.graph, slot.scratchSlot);
|
|
1406
|
-
}
|
|
1407
|
-
for (const [, waiting] of slot.pending) {
|
|
1408
|
-
clearTimeout(waiting.timer);
|
|
1409
|
-
waiting.reject(err);
|
|
1410
|
-
}
|
|
1411
|
-
slot.pending.clear();
|
|
1412
|
-
});
|
|
1413
|
-
slots.push(slot);
|
|
1414
|
-
}
|
|
1415
|
-
if (slots.length === 0) return null;
|
|
1416
|
-
let requestCounter = 0;
|
|
1417
|
-
function send(slot, key, message, timeoutMs) {
|
|
1418
|
-
return new Promise((resolve, reject) => {
|
|
1419
|
-
if (!slot.alive) {
|
|
1420
|
-
reject(new Error("Vector search worker is not running"));
|
|
1421
|
-
return;
|
|
1422
|
-
}
|
|
1423
|
-
const timer = setTimeout(() => {
|
|
1424
|
-
slot.pending.delete(key);
|
|
1425
|
-
reject(new Error(`Vector search worker did not answer within ${timeoutMs}ms`));
|
|
1426
|
-
}, timeoutMs);
|
|
1427
|
-
slot.pending.set(key, { resolve, reject, timer });
|
|
1428
|
-
try {
|
|
1429
|
-
slot.worker.postMessage(message);
|
|
1430
|
-
} catch (err) {
|
|
1431
|
-
slot.pending.delete(key);
|
|
1432
|
-
clearTimeout(timer);
|
|
1433
|
-
reject(err instanceof Error ? err : new Error(String(err)));
|
|
1434
|
-
}
|
|
1435
|
-
});
|
|
1436
|
-
}
|
|
1437
|
-
function refusalOf(message) {
|
|
1438
|
-
return message.type === "error" ? message.message : `the worker answered "${message.type}"`;
|
|
1439
|
-
}
|
|
1440
|
-
function pickSlot() {
|
|
1441
|
-
let slot = null;
|
|
1442
|
-
for (const candidate of slots) {
|
|
1443
|
-
if (!candidate.alive) continue;
|
|
1444
|
-
if (slot === null || candidate.outstanding < slot.outstanding) slot = candidate;
|
|
1445
|
-
if (slot.outstanding === 0) break;
|
|
1446
|
-
}
|
|
1447
|
-
return slot;
|
|
1448
|
-
}
|
|
1449
|
-
async function sendBusy(slot, request, timeoutMs) {
|
|
1450
|
-
slot.outstanding += 1;
|
|
1451
|
-
try {
|
|
1452
|
-
return await send(slot, request.requestId, request, timeoutMs);
|
|
1453
|
-
} finally {
|
|
1454
|
-
slot.outstanding -= 1;
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
async function broadcast(build, timeoutMs) {
|
|
1458
|
-
const outcomes = await Promise.allSettled(
|
|
1459
|
-
slots.map((slot) => {
|
|
1460
|
-
requestCounter += 1;
|
|
1461
|
-
const requestId = `${requestCounter}`;
|
|
1462
|
-
return send(slot, requestId, build(slot, requestId), timeoutMs);
|
|
1463
|
-
})
|
|
1464
|
-
);
|
|
1465
|
-
return outcomes.every(
|
|
1466
|
-
(outcome) => outcome.status === "fulfilled" && outcome.value.type === "ack"
|
|
1467
|
-
);
|
|
1468
|
-
}
|
|
1469
|
-
return {
|
|
1470
|
-
get workerCount() {
|
|
1471
|
-
return slots.filter((slot) => slot.alive).length;
|
|
1472
|
-
},
|
|
1473
|
-
load(handle, snapshot) {
|
|
1474
|
-
return broadcast(
|
|
1475
|
-
(_slot, requestId) => ({ type: "load", requestId, handle, snapshot }),
|
|
1476
|
-
VECTOR_SEARCH_LOAD_TIMEOUT_MS
|
|
1477
|
-
);
|
|
1478
|
-
},
|
|
1479
|
-
async loadShared(handle, handles) {
|
|
1480
|
-
sharedFields.set(handle, handles);
|
|
1481
|
-
const loaded = await broadcast(
|
|
1482
|
-
(slot, requestId) => ({ type: "loadShared", requestId, handle, scratchSlot: slot.scratchSlot, handles }),
|
|
1483
|
-
VECTOR_SEARCH_LOAD_TIMEOUT_MS
|
|
1484
|
-
);
|
|
1485
|
-
if (!loaded) sharedFields.delete(handle);
|
|
1486
|
-
return loaded;
|
|
1487
|
-
},
|
|
1488
|
-
async drop(handle) {
|
|
1489
|
-
sharedFields.delete(handle);
|
|
1490
|
-
await broadcast((_slot, requestId) => ({ type: "drop", requestId, handle }), VECTOR_SEARCH_TIMEOUT_MS);
|
|
1491
|
-
},
|
|
1492
|
-
async insertOrdinals(handle, ordinals) {
|
|
1493
|
-
const slot = pickSlot();
|
|
1494
|
-
if (slot === null) return null;
|
|
1495
|
-
requestCounter += 1;
|
|
1496
|
-
const request = { type: "insertOrdinals", requestId: `${requestCounter}`, handle, ordinals };
|
|
1497
|
-
const message = await sendBusy(slot, request, VECTOR_INSERT_TIMEOUT_MS);
|
|
1498
|
-
if (message.type !== "inserted") {
|
|
1499
|
-
console.warn("A vector worker placed no vectors in the shared graph:", refusalOf(message));
|
|
1500
|
-
return null;
|
|
1501
|
-
}
|
|
1502
|
-
return message.outcome;
|
|
1503
|
-
},
|
|
1504
|
-
async search(handle, query, k, metric, minSimilarity, options = {}) {
|
|
1505
|
-
const slot = pickSlot();
|
|
1506
|
-
if (slot === null) throw new Error("No vector search worker is running");
|
|
1507
|
-
requestCounter += 1;
|
|
1508
|
-
const request = {
|
|
1509
|
-
type: "search",
|
|
1510
|
-
requestId: `${requestCounter}`,
|
|
1511
|
-
handle,
|
|
1512
|
-
query,
|
|
1513
|
-
k,
|
|
1514
|
-
metric,
|
|
1515
|
-
minSimilarity,
|
|
1516
|
-
...graphSearchOptionsOf(options)
|
|
1517
|
-
};
|
|
1518
|
-
const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
|
|
1519
|
-
if (message.type === "error") throw new Error(message.message);
|
|
1520
|
-
if (message.type !== "result") throw new Error("Vector search worker returned an unexpected message");
|
|
1521
|
-
const results = new Array(message.docIds.length);
|
|
1522
|
-
for (let i = 0; i < message.docIds.length; i++) {
|
|
1523
|
-
results[i] = { docId: message.docIds[i], score: message.scores[i] };
|
|
1524
|
-
}
|
|
1525
|
-
return results;
|
|
1526
|
-
},
|
|
1527
|
-
async searchOrdinals(handle, query, k, metric, minSimilarity, options = {}) {
|
|
1528
|
-
const slot = pickSlot();
|
|
1529
|
-
if (slot === null) throw new Error("No vector search worker is running");
|
|
1530
|
-
requestCounter += 1;
|
|
1531
|
-
const request = {
|
|
1532
|
-
type: "searchOrdinals",
|
|
1533
|
-
requestId: `${requestCounter}`,
|
|
1534
|
-
handle,
|
|
1535
|
-
query,
|
|
1536
|
-
k,
|
|
1537
|
-
metric,
|
|
1538
|
-
minSimilarity,
|
|
1539
|
-
...graphSearchOptionsOf(options)
|
|
1540
|
-
};
|
|
1541
|
-
const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
|
|
1542
|
-
if (message.type === "error") throw new Error(message.message);
|
|
1543
|
-
if (message.type !== "ordinalResult") throw new Error("Vector search worker returned an unexpected message");
|
|
1544
|
-
return { ordinals: message.ordinals, scores: message.scores };
|
|
1545
|
-
},
|
|
1546
|
-
async shutdown() {
|
|
1547
|
-
for (const slot of slots) {
|
|
1548
|
-
slot.alive = false;
|
|
1549
|
-
for (const [, waiting] of slot.pending) {
|
|
1550
|
-
clearTimeout(waiting.timer);
|
|
1551
|
-
waiting.reject(new Error("Vector search pool has shut down"));
|
|
1552
|
-
}
|
|
1553
|
-
slot.pending.clear();
|
|
1554
|
-
try {
|
|
1555
|
-
await slot.worker.terminate();
|
|
1556
|
-
} catch {
|
|
1557
|
-
slot.alive = false;
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
}
|
|
1561
|
-
};
|
|
1562
|
-
}
|
|
1563
|
-
var sharedPool = null;
|
|
1564
|
-
var poolHolders = 0;
|
|
1565
|
-
var workersUnavailable = false;
|
|
1566
|
-
function acquireVectorSearchPool(requestedCount) {
|
|
1567
|
-
poolHolders += 1;
|
|
1568
|
-
if (workersUnavailable) return Promise.resolve(null);
|
|
1569
|
-
if (sharedPool === null) {
|
|
1570
|
-
sharedPool = createVectorSearchPool(requestedCount).then(
|
|
1571
|
-
(pool) => {
|
|
1572
|
-
if (pool === null) workersUnavailable = true;
|
|
1573
|
-
return pool;
|
|
1574
|
-
},
|
|
1575
|
-
() => {
|
|
1576
|
-
workersUnavailable = true;
|
|
1577
|
-
return null;
|
|
1578
|
-
}
|
|
1579
|
-
);
|
|
1580
|
-
}
|
|
1581
|
-
return sharedPool;
|
|
1582
|
-
}
|
|
1583
|
-
async function releaseVectorSearchPool() {
|
|
1584
|
-
if (poolHolders === 0) return;
|
|
1585
|
-
poolHolders -= 1;
|
|
1586
|
-
if (poolHolders > 0 || sharedPool === null) return;
|
|
1587
|
-
const pending = sharedPool;
|
|
1588
|
-
sharedPool = null;
|
|
1589
|
-
const pool = await pending;
|
|
1590
|
-
await pool?.shutdown();
|
|
1591
|
-
}
|
|
1592
|
-
|
|
1593
|
-
// src/vector/vector-index/worker-copies.ts
|
|
1594
|
-
var handleCounter = 0;
|
|
1595
|
-
function nextHandle(state) {
|
|
1596
|
-
handleCounter += 1;
|
|
1597
|
-
return `${state.indexName}/${state.fieldName}#${handleCounter}`;
|
|
1598
|
-
}
|
|
1599
|
-
async function dropHandle(state, handle) {
|
|
1600
|
-
const host = state.workerCopies.host;
|
|
1601
|
-
if (host !== void 0) {
|
|
1602
|
-
await host.drop(state.indexName, state.fieldName, handle).catch(() => void 0);
|
|
1603
|
-
return;
|
|
1604
|
-
}
|
|
1605
|
-
const pool = state.workerCopyPool;
|
|
1606
|
-
if (pool !== null) {
|
|
1607
|
-
await pool.drop(handle).catch(() => void 0);
|
|
1608
|
-
await releaseVectorSearchPool().catch(() => void 0);
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
|
-
function dropSharedGraph(state, graph) {
|
|
1612
|
-
const shared = state.sharedHandles.get(graph);
|
|
1613
|
-
if (shared === void 0) return;
|
|
1614
|
-
state.sharedHandles.delete(graph);
|
|
1615
|
-
if (state.workerCopyHandle === shared.handle) {
|
|
1616
|
-
state.workerCopyHandle = null;
|
|
1617
|
-
state.workerCopyRevision = -1;
|
|
1618
|
-
state.workerCopyMode = null;
|
|
1619
|
-
}
|
|
1620
|
-
void dropHandle(state, shared.handle);
|
|
1621
|
-
}
|
|
1622
|
-
function invalidateWorkerCopies(state) {
|
|
1623
|
-
state.revision += 1;
|
|
1624
|
-
for (const graph of [...state.sharedHandles.keys()]) dropSharedGraph(state, graph);
|
|
1625
|
-
if (state.workerCopyHandle !== null) {
|
|
1626
|
-
const handle = state.workerCopyHandle;
|
|
1627
|
-
state.workerCopyHandle = null;
|
|
1628
|
-
state.workerCopyRevision = -1;
|
|
1629
|
-
state.workerCopyMode = null;
|
|
1630
|
-
void dropHandle(state, handle);
|
|
1631
|
-
}
|
|
1632
|
-
if (state.workerCopies.host === void 0 && state.workerCopyPool !== null && state.sharedHandles.size === 0) {
|
|
1633
|
-
state.workerCopyPool = null;
|
|
1634
|
-
}
|
|
1635
|
-
}
|
|
1636
|
-
async function withdrawWorkerCopies(state) {
|
|
1637
|
-
state.revision += 1;
|
|
1638
|
-
const handles = /* @__PURE__ */ new Set();
|
|
1639
|
-
for (const shared of state.sharedHandles.values()) handles.add(shared.handle);
|
|
1640
|
-
if (state.workerCopyHandle !== null) handles.add(state.workerCopyHandle);
|
|
1641
|
-
state.sharedHandles.clear();
|
|
1642
|
-
state.workerCopyHandle = null;
|
|
1643
|
-
state.workerCopyRevision = -1;
|
|
1644
|
-
state.workerCopyMode = null;
|
|
1645
|
-
await Promise.all([...handles].map((handle) => dropHandle(state, handle)));
|
|
1646
|
-
if (state.workerCopies.host === void 0 && state.sharedHandles.size === 0) state.workerCopyPool = null;
|
|
1647
|
-
}
|
|
1648
|
-
async function holdWorkerCopies(state) {
|
|
1649
|
-
state.workerCopiesHeld = true;
|
|
1650
|
-
await state.sharing;
|
|
1651
|
-
await withdrawWorkerCopies(state);
|
|
1652
|
-
}
|
|
1653
|
-
function noteWrite(state) {
|
|
1654
|
-
state.revision += 1;
|
|
1655
|
-
if (state.workerCopyMode === "clone") invalidateWorkerCopies(state);
|
|
1656
|
-
}
|
|
1657
|
-
function refreshWorkerCopies(state) {
|
|
1658
|
-
invalidateWorkerCopies(state);
|
|
1659
|
-
scheduleWorkerCopyLoad(state);
|
|
1660
|
-
}
|
|
1661
|
-
function recordSharedField(state, record) {
|
|
1662
|
-
const { graph, handle, searchable, mode, revision, layoutRevision } = record;
|
|
1663
|
-
state.sharedHandles.set(graph, { handle, searchable, mode });
|
|
1664
|
-
state.sharedLayoutRevision = layoutRevision;
|
|
1665
|
-
if (!searchable) return;
|
|
1666
|
-
state.workerCopyHandle = handle;
|
|
1667
|
-
state.workerCopyRevision = revision;
|
|
1668
|
-
state.workerCopyMode = mode;
|
|
1669
|
-
}
|
|
1670
|
-
async function loadOnHost(state, host, graph, searchable) {
|
|
1671
|
-
if (!host.holdsIndex(state.indexName)) return null;
|
|
1672
|
-
if (!state.store.partitionsKnown) {
|
|
1673
|
-
assignStorePartitions(state, (docId) => host.resolvePartition(state.indexName, docId));
|
|
1674
|
-
}
|
|
1675
|
-
const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
|
|
1676
|
-
const layoutRevision = state.store.handles.layoutRevision;
|
|
1677
|
-
let loaded = false;
|
|
1678
|
-
try {
|
|
1679
|
-
loaded = await host.loadShared(state.indexName, state.fieldName, handle, fieldHandlesOf(state, graph, searchable));
|
|
1680
|
-
} catch {
|
|
1681
|
-
loaded = false;
|
|
1682
|
-
}
|
|
1683
|
-
if (!loaded || state.disposed) return null;
|
|
1684
|
-
recordSharedField(state, { graph, handle, searchable, mode: "hosted", revision: state.revision, layoutRevision });
|
|
1685
|
-
return handle;
|
|
1686
|
-
}
|
|
1687
|
-
function captureCloneCodes(state) {
|
|
1688
|
-
const quantizer = state.osq;
|
|
1689
|
-
const centroid = quantizer?.centroid ?? null;
|
|
1690
|
-
const layout = state.store.handles.codeLayout;
|
|
1691
|
-
if (quantizer === null || centroid === null || layout === null) return null;
|
|
1692
|
-
const slots = state.store.slots;
|
|
1693
|
-
const records = new Uint8Array(slots * layout.slotStride);
|
|
1694
|
-
const present = new Uint8Array(slots);
|
|
1695
|
-
for (let ordinal = 0; ordinal < slots; ordinal++) {
|
|
1696
|
-
const record = quantizer.recordAtOrdinal(ordinal);
|
|
1697
|
-
if (record === void 0) continue;
|
|
1698
|
-
records.set(record, ordinal * layout.slotStride);
|
|
1699
|
-
present[ordinal] = 1;
|
|
1700
|
-
}
|
|
1701
|
-
return { centroid: Float32Array.from(centroid), records, present };
|
|
1702
|
-
}
|
|
1703
|
-
function captureCloneSnapshot(state, graph) {
|
|
1704
|
-
return {
|
|
1705
|
-
dimension: state.dimension,
|
|
1706
|
-
quantization: state.quantizationMode,
|
|
1707
|
-
metric: state.metric,
|
|
1708
|
-
codes: captureCloneCodes(state),
|
|
1709
|
-
store: state.store.exportSnapshot(),
|
|
1710
|
-
graph: graph.exportSnapshot(),
|
|
1711
|
-
tombstones: Array.from(state.tombstones)
|
|
1712
|
-
};
|
|
1713
|
-
}
|
|
1714
|
-
async function loadOnPool(state, graph, searchable) {
|
|
1715
|
-
if (graph === null) return null;
|
|
1716
|
-
const revision = state.revision;
|
|
1717
|
-
const pool = state.workerCopyPool ?? await acquireVectorSearchPool(state.workerCopies.count);
|
|
1718
|
-
if (pool === null) {
|
|
1719
|
-
await releaseVectorSearchPool();
|
|
1720
|
-
return null;
|
|
1721
|
-
}
|
|
1722
|
-
if (state.disposed) {
|
|
1723
|
-
await releaseVectorSearchPool();
|
|
1724
|
-
return null;
|
|
1725
|
-
}
|
|
1726
|
-
state.workerCopyPool = pool;
|
|
1727
|
-
const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
|
|
1728
|
-
const layoutRevision = state.store.handles.layoutRevision;
|
|
1729
|
-
let mode = "shared";
|
|
1730
|
-
let loaded = false;
|
|
1731
|
-
try {
|
|
1732
|
-
if (sharedMemoryAvailable()) {
|
|
1733
|
-
loaded = await pool.loadShared(handle, fieldHandlesOf(state, graph, searchable));
|
|
1734
|
-
}
|
|
1735
|
-
if (!loaded && searchable) {
|
|
1736
|
-
mode = "clone";
|
|
1737
|
-
loaded = await pool.load(handle, captureCloneSnapshot(state, graph));
|
|
1738
|
-
}
|
|
1739
|
-
} catch {
|
|
1740
|
-
loaded = false;
|
|
1741
|
-
}
|
|
1742
|
-
if (!loaded || state.disposed || mode === "clone" && state.revision !== revision) {
|
|
1743
|
-
await pool.drop(handle).catch(() => void 0);
|
|
1744
|
-
return null;
|
|
1745
|
-
}
|
|
1746
|
-
recordSharedField(state, { graph, handle, searchable, mode, revision, layoutRevision });
|
|
1747
|
-
return handle;
|
|
1748
|
-
}
|
|
1749
|
-
function shareGraph(state, graph, searchable) {
|
|
1750
|
-
const run = state.sharing.then(async () => {
|
|
1751
|
-
if (!state.workerCopies.enabled || state.disposed || state.workerCopiesHeld) return null;
|
|
1752
|
-
const existing = state.sharedHandles.get(graph);
|
|
1753
|
-
const layoutUnchanged = threadsHoldCurrentLayout(state);
|
|
1754
|
-
if (existing !== void 0 && layoutUnchanged && (existing.searchable || !searchable)) return existing.handle;
|
|
1755
|
-
const host = state.workerCopies.host;
|
|
1756
|
-
return host !== void 0 ? loadOnHost(state, host, graph, searchable) : loadOnPool(state, graph, searchable);
|
|
1757
|
-
});
|
|
1758
|
-
state.sharing = run.then(
|
|
1759
|
-
() => void 0,
|
|
1760
|
-
() => void 0
|
|
1761
|
-
);
|
|
1762
|
-
return run;
|
|
1763
|
-
}
|
|
1764
|
-
function resendSharedHandles(state) {
|
|
1765
|
-
const run = state.sharing.then(async () => {
|
|
1766
|
-
if (!state.workerCopies.enabled || state.disposed || state.workerCopiesHeld) return;
|
|
1767
|
-
const host = state.workerCopies.host;
|
|
1768
|
-
for (const [graph, shared] of [...state.sharedHandles]) {
|
|
1769
|
-
if (host !== void 0) await loadOnHost(state, host, graph, shared.searchable);
|
|
1770
|
-
else await loadOnPool(state, graph, shared.searchable);
|
|
1771
|
-
}
|
|
1772
|
-
});
|
|
1773
|
-
state.sharing = run.then(
|
|
1774
|
-
() => void 0,
|
|
1775
|
-
() => void 0
|
|
1776
|
-
);
|
|
1777
|
-
return run;
|
|
1778
|
-
}
|
|
1779
|
-
function scheduleWorkerCopyLoad(state) {
|
|
1780
|
-
if (!state.workerCopies.enabled || state.workerCopiesHeld) return;
|
|
1781
|
-
if (state.disposed || state.workerCopyLoading || state.building) return;
|
|
1782
|
-
const graph = state.hnsw;
|
|
1783
|
-
const host = state.workerCopies.host;
|
|
1784
|
-
if (graph === null && host === void 0) return;
|
|
1785
|
-
const existing = state.sharedHandles.get(graph);
|
|
1786
|
-
if (existing?.searchable && threadsHoldCurrentLayout(state)) return;
|
|
1787
|
-
if (host === void 0 && liveSize(state) < WORKER_COPY_MIN_VECTORS) return;
|
|
1788
|
-
state.workerCopyLoading = true;
|
|
1789
|
-
void shareGraph(state, graph, true).then(
|
|
1790
|
-
() => {
|
|
1791
|
-
state.workerCopyLoading = false;
|
|
1792
|
-
},
|
|
1793
|
-
() => {
|
|
1794
|
-
state.workerCopyLoading = false;
|
|
1795
|
-
}
|
|
1796
|
-
);
|
|
1797
|
-
}
|
|
1798
|
-
async function searchViaWorkerCopies(state, query, k, metric, minSimilarity, options) {
|
|
1799
|
-
const pool = state.workerCopyPool;
|
|
1800
|
-
const handle = state.workerCopyHandle;
|
|
1801
|
-
if (pool === null || handle === null || state.workerCopyMode === "hosted") return null;
|
|
1802
|
-
if (state.workerCopyMode === "clone" && state.workerCopyRevision !== state.revision) return null;
|
|
1803
|
-
try {
|
|
1804
|
-
if (state.workerCopyMode === "shared") {
|
|
1805
|
-
const outcome = await pool.searchOrdinals(handle, query, k, metric, minSimilarity, options);
|
|
1806
|
-
const results = [];
|
|
1807
|
-
for (let i = 0; i < outcome.ordinals.length; i++) {
|
|
1808
|
-
const docId = state.store.docIdForOrdinal(outcome.ordinals[i]);
|
|
1809
|
-
if (docId === void 0) continue;
|
|
1810
|
-
results.push({ docId, score: outcome.scores[i] });
|
|
1811
|
-
}
|
|
1812
|
-
return results;
|
|
1813
|
-
}
|
|
1814
|
-
return await pool.search(handle, query, k, metric, minSimilarity, options);
|
|
1815
|
-
} catch {
|
|
1816
|
-
return null;
|
|
1817
|
-
}
|
|
1818
|
-
}
|
|
1819
|
-
|
|
1820
|
-
// src/vector/vector-index/build-host.ts
|
|
1821
|
-
async function dispatcherFor(state, graph) {
|
|
1822
|
-
if (!state.workerCopies.enabled || state.disposed) return null;
|
|
1823
|
-
const host = state.workerCopies.host;
|
|
1824
|
-
if (host !== void 0) {
|
|
1825
|
-
if (!host.holdsIndex(state.indexName)) return null;
|
|
1826
|
-
const handle2 = await shareGraph(state, graph, graph === state.hnsw);
|
|
1827
|
-
if (handle2 === null) return null;
|
|
1828
|
-
return {
|
|
1829
|
-
send: (ordinals) => host.insertOrdinals(state.indexName, state.fieldName, handle2, ordinals),
|
|
1830
|
-
workerCount: Math.max(1, host.workerCount)
|
|
1831
|
-
};
|
|
1832
|
-
}
|
|
1833
|
-
if (liveSize(state) < WORKER_COPY_MIN_VECTORS || !sharedMemoryAvailable()) return null;
|
|
1834
|
-
const handle = await shareGraph(state, graph, graph === state.hnsw);
|
|
1835
|
-
const pool = state.workerCopyPool;
|
|
1836
|
-
if (handle === null || pool === null || state.sharedHandles.get(graph)?.mode !== "shared") return null;
|
|
1837
|
-
return {
|
|
1838
|
-
send: (ordinals) => pool.insertOrdinals(handle, ordinals),
|
|
1839
|
-
workerCount: Math.max(1, pool.workerCount)
|
|
1840
|
-
};
|
|
1841
|
-
}
|
|
1842
|
-
async function insertIntoGraph(state, graph, docIds, admit) {
|
|
1843
|
-
const found = await dispatcherFor(state, graph);
|
|
1844
|
-
if (found === null) return insertLocally(state, graph, docIds, admit);
|
|
1845
|
-
const dispatch = found;
|
|
1846
|
-
const pending = /* @__PURE__ */ new Set();
|
|
1847
|
-
const limit = dispatch.workerCount * GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER;
|
|
1848
|
-
const fallback = { noWorkerWarned: false, shortAnswerWarned: false };
|
|
1849
|
-
let batchDocIds = [];
|
|
1850
|
-
let batchOrdinals = [];
|
|
1851
|
-
async function flush() {
|
|
1852
|
-
if (batchOrdinals.length === 0) return;
|
|
1853
|
-
const chunkDocIds = batchDocIds;
|
|
1854
|
-
const chunkOrdinals = Int32Array.from(batchOrdinals);
|
|
1855
|
-
batchDocIds = [];
|
|
1856
|
-
batchOrdinals = [];
|
|
1857
|
-
if (!threadsHoldCurrentLayout(state)) await shareGraph(state, graph, graph === state.hnsw);
|
|
1858
|
-
const run = placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback).finally(() => {
|
|
1859
|
-
pending.delete(run);
|
|
1860
|
-
});
|
|
1861
|
-
pending.add(run);
|
|
1862
|
-
if (pending.size >= limit) await Promise.race(pending);
|
|
1863
|
-
}
|
|
1864
|
-
for (const docId of docIds) {
|
|
1865
|
-
if (state.disposed) break;
|
|
1866
|
-
if (!admit(docId)) continue;
|
|
1867
|
-
const ordinal = state.store.getOrdinal(docId);
|
|
1868
|
-
if (ordinal === void 0) continue;
|
|
1869
|
-
batchDocIds.push(docId);
|
|
1870
|
-
batchOrdinals.push(ordinal);
|
|
1871
|
-
if (batchOrdinals.length >= GRAPH_BUILD_DISPATCH_CHUNK) await flush();
|
|
1872
|
-
}
|
|
1873
|
-
await flush();
|
|
1874
|
-
await Promise.all(pending);
|
|
1875
|
-
return !state.disposed;
|
|
1876
|
-
}
|
|
1877
|
-
async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback) {
|
|
1878
|
-
let outcome = null;
|
|
1879
|
-
try {
|
|
1880
|
-
outcome = await dispatch.send(chunkOrdinals);
|
|
1881
|
-
} catch {
|
|
1882
|
-
outcome = null;
|
|
1883
|
-
}
|
|
1884
|
-
if (state.disposed) return;
|
|
1885
|
-
if (outcome === null) {
|
|
1886
|
-
if (!fallback.noWorkerWarned) {
|
|
1887
|
-
fallback.noWorkerWarned = true;
|
|
1888
|
-
console.warn(
|
|
1889
|
-
`No worker thread placed the vectors of "${state.indexName}/${state.fieldName}", so this thread is building its graph itself`
|
|
1890
|
-
);
|
|
1891
|
-
}
|
|
1892
|
-
for (const ordinal of chunkOrdinals) graph.insertOrdinal(ordinal);
|
|
1893
|
-
}
|
|
1894
|
-
let placedHere = 0;
|
|
1895
|
-
for (let i = 0; i < chunkDocIds.length; i++) {
|
|
1896
|
-
const docId = chunkDocIds[i];
|
|
1897
|
-
const ordinal = chunkOrdinals[i];
|
|
1898
|
-
if (state.store.getOrdinal(docId) !== ordinal) {
|
|
1899
|
-
graph.markTombstoneOrdinal(ordinal);
|
|
1900
|
-
continue;
|
|
1901
|
-
}
|
|
1902
|
-
if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
|
|
1903
|
-
if (graph === state.hnsw) state.buffer.delete(docId);
|
|
1904
|
-
}
|
|
1905
|
-
if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
|
|
1906
|
-
fallback.shortAnswerWarned = true;
|
|
1907
|
-
console.warn(
|
|
1908
|
-
`A worker thread answered a chunk of "${state.indexName}/${state.fieldName}" without placing ${placedHere} of its vectors, so this thread placed them itself`
|
|
1909
|
-
);
|
|
1910
|
-
}
|
|
1911
|
-
}
|
|
1912
|
-
async function insertLocally(state, graph, docIds, admit) {
|
|
1913
|
-
let count = 0;
|
|
1914
|
-
for (const docId of docIds) {
|
|
1915
|
-
if (state.disposed) return false;
|
|
1916
|
-
if (!admit(docId)) continue;
|
|
1917
|
-
const ordinal = state.store.getOrdinal(docId);
|
|
1918
|
-
if (ordinal === void 0) continue;
|
|
1919
|
-
graph.insertOrdinal(ordinal);
|
|
1920
|
-
if (graph === state.hnsw) state.buffer.delete(docId);
|
|
1921
|
-
count += 1;
|
|
1922
|
-
if (count % BUILD_CHUNK_SIZE === 0) {
|
|
1923
|
-
noteWrite(state);
|
|
1924
|
-
await yieldToEventLoop();
|
|
1925
|
-
}
|
|
1926
|
-
}
|
|
1927
|
-
if (count > 0) noteWrite(state);
|
|
1928
|
-
return !state.disposed;
|
|
1929
|
-
}
|
|
1930
|
-
|
|
1931
|
-
// src/vector/vector-index/disk.ts
|
|
1932
|
-
function readsFromDisk(state) {
|
|
1933
|
-
return state.storage === "disk";
|
|
1934
|
-
}
|
|
1935
|
-
function holdsVectorsOnDisk(state) {
|
|
1936
|
-
return readsFromDisk(state) && state.hnsw !== null;
|
|
1937
|
-
}
|
|
1938
|
-
function* locationsOf(state, layout) {
|
|
1939
|
-
const stride = state.dimension * 4;
|
|
1940
|
-
for (let i = 0; i < layout.docIds.length; i++) {
|
|
1941
|
-
if (isDead(layout.dead, i)) continue;
|
|
1942
|
-
yield [layout.docIds[i], { path: layout.path, offset: layout.vectorsOffset + i * stride }];
|
|
1943
|
-
}
|
|
1944
|
-
}
|
|
1945
|
-
async function shareFilesWithEveryThread(state, paths) {
|
|
1946
|
-
const fileIndexes = /* @__PURE__ */ new Map();
|
|
1947
|
-
for (const path of paths) fileIndexes.set(path, state.store.addVectorFile(path));
|
|
1948
|
-
await resendSharedHandles(state);
|
|
1949
|
-
return fileIndexes;
|
|
1950
|
-
}
|
|
1951
|
-
async function releaseLocations(state, paths, locations) {
|
|
1952
|
-
const fileIndexes = await shareFilesWithEveryThread(state, paths);
|
|
1953
|
-
if (state.disposed) return;
|
|
1954
|
-
let count = 0;
|
|
1955
|
-
for (const [docId, location] of locations) {
|
|
1956
|
-
if (count > 0 && count % DISK_ADOPTION_YIELD_INTERVAL === 0) {
|
|
1957
|
-
await yieldToEventLoop();
|
|
1958
|
-
if (state.disposed) return;
|
|
1959
|
-
}
|
|
1960
|
-
count += 1;
|
|
1961
|
-
const ordinal = state.store.getOrdinal(docId);
|
|
1962
|
-
const fileIndex = fileIndexes.get(location.path);
|
|
1963
|
-
if (ordinal === void 0 || fileIndex === void 0) continue;
|
|
1964
|
-
state.store.releaseToFile(ordinal, { fileIndex, offset: location.offset });
|
|
1965
|
-
}
|
|
1966
|
-
state.store.releaseColdBlocks();
|
|
1967
|
-
await resendSharedHandles(state);
|
|
1968
|
-
}
|
|
1969
|
-
function releaseInTurn(state, release) {
|
|
1970
|
-
const run = state.releaseToFilesInFlight.then(release);
|
|
1971
|
-
state.releaseToFilesInFlight = run.then(
|
|
1972
|
-
() => void 0,
|
|
1973
|
-
() => void 0
|
|
1974
|
-
);
|
|
1975
|
-
return run;
|
|
1976
|
-
}
|
|
1977
|
-
async function adoptDiskLayout(state, layout) {
|
|
1978
|
-
if (!readsFromDisk(state) || state.disposed || layout.docIds.length === 0) return;
|
|
1979
|
-
if (!holdsVectorsOnDisk(state)) {
|
|
1980
|
-
for (const [docId, location] of locationsOf(state, layout)) {
|
|
1981
|
-
if (state.store.has(docId)) state.pendingLocations.set(docId, location);
|
|
1982
|
-
}
|
|
1983
|
-
return;
|
|
1984
|
-
}
|
|
1985
|
-
await releaseInTurn(state, () => releaseLocations(state, [layout.path], locationsOf(state, layout)));
|
|
1986
|
-
}
|
|
1987
|
-
async function releasePendingLocations(state) {
|
|
1988
|
-
if (state.pendingLocations.size === 0 || !holdsVectorsOnDisk(state) || state.disposed) return;
|
|
1989
|
-
const pending = [...state.pendingLocations];
|
|
1990
|
-
state.pendingLocations.clear();
|
|
1991
|
-
const paths = new Set(pending.map(([, location]) => location.path));
|
|
1992
|
-
await releaseInTurn(state, () => releaseLocations(state, paths, pending));
|
|
1993
|
-
}
|
|
1994
|
-
|
|
1995
|
-
// src/vector/vector-index/build.ts
|
|
1996
|
-
async function buildGraphFromStore(state) {
|
|
1997
|
-
const graph = createHNSWIndex(state.dimension, state.store, state.hnswConfig, state.osq ?? void 0);
|
|
1998
|
-
state.freshGraph = graph;
|
|
1999
|
-
let completed = false;
|
|
2000
|
-
try {
|
|
2001
|
-
completed = await insertIntoGraph(state, graph, [...allLiveDocIds(state)], () => true);
|
|
2002
|
-
} finally {
|
|
2003
|
-
state.freshGraph = null;
|
|
2004
|
-
}
|
|
2005
|
-
if (!completed) {
|
|
2006
|
-
dropSharedGraph(state, graph);
|
|
2007
|
-
return;
|
|
2008
|
-
}
|
|
2009
|
-
const previous = state.hnsw;
|
|
2010
|
-
adoptGraph(state, graph);
|
|
2011
|
-
for (const docId of [...state.buffer]) {
|
|
2012
|
-
const ordinal = state.store.getOrdinal(docId);
|
|
2013
|
-
if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) state.buffer.delete(docId);
|
|
2014
|
-
}
|
|
2015
|
-
if (previous !== null && previous !== graph) dropSharedGraph(state, previous);
|
|
2016
|
-
await releasePendingLocations(state);
|
|
2017
|
-
}
|
|
2018
|
-
async function promoteToGraph(state) {
|
|
2019
|
-
if (liveSize(state) === 0) return;
|
|
2020
|
-
calibrateQuantizer(state);
|
|
2021
|
-
await buildGraphFromStore(state);
|
|
2022
|
-
}
|
|
2023
|
-
async function growGraph(state, graph, bufferSnapshot) {
|
|
2024
|
-
const admit = (docId) => {
|
|
2025
|
-
if (state.hnsw !== graph) return false;
|
|
2026
|
-
if (state.tombstones.has(docId) || !state.store.has(docId)) {
|
|
2027
|
-
state.buffer.delete(docId);
|
|
2028
|
-
return false;
|
|
2029
|
-
}
|
|
2030
|
-
return true;
|
|
2031
|
-
};
|
|
2032
|
-
if (state.osq && !state.osq.isCalibrated()) calibrateQuantizer(state);
|
|
2033
|
-
await insertIntoGraph(state, graph, bufferSnapshot, admit);
|
|
2034
|
-
}
|
|
2035
|
-
function triggerBuild(state) {
|
|
2036
|
-
if (state.building) return;
|
|
2037
|
-
state.building = true;
|
|
2038
|
-
const bufferSnapshot = new Set(state.buffer);
|
|
2039
|
-
const existingGraph = state.hnsw;
|
|
2040
|
-
const buildPromise = (async () => {
|
|
2041
|
-
try {
|
|
2042
|
-
if (state.disposed) return;
|
|
2043
|
-
if (existingGraph !== null) {
|
|
2044
|
-
await growGraph(state, existingGraph, bufferSnapshot);
|
|
2045
|
-
return;
|
|
2046
|
-
}
|
|
2047
|
-
await promoteToGraph(state);
|
|
2048
|
-
} finally {
|
|
2049
|
-
state.building = false;
|
|
2050
|
-
state.pendingBuild = null;
|
|
2051
|
-
if (state.buffer.size > 0) {
|
|
2052
|
-
scheduleBuild(state);
|
|
2053
|
-
} else {
|
|
2054
|
-
scheduleWorkerCopyLoad(state);
|
|
2055
|
-
}
|
|
2056
|
-
}
|
|
2057
|
-
})();
|
|
2058
|
-
state.pendingBuild = buildPromise;
|
|
2059
|
-
}
|
|
2060
|
-
function buildDue(state) {
|
|
2061
|
-
if (state.hnsw === null) return liveSize(state) >= state.promotionThreshold;
|
|
2062
|
-
if (state.buffer.size === 0) return false;
|
|
2063
|
-
if (state.workerCopies.enabled && state.workerCopies.host !== void 0) return true;
|
|
2064
|
-
return state.buffer.size >= state.promotionThreshold;
|
|
2065
|
-
}
|
|
2066
|
-
function scheduleBuild(state) {
|
|
2067
|
-
if (state.building || state.buildScheduled || state.disposed) return;
|
|
2068
|
-
if (!buildDue(state)) return;
|
|
2069
|
-
state.buildScheduled = true;
|
|
2070
|
-
setTimeout(() => {
|
|
2071
|
-
state.buildScheduled = false;
|
|
2072
|
-
triggerBuild(state);
|
|
2073
|
-
}, 0);
|
|
2074
|
-
}
|
|
2075
|
-
|
|
2076
|
-
// src/vector/vector-index/checkpoint-restore.ts
|
|
2077
|
-
var CALIBRATION_NEVER_CURRENT = -1;
|
|
2078
|
-
function sameCentroid(a, b) {
|
|
2079
|
-
if (a.length !== b.length) return false;
|
|
2080
|
-
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
2081
|
-
return true;
|
|
2082
|
-
}
|
|
2083
|
-
function bufferEveryVectorOutsideTheGraph(state) {
|
|
2084
|
-
for (const docId of state.store.docIds()) {
|
|
2085
|
-
if (state.tombstones.has(docId)) continue;
|
|
2086
|
-
if (state.hnsw === null || !state.hnsw.has(docId)) state.buffer.add(docId);
|
|
2087
|
-
}
|
|
2088
|
-
}
|
|
2089
|
-
function insertNodesOfLaterGraph(state, graph, later, ordinals) {
|
|
2090
|
-
for (const part of later.parts) requireWholeNeighbourLists(part);
|
|
2091
|
-
for (let number = 0; number < ordinals.length; number++) {
|
|
2092
|
-
const ordinal = ordinals[number];
|
|
2093
|
-
if (ordinal === NO_LIVE_VECTOR_AT_POSITION || topLayerInParts(later, number) < 0) continue;
|
|
2094
|
-
const docId = state.store.docIdForOrdinal(ordinal);
|
|
2095
|
-
if (docId !== void 0 && !graph.has(docId)) graph.insertOrdinal(ordinal);
|
|
2096
|
-
}
|
|
2097
|
-
}
|
|
2098
|
-
function restoreGraph(state, payload, ordinalOfNumber, tombstoned) {
|
|
2099
|
-
const first = payload?.graphs[0]?.parts[0];
|
|
2100
|
-
if (payload === null || first === void 0) {
|
|
2101
|
-
releaseReplacedVectors(state);
|
|
2102
|
-
bufferEveryVectorOutsideTheGraph(state);
|
|
2103
|
-
return;
|
|
2104
|
-
}
|
|
2105
|
-
const graph = createHNSWIndex(
|
|
2106
|
-
state.dimension,
|
|
2107
|
-
state.store,
|
|
2108
|
-
{ m: first.m, efConstruction: first.efConstruction, metric: first.metric },
|
|
2109
|
-
state.osq ?? void 0
|
|
2110
|
-
);
|
|
2111
|
-
graph.deserializeNumbered(payload.graphs[0], ordinalOfNumber);
|
|
2112
|
-
for (const ordinal of tombstoned) graph.markTombstoneOrdinal(ordinal);
|
|
2113
|
-
adoptGraph(state, graph);
|
|
2114
|
-
for (const later of payload.graphs.slice(1)) insertNodesOfLaterGraph(state, graph, later, ordinalOfNumber);
|
|
2115
|
-
bufferEveryVectorOutsideTheGraph(state);
|
|
2116
|
-
if (state.buffer.size <= graph.size) return;
|
|
2117
|
-
adoptGraph(state, null);
|
|
2118
|
-
releaseReplacedVectors(state);
|
|
2119
|
-
state.buffer.clear();
|
|
2120
|
-
bufferEveryVectorOutsideTheGraph(state);
|
|
2121
|
-
}
|
|
2122
|
-
function beginCheckpointRestore(state, shape) {
|
|
2123
|
-
emptyFieldBeforeRestore(state);
|
|
2124
|
-
const cold = readsFromDisk(state) && (shape.holdsGraph || shape.liveVectors >= state.promotionThreshold);
|
|
2125
|
-
const restoresCodes = state.osq !== null && osqBitsOf(state.quantizationMode) !== null && shape.holdsGraph;
|
|
2126
|
-
const files = [];
|
|
2127
|
-
const graphNodes = shape.graph?.graphs[0] ?? null;
|
|
2128
|
-
const tombstoned = /* @__PURE__ */ new Set();
|
|
2129
|
-
let nextNumber = 0;
|
|
2130
|
-
let sharedCodes = null;
|
|
2131
|
-
let codesAgree = true;
|
|
2132
|
-
function restoreRecords(payload, ordinals) {
|
|
2133
|
-
const quantizer = state.osq;
|
|
2134
|
-
const layout = state.store.handles.codeLayout;
|
|
2135
|
-
if (!restoresCodes || quantizer === null || layout === null || !codesAgree) return;
|
|
2136
|
-
const codes = payload.codes;
|
|
2137
|
-
if (codes === null || codes.bits !== quantizer.bits) {
|
|
2138
|
-
codesAgree = false;
|
|
2139
|
-
return;
|
|
2140
|
-
}
|
|
2141
|
-
if (sharedCodes === null) {
|
|
2142
|
-
sharedCodes = codes;
|
|
2143
|
-
quantizer.restoreCentroid(Float32Array.from(codes.centroid));
|
|
2144
|
-
} else if (!sameCentroid(sharedCodes.centroid, codes.centroid)) {
|
|
2145
|
-
codesAgree = false;
|
|
2146
|
-
return;
|
|
2147
|
-
}
|
|
2148
|
-
const stride = layout.slotStride;
|
|
2149
|
-
for (let i = 0; i < ordinals.length; i++) {
|
|
2150
|
-
if (ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) continue;
|
|
2151
|
-
const record = codes.records.subarray(i * stride, (i + 1) * stride);
|
|
2152
|
-
if (tombstoned.has(ordinals[i])) quantizer.restoreRecordAtOrdinal(ordinals[i], record);
|
|
2153
|
-
else quantizer.restoreRecord(payload.docIds[i], record);
|
|
2154
|
-
}
|
|
2155
|
-
}
|
|
2156
|
-
function graphHoldsNodeFor(number) {
|
|
2157
|
-
return graphNodes !== null && topLayerInParts(graphNodes, number) >= 0;
|
|
2158
|
-
}
|
|
2159
|
-
function addFile(file) {
|
|
2160
|
-
const { payload, location } = file;
|
|
2161
|
-
if (payload.dimension !== state.dimension) {
|
|
2162
|
-
throw new NarsilError(
|
|
2163
|
-
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
2164
|
-
`Payload dimension ${payload.dimension} does not match index dimension ${state.dimension}`,
|
|
2165
|
-
{ expected: state.dimension, received: payload.dimension }
|
|
2166
|
-
);
|
|
2167
|
-
}
|
|
2168
|
-
const dimension = state.dimension;
|
|
2169
|
-
const onDisk = readsFromDisk(state) && location !== null;
|
|
2170
|
-
const vectors = bytesToVectors(payload.vectors);
|
|
2171
|
-
const fileIndex = onDisk && cold ? state.store.addVectorFile(location.path) : null;
|
|
2172
|
-
const ordinals = new Int32Array(payload.docIds.length).fill(NO_LIVE_VECTOR_AT_POSITION);
|
|
2173
|
-
const firstNumber = nextNumber;
|
|
2174
|
-
nextNumber += payload.docIds.length;
|
|
2175
|
-
for (let i = 0; i < payload.docIds.length; i++) {
|
|
2176
|
-
const dead = isDead(file.dead, i);
|
|
2177
|
-
if (dead && !graphHoldsNodeFor(firstNumber + i)) continue;
|
|
2178
|
-
const docId = payload.docIds[i];
|
|
2179
|
-
const vector = vectors.subarray(i * dimension, (i + 1) * dimension);
|
|
2180
|
-
const offset = onDisk ? location.vectorsOffset + i * dimension * 4 : 0;
|
|
2181
|
-
if (dead) {
|
|
2182
|
-
ordinals[i] = state.store.insertReplaced(docId, vector, fileIndex === null ? void 0 : { fileIndex, offset });
|
|
2183
|
-
tombstoned.add(ordinals[i]);
|
|
2184
|
-
continue;
|
|
2185
|
-
}
|
|
2186
|
-
const replaced = state.store.getOrdinal(docId);
|
|
2187
|
-
if (replaced !== void 0) state.osq?.removeOrdinal(replaced);
|
|
2188
|
-
if (fileIndex !== null) {
|
|
2189
|
-
ordinals[i] = state.store.insertCold(docId, magnitude(vector), { fileIndex, offset });
|
|
2190
|
-
continue;
|
|
2191
|
-
}
|
|
2192
|
-
ordinals[i] = state.store.insert(docId, vector);
|
|
2193
|
-
if (onDisk) state.pendingLocations.set(docId, { path: location.path, offset });
|
|
2194
|
-
}
|
|
2195
|
-
restoreRecords(payload, ordinals);
|
|
2196
|
-
files.push({ id: file.id, key: file.key, ordinals, coded: payload.codes !== null });
|
|
2197
|
-
}
|
|
2198
|
-
function finish() {
|
|
2199
|
-
const codesRestored = restoresCodes && codesAgree && sharedCodes !== null;
|
|
2200
|
-
if (restoresCodes && !codesRestored) {
|
|
2201
|
-
recalibrateFromStore(state);
|
|
2202
|
-
for (const ordinal of tombstoned) state.osq?.writeCodes(ordinal);
|
|
2203
|
-
}
|
|
2204
|
-
let total = 0;
|
|
2205
|
-
for (const file of files) total += file.ordinals.length;
|
|
2206
|
-
const ordinalOfNumber = new Int32Array(total);
|
|
2207
|
-
let number = 0;
|
|
2208
|
-
for (const file of files) {
|
|
2209
|
-
for (let i = 0; i < file.ordinals.length; i++) {
|
|
2210
|
-
const ordinal = file.ordinals[i];
|
|
2211
|
-
if (!state.store.holdsOrdinal(ordinal) && !tombstoned.has(ordinal)) {
|
|
2212
|
-
file.ordinals[i] = NO_LIVE_VECTOR_AT_POSITION;
|
|
2213
|
-
}
|
|
2214
|
-
}
|
|
2215
|
-
ordinalOfNumber.set(file.ordinals, number);
|
|
2216
|
-
number += file.ordinals.length;
|
|
2217
|
-
}
|
|
2218
|
-
restoreGraph(state, shape.graph, ordinalOfNumber, tombstoned);
|
|
2219
|
-
const current = signatureOf(state).calibration;
|
|
2220
|
-
const codedUnder = codesRestored && current !== null ? current : CALIBRATION_NEVER_CURRENT;
|
|
2221
|
-
const saved = files.map((file) => ({
|
|
2222
|
-
id: file.id,
|
|
2223
|
-
key: file.key,
|
|
2224
|
-
ordinals: file.ordinals,
|
|
2225
|
-
calibration: file.coded ? codedUnder : null
|
|
2226
|
-
}));
|
|
2227
|
-
state.savedFiles = saved;
|
|
2228
|
-
state.savedSignature = signatureOf(state);
|
|
2229
|
-
}
|
|
2230
|
-
return { addFile, finish };
|
|
2231
|
-
}
|
|
2232
|
-
|
|
2233
|
-
// src/vector/vector-index/flagged-documents.ts
|
|
2234
|
-
var FlaggedDocuments = class extends Set {
|
|
2235
|
-
constructor(store, layout) {
|
|
2236
|
-
super();
|
|
2237
|
-
this.store = store;
|
|
2238
|
-
this.layout = layout;
|
|
2239
|
-
}
|
|
2240
|
-
ordinals = /* @__PURE__ */ new Map();
|
|
2241
|
-
handles = null;
|
|
2242
|
-
flags = new Uint8Array(0);
|
|
2243
|
-
chunks = null;
|
|
2244
|
-
add(docId) {
|
|
2245
|
-
super.add(docId);
|
|
2246
|
-
const handles = this.openHandles();
|
|
2247
|
-
if (handles === null) return this;
|
|
2248
|
-
this.flagCurrentOrdinal(docId);
|
|
2249
|
-
Atomics.store(handles.header, this.layout.countWord, this.size);
|
|
2250
|
-
return this;
|
|
2251
|
-
}
|
|
2252
|
-
delete(docId) {
|
|
2253
|
-
const held = super.delete(docId);
|
|
2254
|
-
const handles = this.openHandles();
|
|
2255
|
-
if (handles === null) return held;
|
|
2256
|
-
const ordinal = this.ordinals.get(docId);
|
|
2257
|
-
if (ordinal !== void 0) {
|
|
2258
|
-
this.ordinals.delete(docId);
|
|
2259
|
-
this.unflag(ordinal);
|
|
2260
|
-
}
|
|
2261
|
-
Atomics.store(handles.header, this.layout.countWord, this.size);
|
|
2262
|
-
return held;
|
|
2263
|
-
}
|
|
2264
|
-
clear() {
|
|
2265
|
-
super.clear();
|
|
2266
|
-
const handles = this.openHandles();
|
|
2267
|
-
if (handles !== null) {
|
|
2268
|
-
for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
|
|
2269
|
-
Atomics.store(handles.header, this.layout.countWord, 0);
|
|
2270
|
-
}
|
|
2271
|
-
this.ordinals.clear();
|
|
2272
|
-
}
|
|
2273
|
-
flagCurrentOrdinal(docId) {
|
|
2274
|
-
const ordinal = this.store.getOrdinal(docId);
|
|
2275
|
-
const previous = this.ordinals.get(docId);
|
|
2276
|
-
if (ordinal === void 0 || ordinal === previous) return;
|
|
2277
|
-
if (this.chunks !== null) Atomics.add(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
|
|
2278
|
-
Atomics.store(this.flags, ordinal, 1);
|
|
2279
|
-
this.ordinals.set(docId, ordinal);
|
|
2280
|
-
if (previous !== void 0) this.unflag(previous);
|
|
2281
|
-
}
|
|
2282
|
-
unflag(ordinal) {
|
|
2283
|
-
Atomics.store(this.flags, ordinal, 0);
|
|
2284
|
-
if (this.chunks !== null) Atomics.sub(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
|
|
2285
|
-
}
|
|
2286
|
-
openHandles() {
|
|
2287
|
-
if (this.store.dimension === 0) return null;
|
|
2288
|
-
const handles = this.store.handles;
|
|
2289
|
-
if (handles === this.handles) return handles;
|
|
2290
|
-
this.handles = handles;
|
|
2291
|
-
this.flags = new Uint8Array(this.layout.flagsOf(handles));
|
|
2292
|
-
const chunksOf = this.layout.chunksOf;
|
|
2293
|
-
this.chunks = chunksOf === null ? null : new Int32Array(chunksOf(handles));
|
|
2294
|
-
this.ordinals.clear();
|
|
2295
|
-
for (const docId of this) this.flagCurrentOrdinal(docId);
|
|
2296
|
-
return handles;
|
|
2297
|
-
}
|
|
2298
|
-
};
|
|
2299
|
-
function createUnplacedVectors(store) {
|
|
2300
|
-
return new FlaggedDocuments(store, {
|
|
2301
|
-
flagsOf: (handles) => handles.unplaced,
|
|
2302
|
-
chunksOf: (handles) => handles.unplacedChunks,
|
|
2303
|
-
countWord: STORE_UNPLACED_COUNT
|
|
2304
|
-
});
|
|
2305
|
-
}
|
|
2306
|
-
function createRemovedDocuments(store) {
|
|
2307
|
-
return new FlaggedDocuments(store, {
|
|
2308
|
-
flagsOf: (handles) => handles.removed,
|
|
2309
|
-
chunksOf: null,
|
|
2310
|
-
countWord: STORE_REMOVED_COUNT
|
|
2311
|
-
});
|
|
2312
|
-
}
|
|
2313
|
-
|
|
2314
|
-
// src/vector/vector-index/maintenance.ts
|
|
2315
|
-
function compact(state) {
|
|
2316
|
-
if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
|
|
2317
|
-
if (state.hnsw) {
|
|
2318
|
-
state.compactedNodeCount += state.hnsw.tombstoneCount;
|
|
2319
|
-
state.hnsw.compactTombstones();
|
|
2320
|
-
}
|
|
2321
|
-
state.freshGraph?.compactTombstones();
|
|
2322
|
-
for (const docId of state.tombstones) {
|
|
2323
|
-
state.store.remove(docId);
|
|
2324
|
-
state.buffer.delete(docId);
|
|
2325
|
-
if (state.osq) {
|
|
2326
|
-
state.osq.remove(docId);
|
|
2327
|
-
}
|
|
2328
|
-
}
|
|
2329
|
-
releaseReplacedVectors(state);
|
|
2330
|
-
state.tombstones.clear();
|
|
2331
|
-
}
|
|
2332
|
-
function recalibrateWhileNoThreadSearches(state, graph) {
|
|
2333
|
-
if (graph === null) {
|
|
2334
|
-
recalibrateFromStore(state);
|
|
2335
|
-
return;
|
|
2336
|
-
}
|
|
2337
|
-
graph.exclusively(() => recalibrateFromStore(state));
|
|
2338
|
-
}
|
|
2339
|
-
async function insertMissing(state, graph) {
|
|
2340
|
-
const missingOrReplaced = (docId) => !graph.has(docId) || state.buffer.has(docId);
|
|
2341
|
-
const heldWhenTheCallStarted = [...allLiveDocIds(state)];
|
|
2342
|
-
const stillLiveAndUnplaced = (docId) => !state.tombstones.has(docId) && missingOrReplaced(docId);
|
|
2343
|
-
await insertIntoGraph(state, graph, heldWhenTheCallStarted, stillLiveAndUnplaced);
|
|
2344
|
-
}
|
|
2345
|
-
async function foldIntoGraph(state) {
|
|
2346
|
-
const rebuildNeeded = graphNeedsRebuild(state);
|
|
2347
|
-
compact(state);
|
|
2348
|
-
if (liveSize(state) === 0) {
|
|
2349
|
-
const previous = state.hnsw;
|
|
2350
|
-
adoptGraph(state, null);
|
|
2351
|
-
if (previous !== null) dropSharedGraph(state, previous);
|
|
2352
|
-
state.buffer.clear();
|
|
2353
|
-
if (state.osq) {
|
|
2354
|
-
state.osq.clear();
|
|
2355
|
-
}
|
|
2356
|
-
return;
|
|
2357
|
-
}
|
|
2358
|
-
const graph = state.hnsw;
|
|
2359
|
-
if (graph === null || rebuildNeeded) {
|
|
2360
|
-
if (state.osq?.isCalibrated()) recalibrateWhileNoThreadSearches(state, graph);
|
|
2361
|
-
else calibrateQuantizer(state);
|
|
2362
|
-
await buildGraphFromStore(state);
|
|
2363
|
-
} else {
|
|
2364
|
-
await insertMissing(state, graph);
|
|
2365
|
-
}
|
|
2366
|
-
}
|
|
2367
|
-
async function buildExclusively(state, work) {
|
|
2368
|
-
while (state.pendingBuild) {
|
|
2369
|
-
await state.pendingBuild;
|
|
2370
|
-
}
|
|
2371
|
-
if (state.disposed) return;
|
|
2372
|
-
state.building = true;
|
|
2373
|
-
const run = work();
|
|
2374
|
-
state.pendingBuild = run;
|
|
2375
|
-
try {
|
|
2376
|
-
await run;
|
|
2377
|
-
} finally {
|
|
2378
|
-
state.building = false;
|
|
2379
|
-
state.pendingBuild = null;
|
|
2380
|
-
if (state.buffer.size > 0) {
|
|
2381
|
-
scheduleBuild(state);
|
|
2382
|
-
}
|
|
2383
|
-
}
|
|
2384
|
-
}
|
|
2385
|
-
function optimize(state) {
|
|
2386
|
-
return buildExclusively(state, () => foldIntoGraph(state));
|
|
2387
|
-
}
|
|
2388
|
-
async function fillGraph(state) {
|
|
2389
|
-
const graph = state.hnsw;
|
|
2390
|
-
if (graph === null) {
|
|
2391
|
-
if (liveSize(state) >= state.promotionThreshold) await promoteToGraph(state);
|
|
2392
|
-
return;
|
|
2393
|
-
}
|
|
2394
|
-
if (state.osq && !state.osq.isCalibrated()) calibrateQuantizer(state);
|
|
2395
|
-
if (graphNeedsRebuild(state)) {
|
|
2396
|
-
await buildGraphFromStore(state);
|
|
2397
|
-
return;
|
|
2398
|
-
}
|
|
2399
|
-
await insertMissing(state, graph);
|
|
2400
|
-
}
|
|
2401
|
-
function completeGraph(state) {
|
|
2402
|
-
return buildExclusively(state, () => fillGraph(state));
|
|
2403
|
-
}
|
|
2404
|
-
function maintenanceStatus(state) {
|
|
2405
|
-
const storeSize = state.store.size;
|
|
2406
|
-
const graphCount = state.hnsw ? 1 : 0;
|
|
2407
|
-
const graphNodes = state.hnsw === null ? storeSize : state.hnsw.size + state.hnsw.tombstoneCount;
|
|
2408
|
-
const tombstoned = state.tombstones.size + state.store.replacedVectorCount;
|
|
2409
|
-
const tombstoneRatio = graphNodes > 0 ? tombstoned / graphNodes : 0;
|
|
2410
|
-
const estimatedCompactMs = Math.round(tombstoned * ESTIMATED_MS_PER_TOMBSTONE * state.dimensionScale);
|
|
2411
|
-
const optimizeVectorCount = state.hnsw === null || graphNeedsRebuild(state) ? storeSize : state.buffer.size;
|
|
2412
|
-
const estimatedOptimizeMs = Math.round(optimizeVectorCount * ESTIMATED_MS_PER_VECTOR_REBUILD * state.dimensionScale);
|
|
2413
|
-
return {
|
|
2414
|
-
tombstoneRatio,
|
|
2415
|
-
graphCount,
|
|
2416
|
-
bufferSize: state.buffer.size,
|
|
2417
|
-
building: state.building || state.buildScheduled,
|
|
2418
|
-
estimatedCompactMs,
|
|
2419
|
-
estimatedOptimizeMs
|
|
2420
|
-
};
|
|
2421
|
-
}
|
|
2422
|
-
function flatScanScratchBytes(state) {
|
|
2423
|
-
return state.store.dimension === 0 ? 0 : nativeStoreScratchBytes(state.store.handles);
|
|
2424
|
-
}
|
|
2425
|
-
function estimateMemoryBytes(state) {
|
|
2426
|
-
if (state.hnsw === null) return state.store.memoryBytes() + flatScanScratchBytes(state);
|
|
2427
|
-
return state.store.memoryBytes() + state.hnsw.graphBytes + state.hnsw.searchScratchBytes;
|
|
2428
|
-
}
|
|
2429
|
-
|
|
2430
|
-
// src/vector/vector-index/search.ts
|
|
2431
|
-
function* bufferCandidates(state, filter) {
|
|
2432
|
-
for (const docId of state.buffer) {
|
|
2433
|
-
if (state.tombstones.has(docId)) continue;
|
|
2434
|
-
if (filter) {
|
|
2435
|
-
const ordinal = state.store.getOrdinal(docId);
|
|
2436
|
-
if (ordinal === void 0 || !ordinalFilterHas(filter, ordinal)) continue;
|
|
2437
|
-
}
|
|
2438
|
-
yield docId;
|
|
2439
|
-
}
|
|
2440
|
-
}
|
|
2441
|
-
function* filteredDocIds(state, filter) {
|
|
2442
|
-
for (const ordinal of ordinalFilterValues(filter)) {
|
|
2443
|
-
const docId = state.store.docIdForOrdinal(ordinal);
|
|
2444
|
-
if (docId !== void 0) yield docId;
|
|
2445
|
-
}
|
|
2446
|
-
}
|
|
2447
|
-
function admittedTotal(state, filter) {
|
|
2448
|
-
return filter === void 0 ? liveSize(state) : filter.count;
|
|
2449
|
-
}
|
|
2450
|
-
function fetchedOutcome(state, results, minSimilarity, filter) {
|
|
2451
|
-
if (minSimilarity === Number.NEGATIVE_INFINITY) {
|
|
2452
|
-
return { results, matched: admittedTotal(state, filter), matchedExact: true };
|
|
2453
|
-
}
|
|
2454
|
-
return { results, matched: results.length, matchedExact: false };
|
|
2455
|
-
}
|
|
2456
|
-
function scannedOutcome(scan) {
|
|
2457
|
-
return { results: scan.results, matched: scan.matched, matchedExact: true };
|
|
2458
|
-
}
|
|
2459
|
-
function liveCandidates(state, candidates) {
|
|
2460
|
-
const docIds = [];
|
|
2461
|
-
const ordinals = [];
|
|
2462
|
-
for (const docId of candidates) {
|
|
2463
|
-
if (state.tombstones.has(docId)) continue;
|
|
2464
|
-
const ordinal = state.store.getOrdinal(docId);
|
|
2465
|
-
if (ordinal === void 0) continue;
|
|
2466
|
-
docIds.push(docId);
|
|
2467
|
-
ordinals.push(ordinal);
|
|
2468
|
-
}
|
|
2469
|
-
return { docIds, ordinals };
|
|
2470
|
-
}
|
|
2471
|
-
function scanThroughTheCore(state, query, k, metric, minSimilarity, scanned) {
|
|
2472
|
-
const distances = nativeScoresOf(state.store.handles, query, metric, scanned.ordinals);
|
|
2473
|
-
if (distances === null) return null;
|
|
2474
|
-
const heap = createBoundedMaxHeap(highScoreFirst, k);
|
|
2475
|
-
let matched = 0;
|
|
2476
|
-
for (let i = 0; i < scanned.ordinals.length; i++) {
|
|
2477
|
-
if (distances[i] === Number.POSITIVE_INFINITY) continue;
|
|
2478
|
-
const score = toScore(distances[i], metric);
|
|
2479
|
-
if (score < minSimilarity) continue;
|
|
2480
|
-
matched++;
|
|
2481
|
-
heap.push({ docId: scanned.docIds[i], score });
|
|
2482
|
-
}
|
|
2483
|
-
return { results: heap.toSortedArray().reverse(), matched };
|
|
2484
|
-
}
|
|
2485
|
-
function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
|
|
2486
|
-
const scanned = liveCandidates(state, candidates);
|
|
2487
|
-
const fromTheCore = scanThroughTheCore(state, query, k, metric, minSimilarity, scanned);
|
|
2488
|
-
if (fromTheCore !== null) return fromTheCore;
|
|
2489
|
-
noteFallback("score");
|
|
2490
|
-
const arenaQuery = state.store.prepareQueryArena(query);
|
|
2491
|
-
const queryMag = arenaQuery ? arenaQuery.magnitude : magnitude(query);
|
|
2492
|
-
const heap = createBoundedMaxHeap(highScoreFirst, k);
|
|
2493
|
-
let matched = 0;
|
|
2494
|
-
for (let i = 0; i < scanned.docIds.length; i++) {
|
|
2495
|
-
const docId = scanned.docIds[i];
|
|
2496
|
-
let score;
|
|
2497
|
-
if (arenaQuery) {
|
|
2498
|
-
const distance = state.store.distanceFromArena(arenaQuery, scanned.ordinals[i], metric);
|
|
2499
|
-
if (distance === Number.POSITIVE_INFINITY) continue;
|
|
2500
|
-
score = toScore(distance, metric);
|
|
2501
|
-
} else {
|
|
2502
|
-
const entry = state.store.get(docId);
|
|
2503
|
-
if (!entry) continue;
|
|
2504
|
-
switch (metric) {
|
|
2505
|
-
case "cosine":
|
|
2506
|
-
score = cosineSimilarityWithMagnitudes(query, entry.vector, queryMag, entry.magnitude);
|
|
2507
|
-
break;
|
|
2508
|
-
case "dotProduct":
|
|
2509
|
-
score = dotProduct(query, entry.vector);
|
|
2510
|
-
break;
|
|
2511
|
-
case "euclidean": {
|
|
2512
|
-
const dist = euclideanDistance(query, entry.vector);
|
|
2513
|
-
score = 1 / (1 + dist);
|
|
2514
|
-
break;
|
|
2515
|
-
}
|
|
2516
|
-
}
|
|
2517
|
-
}
|
|
2518
|
-
if (score < minSimilarity) continue;
|
|
2519
|
-
matched++;
|
|
2520
|
-
heap.push({ docId, score });
|
|
2521
|
-
}
|
|
2522
|
-
return { results: heap.toSortedArray().reverse(), matched };
|
|
2523
|
-
}
|
|
2524
|
-
function search(state, query, k, options) {
|
|
2525
|
-
return searchWithFilter(state, query, k, options, filterForOptions(state, options));
|
|
2526
|
-
}
|
|
2527
|
-
function searchWithFilter(state, query, k, options, filter) {
|
|
2528
|
-
if (query.length !== state.dimension) {
|
|
2529
|
-
throw new NarsilError(
|
|
2530
|
-
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
2531
|
-
`Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
|
|
2532
|
-
{ expected: state.dimension, received: query.length }
|
|
2533
|
-
);
|
|
2534
|
-
}
|
|
2535
|
-
const currentLiveSize = liveSize(state);
|
|
2536
|
-
const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = options;
|
|
2537
|
-
if (currentLiveSize === 0) return { results: [], matched: 0, matchedExact: true };
|
|
2538
|
-
if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
|
|
2539
|
-
if (k <= 0) return fetchedOutcome(state, [], minSimilarity, filter);
|
|
2540
|
-
if (state.buffer.size > 0 && !state.building && !state.buildScheduled) {
|
|
2541
|
-
scheduleBuild(state);
|
|
2542
|
-
}
|
|
2543
|
-
if (!state.hnsw) {
|
|
2544
|
-
const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
|
|
2545
|
-
return scannedOutcome(bruteForceSearch(state, query, k, metric, minSimilarity, candidates));
|
|
2546
|
-
}
|
|
2547
|
-
if (filter) {
|
|
2548
|
-
const hnswLiveSize = state.hnsw.size;
|
|
2549
|
-
const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
|
|
2550
|
-
if (selectivity < state.filterThreshold) {
|
|
2551
|
-
const scan = bruteForceSearch(state, query, k, metric, minSimilarity, filteredDocIds(state, filter));
|
|
2552
|
-
return scannedOutcome(scan);
|
|
2553
|
-
}
|
|
2554
|
-
}
|
|
2555
|
-
const hnswResults = state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample, rescoreWindow }).map((r) => ({ docId: r.docId, score: r.score }));
|
|
2556
|
-
return fetchedOutcome(
|
|
2557
|
-
state,
|
|
2558
|
-
withBufferedVectors(state, query, k, options, filter, hnswResults),
|
|
2559
|
-
minSimilarity,
|
|
2560
|
-
filter
|
|
2561
|
-
);
|
|
2562
|
-
}
|
|
2563
|
-
function withBufferedVectors(state, query, k, options, filter, graphResults) {
|
|
2564
|
-
if (state.buffer.size === 0) return graphResults;
|
|
2565
|
-
const candidates = bufferCandidates(state, filter);
|
|
2566
|
-
const buffered = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates);
|
|
2567
|
-
return mergeResults(graphResults, buffered.results, k);
|
|
2568
|
-
}
|
|
2569
|
-
|
|
2570
|
-
// src/vector/vector-index/index.ts
|
|
2571
|
-
function defaultQuantizationFor(dimension) {
|
|
2572
|
-
if (dimension >= OSQ4_MIN_DIMENSION) return "osq4";
|
|
2573
|
-
return "osq8";
|
|
2574
|
-
}
|
|
2575
|
-
function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_WORKER_COPIES_ALLOWED, indexName = "", storage = "memory") {
|
|
2576
|
-
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
2577
|
-
throw new NarsilError(
|
|
2578
|
-
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
2579
|
-
`Vector dimension must be a positive integer, got ${dimension}`,
|
|
2580
|
-
{ dimension }
|
|
2581
|
-
);
|
|
2582
|
-
}
|
|
2583
|
-
const promotionThreshold = config?.threshold ?? DEFAULT_PROMOTION_THRESHOLD;
|
|
2584
|
-
const filterThreshold = Math.max(0, Math.min(1, config?.filterThreshold ?? DEFAULT_FILTER_THRESHOLD));
|
|
2585
|
-
const quantizationMode = config?.quantization ?? defaultQuantizationFor(dimension);
|
|
2586
|
-
const codeBits = osqBitsOf(quantizationMode);
|
|
2587
|
-
const metric = config?.hnswConfig?.metric ?? "cosine";
|
|
2588
|
-
const rawHnswM = config?.hnswConfig?.m;
|
|
2589
|
-
const hnswConfig = config?.hnswConfig ? { ...config.hnswConfig, m: rawHnswM !== void 0 ? Math.max(rawHnswM, 2) : void 0 } : void 0;
|
|
2590
|
-
const dimensionScale = dimension / 256;
|
|
2591
|
-
const store = createVectorStore({
|
|
2592
|
-
dimension,
|
|
2593
|
-
codeBits,
|
|
2594
|
-
...storage === "disk" ? { blockBytes: DISK_STORAGE_BLOCK_BYTES } : {}
|
|
2595
|
-
});
|
|
2596
|
-
const state = {
|
|
2597
|
-
indexName,
|
|
2598
|
-
fieldName,
|
|
2599
|
-
dimension,
|
|
2600
|
-
dimensionScale,
|
|
2601
|
-
promotionThreshold,
|
|
2602
|
-
filterThreshold,
|
|
2603
|
-
quantizationMode,
|
|
2604
|
-
storage,
|
|
2605
|
-
metric,
|
|
2606
|
-
hnswConfig,
|
|
2607
|
-
workerCopies,
|
|
2608
|
-
store,
|
|
2609
|
-
tombstones: createRemovedDocuments(store),
|
|
2610
|
-
buffer: createUnplacedVectors(store),
|
|
2611
|
-
pendingLocations: /* @__PURE__ */ new Map(),
|
|
2612
|
-
savedFiles: [],
|
|
2613
|
-
savedSignature: null,
|
|
2614
|
-
osq: codeBits === null ? null : createOsqQuantizer(dimension, codeBits, metric, store),
|
|
2615
|
-
hnsw: null,
|
|
2616
|
-
freshGraph: null,
|
|
2617
|
-
compactedNodeCount: 0,
|
|
2618
|
-
building: false,
|
|
2619
|
-
buildScheduled: false,
|
|
2620
|
-
pendingBuild: null,
|
|
2621
|
-
disposed: false,
|
|
2622
|
-
revision: 0,
|
|
2623
|
-
workerCopyPool: null,
|
|
2624
|
-
workerCopyHandle: null,
|
|
2625
|
-
workerCopyRevision: -1,
|
|
2626
|
-
workerCopyMode: null,
|
|
2627
|
-
workerCopyLoading: false,
|
|
2628
|
-
workerCopiesHeld: false,
|
|
2629
|
-
sharedHandles: /* @__PURE__ */ new Map(),
|
|
2630
|
-
sharedLayoutRevision: 0,
|
|
2631
|
-
sharing: Promise.resolve(),
|
|
2632
|
-
releaseToFilesInFlight: Promise.resolve()
|
|
2633
|
-
};
|
|
2634
|
-
function validateDimension(vector) {
|
|
2635
|
-
if (vector.length !== state.dimension) {
|
|
2636
|
-
throw new NarsilError(
|
|
2637
|
-
ErrorCodes.VECTOR_DIMENSION_MISMATCH,
|
|
2638
|
-
`Vector dimension mismatch: expected ${state.dimension}, got ${vector.length}`,
|
|
2639
|
-
{ expected: state.dimension, received: vector.length }
|
|
2640
|
-
);
|
|
2641
|
-
}
|
|
2642
|
-
}
|
|
2643
|
-
function heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal) {
|
|
2644
|
-
try {
|
|
2645
|
-
return state.store.entryForOrdinal(ordinal)?.vector;
|
|
2646
|
-
} catch {
|
|
2647
|
-
return void 0;
|
|
2648
|
-
}
|
|
2649
|
-
}
|
|
2650
|
-
function holdsThisVectorAlready(docId, ordinal, vector) {
|
|
2651
|
-
if (state.tombstones.has(docId)) return false;
|
|
2652
|
-
const held = heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal);
|
|
2653
|
-
if (held === void 0) return false;
|
|
2654
|
-
for (let i = 0; i < vector.length; i++) if (held[i] !== vector[i]) return false;
|
|
2655
|
-
return true;
|
|
2656
|
-
}
|
|
2657
|
-
function insert(docId, vector, partitionId) {
|
|
2658
|
-
validateDimension(vector);
|
|
2659
|
-
const previous = state.store.getOrdinal(docId);
|
|
2660
|
-
if (previous !== void 0 && holdsThisVectorAlready(docId, previous, vector)) {
|
|
2661
|
-
if (partitionId !== void 0 && state.store.partitionOfOrdinal(previous) !== partitionId) {
|
|
2662
|
-
noteWrite(state);
|
|
2663
|
-
state.store.setPartition(docId, partitionId);
|
|
2664
|
-
}
|
|
2665
|
-
return;
|
|
2666
|
-
}
|
|
2667
|
-
noteWrite(state);
|
|
2668
|
-
state.tombstones.delete(docId);
|
|
2669
|
-
const graphHoldsPrevious = previous !== void 0 && ((state.hnsw?.holdsNode(previous) ?? false) || (state.freshGraph?.holdsNode(previous) ?? false));
|
|
2670
|
-
state.store.insert(docId, vector, partitionId, graphHoldsPrevious);
|
|
2671
|
-
if (previous !== void 0) {
|
|
2672
|
-
state.hnsw?.markTombstoneOrdinal(previous);
|
|
2673
|
-
state.freshGraph?.markTombstoneOrdinal(previous);
|
|
2674
|
-
if (!graphHoldsPrevious) state.osq?.removeOrdinal(previous);
|
|
2675
|
-
}
|
|
2676
|
-
state.buffer.add(docId);
|
|
2677
|
-
state.pendingLocations.delete(docId);
|
|
2678
|
-
if (!threadsHoldCurrentLayout(state)) scheduleWorkerCopyLoad(state);
|
|
2679
|
-
}
|
|
2680
|
-
function remove(docId) {
|
|
2681
|
-
if (!state.store.has(docId)) return;
|
|
2682
|
-
noteWrite(state);
|
|
2683
|
-
state.tombstones.add(docId);
|
|
2684
|
-
state.buffer.delete(docId);
|
|
2685
|
-
state.pendingLocations.delete(docId);
|
|
2686
|
-
if (state.hnsw) {
|
|
2687
|
-
state.hnsw.markTombstone(docId);
|
|
2688
|
-
}
|
|
2689
|
-
}
|
|
2690
|
-
function getVector(docId) {
|
|
2691
|
-
if (state.tombstones.has(docId)) return null;
|
|
2692
|
-
const entry = state.store.get(docId);
|
|
2693
|
-
if (!entry) return null;
|
|
2694
|
-
return new Float32Array(entry.vector);
|
|
2695
|
-
}
|
|
2696
|
-
function has(docId) {
|
|
2697
|
-
return state.store.has(docId) && !state.tombstones.has(docId);
|
|
2698
|
-
}
|
|
2699
|
-
async function awaitPendingBuild() {
|
|
2700
|
-
while (state.pendingBuild) {
|
|
2701
|
-
await state.pendingBuild;
|
|
2702
|
-
}
|
|
2703
|
-
}
|
|
2704
|
-
function releaseHeldMemory() {
|
|
2705
|
-
state.hnsw = null;
|
|
2706
|
-
state.freshGraph = null;
|
|
2707
|
-
state.osq = null;
|
|
2708
|
-
state.tombstones.clear();
|
|
2709
|
-
state.buffer.clear();
|
|
2710
|
-
state.pendingLocations.clear();
|
|
2711
|
-
state.savedFiles = [];
|
|
2712
|
-
state.savedSignature = null;
|
|
2713
|
-
state.store.release();
|
|
2714
|
-
}
|
|
2715
|
-
function dispose() {
|
|
2716
|
-
state.disposed = true;
|
|
2717
|
-
invalidateWorkerCopies(state);
|
|
2718
|
-
const pending = state.pendingBuild;
|
|
2719
|
-
if (pending !== null) {
|
|
2720
|
-
void pending.then(releaseHeldMemory, releaseHeldMemory);
|
|
2721
|
-
return;
|
|
2722
|
-
}
|
|
2723
|
-
releaseHeldMemory();
|
|
2724
|
-
}
|
|
2725
|
-
async function searchParallel(query, k, options) {
|
|
2726
|
-
const confined = options.filterSubsets !== void 0 || options.filterPartitions !== void 0;
|
|
2727
|
-
let filter = filterForOptions(state, options);
|
|
2728
|
-
if (filter !== void 0) {
|
|
2729
|
-
if (state.hnsw) {
|
|
2730
|
-
const hnswLiveSize = state.hnsw.size;
|
|
2731
|
-
const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
|
|
2732
|
-
if (selectivity < state.filterThreshold) {
|
|
2733
|
-
return searchWithFilter(state, query, k, options, filter);
|
|
2734
|
-
}
|
|
2735
|
-
}
|
|
2736
|
-
}
|
|
2737
|
-
const filterRevision = state.revision;
|
|
2738
|
-
scheduleWorkerCopyLoad(state);
|
|
2739
|
-
const viaWorkerCopy = await searchViaWorkerCopies(state, query, k, options.metric, options.minSimilarity, {
|
|
2740
|
-
filter,
|
|
2741
|
-
efSearch: options.efSearch,
|
|
2742
|
-
oversample: options.oversample,
|
|
2743
|
-
rescoreWindow: options.rescoreWindow
|
|
2744
|
-
});
|
|
2745
|
-
if (confined && state.revision !== filterRevision) {
|
|
2746
|
-
filter = filterForOptions(state, options);
|
|
2747
|
-
}
|
|
2748
|
-
if (viaWorkerCopy !== null) {
|
|
2749
|
-
const results = withBufferedVectors(state, query, k, options, filter, viaWorkerCopy);
|
|
2750
|
-
return fetchedOutcome(state, results, options.minSimilarity, filter);
|
|
2751
|
-
}
|
|
2752
|
-
return searchWithFilter(state, query, k, options, filter);
|
|
2753
|
-
}
|
|
2754
|
-
return {
|
|
2755
|
-
get size() {
|
|
2756
|
-
return liveSize(state);
|
|
2757
|
-
},
|
|
2758
|
-
get dimension() {
|
|
2759
|
-
return state.dimension;
|
|
2760
|
-
},
|
|
2761
|
-
get fieldName() {
|
|
2762
|
-
return state.fieldName;
|
|
2763
|
-
},
|
|
2764
|
-
get quantization() {
|
|
2765
|
-
return state.quantizationMode;
|
|
2766
|
-
},
|
|
2767
|
-
get storage() {
|
|
2768
|
-
return state.storage;
|
|
2769
|
-
},
|
|
2770
|
-
insert,
|
|
2771
|
-
remove,
|
|
2772
|
-
partitionsKnown: () => state.store.partitionsKnown,
|
|
2773
|
-
assignPartitions: (resolve) => assignStorePartitions(state, resolve),
|
|
2774
|
-
scheduleBuild: () => scheduleBuild(state),
|
|
2775
|
-
awaitPendingBuild,
|
|
2776
|
-
completeGraph: () => completeGraph(state),
|
|
2777
|
-
dispose,
|
|
2778
|
-
search: (query, k, options) => search(state, query, k, options),
|
|
2779
|
-
searchParallel,
|
|
2780
|
-
refreshWorkerCopies: () => refreshWorkerCopies(state),
|
|
2781
|
-
getVector,
|
|
2782
|
-
has,
|
|
2783
|
-
compact: () => {
|
|
2784
|
-
noteWrite(state);
|
|
2785
|
-
compact(state);
|
|
2786
|
-
},
|
|
2787
|
-
optimize: async () => {
|
|
2788
|
-
noteWrite(state);
|
|
2789
|
-
await optimize(state);
|
|
2790
|
-
},
|
|
2791
|
-
maintenanceStatus: () => maintenanceStatus(state),
|
|
2792
|
-
estimateMemoryBytes: () => estimateMemoryBytes(state),
|
|
2793
|
-
deserialize: (parts) => {
|
|
2794
|
-
invalidateWorkerCopies(state);
|
|
2795
|
-
deserialize(state, parts);
|
|
2796
|
-
},
|
|
2797
|
-
planCheckpoint: (listedKeys, rewritesGraph) => planCheckpoint(state, listedKeys, rewritesGraph),
|
|
2798
|
-
recordCheckpoint: (plan, written) => recordCheckpoint(state, plan, written),
|
|
2799
|
-
restoreCheckpoint: (shape) => {
|
|
2800
|
-
invalidateWorkerCopies(state);
|
|
2801
|
-
return beginCheckpointRestore(state, shape);
|
|
2802
|
-
},
|
|
2803
|
-
vectorFilesInUse: () => state.store.dimension === 0 ? [] : state.store.handles.vectorFiles.filter((path) => path !== ""),
|
|
2804
|
-
adoptDiskLayout: (layout) => adoptDiskLayout(state, layout),
|
|
2805
|
-
releaseVectorFiles: async () => {
|
|
2806
|
-
await withdrawWorkerCopies(state);
|
|
2807
|
-
state.store.closeFiles();
|
|
2808
|
-
},
|
|
2809
|
-
relocateVectorFiles: (rewrite) => {
|
|
2810
|
-
state.store.relocateVectorFiles(rewrite);
|
|
2811
|
-
for (const [docId, location] of state.pendingLocations) {
|
|
2812
|
-
state.pendingLocations.set(docId, { ...location, path: rewrite(location.path) });
|
|
2813
|
-
}
|
|
2814
|
-
},
|
|
2815
|
-
holdWorkerCopies: () => holdWorkerCopies(state),
|
|
2816
|
-
resumeWorkerCopies: () => {
|
|
2817
|
-
state.workerCopiesHeld = false;
|
|
2818
|
-
}
|
|
2819
|
-
};
|
|
2820
|
-
}
|
|
2821
|
-
|
|
2822
|
-
export { createSharedVectorSearcher, createVectorIndex, openSharedVectorField };
|