@delali/narsil 0.3.1-next.99 → 0.4.0
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 +27 -8
- 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 +406 -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
|
@@ -0,0 +1,1164 @@
|
|
|
1
|
+
import { loadFailed, cachedFileBytes, COLD_LOAD_WAIT_WITHOUT_SEARCH_MS, readWholeSectionChecked, checkEverySectionPage, readFileRange, openPagedFile, readCacheOfThisThread, createSectionReader, closePagedFile, COLD_GRAPH_PART_VECTORS, decodeSections, readListedFile, requireListedChecksum, requireListedSize, SEGMENT_PAGE_BYTES, RECORD_PAGE_BYTES, failNativePage, COLD_WALK_NODES_READ_AHEAD } from './chunk-DXZ6JDWG.mjs';
|
|
2
|
+
import { isDead, VECTOR_FILE_PAYLOAD_VERSION, bytesToVectors, decodeVectorGraphPartPayload, assembleGraphParts, decodeVectorFilePayload, requireBoundedUpperLists, osqRecordBytes, osqBitsOf, countDead, magnitude, rescoreDepth, defaultEfSearch, computeCodeBlockLayout, toScore, osqQuantize, createOsqScratch, stageQueryLevels, scalarQueryProducts, readTrailer, osqEstimate, osqDistance, toDistance, createFixedBuffer } from './chunk-R724DJSG.mjs';
|
|
3
|
+
import { HEADER_SIZE, openChecksummedEnvelope, crc32, readHeader } from './chunk-TKKXPKEO.mjs';
|
|
4
|
+
import { yieldToEventLoop, compareScores } from './chunk-BY6NALRK.mjs';
|
|
5
|
+
import { decode, decodeMulti } from './chunk-GXCDWLQI.mjs';
|
|
6
|
+
import { signalOfSearch, requireSearchActive } from './chunk-FJXJZUWF.mjs';
|
|
7
|
+
import { MIN_M, MAX_M, MAX_LAYER_CAP } from './chunk-ZXX5GZEX.mjs';
|
|
8
|
+
import { NarsilError, ErrorCodes } from './chunk-ZKOQR7EA.mjs';
|
|
9
|
+
import { compareCodePoints } from './chunk-TGPKS2UM.mjs';
|
|
10
|
+
|
|
11
|
+
// src/persistence/paged/cold-heap.ts
|
|
12
|
+
function pushMin(heap, number, distance) {
|
|
13
|
+
let at = heap.numbers.length;
|
|
14
|
+
heap.numbers.push(number);
|
|
15
|
+
heap.distances.push(distance);
|
|
16
|
+
while (at > 0) {
|
|
17
|
+
const parent = at - 1 >> 1;
|
|
18
|
+
if (heap.distances[parent] <= distance) break;
|
|
19
|
+
heap.numbers[at] = heap.numbers[parent];
|
|
20
|
+
heap.distances[at] = heap.distances[parent];
|
|
21
|
+
at = parent;
|
|
22
|
+
}
|
|
23
|
+
heap.numbers[at] = number;
|
|
24
|
+
heap.distances[at] = distance;
|
|
25
|
+
}
|
|
26
|
+
function popMin(heap) {
|
|
27
|
+
const size = heap.numbers.length;
|
|
28
|
+
if (size === 0) return null;
|
|
29
|
+
const top = { number: heap.numbers[0], distance: heap.distances[0] };
|
|
30
|
+
const number = heap.numbers[size - 1];
|
|
31
|
+
const distance = heap.distances[size - 1];
|
|
32
|
+
heap.numbers.pop();
|
|
33
|
+
heap.distances.pop();
|
|
34
|
+
const remaining = size - 1;
|
|
35
|
+
if (remaining === 0) return top;
|
|
36
|
+
let at = 0;
|
|
37
|
+
for (; ; ) {
|
|
38
|
+
let child = 2 * at + 1;
|
|
39
|
+
if (child >= remaining) break;
|
|
40
|
+
if (child + 1 < remaining && heap.distances[child + 1] < heap.distances[child]) child += 1;
|
|
41
|
+
if (heap.distances[child] >= distance) break;
|
|
42
|
+
heap.numbers[at] = heap.numbers[child];
|
|
43
|
+
heap.distances[at] = heap.distances[child];
|
|
44
|
+
at = child;
|
|
45
|
+
}
|
|
46
|
+
heap.numbers[at] = number;
|
|
47
|
+
heap.distances[at] = distance;
|
|
48
|
+
return top;
|
|
49
|
+
}
|
|
50
|
+
function negated(heap) {
|
|
51
|
+
return heap.distances.length === 0 ? Number.POSITIVE_INFINITY : -heap.distances[0];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/persistence/paged/cold-native.ts
|
|
55
|
+
var STOPPED = -7;
|
|
56
|
+
var WORK_CAP = 0x7fffffffffffffffn;
|
|
57
|
+
var UINT64_MAX = 0xffffffffffffffffn;
|
|
58
|
+
var NANOSECONDS_PER_MILLISECOND = 1e6;
|
|
59
|
+
function fieldOf(data, metric) {
|
|
60
|
+
const files = [];
|
|
61
|
+
for (const file of data.files) {
|
|
62
|
+
const vectors = file.vectors.native;
|
|
63
|
+
const records = data.codes === null ? null : file.records?.native ?? null;
|
|
64
|
+
if (vectors === null || data.codes !== null && records === null) return null;
|
|
65
|
+
files.push({ first: file.firstNumber, count: file.count, dead: file.dead, vectors, records });
|
|
66
|
+
}
|
|
67
|
+
const graphParts = [];
|
|
68
|
+
for (let at = 0; at < data.parts.length; at++) {
|
|
69
|
+
const part = data.parts[at];
|
|
70
|
+
const neighbours = part.neighbours.native;
|
|
71
|
+
if (neighbours === null) return null;
|
|
72
|
+
graphParts.push({
|
|
73
|
+
first: at * COLD_GRAPH_PART_VECTORS,
|
|
74
|
+
count: part.levels.length,
|
|
75
|
+
levels: part.levels,
|
|
76
|
+
nodeOffsets: part.nodeOffsets,
|
|
77
|
+
neighbours
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
dimension: data.dimension,
|
|
82
|
+
metric: metric === "cosine" ? 0 : metric === "dotProduct" ? 1 : 2,
|
|
83
|
+
total: data.total,
|
|
84
|
+
entryPoint: data.graph.entryPoint,
|
|
85
|
+
maxLayer: data.graph.maxLayer,
|
|
86
|
+
codes: data.codes,
|
|
87
|
+
files,
|
|
88
|
+
graphParts
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function admission(data, accepts) {
|
|
92
|
+
if (accepts === null) return null;
|
|
93
|
+
const mask = new Uint32Array(Math.max(1, Math.ceil(data.total / 32)));
|
|
94
|
+
for (let ordinal = 0; ordinal < data.total; ordinal++) {
|
|
95
|
+
if (!data.isDeadNumber(ordinal) && accepts(data.idAt(ordinal))) mask[ordinal >>> 5] |= 1 << (ordinal & 31);
|
|
96
|
+
}
|
|
97
|
+
return mask;
|
|
98
|
+
}
|
|
99
|
+
function excludedMask(total, excluded) {
|
|
100
|
+
if (excluded.size === 0) return null;
|
|
101
|
+
const mask = new Uint32Array(Math.max(1, Math.ceil(total / 32)));
|
|
102
|
+
for (const ordinal of excluded) {
|
|
103
|
+
if (Number.isInteger(ordinal) && ordinal >= 0 && ordinal < total) mask[ordinal >>> 5] |= 1 << (ordinal & 31);
|
|
104
|
+
}
|
|
105
|
+
return mask;
|
|
106
|
+
}
|
|
107
|
+
function reportFailure(data, status, fault) {
|
|
108
|
+
let path = "";
|
|
109
|
+
let section = fault[1].toString();
|
|
110
|
+
for (const file of data.files) {
|
|
111
|
+
if (file.file.native?.handle.identity !== fault[0]) continue;
|
|
112
|
+
path = file.file.path;
|
|
113
|
+
if (file.vectors.native?.offset === fault[1]) section = "vectors";
|
|
114
|
+
else if (file.records?.native?.offset === fault[1]) section = "records";
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
for (const part of data.parts) {
|
|
118
|
+
if (part.file.native?.handle.identity !== fault[0]) continue;
|
|
119
|
+
path = part.file.path;
|
|
120
|
+
section = "neighbours";
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
return failNativePage(status, path, section, fault[2]);
|
|
124
|
+
}
|
|
125
|
+
function deadlineFor(core, milliseconds) {
|
|
126
|
+
const deadline = new BigUint64Array(1);
|
|
127
|
+
if (milliseconds !== void 0) {
|
|
128
|
+
const status = core.clockNow(deadline);
|
|
129
|
+
if (status !== 0 || !Number.isFinite(milliseconds) || milliseconds < 0) {
|
|
130
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The search clock could not be read", { status });
|
|
131
|
+
}
|
|
132
|
+
const expires = deadline[0] + BigInt(Math.ceil(milliseconds * NANOSECONDS_PER_MILLISECOND));
|
|
133
|
+
deadline[0] = expires > UINT64_MAX ? UINT64_MAX : expires;
|
|
134
|
+
}
|
|
135
|
+
return deadline;
|
|
136
|
+
}
|
|
137
|
+
function prepareSearch(data, query, k, options, accepts, excluded, shared) {
|
|
138
|
+
const file = data.files[0]?.file;
|
|
139
|
+
const native = file?.native;
|
|
140
|
+
if (native === void 0 || native === null || file === void 0) return null;
|
|
141
|
+
const field = fieldOf(data, options.metric);
|
|
142
|
+
if (field === null) return null;
|
|
143
|
+
const window = options.rescoreWindow ?? k;
|
|
144
|
+
let depth = window;
|
|
145
|
+
if (data.codes !== null) depth = rescoreDepth(data.codes.bits, data.dimension, window, options.oversample);
|
|
146
|
+
const ef = Math.max(options.efSearch ?? defaultEfSearch(data.dimension, window), depth);
|
|
147
|
+
const capacity = Math.min(data.total, ef);
|
|
148
|
+
const allocate = (bytes) => shared ? createFixedBuffer(bytes) : new ArrayBuffer(bytes);
|
|
149
|
+
const ordinals = new Uint32Array(allocate(capacity * Uint32Array.BYTES_PER_ELEMENT));
|
|
150
|
+
const scores = new Float64Array(allocate(capacity * Float64Array.BYTES_PER_ELEMENT));
|
|
151
|
+
const fault = new BigUint64Array(allocate(3 * BigUint64Array.BYTES_PER_ELEMENT));
|
|
152
|
+
const meter = new BigInt64Array(allocate(2 * BigInt64Array.BYTES_PER_ELEMENT));
|
|
153
|
+
meter[1] = WORK_CAP;
|
|
154
|
+
const signal = signalOfSearch(options.clock);
|
|
155
|
+
const stop = new Uint32Array(allocate(Uint32Array.BYTES_PER_ELEMENT));
|
|
156
|
+
const accepted = admission(data, accepts);
|
|
157
|
+
const rejected = excludedMask(data.total, excluded);
|
|
158
|
+
options.clock?.check();
|
|
159
|
+
requireSearchActive(stop, signal);
|
|
160
|
+
const timed = options.clock?.nativeWindow?.();
|
|
161
|
+
const deadline = deadlineFor(native.core, timed?.milliseconds);
|
|
162
|
+
const request = {
|
|
163
|
+
query,
|
|
164
|
+
k,
|
|
165
|
+
ef,
|
|
166
|
+
rescoreDepth: depth,
|
|
167
|
+
explicitRescoreWindow: options.rescoreWindow !== void 0,
|
|
168
|
+
minSimilarity: options.minSimilarity,
|
|
169
|
+
accepted,
|
|
170
|
+
excluded: rejected
|
|
171
|
+
};
|
|
172
|
+
const abort = () => {
|
|
173
|
+
Atomics.store(stop, 0, 1);
|
|
174
|
+
};
|
|
175
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
176
|
+
return {
|
|
177
|
+
core: native.core,
|
|
178
|
+
cache: file.cache,
|
|
179
|
+
field,
|
|
180
|
+
request,
|
|
181
|
+
meter,
|
|
182
|
+
stop,
|
|
183
|
+
deadline,
|
|
184
|
+
ordinals,
|
|
185
|
+
scores,
|
|
186
|
+
fault,
|
|
187
|
+
release() {
|
|
188
|
+
signal?.removeEventListener("abort", abort);
|
|
189
|
+
},
|
|
190
|
+
finish(count) {
|
|
191
|
+
requireSearchActive(stop, signal);
|
|
192
|
+
options.clock?.tick(Number(meter[0]));
|
|
193
|
+
if (count === STOPPED && timed !== void 0) timed.expired();
|
|
194
|
+
if (count < 0) reportFailure(data, count, fault);
|
|
195
|
+
const hits = [];
|
|
196
|
+
for (let at = 0; at < count; at++) hits.push({ docId: data.idAt(ordinals[at]), score: scores[at] });
|
|
197
|
+
hits.sort((left, right) => compareScores(left.score, right.score) || compareCodePoints(left.docId, right.docId));
|
|
198
|
+
return hits.slice(0, k);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function searchNativeColdGraph(data, query, k, options, accepts, excluded) {
|
|
203
|
+
const search = prepareSearch(data, query, k, options, accepts, excluded, false);
|
|
204
|
+
if (search === null) return null;
|
|
205
|
+
const { core, field, request, meter, stop, deadline, ordinals, scores, fault } = search;
|
|
206
|
+
try {
|
|
207
|
+
return search.finish(
|
|
208
|
+
core.searchPagedVectors(field, request, search.cache.pageCache(), meter, stop, deadline, ordinals, scores, fault)
|
|
209
|
+
);
|
|
210
|
+
} finally {
|
|
211
|
+
search.release();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function searchNativeColdGraphConcurrently(data, query, k, options, accepts, excluded) {
|
|
215
|
+
const search = prepareSearch(data, query, k, options, accepts, excluded, true);
|
|
216
|
+
if (search === null) return null;
|
|
217
|
+
const { core, field, request, meter, stop, deadline, ordinals, scores, fault } = search;
|
|
218
|
+
return search.cache.withPageCache(async (cache) => {
|
|
219
|
+
const count = await core.searchPagedVectorsAsync(
|
|
220
|
+
field,
|
|
221
|
+
request,
|
|
222
|
+
cache,
|
|
223
|
+
meter,
|
|
224
|
+
stop,
|
|
225
|
+
deadline,
|
|
226
|
+
ordinals,
|
|
227
|
+
scores,
|
|
228
|
+
fault
|
|
229
|
+
);
|
|
230
|
+
return search.finish(count);
|
|
231
|
+
}).finally(search.release);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/persistence/paged/cold-search.ts
|
|
235
|
+
function neighbourList(data, number, layer) {
|
|
236
|
+
const part = data.parts[Math.floor(number / COLD_GRAPH_PART_VECTORS)];
|
|
237
|
+
const offset = number % COLD_GRAPH_PART_VECTORS;
|
|
238
|
+
const start = part.nodeOffsets[offset];
|
|
239
|
+
const end = part.nodeOffsets[offset + 1];
|
|
240
|
+
const bytes = part.neighbours.bytesAt(start, end - start);
|
|
241
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
242
|
+
let cursor = 0;
|
|
243
|
+
let selected;
|
|
244
|
+
for (let current = 0; current < part.levels[offset]; current++) {
|
|
245
|
+
if (cursor + 4 > bytes.byteLength) loadFailed(`The neighbour lists of vector ${number} end inside a list`);
|
|
246
|
+
const count = view.getUint32(cursor, true);
|
|
247
|
+
if (cursor + 4 + count * 4 > bytes.byteLength)
|
|
248
|
+
loadFailed(`The neighbour list of vector ${number} runs past its node`);
|
|
249
|
+
if (current === layer) selected = { view, count, at: cursor + 4 };
|
|
250
|
+
cursor += 4 + count * 4;
|
|
251
|
+
}
|
|
252
|
+
if (cursor !== bytes.byteLength || selected === void 0)
|
|
253
|
+
loadFailed(`The neighbour lists of vector ${number} do not match its layers`);
|
|
254
|
+
return selected;
|
|
255
|
+
}
|
|
256
|
+
function fetchNeighbourLists(data, numbers) {
|
|
257
|
+
const reads = [];
|
|
258
|
+
for (const number of numbers) {
|
|
259
|
+
const part = data.parts[Math.floor(number / COLD_GRAPH_PART_VECTORS)];
|
|
260
|
+
const offset = number % COLD_GRAPH_PART_VECTORS;
|
|
261
|
+
const start = part.nodeOffsets[offset];
|
|
262
|
+
const size = part.nodeOffsets[offset + 1] - start;
|
|
263
|
+
if (size === 0) continue;
|
|
264
|
+
const read = part.neighbours.fetchRows([start], size);
|
|
265
|
+
if (read !== null) reads.push(read);
|
|
266
|
+
}
|
|
267
|
+
return reads.length === 0 ? null : Promise.all(reads);
|
|
268
|
+
}
|
|
269
|
+
function unseenNeighbours(scope, numbers, layer) {
|
|
270
|
+
const unseen = /* @__PURE__ */ new Set();
|
|
271
|
+
for (const number of numbers) {
|
|
272
|
+
const list = neighbourList(scope.data, number, layer);
|
|
273
|
+
for (let at = 0; at < list.count; at++) {
|
|
274
|
+
const neighbour = list.view.getUint32(list.at + at * 4, true);
|
|
275
|
+
if (neighbour < scope.data.total && scope.visited[neighbour] === 0) unseen.add(neighbour);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return [...unseen];
|
|
279
|
+
}
|
|
280
|
+
function* readTheWalkAhead(scope, next, frontier, layer) {
|
|
281
|
+
const { data } = scope;
|
|
282
|
+
const listOfNext = fetchNeighbourLists(data, [next]);
|
|
283
|
+
const rowsOfNext = listOfNext === null ? scope.fetchDistanceRows(unseenNeighbours(scope, [next], layer)) : null;
|
|
284
|
+
if (listOfNext === null && rowsOfNext === null) return;
|
|
285
|
+
const others = frontier.numbers.slice(0, COLD_WALK_NODES_READ_AHEAD - 1);
|
|
286
|
+
yield Promise.all([listOfNext, rowsOfNext, fetchNeighbourLists(data, others)]);
|
|
287
|
+
const rows = scope.fetchDistanceRows(unseenNeighbours(scope, [next, ...others], layer));
|
|
288
|
+
if (rows !== null) yield rows;
|
|
289
|
+
}
|
|
290
|
+
function* walkLayer(scope, entries, ef, layer, excludes) {
|
|
291
|
+
const { data, distanceOf, isAccepted } = scope;
|
|
292
|
+
const visited = scope.visited;
|
|
293
|
+
visited.fill(0);
|
|
294
|
+
const frontier = { numbers: [], distances: [] };
|
|
295
|
+
const found = { numbers: [], distances: [] };
|
|
296
|
+
function take(number, distance) {
|
|
297
|
+
pushMin(found, number, -distance);
|
|
298
|
+
if (found.numbers.length > ef) popMin(found);
|
|
299
|
+
}
|
|
300
|
+
for (const entry of entries) {
|
|
301
|
+
if (visited[entry] === 1) continue;
|
|
302
|
+
visited[entry] = 1;
|
|
303
|
+
if (data.topLayerOf(entry) < layer) continue;
|
|
304
|
+
const distance = distanceOf(entry);
|
|
305
|
+
if (distance === Number.POSITIVE_INFINITY) continue;
|
|
306
|
+
pushMin(frontier, entry, distance);
|
|
307
|
+
if (excludes && !isAccepted(entry)) continue;
|
|
308
|
+
take(entry, distance);
|
|
309
|
+
}
|
|
310
|
+
let furthest = found.numbers.length > 0 ? negated(found) : Number.POSITIVE_INFINITY;
|
|
311
|
+
for (; ; ) {
|
|
312
|
+
const next = popMin(frontier);
|
|
313
|
+
if (next === null) break;
|
|
314
|
+
if (next.distance > furthest && found.numbers.length >= ef) break;
|
|
315
|
+
if (scope.fetchesRows) yield* readTheWalkAhead(scope, next.number, frontier, layer);
|
|
316
|
+
const list = neighbourList(data, next.number, layer);
|
|
317
|
+
scope.clock?.tick(list.count * data.dimension);
|
|
318
|
+
for (let at = 0; at < list.count; at++) {
|
|
319
|
+
const neighbour = list.view.getUint32(list.at + at * 4, true);
|
|
320
|
+
if (neighbour >= data.total || visited[neighbour] === 1) continue;
|
|
321
|
+
visited[neighbour] = 1;
|
|
322
|
+
if (data.topLayerOf(neighbour) < layer) continue;
|
|
323
|
+
const distance = distanceOf(neighbour);
|
|
324
|
+
if (distance === Number.POSITIVE_INFINITY) continue;
|
|
325
|
+
if (distance < furthest || found.numbers.length < ef) {
|
|
326
|
+
pushMin(frontier, neighbour, distance);
|
|
327
|
+
if (excludes && !isAccepted(neighbour)) continue;
|
|
328
|
+
take(neighbour, distance);
|
|
329
|
+
furthest = negated(found);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return found;
|
|
334
|
+
}
|
|
335
|
+
function nearestFirst(found) {
|
|
336
|
+
const numbers = [];
|
|
337
|
+
const distances = [];
|
|
338
|
+
for (; ; ) {
|
|
339
|
+
const top = popMin(found);
|
|
340
|
+
if (top === null) break;
|
|
341
|
+
numbers.push(top.number);
|
|
342
|
+
distances.push(-top.distance);
|
|
343
|
+
}
|
|
344
|
+
numbers.reverse();
|
|
345
|
+
distances.reverse();
|
|
346
|
+
return { numbers, distances };
|
|
347
|
+
}
|
|
348
|
+
function entryNumberOf(data) {
|
|
349
|
+
const { entryPoint } = data.graph;
|
|
350
|
+
if (entryPoint !== null && data.topLayerOf(entryPoint) >= 0) return entryPoint;
|
|
351
|
+
let best = null;
|
|
352
|
+
let bestTop = -1;
|
|
353
|
+
for (let number = 0; number < data.total; number++) {
|
|
354
|
+
const top = data.topLayerOf(number);
|
|
355
|
+
if (top > bestTop) {
|
|
356
|
+
bestTop = top;
|
|
357
|
+
best = number;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return best;
|
|
361
|
+
}
|
|
362
|
+
function codeDistance(data, query, metric) {
|
|
363
|
+
const { codes, dimension } = data;
|
|
364
|
+
if (codes === null) return null;
|
|
365
|
+
const layout = computeCodeBlockLayout(dimension, codes.bits);
|
|
366
|
+
const code = osqQuantize(query, codes.centroid, layout.queryBits, metric, createOsqScratch(dimension));
|
|
367
|
+
const packed = new Uint8Array(layout.queryBytes);
|
|
368
|
+
stageQueryLevels(code.levels, codes.bits, packed);
|
|
369
|
+
let centroidDot = 0;
|
|
370
|
+
for (let at = 0; at < dimension; at++) centroidDot += codes.centroid[at] * codes.centroid[at];
|
|
371
|
+
const trailer = { lower: 0, upper: 0, correction: 0, sum: 0 };
|
|
372
|
+
const { codeBytes, slotStride, queryBits, bits } = layout;
|
|
373
|
+
const record = new Uint8Array(slotStride);
|
|
374
|
+
const recordView = new DataView(record.buffer);
|
|
375
|
+
return (number) => {
|
|
376
|
+
const file = data.fileOfNumber(number);
|
|
377
|
+
if (file.records === null) return Number.POSITIVE_INFINITY;
|
|
378
|
+
file.records.read((number - file.firstNumber) * slotStride, record);
|
|
379
|
+
const products = scalarQueryProducts(layout, record, 0, packed);
|
|
380
|
+
readTrailer(recordView, 0, codeBytes, trailer);
|
|
381
|
+
const estimate = osqEstimate(products, trailer, bits, code, queryBits, dimension, centroidDot, metric);
|
|
382
|
+
return osqDistance(estimate, metric);
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function numbersThatCanReachTheTop(scored, k) {
|
|
386
|
+
if (k <= 0 || scored.length <= k) return scored;
|
|
387
|
+
scored.sort((a, b) => compareScores(a.score, b.score));
|
|
388
|
+
const lowestKept = scored[k - 1].score;
|
|
389
|
+
let end = k;
|
|
390
|
+
while (end < scored.length && compareScores(scored[end].score, lowestKept) === 0) end += 1;
|
|
391
|
+
return scored.slice(0, end);
|
|
392
|
+
}
|
|
393
|
+
function bestHits(hits, k) {
|
|
394
|
+
return hits.sort((a, b) => compareScores(a.score, b.score) || compareCodePoints(a.docId, b.docId)).slice(0, Math.max(k, 0));
|
|
395
|
+
}
|
|
396
|
+
function searchColdGraph(data, query, k, options, accepts, excluded) {
|
|
397
|
+
const native = searchNativeColdGraph(data, query, k, options, accepts, excluded);
|
|
398
|
+
if (native !== null) return native;
|
|
399
|
+
const walk = walkColdGraph(data, query, k, options, accepts, excluded, false);
|
|
400
|
+
let step = walk.next();
|
|
401
|
+
while (step.done !== true) step = walk.next();
|
|
402
|
+
return step.value;
|
|
403
|
+
}
|
|
404
|
+
async function searchColdGraphConcurrently(data, query, k, options, accepts, excluded) {
|
|
405
|
+
const native = searchNativeColdGraphConcurrently(data, query, k, options, accepts, excluded);
|
|
406
|
+
if (native !== null) return native;
|
|
407
|
+
const walk = walkColdGraph(data, query, k, options, accepts, excluded, true);
|
|
408
|
+
let step = walk.next();
|
|
409
|
+
while (step.done !== true) {
|
|
410
|
+
await step.value;
|
|
411
|
+
step = walk.next();
|
|
412
|
+
}
|
|
413
|
+
return step.value;
|
|
414
|
+
}
|
|
415
|
+
function* walkColdGraph(data, query, k, options, accepts, excluded, fetchesRows) {
|
|
416
|
+
const { metric, minSimilarity } = options;
|
|
417
|
+
if (k === 0) return [];
|
|
418
|
+
const entry = entryNumberOf(data);
|
|
419
|
+
if (entry === null) return [];
|
|
420
|
+
const qMag = magnitude(query);
|
|
421
|
+
const fromCodes = codeDistance(data, query, metric);
|
|
422
|
+
const fullDistance = (number) => {
|
|
423
|
+
const vector = data.reusedVectorAt(number);
|
|
424
|
+
for (const value of vector) {
|
|
425
|
+
if (!Number.isFinite(value)) loadFailed(`Vector ${number} holds a non-finite component`);
|
|
426
|
+
}
|
|
427
|
+
return toDistance(query, vector, qMag, metric === "cosine" ? magnitude(vector) : 0, metric);
|
|
428
|
+
};
|
|
429
|
+
const distanceOf = fromCodes ?? fullDistance;
|
|
430
|
+
const acceptance = /* @__PURE__ */ new Map();
|
|
431
|
+
const isAccepted = (number) => {
|
|
432
|
+
if (excluded.has(number) || data.isDeadNumber(number)) return false;
|
|
433
|
+
if (accepts === null) return true;
|
|
434
|
+
const held = acceptance.get(number);
|
|
435
|
+
if (held !== void 0) return held;
|
|
436
|
+
const accepted = accepts(data.idAt(number));
|
|
437
|
+
acceptance.set(number, accepted);
|
|
438
|
+
return accepted;
|
|
439
|
+
};
|
|
440
|
+
const window = options.rescoreWindow ?? k;
|
|
441
|
+
const codes = data.codes;
|
|
442
|
+
let depth = window;
|
|
443
|
+
if (codes !== null) depth = rescoreDepth(codes.bits, data.dimension, window, options.oversample);
|
|
444
|
+
const ef = Math.max(options.efSearch ?? defaultEfSearch(data.dimension, window), depth);
|
|
445
|
+
const codeRowBytes = codes === null ? null : computeCodeBlockLayout(data.dimension, codes.bits).slotStride;
|
|
446
|
+
const scope = {
|
|
447
|
+
data,
|
|
448
|
+
distanceOf,
|
|
449
|
+
isAccepted,
|
|
450
|
+
visited: new Uint8Array(data.total),
|
|
451
|
+
clock: options.clock,
|
|
452
|
+
fetchesRows,
|
|
453
|
+
fetchDistanceRows: (numbers2) => codeRowBytes === null ? fetchVectorRows(numbers2) : data.fetchRows(numbers2, (file) => file.records, codeRowBytes)
|
|
454
|
+
};
|
|
455
|
+
function fetchVectorRows(numbers2) {
|
|
456
|
+
return data.fetchRows(numbers2, (file) => file.vectors, data.dimension * 4);
|
|
457
|
+
}
|
|
458
|
+
let current = entry;
|
|
459
|
+
for (let layer = Math.min(data.graph.maxLayer, data.topLayerOf(entry)); layer >= 1; layer--) {
|
|
460
|
+
const { numbers: numbers2 } = nearestFirst(yield* walkLayer(scope, [current], 1, layer, false));
|
|
461
|
+
if (numbers2.length > 0) current = numbers2[0];
|
|
462
|
+
}
|
|
463
|
+
const { numbers, distances } = nearestFirst(yield* walkLayer(scope, [current], ef, 0, true));
|
|
464
|
+
const scored = [];
|
|
465
|
+
const rescores = codes !== null;
|
|
466
|
+
const rescoreUntil = rescores ? Math.min(depth, numbers.length) : 0;
|
|
467
|
+
if (fetchesRows && rescoreUntil > 0) {
|
|
468
|
+
const reading = fetchVectorRows(numbers.slice(0, rescoreUntil));
|
|
469
|
+
if (reading !== null) yield reading;
|
|
470
|
+
}
|
|
471
|
+
for (let at = 0; at < numbers.length; at++) {
|
|
472
|
+
const number = numbers[at];
|
|
473
|
+
if (rescores && at >= rescoreUntil && (options.rescoreWindow === void 0 || minSimilarity !== Number.NEGATIVE_INFINITY)) {
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
const distance = at < rescoreUntil ? fullDistance(number) : distances[at];
|
|
477
|
+
const score = toScore(distance, metric);
|
|
478
|
+
if (score < minSimilarity) continue;
|
|
479
|
+
scored.push({ number, score });
|
|
480
|
+
}
|
|
481
|
+
const hits = numbersThatCanReachTheTop(scored, k).map(({ number, score }) => ({ docId: data.idAt(number), score }));
|
|
482
|
+
return bestHits(hits, k);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// src/persistence/paged/cold-field/open-files.ts
|
|
486
|
+
var FLOAT64_BYTES = 8;
|
|
487
|
+
var CODES_KEY_AND_MAP_HEADER_BYTES = 7;
|
|
488
|
+
var BIN8_MAX_BYTES = 255;
|
|
489
|
+
var BIN16_MAX_BYTES = 65535;
|
|
490
|
+
var VECTOR_PAGE_SIZES = /* @__PURE__ */ new Set([SEGMENT_PAGE_BYTES, RECORD_PAGE_BYTES]);
|
|
491
|
+
var NEIGHBOUR_PAGE_SIZES = /* @__PURE__ */ new Set([RECORD_PAGE_BYTES]);
|
|
492
|
+
var LEVEL_PAGE_SIZES = /* @__PURE__ */ new Set([SEGMENT_PAGE_BYTES]);
|
|
493
|
+
function holdsRawPayload(path) {
|
|
494
|
+
const header = new Uint8Array(HEADER_SIZE);
|
|
495
|
+
readFileRange(path, 0, header);
|
|
496
|
+
return !readHeader(header).flags.compressionEnabled;
|
|
497
|
+
}
|
|
498
|
+
function readHead(path, ref) {
|
|
499
|
+
const { headOffset, headLength, headChecksum } = ref;
|
|
500
|
+
if (headOffset === void 0 || headLength === void 0 || headChecksum === void 0) return null;
|
|
501
|
+
if (!holdsRawPayload(path)) return null;
|
|
502
|
+
const head = new Uint8Array(headLength);
|
|
503
|
+
readFileRange(path, HEADER_SIZE + headOffset, head);
|
|
504
|
+
if (crc32(head) !== headChecksum) {
|
|
505
|
+
throw new NarsilError(
|
|
506
|
+
ErrorCodes.PERSISTENCE_CRC_MISMATCH,
|
|
507
|
+
`The head of the file "${ref.key}" does not match its checksum`,
|
|
508
|
+
{ key: ref.key }
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
return head;
|
|
512
|
+
}
|
|
513
|
+
function decodedHead(head, key) {
|
|
514
|
+
let raw;
|
|
515
|
+
try {
|
|
516
|
+
raw = decode(head);
|
|
517
|
+
} catch (error) {
|
|
518
|
+
loadFailed(`The head of "${key}" is not MessagePack: ${error instanceof Error ? error.message : String(error)}`);
|
|
519
|
+
}
|
|
520
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) loadFailed(`The head of "${key}" is not a map`);
|
|
521
|
+
return raw;
|
|
522
|
+
}
|
|
523
|
+
function binHeaderBytes(length) {
|
|
524
|
+
if (length <= BIN8_MAX_BYTES) return 2;
|
|
525
|
+
return length <= BIN16_MAX_BYTES ? 3 : 5;
|
|
526
|
+
}
|
|
527
|
+
function isUint32OfThisHost(bytes) {
|
|
528
|
+
return bytes.byteOffset % 4 === 0 && new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
529
|
+
}
|
|
530
|
+
function uint32ValuesOf(bytes) {
|
|
531
|
+
if (bytes.byteLength % Uint32Array.BYTES_PER_ELEMENT !== 0)
|
|
532
|
+
loadFailed("The node offsets do not hold whole uint32 values");
|
|
533
|
+
if (isUint32OfThisHost(bytes)) return new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
|
|
534
|
+
const values = new Uint32Array(bytes.byteLength / 4);
|
|
535
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
536
|
+
for (let at = 0; at < values.length; at++) values[at] = view.getUint32(at * 4, true);
|
|
537
|
+
return values;
|
|
538
|
+
}
|
|
539
|
+
function codesOfFile(path, sections, key) {
|
|
540
|
+
const records = sections.get("records");
|
|
541
|
+
const docIds = sections.get("docIds");
|
|
542
|
+
if (records === void 0 || docIds === void 0) return null;
|
|
543
|
+
const start = docIds.offset + docIds.length;
|
|
544
|
+
const length = records.offset - start - binHeaderBytes(records.length);
|
|
545
|
+
if (length <= CODES_KEY_AND_MAP_HEADER_BYTES) loadFailed(`The file "${key}" places its codes before its IDs`);
|
|
546
|
+
const bytes = new Uint8Array(length);
|
|
547
|
+
readFileRange(path, HEADER_SIZE + start, bytes);
|
|
548
|
+
const values = [];
|
|
549
|
+
try {
|
|
550
|
+
for (const value of decodeMulti(bytes.subarray(CODES_KEY_AND_MAP_HEADER_BYTES))) values.push(value);
|
|
551
|
+
} catch (error) {
|
|
552
|
+
loadFailed(`The codes of "${key}" are not MessagePack: ${error instanceof Error ? error.message : String(error)}`);
|
|
553
|
+
}
|
|
554
|
+
const [bitsKey, bits, centroidKey, centroid] = values;
|
|
555
|
+
if (bitsKey !== "bits" || centroidKey !== "centroid" || !Array.isArray(centroid)) {
|
|
556
|
+
loadFailed(`The file "${key}" holds its codes in another layout`);
|
|
557
|
+
}
|
|
558
|
+
if (bits !== 1 && bits !== 2 && bits !== 4 && bits !== 8)
|
|
559
|
+
loadFailed(`The file "${key}" holds ${String(bits)} bit codes`);
|
|
560
|
+
const valuesOfCentroid = new Float32Array(centroid.length);
|
|
561
|
+
for (let at = 0; at < centroid.length; at++) {
|
|
562
|
+
const value = centroid[at];
|
|
563
|
+
if (typeof value !== "number" || !Number.isFinite(value) || !Number.isFinite(Math.fround(value)))
|
|
564
|
+
loadFailed(`The centroid of "${key}" holds an invalid float32 value`);
|
|
565
|
+
valuesOfCentroid[at] = value;
|
|
566
|
+
}
|
|
567
|
+
return { bits, centroid: valuesOfCentroid };
|
|
568
|
+
}
|
|
569
|
+
function sameCodes(left, right) {
|
|
570
|
+
if (left.bits !== right.bits || left.centroid.length !== right.centroid.length) return false;
|
|
571
|
+
for (let at = 0; at < left.centroid.length; at++) if (left.centroid[at] !== right.centroid[at]) return false;
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
function requiredSection(sections, name, key) {
|
|
575
|
+
const section = sections.get(name);
|
|
576
|
+
if (section === void 0) loadFailed(`The head of "${key}" lists no section "${name}"`);
|
|
577
|
+
return section;
|
|
578
|
+
}
|
|
579
|
+
function closeHeldFiles(held) {
|
|
580
|
+
for (const { file } of held) closePagedFile(file);
|
|
581
|
+
}
|
|
582
|
+
function openOneFile(input, at, firstNumber, held, codes) {
|
|
583
|
+
const { ref, path } = input.files[at];
|
|
584
|
+
const head = readHead(path, ref);
|
|
585
|
+
if (head === null) return false;
|
|
586
|
+
const sectionsRaw = decodedHead(head, ref.key).sections;
|
|
587
|
+
const list = decodeSections(sectionsRaw, ref.bytes - HEADER_SIZE, `head of "${ref.key}"`, VECTOR_PAGE_SIZES);
|
|
588
|
+
const sections = new Map(list.map((section) => [section.name, section]));
|
|
589
|
+
const fileCodes = codesOfFile(path, sections, ref.key);
|
|
590
|
+
if (input.codeBits !== null) {
|
|
591
|
+
if (fileCodes === null || fileCodes.bits !== input.codeBits) return false;
|
|
592
|
+
if (codes !== null && !sameCodes(codes, fileCodes)) return false;
|
|
593
|
+
}
|
|
594
|
+
const records = sections.get("records");
|
|
595
|
+
const magnitudeCount = requiredSection(sections, "magnitudes", ref.key).length / FLOAT64_BYTES;
|
|
596
|
+
if (magnitudeCount !== ref.count) loadFailed(`The file "${ref.key}" holds ${magnitudeCount} magnitudes`);
|
|
597
|
+
const ids = requiredSection(sections, "docIds", ref.key);
|
|
598
|
+
const idOffsets = requiredSection(sections, "idOffsets", ref.key);
|
|
599
|
+
const idOrder = requiredSection(sections, "idOrder", ref.key);
|
|
600
|
+
const vectors = requiredSection(sections, "vectors", ref.key);
|
|
601
|
+
const file = openPagedFile(path, readCacheOfThisThread());
|
|
602
|
+
held.push({
|
|
603
|
+
firstNumber,
|
|
604
|
+
count: ref.count,
|
|
605
|
+
dead: ref.dead,
|
|
606
|
+
file,
|
|
607
|
+
ids: createSectionReader(file, ids),
|
|
608
|
+
idOffsets: createSectionReader(file, idOffsets),
|
|
609
|
+
idOrder: createSectionReader(file, idOrder),
|
|
610
|
+
records: records === void 0 ? null : createSectionReader(file, records),
|
|
611
|
+
vectors: createSectionReader(file, vectors)
|
|
612
|
+
});
|
|
613
|
+
return input.codeBits === null ? null : fileCodes;
|
|
614
|
+
}
|
|
615
|
+
function openFiles(input) {
|
|
616
|
+
const files = [];
|
|
617
|
+
let codes = null;
|
|
618
|
+
let firstNumber = 0;
|
|
619
|
+
try {
|
|
620
|
+
for (let at = 0; at < input.files.length; at++) {
|
|
621
|
+
const opened = openOneFile(input, at, firstNumber, files, codes);
|
|
622
|
+
if (opened === false) {
|
|
623
|
+
closeHeldFiles(files);
|
|
624
|
+
return null;
|
|
625
|
+
}
|
|
626
|
+
codes = opened;
|
|
627
|
+
firstNumber += input.files[at].ref.count;
|
|
628
|
+
}
|
|
629
|
+
} catch (error) {
|
|
630
|
+
closeHeldFiles(files);
|
|
631
|
+
throw error;
|
|
632
|
+
}
|
|
633
|
+
return { files, codes };
|
|
634
|
+
}
|
|
635
|
+
function graphLayoutOf(head, ref) {
|
|
636
|
+
const { graphs } = head;
|
|
637
|
+
if (!Array.isArray(graphs) || graphs.length !== 1) return null;
|
|
638
|
+
const layout = graphs[0];
|
|
639
|
+
const payloadLength = ref.bytes - HEADER_SIZE;
|
|
640
|
+
const [levels] = decodeSections([layout.levels], payloadLength, `head of "${ref.key}"`, LEVEL_PAGE_SIZES);
|
|
641
|
+
const [neighbours] = decodeSections([layout.neighbours], payloadLength, `head of "${ref.key}"`, NEIGHBOUR_PAGE_SIZES);
|
|
642
|
+
if (!(layout.nodeOffsets instanceof Uint8Array)) loadFailed(`The head of "${ref.key}" holds no node offsets`);
|
|
643
|
+
const nodeOffsets = uint32ValuesOf(layout.nodeOffsets);
|
|
644
|
+
if (nodeOffsets.length !== levels.length + 1) loadFailed(`The head of "${ref.key}" holds the wrong number of offsets`);
|
|
645
|
+
for (let at = 1; at < nodeOffsets.length; at++) {
|
|
646
|
+
if (nodeOffsets[at] < nodeOffsets[at - 1]) loadFailed(`The head of "${ref.key}" holds descending node offsets`);
|
|
647
|
+
}
|
|
648
|
+
if (nodeOffsets[nodeOffsets.length - 1] > neighbours.length) {
|
|
649
|
+
loadFailed(`The node offsets of "${ref.key}" end outside its neighbour lists`);
|
|
650
|
+
}
|
|
651
|
+
return { entryPoint: layout.entryPoint, maxLayer: layout.maxLayer, m: layout.m, levels, nodeOffsets, neighbours };
|
|
652
|
+
}
|
|
653
|
+
function openParts(input) {
|
|
654
|
+
const parts = [];
|
|
655
|
+
let graph = null;
|
|
656
|
+
try {
|
|
657
|
+
for (const { ref, path } of input.graphParts) {
|
|
658
|
+
const head = readHead(path, ref);
|
|
659
|
+
const layout = head === null ? null : graphLayoutOf(decodedHead(head, ref.key), ref);
|
|
660
|
+
if (layout === null) {
|
|
661
|
+
closeHeldFiles(parts);
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
const { entryPoint, maxLayer, m } = layout;
|
|
665
|
+
if (typeof maxLayer !== "number" || typeof m !== "number") loadFailed(`The head of "${ref.key}" holds no layers`);
|
|
666
|
+
if (!Number.isInteger(m) || m < MIN_M || m > MAX_M) {
|
|
667
|
+
loadFailed(`The head of "${ref.key}" gives m ${m}, while m must be a whole number from ${MIN_M} to ${MAX_M}`);
|
|
668
|
+
}
|
|
669
|
+
if (entryPoint !== null && typeof entryPoint !== "number") loadFailed(`The head of "${ref.key}" holds no entry`);
|
|
670
|
+
if (graph === null) graph = { entryPoint, maxLayer, m };
|
|
671
|
+
const levels = readWholeSectionChecked(path, layout.levels, false);
|
|
672
|
+
const file = openPagedFile(path, readCacheOfThisThread());
|
|
673
|
+
parts.push({
|
|
674
|
+
file,
|
|
675
|
+
levels,
|
|
676
|
+
nodeOffsets: layout.nodeOffsets,
|
|
677
|
+
neighbours: createSectionReader(file, layout.neighbours)
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
if (graph !== null) {
|
|
681
|
+
requireBoundedUpperLists(
|
|
682
|
+
parts.map((part) => part.levels),
|
|
683
|
+
graph.m,
|
|
684
|
+
`The vector graph parts of field "${input.fieldPath}"`
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
} catch (error) {
|
|
688
|
+
closeHeldFiles(parts);
|
|
689
|
+
throw error;
|
|
690
|
+
}
|
|
691
|
+
return graph === null ? null : { parts, graph };
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// src/persistence/paged/cold-field/validation.ts
|
|
695
|
+
var UINT32_MAX = 4294967295;
|
|
696
|
+
function validateColdLayout(input, opened, parts, graph) {
|
|
697
|
+
const { dimension } = input;
|
|
698
|
+
const { codes, files } = opened;
|
|
699
|
+
if (!Number.isInteger(dimension) || dimension <= 0 || dimension > UINT32_MAX)
|
|
700
|
+
loadFailed("The disk vector field holds an invalid dimension");
|
|
701
|
+
if (codes !== null && (codes.centroid.length !== dimension || codes.centroid.some((value) => !Number.isFinite(value))))
|
|
702
|
+
loadFailed("The disk vector field holds an invalid centroid");
|
|
703
|
+
let total = 0;
|
|
704
|
+
for (const file of files) {
|
|
705
|
+
if (!Number.isInteger(file.count) || file.count <= 0 || file.count > UINT32_MAX - total || file.firstNumber !== total || file.vectors.length !== file.count * dimension * Float32Array.BYTES_PER_ELEMENT || file.dead !== null && file.dead.length !== Math.ceil(file.count / 8) || codes !== null && file.records?.length !== file.count * osqRecordBytes(dimension, codes.bits))
|
|
706
|
+
loadFailed(`The vector ranges of "${file.file.path}" do not match the field`);
|
|
707
|
+
total += file.count;
|
|
708
|
+
}
|
|
709
|
+
if (!Number.isInteger(graph.maxLayer) || graph.maxLayer < 0 || graph.maxLayer > MAX_LAYER_CAP || graph.entryPoint !== null && (!Number.isInteger(graph.entryPoint) || graph.entryPoint < 0 || graph.entryPoint >= total) || parts.length !== Math.ceil(total / COLD_GRAPH_PART_VECTORS))
|
|
710
|
+
loadFailed("The disk vector graph does not cover the field");
|
|
711
|
+
for (let at = 0; at < parts.length; at++) {
|
|
712
|
+
const part = parts[at];
|
|
713
|
+
const count = Math.min(total - at * COLD_GRAPH_PART_VECTORS, COLD_GRAPH_PART_VECTORS);
|
|
714
|
+
const { levels, nodeOffsets } = part;
|
|
715
|
+
if (levels.length !== count || nodeOffsets.length !== count + 1 || nodeOffsets[0] !== 0)
|
|
716
|
+
loadFailed(`The node ranges of "${part.file.path}" do not match the field`);
|
|
717
|
+
for (let node = 0; node < count; node++) {
|
|
718
|
+
if (levels[node] > graph.maxLayer + 1 || nodeOffsets[node] > nodeOffsets[node + 1] || nodeOffsets[node + 1] > part.neighbours.length)
|
|
719
|
+
loadFailed(`The node at position ${node} of "${part.file.path}" has invalid layers or offsets`);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// src/persistence/paged/cold-field/index.ts
|
|
725
|
+
var FIXSTR = 160;
|
|
726
|
+
var FIXSTR_MAX_BYTES = 31;
|
|
727
|
+
var STR8_MAX_BYTES = 255;
|
|
728
|
+
var STR16_MAX_BYTES = 65535;
|
|
729
|
+
var ASCII_MAX = 127;
|
|
730
|
+
var ID_OFFSET_BYTES = 4;
|
|
731
|
+
function shortAsciiIdOf(encoded) {
|
|
732
|
+
const length = encoded[0] - FIXSTR;
|
|
733
|
+
if (length < 0 || length > FIXSTR_MAX_BYTES || encoded.length !== length + 1) return null;
|
|
734
|
+
let id = "";
|
|
735
|
+
for (let at = 1; at <= length; at++) {
|
|
736
|
+
const code = encoded[at];
|
|
737
|
+
if (code > ASCII_MAX) return null;
|
|
738
|
+
id += String.fromCharCode(code);
|
|
739
|
+
}
|
|
740
|
+
return id;
|
|
741
|
+
}
|
|
742
|
+
function everyLiveVectorHoldsANode(files, parts) {
|
|
743
|
+
for (const file of files) {
|
|
744
|
+
for (let at = 0; at < file.count; at++) {
|
|
745
|
+
if (isDead(file.dead, at)) continue;
|
|
746
|
+
const number = file.firstNumber + at;
|
|
747
|
+
const part = parts[Math.floor(number / COLD_GRAPH_PART_VECTORS)];
|
|
748
|
+
if (part === void 0) return false;
|
|
749
|
+
if (part.levels[number % COLD_GRAPH_PART_VECTORS] === 0) return false;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
function createFieldData(input, opened, graph, parts) {
|
|
755
|
+
const { files, codes } = opened;
|
|
756
|
+
const lastFile = files[files.length - 1];
|
|
757
|
+
const total = lastFile === void 0 ? 0 : lastFile.firstNumber + lastFile.count;
|
|
758
|
+
const stride = input.dimension * 4;
|
|
759
|
+
const reusedBytes = new Uint8Array(stride);
|
|
760
|
+
const reusedVector = bytesToVectors(reusedBytes);
|
|
761
|
+
const idOffsetBytes = new Uint8Array(8);
|
|
762
|
+
const idOffsetView = new DataView(idOffsetBytes.buffer);
|
|
763
|
+
function fileOfNumber(number) {
|
|
764
|
+
let low = 0;
|
|
765
|
+
let high = files.length - 1;
|
|
766
|
+
while (low < high) {
|
|
767
|
+
const middle = low + high + 1 >> 1;
|
|
768
|
+
if (files[middle].firstNumber <= number) low = middle;
|
|
769
|
+
else high = middle - 1;
|
|
770
|
+
}
|
|
771
|
+
return files[low];
|
|
772
|
+
}
|
|
773
|
+
function idInFile(file, position) {
|
|
774
|
+
file.idOffsets.read(position * 4, idOffsetBytes);
|
|
775
|
+
const start = idOffsetView.getUint32(0, true);
|
|
776
|
+
const end = idOffsetView.getUint32(4, true);
|
|
777
|
+
if (end < start) loadFailed(`The ID offsets of "${file.file.path}" descend at position ${position}`);
|
|
778
|
+
const encoded = file.ids.bytesAt(start, end - start);
|
|
779
|
+
const id = shortAsciiIdOf(encoded) ?? decode(encoded);
|
|
780
|
+
if (typeof id !== "string") loadFailed(`Position ${position} of "${file.file.path}" holds no ID`);
|
|
781
|
+
return id;
|
|
782
|
+
}
|
|
783
|
+
return {
|
|
784
|
+
dimension: input.dimension,
|
|
785
|
+
metric: input.metric,
|
|
786
|
+
total,
|
|
787
|
+
codes,
|
|
788
|
+
graph,
|
|
789
|
+
files,
|
|
790
|
+
parts,
|
|
791
|
+
fileOfNumber,
|
|
792
|
+
topLayerOf(number) {
|
|
793
|
+
const part = parts[Math.floor(number / COLD_GRAPH_PART_VECTORS)];
|
|
794
|
+
if (part === void 0) return -1;
|
|
795
|
+
const at = number % COLD_GRAPH_PART_VECTORS;
|
|
796
|
+
return at < part.levels.length ? part.levels[at] - 1 : -1;
|
|
797
|
+
},
|
|
798
|
+
idAt(number) {
|
|
799
|
+
const file = fileOfNumber(number);
|
|
800
|
+
return idInFile(file, number - file.firstNumber);
|
|
801
|
+
},
|
|
802
|
+
vectorAt(number) {
|
|
803
|
+
const file = fileOfNumber(number);
|
|
804
|
+
const bytes = new Uint8Array(stride);
|
|
805
|
+
file.vectors.read((number - file.firstNumber) * stride, bytes);
|
|
806
|
+
return bytesToVectors(bytes);
|
|
807
|
+
},
|
|
808
|
+
reusedVectorAt(number) {
|
|
809
|
+
const file = fileOfNumber(number);
|
|
810
|
+
file.vectors.read((number - file.firstNumber) * stride, reusedBytes);
|
|
811
|
+
return reusedVector.buffer === reusedBytes.buffer ? reusedVector : bytesToVectors(reusedBytes);
|
|
812
|
+
},
|
|
813
|
+
isDeadNumber(number) {
|
|
814
|
+
const file = fileOfNumber(number);
|
|
815
|
+
return isDead(file.dead, number - file.firstNumber);
|
|
816
|
+
},
|
|
817
|
+
fetchRows(numbers, sectionOf, rowBytes) {
|
|
818
|
+
const positionsByFile = /* @__PURE__ */ new Map();
|
|
819
|
+
for (const number of numbers) {
|
|
820
|
+
const file = fileOfNumber(number);
|
|
821
|
+
const positions = positionsByFile.get(file);
|
|
822
|
+
const position = (number - file.firstNumber) * rowBytes;
|
|
823
|
+
if (positions === void 0) positionsByFile.set(file, [position]);
|
|
824
|
+
else positions.push(position);
|
|
825
|
+
}
|
|
826
|
+
const reads = [];
|
|
827
|
+
for (const [file, positions] of positionsByFile) {
|
|
828
|
+
const read = sectionOf(file)?.fetchRows(positions, rowBytes) ?? null;
|
|
829
|
+
if (read !== null) reads.push(read);
|
|
830
|
+
}
|
|
831
|
+
return reads.length === 0 ? null : Promise.all(reads);
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
function utf8BytesOfEncodedId(encodedBytes) {
|
|
836
|
+
if (encodedBytes <= FIXSTR_MAX_BYTES + 1) return encodedBytes - 1;
|
|
837
|
+
if (encodedBytes <= STR8_MAX_BYTES + 2) return encodedBytes - 2;
|
|
838
|
+
if (encodedBytes <= STR16_MAX_BYTES + 3) return encodedBytes - 3;
|
|
839
|
+
return encodedBytes - 5;
|
|
840
|
+
}
|
|
841
|
+
function claimsOfLoad(data) {
|
|
842
|
+
const offsets = new Uint8Array(SEGMENT_PAGE_BYTES);
|
|
843
|
+
const view = new DataView(offsets.buffer);
|
|
844
|
+
const offsetsPerRead = offsets.byteLength / ID_OFFSET_BYTES - 1;
|
|
845
|
+
let slots = 0;
|
|
846
|
+
let docIdBytes = 0;
|
|
847
|
+
for (const file of data.files) {
|
|
848
|
+
for (let first = 0; first < file.count; first += offsetsPerRead) {
|
|
849
|
+
const count = Math.min(offsetsPerRead, file.count - first);
|
|
850
|
+
file.idOffsets.read(first * ID_OFFSET_BYTES, offsets.subarray(0, (count + 1) * ID_OFFSET_BYTES));
|
|
851
|
+
for (let at = 0; at < count; at++) {
|
|
852
|
+
const position = first + at;
|
|
853
|
+
if (isDead(file.dead, position) && data.topLayerOf(file.firstNumber + position) < 0) continue;
|
|
854
|
+
const start = view.getUint32(at * ID_OFFSET_BYTES, true);
|
|
855
|
+
const end = view.getUint32((at + 1) * ID_OFFSET_BYTES, true);
|
|
856
|
+
if (end <= start) loadFailed(`The ID offsets of "${file.file.path}" hold no ID at position ${position}`);
|
|
857
|
+
slots += 1;
|
|
858
|
+
docIdBytes += utf8BytesOfEncodedId(end - start);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
return { slots, docIdBytes };
|
|
863
|
+
}
|
|
864
|
+
function positionOfId(file, docId, idAt, slot, view) {
|
|
865
|
+
let low = 0;
|
|
866
|
+
let high = file.count - 1;
|
|
867
|
+
while (low <= high) {
|
|
868
|
+
const middle = low + high >> 1;
|
|
869
|
+
file.idOrder.read(middle * 4, slot);
|
|
870
|
+
const position = view.getUint32(0, true);
|
|
871
|
+
if (position >= file.count) loadFailed(`The ID order of "${file.file.path}" holds position ${position}`);
|
|
872
|
+
const order = compareCodePoints(idAt(file.firstNumber + position), docId);
|
|
873
|
+
if (order === 0) return position;
|
|
874
|
+
if (order < 0) low = middle + 1;
|
|
875
|
+
else high = middle - 1;
|
|
876
|
+
}
|
|
877
|
+
return void 0;
|
|
878
|
+
}
|
|
879
|
+
function openColdVectorField(input) {
|
|
880
|
+
const opened = openFiles(input);
|
|
881
|
+
if (opened === null) return null;
|
|
882
|
+
let graphed;
|
|
883
|
+
try {
|
|
884
|
+
graphed = openParts(input);
|
|
885
|
+
} catch (error) {
|
|
886
|
+
closeHeldFiles(opened.files);
|
|
887
|
+
throw error;
|
|
888
|
+
}
|
|
889
|
+
if (graphed === null) {
|
|
890
|
+
closeHeldFiles(opened.files);
|
|
891
|
+
return null;
|
|
892
|
+
}
|
|
893
|
+
const { parts, graph } = graphed;
|
|
894
|
+
try {
|
|
895
|
+
validateColdLayout(input, opened, parts, graph);
|
|
896
|
+
} catch (error) {
|
|
897
|
+
closeHeldFiles(opened.files);
|
|
898
|
+
closeHeldFiles(parts);
|
|
899
|
+
throw error;
|
|
900
|
+
}
|
|
901
|
+
let activeSearches = 0;
|
|
902
|
+
let closing = false;
|
|
903
|
+
function closeAll() {
|
|
904
|
+
closing = true;
|
|
905
|
+
if (activeSearches > 0) return;
|
|
906
|
+
closeHeldFiles(opened?.files ?? []);
|
|
907
|
+
closeHeldFiles(parts);
|
|
908
|
+
}
|
|
909
|
+
if (!everyLiveVectorHoldsANode(opened.files, parts)) {
|
|
910
|
+
closeAll();
|
|
911
|
+
return null;
|
|
912
|
+
}
|
|
913
|
+
const data = createFieldData(input, opened, graph, parts);
|
|
914
|
+
let live = 0;
|
|
915
|
+
for (const file of opened.files) {
|
|
916
|
+
for (let at = 0; at < file.count; at++) if (!isDead(file.dead, at)) live += 1;
|
|
917
|
+
}
|
|
918
|
+
let residentBytes = 0;
|
|
919
|
+
for (const part of parts) residentBytes += part.levels.byteLength + part.nodeOffsets.byteLength;
|
|
920
|
+
let encodedIdBytes = 0;
|
|
921
|
+
for (const file of opened.files) encodedIdBytes += file.ids.length;
|
|
922
|
+
let claimsOnceRead = null;
|
|
923
|
+
const orderSlot = new Uint8Array(4);
|
|
924
|
+
const orderView = new DataView(orderSlot.buffer);
|
|
925
|
+
function liveNumberOf(docId) {
|
|
926
|
+
for (let at = data.files.length; at > 0; at--) {
|
|
927
|
+
const file = data.files[at - 1];
|
|
928
|
+
const position = positionOfId(file, docId, data.idAt, orderSlot, orderView);
|
|
929
|
+
if (position !== void 0 && !isDead(file.dead, position)) return file.firstNumber + position;
|
|
930
|
+
}
|
|
931
|
+
return void 0;
|
|
932
|
+
}
|
|
933
|
+
return {
|
|
934
|
+
liveCount: live,
|
|
935
|
+
get residentBytes() {
|
|
936
|
+
let bytes = residentBytes;
|
|
937
|
+
for (const file of opened.files) bytes += cachedFileBytes(file.file);
|
|
938
|
+
for (const part of parts) bytes += cachedFileBytes(part.file);
|
|
939
|
+
return bytes;
|
|
940
|
+
},
|
|
941
|
+
claimsAtMost: { slots: data.total, docIdBytes: encodedIdBytes },
|
|
942
|
+
claims() {
|
|
943
|
+
claimsOnceRead ??= claimsOfLoad(data);
|
|
944
|
+
return claimsOnceRead;
|
|
945
|
+
},
|
|
946
|
+
numberOf: liveNumberOf,
|
|
947
|
+
vectorOfNumber: (number) => data.vectorAt(number),
|
|
948
|
+
reusedVectorOfNumber: (number) => data.reusedVectorAt(number),
|
|
949
|
+
partitionOf: (docId) => input.resolvePartition(docId),
|
|
950
|
+
filePaths: () => [...input.files, ...input.graphParts].map((file) => file.path),
|
|
951
|
+
search: (query, k, options, accepts, excluded) => searchColdGraph(data, query, k, options, accepts, excluded),
|
|
952
|
+
async searchConcurrently(query, k, options, accepts, excluded) {
|
|
953
|
+
if (closing) loadFailed("The disk vector field is closed");
|
|
954
|
+
activeSearches += 1;
|
|
955
|
+
try {
|
|
956
|
+
return await searchColdGraphConcurrently(data, query, k, options, accepts, excluded);
|
|
957
|
+
} finally {
|
|
958
|
+
activeSearches -= 1;
|
|
959
|
+
if (closing) closeAll();
|
|
960
|
+
}
|
|
961
|
+
},
|
|
962
|
+
close: closeAll
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// src/persistence/paged/cold-load.ts
|
|
967
|
+
var FILE_PAGE_SIZES = /* @__PURE__ */ new Set([SEGMENT_PAGE_BYTES, RECORD_PAGE_BYTES]);
|
|
968
|
+
var FLOAT32_BYTES = 4;
|
|
969
|
+
var FLOAT64_BYTES2 = 8;
|
|
970
|
+
function waitBeforeColdLoad(loadingWanted) {
|
|
971
|
+
return new Promise((resolve) => {
|
|
972
|
+
const timer = setTimeout(resolve, COLD_LOAD_WAIT_WITHOUT_SEARCH_MS);
|
|
973
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
974
|
+
void loadingWanted.then(() => {
|
|
975
|
+
clearTimeout(timer);
|
|
976
|
+
resolve();
|
|
977
|
+
});
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
function sectionsOfFile(input) {
|
|
981
|
+
const { ref, path } = input;
|
|
982
|
+
const { headOffset, headLength } = ref;
|
|
983
|
+
if (headOffset === void 0 || headLength === void 0) loadFailed(`The vector file "${ref.key}" holds no head`);
|
|
984
|
+
const head = new Uint8Array(headLength);
|
|
985
|
+
readFileRange(path, HEADER_SIZE + headOffset, head);
|
|
986
|
+
const raw = decode(head);
|
|
987
|
+
const list = decodeSections(raw.sections, ref.bytes - HEADER_SIZE, `head of "${ref.key}"`, FILE_PAGE_SIZES);
|
|
988
|
+
return new Map(list.map((section) => [section.name, section]));
|
|
989
|
+
}
|
|
990
|
+
function requiredSection2(sections, name, key) {
|
|
991
|
+
const section = sections.get(name);
|
|
992
|
+
if (section === void 0) loadFailed(`The head of "${key}" lists no section "${name}"`);
|
|
993
|
+
return section;
|
|
994
|
+
}
|
|
995
|
+
function magnitudesOf(bytes) {
|
|
996
|
+
const values = new Float64Array(bytes.byteLength / FLOAT64_BYTES2);
|
|
997
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
998
|
+
for (let at = 0; at < values.length; at++) values[at] = view.getFloat64(at * FLOAT64_BYTES2, true);
|
|
999
|
+
return values;
|
|
1000
|
+
}
|
|
1001
|
+
async function loadFilesByParts(files, restore, fieldPath, dimension, stillWanted) {
|
|
1002
|
+
for (const input of files) {
|
|
1003
|
+
await yieldToEventLoop();
|
|
1004
|
+
if (!stillWanted()) return;
|
|
1005
|
+
const { ref, path } = input;
|
|
1006
|
+
const sections = sectionsOfFile(input);
|
|
1007
|
+
const idBytes = readWholeSectionChecked(path, requiredSection2(sections, "docIds", ref.key), false);
|
|
1008
|
+
const docIds = decode(idBytes);
|
|
1009
|
+
if (!Array.isArray(docIds) || docIds.length !== ref.count) {
|
|
1010
|
+
loadFailed(`The vector file "${ref.key}" lists the wrong number of IDs`);
|
|
1011
|
+
}
|
|
1012
|
+
const recordSection = sections.get("records");
|
|
1013
|
+
const records = recordSection === void 0 ? null : readWholeSectionChecked(path, recordSection, false);
|
|
1014
|
+
const magnitudes = magnitudesOf(
|
|
1015
|
+
readWholeSectionChecked(path, requiredSection2(sections, "magnitudes", ref.key), false)
|
|
1016
|
+
);
|
|
1017
|
+
const vectorSection = requiredSection2(sections, "vectors", ref.key);
|
|
1018
|
+
const stride = dimension * FLOAT32_BYTES;
|
|
1019
|
+
await checkEverySectionPage(path, vectorSection, stillWanted);
|
|
1020
|
+
if (!stillWanted()) return;
|
|
1021
|
+
const fileCodes = codesOfFile(path, sections, ref.key);
|
|
1022
|
+
restore.addFile({
|
|
1023
|
+
id: ref.id,
|
|
1024
|
+
key: ref.key,
|
|
1025
|
+
payload: {
|
|
1026
|
+
v: VECTOR_FILE_PAYLOAD_VERSION,
|
|
1027
|
+
fieldName: fieldPath,
|
|
1028
|
+
dimension,
|
|
1029
|
+
docIds,
|
|
1030
|
+
codes: records === null || fileCodes === null ? null : { bits: fileCodes.bits, centroid: Array.from(fileCodes.centroid), records },
|
|
1031
|
+
vectors: new Uint8Array(0)
|
|
1032
|
+
},
|
|
1033
|
+
dead: ref.dead,
|
|
1034
|
+
location: { path, vectorsOffset: HEADER_SIZE + vectorSection.offset },
|
|
1035
|
+
byParts: {
|
|
1036
|
+
magnitudes,
|
|
1037
|
+
vectorAt(position) {
|
|
1038
|
+
const bytes = new Uint8Array(stride);
|
|
1039
|
+
readFileRange(path, HEADER_SIZE + vectorSection.offset + position * stride, bytes);
|
|
1040
|
+
return bytesToVectors(bytes);
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
await yieldToEventLoop();
|
|
1046
|
+
if (stillWanted()) restore.finish();
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
// src/persistence/durability/segment/vector-load.ts
|
|
1050
|
+
var VECTOR_FILE_ENVELOPE_VERSION = 2;
|
|
1051
|
+
var GRAPH_PART_ENVELOPE_VERSION = 2;
|
|
1052
|
+
function liveVectorsOf(ref) {
|
|
1053
|
+
let live = 0;
|
|
1054
|
+
for (const file of ref.files) live += file.count - countDead(file.dead, file.count);
|
|
1055
|
+
return live;
|
|
1056
|
+
}
|
|
1057
|
+
async function openListedFile(store, ref, what, allowsCompression) {
|
|
1058
|
+
const bytes = await readListedFile(store, ref.key, { bytes: ref.bytes, what });
|
|
1059
|
+
const { header, payload } = await openChecksummedEnvelope(bytes, {
|
|
1060
|
+
envelopeFormatVersion: VECTOR_FILE_ENVELOPE_VERSION,
|
|
1061
|
+
what: `${what} "${ref.key}"`,
|
|
1062
|
+
allowsCompression
|
|
1063
|
+
});
|
|
1064
|
+
requireListedChecksum(what, ref.key, header.checksum, ref.checksum);
|
|
1065
|
+
return { payload, payloadLength: header.payloadLength };
|
|
1066
|
+
}
|
|
1067
|
+
async function readGraphParts(store, ref, stillWanted = () => true) {
|
|
1068
|
+
if (ref.graphParts.length === 0) return null;
|
|
1069
|
+
const parts = [];
|
|
1070
|
+
for (const part of ref.graphParts) {
|
|
1071
|
+
if (!stillWanted()) return null;
|
|
1072
|
+
const { payload } = await openListedFile(store, part, "graph part", true);
|
|
1073
|
+
const decoded = decodeVectorGraphPartPayload(decode(payload));
|
|
1074
|
+
if (decoded.fieldName !== ref.fieldPath) {
|
|
1075
|
+
throw new NarsilError(
|
|
1076
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1077
|
+
`The graph part "${part.key}" names field "${decoded.fieldName}" where the manifest lists "${ref.fieldPath}"`,
|
|
1078
|
+
{ key: part.key }
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
parts.push(decoded);
|
|
1082
|
+
}
|
|
1083
|
+
return assembleGraphParts(parts);
|
|
1084
|
+
}
|
|
1085
|
+
async function readVectorFile(store, file, fieldPath) {
|
|
1086
|
+
const { payload: bytes, payloadLength } = await openListedFile(store, file, "vector file", false);
|
|
1087
|
+
const payload = decodeVectorFilePayload(decode(bytes));
|
|
1088
|
+
if (payload.docIds.length !== file.count) {
|
|
1089
|
+
throw new NarsilError(
|
|
1090
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1091
|
+
`The vector file "${file.key}" holds ${payload.docIds.length} vectors where the manifest counts ${file.count}`,
|
|
1092
|
+
{ key: file.key, held: payload.docIds.length, count: file.count }
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
if (payload.fieldName !== fieldPath) {
|
|
1096
|
+
throw new NarsilError(
|
|
1097
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1098
|
+
`The vector file "${file.key}" names field "${payload.fieldName}" where the manifest lists "${fieldPath}"`,
|
|
1099
|
+
{ key: file.key }
|
|
1100
|
+
);
|
|
1101
|
+
}
|
|
1102
|
+
return { payload, payloadLength };
|
|
1103
|
+
}
|
|
1104
|
+
function holdsEveryHead(ref) {
|
|
1105
|
+
if (ref.graphParts.length === 0 || ref.files.length === 0) return false;
|
|
1106
|
+
return [...ref.files, ...ref.graphParts].every((file) => file.headOffset !== void 0);
|
|
1107
|
+
}
|
|
1108
|
+
async function loadFieldOnDemand(store, ref, vectorIndex, resolvePartition) {
|
|
1109
|
+
const { pathOf } = store;
|
|
1110
|
+
if (pathOf === void 0 || vectorIndex.storage !== "disk" || !holdsEveryHead(ref)) return false;
|
|
1111
|
+
await Promise.all([
|
|
1112
|
+
...ref.files.map((file) => requireListedSize(store, file.key, { bytes: file.bytes, what: "vector file" })),
|
|
1113
|
+
...ref.graphParts.map((part) => requireListedSize(store, part.key, { bytes: part.bytes, what: "graph part" }))
|
|
1114
|
+
]);
|
|
1115
|
+
const files = await Promise.all(
|
|
1116
|
+
ref.files.map(async (file) => ({ ref: file, path: await pathOf.call(store, file.key) }))
|
|
1117
|
+
);
|
|
1118
|
+
const graphParts = await Promise.all(
|
|
1119
|
+
ref.graphParts.map(async (part) => ({ ref: part, path: await pathOf.call(store, part.key) }))
|
|
1120
|
+
);
|
|
1121
|
+
const field = openColdVectorField({
|
|
1122
|
+
fieldPath: ref.fieldPath,
|
|
1123
|
+
dimension: vectorIndex.dimension,
|
|
1124
|
+
metric: vectorIndex.metric,
|
|
1125
|
+
codeBits: osqBitsOf(vectorIndex.quantization),
|
|
1126
|
+
files,
|
|
1127
|
+
graphParts,
|
|
1128
|
+
resolvePartition
|
|
1129
|
+
});
|
|
1130
|
+
if (field === null) return false;
|
|
1131
|
+
vectorIndex.openColdField(field, async (beginRestore, stillWanted, loadingWanted) => {
|
|
1132
|
+
await waitBeforeColdLoad(loadingWanted);
|
|
1133
|
+
if (!stillWanted()) return;
|
|
1134
|
+
const graph = await readGraphParts(store, ref, stillWanted);
|
|
1135
|
+
if (!stillWanted()) return;
|
|
1136
|
+
const restore = beginRestore({ liveVectors: liveVectorsOf(ref), holdsGraph: true, graph });
|
|
1137
|
+
await loadFilesByParts(files, restore, ref.fieldPath, vectorIndex.dimension, stillWanted);
|
|
1138
|
+
});
|
|
1139
|
+
return true;
|
|
1140
|
+
}
|
|
1141
|
+
async function loadVectorField(store, ref, vectorIndex, resolvePartition = null) {
|
|
1142
|
+
if (resolvePartition !== null && await loadFieldOnDemand(store, ref, vectorIndex, resolvePartition)) return;
|
|
1143
|
+
const restore = vectorIndex.restoreCheckpoint({
|
|
1144
|
+
liveVectors: liveVectorsOf(ref),
|
|
1145
|
+
holdsGraph: ref.graphParts.length > 0,
|
|
1146
|
+
graph: await readGraphParts(store, ref)
|
|
1147
|
+
});
|
|
1148
|
+
for (const file of ref.files) {
|
|
1149
|
+
const { payload, payloadLength } = await readVectorFile(store, file, ref.fieldPath);
|
|
1150
|
+
restore.addFile({
|
|
1151
|
+
id: file.id,
|
|
1152
|
+
key: file.key,
|
|
1153
|
+
payload,
|
|
1154
|
+
dead: file.dead,
|
|
1155
|
+
location: store.pathOf === void 0 ? null : {
|
|
1156
|
+
path: await store.pathOf(file.key),
|
|
1157
|
+
vectorsOffset: HEADER_SIZE + payloadLength - payload.vectors.byteLength
|
|
1158
|
+
}
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
restore.finish();
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
export { GRAPH_PART_ENVELOPE_VERSION, VECTOR_FILE_ENVELOPE_VERSION, loadFilesByParts, loadVectorField, openColdVectorField, readGraphParts, readVectorFile, waitBeforeColdLoad };
|