@delali/narsil 0.3.1-next.98 → 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,3230 @@
|
|
|
1
|
+
// src/engine/optional-reads-host-bindings.ts
|
|
2
|
+
var bitsetClear;
|
|
3
|
+
var bitsetFromSet;
|
|
4
|
+
var bitsetSet;
|
|
5
|
+
var buildStoredValues;
|
|
6
|
+
var captureFrozen;
|
|
7
|
+
var captureSegment;
|
|
8
|
+
var checkedSize;
|
|
9
|
+
var clampLimit;
|
|
10
|
+
var clampRowCount;
|
|
11
|
+
var codePointCount;
|
|
12
|
+
var compareCodePoints;
|
|
13
|
+
var compareScores;
|
|
14
|
+
var createBitSet;
|
|
15
|
+
var createPatternWorkMeter;
|
|
16
|
+
var decodePageCursor;
|
|
17
|
+
var DEFAULT_MAX_PER_GROUP;
|
|
18
|
+
var DEFAULT_SUGGEST_LIMIT;
|
|
19
|
+
var defaultSortMode;
|
|
20
|
+
var encodePageCursor;
|
|
21
|
+
var ErrorCodes;
|
|
22
|
+
var everyValueFacetConfig;
|
|
23
|
+
var expansionSource;
|
|
24
|
+
var flattenSchema;
|
|
25
|
+
var fulltextMatches;
|
|
26
|
+
var getFlatSchema;
|
|
27
|
+
var isGeopointOrVectorType;
|
|
28
|
+
var isListBase;
|
|
29
|
+
var isSortMode;
|
|
30
|
+
var isStringValuedBase;
|
|
31
|
+
var keptFacetValues;
|
|
32
|
+
var listBindingOf;
|
|
33
|
+
var liveFrequency;
|
|
34
|
+
var loadSuggestions;
|
|
35
|
+
var lookupExact;
|
|
36
|
+
var lookupPrefix;
|
|
37
|
+
var lowerBound;
|
|
38
|
+
var MAX_SUGGEST_LIMIT;
|
|
39
|
+
var MAX_SUGGEST_SCATTER_LIMIT;
|
|
40
|
+
var mergeFacets;
|
|
41
|
+
var mergeText;
|
|
42
|
+
var NarsilError;
|
|
43
|
+
var normalizeSort;
|
|
44
|
+
var now;
|
|
45
|
+
var parsedTypeOf;
|
|
46
|
+
var partitionDocumentFrequency;
|
|
47
|
+
var partitionsIn;
|
|
48
|
+
var readFrozenSource;
|
|
49
|
+
var readMergeSegment;
|
|
50
|
+
var readStoredFields;
|
|
51
|
+
var regionsOverlap;
|
|
52
|
+
var requireMatchingCursor;
|
|
53
|
+
var requireRoom;
|
|
54
|
+
var requireSegment;
|
|
55
|
+
var requireWithinResultWindow;
|
|
56
|
+
var resolveProjection;
|
|
57
|
+
var SegmentRefusal;
|
|
58
|
+
var SORT_COLUMN_MINIMUM_REBUILD_THRESHOLD;
|
|
59
|
+
var SORT_COLUMN_REBUILD_FRACTION_SHIFT;
|
|
60
|
+
var SORT_MODES;
|
|
61
|
+
var SORT_VALUE_MAX_CODE_POINTS;
|
|
62
|
+
var sortedPageOf;
|
|
63
|
+
var sortModesOf;
|
|
64
|
+
var sortSignatureOf;
|
|
65
|
+
var storedText;
|
|
66
|
+
var storedValuesOf;
|
|
67
|
+
var subMatchesView;
|
|
68
|
+
var termCount;
|
|
69
|
+
var termText;
|
|
70
|
+
var toComparableSortValue;
|
|
71
|
+
var tokenize;
|
|
72
|
+
var truncateSortString;
|
|
73
|
+
var turnIsSpent;
|
|
74
|
+
var typedRegion;
|
|
75
|
+
var undeclaredFieldOnStrictIndex;
|
|
76
|
+
var validQueryFilter;
|
|
77
|
+
var validStoredValue;
|
|
78
|
+
var VECTOR_PATTERN;
|
|
79
|
+
var withSortableOption;
|
|
80
|
+
var yieldTurn;
|
|
81
|
+
function bindOptionalReadsHost(host) {
|
|
82
|
+
({
|
|
83
|
+
bitsetClear,
|
|
84
|
+
bitsetFromSet,
|
|
85
|
+
bitsetSet,
|
|
86
|
+
buildStoredValues,
|
|
87
|
+
captureFrozen,
|
|
88
|
+
captureSegment,
|
|
89
|
+
checkedSize,
|
|
90
|
+
clampLimit,
|
|
91
|
+
clampRowCount,
|
|
92
|
+
codePointCount,
|
|
93
|
+
compareCodePoints,
|
|
94
|
+
compareScores,
|
|
95
|
+
createBitSet,
|
|
96
|
+
createPatternWorkMeter,
|
|
97
|
+
decodePageCursor,
|
|
98
|
+
DEFAULT_MAX_PER_GROUP,
|
|
99
|
+
DEFAULT_SUGGEST_LIMIT,
|
|
100
|
+
defaultSortMode,
|
|
101
|
+
encodePageCursor,
|
|
102
|
+
ErrorCodes,
|
|
103
|
+
everyValueFacetConfig,
|
|
104
|
+
expansionSource,
|
|
105
|
+
flattenSchema,
|
|
106
|
+
fulltextMatches,
|
|
107
|
+
getFlatSchema,
|
|
108
|
+
isGeopointOrVectorType,
|
|
109
|
+
isListBase,
|
|
110
|
+
isSortMode,
|
|
111
|
+
isStringValuedBase,
|
|
112
|
+
keptFacetValues,
|
|
113
|
+
listBindingOf,
|
|
114
|
+
liveFrequency,
|
|
115
|
+
loadSuggestions,
|
|
116
|
+
lookupExact,
|
|
117
|
+
lookupPrefix,
|
|
118
|
+
lowerBound,
|
|
119
|
+
MAX_SUGGEST_LIMIT,
|
|
120
|
+
MAX_SUGGEST_SCATTER_LIMIT,
|
|
121
|
+
mergeFacets,
|
|
122
|
+
mergeText,
|
|
123
|
+
NarsilError,
|
|
124
|
+
normalizeSort,
|
|
125
|
+
now,
|
|
126
|
+
parsedTypeOf,
|
|
127
|
+
partitionDocumentFrequency,
|
|
128
|
+
partitionsIn,
|
|
129
|
+
readFrozenSource,
|
|
130
|
+
readMergeSegment,
|
|
131
|
+
readStoredFields,
|
|
132
|
+
regionsOverlap,
|
|
133
|
+
requireMatchingCursor,
|
|
134
|
+
requireRoom,
|
|
135
|
+
requireSegment,
|
|
136
|
+
requireWithinResultWindow,
|
|
137
|
+
resolveProjection,
|
|
138
|
+
SegmentRefusal,
|
|
139
|
+
SORT_COLUMN_MINIMUM_REBUILD_THRESHOLD,
|
|
140
|
+
SORT_COLUMN_REBUILD_FRACTION_SHIFT,
|
|
141
|
+
SORT_MODES,
|
|
142
|
+
SORT_VALUE_MAX_CODE_POINTS,
|
|
143
|
+
sortedPageOf,
|
|
144
|
+
sortModesOf,
|
|
145
|
+
sortSignatureOf,
|
|
146
|
+
storedText,
|
|
147
|
+
storedValuesOf,
|
|
148
|
+
subMatchesView,
|
|
149
|
+
termCount,
|
|
150
|
+
termText,
|
|
151
|
+
toComparableSortValue,
|
|
152
|
+
tokenize,
|
|
153
|
+
truncateSortString,
|
|
154
|
+
turnIsSpent,
|
|
155
|
+
typedRegion,
|
|
156
|
+
undeclaredFieldOnStrictIndex,
|
|
157
|
+
validQueryFilter,
|
|
158
|
+
validStoredValue,
|
|
159
|
+
VECTOR_PATTERN,
|
|
160
|
+
withSortableOption,
|
|
161
|
+
yieldTurn
|
|
162
|
+
} = host);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/core/stored-values/compare.ts
|
|
166
|
+
function compareText(values, left, right) {
|
|
167
|
+
const leftLength = values.textLength[left];
|
|
168
|
+
const rightLength = values.textLength[right];
|
|
169
|
+
const length = Math.min(leftLength, rightLength);
|
|
170
|
+
const leftStart = values.textStart[left];
|
|
171
|
+
const rightStart = values.textStart[right];
|
|
172
|
+
const region = values.kinds[left] === 5 ? values.bytes : values.units;
|
|
173
|
+
for (let at = 0; at < length; at++) {
|
|
174
|
+
const order = region[leftStart + at] - region[rightStart + at];
|
|
175
|
+
if (order !== 0) return order;
|
|
176
|
+
}
|
|
177
|
+
return leftLength - rightLength;
|
|
178
|
+
}
|
|
179
|
+
function compareStoredElements(values, left, right) {
|
|
180
|
+
const kind = values.kinds[left];
|
|
181
|
+
if (kind !== values.kinds[right]) return kind - values.kinds[right];
|
|
182
|
+
if (kind === 4 || kind === 5) return compareText(values, left, right);
|
|
183
|
+
if (kind !== 3) return 0;
|
|
184
|
+
const first = values.numbers[left];
|
|
185
|
+
const second = values.numbers[right];
|
|
186
|
+
if (first === second) return 0;
|
|
187
|
+
if (Number.isNaN(first)) return Number.isNaN(second) ? 0 : 1;
|
|
188
|
+
return Number.isNaN(second) || first < second ? -1 : 1;
|
|
189
|
+
}
|
|
190
|
+
function effectiveShape(values, ordinal) {
|
|
191
|
+
const shape = values.shapes[ordinal];
|
|
192
|
+
return shape === 1 && values.kinds[values.starts[ordinal]] === 0 ? 0 : shape;
|
|
193
|
+
}
|
|
194
|
+
function compareStoredValues(values, left, right) {
|
|
195
|
+
const shape = effectiveShape(values, left);
|
|
196
|
+
const otherShape = effectiveShape(values, right);
|
|
197
|
+
if (shape !== otherShape) return shape - otherShape;
|
|
198
|
+
if (shape === 0) return 0;
|
|
199
|
+
const first = values.starts[left];
|
|
200
|
+
const second = values.starts[right];
|
|
201
|
+
const firstLength = values.starts[left + 1] - first;
|
|
202
|
+
const secondLength = values.starts[right + 1] - second;
|
|
203
|
+
const length = Math.min(firstLength, secondLength);
|
|
204
|
+
for (let at = 0; at < length; at++) {
|
|
205
|
+
const order = compareStoredElements(values, first + at, second + at);
|
|
206
|
+
if (order !== 0) return order;
|
|
207
|
+
}
|
|
208
|
+
return firstLength - secondLength;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/core/groups/portable.ts
|
|
212
|
+
function unsigned(value) {
|
|
213
|
+
return Number.isInteger(value) && value >= 0 && value <= 4294967295;
|
|
214
|
+
}
|
|
215
|
+
var groupHits = (values, inputOrdinals, inputScores, inputIdRanks, maxPerGroup, limit, outputHeads, outputOffsets, outputHits, outputMembers) => {
|
|
216
|
+
if (!unsigned(maxPerGroup) || limit !== null && !unsigned(limit)) return -1;
|
|
217
|
+
const fields = readStoredFields(values);
|
|
218
|
+
const ordinals = typedRegion(inputOrdinals, "Uint32Array");
|
|
219
|
+
const scores = typedRegion(inputScores, "Float64Array");
|
|
220
|
+
const idRanks = typedRegion(inputIdRanks, "Uint32Array");
|
|
221
|
+
const outHeads = typedRegion(outputHeads, "Uint32Array");
|
|
222
|
+
const outOffsets = typedRegion(outputOffsets, "Uint32Array");
|
|
223
|
+
const outHits = typedRegion(outputHits, "Uint32Array");
|
|
224
|
+
const outMembers = typedRegion(outputMembers, "Uint32Array");
|
|
225
|
+
if (fields === null || ordinals === null || scores === null || idRanks === null || outHeads === null || outOffsets === null || outHits === null || outMembers === null || !unsigned(ordinals.length) || scores.length !== ordinals.length || idRanks.length !== ordinals.length)
|
|
226
|
+
return -1;
|
|
227
|
+
const inputs = [ordinals, scores, idRanks];
|
|
228
|
+
for (const field of fields) inputs.push(...Object.values(field));
|
|
229
|
+
const outputs = [outHeads, outOffsets, outHits, outMembers];
|
|
230
|
+
for (let target = 0; target < outputs.length; target++) {
|
|
231
|
+
if (inputs.some((input) => regionsOverlap(outputs[target], input))) return -1;
|
|
232
|
+
for (let prior = 0; prior < target; prior++) if (regionsOverlap(outputs[target], outputs[prior])) return -1;
|
|
233
|
+
}
|
|
234
|
+
const compare2 = (left, right) => {
|
|
235
|
+
for (const field of fields) {
|
|
236
|
+
const order = compareStoredValues(field, ordinals[left], ordinals[right]);
|
|
237
|
+
if (order !== 0) return order;
|
|
238
|
+
}
|
|
239
|
+
return 0;
|
|
240
|
+
};
|
|
241
|
+
const positions = [];
|
|
242
|
+
for (let at = 0; at < ordinals.length; at++) {
|
|
243
|
+
if (fields.every((field) => validStoredValue(field, ordinals[at]))) positions.push(at);
|
|
244
|
+
}
|
|
245
|
+
positions.sort((left, right) => compare2(left, right) || left - right);
|
|
246
|
+
const groups = [];
|
|
247
|
+
for (let first = 0; first < positions.length; ) {
|
|
248
|
+
let end = first + 1;
|
|
249
|
+
while (end < positions.length && compare2(positions[first], positions[end]) === 0) end++;
|
|
250
|
+
groups.push({ first, end, head: positions[first] });
|
|
251
|
+
first = end;
|
|
252
|
+
}
|
|
253
|
+
groups.sort(
|
|
254
|
+
(left, right) => compareScores(scores[left.head], scores[right.head]) || idRanks[left.head] - idRanks[right.head] || left.head - right.head
|
|
255
|
+
);
|
|
256
|
+
const count = Math.min(limit ?? groups.length, groups.length);
|
|
257
|
+
if (outHeads.length < count || outMembers.length < count || outOffsets.length < count + 1) return -3;
|
|
258
|
+
let needed = 0;
|
|
259
|
+
for (let at = 0; at < count; at++) needed += Math.min(maxPerGroup, groups[at].end - groups[at].first);
|
|
260
|
+
if (outHits.length < needed) return -3;
|
|
261
|
+
let position = 0;
|
|
262
|
+
outOffsets[0] = 0;
|
|
263
|
+
for (let at = 0; at < count; at++) {
|
|
264
|
+
const group = groups[at];
|
|
265
|
+
const retained = Math.min(maxPerGroup, group.end - group.first);
|
|
266
|
+
outHeads[at] = group.head;
|
|
267
|
+
outMembers[at] = group.end - group.first;
|
|
268
|
+
for (let member = 0; member < retained; member++) outHits[position++] = positions[group.first + member];
|
|
269
|
+
outOffsets[at + 1] = position;
|
|
270
|
+
}
|
|
271
|
+
return count;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
// src/core/expansion/constants.ts
|
|
275
|
+
var PREFIX_BUDGET = 50;
|
|
276
|
+
var PARTIAL_BUDGET = 50;
|
|
277
|
+
var PARTIAL_RUN_POINTS = 3;
|
|
278
|
+
|
|
279
|
+
// src/core/expansion/regions.ts
|
|
280
|
+
function expansionText(input) {
|
|
281
|
+
const value = typedRegion(input, "Uint16Array");
|
|
282
|
+
requireSegment(value !== null && value.length <= 1073741824);
|
|
283
|
+
const text = storedText(value, 0, value.length);
|
|
284
|
+
for (let at = 0; at < text.length; at++) {
|
|
285
|
+
const unit = text.charCodeAt(at);
|
|
286
|
+
if (unit < 55296 || unit >= 57344) continue;
|
|
287
|
+
requireSegment(unit < 56320 && ++at < text.length);
|
|
288
|
+
requireSegment(text.charCodeAt(at) >= 56320 && text.charCodeAt(at) < 57344);
|
|
289
|
+
}
|
|
290
|
+
return text;
|
|
291
|
+
}
|
|
292
|
+
function captureExpansionSource(input) {
|
|
293
|
+
requireSegment(typeof input === "object" && input !== null);
|
|
294
|
+
const value = { ...input };
|
|
295
|
+
if (value.growing !== null) {
|
|
296
|
+
requireSegment(value.frozen === null && value.state === null && value.occurrences == null);
|
|
297
|
+
return { growing: captureSegment(value.growing), frozen: null, state: null };
|
|
298
|
+
}
|
|
299
|
+
requireSegment(value.frozen !== null && value.state !== null);
|
|
300
|
+
const { raw, writable } = captureFrozen(value.frozen, value.state);
|
|
301
|
+
return { growing: null, frozen: raw, state: writable, occurrences: value.occurrences };
|
|
302
|
+
}
|
|
303
|
+
function readExpansion(inputs, text, outSegments, outTokens, outFactors) {
|
|
304
|
+
requireSegment(Array.isArray(inputs) && inputs.length <= 4294967295);
|
|
305
|
+
const captured = inputs.map(captureExpansionSource);
|
|
306
|
+
const arrays = [...text];
|
|
307
|
+
const sources = captured.map((source) => {
|
|
308
|
+
if (source.growing !== null) {
|
|
309
|
+
const value2 = readMergeSegment(source.growing);
|
|
310
|
+
arrays.push(...value2.arrays);
|
|
311
|
+
return { growing: value2.segment, frozen: null, state: null };
|
|
312
|
+
}
|
|
313
|
+
requireSegment(source.frozen !== null && source.state !== null);
|
|
314
|
+
const value = readFrozenSource(source.frozen, source.state);
|
|
315
|
+
arrays.push(...value.arrays);
|
|
316
|
+
const occurrences = source.occurrences == null ? null : typedRegion(source.occurrences, "Float64Array");
|
|
317
|
+
requireSegment(source.occurrences == null || occurrences !== null);
|
|
318
|
+
if (occurrences !== null) {
|
|
319
|
+
requireSegment(occurrences.length === value.segment.terms.counts.length);
|
|
320
|
+
for (const count of occurrences) requireSegment(Number.isFinite(count) && count >= 0);
|
|
321
|
+
arrays.push(occurrences);
|
|
322
|
+
}
|
|
323
|
+
return { growing: null, frozen: value.segment, state: value.state, occurrences };
|
|
324
|
+
});
|
|
325
|
+
const words = text.map(expansionText);
|
|
326
|
+
const segments = typedRegion(outSegments, "Uint32Array");
|
|
327
|
+
const tokens = typedRegion(outTokens, "Uint32Array");
|
|
328
|
+
const factors = typedRegion(outFactors, "Float64Array");
|
|
329
|
+
requireSegment(segments !== null && tokens !== null && factors !== null);
|
|
330
|
+
const outputs = [segments, tokens, factors];
|
|
331
|
+
for (let at = 0; at < outputs.length; at++) {
|
|
332
|
+
for (const read of arrays) requireSegment(!regionsOverlap(outputs[at], read));
|
|
333
|
+
for (let before = 0; before < at; before++) requireSegment(!regionsOverlap(outputs[at], outputs[before]));
|
|
334
|
+
}
|
|
335
|
+
return { sources, words, segments, tokens, factors, arrays };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/core/expansion/source.ts
|
|
339
|
+
var EMPTY_BYTES = new Uint8Array(0);
|
|
340
|
+
var EMPTY_WORDS = new Uint32Array(0);
|
|
341
|
+
function expansionDictionary(source) {
|
|
342
|
+
const growing = source.growing;
|
|
343
|
+
if (growing !== null)
|
|
344
|
+
return {
|
|
345
|
+
growing: growing.terms,
|
|
346
|
+
tokens: EMPTY_BYTES,
|
|
347
|
+
offsets: EMPTY_WORDS,
|
|
348
|
+
frequencies: growing.columns.docFrequency,
|
|
349
|
+
removed: growing.columns.deadDocuments
|
|
350
|
+
};
|
|
351
|
+
const { frozen, state } = source;
|
|
352
|
+
requireSegment(frozen !== null && state !== null);
|
|
353
|
+
return {
|
|
354
|
+
growing: null,
|
|
355
|
+
tokens: frozen.terms.text,
|
|
356
|
+
offsets: frozen.terms.offsets,
|
|
357
|
+
frequencies: frozen.terms.counts,
|
|
358
|
+
removed: state.removedDocuments
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
function validExpansionText(text) {
|
|
362
|
+
if (text.length > 1073741824) return false;
|
|
363
|
+
for (let at = 0; at < text.length; at++) {
|
|
364
|
+
const unit = text.charCodeAt(at);
|
|
365
|
+
if (unit < 55296 || unit >= 57344) continue;
|
|
366
|
+
if (unit >= 56320 || ++at >= text.length) return false;
|
|
367
|
+
if (text.charCodeAt(at) < 56320 || text.charCodeAt(at) >= 57344) return false;
|
|
368
|
+
}
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
function expansionToken(dictionary, text) {
|
|
372
|
+
if (!validExpansionText(text)) return null;
|
|
373
|
+
const units3 = Uint16Array.from({ length: text.length }, (_, at) => text.charCodeAt(at));
|
|
374
|
+
const output = new Uint32Array(1);
|
|
375
|
+
const count = lookupExact(dictionary, units3, output);
|
|
376
|
+
requireSegment(count >= 0);
|
|
377
|
+
return count === 0 ? null : output[0];
|
|
378
|
+
}
|
|
379
|
+
function expansionPrefixes(dictionary, prefix) {
|
|
380
|
+
const output = new Uint32Array(termCount(dictionary));
|
|
381
|
+
const count = lookupPrefix(dictionary, prefix, 4294967295, output);
|
|
382
|
+
requireSegment(count >= 0);
|
|
383
|
+
return output.subarray(0, count);
|
|
384
|
+
}
|
|
385
|
+
function* expansionSurfaces(source) {
|
|
386
|
+
const dictionary = expansionDictionary(source);
|
|
387
|
+
const growing = source.growing;
|
|
388
|
+
if (growing !== null) {
|
|
389
|
+
const table2 = growing.surfaces;
|
|
390
|
+
if (table2 === null) return;
|
|
391
|
+
const strings = table2.strings;
|
|
392
|
+
for (let entry = 0; entry < strings.header[0]; entry++) {
|
|
393
|
+
const count = table2.count[entry];
|
|
394
|
+
if (count === 0 || table2.token[entry] >= termCount(dictionary)) continue;
|
|
395
|
+
const from = strings.termOffset[entry];
|
|
396
|
+
const length = strings.termLength[entry];
|
|
397
|
+
if (from > strings.header[4] || length > strings.header[4] - from || length > 1073741824) continue;
|
|
398
|
+
const token = termText(dictionary, table2.token[entry]);
|
|
399
|
+
if (token === null || !validExpansionText(token)) continue;
|
|
400
|
+
const spelling = storedText(strings.units, from, length);
|
|
401
|
+
if (validExpansionText(spelling)) yield { spelling, token, count, entry };
|
|
402
|
+
}
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const { frozen, state } = source;
|
|
406
|
+
requireSegment(frozen !== null && state !== null);
|
|
407
|
+
const table = frozen.surfaces;
|
|
408
|
+
if (table === null) return;
|
|
409
|
+
for (let entry = 0; entry < table.counts.length; entry++) {
|
|
410
|
+
const count = table.counts[entry] - state.removedSurfaces[entry];
|
|
411
|
+
if (count <= 0) continue;
|
|
412
|
+
const spelling = mergeText(table, entry * 2);
|
|
413
|
+
const token = mergeText(table, entry * 2 + 1);
|
|
414
|
+
if (spelling !== null && token !== null && validExpansionText(spelling) && validExpansionText(token))
|
|
415
|
+
yield { spelling, token, count, entry };
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function expansionOccurrences(source, token, surfaces) {
|
|
419
|
+
const growing = source.growing;
|
|
420
|
+
if (growing !== null) return growing.columns.totalTermFrequency[token] - growing.columns.surfaceOccurrences[token];
|
|
421
|
+
const { frozen, state } = source;
|
|
422
|
+
requireSegment(frozen !== null && state !== null);
|
|
423
|
+
const slot = frozen.payloadSlots[token];
|
|
424
|
+
if (slot >= frozen.terms.counts.length) return 0;
|
|
425
|
+
if (source.occurrences != null) return source.occurrences[slot] - state.removedOccurrences[token] - surfaces;
|
|
426
|
+
const start = frozen.postingOffsets[slot];
|
|
427
|
+
const end = frozen.postingOffsets[slot + 1];
|
|
428
|
+
if (start > end || end > frozen.postingDocuments.length) return 0;
|
|
429
|
+
let total = 0;
|
|
430
|
+
for (let at = start; at < end; at++)
|
|
431
|
+
if (frozen.postingDocuments[at] < frozen.documentCount && frozen.postingFields[at] < frozen.partialFields.length)
|
|
432
|
+
total += frozen.postingFrequencies[at];
|
|
433
|
+
return total - state.removedOccurrences[token] - surfaces;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// src/core/merges/facets.ts
|
|
437
|
+
function mergeFacets2(offsets, values, counts2, bounds, valueRanks, ascending, limit, outValues, outCounts, outBound) {
|
|
438
|
+
if (offsets.length === 0 || offsets[0] !== 0 || offsets[offsets.length - 1] !== values.length || values.length !== counts2.length || bounds.length !== offsets.length - 1 || typeof ascending !== "boolean" || limit !== null && (!Number.isInteger(limit) || limit < 0 || limit > 4294967295)) {
|
|
439
|
+
return -1;
|
|
440
|
+
}
|
|
441
|
+
const inputs = [offsets, values, counts2, bounds, valueRanks];
|
|
442
|
+
const outputs = [outValues, outCounts, outBound];
|
|
443
|
+
for (let target = 0; target < outputs.length; target++) {
|
|
444
|
+
if (inputs.some((input) => regionsOverlap(outputs[target], input))) return -1;
|
|
445
|
+
for (let prior = 0; prior < target; prior++) {
|
|
446
|
+
if (regionsOverlap(outputs[target], outputs[prior])) return -1;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
for (let at = 1; at < offsets.length; at++) if (offsets[at] < offsets[at - 1]) return -1;
|
|
450
|
+
for (const key of values) if (key >= valueRanks.length) return -1;
|
|
451
|
+
if (outBound.length === 0) return -3;
|
|
452
|
+
let bound = 0;
|
|
453
|
+
for (const missing of bounds) {
|
|
454
|
+
bound += missing;
|
|
455
|
+
if (bound > 4294967295) return -1;
|
|
456
|
+
}
|
|
457
|
+
const totals = new Float64Array(valueRanks.length).fill(-1);
|
|
458
|
+
const keys = new Uint32Array(valueRanks.length);
|
|
459
|
+
let distinct = 0;
|
|
460
|
+
for (let at = 0; at < values.length; at++) {
|
|
461
|
+
const key = values[at];
|
|
462
|
+
if (totals[key] < 0) {
|
|
463
|
+
keys[distinct++] = key;
|
|
464
|
+
totals[key] = 0;
|
|
465
|
+
}
|
|
466
|
+
totals[key] += counts2[at];
|
|
467
|
+
if (totals[key] > 4294967295) return -1;
|
|
468
|
+
}
|
|
469
|
+
const kept = Math.min(limit ?? distinct, distinct);
|
|
470
|
+
if (kept > outValues.length || kept > outCounts.length) return -3;
|
|
471
|
+
const ordered = keys.subarray(0, distinct);
|
|
472
|
+
ordered.sort(
|
|
473
|
+
(left, right) => (ascending ? totals[left] - totals[right] : totals[right] - totals[left]) || valueRanks[left] - valueRanks[right]
|
|
474
|
+
);
|
|
475
|
+
let largest = 0;
|
|
476
|
+
for (let at = kept; at < distinct; at++) largest = Math.max(largest, totals[ordered[at]]);
|
|
477
|
+
if (bound + largest > 4294967295) return -1;
|
|
478
|
+
for (let at = 0; at < kept; at++) {
|
|
479
|
+
outValues[at] = ordered[at];
|
|
480
|
+
outCounts[at] = totals[ordered[at]];
|
|
481
|
+
}
|
|
482
|
+
outBound[0] = bound + largest;
|
|
483
|
+
return kept;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// src/core/merges/facet-buckets.ts
|
|
487
|
+
function requireCount(value) {
|
|
488
|
+
if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
|
|
489
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "A facet count must fit an unsigned 32-bit integer", { value });
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function mergeFacetBuckets(lists, bounds, ascending, limit) {
|
|
493
|
+
if (bounds.length !== lists.length) {
|
|
494
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Each facet list must have one error bound");
|
|
495
|
+
}
|
|
496
|
+
const byValue = /* @__PURE__ */ new Map();
|
|
497
|
+
const labels = [];
|
|
498
|
+
let entries = 0;
|
|
499
|
+
for (const list of lists) {
|
|
500
|
+
entries += list.length;
|
|
501
|
+
requireCount(entries);
|
|
502
|
+
for (const bucket of list) {
|
|
503
|
+
requireCount(bucket.count);
|
|
504
|
+
if (byValue.has(bucket.value)) continue;
|
|
505
|
+
byValue.set(bucket.value, labels.length);
|
|
506
|
+
labels.push(bucket.value);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
let undercount = 0;
|
|
510
|
+
for (const bound of bounds) {
|
|
511
|
+
requireCount(bound);
|
|
512
|
+
undercount += bound;
|
|
513
|
+
}
|
|
514
|
+
requireCount(undercount);
|
|
515
|
+
const offsets = new Uint32Array(lists.length + 1);
|
|
516
|
+
const values = new Uint32Array(entries);
|
|
517
|
+
const counts2 = new Uint32Array(entries);
|
|
518
|
+
let at = 0;
|
|
519
|
+
for (let source = 0; source < lists.length; source++) {
|
|
520
|
+
offsets[source] = at;
|
|
521
|
+
for (const bucket of lists[source]) {
|
|
522
|
+
const key = byValue.get(bucket.value);
|
|
523
|
+
if (key === void 0) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "A facet value has no key");
|
|
524
|
+
values[at] = key;
|
|
525
|
+
counts2[at++] = bucket.count;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
offsets[lists.length] = at;
|
|
529
|
+
const ordered = Uint32Array.from(labels, (_, key) => key);
|
|
530
|
+
ordered.sort((left, right) => compareCodePoints(labels[left], labels[right]));
|
|
531
|
+
const ranks = new Uint32Array(labels.length);
|
|
532
|
+
for (let rank = 0; rank < ordered.length; rank++) ranks[ordered[rank]] = rank;
|
|
533
|
+
const kept = limit === void 0 || limit === Number.POSITIVE_INFINITY ? labels.length : limit;
|
|
534
|
+
const size = Math.min(kept, labels.length);
|
|
535
|
+
requireCount(size);
|
|
536
|
+
const outValues = new Uint32Array(size);
|
|
537
|
+
const outCounts = new Uint32Array(size);
|
|
538
|
+
const outBound = new Uint32Array(1);
|
|
539
|
+
const found = mergeFacets2(
|
|
540
|
+
offsets,
|
|
541
|
+
values,
|
|
542
|
+
counts2,
|
|
543
|
+
Uint32Array.from(bounds),
|
|
544
|
+
ranks,
|
|
545
|
+
ascending,
|
|
546
|
+
size,
|
|
547
|
+
outValues,
|
|
548
|
+
outCounts,
|
|
549
|
+
outBound
|
|
550
|
+
);
|
|
551
|
+
if (found < 0) {
|
|
552
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The facet lists cannot be merged", { status: found });
|
|
553
|
+
}
|
|
554
|
+
const buckets = Array.from(outValues.subarray(0, found), (key, position) => ({
|
|
555
|
+
value: labels[key],
|
|
556
|
+
count: outCounts[position]
|
|
557
|
+
}));
|
|
558
|
+
return { buckets, errorBound: outBound[0], undercount };
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// src/core/ordering/fold-table.ts
|
|
562
|
+
var SINGLE_FOLDS = "1t,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;2j,lj;b,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;2,w;1,w;1,w;1,w;1,w;1,w;1,w;y,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;4,1;2,1;2,1;3,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;3,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,-3d;1,1;2,1;2,1;2,-7g;2,5u;1,1;2,1;2,5q;1,1;2,5p;1,5p;1,1;3,27;1,5m;1,5n;1,1;2,5p;1,5r;2,5v;1,5t;1,1;4,5v;1,5x;2,5y;1,1;2,1;2,1;2,62;1,1;2,62;3,1;2,62;1,1;2,61;1,61;1,1;2,1;2,63;1,1;4,1;8,2;1,1;2,2;1,1;2,2;1,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;3,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;3,2;1,1;2,1;2,-2p;1,-1k;1,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,-3m;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;8,8bv;1,1;2,-4j;1,8bs;3,1;2,-5f;1,1x;1,1z;1,1;2,1;2,1;2,1;2,1;6v,38;17,1;2,1;4,1;9,38;7,12;2,11;1,11;1,11;2,1s;2,1r;1,1r;2,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;2,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;n,1;d,8;1,-u;1,-p;4,-f;1,-m;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,-1i;1,-1c;3,-1o;1,-1s;2,1;2,-7;1,1;3,-3m;1,-3m;1,-3m;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,28;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1d,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;a,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,f;1,1;2,1;2,1;2,1;2,1;2,1;2,1;3,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;3,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;28a,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;1,5ls;2,5ls;6,5ls;mj,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1oj,-4su;1,-4st;1,-4sk;1,-4si;1,-4si;1,-4sj;1,-4sc;1,-4ro;1,r7n;1,1;7,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;1,-2bk;3,-2bk;1,-2bk;1,-2bk;8x,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;7,-1m;5,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;a,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;9,-8;1,-8;1,-8;1,-8;1,-8;1,-8;b,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;9,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;9,-8;1,-8;1,-8;1,-8;1,-8;1,-8;c,-8;2,-8;2,-8;2,-8;9,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;1,-8;21,-8;1,-8;1,-22;1,-22;3,-5j9;a,-2e;1,-2e;1,-2e;1,-2e;d,-8;1,-8;1,-2s;1,-2s;d,-8;1,-8;1,-34;1,-34;1,-7;c,-3k;1,-3k;1,-3i;1,-3i;8b,-5st;4,-6gv;1,-6di;7,s;1a,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;1,g;k,1;mr,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1,q;1f5,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1,1c;1d,1;2,-8af;1,-2xy;1,-89z;3,1;2,1;2,1;2,-8bg;1,-8al;1,-8bj;1,-8bi;2,1;3,1;9,-8cf;1,-8cf;1,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;9,1;2,1;5,1;nym,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;k,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;3s,1;2,1;2,1;2,1;2,1;2,1;2,1;4,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;b,1;2,1;2,-r9g;1,1;2,1;2,1;2,1;2,1;5,1;2,-wmg;3,1;2,1;4,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,-wn8;1,-wnj;1,-wnf;1,-wn5;1,-wn8;2,-wlu;1,-wmi;1,-wlx;1,ps;1,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,-1c;1,-wn7;1,-raw;1,1;2,1;2,-wo7;1,1;2,1;2,1;2,1;2,1;2,1;2,1;2,1;2,-wu9;p,1;or,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;1,-tzk;ggy,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;xy,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;3t,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;1,14;4d,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;2,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;1,13;2,13;1,13;1,13;1,13;1,13;1,13;1,13;2,13;1,13;1d7,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;1,1s;4e,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;27v,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;gw1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1,w;1t,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;1,r;o5k,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;1,y;";
|
|
563
|
+
var MULTI_FOLDS = "67,37,37;29,2x,lj;p,jg,32;4n,2y,lo;bk,qh,lk,ld;w,qt,lk,ld;d3,12d,136;4y7,2w,mp;1,38,lk;1,3b,lm;1,3d,lm;1,2p,ji;4,37,37;4y,qt,lv;2,qt,lv,lc;2,qt,lv,ld;2,qt,lv,n6;16,64g,qh;1,64h,qh;1,64i,qh;1,64j,qh;1,64k,qh;1,64l,qh;1,64m,qh;1,64n,qh;1,64g,qh;1,64h,qh;1,64i,qh;1,64j,qh;1,64k,qh;1,64l,qh;1,64m,qh;1,64n,qh;1,65c,qh;1,65d,qh;1,65e,qh;1,65f,qh;1,65g,qh;1,65h,qh;1,65i,qh;1,65j,qh;1,65c,qh;1,65d,qh;1,65e,qh;1,65f,qh;1,65g,qh;1,65h,qh;1,65i,qh;1,65j,qh;1,674,qh;1,675,qh;1,676,qh;1,677,qh;1,678,qh;1,679,qh;1,67a,qh;1,67b,qh;1,674,qh;1,675,qh;1,676,qh;1,677,qh;1,678,qh;1,679,qh;1,67a,qh;1,67b,qh;3,67k,qh;1,q9,qh;1,q4,qh;2,q9,n6;1,q9,n6,qh;5,q9,qh;6,67o,qh;1,qf,qh;1,q6,qh;2,qf,n6;1,qf,n6,qh;5,qf,qh;6,qh,lk,lc;1,qh,lk,ld;3,qh,n6;1,qh,lk,n6;b,qt,lk,lc;1,qt,lk,ld;1,qp,lv;2,qt,n6;1,qt,lk,n6;b,67w,qh;1,qx,qh;1,r2,qh;2,qx,n6;1,qx,n6,qh;5,qx,qh;179g,2u,2u;1,2u,2x;1,2u,30;1,2u,2u,2x;1,2u,2u,30;1,37,38;1,37,38;d,12s,12u;1,12s,12d;1,12s,12j;1,132,12u;1,12s,12l;";
|
|
564
|
+
var singleTable = null;
|
|
565
|
+
var multiTable = null;
|
|
566
|
+
function singleFoldTable() {
|
|
567
|
+
if (singleTable === null) {
|
|
568
|
+
singleTable = /* @__PURE__ */ new Map();
|
|
569
|
+
let source = 0;
|
|
570
|
+
for (const entry of SINGLE_FOLDS.split(";")) {
|
|
571
|
+
if (entry.length === 0) continue;
|
|
572
|
+
const [delta, offset] = entry.split(",");
|
|
573
|
+
source += Number.parseInt(delta, 36);
|
|
574
|
+
singleTable.set(source, source + Number.parseInt(offset, 36));
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return singleTable;
|
|
578
|
+
}
|
|
579
|
+
function multiFoldTable() {
|
|
580
|
+
if (multiTable === null) {
|
|
581
|
+
multiTable = /* @__PURE__ */ new Map();
|
|
582
|
+
let source = 0;
|
|
583
|
+
for (const entry of MULTI_FOLDS.split(";")) {
|
|
584
|
+
if (entry.length === 0) continue;
|
|
585
|
+
const parts = entry.split(",");
|
|
586
|
+
source += Number.parseInt(parts[0], 36);
|
|
587
|
+
multiTable.set(
|
|
588
|
+
source,
|
|
589
|
+
parts.slice(1).map((cp) => Number.parseInt(cp, 36))
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return multiTable;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// src/core/ordering/fold-compare.ts
|
|
597
|
+
var EXHAUSTED = -1;
|
|
598
|
+
function foldOf(codePoint, single, multi) {
|
|
599
|
+
if (codePoint < 128) return codePoint >= 65 && codePoint <= 90 ? codePoint + 32 : codePoint;
|
|
600
|
+
return single.get(codePoint) ?? multi.get(codePoint) ?? codePoint;
|
|
601
|
+
}
|
|
602
|
+
function nextFoldedCodePoint(text, cursor, single, multi) {
|
|
603
|
+
if (cursor.pending !== null) {
|
|
604
|
+
const cp2 = cursor.pending[cursor.pendingIndex];
|
|
605
|
+
cursor.pendingIndex++;
|
|
606
|
+
if (cursor.pendingIndex === cursor.pending.length) {
|
|
607
|
+
cursor.pending = null;
|
|
608
|
+
cursor.pendingIndex = 0;
|
|
609
|
+
}
|
|
610
|
+
return cp2;
|
|
611
|
+
}
|
|
612
|
+
if (cursor.index >= text.length) return EXHAUSTED;
|
|
613
|
+
let cp = text.charCodeAt(cursor.index);
|
|
614
|
+
cursor.index++;
|
|
615
|
+
if (cp >= 55296 && cp < 56320 && cursor.index < text.length) {
|
|
616
|
+
const low = text.charCodeAt(cursor.index);
|
|
617
|
+
if (low >= 56320 && low < 57344) {
|
|
618
|
+
cp = 65536 + (cp - 55296 << 10) + (low - 56320);
|
|
619
|
+
cursor.index++;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
const folded = foldOf(cp, single, multi);
|
|
623
|
+
if (typeof folded === "number") return folded;
|
|
624
|
+
if (folded.length > 1) {
|
|
625
|
+
cursor.pending = folded;
|
|
626
|
+
cursor.pendingIndex = 1;
|
|
627
|
+
}
|
|
628
|
+
return folded[0];
|
|
629
|
+
}
|
|
630
|
+
function compareCaseFolded(a, b) {
|
|
631
|
+
const shorter = a.length < b.length ? a.length : b.length;
|
|
632
|
+
let start = 0;
|
|
633
|
+
while (start < shorter && a.charCodeAt(start) === b.charCodeAt(start)) start++;
|
|
634
|
+
if (start === a.length && start === b.length) return 0;
|
|
635
|
+
if (start > 0) {
|
|
636
|
+
const previous = a.charCodeAt(start - 1);
|
|
637
|
+
if (previous >= 55296 && previous < 56320) start--;
|
|
638
|
+
}
|
|
639
|
+
const single = singleFoldTable();
|
|
640
|
+
const multi = multiFoldTable();
|
|
641
|
+
const aCursor = { index: start, pending: null, pendingIndex: 0 };
|
|
642
|
+
const bCursor = { index: start, pending: null, pendingIndex: 0 };
|
|
643
|
+
for (; ; ) {
|
|
644
|
+
const aCp = nextFoldedCodePoint(a, aCursor, single, multi);
|
|
645
|
+
const bCp = nextFoldedCodePoint(b, bCursor, single, multi);
|
|
646
|
+
if (aCp !== bCp) return aCp - bCp;
|
|
647
|
+
if (aCp === EXHAUSTED) return 0;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function compareSortStrings(a, b) {
|
|
651
|
+
const folded = compareCaseFolded(a, b);
|
|
652
|
+
if (folded !== 0) return folded;
|
|
653
|
+
return compareCodePoints(a, b);
|
|
654
|
+
}
|
|
655
|
+
function appendFoldedCodePoint(codePoint, out) {
|
|
656
|
+
const folded = foldOf(codePoint, singleFoldTable(), multiFoldTable());
|
|
657
|
+
if (typeof folded === "number") {
|
|
658
|
+
out.push(folded);
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
for (const foldedCodePoint of folded) out.push(foldedCodePoint);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// src/core/ordering/sort-comparison.ts
|
|
665
|
+
function typeRank(value) {
|
|
666
|
+
if (typeof value === "number") return 0;
|
|
667
|
+
if (typeof value === "string") return 1;
|
|
668
|
+
return 2;
|
|
669
|
+
}
|
|
670
|
+
function comparePresentValues(a, b) {
|
|
671
|
+
const rankDifference = typeRank(a) - typeRank(b);
|
|
672
|
+
if (rankDifference !== 0) return rankDifference;
|
|
673
|
+
if (typeof a === "number" && typeof b === "number") return a - b;
|
|
674
|
+
if (typeof a === "string" && typeof b === "string") return compareSortStrings(a, b);
|
|
675
|
+
if (a === b) return 0;
|
|
676
|
+
return a ? 1 : -1;
|
|
677
|
+
}
|
|
678
|
+
function compareComparableValues(a, b, direction) {
|
|
679
|
+
if (a === null && b === null) return 0;
|
|
680
|
+
if (a === null) return 1;
|
|
681
|
+
if (b === null) return -1;
|
|
682
|
+
const comparison = comparePresentValues(a, b);
|
|
683
|
+
if (comparison === 0) return 0;
|
|
684
|
+
return direction === "desc" ? -comparison : comparison;
|
|
685
|
+
}
|
|
686
|
+
function compareComparableKeys(aKey, bKey, directions) {
|
|
687
|
+
for (let i = 0; i < directions.length; i++) {
|
|
688
|
+
const comparison = compareComparableValues(aKey[i] ?? null, bKey[i] ?? null, directions[i]);
|
|
689
|
+
if (comparison !== 0) return comparison;
|
|
690
|
+
}
|
|
691
|
+
return 0;
|
|
692
|
+
}
|
|
693
|
+
function compareSortValues(aValues, bValues, directions) {
|
|
694
|
+
for (let i = 0; i < directions.length; i++) {
|
|
695
|
+
const a = toComparableSortValue(aValues[i]);
|
|
696
|
+
const b = toComparableSortValue(bValues[i]);
|
|
697
|
+
if (a === null && b === null) continue;
|
|
698
|
+
if (a === null) return 1;
|
|
699
|
+
if (b === null) return -1;
|
|
700
|
+
const comparison = comparePresentValues(a, b);
|
|
701
|
+
if (comparison !== 0) return directions[i] === "desc" ? -comparison : comparison;
|
|
702
|
+
}
|
|
703
|
+
return 0;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// src/core/expansion/suggestions.ts
|
|
707
|
+
function sourceSuggestions(source, segment, typed, stemmed, written, reduced) {
|
|
708
|
+
const dictionary = expansionDictionary(source);
|
|
709
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
710
|
+
const totals = /* @__PURE__ */ new Map();
|
|
711
|
+
let holdsSurfaces = false;
|
|
712
|
+
function append(text, token, spelling) {
|
|
713
|
+
let group = grouped.get(text);
|
|
714
|
+
if (group === void 0) {
|
|
715
|
+
group = { text, segment, token, documents: liveFrequency(dictionary, token), spellings: /* @__PURE__ */ new Map() };
|
|
716
|
+
grouped.set(text, group);
|
|
717
|
+
}
|
|
718
|
+
if (!group.spellings.has(spelling.text)) group.spellings.set(spelling.text, spelling);
|
|
719
|
+
}
|
|
720
|
+
for (const surface of expansionSurfaces(source)) {
|
|
721
|
+
holdsSurfaces = true;
|
|
722
|
+
const token = expansionToken(dictionary, surface.token);
|
|
723
|
+
if (token === null) continue;
|
|
724
|
+
totals.set(token, (totals.get(token) ?? 0) + surface.count);
|
|
725
|
+
if (surface.spelling.startsWith(written))
|
|
726
|
+
append(surface.token, token, {
|
|
727
|
+
text: surface.spelling,
|
|
728
|
+
segment,
|
|
729
|
+
kind: 1,
|
|
730
|
+
ordinal: surface.entry,
|
|
731
|
+
occurrences: surface.count
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
const prefixes = !holdsSurfaces && reduced.length > 0 && written !== reduced ? [typed, stemmed] : [typed];
|
|
735
|
+
for (const prefix of prefixes) {
|
|
736
|
+
for (const token of expansionPrefixes(dictionary, prefix)) {
|
|
737
|
+
const occurrences = expansionOccurrences(source, token, totals.get(token) ?? 0);
|
|
738
|
+
if (!(occurrences > 0)) continue;
|
|
739
|
+
const text = termText(dictionary, token);
|
|
740
|
+
if (text !== null) append(text, token, { text, segment, kind: 0, ordinal: token, occurrences });
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
return grouped;
|
|
744
|
+
}
|
|
745
|
+
var suggestTerms = (sources, typed, stemmed, limit, outTerms, outStarts, outSpellings, outOccurrences) => {
|
|
746
|
+
try {
|
|
747
|
+
const held = readExpansion(sources, [typed, stemmed], outTerms, outSpellings, outOccurrences);
|
|
748
|
+
requireSegment(Number.isInteger(limit) && limit >= 0 && limit <= 4294967295);
|
|
749
|
+
const starts = typedRegion(outStarts, "Uint32Array");
|
|
750
|
+
requireSegment(starts !== null);
|
|
751
|
+
for (const region of [...held.arrays, held.segments, held.tokens, held.factors])
|
|
752
|
+
requireSegment(!regionsOverlap(starts, region));
|
|
753
|
+
const groups = /* @__PURE__ */ new Map();
|
|
754
|
+
if (held.words[0].length > 0 && limit > 0) {
|
|
755
|
+
for (let segment = 0; segment < held.sources.length; segment++) {
|
|
756
|
+
const local = sourceSuggestions(held.sources[segment], segment, typed, stemmed, held.words[0], held.words[1]);
|
|
757
|
+
for (const [text, next2] of local) {
|
|
758
|
+
const previous = groups.get(text);
|
|
759
|
+
if (previous === void 0) groups.set(text, next2);
|
|
760
|
+
else {
|
|
761
|
+
previous.documents += next2.documents;
|
|
762
|
+
requireSegment(previous.documents <= 4294967295);
|
|
763
|
+
for (const [spelling, value] of next2.spellings) {
|
|
764
|
+
const existing = previous.spellings.get(spelling);
|
|
765
|
+
if (existing === void 0) previous.spellings.set(spelling, value);
|
|
766
|
+
else existing.occurrences += value.occurrences;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
const ranked = [...groups.values()];
|
|
773
|
+
ranked.sort((left, right) => right.documents - left.documents || compareCodePoints(left.text, right.text));
|
|
774
|
+
if (ranked.length > limit) ranked.length = limit;
|
|
775
|
+
const spellings = ranked.map(
|
|
776
|
+
(group) => [...group.spellings.values()].sort(
|
|
777
|
+
(left, right) => right.occurrences - left.occurrences || compareCodePoints(left.text, right.text)
|
|
778
|
+
)
|
|
779
|
+
);
|
|
780
|
+
const needed = spellings.reduce((count, entries) => count + entries.length, 0);
|
|
781
|
+
requireSegment(needed <= 4294967295);
|
|
782
|
+
requireRoom(
|
|
783
|
+
ranked.length <= Math.floor(held.segments.length / 3) && starts.length >= ranked.length + 1 && needed <= Math.floor(held.tokens.length / 3) && needed <= held.factors.length
|
|
784
|
+
);
|
|
785
|
+
let next = 0;
|
|
786
|
+
starts[0] = 0;
|
|
787
|
+
for (let at = 0; at < ranked.length; at++) {
|
|
788
|
+
const group = ranked[at];
|
|
789
|
+
held.segments.set([group.segment, group.token, group.documents], at * 3);
|
|
790
|
+
for (const spelling of spellings[at]) {
|
|
791
|
+
held.tokens.set([spelling.segment, spelling.kind, spelling.ordinal], next * 3);
|
|
792
|
+
held.factors[next++] = spelling.occurrences;
|
|
793
|
+
}
|
|
794
|
+
starts[at + 1] = next;
|
|
795
|
+
}
|
|
796
|
+
return ranked.length;
|
|
797
|
+
} catch (error) {
|
|
798
|
+
if (error instanceof SegmentRefusal) return -error.status;
|
|
799
|
+
throw error;
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
|
|
803
|
+
// src/core/expansion/suggestion-host.ts
|
|
804
|
+
function units(text) {
|
|
805
|
+
return Uint16Array.from({ length: text.length }, (_, at) => text.charCodeAt(at));
|
|
806
|
+
}
|
|
807
|
+
function spellingText(source, kind, ordinal) {
|
|
808
|
+
if (kind === 0) return termText(expansionDictionary(source), ordinal);
|
|
809
|
+
if (source.growing !== null) {
|
|
810
|
+
const strings = source.growing.surfaces?.strings;
|
|
811
|
+
if (strings === void 0 || ordinal >= strings.header[0]) return null;
|
|
812
|
+
return storedText(strings.units, strings.termOffset[ordinal], strings.termLength[ordinal]);
|
|
813
|
+
}
|
|
814
|
+
const table = source.frozen?.surfaces;
|
|
815
|
+
return table === void 0 || table === null ? null : mergeText(table, ordinal * 2);
|
|
816
|
+
}
|
|
817
|
+
function suggestPartitionTerms(states, typed, stemmed, limit) {
|
|
818
|
+
if (limit <= 0 || typed.length === 0) return [];
|
|
819
|
+
const sources = [];
|
|
820
|
+
let termCapacity = 0;
|
|
821
|
+
let spellingCapacity = 0;
|
|
822
|
+
for (const state of states) {
|
|
823
|
+
const source = expansionSource(state);
|
|
824
|
+
if (source === null) return null;
|
|
825
|
+
sources.push(source);
|
|
826
|
+
const terms2 = termCount(expansionDictionary(source));
|
|
827
|
+
termCapacity += terms2;
|
|
828
|
+
spellingCapacity += terms2 + (source.growing !== null ? source.growing.surfaces?.strings.header[0] ?? 0 : source.frozen?.surfaces?.counts.length ?? 0);
|
|
829
|
+
}
|
|
830
|
+
const maximum = Math.min(4294967295, limit);
|
|
831
|
+
termCapacity = Math.min(termCapacity, maximum);
|
|
832
|
+
const terms = new Uint32Array(termCapacity * 3);
|
|
833
|
+
const starts = new Uint32Array(termCapacity + 1);
|
|
834
|
+
const spellings = new Uint32Array(spellingCapacity * 3);
|
|
835
|
+
const occurrences = new Float64Array(spellingCapacity);
|
|
836
|
+
const count = suggestTerms(sources, units(typed), units(stemmed), maximum, terms, starts, spellings, occurrences);
|
|
837
|
+
if (count < 0)
|
|
838
|
+
throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, `The suggestion operation refused status ${-count}`);
|
|
839
|
+
const output = [];
|
|
840
|
+
for (let at = 0; at < count; at++) {
|
|
841
|
+
const token = termText(expansionDictionary(sources[terms[at * 3]]), terms[at * 3 + 1]);
|
|
842
|
+
if (token === null)
|
|
843
|
+
throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, "The suggestion refers to an invalid token");
|
|
844
|
+
const surfaces = [];
|
|
845
|
+
for (let next = starts[at]; next < starts[at + 1]; next++) {
|
|
846
|
+
const surface = spellingText(sources[spellings[next * 3]], spellings[next * 3 + 1], spellings[next * 3 + 2]);
|
|
847
|
+
if (surface === null)
|
|
848
|
+
throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, "The suggestion refers to an invalid spelling");
|
|
849
|
+
surfaces.push({ surface, occurrences: occurrences[next] });
|
|
850
|
+
}
|
|
851
|
+
output.push({ token, documentFrequency: terms[at * 3 + 2], surfaces });
|
|
852
|
+
}
|
|
853
|
+
return output;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// src/core/expansion/portable.ts
|
|
857
|
+
function addCandidate(candidates, text, segment, token) {
|
|
858
|
+
const held = candidates.get(text);
|
|
859
|
+
if (held === void 0 || segment < held.segment || segment === held.segment && token < held.token)
|
|
860
|
+
candidates.set(text, { segment, token, documents: 0 });
|
|
861
|
+
}
|
|
862
|
+
function pointCount(text) {
|
|
863
|
+
let count = 0;
|
|
864
|
+
for (const _point of text) count++;
|
|
865
|
+
return count;
|
|
866
|
+
}
|
|
867
|
+
var expandPrefix = (inputs, typed, stemmed, own, outSegments, outTokens, outFactors) => {
|
|
868
|
+
try {
|
|
869
|
+
const { sources, words, segments, tokens, factors } = readExpansion(
|
|
870
|
+
inputs,
|
|
871
|
+
[typed, stemmed, own],
|
|
872
|
+
outSegments,
|
|
873
|
+
outTokens,
|
|
874
|
+
outFactors
|
|
875
|
+
);
|
|
876
|
+
if (words[0].length === 0) return 0;
|
|
877
|
+
const dictionaries = sources.map(expansionDictionary);
|
|
878
|
+
const holdsSurfaces = sources.some((source) => !expansionSurfaces(source).next().done);
|
|
879
|
+
const prefixes = !holdsSurfaces && words[1].length > 0 && words[1] !== words[0] ? [typed, stemmed] : [typed];
|
|
880
|
+
const candidates = /* @__PURE__ */ new Map();
|
|
881
|
+
for (let segment = 0; segment < sources.length; segment++) {
|
|
882
|
+
const source = sources[segment];
|
|
883
|
+
const dictionary = dictionaries[segment];
|
|
884
|
+
const totals = /* @__PURE__ */ new Map();
|
|
885
|
+
for (const surface of expansionSurfaces(source)) {
|
|
886
|
+
const matches = validExpansionText(surface.spelling) && surface.spelling.startsWith(words[0]);
|
|
887
|
+
if (!matches && source.growing !== null) continue;
|
|
888
|
+
const token = expansionToken(dictionary, surface.token);
|
|
889
|
+
if (token === null) continue;
|
|
890
|
+
if (source.growing === null) totals.set(token, (totals.get(token) ?? 0) + surface.count);
|
|
891
|
+
if (matches) addCandidate(candidates, surface.token, segment, token);
|
|
892
|
+
}
|
|
893
|
+
for (const prefix of prefixes) {
|
|
894
|
+
for (const token of expansionPrefixes(dictionary, prefix)) {
|
|
895
|
+
if (!(expansionOccurrences(source, token, totals.get(token) ?? 0) > 0)) continue;
|
|
896
|
+
const text = termText(dictionary, token);
|
|
897
|
+
if (text !== null && text.length > 0 && validExpansionText(text))
|
|
898
|
+
addCandidate(candidates, text, segment, token);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
const ranked = [...candidates].map(([text, candidate]) => {
|
|
903
|
+
for (const dictionary of dictionaries) {
|
|
904
|
+
const token = expansionToken(dictionary, text);
|
|
905
|
+
if (token !== null) candidate.documents += liveFrequency(dictionary, token);
|
|
906
|
+
}
|
|
907
|
+
return { text, ...candidate };
|
|
908
|
+
});
|
|
909
|
+
ranked.sort((left, right) => right.documents - left.documents || compareCodePoints(left.text, right.text));
|
|
910
|
+
const count = Math.min(PREFIX_BUDGET, ranked.length);
|
|
911
|
+
requireRoom(count <= segments.length && count <= tokens.length && count <= factors.length);
|
|
912
|
+
const ownLength = pointCount(words[2]);
|
|
913
|
+
for (let at = 0; at < count; at++) {
|
|
914
|
+
segments[at] = ranked[at].segment;
|
|
915
|
+
tokens[at] = ranked[at].token;
|
|
916
|
+
factors[at] = Math.min(1, ownLength / pointCount(ranked[at].text));
|
|
917
|
+
}
|
|
918
|
+
return count;
|
|
919
|
+
} catch (error) {
|
|
920
|
+
if (error instanceof SegmentRefusal) return -error.status;
|
|
921
|
+
throw error;
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
|
|
925
|
+
// src/core/expansion/host.ts
|
|
926
|
+
function units2(text) {
|
|
927
|
+
return Uint16Array.from({ length: text.length }, (_, at) => text.charCodeAt(at));
|
|
928
|
+
}
|
|
929
|
+
function completePrefix(states, typed, stemmed, limit) {
|
|
930
|
+
const sources = [];
|
|
931
|
+
for (const state of states) {
|
|
932
|
+
const source = expansionSource(state);
|
|
933
|
+
if (source === null) return null;
|
|
934
|
+
sources.push(source);
|
|
935
|
+
}
|
|
936
|
+
const segments = new Uint32Array(PREFIX_BUDGET);
|
|
937
|
+
const tokens = new Uint32Array(PREFIX_BUDGET);
|
|
938
|
+
const factors = new Float64Array(PREFIX_BUDGET);
|
|
939
|
+
const own = units2(stemmed);
|
|
940
|
+
const count = expandPrefix(sources, units2(typed), own, own, segments, tokens, factors);
|
|
941
|
+
if (count < 0)
|
|
942
|
+
throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, `The keyword prefix operation refused status ${-count}`);
|
|
943
|
+
const output = [];
|
|
944
|
+
for (let at = 0; at < Math.min(count, limit); at++) {
|
|
945
|
+
const token = termText(expansionDictionary(sources[segments[at]]), tokens[at]);
|
|
946
|
+
if (token === null)
|
|
947
|
+
throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, "The keyword completion refers to an invalid token");
|
|
948
|
+
output.push({ token, factor: factors[at] });
|
|
949
|
+
}
|
|
950
|
+
return output;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
// src/core/partition/suggestions.ts
|
|
954
|
+
function verbatimOccurrences(state, token) {
|
|
955
|
+
const postingList = state.invertedIdx.lookup(token);
|
|
956
|
+
if (!postingList) return 0;
|
|
957
|
+
const derived = postingList.totalTermFrequency - state.surfaceRegistry.stemChangedTotalFor(token);
|
|
958
|
+
return derived > 0 ? derived : 0;
|
|
959
|
+
}
|
|
960
|
+
function suggestDisplayTerms(state, surfacePrefix, stemmedPrefix, limit) {
|
|
961
|
+
if (limit <= 0 || surfacePrefix.length === 0) return [];
|
|
962
|
+
const native = suggestPartitionTerms([state], surfacePrefix, stemmedPrefix, limit);
|
|
963
|
+
if (native !== null) return native;
|
|
964
|
+
const groups = /* @__PURE__ */ new Map();
|
|
965
|
+
for (const candidate of state.surfaceRegistry.candidatesForPrefix(surfacePrefix)) {
|
|
966
|
+
const postingList = state.invertedIdx.lookup(candidate.token);
|
|
967
|
+
if (!postingList || postingList.docIdSet.size === 0) continue;
|
|
968
|
+
let group = groups.get(candidate.token);
|
|
969
|
+
if (!group) {
|
|
970
|
+
group = { documentFrequency: postingList.docIdSet.size, surfaces: [] };
|
|
971
|
+
groups.set(candidate.token, group);
|
|
972
|
+
}
|
|
973
|
+
group.surfaces.push({ surface: candidate.surface, occurrences: candidate.occurrences });
|
|
974
|
+
}
|
|
975
|
+
const termPrefixes = state.surfaceRegistry.size() === 0 && stemmedPrefix !== surfacePrefix && stemmedPrefix.length > 0 ? [surfacePrefix, stemmedPrefix] : [surfacePrefix];
|
|
976
|
+
for (const prefix of termPrefixes) {
|
|
977
|
+
for (const suggestion of state.invertedIdx.prefixSearch(prefix, Number.MAX_SAFE_INTEGER)) {
|
|
978
|
+
const occurrences = verbatimOccurrences(state, suggestion.term);
|
|
979
|
+
if (occurrences === 0) continue;
|
|
980
|
+
let group = groups.get(suggestion.term);
|
|
981
|
+
if (!group) {
|
|
982
|
+
group = { documentFrequency: suggestion.documentFrequency, surfaces: [] };
|
|
983
|
+
groups.set(suggestion.term, group);
|
|
984
|
+
} else if (group.surfaces.some((s) => s.surface === suggestion.term)) {
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
group.surfaces.push({ surface: suggestion.term, occurrences });
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
const results = [];
|
|
991
|
+
for (const [token, group] of groups) {
|
|
992
|
+
results.push({ token, documentFrequency: group.documentFrequency, surfaces: group.surfaces });
|
|
993
|
+
}
|
|
994
|
+
results.sort((a, b) => b.documentFrequency - a.documentFrequency || compareCodePoints(a.token, b.token));
|
|
995
|
+
if (results.length > limit) results.length = limit;
|
|
996
|
+
return results;
|
|
997
|
+
}
|
|
998
|
+
function expandTermPrefix(subs, surfacePrefix, stemmedToken, maxExpansions) {
|
|
999
|
+
if (maxExpansions <= 0 || surfacePrefix.length === 0) return [];
|
|
1000
|
+
const completions = completePrefix(subs, surfacePrefix, stemmedToken, maxExpansions);
|
|
1001
|
+
if (completions !== null) return completions;
|
|
1002
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
1003
|
+
const holdsSurfaceForms = subs.some((sub) => sub.surfaceRegistry.size() > 0);
|
|
1004
|
+
const termPrefixes = !holdsSurfaceForms && stemmedToken !== surfacePrefix && stemmedToken.length > 0 ? [surfacePrefix, stemmedToken] : [surfacePrefix];
|
|
1005
|
+
for (const sub of subs) {
|
|
1006
|
+
for (const candidate of sub.surfaceRegistry.candidatesForPrefix(surfacePrefix)) candidates.add(candidate.token);
|
|
1007
|
+
for (const prefix of termPrefixes) {
|
|
1008
|
+
for (const suggestion of sub.invertedIdx.prefixSearch(prefix, Number.MAX_SAFE_INTEGER)) {
|
|
1009
|
+
if (candidates.has(suggestion.term)) continue;
|
|
1010
|
+
if (verbatimOccurrences(sub, suggestion.term) > 0) candidates.add(suggestion.term);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
const ranked = [];
|
|
1015
|
+
for (const token of candidates) {
|
|
1016
|
+
const documents = partitionDocumentFrequency(subs, token);
|
|
1017
|
+
if (documents > 0) ranked.push({ token, documents });
|
|
1018
|
+
}
|
|
1019
|
+
ranked.sort((a, b) => b.documents - a.documents || compareCodePoints(a.token, b.token));
|
|
1020
|
+
if (ranked.length > maxExpansions) ranked.length = maxExpansions;
|
|
1021
|
+
const ownLength = codePointCount(stemmedToken);
|
|
1022
|
+
return ranked.map((entry) => ({ token: entry.token, factor: Math.min(1, ownLength / codePointCount(entry.token)) }));
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// src/core/partition/composite/suggest.ts
|
|
1026
|
+
function compositeSuggestTerms(subs, surfacePrefix, stemmedPrefix, limit) {
|
|
1027
|
+
if (limit <= 0) return [];
|
|
1028
|
+
const native = suggestPartitionTerms(subs, surfacePrefix, stemmedPrefix, limit);
|
|
1029
|
+
if (native !== null) return native;
|
|
1030
|
+
const merged = /* @__PURE__ */ new Map();
|
|
1031
|
+
for (const sub of subs) {
|
|
1032
|
+
for (const suggestion of suggestDisplayTerms(sub, surfacePrefix, stemmedPrefix, Number.MAX_SAFE_INTEGER)) {
|
|
1033
|
+
let entry = merged.get(suggestion.token);
|
|
1034
|
+
if (entry === void 0) {
|
|
1035
|
+
entry = { documentFrequency: 0, surfaceOccurrences: /* @__PURE__ */ new Map() };
|
|
1036
|
+
merged.set(suggestion.token, entry);
|
|
1037
|
+
}
|
|
1038
|
+
entry.documentFrequency += suggestion.documentFrequency;
|
|
1039
|
+
for (const surface of suggestion.surfaces) {
|
|
1040
|
+
entry.surfaceOccurrences.set(
|
|
1041
|
+
surface.surface,
|
|
1042
|
+
(entry.surfaceOccurrences.get(surface.surface) ?? 0) + surface.occurrences
|
|
1043
|
+
);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
const suggestions = [];
|
|
1048
|
+
for (const [token, entry] of merged) {
|
|
1049
|
+
const surfaces = Array.from(entry.surfaceOccurrences, ([surface, occurrences]) => ({ surface, occurrences }));
|
|
1050
|
+
surfaces.sort((a, b) => b.occurrences - a.occurrences || compareCodePoints(a.surface, b.surface));
|
|
1051
|
+
suggestions.push({ token, documentFrequency: entry.documentFrequency, surfaces });
|
|
1052
|
+
}
|
|
1053
|
+
suggestions.sort((a, b) => b.documentFrequency - a.documentFrequency || compareCodePoints(a.token, b.token));
|
|
1054
|
+
if (suggestions.length > limit) suggestions.length = limit;
|
|
1055
|
+
return suggestions;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
// src/search/page-start.ts
|
|
1059
|
+
function pageStart(count, sorted, scores, scorePresent, afterAnchor, anchorScore) {
|
|
1060
|
+
const scoreRegion = scores === null ? null : typedRegion(scores, "Float64Array");
|
|
1061
|
+
const presentRegion = scorePresent === null ? null : typedRegion(scorePresent, "Uint32Array");
|
|
1062
|
+
const afterRegion = typedRegion(afterAnchor, "Uint32Array");
|
|
1063
|
+
const words = Math.max(1, Math.ceil(count / 32));
|
|
1064
|
+
if (!Number.isInteger(count) || count < 0 || count > 4294967295 || typeof sorted !== "boolean" || anchorScore !== null && typeof anchorScore !== "number" || afterRegion === null || afterRegion.length < words || (scores === null ? scorePresent !== null : scoreRegion === null || scoreRegion.length !== count) || scorePresent !== null && (presentRegion === null || presentRegion.length < words)) {
|
|
1065
|
+
return -1;
|
|
1066
|
+
}
|
|
1067
|
+
for (let position = 0; position < count; position++) {
|
|
1068
|
+
const word = position >>> 5;
|
|
1069
|
+
const mask = 1 << (position & 31);
|
|
1070
|
+
const after = (afterRegion[word] & mask) !== 0;
|
|
1071
|
+
if (sorted) {
|
|
1072
|
+
if (after) return position;
|
|
1073
|
+
} else {
|
|
1074
|
+
if (anchorScore === null || scoreRegion === null || presentRegion !== null && (presentRegion[word] & mask) === 0) {
|
|
1075
|
+
return position;
|
|
1076
|
+
}
|
|
1077
|
+
const order = compareScores(scoreRegion[position], anchorScore);
|
|
1078
|
+
if (order > 0 || order === 0 && after) return position;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
return count;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
// src/search/cursor-page.ts
|
|
1085
|
+
function startAfterAnchor(results, anchor, sort) {
|
|
1086
|
+
const words = Math.max(1, Math.ceil(results.length / 32));
|
|
1087
|
+
const afterAnchor = new Uint32Array(words);
|
|
1088
|
+
const sorted = sort !== void 0 && anchor.sortKey !== null;
|
|
1089
|
+
const scores = sorted ? null : new Float64Array(results.length);
|
|
1090
|
+
let scorePresent = null;
|
|
1091
|
+
for (let position = 0; position < results.length; position++) {
|
|
1092
|
+
const result = results[position];
|
|
1093
|
+
const comparison = (sort !== void 0 && anchor.sortKey !== null ? compareSortValues(sort.sortKeyOf(result.id), anchor.sortKey, sort.directions) : 0) || compareCodePoints(result.id, anchor.anchor);
|
|
1094
|
+
if (comparison > 0) afterAnchor[position >>> 5] |= 1 << (position & 31);
|
|
1095
|
+
if (scores !== null) {
|
|
1096
|
+
if (result.score === void 0) {
|
|
1097
|
+
scorePresent ??= new Uint32Array(words).fill(4294967295);
|
|
1098
|
+
scorePresent[position >>> 5] &= ~(1 << (position & 31));
|
|
1099
|
+
} else {
|
|
1100
|
+
scores[position] = result.score;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
const start = pageStart(results.length, sorted, scores, scorePresent, afterAnchor, anchor.score);
|
|
1105
|
+
if (start < 0) {
|
|
1106
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The result page cannot be selected", { status: start });
|
|
1107
|
+
}
|
|
1108
|
+
return start;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
// src/core/facets/boolean.ts
|
|
1112
|
+
function countMatches(bits, matched) {
|
|
1113
|
+
let count = 0;
|
|
1114
|
+
const length = Math.min(bits.length, matched.length);
|
|
1115
|
+
for (let index = 0; index < length; index++) {
|
|
1116
|
+
let word = bits[index] & matched[index];
|
|
1117
|
+
word -= word >>> 1 & 1431655765;
|
|
1118
|
+
word = (word & 858993459) + (word >>> 2 & 858993459);
|
|
1119
|
+
count += (word + (word >>> 4) & 252645135) * 16843009 >>> 24;
|
|
1120
|
+
}
|
|
1121
|
+
return count;
|
|
1122
|
+
}
|
|
1123
|
+
var booleanFacet = (trueInput, falseInput, matchedInput, countsInput) => {
|
|
1124
|
+
const trueBits = typedRegion(trueInput, "Uint32Array");
|
|
1125
|
+
const falseBits = typedRegion(falseInput, "Uint32Array");
|
|
1126
|
+
const matched = typedRegion(matchedInput, "Uint32Array");
|
|
1127
|
+
const counts2 = typedRegion(countsInput, "Uint32Array");
|
|
1128
|
+
if (trueBits === null || falseBits === null || matched === null || counts2 === null || trueBits.length === 0 || falseBits.length === 0 || matched.length === 0)
|
|
1129
|
+
return -1;
|
|
1130
|
+
if ([trueBits, falseBits, matched].some((input) => regionsOverlap(input, counts2))) return -1;
|
|
1131
|
+
if (counts2.length < 2) return -3;
|
|
1132
|
+
const trueCount = countMatches(trueBits, matched);
|
|
1133
|
+
const falseCount = countMatches(falseBits, matched);
|
|
1134
|
+
if (trueCount > 4294967295 || falseCount > 4294967295) return -1;
|
|
1135
|
+
counts2[0] = trueCount;
|
|
1136
|
+
counts2[1] = falseCount;
|
|
1137
|
+
return 0;
|
|
1138
|
+
};
|
|
1139
|
+
|
|
1140
|
+
// src/core/facets/numeric.ts
|
|
1141
|
+
var seen = new Uint32Array(0);
|
|
1142
|
+
function countSpan(ordinals, matched, start, end) {
|
|
1143
|
+
let count = 0;
|
|
1144
|
+
for (let at = start; at < end; at++) {
|
|
1145
|
+
const ordinal = ordinals[at];
|
|
1146
|
+
const word = ordinal >>> 5;
|
|
1147
|
+
const bit = 1 << (ordinal & 31);
|
|
1148
|
+
if (word < matched.length && (matched[word] & bit) !== 0 && (seen[word] & bit) === 0) {
|
|
1149
|
+
seen[word] |= bit;
|
|
1150
|
+
count++;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
for (let at = start; at < end; at++) {
|
|
1154
|
+
const ordinal = ordinals[at];
|
|
1155
|
+
const word = ordinal >>> 5;
|
|
1156
|
+
if (word < matched.length) seen[word] &= ~(1 << (ordinal & 31));
|
|
1157
|
+
}
|
|
1158
|
+
return count;
|
|
1159
|
+
}
|
|
1160
|
+
function prepareSeen(words) {
|
|
1161
|
+
const needed = Math.min(words, 134217728);
|
|
1162
|
+
if (seen.length < needed) seen = new Uint32Array(needed);
|
|
1163
|
+
}
|
|
1164
|
+
var numberFacet = (valuesInput, ordinalsInput, matchedInput, valuesOutput, countsOutput) => {
|
|
1165
|
+
const values = typedRegion(valuesInput, "Float64Array");
|
|
1166
|
+
const ordinals = typedRegion(ordinalsInput, "Uint32Array");
|
|
1167
|
+
const matched = typedRegion(matchedInput, "Uint32Array");
|
|
1168
|
+
const outValues = typedRegion(valuesOutput, "Float64Array");
|
|
1169
|
+
const outCounts = typedRegion(countsOutput, "Uint32Array");
|
|
1170
|
+
if (values === null || ordinals === null || matched === null || outValues === null || outCounts === null || values.length > 4294967295 || values.length !== ordinals.length || matched.length === 0)
|
|
1171
|
+
return -1;
|
|
1172
|
+
if (regionsOverlap(outValues, outCounts) || [values, ordinals, matched].some((input) => regionsOverlap(input, outValues) || regionsOverlap(input, outCounts)))
|
|
1173
|
+
return -1;
|
|
1174
|
+
prepareSeen(matched.length);
|
|
1175
|
+
const capacity = Math.min(outValues.length, outCounts.length, values.length);
|
|
1176
|
+
const bucketValues = new Float64Array(capacity);
|
|
1177
|
+
const bucketCounts = new Uint32Array(capacity);
|
|
1178
|
+
let buckets = 0;
|
|
1179
|
+
for (let start = 0; start < values.length; ) {
|
|
1180
|
+
const value = values[start];
|
|
1181
|
+
let end = start + 1;
|
|
1182
|
+
while (end < values.length && values[end] === value) end++;
|
|
1183
|
+
if (!Number.isNaN(value)) {
|
|
1184
|
+
const count = countSpan(ordinals, matched, start, end);
|
|
1185
|
+
if (count > 0) {
|
|
1186
|
+
if (buckets === capacity) return -3;
|
|
1187
|
+
bucketValues[buckets] = value === 0 ? 0 : value;
|
|
1188
|
+
bucketCounts[buckets++] = count;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
start = end;
|
|
1192
|
+
}
|
|
1193
|
+
outValues.set(bucketValues.subarray(0, buckets));
|
|
1194
|
+
outCounts.set(bucketCounts.subarray(0, buckets));
|
|
1195
|
+
return buckets;
|
|
1196
|
+
};
|
|
1197
|
+
var rangeFacet = (valuesInput, ordinalsInput, matchedInput, rangesInput, countsOutput) => {
|
|
1198
|
+
const values = typedRegion(valuesInput, "Float64Array");
|
|
1199
|
+
const ordinals = typedRegion(ordinalsInput, "Uint32Array");
|
|
1200
|
+
const matched = typedRegion(matchedInput, "Uint32Array");
|
|
1201
|
+
const ranges = typedRegion(rangesInput, "Float64Array");
|
|
1202
|
+
const outCounts = typedRegion(countsOutput, "Uint32Array");
|
|
1203
|
+
if (values === null || ordinals === null || matched === null || ranges === null || outCounts === null || values.length > 4294967295 || values.length !== ordinals.length || matched.length === 0 || ranges.length % 2 !== 0 || ranges.length / 2 > 4294967295)
|
|
1204
|
+
return -1;
|
|
1205
|
+
if ([values, ordinals, matched, ranges].some((input) => regionsOverlap(input, outCounts))) return -1;
|
|
1206
|
+
const count = ranges.length / 2;
|
|
1207
|
+
if (outCounts.length < count) return -3;
|
|
1208
|
+
if (count === 0) return 0;
|
|
1209
|
+
prepareSeen(matched.length);
|
|
1210
|
+
const counts2 = new Uint32Array(count);
|
|
1211
|
+
for (let at = 0; at < count; at++) {
|
|
1212
|
+
const from = ranges[at * 2];
|
|
1213
|
+
const to = ranges[at * 2 + 1];
|
|
1214
|
+
if (!Number.isNaN(from) && !Number.isNaN(to))
|
|
1215
|
+
counts2[at] = countSpan(ordinals, matched, lowerBound(values, from), lowerBound(values, to));
|
|
1216
|
+
}
|
|
1217
|
+
outCounts.set(counts2);
|
|
1218
|
+
return 0;
|
|
1219
|
+
};
|
|
1220
|
+
|
|
1221
|
+
// src/core/facets/enum.ts
|
|
1222
|
+
var enumFacet = (offsetsInput, postingsInput, matchedInput, countsOutput) => {
|
|
1223
|
+
const offsets = typedRegion(offsetsInput, "Uint32Array");
|
|
1224
|
+
const postings = typedRegion(postingsInput, "Uint32Array");
|
|
1225
|
+
const matched = typedRegion(matchedInput, "Uint32Array");
|
|
1226
|
+
const outCounts = typedRegion(countsOutput, "Uint32Array");
|
|
1227
|
+
if (offsets === null || postings === null || matched === null || outCounts === null || offsets.length === 0 || offsets.length - 1 > 4294967295 || postings.length > 4294967295 || matched.length === 0)
|
|
1228
|
+
return -1;
|
|
1229
|
+
if ([offsets, postings, matched].some((input) => regionsOverlap(input, outCounts))) return -1;
|
|
1230
|
+
const count = offsets.length - 1;
|
|
1231
|
+
if (outCounts.length < count) return -3;
|
|
1232
|
+
if (count === 0) return 0;
|
|
1233
|
+
prepareSeen(matched.length);
|
|
1234
|
+
const counts2 = new Uint32Array(count);
|
|
1235
|
+
for (let at = 0; at < count; at++) {
|
|
1236
|
+
const start = offsets[at];
|
|
1237
|
+
const end = offsets[at + 1];
|
|
1238
|
+
if (start <= end && end <= postings.length) counts2[at] = countSpan(postings, matched, start, end);
|
|
1239
|
+
}
|
|
1240
|
+
outCounts.set(counts2);
|
|
1241
|
+
return 0;
|
|
1242
|
+
};
|
|
1243
|
+
|
|
1244
|
+
// src/core/facets/stored.ts
|
|
1245
|
+
var valueFacet = (valuesInput, matchedInput, valuesOutput, countsOutput) => {
|
|
1246
|
+
const fields = readStoredFields([valuesInput]);
|
|
1247
|
+
const values = fields?.[0];
|
|
1248
|
+
const matched = typedRegion(matchedInput, "Uint32Array");
|
|
1249
|
+
const outValues = typedRegion(valuesOutput, "Uint32Array");
|
|
1250
|
+
const outCounts = typedRegion(countsOutput, "Uint32Array");
|
|
1251
|
+
if (values === void 0 || matched === null || outValues === null || outCounts === null || matched.length === 0)
|
|
1252
|
+
return -1;
|
|
1253
|
+
if (regionsOverlap(outValues, outCounts) || [...Object.values(values), matched].some(
|
|
1254
|
+
(input) => regionsOverlap(input, outValues) || regionsOverlap(input, outCounts)
|
|
1255
|
+
))
|
|
1256
|
+
return -1;
|
|
1257
|
+
const counts2 = new Uint32Array(values.kinds.length);
|
|
1258
|
+
for (let ordinal = 0; ordinal < values.shapes.length; ordinal++) {
|
|
1259
|
+
const word = ordinal >>> 5;
|
|
1260
|
+
if (word >= matched.length || (matched[word] & 1 << (ordinal & 31)) === 0) continue;
|
|
1261
|
+
const shape = values.shapes[ordinal];
|
|
1262
|
+
const start = values.starts[ordinal];
|
|
1263
|
+
const end = values.starts[ordinal + 1];
|
|
1264
|
+
if (shape === 0 || shape > 2 || start > end || end > counts2.length || shape === 1 && end - start !== 1) continue;
|
|
1265
|
+
for (let at = start; at < end; at++) {
|
|
1266
|
+
const kind = values.kinds[at];
|
|
1267
|
+
if (kind < 1 || kind > 4) continue;
|
|
1268
|
+
if (kind === 4 && (values.textStart[at] > values.units.length || values.textLength[at] > values.units.length - values.textStart[at]))
|
|
1269
|
+
continue;
|
|
1270
|
+
if (counts2[at] === 4294967295) return -1;
|
|
1271
|
+
counts2[at]++;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
const positions = [];
|
|
1275
|
+
for (let at = 0; at < counts2.length; at++) if (counts2[at] > 0) positions.push(at);
|
|
1276
|
+
positions.sort((left, right) => compareStoredElements(values, left, right));
|
|
1277
|
+
let buckets = 0;
|
|
1278
|
+
for (let start = 0; start < positions.length; ) {
|
|
1279
|
+
const first = positions[start];
|
|
1280
|
+
let total = counts2[first];
|
|
1281
|
+
let end = start + 1;
|
|
1282
|
+
while (end < positions.length && compareStoredElements(values, first, positions[end]) === 0) {
|
|
1283
|
+
total += counts2[positions[end++]];
|
|
1284
|
+
if (total > 4294967295) return -1;
|
|
1285
|
+
}
|
|
1286
|
+
positions[buckets++] = first;
|
|
1287
|
+
counts2[first] = total;
|
|
1288
|
+
start = end;
|
|
1289
|
+
}
|
|
1290
|
+
if (buckets > outValues.length || buckets > outCounts.length) return -3;
|
|
1291
|
+
for (let at = 0; at < buckets; at++) {
|
|
1292
|
+
const first = positions[at];
|
|
1293
|
+
outValues[at] = first;
|
|
1294
|
+
outCounts[at] = counts2[first];
|
|
1295
|
+
}
|
|
1296
|
+
return buckets;
|
|
1297
|
+
};
|
|
1298
|
+
|
|
1299
|
+
// src/core/facets/stored-range.ts
|
|
1300
|
+
var storedRangeFacet = (valuesInput, matchedInput, rangesInput, output) => {
|
|
1301
|
+
const values = readStoredFields([valuesInput])?.[0];
|
|
1302
|
+
const matched = typedRegion(matchedInput, "Uint32Array");
|
|
1303
|
+
const ranges = typedRegion(rangesInput, "Float64Array");
|
|
1304
|
+
const counts2 = typedRegion(output, "Uint32Array");
|
|
1305
|
+
if (values === void 0 || matched === null || ranges === null || counts2 === null || matched.length === 0 || ranges.length % 2 !== 0 || ranges.length / 2 > 4294967295)
|
|
1306
|
+
return -1;
|
|
1307
|
+
if ([...Object.values(values), matched, ranges].some((input) => regionsOverlap(input, counts2))) return -1;
|
|
1308
|
+
const rangeCount = ranges.length / 2;
|
|
1309
|
+
if (counts2.length < rangeCount) return -3;
|
|
1310
|
+
for (let range = 0; range < rangeCount; range++) {
|
|
1311
|
+
const from = ranges[range * 2];
|
|
1312
|
+
const to = ranges[range * 2 + 1];
|
|
1313
|
+
let total = 0;
|
|
1314
|
+
if (from < to) {
|
|
1315
|
+
for (let ordinal = 0; ordinal < values.shapes.length; ordinal++) {
|
|
1316
|
+
const word = ordinal >>> 5;
|
|
1317
|
+
if (word >= matched.length || (matched[word] & 1 << (ordinal & 31)) === 0) continue;
|
|
1318
|
+
const shape = values.shapes[ordinal];
|
|
1319
|
+
const start = values.starts[ordinal];
|
|
1320
|
+
const end = values.starts[ordinal + 1];
|
|
1321
|
+
if (shape === 0 || shape > 2 || start > end || end > values.kinds.length || shape === 1 && end - start !== 1)
|
|
1322
|
+
continue;
|
|
1323
|
+
for (let at = start; at < end; at++) {
|
|
1324
|
+
if (values.kinds[at] !== 3) continue;
|
|
1325
|
+
const value = values.numbers[at];
|
|
1326
|
+
if (value >= from && value < to) {
|
|
1327
|
+
total++;
|
|
1328
|
+
break;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
counts2[range] = total;
|
|
1334
|
+
}
|
|
1335
|
+
return 0;
|
|
1336
|
+
};
|
|
1337
|
+
|
|
1338
|
+
// src/core/facets/adapter.ts
|
|
1339
|
+
var counts = new Uint32Array(2);
|
|
1340
|
+
var emptyMatches = new Uint32Array(1);
|
|
1341
|
+
function countEnumFacet(values, offsets, postings, matched) {
|
|
1342
|
+
const counts2 = new Uint32Array(values.length);
|
|
1343
|
+
const status = enumFacet(offsets, postings, matched.length === 0 ? emptyMatches : matched, counts2);
|
|
1344
|
+
if (status !== 0)
|
|
1345
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The enum facet could not be counted", { status: -status });
|
|
1346
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
1347
|
+
for (let at = 0; at < values.length; at++) if (counts2[at] > 0) buckets.set(values[at], counts2[at]);
|
|
1348
|
+
return buckets;
|
|
1349
|
+
}
|
|
1350
|
+
function countNumberFacet(index, matched) {
|
|
1351
|
+
const values = new Float64Array(index.values.length);
|
|
1352
|
+
const counts2 = new Uint32Array(index.values.length);
|
|
1353
|
+
const count = numberFacet(index.values, index.ordinals, matched.length === 0 ? emptyMatches : matched, values, counts2);
|
|
1354
|
+
if (count < 0)
|
|
1355
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The numeric facet could not be counted", { status: -count });
|
|
1356
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
1357
|
+
for (let at = 0; at < count; at++) buckets.set(values[at], counts2[at]);
|
|
1358
|
+
return buckets;
|
|
1359
|
+
}
|
|
1360
|
+
function countRangeFacets(index, matched, ranges) {
|
|
1361
|
+
const counts2 = new Uint32Array(ranges.length / 2);
|
|
1362
|
+
const status = rangeFacet(index.values, index.ordinals, matched.length === 0 ? emptyMatches : matched, ranges, counts2);
|
|
1363
|
+
if (status !== 0)
|
|
1364
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The numeric ranges could not be counted", { status: -status });
|
|
1365
|
+
return counts2;
|
|
1366
|
+
}
|
|
1367
|
+
function countBooleanFacet(trueBits, falseBits, matched) {
|
|
1368
|
+
const status = booleanFacet(trueBits, falseBits, matched.length === 0 ? emptyMatches : matched, counts);
|
|
1369
|
+
if (status !== 0)
|
|
1370
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The boolean facet could not be counted", { status: -status });
|
|
1371
|
+
return { trueCount: counts[0], falseCount: counts[1] };
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
// src/core/facets/stored-adapter.ts
|
|
1375
|
+
var EMPTY_MATCHES = new Uint32Array(1);
|
|
1376
|
+
function countStoredRanges(values, matched, ranges) {
|
|
1377
|
+
const counts2 = new Uint32Array(ranges.length / 2);
|
|
1378
|
+
const status = storedRangeFacet(values, matched.length === 0 ? EMPTY_MATCHES : matched, ranges, counts2);
|
|
1379
|
+
if (status !== 0)
|
|
1380
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The stored numeric ranges could not be counted", {
|
|
1381
|
+
status: -status
|
|
1382
|
+
});
|
|
1383
|
+
return counts2;
|
|
1384
|
+
}
|
|
1385
|
+
function countStoredFacet(values, matched) {
|
|
1386
|
+
const positions = new Uint32Array(values.kinds.length);
|
|
1387
|
+
const counts2 = new Uint32Array(values.kinds.length);
|
|
1388
|
+
const count = valueFacet(values, matched.length === 0 ? EMPTY_MATCHES : matched, positions, counts2);
|
|
1389
|
+
if (count < 0)
|
|
1390
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The stored values could not be counted", { status: -count });
|
|
1391
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
1392
|
+
for (let at = 0; at < count; at++) {
|
|
1393
|
+
const position = positions[at];
|
|
1394
|
+
const kind = values.kinds[position];
|
|
1395
|
+
const key = kind === 1 ? "false" : kind === 2 ? "true" : kind === 3 ? String(values.numbers[position]) : storedText(values.units, values.textStart[position], values.textLength[position]);
|
|
1396
|
+
buckets.set(key, (buckets.get(key) ?? 0) + counts2[at]);
|
|
1397
|
+
}
|
|
1398
|
+
return buckets;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
// src/core/partition/facets.ts
|
|
1402
|
+
var UNDECLARED_FIELD_TYPE = "";
|
|
1403
|
+
function ordinalBitsetOf(state, matched) {
|
|
1404
|
+
if ("ordinalBitset" in matched) return matched.ordinalBitset;
|
|
1405
|
+
const bitset = createBitSet(state.docStore.internalIdCapacity());
|
|
1406
|
+
for (const docId of matched) {
|
|
1407
|
+
const ordinal = state.docStore.getInternalId(docId);
|
|
1408
|
+
if (ordinal !== void 0) bitsetSet(bitset, ordinal);
|
|
1409
|
+
}
|
|
1410
|
+
return bitset;
|
|
1411
|
+
}
|
|
1412
|
+
function countValuesFromDocuments(state, fieldPath, matched) {
|
|
1413
|
+
return countStoredFacet(storedValuesOf(state.docStore, fieldPath), ordinalBitsetOf(state, matched));
|
|
1414
|
+
}
|
|
1415
|
+
function countNumericRanges(state, fieldPath, ranges, bitset) {
|
|
1416
|
+
const counts2 = /* @__PURE__ */ new Map();
|
|
1417
|
+
const index = state.numericIndexes.get(fieldPath);
|
|
1418
|
+
const bounds = new Float64Array(ranges.length * 2);
|
|
1419
|
+
for (let at = 0; at < ranges.length; at++) {
|
|
1420
|
+
bounds[at * 2] = ranges[at].from;
|
|
1421
|
+
bounds[at * 2 + 1] = ranges[at].to;
|
|
1422
|
+
}
|
|
1423
|
+
const totals = index === void 0 ? countStoredRanges(storedValuesOf(state.docStore, fieldPath), bitset(), bounds) : countRangeFacets(index.regions(), bitset(), bounds);
|
|
1424
|
+
for (let at = 0; at < ranges.length; at++) counts2.set(`${ranges[at].from}-${ranges[at].to}`, totals[at]);
|
|
1425
|
+
return counts2;
|
|
1426
|
+
}
|
|
1427
|
+
function countFieldValues(state, fieldPath, fieldType, bitset, matched) {
|
|
1428
|
+
if (fieldType === "enum" || fieldType === "enum[]") {
|
|
1429
|
+
const index = state.enumIndexes.get(fieldPath);
|
|
1430
|
+
if (index !== void 0) return index.facetCounts(bitset());
|
|
1431
|
+
} else if (fieldType === "boolean" || fieldType === "boolean[]") {
|
|
1432
|
+
const index = state.booleanIndexes.get(fieldPath);
|
|
1433
|
+
if (index !== void 0) {
|
|
1434
|
+
const { trueCount, falseCount } = index.facetCounts(bitset());
|
|
1435
|
+
const counts2 = /* @__PURE__ */ new Map();
|
|
1436
|
+
if (trueCount > 0) counts2.set("true", trueCount);
|
|
1437
|
+
if (falseCount > 0) counts2.set("false", falseCount);
|
|
1438
|
+
return counts2;
|
|
1439
|
+
}
|
|
1440
|
+
} else if (fieldType === "number" || fieldType === "number[]") {
|
|
1441
|
+
const index = state.numericIndexes.get(fieldPath);
|
|
1442
|
+
if (index !== void 0) {
|
|
1443
|
+
const counts2 = /* @__PURE__ */ new Map();
|
|
1444
|
+
for (const [value, count] of index.facetValueCounts(bitset())) {
|
|
1445
|
+
counts2.set(String(value), count);
|
|
1446
|
+
}
|
|
1447
|
+
return counts2;
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
return countValuesFromDocuments(state, fieldPath, matched);
|
|
1451
|
+
}
|
|
1452
|
+
function sortFacetedNumbers(state, config, schema) {
|
|
1453
|
+
const flatSchema = getFlatSchema(state, schema);
|
|
1454
|
+
for (const fieldPath of Object.keys(config)) {
|
|
1455
|
+
const fieldType = flatSchema[fieldPath];
|
|
1456
|
+
if (fieldType !== "number" && fieldType !== "number[]") continue;
|
|
1457
|
+
state.numericIndexes.get(fieldPath)?.sortPending();
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
function computeFacets(state, matched, config, schema) {
|
|
1461
|
+
const result = {};
|
|
1462
|
+
const flatSchema = getFlatSchema(state, schema);
|
|
1463
|
+
let matchedBitset = null;
|
|
1464
|
+
const bitset = () => {
|
|
1465
|
+
if (matchedBitset === null) matchedBitset = ordinalBitsetOf(state, matched);
|
|
1466
|
+
return matchedBitset;
|
|
1467
|
+
};
|
|
1468
|
+
for (const [fieldPath, facetOpts] of Object.entries(config)) {
|
|
1469
|
+
const fieldType = flatSchema[fieldPath] ?? UNDECLARED_FIELD_TYPE;
|
|
1470
|
+
const valueCounts = facetOpts.ranges && (fieldType === "number" || fieldType === "number[]" || fieldType === UNDECLARED_FIELD_TYPE) ? countNumericRanges(state, fieldPath, facetOpts.ranges, bitset) : countFieldValues(state, fieldPath, fieldType, bitset, matched);
|
|
1471
|
+
const ordered = Array.from(valueCounts.entries());
|
|
1472
|
+
const sortDir = facetOpts.sort ?? "desc";
|
|
1473
|
+
ordered.sort((a, b) => (sortDir === "asc" ? a[1] - b[1] : b[1] - a[1]) || compareCodePoints(a[0], b[0]));
|
|
1474
|
+
const keep = Math.min(keptFacetValues(facetOpts.limit) ?? ordered.length, ordered.length);
|
|
1475
|
+
const entries = ordered.slice(0, keep);
|
|
1476
|
+
let errorBound = 0;
|
|
1477
|
+
for (let index = keep; index < ordered.length; index++) {
|
|
1478
|
+
if (ordered[index][1] > errorBound) errorBound = ordered[index][1];
|
|
1479
|
+
}
|
|
1480
|
+
const values = /* @__PURE__ */ Object.create(null);
|
|
1481
|
+
for (const [key, count] of entries) {
|
|
1482
|
+
values[key] = count;
|
|
1483
|
+
}
|
|
1484
|
+
result[fieldPath] = { values, count: entries.length, errorBound };
|
|
1485
|
+
}
|
|
1486
|
+
return result;
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
// src/search/facet-merge.ts
|
|
1490
|
+
function requireFacetableFields(facets, flatSchema, strict) {
|
|
1491
|
+
for (const field of Object.keys(facets)) {
|
|
1492
|
+
const fieldType = flatSchema[field];
|
|
1493
|
+
if (strict && fieldType === void 0) throw undeclaredFieldOnStrictIndex(field, "facet");
|
|
1494
|
+
if (isGeopointOrVectorType(fieldType)) {
|
|
1495
|
+
throw new NarsilError(
|
|
1496
|
+
ErrorCodes.SEARCH_INVALID_FIELD,
|
|
1497
|
+
`The engine counts the values of a text, number, boolean, or enum field, and the schema declares field "${field}" as "${fieldType}"`,
|
|
1498
|
+
{ field, fieldType }
|
|
1499
|
+
);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
function mergeFacets3(partitionFacets, config) {
|
|
1504
|
+
const fields = /* @__PURE__ */ new Map();
|
|
1505
|
+
for (const partition of partitionFacets) {
|
|
1506
|
+
for (const [field, result] of Object.entries(partition)) {
|
|
1507
|
+
let merged = fields.get(field);
|
|
1508
|
+
if (merged === void 0) {
|
|
1509
|
+
merged = { lists: [], bounds: [] };
|
|
1510
|
+
fields.set(field, merged);
|
|
1511
|
+
}
|
|
1512
|
+
merged.lists.push(Object.entries(result.values).map(([value, count]) => ({ value, count })));
|
|
1513
|
+
merged.bounds.push(result.errorBound);
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
const results = /* @__PURE__ */ Object.create(null);
|
|
1517
|
+
for (const [field, merged] of fields) {
|
|
1518
|
+
const options = config[field];
|
|
1519
|
+
const answer = mergeFacetBuckets(
|
|
1520
|
+
merged.lists,
|
|
1521
|
+
merged.bounds,
|
|
1522
|
+
options?.sort === "asc",
|
|
1523
|
+
keptFacetValues(options?.limit)
|
|
1524
|
+
);
|
|
1525
|
+
const values = /* @__PURE__ */ Object.create(null);
|
|
1526
|
+
for (const bucket of answer.buckets) values[bucket.value] = bucket.count;
|
|
1527
|
+
results[field] = { values, count: answer.buckets.length, errorBound: answer.errorBound };
|
|
1528
|
+
}
|
|
1529
|
+
return results;
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
// src/core/stored-values/gather.ts
|
|
1533
|
+
function gatherField(rows, field) {
|
|
1534
|
+
let count = 0;
|
|
1535
|
+
let units3 = 0;
|
|
1536
|
+
let bytes = 0;
|
|
1537
|
+
for (const row of rows) {
|
|
1538
|
+
const ref = row?.[field];
|
|
1539
|
+
if (ref === void 0) continue;
|
|
1540
|
+
const { values, ordinal } = ref;
|
|
1541
|
+
const end = values.starts[ordinal + 1];
|
|
1542
|
+
count = checkedSize(count + end - values.starts[ordinal]);
|
|
1543
|
+
for (let at = values.starts[ordinal]; at < end; at++) {
|
|
1544
|
+
if (values.kinds[at] === 4) units3 = checkedSize(units3 + values.textLength[at]);
|
|
1545
|
+
else if (values.kinds[at] === 5) bytes = checkedSize(bytes + values.textLength[at]);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
const result = {
|
|
1549
|
+
shapes: new Uint8Array(rows.length),
|
|
1550
|
+
starts: new Uint32Array(rows.length + 1),
|
|
1551
|
+
kinds: new Uint8Array(count),
|
|
1552
|
+
numbers: new Float64Array(count),
|
|
1553
|
+
textStart: new Uint32Array(count),
|
|
1554
|
+
textLength: new Uint32Array(count),
|
|
1555
|
+
units: new Uint16Array(units3),
|
|
1556
|
+
bytes: new Uint8Array(bytes)
|
|
1557
|
+
};
|
|
1558
|
+
let next = 0;
|
|
1559
|
+
let unit = 0;
|
|
1560
|
+
let byte = 0;
|
|
1561
|
+
for (let row = 0; row < rows.length; row++) {
|
|
1562
|
+
result.starts[row] = next;
|
|
1563
|
+
const ref = rows[row]?.[field];
|
|
1564
|
+
if (ref === void 0) continue;
|
|
1565
|
+
const { values, ordinal } = ref;
|
|
1566
|
+
result.shapes[row] = values.shapes[ordinal];
|
|
1567
|
+
const start = values.starts[ordinal];
|
|
1568
|
+
const end = values.starts[ordinal + 1];
|
|
1569
|
+
result.kinds.set(values.kinds.subarray(start, end), next);
|
|
1570
|
+
result.numbers.set(values.numbers.subarray(start, end), next);
|
|
1571
|
+
result.textLength.set(values.textLength.subarray(start, end), next);
|
|
1572
|
+
for (let at = start; at < end; at++, next++) {
|
|
1573
|
+
const textStart = values.textStart[at];
|
|
1574
|
+
const textEnd = textStart + values.textLength[at];
|
|
1575
|
+
if (values.kinds[at] === 4) {
|
|
1576
|
+
result.textStart[next] = unit;
|
|
1577
|
+
result.units.set(values.units.subarray(textStart, textEnd), unit);
|
|
1578
|
+
unit += textEnd - textStart;
|
|
1579
|
+
} else if (values.kinds[at] === 5) {
|
|
1580
|
+
result.textStart[next] = byte;
|
|
1581
|
+
result.bytes.set(values.bytes.subarray(textStart, textEnd), byte);
|
|
1582
|
+
byte += textEnd - textStart;
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
result.starts[rows.length] = next;
|
|
1587
|
+
return result;
|
|
1588
|
+
}
|
|
1589
|
+
function gatheredStoredFields(rows, fieldCount) {
|
|
1590
|
+
checkedSize(rows.length);
|
|
1591
|
+
const first = rows[0];
|
|
1592
|
+
if (first !== void 0 && first.length > 0 && first.length === fieldCount && rows.every(
|
|
1593
|
+
(row) => row !== void 0 && row.length === fieldCount && row.every((ref, field) => ref.values === first[field].values && ref.ordinal === row[0].ordinal)
|
|
1594
|
+
)) {
|
|
1595
|
+
return { fields: first.map((ref) => ref.values), ordinals: Uint32Array.from(rows, (row) => row?.[0].ordinal ?? 0) };
|
|
1596
|
+
}
|
|
1597
|
+
return {
|
|
1598
|
+
fields: Array.from({ length: fieldCount }, (_, field) => gatherField(rows, field)),
|
|
1599
|
+
ordinals: Uint32Array.from(rows, (_, ordinal) => ordinal)
|
|
1600
|
+
};
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
// src/search/grouping.ts
|
|
1604
|
+
function requireGroupableFields(fields, flatSchema, strict) {
|
|
1605
|
+
for (const field of fields) {
|
|
1606
|
+
const fieldType = flatSchema[field];
|
|
1607
|
+
if (strict && fieldType === void 0) throw undeclaredFieldOnStrictIndex(field, "group");
|
|
1608
|
+
if (fieldType !== void 0 && VECTOR_PATTERN.test(fieldType)) {
|
|
1609
|
+
throw new NarsilError(
|
|
1610
|
+
ErrorCodes.SEARCH_INVALID_FIELD,
|
|
1611
|
+
`The engine groups hits by a text, number, boolean, enum, or geopoint field, and the schema declares field "${field}" as "${fieldType}"`,
|
|
1612
|
+
{ field, fieldType }
|
|
1613
|
+
);
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
function hitsKeptPerGroup(maxPerGroup) {
|
|
1618
|
+
if (maxPerGroup === void 0 || !Number.isFinite(maxPerGroup) || maxPerGroup < 1) return DEFAULT_MAX_PER_GROUP;
|
|
1619
|
+
return Math.floor(maxPerGroup);
|
|
1620
|
+
}
|
|
1621
|
+
function applyGrouping(hits, group, documents) {
|
|
1622
|
+
if (!group.fields || group.fields.length === 0) {
|
|
1623
|
+
return [{ values: {}, hits }];
|
|
1624
|
+
}
|
|
1625
|
+
if (hits.length > 4294967295)
|
|
1626
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The hit count exceeds the group layout");
|
|
1627
|
+
const { fields, ordinals } = gatheredStoredFields(
|
|
1628
|
+
hits.map((hit) => documents.storedFields(hit.id, group.fields)),
|
|
1629
|
+
group.fields.length
|
|
1630
|
+
);
|
|
1631
|
+
const scores = Float64Array.from(hits, (hit) => hit.score ?? 0);
|
|
1632
|
+
const order = Uint32Array.from(hits, (_, position) => position).sort(
|
|
1633
|
+
(left, right) => compareCodePoints(hits[left].id, hits[right].id)
|
|
1634
|
+
);
|
|
1635
|
+
const idRanks = new Uint32Array(hits.length);
|
|
1636
|
+
let rank = 0;
|
|
1637
|
+
for (let at = 0; at < order.length; at++) {
|
|
1638
|
+
if (at > 0 && hits[order[at - 1]].id !== hits[order[at]].id) rank++;
|
|
1639
|
+
idRanks[order[at]] = rank;
|
|
1640
|
+
}
|
|
1641
|
+
const keptPerGroup = hitsKeptPerGroup(group.maxPerGroup);
|
|
1642
|
+
const limit = group.limit !== void 0 && Number.isFinite(group.limit) && group.limit >= 0 ? Math.min(Math.floor(group.limit), hits.length) : null;
|
|
1643
|
+
const heads = new Uint32Array(hits.length);
|
|
1644
|
+
const offsets = new Uint32Array(hits.length + 1);
|
|
1645
|
+
const positions = new Uint32Array(hits.length);
|
|
1646
|
+
const members = new Uint32Array(hits.length);
|
|
1647
|
+
const found = groupHits(
|
|
1648
|
+
fields,
|
|
1649
|
+
ordinals,
|
|
1650
|
+
scores,
|
|
1651
|
+
idRanks,
|
|
1652
|
+
group.reduce === void 0 ? Math.min(keptPerGroup, hits.length) : hits.length,
|
|
1653
|
+
group.reduce === void 0 ? limit : null,
|
|
1654
|
+
heads,
|
|
1655
|
+
offsets,
|
|
1656
|
+
positions,
|
|
1657
|
+
members
|
|
1658
|
+
);
|
|
1659
|
+
if (found < 0) throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The hits cannot be grouped", { status: found });
|
|
1660
|
+
const groups = [];
|
|
1661
|
+
for (let at = 0; at < found; at++) {
|
|
1662
|
+
const values = /* @__PURE__ */ Object.create(null);
|
|
1663
|
+
const row = documents.fieldValues(hits[heads[at]].id, group.fields);
|
|
1664
|
+
for (let field = 0; field < group.fields.length; field++) values[group.fields[field]] = row?.[field];
|
|
1665
|
+
const everyHit = [];
|
|
1666
|
+
for (let member = offsets[at]; member < offsets[at + 1]; member++) everyHit.push(hits[positions[member]]);
|
|
1667
|
+
const result = { values, hits: everyHit.slice(0, keptPerGroup) };
|
|
1668
|
+
if (group.reduce) {
|
|
1669
|
+
const folded = foldGroupReducer(
|
|
1670
|
+
group.reduce,
|
|
1671
|
+
everyHit.map((hit) => ({ document: documents.document(hit.id), score: hit.score ?? 0 }))
|
|
1672
|
+
);
|
|
1673
|
+
if (folded.reducerError !== void 0) {
|
|
1674
|
+
result.reducerError = folded.reducerError;
|
|
1675
|
+
} else {
|
|
1676
|
+
result.reduced = folded.reduced;
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
groups.push(result);
|
|
1680
|
+
}
|
|
1681
|
+
if (limit !== null && groups.length > limit) groups.length = limit;
|
|
1682
|
+
return groups;
|
|
1683
|
+
}
|
|
1684
|
+
function foldGroupReducer(reduce, rows) {
|
|
1685
|
+
try {
|
|
1686
|
+
let accumulator = reduce.initialValue();
|
|
1687
|
+
for (const row of rows) {
|
|
1688
|
+
if (row.document !== void 0) {
|
|
1689
|
+
accumulator = reduce.reducer(accumulator, row.document, row.score);
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
return { reduced: accumulator };
|
|
1693
|
+
} catch (err) {
|
|
1694
|
+
return { reducerError: err instanceof Error ? err.message : String(err) };
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
// src/search/pinned-core.ts
|
|
1699
|
+
var MAX_ENTRIES = 2147483647;
|
|
1700
|
+
var EMPTY_SLOT = -2147483648;
|
|
1701
|
+
function uint32(value) {
|
|
1702
|
+
return Number.isInteger(value) && value >= 0 && value <= 4294967295;
|
|
1703
|
+
}
|
|
1704
|
+
function selectFree(tree, rank) {
|
|
1705
|
+
let step = 2 ** Math.floor(Math.log2(tree.length));
|
|
1706
|
+
let prefix = 0;
|
|
1707
|
+
while (step !== 0) {
|
|
1708
|
+
const next = prefix + step;
|
|
1709
|
+
if (next <= tree.length && tree[next - 1] <= rank) {
|
|
1710
|
+
rank -= tree[next - 1];
|
|
1711
|
+
prefix = next;
|
|
1712
|
+
}
|
|
1713
|
+
step = Math.floor(step / 2);
|
|
1714
|
+
}
|
|
1715
|
+
return prefix;
|
|
1716
|
+
}
|
|
1717
|
+
function placePinned(count, ranked, positions, depth, output) {
|
|
1718
|
+
if (!uint32(count) || count > MAX_ENTRIES || ranked.length > MAX_ENTRIES || ranked.length !== positions.length || depth !== null && !uint32(depth) || regionsOverlap(output, ranked) || regionsOverlap(output, positions)) {
|
|
1719
|
+
return -1;
|
|
1720
|
+
}
|
|
1721
|
+
let removedCount = 0;
|
|
1722
|
+
let placedCount = 0;
|
|
1723
|
+
for (let pin = 0; pin < ranked.length; pin++) {
|
|
1724
|
+
if (ranked[pin] < -1 || ranked[pin] >= count) return -1;
|
|
1725
|
+
if (ranked[pin] >= 0) removedCount++;
|
|
1726
|
+
if (depth === null || positions[pin] < depth) placedCount++;
|
|
1727
|
+
}
|
|
1728
|
+
if (removedCount > count) return -1;
|
|
1729
|
+
const needed = count - removedCount + placedCount;
|
|
1730
|
+
if (needed > output.length) return -3;
|
|
1731
|
+
const removed = new Uint32Array(removedCount);
|
|
1732
|
+
const ordered = new Uint32Array(placedCount);
|
|
1733
|
+
let removedAt = 0;
|
|
1734
|
+
let placedAt = 0;
|
|
1735
|
+
for (let pin = 0; pin < ranked.length; pin++) {
|
|
1736
|
+
if (ranked[pin] >= 0) removed[removedAt++] = ranked[pin];
|
|
1737
|
+
if (depth === null || positions[pin] < depth) ordered[placedAt++] = pin;
|
|
1738
|
+
}
|
|
1739
|
+
removed.sort();
|
|
1740
|
+
for (let at = 1; at < removed.length; at++) if (removed[at] === removed[at - 1]) return -1;
|
|
1741
|
+
ordered.sort((left, right) => positions[left] - positions[right] || left - right);
|
|
1742
|
+
const tree = new Uint32Array(needed);
|
|
1743
|
+
for (let at = 0; at < needed; at++) {
|
|
1744
|
+
const oneBased = at + 1;
|
|
1745
|
+
tree[at] = oneBased & -oneBased;
|
|
1746
|
+
}
|
|
1747
|
+
output.fill(EMPTY_SLOT, 0, needed);
|
|
1748
|
+
const organic = count - removedCount;
|
|
1749
|
+
for (let remaining = ordered.length; remaining > 0; remaining--) {
|
|
1750
|
+
const pin = ordered[remaining - 1];
|
|
1751
|
+
const rank = Math.min(positions[pin], organic + remaining - 1);
|
|
1752
|
+
const slot = selectFree(tree, rank);
|
|
1753
|
+
output[slot] = -1 - pin;
|
|
1754
|
+
for (let at = slot; at < needed; at = (at | at + 1) >>> 0) tree[at]--;
|
|
1755
|
+
}
|
|
1756
|
+
let next = 0;
|
|
1757
|
+
let dropped = 0;
|
|
1758
|
+
for (let at = 0; at < needed; at++) {
|
|
1759
|
+
if (output[at] !== EMPTY_SLOT) continue;
|
|
1760
|
+
while (dropped < removed.length && removed[dropped] === next) {
|
|
1761
|
+
dropped++;
|
|
1762
|
+
next++;
|
|
1763
|
+
}
|
|
1764
|
+
output[at] = next++;
|
|
1765
|
+
}
|
|
1766
|
+
return needed;
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
// src/search/pinned-order.ts
|
|
1770
|
+
function pinnedOrder(ids, pins, depth) {
|
|
1771
|
+
if (ids.length > 2147483647 || pins.length > 2147483647) {
|
|
1772
|
+
throw new NarsilError(
|
|
1773
|
+
ErrorCodes.CONFIG_INVALID,
|
|
1774
|
+
"The ranked result and pin lists must each fit signed 32-bit positions"
|
|
1775
|
+
);
|
|
1776
|
+
}
|
|
1777
|
+
const byId = /* @__PURE__ */ new Map();
|
|
1778
|
+
for (let at = 0; at < ids.length; at++) byId.set(ids[at], at);
|
|
1779
|
+
const ranked = new Int32Array(pins.length);
|
|
1780
|
+
const positions = new Uint32Array(pins.length);
|
|
1781
|
+
let removed = 0;
|
|
1782
|
+
let placed = 0;
|
|
1783
|
+
let widePositions = false;
|
|
1784
|
+
for (let pin = 0; pin < pins.length; pin++) {
|
|
1785
|
+
const entry = pins[pin];
|
|
1786
|
+
if (!Number.isInteger(entry.position) || entry.position < 0) {
|
|
1787
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "A pinned document position must be a non-negative integer", {
|
|
1788
|
+
docId: entry.docId,
|
|
1789
|
+
position: entry.position
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
ranked[pin] = byId.get(entry.docId) ?? -1;
|
|
1793
|
+
positions[pin] = Math.min(entry.position, 4294967295);
|
|
1794
|
+
if (ranked[pin] >= 0) removed++;
|
|
1795
|
+
if (depth === null || entry.position < depth) placed++;
|
|
1796
|
+
if (entry.position > 4294967295) widePositions = true;
|
|
1797
|
+
}
|
|
1798
|
+
const output = new Int32Array(ids.length - removed + placed);
|
|
1799
|
+
const source = widePositions ? Uint32Array.from(pins, (_, pin) => pin).sort(
|
|
1800
|
+
(left, right) => pins[left].position - pins[right].position || left - right
|
|
1801
|
+
) : null;
|
|
1802
|
+
const count = placePinned(
|
|
1803
|
+
ids.length,
|
|
1804
|
+
source === null ? ranked : Int32Array.from(source, (pin) => ranked[pin]),
|
|
1805
|
+
source === null ? positions : Uint32Array.from(source, (pin) => positions[pin]),
|
|
1806
|
+
depth,
|
|
1807
|
+
output
|
|
1808
|
+
);
|
|
1809
|
+
if (count < 0) {
|
|
1810
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The pinned documents cannot be placed", { status: count });
|
|
1811
|
+
}
|
|
1812
|
+
if (source !== null) {
|
|
1813
|
+
for (let at = 0; at < count; at++) if (output[at] < 0) output[at] = -1 - source[-1 - output[at]];
|
|
1814
|
+
}
|
|
1815
|
+
return { order: output.subarray(0, count), ranked };
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
// src/search/pinning.ts
|
|
1819
|
+
function dedupePinnedEntries(pinned) {
|
|
1820
|
+
const deduped = [];
|
|
1821
|
+
const seen2 = /* @__PURE__ */ new Set();
|
|
1822
|
+
for (const entry of pinned) {
|
|
1823
|
+
if (seen2.has(entry.docId)) continue;
|
|
1824
|
+
seen2.add(entry.docId);
|
|
1825
|
+
deduped.push(entry);
|
|
1826
|
+
}
|
|
1827
|
+
return deduped;
|
|
1828
|
+
}
|
|
1829
|
+
function placePinned2(hits, pinned, resolveDoc) {
|
|
1830
|
+
const entries = [];
|
|
1831
|
+
const documents = [];
|
|
1832
|
+
for (const entry of dedupePinnedEntries(pinned)) {
|
|
1833
|
+
const document = resolveDoc(entry.docId);
|
|
1834
|
+
if (document === void 0) continue;
|
|
1835
|
+
entries.push(entry);
|
|
1836
|
+
documents.push(document);
|
|
1837
|
+
}
|
|
1838
|
+
const { order, ranked } = pinnedOrder(
|
|
1839
|
+
hits.map((hit) => hit.id),
|
|
1840
|
+
entries,
|
|
1841
|
+
null
|
|
1842
|
+
);
|
|
1843
|
+
const result = new Array(order.length);
|
|
1844
|
+
let placedFromOutside = 0;
|
|
1845
|
+
for (let at = 0; at < order.length; at++) {
|
|
1846
|
+
const source = order[at];
|
|
1847
|
+
result[at] = source < 0 ? documents[-1 - source] : hits[source];
|
|
1848
|
+
if (source < 0 && ranked[-1 - source] < 0) placedFromOutside++;
|
|
1849
|
+
}
|
|
1850
|
+
return { hits: result, placedFromOutside };
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
// src/core/sort-values/adapter.ts
|
|
1854
|
+
var modes = { min: 0, max: 1, avg: 2, median: 3 };
|
|
1855
|
+
var outNumber = new Float64Array(1);
|
|
1856
|
+
var outText = new Uint32Array(2);
|
|
1857
|
+
var scalar = {
|
|
1858
|
+
shapes: new Uint8Array([1]),
|
|
1859
|
+
starts: new Uint32Array([0, 1]),
|
|
1860
|
+
kinds: new Uint8Array(1),
|
|
1861
|
+
numbers: new Float64Array(1),
|
|
1862
|
+
textStart: new Uint32Array(1),
|
|
1863
|
+
textLength: new Uint32Array(1),
|
|
1864
|
+
units: new Uint16Array(0),
|
|
1865
|
+
bytes: new Uint8Array(0)
|
|
1866
|
+
};
|
|
1867
|
+
function storedValue(value) {
|
|
1868
|
+
if (typeof value === "number") {
|
|
1869
|
+
scalar.kinds[0] = 3;
|
|
1870
|
+
scalar.numbers[0] = value;
|
|
1871
|
+
} else if (typeof value === "boolean") scalar.kinds[0] = value ? 2 : 1;
|
|
1872
|
+
else if (value === null || value === void 0) scalar.kinds[0] = 0;
|
|
1873
|
+
else if (typeof value === "string") {
|
|
1874
|
+
if (value.length > 1073741824) {
|
|
1875
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The stored string exceeds the native text layout");
|
|
1876
|
+
}
|
|
1877
|
+
if (scalar.units.length < value.length) {
|
|
1878
|
+
scalar.units = new Uint16Array(Math.max(value.length, Math.min(1073741824, scalar.units.length * 2)));
|
|
1879
|
+
}
|
|
1880
|
+
scalar.kinds[0] = 4;
|
|
1881
|
+
scalar.textLength[0] = value.length;
|
|
1882
|
+
for (let index = 0; index < value.length; index++) scalar.units[index] = value.charCodeAt(index);
|
|
1883
|
+
} else return buildStoredValues([value]);
|
|
1884
|
+
return scalar;
|
|
1885
|
+
}
|
|
1886
|
+
function reduceWith(operation, value, mode) {
|
|
1887
|
+
return reduceStoredWith(operation, storedValue(value), 0, mode);
|
|
1888
|
+
}
|
|
1889
|
+
function reduceStoredWith(operation, values, ordinal, mode) {
|
|
1890
|
+
const kind = operation(values, ordinal, modes[mode], outNumber, outText);
|
|
1891
|
+
if (kind < 0) {
|
|
1892
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The sort value could not be reduced", { status: -kind });
|
|
1893
|
+
}
|
|
1894
|
+
if (kind === 1) return outNumber[0];
|
|
1895
|
+
if (kind === 2) return String.fromCharCode(...values.units.subarray(outText[0], outText[0] + outText[1]));
|
|
1896
|
+
if (kind === 3) return false;
|
|
1897
|
+
if (kind === 4) return true;
|
|
1898
|
+
return null;
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1901
|
+
// src/core/sort-values/portable.ts
|
|
1902
|
+
function textLength(values, index) {
|
|
1903
|
+
const start = values.textStart[index];
|
|
1904
|
+
const end = start + values.textLength[index];
|
|
1905
|
+
let position = start;
|
|
1906
|
+
for (let count = 0; position < end && count < SORT_VALUE_MAX_CODE_POINTS; count++) {
|
|
1907
|
+
const unit = values.units[position++];
|
|
1908
|
+
if (unit >= 55296 && unit < 56320 && position < end) {
|
|
1909
|
+
const low = values.units[position];
|
|
1910
|
+
if (low >= 56320 && low < 57344) position++;
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
return position - start;
|
|
1914
|
+
}
|
|
1915
|
+
function textValue(values, index) {
|
|
1916
|
+
const start = values.textStart[index];
|
|
1917
|
+
const length = textLength(values, index);
|
|
1918
|
+
return String.fromCharCode(...values.units.subarray(start, start + length));
|
|
1919
|
+
}
|
|
1920
|
+
function kindOf(values, index) {
|
|
1921
|
+
const kind = values.kinds[index];
|
|
1922
|
+
if (kind === 3) return Number.isFinite(values.numbers[index]) ? 1 : 0;
|
|
1923
|
+
if (kind === 4) return 2;
|
|
1924
|
+
if (kind === 1) return 3;
|
|
1925
|
+
if (kind === 2) return 4;
|
|
1926
|
+
return 0;
|
|
1927
|
+
}
|
|
1928
|
+
function compare(values, left, right) {
|
|
1929
|
+
const kind = kindOf(values, left);
|
|
1930
|
+
const other = kindOf(values, right);
|
|
1931
|
+
if (kind !== other) return kind - other;
|
|
1932
|
+
if (kind === 1) return values.numbers[left] - values.numbers[right];
|
|
1933
|
+
if (kind === 2) return compareSortStrings(textValue(values, left), textValue(values, right));
|
|
1934
|
+
return 0;
|
|
1935
|
+
}
|
|
1936
|
+
function reduceNumbers(values, start, end, mode) {
|
|
1937
|
+
let mean = 0;
|
|
1938
|
+
let count = 0;
|
|
1939
|
+
const numbers = [];
|
|
1940
|
+
for (let position = start; position < end; position++) {
|
|
1941
|
+
if (kindOf(values, position) !== 1) continue;
|
|
1942
|
+
const value = values.numbers[position];
|
|
1943
|
+
count++;
|
|
1944
|
+
if (mode === 3) numbers.push(value);
|
|
1945
|
+
else {
|
|
1946
|
+
const difference = value - mean;
|
|
1947
|
+
mean = Number.isFinite(difference) ? mean + difference / count : mean * ((count - 1) / count) + value / count;
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
if (count === 0) return null;
|
|
1951
|
+
if (mode !== 3) return mean;
|
|
1952
|
+
numbers.sort((left, right) => left - right);
|
|
1953
|
+
const middle = Math.floor(count / 2);
|
|
1954
|
+
return count % 2 === 1 ? numbers[middle] : numbers[middle - 1] / 2 + numbers[middle] / 2;
|
|
1955
|
+
}
|
|
1956
|
+
var sortValue = (input, ordinal, mode, outputNumber, outputText) => {
|
|
1957
|
+
if (!Number.isInteger(ordinal) || ordinal < 0 || ordinal > 4294967295 || !Number.isInteger(mode) || mode < 0 || mode > 3)
|
|
1958
|
+
return -1;
|
|
1959
|
+
const fields = readStoredFields([input]);
|
|
1960
|
+
const outNumber2 = typedRegion(outputNumber, "Float64Array");
|
|
1961
|
+
const outText2 = typedRegion(outputText, "Uint32Array");
|
|
1962
|
+
const values = fields?.[0];
|
|
1963
|
+
if (values === void 0 || outNumber2 === null || outText2 === null || ordinal >= values.shapes.length) return -1;
|
|
1964
|
+
if (regionsOverlap(outNumber2, outText2)) return -1;
|
|
1965
|
+
for (const region of Object.values(values)) {
|
|
1966
|
+
if (regionsOverlap(region, outNumber2) || regionsOverlap(region, outText2)) return -1;
|
|
1967
|
+
}
|
|
1968
|
+
if (!validStoredValue(values, ordinal)) return -1;
|
|
1969
|
+
const start = values.starts[ordinal];
|
|
1970
|
+
const end = values.starts[ordinal + 1];
|
|
1971
|
+
for (let position = start; position < end; position++) {
|
|
1972
|
+
if (values.kinds[position] === 4 && values.textLength[position] > 1073741824) return -1;
|
|
1973
|
+
}
|
|
1974
|
+
if (outNumber2.length < 1 || outText2.length < 2) return -3;
|
|
1975
|
+
if (values.shapes[ordinal] === 2 && mode >= 2) {
|
|
1976
|
+
const value = reduceNumbers(values, start, end, mode);
|
|
1977
|
+
if (value === null) return 0;
|
|
1978
|
+
outNumber2[0] = value;
|
|
1979
|
+
return 1;
|
|
1980
|
+
}
|
|
1981
|
+
let chosen = end;
|
|
1982
|
+
for (let position = start; position < end; position++) {
|
|
1983
|
+
if (kindOf(values, position) === 0) continue;
|
|
1984
|
+
if (chosen === end) chosen = position;
|
|
1985
|
+
else {
|
|
1986
|
+
const order = compare(values, position, chosen);
|
|
1987
|
+
if (mode === 1 ? order > 0 : order < 0) chosen = position;
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
const kind = chosen === end ? 0 : kindOf(values, chosen);
|
|
1991
|
+
if (kind === 1) outNumber2[0] = values.numbers[chosen];
|
|
1992
|
+
else if (kind === 2) {
|
|
1993
|
+
outText2[0] = values.textStart[chosen];
|
|
1994
|
+
outText2[1] = textLength(values, chosen);
|
|
1995
|
+
}
|
|
1996
|
+
return kind;
|
|
1997
|
+
};
|
|
1998
|
+
|
|
1999
|
+
// src/core/sort-values/regions.ts
|
|
2000
|
+
function captureSortValues(value) {
|
|
2001
|
+
if (value === null || typeof value !== "object") return null;
|
|
2002
|
+
return {
|
|
2003
|
+
kinds: value.kinds,
|
|
2004
|
+
numbers: value.numbers,
|
|
2005
|
+
textStart: value.textStart,
|
|
2006
|
+
textLength: value.textLength,
|
|
2007
|
+
units: value.units
|
|
2008
|
+
};
|
|
2009
|
+
}
|
|
2010
|
+
function readSortRegions(value) {
|
|
2011
|
+
if (value === null) return null;
|
|
2012
|
+
const kinds = typedRegion(value.kinds, "Uint8Array");
|
|
2013
|
+
const numbers = typedRegion(value.numbers, "Float64Array");
|
|
2014
|
+
const textStart = typedRegion(value.textStart, "Uint32Array");
|
|
2015
|
+
const textLength2 = typedRegion(value.textLength, "Uint32Array");
|
|
2016
|
+
const units3 = typedRegion(value.units, "Uint16Array");
|
|
2017
|
+
if (kinds === null || numbers === null || textStart === null || textLength2 === null || units3 === null) return null;
|
|
2018
|
+
return { kinds, numbers, textStart, textLength: textLength2, units: units3 };
|
|
2019
|
+
}
|
|
2020
|
+
function validSortValue(values, ordinal) {
|
|
2021
|
+
const kind = values.kinds[ordinal];
|
|
2022
|
+
if (kind > 4) return false;
|
|
2023
|
+
if (kind === 1) return Number.isFinite(values.numbers[ordinal]);
|
|
2024
|
+
if (kind !== 2) return true;
|
|
2025
|
+
const start = values.textStart[ordinal];
|
|
2026
|
+
const length = values.textLength[ordinal];
|
|
2027
|
+
if (start > values.units.length || length > values.units.length - start) return false;
|
|
2028
|
+
const end = start + length;
|
|
2029
|
+
let at = start;
|
|
2030
|
+
for (let count = 0; at < end && count < SORT_VALUE_MAX_CODE_POINTS; count++) {
|
|
2031
|
+
const high = values.units[at++];
|
|
2032
|
+
if (high >= 55296 && high < 56320 && at < end) {
|
|
2033
|
+
const low = values.units[at];
|
|
2034
|
+
if (low >= 56320 && low < 57344) at++;
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
return at === end;
|
|
2038
|
+
}
|
|
2039
|
+
function sortValueAt(values, ordinal) {
|
|
2040
|
+
const kind = values.kinds[ordinal];
|
|
2041
|
+
if (kind === 1) return values.numbers[ordinal];
|
|
2042
|
+
if (kind === 2) {
|
|
2043
|
+
const start = values.textStart[ordinal];
|
|
2044
|
+
return String.fromCharCode(...values.units.subarray(start, start + values.textLength[ordinal]));
|
|
2045
|
+
}
|
|
2046
|
+
if (kind === 3) return false;
|
|
2047
|
+
if (kind === 4) return true;
|
|
2048
|
+
return null;
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
// src/core/sort-values/order.ts
|
|
2052
|
+
var buildSortOrder = (currentInput, liveInput, valuesInput, ranksInput, orderedInput, dirtyInput, countsInput) => {
|
|
2053
|
+
const currentHeld = captureSortValues(currentInput);
|
|
2054
|
+
const valuesHeld = captureSortValues(valuesInput);
|
|
2055
|
+
const current = readSortRegions(currentHeld);
|
|
2056
|
+
const values = readSortRegions(valuesHeld);
|
|
2057
|
+
const live = typedRegion(liveInput, "Uint32Array");
|
|
2058
|
+
const ranks = typedRegion(ranksInput, "Int32Array");
|
|
2059
|
+
const ordered = typedRegion(orderedInput, "Uint32Array");
|
|
2060
|
+
const dirty = typedRegion(dirtyInput, "Uint32Array");
|
|
2061
|
+
const counts2 = typedRegion(countsInput, "Uint32Array");
|
|
2062
|
+
if (current === null || values === null || live === null || ranks === null || ordered === null || dirty === null || counts2 === null)
|
|
2063
|
+
return -1;
|
|
2064
|
+
const count = current.kinds.length;
|
|
2065
|
+
if (count > 4294967295 || current.numbers.length !== count || current.textStart.length !== count || current.textLength.length !== count || current.units.length > 4294967295)
|
|
2066
|
+
return -1;
|
|
2067
|
+
const words = Math.max(1, Math.ceil(count / 32));
|
|
2068
|
+
if (live.length < words) return -1;
|
|
2069
|
+
const inputs = [...Object.values(current), live];
|
|
2070
|
+
const outputs = [...Object.values(values), ranks, ordered, dirty, counts2];
|
|
2071
|
+
for (let target = 0; target < outputs.length; target++) {
|
|
2072
|
+
for (const input of inputs) if (regionsOverlap(outputs[target], input)) return -1;
|
|
2073
|
+
for (let prior = 0; prior < target; prior++) if (regionsOverlap(outputs[target], outputs[prior])) return -1;
|
|
2074
|
+
}
|
|
2075
|
+
const present = [];
|
|
2076
|
+
for (let ordinal = 0; ordinal < count; ordinal++) {
|
|
2077
|
+
if ((live[ordinal >>> 5] & 1 << (ordinal & 31)) === 0) continue;
|
|
2078
|
+
if (!validSortValue(current, ordinal)) return -1;
|
|
2079
|
+
if (current.kinds[ordinal] !== 0) present.push(ordinal);
|
|
2080
|
+
}
|
|
2081
|
+
if (ranks.length < count || ordered.length < present.length || dirty.length < words || counts2.length < 2) return -3;
|
|
2082
|
+
const comparable = new Map(present.map((ordinal) => [ordinal, sortValueAt(current, ordinal)]));
|
|
2083
|
+
const compare2 = (left, right) => compareComparableValues(comparable.get(left) ?? null, comparable.get(right) ?? null, "asc");
|
|
2084
|
+
present.sort(compare2);
|
|
2085
|
+
const distinct = [];
|
|
2086
|
+
let units3 = 0;
|
|
2087
|
+
for (let at = 0; at < present.length; at++) {
|
|
2088
|
+
const ordinal = present[at];
|
|
2089
|
+
if (at > 0 && compare2(present[at - 1], ordinal) === 0) continue;
|
|
2090
|
+
distinct.push(ordinal);
|
|
2091
|
+
if (current.kinds[ordinal] === 2) units3 += current.textLength[ordinal];
|
|
2092
|
+
}
|
|
2093
|
+
if (distinct.length > 1073741823 || units3 > 4294967295) return -1;
|
|
2094
|
+
if (distinct.length > values.kinds.length || distinct.length > values.numbers.length || distinct.length > values.textStart.length || distinct.length > values.textLength.length || units3 > values.units.length)
|
|
2095
|
+
return -3;
|
|
2096
|
+
ranks.fill(-1, 0, count);
|
|
2097
|
+
let unique = 0;
|
|
2098
|
+
let unit = 0;
|
|
2099
|
+
for (let at = 0; at < present.length; at++) {
|
|
2100
|
+
const ordinal = present[at];
|
|
2101
|
+
if (at === 0 || compare2(present[at - 1], ordinal) !== 0) {
|
|
2102
|
+
const kind = current.kinds[ordinal];
|
|
2103
|
+
values.kinds[unique] = kind;
|
|
2104
|
+
values.numbers[unique] = 0;
|
|
2105
|
+
values.textStart[unique] = 0;
|
|
2106
|
+
values.textLength[unique] = 0;
|
|
2107
|
+
if (kind === 1) values.numbers[unique] = current.numbers[ordinal] === 0 ? 0 : current.numbers[ordinal];
|
|
2108
|
+
else if (kind === 2) {
|
|
2109
|
+
const start = current.textStart[ordinal];
|
|
2110
|
+
const length = current.textLength[ordinal];
|
|
2111
|
+
values.textStart[unique] = unit;
|
|
2112
|
+
values.textLength[unique] = length;
|
|
2113
|
+
values.units.set(current.units.subarray(start, start + length), unit);
|
|
2114
|
+
unit += length;
|
|
2115
|
+
}
|
|
2116
|
+
unique++;
|
|
2117
|
+
}
|
|
2118
|
+
ranks[ordinal] = unique * 2;
|
|
2119
|
+
ordered[at] = ordinal;
|
|
2120
|
+
}
|
|
2121
|
+
dirty.fill(0, 0, words);
|
|
2122
|
+
counts2[0] = unique;
|
|
2123
|
+
counts2[1] = present.length;
|
|
2124
|
+
return 0;
|
|
2125
|
+
};
|
|
2126
|
+
|
|
2127
|
+
// src/core/sort-values/range.ts
|
|
2128
|
+
function captureColumn(column) {
|
|
2129
|
+
if (column === null || typeof column !== "object") return null;
|
|
2130
|
+
return {
|
|
2131
|
+
current: captureSortValues(column.current),
|
|
2132
|
+
values: captureSortValues(column.values),
|
|
2133
|
+
ranks: column.ranks,
|
|
2134
|
+
ordered: column.ordered,
|
|
2135
|
+
dirty: column.dirty
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
function validShape(values) {
|
|
2139
|
+
const count = values.kinds.length;
|
|
2140
|
+
return count <= 4294967295 && values.numbers.length === count && values.textStart.length === count && values.textLength.length === count && values.units.length <= 4294967295;
|
|
2141
|
+
}
|
|
2142
|
+
function boundText(units3) {
|
|
2143
|
+
return units3 === null ? null : truncateSortString(String.fromCharCode(...units3.subarray(0, SORT_VALUE_MAX_CODE_POINTS * 2)));
|
|
2144
|
+
}
|
|
2145
|
+
var textRange = (columnInput, lowerInput, lowerInclusive, upperInput, upperInclusive, liveInput, outputInput) => {
|
|
2146
|
+
const held = captureColumn(columnInput);
|
|
2147
|
+
if (held === null || typeof lowerInclusive !== "boolean" || typeof upperInclusive !== "boolean") return -1;
|
|
2148
|
+
const current = readSortRegions(held.current);
|
|
2149
|
+
const values = readSortRegions(held.values);
|
|
2150
|
+
const ranks = typedRegion(held.ranks, "Int32Array");
|
|
2151
|
+
const ordered = typedRegion(held.ordered, "Uint32Array");
|
|
2152
|
+
const dirty = typedRegion(held.dirty, "Uint32Array");
|
|
2153
|
+
const live = typedRegion(liveInput, "Uint32Array");
|
|
2154
|
+
const output = typedRegion(outputInput, "Uint32Array");
|
|
2155
|
+
const lowerUnits = lowerInput === null ? null : typedRegion(lowerInput, "Uint16Array");
|
|
2156
|
+
const upperUnits = upperInput === null ? null : typedRegion(upperInput, "Uint16Array");
|
|
2157
|
+
if (current === null || values === null || ranks === null || ordered === null || dirty === null || live === null || output === null || lowerUnits === null && lowerInput !== null || upperUnits === null && upperInput !== null)
|
|
2158
|
+
return -1;
|
|
2159
|
+
if (!validShape(current) || !validShape(values) || values.kinds.length > 1073741823 || ranks.length > current.kinds.length || ordered.length > 4294967295 || lowerUnits !== null && lowerUnits.length > 1073741824 || upperUnits !== null && upperUnits.length > 1073741824)
|
|
2160
|
+
return -1;
|
|
2161
|
+
const inputs = [...Object.values(current), ...Object.values(values), ranks, ordered, dirty, live];
|
|
2162
|
+
if (lowerUnits !== null) inputs.push(lowerUnits);
|
|
2163
|
+
if (upperUnits !== null) inputs.push(upperUnits);
|
|
2164
|
+
if (inputs.some((input) => regionsOverlap(input, output))) return -1;
|
|
2165
|
+
const count = current.kinds.length;
|
|
2166
|
+
const words = Math.max(1, Math.ceil(count / 32));
|
|
2167
|
+
if (live.length < words || dirty.length < words) return -1;
|
|
2168
|
+
for (let index = 0; index < values.kinds.length; index++) {
|
|
2169
|
+
if (!validSortValue(values, index) || values.kinds[index] === 0) return -1;
|
|
2170
|
+
}
|
|
2171
|
+
if (output.length < words) return -3;
|
|
2172
|
+
const lower = boundText(lowerUnits);
|
|
2173
|
+
const upper = boundText(upperUnits);
|
|
2174
|
+
const position = (bound, afterEqual) => {
|
|
2175
|
+
let low = 0;
|
|
2176
|
+
let high = values.kinds.length;
|
|
2177
|
+
while (low < high) {
|
|
2178
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
2179
|
+
const comparison = compareComparableValues(sortValueAt(values, middle), bound, "asc");
|
|
2180
|
+
if (comparison < 0 || comparison === 0 && afterEqual) low = middle + 1;
|
|
2181
|
+
else high = middle;
|
|
2182
|
+
}
|
|
2183
|
+
return low;
|
|
2184
|
+
};
|
|
2185
|
+
const seek = (valueIndex) => {
|
|
2186
|
+
let low = 0;
|
|
2187
|
+
let high = ordered.length;
|
|
2188
|
+
const rank = valueIndex * 2 + 2;
|
|
2189
|
+
while (low < high) {
|
|
2190
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
2191
|
+
const ordinal = ordered[middle];
|
|
2192
|
+
if (ordinal < ranks.length && ranks[ordinal] >= 0 && ranks[ordinal] < rank) low = middle + 1;
|
|
2193
|
+
else high = middle;
|
|
2194
|
+
}
|
|
2195
|
+
return low;
|
|
2196
|
+
};
|
|
2197
|
+
const start = lower === null ? 0 : seek(position(lower, !lowerInclusive));
|
|
2198
|
+
const end = upper === null ? ordered.length : seek(position(upper, upperInclusive));
|
|
2199
|
+
output.fill(0, 0, words);
|
|
2200
|
+
for (let at = start; at < end; at++) {
|
|
2201
|
+
const ordinal = ordered[at];
|
|
2202
|
+
const word = ordinal >>> 5;
|
|
2203
|
+
const bit = 1 << (ordinal & 31);
|
|
2204
|
+
if (ordinal < count && ordinal < ranks.length && ranks[ordinal] >= 2 && (ranks[ordinal] & 1) === 0 && ranks[ordinal] / 2 <= values.kinds.length && (dirty[word] & bit) === 0 && (live[word] & bit) !== 0)
|
|
2205
|
+
output[word] |= bit;
|
|
2206
|
+
}
|
|
2207
|
+
for (let word = 0; word < words; word++) {
|
|
2208
|
+
let pending = dirty[word] & live[word];
|
|
2209
|
+
while (pending !== 0) {
|
|
2210
|
+
const bit = 31 - Math.clz32(pending & -pending);
|
|
2211
|
+
pending &= pending - 1;
|
|
2212
|
+
const ordinal = word * 32 + bit;
|
|
2213
|
+
if (ordinal >= count || !validSortValue(current, ordinal) || current.kinds[ordinal] === 0) continue;
|
|
2214
|
+
const value = sortValueAt(current, ordinal);
|
|
2215
|
+
const lowerOrder = lower === null ? 1 : compareComparableValues(value, lower, "asc");
|
|
2216
|
+
const upperOrder = upper === null ? -1 : compareComparableValues(value, upper, "asc");
|
|
2217
|
+
if ((lowerOrder > 0 || lowerOrder === 0 && lowerInclusive) && (upperOrder < 0 || upperOrder === 0 && upperInclusive))
|
|
2218
|
+
output[word] |= 1 << bit;
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
return 0;
|
|
2222
|
+
};
|
|
2223
|
+
|
|
2224
|
+
// src/core/sort-values/column.ts
|
|
2225
|
+
function captureColumn2(column) {
|
|
2226
|
+
if (column === null || typeof column !== "object") return null;
|
|
2227
|
+
return {
|
|
2228
|
+
current: captureSortValues(column.current),
|
|
2229
|
+
values: captureSortValues(column.values),
|
|
2230
|
+
ranks: column.ranks,
|
|
2231
|
+
ordered: column.ordered,
|
|
2232
|
+
dirty: column.dirty
|
|
2233
|
+
};
|
|
2234
|
+
}
|
|
2235
|
+
function validSortShape(values) {
|
|
2236
|
+
const count = values.kinds.length;
|
|
2237
|
+
return count <= 4294967295 && values.numbers.length === count && values.textStart.length === count && values.textLength.length === count && values.units.length <= 4294967295;
|
|
2238
|
+
}
|
|
2239
|
+
function readColumn(held) {
|
|
2240
|
+
if (held === null) return null;
|
|
2241
|
+
const current = readSortRegions(held.current);
|
|
2242
|
+
const values = readSortRegions(held.values);
|
|
2243
|
+
const ranks = typedRegion(held.ranks, "Int32Array");
|
|
2244
|
+
const ordered = typedRegion(held.ordered, "Uint32Array");
|
|
2245
|
+
const dirty = typedRegion(held.dirty, "Uint32Array");
|
|
2246
|
+
if (current === null || values === null || ranks === null || ordered === null || dirty === null) return null;
|
|
2247
|
+
return { current, values, ranks, ordered, dirty };
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
// src/core/sort-values/select-order.ts
|
|
2251
|
+
function marked(bits, ordinal) {
|
|
2252
|
+
return (bits[ordinal >>> 5] & 1 << (ordinal & 31)) !== 0;
|
|
2253
|
+
}
|
|
2254
|
+
function selectStart(column, descending, anchor) {
|
|
2255
|
+
if (anchor === null) return 0;
|
|
2256
|
+
if (anchor[0] === -1) return column.ordered.length;
|
|
2257
|
+
let low = 0;
|
|
2258
|
+
let high = column.ordered.length;
|
|
2259
|
+
while (low < high) {
|
|
2260
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
2261
|
+
const ordinal = column.ordered[middle];
|
|
2262
|
+
if (ordinal >= column.current.kinds.length || ordinal >= column.ranks.length) return -1;
|
|
2263
|
+
const rank = column.ranks[ordinal];
|
|
2264
|
+
if (rank < 2 || (rank & 1) !== 0 || rank / 2 > column.values.kinds.length) return -1;
|
|
2265
|
+
if (rank < anchor[0] || descending !== 0 && rank === anchor[0]) low = middle + 1;
|
|
2266
|
+
else high = middle;
|
|
2267
|
+
}
|
|
2268
|
+
return descending ? column.ordered.length - low : low;
|
|
2269
|
+
}
|
|
2270
|
+
function columnRank(column, ordinal) {
|
|
2271
|
+
if (!marked(column.dirty, ordinal)) return ordinal < column.ranks.length ? column.ranks[ordinal] : -1;
|
|
2272
|
+
const current = sortValueAt(column.current, ordinal);
|
|
2273
|
+
if (current === null) return -1;
|
|
2274
|
+
let low = 0;
|
|
2275
|
+
let high = column.values.kinds.length;
|
|
2276
|
+
while (low < high) {
|
|
2277
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
2278
|
+
const order = compareComparableValues(sortValueAt(column.values, middle), current, "asc");
|
|
2279
|
+
if (order === 0) return (middle + 1) * 2;
|
|
2280
|
+
if (order < 0) low = middle + 1;
|
|
2281
|
+
else high = middle;
|
|
2282
|
+
}
|
|
2283
|
+
return low * 2 + 1;
|
|
2284
|
+
}
|
|
2285
|
+
function compareRanks(left, right, descending) {
|
|
2286
|
+
if (left === right) return 0;
|
|
2287
|
+
if (left === -1) return 1;
|
|
2288
|
+
if (right === -1) return -1;
|
|
2289
|
+
const order = left < right ? -1 : 1;
|
|
2290
|
+
return descending ? -order : order;
|
|
2291
|
+
}
|
|
2292
|
+
function selectOrder(columns, directions) {
|
|
2293
|
+
function compare2(left, right) {
|
|
2294
|
+
for (let field = 0; field < columns.length; field++) {
|
|
2295
|
+
const column = columns[field];
|
|
2296
|
+
const rank = columnRank(column, left);
|
|
2297
|
+
const order = compareRanks(rank, columnRank(column, right), directions[field]);
|
|
2298
|
+
if (order !== 0) return order;
|
|
2299
|
+
if (rank !== -1 && (rank & 1) !== 0) {
|
|
2300
|
+
const valueOrder = compareComparableValues(
|
|
2301
|
+
sortValueAt(column.current, left),
|
|
2302
|
+
sortValueAt(column.current, right),
|
|
2303
|
+
directions[field] ? "desc" : "asc"
|
|
2304
|
+
);
|
|
2305
|
+
if (valueOrder !== 0) return valueOrder;
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
return 0;
|
|
2309
|
+
}
|
|
2310
|
+
function compareAnchor(ordinal, ranks, values) {
|
|
2311
|
+
if (ranks === null || values === null) return 0;
|
|
2312
|
+
for (let field = 0; field < columns.length; field++) {
|
|
2313
|
+
const column = columns[field];
|
|
2314
|
+
const rank = columnRank(column, ordinal);
|
|
2315
|
+
const order = compareRanks(rank, ranks[field], directions[field]);
|
|
2316
|
+
if (order !== 0) return order;
|
|
2317
|
+
if (rank !== -1 && (rank & 1) !== 0) {
|
|
2318
|
+
const valueOrder = compareComparableValues(
|
|
2319
|
+
sortValueAt(column.current, ordinal),
|
|
2320
|
+
sortValueAt(values, field),
|
|
2321
|
+
directions[field] ? "desc" : "asc"
|
|
2322
|
+
);
|
|
2323
|
+
if (valueOrder !== 0) return valueOrder;
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
return 0;
|
|
2327
|
+
}
|
|
2328
|
+
return { compare: compare2, compareAnchor };
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2331
|
+
// src/core/sort-values/select.ts
|
|
2332
|
+
function validOrdinal(columns, ordinal) {
|
|
2333
|
+
for (const column of columns) {
|
|
2334
|
+
if (ordinal >= column.current.kinds.length || !validSortValue(column.current, ordinal)) return false;
|
|
2335
|
+
if (marked(column.dirty, ordinal) || ordinal >= column.ranks.length) continue;
|
|
2336
|
+
const rank = column.ranks[ordinal];
|
|
2337
|
+
if (rank !== -1 && (rank < 2 || (rank & 1) !== 0 || rank / 2 > column.values.kinds.length)) return false;
|
|
2338
|
+
}
|
|
2339
|
+
return true;
|
|
2340
|
+
}
|
|
2341
|
+
var sortSelect = (columnsInput, directionsInput, filterInput, liveInput, limit, anchorRanksInput, anchorValuesInput, outputInput) => {
|
|
2342
|
+
if (!Array.isArray(columnsInput) || columnsInput.length === 0 || !Number.isInteger(limit) || limit < 0 || limit > 4294967295)
|
|
2343
|
+
return -1;
|
|
2344
|
+
const columnCount = columnsInput.length;
|
|
2345
|
+
const held = [];
|
|
2346
|
+
for (let field = 0; field < columnCount; field++) held.push(captureColumn2(columnsInput[field]));
|
|
2347
|
+
const anchorHeld = anchorValuesInput === null ? null : captureSortValues(anchorValuesInput);
|
|
2348
|
+
const columns = [];
|
|
2349
|
+
for (const entry of held) {
|
|
2350
|
+
const column = readColumn(entry);
|
|
2351
|
+
if (column === null) return -1;
|
|
2352
|
+
columns.push(column);
|
|
2353
|
+
}
|
|
2354
|
+
const anchor = readSortRegions(anchorHeld);
|
|
2355
|
+
const anchorRanks = anchorRanksInput === null ? null : typedRegion(anchorRanksInput, "Int32Array");
|
|
2356
|
+
const directions = typedRegion(directionsInput, "Uint8Array");
|
|
2357
|
+
const filter = typedRegion(filterInput, "Uint32Array");
|
|
2358
|
+
const live = typedRegion(liveInput, "Uint32Array");
|
|
2359
|
+
const output = typedRegion(outputInput, "Uint32Array");
|
|
2360
|
+
if (directions === null || filter === null || live === null || output === null || directions.length !== columns.length || anchor === null && anchorValuesInput !== null || anchorRanks === null && anchorRanksInput !== null || anchor === null !== (anchorRanks === null))
|
|
2361
|
+
return -1;
|
|
2362
|
+
const capacity = columns[0].current.kinds.length;
|
|
2363
|
+
const words = Math.max(1, Math.ceil(capacity / 32));
|
|
2364
|
+
if (filter.length < words || live.length < words || [filter, live, directions].some((input) => regionsOverlap(input, output)))
|
|
2365
|
+
return -1;
|
|
2366
|
+
if (anchor !== null && anchorRanks !== null) {
|
|
2367
|
+
if (!validSortShape(anchor) || anchor.kinds.length !== columns.length || anchorRanks.length !== columns.length || [...Object.values(anchor), anchorRanks].some((input) => regionsOverlap(input, output)))
|
|
2368
|
+
return -1;
|
|
2369
|
+
}
|
|
2370
|
+
for (let field = 0; field < columns.length; field++) {
|
|
2371
|
+
const column = columns[field];
|
|
2372
|
+
if (!validSortShape(column.current) || !validSortShape(column.values) || column.current.kinds.length !== capacity || column.values.kinds.length > 1073741823 || column.ranks.length > capacity || column.ordered.length > capacity || column.dirty.length < words || directions[field] > 1)
|
|
2373
|
+
return -1;
|
|
2374
|
+
const regions = [
|
|
2375
|
+
...Object.values(column.current),
|
|
2376
|
+
...Object.values(column.values),
|
|
2377
|
+
column.ranks,
|
|
2378
|
+
column.ordered,
|
|
2379
|
+
column.dirty
|
|
2380
|
+
];
|
|
2381
|
+
if (regions.some((input) => regionsOverlap(input, output))) return -1;
|
|
2382
|
+
for (let value = 0; value < column.values.kinds.length; value++) {
|
|
2383
|
+
if (!validSortValue(column.values, value) || column.values.kinds[value] === 0) return -1;
|
|
2384
|
+
}
|
|
2385
|
+
if (anchor !== null && anchorRanks !== null) {
|
|
2386
|
+
const rank = anchorRanks[field];
|
|
2387
|
+
if (!validSortValue(anchor, field) || rank < -1 || rank === 0 || rank > column.values.kinds.length * 2 + 1)
|
|
2388
|
+
return -1;
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
if (limit === 0 || capacity === 0) return 0;
|
|
2392
|
+
const { compare: compare2, compareAnchor } = selectOrder(columns, directions);
|
|
2393
|
+
const selected = [];
|
|
2394
|
+
const leading = columns[0];
|
|
2395
|
+
const append = (ordinal) => {
|
|
2396
|
+
if (!marked(live, ordinal) || !marked(filter, ordinal)) return true;
|
|
2397
|
+
if (!validOrdinal(columns, ordinal)) return false;
|
|
2398
|
+
if (compareAnchor(ordinal, anchorRanks, anchor) >= 0) {
|
|
2399
|
+
if (selected.length >= capacity) return false;
|
|
2400
|
+
selected.push(ordinal);
|
|
2401
|
+
}
|
|
2402
|
+
return true;
|
|
2403
|
+
};
|
|
2404
|
+
for (let word = 0; word < words; word++) {
|
|
2405
|
+
let pending = leading.dirty[word] & live[word] & filter[word];
|
|
2406
|
+
while (pending !== 0) {
|
|
2407
|
+
const ordinal = word * 32 + (31 - Math.clz32(pending & -pending));
|
|
2408
|
+
pending &= pending - 1;
|
|
2409
|
+
if (ordinal < capacity && !append(ordinal)) return -1;
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
let clean = 0;
|
|
2413
|
+
let lastRank = -1;
|
|
2414
|
+
const start = selectStart(leading, directions[0], anchorRanks);
|
|
2415
|
+
if (start < 0) return -1;
|
|
2416
|
+
for (let at = start; at < leading.ordered.length; at++) {
|
|
2417
|
+
const ordinal = leading.ordered[directions[0] ? leading.ordered.length - at - 1 : at];
|
|
2418
|
+
if (ordinal >= capacity || ordinal >= leading.ranks.length) return -1;
|
|
2419
|
+
if (marked(leading.dirty, ordinal) || !marked(live, ordinal) || !marked(filter, ordinal)) continue;
|
|
2420
|
+
const rank = leading.ranks[ordinal];
|
|
2421
|
+
if (rank < 2 || (rank & 1) !== 0 || rank / 2 > leading.values.kinds.length) return -1;
|
|
2422
|
+
if (clean >= limit && rank !== lastRank) break;
|
|
2423
|
+
const before = selected.length;
|
|
2424
|
+
if (!append(ordinal)) return -1;
|
|
2425
|
+
if (selected.length !== before) {
|
|
2426
|
+
clean++;
|
|
2427
|
+
lastRank = rank;
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
if (clean < limit) {
|
|
2431
|
+
for (let word = 0; word < words; word++) {
|
|
2432
|
+
let pending = live[word] & filter[word] & ~leading.dirty[word];
|
|
2433
|
+
while (pending !== 0) {
|
|
2434
|
+
const ordinal = word * 32 + (31 - Math.clz32(pending & -pending));
|
|
2435
|
+
pending &= pending - 1;
|
|
2436
|
+
if (ordinal >= capacity || ordinal < leading.ranks.length && leading.ranks[ordinal] !== -1) continue;
|
|
2437
|
+
if (!append(ordinal)) return -1;
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
selected.sort(compare2);
|
|
2442
|
+
let kept = Math.min(limit, selected.length);
|
|
2443
|
+
while (kept < selected.length && compare2(selected[kept - 1], selected[kept]) === 0) kept++;
|
|
2444
|
+
if (kept > output.length) return -3;
|
|
2445
|
+
for (let at = 0; at < kept; at++) output[at] = selected[at];
|
|
2446
|
+
return kept;
|
|
2447
|
+
};
|
|
2448
|
+
|
|
2449
|
+
// src/core/sort-values/browser.ts
|
|
2450
|
+
function reduceSortValue(value, mode) {
|
|
2451
|
+
return reduceWith(sortValue, value, mode);
|
|
2452
|
+
}
|
|
2453
|
+
function reduceStoredSortValue(values, ordinal, mode) {
|
|
2454
|
+
return reduceStoredWith(sortValue, values, ordinal, mode);
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
// src/core/partition/composite/sorted-page.ts
|
|
2458
|
+
function compositeSortedPage(subs, layout, request) {
|
|
2459
|
+
const pages = subs.map(
|
|
2460
|
+
(sub, index) => sortedPageOf(sub, {
|
|
2461
|
+
...request,
|
|
2462
|
+
matches: request.matches === null ? null : subMatchesView(request.matches, layout, index)
|
|
2463
|
+
})
|
|
2464
|
+
);
|
|
2465
|
+
const merged = pages.flat();
|
|
2466
|
+
merged.sort((a, b) => compareComparableKeys(a.key, b.key, request.directions) || compareCodePoints(a.id, b.id));
|
|
2467
|
+
if (merged.length > request.limit) merged.length = request.limit;
|
|
2468
|
+
return merged;
|
|
2469
|
+
}
|
|
2470
|
+
|
|
2471
|
+
// src/core/partition/sort-columns/constants.ts
|
|
2472
|
+
var SORT_VALUES_INITIAL_CAPACITY = 64;
|
|
2473
|
+
var SORT_TEXT_INITIAL_CAPACITY = 256;
|
|
2474
|
+
|
|
2475
|
+
// src/core/partition/sort-columns/values.ts
|
|
2476
|
+
function allocateSortValues(capacity, units3) {
|
|
2477
|
+
return {
|
|
2478
|
+
kinds: new Uint8Array(capacity),
|
|
2479
|
+
numbers: new Float64Array(capacity),
|
|
2480
|
+
textStart: new Uint32Array(capacity),
|
|
2481
|
+
textLength: new Uint32Array(capacity),
|
|
2482
|
+
units: new Uint16Array(units3)
|
|
2483
|
+
};
|
|
2484
|
+
}
|
|
2485
|
+
function createValueStore() {
|
|
2486
|
+
let held = allocateSortValues(0, 0);
|
|
2487
|
+
let usedUnits = 0;
|
|
2488
|
+
let liveUnits = 0;
|
|
2489
|
+
function grow(capacity) {
|
|
2490
|
+
if (capacity <= held.kinds.length) return;
|
|
2491
|
+
const next = allocateSortValues(Math.max(capacity, held.kinds.length * 2, SORT_VALUES_INITIAL_CAPACITY), 0);
|
|
2492
|
+
next.kinds.set(held.kinds);
|
|
2493
|
+
next.numbers.set(held.numbers);
|
|
2494
|
+
next.textStart.set(held.textStart);
|
|
2495
|
+
next.textLength.set(held.textLength);
|
|
2496
|
+
next.units = held.units;
|
|
2497
|
+
held = next;
|
|
2498
|
+
}
|
|
2499
|
+
function compact() {
|
|
2500
|
+
const units3 = new Uint16Array(Math.max(liveUnits, SORT_TEXT_INITIAL_CAPACITY));
|
|
2501
|
+
let at = 0;
|
|
2502
|
+
for (let ordinal = 0; ordinal < held.kinds.length; ordinal++) {
|
|
2503
|
+
if (held.kinds[ordinal] !== 2) continue;
|
|
2504
|
+
const start = held.textStart[ordinal];
|
|
2505
|
+
const length = held.textLength[ordinal];
|
|
2506
|
+
units3.set(held.units.subarray(start, start + length), at);
|
|
2507
|
+
held.textStart[ordinal] = at;
|
|
2508
|
+
at += length;
|
|
2509
|
+
}
|
|
2510
|
+
held.units = units3;
|
|
2511
|
+
usedUnits = at;
|
|
2512
|
+
}
|
|
2513
|
+
function reserveText(length) {
|
|
2514
|
+
if (usedUnits - liveUnits > liveUnits && usedUnits > SORT_TEXT_INITIAL_CAPACITY) compact();
|
|
2515
|
+
const needed = usedUnits + length;
|
|
2516
|
+
if (needed > 4294967295) {
|
|
2517
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The sort strings exceed the native value layout");
|
|
2518
|
+
}
|
|
2519
|
+
if (needed <= held.units.length) return;
|
|
2520
|
+
const units3 = new Uint16Array(
|
|
2521
|
+
Math.max(needed, Math.min(4294967295, held.units.length * 2), SORT_TEXT_INITIAL_CAPACITY)
|
|
2522
|
+
);
|
|
2523
|
+
units3.set(held.units.subarray(0, usedUnits));
|
|
2524
|
+
held.units = units3;
|
|
2525
|
+
}
|
|
2526
|
+
return {
|
|
2527
|
+
set(internalId, value) {
|
|
2528
|
+
grow(internalId + 1);
|
|
2529
|
+
const isText = held.kinds[internalId] === 2;
|
|
2530
|
+
const oldLength = isText ? held.textLength[internalId] : 0;
|
|
2531
|
+
if (typeof value === "string") {
|
|
2532
|
+
if (!isText || value.length > oldLength) reserveText(value.length);
|
|
2533
|
+
const start = isText && value.length <= oldLength ? held.textStart[internalId] : usedUnits;
|
|
2534
|
+
if (!isText || value.length > oldLength) usedUnits += value.length;
|
|
2535
|
+
for (let index = 0; index < value.length; index++) held.units[start + index] = value.charCodeAt(index);
|
|
2536
|
+
held.textStart[internalId] = start;
|
|
2537
|
+
held.textLength[internalId] = value.length;
|
|
2538
|
+
held.kinds[internalId] = 2;
|
|
2539
|
+
liveUnits += value.length - oldLength;
|
|
2540
|
+
return;
|
|
2541
|
+
}
|
|
2542
|
+
liveUnits -= oldLength;
|
|
2543
|
+
held.textStart[internalId] = 0;
|
|
2544
|
+
held.textLength[internalId] = 0;
|
|
2545
|
+
held.kinds[internalId] = value === null ? 0 : typeof value === "number" ? 1 : value ? 4 : 3;
|
|
2546
|
+
held.numbers[internalId] = typeof value === "number" ? value : 0;
|
|
2547
|
+
},
|
|
2548
|
+
clear(internalId) {
|
|
2549
|
+
if (internalId >= held.kinds.length) return;
|
|
2550
|
+
if (held.kinds[internalId] === 2) liveUnits -= held.textLength[internalId];
|
|
2551
|
+
held.kinds[internalId] = 0;
|
|
2552
|
+
held.textStart[internalId] = 0;
|
|
2553
|
+
held.textLength[internalId] = 0;
|
|
2554
|
+
},
|
|
2555
|
+
get(internalId) {
|
|
2556
|
+
return internalId >= held.kinds.length ? null : sortValueAt(held, internalId);
|
|
2557
|
+
},
|
|
2558
|
+
regions(capacity) {
|
|
2559
|
+
grow(capacity);
|
|
2560
|
+
return {
|
|
2561
|
+
kinds: held.kinds.subarray(0, capacity),
|
|
2562
|
+
numbers: held.numbers.subarray(0, capacity),
|
|
2563
|
+
textStart: held.textStart.subarray(0, capacity),
|
|
2564
|
+
textLength: held.textLength.subarray(0, capacity),
|
|
2565
|
+
units: held.units.subarray(0, usedUnits)
|
|
2566
|
+
};
|
|
2567
|
+
},
|
|
2568
|
+
estimateBytes() {
|
|
2569
|
+
return Object.values(held).reduce((bytes, region) => bytes + region.byteLength, 0);
|
|
2570
|
+
}
|
|
2571
|
+
};
|
|
2572
|
+
}
|
|
2573
|
+
|
|
2574
|
+
// src/core/partition/sort-columns/order.ts
|
|
2575
|
+
var MISSING_RANK = -1;
|
|
2576
|
+
var BYTES_PER_RANK = 4;
|
|
2577
|
+
var BYTES_PER_ORDERED_ENTRY = 4;
|
|
2578
|
+
function compareAscending(a, b) {
|
|
2579
|
+
return compareComparableValues(a, b, "asc");
|
|
2580
|
+
}
|
|
2581
|
+
function buildOrder(store, liveIds, capacity) {
|
|
2582
|
+
const current = store.regions(capacity);
|
|
2583
|
+
const live = createBitSet(capacity);
|
|
2584
|
+
const missing = [];
|
|
2585
|
+
let present = 0;
|
|
2586
|
+
let unitCount = 0;
|
|
2587
|
+
for (const internalId of liveIds) {
|
|
2588
|
+
bitsetSet(live, internalId);
|
|
2589
|
+
const kind = current.kinds[internalId];
|
|
2590
|
+
if (kind === 0) {
|
|
2591
|
+
missing.push(internalId);
|
|
2592
|
+
continue;
|
|
2593
|
+
}
|
|
2594
|
+
present++;
|
|
2595
|
+
if (kind === 2) unitCount += current.textLength[internalId];
|
|
2596
|
+
}
|
|
2597
|
+
const dictionary = allocateSortValues(present, unitCount);
|
|
2598
|
+
const ranks = new Int32Array(capacity);
|
|
2599
|
+
const ordered = new Uint32Array(present);
|
|
2600
|
+
const dirty = createBitSet(capacity);
|
|
2601
|
+
const counts2 = new Uint32Array(2);
|
|
2602
|
+
const status = buildSortOrder(current, live, dictionary, ranks, ordered, dirty, counts2);
|
|
2603
|
+
if (status !== 0) {
|
|
2604
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The sort order could not be built", { status: -status });
|
|
2605
|
+
}
|
|
2606
|
+
const values = Array.from({ length: counts2[0] }, (_, index) => sortValueAt(dictionary, index));
|
|
2607
|
+
return {
|
|
2608
|
+
values,
|
|
2609
|
+
dictionary: {
|
|
2610
|
+
kinds: dictionary.kinds.subarray(0, counts2[0]),
|
|
2611
|
+
numbers: dictionary.numbers.subarray(0, counts2[0]),
|
|
2612
|
+
textStart: dictionary.textStart.subarray(0, counts2[0]),
|
|
2613
|
+
textLength: dictionary.textLength.subarray(0, counts2[0]),
|
|
2614
|
+
units: dictionary.units
|
|
2615
|
+
},
|
|
2616
|
+
ranks,
|
|
2617
|
+
ordered,
|
|
2618
|
+
missing: Int32Array.from(missing)
|
|
2619
|
+
};
|
|
2620
|
+
}
|
|
2621
|
+
function rankOfValueIndex(valueIndex) {
|
|
2622
|
+
return valueIndex * 2 + 2;
|
|
2623
|
+
}
|
|
2624
|
+
function rankOfValue(order, value) {
|
|
2625
|
+
if (value === null) return MISSING_RANK;
|
|
2626
|
+
const values = order.values;
|
|
2627
|
+
let low = 0;
|
|
2628
|
+
let high = values.length;
|
|
2629
|
+
while (low < high) {
|
|
2630
|
+
const mid = low + high >>> 1;
|
|
2631
|
+
const comparison = compareAscending(values[mid], value);
|
|
2632
|
+
if (comparison === 0) return rankOfValueIndex(mid);
|
|
2633
|
+
if (comparison < 0) low = mid + 1;
|
|
2634
|
+
else high = mid;
|
|
2635
|
+
}
|
|
2636
|
+
return low * 2 + 1;
|
|
2637
|
+
}
|
|
2638
|
+
function seekPosition(order, rank, direction) {
|
|
2639
|
+
const ordered = order.ordered;
|
|
2640
|
+
const ranks = order.ranks;
|
|
2641
|
+
let low = 0;
|
|
2642
|
+
let high = ordered.length;
|
|
2643
|
+
if (direction === "asc") {
|
|
2644
|
+
while (low < high) {
|
|
2645
|
+
const mid = low + high >>> 1;
|
|
2646
|
+
if (ranks[ordered[mid]] < rank) low = mid + 1;
|
|
2647
|
+
else high = mid;
|
|
2648
|
+
}
|
|
2649
|
+
return low;
|
|
2650
|
+
}
|
|
2651
|
+
while (low < high) {
|
|
2652
|
+
const mid = low + high >>> 1;
|
|
2653
|
+
if (ranks[ordered[mid]] <= rank) low = mid + 1;
|
|
2654
|
+
else high = mid;
|
|
2655
|
+
}
|
|
2656
|
+
return low - 1;
|
|
2657
|
+
}
|
|
2658
|
+
function estimateOrderBytes(order) {
|
|
2659
|
+
const dictionaryBytes = Object.values(order.dictionary).reduce((bytes, region) => bytes + region.buffer.byteLength, 0);
|
|
2660
|
+
return dictionaryBytes + order.ranks.length * BYTES_PER_RANK + (order.ordered.length + order.missing.length) * BYTES_PER_ORDERED_ENTRY;
|
|
2661
|
+
}
|
|
2662
|
+
|
|
2663
|
+
// src/core/partition/sort-columns/index.ts
|
|
2664
|
+
function mayHoldList(fieldType) {
|
|
2665
|
+
const parsed = parsedTypeOf(fieldType);
|
|
2666
|
+
return parsed === void 0 || isListBase(parsed.base);
|
|
2667
|
+
}
|
|
2668
|
+
function columnKeyOf(field, fieldType, mode) {
|
|
2669
|
+
return mayHoldList(fieldType) ? `${mode}:${field}` : `value:${field}`;
|
|
2670
|
+
}
|
|
2671
|
+
function liveInternalIds(docStore) {
|
|
2672
|
+
const ids = [];
|
|
2673
|
+
for (const internalId of docStore.allInternalIds()) ids.push(internalId);
|
|
2674
|
+
return ids;
|
|
2675
|
+
}
|
|
2676
|
+
function createSortColumnSet(docStore) {
|
|
2677
|
+
const columns = /* @__PURE__ */ new Map();
|
|
2678
|
+
let live = createBitSet(docStore.internalIdCapacity());
|
|
2679
|
+
for (const ordinal of docStore.allInternalIds()) bitsetSet(live, ordinal);
|
|
2680
|
+
function rebuildThreshold() {
|
|
2681
|
+
const count = docStore.count();
|
|
2682
|
+
const fraction = count >> SORT_COLUMN_REBUILD_FRACTION_SHIFT;
|
|
2683
|
+
return fraction > SORT_COLUMN_MINIMUM_REBUILD_THRESHOLD ? fraction : SORT_COLUMN_MINIMUM_REBUILD_THRESHOLD;
|
|
2684
|
+
}
|
|
2685
|
+
function rebuild(entry) {
|
|
2686
|
+
entry.order = buildOrder(entry.store, docStore.allInternalIds(), docStore.internalIdCapacity());
|
|
2687
|
+
entry.dirty.clear();
|
|
2688
|
+
entry.dirtyStream = null;
|
|
2689
|
+
}
|
|
2690
|
+
function backfill(key, field, mode) {
|
|
2691
|
+
const store = createValueStore();
|
|
2692
|
+
const values = storedValuesOf(docStore, field);
|
|
2693
|
+
for (const internalId of docStore.allInternalIds()) {
|
|
2694
|
+
store.set(internalId, reduceStoredSortValue(values, internalId, mode));
|
|
2695
|
+
}
|
|
2696
|
+
const entry = {
|
|
2697
|
+
field,
|
|
2698
|
+
mode,
|
|
2699
|
+
store,
|
|
2700
|
+
order: buildOrder(store, liveInternalIds(docStore), docStore.internalIdCapacity()),
|
|
2701
|
+
dirty: /* @__PURE__ */ new Set(),
|
|
2702
|
+
dirtyStream: null
|
|
2703
|
+
};
|
|
2704
|
+
columns.set(key, entry);
|
|
2705
|
+
return entry;
|
|
2706
|
+
}
|
|
2707
|
+
function buildDirtyStream(entry) {
|
|
2708
|
+
const present = [];
|
|
2709
|
+
const missing = [];
|
|
2710
|
+
const rankById = /* @__PURE__ */ new Map();
|
|
2711
|
+
for (const internalId of entry.dirty) {
|
|
2712
|
+
if (docStore.getExternalId(internalId) === void 0) continue;
|
|
2713
|
+
const value = entry.store.get(internalId);
|
|
2714
|
+
if (value === null) {
|
|
2715
|
+
missing.push(internalId);
|
|
2716
|
+
continue;
|
|
2717
|
+
}
|
|
2718
|
+
present.push(internalId);
|
|
2719
|
+
rankById.set(internalId, rankOfValue(entry.order, value));
|
|
2720
|
+
}
|
|
2721
|
+
present.sort((a, b) => {
|
|
2722
|
+
const rankDifference = (rankById.get(a) ?? MISSING_RANK) - (rankById.get(b) ?? MISSING_RANK);
|
|
2723
|
+
if (rankDifference !== 0) return rankDifference;
|
|
2724
|
+
return compareComparableValues(entry.store.get(a), entry.store.get(b), "asc");
|
|
2725
|
+
});
|
|
2726
|
+
return { present: Int32Array.from(present), missing: Int32Array.from(missing) };
|
|
2727
|
+
}
|
|
2728
|
+
function viewOf(entry) {
|
|
2729
|
+
return {
|
|
2730
|
+
get field() {
|
|
2731
|
+
return entry.field;
|
|
2732
|
+
},
|
|
2733
|
+
get order() {
|
|
2734
|
+
return entry.order;
|
|
2735
|
+
},
|
|
2736
|
+
valueOf(internalId) {
|
|
2737
|
+
return entry.store.get(internalId);
|
|
2738
|
+
},
|
|
2739
|
+
rankOf(internalId) {
|
|
2740
|
+
if (entry.dirty.has(internalId)) return rankOfValue(entry.order, entry.store.get(internalId));
|
|
2741
|
+
if (internalId >= entry.order.ranks.length) return MISSING_RANK;
|
|
2742
|
+
return entry.order.ranks[internalId];
|
|
2743
|
+
},
|
|
2744
|
+
isDirty(internalId) {
|
|
2745
|
+
return entry.dirty.has(internalId);
|
|
2746
|
+
},
|
|
2747
|
+
dirtyStream() {
|
|
2748
|
+
if (entry.dirtyStream === null) entry.dirtyStream = buildDirtyStream(entry);
|
|
2749
|
+
return entry.dirtyStream;
|
|
2750
|
+
},
|
|
2751
|
+
seek(rank, direction) {
|
|
2752
|
+
return seekPosition(entry.order, rank, direction);
|
|
2753
|
+
},
|
|
2754
|
+
regions() {
|
|
2755
|
+
const capacity = docStore.internalIdCapacity();
|
|
2756
|
+
return {
|
|
2757
|
+
current: entry.store.regions(capacity),
|
|
2758
|
+
values: entry.order.dictionary,
|
|
2759
|
+
ranks: entry.order.ranks,
|
|
2760
|
+
ordered: entry.order.ordered,
|
|
2761
|
+
dirty: bitsetFromSet(entry.dirty, capacity)
|
|
2762
|
+
};
|
|
2763
|
+
}
|
|
2764
|
+
};
|
|
2765
|
+
}
|
|
2766
|
+
return {
|
|
2767
|
+
holds(field, fieldType, mode) {
|
|
2768
|
+
return columns.has(columnKeyOf(field, fieldType, mode));
|
|
2769
|
+
},
|
|
2770
|
+
column(field, fieldType, mode) {
|
|
2771
|
+
const key = columnKeyOf(field, fieldType, mode);
|
|
2772
|
+
let entry = columns.get(key);
|
|
2773
|
+
if (entry === void 0) {
|
|
2774
|
+
entry = backfill(key, field, mode);
|
|
2775
|
+
} else if (entry.dirty.size > rebuildThreshold()) {
|
|
2776
|
+
rebuild(entry);
|
|
2777
|
+
}
|
|
2778
|
+
return viewOf(entry);
|
|
2779
|
+
},
|
|
2780
|
+
record(internalId) {
|
|
2781
|
+
if (internalId >>> 5 >= live.length) {
|
|
2782
|
+
const grown = createBitSet(docStore.internalIdCapacity());
|
|
2783
|
+
grown.set(live);
|
|
2784
|
+
live = grown;
|
|
2785
|
+
}
|
|
2786
|
+
bitsetSet(live, internalId);
|
|
2787
|
+
for (const entry of columns.values()) {
|
|
2788
|
+
entry.store.set(
|
|
2789
|
+
internalId,
|
|
2790
|
+
reduceStoredSortValue(storedValuesOf(docStore, entry.field), internalId, entry.mode)
|
|
2791
|
+
);
|
|
2792
|
+
entry.dirty.add(internalId);
|
|
2793
|
+
entry.dirtyStream = null;
|
|
2794
|
+
}
|
|
2795
|
+
},
|
|
2796
|
+
forget(internalId) {
|
|
2797
|
+
bitsetClear(live, internalId);
|
|
2798
|
+
for (const entry of columns.values()) {
|
|
2799
|
+
entry.store.clear(internalId);
|
|
2800
|
+
entry.dirty.delete(internalId);
|
|
2801
|
+
entry.dirtyStream = null;
|
|
2802
|
+
}
|
|
2803
|
+
},
|
|
2804
|
+
refresh() {
|
|
2805
|
+
const threshold = rebuildThreshold();
|
|
2806
|
+
for (const entry of columns.values()) {
|
|
2807
|
+
if (entry.dirty.size > threshold) rebuild(entry);
|
|
2808
|
+
}
|
|
2809
|
+
},
|
|
2810
|
+
fieldCount() {
|
|
2811
|
+
return columns.size;
|
|
2812
|
+
},
|
|
2813
|
+
estimateBytes() {
|
|
2814
|
+
let bytes = live.byteLength;
|
|
2815
|
+
for (const entry of columns.values()) {
|
|
2816
|
+
bytes += entry.store.estimateBytes() + estimateOrderBytes(entry.order);
|
|
2817
|
+
}
|
|
2818
|
+
return bytes;
|
|
2819
|
+
},
|
|
2820
|
+
liveBitset() {
|
|
2821
|
+
return live;
|
|
2822
|
+
}
|
|
2823
|
+
};
|
|
2824
|
+
}
|
|
2825
|
+
|
|
2826
|
+
// src/core/partition/sort-columns/range.ts
|
|
2827
|
+
function unitsOf(bound) {
|
|
2828
|
+
if (bound === null) return null;
|
|
2829
|
+
const text = truncateSortString(bound);
|
|
2830
|
+
const units3 = new Uint16Array(text.length);
|
|
2831
|
+
for (let index = 0; index < text.length; index++) units3[index] = text.charCodeAt(index);
|
|
2832
|
+
return units3;
|
|
2833
|
+
}
|
|
2834
|
+
function selectTextRange(column, bounds, capacity, live) {
|
|
2835
|
+
const output = createBitSet(capacity);
|
|
2836
|
+
const status = textRange(
|
|
2837
|
+
column.regions(),
|
|
2838
|
+
unitsOf(bounds.lower),
|
|
2839
|
+
bounds.lowerInclusive,
|
|
2840
|
+
unitsOf(bounds.upper),
|
|
2841
|
+
bounds.upperInclusive,
|
|
2842
|
+
live,
|
|
2843
|
+
output
|
|
2844
|
+
);
|
|
2845
|
+
if (status !== 0)
|
|
2846
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The text range could not be selected", { status: -status });
|
|
2847
|
+
return output;
|
|
2848
|
+
}
|
|
2849
|
+
|
|
2850
|
+
// src/core/partition/sort-columns/select.ts
|
|
2851
|
+
var emptyMatches2 = new Uint32Array(1);
|
|
2852
|
+
function selectSortedPage(state, request) {
|
|
2853
|
+
const { fields, directions, fieldTypes, modes: modes2, limit, anchorKey, anchorId, matches } = request;
|
|
2854
|
+
if (limit <= 0 || fields.length === 0 || state.sortColumns === null) return [];
|
|
2855
|
+
const set = state.sortColumns;
|
|
2856
|
+
const columns = fields.map(
|
|
2857
|
+
(field, index) => set.column(field, fieldTypes[index], modes2[index] ?? defaultSortMode(directions[index] ?? "asc"))
|
|
2858
|
+
);
|
|
2859
|
+
const regions = columns.map((column) => column.regions());
|
|
2860
|
+
const orders = Uint8Array.from(directions, (direction) => direction === "desc" ? 1 : 0);
|
|
2861
|
+
const live = set.liveBitset();
|
|
2862
|
+
const matched = matches === null ? live : matches.ordinalBitset();
|
|
2863
|
+
const filter = matched.length === 0 ? emptyMatches2 : matched;
|
|
2864
|
+
const anchorStore = anchorKey === null || anchorId === null ? null : createValueStore();
|
|
2865
|
+
const anchorRanks = anchorStore === null ? null : new Int32Array(columns.length);
|
|
2866
|
+
if (anchorStore !== null && anchorRanks !== null && anchorKey !== null) {
|
|
2867
|
+
for (let field = 0; field < columns.length; field++) {
|
|
2868
|
+
const value = anchorKey[field] ?? null;
|
|
2869
|
+
anchorStore.set(field, value);
|
|
2870
|
+
anchorRanks[field] = rankOfValue(columns[field].order, value);
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
2873
|
+
const anchor = anchorStore?.regions(columns.length) ?? null;
|
|
2874
|
+
const capacity = state.docStore.count();
|
|
2875
|
+
const output = new Uint32Array(capacity);
|
|
2876
|
+
const resolver = state.docStore.resolver();
|
|
2877
|
+
let requested = Math.min(limit, capacity);
|
|
2878
|
+
for (; ; ) {
|
|
2879
|
+
const count = sortSelect(regions, orders, filter, live, requested, anchorRanks, anchor, output);
|
|
2880
|
+
if (count < 0) {
|
|
2881
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The sorted page could not be selected", { status: -count });
|
|
2882
|
+
}
|
|
2883
|
+
const page = [];
|
|
2884
|
+
for (let at = 0; at < count; at++) {
|
|
2885
|
+
const ordinal = output[at];
|
|
2886
|
+
const id = resolver.toExternal(ordinal);
|
|
2887
|
+
if (id === void 0) continue;
|
|
2888
|
+
const key = columns.map((column) => column.valueOf(ordinal));
|
|
2889
|
+
if (anchorKey !== null && anchorId !== null) {
|
|
2890
|
+
const order = compareComparableKeys(key, anchorKey, directions);
|
|
2891
|
+
if (order < 0 || order === 0 && compareCodePoints(id, anchorId) <= 0) continue;
|
|
2892
|
+
}
|
|
2893
|
+
page.push({ id, key });
|
|
2894
|
+
}
|
|
2895
|
+
page.sort(
|
|
2896
|
+
(left, right) => compareComparableKeys(left.key, right.key, directions) || compareCodePoints(left.id, right.id)
|
|
2897
|
+
);
|
|
2898
|
+
if (page.length >= limit || count < requested || requested === capacity) return page.slice(0, limit);
|
|
2899
|
+
requested = Math.min(capacity, Math.max(requested * 2, requested + limit - page.length));
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
|
|
2903
|
+
// src/engine/query/sorted.ts
|
|
2904
|
+
function sortsWithoutScores(params) {
|
|
2905
|
+
return params.includeScores !== true && params.includeScoreComponents !== true && params.minScore === void 0 && params.group === void 0 && params.pinned === void 0 && (params.termMatch === void 0 || params.termMatch === "any");
|
|
2906
|
+
}
|
|
2907
|
+
async function executeSortedQueryPage(params, context, limit, offset, sortSignature, options) {
|
|
2908
|
+
const { manager, language, config } = context;
|
|
2909
|
+
const schema = config.schema;
|
|
2910
|
+
let anchorKey = null;
|
|
2911
|
+
let anchorId = null;
|
|
2912
|
+
if (params.searchAfter !== void 0) {
|
|
2913
|
+
const decoded = decodePageCursor(params.searchAfter);
|
|
2914
|
+
requireMatchingCursor(decoded, params.searchAfter, sortSignature, true, context.cursorBinding());
|
|
2915
|
+
anchorKey = decoded.sortKey;
|
|
2916
|
+
anchorId = decoded.anchor;
|
|
2917
|
+
}
|
|
2918
|
+
const normalized = normalizeSort(params.sort);
|
|
2919
|
+
const fields = normalized.map((entry) => entry.field);
|
|
2920
|
+
const directions = normalized.map((entry) => entry.direction);
|
|
2921
|
+
const flatSchema = flattenSchema(schema);
|
|
2922
|
+
const fieldTypes = fields.map((field) => flatSchema[field]);
|
|
2923
|
+
const modes2 = sortModesOf(normalized);
|
|
2924
|
+
const partitions = partitionsIn(manager, context.partitionIds);
|
|
2925
|
+
const partitionLimit = offset + limit + 1;
|
|
2926
|
+
const merged = [];
|
|
2927
|
+
const partitionFacets = [];
|
|
2928
|
+
const partitionFacetConfig = params.facets !== void 0 && partitions.length > 1 ? everyValueFacetConfig(params.facets) : params.facets;
|
|
2929
|
+
let count = 0;
|
|
2930
|
+
const clock = context.patternWork.clock;
|
|
2931
|
+
const afterYield = () => context.turns?.afterYield();
|
|
2932
|
+
for (const partition of partitions) {
|
|
2933
|
+
if (turnIsSpent()) await yieldTurn().then(afterYield);
|
|
2934
|
+
partition.prepareSortedPage({ fields, directions, fieldTypes, modes: modes2 });
|
|
2935
|
+
if (partitionFacetConfig !== void 0) {
|
|
2936
|
+
if (turnIsSpent()) await yieldTurn().then(afterYield);
|
|
2937
|
+
partition.prepareFacets(partitionFacetConfig, schema);
|
|
2938
|
+
}
|
|
2939
|
+
if (turnIsSpent()) await yieldTurn().then(afterYield);
|
|
2940
|
+
clock?.check();
|
|
2941
|
+
const matches = fulltextMatches(partition, params, language, schema, options);
|
|
2942
|
+
clock?.check();
|
|
2943
|
+
if (partitionFacetConfig !== void 0) {
|
|
2944
|
+
partitionFacets.push(
|
|
2945
|
+
partition.computeFacets(
|
|
2946
|
+
matches === null ? /* @__PURE__ */ new Set() : { ordinalBitset: matches.ordinalBitset() },
|
|
2947
|
+
partitionFacetConfig,
|
|
2948
|
+
schema
|
|
2949
|
+
)
|
|
2950
|
+
);
|
|
2951
|
+
}
|
|
2952
|
+
if (matches === null || matches.count === 0) continue;
|
|
2953
|
+
count += matches.count;
|
|
2954
|
+
clock?.check();
|
|
2955
|
+
const page2 = partition.sortedPage({
|
|
2956
|
+
fields,
|
|
2957
|
+
directions,
|
|
2958
|
+
fieldTypes,
|
|
2959
|
+
modes: modes2,
|
|
2960
|
+
limit: partitionLimit,
|
|
2961
|
+
anchorKey,
|
|
2962
|
+
anchorId,
|
|
2963
|
+
matches
|
|
2964
|
+
});
|
|
2965
|
+
for (const entry of page2) merged.push(entry);
|
|
2966
|
+
}
|
|
2967
|
+
clock?.check();
|
|
2968
|
+
merged.sort((a, b) => compareComparableKeys(a.key, b.key, directions) || compareCodePoints(a.id, b.id));
|
|
2969
|
+
const hasMore = merged.length > offset + limit;
|
|
2970
|
+
const page = merged.slice(offset, offset + limit);
|
|
2971
|
+
const last = page[page.length - 1];
|
|
2972
|
+
let cursor;
|
|
2973
|
+
if (hasMore && last !== void 0) {
|
|
2974
|
+
cursor = encodePageCursor({
|
|
2975
|
+
anchor: last.id,
|
|
2976
|
+
score: null,
|
|
2977
|
+
sortKey: last.key,
|
|
2978
|
+
sortSignature,
|
|
2979
|
+
binding: context.cursorBinding()
|
|
2980
|
+
});
|
|
2981
|
+
}
|
|
2982
|
+
return {
|
|
2983
|
+
hits: page.map((entry) => ({ id: entry.id, document: void 0 })),
|
|
2984
|
+
count,
|
|
2985
|
+
cursor,
|
|
2986
|
+
facets: params.facets !== void 0 && partitionFacets.length > 0 ? mergeFacets(partitionFacets, params.facets) : void 0
|
|
2987
|
+
};
|
|
2988
|
+
}
|
|
2989
|
+
|
|
2990
|
+
// src/search/sorting-runtime.ts
|
|
2991
|
+
function requireSortMode(entry, fieldType) {
|
|
2992
|
+
if (entry.mode === void 0) return;
|
|
2993
|
+
if (!isSortMode(entry.mode)) {
|
|
2994
|
+
throw new NarsilError(
|
|
2995
|
+
ErrorCodes.SEARCH_INVALID_MODE,
|
|
2996
|
+
`A sort mode is one of ${SORT_MODES.map((mode) => `"${mode}"`).join(", ")}, and the mode on "${entry.field}" is "${String(entry.mode)}"`,
|
|
2997
|
+
{ field: entry.field, mode: String(entry.mode) }
|
|
2998
|
+
);
|
|
2999
|
+
}
|
|
3000
|
+
const averages = entry.mode === "avg" || entry.mode === "median";
|
|
3001
|
+
if (averages && fieldType !== void 0 && fieldType !== "number" && fieldType !== "number[]") {
|
|
3002
|
+
throw new NarsilError(
|
|
3003
|
+
ErrorCodes.SEARCH_INVALID_FIELD,
|
|
3004
|
+
`The engine applies the "${entry.mode}" mode to a number field alone, and "${entry.field}" is a ${fieldType} field`,
|
|
3005
|
+
{ field: entry.field, fieldType, mode: entry.mode }
|
|
3006
|
+
);
|
|
3007
|
+
}
|
|
3008
|
+
}
|
|
3009
|
+
function requireSortableFields(sort, schema, strict = false) {
|
|
3010
|
+
const fields = normalizeSort(sort);
|
|
3011
|
+
if (fields.length === 0) return;
|
|
3012
|
+
const flatSchema = flattenSchema(schema);
|
|
3013
|
+
for (const entry of fields) {
|
|
3014
|
+
const fieldType = flatSchema[entry.field];
|
|
3015
|
+
if (strict && fieldType === void 0) throw undeclaredFieldOnStrictIndex(entry.field, "sort");
|
|
3016
|
+
if (entry.direction !== "asc" && entry.direction !== "desc") {
|
|
3017
|
+
throw new NarsilError(
|
|
3018
|
+
ErrorCodes.SEARCH_INVALID_MODE,
|
|
3019
|
+
`A sort direction is "asc" or "desc", and the direction on "${entry.field}" is "${String(entry.direction)}"`,
|
|
3020
|
+
{ field: entry.field, direction: String(entry.direction) }
|
|
3021
|
+
);
|
|
3022
|
+
}
|
|
3023
|
+
requireSortMode(entry, fieldType);
|
|
3024
|
+
const parsed = parsedTypeOf(fieldType);
|
|
3025
|
+
if (parsed !== void 0 && isStringValuedBase(parsed.base) && !parsed.sortable) {
|
|
3026
|
+
throw new NarsilError(
|
|
3027
|
+
ErrorCodes.SEARCH_INVALID_FIELD,
|
|
3028
|
+
`The engine sorts by text field "${entry.field}" only where its type includes "sortable", as in "${withSortableOption(parsed)}", because it keeps up to the first ${SORT_VALUE_MAX_CODE_POINTS} code points of every document's value in memory to sort by a text field`,
|
|
3029
|
+
{ field: entry.field, fieldType }
|
|
3030
|
+
);
|
|
3031
|
+
}
|
|
3032
|
+
if (isGeopointOrVectorType(fieldType)) {
|
|
3033
|
+
throw new NarsilError(
|
|
3034
|
+
ErrorCodes.SEARCH_INVALID_FIELD,
|
|
3035
|
+
`The engine sorts by a number, boolean, enum, or sortable text field, and "${entry.field}" is a ${fieldType} field`,
|
|
3036
|
+
{ field: entry.field, fieldType }
|
|
3037
|
+
);
|
|
3038
|
+
}
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
function applySorting(hits, sort, sortKeyOf) {
|
|
3042
|
+
const normalized = normalizeSort(sort);
|
|
3043
|
+
if (normalized.length === 0) return hits;
|
|
3044
|
+
const directions = normalized.map((entry) => entry.direction);
|
|
3045
|
+
const keyCache = /* @__PURE__ */ new Map();
|
|
3046
|
+
for (const hit of hits) keyCache.set(hit.id, sortKeyOf(hit.id));
|
|
3047
|
+
return hits.slice().sort(
|
|
3048
|
+
(left, right) => compareComparableKeys(keyCache.get(left.id) ?? [], keyCache.get(right.id) ?? [], directions) || compareCodePoints(left.id, right.id)
|
|
3049
|
+
);
|
|
3050
|
+
}
|
|
3051
|
+
|
|
3052
|
+
// src/engine/list-documents/index.ts
|
|
3053
|
+
function clampListLimit(limit) {
|
|
3054
|
+
const clamped = Math.max(1, clampLimit(limit));
|
|
3055
|
+
requireWithinResultWindow(clamped, 0);
|
|
3056
|
+
return clamped;
|
|
3057
|
+
}
|
|
3058
|
+
function firstIdAfter(sorted, anchor) {
|
|
3059
|
+
let low = 0;
|
|
3060
|
+
let high = sorted.length;
|
|
3061
|
+
while (low < high) {
|
|
3062
|
+
const mid = low + high >>> 1;
|
|
3063
|
+
if (compareCodePoints(sorted[mid], anchor) <= 0) low = mid + 1;
|
|
3064
|
+
else high = mid;
|
|
3065
|
+
}
|
|
3066
|
+
return low;
|
|
3067
|
+
}
|
|
3068
|
+
function collectFilterMatches(partitions, filters, schema, patternWorkCap) {
|
|
3069
|
+
const meter = createPatternWorkMeter(patternWorkCap);
|
|
3070
|
+
const perPartition = partitions.map((partition) => partition.filterMatches(filters, schema, meter));
|
|
3071
|
+
let total = 0;
|
|
3072
|
+
for (const matches of perPartition) total += matches.count;
|
|
3073
|
+
return { matchesFor: (partitionIndex) => perPartition[partitionIndex] ?? null, total };
|
|
3074
|
+
}
|
|
3075
|
+
function collectCandidates(partition, anchor, matches, wanted, out) {
|
|
3076
|
+
const sorted = partition.sortedDocIds();
|
|
3077
|
+
let index = anchor === null ? 0 : firstIdAfter(sorted, anchor);
|
|
3078
|
+
let taken = 0;
|
|
3079
|
+
while (index < sorted.length && taken < wanted) {
|
|
3080
|
+
const docId = sorted[index];
|
|
3081
|
+
index++;
|
|
3082
|
+
if (matches !== null && !matches.hasExternal(docId)) continue;
|
|
3083
|
+
out.push(docId);
|
|
3084
|
+
taken++;
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
function pageInIdOrder(partitions, cursor, filtered, limit, binding) {
|
|
3088
|
+
const anchor = cursor === null ? null : cursor.anchor;
|
|
3089
|
+
const candidates = [];
|
|
3090
|
+
for (let index = 0; index < partitions.length; index++) {
|
|
3091
|
+
collectCandidates(
|
|
3092
|
+
partitions[index],
|
|
3093
|
+
anchor,
|
|
3094
|
+
filtered === null ? null : filtered.matchesFor(index),
|
|
3095
|
+
limit + 1,
|
|
3096
|
+
candidates
|
|
3097
|
+
);
|
|
3098
|
+
}
|
|
3099
|
+
candidates.sort(compareCodePoints);
|
|
3100
|
+
const hasMore = candidates.length > limit;
|
|
3101
|
+
const ids = hasMore ? candidates.slice(0, limit) : candidates;
|
|
3102
|
+
const nextCursor = hasMore && ids.length > 0 ? { anchor: ids[ids.length - 1], score: null, sortKey: null, sortSignature: null, binding } : null;
|
|
3103
|
+
return { ids, nextCursor };
|
|
3104
|
+
}
|
|
3105
|
+
function pageInSortOrder(partitions, cursor, filtered, limit, sort, signature, schema, binding) {
|
|
3106
|
+
const normalized = normalizeSort(sort);
|
|
3107
|
+
const fields = normalized.map((entry) => entry.field);
|
|
3108
|
+
const directions = normalized.map((entry) => entry.direction);
|
|
3109
|
+
const flatSchema = flattenSchema(schema);
|
|
3110
|
+
const fieldTypes = fields.map((field) => flatSchema[field]);
|
|
3111
|
+
const request = {
|
|
3112
|
+
fields,
|
|
3113
|
+
directions,
|
|
3114
|
+
fieldTypes,
|
|
3115
|
+
modes: sortModesOf(normalized),
|
|
3116
|
+
limit: limit + 1,
|
|
3117
|
+
anchorKey: cursor === null ? null : cursor.sortKey,
|
|
3118
|
+
anchorId: cursor === null ? null : cursor.anchor
|
|
3119
|
+
};
|
|
3120
|
+
const merged = [];
|
|
3121
|
+
for (let index = 0; index < partitions.length; index++) {
|
|
3122
|
+
const matches = filtered === null ? null : filtered.matchesFor(index);
|
|
3123
|
+
for (const entry of partitions[index].sortedPage({ ...request, matches })) merged.push(entry);
|
|
3124
|
+
}
|
|
3125
|
+
merged.sort((a, b) => compareComparableKeys(a.key, b.key, directions) || compareCodePoints(a.id, b.id));
|
|
3126
|
+
const hasMore = merged.length > limit;
|
|
3127
|
+
const page = hasMore ? merged.slice(0, limit) : merged;
|
|
3128
|
+
const last = page[page.length - 1];
|
|
3129
|
+
const nextCursor = hasMore && last !== void 0 ? { anchor: last.id, score: null, sortKey: last.key, sortSignature: signature, binding } : null;
|
|
3130
|
+
return { ids: page.map((entry) => entry.id), nextCursor };
|
|
3131
|
+
}
|
|
3132
|
+
function countAcrossPartitions(manager, partitionIds, partitions) {
|
|
3133
|
+
if (partitionIds === void 0) {
|
|
3134
|
+
return manager.countDocuments();
|
|
3135
|
+
}
|
|
3136
|
+
let total = 0;
|
|
3137
|
+
for (const partition of partitions) {
|
|
3138
|
+
total += partition.count();
|
|
3139
|
+
}
|
|
3140
|
+
return total;
|
|
3141
|
+
}
|
|
3142
|
+
function executeListDocuments(params, context) {
|
|
3143
|
+
const { manager, schema } = context;
|
|
3144
|
+
const startTime = now();
|
|
3145
|
+
const limit = clampListLimit(params.limit);
|
|
3146
|
+
const signature = sortSignatureOf(params.sort);
|
|
3147
|
+
requireSortableFields(params.sort, schema, context.strict === true);
|
|
3148
|
+
const filters = validQueryFilter(params.filters, schema);
|
|
3149
|
+
const projection = resolveProjection(params.document);
|
|
3150
|
+
const binding = listBindingOf(params.filters);
|
|
3151
|
+
let cursor = null;
|
|
3152
|
+
if (params.cursor !== void 0) {
|
|
3153
|
+
cursor = decodePageCursor(params.cursor);
|
|
3154
|
+
requireMatchingCursor(cursor, params.cursor, signature, false, binding);
|
|
3155
|
+
}
|
|
3156
|
+
const partitions = context.partitionIds === void 0 ? manager.getAllPartitions() : context.partitionIds.map((partitionId) => manager.partitionAt(partitionId)).filter((partition) => partition !== void 0);
|
|
3157
|
+
const filtered = filters === void 0 ? null : collectFilterMatches(partitions, filters, schema, context.patternWorkCap);
|
|
3158
|
+
const total = filtered === null ? countAcrossPartitions(manager, context.partitionIds, partitions) : filtered.total;
|
|
3159
|
+
const page = params.sort === void 0 || signature === null ? pageInIdOrder(partitions, cursor, filtered, limit, binding) : pageInSortOrder(partitions, cursor, filtered, limit, params.sort, signature, schema, binding);
|
|
3160
|
+
const documents = [];
|
|
3161
|
+
for (const docId of page.ids) {
|
|
3162
|
+
if (projection.kind === "none") {
|
|
3163
|
+
documents.push({ id: docId, document: {} });
|
|
3164
|
+
continue;
|
|
3165
|
+
}
|
|
3166
|
+
const stored = manager.get(docId, projection);
|
|
3167
|
+
if (stored === void 0) continue;
|
|
3168
|
+
documents.push({ id: docId, document: stored });
|
|
3169
|
+
}
|
|
3170
|
+
if (page.nextCursor === null) {
|
|
3171
|
+
for (const partition of partitions) partition.releaseSortedDocIds();
|
|
3172
|
+
}
|
|
3173
|
+
return {
|
|
3174
|
+
documents,
|
|
3175
|
+
cursor: page.nextCursor === null ? null : encodePageCursor(page.nextCursor),
|
|
3176
|
+
total,
|
|
3177
|
+
elapsed: now() - startTime
|
|
3178
|
+
};
|
|
3179
|
+
}
|
|
3180
|
+
|
|
3181
|
+
// src/engine/suggest.ts
|
|
3182
|
+
async function executeSuggest(manager, language, params, partitionIds) {
|
|
3183
|
+
const loading = loadSuggestions();
|
|
3184
|
+
if (loading !== null) await loading;
|
|
3185
|
+
const t0 = performance.now();
|
|
3186
|
+
const ceiling = partitionIds === void 0 ? MAX_SUGGEST_LIMIT : MAX_SUGGEST_SCATTER_LIMIT;
|
|
3187
|
+
const limit = Math.max(1, Math.min(clampRowCount(params.limit, DEFAULT_SUGGEST_LIMIT), ceiling));
|
|
3188
|
+
const rawPrefix = params.prefix.trim();
|
|
3189
|
+
if (rawPrefix.length === 0) {
|
|
3190
|
+
return { terms: [], elapsed: performance.now() - t0 };
|
|
3191
|
+
}
|
|
3192
|
+
const unstemmed = tokenize(rawPrefix, language, { stem: false, removeStopWords: false });
|
|
3193
|
+
const lastToken = unstemmed.tokens.length > 0 ? unstemmed.tokens[unstemmed.tokens.length - 1].token : rawPrefix.toLowerCase();
|
|
3194
|
+
if (lastToken.length === 0) {
|
|
3195
|
+
return { terms: [], elapsed: performance.now() - t0 };
|
|
3196
|
+
}
|
|
3197
|
+
const stemmed = language.stemmer ? language.stemmer(lastToken) : lastToken;
|
|
3198
|
+
const merged = /* @__PURE__ */ new Map();
|
|
3199
|
+
const perPartitionLimit = limit * 2;
|
|
3200
|
+
const partitions = partitionIds === void 0 ? manager.getAllPartitions() : partitionIds.map((partitionId) => manager.partitionAt(partitionId)).filter((partition) => partition !== void 0);
|
|
3201
|
+
for (const partition of partitions) {
|
|
3202
|
+
for (const suggestion of partition.suggestTerms(lastToken, stemmed, perPartitionLimit)) {
|
|
3203
|
+
let entry = merged.get(suggestion.token);
|
|
3204
|
+
if (!entry) {
|
|
3205
|
+
entry = { documentFrequency: 0, surfaceOccurrences: /* @__PURE__ */ new Map() };
|
|
3206
|
+
merged.set(suggestion.token, entry);
|
|
3207
|
+
}
|
|
3208
|
+
entry.documentFrequency += suggestion.documentFrequency;
|
|
3209
|
+
for (const s of suggestion.surfaces) {
|
|
3210
|
+
entry.surfaceOccurrences.set(s.surface, (entry.surfaceOccurrences.get(s.surface) ?? 0) + s.occurrences);
|
|
3211
|
+
}
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
const terms = Array.from(merged.values()).map((entry) => ({ term: pickDisplaySurface(entry.surfaceOccurrences), documentFrequency: entry.documentFrequency })).sort((a, b) => b.documentFrequency - a.documentFrequency || compareCodePoints(a.term, b.term));
|
|
3215
|
+
if (terms.length > limit) terms.length = limit;
|
|
3216
|
+
return { terms, elapsed: performance.now() - t0 };
|
|
3217
|
+
}
|
|
3218
|
+
function pickDisplaySurface(surfaceOccurrences) {
|
|
3219
|
+
let best = "";
|
|
3220
|
+
let bestCount = -1;
|
|
3221
|
+
for (const [surface, occurrences] of surfaceOccurrences) {
|
|
3222
|
+
if (occurrences > bestCount || occurrences === bestCount && compareCodePoints(surface, best) < 0) {
|
|
3223
|
+
best = surface;
|
|
3224
|
+
bestCount = occurrences;
|
|
3225
|
+
}
|
|
3226
|
+
}
|
|
3227
|
+
return best;
|
|
3228
|
+
}
|
|
3229
|
+
|
|
3230
|
+
export { PARTIAL_BUDGET, PARTIAL_RUN_POINTS, appendFoldedCodePoint, applyGrouping, applySorting, bindOptionalReadsHost, buildSortOrder, captureExpansionSource, compareComparableKeys, compareSortValues, compositeSortedPage, compositeSuggestTerms, computeFacets, countBooleanFacet, countEnumFacet, countNumberFacet, countRangeFacets, createSortColumnSet, dedupePinnedEntries, executeListDocuments, executeSortedQueryPage, executeSuggest, expandTermPrefix, expansionDictionary, expansionText, expansionToken, foldGroupReducer, groupHits, hitsKeptPerGroup, mergeFacetBuckets, mergeFacets3 as mergeFacets, pinnedOrder, placePinned2 as placePinned, readExpansion, reduceSortValue, reduceStoredSortValue, requireFacetableFields, requireGroupableFields, requireSortableFields, selectSortedPage, selectTextRange, singleFoldTable, sortFacetedNumbers, sortSelect, sortsWithoutScores, startAfterAnchor, suggestDisplayTerms, textRange, validExpansionText };
|