@delali/narsil 0.3.1-next.144 → 0.3.1-next.146
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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/analysis-change-ZCJNYBJM.mjs +39 -0
- package/dist/{browser/analysis-change-entry-7HR4LWL3.mjs → analysis-change-entry-YN3ELL6C.mjs} +1 -1
- package/dist/browser/{analysis-change-JPV775LR.mjs → analysis-change-M65AZQQG.mjs} +8 -5
- package/dist/{analysis-change-entry-TZTG4AZB.mjs → browser/analysis-change-entry-H3YIOLXZ.mjs} +1 -1
- package/dist/browser/{chunk-VFYMPCOV.mjs → chunk-2JABMB4S.mjs} +10 -6
- package/dist/{chunk-46NZOPHS.mjs → browser/chunk-4DYLCICG.mjs} +2 -2
- package/dist/browser/{chunk-MZI3TEOW.mjs → chunk-4RD3AFSB.mjs} +1 -1
- package/dist/browser/{chunk-WCFCHIBB.mjs → chunk-5V74FFZE.mjs} +2 -2
- package/dist/{chunk-NN33CZRE.mjs → browser/chunk-6OWX3OP2.mjs} +2 -2
- package/dist/browser/chunk-BRWY3JCN.mjs +678 -0
- package/dist/browser/{chunk-5DB2VDGI.mjs → chunk-BVX2MEHA.mjs} +306 -607
- package/dist/browser/{chunk-KTOK275N.mjs → chunk-D6UFGBF5.mjs} +15 -45
- package/dist/browser/{chunk-D33A5VMU.mjs → chunk-IAXZAVYL.mjs} +25 -66
- package/dist/browser/chunk-JNOAKDUM.mjs +31 -0
- package/dist/browser/{chunk-LVY2LI35.mjs → chunk-KAEVGLYN.mjs} +2 -2
- package/dist/browser/{chunk-3ZBJYAS4.mjs → chunk-LURXVCHG.mjs} +1 -1
- package/dist/browser/chunk-MPK76GVS.mjs +63 -0
- package/dist/browser/{chunk-ZEV26AQ6.mjs → chunk-NVEJJP4R.mjs} +1 -1
- package/dist/browser/{chunk-HKX2G6WH.mjs → chunk-ODQP7P3H.mjs} +455 -66
- package/dist/browser/{chunk-35B42UFM.mjs → chunk-PIS7ZAP7.mjs} +12 -25
- package/dist/browser/{chunk-TCLEOZ4P.mjs → chunk-QMNZFFD3.mjs} +4 -3
- package/dist/browser/{chunk-R4WUDZQV.mjs → chunk-SRL47USS.mjs} +2 -2
- package/dist/browser/chunk-TY7L6OXO.mjs +69 -0
- package/dist/browser/{chunk-MVM33ZUL.mjs → chunk-W643QV3L.mjs} +6 -5
- package/dist/browser/chunk-WXLBZJMT.mjs +25 -0
- package/dist/browser/{chunk-MWUK3DA5.mjs → chunk-XILIT5MT.mjs} +8 -8
- package/dist/browser/{cold-module-MJMKNUYJ.mjs → cold-module-A5S6JFDN.mjs} +20 -20
- package/dist/browser/{durability-wiring-SNOWIT6Z.mjs → durability-wiring-W5AVV3RF.mjs} +120 -80
- package/dist/browser/gzip-5S7ROJFJ.mjs +1 -0
- package/dist/browser/import-PTT63AGK.mjs +12 -0
- package/dist/browser/{module-BUFNZMEU.mjs → module-CEKA7MQH.mjs} +13 -10
- package/dist/browser/module-X5PGOE47.mjs +4288 -0
- package/dist/browser/{phrase-module-HGVBO24W.mjs → phrase-module-CNKMUY4U.mjs} +2 -1
- package/dist/browser/pool-DTYSPBMQ.mjs +296 -0
- package/dist/{restore-queue-FEYAC4SB.mjs → browser/restore-queue-DD4LWZK7.mjs} +2 -2
- package/dist/browser/{runtime-VEUH674B.mjs → runtime-D2ULZGCN.mjs} +3 -2
- package/dist/browser/{snapshot-QXBKHJAG.mjs → snapshot-DQ6A4PQP.mjs} +20 -15
- package/dist/browser/{write-WHFBDWQS.mjs → write-MAGRAIEH.mjs} +14 -11
- package/dist/{capabilities-BCdUwK_L.d.ts → capabilities-BT-HLDC8.d.ts} +3 -3
- package/dist/{chunk-O4LJQI4G.mjs → chunk-2HJWQPR6.mjs} +126 -4
- package/dist/{chunk-Z4HFMYNX.mjs → chunk-3BVGJ4JR.mjs} +36 -13
- package/dist/{chunk-ZPWS2SKJ.mjs → chunk-3HV7M5OJ.mjs} +272 -112
- package/dist/{chunk-UIKN2OTQ.mjs → chunk-3MXV3VHR.mjs} +2 -23
- package/dist/{browser/chunk-CCOAOO73.mjs → chunk-3SBJVQLL.mjs} +2 -2
- package/dist/chunk-3TVDTACC.mjs +104 -0
- package/dist/{chunk-YH5OIL7B.mjs → chunk-5QMHMELB.mjs} +6 -1
- package/dist/{chunk-SBMAWTRG.mjs → chunk-5WB2A3XU.mjs} +84 -15
- package/dist/{chunk-NPNK57CA.mjs → chunk-65WCV5JO.mjs} +1 -1
- package/dist/chunk-6BWLDLS3.mjs +58 -0
- package/dist/{chunk-UUZCQQG2.mjs → chunk-6C5EX2QR.mjs} +3 -3
- package/dist/{chunk-VEFN6X6Q.mjs → chunk-6F6USYII.mjs} +120 -13
- package/dist/{chunk-AST3SHFA.mjs → chunk-7HKWO4BJ.mjs} +7 -7
- package/dist/{chunk-W2COJ5AH.mjs → chunk-7N223GWV.mjs} +2 -2
- package/dist/{chunk-GWOS5DYC.mjs → chunk-ADBS5XRI.mjs} +11 -13
- package/dist/{chunk-TNBIK2LQ.mjs → chunk-AWNHDBKV.mjs} +430 -75
- package/dist/{chunk-ZMBFWQXN.mjs → chunk-B2FTZNNF.mjs} +2 -2
- package/dist/chunk-B7XIOAMK.mjs +100 -0
- package/dist/{chunk-W55X2BYN.mjs → chunk-BEZH7VEH.mjs} +1 -1
- package/dist/{chunk-QSK35UC2.mjs → chunk-BTPGQC6Z.mjs} +3 -11
- package/dist/{chunk-LA4WNDHL.mjs → chunk-CYYSZPVV.mjs} +12 -14
- package/dist/{chunk-SRJBFG2T.mjs → chunk-DOY6EG76.mjs} +6 -7
- package/dist/{chunk-6SNBQESJ.mjs → chunk-F22IOCR2.mjs} +10 -6
- package/dist/{chunk-J6YVB2XM.mjs → chunk-IJPK5PGM.mjs} +1 -1
- package/dist/{chunk-L76W3IZJ.mjs → chunk-IRWP524D.mjs} +1 -1
- package/dist/{chunk-XS2VM7UE.mjs → chunk-IZT3G24Q.mjs} +2 -2
- package/dist/{chunk-NOT5QOHU.mjs → chunk-J7M73VY7.mjs} +1 -1
- package/dist/chunk-K5HSUPYS.mjs +11 -0
- package/dist/chunk-KAKQ74LS.mjs +53 -0
- package/dist/{chunk-ZSWHPKGM.mjs → chunk-KYVRZAYP.mjs} +69 -2
- package/dist/{chunk-NRUXBYPH.mjs → chunk-KZ5GQE4Z.mjs} +5 -5
- package/dist/{browser/chunk-XNWPP5NS.mjs → chunk-KZQIVGW7.mjs} +2 -2
- package/dist/{chunk-KZL72VTC.mjs → chunk-L7TIAYPX.mjs} +11 -10
- package/dist/{chunk-DD2673TX.mjs → chunk-LDGGR7YT.mjs} +4 -81
- package/dist/{chunk-XZ3SRNDJ.mjs → chunk-LJ4ENXOD.mjs} +77 -16
- package/dist/chunk-LQG5G2LY.mjs +39 -0
- package/dist/{chunk-HNHCNTVG.mjs → chunk-MDF6PDIA.mjs} +11 -0
- package/dist/{chunk-74XB2SXI.mjs → chunk-NVTFQFUL.mjs} +16 -2
- package/dist/{chunk-BOJZGUS6.mjs → chunk-NYSONUB7.mjs} +1 -1
- package/dist/{chunk-WJGO5PGV.mjs → chunk-O4KUNDVF.mjs} +31 -17
- package/dist/{chunk-PWIWSUCX.mjs → chunk-POUYICOU.mjs} +1 -1
- package/dist/{chunk-EBIHRL7B.mjs → chunk-Q2UL2TPC.mjs} +1 -1
- package/dist/{chunk-NJJCPFXN.mjs → chunk-QEYF2SQ6.mjs} +2 -2
- package/dist/{chunk-7AUASRE7.mjs → chunk-QN5WVV5K.mjs} +7 -7
- package/dist/{chunk-RIIUZJQ3.mjs → chunk-QYRNLLLI.mjs} +1 -1
- package/dist/chunk-SOUZMPME.mjs +1635 -0
- package/dist/{chunk-C3YDYX6C.mjs → chunk-T4CJDBBE.mjs} +1 -1
- package/dist/{chunk-YLNSJQLX.mjs → chunk-TTCKT2RT.mjs} +5 -5
- package/dist/{chunk-KEIYVZUX.mjs → chunk-U2726TL5.mjs} +5 -5
- package/dist/{chunk-RKAVHNRW.mjs → chunk-U46IZNCW.mjs} +10 -5
- package/dist/{chunk-BRUUBXCB.mjs → chunk-U6NE6RCV.mjs} +10 -38
- package/dist/{chunk-VJEGOELZ.mjs → chunk-VP6XRMWR.mjs} +4 -4
- package/dist/{chunk-IO5F7ZBE.mjs → chunk-VQDZVWN3.mjs} +874 -413
- package/dist/{chunk-IWVEG5CX.mjs → chunk-XIHUI4KB.mjs} +2 -2
- package/dist/{chunk-YHUTF6EA.mjs → chunk-XKPNKR3V.mjs} +17 -47
- package/dist/{chunk-HJJWY6PT.mjs → chunk-YGQOPB2Z.mjs} +1 -1
- package/dist/chunk-YKEHQUSZ.mjs +248 -0
- package/dist/{chunk-GLZHNSGB.mjs → chunk-ZAYWOHVG.mjs} +1 -1
- package/dist/{chunk-ICW45TTU.mjs → chunk-ZEKDO7YX.mjs} +2 -2
- package/dist/chunk-ZXNNR73R.mjs +31 -0
- package/dist/{chunk-VJS4JFBQ.mjs → chunk-ZXX5GZEX.mjs} +3 -1
- package/dist/{chunk-RHKRTYGO.mjs → chunk-ZYKUD5NX.mjs} +9 -7
- package/dist/{client-B12YFALP.d.ts → client-Cn3gNxUU.d.ts} +62 -5
- package/dist/client.d.ts +8 -8
- package/dist/client.mjs +46 -0
- package/dist/{cold-module-4X7WJCFM.mjs → cold-module-2WWGBFJC.mjs} +43 -49
- package/dist/{config-CIG5yDyg.d.ts → config-CfhH1IWQ.d.ts} +150 -3
- package/dist/{config-BhZxIeb1.d.ts → config-aIsy7mcL.d.ts} +63 -3
- package/dist/direct-executor-QLVHMELJ.mjs +24 -0
- package/dist/distribution/coordinator/etcd.d.ts +3 -3
- package/dist/distribution/coordinator/etcd.mjs +1 -1
- package/dist/distribution/coordinator/in-memory.d.ts +3 -3
- package/dist/distribution/transport/grpc.mjs +4 -4
- package/dist/distribution/transport/in-memory.mjs +2 -2
- package/dist/distribution/transport/tcp.mjs +3 -3
- package/dist/distribution.d.ts +8 -8
- package/dist/distribution.mjs +16116 -14537
- package/dist/{durability-wiring-324D2DDD.mjs → durability-wiring-GNWQSZI3.mjs} +142 -109
- package/dist/embeddings/openai.d.ts +1 -1
- package/dist/embeddings/openai.mjs +2 -2
- package/dist/{engine-CtzbHOmG.d.ts → engine-CJRUHXgf.d.ts} +3 -3
- package/dist/gzip-F4HMIEEQ.mjs +1 -0
- package/dist/import-DX55PN4G.mjs +41 -0
- package/dist/{index-DsEurhSl.d.ts → index-MsF7e5hL.d.ts} +2 -2
- package/dist/index.browser.mjs +7 -4
- package/dist/index.d.ts +542 -8
- package/dist/index.mjs +29 -32
- package/dist/index.react-native.mjs +19 -0
- package/dist/invalidation/broadcast-channel.d.ts +1 -1
- package/dist/invalidation/filesystem.d.ts +1 -1
- package/dist/invalidation/filesystem.mjs +1 -1
- package/dist/invalidation/noop.d.ts +1 -1
- package/dist/{long-query-thread-4SW7RWDE.mjs → long-query-thread-SCUE62TO.mjs} +23 -25
- package/dist/{module-DXSJ5LT2.mjs → module-HW6IXUM6.mjs} +603 -112
- package/dist/module-V5K4ZP23.mjs +45 -0
- package/dist/{paged-partition-archive-HFP45PLU.mjs → paged-partition-archive-FDIIWZNR.mjs} +42 -41
- package/dist/partial-words.mjs +7 -9
- package/dist/pattern.mjs +4 -6
- package/dist/persistence/durability/checkpoint-worker.d.ts +2 -2
- package/dist/persistence/durability/checkpoint-worker.mjs +36 -39
- package/dist/{phrase-module-5QHBD2VJ.mjs → phrase-module-OUQIRUQU.mjs} +1 -1
- package/dist/pool-UPV5PVPY.mjs +298 -0
- package/dist/react-native/chunks/analysis-change-HGW3L6NY.mjs +893 -0
- package/dist/react-native/chunks/analysis-change-entry-BNRU6STO.mjs +1 -0
- package/dist/react-native/chunks/chunk-2J2727V4.mjs +495 -0
- package/dist/react-native/chunks/chunk-3CTWSQRW.mjs +1251 -0
- package/dist/react-native/chunks/chunk-5FV4ZEVA.mjs +2619 -0
- package/dist/react-native/chunks/chunk-7VWQLEFV.mjs +642 -0
- package/dist/react-native/chunks/chunk-AAAETEJK.mjs +27 -0
- package/dist/react-native/chunks/chunk-AM7RSBPF.mjs +32 -0
- package/dist/react-native/chunks/chunk-B6OD63Z3.mjs +997 -0
- package/dist/react-native/chunks/chunk-BRNAVOKP.mjs +177 -0
- package/dist/react-native/chunks/chunk-DKJTZ625.mjs +207 -0
- package/dist/react-native/chunks/chunk-FKGG4UOF.mjs +177 -0
- package/dist/{browser/chunk-6RHTAVZX.mjs → react-native/chunks/chunk-GV5KFQ6M.mjs} +38 -15
- package/dist/react-native/chunks/chunk-HURV3ASY.mjs +4416 -0
- package/dist/react-native/chunks/chunk-IO2SMUQD.mjs +10 -0
- package/dist/react-native/chunks/chunk-JV6VQY2S.mjs +17 -0
- package/dist/react-native/chunks/chunk-KUJOBRKS.mjs +217 -0
- package/dist/react-native/chunks/chunk-KWRW5U65.mjs +226 -0
- package/dist/react-native/chunks/chunk-L4M7QJNP.mjs +110 -0
- package/dist/react-native/chunks/chunk-LQ6Q4L5B.mjs +360 -0
- package/dist/react-native/chunks/chunk-LQAKO5QV.mjs +523 -0
- package/dist/react-native/chunks/chunk-LRGWK367.mjs +11 -0
- package/dist/react-native/chunks/chunk-LVIKKAA6.mjs +93 -0
- package/dist/react-native/chunks/chunk-NFGBOI46.mjs +14 -0
- package/dist/react-native/chunks/chunk-NNE4VDMM.mjs +8 -0
- package/dist/react-native/chunks/chunk-PPEPTUVV.mjs +56 -0
- package/dist/react-native/chunks/chunk-PSWKQVVT.mjs +9 -0
- package/dist/{chunk-KGOXHLLZ.mjs → react-native/chunks/chunk-PU55IOG4.mjs} +228 -11
- package/dist/react-native/chunks/chunk-QC4ZRBSX.mjs +270 -0
- package/dist/react-native/chunks/chunk-R4EEMYM6.mjs +21 -0
- package/dist/react-native/chunks/chunk-R4OPUZQ5.mjs +86 -0
- package/dist/react-native/chunks/chunk-SLGIAD6B.mjs +2634 -0
- package/dist/react-native/chunks/chunk-TJMSKBEM.mjs +25 -0
- package/dist/react-native/chunks/chunk-TLYD7IJ2.mjs +23 -0
- package/dist/react-native/chunks/chunk-UFNCLEQM.mjs +73 -0
- package/dist/react-native/chunks/chunk-YH7W556M.mjs +179 -0
- package/dist/react-native/chunks/chunk-YJSMEKRE.mjs +22385 -0
- package/dist/react-native/chunks/chunk-ZCKNBPQ4.mjs +79 -0
- package/dist/react-native/chunks/cold-module-TSFWLYWC.mjs +755 -0
- package/dist/react-native/chunks/durability-wiring-PZR6KOQK.mjs +4509 -0
- package/dist/react-native/chunks/gzip.react-native-7JWCLVOB.mjs +3 -0
- package/dist/react-native/chunks/import-VTODLPLO.mjs +22 -0
- package/dist/{browser/module-BVP4TKZU.mjs → react-native/chunks/module-JBBH6PZH.mjs} +671 -146
- package/dist/react-native/chunks/module-VEK2KCKU.mjs +25 -0
- package/dist/react-native/chunks/phrase-module-76UAPPSG.mjs +329 -0
- package/dist/react-native/chunks/pool-CD4ANYUI.mjs +296 -0
- package/dist/react-native/chunks/release-D7ZTIANM.mjs +33 -0
- package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
- package/dist/{browser/restore-queue-ONYNYUDE.mjs → react-native/chunks/restore-queue-OI5EH7R3.mjs} +2 -2
- package/dist/react-native/chunks/runtime-3MCDLJAF.mjs +559 -0
- package/dist/react-native/chunks/snapshot-AUZMEV36.mjs +1205 -0
- package/dist/react-native/chunks/write-DDC2QBRJ.mjs +197 -0
- package/dist/{chunk-DQNKVOJ2.mjs → react-native/partial-words.mjs} +13 -228
- package/dist/react-native/pattern.mjs +2442 -0
- package/dist/react-native/synonyms.mjs +460 -0
- package/dist/react.d.ts +11 -11
- package/dist/react.mjs +11 -0
- package/dist/{rerank-KTLC3E6N.mjs → rerank-3W54N6P2.mjs} +4 -4
- package/dist/rerankers/http.d.ts +1 -1
- package/dist/rerankers/http.mjs +2 -2
- package/dist/restore-queue-BQWWNNQI.mjs +41 -0
- package/dist/{runtime-DYV75DCB.mjs → runtime-L2DZYY6N.mjs} +5 -5
- package/dist/{schema-B-Runv0n.d.ts → schema-D33CyXCy.d.ts} +1 -1
- package/dist/server.d.ts +24 -12
- package/dist/server.mjs +159 -38
- package/dist/{snapshot-CGM65VCD.mjs → snapshot-EHXTOTW5.mjs} +47 -50
- package/dist/synonyms.mjs +3 -4
- package/dist/{tasks-CjRJRGTb.d.ts → tasks-C4ElXr4P.d.ts} +59 -7
- package/dist/thread-I7DVHHW7.mjs +31 -0
- package/dist/vector/search-worker.d.ts +2 -2
- package/dist/vector/search-worker.mjs +8 -7
- package/dist/workers/entry.mjs +9 -9
- package/dist/{write-2DZPEQL2.mjs → write-SDJXXQ2G.mjs} +36 -39
- package/openapi.json +349 -2
- package/package.json +108 -8
- package/dist/analysis-change-HQUT2Z3I.mjs +0 -43
- package/dist/browser/import-TLB2A5LJ.mjs +0 -9
- package/dist/chunk-5WYVUPGD.mjs +0 -427
- package/dist/chunk-AV4IJKD2.mjs +0 -35
- package/dist/chunk-GES7U2PM.mjs +0 -68
- package/dist/chunk-N37VMC62.mjs +0 -18
- package/dist/chunk-WNBRW4Q2.mjs +0 -128
- package/dist/chunk-WQBCBKY3.mjs +0 -8
- package/dist/chunk-ZATRDAYU.mjs +0 -160
- package/dist/direct-executor-7XOQ7VQP.mjs +0 -28
- package/dist/import-OOBUW3RR.mjs +0 -44
- package/dist/module-IYPLWHGC.mjs +0 -48
- package/dist/thread-KGNAIKPD.mjs +0 -33
- /package/dist/{chunk-22YPZVMG.mjs → chunk-ZXNQEW43.mjs} +0 -0
|
@@ -0,0 +1,4509 @@
|
|
|
1
|
+
import { writeMetadataEnvelope, decodeSegmentManifest, metadataFromConfig, SINGLE_NODE_PRIMARY_TERM, readMetadataEnvelope, createSeqOwner, readSegmentManifest, reclaimUnlistedKeys, loadSegmentedCheckpoint, checkpointLastSeqNo, nextSegmentKey, isRecord, invalidManifest, deserializePayloadV2, unpackEnvelopeBytes, checkpointPrimaryTerm, frameRecord, isId, isUint32, isSequenceNumber, writeCheckpointSegments, partitionContentsOf, writeLiveVectors, commitCheckpointManifest, removeCheckpointGarbage, readWalSegments, durableRecordMissing, growPartitionsTo, writeSegmentHeader, VECTOR_FILE_ENVELOPE_VERSION, SEGMENT_HEADER_SIZE, documentsOfPayloadV2, readCommitMarker, nextMarkerSlot, encodeMarkerSlot, markerSlotOffset, metadataConfigOf, durableMetadataConfigOf, buildEntry, readCurrentManifest, captureWholePartition } from './chunk-5FV4ZEVA.mjs';
|
|
2
|
+
import { nodeThreadId } from './chunk-PSWKQVVT.mjs';
|
|
3
|
+
import { decodeVectorIndexParts, VECTOR_FILE_MAX_VECTORS, deadBytesFor, countDead, decodeVectorFilePayload, decodeVectorGraphPayload, graphPartsOfEarlierPayload } from './chunk-LQ6Q4L5B.mjs';
|
|
4
|
+
import './chunk-LRGWK367.mjs';
|
|
5
|
+
import { DEFAULT_SEGMENT_MAX_BYTES, DEFAULT_CHECKPOINT_INTERVAL_MS, DEFAULT_CHECKPOINT_MUTATION_THRESHOLD, DEFAULT_COMPACTION_THRESHOLD, DEFAULT_ASYNC_FLUSH_INTERVAL_MS, WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES, directoryCheckpointStore, manifestKey, readListedFile, adapterCheckpointStore, DIRECTORY_LOCK_ATTEMPTS, snapshotBundleKey, manifestReferencedKeys, manifestFileRefs, PHONE_FILE_CHUNK_BYTES, DIRECTORY_LOCK_SETTLE_MS, DIRECTORY_LOCK_PROCESS_START_TOLERANCE_MS, readSegmentFile, MAX_SEGMENTS_PER_PARTITION, DIRECTORY_IDENTITY_CHECK_INTERVAL_MS, WHOLE_PARTITION_LARGEST_DOCUMENT_COUNT, WHOLE_PARTITION_MIN_CHANGED_SHARE, CHECKPOINT_TIMEOUT_RECOVERY_BACKOFF_MS, CHECKPOINT_WORKER_TIMEOUT_MS, CHECKPOINT_WORKER_IDLE_MS, LOG_RECORDS_PER_CHECKPOINT } from './chunk-B6OD63Z3.mjs';
|
|
6
|
+
import { applyDeleteEntry, applyIndexEntry } from './chunk-7VWQLEFV.mjs';
|
|
7
|
+
import { toError, openChecksummedEnvelope, HEADER_SIZE, buildSegmentPayload, resolvePartitionInsertOptions, freezeSegmentShared, createSharedFrozenSegment } from './chunk-YJSMEKRE.mjs';
|
|
8
|
+
import { turnsAreBudgeted, turnIsSpent, yieldTurn, waitOutsideCountedWrite, detectRuntime } from './chunk-LQAKO5QV.mjs';
|
|
9
|
+
import { createRequestId } from './chunk-NNE4VDMM.mjs';
|
|
10
|
+
import { fnv1a } from './chunk-L4M7QJNP.mjs';
|
|
11
|
+
import './chunk-IO2SMUQD.mjs';
|
|
12
|
+
import { MAX_INDEX_NAME_LENGTH, INDEX_NAME_PATTERN } from './chunk-AM7RSBPF.mjs';
|
|
13
|
+
import './chunk-ZCKNBPQ4.mjs';
|
|
14
|
+
import { encodeDocument, requireValidFieldTypes } from './chunk-PU55IOG4.mjs';
|
|
15
|
+
import { MIN_M, MAX_FILE_BYTES } from './chunk-TJMSKBEM.mjs';
|
|
16
|
+
import './chunk-DKJTZ625.mjs';
|
|
17
|
+
import './chunk-3CTWSQRW.mjs';
|
|
18
|
+
import './chunk-2J2727V4.mjs';
|
|
19
|
+
import './chunk-KWRW5U65.mjs';
|
|
20
|
+
import { compareCodePoints } from './chunk-PPEPTUVV.mjs';
|
|
21
|
+
import './chunk-LVIKKAA6.mjs';
|
|
22
|
+
import './chunk-AAAETEJK.mjs';
|
|
23
|
+
import './chunk-JV6VQY2S.mjs';
|
|
24
|
+
import { reactNativeHost } from './chunk-NFGBOI46.mjs';
|
|
25
|
+
import { NarsilError, ErrorCodes } from './chunk-YH7W556M.mjs';
|
|
26
|
+
import { decode } from '@msgpack/msgpack';
|
|
27
|
+
|
|
28
|
+
// src/runtime/posix-path.ts
|
|
29
|
+
function normalizeParts(path, absolute) {
|
|
30
|
+
const parts = [];
|
|
31
|
+
for (const part of path.split("/")) {
|
|
32
|
+
if (part === "" || part === ".") continue;
|
|
33
|
+
if (part === "..") {
|
|
34
|
+
if (parts.length > 0 && parts[parts.length - 1] !== "..") parts.pop();
|
|
35
|
+
else if (!absolute) parts.push("..");
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
parts.push(part);
|
|
39
|
+
}
|
|
40
|
+
return parts;
|
|
41
|
+
}
|
|
42
|
+
function createPosixPath(currentDirectory) {
|
|
43
|
+
function isAbsolute(path) {
|
|
44
|
+
return path.startsWith("/");
|
|
45
|
+
}
|
|
46
|
+
function normalize(path) {
|
|
47
|
+
if (path.length === 0) return ".";
|
|
48
|
+
const absolute = isAbsolute(path);
|
|
49
|
+
const joined = normalizeParts(path, absolute).join("/");
|
|
50
|
+
if (absolute) return `/${joined}`;
|
|
51
|
+
return joined.length === 0 ? "." : joined;
|
|
52
|
+
}
|
|
53
|
+
function resolve(...segments) {
|
|
54
|
+
let resolved = "";
|
|
55
|
+
for (let index = segments.length - 1; index >= 0 && !isAbsolute(resolved); index--) {
|
|
56
|
+
const segment = segments[index];
|
|
57
|
+
if (segment === void 0 || segment.length === 0) continue;
|
|
58
|
+
resolved = resolved.length === 0 ? segment : `${segment}/${resolved}`;
|
|
59
|
+
}
|
|
60
|
+
if (!isAbsolute(resolved)) resolved = `${currentDirectory()}/${resolved}`;
|
|
61
|
+
return `/${normalizeParts(resolved, true).join("/")}`;
|
|
62
|
+
}
|
|
63
|
+
function dirname(path) {
|
|
64
|
+
if (path.length === 0) return ".";
|
|
65
|
+
const trimmed = path.length > 1 ? path.replace(/\/+$/, "") : path;
|
|
66
|
+
const slash = trimmed.lastIndexOf("/");
|
|
67
|
+
if (slash === -1) return ".";
|
|
68
|
+
if (slash === 0) return "/";
|
|
69
|
+
return trimmed.slice(0, slash);
|
|
70
|
+
}
|
|
71
|
+
function basename(path, suffix) {
|
|
72
|
+
const trimmed = path.length > 1 ? path.replace(/\/+$/, "") : path;
|
|
73
|
+
const base = trimmed.slice(trimmed.lastIndexOf("/") + 1);
|
|
74
|
+
if (suffix !== void 0 && suffix !== base && base.endsWith(suffix)) return base.slice(0, -suffix.length);
|
|
75
|
+
return base;
|
|
76
|
+
}
|
|
77
|
+
function relative(from, to) {
|
|
78
|
+
const fromParts = resolve(from).split("/").filter((part) => part.length > 0);
|
|
79
|
+
const toParts = resolve(to).split("/").filter((part) => part.length > 0);
|
|
80
|
+
let shared = 0;
|
|
81
|
+
while (shared < fromParts.length && shared < toParts.length && fromParts[shared] === toParts[shared]) shared++;
|
|
82
|
+
return [...fromParts.slice(shared).map(() => ".."), ...toParts.slice(shared)].join("/");
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
sep: "/",
|
|
86
|
+
isAbsolute,
|
|
87
|
+
normalize,
|
|
88
|
+
join: (...segments) => normalize(segments.filter((segment) => segment.length > 0).join("/")),
|
|
89
|
+
resolve,
|
|
90
|
+
dirname,
|
|
91
|
+
basename,
|
|
92
|
+
relative
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/persistence/durability/phone-fs.ts
|
|
97
|
+
var CURRENT_POSITION = -1;
|
|
98
|
+
function errnoOf(err) {
|
|
99
|
+
return err?.code;
|
|
100
|
+
}
|
|
101
|
+
function statsOf(status, bigint) {
|
|
102
|
+
return {
|
|
103
|
+
size: status.size,
|
|
104
|
+
dev: bigint ? BigInt(status.device) : status.device,
|
|
105
|
+
ino: bigint ? BigInt(status.inode) : status.inode,
|
|
106
|
+
mtimeMs: status.modifiedMs,
|
|
107
|
+
isDirectory: () => status.isDirectory,
|
|
108
|
+
isFile: () => status.isFile
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function bytesOf(data) {
|
|
112
|
+
return typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
113
|
+
}
|
|
114
|
+
function positionOf(position) {
|
|
115
|
+
return position === null || position === void 0 || position < 0 ? CURRENT_POSITION : position;
|
|
116
|
+
}
|
|
117
|
+
function createPhoneFileHandle(files, descriptor) {
|
|
118
|
+
let closed = false;
|
|
119
|
+
let lastCall = Promise.resolve();
|
|
120
|
+
function requireOpen() {
|
|
121
|
+
if (closed) throw Object.assign(new Error("The file handle is closed"), { code: "EBADF" });
|
|
122
|
+
return descriptor;
|
|
123
|
+
}
|
|
124
|
+
function tracked(call) {
|
|
125
|
+
lastCall = call.catch(() => {
|
|
126
|
+
});
|
|
127
|
+
return call;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
fd: descriptor,
|
|
131
|
+
async read(buffer, offset = 0, length = buffer.byteLength - offset, position) {
|
|
132
|
+
const target = buffer.subarray(offset, offset + length);
|
|
133
|
+
const bytesRead = await tracked(files.readAsync(requireOpen(), target, positionOf(position)));
|
|
134
|
+
return { bytesRead, buffer };
|
|
135
|
+
},
|
|
136
|
+
async write(buffer, offset = 0, length = buffer.byteLength - offset, position) {
|
|
137
|
+
const source = buffer.subarray(offset, offset + length);
|
|
138
|
+
const bytesWritten = await tracked(files.writeAsync(requireOpen(), source, positionOf(position)));
|
|
139
|
+
return { bytesWritten, buffer };
|
|
140
|
+
},
|
|
141
|
+
async sync() {
|
|
142
|
+
await tracked(files.syncAsync(requireOpen()));
|
|
143
|
+
},
|
|
144
|
+
async truncate(byteLength = 0) {
|
|
145
|
+
const open = requireOpen();
|
|
146
|
+
await lastCall;
|
|
147
|
+
files.truncate(open, byteLength);
|
|
148
|
+
},
|
|
149
|
+
async stat() {
|
|
150
|
+
const open = requireOpen();
|
|
151
|
+
await lastCall;
|
|
152
|
+
return statsOf(files.descriptorStatus(open), false);
|
|
153
|
+
},
|
|
154
|
+
async close() {
|
|
155
|
+
if (closed) return;
|
|
156
|
+
closed = true;
|
|
157
|
+
files.close(descriptor);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
async function readWhole(files, path) {
|
|
162
|
+
const descriptor = files.open(path, "r");
|
|
163
|
+
try {
|
|
164
|
+
const size = files.descriptorStatus(descriptor).size;
|
|
165
|
+
const bytes = new Uint8Array(size);
|
|
166
|
+
let filled = 0;
|
|
167
|
+
while (filled < size) {
|
|
168
|
+
const end = Math.min(size, filled + PHONE_FILE_CHUNK_BYTES);
|
|
169
|
+
const chunk = await files.readAsync(descriptor, bytes.subarray(filled, end), filled);
|
|
170
|
+
if (chunk <= 0) break;
|
|
171
|
+
filled += chunk;
|
|
172
|
+
}
|
|
173
|
+
return filled === size ? bytes : bytes.slice(0, filled);
|
|
174
|
+
} finally {
|
|
175
|
+
files.close(descriptor);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async function writeWhole(files, path, data, flag) {
|
|
179
|
+
const descriptor = files.open(path, flag);
|
|
180
|
+
try {
|
|
181
|
+
let written = 0;
|
|
182
|
+
while (written < data.byteLength) {
|
|
183
|
+
const end = Math.min(data.byteLength, written + PHONE_FILE_CHUNK_BYTES);
|
|
184
|
+
const chunk = await files.writeAsync(descriptor, data.subarray(written, end), CURRENT_POSITION);
|
|
185
|
+
if (chunk <= 0) throw Object.assign(new Error(`Writing ${path} made no progress`), { code: "EIO" });
|
|
186
|
+
written += chunk;
|
|
187
|
+
}
|
|
188
|
+
} finally {
|
|
189
|
+
files.close(descriptor);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function removeTree(files, path) {
|
|
193
|
+
const status = files.pathStatus(path);
|
|
194
|
+
if (!status.isDirectory) {
|
|
195
|
+
files.unlink(path);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
for (const entry of files.readDirectory(path)) removeTree(files, `${path}/${entry.name}`);
|
|
199
|
+
files.removeDirectory(path);
|
|
200
|
+
}
|
|
201
|
+
function makeDirectories(files, path, dirname) {
|
|
202
|
+
try {
|
|
203
|
+
files.makeDirectory(path);
|
|
204
|
+
return;
|
|
205
|
+
} catch (err) {
|
|
206
|
+
const code = errnoOf(err);
|
|
207
|
+
if (code === "EEXIST") {
|
|
208
|
+
if (files.pathStatus(path).isDirectory) return;
|
|
209
|
+
throw err;
|
|
210
|
+
}
|
|
211
|
+
if (code !== "ENOENT") throw err;
|
|
212
|
+
}
|
|
213
|
+
const parent = dirname(path);
|
|
214
|
+
if (parent === path) return;
|
|
215
|
+
makeDirectories(files, parent, dirname);
|
|
216
|
+
try {
|
|
217
|
+
files.makeDirectory(path);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
if (errnoOf(err) !== "EEXIST" || !files.pathStatus(path).isDirectory) throw err;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function createPhoneFs(files, dirname) {
|
|
223
|
+
return {
|
|
224
|
+
async open(path, flags = "r") {
|
|
225
|
+
return createPhoneFileHandle(files, files.open(path, flags));
|
|
226
|
+
},
|
|
227
|
+
async mkdir(path, options) {
|
|
228
|
+
if (options?.recursive === true) makeDirectories(files, path, dirname);
|
|
229
|
+
else files.makeDirectory(path);
|
|
230
|
+
},
|
|
231
|
+
async readdir(path, options) {
|
|
232
|
+
const entries = files.readDirectory(path);
|
|
233
|
+
if (options?.withFileTypes !== true) return entries.map((entry) => entry.name);
|
|
234
|
+
return entries.map(
|
|
235
|
+
(entry) => ({
|
|
236
|
+
name: entry.name,
|
|
237
|
+
isDirectory: () => entry.isDirectory,
|
|
238
|
+
isFile: () => entry.isFile
|
|
239
|
+
})
|
|
240
|
+
);
|
|
241
|
+
},
|
|
242
|
+
async rename(from, to) {
|
|
243
|
+
files.rename(from, to);
|
|
244
|
+
},
|
|
245
|
+
async rm(path, options) {
|
|
246
|
+
try {
|
|
247
|
+
if (options?.recursive === true) removeTree(files, path);
|
|
248
|
+
else files.unlink(path);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
if (options?.force === true && errnoOf(err) === "ENOENT") return;
|
|
251
|
+
throw err;
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
async stat(path, options) {
|
|
255
|
+
return statsOf(files.pathStatus(path), options?.bigint === true);
|
|
256
|
+
},
|
|
257
|
+
async unlink(path) {
|
|
258
|
+
files.unlink(path);
|
|
259
|
+
},
|
|
260
|
+
async link(existingPath, newPath) {
|
|
261
|
+
files.link(existingPath, newPath);
|
|
262
|
+
},
|
|
263
|
+
async readFile(path, encoding) {
|
|
264
|
+
const bytes = await readWhole(files, path);
|
|
265
|
+
const named = typeof encoding === "string" ? encoding : encoding?.encoding;
|
|
266
|
+
return named === void 0 ? bytes : new TextDecoder().decode(bytes);
|
|
267
|
+
},
|
|
268
|
+
async writeFile(path, data, options) {
|
|
269
|
+
await writeWhole(files, path, bytesOf(data), options?.flag ?? "w");
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/persistence/durability/node-fs.react-native.ts
|
|
275
|
+
var cachedFs = null;
|
|
276
|
+
var cachedPath = null;
|
|
277
|
+
async function getPath() {
|
|
278
|
+
if (cachedPath === null) {
|
|
279
|
+
const host = reactNativeHost();
|
|
280
|
+
cachedPath = createPosixPath(() => host.appStorageDirectory());
|
|
281
|
+
}
|
|
282
|
+
return cachedPath;
|
|
283
|
+
}
|
|
284
|
+
async function getFs() {
|
|
285
|
+
if (cachedFs === null) {
|
|
286
|
+
const path = await getPath();
|
|
287
|
+
cachedFs = createPhoneFs(reactNativeHost().files, path.dirname);
|
|
288
|
+
}
|
|
289
|
+
return cachedFs;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// src/persistence/durability/lock-holder.react-native.ts
|
|
293
|
+
function errnoOf2(err) {
|
|
294
|
+
return err?.code;
|
|
295
|
+
}
|
|
296
|
+
async function holderIsLive(holder, self, _lockPath) {
|
|
297
|
+
if (holder.pid === self.pid) {
|
|
298
|
+
if (holder.processStartMs === null || self.processStartMs === null) return false;
|
|
299
|
+
return Math.abs(holder.processStartMs - self.processStartMs) <= DIRECTORY_LOCK_PROCESS_START_TOLERANCE_MS;
|
|
300
|
+
}
|
|
301
|
+
return reactNativeHost().processIsAlive(holder.pid);
|
|
302
|
+
}
|
|
303
|
+
function holdDirectoryOpen(_directory) {
|
|
304
|
+
return () => void 0;
|
|
305
|
+
}
|
|
306
|
+
async function removeStaleLock(lockPath, asidePath, isStale) {
|
|
307
|
+
const fs = await getFs();
|
|
308
|
+
try {
|
|
309
|
+
await fs.rename(lockPath, asidePath);
|
|
310
|
+
} catch (err) {
|
|
311
|
+
if (errnoOf2(err) === "ENOENT") return;
|
|
312
|
+
throw err;
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
const moved = await fs.readFile(asidePath, "utf8");
|
|
316
|
+
if (!isStale(moved)) {
|
|
317
|
+
await fs.writeFile(lockPath, moved, { flag: "wx" }).catch((err) => {
|
|
318
|
+
if (errnoOf2(err) !== "EEXIST") throw err;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
} finally {
|
|
322
|
+
await fs.unlink(asidePath).catch(() => void 0);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/persistence/durability/directory-lock.ts
|
|
327
|
+
var LOCK_FILE_NAME = ".narsil.lock";
|
|
328
|
+
var HARD_LINK_UNSUPPORTED_CODES = /* @__PURE__ */ new Set(["EACCES", "EPERM", "ENOTSUP", "EOPNOTSUPP", "ENOSYS"]);
|
|
329
|
+
var lockedDirectories = /* @__PURE__ */ new Set();
|
|
330
|
+
function errnoOf3(err) {
|
|
331
|
+
return err?.code;
|
|
332
|
+
}
|
|
333
|
+
function integerOrNull(text) {
|
|
334
|
+
const value = Number.parseInt(text ?? "", 10);
|
|
335
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
336
|
+
}
|
|
337
|
+
function parseHolder(text) {
|
|
338
|
+
const [pidText, threadText, startText, nonceText] = text.split("\n");
|
|
339
|
+
const pid = integerOrNull(pidText);
|
|
340
|
+
if (pid === null || pid <= 0) return null;
|
|
341
|
+
const nonce = nonceText?.trim() ?? "";
|
|
342
|
+
return {
|
|
343
|
+
pid,
|
|
344
|
+
threadId: integerOrNull(threadText),
|
|
345
|
+
processStartMs: integerOrNull(startText),
|
|
346
|
+
nonce: nonce.length > 0 ? nonce : null
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function formatHolder(holder) {
|
|
350
|
+
return `${holder.pid}
|
|
351
|
+
${holder.threadId ?? ""}
|
|
352
|
+
${holder.processStartMs ?? ""}
|
|
353
|
+
${holder.nonce ?? ""}
|
|
354
|
+
`;
|
|
355
|
+
}
|
|
356
|
+
async function readHolder(fs, lockPath) {
|
|
357
|
+
try {
|
|
358
|
+
return parseHolder(await fs.readFile(lockPath, "utf8")) ?? "unreadable";
|
|
359
|
+
} catch (err) {
|
|
360
|
+
if (errnoOf3(err) === "ENOENT") return "absent";
|
|
361
|
+
throw err;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async function settledHolder(fs, lockPath) {
|
|
365
|
+
const first = await readHolder(fs, lockPath);
|
|
366
|
+
if (first !== "unreadable") return first;
|
|
367
|
+
await new Promise((resolve) => setTimeout(resolve, DIRECTORY_LOCK_SETTLE_MS));
|
|
368
|
+
return readHolder(fs, lockPath);
|
|
369
|
+
}
|
|
370
|
+
function sameReading(first, second) {
|
|
371
|
+
if (typeof first !== "object" || typeof second !== "object") return first === second;
|
|
372
|
+
return formatHolder(first) === formatHolder(second);
|
|
373
|
+
}
|
|
374
|
+
function heldElsewhere(root, holder, lockPath) {
|
|
375
|
+
return new NarsilError(
|
|
376
|
+
ErrorCodes.CONFIG_INVALID,
|
|
377
|
+
`The durability directory "${root}" is in use by ${holder}, and only one engine may write a write-ahead log directory. Point this engine at another directory, or shut the other engine down first. Where no engine uses the directory, delete "${lockPath}"`,
|
|
378
|
+
{ directory: root }
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
function holderLabel(holder, self) {
|
|
382
|
+
if (holder.pid !== self.pid) return `process ${holder.pid}`;
|
|
383
|
+
return holder.threadId === self.threadId ? "another copy of Narsil in this thread" : "another thread of this process";
|
|
384
|
+
}
|
|
385
|
+
async function createLockFileExclusively(fs, lockPath, content) {
|
|
386
|
+
try {
|
|
387
|
+
await fs.writeFile(lockPath, content, { flag: "wx" });
|
|
388
|
+
return true;
|
|
389
|
+
} catch (err) {
|
|
390
|
+
if (errnoOf3(err) === "EEXIST") return false;
|
|
391
|
+
throw err;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
async function createLockFile(fs, lockPath, draftPath, content) {
|
|
395
|
+
await fs.writeFile(draftPath, content, { flag: "w" });
|
|
396
|
+
try {
|
|
397
|
+
await fs.link(draftPath, lockPath);
|
|
398
|
+
return true;
|
|
399
|
+
} catch (err) {
|
|
400
|
+
const code = errnoOf3(err);
|
|
401
|
+
if (code === "EEXIST") return false;
|
|
402
|
+
if (code !== void 0 && HARD_LINK_UNSUPPORTED_CODES.has(code)) {
|
|
403
|
+
return createLockFileExclusively(fs, lockPath, content);
|
|
404
|
+
}
|
|
405
|
+
throw err;
|
|
406
|
+
} finally {
|
|
407
|
+
await fs.unlink(draftPath).catch(() => void 0);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async function acquire(fs, root, lockPath, self) {
|
|
411
|
+
const draftPath = `${lockPath}.${self.pid}.${self.threadId ?? 0}`;
|
|
412
|
+
const content = formatHolder(self);
|
|
413
|
+
for (let attempt = 0; attempt < DIRECTORY_LOCK_ATTEMPTS; attempt++) {
|
|
414
|
+
if (await createLockFile(fs, lockPath, draftPath, content)) return;
|
|
415
|
+
const holder = await settledHolder(fs, lockPath);
|
|
416
|
+
if (typeof holder === "object" && await holderIsLive(holder, self)) {
|
|
417
|
+
throw heldElsewhere(root, holderLabel(holder, self), lockPath);
|
|
418
|
+
}
|
|
419
|
+
await removeStaleLock(
|
|
420
|
+
lockPath,
|
|
421
|
+
`${draftPath}.aside`,
|
|
422
|
+
(text) => sameReading(holder, parseHolder(text) ?? "unreadable")
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
throw heldElsewhere(root, "another process", lockPath);
|
|
426
|
+
}
|
|
427
|
+
async function describeSelf() {
|
|
428
|
+
return {
|
|
429
|
+
pid: process.pid,
|
|
430
|
+
threadId: await nodeThreadId(),
|
|
431
|
+
processStartMs: Math.round(Date.now() - process.uptime() * 1e3),
|
|
432
|
+
nonce: globalThis.crypto.randomUUID()
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
async function lockDirectory(root) {
|
|
436
|
+
const fs = await getFs();
|
|
437
|
+
const pathMod = await getPath();
|
|
438
|
+
const resolvedRoot = pathMod.resolve(root);
|
|
439
|
+
const lockPath = pathMod.join(resolvedRoot, LOCK_FILE_NAME);
|
|
440
|
+
const self = await describeSelf();
|
|
441
|
+
async function holds() {
|
|
442
|
+
const holder = await readHolder(fs, lockPath);
|
|
443
|
+
return typeof holder === "object" && holder.nonce === self.nonce;
|
|
444
|
+
}
|
|
445
|
+
async function removeOwnLockFile() {
|
|
446
|
+
if (await holds().catch(() => false)) await fs.unlink(lockPath).catch(() => void 0);
|
|
447
|
+
}
|
|
448
|
+
let directoryKey = null;
|
|
449
|
+
let closeDirectory = () => void 0;
|
|
450
|
+
try {
|
|
451
|
+
await fs.mkdir(resolvedRoot, { recursive: true });
|
|
452
|
+
const stat = await fs.stat(resolvedRoot, { bigint: true });
|
|
453
|
+
const key = `${stat.dev}:${stat.ino}`;
|
|
454
|
+
if (lockedDirectories.has(key)) throw heldElsewhere(root, "another engine in this process", lockPath);
|
|
455
|
+
lockedDirectories.add(key);
|
|
456
|
+
directoryKey = key;
|
|
457
|
+
await acquire(fs, root, lockPath, self);
|
|
458
|
+
closeDirectory = holdDirectoryOpen(resolvedRoot);
|
|
459
|
+
} catch (err) {
|
|
460
|
+
if (directoryKey !== null) lockedDirectories.delete(directoryKey);
|
|
461
|
+
await removeOwnLockFile();
|
|
462
|
+
if (err instanceof NarsilError) throw err;
|
|
463
|
+
throw new NarsilError(
|
|
464
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
465
|
+
`The engine cannot lock the durability directory "${root}"`,
|
|
466
|
+
{ directory: root, cause: err instanceof Error ? err.message : String(err) }
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
const heldKey = directoryKey;
|
|
470
|
+
let released = false;
|
|
471
|
+
return {
|
|
472
|
+
holds,
|
|
473
|
+
async release() {
|
|
474
|
+
if (released) return;
|
|
475
|
+
released = true;
|
|
476
|
+
lockedDirectories.delete(heldKey);
|
|
477
|
+
await removeOwnLockFile();
|
|
478
|
+
closeDirectory();
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/persistence/durability/list-recursive.ts
|
|
484
|
+
async function listRecursive(dir, base, pathMod, fs) {
|
|
485
|
+
const results = [];
|
|
486
|
+
let entries;
|
|
487
|
+
try {
|
|
488
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
489
|
+
} catch (err) {
|
|
490
|
+
if (err.code === "ENOENT") {
|
|
491
|
+
return results;
|
|
492
|
+
}
|
|
493
|
+
throw err;
|
|
494
|
+
}
|
|
495
|
+
for (const entry of entries) {
|
|
496
|
+
const full = pathMod.join(dir, entry.name);
|
|
497
|
+
if (entry.isDirectory()) {
|
|
498
|
+
results.push(...await listRecursive(full, base, pathMod, fs));
|
|
499
|
+
} else {
|
|
500
|
+
results.push(pathMod.relative(base, full).split(pathMod.sep).join("/"));
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return results;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// src/persistence/durability/durable-filesystem.ts
|
|
507
|
+
function validateRelativeKey(key) {
|
|
508
|
+
if (key.length === 0) {
|
|
509
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: empty");
|
|
510
|
+
}
|
|
511
|
+
if (key.includes("\0")) {
|
|
512
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: null byte detected", { key });
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function wrapFsyncError(err, key) {
|
|
516
|
+
throw new NarsilError(
|
|
517
|
+
ErrorCodes.PERSISTENCE_FSYNC_FAILED,
|
|
518
|
+
`fsync failed for "${key}"; the write is not acknowledged and the node must recover from the durable log`,
|
|
519
|
+
{ key, cause: err instanceof Error ? err.message : String(err) }
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
async function openReadWriteCreating(filePath, fs) {
|
|
523
|
+
try {
|
|
524
|
+
return await fs.open(filePath, "r+");
|
|
525
|
+
} catch (err) {
|
|
526
|
+
if (err.code !== "ENOENT") {
|
|
527
|
+
throw err;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
try {
|
|
531
|
+
return await fs.open(filePath, "wx+");
|
|
532
|
+
} catch (err) {
|
|
533
|
+
if (err.code !== "EEXIST") {
|
|
534
|
+
throw err;
|
|
535
|
+
}
|
|
536
|
+
return fs.open(filePath, "r+");
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
async function writeFully(handle, buffer, key) {
|
|
540
|
+
let offset = 0;
|
|
541
|
+
while (offset < buffer.length) {
|
|
542
|
+
let bytesWritten;
|
|
543
|
+
try {
|
|
544
|
+
;
|
|
545
|
+
({ bytesWritten } = await handle.write(buffer, offset, buffer.length - offset));
|
|
546
|
+
} catch (err) {
|
|
547
|
+
throw new NarsilError(
|
|
548
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
549
|
+
`Write to "${key}" failed; the write is not acknowledged`,
|
|
550
|
+
{
|
|
551
|
+
key,
|
|
552
|
+
written: offset,
|
|
553
|
+
total: buffer.length,
|
|
554
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
555
|
+
}
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
if (bytesWritten <= 0) {
|
|
559
|
+
throw new NarsilError(
|
|
560
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
561
|
+
`Write to "${key}" made no progress; the file would be truncated and the write is not acknowledged`,
|
|
562
|
+
{ key, written: offset, total: buffer.length }
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
offset += bytesWritten;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
async function fsyncDirectory(dirPath, fs) {
|
|
569
|
+
let handle = null;
|
|
570
|
+
try {
|
|
571
|
+
handle = await fs.open(dirPath, "r");
|
|
572
|
+
await handle.sync();
|
|
573
|
+
} catch (err) {
|
|
574
|
+
const code = err.code;
|
|
575
|
+
if (code === "EISDIR" || code === "EPERM" || code === "EINVAL" || code === "ENOTSUP") {
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
wrapFsyncError(err, dirPath);
|
|
579
|
+
} finally {
|
|
580
|
+
if (handle !== null) {
|
|
581
|
+
await handle.close().catch(() => void 0);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
function createDurableDirectory(root) {
|
|
586
|
+
if (root.trim().length === 0) {
|
|
587
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Durable directory requires a non-empty root path");
|
|
588
|
+
}
|
|
589
|
+
async function resolve(key) {
|
|
590
|
+
validateRelativeKey(key);
|
|
591
|
+
const pathMod = await getPath();
|
|
592
|
+
const resolvedBase = pathMod.resolve(root);
|
|
593
|
+
const resolvedPath = pathMod.resolve(root, key);
|
|
594
|
+
if (resolvedPath !== resolvedBase && !resolvedPath.startsWith(resolvedBase + pathMod.sep)) {
|
|
595
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: path traversal detected", { key });
|
|
596
|
+
}
|
|
597
|
+
return { path: resolvedPath, resolvedBase, pathMod };
|
|
598
|
+
}
|
|
599
|
+
async function ensureDir(filePath, pathMod, fs) {
|
|
600
|
+
await fs.mkdir(pathMod.dirname(filePath), { recursive: true });
|
|
601
|
+
}
|
|
602
|
+
return {
|
|
603
|
+
get root() {
|
|
604
|
+
return root;
|
|
605
|
+
},
|
|
606
|
+
lock() {
|
|
607
|
+
return lockDirectory(root);
|
|
608
|
+
},
|
|
609
|
+
async identity() {
|
|
610
|
+
const fs = await getFs();
|
|
611
|
+
const pathMod = await getPath();
|
|
612
|
+
try {
|
|
613
|
+
const stat = await fs.stat(pathMod.resolve(root), { bigint: true });
|
|
614
|
+
return `${stat.dev}:${stat.ino}`;
|
|
615
|
+
} catch (err) {
|
|
616
|
+
if (err.code === "ENOENT") return null;
|
|
617
|
+
throw err;
|
|
618
|
+
}
|
|
619
|
+
},
|
|
620
|
+
async appendHandle(key) {
|
|
621
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
622
|
+
const fs = await getFs();
|
|
623
|
+
await ensureDir(filePath, pathMod, fs);
|
|
624
|
+
const handle = await fs.open(filePath, "a");
|
|
625
|
+
return {
|
|
626
|
+
async append(bytes) {
|
|
627
|
+
await writeFully(handle, bytes, key);
|
|
628
|
+
},
|
|
629
|
+
async sync() {
|
|
630
|
+
try {
|
|
631
|
+
await handle.sync();
|
|
632
|
+
} catch (err) {
|
|
633
|
+
wrapFsyncError(err, key);
|
|
634
|
+
}
|
|
635
|
+
},
|
|
636
|
+
async truncate(byteLength) {
|
|
637
|
+
await handle.truncate(byteLength);
|
|
638
|
+
try {
|
|
639
|
+
await handle.sync();
|
|
640
|
+
} catch (err) {
|
|
641
|
+
wrapFsyncError(err, key);
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
async size() {
|
|
645
|
+
const stat = await handle.stat();
|
|
646
|
+
return stat.size;
|
|
647
|
+
},
|
|
648
|
+
async close() {
|
|
649
|
+
await handle.close();
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
},
|
|
653
|
+
async markerHandle(key) {
|
|
654
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
655
|
+
const fs = await getFs();
|
|
656
|
+
await ensureDir(filePath, pathMod, fs);
|
|
657
|
+
const handle = await openReadWriteCreating(filePath, fs);
|
|
658
|
+
return {
|
|
659
|
+
async read() {
|
|
660
|
+
const stat = await handle.stat();
|
|
661
|
+
if (stat.size === 0) {
|
|
662
|
+
return new Uint8Array(0);
|
|
663
|
+
}
|
|
664
|
+
const bytes = new Uint8Array(stat.size);
|
|
665
|
+
await handle.read(bytes, 0, stat.size, 0);
|
|
666
|
+
return bytes;
|
|
667
|
+
},
|
|
668
|
+
async writeSlot(offset, slot) {
|
|
669
|
+
try {
|
|
670
|
+
await handle.write(slot, 0, slot.length, offset);
|
|
671
|
+
} catch (err) {
|
|
672
|
+
throw new NarsilError(
|
|
673
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
674
|
+
`Write to "${key}" failed; the write is not acknowledged`,
|
|
675
|
+
{ key, cause: err instanceof Error ? err.message : String(err) }
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
try {
|
|
679
|
+
await handle.sync();
|
|
680
|
+
} catch (err) {
|
|
681
|
+
wrapFsyncError(err, key);
|
|
682
|
+
}
|
|
683
|
+
},
|
|
684
|
+
async close() {
|
|
685
|
+
await handle.close();
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
},
|
|
689
|
+
async syncDirectoryOf(key) {
|
|
690
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
691
|
+
const fs = await getFs();
|
|
692
|
+
await fsyncDirectory(pathMod.dirname(filePath), fs);
|
|
693
|
+
},
|
|
694
|
+
async atomicWrite(key, data) {
|
|
695
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
696
|
+
const fs = await getFs();
|
|
697
|
+
await ensureDir(filePath, pathMod, fs);
|
|
698
|
+
const dir = pathMod.dirname(filePath);
|
|
699
|
+
const tempName = `${pathMod.basename(filePath)}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
700
|
+
const tempPath = pathMod.join(dir, tempName);
|
|
701
|
+
const buffers = Array.isArray(data) ? data : [data];
|
|
702
|
+
let handle = null;
|
|
703
|
+
try {
|
|
704
|
+
handle = await fs.open(tempPath, "w");
|
|
705
|
+
for (const buffer of buffers) {
|
|
706
|
+
await writeFully(handle, buffer, tempPath);
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
await handle.sync();
|
|
710
|
+
} catch (err) {
|
|
711
|
+
wrapFsyncError(err, tempPath);
|
|
712
|
+
}
|
|
713
|
+
await handle.close();
|
|
714
|
+
handle = null;
|
|
715
|
+
await fs.rename(tempPath, filePath);
|
|
716
|
+
await fsyncDirectory(dir, fs);
|
|
717
|
+
} catch (err) {
|
|
718
|
+
if (handle !== null) {
|
|
719
|
+
await handle.close().catch(() => void 0);
|
|
720
|
+
}
|
|
721
|
+
await fs.unlink(tempPath).catch(() => void 0);
|
|
722
|
+
throw err;
|
|
723
|
+
}
|
|
724
|
+
},
|
|
725
|
+
async read(key) {
|
|
726
|
+
const { path: filePath } = await resolve(key);
|
|
727
|
+
const fs = await getFs();
|
|
728
|
+
try {
|
|
729
|
+
const data = await fs.readFile(filePath);
|
|
730
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
731
|
+
} catch (err) {
|
|
732
|
+
if (err.code === "ENOENT") {
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read "${key}"`, {
|
|
736
|
+
key,
|
|
737
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
},
|
|
741
|
+
async readRange(key, offset, length) {
|
|
742
|
+
const { path: filePath } = await resolve(key);
|
|
743
|
+
const fs = await getFs();
|
|
744
|
+
let handle = null;
|
|
745
|
+
try {
|
|
746
|
+
handle = await fs.open(filePath, "r");
|
|
747
|
+
const bytes = new Uint8Array(length);
|
|
748
|
+
let filled = 0;
|
|
749
|
+
while (filled < length) {
|
|
750
|
+
const { bytesRead } = await handle.read(bytes, filled, length - filled, offset + filled);
|
|
751
|
+
if (bytesRead === 0) break;
|
|
752
|
+
filled += bytesRead;
|
|
753
|
+
}
|
|
754
|
+
if (filled < length) {
|
|
755
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `"${key}" ends before the bytes a read asked for`, {
|
|
756
|
+
key,
|
|
757
|
+
offset,
|
|
758
|
+
length
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
return bytes;
|
|
762
|
+
} finally {
|
|
763
|
+
await handle?.close();
|
|
764
|
+
}
|
|
765
|
+
},
|
|
766
|
+
async byteLength(key) {
|
|
767
|
+
const { path: filePath } = await resolve(key);
|
|
768
|
+
const fs = await getFs();
|
|
769
|
+
try {
|
|
770
|
+
return (await fs.stat(filePath)).size;
|
|
771
|
+
} catch (err) {
|
|
772
|
+
if (err.code === "ENOENT") {
|
|
773
|
+
return null;
|
|
774
|
+
}
|
|
775
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read the length of "${key}"`, {
|
|
776
|
+
key,
|
|
777
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
},
|
|
781
|
+
async remove(key) {
|
|
782
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
783
|
+
const fs = await getFs();
|
|
784
|
+
try {
|
|
785
|
+
await fs.unlink(filePath);
|
|
786
|
+
} catch (err) {
|
|
787
|
+
if (err.code === "ENOENT") {
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_DELETE_FAILED, `Failed to delete "${key}"`, {
|
|
791
|
+
key,
|
|
792
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
await fsyncDirectory(pathMod.dirname(filePath), fs);
|
|
796
|
+
},
|
|
797
|
+
async move(from, to) {
|
|
798
|
+
const source = await resolve(from);
|
|
799
|
+
const target = await resolve(to);
|
|
800
|
+
const fs = await getFs();
|
|
801
|
+
await ensureDir(target.path, target.pathMod, fs);
|
|
802
|
+
await fs.rename(source.path, target.path);
|
|
803
|
+
const directories = new Set([target.path, source.path].map((path) => target.pathMod.dirname(path)));
|
|
804
|
+
for (const movedIn of directories) await fsyncDirectory(movedIn, fs);
|
|
805
|
+
},
|
|
806
|
+
async list(prefix) {
|
|
807
|
+
const fs = await getFs();
|
|
808
|
+
const pathMod = await getPath();
|
|
809
|
+
const resolvedBase = pathMod.resolve(root);
|
|
810
|
+
const all = await listRecursive(resolvedBase, resolvedBase, pathMod, fs);
|
|
811
|
+
return all.filter((entry) => entry.startsWith(prefix)).sort(compareCodePoints);
|
|
812
|
+
},
|
|
813
|
+
async pathOf(key) {
|
|
814
|
+
const { path: filePath } = await resolve(key);
|
|
815
|
+
return filePath;
|
|
816
|
+
}
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// src/persistence/durability/sha256.ts
|
|
821
|
+
var ROUND_CONSTANTS = new Uint32Array([
|
|
822
|
+
1116352408,
|
|
823
|
+
1899447441,
|
|
824
|
+
3049323471,
|
|
825
|
+
3921009573,
|
|
826
|
+
961987163,
|
|
827
|
+
1508970993,
|
|
828
|
+
2453635748,
|
|
829
|
+
2870763221,
|
|
830
|
+
3624381080,
|
|
831
|
+
310598401,
|
|
832
|
+
607225278,
|
|
833
|
+
1426881987,
|
|
834
|
+
1925078388,
|
|
835
|
+
2162078206,
|
|
836
|
+
2614888103,
|
|
837
|
+
3248222580,
|
|
838
|
+
3835390401,
|
|
839
|
+
4022224774,
|
|
840
|
+
264347078,
|
|
841
|
+
604807628,
|
|
842
|
+
770255983,
|
|
843
|
+
1249150122,
|
|
844
|
+
1555081692,
|
|
845
|
+
1996064986,
|
|
846
|
+
2554220882,
|
|
847
|
+
2821834349,
|
|
848
|
+
2952996808,
|
|
849
|
+
3210313671,
|
|
850
|
+
3336571891,
|
|
851
|
+
3584528711,
|
|
852
|
+
113926993,
|
|
853
|
+
338241895,
|
|
854
|
+
666307205,
|
|
855
|
+
773529912,
|
|
856
|
+
1294757372,
|
|
857
|
+
1396182291,
|
|
858
|
+
1695183700,
|
|
859
|
+
1986661051,
|
|
860
|
+
2177026350,
|
|
861
|
+
2456956037,
|
|
862
|
+
2730485921,
|
|
863
|
+
2820302411,
|
|
864
|
+
3259730800,
|
|
865
|
+
3345764771,
|
|
866
|
+
3516065817,
|
|
867
|
+
3600352804,
|
|
868
|
+
4094571909,
|
|
869
|
+
275423344,
|
|
870
|
+
430227734,
|
|
871
|
+
506948616,
|
|
872
|
+
659060556,
|
|
873
|
+
883997877,
|
|
874
|
+
958139571,
|
|
875
|
+
1322822218,
|
|
876
|
+
1537002063,
|
|
877
|
+
1747873779,
|
|
878
|
+
1955562222,
|
|
879
|
+
2024104815,
|
|
880
|
+
2227730452,
|
|
881
|
+
2361852424,
|
|
882
|
+
2428436474,
|
|
883
|
+
2756734187,
|
|
884
|
+
3204031479,
|
|
885
|
+
3329325298
|
|
886
|
+
]);
|
|
887
|
+
var INITIAL_STATE = [1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225];
|
|
888
|
+
var BLOCK_BYTES = 64;
|
|
889
|
+
var LENGTH_BYTES = 8;
|
|
890
|
+
function rotateRight(value, bits) {
|
|
891
|
+
return value >>> bits | value << 32 - bits;
|
|
892
|
+
}
|
|
893
|
+
function padded(bytes) {
|
|
894
|
+
const total = Math.ceil((bytes.byteLength + 1 + LENGTH_BYTES) / BLOCK_BYTES) * BLOCK_BYTES;
|
|
895
|
+
const message = new Uint8Array(total);
|
|
896
|
+
message.set(bytes);
|
|
897
|
+
message[bytes.byteLength] = 128;
|
|
898
|
+
const view = new DataView(message.buffer);
|
|
899
|
+
const bitLength = bytes.byteLength * 8;
|
|
900
|
+
view.setUint32(total - LENGTH_BYTES, Math.floor(bitLength / 4294967296), false);
|
|
901
|
+
view.setUint32(total - 4, bitLength >>> 0, false);
|
|
902
|
+
return view;
|
|
903
|
+
}
|
|
904
|
+
function sha256(bytes) {
|
|
905
|
+
const view = padded(bytes);
|
|
906
|
+
const state = INITIAL_STATE.slice();
|
|
907
|
+
const words = new Uint32Array(64);
|
|
908
|
+
for (let block = 0; block < view.byteLength; block += BLOCK_BYTES) {
|
|
909
|
+
for (let i = 0; i < 16; i++) words[i] = view.getUint32(block + i * 4, false);
|
|
910
|
+
for (let i = 16; i < 64; i++) {
|
|
911
|
+
const w15 = words[i - 15];
|
|
912
|
+
const w2 = words[i - 2];
|
|
913
|
+
const s0 = rotateRight(w15, 7) ^ rotateRight(w15, 18) ^ w15 >>> 3;
|
|
914
|
+
const s1 = rotateRight(w2, 17) ^ rotateRight(w2, 19) ^ w2 >>> 10;
|
|
915
|
+
words[i] = words[i - 16] + s0 + words[i - 7] + s1 >>> 0;
|
|
916
|
+
}
|
|
917
|
+
let [a, b, c, d, e, f, g, h] = state;
|
|
918
|
+
for (let i = 0; i < 64; i++) {
|
|
919
|
+
const s1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
|
|
920
|
+
const choice = e & f ^ ~e & g;
|
|
921
|
+
const t1 = h + s1 + choice + ROUND_CONSTANTS[i] + words[i] >>> 0;
|
|
922
|
+
const s0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
|
|
923
|
+
const majority = a & b ^ a & c ^ b & c;
|
|
924
|
+
const t2 = s0 + majority >>> 0;
|
|
925
|
+
h = g;
|
|
926
|
+
g = f;
|
|
927
|
+
f = e;
|
|
928
|
+
e = d + t1 >>> 0;
|
|
929
|
+
d = c;
|
|
930
|
+
c = b;
|
|
931
|
+
b = a;
|
|
932
|
+
a = t1 + t2 >>> 0;
|
|
933
|
+
}
|
|
934
|
+
state[0] = state[0] + a >>> 0;
|
|
935
|
+
state[1] = state[1] + b >>> 0;
|
|
936
|
+
state[2] = state[2] + c >>> 0;
|
|
937
|
+
state[3] = state[3] + d >>> 0;
|
|
938
|
+
state[4] = state[4] + e >>> 0;
|
|
939
|
+
state[5] = state[5] + f >>> 0;
|
|
940
|
+
state[6] = state[6] + g >>> 0;
|
|
941
|
+
state[7] = state[7] + h >>> 0;
|
|
942
|
+
}
|
|
943
|
+
const digest = new Uint8Array(32);
|
|
944
|
+
const out = new DataView(digest.buffer);
|
|
945
|
+
for (let i = 0; i < 8; i++) out.setUint32(i * 4, state[i], false);
|
|
946
|
+
return digest;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// src/persistence/durability/restore-journal.ts
|
|
950
|
+
var RESTORE_JOURNAL_PREFIX = ".swap-";
|
|
951
|
+
var RESTORE_STAGING_PREFIX = ".restore-";
|
|
952
|
+
var RETIRED_INDEX_PREFIX = ".retired-";
|
|
953
|
+
var LOCK_FILE_KEY = ".narsil.lock";
|
|
954
|
+
var STAGING_PREFIX_PATTERN = /^\.[A-Za-z0-9][A-Za-z0-9._-]*\/$/;
|
|
955
|
+
function digestOf(indexName) {
|
|
956
|
+
let hex = "";
|
|
957
|
+
for (const byte of sha256(new TextEncoder().encode(indexName))) hex += byte.toString(16).padStart(2, "0");
|
|
958
|
+
return hex;
|
|
959
|
+
}
|
|
960
|
+
function journalKey(indexName) {
|
|
961
|
+
return `${RESTORE_JOURNAL_PREFIX}${digestOf(indexName)}`;
|
|
962
|
+
}
|
|
963
|
+
function randomSuffix() {
|
|
964
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
965
|
+
}
|
|
966
|
+
function newStagingPrefix() {
|
|
967
|
+
return `${RESTORE_STAGING_PREFIX}${randomSuffix()}/`;
|
|
968
|
+
}
|
|
969
|
+
function newRetiredPrefix() {
|
|
970
|
+
return `${RETIRED_INDEX_PREFIX}${randomSuffix()}/`;
|
|
971
|
+
}
|
|
972
|
+
function encodeJournal(indexName, stagingPrefix) {
|
|
973
|
+
return new TextEncoder().encode(`${indexName}
|
|
974
|
+
${stagingPrefix}`);
|
|
975
|
+
}
|
|
976
|
+
function isStagingPrefix(prefix) {
|
|
977
|
+
return STAGING_PREFIX_PATTERN.test(prefix) && !prefix.includes("..");
|
|
978
|
+
}
|
|
979
|
+
function corruptJournal(key, reason) {
|
|
980
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The restore journal "${key}" ${reason}`, { key });
|
|
981
|
+
}
|
|
982
|
+
function readJournal(key, bytes) {
|
|
983
|
+
const text = new TextDecoder().decode(bytes);
|
|
984
|
+
const lineEnd = text.indexOf("\n");
|
|
985
|
+
const indexName = lineEnd === -1 ? text : text.slice(0, lineEnd);
|
|
986
|
+
if (key.slice(RESTORE_JOURNAL_PREFIX.length) !== digestOf(indexName)) return { kind: "leftover" };
|
|
987
|
+
if (indexName.length > MAX_INDEX_NAME_LENGTH || !INDEX_NAME_PATTERN.test(indexName) || indexName.includes("..")) {
|
|
988
|
+
corruptJournal(key, "names an index that no index name can match");
|
|
989
|
+
}
|
|
990
|
+
const stagingPrefix = lineEnd === -1 ? "" : text.slice(lineEnd + 1);
|
|
991
|
+
if (!isStagingPrefix(stagingPrefix) || stagingPrefix === `${LOCK_FILE_KEY}/`) {
|
|
992
|
+
corruptJournal(key, "names no staging prefix that a restore writes");
|
|
993
|
+
}
|
|
994
|
+
return { kind: "journal", indexName, stagingPrefix };
|
|
995
|
+
}
|
|
996
|
+
async function listedKeys(storage, indexName, stagingPrefix) {
|
|
997
|
+
const manifestBytes = await storage.read(`${stagingPrefix}manifest`) ?? await storage.read(manifestKey(indexName));
|
|
998
|
+
if (manifestBytes === null) {
|
|
999
|
+
throw new NarsilError(
|
|
1000
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1001
|
+
`The restore of index "${indexName}" lost its manifest, both from staging and from the index`,
|
|
1002
|
+
{ indexName, stagingPrefix }
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
const decoded = await decodeSegmentManifest(manifestBytes, indexName);
|
|
1006
|
+
if (decoded.kind !== "current") {
|
|
1007
|
+
throw new NarsilError(
|
|
1008
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1009
|
+
`The restore of index "${indexName}" staged a manifest in the layout of an earlier release`,
|
|
1010
|
+
{ indexName }
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
const root = `${indexName}/`;
|
|
1014
|
+
const keys = /* @__PURE__ */ new Set(["meta", "manifest"]);
|
|
1015
|
+
for (const ref of manifestFileRefs(decoded.manifest)) keys.add(ref.key.slice(root.length));
|
|
1016
|
+
return keys;
|
|
1017
|
+
}
|
|
1018
|
+
function withoutTrailingSlash(prefix) {
|
|
1019
|
+
return prefix.slice(0, -1);
|
|
1020
|
+
}
|
|
1021
|
+
async function finishJournal(storage, indexName, stagingPrefix) {
|
|
1022
|
+
const root = `${indexName}/`;
|
|
1023
|
+
const listed = await listedKeys(storage, indexName, stagingPrefix);
|
|
1024
|
+
const staged = await storage.list(stagingPrefix);
|
|
1025
|
+
let retiredPrefix = null;
|
|
1026
|
+
if (storage.moveTree !== void 0 && staged.length > 0) {
|
|
1027
|
+
retiredPrefix = newRetiredPrefix();
|
|
1028
|
+
await storage.moveTree(indexName, withoutTrailingSlash(retiredPrefix));
|
|
1029
|
+
await storage.moveTree(withoutTrailingSlash(stagingPrefix), indexName);
|
|
1030
|
+
} else {
|
|
1031
|
+
const stagedKeys = new Set(staged);
|
|
1032
|
+
for (const key of listed) {
|
|
1033
|
+
if (stagedKeys.has(`${stagingPrefix}${key}`)) await storage.moveKey(`${stagingPrefix}${key}`, `${root}${key}`);
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
for (const key of await storage.list(root)) {
|
|
1037
|
+
if (!listed.has(key.slice(root.length))) await storage.remove(key);
|
|
1038
|
+
}
|
|
1039
|
+
await storage.remove(journalKey(indexName));
|
|
1040
|
+
return retiredPrefix;
|
|
1041
|
+
}
|
|
1042
|
+
async function removePrefix(storage, prefix) {
|
|
1043
|
+
if (storage.removeTree !== void 0) {
|
|
1044
|
+
await storage.removeTree(prefix);
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
for (const key of await storage.list(prefix)) await storage.remove(key);
|
|
1048
|
+
}
|
|
1049
|
+
async function commitJournal(storage, indexName, stagingPrefix) {
|
|
1050
|
+
await storage.writeJournal(journalKey(indexName), encodeJournal(indexName, stagingPrefix));
|
|
1051
|
+
}
|
|
1052
|
+
function isTopLevelJournal(key) {
|
|
1053
|
+
return key.startsWith(RESTORE_JOURNAL_PREFIX) && !key.includes("/");
|
|
1054
|
+
}
|
|
1055
|
+
async function removeStagingPrefixes(storage) {
|
|
1056
|
+
const prefixes = /* @__PURE__ */ new Set();
|
|
1057
|
+
for (const key of await storage.list(".")) {
|
|
1058
|
+
const slash = key.indexOf("/");
|
|
1059
|
+
if (slash !== -1 && isStagingPrefix(key.slice(0, slash + 1))) prefixes.add(key.slice(0, slash + 1));
|
|
1060
|
+
}
|
|
1061
|
+
for (const prefix of prefixes) await removePrefix(storage, prefix);
|
|
1062
|
+
}
|
|
1063
|
+
async function recoverRestoreJournals(storage) {
|
|
1064
|
+
for (const key of await storage.list(RESTORE_JOURNAL_PREFIX)) {
|
|
1065
|
+
if (!isTopLevelJournal(key)) continue;
|
|
1066
|
+
const bytes = await storage.read(key);
|
|
1067
|
+
if (bytes === null) continue;
|
|
1068
|
+
const reading = readJournal(key, bytes);
|
|
1069
|
+
if (reading.kind === "leftover") {
|
|
1070
|
+
await storage.remove(key);
|
|
1071
|
+
continue;
|
|
1072
|
+
}
|
|
1073
|
+
await finishJournal(storage, reading.indexName, reading.stagingPrefix);
|
|
1074
|
+
}
|
|
1075
|
+
await removeStagingPrefixes(storage);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
// src/persistence/durability/restore-directory.ts
|
|
1079
|
+
function missing(error) {
|
|
1080
|
+
return error.code === "ENOENT";
|
|
1081
|
+
}
|
|
1082
|
+
function storageFailed(code, message, key, error) {
|
|
1083
|
+
return new NarsilError(code, message, { key, cause: error instanceof Error ? error.message : String(error) });
|
|
1084
|
+
}
|
|
1085
|
+
function directoryRestoreStorage(directory) {
|
|
1086
|
+
return {
|
|
1087
|
+
directory,
|
|
1088
|
+
read: (key) => directory.read(key),
|
|
1089
|
+
write: (key, chunks) => directory.atomicWrite(key, chunks),
|
|
1090
|
+
writeJournal: (key, bytes) => directory.atomicWrite(key, bytes),
|
|
1091
|
+
remove: (key) => directory.remove(key),
|
|
1092
|
+
list: (prefix) => directory.list(prefix),
|
|
1093
|
+
moveKey: (from, to) => directory.move(from, to),
|
|
1094
|
+
pathOf: (key) => directory.pathOf(key),
|
|
1095
|
+
async moveTree(from, to) {
|
|
1096
|
+
const source = await directory.pathOf(from);
|
|
1097
|
+
const target = await directory.pathOf(to);
|
|
1098
|
+
const fs = await getFs();
|
|
1099
|
+
try {
|
|
1100
|
+
await fs.rename(source, target);
|
|
1101
|
+
} catch (error) {
|
|
1102
|
+
if (missing(error)) return;
|
|
1103
|
+
throw storageFailed(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Failed to move "${from}" to "${to}"`, from, error);
|
|
1104
|
+
}
|
|
1105
|
+
await directory.syncDirectoryOf(to);
|
|
1106
|
+
},
|
|
1107
|
+
async removeTree(prefix) {
|
|
1108
|
+
const key = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
1109
|
+
const fs = await getFs();
|
|
1110
|
+
try {
|
|
1111
|
+
await fs.rm(await directory.pathOf(key), { recursive: true, force: true });
|
|
1112
|
+
} catch (error) {
|
|
1113
|
+
throw storageFailed(ErrorCodes.PERSISTENCE_DELETE_FAILED, `Failed to delete "${key}"`, key, error);
|
|
1114
|
+
}
|
|
1115
|
+
await directory.syncDirectoryOf(key);
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
function recoverDirectoryRestores(directory) {
|
|
1120
|
+
return recoverRestoreJournals(directoryRestoreStorage(directory));
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// src/persistence/durability/restore-staging.ts
|
|
1124
|
+
function toError2(error) {
|
|
1125
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
1126
|
+
}
|
|
1127
|
+
function pathUnder(path, root) {
|
|
1128
|
+
if (!path.startsWith(root)) return false;
|
|
1129
|
+
const next = path.charAt(root.length);
|
|
1130
|
+
return next === "" || next === "/" || next === "\\";
|
|
1131
|
+
}
|
|
1132
|
+
function stagedDirectory(directory, staged, unstaged) {
|
|
1133
|
+
const { readRange, byteLength } = directory;
|
|
1134
|
+
return {
|
|
1135
|
+
root: directory.root,
|
|
1136
|
+
appendHandle: (key) => directory.appendHandle(staged(key)),
|
|
1137
|
+
markerHandle: (key) => directory.markerHandle(staged(key)),
|
|
1138
|
+
atomicWrite: (key, data) => directory.atomicWrite(staged(key), data),
|
|
1139
|
+
syncDirectoryOf: (key) => directory.syncDirectoryOf(staged(key)),
|
|
1140
|
+
read: (key) => directory.read(staged(key)),
|
|
1141
|
+
remove: (key) => directory.remove(staged(key)),
|
|
1142
|
+
move: (from, to) => directory.move(staged(from), staged(to)),
|
|
1143
|
+
list: async (prefix) => (await directory.list(staged(prefix))).map(unstaged),
|
|
1144
|
+
pathOf: (key) => directory.pathOf(staged(key)),
|
|
1145
|
+
...readRange === void 0 ? {} : {
|
|
1146
|
+
readRange: (key, offset, length) => readRange.call(directory, staged(key), offset, length)
|
|
1147
|
+
},
|
|
1148
|
+
...byteLength === void 0 ? {} : { byteLength: (key) => byteLength.call(directory, staged(key)) }
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
function createRestoreStaging(storage, indexName, options = {}) {
|
|
1152
|
+
const stagingPrefix = newStagingPrefix();
|
|
1153
|
+
const indexRoot = `${indexName}/`;
|
|
1154
|
+
let committed = false;
|
|
1155
|
+
const staged = (key) => {
|
|
1156
|
+
if (!key.startsWith(indexRoot)) {
|
|
1157
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `A restore of "${indexName}" cannot stage "${key}"`, {
|
|
1158
|
+
key
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
return `${stagingPrefix}${key.slice(indexRoot.length)}`;
|
|
1162
|
+
};
|
|
1163
|
+
const unstaged = (key) => `${indexRoot}${key.slice(stagingPrefix.length)}`;
|
|
1164
|
+
const store = {
|
|
1165
|
+
read: (key) => storage.read(staged(key)),
|
|
1166
|
+
write: (key, chunks) => storage.write(staged(key), chunks),
|
|
1167
|
+
remove: (key) => storage.remove(staged(key)),
|
|
1168
|
+
list: async (prefix) => (await storage.list(staged(prefix))).map(unstaged)
|
|
1169
|
+
};
|
|
1170
|
+
const pathOf = storage.pathOf;
|
|
1171
|
+
if (pathOf !== void 0) store.pathOf = (key) => pathOf.call(storage, staged(key));
|
|
1172
|
+
if (storage.directory !== void 0) store.directory = stagedDirectory(storage.directory, staged, unstaged);
|
|
1173
|
+
let retiredPrefix = null;
|
|
1174
|
+
async function finishOnStorage() {
|
|
1175
|
+
retiredPrefix = await finishJournal(storage, indexName, stagingPrefix);
|
|
1176
|
+
}
|
|
1177
|
+
return {
|
|
1178
|
+
store,
|
|
1179
|
+
async relocation() {
|
|
1180
|
+
if (pathOf === void 0 || (await storage.list(stagingPrefix)).length > 0) return null;
|
|
1181
|
+
const from = await pathOf.call(storage, stagingPrefix.slice(0, -1));
|
|
1182
|
+
const to = await pathOf.call(storage, indexName);
|
|
1183
|
+
return (path) => pathUnder(path, from) ? `${to}${path.slice(from.length)}` : path;
|
|
1184
|
+
},
|
|
1185
|
+
async commit() {
|
|
1186
|
+
try {
|
|
1187
|
+
await commitJournal(storage, indexName, stagingPrefix);
|
|
1188
|
+
} catch (error) {
|
|
1189
|
+
try {
|
|
1190
|
+
await storage.remove(journalKey(indexName));
|
|
1191
|
+
} catch (removal) {
|
|
1192
|
+
committed = true;
|
|
1193
|
+
options.onStorageLost?.(toError2(removal));
|
|
1194
|
+
}
|
|
1195
|
+
throw error;
|
|
1196
|
+
}
|
|
1197
|
+
committed = true;
|
|
1198
|
+
},
|
|
1199
|
+
async finish() {
|
|
1200
|
+
try {
|
|
1201
|
+
await (options.finish === void 0 ? finishOnStorage() : options.finish(finishOnStorage));
|
|
1202
|
+
} catch (error) {
|
|
1203
|
+
options.onStorageLost?.(toError2(error));
|
|
1204
|
+
}
|
|
1205
|
+
},
|
|
1206
|
+
async removeRetired() {
|
|
1207
|
+
const prefix = retiredPrefix;
|
|
1208
|
+
retiredPrefix = null;
|
|
1209
|
+
if (prefix !== null) await removePrefix(storage, prefix);
|
|
1210
|
+
},
|
|
1211
|
+
async discard() {
|
|
1212
|
+
if (committed) return;
|
|
1213
|
+
await removePrefix(storage, stagingPrefix);
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
function adapterRestoreStorage(adapter) {
|
|
1218
|
+
const store = adapterCheckpointStore(adapter);
|
|
1219
|
+
return {
|
|
1220
|
+
read: (key) => store.read(key),
|
|
1221
|
+
write: (key, chunks) => store.write(key, chunks),
|
|
1222
|
+
writeJournal: (key, bytes) => store.write(key, [bytes]),
|
|
1223
|
+
remove: (key) => store.remove(key),
|
|
1224
|
+
list: (prefix) => store.list(prefix),
|
|
1225
|
+
async moveKey(from, to) {
|
|
1226
|
+
const bytes = await store.read(from);
|
|
1227
|
+
if (bytes === null) return;
|
|
1228
|
+
await store.write(to, [bytes]);
|
|
1229
|
+
await store.remove(from);
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
// src/persistence/durability/archive-checkpoint.ts
|
|
1235
|
+
function toError3(error) {
|
|
1236
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
1237
|
+
}
|
|
1238
|
+
function stageDirectoryRestore(deps, indexName) {
|
|
1239
|
+
return createRestoreStaging(directoryRestoreStorage(deps.directory), indexName, {
|
|
1240
|
+
async finish(run) {
|
|
1241
|
+
try {
|
|
1242
|
+
await run();
|
|
1243
|
+
} catch (error) {
|
|
1244
|
+
deps.markFatal(toError3(error));
|
|
1245
|
+
throw error;
|
|
1246
|
+
}
|
|
1247
|
+
},
|
|
1248
|
+
onStorageLost: deps.markFatal
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1251
|
+
function releaser(held, indexName) {
|
|
1252
|
+
let released = false;
|
|
1253
|
+
return () => {
|
|
1254
|
+
if (released) return;
|
|
1255
|
+
released = true;
|
|
1256
|
+
const count = (held.get(indexName) ?? 1) - 1;
|
|
1257
|
+
if (count <= 0) held.delete(indexName);
|
|
1258
|
+
else held.set(indexName, count);
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
function createArchiveCheckpointAccess(deps) {
|
|
1262
|
+
const store = directoryCheckpointStore(deps.directory);
|
|
1263
|
+
return {
|
|
1264
|
+
async openArchiveCheckpoint(indexName) {
|
|
1265
|
+
deps.held.set(indexName, (deps.held.get(indexName) ?? 0) + 1);
|
|
1266
|
+
const release = releaser(deps.held, indexName);
|
|
1267
|
+
try {
|
|
1268
|
+
await deps.checkpoint(indexName, false);
|
|
1269
|
+
await deps.checkpoint(indexName, true);
|
|
1270
|
+
const manifestBytes = await deps.directory.read(manifestKey(indexName));
|
|
1271
|
+
if (manifestBytes === null) {
|
|
1272
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Index "${indexName}" holds no checkpoint`, {
|
|
1273
|
+
indexName
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
const decoded = await decodeSegmentManifest(manifestBytes, indexName);
|
|
1277
|
+
if (decoded.kind !== "current") {
|
|
1278
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Index "${indexName}" holds an earlier layout`, {
|
|
1279
|
+
indexName
|
|
1280
|
+
});
|
|
1281
|
+
}
|
|
1282
|
+
return {
|
|
1283
|
+
manifestBytes,
|
|
1284
|
+
manifest: decoded.manifest,
|
|
1285
|
+
readFile: (ref) => readListedFile(store, ref.key, { bytes: ref.bytes, what: "checkpoint file" }),
|
|
1286
|
+
readRange: async (ref, offset, length) => {
|
|
1287
|
+
const readRange = deps.directory.readRange;
|
|
1288
|
+
if (readRange !== void 0) return readRange.call(deps.directory, ref.key, offset, length);
|
|
1289
|
+
const whole = await readListedFile(store, ref.key, { bytes: ref.bytes, what: "checkpoint file" });
|
|
1290
|
+
return whole.slice(offset, offset + length);
|
|
1291
|
+
},
|
|
1292
|
+
release,
|
|
1293
|
+
directory: deps.directory
|
|
1294
|
+
};
|
|
1295
|
+
} catch (error) {
|
|
1296
|
+
release();
|
|
1297
|
+
throw error;
|
|
1298
|
+
}
|
|
1299
|
+
},
|
|
1300
|
+
stageRestore(indexName) {
|
|
1301
|
+
return stageDirectoryRestore(deps, indexName);
|
|
1302
|
+
},
|
|
1303
|
+
adoptRestoredCheckpoint(indexName, checkpoint) {
|
|
1304
|
+
for (const { partitionId, lastSeqNo, primaryTerm } of checkpoint) {
|
|
1305
|
+
deps.registerPartition(indexName, partitionId, lastSeqNo, primaryTerm);
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
// src/persistence/durability/checkpoint.ts
|
|
1312
|
+
function walPrefix(indexName, partitionId) {
|
|
1313
|
+
return `${indexName}/wal/${partitionId}/`;
|
|
1314
|
+
}
|
|
1315
|
+
async function truncateCoveredSegments(directory, indexName, checkpoint) {
|
|
1316
|
+
for (const { partitionId, lastSeqNo } of checkpoint) {
|
|
1317
|
+
const prefix = walPrefix(indexName, partitionId);
|
|
1318
|
+
const markerBytes = await directory.read(`${prefix}commit`);
|
|
1319
|
+
const marker = markerBytes === null ? null : readCommitMarker(markerBytes);
|
|
1320
|
+
if (marker === null) {
|
|
1321
|
+
continue;
|
|
1322
|
+
}
|
|
1323
|
+
const activeSegmentSeqNo = marker.state.activeSegmentSeqNo;
|
|
1324
|
+
const segments = await directory.list(prefix);
|
|
1325
|
+
const startSeqNos = segments.map((key) => parseSegmentStartSeqNo(key, prefix)).filter((value) => value !== null).sort((a, b) => a - b);
|
|
1326
|
+
for (let i = 0; i < startSeqNos.length; i += 1) {
|
|
1327
|
+
const start = startSeqNos[i];
|
|
1328
|
+
if (start >= activeSegmentSeqNo) {
|
|
1329
|
+
break;
|
|
1330
|
+
}
|
|
1331
|
+
const nextStart = startSeqNos[i + 1];
|
|
1332
|
+
const segmentCoversBeyondCheckpoint = nextStart === void 0 || nextStart > lastSeqNo + 1;
|
|
1333
|
+
if (segmentCoversBeyondCheckpoint) {
|
|
1334
|
+
break;
|
|
1335
|
+
}
|
|
1336
|
+
await directory.remove(`${prefix}${formatSegmentStart(start)}`);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
async function reclaimWalBeyondCount(directory, indexName, partitionCount, partitions, onCloseError) {
|
|
1341
|
+
for (const [partitionId, partition] of [...partitions]) {
|
|
1342
|
+
if (partitionId < partitionCount) continue;
|
|
1343
|
+
try {
|
|
1344
|
+
await partition.walWriter.close();
|
|
1345
|
+
} catch (err) {
|
|
1346
|
+
onCloseError(err instanceof Error ? err : new Error(String(err)));
|
|
1347
|
+
}
|
|
1348
|
+
partitions.delete(partitionId);
|
|
1349
|
+
}
|
|
1350
|
+
const walRoot = `${indexName}/wal/`;
|
|
1351
|
+
for (const key of await directory.list(walRoot)) {
|
|
1352
|
+
const partitionId = walKeyPartitionId(key, walRoot);
|
|
1353
|
+
if (partitionId === null || partitionId < partitionCount) continue;
|
|
1354
|
+
await directory.remove(key);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
function walKeyPartitionId(key, walRoot) {
|
|
1358
|
+
const tail = key.slice(walRoot.length);
|
|
1359
|
+
const separator = tail.indexOf("/");
|
|
1360
|
+
if (separator <= 0) {
|
|
1361
|
+
return null;
|
|
1362
|
+
}
|
|
1363
|
+
const segment = tail.slice(0, separator);
|
|
1364
|
+
if (!/^\d+$/.test(segment)) {
|
|
1365
|
+
return null;
|
|
1366
|
+
}
|
|
1367
|
+
const value = Number.parseInt(segment, 10);
|
|
1368
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
1369
|
+
}
|
|
1370
|
+
function parseSegmentStartSeqNo(key, prefix) {
|
|
1371
|
+
if (!key.startsWith(prefix)) {
|
|
1372
|
+
return null;
|
|
1373
|
+
}
|
|
1374
|
+
const tail = key.slice(prefix.length);
|
|
1375
|
+
if (!/^\d{16}$/.test(tail)) {
|
|
1376
|
+
return null;
|
|
1377
|
+
}
|
|
1378
|
+
const value = Number.parseInt(tail, 10);
|
|
1379
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
1380
|
+
}
|
|
1381
|
+
function formatSegmentStart(startSeqNo) {
|
|
1382
|
+
return startSeqNo.toString().padStart(16, "0");
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
// src/persistence/durability/recovery.ts
|
|
1386
|
+
async function removeLeftoverRewriteFiles(directory, names) {
|
|
1387
|
+
for (const indexName of names) {
|
|
1388
|
+
for (const place of ["rebuilt", "received"]) {
|
|
1389
|
+
for (const key of await directory.list(`${indexName}/${place}/`)) await directory.remove(key);
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
async function listPersistedIndexes(directory) {
|
|
1394
|
+
const metaKeys = await directory.list("");
|
|
1395
|
+
const names = [];
|
|
1396
|
+
for (const key of metaKeys) {
|
|
1397
|
+
if (key.endsWith("/meta")) {
|
|
1398
|
+
names.push(key.slice(0, -"/meta".length));
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
return names;
|
|
1402
|
+
}
|
|
1403
|
+
async function loadMetadata(directory, indexName) {
|
|
1404
|
+
const bytes = await directory.read(`${indexName}/meta`);
|
|
1405
|
+
if (bytes === null) {
|
|
1406
|
+
return null;
|
|
1407
|
+
}
|
|
1408
|
+
const { metadata } = await readMetadataEnvelope(bytes);
|
|
1409
|
+
return metadata;
|
|
1410
|
+
}
|
|
1411
|
+
async function replayWal(directory, indexName, partitionId, fromSeqNoExclusive, deps) {
|
|
1412
|
+
await directory.remove(nextSegmentKey(indexName, partitionId));
|
|
1413
|
+
const read = await readWalSegments(directory, indexName, partitionId);
|
|
1414
|
+
if (read === null) {
|
|
1415
|
+
return { highestSeqNo: fromSeqNoExclusive, highestPrimaryTerm: 0 };
|
|
1416
|
+
}
|
|
1417
|
+
const { marker, segments, durableEntries, activeTail, highestReadFromWal } = read;
|
|
1418
|
+
if (Math.max(fromSeqNoExclusive, highestReadFromWal) < marker.state.highestDurableSeqNo) {
|
|
1419
|
+
throw durableRecordMissing(indexName, partitionId, highestReadFromWal, marker);
|
|
1420
|
+
}
|
|
1421
|
+
let highestSeqNo = fromSeqNoExclusive;
|
|
1422
|
+
let highestPrimaryTerm = 0;
|
|
1423
|
+
const replay = async (entries) => {
|
|
1424
|
+
for (const entry of entries) {
|
|
1425
|
+
if (entry.seqNo <= highestSeqNo) {
|
|
1426
|
+
continue;
|
|
1427
|
+
}
|
|
1428
|
+
if (turnIsSpent()) await yieldTurn();
|
|
1429
|
+
applyEntry(entry, deps);
|
|
1430
|
+
highestSeqNo = entry.seqNo;
|
|
1431
|
+
if (entry.primaryTerm > highestPrimaryTerm) {
|
|
1432
|
+
highestPrimaryTerm = entry.primaryTerm;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
};
|
|
1436
|
+
await replay(durableEntries);
|
|
1437
|
+
if (activeTail !== null) {
|
|
1438
|
+
await replay(activeTail.entries);
|
|
1439
|
+
}
|
|
1440
|
+
await deleteOrphanSegments(directory, segments, marker.state.activeSegmentSeqNo);
|
|
1441
|
+
if (activeTail !== null && activeTail.cleanEnd < activeTail.segmentLength) {
|
|
1442
|
+
await truncateSegmentTail(directory, activeTail.key, activeTail.cleanEnd);
|
|
1443
|
+
}
|
|
1444
|
+
return { highestSeqNo, highestPrimaryTerm };
|
|
1445
|
+
}
|
|
1446
|
+
async function deleteOrphanSegments(directory, segments, activeSegmentSeqNo) {
|
|
1447
|
+
for (const { key, startSeqNo } of segments) {
|
|
1448
|
+
if (startSeqNo > activeSegmentSeqNo) {
|
|
1449
|
+
await directory.remove(key);
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
async function truncateSegmentTail(directory, key, durableByteLength) {
|
|
1454
|
+
const cleanLength = Math.max(durableByteLength, SEGMENT_HEADER_SIZE);
|
|
1455
|
+
const handle = await directory.appendHandle(key);
|
|
1456
|
+
try {
|
|
1457
|
+
const currentSize = await handle.size();
|
|
1458
|
+
if (cleanLength < currentSize) {
|
|
1459
|
+
await handle.truncate(cleanLength);
|
|
1460
|
+
}
|
|
1461
|
+
} finally {
|
|
1462
|
+
await handle.close();
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
function applyEntry(entry, deps) {
|
|
1466
|
+
deps.writeRecords?.replayed(entry);
|
|
1467
|
+
if (entry.operation === "DELETE") {
|
|
1468
|
+
applyDeleteEntry(entry, deps.manager, deps.vectorIndexes);
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
applyIndexEntry(entry, deps.manager, deps.vectorFieldPaths, deps.vectorIndexes, deps.defersGraphBuilds !== true);
|
|
1472
|
+
}
|
|
1473
|
+
function snapshotCheckpointFor(checkpoint, partitionId) {
|
|
1474
|
+
return checkpointLastSeqNo(checkpoint, partitionId);
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
// src/persistence/durability/checkpoint-capture.ts
|
|
1478
|
+
function whileNoMutationApplies(partitions, capture2) {
|
|
1479
|
+
return new Promise((resolve, reject) => {
|
|
1480
|
+
let release = () => void 0;
|
|
1481
|
+
const released = new Promise((settle) => {
|
|
1482
|
+
release = settle;
|
|
1483
|
+
});
|
|
1484
|
+
function captureNow() {
|
|
1485
|
+
try {
|
|
1486
|
+
resolve(capture2());
|
|
1487
|
+
} catch (error) {
|
|
1488
|
+
reject(error);
|
|
1489
|
+
} finally {
|
|
1490
|
+
release();
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
let waiting = partitions.length;
|
|
1494
|
+
if (waiting === 0) {
|
|
1495
|
+
captureNow();
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
for (const partition of partitions) {
|
|
1499
|
+
partition.appendChain = partition.appendChain.then(() => {
|
|
1500
|
+
waiting -= 1;
|
|
1501
|
+
if (waiting === 0) captureNow();
|
|
1502
|
+
return released;
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
});
|
|
1506
|
+
}
|
|
1507
|
+
async function makeEveryAppliedMutationDurable(indexState, failPartition2) {
|
|
1508
|
+
for (const [partitionId, partition] of [...indexState.partitions]) {
|
|
1509
|
+
await partition.appendChain;
|
|
1510
|
+
if (partition.failed !== null) continue;
|
|
1511
|
+
try {
|
|
1512
|
+
await partition.walWriter.commit();
|
|
1513
|
+
} catch (error) {
|
|
1514
|
+
failPartition2(partitionId, partition, error);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
async function openNewLogSegments(indexState, failPartition2) {
|
|
1519
|
+
const rolls = [...indexState.partitions].map(async ([partitionId, partition]) => {
|
|
1520
|
+
try {
|
|
1521
|
+
if (partition.failed === null) await partition.walWriter.prepareCheckpointRoll();
|
|
1522
|
+
} catch (error) {
|
|
1523
|
+
failPartition2(partitionId, partition, error);
|
|
1524
|
+
return;
|
|
1525
|
+
}
|
|
1526
|
+
const rolled = partition.appendChain.then(() => partition.failed === null ? partition.walWriter.rollForCheckpoint() : void 0).catch((error) => {
|
|
1527
|
+
failPartition2(partitionId, partition, error);
|
|
1528
|
+
});
|
|
1529
|
+
partition.appendChain = rolled;
|
|
1530
|
+
await rolled;
|
|
1531
|
+
});
|
|
1532
|
+
await Promise.all(rolls);
|
|
1533
|
+
}
|
|
1534
|
+
function aTargetFailed(indexState, targets) {
|
|
1535
|
+
return targets.some((target) => (indexState.partitions.get(target.partitionId)?.failed ?? null) !== null);
|
|
1536
|
+
}
|
|
1537
|
+
function noWholePartitions() {
|
|
1538
|
+
return { contents: [], captured: [] };
|
|
1539
|
+
}
|
|
1540
|
+
function wholePartitionsWhereMostChanged(manager, rules) {
|
|
1541
|
+
const { priorCheckpoint, priorPartitionIds, everyPartition, aWorkerCanSerialise, captureRecords } = rules;
|
|
1542
|
+
return (targets) => {
|
|
1543
|
+
const whole = { contents: [], captured: [] };
|
|
1544
|
+
const changed = targets.filter(
|
|
1545
|
+
(target) => everyPartition || target.lastSeqNo !== snapshotCheckpointFor(priorCheckpoint, target.partitionId)
|
|
1546
|
+
);
|
|
1547
|
+
const mostOfEachChanged = changed.every((target) => {
|
|
1548
|
+
const records = target.lastSeqNo - snapshotCheckpointFor(priorCheckpoint, target.partitionId);
|
|
1549
|
+
const documents = manager.getPartition(target.partitionId).count();
|
|
1550
|
+
return documents <= WHOLE_PARTITION_LARGEST_DOCUMENT_COUNT && records >= documents * WHOLE_PARTITION_MIN_CHANGED_SHARE;
|
|
1551
|
+
});
|
|
1552
|
+
const logMatchesMemory = targets.length === 1 && priorPartitionIds.every((partitionId) => partitionId === 0);
|
|
1553
|
+
if (!everyPartition && !(mostOfEachChanged && logMatchesMemory)) return whole;
|
|
1554
|
+
for (const { partitionId } of changed) {
|
|
1555
|
+
const capture2 = captureRecords?.(partitionId);
|
|
1556
|
+
const captured = aWorkerCanSerialise ? captureWholePartition(manager, partitionId, capture2) : null;
|
|
1557
|
+
if (captured !== null) {
|
|
1558
|
+
whole.captured.push(captured);
|
|
1559
|
+
} else if (everyPartition || !aWorkerCanSerialise) {
|
|
1560
|
+
whole.contents.push(partitionContentsOf(manager, partitionId, capture2));
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
return whole;
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1566
|
+
async function captureCheckpoint(indexState, manager, vectorIndexes, wholePartitionsOf = noWholePartitions, priorCheckpointThatLimitsTheRecords, listedVectorFields = [], termHistoryOf, onCaptured, pinsRetiredVectors = false) {
|
|
1567
|
+
for (const vectorIndex of vectorIndexes.values()) await vectorIndex.completeGraph();
|
|
1568
|
+
return whileNoMutationApplies([...indexState.partitions.values()], () => {
|
|
1569
|
+
onCaptured?.();
|
|
1570
|
+
const documentCount = manager.countDocuments();
|
|
1571
|
+
const targets = [];
|
|
1572
|
+
let recordsLeftInLog = 0;
|
|
1573
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
1574
|
+
const partition = indexState.partitions.get(partitionId);
|
|
1575
|
+
if (partition !== void 0 && partition.failed !== null) continue;
|
|
1576
|
+
const appliedSeqNo = partition?.appliedSeqNo ?? 0;
|
|
1577
|
+
const lastSeqNo = priorCheckpointThatLimitsTheRecords === void 0 ? appliedSeqNo : Math.min(
|
|
1578
|
+
appliedSeqNo,
|
|
1579
|
+
snapshotCheckpointFor(priorCheckpointThatLimitsTheRecords, partitionId) + LOG_RECORDS_PER_CHECKPOINT
|
|
1580
|
+
);
|
|
1581
|
+
recordsLeftInLog += appliedSeqNo - lastSeqNo;
|
|
1582
|
+
const target = {
|
|
1583
|
+
partitionId,
|
|
1584
|
+
lastSeqNo,
|
|
1585
|
+
primaryTerm: partition?.seqOwner.primaryTerm ?? SINGLE_NODE_PRIMARY_TERM
|
|
1586
|
+
};
|
|
1587
|
+
if (termHistoryOf !== void 0) target.termHistory = termHistoryOf(partitionId, lastSeqNo);
|
|
1588
|
+
targets.push(target);
|
|
1589
|
+
}
|
|
1590
|
+
const vectorPlans = /* @__PURE__ */ new Map();
|
|
1591
|
+
try {
|
|
1592
|
+
for (const [fieldPath, vectorIndex] of vectorIndexes) {
|
|
1593
|
+
const listed = listedVectorFields.find((ref) => ref.fieldPath === fieldPath);
|
|
1594
|
+
const listedKeys2 = listed === void 0 ? null : listed.files.map((file) => file.key);
|
|
1595
|
+
vectorPlans.set(fieldPath, vectorIndex.planCheckpoint(listedKeys2, false, pinsRetiredVectors));
|
|
1596
|
+
}
|
|
1597
|
+
const memoryMatchesTheTargets = recordsLeftInLog === 0;
|
|
1598
|
+
const wholePartitions = memoryMatchesTheTargets ? wholePartitionsOf(targets) : noWholePartitions();
|
|
1599
|
+
return { targets, documentCount, vectorPlans, wholePartitions, recordsLeftInLog };
|
|
1600
|
+
} catch (error) {
|
|
1601
|
+
for (const plan of vectorPlans.values()) plan.release();
|
|
1602
|
+
throw error;
|
|
1603
|
+
}
|
|
1604
|
+
});
|
|
1605
|
+
}
|
|
1606
|
+
async function writeCapturedVectors(store, indexName, capture2, priorManifest, rewriteUnchanged) {
|
|
1607
|
+
if (capture2.vectorPlans.size === 0) return { vectors: [], layouts: [], written: [] };
|
|
1608
|
+
const priorVectors = priorManifest?.vectors ?? [];
|
|
1609
|
+
const priorCheckpoint = priorManifest?.checkpoint ?? [];
|
|
1610
|
+
const noPartitionChanged = capture2.targets.every(
|
|
1611
|
+
(target) => target.lastSeqNo === snapshotCheckpointFor(priorCheckpoint, target.partitionId)
|
|
1612
|
+
);
|
|
1613
|
+
const everyFieldWritten = [...capture2.vectorPlans.keys()].every(
|
|
1614
|
+
(fieldPath) => priorVectors.some((ref) => ref.fieldPath === fieldPath)
|
|
1615
|
+
);
|
|
1616
|
+
const everyPlanUnchanged = [...capture2.vectorPlans.values()].every((plan) => plan.unchanged);
|
|
1617
|
+
if (!rewriteUnchanged && everyFieldWritten && noPartitionChanged && everyPlanUnchanged) {
|
|
1618
|
+
return { vectors: priorVectors, layouts: [], written: [] };
|
|
1619
|
+
}
|
|
1620
|
+
const written = await writeLiveVectors({ store, indexName, plans: capture2.vectorPlans, priorVectors });
|
|
1621
|
+
return { vectors: written.refs, layouts: written.layouts, written: written.written };
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
// src/persistence/durability/checkpoint-worker-dispatch.ts
|
|
1625
|
+
var workerUsable = true;
|
|
1626
|
+
var pooledWorker = null;
|
|
1627
|
+
var workerBusy = false;
|
|
1628
|
+
var workerIdleMs = CHECKPOINT_WORKER_IDLE_MS;
|
|
1629
|
+
var idleTimer = null;
|
|
1630
|
+
async function spawnWorker() {
|
|
1631
|
+
return null;
|
|
1632
|
+
}
|
|
1633
|
+
function discardWorker(worker) {
|
|
1634
|
+
if (pooledWorker === worker) {
|
|
1635
|
+
pooledWorker = null;
|
|
1636
|
+
}
|
|
1637
|
+
try {
|
|
1638
|
+
void worker.terminate();
|
|
1639
|
+
} catch {
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
function unrefTimer(timer) {
|
|
1643
|
+
if (typeof timer.unref === "function") {
|
|
1644
|
+
timer.unref();
|
|
1645
|
+
}
|
|
1646
|
+
return timer;
|
|
1647
|
+
}
|
|
1648
|
+
function runWorker(worker, request) {
|
|
1649
|
+
return new Promise((resolve) => {
|
|
1650
|
+
let settled = false;
|
|
1651
|
+
let timeoutId = armSilenceTimeout();
|
|
1652
|
+
function armSilenceTimeout() {
|
|
1653
|
+
return unrefTimer(setTimeout(() => settle({ written: null, timedOut: true }, true), CHECKPOINT_WORKER_TIMEOUT_MS));
|
|
1654
|
+
}
|
|
1655
|
+
const onMessage = (msg) => {
|
|
1656
|
+
const response = msg;
|
|
1657
|
+
if (response.type === "heartbeat") {
|
|
1658
|
+
clearTimeout(timeoutId);
|
|
1659
|
+
timeoutId = armSilenceTimeout();
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
if (response.type === "success") {
|
|
1663
|
+
settle({ written: response.segments, timedOut: false }, false);
|
|
1664
|
+
} else {
|
|
1665
|
+
settle({ written: null, timedOut: false }, true);
|
|
1666
|
+
}
|
|
1667
|
+
};
|
|
1668
|
+
const onError = () => {
|
|
1669
|
+
settle({ written: null, timedOut: false }, true);
|
|
1670
|
+
};
|
|
1671
|
+
const onExit = () => {
|
|
1672
|
+
settle({ written: null, timedOut: false }, true);
|
|
1673
|
+
};
|
|
1674
|
+
function settle(outcome, discard) {
|
|
1675
|
+
if (settled) {
|
|
1676
|
+
return;
|
|
1677
|
+
}
|
|
1678
|
+
settled = true;
|
|
1679
|
+
clearTimeout(timeoutId);
|
|
1680
|
+
worker.off("message", onMessage);
|
|
1681
|
+
worker.off("error", onError);
|
|
1682
|
+
worker.off("exit", onExit);
|
|
1683
|
+
if (discard) {
|
|
1684
|
+
discardWorker(worker);
|
|
1685
|
+
}
|
|
1686
|
+
resolve(outcome);
|
|
1687
|
+
}
|
|
1688
|
+
worker.on("message", onMessage);
|
|
1689
|
+
worker.on("error", onError);
|
|
1690
|
+
worker.on("exit", onExit);
|
|
1691
|
+
try {
|
|
1692
|
+
worker.postMessage(request);
|
|
1693
|
+
} catch {
|
|
1694
|
+
settle({ written: null, timedOut: false }, true);
|
|
1695
|
+
}
|
|
1696
|
+
});
|
|
1697
|
+
}
|
|
1698
|
+
function delay(ms) {
|
|
1699
|
+
return new Promise((resolve) => {
|
|
1700
|
+
unrefTimer(setTimeout(resolve, ms));
|
|
1701
|
+
});
|
|
1702
|
+
}
|
|
1703
|
+
function checkpointWorkerIsUsable() {
|
|
1704
|
+
const runtime = detectRuntime();
|
|
1705
|
+
return runtime.supportsWorkerThreads && runtime.supportsFileSystem && workerUsable;
|
|
1706
|
+
}
|
|
1707
|
+
async function runCheckpointOnWorker(request) {
|
|
1708
|
+
const runtime = detectRuntime();
|
|
1709
|
+
if (!runtime.supportsWorkerThreads || !runtime.supportsFileSystem || !workerUsable) {
|
|
1710
|
+
return null;
|
|
1711
|
+
}
|
|
1712
|
+
if (workerBusy) {
|
|
1713
|
+
return null;
|
|
1714
|
+
}
|
|
1715
|
+
workerBusy = true;
|
|
1716
|
+
cancelIdleRetirement();
|
|
1717
|
+
try {
|
|
1718
|
+
if (pooledWorker === null) {
|
|
1719
|
+
pooledWorker = await spawnWorker();
|
|
1720
|
+
}
|
|
1721
|
+
const worker = pooledWorker;
|
|
1722
|
+
if (worker === null) {
|
|
1723
|
+
workerUsable = false;
|
|
1724
|
+
return null;
|
|
1725
|
+
}
|
|
1726
|
+
const outcome = await runWorker(worker, request);
|
|
1727
|
+
if (outcome.written !== null) {
|
|
1728
|
+
return outcome.written;
|
|
1729
|
+
}
|
|
1730
|
+
if (outcome.timedOut) {
|
|
1731
|
+
await delay(CHECKPOINT_TIMEOUT_RECOVERY_BACKOFF_MS);
|
|
1732
|
+
}
|
|
1733
|
+
return null;
|
|
1734
|
+
} finally {
|
|
1735
|
+
workerBusy = false;
|
|
1736
|
+
retireWorkerOnceIdle();
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
function cancelIdleRetirement() {
|
|
1740
|
+
if (idleTimer !== null) {
|
|
1741
|
+
clearTimeout(idleTimer);
|
|
1742
|
+
idleTimer = null;
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
function retireWorkerOnceIdle() {
|
|
1746
|
+
cancelIdleRetirement();
|
|
1747
|
+
idleTimer = unrefTimer(
|
|
1748
|
+
setTimeout(() => {
|
|
1749
|
+
idleTimer = null;
|
|
1750
|
+
if (!workerBusy) terminateCheckpointWorker();
|
|
1751
|
+
}, workerIdleMs)
|
|
1752
|
+
);
|
|
1753
|
+
}
|
|
1754
|
+
function terminateCheckpointWorker() {
|
|
1755
|
+
cancelIdleRetirement();
|
|
1756
|
+
const worker = pooledWorker;
|
|
1757
|
+
if (worker === null) {
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
pooledWorker = null;
|
|
1761
|
+
try {
|
|
1762
|
+
void worker.terminate();
|
|
1763
|
+
} catch {
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
// src/persistence/durability/checkpoint-write.ts
|
|
1768
|
+
function workerCanWriteEverySegment(input) {
|
|
1769
|
+
if (!input.canOffload || input.wholePartitions.length > 0) return false;
|
|
1770
|
+
const analysesTextInProcessOnly = input.metadata.tokenizer !== void 0 || input.metadata.stopWords !== void 0;
|
|
1771
|
+
if (!analysesTextInProcessOnly) return true;
|
|
1772
|
+
return input.targets.every(
|
|
1773
|
+
(target) => input.capturedPartitions.some((captured) => captured.partitionId === target.partitionId)
|
|
1774
|
+
);
|
|
1775
|
+
}
|
|
1776
|
+
async function writeIndexCheckpointSegments(input) {
|
|
1777
|
+
const { directory, metadata, targets, compactionThreshold, prior, wholePartitions, capturedPartitions } = input;
|
|
1778
|
+
const { keepsWriteRecords } = input;
|
|
1779
|
+
const offloaded = workerCanWriteEverySegment(input) ? await runCheckpointOnWorker({
|
|
1780
|
+
root: directory.root,
|
|
1781
|
+
metadata,
|
|
1782
|
+
targets,
|
|
1783
|
+
compactionThreshold,
|
|
1784
|
+
prior,
|
|
1785
|
+
capturedPartitions: [...capturedPartitions],
|
|
1786
|
+
keepsWriteRecords
|
|
1787
|
+
}) : null;
|
|
1788
|
+
if (offloaded !== null) {
|
|
1789
|
+
return offloaded;
|
|
1790
|
+
}
|
|
1791
|
+
return writeCheckpointSegments({
|
|
1792
|
+
store: directoryCheckpointStore(directory),
|
|
1793
|
+
directory,
|
|
1794
|
+
metadata,
|
|
1795
|
+
targets,
|
|
1796
|
+
compactionThreshold,
|
|
1797
|
+
prior,
|
|
1798
|
+
wholePartitions,
|
|
1799
|
+
capturedPartitions,
|
|
1800
|
+
keepsWriteRecords
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
// src/persistence/durability/segment/vector-adoption.ts
|
|
1805
|
+
function recordWrittenVectors(vectorIndexes, written) {
|
|
1806
|
+
for (const field of written) {
|
|
1807
|
+
vectorIndexes.get(field.fieldPath)?.recordCheckpoint(field.plan, field.files);
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
async function adoptVectorLayouts(store, vectorIndexes, layouts) {
|
|
1811
|
+
const pathOf = store.pathOf;
|
|
1812
|
+
if (pathOf === void 0) return;
|
|
1813
|
+
for (const layout of layouts) {
|
|
1814
|
+
const vecIndex = vectorIndexes.get(layout.fieldPath);
|
|
1815
|
+
if (vecIndex === void 0 || vecIndex.storage !== "disk") continue;
|
|
1816
|
+
const path = await pathOf.call(store, layout.key);
|
|
1817
|
+
await vecIndex.adoptDiskLayout({
|
|
1818
|
+
path,
|
|
1819
|
+
docIds: layout.docIds,
|
|
1820
|
+
dead: layout.dead,
|
|
1821
|
+
vectorsOffset: layout.vectorsOffset
|
|
1822
|
+
});
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
async function garbageThatNoFieldStillReads(store, vectorIndexes, keys) {
|
|
1826
|
+
const pathOf = store.pathOf;
|
|
1827
|
+
if (pathOf === void 0) return [...keys];
|
|
1828
|
+
const read = /* @__PURE__ */ new Set();
|
|
1829
|
+
for (const vectorIndex of vectorIndexes.values()) {
|
|
1830
|
+
for (const path of vectorIndex.vectorFilesInUse()) read.add(path);
|
|
1831
|
+
}
|
|
1832
|
+
if (read.size === 0) return [...keys];
|
|
1833
|
+
const unread = [];
|
|
1834
|
+
for (const key of keys) {
|
|
1835
|
+
if (!read.has(await pathOf.call(store, key))) unread.push(key);
|
|
1836
|
+
}
|
|
1837
|
+
return unread;
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
// src/persistence/durability/write-stall.ts
|
|
1841
|
+
function checkpointIsDue(indexState, mutationThreshold, stallAboveBytes = WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES) {
|
|
1842
|
+
return indexState.mutationsSinceCheckpoint >= mutationThreshold || indexState.documentBytesSinceCheckpoint >= stallAboveBytes;
|
|
1843
|
+
}
|
|
1844
|
+
async function stallWhileCheckpointsFallBehind(indexState, stallAboveBytes = WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES) {
|
|
1845
|
+
while (indexState.documentBytesSinceCheckpoint >= stallAboveBytes && indexState.checkpointInFlight !== null && !indexState.unloading) {
|
|
1846
|
+
await waitOutsideCountedWrite(
|
|
1847
|
+
() => new Promise((resume) => {
|
|
1848
|
+
indexState.stalledWrites.push(resume);
|
|
1849
|
+
})
|
|
1850
|
+
);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
function noteCheckpointCommitted(indexState, recordsBeyondTheCheckpoint) {
|
|
1854
|
+
const counted = indexState.mutationsSinceCheckpoint;
|
|
1855
|
+
const shareLeft = counted <= 0 ? 0 : Math.min(1, recordsBeyondTheCheckpoint / counted);
|
|
1856
|
+
indexState.documentBytesSinceCheckpoint = Math.round(indexState.documentBytesSinceCheckpoint * shareLeft);
|
|
1857
|
+
indexState.mutationsSinceCheckpoint = recordsBeyondTheCheckpoint;
|
|
1858
|
+
releaseStalledWrites(indexState);
|
|
1859
|
+
}
|
|
1860
|
+
function releaseStalledWrites(indexState) {
|
|
1861
|
+
const waiting = indexState.stalledWrites.splice(0);
|
|
1862
|
+
for (const resume of waiting) resume();
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
// src/persistence/durability/checkpoint-run.ts
|
|
1866
|
+
async function bothOrTheFirstFailure(first, second) {
|
|
1867
|
+
const [firstSettled, secondSettled] = await Promise.allSettled([first, second]);
|
|
1868
|
+
if (firstSettled.status === "rejected") throw firstSettled.reason;
|
|
1869
|
+
if (secondSettled.status === "rejected") throw secondSettled.reason;
|
|
1870
|
+
return [firstSettled.value, secondSettled.value];
|
|
1871
|
+
}
|
|
1872
|
+
async function runDurableCheckpoint(input) {
|
|
1873
|
+
try {
|
|
1874
|
+
let recordsLeftInLog = await writeOneBoundedCheckpoint(input);
|
|
1875
|
+
while (recordsLeftInLog > 0 && input.mayContinue()) {
|
|
1876
|
+
recordsLeftInLog = await writeOneBoundedCheckpoint(input);
|
|
1877
|
+
}
|
|
1878
|
+
} catch (err) {
|
|
1879
|
+
if (!isSystemError(err)) throw err;
|
|
1880
|
+
const error = new NarsilError(
|
|
1881
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1882
|
+
`The engine cannot write the checkpoint of index "${input.indexName}" to the durability directory "${input.directory.root}"`,
|
|
1883
|
+
{
|
|
1884
|
+
indexName: input.indexName,
|
|
1885
|
+
directory: input.directory.root,
|
|
1886
|
+
cause: err.message
|
|
1887
|
+
}
|
|
1888
|
+
);
|
|
1889
|
+
input.markFatal(error);
|
|
1890
|
+
throw error;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
function isSystemError(err) {
|
|
1894
|
+
return err instanceof Error && !(err instanceof NarsilError) && typeof err.code === "string";
|
|
1895
|
+
}
|
|
1896
|
+
async function writeOneBoundedCheckpoint(input) {
|
|
1897
|
+
const manager = input.hooks.getManager(input.indexName);
|
|
1898
|
+
if (manager === void 0) {
|
|
1899
|
+
return 0;
|
|
1900
|
+
}
|
|
1901
|
+
const metadata = input.hooks.buildTermsMetadata(input.indexName);
|
|
1902
|
+
if (metadata === void 0) {
|
|
1903
|
+
return 0;
|
|
1904
|
+
}
|
|
1905
|
+
const fromMemory = input.fromMemory || input.hooks.mustRewriteTerms(input.indexName);
|
|
1906
|
+
const vectorIndexes = input.hooks.getVectorIndexes(input.indexName);
|
|
1907
|
+
const store = directoryCheckpointStore(input.directory);
|
|
1908
|
+
const writeRecords = input.hooks.writeRecords ?? null;
|
|
1909
|
+
const { indexName } = input;
|
|
1910
|
+
const priorManifest = await readCurrentManifest(store, input.indexName);
|
|
1911
|
+
const textOnDisk = metadata.textStorage === "disk";
|
|
1912
|
+
const wholePartitionsOf = wholePartitionsWhereMostChanged(manager, {
|
|
1913
|
+
priorCheckpoint: priorManifest?.checkpoint ?? [],
|
|
1914
|
+
priorPartitionIds: (priorManifest?.partitions ?? []).map((partition) => partition.partitionId),
|
|
1915
|
+
everyPartition: fromMemory,
|
|
1916
|
+
aWorkerCanSerialise: input.canOffload && !textOnDisk && checkpointWorkerIsUsable(),
|
|
1917
|
+
...writeRecords === null ? {} : {
|
|
1918
|
+
captureRecords: (partitionId) => (segments) => writeRecords.captureSegments(indexName, partitionId, segments)
|
|
1919
|
+
}
|
|
1920
|
+
});
|
|
1921
|
+
let changesSinceCapture = null;
|
|
1922
|
+
const capture2 = await captureCheckpoint(
|
|
1923
|
+
input.indexState,
|
|
1924
|
+
manager,
|
|
1925
|
+
vectorIndexes,
|
|
1926
|
+
textOnDisk && !fromMemory ? void 0 : wholePartitionsOf,
|
|
1927
|
+
fromMemory ? void 0 : priorManifest?.checkpoint ?? [],
|
|
1928
|
+
priorManifest?.vectors ?? [],
|
|
1929
|
+
writeRecords === null ? void 0 : (partitionId, lastSeqNo) => writeRecords.termHistoryOf(indexName, partitionId, lastSeqNo),
|
|
1930
|
+
textOnDisk ? () => {
|
|
1931
|
+
changesSinceCapture = manager.openCheckpointChangeLog();
|
|
1932
|
+
} : void 0,
|
|
1933
|
+
input.pinsRetiredVectors
|
|
1934
|
+
);
|
|
1935
|
+
try {
|
|
1936
|
+
await openNewLogSegments(input.indexState, input.failPartition);
|
|
1937
|
+
return await finishBoundedCheckpoint(input, manager, store, metadata, priorManifest, capture2, fromMemory, () => {
|
|
1938
|
+
return changesSinceCapture;
|
|
1939
|
+
});
|
|
1940
|
+
} finally {
|
|
1941
|
+
for (const plan of capture2.vectorPlans.values()) plan.release();
|
|
1942
|
+
changesSinceCapture?.close();
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
async function adoptWrittenSegments(input, manager, partitions, changes) {
|
|
1946
|
+
const { indexName, indexState, hooks } = input;
|
|
1947
|
+
const { actionPartitionsOf, closeUnusedSegments, prepareDiskAdoption, swapPreparedPartitions } = await import('./module-VEK2KCKU.mjs');
|
|
1948
|
+
const adoption = await prepareDiskAdoption(input.directory, manager, partitions);
|
|
1949
|
+
if (adoption === null) return [];
|
|
1950
|
+
const adopted = await whileNoMutationApplies([...indexState.partitions.values()], () => {
|
|
1951
|
+
if (changes.partitionsReplaced) return false;
|
|
1952
|
+
const changed = changes.takeWrittenDocIds();
|
|
1953
|
+
swapPreparedPartitions(manager, adoption.partitions, changed, (partitionId, segments) => {
|
|
1954
|
+
hooks.writeRecords?.adoptPagedSegments(indexName, partitionId, segments, changed);
|
|
1955
|
+
});
|
|
1956
|
+
hooks.queueDiskAdoption?.({
|
|
1957
|
+
type: "adoptDiskSegments",
|
|
1958
|
+
indexName,
|
|
1959
|
+
partitions: actionPartitionsOf(adoption),
|
|
1960
|
+
changed: [...changed],
|
|
1961
|
+
requestId: createRequestId()
|
|
1962
|
+
});
|
|
1963
|
+
return true;
|
|
1964
|
+
});
|
|
1965
|
+
if (!adopted) {
|
|
1966
|
+
closeUnusedSegments(adoption);
|
|
1967
|
+
return [];
|
|
1968
|
+
}
|
|
1969
|
+
await hooks.awaitCopiesOfIndex?.(indexName);
|
|
1970
|
+
return adoption.droppedKeys;
|
|
1971
|
+
}
|
|
1972
|
+
async function finishBoundedCheckpoint(input, manager, store, metadata, priorManifest, capture2, fromMemory, changesSinceCapture) {
|
|
1973
|
+
const vectorIndexes = input.hooks.getVectorIndexes(input.indexName);
|
|
1974
|
+
const writeRecords = input.hooks.writeRecords ?? null;
|
|
1975
|
+
const { targets, documentCount } = capture2;
|
|
1976
|
+
await makeEveryAppliedMutationDurable(input.indexState, input.failPartition);
|
|
1977
|
+
if (aTargetFailed(input.indexState, targets)) return 0;
|
|
1978
|
+
const [liveVectors, segments] = await bothOrTheFirstFailure(
|
|
1979
|
+
writeCapturedVectors(store, input.indexName, capture2, priorManifest, fromMemory),
|
|
1980
|
+
writeIndexCheckpointSegments({
|
|
1981
|
+
directory: input.directory,
|
|
1982
|
+
metadata,
|
|
1983
|
+
targets,
|
|
1984
|
+
compactionThreshold: input.compactionThreshold,
|
|
1985
|
+
canOffload: input.canOffload,
|
|
1986
|
+
prior: priorManifest,
|
|
1987
|
+
wholePartitions: capture2.wholePartitions.contents,
|
|
1988
|
+
capturedPartitions: capture2.wholePartitions.captured,
|
|
1989
|
+
keepsWriteRecords: writeRecords !== null
|
|
1990
|
+
})
|
|
1991
|
+
);
|
|
1992
|
+
await makeEveryAppliedMutationDurable(input.indexState, input.failPartition);
|
|
1993
|
+
if (aTargetFailed(input.indexState, targets)) return 0;
|
|
1994
|
+
const written = await commitCheckpointManifest(store, metadata, segments, liveVectors.vectors);
|
|
1995
|
+
input.hooks.onManifestCommitted(input.indexName, metadata.analysisGeneration ?? 0);
|
|
1996
|
+
recordWrittenVectors(vectorIndexes, liveVectors.written);
|
|
1997
|
+
await adoptVectorLayouts(store, vectorIndexes, liveVectors.layouts);
|
|
1998
|
+
const changes = changesSinceCapture();
|
|
1999
|
+
const droppedThisCheckpoint = changes !== null && capture2.recordsLeftInLog === 0 ? new Set(await adoptWrittenSegments(input, manager, segments.partitions, changes)) : /* @__PURE__ */ new Set();
|
|
2000
|
+
if (!input.holdsCheckpointFiles()) {
|
|
2001
|
+
const removable = await garbageThatNoFieldStillReads(store, vectorIndexes, written.garbage);
|
|
2002
|
+
const stillRead = metadata.textStorage === "disk" ? (await import('./module-VEK2KCKU.mjs')).keysThatSegmentsStillRead(manager) : /* @__PURE__ */ new Set();
|
|
2003
|
+
await removeCheckpointGarbage(
|
|
2004
|
+
store,
|
|
2005
|
+
removable.filter((key) => !droppedThisCheckpoint.has(key) && !stillRead.has(key))
|
|
2006
|
+
);
|
|
2007
|
+
}
|
|
2008
|
+
const checkpointDocumentCount = documentCount;
|
|
2009
|
+
await input.queueMetadataWrite(input.indexName, async () => {
|
|
2010
|
+
const checkpointMetadata = input.hooks.buildMetadata(input.indexName, checkpointDocumentCount);
|
|
2011
|
+
if (checkpointMetadata === void 0) {
|
|
2012
|
+
return;
|
|
2013
|
+
}
|
|
2014
|
+
const bytes = await writeMetadataEnvelope(checkpointMetadata, { checksum: true });
|
|
2015
|
+
await input.directory.atomicWrite(`${input.indexName}/meta`, bytes);
|
|
2016
|
+
input.hooks.recordCheckpoint?.(input.indexName, checkpointDocumentCount, manager.partitionCount);
|
|
2017
|
+
});
|
|
2018
|
+
await truncateCoveredSegments(input.directory, input.indexName, targets);
|
|
2019
|
+
await reclaimWalBeyondCount(
|
|
2020
|
+
input.directory,
|
|
2021
|
+
input.indexName,
|
|
2022
|
+
manager.partitionCount,
|
|
2023
|
+
input.indexState.partitions,
|
|
2024
|
+
input.markFatal
|
|
2025
|
+
);
|
|
2026
|
+
const beyondTheCheckpoint = recordsBeyond(input.indexState, targets);
|
|
2027
|
+
noteCheckpointCommitted(input.indexState, beyondTheCheckpoint);
|
|
2028
|
+
const anotherIsDue = input.mutationThreshold > 0 && beyondTheCheckpoint >= input.mutationThreshold;
|
|
2029
|
+
return anotherIsDue ? beyondTheCheckpoint : capture2.recordsLeftInLog;
|
|
2030
|
+
}
|
|
2031
|
+
function recordsBeyond(indexState, targets) {
|
|
2032
|
+
let records = 0;
|
|
2033
|
+
for (const [partitionId, partition] of indexState.partitions) {
|
|
2034
|
+
if (partition.failed !== null) continue;
|
|
2035
|
+
records += Math.max(0, partition.appliedSeqNo - snapshotCheckpointFor(targets, partitionId));
|
|
2036
|
+
}
|
|
2037
|
+
return records;
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
// src/persistence/durability/checkpoint-turn.ts
|
|
2041
|
+
function createIndexLoads(vectorIndexesOf2) {
|
|
2042
|
+
const loads = /* @__PURE__ */ new Map();
|
|
2043
|
+
const startGraphRepairs = (indexName) => {
|
|
2044
|
+
for (const vectorIndex of vectorIndexesOf2(indexName).values()) {
|
|
2045
|
+
vectorIndex.scheduleBuild();
|
|
2046
|
+
vectorIndex.startShuffledRebuildWhenDue();
|
|
2047
|
+
}
|
|
2048
|
+
};
|
|
2049
|
+
return {
|
|
2050
|
+
track(indexName, load, startsGraphRepairs = true) {
|
|
2051
|
+
const loaded = startsGraphRepairs ? load.then(() => startGraphRepairs(indexName)) : load;
|
|
2052
|
+
loads.set(
|
|
2053
|
+
indexName,
|
|
2054
|
+
loaded.catch(() => void 0)
|
|
2055
|
+
);
|
|
2056
|
+
return loaded;
|
|
2057
|
+
},
|
|
2058
|
+
settled: (indexName) => loads.get(indexName) ?? Promise.resolve(),
|
|
2059
|
+
startGraphRepairs
|
|
2060
|
+
};
|
|
2061
|
+
}
|
|
2062
|
+
async function switchInTurn(turns, turn) {
|
|
2063
|
+
let swapped = false;
|
|
2064
|
+
await checkpointInTurn(turns, {
|
|
2065
|
+
joinsInFlight: false,
|
|
2066
|
+
mayStart: turn.mayStart,
|
|
2067
|
+
perform: async () => {
|
|
2068
|
+
swapped = turn.swap();
|
|
2069
|
+
if (swapped) await turn.checkpoint();
|
|
2070
|
+
},
|
|
2071
|
+
afterSettling: turn.afterSettling
|
|
2072
|
+
});
|
|
2073
|
+
return swapped;
|
|
2074
|
+
}
|
|
2075
|
+
async function checkpointInTurn(turns, turn) {
|
|
2076
|
+
if (turn.joinsInFlight && turns.checkpointInFlight !== null) return turns.checkpointInFlight;
|
|
2077
|
+
while (turns.checkpointInFlight !== null) await turns.checkpointInFlight.catch(() => void 0);
|
|
2078
|
+
if (!turn.mayStart()) return;
|
|
2079
|
+
const run = turn.perform().finally(() => {
|
|
2080
|
+
turns.checkpointInFlight = null;
|
|
2081
|
+
turn.afterSettling();
|
|
2082
|
+
});
|
|
2083
|
+
turns.checkpointInFlight = run;
|
|
2084
|
+
return run;
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
// src/persistence/durability/directory-watch.ts
|
|
2088
|
+
function unrefInterval(tick, intervalMs) {
|
|
2089
|
+
const timer = setInterval(tick, intervalMs);
|
|
2090
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
2091
|
+
return timer;
|
|
2092
|
+
}
|
|
2093
|
+
function createDirectoryWatch(directory, isStopped, onLost) {
|
|
2094
|
+
let expected = null;
|
|
2095
|
+
let timer = null;
|
|
2096
|
+
let lock = null;
|
|
2097
|
+
function cannotCheck(err) {
|
|
2098
|
+
return new NarsilError(
|
|
2099
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2100
|
+
`The engine cannot check the durability directory "${directory.root}", so it cannot confirm that a write is durable`,
|
|
2101
|
+
{ directory: directory.root, cause: err instanceof Error ? err.message : String(err) }
|
|
2102
|
+
);
|
|
2103
|
+
}
|
|
2104
|
+
async function currentIdentity() {
|
|
2105
|
+
try {
|
|
2106
|
+
return await directory.identity?.() ?? null;
|
|
2107
|
+
} catch (err) {
|
|
2108
|
+
return cannotCheck(err);
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
async function lockTakenOver() {
|
|
2112
|
+
if (lock === null) return null;
|
|
2113
|
+
try {
|
|
2114
|
+
if (await lock.holds()) return null;
|
|
2115
|
+
} catch (err) {
|
|
2116
|
+
return cannotCheck(err);
|
|
2117
|
+
}
|
|
2118
|
+
return new NarsilError(
|
|
2119
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2120
|
+
`The lock file of the durability directory "${directory.root}" records another engine, which may be writing the same log`,
|
|
2121
|
+
{ directory: directory.root }
|
|
2122
|
+
);
|
|
2123
|
+
}
|
|
2124
|
+
async function verify() {
|
|
2125
|
+
if (directory.identity === void 0 || isStopped()) return true;
|
|
2126
|
+
const current = await currentIdentity();
|
|
2127
|
+
if (current instanceof Error) {
|
|
2128
|
+
onLost(current);
|
|
2129
|
+
return false;
|
|
2130
|
+
}
|
|
2131
|
+
if (expected === null) {
|
|
2132
|
+
expected = current;
|
|
2133
|
+
} else if (current !== expected) {
|
|
2134
|
+
onLost(
|
|
2135
|
+
new NarsilError(
|
|
2136
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2137
|
+
`The durability directory "${directory.root}" is missing or is a different directory from the one that this instance opens at start-up, so a write to it is not durable`,
|
|
2138
|
+
{ directory: directory.root }
|
|
2139
|
+
)
|
|
2140
|
+
);
|
|
2141
|
+
return false;
|
|
2142
|
+
}
|
|
2143
|
+
const takenOver = await lockTakenOver();
|
|
2144
|
+
if (takenOver === null) return true;
|
|
2145
|
+
onLost(takenOver);
|
|
2146
|
+
return false;
|
|
2147
|
+
}
|
|
2148
|
+
function stop() {
|
|
2149
|
+
if (timer !== null) {
|
|
2150
|
+
clearInterval(timer);
|
|
2151
|
+
timer = null;
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
return {
|
|
2155
|
+
async claim() {
|
|
2156
|
+
if (lock === null && directory.lock !== void 0) lock = await directory.lock();
|
|
2157
|
+
},
|
|
2158
|
+
async release() {
|
|
2159
|
+
const held = lock;
|
|
2160
|
+
lock = null;
|
|
2161
|
+
await held?.release();
|
|
2162
|
+
},
|
|
2163
|
+
verify,
|
|
2164
|
+
async verifyOnce() {
|
|
2165
|
+
if (expected === null && directory.identity !== void 0) await verify();
|
|
2166
|
+
},
|
|
2167
|
+
start() {
|
|
2168
|
+
if (directory.identity === void 0 || timer !== null || isStopped()) return;
|
|
2169
|
+
timer = unrefInterval(() => void verify(), DIRECTORY_IDENTITY_CHECK_INTERVAL_MS);
|
|
2170
|
+
},
|
|
2171
|
+
stop
|
|
2172
|
+
};
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
// src/persistence/durability/held-checkpoints.ts
|
|
2176
|
+
function failedPartitionIds(indexState) {
|
|
2177
|
+
const failed = [];
|
|
2178
|
+
for (const [partitionId, partition] of indexState.partitions) if (partition.failed !== null) failed.push(partitionId);
|
|
2179
|
+
return failed;
|
|
2180
|
+
}
|
|
2181
|
+
function createHeldCheckpoints(hooks) {
|
|
2182
|
+
const reported = /* @__PURE__ */ new WeakSet();
|
|
2183
|
+
function heldReport(indexName, indexState) {
|
|
2184
|
+
const partitionIds = failedPartitionIds(indexState);
|
|
2185
|
+
if (partitionIds.length === 0 || hooks.getVectorIndexes(indexName).size === 0) return null;
|
|
2186
|
+
return {
|
|
2187
|
+
indexName,
|
|
2188
|
+
partitionIds,
|
|
2189
|
+
recordsSinceCheckpoint: indexState.mutationsSinceCheckpoint,
|
|
2190
|
+
documentBytesSinceCheckpoint: indexState.documentBytesSinceCheckpoint
|
|
2191
|
+
};
|
|
2192
|
+
}
|
|
2193
|
+
return {
|
|
2194
|
+
refuse(indexName, indexState) {
|
|
2195
|
+
const partitionIds = indexState === void 0 ? [] : failedPartitionIds(indexState);
|
|
2196
|
+
if (partitionIds.length === 0) return;
|
|
2197
|
+
throw new NarsilError(
|
|
2198
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2199
|
+
`Index "${indexName}" cannot take a checkpoint that covers every acknowledged write, because partitions ${partitionIds.join(", ")} failed and have not recovered`,
|
|
2200
|
+
{ indexName, partitionIds }
|
|
2201
|
+
);
|
|
2202
|
+
},
|
|
2203
|
+
holds(indexName, indexState) {
|
|
2204
|
+
const held = heldReport(indexName, indexState);
|
|
2205
|
+
if (held === null) return false;
|
|
2206
|
+
const unreported = held.partitionIds.some((partitionId) => {
|
|
2207
|
+
const partition = indexState.partitions.get(partitionId);
|
|
2208
|
+
return partition !== void 0 && !reported.has(partition);
|
|
2209
|
+
});
|
|
2210
|
+
if (unreported) {
|
|
2211
|
+
for (const partitionId of held.partitionIds) {
|
|
2212
|
+
const partition = indexState.partitions.get(partitionId);
|
|
2213
|
+
if (partition !== void 0) reported.add(partition);
|
|
2214
|
+
}
|
|
2215
|
+
hooks.onCheckpointHeld?.(held);
|
|
2216
|
+
}
|
|
2217
|
+
return true;
|
|
2218
|
+
},
|
|
2219
|
+
report(indexes) {
|
|
2220
|
+
const held = [];
|
|
2221
|
+
for (const [indexName, indexState] of indexes) {
|
|
2222
|
+
const entry = heldReport(indexName, indexState);
|
|
2223
|
+
if (entry !== null) held.push(entry);
|
|
2224
|
+
}
|
|
2225
|
+
return held;
|
|
2226
|
+
}
|
|
2227
|
+
};
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
// src/persistence/durability/manager-state.ts
|
|
2231
|
+
function closePartitionWriter(partition) {
|
|
2232
|
+
return partition.failed === null ? partition.walWriter.close() : partition.walWriter.abandon();
|
|
2233
|
+
}
|
|
2234
|
+
async function drainIndexStateForUnload(indexState, pendingMetadataWrite, closeWalWriter) {
|
|
2235
|
+
indexState.unloading = true;
|
|
2236
|
+
try {
|
|
2237
|
+
if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
|
|
2238
|
+
await pendingMetadataWrite;
|
|
2239
|
+
for (const partition of indexState.partitions.values()) {
|
|
2240
|
+
await closeWalWriter(closePartitionWriter(partition));
|
|
2241
|
+
}
|
|
2242
|
+
} catch (error) {
|
|
2243
|
+
indexState.unloading = false;
|
|
2244
|
+
throw error;
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2248
|
+
// src/persistence/durability/metadata-write-queue.ts
|
|
2249
|
+
function createMetadataWriteQueue() {
|
|
2250
|
+
const writes = /* @__PURE__ */ new Map();
|
|
2251
|
+
return {
|
|
2252
|
+
queue(indexName, write) {
|
|
2253
|
+
const previous = writes.get(indexName) ?? Promise.resolve();
|
|
2254
|
+
const queued = previous.then(write);
|
|
2255
|
+
writes.set(
|
|
2256
|
+
indexName,
|
|
2257
|
+
queued.catch(() => void 0)
|
|
2258
|
+
);
|
|
2259
|
+
return queued;
|
|
2260
|
+
},
|
|
2261
|
+
pending: (indexName) => writes.get(indexName),
|
|
2262
|
+
forget(indexName) {
|
|
2263
|
+
writes.delete(indexName);
|
|
2264
|
+
}
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
|
|
2268
|
+
// src/persistence/durability/mutation-batch.ts
|
|
2269
|
+
function toError4(err) {
|
|
2270
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
2271
|
+
}
|
|
2272
|
+
function requireAscendingSeqNo(record, seqNo, previousSeqNo) {
|
|
2273
|
+
if (seqNo > previousSeqNo) return;
|
|
2274
|
+
throw new NarsilError(
|
|
2275
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2276
|
+
`Log record ${seqNo} for partition ${record.partitionId} of index "${record.indexName}" does not follow record ${previousSeqNo}`,
|
|
2277
|
+
{ indexName: record.indexName, partitionId: record.partitionId, seqNo, previousSeqNo }
|
|
2278
|
+
);
|
|
2279
|
+
}
|
|
2280
|
+
function failUnlessAlreadyFailed(outcomes, count, error) {
|
|
2281
|
+
for (let i = 0; i < count; i++) {
|
|
2282
|
+
const outcome = outcomes[i];
|
|
2283
|
+
if (outcome === void 0 || outcome.ok) {
|
|
2284
|
+
outcomes[i] = { ok: false, error };
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
function failPartition(partition, address, deps, err) {
|
|
2289
|
+
const error = toError4(err);
|
|
2290
|
+
if (partition.failed !== null) return partition.failed;
|
|
2291
|
+
partition.failed = error;
|
|
2292
|
+
if (!deps.failsPartitionAlone(address.indexName, address.partitionId, error)) deps.markFatal(error);
|
|
2293
|
+
return error;
|
|
2294
|
+
}
|
|
2295
|
+
async function commitEveryPartition(indexes, deps) {
|
|
2296
|
+
for (const [indexName, indexState] of indexes) {
|
|
2297
|
+
for (const [partitionId, partition] of indexState.partitions) {
|
|
2298
|
+
if (partition.failed !== null || deps.fatalError() !== null) continue;
|
|
2299
|
+
try {
|
|
2300
|
+
await partition.walWriter.commit();
|
|
2301
|
+
} catch (err) {
|
|
2302
|
+
failPartition(partition, { indexName, partitionId }, deps, err);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
function partitionFailureFor(indexName, deps) {
|
|
2308
|
+
return (partitionId, partition, error) => failPartition(partition, { indexName, partitionId }, deps, error);
|
|
2309
|
+
}
|
|
2310
|
+
async function recordBatchesByPartition(records, partitionOf, deps) {
|
|
2311
|
+
const positionsByPartition = /* @__PURE__ */ new Map();
|
|
2312
|
+
for (let i = 0; i < records.length; i++) {
|
|
2313
|
+
const partition = partitionOf(records[i]);
|
|
2314
|
+
const positions = positionsByPartition.get(partition);
|
|
2315
|
+
if (positions === void 0) {
|
|
2316
|
+
positionsByPartition.set(partition, [i]);
|
|
2317
|
+
} else {
|
|
2318
|
+
positions.push(i);
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
const outcomes = new Array(records.length);
|
|
2322
|
+
await Promise.all(
|
|
2323
|
+
[...positionsByPartition].map(async ([partition, positions]) => {
|
|
2324
|
+
const partitionOutcomes = await recordPartitionBatch(
|
|
2325
|
+
partition,
|
|
2326
|
+
positions.map((position) => records[position]),
|
|
2327
|
+
deps
|
|
2328
|
+
);
|
|
2329
|
+
for (let i = 0; i < positions.length; i++) {
|
|
2330
|
+
outcomes[positions[i]] = partitionOutcomes[i];
|
|
2331
|
+
}
|
|
2332
|
+
})
|
|
2333
|
+
);
|
|
2334
|
+
return outcomes;
|
|
2335
|
+
}
|
|
2336
|
+
async function recordPartitionBatch(partition, records, deps) {
|
|
2337
|
+
const outcomes = new Array(records.length);
|
|
2338
|
+
let appendedCount = 0;
|
|
2339
|
+
let commitsHere = false;
|
|
2340
|
+
const appended = partition.appendChain.then(async () => {
|
|
2341
|
+
const fatal = deps.fatalError();
|
|
2342
|
+
if (fatal !== null) {
|
|
2343
|
+
throw fatal;
|
|
2344
|
+
}
|
|
2345
|
+
if (partition.failed !== null) {
|
|
2346
|
+
throw partition.failed;
|
|
2347
|
+
}
|
|
2348
|
+
const applied = [];
|
|
2349
|
+
for (let i = 0; i < records.length; i++) {
|
|
2350
|
+
if (turnsAreBudgeted && turnIsSpent()) await yieldTurn();
|
|
2351
|
+
try {
|
|
2352
|
+
await records[i].apply();
|
|
2353
|
+
applied.push(i);
|
|
2354
|
+
} catch (err) {
|
|
2355
|
+
outcomes[i] = { ok: false, error: err };
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
if (applied.length === 0) {
|
|
2359
|
+
return;
|
|
2360
|
+
}
|
|
2361
|
+
let lastSeqNo = partition.appliedSeqNo;
|
|
2362
|
+
const entries = [];
|
|
2363
|
+
try {
|
|
2364
|
+
for (const i of applied) {
|
|
2365
|
+
if (turnsAreBudgeted && turnIsSpent()) await yieldTurn();
|
|
2366
|
+
let numbered;
|
|
2367
|
+
try {
|
|
2368
|
+
numbered = deps.numberEntry(records[i]);
|
|
2369
|
+
} catch (err) {
|
|
2370
|
+
outcomes[i] = { ok: false, error: err };
|
|
2371
|
+
continue;
|
|
2372
|
+
}
|
|
2373
|
+
if (numbered === null) {
|
|
2374
|
+
lastSeqNo = partition.seqOwner.next();
|
|
2375
|
+
entries.push(deps.buildEntry(records[i], lastSeqNo));
|
|
2376
|
+
} else {
|
|
2377
|
+
requireAscendingSeqNo(records[i], numbered.seqNo, lastSeqNo);
|
|
2378
|
+
partition.seqOwner.advanceTo(numbered.seqNo, numbered.primaryTerm);
|
|
2379
|
+
lastSeqNo = numbered.seqNo;
|
|
2380
|
+
entries.push(numbered);
|
|
2381
|
+
}
|
|
2382
|
+
outcomes[i] = { ok: true, seqNo: lastSeqNo };
|
|
2383
|
+
if (!deps.defersCommit(records[i])) commitsHere = true;
|
|
2384
|
+
}
|
|
2385
|
+
if (entries.length > 0) await partition.walWriter.appendAll(entries);
|
|
2386
|
+
} catch (err) {
|
|
2387
|
+
throw failPartition(partition, records[0], deps, err);
|
|
2388
|
+
}
|
|
2389
|
+
partition.appliedSeqNo = lastSeqNo;
|
|
2390
|
+
appendedCount = entries.length;
|
|
2391
|
+
});
|
|
2392
|
+
partition.appendChain = appended.catch(() => void 0);
|
|
2393
|
+
try {
|
|
2394
|
+
await appended;
|
|
2395
|
+
if (deps.syncEachBatch && appendedCount > 0 && commitsHere) {
|
|
2396
|
+
try {
|
|
2397
|
+
await partition.walWriter.commit();
|
|
2398
|
+
} catch (err) {
|
|
2399
|
+
throw failPartition(partition, records[0], deps, err);
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
} catch (err) {
|
|
2403
|
+
failUnlessAlreadyFailed(outcomes, records.length, toError4(err));
|
|
2404
|
+
}
|
|
2405
|
+
return outcomes;
|
|
2406
|
+
}
|
|
2407
|
+
async function commitAppended(partition, address, deps) {
|
|
2408
|
+
await partition.appendChain;
|
|
2409
|
+
const failed = partition.failed ?? deps.fatalError();
|
|
2410
|
+
if (failed !== null) throw failed;
|
|
2411
|
+
try {
|
|
2412
|
+
await partition.walWriter.commit();
|
|
2413
|
+
} catch (err) {
|
|
2414
|
+
throw failPartition(partition, address, deps, err);
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
|
|
2418
|
+
// src/persistence/durability/mutation-entry.ts
|
|
2419
|
+
function buildMutationEntry(record, seqNo) {
|
|
2420
|
+
return buildEntry({
|
|
2421
|
+
seqNo,
|
|
2422
|
+
primaryTerm: SINGLE_NODE_PRIMARY_TERM,
|
|
2423
|
+
operation: record.operation,
|
|
2424
|
+
partitionId: record.partitionId,
|
|
2425
|
+
indexName: record.indexName,
|
|
2426
|
+
documentId: record.documentId,
|
|
2427
|
+
document: record.document
|
|
2428
|
+
});
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
// src/persistence/durability/partition-failure.ts
|
|
2432
|
+
function createPartitionFailureRouting() {
|
|
2433
|
+
let onFailed = null;
|
|
2434
|
+
return {
|
|
2435
|
+
isolate(handler) {
|
|
2436
|
+
onFailed = handler;
|
|
2437
|
+
},
|
|
2438
|
+
failsAlone(indexName, partitionId, error) {
|
|
2439
|
+
if (onFailed === null) return false;
|
|
2440
|
+
onFailed(indexName, partitionId, error);
|
|
2441
|
+
return true;
|
|
2442
|
+
}
|
|
2443
|
+
};
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
// src/persistence/durability/group-commit.ts
|
|
2447
|
+
function createGroupCommitCoordinator(sync) {
|
|
2448
|
+
let inFlight = null;
|
|
2449
|
+
let next = null;
|
|
2450
|
+
function makeBatch() {
|
|
2451
|
+
let resolve = () => void 0;
|
|
2452
|
+
let reject = () => void 0;
|
|
2453
|
+
const promise = new Promise((res, rej) => {
|
|
2454
|
+
resolve = res;
|
|
2455
|
+
reject = rej;
|
|
2456
|
+
});
|
|
2457
|
+
return { promise, resolve, reject, waiters: 0 };
|
|
2458
|
+
}
|
|
2459
|
+
function drain() {
|
|
2460
|
+
if (inFlight !== null || next === null) {
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
const batch = next;
|
|
2464
|
+
next = null;
|
|
2465
|
+
inFlight = sync().then(() => {
|
|
2466
|
+
batch.resolve();
|
|
2467
|
+
}).catch((err) => {
|
|
2468
|
+
batch.reject(err);
|
|
2469
|
+
}).finally(() => {
|
|
2470
|
+
inFlight = null;
|
|
2471
|
+
drain();
|
|
2472
|
+
});
|
|
2473
|
+
}
|
|
2474
|
+
return {
|
|
2475
|
+
commit() {
|
|
2476
|
+
if (next === null) {
|
|
2477
|
+
next = makeBatch();
|
|
2478
|
+
}
|
|
2479
|
+
next.waiters += 1;
|
|
2480
|
+
const promise = next.promise;
|
|
2481
|
+
drain();
|
|
2482
|
+
return promise;
|
|
2483
|
+
},
|
|
2484
|
+
get pendingCount() {
|
|
2485
|
+
return next === null ? 0 : next.waiters;
|
|
2486
|
+
}
|
|
2487
|
+
};
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
// src/persistence/durability/marker-writer.ts
|
|
2491
|
+
function markerKey(indexName, partitionId) {
|
|
2492
|
+
return `${indexName}/wal/${partitionId}/commit`;
|
|
2493
|
+
}
|
|
2494
|
+
async function createMarkerWriter(directory, indexName, partitionId) {
|
|
2495
|
+
const key = markerKey(indexName, partitionId);
|
|
2496
|
+
const handle = await directory.markerHandle(key);
|
|
2497
|
+
const existing = await handle.read();
|
|
2498
|
+
const current = readCommitMarker(existing);
|
|
2499
|
+
const createdFresh = existing.length === 0;
|
|
2500
|
+
let lastSlot = current?.slotIndex ?? null;
|
|
2501
|
+
let writeSeq = current?.state.writeSeq ?? 0;
|
|
2502
|
+
return {
|
|
2503
|
+
created: createdFresh,
|
|
2504
|
+
existingHighestDurableSeqNo: current?.state.highestDurableSeqNo ?? 0,
|
|
2505
|
+
async commit(state) {
|
|
2506
|
+
const slotIndex = nextMarkerSlot(lastSlot);
|
|
2507
|
+
writeSeq += 1;
|
|
2508
|
+
const slot = encodeMarkerSlot({ ...state, writeSeq });
|
|
2509
|
+
await handle.writeSlot(markerSlotOffset(slotIndex), slot);
|
|
2510
|
+
lastSlot = slotIndex;
|
|
2511
|
+
},
|
|
2512
|
+
async close() {
|
|
2513
|
+
await handle.close();
|
|
2514
|
+
}
|
|
2515
|
+
};
|
|
2516
|
+
}
|
|
2517
|
+
|
|
2518
|
+
// src/persistence/durability/wal-writer.ts
|
|
2519
|
+
var SEGMENT_TAIL_PATTERN = /^\d{16}$/;
|
|
2520
|
+
function segmentKey(indexName, partitionId, startSeqNo) {
|
|
2521
|
+
const padded2 = startSeqNo.toString().padStart(16, "0");
|
|
2522
|
+
return `${indexName}/wal/${partitionId}/${padded2}`;
|
|
2523
|
+
}
|
|
2524
|
+
function parseSegmentStartSeqNo2(key) {
|
|
2525
|
+
const tail = key.slice(key.lastIndexOf("/") + 1);
|
|
2526
|
+
if (!SEGMENT_TAIL_PATTERN.test(tail)) {
|
|
2527
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Malformed WAL segment key: "${key}"`, { key });
|
|
2528
|
+
}
|
|
2529
|
+
const value = Number.parseInt(tail, 10);
|
|
2530
|
+
if (!Number.isSafeInteger(value)) {
|
|
2531
|
+
throw new NarsilError(
|
|
2532
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
2533
|
+
`WAL segment start sequence number is out of range: "${key}"`,
|
|
2534
|
+
{
|
|
2535
|
+
key
|
|
2536
|
+
}
|
|
2537
|
+
);
|
|
2538
|
+
}
|
|
2539
|
+
return value;
|
|
2540
|
+
}
|
|
2541
|
+
function createWalWriter(directory, config) {
|
|
2542
|
+
const segmentMaxBytes = config.segmentMaxBytes ?? DEFAULT_SEGMENT_MAX_BYTES;
|
|
2543
|
+
const commitKey = `${config.indexName}/wal/${config.partitionId}/commit`;
|
|
2544
|
+
const preparedKey = nextSegmentKey(config.indexName, config.partitionId);
|
|
2545
|
+
let nextSegmentPrepared = false;
|
|
2546
|
+
let handle = null;
|
|
2547
|
+
let activeKey = null;
|
|
2548
|
+
let activeStartSeqNo = 0;
|
|
2549
|
+
let activeBytes = 0;
|
|
2550
|
+
let highestAppendedSeqNo = 0;
|
|
2551
|
+
let highestDurableSeqNo = 0;
|
|
2552
|
+
let markerWriter = null;
|
|
2553
|
+
let durabilityLock = Promise.resolve();
|
|
2554
|
+
const coordinator = createGroupCommitCoordinator(() => withDurabilityLock(flushActiveSegment));
|
|
2555
|
+
function withDurabilityLock(fn) {
|
|
2556
|
+
const result = durabilityLock.then(fn);
|
|
2557
|
+
durabilityLock = result.then(
|
|
2558
|
+
() => void 0,
|
|
2559
|
+
() => void 0
|
|
2560
|
+
);
|
|
2561
|
+
return result;
|
|
2562
|
+
}
|
|
2563
|
+
async function getMarkerWriter() {
|
|
2564
|
+
if (markerWriter === null) {
|
|
2565
|
+
const writer = await createMarkerWriter(directory, config.indexName, config.partitionId);
|
|
2566
|
+
markerWriter = writer;
|
|
2567
|
+
if (writer.existingHighestDurableSeqNo > highestDurableSeqNo) {
|
|
2568
|
+
highestDurableSeqNo = writer.existingHighestDurableSeqNo;
|
|
2569
|
+
}
|
|
2570
|
+
if (writer.created) {
|
|
2571
|
+
await directory.syncDirectoryOf(commitKey);
|
|
2572
|
+
}
|
|
2573
|
+
}
|
|
2574
|
+
return markerWriter;
|
|
2575
|
+
}
|
|
2576
|
+
async function flushActiveSegment() {
|
|
2577
|
+
if (handle === null) {
|
|
2578
|
+
return;
|
|
2579
|
+
}
|
|
2580
|
+
const writer = await getMarkerWriter();
|
|
2581
|
+
const durableByteLength = activeBytes;
|
|
2582
|
+
const activeSegmentSeqNo = activeStartSeqNo;
|
|
2583
|
+
const durableSeqNo = Math.max(highestAppendedSeqNo, highestDurableSeqNo);
|
|
2584
|
+
await handle.sync();
|
|
2585
|
+
await writer.commit({ activeSegmentSeqNo, durableByteLength, highestDurableSeqNo: durableSeqNo });
|
|
2586
|
+
if (durableSeqNo > highestDurableSeqNo) {
|
|
2587
|
+
highestDurableSeqNo = durableSeqNo;
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
async function openSegmentByKey(key) {
|
|
2591
|
+
const nextHandle = await directory.appendHandle(key);
|
|
2592
|
+
const existingSize = await nextHandle.size();
|
|
2593
|
+
let createdNew = false;
|
|
2594
|
+
if (existingSize === 0) {
|
|
2595
|
+
await nextHandle.append(writeSegmentHeader());
|
|
2596
|
+
activeBytes = SEGMENT_HEADER_SIZE;
|
|
2597
|
+
createdNew = true;
|
|
2598
|
+
} else {
|
|
2599
|
+
activeBytes = existingSize;
|
|
2600
|
+
}
|
|
2601
|
+
handle = nextHandle;
|
|
2602
|
+
activeKey = key;
|
|
2603
|
+
activeStartSeqNo = parseSegmentStartSeqNo2(key);
|
|
2604
|
+
if (createdNew) {
|
|
2605
|
+
await directory.syncDirectoryOf(key);
|
|
2606
|
+
}
|
|
2607
|
+
return createdNew;
|
|
2608
|
+
}
|
|
2609
|
+
async function findActiveSegmentKey() {
|
|
2610
|
+
const prefix = `${config.indexName}/wal/${config.partitionId}/`;
|
|
2611
|
+
const keys = (await directory.list(prefix)).filter((k) => SEGMENT_TAIL_PATTERN.test(k.slice(prefix.length))).sort(compareCodePoints);
|
|
2612
|
+
return keys.length > 0 ? keys[keys.length - 1] : null;
|
|
2613
|
+
}
|
|
2614
|
+
async function ensureSegment(seqNo) {
|
|
2615
|
+
if (handle !== null) {
|
|
2616
|
+
return;
|
|
2617
|
+
}
|
|
2618
|
+
const existing = await findActiveSegmentKey();
|
|
2619
|
+
const createdNew = await openSegmentByKey(existing ?? segmentKey(config.indexName, config.partitionId, seqNo));
|
|
2620
|
+
if (createdNew) {
|
|
2621
|
+
await withDurabilityLock(flushActiveSegment);
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2624
|
+
async function rollUnderLock(startSeqNo) {
|
|
2625
|
+
if (handle !== null) {
|
|
2626
|
+
await flushActiveSegment();
|
|
2627
|
+
await handle.close();
|
|
2628
|
+
handle = null;
|
|
2629
|
+
}
|
|
2630
|
+
await openSegmentByKey(segmentKey(config.indexName, config.partitionId, startSeqNo));
|
|
2631
|
+
await flushActiveSegment();
|
|
2632
|
+
}
|
|
2633
|
+
async function rollToNewSegment(startSeqNo) {
|
|
2634
|
+
await withDurabilityLock(() => rollUnderLock(startSeqNo));
|
|
2635
|
+
}
|
|
2636
|
+
function activeSegmentHoldsRecords() {
|
|
2637
|
+
return handle !== null && activeBytes > SEGMENT_HEADER_SIZE && highestAppendedSeqNo >= activeStartSeqNo;
|
|
2638
|
+
}
|
|
2639
|
+
async function prepareNextSegment() {
|
|
2640
|
+
const prepared = await directory.appendHandle(preparedKey);
|
|
2641
|
+
try {
|
|
2642
|
+
if (await prepared.size() > 0) await prepared.truncate(0);
|
|
2643
|
+
await prepared.append(writeSegmentHeader());
|
|
2644
|
+
await prepared.sync();
|
|
2645
|
+
nextSegmentPrepared = true;
|
|
2646
|
+
} finally {
|
|
2647
|
+
await prepared.close();
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
async function swapToPreparedSegment(sealed) {
|
|
2651
|
+
const writer = await getMarkerWriter();
|
|
2652
|
+
const startSeqNo = highestAppendedSeqNo + 1;
|
|
2653
|
+
const durableSeqNo = Math.max(highestAppendedSeqNo, highestDurableSeqNo);
|
|
2654
|
+
const key = segmentKey(config.indexName, config.partitionId, startSeqNo);
|
|
2655
|
+
await Promise.all([sealed.sync(), directory.move(preparedKey, key)]);
|
|
2656
|
+
handle = await directory.appendHandle(key);
|
|
2657
|
+
activeKey = key;
|
|
2658
|
+
activeStartSeqNo = startSeqNo;
|
|
2659
|
+
activeBytes = SEGMENT_HEADER_SIZE;
|
|
2660
|
+
await writer.commit({
|
|
2661
|
+
activeSegmentSeqNo: startSeqNo,
|
|
2662
|
+
durableByteLength: SEGMENT_HEADER_SIZE,
|
|
2663
|
+
highestDurableSeqNo: durableSeqNo
|
|
2664
|
+
});
|
|
2665
|
+
highestDurableSeqNo = durableSeqNo;
|
|
2666
|
+
await sealed.close();
|
|
2667
|
+
}
|
|
2668
|
+
async function maybeRoll(nextSeqNo, appendedBytes) {
|
|
2669
|
+
if (handle === null || activeBytes <= SEGMENT_HEADER_SIZE) {
|
|
2670
|
+
return;
|
|
2671
|
+
}
|
|
2672
|
+
if (activeBytes >= segmentMaxBytes || activeBytes + appendedBytes > segmentMaxBytes) {
|
|
2673
|
+
await rollToNewSegment(nextSeqNo);
|
|
2674
|
+
}
|
|
2675
|
+
}
|
|
2676
|
+
function joinFrames(frames) {
|
|
2677
|
+
if (frames.length === 1) {
|
|
2678
|
+
return frames[0];
|
|
2679
|
+
}
|
|
2680
|
+
let totalBytes = 0;
|
|
2681
|
+
for (const frame of frames) {
|
|
2682
|
+
totalBytes += frame.length;
|
|
2683
|
+
}
|
|
2684
|
+
const joined = new Uint8Array(totalBytes);
|
|
2685
|
+
let offset = 0;
|
|
2686
|
+
for (const frame of frames) {
|
|
2687
|
+
joined.set(frame, offset);
|
|
2688
|
+
offset += frame.length;
|
|
2689
|
+
}
|
|
2690
|
+
return joined;
|
|
2691
|
+
}
|
|
2692
|
+
function framesThatFit(frames, start) {
|
|
2693
|
+
const room = segmentMaxBytes - activeBytes;
|
|
2694
|
+
let end = start + 1;
|
|
2695
|
+
let bytes = frames[start].length;
|
|
2696
|
+
while (end < frames.length && bytes + frames[end].length <= room) {
|
|
2697
|
+
bytes += frames[end].length;
|
|
2698
|
+
end += 1;
|
|
2699
|
+
}
|
|
2700
|
+
return end;
|
|
2701
|
+
}
|
|
2702
|
+
async function appendGroup(frames, entries) {
|
|
2703
|
+
const activeHandle = handle;
|
|
2704
|
+
if (activeHandle === null) {
|
|
2705
|
+
throw new NarsilError(
|
|
2706
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2707
|
+
`WAL segment for "${config.indexName}" partition ${config.partitionId} is not open`,
|
|
2708
|
+
{ indexName: config.indexName, partitionId: config.partitionId }
|
|
2709
|
+
);
|
|
2710
|
+
}
|
|
2711
|
+
const joined = joinFrames([...frames]);
|
|
2712
|
+
if (activeBytes + joined.length > MAX_FILE_BYTES) {
|
|
2713
|
+
throw new NarsilError(
|
|
2714
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2715
|
+
`A log record of ${joined.length} bytes would carry WAL segment "${activeKey}" past the file size limit of ${MAX_FILE_BYTES} bytes`,
|
|
2716
|
+
{ indexName: config.indexName, partitionId: config.partitionId, bytes: joined.length }
|
|
2717
|
+
);
|
|
2718
|
+
}
|
|
2719
|
+
await activeHandle.append(joined);
|
|
2720
|
+
activeBytes += joined.length;
|
|
2721
|
+
const lastSeqNo = entries[entries.length - 1].seqNo;
|
|
2722
|
+
if (lastSeqNo > highestAppendedSeqNo) {
|
|
2723
|
+
highestAppendedSeqNo = lastSeqNo;
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
async function appendFrames(entries) {
|
|
2727
|
+
if (entries.length === 0) {
|
|
2728
|
+
return;
|
|
2729
|
+
}
|
|
2730
|
+
const frames = [];
|
|
2731
|
+
for (const entry of entries) {
|
|
2732
|
+
if (turnsAreBudgeted && turnIsSpent()) await yieldTurn();
|
|
2733
|
+
frames.push(frameRecord(entry));
|
|
2734
|
+
}
|
|
2735
|
+
await ensureSegment(entries[0].seqNo);
|
|
2736
|
+
let start = 0;
|
|
2737
|
+
while (start < frames.length) {
|
|
2738
|
+
await maybeRoll(entries[start].seqNo, frames[start].length);
|
|
2739
|
+
const end = framesThatFit(frames, start);
|
|
2740
|
+
await appendGroup(frames.slice(start, end), entries.slice(start, end));
|
|
2741
|
+
start = end;
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
return {
|
|
2745
|
+
async appendAll(entries) {
|
|
2746
|
+
await appendFrames(entries);
|
|
2747
|
+
},
|
|
2748
|
+
async appendDurable(entry) {
|
|
2749
|
+
await appendFrames([entry]);
|
|
2750
|
+
await coordinator.commit();
|
|
2751
|
+
},
|
|
2752
|
+
async commit() {
|
|
2753
|
+
await coordinator.commit();
|
|
2754
|
+
},
|
|
2755
|
+
rollToNewSegment,
|
|
2756
|
+
async prepareCheckpointRoll() {
|
|
2757
|
+
if (activeSegmentHoldsRecords()) await prepareNextSegment();
|
|
2758
|
+
},
|
|
2759
|
+
async rollForCheckpoint() {
|
|
2760
|
+
await withDurabilityLock(async () => {
|
|
2761
|
+
const sealed = handle;
|
|
2762
|
+
const prepared = nextSegmentPrepared;
|
|
2763
|
+
nextSegmentPrepared = false;
|
|
2764
|
+
if (sealed === null || !activeSegmentHoldsRecords()) return;
|
|
2765
|
+
if (prepared) await swapToPreparedSegment(sealed);
|
|
2766
|
+
else await rollUnderLock(highestAppendedSeqNo + 1);
|
|
2767
|
+
});
|
|
2768
|
+
},
|
|
2769
|
+
async close() {
|
|
2770
|
+
await withDurabilityLock(async () => {
|
|
2771
|
+
if (handle !== null) {
|
|
2772
|
+
await flushActiveSegment();
|
|
2773
|
+
await handle.close();
|
|
2774
|
+
handle = null;
|
|
2775
|
+
}
|
|
2776
|
+
if (markerWriter !== null) {
|
|
2777
|
+
await markerWriter.close();
|
|
2778
|
+
markerWriter = null;
|
|
2779
|
+
}
|
|
2780
|
+
activeKey = null;
|
|
2781
|
+
});
|
|
2782
|
+
},
|
|
2783
|
+
async abandon() {
|
|
2784
|
+
await withDurabilityLock(async () => {
|
|
2785
|
+
const closing = [handle?.close(), markerWriter?.close()];
|
|
2786
|
+
handle = null;
|
|
2787
|
+
markerWriter = null;
|
|
2788
|
+
activeKey = null;
|
|
2789
|
+
await Promise.allSettled(closing);
|
|
2790
|
+
});
|
|
2791
|
+
},
|
|
2792
|
+
get activeSegmentKey() {
|
|
2793
|
+
return activeKey;
|
|
2794
|
+
}
|
|
2795
|
+
};
|
|
2796
|
+
}
|
|
2797
|
+
|
|
2798
|
+
// src/persistence/durability/partition-registry.ts
|
|
2799
|
+
function createPartitionRegistry(directory, segmentMaxBytes) {
|
|
2800
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
2801
|
+
function indexState(indexName) {
|
|
2802
|
+
let state = indexes.get(indexName);
|
|
2803
|
+
if (state === void 0) {
|
|
2804
|
+
state = {
|
|
2805
|
+
partitions: /* @__PURE__ */ new Map(),
|
|
2806
|
+
mutationsSinceCheckpoint: 0,
|
|
2807
|
+
documentBytesSinceCheckpoint: 0,
|
|
2808
|
+
checkpointInFlight: null,
|
|
2809
|
+
unloading: false,
|
|
2810
|
+
stalledWrites: []
|
|
2811
|
+
};
|
|
2812
|
+
indexes.set(indexName, state);
|
|
2813
|
+
}
|
|
2814
|
+
return state;
|
|
2815
|
+
}
|
|
2816
|
+
function freshPartition(indexName, partitionId, position) {
|
|
2817
|
+
return {
|
|
2818
|
+
walWriter: createWalWriter(directory, { indexName, partitionId, segmentMaxBytes }),
|
|
2819
|
+
seqOwner: createSeqOwner(position.seqNo, position.primaryTerm),
|
|
2820
|
+
appendChain: Promise.resolve(),
|
|
2821
|
+
appliedSeqNo: position.seqNo,
|
|
2822
|
+
failed: null
|
|
2823
|
+
};
|
|
2824
|
+
}
|
|
2825
|
+
return {
|
|
2826
|
+
directory,
|
|
2827
|
+
indexes,
|
|
2828
|
+
indexState,
|
|
2829
|
+
freshPartition,
|
|
2830
|
+
partition(indexName, partitionId, startSeqNo, primaryTerm = SINGLE_NODE_PRIMARY_TERM) {
|
|
2831
|
+
const state = indexState(indexName);
|
|
2832
|
+
let partition = state.partitions.get(partitionId);
|
|
2833
|
+
if (partition === void 0) {
|
|
2834
|
+
partition = freshPartition(indexName, partitionId, { seqNo: startSeqNo, primaryTerm });
|
|
2835
|
+
state.partitions.set(partitionId, partition);
|
|
2836
|
+
}
|
|
2837
|
+
return partition;
|
|
2838
|
+
}
|
|
2839
|
+
};
|
|
2840
|
+
}
|
|
2841
|
+
var SNAPSHOT_BUNDLE_VERSION = 2;
|
|
2842
|
+
function invalid(message) {
|
|
2843
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Snapshot bundle ${message}`);
|
|
2844
|
+
}
|
|
2845
|
+
function vectorIndexesOf(raw) {
|
|
2846
|
+
if (raw === void 0 || raw === null) return {};
|
|
2847
|
+
if (typeof raw !== "object" || Array.isArray(raw)) invalid("vectorIndexes must be a map");
|
|
2848
|
+
const result = {};
|
|
2849
|
+
for (const [fieldPath, parts] of Object.entries(raw)) {
|
|
2850
|
+
result[fieldPath] = decodeVectorIndexParts(parts);
|
|
2851
|
+
}
|
|
2852
|
+
return result;
|
|
2853
|
+
}
|
|
2854
|
+
function checkpointOf(raw) {
|
|
2855
|
+
if (!Array.isArray(raw)) return [];
|
|
2856
|
+
const result = [];
|
|
2857
|
+
for (const entry of raw) {
|
|
2858
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
2859
|
+
const { partitionId, lastSeqNo, primaryTerm } = entry;
|
|
2860
|
+
if (typeof partitionId !== "number" || typeof lastSeqNo !== "number" || typeof primaryTerm !== "number") continue;
|
|
2861
|
+
result.push({ partitionId, lastSeqNo, primaryTerm });
|
|
2862
|
+
}
|
|
2863
|
+
return result;
|
|
2864
|
+
}
|
|
2865
|
+
async function decodeSnapshotBundle(data) {
|
|
2866
|
+
const { payloadBytes } = await unpackEnvelopeBytes(data);
|
|
2867
|
+
const raw = decode(payloadBytes);
|
|
2868
|
+
if (raw.version !== SNAPSHOT_BUNDLE_VERSION) {
|
|
2869
|
+
throw new NarsilError(
|
|
2870
|
+
ErrorCodes.ENVELOPE_VERSION_MISMATCH,
|
|
2871
|
+
`The snapshot bundle holds layout ${String(raw.version)}, which this release cannot open. Export the index with the release that wrote it and import the export`,
|
|
2872
|
+
{ version: raw.version }
|
|
2873
|
+
);
|
|
2874
|
+
}
|
|
2875
|
+
if (raw.schema === null || typeof raw.schema !== "object" || Array.isArray(raw.schema)) invalid("missing schema");
|
|
2876
|
+
requireValidFieldTypes(raw.schema);
|
|
2877
|
+
if (typeof raw.language !== "string") invalid("missing language");
|
|
2878
|
+
if (!Array.isArray(raw.partitions) || !raw.partitions.every((part) => part instanceof Uint8Array)) {
|
|
2879
|
+
invalid("missing partitions");
|
|
2880
|
+
}
|
|
2881
|
+
return {
|
|
2882
|
+
schema: raw.schema,
|
|
2883
|
+
language: raw.language,
|
|
2884
|
+
partitions: raw.partitions,
|
|
2885
|
+
vectorIndexes: vectorIndexesOf(raw.vectorIndexes),
|
|
2886
|
+
checkpoint: checkpointOf(raw.checkpoint)
|
|
2887
|
+
};
|
|
2888
|
+
}
|
|
2889
|
+
function countSnapshotBundleDocuments(bundle) {
|
|
2890
|
+
let total = 0;
|
|
2891
|
+
for (const partitionBytes of bundle.partitions)
|
|
2892
|
+
total += Object.keys(deserializePayloadV2(partitionBytes).documents).length;
|
|
2893
|
+
return total;
|
|
2894
|
+
}
|
|
2895
|
+
async function loadSnapshotBundle(bytes, deps) {
|
|
2896
|
+
const bundle = await decodeSnapshotBundle(bytes);
|
|
2897
|
+
while (deps.manager.partitionCount < bundle.partitions.length) deps.manager.addPartition();
|
|
2898
|
+
if (bundle.partitions.length > 0) deps.manager.trimPartitions(bundle.partitions.length);
|
|
2899
|
+
for (let i = 0; i < bundle.partitions.length; i += 1) {
|
|
2900
|
+
deps.manager.deserializePartition(i, deserializePayloadV2(bundle.partitions[i]));
|
|
2901
|
+
}
|
|
2902
|
+
for (const [fieldPath, parts] of Object.entries(bundle.vectorIndexes)) {
|
|
2903
|
+
const rebuildsGraph = parts.some((part) => part.graphs.some((graph) => graph.m < MIN_M));
|
|
2904
|
+
deps.vectorIndexes.get(fieldPath)?.deserialize(rebuildsGraph ? parts.map((part) => ({ ...part, graphs: [] })) : parts);
|
|
2905
|
+
}
|
|
2906
|
+
return bundle.checkpoint;
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
// src/persistence/durability/earlier-layout/manifest.ts
|
|
2910
|
+
function isKey(value) {
|
|
2911
|
+
return typeof value === "string" && value.length > 0 && !value.includes("\0");
|
|
2912
|
+
}
|
|
2913
|
+
function checkpointOf2(raw) {
|
|
2914
|
+
if (!Array.isArray(raw)) return [];
|
|
2915
|
+
const result = [];
|
|
2916
|
+
for (const entry of raw) {
|
|
2917
|
+
if (!isRecord(entry) || !isUint32(entry.partitionId)) continue;
|
|
2918
|
+
if (!isSequenceNumber(entry.lastSeqNo) || !isSequenceNumber(entry.primaryTerm)) continue;
|
|
2919
|
+
result.push({ partitionId: entry.partitionId, lastSeqNo: entry.lastSeqNo, primaryTerm: entry.primaryTerm });
|
|
2920
|
+
}
|
|
2921
|
+
return result;
|
|
2922
|
+
}
|
|
2923
|
+
function segmentsOf(raw, partitionId) {
|
|
2924
|
+
if (raw === void 0) return [];
|
|
2925
|
+
if (!Array.isArray(raw) || raw.length > MAX_SEGMENTS_PER_PARTITION) {
|
|
2926
|
+
invalidManifest("has malformed segment references", { partitionId });
|
|
2927
|
+
}
|
|
2928
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2929
|
+
return raw.map((segment) => {
|
|
2930
|
+
if (!isRecord(segment) || !isId(segment.id) || !isUint32(segment.docCount) || !isUint32(segment.tombstoneCount) || !isKey(segment.key) || seen.has(segment.id)) {
|
|
2931
|
+
invalidManifest("has an invalid segment reference", { partitionId });
|
|
2932
|
+
}
|
|
2933
|
+
seen.add(segment.id);
|
|
2934
|
+
return { id: segment.id, key: segment.key, docCount: segment.docCount, tombstoneCount: segment.tombstoneCount };
|
|
2935
|
+
});
|
|
2936
|
+
}
|
|
2937
|
+
function partitionsOf(raw) {
|
|
2938
|
+
if (!Array.isArray(raw)) invalidManifest("is missing partition entries");
|
|
2939
|
+
return raw.map((entry) => {
|
|
2940
|
+
if (!isRecord(entry) || !isUint32(entry.partitionId)) invalidManifest("has a partition without a valid id");
|
|
2941
|
+
const segments = segmentsOf(entry.segments, entry.partitionId);
|
|
2942
|
+
const nextSegmentId = entry.nextSegmentId;
|
|
2943
|
+
if (!isId(nextSegmentId) || segments.some((segment) => segment.id >= nextSegmentId)) {
|
|
2944
|
+
invalidManifest("has an invalid next segment id", { partitionId: entry.partitionId });
|
|
2945
|
+
}
|
|
2946
|
+
return { partitionId: entry.partitionId, nextSegmentId, segments };
|
|
2947
|
+
});
|
|
2948
|
+
}
|
|
2949
|
+
function vectorFileOf(raw, fieldPath) {
|
|
2950
|
+
if (!isRecord(raw) || !isId(raw.id) || !isKey(raw.key)) invalidManifest("has an invalid vector file reference");
|
|
2951
|
+
const count = raw.count;
|
|
2952
|
+
if (!isUint32(count) || count > VECTOR_FILE_MAX_VECTORS) invalidManifest(`gives "${fieldPath}" an invalid count`);
|
|
2953
|
+
const dead = raw.dead ?? null;
|
|
2954
|
+
if (dead !== null && (!(dead instanceof Uint8Array) || dead.byteLength !== deadBytesFor(count))) {
|
|
2955
|
+
invalidManifest(`marks the dead vectors of "${fieldPath}" with the wrong number of bytes`);
|
|
2956
|
+
}
|
|
2957
|
+
return { id: raw.id, key: raw.key, count, dead };
|
|
2958
|
+
}
|
|
2959
|
+
function vectorsOf(raw) {
|
|
2960
|
+
if (raw === void 0 || raw === null) return [];
|
|
2961
|
+
if (!Array.isArray(raw)) invalidManifest("has malformed vector references");
|
|
2962
|
+
return raw.map((field) => {
|
|
2963
|
+
if (!isRecord(field) || typeof field.fieldPath !== "string") invalidManifest("has an invalid vector reference");
|
|
2964
|
+
const fieldPath = field.fieldPath;
|
|
2965
|
+
if (!isId(field.nextFileId) || !Array.isArray(field.files) || !isId(field.graphGeneration)) {
|
|
2966
|
+
invalidManifest(`gives "${fieldPath}" an invalid vector reference`);
|
|
2967
|
+
}
|
|
2968
|
+
const graphKey = field.graphKey ?? null;
|
|
2969
|
+
if (graphKey !== null && (!isKey(graphKey) || field.graphGeneration === 0)) {
|
|
2970
|
+
invalidManifest(`gives "${fieldPath}" an invalid graph key`);
|
|
2971
|
+
}
|
|
2972
|
+
return {
|
|
2973
|
+
fieldPath,
|
|
2974
|
+
nextFileId: field.nextFileId,
|
|
2975
|
+
files: field.files.map((file) => vectorFileOf(file, fieldPath)),
|
|
2976
|
+
graphGeneration: field.graphGeneration,
|
|
2977
|
+
graphKey
|
|
2978
|
+
};
|
|
2979
|
+
});
|
|
2980
|
+
}
|
|
2981
|
+
function decodeEarlierManifest(raw) {
|
|
2982
|
+
const schema = raw.schema;
|
|
2983
|
+
if (!isRecord(schema)) invalidManifest("is missing its schema");
|
|
2984
|
+
requireValidFieldTypes(schema);
|
|
2985
|
+
if (typeof raw.language !== "string") invalidManifest("is missing its language");
|
|
2986
|
+
return {
|
|
2987
|
+
schema,
|
|
2988
|
+
language: raw.language,
|
|
2989
|
+
checkpoint: checkpointOf2(raw.checkpoint),
|
|
2990
|
+
partitions: partitionsOf(raw.partitions),
|
|
2991
|
+
vectors: vectorsOf(raw.vectors)
|
|
2992
|
+
};
|
|
2993
|
+
}
|
|
2994
|
+
function earlierReferencedKeys(manifest) {
|
|
2995
|
+
const keys = /* @__PURE__ */ new Set();
|
|
2996
|
+
for (const partition of manifest.partitions) {
|
|
2997
|
+
for (const segment of partition.segments) keys.add(segment.key);
|
|
2998
|
+
}
|
|
2999
|
+
for (const vector of manifest.vectors) {
|
|
3000
|
+
for (const file of vector.files) keys.add(file.key);
|
|
3001
|
+
if (vector.graphKey !== null) keys.add(vector.graphKey);
|
|
3002
|
+
}
|
|
3003
|
+
return keys;
|
|
3004
|
+
}
|
|
3005
|
+
|
|
3006
|
+
// src/persistence/durability/earlier-layout/merge.ts
|
|
3007
|
+
function mergeTimeOrderedSegments(ordered, fallback) {
|
|
3008
|
+
const winner = resolveWinners(ordered);
|
|
3009
|
+
const first = ordered[0]?.partition;
|
|
3010
|
+
const merged = {
|
|
3011
|
+
indexName: first?.indexName ?? fallback.indexName,
|
|
3012
|
+
partitionId: first?.partitionId ?? fallback.partitionId,
|
|
3013
|
+
totalPartitions: first?.totalPartitions ?? fallback.totalPartitions,
|
|
3014
|
+
language: first?.language ?? fallback.language,
|
|
3015
|
+
schema: {},
|
|
3016
|
+
docCount: 0,
|
|
3017
|
+
avgDocLength: 0,
|
|
3018
|
+
documents: /* @__PURE__ */ Object.create(null),
|
|
3019
|
+
invertedIndex: /* @__PURE__ */ Object.create(null),
|
|
3020
|
+
fieldIndexes: {
|
|
3021
|
+
numeric: /* @__PURE__ */ Object.create(null),
|
|
3022
|
+
boolean: /* @__PURE__ */ Object.create(null),
|
|
3023
|
+
enum: /* @__PURE__ */ Object.create(null),
|
|
3024
|
+
geopoint: /* @__PURE__ */ Object.create(null)
|
|
3025
|
+
},
|
|
3026
|
+
statistics: {
|
|
3027
|
+
totalDocuments: 0,
|
|
3028
|
+
totalFieldLengths: /* @__PURE__ */ Object.create(null),
|
|
3029
|
+
averageFieldLengths: /* @__PURE__ */ Object.create(null),
|
|
3030
|
+
docFrequencies: /* @__PURE__ */ Object.create(null)
|
|
3031
|
+
}
|
|
3032
|
+
};
|
|
3033
|
+
for (let segIndex = 0; segIndex < ordered.length; segIndex += 1) {
|
|
3034
|
+
const part = ordered[segIndex].partition;
|
|
3035
|
+
addSegmentSchema(merged.schema, part.schema);
|
|
3036
|
+
collectDocuments(merged, part, winner, segIndex);
|
|
3037
|
+
collectInvertedIndex(merged, part, winner, segIndex);
|
|
3038
|
+
collectFieldIndexes(merged, part, winner, segIndex);
|
|
3039
|
+
collectSurfaceForms(merged, part);
|
|
3040
|
+
}
|
|
3041
|
+
recomputeStatistics(merged);
|
|
3042
|
+
validateMergedPartition(merged);
|
|
3043
|
+
return merged;
|
|
3044
|
+
}
|
|
3045
|
+
function countLiveDocuments(ordered) {
|
|
3046
|
+
return resolveWinners(ordered).size;
|
|
3047
|
+
}
|
|
3048
|
+
function resolveWinners(ordered) {
|
|
3049
|
+
const winner = /* @__PURE__ */ new Map();
|
|
3050
|
+
for (let segIndex = 0; segIndex < ordered.length; segIndex += 1) {
|
|
3051
|
+
const segment = ordered[segIndex];
|
|
3052
|
+
for (const docId of segment.tombstones) {
|
|
3053
|
+
winner.delete(docId);
|
|
3054
|
+
}
|
|
3055
|
+
for (const docId of Object.keys(segment.partition.documents)) {
|
|
3056
|
+
winner.set(docId, segIndex);
|
|
3057
|
+
}
|
|
3058
|
+
}
|
|
3059
|
+
return winner;
|
|
3060
|
+
}
|
|
3061
|
+
function owns(winner, docId, segIndex) {
|
|
3062
|
+
return winner.get(docId) === segIndex;
|
|
3063
|
+
}
|
|
3064
|
+
function addSegmentSchema(agreed, schema) {
|
|
3065
|
+
for (const [field, type] of Object.entries(schema)) {
|
|
3066
|
+
const existing = agreed[field];
|
|
3067
|
+
if (existing !== void 0 && existing !== type) {
|
|
3068
|
+
throw new NarsilError(
|
|
3069
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
3070
|
+
`Segment schemas disagree on field "${field}": "${existing}" vs "${type}"`,
|
|
3071
|
+
{ field, existing, found: type }
|
|
3072
|
+
);
|
|
3073
|
+
}
|
|
3074
|
+
agreed[field] = type;
|
|
3075
|
+
}
|
|
3076
|
+
}
|
|
3077
|
+
function collectDocuments(merged, part, winner, segIndex) {
|
|
3078
|
+
for (const [docId, doc] of Object.entries(part.documents)) {
|
|
3079
|
+
if (owns(winner, docId, segIndex)) {
|
|
3080
|
+
merged.documents[docId] = doc;
|
|
3081
|
+
}
|
|
3082
|
+
}
|
|
3083
|
+
}
|
|
3084
|
+
function collectInvertedIndex(merged, part, winner, segIndex) {
|
|
3085
|
+
for (const [token, list] of Object.entries(part.invertedIndex)) {
|
|
3086
|
+
let target = merged.invertedIndex[token];
|
|
3087
|
+
for (const posting of list.postings) {
|
|
3088
|
+
if (!owns(winner, posting.docId, segIndex)) continue;
|
|
3089
|
+
if (target === void 0) {
|
|
3090
|
+
target = { docFrequency: 0, postings: [] };
|
|
3091
|
+
merged.invertedIndex[token] = target;
|
|
3092
|
+
}
|
|
3093
|
+
target.postings.push(posting);
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
3096
|
+
}
|
|
3097
|
+
function collectEntries(target, source, winner, segIndex) {
|
|
3098
|
+
for (const [path, entries] of Object.entries(source)) {
|
|
3099
|
+
for (const entry of entries) {
|
|
3100
|
+
if (!owns(winner, entry.docId, segIndex)) continue;
|
|
3101
|
+
let bucket = target[path];
|
|
3102
|
+
if (bucket === void 0) {
|
|
3103
|
+
bucket = [];
|
|
3104
|
+
target[path] = bucket;
|
|
3105
|
+
}
|
|
3106
|
+
bucket.push(entry);
|
|
3107
|
+
}
|
|
3108
|
+
}
|
|
3109
|
+
}
|
|
3110
|
+
function collectFieldIndexes(merged, part, winner, segIndex) {
|
|
3111
|
+
collectEntries(merged.fieldIndexes.numeric, part.fieldIndexes.numeric, winner, segIndex);
|
|
3112
|
+
collectEntries(merged.fieldIndexes.geopoint, part.fieldIndexes.geopoint, winner, segIndex);
|
|
3113
|
+
for (const [path, idx] of Object.entries(part.fieldIndexes.boolean)) {
|
|
3114
|
+
for (const [docIds, flag] of [
|
|
3115
|
+
[idx.trueDocs, true],
|
|
3116
|
+
[idx.falseDocs, false]
|
|
3117
|
+
]) {
|
|
3118
|
+
for (const docId of docIds) {
|
|
3119
|
+
if (!owns(winner, docId, segIndex)) continue;
|
|
3120
|
+
let target = merged.fieldIndexes.boolean[path];
|
|
3121
|
+
if (target === void 0) {
|
|
3122
|
+
target = { trueDocs: [], falseDocs: [] };
|
|
3123
|
+
merged.fieldIndexes.boolean[path] = target;
|
|
3124
|
+
}
|
|
3125
|
+
(flag ? target.trueDocs : target.falseDocs).push(docId);
|
|
3126
|
+
}
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
3129
|
+
for (const [path, values] of Object.entries(part.fieldIndexes.enum)) {
|
|
3130
|
+
for (const [value, docIds] of Object.entries(values)) {
|
|
3131
|
+
for (const docId of docIds) {
|
|
3132
|
+
if (!owns(winner, docId, segIndex)) continue;
|
|
3133
|
+
let target = merged.fieldIndexes.enum[path];
|
|
3134
|
+
if (target === void 0) {
|
|
3135
|
+
target = /* @__PURE__ */ Object.create(null);
|
|
3136
|
+
merged.fieldIndexes.enum[path] = target;
|
|
3137
|
+
}
|
|
3138
|
+
let bucket = target[value];
|
|
3139
|
+
if (bucket === void 0) {
|
|
3140
|
+
bucket = [];
|
|
3141
|
+
target[value] = bucket;
|
|
3142
|
+
}
|
|
3143
|
+
bucket.push(docId);
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
}
|
|
3147
|
+
}
|
|
3148
|
+
function collectSurfaceForms(merged, part) {
|
|
3149
|
+
if (!part.surfaceForms) return;
|
|
3150
|
+
let target = merged.surfaceForms;
|
|
3151
|
+
if (!target) {
|
|
3152
|
+
target = /* @__PURE__ */ Object.create(null);
|
|
3153
|
+
merged.surfaceForms = target;
|
|
3154
|
+
}
|
|
3155
|
+
for (const [surface, value] of Object.entries(part.surfaceForms)) {
|
|
3156
|
+
const count = typeof value === "number" ? value : value[0];
|
|
3157
|
+
const token = typeof value === "number" ? surface : value[1];
|
|
3158
|
+
const existing = target[surface];
|
|
3159
|
+
if (existing === void 0) {
|
|
3160
|
+
target[surface] = value;
|
|
3161
|
+
continue;
|
|
3162
|
+
}
|
|
3163
|
+
const existingCount = typeof existing === "number" ? existing : existing[0];
|
|
3164
|
+
const total = existingCount + count;
|
|
3165
|
+
target[surface] = token === surface ? total : [total, token];
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
3168
|
+
function recomputeStatistics(merged) {
|
|
3169
|
+
const totalFieldLengths = /* @__PURE__ */ Object.create(null);
|
|
3170
|
+
let totalDocuments = 0;
|
|
3171
|
+
for (const doc of Object.values(merged.documents)) {
|
|
3172
|
+
totalDocuments += 1;
|
|
3173
|
+
for (const [field, length] of Object.entries(doc.fieldLengths)) {
|
|
3174
|
+
totalFieldLengths[field] = (totalFieldLengths[field] ?? 0) + length;
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
const docFrequencies = /* @__PURE__ */ Object.create(null);
|
|
3178
|
+
for (const [token, list] of Object.entries(merged.invertedIndex)) {
|
|
3179
|
+
const distinct = /* @__PURE__ */ new Set();
|
|
3180
|
+
for (const posting of list.postings) distinct.add(posting.docId);
|
|
3181
|
+
list.docFrequency = distinct.size;
|
|
3182
|
+
docFrequencies[token] = distinct.size;
|
|
3183
|
+
}
|
|
3184
|
+
const averageFieldLengths = /* @__PURE__ */ Object.create(null);
|
|
3185
|
+
for (const [field, length] of Object.entries(totalFieldLengths)) {
|
|
3186
|
+
averageFieldLengths[field] = totalDocuments > 0 ? length / totalDocuments : 0;
|
|
3187
|
+
}
|
|
3188
|
+
merged.statistics.totalDocuments = totalDocuments;
|
|
3189
|
+
merged.statistics.totalFieldLengths = totalFieldLengths;
|
|
3190
|
+
merged.statistics.averageFieldLengths = averageFieldLengths;
|
|
3191
|
+
merged.statistics.docFrequencies = docFrequencies;
|
|
3192
|
+
merged.docCount = totalDocuments;
|
|
3193
|
+
merged.avgDocLength = Object.values(averageFieldLengths).reduce((sum, value) => sum + value, 0);
|
|
3194
|
+
}
|
|
3195
|
+
function validateMergedPartition(merged) {
|
|
3196
|
+
for (const [token, list] of Object.entries(merged.invertedIndex)) {
|
|
3197
|
+
for (const posting of list.postings) {
|
|
3198
|
+
if (!(posting.docId in merged.documents)) {
|
|
3199
|
+
throw new NarsilError(
|
|
3200
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
3201
|
+
`Merged segment posting references document "${posting.docId}" that is absent from the partition`,
|
|
3202
|
+
{ token, docId: posting.docId }
|
|
3203
|
+
);
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
}
|
|
3208
|
+
function normalizeTombstones(raw) {
|
|
3209
|
+
if (raw === void 0) return [];
|
|
3210
|
+
if (!Array.isArray(raw) || !raw.every((value) => typeof value === "string")) {
|
|
3211
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "Segment file has a malformed tombstone list");
|
|
3212
|
+
}
|
|
3213
|
+
return raw;
|
|
3214
|
+
}
|
|
3215
|
+
async function openContainer(store, key) {
|
|
3216
|
+
const bytes = await store.read(key);
|
|
3217
|
+
if (bytes === null) {
|
|
3218
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Segment file "${key}" is missing`, { key });
|
|
3219
|
+
}
|
|
3220
|
+
const { payloadBytes } = await unpackEnvelopeBytes(bytes);
|
|
3221
|
+
const raw = decode(payloadBytes);
|
|
3222
|
+
if (!(raw.payload instanceof Uint8Array)) {
|
|
3223
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "Segment file is missing its partition payload", { key });
|
|
3224
|
+
}
|
|
3225
|
+
return { payload: raw.payload, tombstones: normalizeTombstones(raw.tombstones) };
|
|
3226
|
+
}
|
|
3227
|
+
async function readEarlierSegmentContents(store, key) {
|
|
3228
|
+
const { payload, tombstones } = await openContainer(store, key);
|
|
3229
|
+
return { partition: deserializePayloadV2(payload), tombstones };
|
|
3230
|
+
}
|
|
3231
|
+
async function readEarlierSegmentDocuments(store, key) {
|
|
3232
|
+
const { payload, tombstones } = await openContainer(store, key);
|
|
3233
|
+
const stored = documentsOfPayloadV2(payload);
|
|
3234
|
+
const documents = [];
|
|
3235
|
+
for (const [docId, document] of Object.entries(stored.documents)) {
|
|
3236
|
+
documents.push({ docId, document: document.fields });
|
|
3237
|
+
}
|
|
3238
|
+
return { schema: stored.schema, documents, tombstones };
|
|
3239
|
+
}
|
|
3240
|
+
|
|
3241
|
+
// src/persistence/durability/checkpoint-count.ts
|
|
3242
|
+
var decoder = new TextDecoder();
|
|
3243
|
+
async function countPagedPartition(store, partition) {
|
|
3244
|
+
const { directory } = store;
|
|
3245
|
+
if (directory === void 0 || partition.segments.length === 0) return null;
|
|
3246
|
+
const { countsAsLatestHolder, isPagedSegmentFile, openPagedSegmentScan } = await import('./module-VEK2KCKU.mjs');
|
|
3247
|
+
for (const ref of partition.segments) if (!await isPagedSegmentFile(directory, ref.key)) return null;
|
|
3248
|
+
const scans = [];
|
|
3249
|
+
try {
|
|
3250
|
+
for (const ref of partition.segments) scans.push(await openPagedSegmentScan(directory, ref));
|
|
3251
|
+
let live = 0;
|
|
3252
|
+
for (let at = 0; at < scans.length; at++) {
|
|
3253
|
+
for (let ordinal = 0; ordinal < scans[at].count; ordinal++) {
|
|
3254
|
+
if (countsAsLatestHolder(scans, at, scans[at].idAt(ordinal))) live += 1;
|
|
3255
|
+
}
|
|
3256
|
+
}
|
|
3257
|
+
return live;
|
|
3258
|
+
} finally {
|
|
3259
|
+
for (const scan of scans) scan.close();
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
async function countCurrentLayout(store, manifest) {
|
|
3263
|
+
let total = 0;
|
|
3264
|
+
for (const partition of manifest.partitions) {
|
|
3265
|
+
const paged = await countPagedPartition(store, partition);
|
|
3266
|
+
if (paged !== null) {
|
|
3267
|
+
total += paged;
|
|
3268
|
+
continue;
|
|
3269
|
+
}
|
|
3270
|
+
const live = /* @__PURE__ */ new Set();
|
|
3271
|
+
for (const ref of partition.segments) {
|
|
3272
|
+
const contents = await readSegmentFile(store, ref);
|
|
3273
|
+
for (const docId of contents.tombstones) live.delete(docId);
|
|
3274
|
+
const ids = contents.arrays.idTable;
|
|
3275
|
+
for (let ordinal = 0; ordinal < contents.arrays.documentCount; ordinal++) {
|
|
3276
|
+
live.add(decoder.decode(ids.blob.subarray(ids.offsets[ordinal], ids.offsets[ordinal + 1])));
|
|
3277
|
+
}
|
|
3278
|
+
}
|
|
3279
|
+
total += live.size;
|
|
3280
|
+
}
|
|
3281
|
+
return total;
|
|
3282
|
+
}
|
|
3283
|
+
async function countCheckpointDocuments(store, indexName) {
|
|
3284
|
+
const decoded = await readSegmentManifest(store, indexName);
|
|
3285
|
+
if (decoded?.kind === "current") return countCurrentLayout(store, decoded.manifest);
|
|
3286
|
+
if (decoded?.kind === "earlier") {
|
|
3287
|
+
let total = 0;
|
|
3288
|
+
for (const partition of decodeEarlierManifest(decoded.payload).partitions) {
|
|
3289
|
+
const ordered = [];
|
|
3290
|
+
for (const segment of partition.segments) ordered.push(await readEarlierSegmentContents(store, segment.key));
|
|
3291
|
+
total += countLiveDocuments(ordered);
|
|
3292
|
+
}
|
|
3293
|
+
return total;
|
|
3294
|
+
}
|
|
3295
|
+
const bundle = await store.read(snapshotBundleKey(indexName));
|
|
3296
|
+
return bundle === null ? 0 : countSnapshotBundleDocuments(await decodeSnapshotBundle(bundle));
|
|
3297
|
+
}
|
|
3298
|
+
function removeWhatTheSegmentReplaces(manager, segment) {
|
|
3299
|
+
manager.beginBatchRemove();
|
|
3300
|
+
try {
|
|
3301
|
+
for (const docId of segment.tombstones) {
|
|
3302
|
+
if (manager.has(docId)) manager.remove(docId);
|
|
3303
|
+
}
|
|
3304
|
+
for (const { docId } of segment.documents) {
|
|
3305
|
+
if (manager.has(docId)) manager.remove(docId);
|
|
3306
|
+
}
|
|
3307
|
+
} finally {
|
|
3308
|
+
manager.endBatchRemove();
|
|
3309
|
+
}
|
|
3310
|
+
}
|
|
3311
|
+
function attachDocuments(manager, partitionId, segment) {
|
|
3312
|
+
if (segment.documents.length === 0) return;
|
|
3313
|
+
const payload = buildSegmentPayload(
|
|
3314
|
+
segment.documents,
|
|
3315
|
+
manager.config.schema,
|
|
3316
|
+
manager.language,
|
|
3317
|
+
resolvePartitionInsertOptions(manager.config, manager.analysis),
|
|
3318
|
+
manager.config.trackPositions ?? true
|
|
3319
|
+
);
|
|
3320
|
+
const documents = segment.documents.map((entry) => entry.document);
|
|
3321
|
+
const snapshot = freezeSegmentShared(payload, documents);
|
|
3322
|
+
if (snapshot === null) {
|
|
3323
|
+
manager.mergeSegment(partitionId, payload, documents);
|
|
3324
|
+
return;
|
|
3325
|
+
}
|
|
3326
|
+
manager.attachFrozenSegment(partitionId, createSharedFrozenSegment(snapshot));
|
|
3327
|
+
}
|
|
3328
|
+
async function loadPartition(store, indexName, partition, deps) {
|
|
3329
|
+
if (deps.storedTermsAreCurrent) {
|
|
3330
|
+
const agreedSchema = {};
|
|
3331
|
+
for (const ref of partition.segments) {
|
|
3332
|
+
const segment = await readEarlierSegmentDocuments(store, ref.key);
|
|
3333
|
+
addSegmentSchema(agreedSchema, segment.schema);
|
|
3334
|
+
removeWhatTheSegmentReplaces(deps.manager, segment);
|
|
3335
|
+
attachDocuments(deps.manager, partition.partitionId, segment);
|
|
3336
|
+
}
|
|
3337
|
+
return;
|
|
3338
|
+
}
|
|
3339
|
+
const ordered = [];
|
|
3340
|
+
for (const ref of partition.segments) ordered.push(await readEarlierSegmentContents(store, ref.key));
|
|
3341
|
+
const merged = mergeTimeOrderedSegments(ordered, {
|
|
3342
|
+
indexName,
|
|
3343
|
+
partitionId: partition.partitionId,
|
|
3344
|
+
totalPartitions: deps.manager.partitionCount,
|
|
3345
|
+
language: deps.manager.language.name
|
|
3346
|
+
});
|
|
3347
|
+
deps.manager.deserializePartition(partition.partitionId, merged);
|
|
3348
|
+
}
|
|
3349
|
+
async function readGraph(store, ref) {
|
|
3350
|
+
if (ref.graphKey === null) return null;
|
|
3351
|
+
const bytes = await store.read(ref.graphKey);
|
|
3352
|
+
if (bytes === null) {
|
|
3353
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The graph file "${ref.graphKey}" is missing`, {
|
|
3354
|
+
key: ref.graphKey
|
|
3355
|
+
});
|
|
3356
|
+
}
|
|
3357
|
+
const { payload } = await openChecksummedEnvelope(bytes, {
|
|
3358
|
+
envelopeFormatVersion: VECTOR_FILE_ENVELOPE_VERSION,
|
|
3359
|
+
what: `graph file "${ref.graphKey}"`,
|
|
3360
|
+
allowsCompression: true
|
|
3361
|
+
});
|
|
3362
|
+
const graph = decodeVectorGraphPayload(decode(payload));
|
|
3363
|
+
if (graph.graphs.some((numbered) => numbered.m < MIN_M)) return null;
|
|
3364
|
+
return graphPartsOfEarlierPayload(graph);
|
|
3365
|
+
}
|
|
3366
|
+
async function loadVectorField(store, ref, vectorIndex, fileRefs) {
|
|
3367
|
+
let liveVectors = 0;
|
|
3368
|
+
for (const file of ref.files) liveVectors += file.count - countDead(file.dead, file.count);
|
|
3369
|
+
const restore = vectorIndex.restoreCheckpoint({
|
|
3370
|
+
liveVectors,
|
|
3371
|
+
holdsGraph: ref.graphKey !== null,
|
|
3372
|
+
graph: await readGraph(store, ref)
|
|
3373
|
+
});
|
|
3374
|
+
for (const file of ref.files) {
|
|
3375
|
+
const bytes = await store.read(file.key);
|
|
3376
|
+
if (bytes === null) {
|
|
3377
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The vector file "${file.key}" is missing`, {
|
|
3378
|
+
key: file.key
|
|
3379
|
+
});
|
|
3380
|
+
}
|
|
3381
|
+
const { header, payload: payloadBytes } = await openChecksummedEnvelope(bytes, {
|
|
3382
|
+
envelopeFormatVersion: VECTOR_FILE_ENVELOPE_VERSION,
|
|
3383
|
+
what: `vector file "${file.key}"`,
|
|
3384
|
+
allowsCompression: false
|
|
3385
|
+
});
|
|
3386
|
+
const payload = decodeVectorFilePayload(decode(payloadBytes));
|
|
3387
|
+
if (payload.docIds.length !== file.count) {
|
|
3388
|
+
throw new NarsilError(
|
|
3389
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
3390
|
+
`The vector file "${file.key}" holds ${payload.docIds.length} vectors where the manifest counts ${file.count}`,
|
|
3391
|
+
{ key: file.key }
|
|
3392
|
+
);
|
|
3393
|
+
}
|
|
3394
|
+
fileRefs.set(file.key, { key: file.key, bytes: bytes.byteLength, checksum: header.checksum });
|
|
3395
|
+
restore.addFile({
|
|
3396
|
+
id: file.id,
|
|
3397
|
+
key: file.key,
|
|
3398
|
+
payload,
|
|
3399
|
+
dead: file.dead,
|
|
3400
|
+
location: store.pathOf === void 0 ? null : {
|
|
3401
|
+
path: await store.pathOf(file.key),
|
|
3402
|
+
vectorsOffset: HEADER_SIZE + header.payloadLength - payload.vectors.byteLength
|
|
3403
|
+
}
|
|
3404
|
+
});
|
|
3405
|
+
}
|
|
3406
|
+
restore.finish();
|
|
3407
|
+
}
|
|
3408
|
+
async function loadEarlierLayout(store, indexName, manifest, deps) {
|
|
3409
|
+
let highestPartitionId = manifest.partitions.length - 1;
|
|
3410
|
+
for (const partition of manifest.partitions) {
|
|
3411
|
+
if (partition.partitionId > highestPartitionId) highestPartitionId = partition.partitionId;
|
|
3412
|
+
}
|
|
3413
|
+
growPartitionsTo(deps.manager, highestPartitionId);
|
|
3414
|
+
for (const partition of manifest.partitions) {
|
|
3415
|
+
if (partition.partitionId >= deps.manager.partitionCount) {
|
|
3416
|
+
throw new NarsilError(
|
|
3417
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
3418
|
+
`Segment manifest references partition ${partition.partitionId} beyond the partition count ${deps.manager.partitionCount}`,
|
|
3419
|
+
{ indexName, partitionId: partition.partitionId }
|
|
3420
|
+
);
|
|
3421
|
+
}
|
|
3422
|
+
await loadPartition(store, indexName, partition, deps);
|
|
3423
|
+
}
|
|
3424
|
+
const vectorFileRefs = /* @__PURE__ */ new Map();
|
|
3425
|
+
for (const vector of manifest.vectors) {
|
|
3426
|
+
const vectorIndex = deps.vectorIndexes.get(vector.fieldPath);
|
|
3427
|
+
if (vectorIndex === void 0) continue;
|
|
3428
|
+
await loadVectorField(store, vector, vectorIndex, vectorFileRefs);
|
|
3429
|
+
}
|
|
3430
|
+
return { checkpoint: manifest.checkpoint, vectorFileRefs };
|
|
3431
|
+
}
|
|
3432
|
+
|
|
3433
|
+
// src/persistence/durability/earlier-layout/upgrade.ts
|
|
3434
|
+
async function loadEarlier(deps, earlier) {
|
|
3435
|
+
const loadDeps = {
|
|
3436
|
+
manager: deps.manager,
|
|
3437
|
+
vectorIndexes: deps.vectorIndexes,
|
|
3438
|
+
storedTermsAreCurrent: deps.storedTermsAreCurrent
|
|
3439
|
+
};
|
|
3440
|
+
if (earlier.kind === "bundle") {
|
|
3441
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, /* @__PURE__ */ new Set());
|
|
3442
|
+
return { checkpoint: await loadSnapshotBundle(earlier.bytes, loadDeps), fileRefs: /* @__PURE__ */ new Map() };
|
|
3443
|
+
}
|
|
3444
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, earlierReferencedKeys(earlier.manifest));
|
|
3445
|
+
const loaded = await loadEarlierLayout(deps.store, deps.indexName, earlier.manifest, loadDeps);
|
|
3446
|
+
return { checkpoint: loaded.checkpoint, fileRefs: loaded.vectorFileRefs };
|
|
3447
|
+
}
|
|
3448
|
+
function priorVectorsOf(earlier, fileRefs) {
|
|
3449
|
+
if (earlier.kind === "bundle") return [];
|
|
3450
|
+
return earlier.manifest.vectors.map((field) => ({
|
|
3451
|
+
fieldPath: field.fieldPath,
|
|
3452
|
+
nextFileId: field.nextFileId,
|
|
3453
|
+
graphGeneration: field.graphGeneration,
|
|
3454
|
+
graphParts: [],
|
|
3455
|
+
files: field.files.flatMap((file) => {
|
|
3456
|
+
const ref = fileRefs.get(file.key);
|
|
3457
|
+
return ref === void 0 ? [] : [{ ...file, bytes: ref.bytes, checksum: ref.checksum }];
|
|
3458
|
+
})
|
|
3459
|
+
}));
|
|
3460
|
+
}
|
|
3461
|
+
function priorPartitionsOf(earlier, partitionCount) {
|
|
3462
|
+
if (earlier.kind === "bundle") return [];
|
|
3463
|
+
return earlier.manifest.partitions.filter((partition) => partition.partitionId < partitionCount).map((partition) => ({ partitionId: partition.partitionId, nextSegmentId: partition.nextSegmentId, segments: [] }));
|
|
3464
|
+
}
|
|
3465
|
+
async function writeCurrentLayout(deps, earlier, targets, fileRefs) {
|
|
3466
|
+
const priorVectors = priorVectorsOf(earlier, fileRefs);
|
|
3467
|
+
const plans = /* @__PURE__ */ new Map();
|
|
3468
|
+
for (const [fieldPath, vectorIndex] of deps.vectorIndexes) {
|
|
3469
|
+
await vectorIndex.completeGraph();
|
|
3470
|
+
const listed = priorVectors.find((ref) => ref.fieldPath === fieldPath);
|
|
3471
|
+
plans.set(
|
|
3472
|
+
fieldPath,
|
|
3473
|
+
vectorIndex.planCheckpoint(listed === void 0 ? null : listed.files.map((file) => file.key), true)
|
|
3474
|
+
);
|
|
3475
|
+
}
|
|
3476
|
+
const written = await writeCheckpointSegments({
|
|
3477
|
+
store: deps.store,
|
|
3478
|
+
metadata: deps.metadata,
|
|
3479
|
+
targets,
|
|
3480
|
+
compactionThreshold: deps.compactionThreshold,
|
|
3481
|
+
prior: { checkpoint: [], partitions: priorPartitionsOf(earlier, deps.manager.partitionCount) },
|
|
3482
|
+
wholePartitions: targets.map((target) => partitionContentsOf(deps.manager, target.partitionId))
|
|
3483
|
+
});
|
|
3484
|
+
const vectors = await writeLiveVectors({
|
|
3485
|
+
store: deps.store,
|
|
3486
|
+
indexName: deps.indexName,
|
|
3487
|
+
plans,
|
|
3488
|
+
priorVectors,
|
|
3489
|
+
priorFileRefs: fileRefs
|
|
3490
|
+
});
|
|
3491
|
+
const committed = await commitCheckpointManifest(deps.store, deps.metadata, written, vectors.refs);
|
|
3492
|
+
recordWrittenVectors(deps.vectorIndexes, vectors.written);
|
|
3493
|
+
await adoptVectorLayouts(deps.store, deps.vectorIndexes, vectors.layouts);
|
|
3494
|
+
await removeCheckpointGarbage(
|
|
3495
|
+
deps.store,
|
|
3496
|
+
await garbageThatNoFieldStillReads(deps.store, deps.vectorIndexes, committed.garbage)
|
|
3497
|
+
);
|
|
3498
|
+
}
|
|
3499
|
+
async function upgradeEarlierLayout(deps, earlier) {
|
|
3500
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "started", layout: earlier.kind });
|
|
3501
|
+
try {
|
|
3502
|
+
const loaded = await loadEarlier(deps, earlier);
|
|
3503
|
+
const targets = await deps.replay(loaded.checkpoint);
|
|
3504
|
+
await writeCurrentLayout(deps, earlier, targets, loaded.fileRefs);
|
|
3505
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "completed", layout: earlier.kind });
|
|
3506
|
+
return { checkpoint: targets, documentCount: deps.manager.countDocuments() };
|
|
3507
|
+
} catch (error) {
|
|
3508
|
+
const failure = error instanceof Error ? error : new Error(String(error));
|
|
3509
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "failed", layout: earlier.kind, error: failure });
|
|
3510
|
+
throw failure;
|
|
3511
|
+
}
|
|
3512
|
+
}
|
|
3513
|
+
|
|
3514
|
+
// src/persistence/durability/checkpoint-recovery.ts
|
|
3515
|
+
async function earlierLayoutOf(deps, decoded) {
|
|
3516
|
+
if (decoded?.kind === "earlier") return { kind: "manifest", manifest: decodeEarlierManifest(decoded.payload) };
|
|
3517
|
+
if (decoded !== null) return null;
|
|
3518
|
+
const bundle = await deps.store.read(snapshotBundleKey(deps.indexName));
|
|
3519
|
+
return bundle === null ? null : { kind: "bundle", bytes: bundle };
|
|
3520
|
+
}
|
|
3521
|
+
async function recoverCheckpoint(deps) {
|
|
3522
|
+
const decoded = await readSegmentManifest(deps.store, deps.indexName);
|
|
3523
|
+
const earlier = await earlierLayoutOf(deps, decoded);
|
|
3524
|
+
if (earlier !== null) {
|
|
3525
|
+
const upgraded = await upgradeEarlierLayout(deps, earlier);
|
|
3526
|
+
return { checkpoint: upgraded.checkpoint, upgradedDocumentCount: upgraded.documentCount };
|
|
3527
|
+
}
|
|
3528
|
+
if (decoded === null || decoded.kind !== "current") {
|
|
3529
|
+
if (!deps.keepsUnlistedFiles) await reclaimUnlistedKeys(deps.store, deps.indexName, /* @__PURE__ */ new Set());
|
|
3530
|
+
return { checkpoint: await deps.replay([]), upgradedDocumentCount: null };
|
|
3531
|
+
}
|
|
3532
|
+
await deps.store.remove(snapshotBundleKey(deps.indexName));
|
|
3533
|
+
if (!deps.keepsUnlistedFiles) {
|
|
3534
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, manifestReferencedKeys(decoded.manifest));
|
|
3535
|
+
}
|
|
3536
|
+
const checkpoint = await loadSegmentedCheckpoint(deps.store, deps.indexName, decoded.manifest, {
|
|
3537
|
+
manager: deps.manager,
|
|
3538
|
+
vectorIndexes: deps.vectorIndexes,
|
|
3539
|
+
writeRecords: deps.writeRecords ?? null
|
|
3540
|
+
});
|
|
3541
|
+
return { checkpoint: await deps.replay(checkpoint), upgradedDocumentCount: null };
|
|
3542
|
+
}
|
|
3543
|
+
|
|
3544
|
+
// src/persistence/durability/stored-analysis.ts
|
|
3545
|
+
function generationOfStoredTerms(store, indexName) {
|
|
3546
|
+
return readSegmentManifest(store, indexName).then((decoded) => {
|
|
3547
|
+
if (decoded === null) return null;
|
|
3548
|
+
return decoded.kind === "current" ? decoded.manifest.analysisGeneration ?? 0 : 0;
|
|
3549
|
+
});
|
|
3550
|
+
}
|
|
3551
|
+
function promotePendingAnalysis(metadata) {
|
|
3552
|
+
const pending = metadata.pendingAnalysis;
|
|
3553
|
+
if (pending === void 0) return;
|
|
3554
|
+
metadata.language = pending.language;
|
|
3555
|
+
metadata.analysisGeneration = pending.generation;
|
|
3556
|
+
metadata.analysisRevision = void 0;
|
|
3557
|
+
metadata.tokenizer = pending.tokenizer;
|
|
3558
|
+
metadata.stopWords = pending.stopWords;
|
|
3559
|
+
metadata.stopWordList = pending.stopWordList;
|
|
3560
|
+
metadata.pendingAnalysis = void 0;
|
|
3561
|
+
}
|
|
3562
|
+
async function settleStoredAnalysis(store, metadata, metadataOnly) {
|
|
3563
|
+
if (metadataOnly && metadata.pendingAnalysis === void 0) return false;
|
|
3564
|
+
const stored = await generationOfStoredTerms(store, metadata.indexName);
|
|
3565
|
+
const current = metadata.analysisGeneration ?? 0;
|
|
3566
|
+
if (stored === null || stored === current) return false;
|
|
3567
|
+
if (stored === metadata.pendingAnalysis?.generation) {
|
|
3568
|
+
promotePendingAnalysis(metadata);
|
|
3569
|
+
return true;
|
|
3570
|
+
}
|
|
3571
|
+
throw new NarsilError(
|
|
3572
|
+
ErrorCodes.CONFIG_INVALID,
|
|
3573
|
+
`The saved terms of index "${metadata.indexName}" come from analysis generation ${stored}, which matches no settings that its metadata records`,
|
|
3574
|
+
{
|
|
3575
|
+
indexName: metadata.indexName,
|
|
3576
|
+
storedGeneration: stored,
|
|
3577
|
+
currentGeneration: current,
|
|
3578
|
+
pendingGeneration: metadata.pendingAnalysis?.generation
|
|
3579
|
+
}
|
|
3580
|
+
);
|
|
3581
|
+
}
|
|
3582
|
+
|
|
3583
|
+
// src/persistence/durability/recover-index.ts
|
|
3584
|
+
async function writeCountedMetadata(deps, indexName, documentCount) {
|
|
3585
|
+
await deps.queueMetadataWrite(indexName, async () => {
|
|
3586
|
+
const metadata = deps.hooks.buildMetadata(indexName, documentCount);
|
|
3587
|
+
if (metadata === void 0) return;
|
|
3588
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
3589
|
+
await deps.directory.atomicWrite(`${indexName}/meta`, bytes);
|
|
3590
|
+
}).catch(() => void 0);
|
|
3591
|
+
}
|
|
3592
|
+
async function recoverPersistedIndex(deps, indexName, metadataOnly) {
|
|
3593
|
+
const { directory, hooks } = deps;
|
|
3594
|
+
const store = directoryCheckpointStore(directory);
|
|
3595
|
+
const metadata = await loadMetadata(directory, indexName);
|
|
3596
|
+
if (metadata === null) {
|
|
3597
|
+
return;
|
|
3598
|
+
}
|
|
3599
|
+
let derivedCount;
|
|
3600
|
+
if (metadataOnly && metadata.documentCount === void 0) {
|
|
3601
|
+
try {
|
|
3602
|
+
derivedCount = await countCheckpointDocuments(store, indexName);
|
|
3603
|
+
metadata.documentCount = derivedCount;
|
|
3604
|
+
} catch {
|
|
3605
|
+
derivedCount = void 0;
|
|
3606
|
+
}
|
|
3607
|
+
}
|
|
3608
|
+
const promoted = await settleStoredAnalysis(store, metadata, metadataOnly);
|
|
3609
|
+
await hooks.createIndexFromMetadata(metadata, !metadataOnly);
|
|
3610
|
+
if (metadataOnly) {
|
|
3611
|
+
if (derivedCount !== void 0 || promoted) {
|
|
3612
|
+
await writeCountedMetadata(deps, indexName, derivedCount ?? metadata.documentCount ?? 0);
|
|
3613
|
+
}
|
|
3614
|
+
return;
|
|
3615
|
+
}
|
|
3616
|
+
const manager = hooks.getManager(indexName);
|
|
3617
|
+
const current = hooks.buildMetadata(indexName);
|
|
3618
|
+
if (manager === void 0 || current === void 0) {
|
|
3619
|
+
return;
|
|
3620
|
+
}
|
|
3621
|
+
const writeRecords = hooks.writeRecords ?? null;
|
|
3622
|
+
writeRecords?.dropIndex(indexName);
|
|
3623
|
+
const replayDeps = {
|
|
3624
|
+
manager,
|
|
3625
|
+
vectorFieldPaths: hooks.getVectorFieldPaths(indexName),
|
|
3626
|
+
vectorIndexes: hooks.getVectorIndexes(indexName),
|
|
3627
|
+
writeRecords,
|
|
3628
|
+
defersGraphBuilds: true
|
|
3629
|
+
};
|
|
3630
|
+
const replay = async (checkpoint) => {
|
|
3631
|
+
const replayed = [];
|
|
3632
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
3633
|
+
const fromSeqNo = snapshotCheckpointFor(checkpoint, partitionId);
|
|
3634
|
+
writeRecords?.loadTermHistory(
|
|
3635
|
+
indexName,
|
|
3636
|
+
partitionId,
|
|
3637
|
+
checkpoint.find((entry) => entry.partitionId === partitionId)
|
|
3638
|
+
);
|
|
3639
|
+
const { highestSeqNo, highestPrimaryTerm } = await replayWal(
|
|
3640
|
+
directory,
|
|
3641
|
+
indexName,
|
|
3642
|
+
partitionId,
|
|
3643
|
+
fromSeqNo,
|
|
3644
|
+
replayDeps
|
|
3645
|
+
);
|
|
3646
|
+
const primaryTerm = Math.max(
|
|
3647
|
+
checkpointPrimaryTerm(checkpoint, partitionId),
|
|
3648
|
+
highestPrimaryTerm,
|
|
3649
|
+
SINGLE_NODE_PRIMARY_TERM
|
|
3650
|
+
);
|
|
3651
|
+
replayed.push({ partitionId, lastSeqNo: highestSeqNo, primaryTerm });
|
|
3652
|
+
}
|
|
3653
|
+
return replayed;
|
|
3654
|
+
};
|
|
3655
|
+
const recovered = await recoverCheckpoint({
|
|
3656
|
+
store,
|
|
3657
|
+
indexName,
|
|
3658
|
+
metadata: current,
|
|
3659
|
+
manager,
|
|
3660
|
+
vectorIndexes: replayDeps.vectorIndexes,
|
|
3661
|
+
storedTermsAreCurrent: metadata.analysisRevision === manager.language.revision,
|
|
3662
|
+
compactionThreshold: deps.compactionThreshold,
|
|
3663
|
+
keepsUnlistedFiles: deps.holdsCheckpointFiles(indexName),
|
|
3664
|
+
replay,
|
|
3665
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade,
|
|
3666
|
+
writeRecords
|
|
3667
|
+
});
|
|
3668
|
+
for (const { partitionId, lastSeqNo, primaryTerm } of recovered.checkpoint) {
|
|
3669
|
+
deps.registerPartition(indexName, partitionId, lastSeqNo, primaryTerm);
|
|
3670
|
+
}
|
|
3671
|
+
if (recovered.upgradedDocumentCount !== null || promoted) {
|
|
3672
|
+
await writeCountedMetadata(deps, indexName, recovered.upgradedDocumentCount ?? manager.countDocuments());
|
|
3673
|
+
}
|
|
3674
|
+
}
|
|
3675
|
+
|
|
3676
|
+
// src/persistence/durability/manager.ts
|
|
3677
|
+
function createDurabilityManager(config, hooks, directoryOverride) {
|
|
3678
|
+
const directory = directoryOverride ?? createDurableDirectory(config.directory ?? "");
|
|
3679
|
+
const canOffloadCheckpoint = directoryOverride === void 0;
|
|
3680
|
+
const segmentMaxBytes = config.segmentMaxBytes ?? DEFAULT_SEGMENT_MAX_BYTES;
|
|
3681
|
+
const checkpointIntervalMs = config.checkpointIntervalMs ?? DEFAULT_CHECKPOINT_INTERVAL_MS;
|
|
3682
|
+
const checkpointMutationThreshold = config.checkpointMutationThreshold ?? DEFAULT_CHECKPOINT_MUTATION_THRESHOLD;
|
|
3683
|
+
const compactionThreshold = config.compactionThreshold ?? DEFAULT_COMPACTION_THRESHOLD;
|
|
3684
|
+
const mode = config.mode ?? "sync";
|
|
3685
|
+
const flushIntervalMs = config.flushIntervalMs ?? DEFAULT_ASYNC_FLUSH_INTERVAL_MS;
|
|
3686
|
+
const registry = createPartitionRegistry(directory, segmentMaxBytes);
|
|
3687
|
+
const indexes = registry.indexes;
|
|
3688
|
+
const getOrCreateIndexState = registry.indexState;
|
|
3689
|
+
const getOrCreatePartition = registry.partition;
|
|
3690
|
+
let entryNumbering = null;
|
|
3691
|
+
let commitDeferral = null;
|
|
3692
|
+
const partitionFailures = createPartitionFailureRouting();
|
|
3693
|
+
const heldCheckpoints = createHeldCheckpoints(hooks);
|
|
3694
|
+
const metadataWrites = createMetadataWriteQueue();
|
|
3695
|
+
const queueMetadataWrite = metadataWrites.queue;
|
|
3696
|
+
let checkpointTimer = null;
|
|
3697
|
+
let asyncFlushTimer = null;
|
|
3698
|
+
let shuttingDown = false;
|
|
3699
|
+
let fatalError = null;
|
|
3700
|
+
const heldCheckpointFiles = /* @__PURE__ */ new Map();
|
|
3701
|
+
const loads = createIndexLoads(hooks.getVectorIndexes);
|
|
3702
|
+
const directoryWatch = createDirectoryWatch(directory, () => shuttingDown || fatalError !== null, markFatal);
|
|
3703
|
+
function stopTimers() {
|
|
3704
|
+
directoryWatch.stop();
|
|
3705
|
+
if (checkpointTimer !== null) clearInterval(checkpointTimer);
|
|
3706
|
+
if (asyncFlushTimer !== null) clearInterval(asyncFlushTimer);
|
|
3707
|
+
checkpointTimer = null;
|
|
3708
|
+
asyncFlushTimer = null;
|
|
3709
|
+
}
|
|
3710
|
+
function markFatal(error) {
|
|
3711
|
+
if (fatalError !== null) return;
|
|
3712
|
+
fatalError = error;
|
|
3713
|
+
stopTimers();
|
|
3714
|
+
hooks.onFatalError(error);
|
|
3715
|
+
}
|
|
3716
|
+
async function closeWriterReportingFailure(closePromise) {
|
|
3717
|
+
try {
|
|
3718
|
+
await closePromise;
|
|
3719
|
+
} catch (err) {
|
|
3720
|
+
markFatal(toError(err));
|
|
3721
|
+
}
|
|
3722
|
+
}
|
|
3723
|
+
function startCheckpointTimer() {
|
|
3724
|
+
if (checkpointTimer !== null || shuttingDown || fatalError !== null || checkpointIntervalMs <= 0) {
|
|
3725
|
+
return;
|
|
3726
|
+
}
|
|
3727
|
+
checkpointTimer = unrefInterval(() => void runScheduledCheckpoints(), checkpointIntervalMs);
|
|
3728
|
+
}
|
|
3729
|
+
function startAsyncFlushTimer() {
|
|
3730
|
+
if (mode !== "async" || asyncFlushTimer !== null || shuttingDown || fatalError !== null || flushIntervalMs <= 0) {
|
|
3731
|
+
return;
|
|
3732
|
+
}
|
|
3733
|
+
asyncFlushTimer = unrefInterval(() => void flushAllPartitions(), flushIntervalMs);
|
|
3734
|
+
}
|
|
3735
|
+
function flushAllPartitions() {
|
|
3736
|
+
return commitEveryPartition(indexes, partitionBatchDeps);
|
|
3737
|
+
}
|
|
3738
|
+
async function runScheduledCheckpoints() {
|
|
3739
|
+
if (fatalError !== null) {
|
|
3740
|
+
return;
|
|
3741
|
+
}
|
|
3742
|
+
for (const indexName of [...indexes.keys()]) {
|
|
3743
|
+
try {
|
|
3744
|
+
await checkpointIndex(indexName);
|
|
3745
|
+
} catch (err) {
|
|
3746
|
+
markFatal(toError(err));
|
|
3747
|
+
}
|
|
3748
|
+
}
|
|
3749
|
+
}
|
|
3750
|
+
async function checkpointIndex(indexName, fromMemory = false, afterInFlight = fromMemory, pinsRetiredVectors = false) {
|
|
3751
|
+
const indexState = indexes.get(indexName);
|
|
3752
|
+
if (indexState === void 0 || indexState.unloading || heldCheckpoints.holds(indexName, indexState)) return;
|
|
3753
|
+
return checkpointInTurn(indexState, {
|
|
3754
|
+
joinsInFlight: !afterInFlight && !pinsRetiredVectors,
|
|
3755
|
+
mayStart: () => !indexState.unloading && !shuttingDown,
|
|
3756
|
+
perform: () => performCheckpoint(indexName, indexState, fromMemory, pinsRetiredVectors),
|
|
3757
|
+
afterSettling: () => releaseStalledWrites(indexState)
|
|
3758
|
+
});
|
|
3759
|
+
}
|
|
3760
|
+
async function requestedCheckpoint(indexName, fromMemory = false, pinsVectors = false) {
|
|
3761
|
+
heldCheckpoints.refuse(indexName, indexes.get(indexName));
|
|
3762
|
+
return checkpointIndex(indexName, fromMemory, fromMemory, pinsVectors);
|
|
3763
|
+
}
|
|
3764
|
+
async function performCheckpoint(indexName, indexState, fromMemory = false, pinsRetiredVectors = false) {
|
|
3765
|
+
await directoryWatch.verify();
|
|
3766
|
+
if (fatalError !== null) throw fatalError;
|
|
3767
|
+
await runDurableCheckpoint({
|
|
3768
|
+
directory,
|
|
3769
|
+
hooks,
|
|
3770
|
+
indexName,
|
|
3771
|
+
indexState,
|
|
3772
|
+
compactionThreshold,
|
|
3773
|
+
canOffload: canOffloadCheckpoint,
|
|
3774
|
+
fromMemory,
|
|
3775
|
+
pinsRetiredVectors,
|
|
3776
|
+
mutationThreshold: checkpointMutationThreshold,
|
|
3777
|
+
mayContinue: () => !indexState.unloading && !shuttingDown && fatalError === null,
|
|
3778
|
+
holdsCheckpointFiles: () => heldCheckpointFiles.has(indexName),
|
|
3779
|
+
queueMetadataWrite,
|
|
3780
|
+
markFatal,
|
|
3781
|
+
failPartition: partitionFailureFor(indexName, partitionBatchDeps)
|
|
3782
|
+
});
|
|
3783
|
+
}
|
|
3784
|
+
const partitionBatchDeps = {
|
|
3785
|
+
syncEachBatch: mode === "sync",
|
|
3786
|
+
fatalError: () => fatalError,
|
|
3787
|
+
markFatal,
|
|
3788
|
+
failsPartitionAlone: partitionFailures.failsAlone,
|
|
3789
|
+
buildEntry: buildMutationEntry,
|
|
3790
|
+
numberEntry: (record) => record.replicated ?? (entryNumbering === null ? null : entryNumbering(record)),
|
|
3791
|
+
defersCommit: (record) => commitDeferral?.(record) === true
|
|
3792
|
+
};
|
|
3793
|
+
async function recordMutations(records) {
|
|
3794
|
+
if (fatalError !== null) {
|
|
3795
|
+
const error = fatalError;
|
|
3796
|
+
return records.map(() => ({ ok: false, error }));
|
|
3797
|
+
}
|
|
3798
|
+
const outcomes = await recordBatchesByPartition(
|
|
3799
|
+
records,
|
|
3800
|
+
(record) => getOrCreatePartition(record.indexName, record.partitionId, 0),
|
|
3801
|
+
partitionBatchDeps
|
|
3802
|
+
);
|
|
3803
|
+
const recordedByIndex = /* @__PURE__ */ new Map();
|
|
3804
|
+
for (let i = 0; i < records.length; i++) {
|
|
3805
|
+
if (!outcomes[i].ok) continue;
|
|
3806
|
+
const recorded = recordedByIndex.get(records[i].indexName) ?? { records: 0, documentBytes: 0 };
|
|
3807
|
+
recorded.records += 1;
|
|
3808
|
+
recorded.documentBytes += records[i].document?.byteLength ?? 0;
|
|
3809
|
+
recordedByIndex.set(records[i].indexName, recorded);
|
|
3810
|
+
}
|
|
3811
|
+
for (const [indexName, recorded] of recordedByIndex) {
|
|
3812
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3813
|
+
indexState.mutationsSinceCheckpoint += recorded.records;
|
|
3814
|
+
indexState.documentBytesSinceCheckpoint += recorded.documentBytes;
|
|
3815
|
+
if (checkpointIsDue(indexState, checkpointMutationThreshold)) {
|
|
3816
|
+
void checkpointIndex(indexName).catch((err) => {
|
|
3817
|
+
markFatal(toError(err));
|
|
3818
|
+
});
|
|
3819
|
+
}
|
|
3820
|
+
await stallWhileCheckpointsFallBehind(indexState);
|
|
3821
|
+
}
|
|
3822
|
+
if (recordedByIndex.size === 0) return outcomes;
|
|
3823
|
+
if (mode === "sync") await directoryWatch.verify();
|
|
3824
|
+
const lost = mode === "sync" ? fatalError : null;
|
|
3825
|
+
if (lost !== null) return outcomes.map((outcome) => outcome.ok ? { ok: false, error: lost } : outcome);
|
|
3826
|
+
await directoryWatch.verifyOnce();
|
|
3827
|
+
directoryWatch.start();
|
|
3828
|
+
startCheckpointTimer();
|
|
3829
|
+
startAsyncFlushTimer();
|
|
3830
|
+
return outcomes;
|
|
3831
|
+
}
|
|
3832
|
+
const recoveryDeps = {
|
|
3833
|
+
directory,
|
|
3834
|
+
hooks,
|
|
3835
|
+
compactionThreshold,
|
|
3836
|
+
registerPartition: (name, partitionId, startSeqNo, primaryTerm) => {
|
|
3837
|
+
getOrCreatePartition(name, partitionId, startSeqNo, primaryTerm);
|
|
3838
|
+
},
|
|
3839
|
+
holdsCheckpointFiles: (name) => heldCheckpointFiles.has(name),
|
|
3840
|
+
queueMetadataWrite
|
|
3841
|
+
};
|
|
3842
|
+
function recoverIndex(indexName, metadataOnly = false, startsGraphRepairs = true) {
|
|
3843
|
+
return loads.track(indexName, recoverPersistedIndex(recoveryDeps, indexName, metadataOnly), startsGraphRepairs);
|
|
3844
|
+
}
|
|
3845
|
+
return {
|
|
3846
|
+
isActive: () => true,
|
|
3847
|
+
async recover(metadataOnly = false) {
|
|
3848
|
+
await directoryWatch.claim();
|
|
3849
|
+
await recoverDirectoryRestores(directory);
|
|
3850
|
+
const names = await listPersistedIndexes(directory);
|
|
3851
|
+
await removeLeftoverRewriteFiles(directory, names);
|
|
3852
|
+
for (const indexName of names) {
|
|
3853
|
+
await recoverIndex(indexName, metadataOnly, false);
|
|
3854
|
+
}
|
|
3855
|
+
await directoryWatch.verify();
|
|
3856
|
+
directoryWatch.start();
|
|
3857
|
+
startCheckpointTimer();
|
|
3858
|
+
for (const indexName of names) loads.startGraphRepairs(indexName);
|
|
3859
|
+
},
|
|
3860
|
+
recoverIndex(indexName) {
|
|
3861
|
+
return recoverIndex(indexName);
|
|
3862
|
+
},
|
|
3863
|
+
highestPersistedSeqNo(indexName, partitionId) {
|
|
3864
|
+
return indexes.get(indexName)?.partitions.get(partitionId)?.appliedSeqNo ?? 0;
|
|
3865
|
+
},
|
|
3866
|
+
partitionRegistry: registry,
|
|
3867
|
+
numberEntriesWith(numbering, defersCommit) {
|
|
3868
|
+
entryNumbering = numbering;
|
|
3869
|
+
commitDeferral = defersCommit ?? null;
|
|
3870
|
+
},
|
|
3871
|
+
isolatePartitionFailures: partitionFailures.isolate,
|
|
3872
|
+
keepWriteRecordsWith(keeper) {
|
|
3873
|
+
hooks.writeRecords = keeper;
|
|
3874
|
+
},
|
|
3875
|
+
rewriteStorage: () => ({ directory, writeRecords: hooks.writeRecords ?? null }),
|
|
3876
|
+
heldCheckpoints: () => heldCheckpoints.report(indexes),
|
|
3877
|
+
async commitPartition(indexName, partitionId) {
|
|
3878
|
+
const partition = indexes.get(indexName)?.partitions.get(partitionId);
|
|
3879
|
+
if (mode !== "sync" || partition === void 0) return;
|
|
3880
|
+
await commitAppended(partition, { indexName, partitionId }, partitionBatchDeps);
|
|
3881
|
+
await directoryWatch.verify();
|
|
3882
|
+
if (fatalError !== null) throw fatalError;
|
|
3883
|
+
},
|
|
3884
|
+
async recordMutation(record) {
|
|
3885
|
+
const [outcome] = await recordMutations([record]);
|
|
3886
|
+
if (!outcome.ok) {
|
|
3887
|
+
throw outcome.error;
|
|
3888
|
+
}
|
|
3889
|
+
return outcome.seqNo;
|
|
3890
|
+
},
|
|
3891
|
+
recordMutations,
|
|
3892
|
+
persistMetadata(indexName) {
|
|
3893
|
+
return queueMetadataWrite(indexName, async () => {
|
|
3894
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
3895
|
+
if (metadata === void 0) {
|
|
3896
|
+
return;
|
|
3897
|
+
}
|
|
3898
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
3899
|
+
await directory.atomicWrite(`${indexName}/meta`, bytes);
|
|
3900
|
+
getOrCreateIndexState(indexName);
|
|
3901
|
+
});
|
|
3902
|
+
},
|
|
3903
|
+
checkpoint(indexName) {
|
|
3904
|
+
return requestedCheckpoint(indexName);
|
|
3905
|
+
},
|
|
3906
|
+
checkpointFromMemory(indexName) {
|
|
3907
|
+
return requestedCheckpoint(indexName, true);
|
|
3908
|
+
},
|
|
3909
|
+
async checkpointAfterSwitch(indexName, swap) {
|
|
3910
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3911
|
+
heldCheckpoints.refuse(indexName, indexState);
|
|
3912
|
+
return switchInTurn(indexState, {
|
|
3913
|
+
mayStart: () => !indexState.unloading && !shuttingDown,
|
|
3914
|
+
swap,
|
|
3915
|
+
checkpoint: () => performCheckpoint(indexName, indexState, true),
|
|
3916
|
+
afterSettling: () => releaseStalledWrites(indexState)
|
|
3917
|
+
});
|
|
3918
|
+
},
|
|
3919
|
+
async checkpointAfterInFlight(indexName) {
|
|
3920
|
+
await loads.settled(indexName);
|
|
3921
|
+
await checkpointIndex(indexName, false, true).catch((err) => markFatal(toError(err)));
|
|
3922
|
+
},
|
|
3923
|
+
async checkpointAll() {
|
|
3924
|
+
for (const indexName of [...indexes.keys()]) {
|
|
3925
|
+
await checkpointIndex(indexName);
|
|
3926
|
+
}
|
|
3927
|
+
},
|
|
3928
|
+
async removeIndex(indexName) {
|
|
3929
|
+
hooks.writeRecords?.dropIndex(indexName);
|
|
3930
|
+
const indexState = indexes.get(indexName);
|
|
3931
|
+
if (indexState !== void 0) {
|
|
3932
|
+
await drainIndexStateForUnload(indexState, metadataWrites.pending(indexName), closeWriterReportingFailure);
|
|
3933
|
+
indexes.delete(indexName);
|
|
3934
|
+
metadataWrites.forget(indexName);
|
|
3935
|
+
}
|
|
3936
|
+
for (const vectorIndex of hooks.getVectorIndexes(indexName).values()) await vectorIndex.releaseVectorFiles();
|
|
3937
|
+
for (const key of await directory.list(`${indexName}/`)) {
|
|
3938
|
+
await directory.remove(key);
|
|
3939
|
+
}
|
|
3940
|
+
},
|
|
3941
|
+
async unloadIndex(indexName) {
|
|
3942
|
+
const indexState = indexes.get(indexName);
|
|
3943
|
+
if (indexState === void 0) {
|
|
3944
|
+
return;
|
|
3945
|
+
}
|
|
3946
|
+
await drainIndexStateForUnload(indexState, metadataWrites.pending(indexName), (close) => close);
|
|
3947
|
+
indexes.delete(indexName);
|
|
3948
|
+
metadataWrites.forget(indexName);
|
|
3949
|
+
},
|
|
3950
|
+
...createArchiveCheckpointAccess({
|
|
3951
|
+
directory,
|
|
3952
|
+
held: heldCheckpointFiles,
|
|
3953
|
+
checkpoint: (name, pinsVectors) => requestedCheckpoint(name, false, pinsVectors),
|
|
3954
|
+
registerPartition: (name, partitionId, startSeqNo, primaryTerm) => getOrCreatePartition(name, partitionId, startSeqNo, primaryTerm),
|
|
3955
|
+
markFatal
|
|
3956
|
+
}),
|
|
3957
|
+
async shutdown() {
|
|
3958
|
+
shuttingDown = true;
|
|
3959
|
+
stopTimers();
|
|
3960
|
+
for (const indexState of indexes.values()) {
|
|
3961
|
+
if (indexState.checkpointInFlight !== null) {
|
|
3962
|
+
await indexState.checkpointInFlight.catch(() => void 0);
|
|
3963
|
+
}
|
|
3964
|
+
for (const partition of indexState.partitions.values()) {
|
|
3965
|
+
await closeWriterReportingFailure(closePartitionWriter(partition));
|
|
3966
|
+
}
|
|
3967
|
+
}
|
|
3968
|
+
indexes.clear();
|
|
3969
|
+
terminateCheckpointWorker();
|
|
3970
|
+
await directoryWatch.release();
|
|
3971
|
+
}
|
|
3972
|
+
};
|
|
3973
|
+
}
|
|
3974
|
+
|
|
3975
|
+
// src/persistence/durability/snapshot-only/checkpoint.ts
|
|
3976
|
+
function capture(input, prior) {
|
|
3977
|
+
const listedIds = new Set((prior?.partitions ?? []).map((partition) => partition.partitionId));
|
|
3978
|
+
const partitionCountChanged = prior !== null && listedIds.size !== input.manager.partitionCount;
|
|
3979
|
+
const targets = [];
|
|
3980
|
+
const contents = [];
|
|
3981
|
+
for (let partitionId = 0; partitionId < input.manager.partitionCount; partitionId += 1) {
|
|
3982
|
+
const lastSeqNo = input.appliedSeqNoByPartition.get(partitionId) ?? 0;
|
|
3983
|
+
const unchanged = !input.rewritesEveryPartition && prior !== null && !partitionCountChanged && listedIds.has(partitionId) && checkpointLastSeqNo(prior.checkpoint, partitionId) === lastSeqNo;
|
|
3984
|
+
if (unchanged) continue;
|
|
3985
|
+
targets.push({ partitionId, lastSeqNo, primaryTerm: SINGLE_NODE_PRIMARY_TERM });
|
|
3986
|
+
contents.push(partitionContentsOf(input.manager, partitionId));
|
|
3987
|
+
}
|
|
3988
|
+
const plans = /* @__PURE__ */ new Map();
|
|
3989
|
+
for (const [fieldPath, vectorIndex] of input.vectorIndexes) {
|
|
3990
|
+
const listed = prior?.vectors.find((ref) => ref.fieldPath === fieldPath);
|
|
3991
|
+
plans.set(fieldPath, vectorIndex.planCheckpoint(listed === void 0 ? null : listed.files.map((file) => file.key)));
|
|
3992
|
+
}
|
|
3993
|
+
return { targets, plans, documentCount: input.manager.countDocuments(), contents };
|
|
3994
|
+
}
|
|
3995
|
+
async function writeCaptured(input, store, prior, captured) {
|
|
3996
|
+
const partitionCount = input.manager.partitionCount;
|
|
3997
|
+
const priorWithinCount = prior === null ? null : {
|
|
3998
|
+
checkpoint: prior.checkpoint.filter((entry) => entry.partitionId < partitionCount),
|
|
3999
|
+
partitions: prior.partitions.filter((partition) => partition.partitionId < partitionCount)
|
|
4000
|
+
};
|
|
4001
|
+
const written = await writeCheckpointSegments({
|
|
4002
|
+
store,
|
|
4003
|
+
metadata: input.metadata,
|
|
4004
|
+
targets: captured.targets,
|
|
4005
|
+
compactionThreshold: DEFAULT_COMPACTION_THRESHOLD,
|
|
4006
|
+
prior: priorWithinCount,
|
|
4007
|
+
wholePartitions: captured.contents
|
|
4008
|
+
});
|
|
4009
|
+
const vectors = await writeLiveVectors({
|
|
4010
|
+
store,
|
|
4011
|
+
indexName: input.indexName,
|
|
4012
|
+
plans: captured.plans,
|
|
4013
|
+
priorVectors: prior?.vectors ?? []
|
|
4014
|
+
});
|
|
4015
|
+
const committed = await commitCheckpointManifest(store, input.metadata, written, vectors.refs);
|
|
4016
|
+
recordWrittenVectors(input.vectorIndexes, vectors.written);
|
|
4017
|
+
await removeCheckpointGarbage(
|
|
4018
|
+
store,
|
|
4019
|
+
await garbageThatNoFieldStillReads(store, input.vectorIndexes, committed.garbage)
|
|
4020
|
+
);
|
|
4021
|
+
}
|
|
4022
|
+
async function writeSnapshotOnlyCheckpoint(input) {
|
|
4023
|
+
const store = adapterCheckpointStore(input.adapter);
|
|
4024
|
+
for (const vectorIndex of input.vectorIndexes.values()) await vectorIndex.completeGraph();
|
|
4025
|
+
const prior = await readCurrentManifest(store, input.indexName);
|
|
4026
|
+
const captured = capture(input, prior);
|
|
4027
|
+
await writeCaptured(input, store, prior, captured);
|
|
4028
|
+
return captured.documentCount;
|
|
4029
|
+
}
|
|
4030
|
+
|
|
4031
|
+
// src/persistence/durability/snapshot-only/index-state.ts
|
|
4032
|
+
function createSnapshotIndexState() {
|
|
4033
|
+
return {
|
|
4034
|
+
mutationsSinceCheckpoint: 0,
|
|
4035
|
+
checkpointInFlight: null,
|
|
4036
|
+
applyChain: Promise.resolve(),
|
|
4037
|
+
unloading: false,
|
|
4038
|
+
appliedSeqNoByPartition: /* @__PURE__ */ new Map()
|
|
4039
|
+
};
|
|
4040
|
+
}
|
|
4041
|
+
|
|
4042
|
+
// src/persistence/durability/snapshot-only/restores.ts
|
|
4043
|
+
function toError5(error) {
|
|
4044
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
4045
|
+
}
|
|
4046
|
+
function createPendingRestores(onFatalError) {
|
|
4047
|
+
const pending = /* @__PURE__ */ new Map();
|
|
4048
|
+
return {
|
|
4049
|
+
finishLater(indexName, run) {
|
|
4050
|
+
let running = null;
|
|
4051
|
+
const finish = () => {
|
|
4052
|
+
running ??= run().then(
|
|
4053
|
+
() => {
|
|
4054
|
+
if (pending.get(indexName) === finish) pending.delete(indexName);
|
|
4055
|
+
},
|
|
4056
|
+
(error) => {
|
|
4057
|
+
running = null;
|
|
4058
|
+
throw error;
|
|
4059
|
+
}
|
|
4060
|
+
);
|
|
4061
|
+
return running;
|
|
4062
|
+
};
|
|
4063
|
+
pending.set(indexName, finish);
|
|
4064
|
+
void finish().catch((error) => onFatalError(toError5(error)));
|
|
4065
|
+
return Promise.resolve();
|
|
4066
|
+
},
|
|
4067
|
+
async settle(indexName) {
|
|
4068
|
+
await pending.get(indexName)?.();
|
|
4069
|
+
},
|
|
4070
|
+
forget(indexName) {
|
|
4071
|
+
pending.delete(indexName);
|
|
4072
|
+
},
|
|
4073
|
+
async settleAll() {
|
|
4074
|
+
for (const finish of [...pending.values()]) await finish().catch(() => void 0);
|
|
4075
|
+
}
|
|
4076
|
+
};
|
|
4077
|
+
}
|
|
4078
|
+
|
|
4079
|
+
// src/persistence/durability/snapshot-only/index.ts
|
|
4080
|
+
function metadataKey(indexName) {
|
|
4081
|
+
return `${indexName}/meta`;
|
|
4082
|
+
}
|
|
4083
|
+
function createSnapshotOnlyManager(adapter, config, hooks, publisher) {
|
|
4084
|
+
const checkpointIntervalMs = config.checkpointIntervalMs ?? DEFAULT_CHECKPOINT_INTERVAL_MS;
|
|
4085
|
+
const checkpointMutationThreshold = config.checkpointMutationThreshold ?? DEFAULT_CHECKPOINT_MUTATION_THRESHOLD;
|
|
4086
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
4087
|
+
const metadataWrites = /* @__PURE__ */ new Map();
|
|
4088
|
+
const loads = createIndexLoads(hooks.getVectorIndexes);
|
|
4089
|
+
const restoreStorage = adapterRestoreStorage(adapter);
|
|
4090
|
+
const restores = createPendingRestores(hooks.onFatalError);
|
|
4091
|
+
let checkpointTimer = null;
|
|
4092
|
+
let shuttingDown = false;
|
|
4093
|
+
function settleRestore(indexName) {
|
|
4094
|
+
return restores.settle(indexName);
|
|
4095
|
+
}
|
|
4096
|
+
function getOrCreateIndexState(indexName) {
|
|
4097
|
+
let state = indexes.get(indexName);
|
|
4098
|
+
if (state === void 0) {
|
|
4099
|
+
state = createSnapshotIndexState();
|
|
4100
|
+
indexes.set(indexName, state);
|
|
4101
|
+
}
|
|
4102
|
+
return state;
|
|
4103
|
+
}
|
|
4104
|
+
function startCheckpointTimer() {
|
|
4105
|
+
if (checkpointTimer !== null || shuttingDown || checkpointIntervalMs <= 0) {
|
|
4106
|
+
return;
|
|
4107
|
+
}
|
|
4108
|
+
checkpointTimer = setInterval(() => {
|
|
4109
|
+
void runScheduledCheckpoints();
|
|
4110
|
+
}, checkpointIntervalMs);
|
|
4111
|
+
if (typeof checkpointTimer.unref === "function") {
|
|
4112
|
+
checkpointTimer.unref();
|
|
4113
|
+
}
|
|
4114
|
+
}
|
|
4115
|
+
async function runScheduledCheckpoints() {
|
|
4116
|
+
for (const indexName of [...indexes.keys()]) {
|
|
4117
|
+
try {
|
|
4118
|
+
await checkpointIndex(indexName);
|
|
4119
|
+
} catch (err) {
|
|
4120
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
4121
|
+
}
|
|
4122
|
+
}
|
|
4123
|
+
}
|
|
4124
|
+
async function performCheckpoint(indexName, indexState, fromMemory) {
|
|
4125
|
+
await settleRestore(indexName);
|
|
4126
|
+
const manager = hooks.getManager(indexName);
|
|
4127
|
+
const metadata = hooks.buildTermsMetadata(indexName);
|
|
4128
|
+
if (manager === void 0 || metadata === void 0) {
|
|
4129
|
+
return;
|
|
4130
|
+
}
|
|
4131
|
+
const documentCount = await writeSnapshotOnlyCheckpoint({
|
|
4132
|
+
adapter,
|
|
4133
|
+
indexName,
|
|
4134
|
+
metadata,
|
|
4135
|
+
manager,
|
|
4136
|
+
vectorIndexes: hooks.getVectorIndexes(indexName),
|
|
4137
|
+
appliedSeqNoByPartition: indexState.appliedSeqNoByPartition,
|
|
4138
|
+
rewritesEveryPartition: fromMemory || hooks.mustRewriteTerms(indexName)
|
|
4139
|
+
});
|
|
4140
|
+
hooks.onManifestCommitted(indexName, metadata.analysisGeneration ?? 0);
|
|
4141
|
+
await queueMetadataWrite(indexName, async () => {
|
|
4142
|
+
const checkpointMetadata = hooks.buildMetadata(indexName, documentCount);
|
|
4143
|
+
if (checkpointMetadata === void 0) return;
|
|
4144
|
+
const metadataBytes = await writeMetadataEnvelope(checkpointMetadata, { checksum: true });
|
|
4145
|
+
await adapter.save(metadataKey(indexName), metadataBytes);
|
|
4146
|
+
hooks.recordCheckpoint?.(indexName, documentCount, manager.partitionCount);
|
|
4147
|
+
});
|
|
4148
|
+
indexState.mutationsSinceCheckpoint = 0;
|
|
4149
|
+
if (publisher !== void 0) {
|
|
4150
|
+
const partitionIds = [];
|
|
4151
|
+
for (let i = 0; i < manager.partitionCount; i += 1) {
|
|
4152
|
+
partitionIds.push(i);
|
|
4153
|
+
}
|
|
4154
|
+
await publisher.publishPartitions(indexName, partitionIds);
|
|
4155
|
+
}
|
|
4156
|
+
}
|
|
4157
|
+
async function checkpointIndex(indexName, fromMemory = false, afterInFlight = fromMemory) {
|
|
4158
|
+
const indexState = indexes.get(indexName);
|
|
4159
|
+
if (indexState === void 0 || indexState.unloading) return;
|
|
4160
|
+
return checkpointInTurn(indexState, {
|
|
4161
|
+
joinsInFlight: !afterInFlight,
|
|
4162
|
+
mayStart: () => !indexState.unloading && !shuttingDown,
|
|
4163
|
+
perform: () => indexState.applyChain.then(() => performCheckpoint(indexName, indexState, fromMemory)),
|
|
4164
|
+
afterSettling: () => void 0
|
|
4165
|
+
});
|
|
4166
|
+
}
|
|
4167
|
+
function queueMetadataWrite(indexName, write) {
|
|
4168
|
+
const previous = metadataWrites.get(indexName) ?? Promise.resolve();
|
|
4169
|
+
const queued = previous.then(() => settleRestore(indexName)).then(write);
|
|
4170
|
+
metadataWrites.set(
|
|
4171
|
+
indexName,
|
|
4172
|
+
queued.catch(() => void 0)
|
|
4173
|
+
);
|
|
4174
|
+
return queued;
|
|
4175
|
+
}
|
|
4176
|
+
async function saveMetadata(indexName, documentCount) {
|
|
4177
|
+
const metadata = hooks.buildMetadata(indexName, documentCount);
|
|
4178
|
+
if (metadata === void 0) return;
|
|
4179
|
+
await adapter.save(metadataKey(indexName), await writeMetadataEnvelope(metadata, { checksum: true }));
|
|
4180
|
+
}
|
|
4181
|
+
function recoverIndex(indexName, metadataOnly = false, startsGraphRepairs = true) {
|
|
4182
|
+
return loads.track(indexName, loadIndex(indexName, metadataOnly), startsGraphRepairs);
|
|
4183
|
+
}
|
|
4184
|
+
async function loadIndex(indexName, metadataOnly) {
|
|
4185
|
+
await settleRestore(indexName);
|
|
4186
|
+
const metaBytes = await adapter.load(metadataKey(indexName));
|
|
4187
|
+
if (metaBytes === null) {
|
|
4188
|
+
return;
|
|
4189
|
+
}
|
|
4190
|
+
const { metadata } = await readMetadataEnvelope(metaBytes);
|
|
4191
|
+
const promoted = await settleStoredAnalysis(adapterCheckpointStore(adapter), metadata, metadataOnly);
|
|
4192
|
+
let derivedCount;
|
|
4193
|
+
if (metadataOnly && metadata.documentCount === void 0) {
|
|
4194
|
+
try {
|
|
4195
|
+
derivedCount = await countStoredBundleDocuments(indexName);
|
|
4196
|
+
metadata.documentCount = derivedCount;
|
|
4197
|
+
} catch {
|
|
4198
|
+
derivedCount = void 0;
|
|
4199
|
+
}
|
|
4200
|
+
}
|
|
4201
|
+
await hooks.createIndexFromMetadata(metadata, !metadataOnly);
|
|
4202
|
+
if (metadataOnly) {
|
|
4203
|
+
if (derivedCount !== void 0 || promoted) {
|
|
4204
|
+
const counted = derivedCount ?? metadata.documentCount;
|
|
4205
|
+
await queueMetadataWrite(indexName, () => saveMetadata(indexName, counted)).catch(() => void 0);
|
|
4206
|
+
}
|
|
4207
|
+
return;
|
|
4208
|
+
}
|
|
4209
|
+
const manager = hooks.getManager(indexName);
|
|
4210
|
+
const current = hooks.buildMetadata(indexName);
|
|
4211
|
+
if (manager === void 0 || current === void 0) {
|
|
4212
|
+
return;
|
|
4213
|
+
}
|
|
4214
|
+
const recovered = await recoverCheckpoint({
|
|
4215
|
+
store: adapterCheckpointStore(adapter),
|
|
4216
|
+
indexName,
|
|
4217
|
+
metadata: current,
|
|
4218
|
+
manager,
|
|
4219
|
+
vectorIndexes: hooks.getVectorIndexes(indexName),
|
|
4220
|
+
storedTermsAreCurrent: metadata.analysisRevision === manager.language.revision,
|
|
4221
|
+
compactionThreshold: DEFAULT_COMPACTION_THRESHOLD,
|
|
4222
|
+
keepsUnlistedFiles: false,
|
|
4223
|
+
replay: async (checkpoint) => checkpoint,
|
|
4224
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade
|
|
4225
|
+
});
|
|
4226
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
4227
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
4228
|
+
const persistedSeqNo = snapshotCheckpointFor(recovered.checkpoint, partitionId);
|
|
4229
|
+
if (persistedSeqNo > 0) {
|
|
4230
|
+
indexState.appliedSeqNoByPartition.set(partitionId, persistedSeqNo);
|
|
4231
|
+
}
|
|
4232
|
+
}
|
|
4233
|
+
if (recovered.upgradedDocumentCount !== null || promoted) {
|
|
4234
|
+
const upgradedCount = recovered.upgradedDocumentCount ?? manager.countDocuments();
|
|
4235
|
+
await queueMetadataWrite(indexName, () => saveMetadata(indexName, upgradedCount)).catch(() => void 0);
|
|
4236
|
+
}
|
|
4237
|
+
}
|
|
4238
|
+
function countStoredBundleDocuments(indexName) {
|
|
4239
|
+
return countCheckpointDocuments(adapterCheckpointStore(adapter), indexName);
|
|
4240
|
+
}
|
|
4241
|
+
async function recordMutation(record) {
|
|
4242
|
+
const indexState = getOrCreateIndexState(record.indexName);
|
|
4243
|
+
const buffered = indexState.applyChain.then(async () => {
|
|
4244
|
+
await record.apply();
|
|
4245
|
+
const appliedSeqNo2 = (indexState.appliedSeqNoByPartition.get(record.partitionId) ?? 0) + 1;
|
|
4246
|
+
indexState.appliedSeqNoByPartition.set(record.partitionId, appliedSeqNo2);
|
|
4247
|
+
return appliedSeqNo2;
|
|
4248
|
+
});
|
|
4249
|
+
indexState.applyChain = buffered.then(
|
|
4250
|
+
() => void 0,
|
|
4251
|
+
() => void 0
|
|
4252
|
+
);
|
|
4253
|
+
const appliedSeqNo = await buffered;
|
|
4254
|
+
indexState.mutationsSinceCheckpoint += 1;
|
|
4255
|
+
startCheckpointTimer();
|
|
4256
|
+
if (indexState.mutationsSinceCheckpoint >= checkpointMutationThreshold) {
|
|
4257
|
+
void checkpointIndex(record.indexName).catch((err) => {
|
|
4258
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
4259
|
+
});
|
|
4260
|
+
}
|
|
4261
|
+
return appliedSeqNo;
|
|
4262
|
+
}
|
|
4263
|
+
async function drainIndex(indexName) {
|
|
4264
|
+
const indexState = indexes.get(indexName);
|
|
4265
|
+
if (indexState !== void 0) {
|
|
4266
|
+
indexState.unloading = true;
|
|
4267
|
+
try {
|
|
4268
|
+
await indexState.applyChain;
|
|
4269
|
+
if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
|
|
4270
|
+
await metadataWrites.get(indexName);
|
|
4271
|
+
} catch (error) {
|
|
4272
|
+
indexState.unloading = false;
|
|
4273
|
+
throw error;
|
|
4274
|
+
}
|
|
4275
|
+
}
|
|
4276
|
+
indexes.delete(indexName);
|
|
4277
|
+
metadataWrites.delete(indexName);
|
|
4278
|
+
}
|
|
4279
|
+
return {
|
|
4280
|
+
isActive() {
|
|
4281
|
+
return true;
|
|
4282
|
+
},
|
|
4283
|
+
async recover(metadataOnly = false) {
|
|
4284
|
+
await recoverRestoreJournals(restoreStorage);
|
|
4285
|
+
const names = (await adapter.list("")).filter((key) => key.endsWith("/meta")).map((key) => key.slice(0, -"/meta".length));
|
|
4286
|
+
for (const indexName of names) await recoverIndex(indexName, metadataOnly, false);
|
|
4287
|
+
startCheckpointTimer();
|
|
4288
|
+
for (const indexName of names) loads.startGraphRepairs(indexName);
|
|
4289
|
+
},
|
|
4290
|
+
recoverIndex(indexName) {
|
|
4291
|
+
return recoverIndex(indexName);
|
|
4292
|
+
},
|
|
4293
|
+
highestPersistedSeqNo(indexName, partitionId) {
|
|
4294
|
+
return indexes.get(indexName)?.appliedSeqNoByPartition.get(partitionId) ?? 0;
|
|
4295
|
+
},
|
|
4296
|
+
recordMutation,
|
|
4297
|
+
async recordMutations(records) {
|
|
4298
|
+
const outcomes = [];
|
|
4299
|
+
for (const record of records) {
|
|
4300
|
+
try {
|
|
4301
|
+
outcomes.push({ ok: true, seqNo: await recordMutation(record) });
|
|
4302
|
+
} catch (error) {
|
|
4303
|
+
outcomes.push({ ok: false, error });
|
|
4304
|
+
}
|
|
4305
|
+
}
|
|
4306
|
+
return outcomes;
|
|
4307
|
+
},
|
|
4308
|
+
persistMetadata(indexName) {
|
|
4309
|
+
return queueMetadataWrite(indexName, async () => {
|
|
4310
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
4311
|
+
if (metadata === void 0) {
|
|
4312
|
+
return;
|
|
4313
|
+
}
|
|
4314
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
4315
|
+
await adapter.save(metadataKey(indexName), bytes);
|
|
4316
|
+
getOrCreateIndexState(indexName);
|
|
4317
|
+
});
|
|
4318
|
+
},
|
|
4319
|
+
checkpoint(indexName) {
|
|
4320
|
+
return checkpointIndex(indexName);
|
|
4321
|
+
},
|
|
4322
|
+
checkpointFromMemory(indexName) {
|
|
4323
|
+
return checkpointIndex(indexName, true);
|
|
4324
|
+
},
|
|
4325
|
+
async checkpointAfterSwitch(indexName, swap) {
|
|
4326
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
4327
|
+
return switchInTurn(indexState, {
|
|
4328
|
+
mayStart: () => !indexState.unloading && !shuttingDown,
|
|
4329
|
+
swap,
|
|
4330
|
+
checkpoint: () => performCheckpoint(indexName, indexState, true),
|
|
4331
|
+
afterSettling: () => void 0
|
|
4332
|
+
});
|
|
4333
|
+
},
|
|
4334
|
+
async checkpointAfterInFlight(indexName) {
|
|
4335
|
+
await loads.settled(indexName);
|
|
4336
|
+
await checkpointIndex(indexName, false, true).catch((err) => {
|
|
4337
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
4338
|
+
});
|
|
4339
|
+
},
|
|
4340
|
+
async checkpointAll() {
|
|
4341
|
+
for (const indexName of [...indexes.keys()]) {
|
|
4342
|
+
await checkpointIndex(indexName);
|
|
4343
|
+
}
|
|
4344
|
+
},
|
|
4345
|
+
stageRestore(indexName) {
|
|
4346
|
+
return createRestoreStaging(restoreStorage, indexName, {
|
|
4347
|
+
finish: (run) => restores.finishLater(indexName, run),
|
|
4348
|
+
onStorageLost: hooks.onFatalError
|
|
4349
|
+
});
|
|
4350
|
+
},
|
|
4351
|
+
adoptRestoredCheckpoint(indexName, checkpoint) {
|
|
4352
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
4353
|
+
indexState.appliedSeqNoByPartition.clear();
|
|
4354
|
+
for (const { partitionId, lastSeqNo } of checkpoint) {
|
|
4355
|
+
if (lastSeqNo > 0) indexState.appliedSeqNoByPartition.set(partitionId, lastSeqNo);
|
|
4356
|
+
}
|
|
4357
|
+
},
|
|
4358
|
+
async removeIndex(indexName) {
|
|
4359
|
+
await settleRestore(indexName).catch(() => void 0);
|
|
4360
|
+
restores.forget(indexName);
|
|
4361
|
+
await restoreStorage.remove(journalKey(indexName));
|
|
4362
|
+
await drainIndex(indexName);
|
|
4363
|
+
for (const key of await adapter.list(`${indexName}/`)) {
|
|
4364
|
+
await adapter.delete(key);
|
|
4365
|
+
}
|
|
4366
|
+
},
|
|
4367
|
+
async unloadIndex(indexName) {
|
|
4368
|
+
await settleRestore(indexName);
|
|
4369
|
+
await drainIndex(indexName);
|
|
4370
|
+
},
|
|
4371
|
+
async reloadIndex(indexName) {
|
|
4372
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
4373
|
+
const reload = indexState.applyChain.then(() => recoverIndex(indexName));
|
|
4374
|
+
indexState.applyChain = reload.catch(() => void 0);
|
|
4375
|
+
await reload;
|
|
4376
|
+
},
|
|
4377
|
+
async shutdown() {
|
|
4378
|
+
shuttingDown = true;
|
|
4379
|
+
if (checkpointTimer !== null) {
|
|
4380
|
+
clearInterval(checkpointTimer);
|
|
4381
|
+
checkpointTimer = null;
|
|
4382
|
+
}
|
|
4383
|
+
await restores.settleAll();
|
|
4384
|
+
for (const indexState of indexes.values()) {
|
|
4385
|
+
if (indexState.checkpointInFlight !== null) {
|
|
4386
|
+
await indexState.checkpointInFlight.catch(() => void 0);
|
|
4387
|
+
}
|
|
4388
|
+
}
|
|
4389
|
+
indexes.clear();
|
|
4390
|
+
}
|
|
4391
|
+
};
|
|
4392
|
+
}
|
|
4393
|
+
|
|
4394
|
+
// src/engine/durability-integration.ts
|
|
4395
|
+
function buildMetadata(indexName, hooks, documentCount) {
|
|
4396
|
+
const config = hooks.getIndexConfig(indexName);
|
|
4397
|
+
if (config === void 0) {
|
|
4398
|
+
return void 0;
|
|
4399
|
+
}
|
|
4400
|
+
return metadataFromConfig(indexName, config, hooks.getManager(indexName)?.partitionCount, documentCount);
|
|
4401
|
+
}
|
|
4402
|
+
function createDurabilityIntegration(tier, hooks) {
|
|
4403
|
+
const managerHooks = {
|
|
4404
|
+
getManager: hooks.getManager,
|
|
4405
|
+
getVectorFieldPaths: hooks.getVectorFieldPaths,
|
|
4406
|
+
getVectorIndexes: hooks.getVectorIndexes,
|
|
4407
|
+
buildMetadata: (indexName, documentCount) => buildMetadata(indexName, hooks, documentCount),
|
|
4408
|
+
buildTermsMetadata(indexName) {
|
|
4409
|
+
const config = hooks.getTermsConfig(indexName);
|
|
4410
|
+
return config === void 0 ? void 0 : metadataFromConfig(indexName, config, hooks.getManager(indexName)?.partitionCount);
|
|
4411
|
+
},
|
|
4412
|
+
mustRewriteTerms: hooks.mustRewriteTerms,
|
|
4413
|
+
onManifestCommitted: hooks.onManifestCommitted,
|
|
4414
|
+
...hooks.queueDiskAdoption === void 0 ? {} : { queueDiskAdoption: hooks.queueDiskAdoption },
|
|
4415
|
+
...hooks.awaitCopiesOfIndex === void 0 ? {} : { awaitCopiesOfIndex: hooks.awaitCopiesOfIndex },
|
|
4416
|
+
createIndexFromMetadata: hooks.createIndexFromMetadata,
|
|
4417
|
+
recordCheckpoint: hooks.recordCheckpoint,
|
|
4418
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade,
|
|
4419
|
+
onCheckpointHeld: hooks.onCheckpointHeld,
|
|
4420
|
+
onFatalError: hooks.onFatalError
|
|
4421
|
+
};
|
|
4422
|
+
const manager = tier.kind === "wal" ? createDurabilityManager(tier.config, managerHooks) : createSnapshotOnlyManager(tier.adapter, tier.config, managerHooks, hooks.checkpointPublisher);
|
|
4423
|
+
function partitionFor(indexName, docId) {
|
|
4424
|
+
const partitionManager = hooks.getManager(indexName);
|
|
4425
|
+
const partitionCount = partitionManager?.partitionCount ?? 1;
|
|
4426
|
+
return fnv1a(docId) % Math.max(1, partitionCount);
|
|
4427
|
+
}
|
|
4428
|
+
async function recordMutation(indexName, docId, operation, document, apply) {
|
|
4429
|
+
const partitionId = partitionFor(indexName, docId);
|
|
4430
|
+
const seqNo = await manager.recordMutation({
|
|
4431
|
+
indexName,
|
|
4432
|
+
partitionId,
|
|
4433
|
+
operation,
|
|
4434
|
+
documentId: docId,
|
|
4435
|
+
document,
|
|
4436
|
+
apply
|
|
4437
|
+
});
|
|
4438
|
+
return { indexName, partitionId, seqNo };
|
|
4439
|
+
}
|
|
4440
|
+
return {
|
|
4441
|
+
manager,
|
|
4442
|
+
recordInsertOrUpdate(indexName, docId, document, apply) {
|
|
4443
|
+
return recordMutation(indexName, docId, "INDEX", encodeDocument(document), apply);
|
|
4444
|
+
},
|
|
4445
|
+
async recordMutationBatch(indexName, mutations) {
|
|
4446
|
+
const records = [];
|
|
4447
|
+
const outcomes = new Array(mutations.length);
|
|
4448
|
+
const recordPositions = [];
|
|
4449
|
+
for (let i = 0; i < mutations.length; i++) {
|
|
4450
|
+
if (turnsAreBudgeted && turnIsSpent()) await yieldTurn();
|
|
4451
|
+
const mutation = mutations[i];
|
|
4452
|
+
try {
|
|
4453
|
+
records.push({
|
|
4454
|
+
indexName,
|
|
4455
|
+
partitionId: partitionFor(indexName, mutation.docId),
|
|
4456
|
+
operation: mutation.document === null ? "DELETE" : "INDEX",
|
|
4457
|
+
documentId: mutation.docId,
|
|
4458
|
+
document: mutation.document === null ? null : encodeDocument(mutation.document),
|
|
4459
|
+
apply: mutation.apply
|
|
4460
|
+
});
|
|
4461
|
+
recordPositions.push(i);
|
|
4462
|
+
} catch (error) {
|
|
4463
|
+
outcomes[i] = { ok: false, error };
|
|
4464
|
+
}
|
|
4465
|
+
}
|
|
4466
|
+
const recorded = await manager.recordMutations(records);
|
|
4467
|
+
for (let i = 0; i < recorded.length; i++) {
|
|
4468
|
+
const outcome = recorded[i];
|
|
4469
|
+
outcomes[recordPositions[i]] = outcome.ok ? { ok: true, write: { indexName, partitionId: records[i].partitionId, seqNo: outcome.seqNo } } : outcome;
|
|
4470
|
+
}
|
|
4471
|
+
return outcomes;
|
|
4472
|
+
},
|
|
4473
|
+
recordRemove(indexName, docId, apply) {
|
|
4474
|
+
return recordMutation(indexName, docId, "DELETE", null, apply);
|
|
4475
|
+
}
|
|
4476
|
+
};
|
|
4477
|
+
}
|
|
4478
|
+
|
|
4479
|
+
// src/engine/durability-wiring.ts
|
|
4480
|
+
function createDurabilityFromTier(tier, wiring) {
|
|
4481
|
+
return createDurabilityIntegration(tier, {
|
|
4482
|
+
checkpointPublisher: { publishPartitions: wiring.publishCheckpointedPartitions },
|
|
4483
|
+
getManager: (indexName) => wiring.indexRegistry.has(indexName) ? wiring.getManager(indexName) : void 0,
|
|
4484
|
+
getVectorFieldPaths: (indexName) => wiring.indexRegistry.get(indexName)?.vectorFieldPaths ?? /* @__PURE__ */ new Set(),
|
|
4485
|
+
getVectorIndexes: (indexName) => (wiring.indexRegistry.has(indexName) ? wiring.getManager(indexName)?.getVectorIndexes() : void 0) ?? /* @__PURE__ */ new Map(),
|
|
4486
|
+
getIndexConfig: (indexName) => {
|
|
4487
|
+
const entry = wiring.indexRegistry.get(indexName);
|
|
4488
|
+
return entry === void 0 ? void 0 : durableMetadataConfigOf(entry);
|
|
4489
|
+
},
|
|
4490
|
+
getTermsConfig: (indexName) => {
|
|
4491
|
+
const entry = wiring.indexRegistry.get(indexName);
|
|
4492
|
+
return entry === void 0 ? void 0 : metadataConfigOf(entry);
|
|
4493
|
+
},
|
|
4494
|
+
mustRewriteTerms: (indexName) => (wiring.indexRegistry.get(indexName)?.durableAnalysis ?? null) !== null,
|
|
4495
|
+
onManifestCommitted(indexName, analysisGeneration) {
|
|
4496
|
+
const entry = wiring.indexRegistry.get(indexName);
|
|
4497
|
+
if (entry !== void 0 && entry.analysisGeneration === analysisGeneration) entry.durableAnalysis = null;
|
|
4498
|
+
},
|
|
4499
|
+
queueDiskAdoption: wiring.queueDiskAdoption,
|
|
4500
|
+
awaitCopiesOfIndex: wiring.awaitCopiesOfIndex,
|
|
4501
|
+
createIndexFromMetadata: wiring.createIndexFromMetadata,
|
|
4502
|
+
recordCheckpoint: wiring.recordCheckpoint,
|
|
4503
|
+
onLayoutUpgrade: wiring.emitLayoutUpgrade,
|
|
4504
|
+
onCheckpointHeld: wiring.emitCheckpointHeld,
|
|
4505
|
+
onFatalError: wiring.emitFatalError
|
|
4506
|
+
});
|
|
4507
|
+
}
|
|
4508
|
+
|
|
4509
|
+
export { createDurabilityFromTier };
|