@delali/narsil 0.3.1-next.131 → 0.3.1-next.141

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 (139) hide show
  1. package/dist/adapters/filesystem.mjs +1 -1
  2. package/dist/adapters/indexeddb.mjs +1 -1
  3. package/dist/analysis-change-HQUT2Z3I.mjs +43 -0
  4. package/dist/analysis-change-entry-TZTG4AZB.mjs +1 -0
  5. package/dist/browser/analysis-change-JPV775LR.mjs +900 -0
  6. package/dist/browser/analysis-change-entry-7HR4LWL3.mjs +1 -0
  7. package/dist/browser/{chunk-UDGB2MWT.mjs → chunk-35B42UFM.mjs} +4 -0
  8. package/dist/browser/chunk-3ZBJYAS4.mjs +19 -0
  9. package/dist/browser/{chunk-2Y3CO4EQ.mjs → chunk-5DB2VDGI.mjs} +1149 -1853
  10. package/dist/browser/chunk-6RHTAVZX.mjs +656 -0
  11. package/dist/browser/chunk-CCOAOO73.mjs +86 -0
  12. package/dist/browser/{chunk-AIIJDWMG.mjs → chunk-D33A5VMU.mjs} +16 -16
  13. package/dist/browser/{chunk-VBBLTD7O.mjs → chunk-HKX2G6WH.mjs} +1520 -2159
  14. package/dist/browser/{chunk-NSCSR6MA.mjs → chunk-KTOK275N.mjs} +606 -830
  15. package/dist/browser/{chunk-D7WQF6DV.mjs → chunk-LVY2LI35.mjs} +2 -2
  16. package/dist/browser/chunk-MVM33ZUL.mjs +993 -0
  17. package/dist/browser/chunk-MWUK3DA5.mjs +2634 -0
  18. package/dist/browser/{chunk-VEGHFP2P.mjs → chunk-MZI3TEOW.mjs} +4 -2
  19. package/dist/browser/chunk-OIZS5KIX.mjs +6 -0
  20. package/dist/browser/chunk-R4WUDZQV.mjs +176 -0
  21. package/dist/browser/{chunk-M5UAAXGU.mjs → chunk-TCLEOZ4P.mjs} +3 -3
  22. package/dist/browser/chunk-VFYMPCOV.mjs +212 -0
  23. package/dist/browser/chunk-WCFCHIBB.mjs +177 -0
  24. package/dist/browser/chunk-XNWPP5NS.mjs +21 -0
  25. package/dist/browser/chunk-ZEV26AQ6.mjs +270 -0
  26. package/dist/browser/cold-module-MJMKNUYJ.mjs +745 -0
  27. package/dist/browser/{durability-wiring-FNZSRH4N.mjs → durability-wiring-SNOWIT6Z.mjs} +370 -95
  28. package/dist/browser/import-TLB2A5LJ.mjs +9 -0
  29. package/dist/browser/module-BUFNZMEU.mjs +12 -0
  30. package/dist/browser/{module-4HIMOQM5.mjs → module-BVP4TKZU.mjs} +397 -109
  31. package/dist/browser/{phrase-module-WESYVU7K.mjs → phrase-module-HGVBO24W.mjs} +1 -1
  32. package/dist/{restore-queue-JAMNRC6T.mjs → browser/restore-queue-ONYNYUDE.mjs} +8 -2
  33. package/dist/browser/{runtime-3X7ZJ5T3.mjs → runtime-VEUH674B.mjs} +2 -2
  34. package/dist/browser/{snapshot-V7VF6BN6.mjs → snapshot-QXBKHJAG.mjs} +135 -183
  35. package/dist/browser/{write-YIC2MKRY.mjs → write-WHFBDWQS.mjs} +64 -90
  36. package/dist/{capabilities-B6WxrWxt.d.ts → capabilities-BCdUwK_L.d.ts} +10 -3
  37. package/dist/chunk-46NZOPHS.mjs +21 -0
  38. package/dist/{chunk-JT3YZGCE.mjs → chunk-5WYVUPGD.mjs} +3 -188
  39. package/dist/chunk-6SNBQESJ.mjs +214 -0
  40. package/dist/{chunk-2C3WXBQS.mjs → chunk-74XB2SXI.mjs} +1 -1
  41. package/dist/chunk-7AUASRE7.mjs +653 -0
  42. package/dist/{chunk-NMXRXFGZ.mjs → chunk-AST3SHFA.mjs} +27 -25
  43. package/dist/chunk-BOJZGUS6.mjs +177 -0
  44. package/dist/{chunk-T4OFG4IQ.mjs → chunk-BRUUBXCB.mjs} +92 -17
  45. package/dist/{chunk-5LX6MOIB.mjs → chunk-C3YDYX6C.mjs} +1 -1
  46. package/dist/chunk-CZQ33DLW.mjs +78 -0
  47. package/dist/{chunk-4GDLFDPZ.mjs → chunk-DD2673TX.mjs} +3 -2
  48. package/dist/{chunk-EUCX5K74.mjs → chunk-DQNKVOJ2.mjs} +65 -65
  49. package/dist/{chunk-RNSC4NFI.mjs → chunk-EEBCDP36.mjs} +3 -1
  50. package/dist/chunk-FOCV4CBZ.mjs +87 -0
  51. package/dist/{chunk-HCGJKDZV.mjs → chunk-GLZHNSGB.mjs} +1 -1
  52. package/dist/chunk-GWOS5DYC.mjs +2637 -0
  53. package/dist/{chunk-PATMDIMN.mjs → chunk-HJJWY6PT.mjs} +1 -1
  54. package/dist/{chunk-CZ7ISLXA.mjs → chunk-HNHCNTVG.mjs} +4 -0
  55. package/dist/{chunk-Y2U5VVWX.mjs → chunk-ICW45TTU.mjs} +1 -1
  56. package/dist/{chunk-TGHNXN7W.mjs → chunk-IO5F7ZBE.mjs} +261 -948
  57. package/dist/{chunk-P36MUFEJ.mjs → chunk-JDDXV6UF.mjs} +2 -1
  58. package/dist/{chunk-7OIQ2SIO.mjs → chunk-KEIYVZUX.mjs} +5 -5
  59. package/dist/{chunk-PU6SKH7O.mjs → chunk-KGOXHLLZ.mjs} +7 -3
  60. package/dist/{chunk-WVRDRLIC.mjs → chunk-KZL72VTC.mjs} +70 -18
  61. package/dist/chunk-L76W3IZJ.mjs +193 -0
  62. package/dist/{chunk-2JWNJSNE.mjs → chunk-LA4WNDHL.mjs} +88 -801
  63. package/dist/{chunk-ZR4VM4A6.mjs → chunk-N37VMC62.mjs} +1 -18
  64. package/dist/{chunk-JJ6EUMXB.mjs → chunk-NJJCPFXN.mjs} +2 -2
  65. package/dist/chunk-NN33CZRE.mjs +86 -0
  66. package/dist/{chunk-2WBY4Y4M.mjs → chunk-NOT5QOHU.mjs} +1 -1
  67. package/dist/{chunk-D5TFRB5G.mjs → chunk-NPNK57CA.mjs} +1 -1
  68. package/dist/chunk-NRUXBYPH.mjs +121 -0
  69. package/dist/{chunk-6SSVIW7Y.mjs → chunk-O4LJQI4G.mjs} +31 -5
  70. package/dist/{chunk-UJWGQIXU.mjs → chunk-QHPYS427.mjs} +1 -0
  71. package/dist/{chunk-F6AAXI7T.mjs → chunk-RHKRTYGO.mjs} +13 -4
  72. package/dist/{chunk-2SEL6SBS.mjs → chunk-RKAVHNRW.mjs} +2 -2
  73. package/dist/{chunk-RCMNL7RI.mjs → chunk-SBMAWTRG.mjs} +41 -4
  74. package/dist/chunk-SRJBFG2T.mjs +881 -0
  75. package/dist/{chunk-X5CNUKD5.mjs → chunk-TNBIK2LQ.mjs} +1663 -1652
  76. package/dist/{chunk-NNO7CO3A.mjs → chunk-UIKN2OTQ.mjs} +1 -1
  77. package/dist/chunk-UUZCQQG2.mjs +209 -0
  78. package/dist/{chunk-6M6SDH3S.mjs → chunk-VEFN6X6Q.mjs} +8 -8
  79. package/dist/chunk-VJEGOELZ.mjs +177 -0
  80. package/dist/chunk-W2COJ5AH.mjs +148 -0
  81. package/dist/{chunk-RWESFSJI.mjs → chunk-WJGO5PGV.mjs} +317 -174
  82. package/dist/{chunk-7X5JST5L.mjs → chunk-WNBRW4Q2.mjs} +16 -2
  83. package/dist/{chunk-SZGRYEIC.mjs → chunk-XS2VM7UE.mjs} +3 -3
  84. package/dist/{chunk-Y4BVTQ53.mjs → chunk-XZ3SRNDJ.mjs} +25 -64
  85. package/dist/{chunk-K77FJXKS.mjs → chunk-YHUTF6EA.mjs} +474 -93
  86. package/dist/{chunk-OGLV4I3L.mjs → chunk-YLNSJQLX.mjs} +12 -7
  87. package/dist/{chunk-SOHMMNCH.mjs → chunk-Z4HFMYNX.mjs} +32 -27
  88. package/dist/{chunk-5W7AEVTH.mjs → chunk-ZMBFWQXN.mjs} +2 -2
  89. package/dist/{chunk-2MKX2SCO.mjs → chunk-ZPWS2SKJ.mjs} +297 -274
  90. package/dist/{chunk-7XY6VD7B.mjs → chunk-ZSWHPKGM.mjs} +1 -1
  91. package/dist/{client-gNAfaHcE.d.ts → client-B12YFALP.d.ts} +18 -3
  92. package/dist/client.d.ts +7 -7
  93. package/dist/client.mjs +11 -1
  94. package/dist/cold-module-4X7WJCFM.mjs +783 -0
  95. package/dist/{config-D1cDv3sE.d.ts → config-BhZxIeb1.d.ts} +10 -1
  96. package/dist/{config-CmYJO34X.d.ts → config-CIG5yDyg.d.ts} +2 -2
  97. package/dist/{direct-executor-HQ4QVDX7.mjs → direct-executor-7XOQ7VQP.mjs} +14 -14
  98. package/dist/distribution/coordinator/etcd.d.ts +2 -2
  99. package/dist/distribution/coordinator/etcd.mjs +1 -1
  100. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  101. package/dist/distribution/transport/grpc.mjs +1 -1
  102. package/dist/distribution.d.ts +24 -8
  103. package/dist/distribution.mjs +982 -211
  104. package/dist/{durability-wiring-S7OX2MAI.mjs → durability-wiring-324D2DDD.mjs} +362 -94
  105. package/dist/embeddings/openai.mjs +2 -2
  106. package/dist/{engine-m_Ddr1bz.d.ts → engine-CtzbHOmG.d.ts} +64 -7
  107. package/dist/import-OOBUW3RR.mjs +44 -0
  108. package/dist/{index-HYnqS1SE.d.ts → index-DsEurhSl.d.ts} +1 -1
  109. package/dist/index.browser.mjs +5 -5
  110. package/dist/index.d.ts +6 -6
  111. package/dist/index.mjs +22 -22
  112. package/dist/{long-query-thread-BSL25PD6.mjs → long-query-thread-4SW7RWDE.mjs} +15 -15
  113. package/dist/{module-2E4XYSGT.mjs → module-DXSJ5LT2.mjs} +378 -87
  114. package/dist/module-IYPLWHGC.mjs +48 -0
  115. package/dist/paged-partition-archive-HFP45PLU.mjs +269 -0
  116. package/dist/partial-words.mjs +5 -5
  117. package/dist/pattern.mjs +3 -3
  118. package/dist/persistence/durability/checkpoint-worker.d.ts +16 -1
  119. package/dist/persistence/durability/checkpoint-worker.mjs +28 -26
  120. package/dist/{phrase-module-JERF2IDG.mjs → phrase-module-5QHBD2VJ.mjs} +1 -1
  121. package/dist/react.d.ts +9 -9
  122. package/dist/react.mjs +4 -0
  123. package/dist/{rerank-DMMT4FZA.mjs → rerank-KTLC3E6N.mjs} +5 -5
  124. package/dist/rerankers/http.mjs +2 -2
  125. package/dist/{browser/restore-queue-GEZ54CA5.mjs → restore-queue-FEYAC4SB.mjs} +8 -2
  126. package/dist/{runtime-7MLQ7J64.mjs → runtime-DYV75DCB.mjs} +6 -6
  127. package/dist/{schema-BTXbHCqA.d.ts → schema-B-Runv0n.d.ts} +7 -0
  128. package/dist/server.d.ts +12 -10
  129. package/dist/server.mjs +54 -21
  130. package/dist/{snapshot-DOQLGGEX.mjs → snapshot-CGM65VCD.mjs} +101 -197
  131. package/dist/synonyms.mjs +6 -4
  132. package/dist/{tasks-Co8HfRD7.d.ts → tasks-CjRJRGTb.d.ts} +26 -8
  133. package/dist/{thread-SKO7CN3R.mjs → thread-KGNAIKPD.mjs} +14 -14
  134. package/dist/vector/search-worker.d.ts +1 -1
  135. package/dist/vector/search-worker.mjs +6 -5
  136. package/dist/workers/entry.mjs +8 -8
  137. package/dist/{write-ZKR7SNRT.mjs → write-2DZPEQL2.mjs} +82 -107
  138. package/openapi.json +114 -6
  139. package/package.json +12 -8
@@ -0,0 +1,900 @@
1
+ import { requireAnalysisRebuildMemory } from './chunk-R4WUDZQV.mjs';
2
+ import { DEFAULT_WRITE_AHEAD_QUEUE_MAX_SIZE, rewriteSpillOf, emitEngineEvent, reconstructSchemaFromMetadata, getLanguage, resolveIndexAnalysis, prepareStoredSynonyms, rulesOfStoredSynonyms, resolvePartialWordAnalysis, requireConfigurationWithinSizeLimit, prepareConfiguredSynonyms, createPartitionIndex, REBALANCE_CHUNK_SIZE, yieldToEventLoop, prepareDocumentVectors, removeDocumentVectors, insertDocumentVectors, vectorsEqual, rebuildTextIndexes, textRebuildTargetOf, extractVectorFromDoc, deleteNestedValue, ARRAY_POSITION_GAP } from './chunk-5DB2VDGI.mjs';
3
+ import './chunk-JPTQZWJQ.mjs';
4
+ import './chunk-26A5JFT2.mjs';
5
+ import './chunk-OIZS5KIX.mjs';
6
+ import { compareCodePoints } from './chunk-D33A5VMU.mjs';
7
+ import { ANALYSIS_CHANGE_MEMBERS, ANALYSIS_HOLD_APPLY_INTERVAL_MS } from './chunk-MZI3TEOW.mjs';
8
+ import { NarsilError, ErrorCodes } from './chunk-35B42UFM.mjs';
9
+
10
+ // src/engine/analysis-change/pending-target.ts
11
+ function configUnderAnalysis(base, language, tokenizer, stopWords) {
12
+ const { tokenizer: _tokenizer, stopWords: _stopWords, ...rest } = base;
13
+ return {
14
+ ...rest,
15
+ language,
16
+ ...tokenizer !== void 0 ? { tokenizer } : {},
17
+ ...stopWords !== void 0 ? { stopWords } : {}
18
+ };
19
+ }
20
+ function configOfNamedSettings(base, settings) {
21
+ const { language, tokenizer, stopWords, stopWordList } = settings;
22
+ const listed = stopWordList === void 0 ? void 0 : new Set(stopWordList);
23
+ return configUnderAnalysis(base, language, tokenizer, stopWords ?? listed);
24
+ }
25
+ function targetOfPending(entry, pending) {
26
+ const config = configOfNamedSettings(entry.config, pending);
27
+ return { config, language: getLanguage(pending.language), analysis: resolveIndexAnalysis(config) };
28
+ }
29
+
30
+ // src/engine/analysis-change/run.ts
31
+ function createHold(generation) {
32
+ let markReady = () => void 0;
33
+ let refuse = () => void 0;
34
+ let wake = () => void 0;
35
+ const ready = new Promise((resolve, reject) => {
36
+ markReady = resolve;
37
+ refuse = reject;
38
+ });
39
+ ready.catch(() => void 0);
40
+ return {
41
+ generation,
42
+ built: false,
43
+ released: false,
44
+ ready,
45
+ markReady,
46
+ refuse,
47
+ wake: () => wake(),
48
+ pause: () => new Promise((resolve) => {
49
+ const timer = setTimeout(resolve, ANALYSIS_HOLD_APPLY_INTERVAL_MS);
50
+ wake = () => {
51
+ clearTimeout(timer);
52
+ resolve();
53
+ };
54
+ })
55
+ };
56
+ }
57
+ var runsByCore = /* @__PURE__ */ new WeakMap();
58
+ function runsOf(core) {
59
+ let runs = runsByCore.get(core);
60
+ if (runs === void 0) {
61
+ runs = /* @__PURE__ */ new Map();
62
+ runsByCore.set(core, runs);
63
+ }
64
+ return runs;
65
+ }
66
+ function createRun(target, hold = null) {
67
+ let finish = () => void 0;
68
+ let rejectFinished = () => void 0;
69
+ let settle = () => void 0;
70
+ const finished = new Promise((resolve, reject) => {
71
+ finish = resolve;
72
+ rejectFinished = reject;
73
+ });
74
+ finished.catch(() => void 0);
75
+ const settled = new Promise((resolve) => {
76
+ settle = resolve;
77
+ });
78
+ const fail = (error) => {
79
+ rejectFinished(error);
80
+ hold?.refuse(error);
81
+ hold?.wake();
82
+ };
83
+ return { target, stopReason: null, switched: false, settled, finish, fail, finished, settle, hold };
84
+ }
85
+ async function waitForRelease(run, hold, keepCurrent) {
86
+ hold.built = true;
87
+ hold.markReady();
88
+ while (!hold.released) {
89
+ if (run.stopReason !== null || !keepCurrent()) {
90
+ hold.built = false;
91
+ return false;
92
+ }
93
+ await hold.pause();
94
+ }
95
+ return true;
96
+ }
97
+ function stopRun(run, indexName) {
98
+ run.stopReason = new NarsilError(
99
+ ErrorCodes.ANALYSIS_CHANGE_REPLACED,
100
+ `A later change or a restore replaced this analysis change of index "${indexName}"`,
101
+ { indexName }
102
+ );
103
+ run.fail(run.stopReason);
104
+ }
105
+
106
+ // src/engine/analysis-change/settings.ts
107
+ function refuseTokenizer(indexName) {
108
+ throw new NarsilError(
109
+ ErrorCodes.CONFIG_INVALID,
110
+ `The tokenizer of index "${indexName}" must change to a name registered with registerTokenizer, because an instance survives neither a worker boundary nor a restart`,
111
+ { indexName }
112
+ );
113
+ }
114
+ function refuseStopWords(indexName) {
115
+ throw new NarsilError(
116
+ ErrorCodes.CONFIG_INVALID,
117
+ `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`,
118
+ { indexName }
119
+ );
120
+ }
121
+ function stopWordsOfChange(indexName, value) {
122
+ if (value === void 0 || value === null || typeof value === "string") return value;
123
+ const words = value instanceof Set ? [...value] : value;
124
+ if (!Array.isArray(words) || words.some((word) => typeof word !== "string")) refuseStopWords(indexName);
125
+ return new Set(words);
126
+ }
127
+ function tokenizerOfChange(indexName, value) {
128
+ if (value === void 0 || value === null || typeof value === "string") return value;
129
+ refuseTokenizer(indexName);
130
+ }
131
+ function requireRegisteredLanguage(name) {
132
+ if (typeof name !== "string") {
133
+ throw new NarsilError(ErrorCodes.LANGUAGE_NOT_SUPPORTED, "The language of an analysis change must be a name");
134
+ }
135
+ return getLanguage(name);
136
+ }
137
+ function resolveAnalysisTarget(indexName, base, change) {
138
+ if (change === null || typeof change !== "object" || Array.isArray(change)) {
139
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "An analysis change must be an object", { indexName });
140
+ }
141
+ const unknownMember = Object.keys(change).find((member) => !ANALYSIS_CHANGE_MEMBERS.includes(member));
142
+ if (unknownMember !== void 0) {
143
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `"${unknownMember}" is not an analysis setting`, { indexName });
144
+ }
145
+ const language = change.language === void 0 ? getLanguage(base.language ?? "english") : requireRegisteredLanguage(change.language);
146
+ const tokenizer = tokenizerOfChange(indexName, change.tokenizer);
147
+ const stopWords = stopWordsOfChange(indexName, change.stopWords);
148
+ const config = configUnderAnalysis(
149
+ base,
150
+ language.name,
151
+ tokenizer === void 0 ? base.tokenizer : tokenizer ?? void 0,
152
+ stopWords === void 0 ? base.stopWords : stopWords ?? void 0
153
+ );
154
+ requireConfigurationWithinSizeLimit(config, language);
155
+ return { config, language, analysis: resolveIndexAnalysis(config) };
156
+ }
157
+ function currentConfigUnderTarget(current, target) {
158
+ return configUnderAnalysis(current, target.language.name, target.config.tokenizer, target.config.stopWords);
159
+ }
160
+ function sameStopWords(left, right) {
161
+ if (left === right) return true;
162
+ if (!(left instanceof Set) || !(right instanceof Set) || left.size !== right.size) return false;
163
+ for (const word of left) if (!right.has(word)) return false;
164
+ return true;
165
+ }
166
+ function holdsSettings(config, target) {
167
+ return (config.language ?? "english") === target.language.name && config.tokenizer === target.config.tokenizer && sameStopWords(config.stopWords, target.config.stopWords);
168
+ }
169
+ function namedSettingsOf(config, language) {
170
+ return {
171
+ language,
172
+ ...typeof config.tokenizer === "string" ? { tokenizer: config.tokenizer } : {},
173
+ ...typeof config.stopWords === "string" ? { stopWords: config.stopWords } : {},
174
+ ...config.stopWords instanceof Set ? { stopWordList: [...config.stopWords].sort(compareCodePoints) } : {}
175
+ };
176
+ }
177
+ function durableSettingsOf(entry) {
178
+ return {
179
+ ...namedSettingsOf(entry.config, entry.language.name),
180
+ generation: entry.analysisGeneration,
181
+ analysisRevision: entry.analysisRevision,
182
+ arrayPositionGap: entry.arrayPositionGap
183
+ };
184
+ }
185
+ function requireSynonymsUnderTarget(entry, target) {
186
+ if (entry.synonyms === null) return;
187
+ prepareConfiguredSynonyms({ ...target.config, synonyms: rulesOfStoredSynonyms(entry.synonyms) }, target.language);
188
+ }
189
+
190
+ // src/partitioning/rebalancer.ts
191
+ function createRebalancer() {
192
+ const activeManagers = /* @__PURE__ */ new WeakSet();
193
+ let activeCount = 0;
194
+ async function rebalance(manager, newPartitionCount, router, onProgress, spill = null) {
195
+ if (activeManagers.has(manager)) {
196
+ throw new NarsilError(
197
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
198
+ "A rebalance operation is already in progress"
199
+ );
200
+ }
201
+ if (newPartitionCount <= 0) {
202
+ throw new NarsilError(
203
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
204
+ `New partition count must be greater than 0, received ${newPartitionCount}`,
205
+ { newPartitionCount }
206
+ );
207
+ }
208
+ if (newPartitionCount === manager.partitionCount) {
209
+ throw new NarsilError(
210
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
211
+ `New partition count (${newPartitionCount}) is the same as the current count`,
212
+ { newPartitionCount, currentPartitionCount: manager.partitionCount }
213
+ );
214
+ }
215
+ activeManagers.add(manager);
216
+ activeCount++;
217
+ try {
218
+ const currentPartitions = manager.getAllPartitions();
219
+ let documentsTotal = 0;
220
+ for (const partition of currentPartitions) {
221
+ documentsTotal += partition.count();
222
+ }
223
+ onProgress?.({
224
+ phase: "scanning",
225
+ documentsProcessed: 0,
226
+ documentsTotal
227
+ });
228
+ const newPartitions = [];
229
+ for (let i = 0; i < newPartitionCount; i++) {
230
+ newPartitions.push(spill?.newPartition(i) ?? createPartitionIndex(i, manager.config.trackPositions ?? true));
231
+ }
232
+ let documentsProcessed = 0;
233
+ let chunkFill = 0;
234
+ const insertOptions = {
235
+ validate: false,
236
+ skipClone: true,
237
+ stopWordOverride: manager.analysis.stopWords,
238
+ customTokenizer: manager.analysis.customTokenizer,
239
+ collectSurfaces: manager.config.surfaceForms !== false
240
+ };
241
+ for (const partition of currentPartitions) {
242
+ const docIds = [...partition.docIds()];
243
+ for (const docId of docIds) {
244
+ const document = partition.getRef(docId);
245
+ if (!document) continue;
246
+ const targetPartitionId = router.route(docId, newPartitionCount);
247
+ const targetPartition = newPartitions[targetPartitionId];
248
+ if (!targetPartition.has(docId)) {
249
+ targetPartition.insert(docId, document, manager.schema, manager.language, insertOptions);
250
+ documentsProcessed++;
251
+ }
252
+ chunkFill++;
253
+ if (chunkFill >= REBALANCE_CHUNK_SIZE) {
254
+ chunkFill = 0;
255
+ onProgress?.({
256
+ phase: "moving",
257
+ documentsProcessed,
258
+ documentsTotal
259
+ });
260
+ for (let at = 0; at < newPartitions.length; at++) await spill?.spillIfFull(newPartitions[at], at);
261
+ await yieldToEventLoop();
262
+ }
263
+ }
264
+ }
265
+ if (chunkFill > 0) {
266
+ onProgress?.({
267
+ phase: "moving",
268
+ documentsProcessed,
269
+ documentsTotal
270
+ });
271
+ await yieldToEventLoop();
272
+ }
273
+ onProgress?.({
274
+ phase: "swapping",
275
+ documentsProcessed,
276
+ documentsTotal
277
+ });
278
+ manager.setPartitions(newPartitions);
279
+ onProgress?.({
280
+ phase: "complete",
281
+ documentsProcessed,
282
+ documentsTotal
283
+ });
284
+ } finally {
285
+ activeManagers.delete(manager);
286
+ activeCount--;
287
+ }
288
+ }
289
+ return {
290
+ rebalance,
291
+ isRebalancing() {
292
+ return activeCount > 0;
293
+ }
294
+ };
295
+ }
296
+
297
+ // src/partitioning/write-ahead-queue.ts
298
+ function createWriteAheadQueue(maxSize = DEFAULT_WRITE_AHEAD_QUEUE_MAX_SIZE) {
299
+ const entries = [];
300
+ const docStates = /* @__PURE__ */ new Map();
301
+ let nextSequence = 1;
302
+ let resolveReplayed = () => void 0;
303
+ const replayed = new Promise((resolve) => {
304
+ resolveReplayed = resolve;
305
+ });
306
+ return {
307
+ push(entry) {
308
+ if (entries.length >= maxSize) {
309
+ throw new NarsilError(
310
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
311
+ `Write-ahead queue is full (${maxSize} entries). Retry after rebalancing completes.`,
312
+ { maxSize, currentSize: entries.length }
313
+ );
314
+ }
315
+ const seq = nextSequence++;
316
+ entries.push({ ...entry, sequenceNumber: seq });
317
+ docStates.set(entry.docId, entry.action === "remove" ? "absent" : "present");
318
+ return seq;
319
+ },
320
+ drain() {
321
+ const result = entries.splice(0);
322
+ result.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
323
+ return result;
324
+ },
325
+ clear() {
326
+ entries.length = 0;
327
+ docStates.clear();
328
+ },
329
+ bufferedDocState(docId) {
330
+ return docStates.get(docId);
331
+ },
332
+ whenReplayed() {
333
+ return replayed;
334
+ },
335
+ markReplayed() {
336
+ resolveReplayed();
337
+ },
338
+ get size() {
339
+ return entries.length;
340
+ },
341
+ get isFull() {
342
+ return entries.length >= maxSize;
343
+ }
344
+ };
345
+ }
346
+
347
+ // src/engine/rebalance-executor.ts
348
+ function warnHookError(hook, err) {
349
+ console.warn(`${hook} plugin hook error:`, err instanceof Error ? err.message : String(err));
350
+ }
351
+ function applyReplayUpdateVectors(entry, vectorFieldPaths) {
352
+ const updateVectors = /* @__PURE__ */ new Map();
353
+ for (const fieldPath of vectorFieldPaths) {
354
+ updateVectors.set(fieldPath, extractVectorFromDoc(entry.document, fieldPath));
355
+ }
356
+ let updateDoc = entry.document;
357
+ if (updateVectors.size > 0) {
358
+ updateDoc = structuredClone(entry.document);
359
+ for (const fieldPath of updateVectors.keys()) {
360
+ deleteNestedValue(updateDoc, fieldPath);
361
+ }
362
+ }
363
+ return { updateDoc, updateVectors };
364
+ }
365
+ async function replayEntry(manager, indexName, entry, ctx) {
366
+ const vecIndexes = manager.getVectorIndexes();
367
+ const vectorFieldPaths = ctx.requireIndex(indexName).vectorFieldPaths;
368
+ if (entry.action === "insert" && entry.document) {
369
+ const { partitionDoc, extractedVectors } = prepareDocumentVectors(
370
+ entry.document,
371
+ vectorFieldPaths
372
+ );
373
+ const overwrote = manager.has(entry.docId);
374
+ const removedSpellings = overwrote ? manager.removedSpellingsOf(entry.docId) : void 0;
375
+ if (overwrote) {
376
+ removeDocumentVectors(entry.docId, vecIndexes);
377
+ manager.remove(entry.docId, removedSpellings);
378
+ }
379
+ manager.insert(entry.docId, partitionDoc);
380
+ if (extractedVectors.size > 0) {
381
+ insertDocumentVectors(entry.docId, extractedVectors, vecIndexes, manager.partitionIdOf(entry.docId));
382
+ for (const fieldPath of extractedVectors.keys()) {
383
+ vecIndexes.get(fieldPath)?.scheduleBuild();
384
+ }
385
+ }
386
+ try {
387
+ await ctx.pluginRegistry.runHook("afterInsert", { indexName, docId: entry.docId, document: entry.document });
388
+ } catch (err) {
389
+ warnHookError("afterInsert", err);
390
+ }
391
+ const replicated = {
392
+ indexName,
393
+ docId: entry.docId,
394
+ document: entry.document,
395
+ requestId: `replicate-insert-${entry.docId}`
396
+ };
397
+ await ctx.orchestrator.replicateToWorkers(
398
+ overwrote ? { type: "update", ...replicated, removedSpellings } : { type: "insert", ...replicated }
399
+ );
400
+ return;
401
+ }
402
+ if (entry.action === "remove") {
403
+ const removedSpellings = manager.removedSpellingsOf(entry.docId);
404
+ manager.remove(entry.docId, removedSpellings);
405
+ removeDocumentVectors(entry.docId, vecIndexes);
406
+ try {
407
+ await ctx.pluginRegistry.runHook("afterRemove", { indexName, docId: entry.docId });
408
+ } catch (err) {
409
+ warnHookError("afterRemove", err);
410
+ }
411
+ await ctx.orchestrator.replicateToWorkers({
412
+ type: "remove",
413
+ indexName,
414
+ docId: entry.docId,
415
+ requestId: `replicate-remove-${entry.docId}`,
416
+ removedSpellings
417
+ });
418
+ return;
419
+ }
420
+ if (entry.action === "update" && entry.document) {
421
+ const oldDocument = manager.get(entry.docId);
422
+ const { updateDoc, updateVectors } = applyReplayUpdateVectors(entry, vectorFieldPaths);
423
+ const removedSpellings = manager.removedSpellingsOf(entry.docId);
424
+ if (manager.has(entry.docId)) {
425
+ manager.update(entry.docId, updateDoc, void 0, removedSpellings);
426
+ } else {
427
+ manager.insert(entry.docId, updateDoc);
428
+ }
429
+ for (const [fieldPath, newVec] of updateVectors) {
430
+ const vecIndex = vecIndexes.get(fieldPath);
431
+ if (!vecIndex) continue;
432
+ if (newVec === null) {
433
+ if (vecIndex.has(entry.docId)) {
434
+ vecIndex.remove(entry.docId);
435
+ }
436
+ } else {
437
+ const oldVec = vecIndex.getVector(entry.docId);
438
+ if (!vectorsEqual(oldVec, newVec)) {
439
+ if (vecIndex.has(entry.docId)) {
440
+ vecIndex.remove(entry.docId);
441
+ }
442
+ vecIndex.insert(entry.docId, newVec);
443
+ vecIndex.scheduleBuild();
444
+ }
445
+ }
446
+ }
447
+ try {
448
+ await ctx.pluginRegistry.runHook("afterUpdate", {
449
+ indexName,
450
+ docId: entry.docId,
451
+ oldDocument: oldDocument ?? {},
452
+ newDocument: entry.document
453
+ });
454
+ } catch (err) {
455
+ warnHookError("afterUpdate", err);
456
+ }
457
+ await ctx.orchestrator.replicateToWorkers({
458
+ type: "update",
459
+ indexName,
460
+ docId: entry.docId,
461
+ document: entry.document,
462
+ requestId: `replicate-update-${entry.docId}`,
463
+ removedSpellings
464
+ });
465
+ }
466
+ }
467
+ async function replayQueued(manager, indexName, waq, ctx) {
468
+ while (true) {
469
+ const entries = waq.drain();
470
+ if (entries.length === 0) break;
471
+ for (const entry of entries) {
472
+ try {
473
+ await replayEntry(manager, indexName, entry, ctx);
474
+ } catch (replayErr) {
475
+ const isMissing = replayErr instanceof NarsilError && replayErr.code === ErrorCodes.DOC_NOT_FOUND;
476
+ if (!isMissing) {
477
+ console.warn(`WAQ replay failed for ${entry.action} on doc "${entry.docId}":`, replayErr);
478
+ }
479
+ }
480
+ }
481
+ }
482
+ }
483
+ async function executeRebalance(manager, indexName, targetPartitionCount, ctx) {
484
+ if (targetPartitionCount <= 0 || !Number.isInteger(targetPartitionCount)) {
485
+ throw new NarsilError(
486
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
487
+ `Target partition count must be a positive integer, got ${targetPartitionCount}`,
488
+ { targetPartitionCount }
489
+ );
490
+ }
491
+ const entry = ctx.requireIndex(indexName);
492
+ const maxPartitions = entry.config.partitions?.maxPartitions;
493
+ if (maxPartitions !== void 0 && targetPartitionCount > maxPartitions) {
494
+ throw new NarsilError(
495
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
496
+ `Target partition count (${targetPartitionCount}) is above the maximum of ${maxPartitions} partitions`,
497
+ { targetPartitionCount, maxPartitions }
498
+ );
499
+ }
500
+ if (ctx.rebalancingIndexes.has(indexName)) {
501
+ throw new NarsilError(
502
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
503
+ `Index "${indexName}" is already being rebalanced`
504
+ );
505
+ }
506
+ const registered = ctx.requireIndex(indexName);
507
+ if (registered.pendingAnalysis !== null || registered.durableAnalysis !== null) {
508
+ throw new NarsilError(
509
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
510
+ `Index "${indexName}" is changing its analysis settings, so try the rebalance again once the change ends`,
511
+ { indexName }
512
+ );
513
+ }
514
+ if (targetPartitionCount === manager.partitionCount) {
515
+ return;
516
+ }
517
+ const waq = createWriteAheadQueue();
518
+ ctx.waqMap.set(indexName, waq);
519
+ ctx.rebalancingIndexes.add(indexName);
520
+ ctx.rebalanceTargets.set(indexName, targetPartitionCount);
521
+ const wasPromoted = ctx.orchestrator.desyncIndex(indexName);
522
+ let spill = null;
523
+ try {
524
+ const oldCount = manager.partitionCount;
525
+ spill = await rewriteSpillOf(ctx.durabilityManager?.rewriteStorage?.(), manager);
526
+ await createRebalancer().rebalance(
527
+ manager,
528
+ targetPartitionCount,
529
+ ctx.router,
530
+ (progress) => {
531
+ if (progress.phase === "complete") {
532
+ emitEngineEvent(ctx.eventHandlers, "partitionRebalance", {
533
+ indexName,
534
+ oldCount,
535
+ newCount: targetPartitionCount
536
+ });
537
+ }
538
+ },
539
+ spill
540
+ );
541
+ try {
542
+ await ctx.pluginRegistry.runHook("onPartitionSplit", {
543
+ indexName,
544
+ oldPartitionCount: oldCount,
545
+ newPartitionCount: targetPartitionCount
546
+ });
547
+ } catch (hookErr) {
548
+ console.warn("onPartitionSplit plugin hook failed:", hookErr instanceof Error ? hookErr.message : String(hookErr));
549
+ }
550
+ await replayQueued(manager, indexName, waq, ctx);
551
+ if (ctx.durabilityManager) {
552
+ await ctx.durabilityManager.persistMetadata(indexName);
553
+ await (ctx.durabilityManager.checkpointFromMemory?.(indexName) ?? ctx.durabilityManager.checkpoint(indexName));
554
+ await spill?.removeUnread(manager);
555
+ await replayQueued(manager, indexName, waq, ctx);
556
+ }
557
+ await ctx.orchestrator.resyncIndex(indexName, wasPromoted);
558
+ do {
559
+ await replayQueued(manager, indexName, waq, ctx);
560
+ } while (waq.size > 0);
561
+ } catch (err) {
562
+ await spill?.removeUnread(manager).catch(() => void 0);
563
+ try {
564
+ await replayQueued(manager, indexName, waq, ctx);
565
+ await ctx.orchestrator.resyncIndex(indexName, wasPromoted);
566
+ do {
567
+ await replayQueued(manager, indexName, waq, ctx);
568
+ } while (waq.size > 0);
569
+ } catch (recoveryErr) {
570
+ console.warn(
571
+ `Rebalance failure recovery for index "${indexName}" did not complete:`,
572
+ recoveryErr instanceof Error ? recoveryErr.message : String(recoveryErr)
573
+ );
574
+ }
575
+ throw err;
576
+ } finally {
577
+ ctx.rebalancingIndexes.delete(indexName);
578
+ ctx.rebalanceTargets.delete(indexName);
579
+ ctx.waqMap.delete(indexName);
580
+ waq.markReplayed();
581
+ }
582
+ ctx.checkWatermark(indexName);
583
+ }
584
+
585
+ // src/engine/analysis-change/adopt-saved.ts
586
+ function adoptSavedAnalysis(manager, entry, metadata) {
587
+ const saved = reconstructSchemaFromMetadata(metadata);
588
+ const { tokenizer: _tokenizer, stopWords: _stopWords, ...rest } = entry.config;
589
+ const config = {
590
+ ...rest,
591
+ language: saved.language,
592
+ ...saved.tokenizer !== void 0 ? { tokenizer: saved.tokenizer } : {},
593
+ ...saved.stopWords !== void 0 ? { stopWords: saved.stopWords } : {}
594
+ };
595
+ const language = getLanguage(config.language ?? "english");
596
+ const analysis = resolveIndexAnalysis(config);
597
+ const synonyms = prepareStoredSynonyms(rulesOfStoredSynonyms(entry.synonyms), config, language);
598
+ entry.config = config;
599
+ entry.language = language;
600
+ entry.analysisGeneration = metadata.analysisGeneration ?? 0;
601
+ entry.analysisRevision = metadata.analysisRevision ?? null;
602
+ entry.arrayPositionGap = metadata.arrayPositionGap ?? null;
603
+ manager?.adoptAnalysis({
604
+ config,
605
+ language,
606
+ analysis,
607
+ partialWordAnalysis: resolvePartialWordAnalysis(config, language, analysis),
608
+ synonyms: synonyms?.lookup ?? null
609
+ });
610
+ }
611
+
612
+ // src/engine/analysis-change/index.ts
613
+ function termBytesOf(core, indexName) {
614
+ let bytes = 0;
615
+ for (const partition of core.requireManager(indexName).getAllPartitions()) {
616
+ bytes += partition.estimateMemoryBytes();
617
+ for (const state of partition.readStates()) bytes -= state.docStore.contentBytes();
618
+ }
619
+ return Math.max(0, bytes);
620
+ }
621
+ function toError(value) {
622
+ return value instanceof Error ? value : new Error(String(value));
623
+ }
624
+ async function requireNoUndurableSwitch(core, indexName, entry) {
625
+ if (entry.durableAnalysis === null) return;
626
+ const durable = core.durability?.manager;
627
+ await (durable?.checkpointFromMemory?.(indexName) ?? durable?.checkpoint(indexName));
628
+ if (entry.durableAnalysis === null) return;
629
+ throw new NarsilError(
630
+ ErrorCodes.ANALYSIS_CHANGE_NOT_READY,
631
+ `The previous analysis change of index "${indexName}" has no durable checkpoint yet, so try this change again once a checkpoint succeeds`,
632
+ { indexName }
633
+ );
634
+ }
635
+ function switchIndex({ core, indexName, entry, run, generation }) {
636
+ const { language, analysis } = run.target;
637
+ const config = currentConfigUnderTarget(entry.config, run.target);
638
+ const synonyms = prepareStoredSynonyms(rulesOfStoredSynonyms(entry.synonyms), config, language);
639
+ return () => {
640
+ run.switched = true;
641
+ entry.durableAnalysis = core.durability === null ? null : durableSettingsOf(entry);
642
+ entry.config = config;
643
+ entry.language = language;
644
+ entry.analysisGeneration = generation;
645
+ entry.analysisRevision = language.revision;
646
+ entry.arrayPositionGap = ARRAY_POSITION_GAP;
647
+ entry.pendingAnalysis = null;
648
+ entry.synonyms = synonyms?.encoded ?? null;
649
+ core.requireManager(indexName).adoptAnalysis({
650
+ ...textRebuildTargetOf(config, language, analysis),
651
+ synonyms: synonyms?.lookup ?? null
652
+ });
653
+ core.analysisRebuild.clearStale(indexName);
654
+ };
655
+ }
656
+ async function buildAndSwitch(scope) {
657
+ const { core, indexName, entry, run } = scope;
658
+ const { hold } = run;
659
+ const manager = core.requireManager(indexName);
660
+ const emit = (status, partitionsRebuilt2, error) => {
661
+ emitEngineEvent(core.eventHandlers, "analysisRebuild", {
662
+ indexName,
663
+ status,
664
+ partitionsRebuilt: partitionsRebuilt2,
665
+ partitionCount: manager.partitionCount,
666
+ ...error !== void 0 ? { error } : {}
667
+ });
668
+ };
669
+ let partitionsRebuilt = 0;
670
+ let adopt = () => void 0;
671
+ const isCurrent = () => run.stopReason === null && !core.shutdownState.isShutdown && core.indexRegistry.get(indexName) === entry && core.executor.getManager(indexName) === manager;
672
+ emit("started", 0);
673
+ const wasPromoted = core.orchestrator.desyncIndex(indexName);
674
+ try {
675
+ const durable = core.durability?.manager;
676
+ let spill = null;
677
+ const rebuilt = await rebuildTextIndexes(
678
+ manager,
679
+ {
680
+ isCurrent,
681
+ rewriteStorage: () => durable?.rewriteStorage?.(),
682
+ onRewriteSpill(kept) {
683
+ spill = kept;
684
+ },
685
+ onPartitionBuilt(built) {
686
+ partitionsRebuilt = built;
687
+ },
688
+ onSwitch: () => adopt(),
689
+ ...hold === null ? {} : { holdBeforeSwitch: (keepCurrent) => waitForRelease(run, hold, keepCurrent) },
690
+ runSwitch(swap) {
691
+ const swapWithSynonyms = () => {
692
+ if (!isCurrent()) return false;
693
+ adopt = switchIndex(scope);
694
+ return swap();
695
+ };
696
+ return durable?.checkpointAfterSwitch?.(indexName, swapWithSynonyms) ?? Promise.resolve(swapWithSynonyms());
697
+ }
698
+ },
699
+ textRebuildTargetOf(run.target.config, run.target.language, run.target.analysis)
700
+ );
701
+ if (!rebuilt) throw run.stopReason ?? stoppedError(core, indexName);
702
+ await spill?.removeUnread(manager);
703
+ if (entry.durableAnalysis !== null) {
704
+ throw new NarsilError(
705
+ ErrorCodes.PERSISTENCE_SAVE_FAILED,
706
+ `The new terms of index "${indexName}" answer queries, but they have no durable checkpoint yet`,
707
+ { indexName }
708
+ );
709
+ }
710
+ emit("completed", partitionsRebuilt);
711
+ } catch (error) {
712
+ emit("failed", partitionsRebuilt, toError(error));
713
+ throw error;
714
+ } finally {
715
+ if (core.executor.getManager(indexName) === manager) {
716
+ await core.orchestrator.resyncIndex(indexName, wasPromoted).catch(() => void 0);
717
+ }
718
+ }
719
+ }
720
+ function stoppedError(core, indexName) {
721
+ if (core.shutdownState.isShutdown) {
722
+ return new NarsilError(ErrorCodes.INSTANCE_SHUT_DOWN, "This Narsil instance has been shut down");
723
+ }
724
+ return new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" closed during its analysis change`, {
725
+ indexName
726
+ });
727
+ }
728
+ async function clearPending(core, indexName, entry) {
729
+ entry.pendingAnalysis = null;
730
+ await core.durability?.manager.persistMetadata(indexName);
731
+ }
732
+ function requireNotRebalancing(core, indexName) {
733
+ if (!core.rebalancingIndexes.has(indexName)) return;
734
+ throw new NarsilError(
735
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
736
+ `Index "${indexName}" is being rebalanced, so try its analysis change again once the rebalance ends`,
737
+ { indexName }
738
+ );
739
+ }
740
+ async function driveRun(core, indexName, entry, run, previous, resumedGeneration) {
741
+ const runs = runsOf(core);
742
+ try {
743
+ if (previous !== void 0) await previous.settled;
744
+ if (runs.get(indexName) !== run) return;
745
+ await requireNoUndurableSwitch(core, indexName, entry);
746
+ if (run.stopReason !== null) return;
747
+ if (core.indexRegistry.get(indexName) !== entry) throw stoppedError(core, indexName);
748
+ requireNotRebalancing(core, indexName);
749
+ if (resumedGeneration === null && holdsSettings(entry.config, run.target)) {
750
+ if (entry.pendingAnalysis !== null) await clearPending(core, indexName, entry);
751
+ run.finish();
752
+ return;
753
+ }
754
+ const generation = resumedGeneration ?? Math.max(entry.analysisGeneration, entry.pendingAnalysis?.generation ?? 0) + 1;
755
+ if (entry.pendingAnalysis?.generation !== generation || run.hold !== null) {
756
+ entry.pendingAnalysis = { generation, ...namedSettingsOf(run.target.config, run.target.language.name) };
757
+ await core.durability?.manager.persistMetadata(indexName);
758
+ }
759
+ if (run.stopReason !== null) return;
760
+ await core.analysisRebuild.abandon(indexName);
761
+ await core.analysisRebuild.runExclusive(
762
+ indexName,
763
+ () => buildAndSwitch({ core, indexName, entry, run, generation }),
764
+ run.hold?.ready
765
+ );
766
+ run.finish();
767
+ } catch (error) {
768
+ run.fail(error);
769
+ const keepsPending = resumedGeneration !== null || core.shutdownState.isShutdown;
770
+ const stillOwnsPending = runs.get(indexName) === run && run.stopReason === null;
771
+ if (!keepsPending && stillOwnsPending && core.indexRegistry.get(indexName) === entry) {
772
+ if (entry.pendingAnalysis !== null) await clearPending(core, indexName, entry).catch(() => void 0);
773
+ }
774
+ } finally {
775
+ if (runs.get(indexName) === run) runs.delete(indexName);
776
+ run.settle();
777
+ }
778
+ }
779
+ async function changeIndexAnalysis(core, indexName, change) {
780
+ const entry = core.requireIndex(indexName);
781
+ requireNotRebalancing(core, indexName);
782
+ const target = resolveAnalysisTarget(indexName, entry.config, change);
783
+ const runs = runsOf(core);
784
+ const previous = runs.get(indexName);
785
+ if (previous !== void 0 && holdsSettings(previous.target.config, target)) return previous.finished;
786
+ const heldAlready = holdsSettings(entry.config, target);
787
+ if (previous === void 0 && entry.durableAnalysis === null && heldAlready) return;
788
+ if (!heldAlready) {
789
+ requireSynonymsUnderTarget(entry, target);
790
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
791
+ }
792
+ const run = createRun(target);
793
+ if (previous !== void 0 && !previous.switched) stopRun(previous, indexName);
794
+ runs.set(indexName, run);
795
+ void driveRun(core, indexName, entry, run, previous, null);
796
+ return run.finished;
797
+ }
798
+ function resumeAnalysisChange(core, indexName) {
799
+ const entry = core.requireIndex(indexName);
800
+ const runs = runsOf(core);
801
+ const running = runs.get(indexName);
802
+ if (running !== void 0) return running.finished;
803
+ const pending = entry.pendingAnalysis;
804
+ if (pending === null) return Promise.resolve();
805
+ let target;
806
+ try {
807
+ target = targetOfPending(entry, pending);
808
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
809
+ } catch (error) {
810
+ emitEngineEvent(core.eventHandlers, "analysisRebuild", {
811
+ indexName,
812
+ status: "failed",
813
+ partitionsRebuilt: 0,
814
+ partitionCount: entry.partitionCount,
815
+ error: toError(error)
816
+ });
817
+ return Promise.reject(error);
818
+ }
819
+ const run = createRun(target);
820
+ runs.set(indexName, run);
821
+ void driveRun(core, indexName, entry, run, void 0, pending.generation);
822
+ return run.finished;
823
+ }
824
+ async function abandonAnalysisChange(core, indexName) {
825
+ const run = runsOf(core).get(indexName);
826
+ if (run === void 0) return;
827
+ stopRun(run, indexName);
828
+ await run.settled;
829
+ }
830
+ function holdServes(run, entry, pending) {
831
+ return run.hold?.generation === pending.generation && holdsSettings(run.target.config, targetOfPending(entry, pending));
832
+ }
833
+ function startHeldRun(core, indexName, pending) {
834
+ const entry = core.requireIndex(indexName);
835
+ if (entry.analysisGeneration >= pending.generation) return null;
836
+ const runs = runsOf(core);
837
+ const previous = runs.get(indexName);
838
+ if (previous !== void 0 && holdServes(previous, entry, pending)) return previous;
839
+ const target = targetOfPending(entry, pending);
840
+ requireSynonymsUnderTarget(entry, target);
841
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
842
+ const run = createRun(target, createHold(pending.generation));
843
+ if (previous !== void 0 && !previous.switched) stopRun(previous, indexName);
844
+ runs.set(indexName, run);
845
+ void driveRun(core, indexName, entry, run, previous, pending.generation);
846
+ return run;
847
+ }
848
+ async function buildHeldAnalysisChange(core, indexName, pending) {
849
+ const running = runsOf(core).get(indexName);
850
+ const entry = core.indexRegistry.get(indexName);
851
+ if (running?.hold != null && entry !== void 0 && holdServes(running, entry, pending)) return running.hold.ready;
852
+ const release = await core.indexState.acquire(indexName, false);
853
+ let run;
854
+ try {
855
+ run = startHeldRun(core, indexName, pending);
856
+ } catch (error) {
857
+ release();
858
+ throw error;
859
+ }
860
+ if (run?.hold === void 0 || run.hold === null) {
861
+ release();
862
+ return;
863
+ }
864
+ void run.settled.finally(release);
865
+ return run.hold.ready;
866
+ }
867
+ function heldAnalysisIsBuilt(core, indexName, generation) {
868
+ const hold = runsOf(core).get(indexName)?.hold;
869
+ return hold !== void 0 && hold !== null && hold.generation === generation && hold.built;
870
+ }
871
+ async function releaseHeldAnalysisChange(core, indexName, generation) {
872
+ const run = runsOf(core).get(indexName);
873
+ if (run?.hold?.generation !== generation) return;
874
+ run.hold.released = true;
875
+ run.hold.wake();
876
+ await run.finished;
877
+ }
878
+ async function dropHeldAnalysisChange(core, indexName) {
879
+ await abandonAnalysisChange(core, indexName);
880
+ const entry = core.indexRegistry.get(indexName);
881
+ if (entry !== void 0 && entry.pendingAnalysis !== null) await clearPending(core, indexName, entry);
882
+ }
883
+ function holdsNamedSettings(entry, settings) {
884
+ return holdsSettings(entry.config, targetOfPending(entry, { ...settings }));
885
+ }
886
+ async function stampAnalysisGeneration(core, indexName, generation) {
887
+ const entry = core.requireIndex(indexName);
888
+ if (entry.analysisGeneration >= generation) return;
889
+ await requireNoUndurableSwitch(core, indexName, entry);
890
+ const durable = core.durability?.manager;
891
+ const stamp = () => {
892
+ entry.durableAnalysis = core.durability === null ? null : durableSettingsOf(entry);
893
+ entry.analysisGeneration = generation;
894
+ entry.pendingAnalysis = null;
895
+ return true;
896
+ };
897
+ await (durable?.checkpointAfterSwitch?.(indexName, stamp) ?? Promise.resolve(stamp()));
898
+ }
899
+
900
+ export { abandonAnalysisChange, adoptSavedAnalysis, buildHeldAnalysisChange, changeIndexAnalysis, dropHeldAnalysisChange, executeRebalance, heldAnalysisIsBuilt, holdsNamedSettings, releaseHeldAnalysisChange, resumeAnalysisChange, stampAnalysisGeneration };