@delali/narsil 0.3.0 → 0.3.1-next.101

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 (165) hide show
  1. package/README.md +9 -8
  2. package/dist/adapters/filesystem.d.ts +1 -1
  3. package/dist/adapters/filesystem.mjs +1 -1
  4. package/dist/adapters/indexeddb.d.ts +1 -1
  5. package/dist/adapters/indexeddb.mjs +1 -1
  6. package/dist/adapters/memory.d.ts +1 -1
  7. package/dist/{adapters-79OIa4xm.d.ts → adapters-BQFQ4PRz.d.ts} +27 -1
  8. package/dist/browser/chunk-BDOSMEEP.mjs +493 -0
  9. package/dist/browser/chunk-EJDUFWSO.mjs +602 -0
  10. package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
  11. package/dist/browser/chunk-JOVZ6HMT.mjs +4358 -0
  12. package/dist/browser/chunk-JPTQZWJQ.mjs +10 -0
  13. package/dist/browser/chunk-KPJMDPHG.mjs +24558 -0
  14. package/dist/browser/chunk-PGIM3HIM.mjs +2743 -0
  15. package/dist/browser/chunk-RHG4O77A.mjs +206 -0
  16. package/dist/browser/chunk-RIJSLY2I.mjs +360 -0
  17. package/dist/browser/durability-wiring-SRUYT3VV.mjs +3875 -0
  18. package/dist/browser/module-CBB2ETYP.mjs +3111 -0
  19. package/dist/browser/release-76MXRPGV.mjs +33 -0
  20. package/dist/browser/replace-RURXTGVG.mjs +57 -0
  21. package/dist/browser/restore-queue-EEZQO6RF.mjs +35 -0
  22. package/dist/browser/runtime-7ZAHCHWR.mjs +557 -0
  23. package/dist/browser/snapshot-OAAA5UJ7.mjs +1210 -0
  24. package/dist/browser/write-7D6VIK6O.mjs +208 -0
  25. package/dist/{capabilities-DSsOGz6F.d.ts → capabilities-DiGlB6Sr.d.ts} +68 -7
  26. package/dist/chunk-2RIRGOUL.mjs +113 -0
  27. package/dist/chunk-37CD3OKS.mjs +87 -0
  28. package/dist/chunk-44W3CBNG.mjs +7 -0
  29. package/dist/chunk-46D6JQIG.mjs +426 -0
  30. package/dist/{chunk-VMRR3DQU.mjs → chunk-5JNA5SKS.mjs} +546 -35
  31. package/dist/chunk-747OHOUA.mjs +11 -0
  32. package/dist/chunk-75XIEF6H.mjs +126 -0
  33. package/dist/chunk-7X5JST5L.mjs +114 -0
  34. package/dist/chunk-BOFBLJFZ.mjs +209 -0
  35. package/dist/{chunk-USKFU7J5.mjs → chunk-BWJVNZPR.mjs} +8 -1
  36. package/dist/chunk-C7SLA6CM.mjs +19 -0
  37. package/dist/{chunk-POWOTUKJ.mjs → chunk-CQUMNIIG.mjs} +3 -6
  38. package/dist/chunk-CVXLLEQ3.mjs +1011 -0
  39. package/dist/chunk-DJHHF2L5.mjs +17 -0
  40. package/dist/chunk-E74YLAIG.mjs +24 -0
  41. package/dist/{chunk-UXNWQZST.mjs → chunk-EMRMGYRE.mjs} +236 -18
  42. package/dist/chunk-EWZLIYHV.mjs +57 -0
  43. package/dist/chunk-GBEEG5AN.mjs +35 -0
  44. package/dist/chunk-GES7U2PM.mjs +68 -0
  45. package/dist/chunk-HJSN7UYG.mjs +10 -0
  46. package/dist/chunk-HLPRHOSO.mjs +17 -0
  47. package/dist/chunk-HMFNFVP7.mjs +14 -0
  48. package/dist/chunk-J5QP52HA.mjs +1751 -0
  49. package/dist/{chunk-YNVIUGF5.mjs → chunk-JZVARCRZ.mjs} +3 -28
  50. package/dist/chunk-KOKM4PQN.mjs +52 -0
  51. package/dist/{chunk-B2TFROFX.mjs → chunk-KWTW6VTG.mjs} +465 -250
  52. package/dist/chunk-KZ3NX2P7.mjs +21 -0
  53. package/dist/chunk-L5ORED7V.mjs +25 -0
  54. package/dist/{chunk-THYRHHO2.mjs → chunk-LGAMKHA2.mjs} +2009 -2351
  55. package/dist/chunk-LKOB5COA.mjs +117 -0
  56. package/dist/chunk-LPQOEU5M.mjs +96 -0
  57. package/dist/{chunk-3LHGNRYE.mjs → chunk-LXWDQLTW.mjs} +7 -2
  58. package/dist/{chunk-L6TNPLAR.mjs → chunk-OEYI3KRB.mjs} +270 -108
  59. package/dist/chunk-OM362BRO.mjs +948 -0
  60. package/dist/chunk-OZDFA4WC.mjs +67 -0
  61. package/dist/chunk-P36MUFEJ.mjs +10 -0
  62. package/dist/chunk-PC2G76VW.mjs +2540 -0
  63. package/dist/{chunk-GCM3JFVA.mjs → chunk-PNEYIHA4.mjs} +11 -2
  64. package/dist/chunk-PNFKUCG3.mjs +1130 -0
  65. package/dist/chunk-Q6XWVUZX.mjs +44 -0
  66. package/dist/chunk-R27EMH5M.mjs +617 -0
  67. package/dist/{chunk-YMZCMH5M.mjs → chunk-R6A773WK.mjs} +19 -24
  68. package/dist/chunk-RF7PLG43.mjs +106 -0
  69. package/dist/chunk-RFBLQLLZ.mjs +16 -0
  70. package/dist/{chunk-Y3L5MDRV.mjs → chunk-RFVUDM5E.mjs} +17 -2
  71. package/dist/chunk-S7FL6UAH.mjs +521 -0
  72. package/dist/chunk-S7GNDRPS.mjs +651 -0
  73. package/dist/chunk-SP3NM3ZG.mjs +3537 -0
  74. package/dist/chunk-U476IXKQ.mjs +10 -0
  75. package/dist/chunk-UA3AUGNV.mjs +462 -0
  76. package/dist/chunk-UTG5YNXV.mjs +507 -0
  77. package/dist/chunk-VI4R6W25.mjs +8484 -0
  78. package/dist/chunk-WQBCBKY3.mjs +8 -0
  79. package/dist/{chunk-24RXHMEL.mjs → chunk-WVCOCKBU.mjs} +3794 -7570
  80. package/dist/chunk-X5CV2E2B.mjs +1 -0
  81. package/dist/chunk-XADZPPZI.mjs +614 -0
  82. package/dist/chunk-YEEQHT4V.mjs +212 -0
  83. package/dist/chunk-YTJCGKM4.mjs +56 -0
  84. package/dist/chunk-YYQIODMF.mjs +275 -0
  85. package/dist/chunk-ZI34TVOA.mjs +144 -0
  86. package/dist/{chunk-OOSIDHA3.mjs → chunk-ZKWYWW4Y.mjs} +1 -1
  87. package/dist/chunk-ZM7KU4YW.mjs +35 -0
  88. package/dist/{client-BQK6Tf6A.d.ts → client-BOStAkc5.d.ts} +160 -37
  89. package/dist/client.d.ts +11 -10
  90. package/dist/client.mjs +209 -45
  91. package/dist/{config-DmXULPa-.d.ts → config-Cjvzbs_j.d.ts} +51 -3
  92. package/dist/config-DffQlmRq.d.ts +421 -0
  93. package/dist/direct-executor-ZD64PPEZ.mjs +24 -0
  94. package/dist/distribution/coordinator/etcd.d.ts +79 -18
  95. package/dist/distribution/coordinator/etcd.mjs +553 -109
  96. package/dist/distribution/coordinator/in-memory.d.ts +4 -3
  97. package/dist/distribution/coordinator/in-memory.mjs +22 -3
  98. package/dist/distribution/transport/grpc.d.ts +2 -2
  99. package/dist/distribution/transport/grpc.mjs +79 -14
  100. package/dist/distribution/transport/in-memory.d.ts +2 -2
  101. package/dist/distribution/transport/in-memory.mjs +12 -7
  102. package/dist/distribution/transport/tcp.d.ts +2 -2
  103. package/dist/distribution/transport/tcp.mjs +50 -28
  104. package/dist/distribution.d.ts +144 -23
  105. package/dist/distribution.mjs +14479 -8173
  106. package/dist/durability-wiring-FEUBR5BT.mjs +3330 -0
  107. package/dist/embeddings/openai.d.ts +2 -2
  108. package/dist/embeddings/openai.mjs +22 -133
  109. package/dist/{engine-CIOyFozK.d.ts → engine-rC0vjjY-.d.ts} +165 -141
  110. package/dist/{memory-yFaVn2xZ.d.ts → events-DqOIg5xa.d.ts} +645 -136
  111. package/dist/{types-DAK7mnUZ.d.ts → index-Bkdt8nDw.d.ts} +182 -45
  112. package/dist/index.browser.mjs +4 -32180
  113. package/dist/index.d.ts +91 -20
  114. package/dist/index.mjs +33 -16
  115. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  116. package/dist/invalidation/filesystem.d.ts +1 -1
  117. package/dist/invalidation/filesystem.mjs +2 -1
  118. package/dist/invalidation/noop.d.ts +1 -1
  119. package/dist/module-3S65MVCM.mjs +2151 -0
  120. package/dist/partial-words-DTIHO8G4.d.ts +19 -0
  121. package/dist/partial-words.d.ts +22 -0
  122. package/dist/partial-words.mjs +17 -0
  123. package/dist/pattern-Cledccbl.d.ts +18 -0
  124. package/dist/pattern.d.ts +22 -0
  125. package/dist/pattern.mjs +12 -0
  126. package/dist/persistence/durability/checkpoint-worker.d.ts +119 -72
  127. package/dist/persistence/durability/checkpoint-worker.mjs +51 -18
  128. package/dist/react.d.ts +279 -183
  129. package/dist/react.mjs +201 -48
  130. package/dist/release-BH3DKW3T.mjs +33 -0
  131. package/dist/replace-CEO5KJJ3.mjs +57 -0
  132. package/dist/rerank-K7C4WZFG.mjs +60 -0
  133. package/dist/rerankers/http.d.ts +50 -0
  134. package/dist/rerankers/http.mjs +104 -0
  135. package/dist/restore-queue-KQHA7ZV2.mjs +35 -0
  136. package/dist/runtime-6Y4ZYXOG.mjs +177 -0
  137. package/dist/{schema-d5Afu03A.d.ts → schema-4W_7tk1f.d.ts} +81 -11
  138. package/dist/server.d.ts +13 -12
  139. package/dist/server.mjs +611 -80
  140. package/dist/snapshot-QGZPODM5.mjs +799 -0
  141. package/dist/synonyms-CpOAHPa-.d.ts +52 -0
  142. package/dist/synonyms.d.ts +22 -0
  143. package/dist/synonyms.mjs +12 -0
  144. package/dist/{tasks-CH2Dod8w.d.ts → tasks-CwPTz-tP.d.ts} +10 -3
  145. package/dist/thread-N3IUGUS3.mjs +697 -0
  146. package/dist/{types-BQDlIvfS.d.ts → types-2qoitTXg.d.ts} +1 -1
  147. package/dist/{types-D5OwhtsQ.d.ts → types-Cz5okLml.d.ts} +2 -2
  148. package/dist/vector/search-worker.d.ts +46 -4
  149. package/dist/vector/search-worker.mjs +21 -11
  150. package/dist/workers/entry.mjs +9 -6
  151. package/dist/write-U5RI7MAX.mjs +241 -0
  152. package/package.json +55 -9
  153. package/dist/chunk-3L4U6EJQ.mjs +0 -49
  154. package/dist/chunk-IFBKLBGB.mjs +0 -7
  155. package/dist/chunk-ORBLD3EL.mjs +0 -6
  156. package/dist/chunk-U7KYW4BA.mjs +0 -135
  157. package/dist/chunk-ZJ4CSAB2.mjs +0 -13
  158. package/dist/chunk-ZNZBXMP5.mjs +0 -2117
  159. package/dist/chunk-ZZT3EAUC.mjs +0 -11168
  160. package/dist/config-BhldZgaK.d.ts +0 -194
  161. package/dist/direct-executor-ALAVE7FC.mjs +0 -12
  162. package/dist/serialization/crc32-worker.d.ts +0 -20
  163. package/dist/serialization/crc32-worker.mjs +0 -62
  164. package/dist/simd-DW5AtJ6B.d.ts +0 -41
  165. package/dist/thread-DYHEQRVY.mjs +0 -402
@@ -0,0 +1,212 @@
1
+ import { rerankQueryTextOf } from './chunk-KZ3NX2P7.mjs';
2
+ import { fixedHybridDepthOf, pageByPosition, requireWithinHybridDepth } from './chunk-EWZLIYHV.mjs';
3
+ import { DEFAULT_RERANK_WINDOW, MIN_RERANK_WINDOW } from './chunk-HJSN7UYG.mjs';
4
+ import { DEFAULT_LIMIT, DEFAULT_OFFSET } from './chunk-LXWDQLTW.mjs';
5
+ import { RESULT_WINDOW } from './chunk-E74YLAIG.mjs';
6
+ import { NarsilError, ErrorCodes } from './chunk-BWJVNZPR.mjs';
7
+
8
+ // src/rerank/params.ts
9
+ function invalid(message, details = {}) {
10
+ return new NarsilError(ErrorCodes.RERANK_CONFIG_INVALID, message, details);
11
+ }
12
+ function requireTextFields(fields, schema, host) {
13
+ if (!Array.isArray(fields) || fields.length === 0) {
14
+ throw invalid("rerank.fields must list at least one text field");
15
+ }
16
+ const flat = host.flattenSchema(schema);
17
+ const checked = [];
18
+ const seen = /* @__PURE__ */ new Set();
19
+ for (const field of fields) {
20
+ if (typeof field !== "string") {
21
+ throw invalid("rerank.fields accepts field paths written as strings", { field: String(field) });
22
+ }
23
+ const parsed = host.parsedTypeOf(Object.hasOwn(flat, field) ? flat[field] : void 0);
24
+ if (parsed === void 0 || !host.isStringValuedBase(parsed.base)) {
25
+ throw invalid(`rerank.fields gives "${field}", which the schema does not declare as a text field`, { field });
26
+ }
27
+ if (seen.has(field)) {
28
+ throw invalid(`rerank.fields gives "${field}" more than once`, { field });
29
+ }
30
+ seen.add(field);
31
+ checked.push(field);
32
+ }
33
+ return checked;
34
+ }
35
+ function requireWindow(window, maxWindow, hybridDepth) {
36
+ if (window === void 0) return Math.min(DEFAULT_RERANK_WINDOW, maxWindow, hybridDepth ?? maxWindow);
37
+ if (typeof window !== "number" || !Number.isInteger(window) || window < MIN_RERANK_WINDOW || window > maxWindow) {
38
+ throw invalid(`rerank.window must be a whole number from ${MIN_RERANK_WINDOW} to ${maxWindow}`, {
39
+ window: typeof window === "number" ? window : String(window),
40
+ maxRerankWindow: maxWindow
41
+ });
42
+ }
43
+ if (hybridDepth !== void 0 && window > hybridDepth) {
44
+ throw invalid(
45
+ `rerank.window is ${window}, above the hybrid.depth of ${hybridDepth} that a hybrid query reranks from`,
46
+ {
47
+ window,
48
+ depth: hybridDepth
49
+ }
50
+ );
51
+ }
52
+ return window;
53
+ }
54
+ function resolveRerankParams(params, schema, maxWindow, hybridDepth, host) {
55
+ const rerank = params.rerank;
56
+ if (typeof rerank !== "object" || rerank === null || Array.isArray(rerank)) {
57
+ throw invalid("rerank must be an object that gives an adapter and the fields to rerank by");
58
+ }
59
+ if (host.normalizeSort(params.sort).length > 0) {
60
+ throw new NarsilError(
61
+ ErrorCodes.SEARCH_INVALID_MODE,
62
+ "A query cannot carry both rerank and a sort, because each of them sets the order of the hits"
63
+ );
64
+ }
65
+ const { adapter, fields, window } = rerank;
66
+ if (typeof adapter !== "string" || adapter.length === 0) {
67
+ throw invalid("rerank.adapter must give the name of a registered rerank adapter");
68
+ }
69
+ const checkedFields = requireTextFields(fields, schema, host);
70
+ const checkedWindow = requireWindow(window, maxWindow, hybridDepth);
71
+ const query = rerankQueryTextOf(params);
72
+ if (query === void 0) {
73
+ throw invalid("A reranked query needs text to compare each hit with: set rerank.query, term, or vector.text");
74
+ }
75
+ return { adapter, fields: checkedFields, query, window: checkedWindow };
76
+ }
77
+ function rerankTextOf(values) {
78
+ if (values === void 0) return "";
79
+ const lines = [];
80
+ for (const value of values) {
81
+ if (typeof value === "string") {
82
+ lines.push(value);
83
+ } else if (Array.isArray(value)) {
84
+ for (const member of value) if (typeof member === "string") lines.push(member);
85
+ }
86
+ }
87
+ return lines.join("\n");
88
+ }
89
+
90
+ // src/rerank/pipeline.ts
91
+ function rerankCursorDepth(cursor, binding, host) {
92
+ const decoded = host.decodePageCursor(cursor);
93
+ host.requireMatchingCursor(decoded, cursor, null, true, binding);
94
+ return decoded.depth ?? 0;
95
+ }
96
+ function requireWithinReach(cursorDepth, offset, limit) {
97
+ const reach = cursorDepth + offset + limit;
98
+ if (reach <= RESULT_WINDOW) return;
99
+ throw new NarsilError(
100
+ ErrorCodes.SEARCH_RESULT_WINDOW_EXCEEDED,
101
+ `The engine pages a reranked query through the first ${RESULT_WINDOW} results, and this page ends at ${reach}`,
102
+ { limit, offset, cursorDepth, window: RESULT_WINDOW }
103
+ );
104
+ }
105
+ function reorderByRerank(ranked, windowSize, scores, compareCodePoints) {
106
+ let initialLow = Number.POSITIVE_INFINITY;
107
+ let initialHigh = Number.NEGATIVE_INFINITY;
108
+ let finalLow = Number.POSITIVE_INFINITY;
109
+ let finalHigh = Number.NEGATIVE_INFINITY;
110
+ const ordered = new Array(windowSize);
111
+ for (let i = 0; i < windowSize; i++) {
112
+ const first = ranked[i].score ?? 0;
113
+ const score = scores[i];
114
+ if (first < initialLow) initialLow = first;
115
+ if (first > initialHigh) initialHigh = first;
116
+ if (score < finalLow) finalLow = score;
117
+ if (score > finalHigh) finalHigh = score;
118
+ ordered[i] = { ...ranked[i], score };
119
+ }
120
+ ordered.sort((a, b) => (b.score ?? 0) - (a.score ?? 0) || compareCodePoints(a.id, b.id));
121
+ const rescale = initialLow > finalLow;
122
+ const scale = rescale ? Math.max(finalHigh - finalLow, 1) / Math.max(initialHigh - initialLow, 1) : 1;
123
+ const bias = rescale ? finalLow - initialLow * scale : 0;
124
+ for (let i = windowSize; i < ranked.length; i++) {
125
+ const hit = ranked[i];
126
+ ordered.push(rescale ? { ...hit, score: (hit.score ?? 0) * scale + bias } : hit);
127
+ }
128
+ return ordered;
129
+ }
130
+ function firstStageDepth(hybridDepth, window, cursorDepth, offset, limit) {
131
+ if (hybridDepth === void 0) {
132
+ requireWithinReach(cursorDepth, offset, limit);
133
+ return Math.max(window + 1, Math.min(cursorDepth + offset + limit + 1, RESULT_WINDOW));
134
+ }
135
+ requireWithinHybridDepth(cursorDepth, offset, hybridDepth);
136
+ return hybridDepth;
137
+ }
138
+ async function runRerankedQuery(request, io) {
139
+ const fuses = io.fusesRankings(request);
140
+ const hybridDepth = fuses ? fixedHybridDepthOf(request.hybrid) : void 0;
141
+ const { host } = io;
142
+ const plan = resolveRerankParams(request, io.schema, io.maxWindow, hybridDepth, host);
143
+ const limit = host.clampRowCount(request.limit, DEFAULT_LIMIT);
144
+ const offset = host.clampRowCount(request.offset, DEFAULT_OFFSET);
145
+ const cursorDepth = request.searchAfter === void 0 ? 0 : rerankCursorDepth(request.searchAfter, io.binding, host);
146
+ const depth = firstStageDepth(hybridDepth, plan.window, cursorDepth, offset, limit);
147
+ const projection = host.resolveProjection(request.document);
148
+ const { rerank: _rerank, pinned: _pinned, searchAfter: _searchAfter, highlight: _highlight, ...firstParams } = request;
149
+ const placesPins = request.searchAfter === void 0 && request.pinned !== void 0;
150
+ const countsApart = placesPins || request.vector !== void 0 && !fuses;
151
+ const [first, plain] = await Promise.all([
152
+ io.firstStage({ ...firstParams, offset: 0, limit: depth, document: false }),
153
+ countsApart ? io.firstStage({
154
+ ...firstParams,
155
+ ...placesPins ? { pinned: request.pinned } : {},
156
+ facets: void 0,
157
+ group: void 0,
158
+ offset: cursorDepth + offset,
159
+ limit,
160
+ document: false
161
+ }) : void 0
162
+ ]);
163
+ const ranked = first.hits;
164
+ let pins = placesPins ? request.pinned : void 0;
165
+ if (pins !== void 0) {
166
+ const stored = await io.storedPins(host.dedupePinnedEntries(pins).map((entry) => entry.docId));
167
+ const clamps = io.clampsPins || first.countExact && ranked.length >= first.count;
168
+ pins = pins.filter((entry) => stored.has(entry.docId) && (clamps || entry.position < depth));
169
+ }
170
+ const placedPins = pins;
171
+ const place = (hits) => placedPins === void 0 ? hits : host.placePinned(hits, placedPins, (docId) => ({ id: docId, score: 0, document: void 0 })).hits;
172
+ const start = cursorDepth + offset;
173
+ const windowSize = Math.min(plan.window, ranked.length);
174
+ const pageHoldsHits = limit > 0 && start < place(ranked).length;
175
+ let ordered = ranked;
176
+ if (windowSize > 0 && pageHoldsHits) {
177
+ const texts = await io.texts(
178
+ ranked.slice(0, windowSize).map((hit) => hit.id),
179
+ plan.fields
180
+ );
181
+ ordered = reorderByRerank(
182
+ ranked,
183
+ windowSize,
184
+ await io.score(plan.adapter, plan.query, texts),
185
+ host.compareCodePoints
186
+ );
187
+ } else {
188
+ await io.score(plan.adapter, plan.query, []);
189
+ }
190
+ const pinnedIds = placedPins === void 0 ? void 0 : new Set(placedPins.map((entry) => entry.docId));
191
+ const { page, cursor } = pageByPosition(place(ordered), ordered, {
192
+ start,
193
+ limit,
194
+ pinnedIds,
195
+ binding: io.binding,
196
+ encodeCursor: host.encodePageCursor
197
+ });
198
+ await io.fillDocuments(page, first.groups, projection);
199
+ if (request.highlight) io.highlight?.(page, projection);
200
+ return {
201
+ hits: page,
202
+ count: (plain ?? first).count,
203
+ countExact: (plain ?? first).countExact,
204
+ elapsed: 0,
205
+ cursor,
206
+ facets: first.facets,
207
+ groups: first.groups,
208
+ coverage: first.coverage
209
+ };
210
+ }
211
+
212
+ export { rerankTextOf, runRerankedQuery };
@@ -0,0 +1,56 @@
1
+ // src/core/ordering/code-points.ts
2
+ function exceedsCodePoints(value, maximum) {
3
+ if (value.length <= maximum) return false;
4
+ let counted = 0;
5
+ let i = 0;
6
+ while (i < value.length) {
7
+ const unit = value.charCodeAt(i);
8
+ if (unit >= 55296 && unit < 56320 && i + 1 < value.length) {
9
+ const low = value.charCodeAt(i + 1);
10
+ if (low >= 56320 && low < 57344) i++;
11
+ }
12
+ i++;
13
+ counted++;
14
+ if (counted > maximum) return true;
15
+ }
16
+ return false;
17
+ }
18
+ var SURROGATE_UNIT = /[\ud800-\udfff]/;
19
+ function holdsUnpairedSurrogate(value) {
20
+ if (!SURROGATE_UNIT.test(value)) return false;
21
+ for (let i = 0; i < value.length; i++) {
22
+ const unit = value.charCodeAt(i);
23
+ if (unit < 55296 || unit >= 57344) continue;
24
+ if (unit >= 56320) return true;
25
+ const low = value.charCodeAt(i + 1);
26
+ if (!(low >= 56320 && low < 57344)) return true;
27
+ i++;
28
+ }
29
+ return false;
30
+ }
31
+ function codePointCount(value) {
32
+ let counted = 0;
33
+ for (let i = 0; i < value.length; i++) {
34
+ const unit = value.charCodeAt(i);
35
+ if (unit >= 55296 && unit < 56320 && i + 1 < value.length) {
36
+ const low = value.charCodeAt(i + 1);
37
+ if (low >= 56320 && low < 57344) i++;
38
+ }
39
+ counted++;
40
+ }
41
+ return counted;
42
+ }
43
+ function adjustUtf16Unit(unit) {
44
+ if (unit < 55296) return unit;
45
+ if (unit < 57344) return unit + 8192;
46
+ return unit - 2048;
47
+ }
48
+ function compareCodePoints(a, b) {
49
+ const shorter = a.length < b.length ? a.length : b.length;
50
+ let i = 0;
51
+ while (i < shorter && a.charCodeAt(i) === b.charCodeAt(i)) i++;
52
+ if (i === shorter) return a.length - b.length;
53
+ return adjustUtf16Unit(a.charCodeAt(i)) - adjustUtf16Unit(b.charCodeAt(i));
54
+ }
55
+
56
+ export { codePointCount, compareCodePoints, exceedsCodePoints, holdsUnpairedSurrogate };
@@ -0,0 +1,275 @@
1
+ import { osqRecordBytes } from './chunk-37CD3OKS.mjs';
2
+ import { MAX_M, EARLIER_RELEASE_MIN_M, MIN_M } from './chunk-HMFNFVP7.mjs';
3
+ import { NarsilError, ErrorCodes } from './chunk-BWJVNZPR.mjs';
4
+
5
+ // src/vector/vector-index/dead-bits.ts
6
+ var NO_LIVE_VECTOR_AT_POSITION = -1;
7
+ function deadBytesFor(count) {
8
+ return Math.ceil(count / 8);
9
+ }
10
+ function isDead(dead, position) {
11
+ return dead !== null && (dead[position >> 3] >> (position & 7) & 1) === 1;
12
+ }
13
+ function countDead(dead, count) {
14
+ if (dead === null) return 0;
15
+ let found = 0;
16
+ for (let position = 0; position < count; position++) {
17
+ if (isDead(dead, position)) found += 1;
18
+ }
19
+ return found;
20
+ }
21
+ function deadBitsWhere(count, isDeadAt) {
22
+ let dead = null;
23
+ for (let position = 0; position < count; position++) {
24
+ if (!isDeadAt(position)) continue;
25
+ dead ??= new Uint8Array(deadBytesFor(count));
26
+ dead[position >> 3] |= 1 << (position & 7);
27
+ }
28
+ return dead;
29
+ }
30
+
31
+ // src/vector/vector-index/payload.ts
32
+ var VECTOR_INDEX_PAYLOAD_VERSION = 3;
33
+ var VECTOR_INDEX_PART_VECTORS = 65536;
34
+ var LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
35
+ function vectorsToBytes(vectors) {
36
+ if (LITTLE_ENDIAN) return new Uint8Array(vectors.buffer, vectors.byteOffset, vectors.byteLength);
37
+ const bytes = new Uint8Array(vectors.byteLength);
38
+ const view = new DataView(bytes.buffer);
39
+ for (let i = 0; i < vectors.length; i++) view.setFloat32(i * 4, vectors[i], true);
40
+ return bytes;
41
+ }
42
+ function bytesToVectors(bytes) {
43
+ if (LITTLE_ENDIAN && bytes.byteOffset % 4 === 0) {
44
+ return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
45
+ }
46
+ const vectors = new Float32Array(bytes.byteLength / 4);
47
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
48
+ for (let i = 0; i < vectors.length; i++) vectors[i] = view.getFloat32(i * 4, true);
49
+ return vectors;
50
+ }
51
+ function invalid(message, details) {
52
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Invalid vector index payload: ${message}`, details);
53
+ }
54
+ function isRecord(value) {
55
+ return typeof value === "object" && value !== null && !Array.isArray(value);
56
+ }
57
+ function isNonNegativeInteger(value) {
58
+ return typeof value === "number" && Number.isInteger(value) && value >= 0;
59
+ }
60
+ function isSerializedGraph(value) {
61
+ return isRecord(value) && Array.isArray(value.nodes);
62
+ }
63
+ function codesOf(raw, dimension, count) {
64
+ if (raw === null || raw === void 0) return null;
65
+ if (!isRecord(raw)) invalid("codes must be a map or nil");
66
+ const bits = raw.bits;
67
+ if (bits !== 1 && bits !== 2 && bits !== 4 && bits !== 8) invalid("codes.bits must be 8, 4, 2, or 1", { bits });
68
+ if (!Array.isArray(raw.centroid) || raw.centroid.length !== dimension) {
69
+ invalid("codes.centroid must hold one number per dimension");
70
+ }
71
+ if (!(raw.records instanceof Uint8Array)) invalid("codes.records must be bytes");
72
+ if (raw.records.byteLength !== count * osqRecordBytes(dimension, bits)) {
73
+ invalid("codes.records must hold one record per vector", { count, bytes: raw.records.byteLength });
74
+ }
75
+ return { bits, centroid: raw.centroid.map(Number), records: raw.records };
76
+ }
77
+ function decodeVectorIndexPart(raw) {
78
+ if (!isRecord(raw)) invalid("payload must be a map");
79
+ if (raw.v !== VECTOR_INDEX_PAYLOAD_VERSION) {
80
+ throw new NarsilError(
81
+ ErrorCodes.ENVELOPE_VERSION_MISMATCH,
82
+ `This vector index payload was written in layout ${String(raw.v ?? 1)}, and this build reads layout ${VECTOR_INDEX_PAYLOAD_VERSION} alone`,
83
+ { version: raw.v ?? 1 }
84
+ );
85
+ }
86
+ if (typeof raw.fieldName !== "string") invalid("fieldName must be a string");
87
+ if (!isNonNegativeInteger(raw.dimension) || raw.dimension === 0) invalid("dimension must be a positive integer");
88
+ if (!isNonNegativeInteger(raw.part)) invalid("part must be a non-negative integer");
89
+ if (!isNonNegativeInteger(raw.parts) || raw.parts === 0 || raw.part >= raw.parts) {
90
+ invalid("parts must exceed part", { part: raw.part, parts: raw.parts });
91
+ }
92
+ if (!Array.isArray(raw.docIds) || !raw.docIds.every((docId) => typeof docId === "string")) {
93
+ invalid("docIds must be a list of strings");
94
+ }
95
+ if (raw.docIds.length > VECTOR_INDEX_PART_VECTORS) {
96
+ invalid(`a part holds at most ${VECTOR_INDEX_PART_VECTORS} vectors`, { count: raw.docIds.length });
97
+ }
98
+ if (!Array.isArray(raw.graphs) || !raw.graphs.every(isSerializedGraph)) {
99
+ invalid("graphs must be a list of graphs, each holding a list of nodes");
100
+ }
101
+ if (!(raw.vectors instanceof Uint8Array)) invalid("vectors must be bytes");
102
+ if (raw.vectors.byteLength !== raw.docIds.length * raw.dimension * 4) {
103
+ invalid("vectors must hold dimension float32 components per document", {
104
+ count: raw.docIds.length,
105
+ bytes: raw.vectors.byteLength
106
+ });
107
+ }
108
+ return {
109
+ v: VECTOR_INDEX_PAYLOAD_VERSION,
110
+ fieldName: raw.fieldName,
111
+ dimension: raw.dimension,
112
+ part: raw.part,
113
+ parts: raw.parts,
114
+ docIds: raw.docIds,
115
+ graphs: raw.graphs,
116
+ codes: codesOf(raw.codes, raw.dimension, raw.docIds.length),
117
+ vectors: raw.vectors
118
+ };
119
+ }
120
+ function decodeVectorIndexParts(raw) {
121
+ if (!Array.isArray(raw)) invalid("a field must carry a list of parts");
122
+ return raw.map(decodeVectorIndexPart);
123
+ }
124
+
125
+ // src/vector/vector-index/checkpoint-payload.ts
126
+ var VECTOR_FILE_PAYLOAD_VERSION = 1;
127
+ var VECTOR_GRAPH_PAYLOAD_VERSION = 1;
128
+ var VECTOR_GRAPH_PART_PAYLOAD_VERSION = 2;
129
+ var VECTOR_GRAPH_PART_VECTORS = 16384;
130
+ var VECTOR_FILE_MAX_VECTORS = VECTOR_INDEX_PART_VECTORS;
131
+ var METRICS = ["cosine", "dotProduct", "euclidean"];
132
+ function invalid2(payload, message, details) {
133
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Invalid ${payload} payload: ${message}`, details);
134
+ }
135
+ function isRecord2(value) {
136
+ return typeof value === "object" && value !== null && !Array.isArray(value);
137
+ }
138
+ function isIntegerUpTo(value, highest) {
139
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= highest;
140
+ }
141
+ function requireVersion(payload, found, expected) {
142
+ if (found === expected) return;
143
+ throw new NarsilError(
144
+ ErrorCodes.ENVELOPE_VERSION_MISMATCH,
145
+ `This ${payload} payload was written in layout ${String(found)}, and this build reads layout ${expected} alone`,
146
+ { version: found }
147
+ );
148
+ }
149
+ function decodeVectorFilePayload(raw) {
150
+ const name = "vector file";
151
+ if (!isRecord2(raw)) invalid2(name, "payload must be a map");
152
+ requireVersion(name, raw.v, VECTOR_FILE_PAYLOAD_VERSION);
153
+ if (typeof raw.fieldName !== "string") invalid2(name, "fieldName must be a string");
154
+ if (!isIntegerUpTo(raw.dimension, 65535) || raw.dimension === 0) invalid2(name, "dimension must be a positive uint16");
155
+ if (!Array.isArray(raw.docIds) || !raw.docIds.every((docId) => typeof docId === "string")) {
156
+ invalid2(name, "docIds must be a list of strings");
157
+ }
158
+ if (raw.docIds.length > VECTOR_FILE_MAX_VECTORS) {
159
+ invalid2(name, `a file holds at most ${VECTOR_FILE_MAX_VECTORS} vectors`, { count: raw.docIds.length });
160
+ }
161
+ if (!(raw.vectors instanceof Uint8Array)) invalid2(name, "vectors must be bytes");
162
+ if (raw.vectors.byteLength !== raw.docIds.length * raw.dimension * 4) {
163
+ invalid2(name, "vectors must hold dimension float32 components per document", {
164
+ count: raw.docIds.length,
165
+ bytes: raw.vectors.byteLength
166
+ });
167
+ }
168
+ return {
169
+ v: VECTOR_FILE_PAYLOAD_VERSION,
170
+ fieldName: raw.fieldName,
171
+ dimension: raw.dimension,
172
+ docIds: raw.docIds,
173
+ codes: codesOf(raw.codes, raw.dimension, raw.docIds.length),
174
+ vectors: raw.vectors
175
+ };
176
+ }
177
+ function isMetric(value) {
178
+ return METRICS.some((metric) => metric === value);
179
+ }
180
+ function calibratedMeanLengthOf(name, value) {
181
+ if (value === void 0 || value === null) return null;
182
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
183
+ invalid2(name, "calibratedMeanLength must be a finite float64 of at least 0");
184
+ }
185
+ return value;
186
+ }
187
+ function decodeNumberedGraph(raw, minimumM) {
188
+ const name = "vector graph";
189
+ if (!isRecord2(raw)) invalid2(name, "each graph must be a map");
190
+ const entryPoint = raw.entryPoint ?? null;
191
+ if (entryPoint !== null && !isIntegerUpTo(entryPoint, 4294967295))
192
+ invalid2(name, "entryPoint must be a uint32 or nil");
193
+ if (!isIntegerUpTo(raw.maxLayer, 255)) invalid2(name, "maxLayer must be a uint8");
194
+ if (!isIntegerUpTo(raw.m, MAX_M) || raw.m < minimumM) {
195
+ invalid2(name, `m must be a whole number from ${minimumM} to ${MAX_M}`);
196
+ }
197
+ if (!isIntegerUpTo(raw.efConstruction, 65535)) invalid2(name, "efConstruction must be a uint16");
198
+ if (!isMetric(raw.metric)) invalid2(name, "metric must name a supported metric", { metric: raw.metric });
199
+ if (!(raw.levels instanceof Uint8Array)) invalid2(name, "levels must be bytes");
200
+ if (!(raw.neighbours instanceof Uint8Array)) invalid2(name, "neighbours must be bytes");
201
+ const graph = {
202
+ entryPoint,
203
+ maxLayer: raw.maxLayer,
204
+ m: raw.m,
205
+ efConstruction: raw.efConstruction,
206
+ metric: raw.metric,
207
+ levels: raw.levels,
208
+ neighbours: raw.neighbours
209
+ };
210
+ const calibratedMeanLength = calibratedMeanLengthOf(name, raw.calibratedMeanLength);
211
+ const shuffledBuildCount = raw.shuffledBuildCount ?? null;
212
+ if (shuffledBuildCount === null) return graph;
213
+ if (!isIntegerUpTo(shuffledBuildCount, 4294967295)) invalid2(name, "shuffledBuildCount must be a uint32");
214
+ if (calibratedMeanLength === null) return { ...graph, shuffledBuildCount };
215
+ return { ...graph, shuffledBuildCount, calibratedMeanLength };
216
+ }
217
+ function decodeVectorGraphPayload(raw) {
218
+ const name = "vector graph";
219
+ if (!isRecord2(raw)) invalid2(name, "payload must be a map");
220
+ requireVersion(name, raw.v, VECTOR_GRAPH_PAYLOAD_VERSION);
221
+ if (typeof raw.fieldName !== "string") invalid2(name, "fieldName must be a string");
222
+ if (!Array.isArray(raw.graphs)) invalid2(name, "graphs must be a list");
223
+ return {
224
+ v: VECTOR_GRAPH_PAYLOAD_VERSION,
225
+ fieldName: raw.fieldName,
226
+ graphs: raw.graphs.map((graph) => decodeNumberedGraph(graph, EARLIER_RELEASE_MIN_M))
227
+ };
228
+ }
229
+ function decodeVectorGraphPartPayload(raw) {
230
+ const name = "vector graph part";
231
+ if (!isRecord2(raw)) invalid2(name, "payload must be a map");
232
+ requireVersion(name, raw.v, VECTOR_GRAPH_PART_PAYLOAD_VERSION);
233
+ if (typeof raw.fieldName !== "string") invalid2(name, "fieldName must be a string");
234
+ if (!Array.isArray(raw.graphs)) invalid2(name, "graphs must be a list");
235
+ return {
236
+ v: VECTOR_GRAPH_PART_PAYLOAD_VERSION,
237
+ fieldName: raw.fieldName,
238
+ graphs: raw.graphs.map((graph) => decodeNumberedGraph(graph, MIN_M))
239
+ };
240
+ }
241
+ function sameGraphHeader(a, b) {
242
+ return a.entryPoint === b.entryPoint && a.maxLayer === b.maxLayer && a.m === b.m && a.efConstruction === b.efConstruction && a.metric === b.metric && a.shuffledBuildCount === b.shuffledBuildCount && a.calibratedMeanLength === b.calibratedMeanLength;
243
+ }
244
+ function assembleGraphParts(parts) {
245
+ const name = "vector graph part";
246
+ const first = parts[0];
247
+ if (first === void 0) invalid2(name, "a field that holds a graph must have at least one part");
248
+ const partLists = first.graphs.map(() => []);
249
+ for (let p = 0; p < parts.length; p++) {
250
+ const part = parts[p];
251
+ const isLast = p === parts.length - 1;
252
+ if (part.fieldName !== first.fieldName || part.graphs.length !== first.graphs.length) {
253
+ invalid2(name, `part ${p} lists other graphs than part 0`, { part: p });
254
+ }
255
+ for (let g = 0; g < part.graphs.length; g++) {
256
+ const graph = part.graphs[g];
257
+ if (!sameGraphHeader(graph, first.graphs[g]))
258
+ invalid2(name, `part ${p} gives graph ${g} other headers`, { part: p });
259
+ const levels = graph.levels.length;
260
+ const fits = isLast ? levels >= 1 && levels <= VECTOR_GRAPH_PART_VECTORS : levels === VECTOR_GRAPH_PART_VECTORS;
261
+ if (!fits) invalid2(name, `part ${p} gives graph ${g} ${levels} bytes of levels`, { part: p, levels });
262
+ partLists[g].push(graph);
263
+ }
264
+ }
265
+ const graphs = partLists.map((list) => ({ partSize: VECTOR_GRAPH_PART_VECTORS, parts: list }));
266
+ return { fieldName: first.fieldName, graphs };
267
+ }
268
+ function graphPartsOfEarlierPayload(payload) {
269
+ return {
270
+ fieldName: payload.fieldName,
271
+ graphs: payload.graphs.map((graph) => ({ partSize: Math.max(graph.levels.length, 1), parts: [graph] }))
272
+ };
273
+ }
274
+
275
+ export { NO_LIVE_VECTOR_AT_POSITION, VECTOR_FILE_MAX_VECTORS, VECTOR_FILE_PAYLOAD_VERSION, VECTOR_GRAPH_PART_PAYLOAD_VERSION, VECTOR_GRAPH_PART_VECTORS, assembleGraphParts, bytesToVectors, countDead, deadBitsWhere, deadBytesFor, decodeVectorFilePayload, decodeVectorGraphPartPayload, decodeVectorGraphPayload, decodeVectorIndexParts, graphPartsOfEarlierPayload, isDead, vectorsToBytes };
@@ -0,0 +1,144 @@
1
+ import { readMetadataEnvelope } from './chunk-PNFKUCG3.mjs';
2
+ import { readWalSegments, durableRecordMissing, checkpointLastSeqNo, SEGMENT_HEADER_SIZE } from './chunk-J5QP52HA.mjs';
3
+ import { applyDeleteEntry, applyIndexEntry } from './chunk-XADZPPZI.mjs';
4
+
5
+ // src/persistence/durability/recovery.ts
6
+ async function listPersistedIndexes(directory) {
7
+ const metaKeys = await directory.list("");
8
+ const names = [];
9
+ for (const key of metaKeys) {
10
+ if (key.endsWith("/meta")) {
11
+ names.push(key.slice(0, -"/meta".length));
12
+ }
13
+ }
14
+ return names;
15
+ }
16
+ async function loadMetadata(directory, indexName) {
17
+ const bytes = await directory.read(`${indexName}/meta`);
18
+ if (bytes === null) {
19
+ return null;
20
+ }
21
+ const { metadata } = await readMetadataEnvelope(bytes);
22
+ return metadata;
23
+ }
24
+ async function replayWal(directory, indexName, partitionId, fromSeqNoExclusive, deps) {
25
+ const read = await readWalSegments(directory, indexName, partitionId);
26
+ if (read === null) {
27
+ return { highestSeqNo: fromSeqNoExclusive, highestPrimaryTerm: 0 };
28
+ }
29
+ const { marker, segments, durableEntries, activeTail, highestReadFromWal } = read;
30
+ if (Math.max(fromSeqNoExclusive, highestReadFromWal) < marker.state.highestDurableSeqNo) {
31
+ throw durableRecordMissing(indexName, partitionId, highestReadFromWal, marker);
32
+ }
33
+ let highestSeqNo = fromSeqNoExclusive;
34
+ let highestPrimaryTerm = 0;
35
+ const replay = (entries) => {
36
+ for (const entry of entries) {
37
+ if (entry.seqNo <= highestSeqNo) {
38
+ continue;
39
+ }
40
+ applyEntry(entry, deps);
41
+ highestSeqNo = entry.seqNo;
42
+ if (entry.primaryTerm > highestPrimaryTerm) {
43
+ highestPrimaryTerm = entry.primaryTerm;
44
+ }
45
+ }
46
+ };
47
+ replay(durableEntries);
48
+ if (activeTail !== null) {
49
+ replay(activeTail.entries);
50
+ }
51
+ await deleteOrphanSegments(directory, segments, marker.state.activeSegmentSeqNo);
52
+ if (activeTail !== null && activeTail.cleanEnd < activeTail.segmentLength) {
53
+ await truncateSegmentTail(directory, activeTail.key, activeTail.cleanEnd);
54
+ }
55
+ return { highestSeqNo, highestPrimaryTerm };
56
+ }
57
+ async function replaySyncedWal(directory, indexName, partitionId, fromSeqNoExclusive, deps) {
58
+ const read = await readWalSegments(directory, indexName, partitionId);
59
+ if (read === null) {
60
+ return { highestSeqNo: fromSeqNoExclusive, highestPrimaryTerm: 0 };
61
+ }
62
+ const replayed = replayDurableEntries(
63
+ read,
64
+ indexName,
65
+ partitionId,
66
+ fromSeqNoExclusive,
67
+ Number.POSITIVE_INFINITY,
68
+ deps
69
+ );
70
+ await deleteOrphanSegments(directory, read.segments, read.marker.state.activeSegmentSeqNo);
71
+ if (read.activeTail !== null) {
72
+ await truncateSegmentTail(directory, read.activeTail.key, read.marker.state.durableByteLength);
73
+ }
74
+ return replayed;
75
+ }
76
+ function replayDurableEntries(read, indexName, partitionId, fromSeqNoExclusive, upToSeqNoInclusive, deps) {
77
+ if (Math.max(fromSeqNoExclusive, read.highestReadFromWal) < read.marker.state.highestDurableSeqNo) {
78
+ throw durableRecordMissing(indexName, partitionId, read.highestReadFromWal, read.marker);
79
+ }
80
+ let highestSeqNo = fromSeqNoExclusive;
81
+ let highestPrimaryTerm = 0;
82
+ for (const entry of read.durableEntries) {
83
+ if (entry.seqNo <= fromSeqNoExclusive || entry.seqNo > upToSeqNoInclusive) {
84
+ continue;
85
+ }
86
+ applyEntry(entry, deps);
87
+ if (entry.seqNo > highestSeqNo) {
88
+ highestSeqNo = entry.seqNo;
89
+ }
90
+ if (entry.primaryTerm > highestPrimaryTerm) {
91
+ highestPrimaryTerm = entry.primaryTerm;
92
+ }
93
+ }
94
+ return { highestSeqNo, highestPrimaryTerm };
95
+ }
96
+ async function deleteOrphanSegments(directory, segments, activeSegmentSeqNo) {
97
+ for (const { key, startSeqNo } of segments) {
98
+ if (startSeqNo > activeSegmentSeqNo) {
99
+ await directory.remove(key);
100
+ }
101
+ }
102
+ }
103
+ async function truncateSegmentTail(directory, key, durableByteLength) {
104
+ const cleanLength = Math.max(durableByteLength, SEGMENT_HEADER_SIZE);
105
+ const handle = await directory.appendHandle(key);
106
+ try {
107
+ const currentSize = await handle.size();
108
+ if (cleanLength < currentSize) {
109
+ await handle.truncate(cleanLength);
110
+ }
111
+ } finally {
112
+ await handle.close();
113
+ }
114
+ }
115
+ function applyEntry(entry, deps) {
116
+ if (entry.operation === "DELETE") {
117
+ applyDeleteEntry(entry, deps.manager, deps.vectorIndexes);
118
+ return;
119
+ }
120
+ applyIndexEntry(entry, deps.manager, deps.vectorFieldPaths, deps.vectorIndexes);
121
+ }
122
+ function snapshotCheckpointFor(checkpoint, partitionId) {
123
+ return checkpointLastSeqNo(checkpoint, partitionId);
124
+ }
125
+
126
+ // src/persistence/durability/manager-state.ts
127
+ function closePartitionWriter(partition) {
128
+ return partition.failed === null ? partition.walWriter.close() : partition.walWriter.abandon();
129
+ }
130
+ async function drainIndexStateForUnload(indexState, pendingMetadataWrite, closeWalWriter) {
131
+ indexState.unloading = true;
132
+ try {
133
+ if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
134
+ await pendingMetadataWrite;
135
+ for (const partition of indexState.partitions.values()) {
136
+ await closeWalWriter(closePartitionWriter(partition));
137
+ }
138
+ } catch (error) {
139
+ indexState.unloading = false;
140
+ throw error;
141
+ }
142
+ }
143
+
144
+ export { closePartitionWriter, drainIndexStateForUnload, listPersistedIndexes, loadMetadata, replaySyncedWal, replayWal, snapshotCheckpointFor };