@delali/narsil 0.3.1-next.145 → 0.3.1-next.149

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.
Files changed (207) hide show
  1. package/dist/adapters/filesystem.mjs +2 -2
  2. package/dist/adapters/gcs.mjs +2 -2
  3. package/dist/adapters/indexeddb.mjs +1 -1
  4. package/dist/adapters/s3.mjs +2 -2
  5. package/dist/analysis-change-ZCJNYBJM.mjs +39 -0
  6. package/dist/{analysis-change-entry-V4SQQSXQ.mjs → analysis-change-entry-YN3ELL6C.mjs} +1 -1
  7. package/dist/browser/{analysis-change-QTCQ5ETU.mjs → analysis-change-M65AZQQG.mjs} +8 -5
  8. package/dist/browser/{analysis-change-entry-KYEQSP3I.mjs → analysis-change-entry-H3YIOLXZ.mjs} +1 -1
  9. package/dist/browser/{chunk-GP4QLUQM.mjs → chunk-2JABMB4S.mjs} +10 -6
  10. package/dist/browser/{chunk-5WYWLTM5.mjs → chunk-4DYLCICG.mjs} +2 -2
  11. package/dist/browser/{chunk-75PMIFQA.mjs → chunk-4RD3AFSB.mjs} +1 -1
  12. package/dist/browser/{chunk-XWMEGKDC.mjs → chunk-5V74FFZE.mjs} +2 -2
  13. package/dist/browser/{chunk-PMBSBSCN.mjs → chunk-6OWX3OP2.mjs} +2 -2
  14. package/dist/browser/chunk-BRWY3JCN.mjs +678 -0
  15. package/dist/browser/{chunk-ZALEKEUH.mjs → chunk-BVX2MEHA.mjs} +306 -607
  16. package/dist/browser/{chunk-FXPMF65P.mjs → chunk-D6UFGBF5.mjs} +15 -45
  17. package/dist/browser/{chunk-QNDWMPV2.mjs → chunk-IAXZAVYL.mjs} +25 -66
  18. package/dist/browser/chunk-JNOAKDUM.mjs +31 -0
  19. package/dist/browser/{chunk-BTMRV5MY.mjs → chunk-KAEVGLYN.mjs} +2 -2
  20. package/dist/browser/{chunk-FHO5N7FD.mjs → chunk-LURXVCHG.mjs} +1 -1
  21. package/dist/browser/chunk-MPK76GVS.mjs +63 -0
  22. package/dist/browser/{chunk-6OFS7RUN.mjs → chunk-NVEJJP4R.mjs} +1 -1
  23. package/dist/browser/{chunk-U77XQJOY.mjs → chunk-ODQP7P3H.mjs} +455 -66
  24. package/dist/browser/{chunk-IFGTRRPU.mjs → chunk-PIS7ZAP7.mjs} +3 -25
  25. package/dist/browser/{chunk-XIHLIMFX.mjs → chunk-QMNZFFD3.mjs} +4 -3
  26. package/dist/browser/{chunk-AOKLYVTT.mjs → chunk-SRL47USS.mjs} +2 -2
  27. package/dist/browser/chunk-TY7L6OXO.mjs +69 -0
  28. package/dist/browser/{chunk-LGJ6XMH5.mjs → chunk-W643QV3L.mjs} +6 -5
  29. package/dist/browser/chunk-WXLBZJMT.mjs +25 -0
  30. package/dist/browser/{chunk-ZKVDQ5ZH.mjs → chunk-XILIT5MT.mjs} +8 -8
  31. package/dist/browser/{cold-module-Y2IJAP7S.mjs → cold-module-A5S6JFDN.mjs} +20 -20
  32. package/dist/browser/{durability-wiring-MLXZ5QQF.mjs → durability-wiring-W5AVV3RF.mjs} +53 -30
  33. package/dist/browser/gzip-5S7ROJFJ.mjs +1 -0
  34. package/dist/browser/import-PTT63AGK.mjs +12 -0
  35. package/dist/browser/{module-5LMDGXQH.mjs → module-CEKA7MQH.mjs} +13 -10
  36. package/dist/browser/module-X5PGOE47.mjs +4288 -0
  37. package/dist/browser/{phrase-module-KXNRTWCS.mjs → phrase-module-CNKMUY4U.mjs} +2 -1
  38. package/dist/browser/pool-DTYSPBMQ.mjs +296 -0
  39. package/dist/{restore-queue-UAVGBSJG.mjs → browser/restore-queue-DD4LWZK7.mjs} +2 -2
  40. package/dist/browser/{runtime-ZNEGK33G.mjs → runtime-D2ULZGCN.mjs} +3 -2
  41. package/dist/browser/{snapshot-2EGS2FYU.mjs → snapshot-DQ6A4PQP.mjs} +18 -14
  42. package/dist/browser/{write-WMZA6NSF.mjs → write-MAGRAIEH.mjs} +14 -11
  43. package/dist/{capabilities-CNnzGtqP.d.ts → capabilities-BT-HLDC8.d.ts} +2 -2
  44. package/dist/{chunk-FRJ5RGX7.mjs → chunk-2HJWQPR6.mjs} +2 -2
  45. package/dist/{chunk-DKMZ7JFW.mjs → chunk-3BVGJ4JR.mjs} +10 -4
  46. package/dist/{chunk-QYMAYU7M.mjs → chunk-3HV7M5OJ.mjs} +155 -149
  47. package/dist/chunk-3MXV3VHR.mjs +46 -0
  48. package/dist/{chunk-6FOJKNUR.mjs → chunk-3SBJVQLL.mjs} +2 -2
  49. package/dist/{chunk-PRID7CA6.mjs → chunk-3TVDTACC.mjs} +1 -1
  50. package/dist/{chunk-RTVAWNUL.mjs → chunk-5WB2A3XU.mjs} +9 -6
  51. package/dist/{chunk-LWFK3TC2.mjs → chunk-65WCV5JO.mjs} +1 -1
  52. package/dist/{chunk-G6Q5DEF5.mjs → chunk-6BWLDLS3.mjs} +3 -46
  53. package/dist/{chunk-W6M4RRAT.mjs → chunk-6C5EX2QR.mjs} +3 -3
  54. package/dist/{chunk-HOC5JP2H.mjs → chunk-6F6USYII.mjs} +119 -11
  55. package/dist/{chunk-7523VCM4.mjs → chunk-7HKWO4BJ.mjs} +7 -7
  56. package/dist/{chunk-ONDEEBTA.mjs → chunk-7N223GWV.mjs} +2 -2
  57. package/dist/{chunk-UGQULMSU.mjs → chunk-ADBS5XRI.mjs} +10 -10
  58. package/dist/{chunk-MJTEU2XK.mjs → chunk-AWNHDBKV.mjs} +429 -73
  59. package/dist/{chunk-2E2ZFOPT.mjs → chunk-B2FTZNNF.mjs} +2 -2
  60. package/dist/{chunk-NWTEJ2T7.mjs → chunk-B7XIOAMK.mjs} +3 -3
  61. package/dist/{chunk-W55X2BYN.mjs → chunk-BEZH7VEH.mjs} +1 -1
  62. package/dist/chunk-BTPGQC6Z.mjs +27 -0
  63. package/dist/{chunk-KG5N7LE6.mjs → chunk-CYYSZPVV.mjs} +11 -11
  64. package/dist/{chunk-HTMUGEFU.mjs → chunk-DOY6EG76.mjs} +6 -6
  65. package/dist/{chunk-UHVTEVF5.mjs → chunk-F22IOCR2.mjs} +10 -6
  66. package/dist/{chunk-FTQLF5JI.mjs → chunk-IRWP524D.mjs} +1 -1
  67. package/dist/{chunk-XLAI4OIO.mjs → chunk-IZT3G24Q.mjs} +2 -2
  68. package/dist/{chunk-SE7MKJVK.mjs → chunk-J7M73VY7.mjs} +1 -1
  69. package/dist/chunk-KAKQ74LS.mjs +53 -0
  70. package/dist/{chunk-GVPYMSLO.mjs → chunk-KYVRZAYP.mjs} +1 -1
  71. package/dist/{chunk-F442VZUL.mjs → chunk-KZ5GQE4Z.mjs} +5 -5
  72. package/dist/{chunk-WDEWU46C.mjs → chunk-KZQIVGW7.mjs} +2 -2
  73. package/dist/{chunk-7AJAW6NN.mjs → chunk-L7TIAYPX.mjs} +8 -8
  74. package/dist/{chunk-GRFXSEFF.mjs → chunk-LDGGR7YT.mjs} +4 -81
  75. package/dist/{chunk-WMIV7PJ7.mjs → chunk-LJ4ENXOD.mjs} +15 -14
  76. package/dist/chunk-LQG5G2LY.mjs +39 -0
  77. package/dist/{chunk-IQHZMCLX.mjs → chunk-MDF6PDIA.mjs} +2 -0
  78. package/dist/{chunk-QXH2CS4V.mjs → chunk-NVTFQFUL.mjs} +7 -26
  79. package/dist/{chunk-5K2JHKM3.mjs → chunk-NYSONUB7.mjs} +1 -1
  80. package/dist/{chunk-OH7SX5MY.mjs → chunk-O4KUNDVF.mjs} +12 -10
  81. package/dist/{chunk-EBIHRL7B.mjs → chunk-Q2UL2TPC.mjs} +1 -1
  82. package/dist/{chunk-RNUP6WZ6.mjs → chunk-QEYF2SQ6.mjs} +2 -2
  83. package/dist/{chunk-RVBUQITB.mjs → chunk-QN5WVV5K.mjs} +7 -7
  84. package/dist/{chunk-RIIUZJQ3.mjs → chunk-QYRNLLLI.mjs} +1 -1
  85. package/dist/{chunk-BTARFTEN.mjs → chunk-SOUZMPME.mjs} +4 -4
  86. package/dist/{chunk-ISUOSACK.mjs → chunk-T4CJDBBE.mjs} +1 -1
  87. package/dist/{chunk-DHEJAGVW.mjs → chunk-TTCKT2RT.mjs} +5 -5
  88. package/dist/{chunk-QU2XF7VI.mjs → chunk-U2726TL5.mjs} +5 -4
  89. package/dist/{chunk-LYKUJNAV.mjs → chunk-U46IZNCW.mjs} +2 -2
  90. package/dist/{chunk-SNB4DYL3.mjs → chunk-U6NE6RCV.mjs} +4 -4
  91. package/dist/{chunk-SA52N7G3.mjs → chunk-VP6XRMWR.mjs} +4 -4
  92. package/dist/{chunk-YUCGFPMS.mjs → chunk-VQDZVWN3.mjs} +449 -405
  93. package/dist/{chunk-IWVEG5CX.mjs → chunk-XIHUI4KB.mjs} +2 -2
  94. package/dist/{chunk-IV2XTSS2.mjs → chunk-XKPNKR3V.mjs} +16 -46
  95. package/dist/{chunk-UQS5PAS5.mjs → chunk-YGQOPB2Z.mjs} +1 -1
  96. package/dist/{chunk-SVO6NV4D.mjs → chunk-YKEHQUSZ.mjs} +4 -4
  97. package/dist/{chunk-6Z22L5RN.mjs → chunk-ZAYWOHVG.mjs} +1 -1
  98. package/dist/{chunk-QLDLQW36.mjs → chunk-ZEKDO7YX.mjs} +2 -2
  99. package/dist/chunk-ZXNNR73R.mjs +31 -0
  100. package/dist/{chunk-VJS4JFBQ.mjs → chunk-ZXX5GZEX.mjs} +3 -1
  101. package/dist/{chunk-X5SBWARI.mjs → chunk-ZYKUD5NX.mjs} +9 -7
  102. package/dist/{client-DoYPHcUn.d.ts → client-Cn3gNxUU.d.ts} +3 -3
  103. package/dist/client.d.ts +5 -5
  104. package/dist/client.mjs +2 -0
  105. package/dist/{cold-module-NUD332YD.mjs → cold-module-2WWGBFJC.mjs} +36 -33
  106. package/dist/{config-CuUfls_H.d.ts → config-CfhH1IWQ.d.ts} +1 -1
  107. package/dist/{config-Dzs46im6.d.ts → config-aIsy7mcL.d.ts} +52 -1
  108. package/dist/direct-executor-QLVHMELJ.mjs +24 -0
  109. package/dist/distribution/coordinator/etcd.mjs +1 -1
  110. package/dist/distribution/transport/grpc.mjs +1 -1
  111. package/dist/distribution.d.ts +4 -4
  112. package/dist/distribution.mjs +150 -255
  113. package/dist/{durability-wiring-K7RDXWL6.mjs → durability-wiring-GNWQSZI3.mjs} +67 -42
  114. package/dist/embeddings/openai.mjs +2 -2
  115. package/dist/{engine-C0ifUEcm.d.ts → engine-CJRUHXgf.d.ts} +1 -1
  116. package/dist/gzip-F4HMIEEQ.mjs +1 -0
  117. package/dist/import-DX55PN4G.mjs +41 -0
  118. package/dist/index.browser.mjs +7 -4
  119. package/dist/index.d.ts +539 -5
  120. package/dist/index.mjs +22 -16
  121. package/dist/index.react-native.mjs +19 -0
  122. package/dist/{long-query-thread-FKVHRCHF.mjs → long-query-thread-SCUE62TO.mjs} +20 -17
  123. package/dist/{module-GJUKW5VU.mjs → module-HW6IXUM6.mjs} +601 -108
  124. package/dist/{module-TMFIWOD2.mjs → module-V5K4ZP23.mjs} +29 -23
  125. package/dist/{paged-partition-archive-WNUEMYWM.mjs → paged-partition-archive-FDIIWZNR.mjs} +31 -25
  126. package/dist/partial-words.mjs +5 -5
  127. package/dist/pattern.mjs +3 -3
  128. package/dist/persistence/durability/checkpoint-worker.mjs +28 -22
  129. package/dist/{phrase-module-OMY3QQV7.mjs → phrase-module-OUQIRUQU.mjs} +1 -1
  130. package/dist/pool-UPV5PVPY.mjs +298 -0
  131. package/dist/react-native/chunks/analysis-change-HGW3L6NY.mjs +893 -0
  132. package/dist/react-native/chunks/analysis-change-entry-BNRU6STO.mjs +1 -0
  133. package/dist/react-native/chunks/chunk-2J2727V4.mjs +495 -0
  134. package/dist/react-native/chunks/chunk-3CTWSQRW.mjs +1251 -0
  135. package/dist/react-native/chunks/chunk-5FV4ZEVA.mjs +2619 -0
  136. package/dist/react-native/chunks/chunk-7VWQLEFV.mjs +642 -0
  137. package/dist/react-native/chunks/chunk-AAAETEJK.mjs +27 -0
  138. package/dist/react-native/chunks/chunk-AM7RSBPF.mjs +32 -0
  139. package/dist/react-native/chunks/chunk-B6OD63Z3.mjs +997 -0
  140. package/dist/react-native/chunks/chunk-BRNAVOKP.mjs +177 -0
  141. package/dist/react-native/chunks/chunk-DKJTZ625.mjs +207 -0
  142. package/dist/react-native/chunks/chunk-FKGG4UOF.mjs +177 -0
  143. package/dist/{browser/chunk-K7UFXEJS.mjs → react-native/chunks/chunk-GV5KFQ6M.mjs} +12 -6
  144. package/dist/react-native/chunks/chunk-HURV3ASY.mjs +4416 -0
  145. package/dist/react-native/chunks/chunk-IO2SMUQD.mjs +10 -0
  146. package/dist/react-native/chunks/chunk-JV6VQY2S.mjs +17 -0
  147. package/dist/react-native/chunks/chunk-KUJOBRKS.mjs +217 -0
  148. package/dist/react-native/chunks/chunk-KWRW5U65.mjs +226 -0
  149. package/dist/react-native/chunks/chunk-L4M7QJNP.mjs +110 -0
  150. package/dist/react-native/chunks/chunk-LQ6Q4L5B.mjs +360 -0
  151. package/dist/react-native/chunks/chunk-LQAKO5QV.mjs +523 -0
  152. package/dist/react-native/chunks/chunk-LRGWK367.mjs +11 -0
  153. package/dist/react-native/chunks/chunk-LVIKKAA6.mjs +93 -0
  154. package/dist/react-native/chunks/chunk-NFGBOI46.mjs +14 -0
  155. package/dist/react-native/chunks/chunk-NNE4VDMM.mjs +8 -0
  156. package/dist/react-native/chunks/chunk-PPEPTUVV.mjs +56 -0
  157. package/dist/react-native/chunks/chunk-PSWKQVVT.mjs +9 -0
  158. package/dist/react-native/chunks/chunk-PU55IOG4.mjs +910 -0
  159. package/dist/react-native/chunks/chunk-QC4ZRBSX.mjs +270 -0
  160. package/dist/react-native/chunks/chunk-R4EEMYM6.mjs +21 -0
  161. package/dist/react-native/chunks/chunk-R4OPUZQ5.mjs +86 -0
  162. package/dist/react-native/chunks/chunk-SLGIAD6B.mjs +2634 -0
  163. package/dist/react-native/chunks/chunk-TJMSKBEM.mjs +25 -0
  164. package/dist/react-native/chunks/chunk-TLYD7IJ2.mjs +23 -0
  165. package/dist/react-native/chunks/chunk-UFNCLEQM.mjs +73 -0
  166. package/dist/react-native/chunks/chunk-YH7W556M.mjs +179 -0
  167. package/dist/react-native/chunks/chunk-YJSMEKRE.mjs +22385 -0
  168. package/dist/react-native/chunks/chunk-ZCKNBPQ4.mjs +79 -0
  169. package/dist/react-native/chunks/cold-module-TSFWLYWC.mjs +755 -0
  170. package/dist/react-native/chunks/durability-wiring-PZR6KOQK.mjs +4509 -0
  171. package/dist/react-native/chunks/gzip.react-native-7JWCLVOB.mjs +3 -0
  172. package/dist/react-native/chunks/import-VTODLPLO.mjs +22 -0
  173. package/dist/{browser/module-Q6VFGLMG.mjs → react-native/chunks/module-JBBH6PZH.mjs} +609 -144
  174. package/dist/react-native/chunks/module-VEK2KCKU.mjs +25 -0
  175. package/dist/react-native/chunks/phrase-module-76UAPPSG.mjs +329 -0
  176. package/dist/react-native/chunks/pool-CD4ANYUI.mjs +296 -0
  177. package/dist/react-native/chunks/release-D7ZTIANM.mjs +33 -0
  178. package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
  179. package/dist/{browser/restore-queue-SOAHKZSQ.mjs → react-native/chunks/restore-queue-OI5EH7R3.mjs} +2 -2
  180. package/dist/react-native/chunks/runtime-3MCDLJAF.mjs +559 -0
  181. package/dist/react-native/chunks/snapshot-AUZMEV36.mjs +1205 -0
  182. package/dist/react-native/chunks/write-DDC2QBRJ.mjs +197 -0
  183. package/dist/react-native/partial-words.mjs +733 -0
  184. package/dist/react-native/pattern.mjs +2442 -0
  185. package/dist/react-native/synonyms.mjs +460 -0
  186. package/dist/react.d.ts +7 -7
  187. package/dist/react.mjs +2 -0
  188. package/dist/{rerank-ZZ5CHKLW.mjs → rerank-3W54N6P2.mjs} +4 -4
  189. package/dist/rerankers/http.mjs +2 -2
  190. package/dist/restore-queue-BQWWNNQI.mjs +41 -0
  191. package/dist/{runtime-RAOCKSYA.mjs → runtime-L2DZYY6N.mjs} +5 -5
  192. package/dist/server.d.ts +8 -8
  193. package/dist/server.mjs +32 -28
  194. package/dist/{snapshot-DB5ZVCDN.mjs → snapshot-EHXTOTW5.mjs} +39 -33
  195. package/dist/synonyms.mjs +3 -3
  196. package/dist/{tasks-ChWp0wrF.d.ts → tasks-C4ElXr4P.d.ts} +2 -2
  197. package/dist/thread-I7DVHHW7.mjs +31 -0
  198. package/dist/vector/search-worker.mjs +7 -5
  199. package/dist/workers/entry.mjs +9 -9
  200. package/dist/{write-KZVTLGCI.mjs → write-SDJXXQ2G.mjs} +29 -23
  201. package/package.json +88 -7
  202. package/dist/analysis-change-D5APH4Z3.mjs +0 -34
  203. package/dist/browser/import-FTMLTPGQ.mjs +0 -9
  204. package/dist/direct-executor-FDQYQEJQ.mjs +0 -22
  205. package/dist/import-6KXFY6XI.mjs +0 -35
  206. package/dist/thread-OV6T555H.mjs +0 -28
  207. /package/dist/{chunk-22YPZVMG.mjs → chunk-ZXNQEW43.mjs} +0 -0
@@ -0,0 +1,893 @@
1
+ import { requireAnalysisRebuildMemory } from './chunk-FKGG4UOF.mjs';
2
+ import { targetOfPending, configUnderAnalysis } from './chunk-TLYD7IJ2.mjs';
3
+ import { DEFAULT_WRITE_AHEAD_QUEUE_MAX_SIZE, rewriteSpillOf, emitEngineEvent, reconstructSchemaFromMetadata, prepareStoredSynonyms, rulesOfStoredSynonyms, resolvePartialWordAnalysis, requireConfigurationWithinSizeLimit, prepareConfiguredSynonyms, createPartitionIndex, REBALANCE_CHUNK_SIZE, yieldToEventLoop, prepareDocumentVectors, removeDocumentVectors, insertDocumentVectors, vectorsEqual, rebuildTextIndexes, textRebuildTargetOf, extractVectorFromDoc, deleteNestedValue, ARRAY_POSITION_GAP } from './chunk-YJSMEKRE.mjs';
4
+ import './chunk-LQAKO5QV.mjs';
5
+ import './chunk-NNE4VDMM.mjs';
6
+ import './chunk-L4M7QJNP.mjs';
7
+ import './chunk-IO2SMUQD.mjs';
8
+ import { ANALYSIS_CHANGE_MEMBERS, ANALYSIS_HOLD_APPLY_INTERVAL_MS } from './chunk-AM7RSBPF.mjs';
9
+ import './chunk-ZCKNBPQ4.mjs';
10
+ import './chunk-PU55IOG4.mjs';
11
+ import './chunk-TJMSKBEM.mjs';
12
+ import './chunk-DKJTZ625.mjs';
13
+ import { getLanguage, resolveIndexAnalysis } from './chunk-3CTWSQRW.mjs';
14
+ import './chunk-2J2727V4.mjs';
15
+ import './chunk-KWRW5U65.mjs';
16
+ import { compareCodePoints } from './chunk-PPEPTUVV.mjs';
17
+ import './chunk-LVIKKAA6.mjs';
18
+ import './chunk-AAAETEJK.mjs';
19
+ import './chunk-JV6VQY2S.mjs';
20
+ import './chunk-NFGBOI46.mjs';
21
+ import { NarsilError, ErrorCodes } from './chunk-YH7W556M.mjs';
22
+
23
+ // src/engine/analysis-change/run.ts
24
+ function createHold(generation) {
25
+ let markReady = () => void 0;
26
+ let refuse = () => void 0;
27
+ let wake = () => void 0;
28
+ const ready = new Promise((resolve, reject) => {
29
+ markReady = resolve;
30
+ refuse = reject;
31
+ });
32
+ ready.catch(() => void 0);
33
+ return {
34
+ generation,
35
+ built: false,
36
+ released: false,
37
+ ready,
38
+ markReady,
39
+ refuse,
40
+ wake: () => wake(),
41
+ pause: () => new Promise((resolve) => {
42
+ const timer = setTimeout(resolve, ANALYSIS_HOLD_APPLY_INTERVAL_MS);
43
+ wake = () => {
44
+ clearTimeout(timer);
45
+ resolve();
46
+ };
47
+ })
48
+ };
49
+ }
50
+ var runsByCore = /* @__PURE__ */ new WeakMap();
51
+ function runsOf(core) {
52
+ let runs = runsByCore.get(core);
53
+ if (runs === void 0) {
54
+ runs = /* @__PURE__ */ new Map();
55
+ runsByCore.set(core, runs);
56
+ }
57
+ return runs;
58
+ }
59
+ function createRun(target, hold = null) {
60
+ let finish = () => void 0;
61
+ let rejectFinished = () => void 0;
62
+ let settle = () => void 0;
63
+ const finished = new Promise((resolve, reject) => {
64
+ finish = resolve;
65
+ rejectFinished = reject;
66
+ });
67
+ finished.catch(() => void 0);
68
+ const settled = new Promise((resolve) => {
69
+ settle = resolve;
70
+ });
71
+ const fail = (error) => {
72
+ rejectFinished(error);
73
+ hold?.refuse(error);
74
+ hold?.wake();
75
+ };
76
+ return { target, stopReason: null, switched: false, settled, finish, fail, finished, settle, hold };
77
+ }
78
+ async function waitForRelease(run, hold, keepCurrent) {
79
+ hold.built = true;
80
+ hold.markReady();
81
+ while (!hold.released) {
82
+ if (run.stopReason !== null || !keepCurrent()) {
83
+ hold.built = false;
84
+ return false;
85
+ }
86
+ await hold.pause();
87
+ }
88
+ return true;
89
+ }
90
+ function stopRun(run, indexName) {
91
+ run.stopReason = new NarsilError(
92
+ ErrorCodes.ANALYSIS_CHANGE_REPLACED,
93
+ `A later change or a restore replaced this analysis change of index "${indexName}"`,
94
+ { indexName }
95
+ );
96
+ run.fail(run.stopReason);
97
+ }
98
+
99
+ // src/engine/analysis-change/settings.ts
100
+ function refuseTokenizer(indexName) {
101
+ throw new NarsilError(
102
+ ErrorCodes.CONFIG_INVALID,
103
+ `The tokenizer of index "${indexName}" must change to a name registered with registerTokenizer, because an instance survives neither a worker boundary nor a restart`,
104
+ { indexName }
105
+ );
106
+ }
107
+ function refuseStopWords(indexName) {
108
+ throw new NarsilError(
109
+ ErrorCodes.CONFIG_INVALID,
110
+ `The stop words of index "${indexName}" must change to a name registered with registerStopWords or to a list of words, because a function survives neither a worker boundary nor a restart`,
111
+ { indexName }
112
+ );
113
+ }
114
+ function stopWordsOfChange(indexName, value) {
115
+ if (value === void 0 || value === null || typeof value === "string") return value;
116
+ const words = value instanceof Set ? [...value] : value;
117
+ if (!Array.isArray(words) || words.some((word) => typeof word !== "string")) refuseStopWords(indexName);
118
+ return new Set(words);
119
+ }
120
+ function tokenizerOfChange(indexName, value) {
121
+ if (value === void 0 || value === null || typeof value === "string") return value;
122
+ refuseTokenizer(indexName);
123
+ }
124
+ function requireRegisteredLanguage(name) {
125
+ if (typeof name !== "string") {
126
+ throw new NarsilError(ErrorCodes.LANGUAGE_NOT_SUPPORTED, "The language of an analysis change must be a name");
127
+ }
128
+ return getLanguage(name);
129
+ }
130
+ function resolveAnalysisTarget(indexName, base, change) {
131
+ if (change === null || typeof change !== "object" || Array.isArray(change)) {
132
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "An analysis change must be an object", { indexName });
133
+ }
134
+ const unknownMember = Object.keys(change).find((member) => !ANALYSIS_CHANGE_MEMBERS.includes(member));
135
+ if (unknownMember !== void 0) {
136
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `"${unknownMember}" is not an analysis setting`, { indexName });
137
+ }
138
+ const language = change.language === void 0 ? getLanguage(base.language ?? "english") : requireRegisteredLanguage(change.language);
139
+ const tokenizer = tokenizerOfChange(indexName, change.tokenizer);
140
+ const stopWords = stopWordsOfChange(indexName, change.stopWords);
141
+ const config = configUnderAnalysis(
142
+ base,
143
+ language.name,
144
+ tokenizer === void 0 ? base.tokenizer : tokenizer ?? void 0,
145
+ stopWords === void 0 ? base.stopWords : stopWords ?? void 0
146
+ );
147
+ requireConfigurationWithinSizeLimit(config, language);
148
+ return { config, language, analysis: resolveIndexAnalysis(config) };
149
+ }
150
+ function currentConfigUnderTarget(current, target) {
151
+ return configUnderAnalysis(current, target.language.name, target.config.tokenizer, target.config.stopWords);
152
+ }
153
+ function sameStopWords(left, right) {
154
+ if (left === right) return true;
155
+ if (!(left instanceof Set) || !(right instanceof Set) || left.size !== right.size) return false;
156
+ for (const word of left) if (!right.has(word)) return false;
157
+ return true;
158
+ }
159
+ function holdsSettings(config, target) {
160
+ return (config.language ?? "english") === target.language.name && config.tokenizer === target.config.tokenizer && sameStopWords(config.stopWords, target.config.stopWords);
161
+ }
162
+ function namedSettingsOf(config, language) {
163
+ return {
164
+ language,
165
+ ...typeof config.tokenizer === "string" ? { tokenizer: config.tokenizer } : {},
166
+ ...typeof config.stopWords === "string" ? { stopWords: config.stopWords } : {},
167
+ ...config.stopWords instanceof Set ? { stopWordList: [...config.stopWords].sort(compareCodePoints) } : {}
168
+ };
169
+ }
170
+ function durableSettingsOf(entry) {
171
+ return {
172
+ ...namedSettingsOf(entry.config, entry.language.name),
173
+ generation: entry.analysisGeneration,
174
+ analysisRevision: entry.analysisRevision,
175
+ arrayPositionGap: entry.arrayPositionGap
176
+ };
177
+ }
178
+ function requireSynonymsUnderTarget(entry, target) {
179
+ if (entry.synonyms === null) return;
180
+ prepareConfiguredSynonyms({ ...target.config, synonyms: rulesOfStoredSynonyms(entry.synonyms) }, target.language);
181
+ }
182
+
183
+ // src/partitioning/rebalancer.ts
184
+ function createRebalancer() {
185
+ const activeManagers = /* @__PURE__ */ new WeakSet();
186
+ let activeCount = 0;
187
+ async function rebalance(manager, newPartitionCount, router, onProgress, spill = null) {
188
+ if (activeManagers.has(manager)) {
189
+ throw new NarsilError(
190
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
191
+ "A rebalance operation is already in progress"
192
+ );
193
+ }
194
+ if (newPartitionCount <= 0) {
195
+ throw new NarsilError(
196
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
197
+ `New partition count must be greater than 0, received ${newPartitionCount}`,
198
+ { newPartitionCount }
199
+ );
200
+ }
201
+ if (newPartitionCount === manager.partitionCount) {
202
+ throw new NarsilError(
203
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
204
+ `New partition count (${newPartitionCount}) is the same as the current count`,
205
+ { newPartitionCount, currentPartitionCount: manager.partitionCount }
206
+ );
207
+ }
208
+ activeManagers.add(manager);
209
+ activeCount++;
210
+ try {
211
+ const currentPartitions = manager.getAllPartitions();
212
+ let documentsTotal = 0;
213
+ for (const partition of currentPartitions) {
214
+ documentsTotal += partition.count();
215
+ }
216
+ onProgress?.({
217
+ phase: "scanning",
218
+ documentsProcessed: 0,
219
+ documentsTotal
220
+ });
221
+ const newPartitions = [];
222
+ for (let i = 0; i < newPartitionCount; i++) {
223
+ newPartitions.push(spill?.newPartition(i) ?? createPartitionIndex(i, manager.config.trackPositions ?? true));
224
+ }
225
+ let documentsProcessed = 0;
226
+ let chunkFill = 0;
227
+ const insertOptions = {
228
+ validate: false,
229
+ skipClone: true,
230
+ stopWordOverride: manager.analysis.stopWords,
231
+ customTokenizer: manager.analysis.customTokenizer,
232
+ collectSurfaces: manager.config.surfaceForms !== false
233
+ };
234
+ for (const partition of currentPartitions) {
235
+ const docIds = [...partition.docIds()];
236
+ for (const docId of docIds) {
237
+ const document = partition.getRef(docId);
238
+ if (!document) continue;
239
+ const targetPartitionId = router.route(docId, newPartitionCount);
240
+ const targetPartition = newPartitions[targetPartitionId];
241
+ if (!targetPartition.has(docId)) {
242
+ targetPartition.insert(docId, document, manager.schema, manager.language, insertOptions);
243
+ documentsProcessed++;
244
+ }
245
+ chunkFill++;
246
+ if (chunkFill >= REBALANCE_CHUNK_SIZE) {
247
+ chunkFill = 0;
248
+ onProgress?.({
249
+ phase: "moving",
250
+ documentsProcessed,
251
+ documentsTotal
252
+ });
253
+ for (let at = 0; at < newPartitions.length; at++) await spill?.spillIfFull(newPartitions[at], at);
254
+ await yieldToEventLoop();
255
+ }
256
+ }
257
+ }
258
+ if (chunkFill > 0) {
259
+ onProgress?.({
260
+ phase: "moving",
261
+ documentsProcessed,
262
+ documentsTotal
263
+ });
264
+ await yieldToEventLoop();
265
+ }
266
+ onProgress?.({
267
+ phase: "swapping",
268
+ documentsProcessed,
269
+ documentsTotal
270
+ });
271
+ manager.setPartitions(newPartitions);
272
+ onProgress?.({
273
+ phase: "complete",
274
+ documentsProcessed,
275
+ documentsTotal
276
+ });
277
+ } finally {
278
+ activeManagers.delete(manager);
279
+ activeCount--;
280
+ }
281
+ }
282
+ return {
283
+ rebalance,
284
+ isRebalancing() {
285
+ return activeCount > 0;
286
+ }
287
+ };
288
+ }
289
+
290
+ // src/partitioning/write-ahead-queue.ts
291
+ function createWriteAheadQueue(maxSize = DEFAULT_WRITE_AHEAD_QUEUE_MAX_SIZE) {
292
+ const entries = [];
293
+ const docStates = /* @__PURE__ */ new Map();
294
+ let nextSequence = 1;
295
+ let resolveReplayed = () => void 0;
296
+ const replayed = new Promise((resolve) => {
297
+ resolveReplayed = resolve;
298
+ });
299
+ return {
300
+ push(entry) {
301
+ if (entries.length >= maxSize) {
302
+ throw new NarsilError(
303
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
304
+ `Write-ahead queue is full (${maxSize} entries). Retry after rebalancing completes.`,
305
+ { maxSize, currentSize: entries.length }
306
+ );
307
+ }
308
+ const seq = nextSequence++;
309
+ entries.push({ ...entry, sequenceNumber: seq });
310
+ docStates.set(entry.docId, entry.action === "remove" ? "absent" : "present");
311
+ return seq;
312
+ },
313
+ drain() {
314
+ const result = entries.splice(0);
315
+ result.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
316
+ return result;
317
+ },
318
+ clear() {
319
+ entries.length = 0;
320
+ docStates.clear();
321
+ },
322
+ bufferedDocState(docId) {
323
+ return docStates.get(docId);
324
+ },
325
+ whenReplayed() {
326
+ return replayed;
327
+ },
328
+ markReplayed() {
329
+ resolveReplayed();
330
+ },
331
+ get size() {
332
+ return entries.length;
333
+ },
334
+ get isFull() {
335
+ return entries.length >= maxSize;
336
+ }
337
+ };
338
+ }
339
+
340
+ // src/engine/rebalance-executor.ts
341
+ function warnHookError(hook, err) {
342
+ console.warn(`${hook} plugin hook error:`, err instanceof Error ? err.message : String(err));
343
+ }
344
+ function applyReplayUpdateVectors(entry, vectorFieldPaths) {
345
+ const updateVectors = /* @__PURE__ */ new Map();
346
+ for (const fieldPath of vectorFieldPaths) {
347
+ updateVectors.set(fieldPath, extractVectorFromDoc(entry.document, fieldPath));
348
+ }
349
+ let updateDoc = entry.document;
350
+ if (updateVectors.size > 0) {
351
+ updateDoc = structuredClone(entry.document);
352
+ for (const fieldPath of updateVectors.keys()) {
353
+ deleteNestedValue(updateDoc, fieldPath);
354
+ }
355
+ }
356
+ return { updateDoc, updateVectors };
357
+ }
358
+ async function replayEntry(manager, indexName, entry, ctx) {
359
+ const vecIndexes = manager.getVectorIndexes();
360
+ const vectorFieldPaths = ctx.requireIndex(indexName).vectorFieldPaths;
361
+ if (entry.action === "insert" && entry.document) {
362
+ const { partitionDoc, extractedVectors } = prepareDocumentVectors(
363
+ entry.document,
364
+ vectorFieldPaths
365
+ );
366
+ const overwrote = manager.has(entry.docId);
367
+ const removedSpellings = overwrote ? manager.removedSpellingsOf(entry.docId) : void 0;
368
+ if (overwrote) {
369
+ removeDocumentVectors(entry.docId, vecIndexes);
370
+ manager.remove(entry.docId, removedSpellings);
371
+ }
372
+ manager.insert(entry.docId, partitionDoc);
373
+ if (extractedVectors.size > 0) {
374
+ insertDocumentVectors(entry.docId, extractedVectors, vecIndexes, manager.partitionIdOf(entry.docId));
375
+ for (const fieldPath of extractedVectors.keys()) {
376
+ vecIndexes.get(fieldPath)?.scheduleBuild();
377
+ }
378
+ }
379
+ try {
380
+ await ctx.pluginRegistry.runHook("afterInsert", { indexName, docId: entry.docId, document: entry.document });
381
+ } catch (err) {
382
+ warnHookError("afterInsert", err);
383
+ }
384
+ const replicated = {
385
+ indexName,
386
+ docId: entry.docId,
387
+ document: entry.document,
388
+ requestId: `replicate-insert-${entry.docId}`
389
+ };
390
+ await ctx.orchestrator.replicateToWorkers(
391
+ overwrote ? { type: "update", ...replicated, removedSpellings } : { type: "insert", ...replicated }
392
+ );
393
+ return;
394
+ }
395
+ if (entry.action === "remove") {
396
+ const removedSpellings = manager.removedSpellingsOf(entry.docId);
397
+ manager.remove(entry.docId, removedSpellings);
398
+ removeDocumentVectors(entry.docId, vecIndexes);
399
+ try {
400
+ await ctx.pluginRegistry.runHook("afterRemove", { indexName, docId: entry.docId });
401
+ } catch (err) {
402
+ warnHookError("afterRemove", err);
403
+ }
404
+ await ctx.orchestrator.replicateToWorkers({
405
+ type: "remove",
406
+ indexName,
407
+ docId: entry.docId,
408
+ requestId: `replicate-remove-${entry.docId}`,
409
+ removedSpellings
410
+ });
411
+ return;
412
+ }
413
+ if (entry.action === "update" && entry.document) {
414
+ const oldDocument = manager.get(entry.docId);
415
+ const { updateDoc, updateVectors } = applyReplayUpdateVectors(entry, vectorFieldPaths);
416
+ const removedSpellings = manager.removedSpellingsOf(entry.docId);
417
+ if (manager.has(entry.docId)) {
418
+ manager.update(entry.docId, updateDoc, void 0, removedSpellings);
419
+ } else {
420
+ manager.insert(entry.docId, updateDoc);
421
+ }
422
+ for (const [fieldPath, newVec] of updateVectors) {
423
+ const vecIndex = vecIndexes.get(fieldPath);
424
+ if (!vecIndex) continue;
425
+ if (newVec === null) {
426
+ if (vecIndex.has(entry.docId)) {
427
+ vecIndex.remove(entry.docId);
428
+ }
429
+ } else {
430
+ const oldVec = vecIndex.getVector(entry.docId);
431
+ if (!vectorsEqual(oldVec, newVec)) {
432
+ if (vecIndex.has(entry.docId)) {
433
+ vecIndex.remove(entry.docId);
434
+ }
435
+ vecIndex.insert(entry.docId, newVec);
436
+ vecIndex.scheduleBuild();
437
+ }
438
+ }
439
+ }
440
+ try {
441
+ await ctx.pluginRegistry.runHook("afterUpdate", {
442
+ indexName,
443
+ docId: entry.docId,
444
+ oldDocument: oldDocument ?? {},
445
+ newDocument: entry.document
446
+ });
447
+ } catch (err) {
448
+ warnHookError("afterUpdate", err);
449
+ }
450
+ await ctx.orchestrator.replicateToWorkers({
451
+ type: "update",
452
+ indexName,
453
+ docId: entry.docId,
454
+ document: entry.document,
455
+ requestId: `replicate-update-${entry.docId}`,
456
+ removedSpellings
457
+ });
458
+ }
459
+ }
460
+ async function replayQueued(manager, indexName, waq, ctx) {
461
+ while (true) {
462
+ const entries = waq.drain();
463
+ if (entries.length === 0) break;
464
+ for (const entry of entries) {
465
+ try {
466
+ await replayEntry(manager, indexName, entry, ctx);
467
+ } catch (replayErr) {
468
+ const isMissing = replayErr instanceof NarsilError && replayErr.code === ErrorCodes.DOC_NOT_FOUND;
469
+ if (!isMissing) {
470
+ console.warn(`WAQ replay failed for ${entry.action} on doc "${entry.docId}":`, replayErr);
471
+ }
472
+ }
473
+ }
474
+ }
475
+ }
476
+ async function executeRebalance(manager, indexName, targetPartitionCount, ctx) {
477
+ if (targetPartitionCount <= 0 || !Number.isInteger(targetPartitionCount)) {
478
+ throw new NarsilError(
479
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
480
+ `Target partition count must be a positive integer, got ${targetPartitionCount}`,
481
+ { targetPartitionCount }
482
+ );
483
+ }
484
+ const entry = ctx.requireIndex(indexName);
485
+ const maxPartitions = entry.config.partitions?.maxPartitions;
486
+ if (maxPartitions !== void 0 && targetPartitionCount > maxPartitions) {
487
+ throw new NarsilError(
488
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
489
+ `Target partition count (${targetPartitionCount}) is above the maximum of ${maxPartitions} partitions`,
490
+ { targetPartitionCount, maxPartitions }
491
+ );
492
+ }
493
+ if (ctx.rebalancingIndexes.has(indexName)) {
494
+ throw new NarsilError(
495
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
496
+ `Index "${indexName}" is already being rebalanced`
497
+ );
498
+ }
499
+ const registered = ctx.requireIndex(indexName);
500
+ if (registered.pendingAnalysis !== null || registered.durableAnalysis !== null) {
501
+ throw new NarsilError(
502
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
503
+ `Index "${indexName}" is changing its analysis settings, so try the rebalance again once the change ends`,
504
+ { indexName }
505
+ );
506
+ }
507
+ if (targetPartitionCount === manager.partitionCount) {
508
+ return;
509
+ }
510
+ const waq = createWriteAheadQueue();
511
+ ctx.waqMap.set(indexName, waq);
512
+ ctx.rebalancingIndexes.add(indexName);
513
+ ctx.rebalanceTargets.set(indexName, targetPartitionCount);
514
+ const wasPromoted = ctx.orchestrator.desyncIndex(indexName);
515
+ let spill = null;
516
+ try {
517
+ const oldCount = manager.partitionCount;
518
+ spill = await rewriteSpillOf(ctx.durabilityManager?.rewriteStorage?.(), manager);
519
+ await createRebalancer().rebalance(
520
+ manager,
521
+ targetPartitionCount,
522
+ ctx.router,
523
+ (progress) => {
524
+ if (progress.phase === "complete") {
525
+ emitEngineEvent(ctx.eventHandlers, "partitionRebalance", {
526
+ indexName,
527
+ oldCount,
528
+ newCount: targetPartitionCount
529
+ });
530
+ }
531
+ },
532
+ spill
533
+ );
534
+ try {
535
+ await ctx.pluginRegistry.runHook("onPartitionSplit", {
536
+ indexName,
537
+ oldPartitionCount: oldCount,
538
+ newPartitionCount: targetPartitionCount
539
+ });
540
+ } catch (hookErr) {
541
+ console.warn("onPartitionSplit plugin hook failed:", hookErr instanceof Error ? hookErr.message : String(hookErr));
542
+ }
543
+ await replayQueued(manager, indexName, waq, ctx);
544
+ if (ctx.durabilityManager) {
545
+ await ctx.durabilityManager.persistMetadata(indexName);
546
+ await (ctx.durabilityManager.checkpointFromMemory?.(indexName) ?? ctx.durabilityManager.checkpoint(indexName));
547
+ await spill?.removeUnread(manager);
548
+ await replayQueued(manager, indexName, waq, ctx);
549
+ }
550
+ await ctx.orchestrator.resyncIndex(indexName, wasPromoted);
551
+ do {
552
+ await replayQueued(manager, indexName, waq, ctx);
553
+ } while (waq.size > 0);
554
+ } catch (err) {
555
+ await spill?.removeUnread(manager).catch(() => void 0);
556
+ try {
557
+ await replayQueued(manager, indexName, waq, ctx);
558
+ await ctx.orchestrator.resyncIndex(indexName, wasPromoted);
559
+ do {
560
+ await replayQueued(manager, indexName, waq, ctx);
561
+ } while (waq.size > 0);
562
+ } catch (recoveryErr) {
563
+ console.warn(
564
+ `Rebalance failure recovery for index "${indexName}" did not complete:`,
565
+ recoveryErr instanceof Error ? recoveryErr.message : String(recoveryErr)
566
+ );
567
+ }
568
+ throw err;
569
+ } finally {
570
+ ctx.rebalancingIndexes.delete(indexName);
571
+ ctx.rebalanceTargets.delete(indexName);
572
+ ctx.waqMap.delete(indexName);
573
+ waq.markReplayed();
574
+ }
575
+ ctx.checkWatermark(indexName);
576
+ }
577
+
578
+ // src/engine/analysis-change/adopt-saved.ts
579
+ function adoptSavedAnalysis(manager, entry, metadata) {
580
+ const saved = reconstructSchemaFromMetadata(metadata);
581
+ const { tokenizer: _tokenizer, stopWords: _stopWords, ...rest } = entry.config;
582
+ const config = {
583
+ ...rest,
584
+ language: saved.language,
585
+ ...saved.tokenizer !== void 0 ? { tokenizer: saved.tokenizer } : {},
586
+ ...saved.stopWords !== void 0 ? { stopWords: saved.stopWords } : {}
587
+ };
588
+ const language = getLanguage(config.language ?? "english");
589
+ const analysis = resolveIndexAnalysis(config);
590
+ const synonyms = prepareStoredSynonyms(rulesOfStoredSynonyms(entry.synonyms), config, language);
591
+ entry.config = config;
592
+ entry.language = language;
593
+ entry.analysisGeneration = metadata.analysisGeneration ?? 0;
594
+ entry.analysisRevision = metadata.analysisRevision ?? null;
595
+ entry.arrayPositionGap = metadata.arrayPositionGap ?? null;
596
+ manager?.adoptAnalysis({
597
+ config,
598
+ language,
599
+ analysis,
600
+ partialWordAnalysis: resolvePartialWordAnalysis(config, language, analysis),
601
+ synonyms: synonyms?.lookup ?? null
602
+ });
603
+ }
604
+
605
+ // src/engine/analysis-change/index.ts
606
+ function termBytesOf(core, indexName) {
607
+ let bytes = 0;
608
+ for (const partition of core.requireManager(indexName).getAllPartitions()) {
609
+ bytes += partition.estimateMemoryBytes();
610
+ for (const state of partition.readStates()) bytes -= state.docStore.contentBytes();
611
+ }
612
+ return Math.max(0, bytes);
613
+ }
614
+ function toError(value) {
615
+ return value instanceof Error ? value : new Error(String(value));
616
+ }
617
+ async function requireNoUndurableSwitch(core, indexName, entry) {
618
+ if (entry.durableAnalysis === null) return;
619
+ const durable = core.durability?.manager;
620
+ await (durable?.checkpointFromMemory?.(indexName) ?? durable?.checkpoint(indexName));
621
+ if (entry.durableAnalysis === null) return;
622
+ throw new NarsilError(
623
+ ErrorCodes.ANALYSIS_CHANGE_NOT_READY,
624
+ `The previous analysis change of index "${indexName}" has no durable checkpoint yet, so try this change again once a checkpoint succeeds`,
625
+ { indexName }
626
+ );
627
+ }
628
+ function switchIndex({ core, indexName, entry, run, generation }) {
629
+ const { language, analysis } = run.target;
630
+ const config = currentConfigUnderTarget(entry.config, run.target);
631
+ const synonyms = prepareStoredSynonyms(rulesOfStoredSynonyms(entry.synonyms), config, language);
632
+ return () => {
633
+ run.switched = true;
634
+ entry.durableAnalysis = core.durability === null ? null : durableSettingsOf(entry);
635
+ entry.config = config;
636
+ entry.language = language;
637
+ entry.analysisGeneration = generation;
638
+ entry.analysisRevision = language.revision;
639
+ entry.arrayPositionGap = ARRAY_POSITION_GAP;
640
+ entry.pendingAnalysis = null;
641
+ entry.synonyms = synonyms?.encoded ?? null;
642
+ core.requireManager(indexName).adoptAnalysis({
643
+ ...textRebuildTargetOf(config, language, analysis),
644
+ synonyms: synonyms?.lookup ?? null
645
+ });
646
+ core.analysisRebuild.clearStale(indexName);
647
+ };
648
+ }
649
+ async function buildAndSwitch(scope) {
650
+ const { core, indexName, entry, run } = scope;
651
+ const { hold } = run;
652
+ const manager = core.requireManager(indexName);
653
+ const emit = (status, partitionsRebuilt2, error) => {
654
+ emitEngineEvent(core.eventHandlers, "analysisRebuild", {
655
+ indexName,
656
+ status,
657
+ partitionsRebuilt: partitionsRebuilt2,
658
+ partitionCount: manager.partitionCount,
659
+ ...error !== void 0 ? { error } : {}
660
+ });
661
+ };
662
+ let partitionsRebuilt = 0;
663
+ let adopt = () => void 0;
664
+ const isCurrent = () => run.stopReason === null && !core.shutdownState.isShutdown && core.indexRegistry.get(indexName) === entry && core.executor.getManager(indexName) === manager;
665
+ emit("started", 0);
666
+ const wasPromoted = core.orchestrator.desyncIndex(indexName);
667
+ try {
668
+ const durable = core.durability?.manager;
669
+ let spill = null;
670
+ const rebuilt = await rebuildTextIndexes(
671
+ manager,
672
+ {
673
+ isCurrent,
674
+ rewriteStorage: () => durable?.rewriteStorage?.(),
675
+ onRewriteSpill(kept) {
676
+ spill = kept;
677
+ },
678
+ onPartitionBuilt(built) {
679
+ partitionsRebuilt = built;
680
+ },
681
+ onSwitch: () => adopt(),
682
+ ...hold === null ? {} : { holdBeforeSwitch: (keepCurrent) => waitForRelease(run, hold, keepCurrent) },
683
+ runSwitch(swap) {
684
+ const swapWithSynonyms = () => {
685
+ if (!isCurrent()) return false;
686
+ adopt = switchIndex(scope);
687
+ return swap();
688
+ };
689
+ return durable?.checkpointAfterSwitch?.(indexName, swapWithSynonyms) ?? Promise.resolve(swapWithSynonyms());
690
+ }
691
+ },
692
+ textRebuildTargetOf(run.target.config, run.target.language, run.target.analysis)
693
+ );
694
+ if (!rebuilt) throw run.stopReason ?? stoppedError(core, indexName);
695
+ await spill?.removeUnread(manager);
696
+ if (entry.durableAnalysis !== null) {
697
+ throw new NarsilError(
698
+ ErrorCodes.PERSISTENCE_SAVE_FAILED,
699
+ `The new terms of index "${indexName}" answer queries, but they have no durable checkpoint yet`,
700
+ { indexName }
701
+ );
702
+ }
703
+ emit("completed", partitionsRebuilt);
704
+ } catch (error) {
705
+ emit("failed", partitionsRebuilt, toError(error));
706
+ throw error;
707
+ } finally {
708
+ if (core.executor.getManager(indexName) === manager) {
709
+ await core.orchestrator.resyncIndex(indexName, wasPromoted).catch(() => void 0);
710
+ }
711
+ }
712
+ }
713
+ function stoppedError(core, indexName) {
714
+ if (core.shutdownState.isShutdown) {
715
+ return new NarsilError(ErrorCodes.INSTANCE_SHUT_DOWN, "This Narsil instance has been shut down");
716
+ }
717
+ return new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" closed during its analysis change`, {
718
+ indexName
719
+ });
720
+ }
721
+ async function clearPending(core, indexName, entry) {
722
+ entry.pendingAnalysis = null;
723
+ await core.durability?.manager.persistMetadata(indexName);
724
+ }
725
+ function requireNotRebalancing(core, indexName) {
726
+ if (!core.rebalancingIndexes.has(indexName)) return;
727
+ throw new NarsilError(
728
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
729
+ `Index "${indexName}" is being rebalanced, so try its analysis change again once the rebalance ends`,
730
+ { indexName }
731
+ );
732
+ }
733
+ async function driveRun(core, indexName, entry, run, previous, resumedGeneration) {
734
+ const runs = runsOf(core);
735
+ try {
736
+ if (previous !== void 0) await previous.settled;
737
+ if (runs.get(indexName) !== run) return;
738
+ await requireNoUndurableSwitch(core, indexName, entry);
739
+ if (run.stopReason !== null) return;
740
+ if (core.indexRegistry.get(indexName) !== entry) throw stoppedError(core, indexName);
741
+ requireNotRebalancing(core, indexName);
742
+ if (resumedGeneration === null && holdsSettings(entry.config, run.target)) {
743
+ if (entry.pendingAnalysis !== null) await clearPending(core, indexName, entry);
744
+ run.finish();
745
+ return;
746
+ }
747
+ const generation = resumedGeneration ?? Math.max(entry.analysisGeneration, entry.pendingAnalysis?.generation ?? 0) + 1;
748
+ if (entry.pendingAnalysis?.generation !== generation || run.hold !== null) {
749
+ entry.pendingAnalysis = { generation, ...namedSettingsOf(run.target.config, run.target.language.name) };
750
+ await core.durability?.manager.persistMetadata(indexName);
751
+ }
752
+ if (run.stopReason !== null) return;
753
+ await core.analysisRebuild.abandon(indexName);
754
+ await core.analysisRebuild.runExclusive(
755
+ indexName,
756
+ () => buildAndSwitch({ core, indexName, entry, run, generation }),
757
+ run.hold?.ready
758
+ );
759
+ run.finish();
760
+ } catch (error) {
761
+ run.fail(error);
762
+ const keepsPending = resumedGeneration !== null || core.shutdownState.isShutdown;
763
+ const stillOwnsPending = runs.get(indexName) === run && run.stopReason === null;
764
+ if (!keepsPending && stillOwnsPending && core.indexRegistry.get(indexName) === entry) {
765
+ if (entry.pendingAnalysis !== null) await clearPending(core, indexName, entry).catch(() => void 0);
766
+ }
767
+ } finally {
768
+ if (runs.get(indexName) === run) runs.delete(indexName);
769
+ run.settle();
770
+ }
771
+ }
772
+ async function changeIndexAnalysis(core, indexName, change) {
773
+ const entry = core.requireIndex(indexName);
774
+ requireNotRebalancing(core, indexName);
775
+ const target = resolveAnalysisTarget(indexName, entry.config, change);
776
+ const runs = runsOf(core);
777
+ const previous = runs.get(indexName);
778
+ if (previous !== void 0 && holdsSettings(previous.target.config, target)) return previous.finished;
779
+ const heldAlready = holdsSettings(entry.config, target);
780
+ if (previous === void 0 && entry.durableAnalysis === null && heldAlready) return;
781
+ if (!heldAlready) {
782
+ requireSynonymsUnderTarget(entry, target);
783
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
784
+ }
785
+ const run = createRun(target);
786
+ if (previous !== void 0 && !previous.switched) stopRun(previous, indexName);
787
+ runs.set(indexName, run);
788
+ void driveRun(core, indexName, entry, run, previous, null);
789
+ return run.finished;
790
+ }
791
+ function resumeAnalysisChange(core, indexName) {
792
+ const entry = core.requireIndex(indexName);
793
+ const runs = runsOf(core);
794
+ const running = runs.get(indexName);
795
+ if (running !== void 0) return running.finished;
796
+ const pending = entry.pendingAnalysis;
797
+ if (pending === null) return Promise.resolve();
798
+ let target;
799
+ try {
800
+ target = targetOfPending(entry, pending);
801
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
802
+ } catch (error) {
803
+ emitEngineEvent(core.eventHandlers, "analysisRebuild", {
804
+ indexName,
805
+ status: "failed",
806
+ partitionsRebuilt: 0,
807
+ partitionCount: entry.partitionCount,
808
+ error: toError(error)
809
+ });
810
+ return Promise.reject(error);
811
+ }
812
+ const run = createRun(target);
813
+ runs.set(indexName, run);
814
+ void driveRun(core, indexName, entry, run, void 0, pending.generation);
815
+ return run.finished;
816
+ }
817
+ async function abandonAnalysisChange(core, indexName) {
818
+ const run = runsOf(core).get(indexName);
819
+ if (run === void 0) return;
820
+ stopRun(run, indexName);
821
+ await run.settled;
822
+ }
823
+ function holdServes(run, entry, pending) {
824
+ return run.hold?.generation === pending.generation && holdsSettings(run.target.config, targetOfPending(entry, pending));
825
+ }
826
+ function startHeldRun(core, indexName, pending) {
827
+ const entry = core.requireIndex(indexName);
828
+ if (entry.analysisGeneration >= pending.generation) return null;
829
+ const runs = runsOf(core);
830
+ const previous = runs.get(indexName);
831
+ if (previous !== void 0 && holdServes(previous, entry, pending)) return previous;
832
+ const target = targetOfPending(entry, pending);
833
+ requireSynonymsUnderTarget(entry, target);
834
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
835
+ const run = createRun(target, createHold(pending.generation));
836
+ if (previous !== void 0 && !previous.switched) stopRun(previous, indexName);
837
+ runs.set(indexName, run);
838
+ void driveRun(core, indexName, entry, run, previous, pending.generation);
839
+ return run;
840
+ }
841
+ async function buildHeldAnalysisChange(core, indexName, pending) {
842
+ const running = runsOf(core).get(indexName);
843
+ const entry = core.indexRegistry.get(indexName);
844
+ if (running?.hold != null && entry !== void 0 && holdServes(running, entry, pending)) return running.hold.ready;
845
+ const release = await core.indexState.acquire(indexName, false);
846
+ let run;
847
+ try {
848
+ run = startHeldRun(core, indexName, pending);
849
+ } catch (error) {
850
+ release();
851
+ throw error;
852
+ }
853
+ if (run?.hold === void 0 || run.hold === null) {
854
+ release();
855
+ return;
856
+ }
857
+ void run.settled.finally(release);
858
+ return run.hold.ready;
859
+ }
860
+ function heldAnalysisIsBuilt(core, indexName, generation) {
861
+ const hold = runsOf(core).get(indexName)?.hold;
862
+ return hold !== void 0 && hold !== null && hold.generation === generation && hold.built;
863
+ }
864
+ async function releaseHeldAnalysisChange(core, indexName, generation) {
865
+ const run = runsOf(core).get(indexName);
866
+ if (run?.hold?.generation !== generation) return;
867
+ run.hold.released = true;
868
+ run.hold.wake();
869
+ await run.finished;
870
+ }
871
+ async function dropHeldAnalysisChange(core, indexName) {
872
+ await abandonAnalysisChange(core, indexName);
873
+ const entry = core.indexRegistry.get(indexName);
874
+ if (entry !== void 0 && entry.pendingAnalysis !== null) await clearPending(core, indexName, entry);
875
+ }
876
+ function holdsNamedSettings(entry, settings) {
877
+ return holdsSettings(entry.config, targetOfPending(entry, { generation: 0, ...settings }));
878
+ }
879
+ async function stampAnalysisGeneration(core, indexName, generation) {
880
+ const entry = core.requireIndex(indexName);
881
+ if (entry.analysisGeneration >= generation) return;
882
+ await requireNoUndurableSwitch(core, indexName, entry);
883
+ const durable = core.durability?.manager;
884
+ const stamp = () => {
885
+ entry.durableAnalysis = core.durability === null ? null : durableSettingsOf(entry);
886
+ entry.analysisGeneration = generation;
887
+ entry.pendingAnalysis = null;
888
+ return true;
889
+ };
890
+ await (durable?.checkpointAfterSwitch?.(indexName, stamp) ?? Promise.resolve(stamp()));
891
+ }
892
+
893
+ export { abandonAnalysisChange, adoptSavedAnalysis, buildHeldAnalysisChange, changeIndexAnalysis, dropHeldAnalysisChange, executeRebalance, heldAnalysisIsBuilt, holdsNamedSettings, releaseHeldAnalysisChange, resumeAnalysisChange, stampAnalysisGeneration };