@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,3537 @@
1
+ import { requirePatternSearchFor, requirePartialWordSearchFor, createPartitionIndex, buildPartialWordTableFrom, recordValueSize, patternIndexOf, encodeFieldIndexes, encodePartialSpellingsOf, countPartialSpelling, copyVectorsInto, isCompositePartition, fnv1a, partialSpellingsOfDocuments, createFrozenPartialWords, requirePatternSearch, documentValueBytes, cloneProjected, projectIncludedPaths, valueWithOwnBuffers, createSurfaceRegistry, MAX_TERM_FREQUENCY, joinPatternWorkMeter, fanOutQuery, sanitizeGlobalStats, countsWithoutScores, fanOutMatchCount, createCompositePartition, createGeoIndex, documentWithOwnBuffers, readProjectedPath, createNumericIndex, createBooleanIndex, createEnumIndex, ValueAffixes } from './chunk-VI4R6W25.mjs';
2
+ import { generateId, getLanguage } from './chunk-ZM7KU4YW.mjs';
3
+ import { readHeapStatistics } from './chunk-LPQOEU5M.mjs';
4
+ import { GrowableUint32, bitsetHas, bitsetSet, createBitSet } from './chunk-PC2G76VW.mjs';
5
+ import { tokenizeOptions, getNestedValue, sameFields, visitDocumentSurfaceSpellings, visitDocumentSpellings } from './chunk-OM362BRO.mjs';
6
+ import { schemaFieldsOf, partialFieldsOf, encodeDocument, validateDocument, validateDocumentStrict, extractVectorFieldsFromSchema } from './chunk-OEYI3KRB.mjs';
7
+ import { fuzzyPrefixOf, boundedLevenshtein } from './chunk-GES7U2PM.mjs';
8
+ import { isWordIndexedBase } from './chunk-LKOB5COA.mjs';
9
+ import { resolveIndexAnalysis, synonymSearchModule } from './chunk-46D6JQIG.mjs';
10
+ import { visitTokens, codePointOrder, encodeStringBlob, encodeLookupKey, SEGMENT_TOKEN_TABLE_START, SEGMENT_POSTING_LOG_START, SEGMENT_POSITION_POOL_START, SEGMENT_FIELD_SCRATCH_START, FROZEN_REMOVAL_INDEX_MAX_PROCESS_FRACTION } from './chunk-UTG5YNXV.mjs';
11
+ import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
12
+ import { loadedVectorCode, loadVectorCode, loadVectorCodeFor, requireLoadedVectorCode } from './chunk-Q6XWVUZX.mjs';
13
+ import { PROTOTYPE_POLLUTION_KEYS } from './chunk-L5ORED7V.mjs';
14
+ import { NarsilError, ErrorCodes } from './chunk-BWJVNZPR.mjs';
15
+ import { Decoder, decode } from '@msgpack/msgpack';
16
+ import { totalmem } from 'os';
17
+
18
+ // src/core/synonyms/platform.ts
19
+ function platformSynonymSearch() {
20
+ return synonymSearchModule;
21
+ }
22
+
23
+ // src/core/synonyms/registry.ts
24
+ var registered = null;
25
+ var MODULE_FUNCTIONS = ["prepare", "storedLookup", "decodeRules", "replace", "applyLoaded"];
26
+ function isSynonymSearchModule(value) {
27
+ if (value === null || typeof value !== "object" || value.name !== "synonyms") return false;
28
+ return MODULE_FUNCTIONS.every((name) => typeof Reflect.get(value, name) === "function");
29
+ }
30
+ function registerSynonyms(module) {
31
+ if (!isSynonymSearchModule(module)) {
32
+ throw new NarsilError(
33
+ ErrorCodes.CONFIG_INVALID,
34
+ 'Pass registerSynonyms the synonyms object that "@delali/narsil/synonyms" exports'
35
+ );
36
+ }
37
+ registered = module;
38
+ }
39
+ function availableSynonymModule() {
40
+ return registered ?? platformSynonymSearch();
41
+ }
42
+ function synonymModule() {
43
+ const module = availableSynonymModule();
44
+ if (module !== null) return module;
45
+ throw new NarsilError(
46
+ ErrorCodes.CONFIG_INVALID,
47
+ "This bundle holds no synonym code, so import { synonyms } from '@delali/narsil/synonyms' and pass it to registerSynonyms before you use a synonym set"
48
+ );
49
+ }
50
+ function isEmptySynonymSet(value) {
51
+ return value === void 0 || Array.isArray(value) && value.length === 0;
52
+ }
53
+
54
+ // src/partitioning/insert-options.ts
55
+ function resolvePartialWordAnalysis(config, language, analysis) {
56
+ const fields = partialFieldsOf(config.schema);
57
+ if (fields.length === 0) return null;
58
+ return { fields, language, options: resolvePartitionInsertOptions(config, analysis) };
59
+ }
60
+ function resolvePartitionInsertOptions(config, analysis, options) {
61
+ const applyStrict = config.strict === true;
62
+ const applyAnalyzer = analysis.stopWords !== void 0 || analysis.customTokenizer !== void 0;
63
+ const applySurfaces = config.surfaceForms !== false;
64
+ const applyPatternLimit = config.patternValueLimit !== void 0;
65
+ if (!applyStrict && !applyAnalyzer && !applySurfaces && !applyPatternLimit) return options;
66
+ const resolved = { ...options };
67
+ if (applyStrict) resolved.strict = true;
68
+ if (applyPatternLimit) resolved.patternValueLimit = config.patternValueLimit;
69
+ if (applyAnalyzer) {
70
+ resolved.stopWordOverride = options?.stopWordOverride ?? analysis.stopWords;
71
+ resolved.customTokenizer = options?.customTokenizer ?? analysis.customTokenizer;
72
+ }
73
+ if (applySurfaces) resolved.collectSurfaces = true;
74
+ return resolved;
75
+ }
76
+
77
+ // src/partitioning/manager/capacity.ts
78
+ function createCapacityChecks(source) {
79
+ const { indexName, config } = source;
80
+ return {
81
+ assertCapacity(pendingWrites = 0, partitionCountCap) {
82
+ const maxDocsPerPartition = config.partitions?.maxDocsPerPartition;
83
+ if (maxDocsPerPartition === void 0) return;
84
+ const partitionCount = source.partitionCount();
85
+ const effectivePartitionCount = partitionCountCap === void 0 ? partitionCount : Math.min(partitionCount, partitionCountCap);
86
+ const totalCapacity = maxDocsPerPartition * effectivePartitionCount;
87
+ const currentCount = source.documentCount() + pendingWrites;
88
+ if (currentCount < totalCapacity) return;
89
+ throw new NarsilError(
90
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
91
+ `Index "${indexName}" has reached its capacity of ${totalCapacity} documents (${maxDocsPerPartition} per partition \xD7 ${effectivePartitionCount} partitions)`,
92
+ {
93
+ indexName,
94
+ currentCount,
95
+ totalCapacity,
96
+ maxDocsPerPartition,
97
+ partitionCount: effectivePartitionCount
98
+ }
99
+ );
100
+ },
101
+ assertPartitionCapacity(partitionId, pendingWrites = 0) {
102
+ const maxDocsPerPartition = config.partitions?.maxDocsPerPartition;
103
+ if (maxDocsPerPartition === void 0) return;
104
+ const currentCount = source.getPartition(partitionId).count() + pendingWrites;
105
+ if (currentCount < maxDocsPerPartition) return;
106
+ throw new NarsilError(
107
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
108
+ `Partition ${partitionId} of index "${indexName}" has reached its capacity of ${maxDocsPerPartition} documents`,
109
+ { indexName, partitionId, currentCount, maxDocsPerPartition }
110
+ );
111
+ }
112
+ };
113
+ }
114
+
115
+ // src/partitioning/manager/segments.ts
116
+ function requireNewSegmentDocuments(segment, docPartitionMap) {
117
+ for (const internalId of segment.docStore.allInternalIds()) {
118
+ const docId = segment.docStore.getExternalId(internalId);
119
+ if (docId === void 0 || !docPartitionMap.has(docId)) continue;
120
+ throw new NarsilError(ErrorCodes.DOC_ALREADY_EXISTS, `Document "${docId}" already exists`, {
121
+ docId,
122
+ partitionId: docPartitionMap.get(docId)
123
+ });
124
+ }
125
+ }
126
+
127
+ // src/partitioning/manager/stats.ts
128
+ function aggregateStatsOf(partitions) {
129
+ let totalDocuments = 0;
130
+ const docFrequencies = /* @__PURE__ */ Object.create(null);
131
+ const totalFieldLengths = /* @__PURE__ */ Object.create(null);
132
+ for (let i = 0; i < partitions.length; i++) {
133
+ const stats = partitions[i].stats;
134
+ totalDocuments += stats.totalDocuments;
135
+ for (const [term, freq] of Object.entries(stats.docFrequencies)) {
136
+ docFrequencies[term] = (docFrequencies[term] ?? 0) + freq;
137
+ }
138
+ for (const [field, length] of Object.entries(stats.totalFieldLengths)) {
139
+ totalFieldLengths[field] = (totalFieldLengths[field] ?? 0) + length;
140
+ }
141
+ }
142
+ return { totalDocuments, docFrequencies, totalFieldLengths };
143
+ }
144
+ function estimateBytesOf(partitions, vecIndexes) {
145
+ let total = 0;
146
+ for (let i = 0; i < partitions.length; i++) {
147
+ total += partitions[i].estimateMemoryBytes();
148
+ }
149
+ for (const [, vecIndex] of vecIndexes) {
150
+ total += vecIndex.estimateMemoryBytes();
151
+ }
152
+ return total;
153
+ }
154
+ function partitionStatsOf(partitions) {
155
+ return partitions.map((p, i) => ({
156
+ partitionId: i,
157
+ documentCount: p.count(),
158
+ estimatedMemoryBytes: p.estimateMemoryBytes()
159
+ }));
160
+ }
161
+
162
+ // src/partitioning/manager/write-log.ts
163
+ function createWriteLogRecorder() {
164
+ let current = null;
165
+ return {
166
+ open() {
167
+ const log = { docIds: /* @__PURE__ */ new Set(), replaced: false };
168
+ current = log;
169
+ return {
170
+ get partitionsReplaced() {
171
+ return log.replaced;
172
+ },
173
+ get pendingDocuments() {
174
+ return log.docIds.size;
175
+ },
176
+ takeWrittenDocIds() {
177
+ const taken = log.docIds;
178
+ log.docIds = /* @__PURE__ */ new Set();
179
+ return taken;
180
+ },
181
+ close() {
182
+ if (current === log) current = null;
183
+ }
184
+ };
185
+ },
186
+ documentWritten(docId) {
187
+ if (current !== null) current.docIds.add(docId);
188
+ },
189
+ segmentWritten(segment) {
190
+ if (current === null) return;
191
+ for (const internalId of segment.docStore.allInternalIds()) {
192
+ const docId = segment.docStore.getExternalId(internalId);
193
+ if (docId !== void 0) current.docIds.add(docId);
194
+ }
195
+ },
196
+ partitionsReplaced() {
197
+ if (current !== null) current.replaced = true;
198
+ }
199
+ };
200
+ }
201
+
202
+ // src/partitioning/manager/index.ts
203
+ function createPartitionManager(indexName, config, language, router, initialPartitionCount, vectorIndexes) {
204
+ requirePatternSearchFor(config.schema);
205
+ requirePartialWordSearchFor(config.schema);
206
+ const count = initialPartitionCount ?? 1;
207
+ const analysis = resolveIndexAnalysis(config);
208
+ const partialWordAnalysis = resolvePartialWordAnalysis(config, language, analysis);
209
+ const textAnalysis = { language, options: resolvePartitionInsertOptions(config, analysis) };
210
+ const trackPositions = config.trackPositions ?? true;
211
+ const vecIndexes = vectorIndexes ?? /* @__PURE__ */ new Map();
212
+ let partitions = [];
213
+ const docPartitionMap = /* @__PURE__ */ new Map();
214
+ const writeLog = createWriteLogRecorder();
215
+ for (let i = 0; i < count; i++) {
216
+ partitions.push(createPartitionIndex(i, trackPositions));
217
+ }
218
+ function validatePartitionId(partitionId) {
219
+ if (partitionId < 0 || partitionId >= partitions.length) {
220
+ throw new NarsilError(
221
+ ErrorCodes.INDEX_NOT_FOUND,
222
+ `Partition ${partitionId} is out of bounds (0..${partitions.length - 1})`,
223
+ { partitionId, partitionCount: partitions.length }
224
+ );
225
+ }
226
+ }
227
+ function resolveInsertOptions(options) {
228
+ return resolvePartitionInsertOptions(config, analysis, options);
229
+ }
230
+ function preparePartialWords(partitionId) {
231
+ if (partialWordAnalysis !== null) partitions[partitionId].preparePartialWords(partialWordAnalysis);
232
+ }
233
+ function locateDocument(docId) {
234
+ return docPartitionMap.get(docId);
235
+ }
236
+ function asCompositePartition(partitionId) {
237
+ const existing = partitions[partitionId];
238
+ if (isCompositePartition(existing)) return existing;
239
+ const composite = createCompositePartition(partitionId, trackPositions, existing, textAnalysis);
240
+ partitions[partitionId] = composite;
241
+ return composite;
242
+ }
243
+ function rebuildDocPartitionMap() {
244
+ docPartitionMap.clear();
245
+ for (let i = 0; i < partitions.length; i++) {
246
+ for (const docId of partitions[i].docIds()) {
247
+ docPartitionMap.set(docId, i);
248
+ }
249
+ }
250
+ }
251
+ function registerSegmentDocuments(partitionId, segment) {
252
+ for (const internalId of segment.docStore.allInternalIds()) {
253
+ const docId = segment.docStore.getExternalId(internalId);
254
+ if (docId !== void 0) docPartitionMap.set(docId, partitionId);
255
+ }
256
+ }
257
+ function rebuildDocPartitionMapForPartition(partitionId) {
258
+ for (const [docId, pid] of docPartitionMap) {
259
+ if (pid === partitionId) {
260
+ docPartitionMap.delete(docId);
261
+ }
262
+ }
263
+ for (const docId of partitions[partitionId].docIds()) {
264
+ docPartitionMap.set(docId, partitionId);
265
+ }
266
+ }
267
+ const manager = {
268
+ get partitionCount() {
269
+ return partitions.length;
270
+ },
271
+ get indexName() {
272
+ return indexName;
273
+ },
274
+ get schema() {
275
+ return config.schema;
276
+ },
277
+ get language() {
278
+ return language;
279
+ },
280
+ get config() {
281
+ return config;
282
+ },
283
+ get analysis() {
284
+ return analysis;
285
+ },
286
+ get partialWordAnalysis() {
287
+ return partialWordAnalysis;
288
+ },
289
+ synonyms: null,
290
+ get textAnalysis() {
291
+ return textAnalysis;
292
+ },
293
+ getPartition(partitionId) {
294
+ validatePartitionId(partitionId);
295
+ return partitions[partitionId];
296
+ },
297
+ partitionAt(index) {
298
+ return partitions[index];
299
+ },
300
+ getAllPartitions() {
301
+ return [...partitions];
302
+ },
303
+ setPartitions(newPartitions) {
304
+ writeLog.partitionsReplaced();
305
+ partitions = [...newPartitions];
306
+ rebuildDocPartitionMap();
307
+ },
308
+ openWriteLog() {
309
+ return writeLog.open();
310
+ },
311
+ adoptRebuiltPartitions(rebuilt) {
312
+ if (rebuilt.length !== partitions.length) {
313
+ throw new NarsilError(
314
+ ErrorCodes.PARTITION_CORRUPTED,
315
+ `Index "${indexName}" holds ${partitions.length} partitions, but the rebuild produced ${rebuilt.length}`,
316
+ { indexName, partitionCount: partitions.length, rebuiltCount: rebuilt.length }
317
+ );
318
+ }
319
+ partitions = [...rebuilt];
320
+ },
321
+ addPartition() {
322
+ const currentMaxPartitions = config.partitions?.maxPartitions;
323
+ if (currentMaxPartitions !== void 0 && partitions.length >= currentMaxPartitions) {
324
+ throw new NarsilError(
325
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
326
+ `Cannot add partition: maximum of ${currentMaxPartitions} partitions reached`,
327
+ { maxPartitions: currentMaxPartitions, currentCount: partitions.length }
328
+ );
329
+ }
330
+ const partition = createPartitionIndex(partitions.length, trackPositions);
331
+ writeLog.partitionsReplaced();
332
+ partitions.push(partition);
333
+ return partition;
334
+ },
335
+ removePartition(partitionId) {
336
+ validatePartitionId(partitionId);
337
+ writeLog.partitionsReplaced();
338
+ partitions.splice(partitionId, 1);
339
+ rebuildDocPartitionMap();
340
+ },
341
+ trimPartitions(newCount) {
342
+ if (newCount < 1) {
343
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `Cannot trim index "${indexName}" below one partition`, {
344
+ indexName,
345
+ requestedCount: newCount
346
+ });
347
+ }
348
+ if (newCount >= partitions.length) {
349
+ return;
350
+ }
351
+ writeLog.partitionsReplaced();
352
+ for (const [docId, pid] of docPartitionMap) {
353
+ if (pid >= newCount) {
354
+ docPartitionMap.delete(docId);
355
+ }
356
+ }
357
+ partitions.length = newCount;
358
+ },
359
+ ...createCapacityChecks({
360
+ indexName,
361
+ config,
362
+ partitionCount: () => partitions.length,
363
+ documentCount: () => manager.countDocuments(),
364
+ getPartition: (partitionId) => manager.getPartition(partitionId)
365
+ }),
366
+ routePartition(docId) {
367
+ return router.route(docId, partitions.length);
368
+ },
369
+ insert(docId, document, options) {
370
+ const pid = router.route(docId, partitions.length);
371
+ const insertOpts = resolveInsertOptions(options);
372
+ partitions[pid].insert(docId, document, config.schema, language, insertOpts);
373
+ docPartitionMap.set(docId, pid);
374
+ writeLog.documentWritten(docId);
375
+ },
376
+ removedSpellingsOf(docId) {
377
+ const pid = locateDocument(docId);
378
+ const partition = pid === void 0 ? void 0 : partitions[pid];
379
+ return partition !== void 0 && isCompositePartition(partition) ? partition.removedSpellingsOf(docId) : void 0;
380
+ },
381
+ remove(docId, removedSpellings) {
382
+ const pid = locateDocument(docId);
383
+ if (pid === void 0) {
384
+ throw new NarsilError(ErrorCodes.DOC_NOT_FOUND, `Document "${docId}" not found in any partition`, { docId });
385
+ }
386
+ partitions[pid].remove(docId, config.schema, language, resolveInsertOptions(), removedSpellings);
387
+ docPartitionMap.delete(docId);
388
+ writeLog.documentWritten(docId);
389
+ },
390
+ beginBatchRemove() {
391
+ for (let i = 0; i < partitions.length; i++) {
392
+ partitions[i].beginBatch();
393
+ }
394
+ },
395
+ endBatchRemove() {
396
+ for (let i = 0; i < partitions.length; i++) {
397
+ partitions[i].endBatch();
398
+ }
399
+ },
400
+ update(docId, document, options, removedSpellings) {
401
+ const pid = locateDocument(docId);
402
+ if (pid === void 0) {
403
+ throw new NarsilError(ErrorCodes.DOC_NOT_FOUND, `Document "${docId}" not found in any partition`, { docId });
404
+ }
405
+ const updateOpts = resolveInsertOptions(options);
406
+ partitions[pid].update(docId, document, config.schema, language, updateOpts, removedSpellings);
407
+ docPartitionMap.set(docId, pid);
408
+ writeLog.documentWritten(docId);
409
+ },
410
+ get(docId, projection) {
411
+ const pid = locateDocument(docId);
412
+ if (pid === void 0) return void 0;
413
+ const doc = partitions[pid].get(docId, projection);
414
+ if (doc) copyVectorsInto(doc, docId, vecIndexes, projection);
415
+ return doc;
416
+ },
417
+ getRef(docId) {
418
+ const pid = locateDocument(docId);
419
+ if (pid === void 0) return void 0;
420
+ return partitions[pid].getRef(docId);
421
+ },
422
+ fieldValues(docId, fieldPaths) {
423
+ const pid = locateDocument(docId);
424
+ return pid === void 0 ? void 0 : partitions[pid].fieldValues(docId, fieldPaths);
425
+ },
426
+ sortValues(docId, fields, fieldTypes, modes) {
427
+ const pid = locateDocument(docId);
428
+ if (pid === void 0) return fields.map(() => null);
429
+ return partitions[pid].sortValues(docId, fields, fieldTypes, modes);
430
+ },
431
+ has(docId) {
432
+ return locateDocument(docId) !== void 0;
433
+ },
434
+ partitionIdOf(docId) {
435
+ return locateDocument(docId);
436
+ },
437
+ countDocuments() {
438
+ let total = 0;
439
+ for (let i = 0; i < partitions.length; i++) {
440
+ total += partitions[i].count();
441
+ }
442
+ return total;
443
+ },
444
+ serializePartition(partitionId) {
445
+ validatePartitionId(partitionId);
446
+ return partitions[partitionId].serialize(indexName, partitions.length, language.name, config.schema);
447
+ },
448
+ mergeSegment(partitionId, payload, documents) {
449
+ validatePartitionId(partitionId);
450
+ const partition = partitions[partitionId];
451
+ partition.beginBatch();
452
+ partition.mergeSegmentPayload(payload, documents);
453
+ partition.endBatch();
454
+ for (const docId of payload.docIds) {
455
+ docPartitionMap.set(docId, partitionId);
456
+ writeLog.documentWritten(docId);
457
+ }
458
+ },
459
+ attachFrozenSegment(partitionId, segment) {
460
+ validatePartitionId(partitionId);
461
+ requireNewSegmentDocuments(segment, docPartitionMap);
462
+ asCompositePartition(partitionId).attachFrozenSegment(segment);
463
+ registerSegmentDocuments(partitionId, segment);
464
+ writeLog.segmentWritten(segment);
465
+ },
466
+ swapFrozenSegments(partitionId, dropSegmentIds, replacement, removedSpellings) {
467
+ validatePartitionId(partitionId);
468
+ const applied = asCompositePartition(partitionId).swapFrozenSegments(
469
+ dropSegmentIds,
470
+ replacement,
471
+ removedSpellings
472
+ );
473
+ registerSegmentDocuments(partitionId, replacement);
474
+ return applied;
475
+ },
476
+ freezeLiveTail(partitionId, freeze) {
477
+ validatePartitionId(partitionId);
478
+ return asCompositePartition(partitionId).freezeLiveTail(freeze);
479
+ },
480
+ replaceLiveTail(partitionId, segment) {
481
+ validatePartitionId(partitionId);
482
+ asCompositePartition(partitionId).replaceLiveTail(segment);
483
+ registerSegmentDocuments(partitionId, segment);
484
+ },
485
+ deserializePartition(partitionId, data) {
486
+ validatePartitionId(partitionId);
487
+ writeLog.partitionsReplaced();
488
+ partitions[partitionId].deserialize(data, config.schema);
489
+ preparePartialWords(partitionId);
490
+ rebuildDocPartitionMapForPartition(partitionId);
491
+ },
492
+ getAggregateStats() {
493
+ return aggregateStatsOf(partitions);
494
+ },
495
+ estimateMemoryBytes() {
496
+ return estimateBytesOf(partitions, vecIndexes);
497
+ },
498
+ getPartitionStats() {
499
+ return partitionStatsOf(partitions);
500
+ },
501
+ getVectorIndexes() {
502
+ return vecIndexes;
503
+ },
504
+ resetVectorIndexes(newIndexes) {
505
+ vecIndexes.clear();
506
+ for (const [key, value] of newIndexes) {
507
+ vecIndexes.set(key, value);
508
+ }
509
+ }
510
+ };
511
+ return manager;
512
+ }
513
+
514
+ // src/partitioning/router.ts
515
+ function createPartitionRouter() {
516
+ function route(docId, partitionCount) {
517
+ if (partitionCount <= 0) {
518
+ throw new NarsilError(
519
+ ErrorCodes.INDEX_NOT_FOUND,
520
+ `Partition count must be greater than 0, received ${partitionCount}`,
521
+ { partitionCount }
522
+ );
523
+ }
524
+ return fnv1a(docId) % partitionCount;
525
+ }
526
+ function routeBatch(docIds, partitionCount) {
527
+ const groups = /* @__PURE__ */ new Map();
528
+ for (let i = 0; i < docIds.length; i++) {
529
+ const pid = route(docIds[i], partitionCount);
530
+ let group = groups.get(pid);
531
+ if (!group) {
532
+ group = [];
533
+ groups.set(pid, group);
534
+ }
535
+ group.push(docIds[i]);
536
+ }
537
+ return groups;
538
+ }
539
+ return { route, routeBatch };
540
+ }
541
+
542
+ // src/workers/query-actions.ts
543
+ async function runQueryAction(entry, action) {
544
+ const patternWork = joinPatternWorkMeter(action.patternWork);
545
+ const searchOptions = { ...entry.searchOptions, patternWork };
546
+ if (action.type === "query") {
547
+ const result2 = await fanOutQuery(
548
+ entry.manager,
549
+ action.params,
550
+ entry.language,
551
+ entry.config.schema,
552
+ {
553
+ scoringMode: action.params.scoring ?? entry.config.defaultScoring ?? "local",
554
+ partitionIds: action.partitionIds,
555
+ ...action.globalStats !== void 0 ? { globalStats: sanitizeGlobalStats(action.globalStats) } : {}
556
+ },
557
+ searchOptions
558
+ );
559
+ result2.patternWorkSpent = patternWork.spent();
560
+ return result2;
561
+ }
562
+ if (countsWithoutScores(action.params)) {
563
+ const count = fanOutMatchCount(entry.manager, action.params, entry.language, entry.config.schema, {
564
+ searchOptions
565
+ });
566
+ return { count };
567
+ }
568
+ const result = await fanOutQuery(
569
+ entry.manager,
570
+ action.params,
571
+ entry.language,
572
+ entry.config.schema,
573
+ { scoringMode: entry.config.defaultScoring ?? "local" },
574
+ searchOptions
575
+ );
576
+ return { count: result.totalMatched };
577
+ }
578
+
579
+ // src/core/partition/frozen/doc-table.ts
580
+ function createFrozenDocTable(idTable, lengthColumns, documents, tombstones) {
581
+ const capacity = idTable.count;
582
+ let sortedIds = null;
583
+ let sortedRevision = -1;
584
+ function storedAt(ordinal) {
585
+ const fieldLengths = {};
586
+ for (const [fieldName, column] of lengthColumns) {
587
+ const length = column[ordinal];
588
+ if (length > 0) fieldLengths[fieldName] = length;
589
+ }
590
+ return { fields: documents.docAt(ordinal), fieldLengths };
591
+ }
592
+ function liveOrdinal(docId) {
593
+ const ordinal = idTable.ordinalOf(docId);
594
+ if (ordinal < 0 || tombstones.has(ordinal)) return void 0;
595
+ return ordinal;
596
+ }
597
+ const resolver = {
598
+ toExternal(internalId) {
599
+ if (internalId < 0 || internalId >= capacity || tombstones.has(internalId)) return void 0;
600
+ return idTable.idAt(internalId);
601
+ },
602
+ toInternal(externalId) {
603
+ return liveOrdinal(externalId);
604
+ },
605
+ isLive(internalId) {
606
+ return internalId >= 0 && internalId < capacity && !tombstones.has(internalId);
607
+ }
608
+ };
609
+ return {
610
+ get(docId) {
611
+ const ordinal = liveOrdinal(docId);
612
+ if (ordinal === void 0) return void 0;
613
+ return storedAt(ordinal);
614
+ },
615
+ has(docId) {
616
+ return liveOrdinal(docId) !== void 0;
617
+ },
618
+ count() {
619
+ return capacity - tombstones.size;
620
+ },
621
+ *all() {
622
+ for (let ordinal = 0; ordinal < capacity; ordinal++) {
623
+ if (tombstones.has(ordinal)) continue;
624
+ yield [idTable.idAt(ordinal), storedAt(ordinal)];
625
+ }
626
+ },
627
+ sortedDocIds() {
628
+ if (sortedIds === null || sortedRevision !== tombstones.revision) {
629
+ sortedIds = idTable.collectSortedIds((ordinal) => tombstones.has(ordinal));
630
+ sortedRevision = tombstones.revision;
631
+ }
632
+ return sortedIds;
633
+ },
634
+ releaseSortedDocIds() {
635
+ sortedIds = null;
636
+ },
637
+ fieldLengthColumn(fieldName) {
638
+ return lengthColumns.get(fieldName) ?? null;
639
+ },
640
+ getInternalId(docId) {
641
+ return liveOrdinal(docId);
642
+ },
643
+ getExternalId(internalId) {
644
+ return resolver.toExternal(internalId);
645
+ },
646
+ *allInternalIds() {
647
+ for (let ordinal = 0; ordinal < capacity; ordinal++) {
648
+ if (!tombstones.has(ordinal)) yield ordinal;
649
+ }
650
+ },
651
+ internalIdCapacity() {
652
+ return capacity;
653
+ },
654
+ numberingGeneration() {
655
+ return 0;
656
+ },
657
+ contentBytes() {
658
+ return documents.byteLength;
659
+ },
660
+ resolver() {
661
+ return resolver;
662
+ },
663
+ fieldReader(fieldPath) {
664
+ const read = documents.fieldReader(fieldPath);
665
+ return (internalId) => resolver.isLive(internalId) ? read(internalId) : void 0;
666
+ },
667
+ projectedDocument(docId, projection) {
668
+ const ordinal = liveOrdinal(docId);
669
+ return ordinal === void 0 ? void 0 : documents.projectedAt(ordinal, projection);
670
+ }
671
+ };
672
+ }
673
+ var FORBIDDEN_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
674
+ var keyDecoder = new TextDecoder("utf-8", { fatal: true });
675
+ function headerAt(view, offset) {
676
+ if (offset >= view.byteLength) return null;
677
+ const tag = view.getUint8(offset);
678
+ if (tag <= 127 || tag >= 224) return { headerBytes: 1, bodyBytes: 0, children: 0 };
679
+ if (tag <= 143) return { headerBytes: 1, bodyBytes: 0, children: (tag & 15) * 2 };
680
+ if (tag <= 159) return { headerBytes: 1, bodyBytes: 0, children: tag & 15 };
681
+ if (tag <= 191) return { headerBytes: 1, bodyBytes: tag & 31, children: 0 };
682
+ const lengthBytes = LENGTH_BYTES_BY_TAG.get(tag);
683
+ if (lengthBytes !== void 0) {
684
+ if (offset + 1 + lengthBytes > view.byteLength) return null;
685
+ const length = lengthBytes === 1 ? view.getUint8(offset + 1) : lengthBytes === 2 ? view.getUint16(offset + 1, false) : view.getUint32(offset + 1, false);
686
+ const headerBytes = 1 + lengthBytes;
687
+ if (ARRAY_TAGS.has(tag)) return { headerBytes, bodyBytes: 0, children: length };
688
+ if (MAP_TAGS.has(tag)) return { headerBytes, bodyBytes: 0, children: length * 2 };
689
+ return { headerBytes, bodyBytes: length + (EXT_TAGS.has(tag) ? 1 : 0), children: 0 };
690
+ }
691
+ const fixedBytes = FIXED_BODY_BYTES_BY_TAG.get(tag);
692
+ return fixedBytes === void 0 ? null : { headerBytes: 1, bodyBytes: fixedBytes, children: 0 };
693
+ }
694
+ var LENGTH_BYTES_BY_TAG = /* @__PURE__ */ new Map([
695
+ [196, 1],
696
+ [197, 2],
697
+ [198, 4],
698
+ [199, 1],
699
+ [200, 2],
700
+ [201, 4],
701
+ [217, 1],
702
+ [218, 2],
703
+ [219, 4],
704
+ [220, 2],
705
+ [221, 4],
706
+ [222, 2],
707
+ [223, 4]
708
+ ]);
709
+ var ARRAY_TAGS = /* @__PURE__ */ new Set([220, 221]);
710
+ var MAP_TAGS = /* @__PURE__ */ new Set([222, 223]);
711
+ var EXT_TAGS = /* @__PURE__ */ new Set([199, 200, 201]);
712
+ var FIXED_BODY_BYTES_BY_TAG = /* @__PURE__ */ new Map([
713
+ [192, 0],
714
+ [194, 0],
715
+ [195, 0],
716
+ [202, 4],
717
+ [203, 8],
718
+ [204, 1],
719
+ [205, 2],
720
+ [206, 4],
721
+ [207, 8],
722
+ [208, 1],
723
+ [209, 2],
724
+ [210, 4],
725
+ [211, 8],
726
+ [212, 2],
727
+ [213, 3],
728
+ [214, 5],
729
+ [215, 9],
730
+ [216, 17]
731
+ ]);
732
+ function endOfValue(view, start) {
733
+ let offset = start;
734
+ let pending = 1;
735
+ while (pending > 0) {
736
+ const header = headerAt(view, offset);
737
+ if (header === null) return null;
738
+ offset += header.headerBytes + header.bodyBytes;
739
+ if (offset > view.byteLength) return null;
740
+ pending += header.children - 1;
741
+ }
742
+ return offset;
743
+ }
744
+ function stringKeyAt(bytes, view, offset) {
745
+ const header = headerAt(view, offset);
746
+ if (header === null || header.children !== 0) return null;
747
+ const tag = view.getUint8(offset);
748
+ const isString = tag >= 160 && tag <= 191 || tag === 217 || tag === 218 || tag === 219;
749
+ const end = offset + header.headerBytes + header.bodyBytes;
750
+ if (!isString || end > bytes.length) return null;
751
+ try {
752
+ return { key: keyDecoder.decode(bytes.subarray(offset + header.headerBytes, end)), end };
753
+ } catch {
754
+ return null;
755
+ }
756
+ }
757
+ function topLevelMapKeys(bytes) {
758
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
759
+ const top = headerAt(view, 0);
760
+ if (top === null || top.bodyBytes !== 0) return null;
761
+ const tag = view.getUint8(0);
762
+ if (!(tag >= 128 && tag <= 143 || MAP_TAGS.has(tag))) return null;
763
+ const keys = /* @__PURE__ */ new Set();
764
+ let offset = top.headerBytes;
765
+ for (let pair = 0; pair < top.children / 2; pair += 1) {
766
+ const key = stringKeyAt(bytes, view, offset);
767
+ if (key === null) return null;
768
+ const valueEnd = endOfValue(view, key.end);
769
+ if (valueEnd === null) return null;
770
+ keys.add(key.key);
771
+ offset = valueEnd;
772
+ }
773
+ return keys;
774
+ }
775
+ function decodeMapWithoutFields(bytes, skippedFields) {
776
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
777
+ const top = headerAt(view, 0);
778
+ if (top === null || top.children === 0 || top.bodyBytes !== 0) return null;
779
+ const tag = view.getUint8(0);
780
+ const isMap = tag >= 128 && tag <= 143 || MAP_TAGS.has(tag);
781
+ if (!isMap) return null;
782
+ const document = {};
783
+ let offset = top.headerBytes;
784
+ for (let pair = 0; pair < top.children / 2; pair += 1) {
785
+ const key = stringKeyAt(bytes, view, offset);
786
+ if (key === null || FORBIDDEN_KEYS.has(key.key)) return null;
787
+ const valueEnd = endOfValue(view, key.end);
788
+ if (valueEnd === null) return null;
789
+ if (!skippedFields.has(key.key)) {
790
+ try {
791
+ document[key.key] = decode(bytes.subarray(key.end, valueEnd));
792
+ } catch {
793
+ return null;
794
+ }
795
+ }
796
+ offset = valueEnd;
797
+ }
798
+ return offset === bytes.length ? document : null;
799
+ }
800
+
801
+ // src/serialization/msgpack-path.ts
802
+ var encoder = new TextEncoder();
803
+ var PROTO_KEY_BYTES = encoder.encode("__proto__");
804
+ var MISSING = { found: false, value: void 0 };
805
+ var pathDecoder = new Decoder();
806
+ function encodePathSegments(segments) {
807
+ if (segments.length === 0) return null;
808
+ if (segments.some((segment) => PROTOTYPE_POLLUTION_KEYS.has(segment))) return null;
809
+ return segments.map((segment) => encoder.encode(segment));
810
+ }
811
+ function isStringTag(tag) {
812
+ return tag >= 160 && tag <= 191 || tag === 217 || tag === 218 || tag === 219;
813
+ }
814
+ function isMapTag(tag) {
815
+ return tag >= 128 && tag <= 143 || tag === 222 || tag === 223;
816
+ }
817
+ function isArrayTag(tag) {
818
+ return tag >= 144 && tag <= 159 || tag === 220 || tag === 221;
819
+ }
820
+ function isScalarTag(tag) {
821
+ if (tag <= 127 || tag >= 224 || isStringTag(tag)) return true;
822
+ return tag === 192 || tag === 194 || tag === 195 || tag >= 202 && tag <= 211;
823
+ }
824
+ function sameBytes(bytes, start, end, target) {
825
+ if (end - start !== target.length) return false;
826
+ for (let at = 0; at < target.length; at++) {
827
+ if (bytes[start + at] !== target[at]) return false;
828
+ }
829
+ return true;
830
+ }
831
+ function keyRangeAt(view, offset) {
832
+ if (!isStringTag(view.getUint8(offset))) return null;
833
+ const header = headerAt(view, offset);
834
+ if (header === null) return null;
835
+ const start = offset + header.headerBytes;
836
+ return { start, end: start + header.bodyBytes };
837
+ }
838
+ function readEncodedPath(bytes, segments, arrays) {
839
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
840
+ let start = 0;
841
+ let end = bytes.length;
842
+ for (let depth = 0; depth < segments.length; depth++) {
843
+ if (start >= end) return null;
844
+ const tag = view.getUint8(start);
845
+ if (!isMapTag(tag)) {
846
+ if (depth === 0) return null;
847
+ if (isArrayTag(tag)) return arrays === "missing" ? MISSING : null;
848
+ return isScalarTag(tag) ? MISSING : null;
849
+ }
850
+ const header = headerAt(view, start);
851
+ if (header === null) return null;
852
+ let offset = start + header.headerBytes;
853
+ let valueStart = -1;
854
+ let valueEnd = -1;
855
+ for (let pair = 0; pair < header.children / 2; pair++) {
856
+ if (offset >= end) return null;
857
+ const key = keyRangeAt(view, offset);
858
+ if (key === null || key.end > end) return null;
859
+ if (sameBytes(bytes, key.start, key.end, PROTO_KEY_BYTES)) return null;
860
+ const next = endOfValue(view, key.end);
861
+ if (next === null || next > end) return null;
862
+ if (sameBytes(bytes, key.start, key.end, segments[depth])) {
863
+ valueStart = key.end;
864
+ valueEnd = next;
865
+ }
866
+ offset = next;
867
+ }
868
+ if (offset !== end) return null;
869
+ if (valueStart < 0) return MISSING;
870
+ start = valueStart;
871
+ end = valueEnd;
872
+ }
873
+ try {
874
+ return { found: true, value: pathDecoder.decode(bytes.subarray(start, end)) };
875
+ } catch {
876
+ return null;
877
+ }
878
+ }
879
+
880
+ // src/core/partition/frozen/document-source.ts
881
+ var encodedProjectionPaths = /* @__PURE__ */ new WeakMap();
882
+ function encodedProjectionPathOf(path) {
883
+ let segments = encodedProjectionPaths.get(path);
884
+ if (segments === void 0) {
885
+ segments = encodePathSegments(path);
886
+ encodedProjectionPaths.set(path, segments);
887
+ }
888
+ return segments;
889
+ }
890
+ function sharesPrefix(left, right) {
891
+ const shared = Math.min(left.length, right.length);
892
+ for (let at = 0; at < shared; at++) {
893
+ if (left[at] !== right[at]) return false;
894
+ }
895
+ return true;
896
+ }
897
+ function removeFieldAtPath(document, fieldPath) {
898
+ const segments = fieldPath.split(".");
899
+ let current = document;
900
+ for (let i = 0; i < segments.length - 1; i += 1) {
901
+ const next = current[segments[i]];
902
+ if (next === null || typeof next !== "object" || Array.isArray(next)) return;
903
+ current = next;
904
+ }
905
+ delete current[segments[segments.length - 1]];
906
+ }
907
+ function decodeStoredDocument(bytes, discardedFields) {
908
+ let decoded;
909
+ try {
910
+ decoded = decode(bytes);
911
+ } catch (error) {
912
+ throw new NarsilError(
913
+ ErrorCodes.PERSISTENCE_LOAD_FAILED,
914
+ `A stored document is not MessagePack: ${error instanceof Error ? error.message : String(error)}`
915
+ );
916
+ }
917
+ if (decoded === null || typeof decoded !== "object" || Array.isArray(decoded) || decoded instanceof Uint8Array) {
918
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "A stored document decodes to something other than a map");
919
+ }
920
+ const document = decoded;
921
+ if (discardedFields !== void 0) {
922
+ for (const fieldPath of discardedFields) removeFieldAtPath(document, fieldPath);
923
+ }
924
+ return document;
925
+ }
926
+ function* storedDocumentsOf(data) {
927
+ const count = data.offsets.length - 1;
928
+ for (let ordinal = 0; ordinal < count; ordinal++) {
929
+ yield decodeStoredDocument(
930
+ data.blob.subarray(data.offsets[ordinal], data.offsets[ordinal + 1]),
931
+ data.discardedFields
932
+ );
933
+ }
934
+ }
935
+ function decodeEveryStoredDocument(data) {
936
+ const count = data.offsets.length - 1;
937
+ for (let ordinal = 0; ordinal < count; ordinal++) {
938
+ decodeStoredDocument(data.blob.subarray(data.offsets[ordinal], data.offsets[ordinal + 1]), data.discardedFields);
939
+ }
940
+ }
941
+ function encodeDocumentTableData(documents) {
942
+ const encoded = new Array(documents.length);
943
+ let blobLength = 0;
944
+ for (let ordinal = 0; ordinal < documents.length; ordinal++) {
945
+ const bytes = encodeDocument(documents[ordinal]);
946
+ encoded[ordinal] = bytes;
947
+ blobLength += bytes.length;
948
+ }
949
+ const blob = new Uint8Array(blobLength);
950
+ const offsets = new Uint32Array(documents.length + 1);
951
+ let cursor = 0;
952
+ for (let ordinal = 0; ordinal < documents.length; ordinal++) {
953
+ blob.set(encoded[ordinal], cursor);
954
+ cursor += encoded[ordinal].length;
955
+ offsets[ordinal + 1] = cursor;
956
+ }
957
+ return { blob, offsets };
958
+ }
959
+ function wrapEncodedDocumentTable(data) {
960
+ const { blob, offsets } = data;
961
+ const count = offsets.length - 1;
962
+ const discarded = (data.discardedFields ?? []).map((fieldPath) => fieldPath.split("."));
963
+ const touchesDiscarded = (path) => discarded.some((discardedPath) => sharesPrefix(discardedPath, path));
964
+ const bytesAt = (ordinal) => blob.subarray(offsets[ordinal], offsets[ordinal + 1]);
965
+ const docAt = (ordinal) => documentWithOwnBuffers(decodeStoredDocument(bytesAt(ordinal), data.discardedFields));
966
+ function projectedPathAt(ordinal, path) {
967
+ const segments = encodedProjectionPathOf(path);
968
+ if (segments === null) return { found: false, value: void 0 };
969
+ const read = touchesDiscarded(path) ? null : readEncodedPath(bytesAt(ordinal), segments, "missing");
970
+ if (read === null) return readProjectedPath(docAt(ordinal), path);
971
+ return read.found ? { found: true, value: valueWithOwnBuffers(read.value) } : read;
972
+ }
973
+ return {
974
+ count,
975
+ byteLength: blob.byteLength + offsets.byteLength,
976
+ docAt,
977
+ readAt(ordinal) {
978
+ return decodeStoredDocument(bytesAt(ordinal), data.discardedFields);
979
+ },
980
+ fieldReader(fieldPath) {
981
+ const path = fieldPath.split(".");
982
+ const segments = encodePathSegments(path);
983
+ if (segments === null) return () => void 0;
984
+ const decodesDocument = touchesDiscarded(path);
985
+ return (ordinal) => {
986
+ const read = decodesDocument ? null : readEncodedPath(bytesAt(ordinal), segments, "decode-document");
987
+ if (read !== null) return valueWithOwnBuffers(read.value);
988
+ return getNestedValue(docAt(ordinal), fieldPath);
989
+ };
990
+ },
991
+ projectedAt(ordinal, projection) {
992
+ if (projection === void 0 || projection.kind === "full") return docAt(ordinal);
993
+ if (projection.kind === "none") return {};
994
+ if (projection.include === null) return cloneProjected(docAt(ordinal), projection);
995
+ return projectIncludedPaths(projection.include, projection.exclude, (path) => projectedPathAt(ordinal, path));
996
+ }
997
+ };
998
+ }
999
+ function wrapDocumentArray(documents) {
1000
+ let residentBytes = 0;
1001
+ for (const document of documents) {
1002
+ residentBytes += documentValueBytes(document);
1003
+ }
1004
+ return {
1005
+ count: documents.length,
1006
+ byteLength: residentBytes,
1007
+ docAt(ordinal) {
1008
+ return documents[ordinal];
1009
+ },
1010
+ readAt(ordinal) {
1011
+ return documents[ordinal];
1012
+ },
1013
+ fieldReader(fieldPath) {
1014
+ return (ordinal) => getNestedValue(documents[ordinal], fieldPath);
1015
+ },
1016
+ projectedAt(ordinal, projection) {
1017
+ return cloneProjected(documents[ordinal], projection);
1018
+ }
1019
+ };
1020
+ }
1021
+
1022
+ // src/core/partition/frozen/external-ids.ts
1023
+ var decoder = new TextDecoder();
1024
+ function encodeExternalIdTableData(docIds) {
1025
+ const { blob, offsets } = encodeStringBlob(docIds);
1026
+ return { blob, offsets, sortedOrdinals: Uint32Array.from(codePointOrder(docIds)) };
1027
+ }
1028
+ function compareBytes(blob, start, end, query) {
1029
+ const length = end - start;
1030
+ const shared = length < query.length ? length : query.length;
1031
+ for (let i = 0; i < shared; i++) {
1032
+ const diff = blob[start + i] - query[i];
1033
+ if (diff !== 0) return diff;
1034
+ }
1035
+ return length - query.length;
1036
+ }
1037
+ function wrapExternalIdTable(data) {
1038
+ const { blob, offsets, sortedOrdinals } = data;
1039
+ const count = sortedOrdinals.length;
1040
+ const decoded = new Array(count);
1041
+ const decodedOrdinals = /* @__PURE__ */ new Map();
1042
+ function idAt(ordinal) {
1043
+ let id = decoded[ordinal];
1044
+ if (id === void 0) {
1045
+ id = decoder.decode(blob.subarray(offsets[ordinal], offsets[ordinal + 1]));
1046
+ decoded[ordinal] = id;
1047
+ decodedOrdinals.set(id, ordinal);
1048
+ }
1049
+ return id;
1050
+ }
1051
+ return {
1052
+ count,
1053
+ bytes: blob.byteLength + offsets.byteLength + sortedOrdinals.byteLength,
1054
+ idAt,
1055
+ ordinalOf(docId) {
1056
+ const known = decodedOrdinals.get(docId);
1057
+ if (known !== void 0) return known;
1058
+ const query = encodeLookupKey(docId);
1059
+ let lo = 0;
1060
+ let hi = count;
1061
+ while (lo < hi) {
1062
+ const mid = lo + hi >>> 1;
1063
+ const ordinal2 = sortedOrdinals[mid];
1064
+ if (compareBytes(blob, offsets[ordinal2], offsets[ordinal2 + 1], query) < 0) lo = mid + 1;
1065
+ else hi = mid;
1066
+ }
1067
+ if (lo >= count) return -1;
1068
+ const ordinal = sortedOrdinals[lo];
1069
+ return compareBytes(blob, offsets[ordinal], offsets[ordinal + 1], query) === 0 ? ordinal : -1;
1070
+ },
1071
+ collectSortedIds(excluded) {
1072
+ const ids = [];
1073
+ for (let position = 0; position < count; position++) {
1074
+ const ordinal = sortedOrdinals[position];
1075
+ if (excluded(ordinal)) continue;
1076
+ ids.push(idAt(ordinal));
1077
+ }
1078
+ return ids;
1079
+ }
1080
+ };
1081
+ }
1082
+ function buildExternalIdTable(docIds) {
1083
+ const ordinals = /* @__PURE__ */ new Map();
1084
+ let characters = 0;
1085
+ for (let ordinal = 0; ordinal < docIds.length; ordinal++) {
1086
+ ordinals.set(docIds[ordinal], ordinal);
1087
+ characters += docIds[ordinal].length;
1088
+ }
1089
+ let sortedIds = null;
1090
+ return {
1091
+ count: docIds.length,
1092
+ bytes: characters,
1093
+ idAt(ordinal) {
1094
+ return docIds[ordinal];
1095
+ },
1096
+ ordinalOf(docId) {
1097
+ return ordinals.get(docId) ?? -1;
1098
+ },
1099
+ collectSortedIds(excluded) {
1100
+ if (sortedIds === null) {
1101
+ const ids = [...docIds];
1102
+ ids.sort(compareCodePoints);
1103
+ sortedIds = ids;
1104
+ }
1105
+ const kept = [];
1106
+ for (const docId of sortedIds) {
1107
+ const ordinal = ordinals.get(docId);
1108
+ if (ordinal === void 0 || excluded(ordinal)) continue;
1109
+ kept.push(docId);
1110
+ }
1111
+ return kept;
1112
+ }
1113
+ };
1114
+ }
1115
+
1116
+ // src/core/partition/frozen/field-indexes.ts
1117
+ function lowerBound(values, value) {
1118
+ let lo = 0;
1119
+ let hi = values.length;
1120
+ while (lo < hi) {
1121
+ const mid = lo + hi >>> 1;
1122
+ if (values[mid] < value) lo = mid + 1;
1123
+ else hi = mid;
1124
+ }
1125
+ return lo;
1126
+ }
1127
+ function upperBound(values, value) {
1128
+ let lo = 0;
1129
+ let hi = values.length;
1130
+ while (lo < hi) {
1131
+ const mid = lo + hi >>> 1;
1132
+ if (values[mid] <= value) lo = mid + 1;
1133
+ else hi = mid;
1134
+ }
1135
+ return lo;
1136
+ }
1137
+ function setOfRange(docIds, from, to) {
1138
+ const result = /* @__PURE__ */ new Set();
1139
+ for (let i = from; i < to; i++) result.add(docIds[i]);
1140
+ return result;
1141
+ }
1142
+ function setOutsideRange(docIds, from, to) {
1143
+ const result = /* @__PURE__ */ new Set();
1144
+ for (let i = 0; i < from; i++) result.add(docIds[i]);
1145
+ for (let i = to; i < docIds.length; i++) result.add(docIds[i]);
1146
+ return result;
1147
+ }
1148
+ function bitsetOfRange(docIds, from, to, capacity) {
1149
+ const bits = createBitSet(capacity);
1150
+ for (let i = from; i < to; i++) bitsetSet(bits, docIds[i]);
1151
+ return bits;
1152
+ }
1153
+ function createFrozenNumericReader(entry) {
1154
+ const { docIds, values } = entry;
1155
+ return {
1156
+ serialize: () => Array.from(docIds, (docId, i) => ({ docId, value: values[i] })),
1157
+ queryEq: (value) => setOfRange(docIds, lowerBound(values, value), upperBound(values, value)),
1158
+ queryNe: (value) => setOutsideRange(docIds, lowerBound(values, value), upperBound(values, value)),
1159
+ queryGt: (value) => setOfRange(docIds, upperBound(values, value), docIds.length),
1160
+ queryGte: (value) => setOfRange(docIds, lowerBound(values, value), docIds.length),
1161
+ queryLt: (value) => setOfRange(docIds, 0, lowerBound(values, value)),
1162
+ queryLte: (value) => setOfRange(docIds, 0, upperBound(values, value)),
1163
+ queryBetween: (min, max) => setOfRange(docIds, lowerBound(values, min), upperBound(values, max)),
1164
+ getAllDocIds: () => setOfRange(docIds, 0, docIds.length),
1165
+ queryEqBitset: (value, capacity) => bitsetOfRange(docIds, lowerBound(values, value), upperBound(values, value), capacity),
1166
+ queryGtBitset: (value, capacity) => bitsetOfRange(docIds, upperBound(values, value), docIds.length, capacity),
1167
+ queryGteBitset: (value, capacity) => bitsetOfRange(docIds, lowerBound(values, value), docIds.length, capacity),
1168
+ queryLtBitset: (value, capacity) => bitsetOfRange(docIds, 0, lowerBound(values, value), capacity),
1169
+ queryLteBitset: (value, capacity) => bitsetOfRange(docIds, 0, upperBound(values, value), capacity),
1170
+ queryBetweenBitset: (min, max, capacity) => bitsetOfRange(docIds, lowerBound(values, min), upperBound(values, max), capacity),
1171
+ getAllDocIdsBitset: (capacity) => bitsetOfRange(docIds, 0, docIds.length, capacity),
1172
+ count: () => docIds.length,
1173
+ facetValueCounts: (matched) => {
1174
+ const counts = /* @__PURE__ */ new Map();
1175
+ let i = 0;
1176
+ while (i < values.length) {
1177
+ const value = values[i];
1178
+ let runEnd = i + 1;
1179
+ while (runEnd < values.length && values[runEnd] === value) runEnd++;
1180
+ if (runEnd - i === 1) {
1181
+ if (bitsetHas(matched, docIds[i])) counts.set(value, 1);
1182
+ } else {
1183
+ const seen = /* @__PURE__ */ new Set();
1184
+ for (let j = i; j < runEnd; j++) {
1185
+ if (bitsetHas(matched, docIds[j])) seen.add(docIds[j]);
1186
+ }
1187
+ if (seen.size > 0) counts.set(value, seen.size);
1188
+ }
1189
+ i = runEnd;
1190
+ }
1191
+ return counts;
1192
+ },
1193
+ facetRangeCount: (from, to, matched) => {
1194
+ const start = lowerBound(values, from);
1195
+ const end = lowerBound(values, to);
1196
+ const seen = /* @__PURE__ */ new Set();
1197
+ for (let i = start; i < end; i++) {
1198
+ if (bitsetHas(matched, docIds[i])) seen.add(docIds[i]);
1199
+ }
1200
+ return seen.size;
1201
+ }
1202
+ };
1203
+ }
1204
+ function createFrozenBooleanReader(entry) {
1205
+ const { trueDocs, falseDocs } = entry;
1206
+ return {
1207
+ serialize: () => ({ trueDocs: [...trueDocs], falseDocs: [...falseDocs] }),
1208
+ queryEq: (value) => setOfRange(value ? trueDocs : falseDocs, 0, value ? trueDocs.length : falseDocs.length),
1209
+ queryNe: (value) => setOfRange(value ? falseDocs : trueDocs, 0, value ? falseDocs.length : trueDocs.length),
1210
+ getAllDocIds: () => {
1211
+ const all = setOfRange(trueDocs, 0, trueDocs.length);
1212
+ for (let i = 0; i < falseDocs.length; i++) all.add(falseDocs[i]);
1213
+ return all;
1214
+ },
1215
+ queryEqBitset: (value, capacity) => bitsetOfRange(value ? trueDocs : falseDocs, 0, value ? trueDocs.length : falseDocs.length, capacity),
1216
+ getAllDocIdsBitset: (capacity) => {
1217
+ const bits = bitsetOfRange(trueDocs, 0, trueDocs.length, capacity);
1218
+ for (let i = 0; i < falseDocs.length; i++) bitsetSet(bits, falseDocs[i]);
1219
+ return bits;
1220
+ },
1221
+ count: () => trueDocs.length + falseDocs.length,
1222
+ facetCounts: (matched) => {
1223
+ let trueCount = 0;
1224
+ let falseCount = 0;
1225
+ for (let i = 0; i < trueDocs.length; i++) {
1226
+ if (bitsetHas(matched, trueDocs[i])) trueCount++;
1227
+ }
1228
+ for (let i = 0; i < falseDocs.length; i++) {
1229
+ if (bitsetHas(matched, falseDocs[i])) falseCount++;
1230
+ }
1231
+ return { trueCount, falseCount };
1232
+ }
1233
+ };
1234
+ }
1235
+ function createFrozenEnumReader(entry) {
1236
+ const { values, offsets, docIds } = entry;
1237
+ const rangeByValue = /* @__PURE__ */ new Map();
1238
+ for (let i = 0; i < values.length; i++) {
1239
+ rangeByValue.set(values[i], { from: offsets[i], to: offsets[i + 1] });
1240
+ }
1241
+ function rangeOf(value) {
1242
+ return rangeByValue.get(value) ?? { from: 0, to: 0 };
1243
+ }
1244
+ let affixes = null;
1245
+ function affixesOf() {
1246
+ affixes ??= new ValueAffixes(() => true, values);
1247
+ return affixes;
1248
+ }
1249
+ function bitsetOfValues(matching, capacity) {
1250
+ const bits = createBitSet(capacity);
1251
+ for (const value of matching) {
1252
+ const { from, to } = rangeOf(value);
1253
+ for (let i = from; i < to; i++) bitsetSet(bits, docIds[i]);
1254
+ }
1255
+ return bits;
1256
+ }
1257
+ return {
1258
+ queryPrefixBitset: (prefix, capacity) => bitsetOfValues(affixesOf().withPrefix(prefix), capacity),
1259
+ querySuffixBitset: (suffix, capacity) => bitsetOfValues(affixesOf().withSuffix(suffix), capacity),
1260
+ serialize: () => {
1261
+ const byValue = /* @__PURE__ */ Object.create(null);
1262
+ for (let i = 0; i < values.length; i++) {
1263
+ byValue[values[i]] = [...docIds.subarray(offsets[i], offsets[i + 1])];
1264
+ }
1265
+ return byValue;
1266
+ },
1267
+ queryEq: (value) => {
1268
+ const { from, to } = rangeOf(value);
1269
+ return setOfRange(docIds, from, to);
1270
+ },
1271
+ queryNe: (value) => {
1272
+ const { from, to } = rangeOf(value);
1273
+ return setOutsideRange(docIds, from, to);
1274
+ },
1275
+ queryIn: (requested) => {
1276
+ const result = /* @__PURE__ */ new Set();
1277
+ for (const value of requested) {
1278
+ const { from, to } = rangeOf(value);
1279
+ for (let i = from; i < to; i++) result.add(docIds[i]);
1280
+ }
1281
+ return result;
1282
+ },
1283
+ queryNin: (excluded) => {
1284
+ const excludedSet = /* @__PURE__ */ new Set();
1285
+ for (const value of excluded) {
1286
+ const { from, to } = rangeOf(value);
1287
+ for (let i = from; i < to; i++) excludedSet.add(docIds[i]);
1288
+ }
1289
+ const result = /* @__PURE__ */ new Set();
1290
+ for (let i = 0; i < docIds.length; i++) {
1291
+ if (!excludedSet.has(docIds[i])) result.add(docIds[i]);
1292
+ }
1293
+ return result;
1294
+ },
1295
+ getAllDocIds: () => setOfRange(docIds, 0, docIds.length),
1296
+ queryEqBitset: (value, capacity) => {
1297
+ const { from, to } = rangeOf(value);
1298
+ return bitsetOfRange(docIds, from, to, capacity);
1299
+ },
1300
+ queryInBitset: (requested, capacity) => {
1301
+ const bits = createBitSet(capacity);
1302
+ for (const value of requested) {
1303
+ const { from, to } = rangeOf(value);
1304
+ for (let i = from; i < to; i++) bitsetSet(bits, docIds[i]);
1305
+ }
1306
+ return bits;
1307
+ },
1308
+ getAllDocIdsBitset: (capacity) => bitsetOfRange(docIds, 0, docIds.length, capacity),
1309
+ count: () => docIds.length,
1310
+ facetCounts: (matched) => {
1311
+ const counts = /* @__PURE__ */ new Map();
1312
+ for (let i = 0; i < values.length; i++) {
1313
+ let count = 0;
1314
+ for (let j = offsets[i]; j < offsets[i + 1]; j++) {
1315
+ if (bitsetHas(matched, docIds[j])) count++;
1316
+ }
1317
+ if (count > 0) counts.set(values[i], count);
1318
+ }
1319
+ return counts;
1320
+ }
1321
+ };
1322
+ }
1323
+
1324
+ // src/core/partition/frozen/inverted-reader.ts
1325
+ function createFrozenInvertedReader(table, views, counts) {
1326
+ function viewAtSorted(sortedIndex) {
1327
+ return views.viewAt(table.payloadSlot(sortedIndex), sortedIndex);
1328
+ }
1329
+ function liveAt(token) {
1330
+ const at = table.find(token);
1331
+ return at < 0 || counts.documentFrequency(at) === 0 ? -1 : at;
1332
+ }
1333
+ function candidateRange(prefix) {
1334
+ if (prefix.length === 0) return { start: 0, end: table.size };
1335
+ return table.firstCharRange(prefix);
1336
+ }
1337
+ return {
1338
+ lookup(token) {
1339
+ const at = liveAt(token);
1340
+ if (at < 0) return void 0;
1341
+ return viewAtSorted(at);
1342
+ },
1343
+ lookupReadOnce(token) {
1344
+ const at = liveAt(token);
1345
+ if (at < 0) return void 0;
1346
+ return views.viewReadOnce(table.payloadSlot(at), at);
1347
+ },
1348
+ documentFrequency(token) {
1349
+ const at = table.find(token);
1350
+ return at < 0 ? 0 : counts.documentFrequency(at);
1351
+ },
1352
+ fuzzyLookup(token, tolerance, prefixLength) {
1353
+ if (tolerance === 0) {
1354
+ const at = liveAt(token);
1355
+ return at < 0 ? [] : [{ token, postingList: viewAtSorted(at) }];
1356
+ }
1357
+ const prefix = fuzzyPrefixOf(token, prefixLength);
1358
+ const range = candidateRange(prefix);
1359
+ const requiredPrefix = prefix.length > 1 ? prefix : "";
1360
+ const results = [];
1361
+ for (let at = range.start; at < range.end; at++) {
1362
+ const candidate = table.tokenAt(at);
1363
+ if (!candidate.startsWith(requiredPrefix)) continue;
1364
+ const { withinTolerance } = boundedLevenshtein(token, candidate, tolerance);
1365
+ if (withinTolerance && counts.documentFrequency(at) > 0) {
1366
+ results.push({ token: candidate, postingList: viewAtSorted(at) });
1367
+ }
1368
+ }
1369
+ return results;
1370
+ },
1371
+ prefixSearch(prefix, limit) {
1372
+ if (prefix.length === 0 || limit <= 0) return [];
1373
+ const range = table.prefixRange(prefix);
1374
+ const results = [];
1375
+ for (let at = range.start; at < range.end; at++) {
1376
+ const documentFrequency = counts.documentFrequency(at);
1377
+ if (documentFrequency > 0) results.push({ term: table.tokenAt(at), documentFrequency });
1378
+ }
1379
+ results.sort((a, b) => b.documentFrequency - a.documentFrequency || compareCodePoints(a.term, b.term));
1380
+ if (results.length > limit) results.length = limit;
1381
+ return results;
1382
+ },
1383
+ has(token) {
1384
+ return liveAt(token) >= 0;
1385
+ },
1386
+ *tokens() {
1387
+ for (let at = 0; at < table.size; at++) {
1388
+ yield table.tokenAt(at);
1389
+ }
1390
+ },
1391
+ size() {
1392
+ return table.size;
1393
+ }
1394
+ };
1395
+ }
1396
+ function processLimitBytes() {
1397
+ const hostBytes = totalmem();
1398
+ const constrained = typeof process.constrainedMemory === "function" ? process.constrainedMemory() : 0;
1399
+ return Number.isFinite(constrained) && constrained > 0 && constrained < hostBytes ? constrained : hostBytes;
1400
+ }
1401
+ function readResidentMemory() {
1402
+ try {
1403
+ const residentBytes = process.memoryUsage.rss();
1404
+ const limitBytes = processLimitBytes();
1405
+ if (!Number.isFinite(residentBytes) || !Number.isFinite(limitBytes) || limitBytes <= 0) return null;
1406
+ return { residentBytes, limitBytes };
1407
+ } catch {
1408
+ return null;
1409
+ }
1410
+ }
1411
+
1412
+ // src/core/partition/frozen/live-counts.ts
1413
+ var UINT32_BYTES = 4;
1414
+ var FLOAT64_BYTES = 8;
1415
+ function removalIndexBytes(columns, terms) {
1416
+ const documentWords = 2 * (columns.documentCount + 1);
1417
+ return (columns.postingDocIds.length + documentWords + 2 * terms) * UINT32_BYTES + terms * FLOAT64_BYTES;
1418
+ }
1419
+ function processHasRoomFor(bytes) {
1420
+ const memory = readResidentMemory();
1421
+ if (memory === null) return true;
1422
+ return memory.residentBytes + bytes <= FROZEN_REMOVAL_INDEX_MAX_PROCESS_FRACTION * memory.limitBytes;
1423
+ }
1424
+ function buildRemovalIndex(columns, table) {
1425
+ const { documentCount, postingDocIds } = columns;
1426
+ const starts = new Uint32Array(documentCount + 1);
1427
+ for (let row = 0; row < postingDocIds.length; row++) starts[postingDocIds[row] + 1]++;
1428
+ for (let ordinal = 0; ordinal < documentCount; ordinal++) starts[ordinal + 1] += starts[ordinal];
1429
+ const cursors = starts.slice(0, documentCount);
1430
+ const rows = new Uint32Array(postingDocIds.length);
1431
+ for (let row = 0; row < postingDocIds.length; row++) rows[cursors[postingDocIds[row]]++] = row;
1432
+ const termOfSlot = new Uint32Array(table.size);
1433
+ for (let sortedIndex = 0; sortedIndex < table.size; sortedIndex++) {
1434
+ termOfSlot[table.payloadSlot(sortedIndex)] = sortedIndex;
1435
+ }
1436
+ return {
1437
+ starts,
1438
+ rows,
1439
+ termOfSlot,
1440
+ documents: new Uint32Array(table.size),
1441
+ occurrences: new Float64Array(table.size)
1442
+ };
1443
+ }
1444
+ function tryBuildRemovalIndex(columns, table) {
1445
+ if (!processHasRoomFor(removalIndexBytes(columns, table.size))) return null;
1446
+ try {
1447
+ return buildRemovalIndex(columns, table);
1448
+ } catch (error) {
1449
+ if (error instanceof RangeError) return null;
1450
+ throw error;
1451
+ }
1452
+ }
1453
+ function slotOfRow(postingOffsets, slots, row) {
1454
+ let low = 0;
1455
+ let high = slots;
1456
+ while (low < high) {
1457
+ const middle = low + high >>> 1;
1458
+ if (postingOffsets[middle + 1] <= row) low = middle + 1;
1459
+ else high = middle;
1460
+ }
1461
+ return low;
1462
+ }
1463
+ function createFrozenLiveCounts(columns, table, tombstones, storedFrequencies) {
1464
+ const { postingOffsets, postingDocIds, postingFrequencies } = columns;
1465
+ let index = null;
1466
+ let lengths = null;
1467
+ let frequencies = null;
1468
+ let frequenciesRevision = -1;
1469
+ function countRemoval(built, ordinal) {
1470
+ const { starts, rows, termOfSlot, documents, occurrences } = built;
1471
+ let previousSlot = -1;
1472
+ for (let at = starts[ordinal]; at < starts[ordinal + 1]; at++) {
1473
+ const row = rows[at];
1474
+ const slot = slotOfRow(postingOffsets, table.size, row);
1475
+ const sortedIndex = termOfSlot[slot];
1476
+ if (slot !== previousSlot) documents[sortedIndex]++;
1477
+ occurrences[sortedIndex] += postingFrequencies[row];
1478
+ previousSlot = slot;
1479
+ }
1480
+ }
1481
+ function removedDocumentsOf(sortedIndex) {
1482
+ const slot = table.payloadSlot(sortedIndex);
1483
+ const end = postingOffsets[slot + 1];
1484
+ let removed = 0;
1485
+ let previous = -1;
1486
+ let ordered = true;
1487
+ for (let row = postingOffsets[slot]; row < end; row++) {
1488
+ const ordinal = postingDocIds[row];
1489
+ if (ordinal < previous) ordered = false;
1490
+ if (ordinal !== previous && tombstones.has(ordinal)) removed++;
1491
+ previous = ordinal;
1492
+ }
1493
+ if (ordered) return removed;
1494
+ const seen = /* @__PURE__ */ new Set();
1495
+ for (let row = postingOffsets[slot]; row < end; row++) {
1496
+ const ordinal = postingDocIds[row];
1497
+ if (tombstones.has(ordinal)) seen.add(ordinal);
1498
+ }
1499
+ return seen.size;
1500
+ }
1501
+ function removedOccurrencesOf(sortedIndex) {
1502
+ const slot = table.payloadSlot(sortedIndex);
1503
+ const end = postingOffsets[slot + 1];
1504
+ let removed = 0;
1505
+ for (let row = postingOffsets[slot]; row < end; row++) {
1506
+ if (tombstones.has(postingDocIds[row])) removed += postingFrequencies[row];
1507
+ }
1508
+ return removed;
1509
+ }
1510
+ function documentFrequency(sortedIndex) {
1511
+ const stored = table.documentFrequencyAt(sortedIndex);
1512
+ if (tombstones.size === 0) return stored;
1513
+ if (index !== null) return stored - index.documents[sortedIndex];
1514
+ return stored - removedDocumentsOf(sortedIndex);
1515
+ }
1516
+ return {
1517
+ totalDocuments() {
1518
+ return columns.documentCount - tombstones.size;
1519
+ },
1520
+ totalFieldLengths() {
1521
+ return lengths ?? columns.totalFieldLengths;
1522
+ },
1523
+ documentFrequency,
1524
+ removedOccurrences(sortedIndex) {
1525
+ if (tombstones.size === 0) return 0;
1526
+ if (index !== null) return index.occurrences[sortedIndex];
1527
+ return removedOccurrencesOf(sortedIndex);
1528
+ },
1529
+ docFrequencies() {
1530
+ if (tombstones.size === 0) return storedFrequencies();
1531
+ if (frequencies !== null && frequenciesRevision === tombstones.revision) return frequencies;
1532
+ const live = /* @__PURE__ */ Object.create(null);
1533
+ for (let at = 0; at < table.size; at++) {
1534
+ const documents = documentFrequency(at);
1535
+ if (documents > 0) live[table.tokenAt(at)] = documents;
1536
+ }
1537
+ frequencies = live;
1538
+ frequenciesRevision = tombstones.revision;
1539
+ return frequencies;
1540
+ },
1541
+ subtractRemoved(ordinal) {
1542
+ lengths ??= { ...columns.totalFieldLengths };
1543
+ for (let field = 0; field < columns.fieldLengthNames.length; field++) {
1544
+ lengths[columns.fieldLengthNames[field]] -= columns.fieldLengthColumns[field][ordinal];
1545
+ }
1546
+ if (index !== null) {
1547
+ countRemoval(index, ordinal);
1548
+ return;
1549
+ }
1550
+ const built = tryBuildRemovalIndex(columns, table);
1551
+ if (built === null) return;
1552
+ for (const removed of tombstones.values()) countRemoval(built, removed);
1553
+ index = built;
1554
+ },
1555
+ bytes() {
1556
+ if (index === null) return 0;
1557
+ return index.starts.byteLength + index.rows.byteLength + index.termOfSlot.byteLength + index.documents.byteLength + index.occurrences.byteLength;
1558
+ }
1559
+ };
1560
+ }
1561
+
1562
+ // src/core/partition/frozen/posting-views.ts
1563
+ function isOrdered(docIds) {
1564
+ for (let i = 1; i < docIds.length; i++) {
1565
+ if (docIds[i] < docIds[i - 1]) return false;
1566
+ }
1567
+ return true;
1568
+ }
1569
+ function sumFrequencies(frequencies) {
1570
+ let total = 0;
1571
+ for (let i = 0; i < frequencies.length; i++) total += frequencies[i];
1572
+ return total;
1573
+ }
1574
+ function createFrozenPostingViews(payload, tombstones, counts) {
1575
+ const views = /* @__PURE__ */ new Map();
1576
+ function buildView(payloadSlot, sortedIndex, keepsPositions) {
1577
+ const start = payload.postingOffsets[payloadSlot];
1578
+ const end = payload.postingOffsets[payloadSlot + 1];
1579
+ const docIds = payload.postingDocIds.subarray(start, end);
1580
+ const termFrequencies = payload.postingFrequencies.subarray(start, end);
1581
+ const fieldNameIndices = payload.postingFieldIndices.subarray(start, end);
1582
+ const ordered = isOrdered(docIds);
1583
+ const storedTermFrequency = sumFrequencies(termFrequencies);
1584
+ const { positionOffsets, positionValues } = payload;
1585
+ let materializedPositions = null;
1586
+ return {
1587
+ length: end - start,
1588
+ docIds,
1589
+ termFrequencies,
1590
+ fieldNameIndices,
1591
+ get positions() {
1592
+ if (positionOffsets === null || positionValues === null) return null;
1593
+ if (materializedPositions !== null) return materializedPositions;
1594
+ const positions = new Array(end - start);
1595
+ for (let row = start; row < end; row++) {
1596
+ positions[row - start] = [...positionValues.subarray(positionOffsets[row], positionOffsets[row + 1])];
1597
+ }
1598
+ if (keepsPositions) materializedPositions = positions;
1599
+ return positions;
1600
+ },
1601
+ positionCountAt(row) {
1602
+ if (positionOffsets === null) return 0;
1603
+ return positionOffsets[start + row + 1] - positionOffsets[start + row];
1604
+ },
1605
+ docIdSet: {
1606
+ get size() {
1607
+ return counts.documentFrequency(sortedIndex);
1608
+ }
1609
+ },
1610
+ deletedDocs: tombstones,
1611
+ get totalTermFrequency() {
1612
+ return storedTermFrequency - counts.removedOccurrences(sortedIndex);
1613
+ },
1614
+ get structureRevision() {
1615
+ return tombstones.revision;
1616
+ },
1617
+ ordered
1618
+ };
1619
+ }
1620
+ return {
1621
+ viewAt(payloadSlot, sortedIndex) {
1622
+ let view = views.get(payloadSlot);
1623
+ if (view === void 0) {
1624
+ view = buildView(payloadSlot, sortedIndex, true);
1625
+ views.set(payloadSlot, view);
1626
+ }
1627
+ return view;
1628
+ },
1629
+ viewReadOnce(payloadSlot, sortedIndex) {
1630
+ return buildView(payloadSlot, sortedIndex, false);
1631
+ }
1632
+ };
1633
+ }
1634
+
1635
+ // src/core/partition/frozen/removal-pass.ts
1636
+ var NO_REMOVED_SPELLINGS = [];
1637
+ function createFrozenRemovalPass(documents, surfaces) {
1638
+ let bound = null;
1639
+ let pending = [];
1640
+ function heldSpellingsOf(ordinal, analysis) {
1641
+ const spellings = [];
1642
+ if (!surfaces.holdsAny()) return spellings;
1643
+ visitDocumentSurfaceSpellings(documents.documentAt(ordinal), analysis, (spelling) => {
1644
+ if (surfaces.holds(spelling)) spellings.push(spelling);
1645
+ });
1646
+ return spellings;
1647
+ }
1648
+ return {
1649
+ get settled() {
1650
+ return pending.length === 0;
1651
+ },
1652
+ bind(analysis) {
1653
+ if (bound !== null) return;
1654
+ const scoped = { ...analysis, fields: documents.textFields };
1655
+ bound = scoped;
1656
+ for (const ordinal of pending) surfaces.subtractSpellings(ordinal, heldSpellingsOf(ordinal, scoped));
1657
+ pending = [];
1658
+ },
1659
+ spellingsOf(ordinal) {
1660
+ return bound === null ? void 0 : heldSpellingsOf(ordinal, bound);
1661
+ },
1662
+ afterRemoval(ordinal, spellings) {
1663
+ if (spellings !== void 0) {
1664
+ surfaces.subtractSpellings(ordinal, spellings);
1665
+ } else if (bound !== null) {
1666
+ surfaces.subtractSpellings(ordinal, heldSpellingsOf(ordinal, bound));
1667
+ } else {
1668
+ pending.push(ordinal);
1669
+ }
1670
+ }
1671
+ };
1672
+ }
1673
+
1674
+ // src/core/partition/frozen/surface-slot.ts
1675
+ function applyDocumentSurfaces(target, document, analysis, direction) {
1676
+ if (direction === -1) {
1677
+ visitDocumentSurfaceSpellings(document, analysis, (spelling) => target.subtract(spelling, 1));
1678
+ return;
1679
+ }
1680
+ visitDocumentSpellings(document, analysis, (token, spelling) => {
1681
+ if (spelling !== token) target.add(spelling, token, 1);
1682
+ });
1683
+ }
1684
+ function addForms(target, forms) {
1685
+ for (const surface of Object.keys(forms)) {
1686
+ const value = forms[surface];
1687
+ if (Array.isArray(value)) target.add(surface, value[1], value[0]);
1688
+ }
1689
+ }
1690
+ function createFrozenSurfaceSlot(load, documents) {
1691
+ let registry = null;
1692
+ let uncounted = null;
1693
+ function loaded() {
1694
+ if (registry === null) {
1695
+ const built = createSurfaceRegistry();
1696
+ const forms = load();
1697
+ if (forms !== null) built.deserialize(forms);
1698
+ registry = built;
1699
+ }
1700
+ return registry;
1701
+ }
1702
+ function isCounted(ordinal) {
1703
+ return uncounted === null || uncounted[ordinal] === 0;
1704
+ }
1705
+ return {
1706
+ reader: {
1707
+ candidatesForPrefix: (prefix) => loaded().candidatesForPrefix(prefix),
1708
+ stemChangedTotalFor: (token) => loaded().stemChangedTotalFor(token),
1709
+ size: () => loaded().size(),
1710
+ serialize: () => loaded().serialize()
1711
+ },
1712
+ holdsAny() {
1713
+ return loaded().size() > 0;
1714
+ },
1715
+ holds(spelling) {
1716
+ return loaded().holds(spelling);
1717
+ },
1718
+ subtractSpellings(ordinal, spellings) {
1719
+ uncounted ??= new Uint8Array(documents.count);
1720
+ uncounted[ordinal] = 1;
1721
+ if (spellings.length === 0) return;
1722
+ const target = loaded();
1723
+ for (const spelling of spellings) target.subtract(spelling, 1);
1724
+ },
1725
+ addKeptForms(target, mergedOrdinals, analysis) {
1726
+ const source = loaded();
1727
+ if (analysis === null) {
1728
+ addForms(target, source.serialize());
1729
+ return;
1730
+ }
1731
+ const scoped = { ...analysis, fields: documents.textFields };
1732
+ let kept = 0;
1733
+ let corrections = 0;
1734
+ for (let ordinal = 0; ordinal < mergedOrdinals.length; ordinal++) {
1735
+ const keeps = mergedOrdinals[ordinal] >= 0;
1736
+ if (keeps) kept++;
1737
+ if (keeps !== isCounted(ordinal)) corrections++;
1738
+ }
1739
+ if (corrections > kept) {
1740
+ for (let ordinal = 0; ordinal < mergedOrdinals.length; ordinal++) {
1741
+ if (mergedOrdinals[ordinal] >= 0) applyDocumentSurfaces(target, documents.documentAt(ordinal), scoped, 1);
1742
+ }
1743
+ return;
1744
+ }
1745
+ const corrected = createSurfaceRegistry();
1746
+ addForms(corrected, source.serialize());
1747
+ for (let ordinal = 0; ordinal < mergedOrdinals.length; ordinal++) {
1748
+ const keeps = mergedOrdinals[ordinal] >= 0;
1749
+ if (keeps === isCounted(ordinal)) continue;
1750
+ applyDocumentSurfaces(corrected, documents.documentAt(ordinal), scoped, keeps ? 1 : -1);
1751
+ }
1752
+ addForms(target, corrected.serialize());
1753
+ }
1754
+ };
1755
+ }
1756
+
1757
+ // src/core/partition/frozen/surface-table.ts
1758
+ var decoder2 = new TextDecoder();
1759
+ function encodeSurfaceTable(forms) {
1760
+ const surfaceThenToken = [];
1761
+ const entryCounts = [];
1762
+ for (const surface of Object.keys(forms)) {
1763
+ const value = forms[surface];
1764
+ if (!Array.isArray(value)) continue;
1765
+ surfaceThenToken.push(surface, value[1]);
1766
+ entryCounts.push(value[0]);
1767
+ }
1768
+ const { blob, offsets } = encodeStringBlob(surfaceThenToken);
1769
+ return { blob, offsets, counts: Uint32Array.from(entryCounts) };
1770
+ }
1771
+ function decodeSurfaceTable(table) {
1772
+ const forms = /* @__PURE__ */ Object.create(null);
1773
+ for (let i = 0; i < table.counts.length; i++) {
1774
+ const surface = decoder2.decode(table.blob.subarray(table.offsets[2 * i], table.offsets[2 * i + 1]));
1775
+ const token = decoder2.decode(table.blob.subarray(table.offsets[2 * i + 1], table.offsets[2 * i + 2]));
1776
+ forms[surface] = [table.counts[i], token];
1777
+ }
1778
+ return forms;
1779
+ }
1780
+
1781
+ // src/core/partition/frozen/token-table.ts
1782
+ var encoder2 = new TextEncoder();
1783
+ var decoder3 = new TextDecoder();
1784
+ function compareBytes2(blob, start, end, query) {
1785
+ const length = end - start;
1786
+ const shared = length < query.length ? length : query.length;
1787
+ for (let i = 0; i < shared; i++) {
1788
+ const diff = blob[start + i] - query[i];
1789
+ if (diff !== 0) return diff;
1790
+ }
1791
+ return length - query.length;
1792
+ }
1793
+ function startsWithBytes(blob, start, end, prefix) {
1794
+ if (end - start < prefix.length) return false;
1795
+ for (let i = 0; i < prefix.length; i++) {
1796
+ if (blob[start + i] !== prefix[i]) return false;
1797
+ }
1798
+ return true;
1799
+ }
1800
+ function nextCodePoint(codePoint) {
1801
+ const next = codePoint + 1;
1802
+ if (next >= 55296 && next <= 57343) return 57344;
1803
+ return next;
1804
+ }
1805
+ function encodeFrozenTokenTableData(tokens, docFrequencies) {
1806
+ const size = tokens.length;
1807
+ const payloadSlots = Uint32Array.from(codePointOrder(tokens));
1808
+ const sortedTokens = new Array(size);
1809
+ const documentFrequencies = new Uint32Array(size);
1810
+ for (let i = 0; i < size; i++) {
1811
+ sortedTokens[i] = tokens[payloadSlots[i]];
1812
+ documentFrequencies[i] = docFrequencies[sortedTokens[i]] ?? 0;
1813
+ }
1814
+ const { blob, offsets } = encodeStringBlob(sortedTokens);
1815
+ return { blob, offsets, payloadSlots, documentFrequencies };
1816
+ }
1817
+ function wrapFrozenTokenTable(data) {
1818
+ const { blob, offsets, payloadSlots, documentFrequencies } = data;
1819
+ const size = payloadSlots.length;
1820
+ const decodedTokens = new Array(size);
1821
+ function lowerBound2(query) {
1822
+ let lo = 0;
1823
+ let hi = size;
1824
+ while (lo < hi) {
1825
+ const mid = lo + hi >>> 1;
1826
+ if (compareBytes2(blob, offsets[mid], offsets[mid + 1], query) < 0) lo = mid + 1;
1827
+ else hi = mid;
1828
+ }
1829
+ return lo;
1830
+ }
1831
+ function prefixEnd(from, prefix) {
1832
+ let lo = from;
1833
+ let hi = size;
1834
+ while (lo < hi) {
1835
+ const mid = lo + hi >>> 1;
1836
+ if (startsWithBytes(blob, offsets[mid], offsets[mid + 1], prefix)) lo = mid + 1;
1837
+ else if (compareBytes2(blob, offsets[mid], offsets[mid + 1], prefix) < 0) lo = mid + 1;
1838
+ else hi = mid;
1839
+ }
1840
+ return lo;
1841
+ }
1842
+ return {
1843
+ size,
1844
+ bytes: blob.byteLength + offsets.byteLength + payloadSlots.byteLength + documentFrequencies.byteLength,
1845
+ find(token) {
1846
+ const query = encodeLookupKey(token);
1847
+ const at = lowerBound2(query);
1848
+ if (at >= size) return -1;
1849
+ return compareBytes2(blob, offsets[at], offsets[at + 1], query) === 0 ? at : -1;
1850
+ },
1851
+ tokenAt(sortedIndex) {
1852
+ let token = decodedTokens[sortedIndex];
1853
+ if (token === void 0) {
1854
+ token = decoder3.decode(blob.subarray(offsets[sortedIndex], offsets[sortedIndex + 1]));
1855
+ decodedTokens[sortedIndex] = token;
1856
+ }
1857
+ return token;
1858
+ },
1859
+ byteLengthAt(sortedIndex) {
1860
+ return offsets[sortedIndex + 1] - offsets[sortedIndex];
1861
+ },
1862
+ payloadSlot(sortedIndex) {
1863
+ return payloadSlots[sortedIndex];
1864
+ },
1865
+ documentFrequencyAt(sortedIndex) {
1866
+ return documentFrequencies[sortedIndex];
1867
+ },
1868
+ prefixRange(prefix) {
1869
+ const bytes = encoder2.encode(prefix);
1870
+ const start = lowerBound2(bytes);
1871
+ return { start, end: prefixEnd(start, bytes) };
1872
+ },
1873
+ firstCharRange(token) {
1874
+ const unit = token.charCodeAt(0);
1875
+ if (Number.isNaN(unit)) return { start: 0, end: 0 };
1876
+ let fromCodePoint;
1877
+ let toCodePoint;
1878
+ if (unit >= 55296 && unit <= 56319) {
1879
+ fromCodePoint = 65536 + (unit - 55296 << 10);
1880
+ toCodePoint = fromCodePoint + 1024;
1881
+ } else if (unit >= 56320 && unit <= 57343) {
1882
+ return { start: 0, end: 0 };
1883
+ } else {
1884
+ fromCodePoint = unit;
1885
+ toCodePoint = nextCodePoint(unit);
1886
+ }
1887
+ const from = encoder2.encode(String.fromCodePoint(fromCodePoint));
1888
+ const to = encoder2.encode(String.fromCodePoint(toCodePoint));
1889
+ return { start: lowerBound2(from), end: lowerBound2(to) };
1890
+ }
1891
+ };
1892
+ }
1893
+ function buildFrozenTokenTable(tokens, docFrequencies) {
1894
+ return wrapFrozenTokenTable(encodeFrozenTokenTableData(tokens, docFrequencies));
1895
+ }
1896
+
1897
+ // src/core/partition/frozen/tombstones.ts
1898
+ function createFrozenTombstones() {
1899
+ const removed = /* @__PURE__ */ new Set();
1900
+ const arrivals = [];
1901
+ let revision = 0;
1902
+ return {
1903
+ get size() {
1904
+ return removed.size;
1905
+ },
1906
+ get revision() {
1907
+ return revision;
1908
+ },
1909
+ has(internalId) {
1910
+ return removed.size > 0 && removed.has(internalId);
1911
+ },
1912
+ add(internalId) {
1913
+ if (removed.has(internalId)) return false;
1914
+ removed.add(internalId);
1915
+ arrivals.push(internalId);
1916
+ revision++;
1917
+ return true;
1918
+ },
1919
+ at(arrival) {
1920
+ return arrivals[arrival];
1921
+ },
1922
+ values() {
1923
+ return removed.values();
1924
+ }
1925
+ };
1926
+ }
1927
+
1928
+ // src/core/partition/frozen/shared-snapshot.ts
1929
+ function sharedUint32(source) {
1930
+ const copy = new Uint32Array(new SharedArrayBuffer(source.length * 4));
1931
+ copy.set(source);
1932
+ return copy;
1933
+ }
1934
+ function sharedUint16(source) {
1935
+ const copy = new Uint16Array(new SharedArrayBuffer(source.length * 2));
1936
+ copy.set(source);
1937
+ return copy;
1938
+ }
1939
+ function sharedUint8(source) {
1940
+ const copy = new Uint8Array(new SharedArrayBuffer(source.length));
1941
+ copy.set(source);
1942
+ return copy;
1943
+ }
1944
+ function sharedFloat64(source) {
1945
+ const copy = new Float64Array(new SharedArrayBuffer(source.length * 8));
1946
+ copy.set(source);
1947
+ return copy;
1948
+ }
1949
+ function sharedSurfaceTable(payload) {
1950
+ if (payload.surfaceForms === null) return null;
1951
+ const table = encodeSurfaceTable(payload.surfaceForms);
1952
+ if (table.counts.length === 0) return null;
1953
+ return { blob: sharedUint8(table.blob), offsets: sharedUint32(table.offsets), counts: sharedUint32(table.counts) };
1954
+ }
1955
+ function sharePartialWordTable(table) {
1956
+ if (typeof SharedArrayBuffer !== "function" || table.runEntries.buffer instanceof SharedArrayBuffer) return table;
1957
+ return {
1958
+ fields: [...table.fields],
1959
+ spellings: sharedUint8(table.spellings),
1960
+ spellingOffsets: sharedUint32(table.spellingOffsets),
1961
+ spellingTokens: sharedUint32(table.spellingTokens),
1962
+ spellingCounts: sharedUint32(table.spellingCounts),
1963
+ runs: sharedUint32(table.runs),
1964
+ runOffsets: sharedUint32(table.runOffsets),
1965
+ runEntries: sharedUint32(table.runEntries)
1966
+ };
1967
+ }
1968
+ function sharedPartialWordsOf(tokenData, spellings, documentData, analysis) {
1969
+ let source = spellings;
1970
+ if (source === null && analysis !== null && analysis.fields.length > 0) {
1971
+ source = partialSpellingsOfDocuments(storedDocumentsOf(documentData), analysis);
1972
+ }
1973
+ if (source === null || source.fields.length === 0) return null;
1974
+ const table = buildPartialWordTableFrom(source, wrapFrozenTokenTable(tokenData));
1975
+ return table === null ? null : sharePartialWordTable(table);
1976
+ }
1977
+ function partialWordsMatch(table, analysis) {
1978
+ if (table === null) return analysis === null || analysis.fields.length === 0;
1979
+ return analysis === null || sameFields(table.fields, analysis.fields);
1980
+ }
1981
+ function shareSegmentArrays(arrays, analysis, segmentId) {
1982
+ const partialWords = partialWordsMatch(arrays.partialWords, analysis) ? arrays.partialWords : sharedPartialWordsOf(arrays.tokenTable, null, arrays.documentTable, analysis);
1983
+ return { ...arrays, segmentId: segmentId ?? generateId(), partialWords };
1984
+ }
1985
+ function segmentArraysOf(payload, documentData, reusedPartialWords = null) {
1986
+ const tokenTable = encodeFrozenTokenTableData(payload.tokens, payload.docFrequencies);
1987
+ const idTable = encodeExternalIdTableData(payload.docIds);
1988
+ const surfaceTable = payload.surfaceForms === null ? null : encodeSurfaceTable(payload.surfaceForms);
1989
+ const partialWords = reusedPartialWords ?? (payload.partialSpellings === null ? null : buildPartialWordTableFrom(payload.partialSpellings, wrapFrozenTokenTable(tokenTable)));
1990
+ return {
1991
+ documentCount: payload.documentCount,
1992
+ fieldNames: payload.fieldNames,
1993
+ fieldLengthNames: payload.fieldLengthNames,
1994
+ fieldLengthColumns: payload.fieldLengthColumns,
1995
+ totalFieldLengths: payload.totalFieldLengths,
1996
+ tokenTable,
1997
+ idTable,
1998
+ documentTable: documentData,
1999
+ postingOffsets: payload.postingOffsets,
2000
+ postingDocIds: payload.postingDocIds,
2001
+ postingFrequencies: payload.postingFrequencies,
2002
+ postingFieldIndices: payload.postingFieldIndices,
2003
+ positionOffsets: payload.positionOffsets,
2004
+ positionValues: payload.positionValues,
2005
+ numeric: payload.numeric,
2006
+ sizes: payload.sizes,
2007
+ boolean: payload.boolean,
2008
+ enums: payload.enums,
2009
+ geo: payload.geo,
2010
+ pattern: payload.pattern,
2011
+ surfaceTable: surfaceTable === null || surfaceTable.counts.length === 0 ? null : surfaceTable,
2012
+ partialWords
2013
+ };
2014
+ }
2015
+ function freezeSegmentShared(payload, documents, segmentId, analysis = null) {
2016
+ if (typeof SharedArrayBuffer !== "function") return null;
2017
+ return freezeEncodedSegmentShared(payload, encodeDocumentTableData(documents), segmentId, analysis);
2018
+ }
2019
+ function freezeEncodedSegmentShared(payload, documentData, segmentId, analysis = null, reusedPartialWords = null) {
2020
+ if (typeof SharedArrayBuffer !== "function") return null;
2021
+ const tokenData = encodeFrozenTokenTableData(payload.tokens, payload.docFrequencies);
2022
+ const idData = encodeExternalIdTableData(payload.docIds);
2023
+ return {
2024
+ segmentId: segmentId ?? generateId(),
2025
+ documentCount: payload.documentCount,
2026
+ fieldNames: [...payload.fieldNames],
2027
+ fieldLengthNames: [...payload.fieldLengthNames],
2028
+ fieldLengthColumns: payload.fieldLengthColumns.map(sharedUint32),
2029
+ totalFieldLengths: { ...payload.totalFieldLengths },
2030
+ tokenTable: {
2031
+ blob: sharedUint8(tokenData.blob),
2032
+ offsets: sharedUint32(tokenData.offsets),
2033
+ payloadSlots: sharedUint32(tokenData.payloadSlots),
2034
+ documentFrequencies: sharedUint32(tokenData.documentFrequencies)
2035
+ },
2036
+ idTable: {
2037
+ blob: sharedUint8(idData.blob),
2038
+ offsets: sharedUint32(idData.offsets),
2039
+ sortedOrdinals: sharedUint32(idData.sortedOrdinals)
2040
+ },
2041
+ documentTable: {
2042
+ blob: sharedUint8(documentData.blob),
2043
+ offsets: sharedUint32(documentData.offsets),
2044
+ ...documentData.discardedFields === void 0 ? {} : { discardedFields: documentData.discardedFields }
2045
+ },
2046
+ postingOffsets: sharedUint32(payload.postingOffsets),
2047
+ postingDocIds: sharedUint32(payload.postingDocIds),
2048
+ postingFrequencies: sharedUint16(payload.postingFrequencies),
2049
+ postingFieldIndices: sharedUint16(payload.postingFieldIndices),
2050
+ positionOffsets: payload.positionOffsets === null ? null : sharedUint32(payload.positionOffsets),
2051
+ positionValues: payload.positionValues === null ? null : sharedUint32(payload.positionValues),
2052
+ numeric: payload.numeric.map((entry) => ({
2053
+ fieldPath: entry.fieldPath,
2054
+ docIds: sharedUint32(entry.docIds),
2055
+ values: sharedFloat64(entry.values)
2056
+ })),
2057
+ sizes: payload.sizes.map((entry) => ({
2058
+ fieldPath: entry.fieldPath,
2059
+ docIds: sharedUint32(entry.docIds),
2060
+ values: sharedFloat64(entry.values)
2061
+ })),
2062
+ boolean: payload.boolean.map((entry) => ({
2063
+ fieldPath: entry.fieldPath,
2064
+ trueDocs: sharedUint32(entry.trueDocs),
2065
+ falseDocs: sharedUint32(entry.falseDocs)
2066
+ })),
2067
+ enums: payload.enums.map((entry) => ({
2068
+ fieldPath: entry.fieldPath,
2069
+ values: [...entry.values],
2070
+ offsets: sharedUint32(entry.offsets),
2071
+ docIds: sharedUint32(entry.docIds)
2072
+ })),
2073
+ geo: payload.geo.map((entry) => ({
2074
+ fieldPath: entry.fieldPath,
2075
+ docIds: sharedUint32(entry.docIds),
2076
+ latitudes: sharedFloat64(entry.latitudes),
2077
+ longitudes: sharedFloat64(entry.longitudes)
2078
+ })),
2079
+ pattern: payload.pattern.map((entry) => ({
2080
+ fieldPath: entry.fieldPath,
2081
+ docIds: sharedUint32(entry.docIds),
2082
+ runs: sharedUint32(entry.runs),
2083
+ offsets: sharedUint32(entry.offsets),
2084
+ postings: sharedUint32(entry.postings)
2085
+ })),
2086
+ surfaceTable: sharedSurfaceTable(payload),
2087
+ partialWords: reusedPartialWords === null ? sharedPartialWordsOf(tokenData, payload.partialSpellings, documentData, analysis) : sharePartialWordTable(reusedPartialWords)
2088
+ };
2089
+ }
2090
+
2091
+ // src/core/partition/frozen/index.ts
2092
+ function buildStatsView(counts) {
2093
+ return {
2094
+ get totalDocuments() {
2095
+ return counts.totalDocuments();
2096
+ },
2097
+ get totalFieldLengths() {
2098
+ return counts.totalFieldLengths();
2099
+ },
2100
+ get averageFieldLengths() {
2101
+ const documents = counts.totalDocuments();
2102
+ const averages = {};
2103
+ if (documents > 0) {
2104
+ for (const [fieldName, total] of Object.entries(counts.totalFieldLengths())) {
2105
+ averages[fieldName] = total / documents;
2106
+ }
2107
+ }
2108
+ return averages;
2109
+ },
2110
+ get docFrequencies() {
2111
+ return counts.docFrequencies();
2112
+ }
2113
+ };
2114
+ }
2115
+ function storedFrequenciesOf(source) {
2116
+ let materialized = null;
2117
+ return () => {
2118
+ if (materialized === null) materialized = source.docFrequencies();
2119
+ return materialized;
2120
+ };
2121
+ }
2122
+ function buildGeoReaders(entries) {
2123
+ const readers = /* @__PURE__ */ new Map();
2124
+ for (const entry of entries) {
2125
+ const index = createGeoIndex();
2126
+ for (let i = 0; i < entry.docIds.length; i++) {
2127
+ index.insert(entry.docIds[i], entry.latitudes[i], entry.longitudes[i]);
2128
+ }
2129
+ readers.set(entry.fieldPath, index);
2130
+ }
2131
+ return readers;
2132
+ }
2133
+ function assembleFrozenSegment(segmentId, source, documentSource, sharedSnapshot, partialWordTable, survivingSurfaceForms) {
2134
+ const tombstones = createFrozenTombstones();
2135
+ const counts = createFrozenLiveCounts(source, source.tokenTable, tombstones, storedFrequenciesOf(source));
2136
+ const postingViews = createFrozenPostingViews(source, tombstones, counts);
2137
+ const lengthColumns = /* @__PURE__ */ new Map();
2138
+ for (let i = 0; i < source.fieldLengthNames.length; i++) {
2139
+ lengthColumns.set(source.fieldLengthNames[i], source.fieldLengthColumns[i]);
2140
+ }
2141
+ const docStore = createFrozenDocTable(source.idTable, lengthColumns, documentSource, tombstones);
2142
+ const partialWords = createFrozenPartialWords(partialWordTable, source.tokenTable, {
2143
+ count: documentSource.count,
2144
+ documentAt: (ordinal) => documentSource.readAt(ordinal),
2145
+ isTombstoned: (ordinal) => tombstones.has(ordinal)
2146
+ });
2147
+ const textDocuments = {
2148
+ count: documentSource.count,
2149
+ textFields: source.fieldLengthNames,
2150
+ documentAt: (ordinal) => documentSource.readAt(ordinal)
2151
+ };
2152
+ const loadSurfaceForms = survivingSurfaceForms === void 0 ? source.readSurfaceForms : () => survivingSurfaceForms;
2153
+ const surfaces = createFrozenSurfaceSlot(loadSurfaceForms, textDocuments);
2154
+ const removals = createFrozenRemovalPass(textDocuments, surfaces);
2155
+ const numericIndexes = /* @__PURE__ */ new Map();
2156
+ for (const entry of source.numeric) numericIndexes.set(entry.fieldPath, createFrozenNumericReader(entry));
2157
+ const sizeIndexes = /* @__PURE__ */ new Map();
2158
+ for (const entry of source.sizes) sizeIndexes.set(entry.fieldPath, createFrozenNumericReader(entry));
2159
+ const booleanIndexes = /* @__PURE__ */ new Map();
2160
+ for (const entry of source.boolean) booleanIndexes.set(entry.fieldPath, createFrozenBooleanReader(entry));
2161
+ const enumIndexes = /* @__PURE__ */ new Map();
2162
+ for (const entry of source.enums) enumIndexes.set(entry.fieldPath, createFrozenEnumReader(entry));
2163
+ const patternIndexes = /* @__PURE__ */ new Map();
2164
+ for (const entry of source.pattern) {
2165
+ patternIndexes.set(entry.fieldPath, requirePatternSearch(entry.fieldPath).readArrays(entry));
2166
+ }
2167
+ const fieldNameTable = {
2168
+ names: [...source.fieldNames],
2169
+ indexMap: new Map(source.fieldNames.map((name, index) => [name, index]))
2170
+ };
2171
+ const segment = {
2172
+ segmentId,
2173
+ documentSource,
2174
+ sharedSnapshot,
2175
+ arrays: source,
2176
+ invertedIdx: createFrozenInvertedReader(source.tokenTable, postingViews, counts),
2177
+ docStore,
2178
+ stats: buildStatsView(counts),
2179
+ surfaceRegistry: surfaces.reader,
2180
+ surfaces,
2181
+ removals,
2182
+ numericIndexes,
2183
+ booleanIndexes,
2184
+ enumIndexes,
2185
+ geoIndexes: buildGeoReaders(source.geo),
2186
+ patternIndexes,
2187
+ sizeIndexes,
2188
+ fieldNameTable,
2189
+ partialWords,
2190
+ trackPositions: source.positionOffsets !== null,
2191
+ flatSchemaCache: null,
2192
+ lastSchemaRef: null,
2193
+ sortColumns: null,
2194
+ scoreBuffer: null,
2195
+ vectorOrdinalMaps: null,
2196
+ liveDocumentCount() {
2197
+ return source.documentCount - tombstones.size;
2198
+ },
2199
+ hasDocument(docId) {
2200
+ return docStore.has(docId);
2201
+ },
2202
+ isTombstoned(ordinal) {
2203
+ return tombstones.has(ordinal);
2204
+ },
2205
+ removedSpellingsOf(docId) {
2206
+ const ordinal = docStore.getInternalId(docId);
2207
+ if (ordinal === void 0 || tombstones.has(ordinal)) return void 0;
2208
+ return removals.spellingsOf(ordinal);
2209
+ },
2210
+ tombstoneDocument(docId, removedSpellings) {
2211
+ const ordinal = docStore.getInternalId(docId);
2212
+ if (ordinal === void 0) return false;
2213
+ const added = tombstones.add(ordinal);
2214
+ if (added) {
2215
+ counts.subtractRemoved(ordinal);
2216
+ partialWords.afterRemoval(ordinal);
2217
+ removals.afterRemoval(ordinal, removedSpellings);
2218
+ docStore.releaseSortedDocIds();
2219
+ segment.sortColumns?.forget(ordinal);
2220
+ }
2221
+ return added;
2222
+ },
2223
+ tombstonedDocIds() {
2224
+ const docIds = [];
2225
+ for (const ordinal of tombstones.values()) docIds.push(source.idTable.idAt(ordinal));
2226
+ return docIds;
2227
+ },
2228
+ removalBytes() {
2229
+ return counts.bytes();
2230
+ }
2231
+ };
2232
+ return segment;
2233
+ }
2234
+ function createFrozenSegment(payload, documents, segmentId) {
2235
+ const tokenTable = buildFrozenTokenTable(payload.tokens, payload.docFrequencies);
2236
+ return assembleFrozenSegment(
2237
+ segmentId ?? generateId(),
2238
+ {
2239
+ ...payload,
2240
+ tokenTable,
2241
+ idTable: buildExternalIdTable(payload.docIds),
2242
+ documentTable: null,
2243
+ readSurfaceForms: () => payload.surfaceForms,
2244
+ docFrequencies: () => payload.docFrequencies
2245
+ },
2246
+ wrapDocumentArray(documents),
2247
+ null,
2248
+ payload.partialSpellings === null ? null : buildPartialWordTableFrom(payload.partialSpellings, tokenTable)
2249
+ );
2250
+ }
2251
+ function createSharedFrozenSegment(snapshot, survivingSurfaceForms) {
2252
+ const tokenTable = wrapFrozenTokenTable(snapshot.tokenTable);
2253
+ return assembleFrozenSegment(
2254
+ snapshot.segmentId,
2255
+ {
2256
+ ...snapshot,
2257
+ tokenTable,
2258
+ idTable: wrapExternalIdTable(snapshot.idTable),
2259
+ documentTable: snapshot.documentTable,
2260
+ readSurfaceForms: () => snapshot.surfaceTable === null ? null : decodeSurfaceTable(snapshot.surfaceTable),
2261
+ docFrequencies: () => {
2262
+ const frequencies = /* @__PURE__ */ Object.create(null);
2263
+ for (let at = 0; at < tokenTable.size; at++) {
2264
+ frequencies[tokenTable.tokenAt(at)] = tokenTable.documentFrequencyAt(at);
2265
+ }
2266
+ return frequencies;
2267
+ }
2268
+ },
2269
+ wrapEncodedDocumentTable(snapshot.documentTable),
2270
+ snapshot,
2271
+ snapshot.partialWords,
2272
+ survivingSurfaceForms
2273
+ );
2274
+ }
2275
+
2276
+ // src/core/partition/frozen/merge-fields.ts
2277
+ function indexByFieldPath(entries) {
2278
+ const byFieldPath = /* @__PURE__ */ new Map();
2279
+ for (const entry of entries) byFieldPath.set(entry.fieldPath, entry);
2280
+ return byFieldPath;
2281
+ }
2282
+ function rowsByFieldPath(inputs, entriesOf, valuesOf) {
2283
+ const byFieldPath = /* @__PURE__ */ new Map();
2284
+ for (const input of inputs) {
2285
+ for (const entry of entriesOf(input)) {
2286
+ let rows = byFieldPath.get(entry.fieldPath);
2287
+ if (rows === void 0) {
2288
+ rows = [];
2289
+ byFieldPath.set(entry.fieldPath, rows);
2290
+ }
2291
+ for (let i = 0; i < entry.docIds.length; i++) {
2292
+ const docId = input.remap[entry.docIds[i]];
2293
+ if (docId < 0) continue;
2294
+ rows.push({ docId, values: valuesOf(entry, i) });
2295
+ }
2296
+ }
2297
+ }
2298
+ return byFieldPath;
2299
+ }
2300
+ function docIdColumn(rows) {
2301
+ const docIds = new Uint32Array(rows.length);
2302
+ for (let i = 0; i < rows.length; i++) docIds[i] = rows[i].docId;
2303
+ return docIds;
2304
+ }
2305
+ function valueColumn(rows, column) {
2306
+ const values = new Float64Array(rows.length);
2307
+ for (let i = 0; i < rows.length; i++) values[i] = rows[i].values[column];
2308
+ return values;
2309
+ }
2310
+ function remapDocIds(inputs, pick) {
2311
+ let total = 0;
2312
+ for (let at = 0; at < inputs.length; at++) {
2313
+ const docIds = pick(at);
2314
+ if (docIds === void 0) continue;
2315
+ for (let i = 0; i < docIds.length; i++) {
2316
+ if (inputs[at].remap[docIds[i]] >= 0) total += 1;
2317
+ }
2318
+ }
2319
+ const out = new Uint32Array(total);
2320
+ let cursor = 0;
2321
+ for (let at = 0; at < inputs.length; at++) {
2322
+ const docIds = pick(at);
2323
+ if (docIds === void 0) continue;
2324
+ for (let i = 0; i < docIds.length; i++) {
2325
+ const target = inputs[at].remap[docIds[i]];
2326
+ if (target >= 0) out[cursor++] = target;
2327
+ }
2328
+ }
2329
+ return out;
2330
+ }
2331
+ function mergeNumeric(inputs, pick) {
2332
+ const byFieldPath = rowsByFieldPath(inputs, pick, (entry, at) => [entry.values[at]]);
2333
+ const merged = [];
2334
+ for (const [fieldPath, rows] of byFieldPath) {
2335
+ rows.sort((a, b) => a.values[0] - b.values[0] || a.docId - b.docId);
2336
+ merged.push({ fieldPath, docIds: docIdColumn(rows), values: valueColumn(rows, 0) });
2337
+ }
2338
+ return merged;
2339
+ }
2340
+ function mergeGeo(inputs) {
2341
+ const byFieldPath = rowsByFieldPath(
2342
+ inputs,
2343
+ (input) => input.segment.arrays.geo,
2344
+ (entry, at) => [entry.latitudes[at], entry.longitudes[at]]
2345
+ );
2346
+ const merged = [];
2347
+ for (const [fieldPath, rows] of byFieldPath) {
2348
+ merged.push({
2349
+ fieldPath,
2350
+ docIds: docIdColumn(rows),
2351
+ latitudes: valueColumn(rows, 0),
2352
+ longitudes: valueColumn(rows, 1)
2353
+ });
2354
+ }
2355
+ return merged;
2356
+ }
2357
+ function mergeBoolean(inputs) {
2358
+ const perInput = inputs.map((input) => indexByFieldPath(input.segment.arrays.boolean));
2359
+ const fieldPaths = [];
2360
+ for (const index of perInput) {
2361
+ for (const fieldPath of index.keys()) {
2362
+ if (!fieldPaths.includes(fieldPath)) fieldPaths.push(fieldPath);
2363
+ }
2364
+ }
2365
+ return fieldPaths.map((fieldPath) => ({
2366
+ fieldPath,
2367
+ trueDocs: remapDocIds(inputs, (at) => perInput[at].get(fieldPath)?.trueDocs),
2368
+ falseDocs: remapDocIds(inputs, (at) => perInput[at].get(fieldPath)?.falseDocs)
2369
+ }));
2370
+ }
2371
+ function enumDocIds(entry, value) {
2372
+ if (entry === void 0) return void 0;
2373
+ const at = entry.values.indexOf(value);
2374
+ return at < 0 ? void 0 : entry.docIds.subarray(entry.offsets[at], entry.offsets[at + 1]);
2375
+ }
2376
+ function mergeEnums(inputs) {
2377
+ const perInput = inputs.map((input) => indexByFieldPath(input.segment.arrays.enums));
2378
+ const valuesByFieldPath = /* @__PURE__ */ new Map();
2379
+ for (const index of perInput) {
2380
+ for (const [fieldPath, entry] of index) {
2381
+ let values = valuesByFieldPath.get(fieldPath);
2382
+ if (values === void 0) {
2383
+ values = /* @__PURE__ */ new Set();
2384
+ valuesByFieldPath.set(fieldPath, values);
2385
+ }
2386
+ for (const value of entry.values) values.add(value);
2387
+ }
2388
+ }
2389
+ const merged = [];
2390
+ for (const [fieldPath, valueSet] of valuesByFieldPath) {
2391
+ const values = [...valueSet].sort(compareCodePoints);
2392
+ const perValue = values.map((value) => remapDocIds(inputs, (at) => enumDocIds(perInput[at].get(fieldPath), value)));
2393
+ const offsets = new Uint32Array(values.length + 1);
2394
+ let total = 0;
2395
+ for (let i = 0; i < values.length; i++) {
2396
+ total += perValue[i].length;
2397
+ offsets[i + 1] = total;
2398
+ }
2399
+ const docIds = new Uint32Array(total);
2400
+ for (let i = 0; i < values.length; i++) docIds.set(perValue[i], offsets[i]);
2401
+ merged.push({ fieldPath, values, offsets, docIds });
2402
+ }
2403
+ return merged;
2404
+ }
2405
+ function mergePattern(inputs) {
2406
+ const byFieldPath = /* @__PURE__ */ new Map();
2407
+ for (const input of inputs) {
2408
+ for (const arrays of input.segment.arrays.pattern) {
2409
+ let entries = byFieldPath.get(arrays.fieldPath);
2410
+ if (entries === void 0) {
2411
+ entries = [];
2412
+ byFieldPath.set(arrays.fieldPath, entries);
2413
+ }
2414
+ entries.push({ arrays, remap: input.remap });
2415
+ }
2416
+ }
2417
+ const merged = [];
2418
+ for (const [fieldPath, entries] of byFieldPath) {
2419
+ merged.push(requirePatternSearch(fieldPath).mergeArrays(fieldPath, entries));
2420
+ }
2421
+ return merged;
2422
+ }
2423
+ function mergeFieldIndexes(inputs) {
2424
+ return {
2425
+ numeric: mergeNumeric(inputs, (input) => input.segment.arrays.numeric),
2426
+ boolean: mergeBoolean(inputs),
2427
+ enums: mergeEnums(inputs),
2428
+ geo: mergeGeo(inputs),
2429
+ pattern: mergePattern(inputs),
2430
+ sizes: mergeNumeric(inputs, (input) => input.segment.arrays.sizes)
2431
+ };
2432
+ }
2433
+
2434
+ // src/core/partition/frozen/merge-postings.ts
2435
+ function advance(cursor) {
2436
+ cursor.at += 1;
2437
+ if (cursor.at >= cursor.input.segment.arrays.tokenTable.size) return false;
2438
+ cursor.token = cursor.input.segment.arrays.tokenTable.tokenAt(cursor.at);
2439
+ return true;
2440
+ }
2441
+ function openCursors(inputs) {
2442
+ const cursors = [];
2443
+ for (const input of inputs) {
2444
+ const table = input.segment.arrays.tokenTable;
2445
+ if (table.size === 0) continue;
2446
+ cursors.push({ input, at: 0, token: table.tokenAt(0) });
2447
+ }
2448
+ return cursors;
2449
+ }
2450
+ function walkTokens(inputs, visit) {
2451
+ const cursors = openCursors(inputs);
2452
+ while (cursors.length > 0) {
2453
+ let smallest = cursors[0].token;
2454
+ for (let i = 1; i < cursors.length; i++) {
2455
+ if (compareCodePoints(cursors[i].token, smallest) < 0) smallest = cursors[i].token;
2456
+ }
2457
+ const ranges = [];
2458
+ for (let i = cursors.length - 1; i >= 0; i--) {
2459
+ const cursor = cursors[i];
2460
+ if (cursor.token !== smallest) continue;
2461
+ const arrays = cursor.input.segment.arrays;
2462
+ const slot = arrays.tokenTable.payloadSlot(cursor.at);
2463
+ ranges.push({ input: cursor.input, start: arrays.postingOffsets[slot], end: arrays.postingOffsets[slot + 1] });
2464
+ if (!advance(cursor)) cursors.splice(i, 1);
2465
+ }
2466
+ ranges.reverse();
2467
+ visit(smallest, ranges);
2468
+ }
2469
+ }
2470
+ function uniqueSurvivors(range) {
2471
+ const { arrays } = range.input.segment;
2472
+ const remap = range.input.remap;
2473
+ let docs = 0;
2474
+ let previous = -1;
2475
+ let ordered = true;
2476
+ for (let p = range.start; p < range.end; p++) {
2477
+ const internalId = arrays.postingDocIds[p];
2478
+ if (remap[internalId] < 0) continue;
2479
+ if (internalId < previous) ordered = false;
2480
+ if (internalId !== previous) docs += 1;
2481
+ previous = internalId;
2482
+ }
2483
+ if (ordered) return docs;
2484
+ const seen = /* @__PURE__ */ new Set();
2485
+ for (let p = range.start; p < range.end; p++) {
2486
+ const internalId = arrays.postingDocIds[p];
2487
+ if (remap[internalId] >= 0) seen.add(internalId);
2488
+ }
2489
+ return seen.size;
2490
+ }
2491
+ function countRange(range, counts, keepsEveryDocument2) {
2492
+ const { arrays } = range.input.segment;
2493
+ if (keepsEveryDocument2) {
2494
+ counts.postings += range.end - range.start;
2495
+ if (arrays.positionOffsets !== null) {
2496
+ counts.positions += arrays.positionOffsets[range.end] - arrays.positionOffsets[range.start];
2497
+ }
2498
+ return;
2499
+ }
2500
+ const remap = range.input.remap;
2501
+ for (let p = range.start; p < range.end; p++) {
2502
+ const internalId = arrays.postingDocIds[p];
2503
+ if (remap[internalId] < 0) continue;
2504
+ counts.postings += 1;
2505
+ if (arrays.positionOffsets !== null) {
2506
+ counts.positions += arrays.positionOffsets[p + 1] - arrays.positionOffsets[p];
2507
+ }
2508
+ }
2509
+ }
2510
+ function mergePostings(inputs) {
2511
+ let totalPostings = 0;
2512
+ let totalPositions = 0;
2513
+ let tokenCount = 0;
2514
+ let hasPositions = false;
2515
+ for (const input of inputs) {
2516
+ if (input.segment.arrays.positionOffsets !== null) hasPositions = true;
2517
+ }
2518
+ const keepsEveryDocument2 = /* @__PURE__ */ new Map();
2519
+ for (const input of inputs)
2520
+ keepsEveryDocument2.set(
2521
+ input,
2522
+ input.remap.every((target) => target >= 0)
2523
+ );
2524
+ walkTokens(inputs, (_token, ranges) => {
2525
+ const counts = { postings: 0, positions: 0 };
2526
+ for (const range of ranges) countRange(range, counts, keepsEveryDocument2.get(range.input) === true);
2527
+ if (counts.postings === 0) return;
2528
+ tokenCount += 1;
2529
+ totalPostings += counts.postings;
2530
+ totalPositions += counts.positions;
2531
+ });
2532
+ const tokens = [];
2533
+ const docFrequencies = /* @__PURE__ */ Object.create(null);
2534
+ const postingOffsets = new Uint32Array(tokenCount + 1);
2535
+ const postingDocIds = new Uint32Array(totalPostings);
2536
+ const postingFrequencies = new Uint16Array(totalPostings);
2537
+ const postingFieldIndices = new Uint16Array(totalPostings);
2538
+ const positionOffsets = hasPositions ? new Uint32Array(totalPostings + 1) : null;
2539
+ const positionValues = hasPositions ? new Uint32Array(totalPositions) : null;
2540
+ let postingCursor = 0;
2541
+ let positionCursor = 0;
2542
+ walkTokens(inputs, (token, ranges) => {
2543
+ const tokenStart = postingCursor;
2544
+ let docs = 0;
2545
+ for (const range of ranges) {
2546
+ const { arrays } = range.input.segment;
2547
+ const remap = range.input.remap;
2548
+ const fieldIndexRemap = range.input.fieldIndexRemap;
2549
+ docs += uniqueSurvivors(range);
2550
+ if (keepsEveryDocument2.get(range.input) === true) {
2551
+ const count = range.end - range.start;
2552
+ for (let k = 0; k < count; k++) {
2553
+ postingDocIds[postingCursor + k] = remap[arrays.postingDocIds[range.start + k]];
2554
+ postingFieldIndices[postingCursor + k] = fieldIndexRemap[arrays.postingFieldIndices[range.start + k]];
2555
+ }
2556
+ postingFrequencies.set(arrays.postingFrequencies.subarray(range.start, range.end), postingCursor);
2557
+ if (positionOffsets !== null && positionValues !== null) {
2558
+ if (arrays.positionOffsets !== null && arrays.positionValues !== null) {
2559
+ const from = arrays.positionOffsets[range.start];
2560
+ const to = arrays.positionOffsets[range.end];
2561
+ positionValues.set(arrays.positionValues.subarray(from, to), positionCursor);
2562
+ for (let k = 0; k < count; k++) {
2563
+ positionOffsets[postingCursor + k] = positionCursor + arrays.positionOffsets[range.start + k] - from;
2564
+ }
2565
+ positionCursor += to - from;
2566
+ } else {
2567
+ positionOffsets.fill(positionCursor, postingCursor, postingCursor + count);
2568
+ }
2569
+ }
2570
+ postingCursor += count;
2571
+ continue;
2572
+ }
2573
+ for (let p = range.start; p < range.end; p++) {
2574
+ const internalId = arrays.postingDocIds[p];
2575
+ const target = remap[internalId];
2576
+ if (target < 0) continue;
2577
+ postingDocIds[postingCursor] = target;
2578
+ postingFrequencies[postingCursor] = arrays.postingFrequencies[p];
2579
+ postingFieldIndices[postingCursor] = fieldIndexRemap[arrays.postingFieldIndices[p]];
2580
+ if (positionOffsets !== null && positionValues !== null) {
2581
+ positionOffsets[postingCursor] = positionCursor;
2582
+ if (arrays.positionOffsets !== null && arrays.positionValues !== null) {
2583
+ const from = arrays.positionOffsets[p];
2584
+ const to = arrays.positionOffsets[p + 1];
2585
+ positionValues.set(arrays.positionValues.subarray(from, to), positionCursor);
2586
+ positionCursor += to - from;
2587
+ }
2588
+ }
2589
+ postingCursor += 1;
2590
+ }
2591
+ }
2592
+ if (postingCursor === tokenStart) return;
2593
+ tokens.push(token);
2594
+ docFrequencies[token] = docs;
2595
+ postingOffsets[tokens.length] = postingCursor;
2596
+ });
2597
+ if (positionOffsets !== null) positionOffsets[postingCursor] = positionCursor;
2598
+ return {
2599
+ tokens,
2600
+ docFrequencies,
2601
+ postingOffsets,
2602
+ postingDocIds,
2603
+ postingFrequencies,
2604
+ postingFieldIndices,
2605
+ positionOffsets,
2606
+ positionValues
2607
+ };
2608
+ }
2609
+
2610
+ // src/core/partition/frozen/merge.ts
2611
+ function selectionKeeps(selection, ordinal) {
2612
+ return selection.keep === void 0 ? !selection.segment.isTombstoned(ordinal) : selection.keep(ordinal);
2613
+ }
2614
+ function remapOf(selection, docIds) {
2615
+ const { segment } = selection;
2616
+ const count = segment.arrays.documentCount;
2617
+ const remap = new Int32Array(count).fill(-1);
2618
+ for (let ordinal = 0; ordinal < count; ordinal++) {
2619
+ if (!selectionKeeps(selection, ordinal)) continue;
2620
+ remap[ordinal] = docIds.length;
2621
+ docIds.push(segment.arrays.idTable.idAt(ordinal));
2622
+ }
2623
+ return remap;
2624
+ }
2625
+ function fieldNameIndex(fieldNames, name) {
2626
+ const at = fieldNames.indexOf(name);
2627
+ if (at >= 0) return at;
2628
+ fieldNames.push(name);
2629
+ return fieldNames.length - 1;
2630
+ }
2631
+ function fieldIndexRemapOf(segment, fieldNames) {
2632
+ const remap = new Uint16Array(segment.arrays.fieldNames.length);
2633
+ for (let i = 0; i < segment.arrays.fieldNames.length; i++) {
2634
+ remap[i] = fieldNameIndex(fieldNames, segment.arrays.fieldNames[i]);
2635
+ }
2636
+ return remap;
2637
+ }
2638
+ function mergeFieldLengths(inputs, documentCount) {
2639
+ const names = [];
2640
+ const columns = [];
2641
+ const totals = {};
2642
+ for (const input of inputs) {
2643
+ const arrays = input.segment.arrays;
2644
+ const remap = input.remap;
2645
+ for (let f = 0; f < arrays.fieldLengthNames.length; f++) {
2646
+ const name = arrays.fieldLengthNames[f];
2647
+ let at = names.indexOf(name);
2648
+ if (at < 0) {
2649
+ at = names.length;
2650
+ names.push(name);
2651
+ columns.push(new Uint32Array(documentCount));
2652
+ totals[name] = 0;
2653
+ }
2654
+ const source = arrays.fieldLengthColumns[f];
2655
+ const column = columns[at];
2656
+ for (let ordinal = 0; ordinal < source.length; ordinal++) {
2657
+ const target = remap[ordinal];
2658
+ if (target < 0) continue;
2659
+ column[target] = source[ordinal];
2660
+ totals[name] += source[ordinal];
2661
+ }
2662
+ }
2663
+ }
2664
+ return { fieldLengthNames: names, fieldLengthColumns: columns, totalFieldLengths: totals };
2665
+ }
2666
+ function mergeSurfaceForms(inputs, analysis) {
2667
+ const registry = createSurfaceRegistry();
2668
+ for (const input of inputs) input.segment.surfaces.addKeptForms(registry, input.remap, analysis);
2669
+ return registry.size() === 0 ? null : registry.serialize();
2670
+ }
2671
+ function holdsADiscardedField(bytes, discardedFields) {
2672
+ const keys = topLevelMapKeys(bytes);
2673
+ if (keys === null) return true;
2674
+ for (const fieldPath of discardedFields) {
2675
+ const dot = fieldPath.indexOf(".");
2676
+ if (keys.has(dot < 0 ? fieldPath : fieldPath.slice(0, dot))) return true;
2677
+ }
2678
+ return false;
2679
+ }
2680
+ function encodedDocumentsOf(input) {
2681
+ const table = input.segment.arrays.documentTable;
2682
+ const encoded = new Array(input.remap.length);
2683
+ const encodeOrdinal = (ordinal) => {
2684
+ let bytes = encoded[ordinal];
2685
+ if (bytes === void 0) {
2686
+ bytes = encodeDocument(input.segment.documentSource.docAt(ordinal));
2687
+ encoded[ordinal] = bytes;
2688
+ }
2689
+ return bytes;
2690
+ };
2691
+ if (table === null) return encodeOrdinal;
2692
+ const discardedFields = table.discardedFields;
2693
+ return (ordinal) => {
2694
+ if (encoded[ordinal] !== void 0) return encodeOrdinal(ordinal);
2695
+ const stored = table.blob.subarray(table.offsets[ordinal], table.offsets[ordinal + 1]);
2696
+ if (discardedFields === void 0 || !holdsADiscardedField(stored, discardedFields)) return stored;
2697
+ return encodeOrdinal(ordinal);
2698
+ };
2699
+ }
2700
+ function mergeDocuments(inputs, documentCount) {
2701
+ const readers = inputs.map(encodedDocumentsOf);
2702
+ const offsets = new Uint32Array(documentCount + 1);
2703
+ for (let i = 0; i < inputs.length; i++) {
2704
+ const { remap } = inputs[i];
2705
+ for (let ordinal = 0; ordinal < remap.length; ordinal++) {
2706
+ if (remap[ordinal] >= 0) offsets[remap[ordinal] + 1] = readers[i](ordinal).length;
2707
+ }
2708
+ }
2709
+ for (let ordinal = 0; ordinal < documentCount; ordinal++) offsets[ordinal + 1] += offsets[ordinal];
2710
+ const blob = new Uint8Array(offsets[documentCount]);
2711
+ for (let i = 0; i < inputs.length; i++) {
2712
+ const { remap } = inputs[i];
2713
+ for (let ordinal = 0; ordinal < remap.length; ordinal++) {
2714
+ const target = remap[ordinal];
2715
+ if (target >= 0) blob.set(readers[i](ordinal), offsets[target]);
2716
+ }
2717
+ }
2718
+ return { blob, offsets };
2719
+ }
2720
+ function keepsEveryDocument(remap) {
2721
+ for (let ordinal = 0; ordinal < remap.length; ordinal++) {
2722
+ if (remap[ordinal] < 0) return false;
2723
+ }
2724
+ return true;
2725
+ }
2726
+ function mergePartialWords(inputs, mergedTokenCount, analysis) {
2727
+ if (analysis === null || analysis.fields.length === 0) return { spellings: null, reused: null };
2728
+ if (inputs.length === 1) {
2729
+ const [only] = inputs;
2730
+ const table = only.segment.partialWords.tableOfEveryDocument(analysis);
2731
+ const keepsTokens = mergedTokenCount === only.segment.arrays.tokenTable.size;
2732
+ if (table !== null && keepsTokens && keepsEveryDocument(only.remap)) return { spellings: null, reused: table };
2733
+ }
2734
+ const counts = /* @__PURE__ */ new Map();
2735
+ for (const input of inputs) input.segment.partialWords.countKeptSpellings(counts, analysis, input.remap);
2736
+ return { spellings: encodePartialSpellingsOf(analysis.fields, counts), reused: null };
2737
+ }
2738
+ function mergeFrozenSegments(segments, analysis, textAnalysis) {
2739
+ if (typeof SharedArrayBuffer !== "function") return null;
2740
+ const merged = mergeSegmentContents(
2741
+ segments.map((segment) => ({ segment })),
2742
+ analysis,
2743
+ textAnalysis
2744
+ );
2745
+ return freezeEncodedSegmentShared(merged.payload, merged.documents, void 0, analysis, merged.reusedPartialWords);
2746
+ }
2747
+ function mergeSegmentContents(selections, analysis = null, textAnalysis = null) {
2748
+ const docIds = [];
2749
+ const fieldNames = [];
2750
+ const inputs = selections.map((selection) => ({
2751
+ segment: selection.segment,
2752
+ remap: remapOf(selection, docIds),
2753
+ fieldIndexRemap: fieldIndexRemapOf(selection.segment, fieldNames)
2754
+ }));
2755
+ const documentCount = docIds.length;
2756
+ const postings = mergePostings(inputs);
2757
+ const lengths = mergeFieldLengths(inputs, documentCount);
2758
+ const partialWords = mergePartialWords(inputs, postings.tokens.length, analysis);
2759
+ const payload = {
2760
+ documentCount,
2761
+ docIds,
2762
+ fieldNames,
2763
+ tokens: postings.tokens,
2764
+ postingOffsets: postings.postingOffsets,
2765
+ postingDocIds: postings.postingDocIds,
2766
+ postingFrequencies: postings.postingFrequencies,
2767
+ postingFieldIndices: postings.postingFieldIndices,
2768
+ positionOffsets: postings.positionOffsets,
2769
+ positionValues: postings.positionValues,
2770
+ fieldLengthNames: lengths.fieldLengthNames,
2771
+ fieldLengthColumns: lengths.fieldLengthColumns,
2772
+ totalFieldLengths: lengths.totalFieldLengths,
2773
+ docFrequencies: postings.docFrequencies,
2774
+ surfaceForms: mergeSurfaceForms(inputs, textAnalysis),
2775
+ partialSpellings: partialWords.spellings,
2776
+ ...mergeFieldIndexes(inputs)
2777
+ };
2778
+ return { payload, documents: mergeDocuments(inputs, documentCount), reusedPartialWords: partialWords.reused };
2779
+ }
2780
+
2781
+ // src/core/partition/segment-postings.ts
2782
+ var TextPostings = class {
2783
+ constructor(trackPositions) {
2784
+ this.trackPositions = trackPositions;
2785
+ }
2786
+ tokens = [];
2787
+ fieldNames = [];
2788
+ tokenIds = /* @__PURE__ */ new Map();
2789
+ fieldNameIndexes = /* @__PURE__ */ new Map();
2790
+ postingsPerToken = new GrowableUint32(SEGMENT_TOKEN_TABLE_START);
2791
+ docFrequencyPerToken = new GrowableUint32(SEGMENT_TOKEN_TABLE_START);
2792
+ lastDocPerToken = new GrowableUint32(SEGMENT_TOKEN_TABLE_START);
2793
+ fieldStampPerToken = new GrowableUint32(SEGMENT_TOKEN_TABLE_START);
2794
+ fieldSlotPerToken = new GrowableUint32(SEGMENT_TOKEN_TABLE_START);
2795
+ logToken = new GrowableUint32(SEGMENT_POSTING_LOG_START);
2796
+ logDoc = new GrowableUint32(SEGMENT_POSTING_LOG_START);
2797
+ logFrequency = new GrowableUint32(SEGMENT_POSTING_LOG_START);
2798
+ logField = new GrowableUint32(SEGMENT_POSTING_LOG_START);
2799
+ logPositionStart = new GrowableUint32(SEGMENT_POSTING_LOG_START);
2800
+ positionPool = new GrowableUint32(SEGMENT_POSITION_POOL_START);
2801
+ slotToken = new GrowableUint32(SEGMENT_FIELD_SCRATCH_START);
2802
+ slotCount = new GrowableUint32(SEGMENT_FIELD_SCRATCH_START);
2803
+ slotCursor = new GrowableUint32(SEGMENT_FIELD_SCRATCH_START);
2804
+ eventSlot = new GrowableUint32(SEGMENT_FIELD_SCRATCH_START);
2805
+ eventPosition = new GrowableUint32(SEGMENT_FIELD_SCRATCH_START);
2806
+ fieldStamp = 0;
2807
+ beginField() {
2808
+ this.fieldStamp++;
2809
+ this.slotToken.clear();
2810
+ this.slotCount.clear();
2811
+ this.eventSlot.clear();
2812
+ this.eventPosition.clear();
2813
+ }
2814
+ get fieldLength() {
2815
+ return this.eventSlot.length;
2816
+ }
2817
+ tokenIdOf(token) {
2818
+ const known = this.tokenIds.get(token);
2819
+ if (known !== void 0) return known;
2820
+ const id = this.tokens.length;
2821
+ this.tokenIds.set(token, id);
2822
+ this.tokens.push(token);
2823
+ this.postingsPerToken.ensureIndex(id);
2824
+ this.docFrequencyPerToken.ensureIndex(id);
2825
+ this.lastDocPerToken.ensureIndex(id);
2826
+ this.fieldStampPerToken.ensureIndex(id);
2827
+ this.fieldSlotPerToken.ensureIndex(id);
2828
+ return id;
2829
+ }
2830
+ addToken(token, position) {
2831
+ const id = this.tokenIdOf(token);
2832
+ let slot;
2833
+ if (this.fieldStampPerToken.values[id] === this.fieldStamp) {
2834
+ slot = this.fieldSlotPerToken.values[id];
2835
+ this.slotCount.values[slot]++;
2836
+ } else {
2837
+ slot = this.slotToken.length;
2838
+ this.fieldStampPerToken.values[id] = this.fieldStamp;
2839
+ this.fieldSlotPerToken.values[id] = slot;
2840
+ this.slotToken.push(id);
2841
+ this.slotCount.push(1);
2842
+ }
2843
+ this.eventSlot.push(slot);
2844
+ this.eventPosition.push(position);
2845
+ }
2846
+ recordFieldPositions() {
2847
+ const events = this.eventSlot.length;
2848
+ const poolBase = this.positionPool.length;
2849
+ this.positionPool.reserve(events);
2850
+ this.slotCursor.clear();
2851
+ let cursor = poolBase;
2852
+ for (let slot = 0; slot < this.slotToken.length; slot++) {
2853
+ this.slotCursor.push(cursor);
2854
+ this.logPositionStart.push(cursor);
2855
+ cursor += this.slotCount.values[slot];
2856
+ }
2857
+ for (let event = 0; event < events; event++) {
2858
+ const slot = this.eventSlot.values[event];
2859
+ this.positionPool.values[this.slotCursor.values[slot]++] = this.eventPosition.values[event];
2860
+ }
2861
+ this.positionPool.length = poolBase + events;
2862
+ }
2863
+ endField(ordinal, fieldPath) {
2864
+ let fieldIndex = this.fieldNameIndexes.get(fieldPath);
2865
+ if (fieldIndex === void 0) {
2866
+ fieldIndex = this.fieldNames.length;
2867
+ this.fieldNames.push(fieldPath);
2868
+ this.fieldNameIndexes.set(fieldPath, fieldIndex);
2869
+ }
2870
+ if (this.trackPositions) this.recordFieldPositions();
2871
+ for (let slot = 0; slot < this.slotToken.length; slot++) {
2872
+ const id = this.slotToken.values[slot];
2873
+ this.logToken.push(id);
2874
+ this.logDoc.push(ordinal);
2875
+ this.logFrequency.push(this.slotCount.values[slot]);
2876
+ this.logField.push(fieldIndex);
2877
+ this.postingsPerToken.values[id]++;
2878
+ if (this.lastDocPerToken.values[id] !== ordinal + 1) {
2879
+ this.lastDocPerToken.values[id] = ordinal + 1;
2880
+ this.docFrequencyPerToken.values[id]++;
2881
+ }
2882
+ }
2883
+ }
2884
+ encode() {
2885
+ const tokenCount = this.tokens.length;
2886
+ const postings = this.logToken.length;
2887
+ const postingOffsets = new Uint32Array(tokenCount + 1);
2888
+ for (let t = 0; t < tokenCount; t++) postingOffsets[t + 1] = postingOffsets[t] + this.postingsPerToken.values[t];
2889
+ const writeCursor = postingOffsets.slice(0, tokenCount);
2890
+ const postingDocIds = new Uint32Array(postings);
2891
+ const postingFrequencies = new Uint16Array(postings);
2892
+ const postingFieldIndices = new Uint16Array(postings);
2893
+ const logRowOfPosting = new Uint32Array(postings);
2894
+ for (let row = 0; row < postings; row++) {
2895
+ const target = writeCursor[this.logToken.values[row]]++;
2896
+ const frequency = this.logFrequency.values[row];
2897
+ postingDocIds[target] = this.logDoc.values[row];
2898
+ postingFrequencies[target] = frequency > MAX_TERM_FREQUENCY ? MAX_TERM_FREQUENCY : frequency;
2899
+ postingFieldIndices[target] = this.logField.values[row];
2900
+ logRowOfPosting[target] = row;
2901
+ }
2902
+ let positionOffsets = null;
2903
+ let positionValues = null;
2904
+ if (this.trackPositions && postings > 0) {
2905
+ positionOffsets = new Uint32Array(postings + 1);
2906
+ positionValues = new Uint32Array(this.positionPool.length);
2907
+ let cursor = 0;
2908
+ for (let target = 0; target < postings; target++) {
2909
+ const row = logRowOfPosting[target];
2910
+ const start = this.logPositionStart.values[row];
2911
+ const count = this.logFrequency.values[row];
2912
+ positionOffsets[target] = cursor;
2913
+ positionValues.set(this.positionPool.values.subarray(start, start + count), cursor);
2914
+ cursor += count;
2915
+ }
2916
+ positionOffsets[postings] = cursor;
2917
+ }
2918
+ const docFrequencies = /* @__PURE__ */ Object.create(null);
2919
+ for (let t = 0; t < tokenCount; t++) docFrequencies[this.tokens[t]] = this.docFrequencyPerToken.values[t];
2920
+ return {
2921
+ tokens: this.tokens,
2922
+ fieldNames: this.fieldNames,
2923
+ postingOffsets,
2924
+ postingDocIds,
2925
+ postingFrequencies,
2926
+ postingFieldIndices,
2927
+ positionOffsets,
2928
+ positionValues,
2929
+ docFrequencies
2930
+ };
2931
+ }
2932
+ };
2933
+
2934
+ // src/core/partition/segment-builder.ts
2935
+ function indexOf(indexes, fieldPath, create) {
2936
+ let index = indexes.get(fieldPath);
2937
+ if (index === void 0) {
2938
+ index = create();
2939
+ indexes.set(fieldPath, index);
2940
+ }
2941
+ return index;
2942
+ }
2943
+ function indexFilterValue(indexes, ordinal, fieldPath, fieldType, value) {
2944
+ if (fieldType === "number" || fieldType === "number[]") {
2945
+ const index = indexOf(indexes.numericIndexes, fieldPath, createNumericIndex);
2946
+ for (const entry of fieldType === "number" ? [value] : value) index.insert(ordinal, entry);
2947
+ } else if (fieldType === "boolean" || fieldType === "boolean[]") {
2948
+ const index = indexOf(indexes.booleanIndexes, fieldPath, createBooleanIndex);
2949
+ for (const entry of fieldType === "boolean" ? [value] : value)
2950
+ index.insert(ordinal, entry);
2951
+ } else if (fieldType === "enum" || fieldType === "enum[]") {
2952
+ const index = indexOf(indexes.enumIndexes, fieldPath, createEnumIndex);
2953
+ for (const entry of fieldType === "enum" ? [value] : value) index.insert(ordinal, entry);
2954
+ } else if (fieldType === "geopoint") {
2955
+ const point = value;
2956
+ indexOf(indexes.geoIndexes, fieldPath, createGeoIndex).insert(ordinal, point.lat, point.lon);
2957
+ }
2958
+ }
2959
+ function refuseRepeatedDocIds(documents) {
2960
+ const seen = /* @__PURE__ */ new Set();
2961
+ for (const { docId } of documents) {
2962
+ if (seen.has(docId)) {
2963
+ throw new NarsilError(ErrorCodes.DOC_ALREADY_EXISTS, `Document "${docId}" already exists in partition 0`, {
2964
+ docId,
2965
+ partitionId: 0
2966
+ });
2967
+ }
2968
+ seen.add(docId);
2969
+ }
2970
+ }
2971
+ function validateAsAnInsertDoes(documents, schema, options) {
2972
+ if (options?.validate === false) return;
2973
+ for (const { document } of documents) {
2974
+ validateDocument(document, schema, options?.patternValueLimit);
2975
+ if (options?.strict) validateDocumentStrict(document, schema);
2976
+ }
2977
+ }
2978
+ var SurfaceCounts = class {
2979
+ ids = /* @__PURE__ */ new Map();
2980
+ surfaces = [];
2981
+ tokens = [];
2982
+ counts = new GrowableUint32(SEGMENT_TOKEN_TABLE_START);
2983
+ count(surface, token) {
2984
+ if (surface === void 0 || surface.length === 0) return;
2985
+ const known = this.ids.get(surface);
2986
+ if (known !== void 0) {
2987
+ this.counts.values[known]++;
2988
+ return;
2989
+ }
2990
+ const id = this.surfaces.length;
2991
+ this.ids.set(surface, id);
2992
+ this.surfaces.push(surface);
2993
+ this.tokens.push(token);
2994
+ this.counts.ensureIndex(id);
2995
+ this.counts.values[id] = 1;
2996
+ }
2997
+ encode() {
2998
+ if (this.surfaces.length === 0) return null;
2999
+ const forms = /* @__PURE__ */ Object.create(null);
3000
+ for (let s = 0; s < this.surfaces.length; s++) forms[this.surfaces[s]] = [this.counts.values[s], this.tokens[s]];
3001
+ return forms;
3002
+ }
3003
+ };
3004
+ var FieldLengths = class {
3005
+ constructor(documentCount) {
3006
+ this.documentCount = documentCount;
3007
+ }
3008
+ names = [];
3009
+ columns = [];
3010
+ indexes = /* @__PURE__ */ new Map();
3011
+ totals = [];
3012
+ record(ordinal, fieldPath, length) {
3013
+ let index = this.indexes.get(fieldPath);
3014
+ if (index === void 0) {
3015
+ index = this.names.length;
3016
+ this.names.push(fieldPath);
3017
+ this.indexes.set(fieldPath, index);
3018
+ this.columns.push(new Uint32Array(this.documentCount));
3019
+ this.totals.push(0);
3020
+ }
3021
+ this.columns[index][ordinal] = length;
3022
+ this.totals[index] += length;
3023
+ }
3024
+ totalsByName() {
3025
+ const totals = {};
3026
+ for (let f = 0; f < this.names.length; f++) totals[this.names[f]] = this.totals[f];
3027
+ return totals;
3028
+ }
3029
+ };
3030
+ function buildSegmentPayload(documents, schema, language, options, trackPositions) {
3031
+ refuseRepeatedDocIds(documents);
3032
+ validateAsAnInsertDoes(documents, schema, options);
3033
+ const fields = schemaFieldsOf(schema);
3034
+ const tokenizing = tokenizeOptions(options);
3035
+ const postings = new TextPostings(trackPositions);
3036
+ const surfaces = new SurfaceCounts();
3037
+ const lengths = new FieldLengths(documents.length);
3038
+ const filters = {
3039
+ numericIndexes: /* @__PURE__ */ new Map(),
3040
+ booleanIndexes: /* @__PURE__ */ new Map(),
3041
+ enumIndexes: /* @__PURE__ */ new Map(),
3042
+ geoIndexes: /* @__PURE__ */ new Map(),
3043
+ patternIndexes: /* @__PURE__ */ new Map(),
3044
+ sizeIndexes: /* @__PURE__ */ new Map()
3045
+ };
3046
+ const partialFields = partialFieldsOf(schema);
3047
+ const spellings = /* @__PURE__ */ new Map();
3048
+ let fieldSpellings = null;
3049
+ let positionOffset = 0;
3050
+ const addToken = (token, position, surface) => {
3051
+ postings.addToken(token, positionOffset + position);
3052
+ surfaces.count(surface, token);
3053
+ if (fieldSpellings !== null) countPartialSpelling(fieldSpellings, token, surface ?? token, 1);
3054
+ };
3055
+ for (let ordinal = 0; ordinal < documents.length; ordinal++) {
3056
+ const document = documents[ordinal].document;
3057
+ for (const { path: fieldPath, base: fieldType, pattern, partial } of fields) {
3058
+ const value = getNestedValue(document, fieldPath);
3059
+ if (value === void 0 || value === null) continue;
3060
+ recordValueSize(filters.sizeIndexes, ordinal, fieldPath, fieldType, value);
3061
+ if (pattern) patternIndexOf(filters.patternIndexes, fieldPath).add(ordinal, value);
3062
+ if (!isWordIndexedBase(fieldType)) {
3063
+ indexFilterValue(filters, ordinal, fieldPath, fieldType, value);
3064
+ continue;
3065
+ }
3066
+ fieldSpellings = partial ? spellings : null;
3067
+ postings.beginField();
3068
+ positionOffset = 0;
3069
+ for (const text of fieldType === "string" ? [value] : value) {
3070
+ visitTokens(text, language, tokenizing, addToken);
3071
+ positionOffset = postings.fieldLength;
3072
+ }
3073
+ postings.endField(ordinal, fieldPath);
3074
+ lengths.record(ordinal, fieldPath, postings.fieldLength);
3075
+ }
3076
+ }
3077
+ const identity = new Int32Array(documents.length);
3078
+ for (let ordinal = 0; ordinal < identity.length; ordinal++) identity[ordinal] = ordinal;
3079
+ return {
3080
+ documentCount: documents.length,
3081
+ docIds: documents.map((entry) => entry.docId),
3082
+ ...postings.encode(),
3083
+ fieldLengthNames: lengths.names,
3084
+ fieldLengthColumns: lengths.columns,
3085
+ totalFieldLengths: lengths.totalsByName(),
3086
+ surfaceForms: surfaces.encode(),
3087
+ partialSpellings: partialFields.length === 0 ? null : encodePartialSpellingsOf(partialFields, spellings),
3088
+ ...encodeFieldIndexes(filters, identity)
3089
+ };
3090
+ }
3091
+
3092
+ // src/workers/segment-actions.ts
3093
+ function isSegmentAction(action) {
3094
+ return action.type === "buildSegment" || action.type === "mergeSegments" || action.type === "attachSegments" || action.type === "compactSegments" || action.type === "swapSegments" || action.type === "freezeLiveTail";
3095
+ }
3096
+ function growPartitionsTo(manager, partitionId) {
3097
+ while (manager.partitionCount <= partitionId) {
3098
+ manager.addPartition();
3099
+ }
3100
+ }
3101
+ function requireComposite(entry, indexName, partitionId, verb) {
3102
+ const partition = entry.manager.getPartition(partitionId);
3103
+ if (!isCompositePartition(partition)) {
3104
+ throw new NarsilError(
3105
+ ErrorCodes.PARTITION_CORRUPTED,
3106
+ `Partition ${partitionId} of "${indexName}" holds no frozen segments to ${verb}`,
3107
+ { indexName, partitionId }
3108
+ );
3109
+ }
3110
+ return partition;
3111
+ }
3112
+ function runSegmentAction(entry, action) {
3113
+ switch (action.type) {
3114
+ case "buildSegment": {
3115
+ const options = resolvePartitionInsertOptions(entry.config, entry.manager.analysis, action.options);
3116
+ const payload = buildSegmentPayload(
3117
+ action.documents,
3118
+ entry.config.schema,
3119
+ entry.language,
3120
+ options,
3121
+ entry.config.trackPositions ?? true
3122
+ );
3123
+ const snapshot = freezeSegmentShared(
3124
+ payload,
3125
+ action.documents.map((doc) => doc.document),
3126
+ action.segmentId
3127
+ );
3128
+ const built = snapshot === null ? { kind: "payload", payload } : { kind: "shared", snapshot };
3129
+ return built;
3130
+ }
3131
+ case "mergeSegments": {
3132
+ for (const segment of action.segments) {
3133
+ if (segment.payload.docIds.some((docId) => entry.manager.has(docId))) {
3134
+ console.warn(
3135
+ `Skipping replicated segment for index "${action.indexName}": its documents already exist on this copy`
3136
+ );
3137
+ continue;
3138
+ }
3139
+ entry.manager.mergeSegment(segment.partitionId, segment.payload, segment.documents);
3140
+ }
3141
+ return void 0;
3142
+ }
3143
+ case "attachSegments": {
3144
+ for (const segment of action.segments) {
3145
+ growPartitionsTo(entry.manager, segment.partitionId);
3146
+ const surfaceForms = segment.survivingSurfaceForms;
3147
+ const frozen = createSharedFrozenSegment(segment.snapshot, surfaceForms);
3148
+ const spellings = surfaceForms === void 0 ? void 0 : NO_REMOVED_SPELLINGS;
3149
+ for (const docId of segment.tombstonedDocIds ?? []) frozen.tombstoneDocument(docId, spellings);
3150
+ entry.manager.attachFrozenSegment(segment.partitionId, frozen);
3151
+ }
3152
+ return void 0;
3153
+ }
3154
+ case "compactSegments": {
3155
+ const partition = requireComposite(entry, action.indexName, action.partitionId, "compact");
3156
+ return mergeFrozenSegments(
3157
+ partition.frozenSegmentsById(action.segmentIds),
3158
+ entry.manager.partialWordAnalysis,
3159
+ entry.manager.textAnalysis
3160
+ );
3161
+ }
3162
+ case "swapSegments": {
3163
+ requireComposite(entry, action.indexName, action.partitionId, "swap");
3164
+ entry.manager.swapFrozenSegments(
3165
+ action.partitionId,
3166
+ action.dropSegmentIds,
3167
+ createSharedFrozenSegment(action.snapshot),
3168
+ action.removedSpellings
3169
+ );
3170
+ return void 0;
3171
+ }
3172
+ case "freezeLiveTail": {
3173
+ growPartitionsTo(entry.manager, action.partitionId);
3174
+ const tail = entry.manager.getPartition(action.partitionId);
3175
+ const held = isCompositePartition(tail) ? tail.live.count() : tail.count();
3176
+ if (held !== action.snapshot.documentCount) {
3177
+ console.warn(
3178
+ `Partition ${action.partitionId} of "${action.indexName}" held ${held} live documents where the frozen tail holds ${action.snapshot.documentCount}`
3179
+ );
3180
+ }
3181
+ entry.manager.replaceLiveTail(action.partitionId, createSharedFrozenSegment(action.snapshot));
3182
+ return void 0;
3183
+ }
3184
+ }
3185
+ }
3186
+
3187
+ // src/workers/vector-copies.ts
3188
+ function createHeldVectorCopies() {
3189
+ return { searchers: /* @__PURE__ */ new Map(), fields: /* @__PURE__ */ new Map(), builds: /* @__PURE__ */ new Map() };
3190
+ }
3191
+ function fieldUnderHandle(held, fieldName, handle) {
3192
+ const searchable = held.fields.get(fieldName);
3193
+ if (searchable?.handle === handle) return searchable;
3194
+ return held.builds.get(handle);
3195
+ }
3196
+ function loadHeldVectorCopy(held, manager, fieldName, handle, handles, threadSlot) {
3197
+ const { createSharedVectorSearcher, openSharedVectorField } = requireLoadedVectorCode();
3198
+ const existing = fieldUnderHandle(held, fieldName, handle);
3199
+ const view = existing?.view ?? openSharedVectorField(handles, threadSlot);
3200
+ if (existing !== void 0) view.adopt(handles);
3201
+ if (!handles.searchable) {
3202
+ held.builds.set(handle, { handle, view });
3203
+ return;
3204
+ }
3205
+ held.builds.delete(handle);
3206
+ held.fields.set(fieldName, { handle, view });
3207
+ if (handles.graph === null) {
3208
+ held.searchers.delete(fieldName);
3209
+ return;
3210
+ }
3211
+ held.searchers.set(
3212
+ fieldName,
3213
+ createSharedVectorSearcher({ fieldName, view, holdsDocument: (docId) => manager.has(docId) })
3214
+ );
3215
+ }
3216
+ function holdsVectorField(held, fieldName) {
3217
+ const view = held.fields.get(fieldName)?.view;
3218
+ return view?.readsEveryVector === true && !view.graphRetired;
3219
+ }
3220
+ function dropHeldVectorCopy(held, fieldName, handle) {
3221
+ held.builds.get(handle)?.view.close();
3222
+ held.builds.delete(handle);
3223
+ const field = held.fields.get(fieldName);
3224
+ if (field?.handle !== handle) return;
3225
+ field.view.close();
3226
+ held.fields.delete(fieldName);
3227
+ held.searchers.delete(fieldName);
3228
+ }
3229
+ function closeHeldVectorCopies(held) {
3230
+ for (const build of held.builds.values()) build.view.close();
3231
+ for (const field of held.fields.values()) field.view.close();
3232
+ held.builds.clear();
3233
+ held.fields.clear();
3234
+ held.searchers.clear();
3235
+ }
3236
+ function yieldToEventLoop() {
3237
+ return new Promise((resolve) => {
3238
+ if (typeof setImmediate === "function") setImmediate(resolve);
3239
+ else setTimeout(resolve, 0);
3240
+ });
3241
+ }
3242
+ async function insertIntoHeldGraph(held, fieldName, handle, ordinals) {
3243
+ const field = fieldUnderHandle(held, fieldName, handle);
3244
+ if (field === void 0) return null;
3245
+ for (const ordinal of ordinals) {
3246
+ field.view.insertOrdinal(ordinal);
3247
+ await yieldToEventLoop();
3248
+ }
3249
+ return field.view.takeOutcome();
3250
+ }
3251
+ function heldVectorOf(held, fieldName, docId) {
3252
+ return held.fields.get(fieldName)?.view.vectorOf(docId);
3253
+ }
3254
+
3255
+ // src/workers/direct-executor/index.ts
3256
+ var NO_VECTOR_WORKER_COPIES = { enabled: false };
3257
+ function createDirectExecutor(options) {
3258
+ const indexes = /* @__PURE__ */ new Map();
3259
+ const vectorWorkerCopies = options?.vectorWorkerCopies ?? NO_VECTOR_WORKER_COPIES;
3260
+ const threadSlot = options?.threadSlot ?? 0;
3261
+ const vectorStorage = options?.vectorStorage ?? "memory";
3262
+ function requireIndex(indexName) {
3263
+ const entry = indexes.get(indexName);
3264
+ if (!entry) {
3265
+ throw new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" does not exist`, {
3266
+ indexName
3267
+ });
3268
+ }
3269
+ return entry;
3270
+ }
3271
+ function vectorIndexesFor(indexName, config, copies = vectorWorkerCopies) {
3272
+ const vectorIndexes = /* @__PURE__ */ new Map();
3273
+ const vectorFields = extractVectorFieldsFromSchema(config.schema);
3274
+ if (vectorFields.size === 0) return vectorIndexes;
3275
+ const { createVectorIndex } = requireLoadedVectorCode();
3276
+ const promotion = config.vectorPromotion;
3277
+ const storage = promotion?.storage ?? vectorStorage;
3278
+ const repaired = () => options?.onVectorGraphRepaired?.(indexName);
3279
+ for (const [fieldPath, dim] of vectorFields) {
3280
+ const index = createVectorIndex(fieldPath, dim, promotion, copies, indexName, storage, repaired);
3281
+ vectorIndexes.set(fieldPath, index);
3282
+ }
3283
+ return vectorIndexes;
3284
+ }
3285
+ function createIndex(indexName, config, language, analysisStale = false, synonyms = null) {
3286
+ addIndex(indexName, config, language, analysisStale, null).synonyms = synonyms;
3287
+ }
3288
+ function lookupOf(config, language, encoded) {
3289
+ return encoded === void 0 ? null : synonymModule().storedLookup(encoded, config, language);
3290
+ }
3291
+ function addIndex(indexName, config, language, analysisStale, awaitedCopyLoad) {
3292
+ if (indexes.has(indexName)) {
3293
+ throw new NarsilError(ErrorCodes.INDEX_ALREADY_EXISTS, `Index "${indexName}" already exists`, {
3294
+ indexName
3295
+ });
3296
+ }
3297
+ const entry = buildEntry(indexName, config, language, analysisStale, awaitedCopyLoad, vectorWorkerCopies);
3298
+ indexes.set(indexName, entry);
3299
+ return entry.manager;
3300
+ }
3301
+ function buildEntry(indexName, config, language, analysisStale, awaitedCopyLoad, copies) {
3302
+ const router = createPartitionRouter();
3303
+ const partitionCount = config.partitions?.maxPartitions ?? 1;
3304
+ const vectorIndexes = vectorIndexesFor(indexName, config, copies);
3305
+ const manager = createPartitionManager(indexName, config, language, router, partitionCount, vectorIndexes);
3306
+ return {
3307
+ manager,
3308
+ config,
3309
+ language,
3310
+ searchOptions: {
3311
+ bm25Params: config.bm25,
3312
+ stopWords: manager.analysis.stopWords,
3313
+ customTokenizer: manager.analysis.customTokenizer,
3314
+ get synonyms() {
3315
+ return manager.synonyms;
3316
+ }
3317
+ },
3318
+ vectorIndexes,
3319
+ vectorCopies: createHeldVectorCopies(),
3320
+ analysisStale,
3321
+ awaitedCopyLoad
3322
+ };
3323
+ }
3324
+ function disposeEntry(entry) {
3325
+ for (const vectorIndex of entry.vectorIndexes.values()) {
3326
+ vectorIndex.dispose();
3327
+ }
3328
+ closeHeldVectorCopies(entry.vectorCopies);
3329
+ for (const partition of entry.manager.getAllPartitions()) {
3330
+ partition.clear();
3331
+ }
3332
+ }
3333
+ function dropIndex(indexName) {
3334
+ disposeEntry(requireIndex(indexName));
3335
+ indexes.delete(indexName);
3336
+ }
3337
+ function installDetachedIndex(detached) {
3338
+ const previous = indexes.get(detached.indexName);
3339
+ indexes.set(detached.indexName, detached.entry);
3340
+ if (previous !== void 0 && previous !== detached.entry) disposeEntry(previous);
3341
+ }
3342
+ function listIndexes() {
3343
+ return Array.from(indexes.keys());
3344
+ }
3345
+ function getManager(indexName) {
3346
+ return indexes.get(indexName)?.manager;
3347
+ }
3348
+ function queryContextOf(indexName) {
3349
+ const entry = indexes.get(indexName);
3350
+ if (entry === void 0 || entry.awaitedCopyLoad !== null) return void 0;
3351
+ return {
3352
+ manager: entry.manager,
3353
+ config: entry.config,
3354
+ language: entry.language,
3355
+ vectorSearchers: entry.vectorCopies.searchers,
3356
+ analysisStale: entry.analysisStale
3357
+ };
3358
+ }
3359
+ async function execute(action) {
3360
+ if (isSegmentAction(action)) {
3361
+ return runSegmentAction(requireIndex(action.indexName), action);
3362
+ }
3363
+ switch (action.type) {
3364
+ case "bootstrap": {
3365
+ throw new NarsilError(
3366
+ ErrorCodes.CONFIG_INVALID,
3367
+ "A bootstrap module loads inside a worker, and the thread that owns this executor imports it directly",
3368
+ { moduleUrl: action.moduleUrl }
3369
+ );
3370
+ }
3371
+ case "serveRequests":
3372
+ case "stopServing": {
3373
+ throw new NarsilError(
3374
+ ErrorCodes.CONFIG_INVALID,
3375
+ "Only a worker thread serves requests, and the thread that owns this executor receives them itself"
3376
+ );
3377
+ }
3378
+ case "createIndex": {
3379
+ const loading = loadVectorCodeFor(action.config.schema);
3380
+ if (loading !== null) await loading;
3381
+ const language = getLanguage(action.config.language ?? "english");
3382
+ const manager = addIndex(
3383
+ action.indexName,
3384
+ action.config,
3385
+ language,
3386
+ action.analysisStale ?? false,
3387
+ action.copyLoad ?? null
3388
+ );
3389
+ try {
3390
+ manager.synonyms = lookupOf(action.config, language, action.synonyms);
3391
+ } catch (err) {
3392
+ dropIndex(action.indexName);
3393
+ throw err;
3394
+ }
3395
+ return void 0;
3396
+ }
3397
+ case "copyLoaded": {
3398
+ const entry = indexes.get(action.indexName);
3399
+ if (entry?.awaitedCopyLoad === action.copyLoad) entry.awaitedCopyLoad = null;
3400
+ return void 0;
3401
+ }
3402
+ case "dropIndex": {
3403
+ dropIndex(action.indexName);
3404
+ return void 0;
3405
+ }
3406
+ case "loadVectorCopy": {
3407
+ if (loadedVectorCode() === null) await loadVectorCode();
3408
+ const entry = requireIndex(action.indexName);
3409
+ loadHeldVectorCopy(
3410
+ entry.vectorCopies,
3411
+ entry.manager,
3412
+ action.fieldName,
3413
+ action.handle,
3414
+ action.handles,
3415
+ threadSlot
3416
+ );
3417
+ return void 0;
3418
+ }
3419
+ case "dropVectorCopy": {
3420
+ const entry = indexes.get(action.indexName);
3421
+ if (entry !== void 0) dropHeldVectorCopy(entry.vectorCopies, action.fieldName, action.handle);
3422
+ return void 0;
3423
+ }
3424
+ case "insertVectorOrdinals": {
3425
+ const entry = requireIndex(action.indexName);
3426
+ return await insertIntoHeldGraph(entry.vectorCopies, action.fieldName, action.handle, action.ordinals);
3427
+ }
3428
+ case "insert": {
3429
+ const entry = requireIndex(action.indexName);
3430
+ entry.manager.insert(action.docId, action.document, action.skipClone ? { skipClone: true } : void 0);
3431
+ return void 0;
3432
+ }
3433
+ case "remove": {
3434
+ const entry = requireIndex(action.indexName);
3435
+ entry.manager.remove(action.docId, action.removedSpellings);
3436
+ return void 0;
3437
+ }
3438
+ case "update": {
3439
+ const entry = requireIndex(action.indexName);
3440
+ entry.manager.update(action.docId, action.document, void 0, action.removedSpellings);
3441
+ return void 0;
3442
+ }
3443
+ case "query":
3444
+ case "preflight":
3445
+ return await runQueryAction(requireIndex(action.indexName), action);
3446
+ case "replaceSynonyms": {
3447
+ const entry = indexes.get(action.indexName);
3448
+ if (entry !== void 0) entry.manager.synonyms = lookupOf(entry.config, entry.language, action.synonyms);
3449
+ return void 0;
3450
+ }
3451
+ case "get": {
3452
+ const entry = requireIndex(action.indexName);
3453
+ return entry.manager.get(action.docId);
3454
+ }
3455
+ case "has": {
3456
+ const entry = requireIndex(action.indexName);
3457
+ return entry.manager.has(action.docId);
3458
+ }
3459
+ case "count": {
3460
+ const entry = requireIndex(action.indexName);
3461
+ return entry.manager.countDocuments();
3462
+ }
3463
+ case "getStats": {
3464
+ const entry = requireIndex(action.indexName);
3465
+ return {
3466
+ documentCount: entry.manager.countDocuments(),
3467
+ partitionCount: entry.manager.partitionCount,
3468
+ language: entry.language.name,
3469
+ schema: entry.config.schema
3470
+ };
3471
+ }
3472
+ case "clear": {
3473
+ const entry = requireIndex(action.indexName);
3474
+ const partitions = entry.manager.getAllPartitions();
3475
+ for (const partition of partitions) {
3476
+ partition.clear();
3477
+ }
3478
+ entry.manager.setPartitions(partitions);
3479
+ entry.manager.resetVectorIndexes(vectorIndexesFor(action.indexName, entry.config));
3480
+ entry.vectorIndexes = entry.manager.getVectorIndexes();
3481
+ entry.vectorCopies = createHeldVectorCopies();
3482
+ return void 0;
3483
+ }
3484
+ case "serialize": {
3485
+ const entry = requireIndex(action.indexName);
3486
+ return entry.manager.serializePartition(action.partitionId);
3487
+ }
3488
+ case "deserialize": {
3489
+ const entry = requireIndex(action.indexName);
3490
+ growPartitionsTo(entry.manager, action.partitionId);
3491
+ entry.manager.deserializePartition(action.partitionId, action.data);
3492
+ return void 0;
3493
+ }
3494
+ case "memoryReport": {
3495
+ const usage = typeof process !== "undefined" && process.memoryUsage ? process.memoryUsage() : {};
3496
+ const heap = readHeapStatistics();
3497
+ const heapLimit = heap === null ? null : heap.configuredLimitBytes ?? heap.limitBytes;
3498
+ return { ...usage, heapLimit };
3499
+ }
3500
+ case "shutdown": {
3501
+ for (const [, entry] of indexes) {
3502
+ for (const partition of entry.manager.getAllPartitions()) {
3503
+ partition.clear();
3504
+ }
3505
+ }
3506
+ indexes.clear();
3507
+ return void 0;
3508
+ }
3509
+ }
3510
+ }
3511
+ async function shutdown() {
3512
+ await execute({ type: "shutdown", requestId: "internal-shutdown" });
3513
+ }
3514
+ return {
3515
+ execute,
3516
+ shutdown,
3517
+ getManager,
3518
+ queryContextOf,
3519
+ holdsVectorField: (indexName, fieldName) => {
3520
+ const entry = indexes.get(indexName);
3521
+ return entry !== void 0 && holdsVectorField(entry.vectorCopies, fieldName);
3522
+ },
3523
+ heldVectorOf: (indexName, fieldName, docId) => {
3524
+ const entry = indexes.get(indexName);
3525
+ return entry === void 0 ? void 0 : heldVectorOf(entry.vectorCopies, fieldName, docId);
3526
+ },
3527
+ createIndex,
3528
+ dropIndex,
3529
+ listIndexes,
3530
+ vectorWorkerCopies,
3531
+ createDetachedEntry: (indexName, config, language, copies) => buildEntry(indexName, config, language, false, null, copies),
3532
+ installDetachedIndex,
3533
+ discardDetachedIndex: (detached) => disposeEntry(detached.entry)
3534
+ };
3535
+ }
3536
+
3537
+ export { availableSynonymModule, buildSegmentPayload, createDirectExecutor, createFrozenSegment, createPartitionManager, createPartitionRouter, createSharedFrozenSegment, decodeEveryStoredDocument, decodeMapWithoutFields, decodeStoredDocument, freezeSegmentShared, isEmptySynonymSet, mergeFrozenSegments, mergeSegmentContents, readResidentMemory, registerSynonyms, resolvePartitionInsertOptions, segmentArraysOf, selectionKeeps, shareSegmentArrays, synonymModule };