@delali/narsil 0.3.1-next.71 → 0.3.1-next.73

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 (69) hide show
  1. package/README.md +2 -2
  2. package/dist/adapters/filesystem.mjs +1 -1
  3. package/dist/adapters/indexeddb.mjs +1 -1
  4. package/dist/{capabilities-Dt-7B4Wz.d.ts → capabilities-CYeAWYxQ.d.ts} +19 -4
  5. package/dist/{chunk-3JGWULDF.mjs → chunk-2WLD2NFW.mjs} +5 -5
  6. package/dist/{chunk-LMO5TJUS.mjs → chunk-3YKWSO54.mjs} +642 -196
  7. package/dist/{chunk-HWQLTLNG.mjs → chunk-4MQ5F4IL.mjs} +1 -1
  8. package/dist/{chunk-FVLQ63B7.mjs → chunk-AFKH4ZK3.mjs} +30 -6
  9. package/dist/{chunk-653BNMAF.mjs → chunk-AK3BLGCI.mjs} +195 -25
  10. package/dist/{chunk-IOR2K26O.mjs → chunk-CVFAFZSN.mjs} +8000 -4391
  11. package/dist/{chunk-USKFU7J5.mjs → chunk-I64LXKO3.mjs} +1 -1
  12. package/dist/{chunk-OI42GWLR.mjs → chunk-LJYTW7GT.mjs} +57 -11
  13. package/dist/{chunk-2STVUTNA.mjs → chunk-LPBWCHBE.mjs} +194 -13
  14. package/dist/{chunk-YNVIUGF5.mjs → chunk-PU43WDY4.mjs} +4 -2
  15. package/dist/{chunk-M76V3OGJ.mjs → chunk-THMSFOJO.mjs} +1289 -1405
  16. package/dist/{chunk-YJDQ7NLA.mjs → chunk-WVDFG45M.mjs} +93 -95
  17. package/dist/chunk-XW25X6AU.mjs +614 -0
  18. package/dist/chunk-ZKRV5M2P.mjs +75 -0
  19. package/dist/{chunk-P4DSQ6M4.mjs → chunk-ZULSEZCE.mjs} +68 -28
  20. package/dist/{client-COek5GXK.d.ts → client-D698E5ge.d.ts} +77 -26
  21. package/dist/client.d.ts +9 -9
  22. package/dist/client.mjs +45 -22
  23. package/dist/{config-rBpbgTw8.d.ts → config-Bv8etwUZ.d.ts} +11 -4
  24. package/dist/{config-Dfanyqqt.d.ts → config-QXU3HMpq.d.ts} +26 -2
  25. package/dist/direct-executor-5SRDFZAS.mjs +13 -0
  26. package/dist/distribution/coordinator/etcd.d.ts +29 -16
  27. package/dist/distribution/coordinator/etcd.mjs +163 -76
  28. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  29. package/dist/distribution/coordinator/in-memory.mjs +1 -1
  30. package/dist/distribution/transport/grpc.d.ts +2 -2
  31. package/dist/distribution/transport/grpc.mjs +22 -2
  32. package/dist/distribution/transport/in-memory.d.ts +2 -2
  33. package/dist/distribution/transport/in-memory.mjs +6 -3
  34. package/dist/distribution/transport/tcp.d.ts +2 -2
  35. package/dist/distribution/transport/tcp.mjs +40 -16
  36. package/dist/distribution.d.ts +9 -9
  37. package/dist/distribution.mjs +682 -388
  38. package/dist/embeddings/openai.mjs +1 -1
  39. package/dist/{engine-igk0aMpQ.d.ts → engine-D_OzKZ-6.d.ts} +97 -188
  40. package/dist/index.browser.mjs +11671 -7427
  41. package/dist/index.d.ts +27 -8
  42. package/dist/index.mjs +13 -14
  43. package/dist/invalidation/filesystem.mjs +1 -1
  44. package/dist/{memory-C7UMCMH3.d.ts → memory-B4jMmygw.d.ts} +279 -27
  45. package/dist/pattern-Cledccbl.d.ts +18 -0
  46. package/dist/pattern.d.ts +22 -0
  47. package/dist/pattern.mjs +7 -0
  48. package/dist/persistence/durability/checkpoint-worker.d.ts +41 -17
  49. package/dist/persistence/durability/checkpoint-worker.mjs +24 -16
  50. package/dist/react.d.ts +7 -7
  51. package/dist/react.mjs +1 -1
  52. package/dist/{schema-Bgzr4g7b.d.ts → schema-BBOCdhND.d.ts} +9 -7
  53. package/dist/server.d.ts +11 -11
  54. package/dist/server.mjs +213 -34
  55. package/dist/{tasks-BZt_zes6.d.ts → tasks-DpgCpW1x.d.ts} +3 -3
  56. package/dist/{thread-JEVAYETR.mjs → thread-3LNVA43V.mjs} +94 -27
  57. package/dist/{types-BQDlIvfS.d.ts → types-B3b68nfF.d.ts} +1 -1
  58. package/dist/{types-gOUqhs2F.d.ts → types-BiUmaiWT.d.ts} +1 -1
  59. package/dist/{types-D5OwhtsQ.d.ts → types-Cb9kiY5P.d.ts} +1 -1
  60. package/dist/vector/search-worker.d.ts +1 -1
  61. package/dist/vector/search-worker.mjs +4 -4
  62. package/dist/workers/entry.mjs +4 -4
  63. package/package.json +15 -7
  64. package/dist/chunk-3L4U6EJQ.mjs +0 -49
  65. package/dist/chunk-GIVAGD3Q.mjs +0 -25
  66. package/dist/chunk-IFBKLBGB.mjs +0 -7
  67. package/dist/direct-executor-GYJEKNV5.mjs +0 -12
  68. package/dist/serialization/crc32-worker.d.ts +0 -20
  69. package/dist/serialization/crc32-worker.mjs +0 -62
@@ -0,0 +1,614 @@
1
+ import { bitsetHas, bitsetSet, PATTERN_RUN_CODE_POINTS } from './chunk-ZKRV5M2P.mjs';
2
+
3
+ // src/pattern/constants.ts
4
+ var SHORTLIST_RUN_LOOKUPS = 16;
5
+ var SHORTLIST_INTERSECT_RATIO = 8;
6
+ var RUN_LIST_INITIAL_CAPACITY = 4;
7
+ var RUN_LIST_COMPACTION_FRACTION = 0.5;
8
+ var RUN_ENTRY_OVERHEAD_BYTES = 140;
9
+ var DOCUMENT_ENTRY_BYTES = 24;
10
+
11
+ // src/pattern/literal-matcher.ts
12
+ function bordersOf(codePoints) {
13
+ const length = codePoints.length;
14
+ const borders = new Uint32Array(length + 1);
15
+ let border = 0;
16
+ for (let at = 1; at < length; at++) {
17
+ while (border > 0 && codePoints[at] !== codePoints[border]) border = borders[border];
18
+ if (codePoints[at] === codePoints[border]) border++;
19
+ borders[at + 1] = border;
20
+ }
21
+ return borders;
22
+ }
23
+ function reachablePositionsOf(borders, length) {
24
+ const reachable = new Uint32Array(length + 1);
25
+ for (let prefix = 1; prefix <= length; prefix++) {
26
+ reachable[prefix] = (prefix < length ? 1 : 0) + reachable[borders[prefix]];
27
+ }
28
+ return reachable;
29
+ }
30
+ var LiteralMatcher = class {
31
+ constructor(codePoints, anchoredStart, anchoredEnd) {
32
+ this.codePoints = codePoints;
33
+ this.anchoredStart = anchoredStart;
34
+ this.anchoredEnd = anchoredEnd;
35
+ this.borders = bordersOf(codePoints);
36
+ this.reachable = reachablePositionsOf(this.borders, codePoints.length);
37
+ }
38
+ borders;
39
+ reachable;
40
+ prefix = 0;
41
+ alive = true;
42
+ accepting = false;
43
+ decided = null;
44
+ begin() {
45
+ this.decided = null;
46
+ this.prefix = 0;
47
+ this.alive = this.codePoints.length > 0;
48
+ this.accepting = this.codePoints.length === 0;
49
+ if (!this.anchoredEnd && this.accepting) return this.decide(true);
50
+ return true;
51
+ }
52
+ feed(codePoint, tally) {
53
+ const steps = this.steps();
54
+ if (steps === 0) return this.decide(!this.anchoredStart && this.codePoints.length === 0);
55
+ tally.add(steps);
56
+ if (this.anchoredStart) this.advanceAnchored(codePoint);
57
+ else this.advance(codePoint);
58
+ if (!this.anchoredEnd && this.accepting) return this.decide(true);
59
+ return true;
60
+ }
61
+ matched() {
62
+ return this.decided ?? (this.anchoredEnd && this.accepting);
63
+ }
64
+ steps() {
65
+ const length = this.codePoints.length;
66
+ if (length === 0) return 0;
67
+ if (this.anchoredStart) return this.alive && this.prefix < length ? 1 : 0;
68
+ return 1 + this.reachable[this.prefix];
69
+ }
70
+ advanceAnchored(codePoint) {
71
+ if (this.codePoints[this.prefix] === codePoint) {
72
+ this.prefix++;
73
+ this.accepting = this.prefix === this.codePoints.length;
74
+ return;
75
+ }
76
+ this.alive = false;
77
+ this.accepting = false;
78
+ }
79
+ advance(codePoint) {
80
+ const { codePoints, borders } = this;
81
+ let prefix = this.prefix === codePoints.length ? borders[this.prefix] : this.prefix;
82
+ while (prefix > 0 && codePoints[prefix] !== codePoint) prefix = borders[prefix];
83
+ if (codePoints[prefix] === codePoint) prefix++;
84
+ this.prefix = prefix;
85
+ this.accepting = prefix === codePoints.length;
86
+ }
87
+ decide(outcome) {
88
+ this.decided = outcome;
89
+ return false;
90
+ }
91
+ };
92
+
93
+ // src/pattern/text.ts
94
+ var BASIC_PLANE_LAST = 65535;
95
+ function appendTextCodePoints(text, caseFold, host, out) {
96
+ for (let at = 0; at < text.length; at++) {
97
+ const codePoint = text.codePointAt(at) ?? 0;
98
+ if (codePoint > BASIC_PLANE_LAST) at++;
99
+ if (caseFold) host.appendFoldedCodePoint(codePoint, out);
100
+ else out.push(codePoint);
101
+ }
102
+ }
103
+ function runKey(first, second, third) {
104
+ return String.fromCodePoint(first, second, third);
105
+ }
106
+ function runKeyCodePoints(key, out, offset) {
107
+ let slot = offset;
108
+ for (let at = 0; at < key.length && slot < offset + PATTERN_RUN_CODE_POINTS; at++) {
109
+ const codePoint = key.codePointAt(at) ?? 0;
110
+ out[slot++] = codePoint;
111
+ if (codePoint > BASIC_PLANE_LAST) at++;
112
+ }
113
+ }
114
+ function compareRuns(left, leftAt, right, rightAt) {
115
+ for (let offset = 0; offset < PATTERN_RUN_CODE_POINTS; offset++) {
116
+ const difference = left[leftAt + offset] - right[rightAt + offset];
117
+ if (difference !== 0) return difference;
118
+ }
119
+ return 0;
120
+ }
121
+ function collectRunKeys(foldedCodePoints, out) {
122
+ for (let at = 0; at + PATTERN_RUN_CODE_POINTS <= foldedCodePoints.length; at++) {
123
+ out.add(runKey(foldedCodePoints[at], foldedCodePoints[at + 1], foldedCodePoints[at + 2]));
124
+ }
125
+ }
126
+ function forEachTextValue(value, visit) {
127
+ if (typeof value === "string") {
128
+ visit(value);
129
+ return;
130
+ }
131
+ if (!Array.isArray(value)) return;
132
+ for (const element of value) {
133
+ if (typeof element === "string") visit(element);
134
+ }
135
+ }
136
+ function holdsTextValue(value) {
137
+ return typeof value === "string" || Array.isArray(value);
138
+ }
139
+
140
+ // src/pattern/work-tally.ts
141
+ var WorkTally = class {
142
+ constructor(meter) {
143
+ this.meter = meter;
144
+ this.headroom = meter.remaining();
145
+ }
146
+ pending = 0;
147
+ headroom;
148
+ add(units) {
149
+ this.pending += units;
150
+ if (this.pending >= this.meter.flushUnits || this.pending > this.headroom) this.flush();
151
+ }
152
+ flush() {
153
+ if (this.pending === 0) return;
154
+ const units = this.pending;
155
+ this.pending = 0;
156
+ this.meter.add(units);
157
+ this.headroom = this.meter.remaining();
158
+ }
159
+ };
160
+
161
+ // src/pattern/evaluate.ts
162
+ var ANCHORED_START = /* @__PURE__ */ new Set(["eq", "startsWith"]);
163
+ var ANCHORED_END = /* @__PURE__ */ new Set(["eq", "endsWith"]);
164
+ var shortlistRunsBySearch = /* @__PURE__ */ new WeakMap();
165
+ function spreadRuns(folded) {
166
+ const runCount = folded.length - PATTERN_RUN_CODE_POINTS + 1;
167
+ if (runCount <= 0) return new Uint32Array(0);
168
+ const picks = Math.min(runCount, SHORTLIST_RUN_LOOKUPS);
169
+ const seen = /* @__PURE__ */ new Set();
170
+ const runs = [];
171
+ for (let pick = 0; pick < picks; pick++) {
172
+ const at = picks === 1 ? 0 : Math.round(pick * (runCount - 1) / (picks - 1));
173
+ const key = runKey(folded[at], folded[at + 1], folded[at + 2]);
174
+ if (seen.has(key)) continue;
175
+ seen.add(key);
176
+ runs.push(folded[at], folded[at + 1], folded[at + 2]);
177
+ }
178
+ return Uint32Array.from(runs);
179
+ }
180
+ function shortlistRuns(text, host, meter) {
181
+ let runsByText = shortlistRunsBySearch.get(meter);
182
+ if (runsByText === void 0) {
183
+ runsByText = /* @__PURE__ */ new Map();
184
+ shortlistRunsBySearch.set(meter, runsByText);
185
+ }
186
+ const known = runsByText.get(text);
187
+ if (known !== void 0) return known;
188
+ const folded = [];
189
+ appendTextCodePoints(text, true, host, folded);
190
+ const runs = spreadRuns(folded);
191
+ runsByText.set(text, runs);
192
+ return runs;
193
+ }
194
+ function intersect(candidates, list, tally) {
195
+ const kept = new Uint32Array(Math.min(candidates.length, list.length));
196
+ let keptCount = 0;
197
+ let read = 0;
198
+ for (const candidate of candidates) {
199
+ while (read < list.length && list[read] < candidate) read++;
200
+ if (read === list.length) break;
201
+ if (list[read] === candidate) kept[keptCount++] = candidate;
202
+ }
203
+ tally.add(Math.min(list.length, read + 1));
204
+ return kept.subarray(0, keptCount);
205
+ }
206
+ function shortlist(index, runs, tally) {
207
+ if (runs.length === 0) return null;
208
+ const lists = [];
209
+ for (let at = 0; at < runs.length; at += PATTERN_RUN_CODE_POINTS) {
210
+ tally.add(1);
211
+ lists.push(index.postings(runs[at], runs[at + 1], runs[at + 2]));
212
+ }
213
+ lists.sort((left, right) => left.length - right.length);
214
+ let candidates = lists[0];
215
+ tally.add(candidates.length);
216
+ for (let at = 1; at < lists.length && candidates.length > 0; at++) {
217
+ if (lists[at].length > candidates.length * SHORTLIST_INTERSECT_RATIO) break;
218
+ candidates = intersect(candidates, lists[at], tally);
219
+ }
220
+ return candidates;
221
+ }
222
+ function matcherFor(request) {
223
+ const { operator, text, caseFold, host } = request;
224
+ const testCodePoints = [];
225
+ appendTextCodePoints(text, caseFold, host, testCodePoints);
226
+ return new LiteralMatcher(Uint32Array.from(testCodePoints), ANCHORED_START.has(operator), ANCHORED_END.has(operator));
227
+ }
228
+ function matchesText(matcher, text, caseFold, host, folded, tally) {
229
+ if (!matcher.begin()) return matcher.matched();
230
+ for (let at = 0; at < text.length; at++) {
231
+ const codePoint = text.codePointAt(at) ?? 0;
232
+ if (codePoint > BASIC_PLANE_LAST) at++;
233
+ if (!caseFold) {
234
+ if (!matcher.feed(codePoint, tally)) return matcher.matched();
235
+ continue;
236
+ }
237
+ folded.length = 0;
238
+ host.appendFoldedCodePoint(codePoint, folded);
239
+ for (const foldedCodePoint of folded) {
240
+ if (!matcher.feed(foldedCodePoint, tally)) return matcher.matched();
241
+ }
242
+ }
243
+ return matcher.matched();
244
+ }
245
+ function matchPattern(request) {
246
+ const { text, caseFold, index, matches, host } = request;
247
+ const tally = new WorkTally(request.meter);
248
+ const shortlisted = shortlist(index, shortlistRuns(text, host, request.meter), tally);
249
+ const scanning = shortlisted === null;
250
+ const candidates = shortlisted ?? index.documents();
251
+ const folded = [];
252
+ let matcher = null;
253
+ let matched = false;
254
+ const testValue = (value) => {
255
+ if (!matched && matcher !== null) matched = matchesText(matcher, value, caseFold, host, folded, tally);
256
+ };
257
+ for (const ordinal of candidates) {
258
+ if (ordinal >>> 5 >= matches.length || bitsetHas(matches, ordinal)) continue;
259
+ if (scanning) tally.add(1);
260
+ matcher ??= matcherFor(request);
261
+ matched = false;
262
+ forEachTextValue(request.valueOf(ordinal), testValue);
263
+ if (matched) bitsetSet(matches, ordinal);
264
+ }
265
+ tally.flush();
266
+ }
267
+
268
+ // src/core/partition/growable-uint32.ts
269
+ var GrowableUint32 = class {
270
+ values;
271
+ length = 0;
272
+ constructor(capacity) {
273
+ this.values = new Uint32Array(capacity);
274
+ }
275
+ growTo(needed, kept) {
276
+ let capacity = this.values.length * 2;
277
+ while (capacity < needed) capacity *= 2;
278
+ const next = new Uint32Array(capacity);
279
+ next.set(this.values.subarray(0, kept));
280
+ this.values = next;
281
+ }
282
+ reserve(extra) {
283
+ const needed = this.length + extra;
284
+ if (needed > this.values.length) this.growTo(needed, this.length);
285
+ }
286
+ push(value) {
287
+ if (this.length === this.values.length) this.growTo(this.length + 1, this.length);
288
+ this.values[this.length++] = value;
289
+ }
290
+ ensureIndex(index) {
291
+ if (index >= this.values.length) this.growTo(index + 1, this.values.length);
292
+ }
293
+ clear() {
294
+ this.length = 0;
295
+ }
296
+ };
297
+
298
+ // src/pattern/runs-arrays.ts
299
+ var EMPTY_POSTINGS = new Uint32Array(0);
300
+ function remapAscending(source, remap, out) {
301
+ const start = out.length;
302
+ let ordered = true;
303
+ for (const ordinal of source) {
304
+ const mapped = remap === null ? ordinal : ordinal < remap.length ? remap[ordinal] : -1;
305
+ if (mapped < 0) continue;
306
+ if (out.length > start && out.values[out.length - 1] > mapped) ordered = false;
307
+ out.push(mapped);
308
+ }
309
+ if (!ordered) out.values.subarray(start, out.length).sort();
310
+ }
311
+ function sortedRunOrder(codePoints, count) {
312
+ const order = new Array(count);
313
+ for (let run = 0; run < count; run++) order[run] = run;
314
+ order.sort(
315
+ (left, right) => compareRuns(codePoints, left * PATTERN_RUN_CODE_POINTS, codePoints, right * PATTERN_RUN_CODE_POINTS)
316
+ );
317
+ return order;
318
+ }
319
+ function buildArrays(fieldPath, documents, source, order, remap) {
320
+ const docIds = new GrowableUint32(Math.max(1, documents.length));
321
+ remapAscending(documents, remap, docIds);
322
+ const runs = new GrowableUint32(Math.max(1, source.count * PATTERN_RUN_CODE_POINTS));
323
+ const offsets = new GrowableUint32(source.count + 1);
324
+ const postings = new GrowableUint32(Math.max(1, source.count));
325
+ offsets.push(0);
326
+ for (let position = 0; position < source.count; position++) {
327
+ const run = order === null ? position : order[position];
328
+ const before = postings.length;
329
+ remapAscending(source.postingsAt(run), remap, postings);
330
+ if (postings.length === before) continue;
331
+ const at = run * PATTERN_RUN_CODE_POINTS;
332
+ runs.push(source.codePoints[at]);
333
+ runs.push(source.codePoints[at + 1]);
334
+ runs.push(source.codePoints[at + 2]);
335
+ offsets.push(postings.length);
336
+ }
337
+ return trimmedArrays(fieldPath, docIds, runs, offsets, postings);
338
+ }
339
+ function trimmedArrays(fieldPath, docIds, runs, offsets, postings) {
340
+ return {
341
+ fieldPath,
342
+ docIds: docIds.values.slice(0, docIds.length),
343
+ runs: runs.values.slice(0, runs.length),
344
+ offsets: offsets.values.slice(0, offsets.length),
345
+ postings: postings.values.slice(0, postings.length)
346
+ };
347
+ }
348
+ function findRun(runs, first, second, third) {
349
+ let low = 0;
350
+ let high = runs.length / PATTERN_RUN_CODE_POINTS;
351
+ while (low < high) {
352
+ const middle = low + high >>> 1;
353
+ const at = middle * PATTERN_RUN_CODE_POINTS;
354
+ const difference = runs[at] - first || runs[at + 1] - second || runs[at + 2] - third;
355
+ if (difference === 0) return middle;
356
+ if (difference < 0) low = middle + 1;
357
+ else high = middle;
358
+ }
359
+ return -1;
360
+ }
361
+
362
+ // src/pattern/frozen-index.ts
363
+ function readPatternArrays(arrays) {
364
+ const source = {
365
+ count: arrays.offsets.length - 1,
366
+ codePoints: arrays.runs,
367
+ postingsAt: (run) => arrays.postings.subarray(arrays.offsets[run], arrays.offsets[run + 1])
368
+ };
369
+ return {
370
+ documents: () => arrays.docIds,
371
+ postings(first, second, third) {
372
+ const run = findRun(arrays.runs, first, second, third);
373
+ return run < 0 ? EMPTY_POSTINGS : source.postingsAt(run);
374
+ },
375
+ toArrays(fieldPath, remap) {
376
+ return buildArrays(fieldPath, arrays.docIds, source, null, remap);
377
+ },
378
+ bytes() {
379
+ return arrays.docIds.byteLength + arrays.runs.byteLength + arrays.offsets.byteLength + arrays.postings.byteLength;
380
+ }
381
+ };
382
+ }
383
+
384
+ // src/pattern/growing-index.ts
385
+ function createRunList() {
386
+ return { ids: new Uint32Array(RUN_LIST_INITIAL_CAPACITY), length: 0, removed: null, ordered: true };
387
+ }
388
+ function compactRemoved(list) {
389
+ const removed = list.removed;
390
+ if (removed === null) return;
391
+ let kept = 0;
392
+ for (let at = 0; at < list.length; at++) {
393
+ const ordinal = list.ids[at];
394
+ if (removed.has(ordinal)) continue;
395
+ list.ids[kept++] = ordinal;
396
+ }
397
+ list.length = kept;
398
+ list.removed = null;
399
+ }
400
+ function compactAndSort(list) {
401
+ compactRemoved(list);
402
+ if (!list.ordered) {
403
+ list.ids.subarray(0, list.length).sort();
404
+ list.ordered = true;
405
+ }
406
+ }
407
+ function createGrowingPatternIndex(host) {
408
+ const runs = /* @__PURE__ */ new Map();
409
+ const documents = /* @__PURE__ */ new Set();
410
+ let documentsCache = null;
411
+ let listBytes = 0;
412
+ const folded = [];
413
+ function runKeysOf(value) {
414
+ const keys = /* @__PURE__ */ new Set();
415
+ forEachTextValue(value, (text) => {
416
+ folded.length = 0;
417
+ appendTextCodePoints(text, true, host, folded);
418
+ collectRunKeys(folded, keys);
419
+ });
420
+ return keys;
421
+ }
422
+ function grow(list) {
423
+ const next = new Uint32Array(list.ids.length * 2);
424
+ next.set(list.ids.subarray(0, list.length));
425
+ listBytes += next.byteLength - list.ids.byteLength;
426
+ list.ids = next;
427
+ }
428
+ function listFor(key) {
429
+ let list = runs.get(key);
430
+ if (list === void 0) {
431
+ list = createRunList();
432
+ listBytes += list.ids.byteLength;
433
+ runs.set(key, list);
434
+ }
435
+ return list;
436
+ }
437
+ function append(list, ordinal) {
438
+ if (list.removed?.delete(ordinal)) return;
439
+ if (list.length === list.ids.length) grow(list);
440
+ if (list.length > 0 && list.ids[list.length - 1] > ordinal) list.ordered = false;
441
+ list.ids[list.length++] = ordinal;
442
+ }
443
+ function dropIfEmpty(key, list) {
444
+ if (list.length > 0) return;
445
+ listBytes -= list.ids.byteLength;
446
+ runs.delete(key);
447
+ }
448
+ function markRemoved(key, list, ordinal) {
449
+ if (list.removed === null) list.removed = /* @__PURE__ */ new Set();
450
+ list.removed.add(ordinal);
451
+ if (list.removed.size < list.length * RUN_LIST_COMPACTION_FRACTION) return;
452
+ compactRemoved(list);
453
+ dropIfEmpty(key, list);
454
+ }
455
+ function settledPostings(key) {
456
+ const list = runs.get(key);
457
+ if (list === void 0) return EMPTY_POSTINGS;
458
+ compactAndSort(list);
459
+ if (list.length === 0) {
460
+ dropIfEmpty(key, list);
461
+ return EMPTY_POSTINGS;
462
+ }
463
+ return list.ids.subarray(0, list.length);
464
+ }
465
+ function sortedDocuments() {
466
+ if (documentsCache === null) {
467
+ documentsCache = Uint32Array.from(documents).sort();
468
+ }
469
+ return documentsCache;
470
+ }
471
+ function runSource() {
472
+ const keys = [];
473
+ const lists = [];
474
+ for (const key of [...runs.keys()]) {
475
+ const postings = settledPostings(key);
476
+ if (postings.length === 0) continue;
477
+ keys.push(key);
478
+ lists.push(postings);
479
+ }
480
+ const codePoints = new Uint32Array(keys.length * PATTERN_RUN_CODE_POINTS);
481
+ for (let run = 0; run < keys.length; run++) {
482
+ runKeyCodePoints(keys[run], codePoints, run * PATTERN_RUN_CODE_POINTS);
483
+ }
484
+ return { count: keys.length, codePoints, postingsAt: (run) => lists[run] };
485
+ }
486
+ return {
487
+ add(ordinal, value) {
488
+ if (!holdsTextValue(value)) return;
489
+ documents.add(ordinal);
490
+ documentsCache = null;
491
+ for (const key of runKeysOf(value)) append(listFor(key), ordinal);
492
+ },
493
+ remove(ordinal, value) {
494
+ if (!holdsTextValue(value)) return;
495
+ if (documents.delete(ordinal)) documentsCache = null;
496
+ for (const key of runKeysOf(value)) {
497
+ const list = runs.get(key);
498
+ if (list !== void 0) markRemoved(key, list, ordinal);
499
+ }
500
+ },
501
+ addArrays(arrays, mapOrdinal) {
502
+ for (const ordinal of arrays.docIds) {
503
+ const mapped = mapOrdinal(ordinal);
504
+ if (mapped < 0) continue;
505
+ documents.add(mapped);
506
+ documentsCache = null;
507
+ }
508
+ const runCount = arrays.offsets.length - 1;
509
+ for (let run = 0; run < runCount; run++) {
510
+ const at = run * PATTERN_RUN_CODE_POINTS;
511
+ const key = runKey(arrays.runs[at], arrays.runs[at + 1], arrays.runs[at + 2]);
512
+ const list = listFor(key);
513
+ for (let posting = arrays.offsets[run]; posting < arrays.offsets[run + 1]; posting++) {
514
+ const mapped = mapOrdinal(arrays.postings[posting]);
515
+ if (mapped >= 0) append(list, mapped);
516
+ }
517
+ dropIfEmpty(key, list);
518
+ }
519
+ },
520
+ clear() {
521
+ runs.clear();
522
+ documents.clear();
523
+ documentsCache = null;
524
+ listBytes = 0;
525
+ },
526
+ documents: sortedDocuments,
527
+ postings(first, second, third) {
528
+ return settledPostings(runKey(first, second, third));
529
+ },
530
+ toArrays(fieldPath, remap) {
531
+ const source = runSource();
532
+ return buildArrays(fieldPath, sortedDocuments(), source, sortedRunOrder(source.codePoints, source.count), remap);
533
+ },
534
+ bytes() {
535
+ return listBytes + runs.size * RUN_ENTRY_OVERHEAD_BYTES + documents.size * DOCUMENT_ENTRY_BYTES + (documentsCache?.byteLength ?? 0);
536
+ }
537
+ };
538
+ }
539
+
540
+ // src/pattern/merge.ts
541
+ function sortTailIfUnordered(values, start) {
542
+ for (let at = start + 1; at < values.length; at++) {
543
+ if (values.values[at - 1] > values.values[at]) {
544
+ values.values.subarray(start, values.length).sort();
545
+ return;
546
+ }
547
+ }
548
+ }
549
+ function smallestRun(inputs, cursors) {
550
+ let smallest = -1;
551
+ for (let input = 0; input < inputs.length; input++) {
552
+ const runCount = inputs[input].arrays.offsets.length - 1;
553
+ if (cursors[input] >= runCount) continue;
554
+ if (smallest < 0) {
555
+ smallest = input;
556
+ continue;
557
+ }
558
+ const order = compareRuns(
559
+ inputs[input].arrays.runs,
560
+ cursors[input] * PATTERN_RUN_CODE_POINTS,
561
+ inputs[smallest].arrays.runs,
562
+ cursors[smallest] * PATTERN_RUN_CODE_POINTS
563
+ );
564
+ if (order < 0) smallest = input;
565
+ }
566
+ return smallest;
567
+ }
568
+ function mergePatternArrays(fieldPath, inputs) {
569
+ const docIds = new GrowableUint32(1);
570
+ for (const input of inputs) remapAscending(input.arrays.docIds, input.remap, docIds);
571
+ sortTailIfUnordered(docIds, 0);
572
+ const runs = new GrowableUint32(PATTERN_RUN_CODE_POINTS);
573
+ const offsets = new GrowableUint32(1);
574
+ const postings = new GrowableUint32(1);
575
+ offsets.push(0);
576
+ const cursors = inputs.map(() => 0);
577
+ for (let smallest = smallestRun(inputs, cursors); smallest >= 0; smallest = smallestRun(inputs, cursors)) {
578
+ const source = inputs[smallest].arrays.runs;
579
+ const at = cursors[smallest] * PATTERN_RUN_CODE_POINTS;
580
+ const first = source[at];
581
+ const second = source[at + 1];
582
+ const third = source[at + 2];
583
+ const before = postings.length;
584
+ for (let input = 0; input < inputs.length; input++) {
585
+ const { arrays, remap } = inputs[input];
586
+ const run = cursors[input];
587
+ if (run >= arrays.offsets.length - 1) continue;
588
+ const runAt = run * PATTERN_RUN_CODE_POINTS;
589
+ if (arrays.runs[runAt] !== first || arrays.runs[runAt + 1] !== second || arrays.runs[runAt + 2] !== third) {
590
+ continue;
591
+ }
592
+ remapAscending(arrays.postings.subarray(arrays.offsets[run], arrays.offsets[run + 1]), remap, postings);
593
+ cursors[input] = run + 1;
594
+ }
595
+ if (postings.length === before) continue;
596
+ sortTailIfUnordered(postings, before);
597
+ runs.push(first);
598
+ runs.push(second);
599
+ runs.push(third);
600
+ offsets.push(postings.length);
601
+ }
602
+ return trimmedArrays(fieldPath, docIds, runs, offsets, postings);
603
+ }
604
+
605
+ // src/pattern/module.ts
606
+ var patternSearchModule = {
607
+ name: "pattern",
608
+ createIndex: createGrowingPatternIndex,
609
+ readArrays: readPatternArrays,
610
+ mergeArrays: mergePatternArrays,
611
+ matchInto: matchPattern
612
+ };
613
+
614
+ export { GrowableUint32, patternSearchModule };
@@ -0,0 +1,75 @@
1
+ // src/core/pattern-index/constants.ts
2
+ var DEFAULT_PATTERN_WORK_CAP = 25e6;
3
+ var MAX_PATTERN_TEXT_CODE_POINTS = 1024;
4
+ var MAX_PATTERN_POSITIONS = 2048;
5
+ var MATCHER_STATE_BUDGET_BYTES = 1e6;
6
+ var MATCHER_CODE_POINT_BYTES = 8;
7
+ var MATCHER_SIMULATION_BYTES_PER_POSITION = 16;
8
+ var MATCHER_STATE_OVERHEAD_BYTES = 96;
9
+ var PATTERN_WORK_FLUSH_UNITS = 65536;
10
+ var PATTERN_RUN_CODE_POINTS = 3;
11
+ var PATTERN_TEST_OPERATORS = ["eq", "ne", "in", "nin", "startsWith", "endsWith", "contains"];
12
+
13
+ // src/core/bitset.ts
14
+ function createBitSet(capacity) {
15
+ return new Uint32Array(capacity + 31 >>> 5 || 1);
16
+ }
17
+ function bitsetSet(bs, index) {
18
+ const word = index >>> 5;
19
+ if (word < bs.length) {
20
+ bs[word] |= 1 << (index & 31);
21
+ }
22
+ }
23
+ function bitsetHas(bs, index) {
24
+ const word = index >>> 5;
25
+ if (word >= bs.length) return false;
26
+ return (bs[word] & 1 << (index & 31)) !== 0;
27
+ }
28
+ function bitsetAnd(a, b) {
29
+ const len = Math.min(a.length, b.length);
30
+ const result = new Uint32Array(len);
31
+ for (let i = 0; i < len; i++) {
32
+ result[i] = a[i] & b[i];
33
+ }
34
+ return result;
35
+ }
36
+ function bitsetAndInto(target, other) {
37
+ const shared = Math.min(target.length, other.length);
38
+ for (let i = 0; i < shared; i++) target[i] &= other[i];
39
+ target.fill(0, shared);
40
+ }
41
+ function bitsetOrInto(target, other) {
42
+ const shared = Math.min(target.length, other.length);
43
+ for (let i = 0; i < shared; i++) target[i] |= other[i];
44
+ }
45
+ function bitsetAndNotInto(target, removed) {
46
+ const shared = Math.min(target.length, removed.length);
47
+ for (let i = 0; i < shared; i++) target[i] &= ~removed[i];
48
+ }
49
+ function bitsetNot(bs, capacity) {
50
+ const result = new Uint32Array(bs.length);
51
+ const fullWords = capacity >>> 5;
52
+ for (let i = 0; i < fullWords; i++) {
53
+ result[i] = ~bs[i];
54
+ }
55
+ const remainder = capacity & 31;
56
+ if (remainder > 0 && fullWords < bs.length) {
57
+ result[fullWords] = ~bs[fullWords] & (1 << remainder) - 1;
58
+ }
59
+ return result;
60
+ }
61
+ function bitsetIsEmpty(bs) {
62
+ for (let i = 0; i < bs.length; i++) {
63
+ if (bs[i] !== 0) return false;
64
+ }
65
+ return true;
66
+ }
67
+ function bitsetFromSet(set, capacity) {
68
+ const bs = createBitSet(capacity);
69
+ for (const id of set) {
70
+ bs[id >>> 5] |= 1 << (id & 31);
71
+ }
72
+ return bs;
73
+ }
74
+
75
+ export { DEFAULT_PATTERN_WORK_CAP, MATCHER_CODE_POINT_BYTES, MATCHER_SIMULATION_BYTES_PER_POSITION, MATCHER_STATE_BUDGET_BYTES, MATCHER_STATE_OVERHEAD_BYTES, MAX_PATTERN_POSITIONS, MAX_PATTERN_TEXT_CODE_POINTS, PATTERN_RUN_CODE_POINTS, PATTERN_TEST_OPERATORS, PATTERN_WORK_FLUSH_UNITS, bitsetAnd, bitsetAndInto, bitsetAndNotInto, bitsetFromSet, bitsetHas, bitsetIsEmpty, bitsetNot, bitsetOrInto, bitsetSet, createBitSet };