@devisfuture/mega-collection 2.4.6 → 2.4.8
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.
- package/dist/State-CYIe-3He.d.ts +44 -0
- package/dist/chunks/constants-BXQI1M5E.mjs +1 -0
- package/dist/chunks/internal-COJC3Ik3.mjs +1 -0
- package/dist/chunks/merge-BRXHTBZ0.mjs +1 -0
- package/dist/filter/index.d.ts +111 -0
- package/dist/filter/index.mjs +1 -891
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +1 -2
- package/dist/merge/index.d.ts +128 -0
- package/dist/merge/index.mjs +1 -2
- package/dist/search/index.d.ts +190 -0
- package/dist/search/index.mjs +1 -1010
- package/dist/sort/index.d.ts +55 -0
- package/dist/sort/index.mjs +1 -416
- package/dist/types-DONld7xY.d.ts +74 -0
- package/package.json +14 -34
- package/dist/chunk-BJZg_1dI.mjs +0 -183
- package/dist/chunk-BURK5sks.mjs +0 -505
- package/dist/chunk-C5Zpu_ol.mjs +0 -18
- package/dist/filter/index.d.mts +0 -4
- package/dist/index.d.mts +0 -4
- package/dist/merge/index.d.mts +0 -4
- package/dist/search/index.d.mts +0 -4
- package/dist/sort/index.d.mts +0 -4
package/dist/search/index.mjs
CHANGED
|
@@ -1,1010 +1 @@
|
|
|
1
|
-
import { a as e, i as t, n } from "../chunk-BJZg_1dI.mjs";
|
|
2
|
-
import { n as r } from "../chunk-C5Zpu_ol.mjs";
|
|
3
|
-
//#region src/search/ngram.ts
|
|
4
|
-
function i(e) {
|
|
5
|
-
for (let t = 0; t < e.length - 1; t++) {
|
|
6
|
-
let n = t;
|
|
7
|
-
for (let r = t + 1; r < e.length; r++) e[r].size < e[n].size && (n = r);
|
|
8
|
-
if (n !== t) {
|
|
9
|
-
let r = e[t];
|
|
10
|
-
e[t] = e[n], e[n] = r;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function a(e, t) {
|
|
15
|
-
let n = [];
|
|
16
|
-
for (let r of t) {
|
|
17
|
-
let t = e.get(r);
|
|
18
|
-
if (!t) return null;
|
|
19
|
-
n.push(t);
|
|
20
|
-
}
|
|
21
|
-
return i(n), n;
|
|
22
|
-
}
|
|
23
|
-
function o(e, t) {
|
|
24
|
-
for (let n = 0; n < t.length; n++) if (!t[n].has(e)) return !1;
|
|
25
|
-
return !0;
|
|
26
|
-
}
|
|
27
|
-
function s(e, t, n, r) {
|
|
28
|
-
let i = a(e, t);
|
|
29
|
-
if (i === null) return null;
|
|
30
|
-
let s = i[0];
|
|
31
|
-
return {
|
|
32
|
-
smallestPostingList: s,
|
|
33
|
-
matches: (e) => s.has(e) && o(e, i) && !!n[e]?.includes(r)
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
function c(e) {
|
|
37
|
-
let t = Math.min(3, Math.max(2, e.length)), n = e.length - t + 1, r = Array(n);
|
|
38
|
-
for (let i = 0; i < n; i++) r[i] = e.substring(i, i + t);
|
|
39
|
-
return r;
|
|
40
|
-
}
|
|
41
|
-
function l(e, t) {
|
|
42
|
-
let n = e.get(t);
|
|
43
|
-
if (n) return n;
|
|
44
|
-
let r = /* @__PURE__ */ new Set();
|
|
45
|
-
return e.set(t, r), r;
|
|
46
|
-
}
|
|
47
|
-
function u(e) {
|
|
48
|
-
let t = c(e);
|
|
49
|
-
if (t.length <= 12) return new Set(t);
|
|
50
|
-
let n = /* @__PURE__ */ new Set(), r = t.length - 1;
|
|
51
|
-
for (let e = 0; e <= 11; e++) {
|
|
52
|
-
let i = Math.round(e * r / 11);
|
|
53
|
-
n.add(t[i]);
|
|
54
|
-
}
|
|
55
|
-
return n;
|
|
56
|
-
}
|
|
57
|
-
function d(e, t, n) {
|
|
58
|
-
let r = Math.min(3, t.length);
|
|
59
|
-
for (let i = 2; i <= r; i++) {
|
|
60
|
-
let r = t.length - i;
|
|
61
|
-
for (let a = 0; a <= r; a++) l(e, t.substring(a, a + i)).add(n);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function f(e, t, n, r, i = {}) {
|
|
65
|
-
let { restrictionLookup: a = null, take: o = Infinity } = i, c = s(e, t, n, r);
|
|
66
|
-
if (c === null) return [];
|
|
67
|
-
let l = [];
|
|
68
|
-
for (let e of c.smallestPostingList) if (!(a !== null && !a[e]) && c.matches(e) && (l.push(e), l.length >= o)) break;
|
|
69
|
-
return l;
|
|
70
|
-
}
|
|
71
|
-
function p(e, t, n, r, i) {
|
|
72
|
-
let { candidateIndices: a, restrictionLookup: o = null, take: c = Infinity } = i;
|
|
73
|
-
if (a.length === 0) return [];
|
|
74
|
-
let l = s(e, t, n, r);
|
|
75
|
-
if (l === null) return [];
|
|
76
|
-
let u = [];
|
|
77
|
-
if (o === null || a.length <= l.smallestPostingList.size) {
|
|
78
|
-
for (let e = 0; e < a.length; e++) {
|
|
79
|
-
let t = a[e];
|
|
80
|
-
if (l.matches(t) && (u.push(t), u.length >= c)) break;
|
|
81
|
-
}
|
|
82
|
-
return u;
|
|
83
|
-
}
|
|
84
|
-
for (let e of l.smallestPostingList) if (o[e] && l.matches(e) && (u.push(e), u.length >= c)) break;
|
|
85
|
-
return u;
|
|
86
|
-
}
|
|
87
|
-
function m(e, t, n) {
|
|
88
|
-
let r = Math.min(3, t.length);
|
|
89
|
-
for (let i = 2; i <= r; i++) {
|
|
90
|
-
let r = t.length - i;
|
|
91
|
-
for (let a = 0; a <= r; a++) {
|
|
92
|
-
let r = t.substring(a, a + i), o = e.get(r);
|
|
93
|
-
o && (o.delete(n), o.size === 0 && e.delete(r));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
//#endregion
|
|
98
|
-
//#region src/search/nested.ts
|
|
99
|
-
var h = class {
|
|
100
|
-
constructor(e = {
|
|
101
|
-
ngramIndexes: /* @__PURE__ */ new Map(),
|
|
102
|
-
normalizedFieldValues: /* @__PURE__ */ new Map()
|
|
103
|
-
}) {
|
|
104
|
-
this.storage = e, this.registeredFields = /* @__PURE__ */ new Set(), this.fieldDescriptors = /* @__PURE__ */ new Map();
|
|
105
|
-
}
|
|
106
|
-
registerFields(e) {
|
|
107
|
-
if (e?.length) for (let t of e) this.registerField(t);
|
|
108
|
-
}
|
|
109
|
-
hasRegisteredFields() {
|
|
110
|
-
return this.registeredFields.size > 0;
|
|
111
|
-
}
|
|
112
|
-
hasField(e) {
|
|
113
|
-
return this.registeredFields.has(e);
|
|
114
|
-
}
|
|
115
|
-
hasIndexes() {
|
|
116
|
-
return this.storage.ngramIndexes.size > 0;
|
|
117
|
-
}
|
|
118
|
-
clearIndexes() {
|
|
119
|
-
this.storage.ngramIndexes.clear(), this.storage.normalizedFieldValues.clear();
|
|
120
|
-
}
|
|
121
|
-
buildIndexes(e) {
|
|
122
|
-
this.clearIndexes();
|
|
123
|
-
for (let t of this.registeredFields) this.buildIndex(e, t);
|
|
124
|
-
}
|
|
125
|
-
addItems(e, t) {
|
|
126
|
-
if (!(e.length === 0 || this.storage.ngramIndexes.size === 0)) for (let n of this.storage.ngramIndexes.keys()) this.addItemsToField(n, e, t);
|
|
127
|
-
}
|
|
128
|
-
updateItem(e, t, n) {
|
|
129
|
-
if (this.storage.ngramIndexes.size !== 0) for (let r of this.storage.ngramIndexes.keys()) this.updateItemInField(r, e, t, n);
|
|
130
|
-
}
|
|
131
|
-
removeItem(e, t) {
|
|
132
|
-
if (this.storage.ngramIndexes.size !== 0) for (let n of this.storage.ngramIndexes.keys()) this.removeItemFromField(n, e, t);
|
|
133
|
-
}
|
|
134
|
-
moveItem(e, t, n) {
|
|
135
|
-
if (!(this.storage.ngramIndexes.size === 0 || t === n)) for (let r of this.storage.ngramIndexes.keys()) this.moveItemForField(r, e, t, n);
|
|
136
|
-
}
|
|
137
|
-
searchAllIndexedFieldIndices(e, t, n, r) {
|
|
138
|
-
let i = /* @__PURE__ */ new Set(), a = [];
|
|
139
|
-
for (let o of this.storage.ngramIndexes.keys()) for (let s of this.searchIndexedFieldIndices(o, e, t, n, r)) i.has(s) || (i.add(s), a.push(s));
|
|
140
|
-
return a;
|
|
141
|
-
}
|
|
142
|
-
searchIndexedField(e, t, n, r, i, a, o = Infinity) {
|
|
143
|
-
let s = this.searchIndexedFieldIndices(t, n, r, i, a, o), c = [];
|
|
144
|
-
for (let t = 0; t < s.length; t++) {
|
|
145
|
-
let n = e[s[t]];
|
|
146
|
-
n && c.push(n);
|
|
147
|
-
}
|
|
148
|
-
return c;
|
|
149
|
-
}
|
|
150
|
-
searchIndexedFieldIndices(e, t, n, r, i, a = Infinity) {
|
|
151
|
-
let o = this.storage.ngramIndexes.get(e);
|
|
152
|
-
if (!o) return [];
|
|
153
|
-
let s = this.storage.normalizedFieldValues.get(e) ?? [];
|
|
154
|
-
return i == null ? f(o, n, s, t, {
|
|
155
|
-
restrictionLookup: r,
|
|
156
|
-
take: a
|
|
157
|
-
}) : p(o, n, s, t, {
|
|
158
|
-
candidateIndices: i,
|
|
159
|
-
restrictionLookup: r,
|
|
160
|
-
take: a
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
searchFieldLinear(e, t, n) {
|
|
164
|
-
let r = this.searchFieldLinearIndices(e, t, n), i = [];
|
|
165
|
-
for (let t = 0; t < r.length; t++) {
|
|
166
|
-
let n = e[r[t]];
|
|
167
|
-
n && i.push(n);
|
|
168
|
-
}
|
|
169
|
-
return i;
|
|
170
|
-
}
|
|
171
|
-
searchFieldLinearIndices(e, t, n, r) {
|
|
172
|
-
let i = this.fieldDescriptors.get(t);
|
|
173
|
-
if (!i) return [];
|
|
174
|
-
let { collectionKey: a, nestedKey: o } = i, s = [];
|
|
175
|
-
if (r) {
|
|
176
|
-
for (let t = 0; t < r.length; t++) {
|
|
177
|
-
let i = r[t], c = e[i][a];
|
|
178
|
-
if (!Array.isArray(c)) continue;
|
|
179
|
-
let l = !1;
|
|
180
|
-
for (let e = 0; e < c.length; e++) {
|
|
181
|
-
let t = c[e][o];
|
|
182
|
-
if (typeof t == "string" && t.toLowerCase().includes(n)) {
|
|
183
|
-
l = !0;
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
l && s.push(i);
|
|
188
|
-
}
|
|
189
|
-
return s;
|
|
190
|
-
}
|
|
191
|
-
for (let t = 0; t < e.length; t++) {
|
|
192
|
-
let r = e[t][a];
|
|
193
|
-
if (!Array.isArray(r)) continue;
|
|
194
|
-
let i = !1;
|
|
195
|
-
for (let e = 0; e < r.length; e++) {
|
|
196
|
-
let t = r[e][o];
|
|
197
|
-
if (typeof t == "string" && t.toLowerCase().includes(n)) {
|
|
198
|
-
i = !0;
|
|
199
|
-
break;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
i && s.push(t);
|
|
203
|
-
}
|
|
204
|
-
return s;
|
|
205
|
-
}
|
|
206
|
-
matchesAnyField(e, t) {
|
|
207
|
-
for (let n of this.registeredFields) {
|
|
208
|
-
let r = this.fieldDescriptors.get(n);
|
|
209
|
-
if (!r) continue;
|
|
210
|
-
let { collectionKey: i, nestedKey: a } = r, o = e[i];
|
|
211
|
-
if (Array.isArray(o)) for (let e = 0; e < o.length; e++) {
|
|
212
|
-
let n = o[e][a];
|
|
213
|
-
if (typeof n == "string" && n.toLowerCase().includes(t)) return !0;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return !1;
|
|
217
|
-
}
|
|
218
|
-
registerField(e) {
|
|
219
|
-
let t = r(e);
|
|
220
|
-
t && (this.registeredFields.add(e), this.fieldDescriptors.set(e, t));
|
|
221
|
-
}
|
|
222
|
-
getNormalizedValues(e) {
|
|
223
|
-
let t = this.storage.normalizedFieldValues.get(e);
|
|
224
|
-
if (t) return t;
|
|
225
|
-
let n = [];
|
|
226
|
-
return this.storage.normalizedFieldValues.set(e, n), n;
|
|
227
|
-
}
|
|
228
|
-
buildIndex(e, t) {
|
|
229
|
-
let n = this.fieldDescriptors.get(t);
|
|
230
|
-
if (!n) return;
|
|
231
|
-
let { collectionKey: r, nestedKey: i } = n, a = /* @__PURE__ */ new Map(), o = Array(e.length);
|
|
232
|
-
for (let t = 0, n = e.length; t < n; t++) {
|
|
233
|
-
let n = e[t][r];
|
|
234
|
-
if (!Array.isArray(n)) continue;
|
|
235
|
-
let s = [];
|
|
236
|
-
for (let e = 0; e < n.length; e++) {
|
|
237
|
-
let t = n[e][i];
|
|
238
|
-
typeof t == "string" && s.push(t.toLowerCase());
|
|
239
|
-
}
|
|
240
|
-
if (s.length === 0) continue;
|
|
241
|
-
let c = s.join("\n");
|
|
242
|
-
o[t] = c, d(a, c, t);
|
|
243
|
-
}
|
|
244
|
-
this.storage.ngramIndexes.set(t, a), this.storage.normalizedFieldValues.set(t, o);
|
|
245
|
-
}
|
|
246
|
-
addItemsToField(e, t, n) {
|
|
247
|
-
let r = this.fieldDescriptors.get(e), i = this.storage.ngramIndexes.get(e);
|
|
248
|
-
if (!r || !i) return;
|
|
249
|
-
let a = this.getNormalizedValues(e), { collectionKey: o, nestedKey: s } = r;
|
|
250
|
-
for (let e = 0; e < t.length; e++) {
|
|
251
|
-
let r = t[e][o];
|
|
252
|
-
if (!Array.isArray(r)) continue;
|
|
253
|
-
let c = [], l = n + e;
|
|
254
|
-
for (let e = 0; e < r.length; e++) {
|
|
255
|
-
let t = r[e][s];
|
|
256
|
-
typeof t == "string" && c.push(t.toLowerCase());
|
|
257
|
-
}
|
|
258
|
-
if (c.length === 0) continue;
|
|
259
|
-
let u = c.join("\n");
|
|
260
|
-
a[l] = u, d(i, u, l);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
updateItemInField(e, t, n, r) {
|
|
264
|
-
let i = this.storage.ngramIndexes.get(e);
|
|
265
|
-
if (!i) return;
|
|
266
|
-
let a = this.getNormalizedValues(e), o = this.getNormalizedItemValue(e, n), s = this.getNormalizedItemValue(e, t);
|
|
267
|
-
if (o !== s) {
|
|
268
|
-
if (o && m(i, o, r), !s) {
|
|
269
|
-
delete a[r];
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
a[r] = s, d(i, s, r);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
removeItemFromField(e, t, n) {
|
|
276
|
-
let r = this.storage.ngramIndexes.get(e);
|
|
277
|
-
if (!r) return;
|
|
278
|
-
let i = this.getNormalizedValues(e), a = i[n] ?? this.getNormalizedItemValue(e, t);
|
|
279
|
-
a && m(r, a, n), delete i[n];
|
|
280
|
-
}
|
|
281
|
-
moveItemForField(e, t, n, r) {
|
|
282
|
-
let i = this.storage.ngramIndexes.get(e);
|
|
283
|
-
if (!i) return;
|
|
284
|
-
let a = this.getNormalizedValues(e), o = a[n] ?? this.getNormalizedItemValue(e, t);
|
|
285
|
-
if (!o) {
|
|
286
|
-
delete a[n];
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
m(i, o, n), d(i, o, r), a[r] = o, delete a[n];
|
|
290
|
-
}
|
|
291
|
-
getNormalizedItemValue(e, t) {
|
|
292
|
-
let n = this.fieldDescriptors.get(e);
|
|
293
|
-
if (!n) return null;
|
|
294
|
-
let { collectionKey: r, nestedKey: i } = n, a = t[r];
|
|
295
|
-
if (!Array.isArray(a)) return null;
|
|
296
|
-
let o = [];
|
|
297
|
-
for (let e = 0; e < a.length; e++) {
|
|
298
|
-
let t = a[e][i];
|
|
299
|
-
typeof t == "string" && o.push(t.toLowerCase());
|
|
300
|
-
}
|
|
301
|
-
return o.length === 0 ? null : o.join("\n");
|
|
302
|
-
}
|
|
303
|
-
}, g = () => ({
|
|
304
|
-
indexedFields: /* @__PURE__ */ new Set(),
|
|
305
|
-
flatIndexes: /* @__PURE__ */ new Map(),
|
|
306
|
-
nestedStorage: {
|
|
307
|
-
ngramIndexes: /* @__PURE__ */ new Map(),
|
|
308
|
-
normalizedFieldValues: /* @__PURE__ */ new Map()
|
|
309
|
-
},
|
|
310
|
-
deferredMutationVersion: null,
|
|
311
|
-
filterByPreviousResult: !1,
|
|
312
|
-
previousResultIndices: null,
|
|
313
|
-
previousResultLookup: null,
|
|
314
|
-
previousQuery: null,
|
|
315
|
-
stats: {
|
|
316
|
-
totalQueries: 0,
|
|
317
|
-
indexedQueries: 0,
|
|
318
|
-
fallbackQueries: 0,
|
|
319
|
-
fallbackFields: /* @__PURE__ */ new Map()
|
|
320
|
-
}
|
|
321
|
-
}), _ = class {
|
|
322
|
-
constructor(t = {}) {
|
|
323
|
-
this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.emittedWarningKeys = /* @__PURE__ */ new Set(), this.warnings = [], this.normalizedValuesCache = /* @__PURE__ */ new Map(), this.combinedNormalizedValuesCache = null, this.minQueryLength = t.minQueryLength ?? 1, this.silent = t.silent ?? !1, this.state = t.state ?? new e(t.data ?? []), this.namespace = this.state.createNamespace("search"), this.nestedCollection = new h(this.runtime.nestedStorage), this.nestedCollection.registerFields(t.nestedFields), this.state.subscribe((e) => this.handleStateMutation(e)), t.filterByPreviousResult && (this.runtime.filterByPreviousResult = !0);
|
|
324
|
-
let n = t.fields?.length, r = this.nestedCollection.hasRegisteredFields();
|
|
325
|
-
if (n) for (let e of t.fields) this.indexedFields.add(e);
|
|
326
|
-
this.dataset.length > 0 && (n || r) && this.rebuildConfiguredIndexes();
|
|
327
|
-
}
|
|
328
|
-
get dataset() {
|
|
329
|
-
return this.state.getOriginData();
|
|
330
|
-
}
|
|
331
|
-
get runtime() {
|
|
332
|
-
return this.state.getOrCreateScopedValue(this.namespace, "runtime", g);
|
|
333
|
-
}
|
|
334
|
-
get flatIndexes() {
|
|
335
|
-
return this.runtime.flatIndexes;
|
|
336
|
-
}
|
|
337
|
-
get indexedFields() {
|
|
338
|
-
return this.runtime.indexedFields;
|
|
339
|
-
}
|
|
340
|
-
shouldDeferMutationIndexUpdates() {
|
|
341
|
-
return this.state.getScopedValue(t, n) === !0;
|
|
342
|
-
}
|
|
343
|
-
markDeferredMutationState() {
|
|
344
|
-
this.runtime.deferredMutationVersion = this.state.getMutationVersion(), this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
|
|
345
|
-
}
|
|
346
|
-
ensureConfiguredIndexesReady() {
|
|
347
|
-
this.runtime.deferredMutationVersion !== null && (this.runtime.deferredMutationVersion = null, this.dataset.length > 0 && (this.indexedFields.size > 0 || this.nestedCollection.hasRegisteredFields()) && this.rebuildConfiguredIndexes());
|
|
348
|
-
}
|
|
349
|
-
rebuildConfiguredIndexes() {
|
|
350
|
-
this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null;
|
|
351
|
-
for (let e of this.indexedFields) this.buildIndexFromData(this.dataset, e);
|
|
352
|
-
this.nestedCollection.hasRegisteredFields() && this.nestedCollection.buildIndexes(this.dataset);
|
|
353
|
-
}
|
|
354
|
-
buildIndexFromData(e, t) {
|
|
355
|
-
let n = this.state.getMutationVersion(), r = /* @__PURE__ */ new Map(), i = Array(e.length);
|
|
356
|
-
for (let n = 0, a = e.length; n < a; n++) {
|
|
357
|
-
let a = e[n][t];
|
|
358
|
-
if (typeof a != "string") continue;
|
|
359
|
-
let o = a.toLowerCase();
|
|
360
|
-
i[n] = o, d(r, o, n);
|
|
361
|
-
}
|
|
362
|
-
this.flatIndexes.set(t, {
|
|
363
|
-
ngramMap: r,
|
|
364
|
-
normalizedValues: i,
|
|
365
|
-
version: n
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
search(e, t, n) {
|
|
369
|
-
return typeof t == "string" ? this.searchField(e, t, n) : this.searchAll(e, t);
|
|
370
|
-
}
|
|
371
|
-
searchAll(e, t) {
|
|
372
|
-
return this.searchAllFields(e, t);
|
|
373
|
-
}
|
|
374
|
-
normalizeQuery(e) {
|
|
375
|
-
return e.trim().toLowerCase();
|
|
376
|
-
}
|
|
377
|
-
normalizeSearchWindow(e) {
|
|
378
|
-
let t = Math.max(0, Math.trunc(e?.offset ?? 0)), n = e?.limit, r = n === void 0 ? Infinity : Math.max(0, Math.trunc(n));
|
|
379
|
-
return {
|
|
380
|
-
offset: t,
|
|
381
|
-
limit: r,
|
|
382
|
-
take: Number.isFinite(r) ? t + r : Infinity,
|
|
383
|
-
hasWindow: t > 0 || Number.isFinite(r)
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
shouldTrackPreviousResult(e) {
|
|
387
|
-
return !e.hasWindow;
|
|
388
|
-
}
|
|
389
|
-
hasReachedWindowLimit(e, t) {
|
|
390
|
-
return Number.isFinite(e.limit) && t >= e.limit;
|
|
391
|
-
}
|
|
392
|
-
sliceItems(e, t) {
|
|
393
|
-
return t.hasWindow ? e.slice(t.offset, t.take) : e;
|
|
394
|
-
}
|
|
395
|
-
collectItemsFromIndices(e, t) {
|
|
396
|
-
let n = t.hasWindow && e.length > 0 ? e.slice(t.offset, t.take) : e, r = [];
|
|
397
|
-
for (let e = 0; e < n.length; e++) {
|
|
398
|
-
let t = this.dataset[n[e]];
|
|
399
|
-
t && r.push(t);
|
|
400
|
-
}
|
|
401
|
-
return {
|
|
402
|
-
items: r,
|
|
403
|
-
indices: n
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
createLookup(e) {
|
|
407
|
-
let t = new Uint8Array(this.dataset.length);
|
|
408
|
-
for (let n = 0; n < e.length; n++) t[e[n]] = 1;
|
|
409
|
-
return t;
|
|
410
|
-
}
|
|
411
|
-
getRestrictionLookup(e) {
|
|
412
|
-
let t = this.runtime.previousResultLookup;
|
|
413
|
-
if (t !== null && this.runtime.previousResultIndices === e && t.length === this.dataset.length) return t;
|
|
414
|
-
let n = this.createLookup(e);
|
|
415
|
-
return this.runtime.previousResultLookup = n, n;
|
|
416
|
-
}
|
|
417
|
-
getSearchSource(e) {
|
|
418
|
-
let { runtime: t } = this;
|
|
419
|
-
return t.filterByPreviousResult ? t.previousQuery !== null && t.previousResultIndices !== null && e.includes(t.previousQuery) ? {
|
|
420
|
-
indices: t.previousResultIndices,
|
|
421
|
-
lookup: this.getRestrictionLookup(t.previousResultIndices)
|
|
422
|
-
} : (t.previousResultIndices = null, t.previousResultLookup = null, t.previousQuery = null, {
|
|
423
|
-
indices: null,
|
|
424
|
-
lookup: null
|
|
425
|
-
}) : {
|
|
426
|
-
indices: null,
|
|
427
|
-
lookup: null
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
saveSearchResult(e, t) {
|
|
431
|
-
let { runtime: n } = this;
|
|
432
|
-
n.filterByPreviousResult && (n.previousResultIndices = e, n.previousResultLookup = null, n.previousQuery = t);
|
|
433
|
-
}
|
|
434
|
-
persistSearchResult(e, t, n) {
|
|
435
|
-
n && this.saveSearchResult(e.indices, t);
|
|
436
|
-
}
|
|
437
|
-
resetSearchState() {
|
|
438
|
-
return this.clearPreviousSearchState(), this;
|
|
439
|
-
}
|
|
440
|
-
getWarnings() {
|
|
441
|
-
return [...this.warnings];
|
|
442
|
-
}
|
|
443
|
-
getStats() {
|
|
444
|
-
let { totalQueries: e, indexedQueries: t, fallbackQueries: n, fallbackFields: r } = this.runtime.stats;
|
|
445
|
-
return {
|
|
446
|
-
totalQueries: e,
|
|
447
|
-
indexedQueries: t,
|
|
448
|
-
fallbackQueries: n,
|
|
449
|
-
fallbackRate: e === 0 ? 0 : n / e,
|
|
450
|
-
fallbackFields: Object.fromEntries(r)
|
|
451
|
-
};
|
|
452
|
-
}
|
|
453
|
-
resetStats() {
|
|
454
|
-
let { stats: e } = this.runtime;
|
|
455
|
-
return e.totalQueries = 0, e.indexedQueries = 0, e.fallbackQueries = 0, e.fallbackFields.clear(), this;
|
|
456
|
-
}
|
|
457
|
-
clearPreviousSearchState() {
|
|
458
|
-
let { runtime: e } = this;
|
|
459
|
-
e.previousResultIndices = null, e.previousResultLookup = null, e.previousQuery = null;
|
|
460
|
-
}
|
|
461
|
-
recordIndexedQuery() {
|
|
462
|
-
let { stats: e } = this.runtime;
|
|
463
|
-
e.totalQueries += 1, e.indexedQueries += 1;
|
|
464
|
-
}
|
|
465
|
-
recordFallbackQuery(e) {
|
|
466
|
-
let { stats: t } = this.runtime;
|
|
467
|
-
t.totalQueries += 1, t.fallbackQueries += 1, t.fallbackFields.set(e, (t.fallbackFields.get(e) ?? 0) + 1);
|
|
468
|
-
}
|
|
469
|
-
shouldEmitFallbackWarning(e) {
|
|
470
|
-
return !this.silent && e.length >= 2;
|
|
471
|
-
}
|
|
472
|
-
warnAboutFallback(e, t, n) {
|
|
473
|
-
if (!this.shouldEmitFallbackWarning(t)) return;
|
|
474
|
-
let r = `${e}\u0000${t}\u0000${n}`;
|
|
475
|
-
if (this.emittedWarningKeys.has(r)) return;
|
|
476
|
-
this.emittedWarningKeys.add(r);
|
|
477
|
-
let i = `[TextSearchEngine] warn: query "${t}" on ${e} used linear fallback. ${n}. Add the field(s) to the index schema to enable indexed search.`;
|
|
478
|
-
this.warnings.push(i), typeof process < "u" && process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "test" && console.warn(i);
|
|
479
|
-
}
|
|
480
|
-
getResolvedFlatIndex(e) {
|
|
481
|
-
let t = this.state.getMutationVersion(), n = this.flatIndexes.get(e);
|
|
482
|
-
return n && n.version !== t && this.dataset.length > 0 && (this.buildIndexFromData(this.dataset, e), n = this.flatIndexes.get(e)), n;
|
|
483
|
-
}
|
|
484
|
-
getQueryGrams(e) {
|
|
485
|
-
let t = u(e);
|
|
486
|
-
return t.size > 0 ? t : null;
|
|
487
|
-
}
|
|
488
|
-
searchAllFields(e, t) {
|
|
489
|
-
let n = this.normalizeQuery(e), r = this.normalizeSearchWindow(t), i = "all fields";
|
|
490
|
-
if (r.limit === 0) return [];
|
|
491
|
-
if (!n || n.length < this.minQueryLength) return this.sliceItems(this.dataset, r);
|
|
492
|
-
n.length >= 2 && this.ensureConfiguredIndexesReady();
|
|
493
|
-
let a = this.shouldTrackPreviousResult(r), { indices: o, lookup: s } = this.getSearchSource(n), c, l = () => (c === void 0 && (c = n.length >= 2 ? this.getQueryGrams(n) : null), c);
|
|
494
|
-
if (o !== null) {
|
|
495
|
-
let e = l();
|
|
496
|
-
if (e !== null && (this.flatIndexes.size > 0 || this.nestedCollection.hasIndexes())) {
|
|
497
|
-
this.recordIndexedQuery();
|
|
498
|
-
let t = this.searchAllFieldsIndexed(n, e, r, s, o);
|
|
499
|
-
return this.persistSearchResult(t, n, a), t.items;
|
|
500
|
-
}
|
|
501
|
-
this.recordFallbackQuery(i);
|
|
502
|
-
let t = this.searchLinearAllFields(this.dataset, n, o, r);
|
|
503
|
-
return this.persistSearchResult(t, n, a), t.items;
|
|
504
|
-
}
|
|
505
|
-
if (!this.flatIndexes.size && !this.nestedCollection.hasIndexes()) {
|
|
506
|
-
this.warnAboutFallback(i, n, this.indexedFields.size > 0 ? "configured indexes are not currently built" : "no indexed fields are configured"), this.recordFallbackQuery(i);
|
|
507
|
-
let e = this.searchLinearAllFields(this.dataset, n, null, r);
|
|
508
|
-
return this.persistSearchResult(e, n, a), e.items;
|
|
509
|
-
}
|
|
510
|
-
let u = l();
|
|
511
|
-
if (u === null) {
|
|
512
|
-
this.recordFallbackQuery(i);
|
|
513
|
-
let e = this.searchLinearAllFields(this.dataset, n, null, r);
|
|
514
|
-
return this.persistSearchResult(e, n, a), e.items;
|
|
515
|
-
}
|
|
516
|
-
this.recordIndexedQuery();
|
|
517
|
-
let d = this.searchAllFieldsIndexed(n, u, r, null);
|
|
518
|
-
return this.persistSearchResult(d, n, a), d.items;
|
|
519
|
-
}
|
|
520
|
-
searchAllFieldsIndexed(e, t, n, r, i = null) {
|
|
521
|
-
if (i !== null && !this.nestedCollection.hasRegisteredFields() && this.flatIndexes.size > 0) return this.searchAllFieldsIndexedInCandidates(e, t, n, r, i);
|
|
522
|
-
let a = this.dataset, o = new Uint8Array(a.length), s = [], c = 0;
|
|
523
|
-
for (let a of this.flatIndexes.keys()) {
|
|
524
|
-
let l = this.searchFieldWithPreparedQueryIndices(a, e, t, r, i);
|
|
525
|
-
for (let e = 0; e < l.length; e++) {
|
|
526
|
-
let t = l[e];
|
|
527
|
-
if (!o[t]) {
|
|
528
|
-
if (o[t] = 1, c < n.offset) {
|
|
529
|
-
c += 1;
|
|
530
|
-
continue;
|
|
531
|
-
}
|
|
532
|
-
if (s.push(t), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
for (let a of this.nestedCollection.searchAllIndexedFieldIndices(e, t, r, i)) if (!o[a]) {
|
|
537
|
-
if (o[a] = 1, c < n.offset) {
|
|
538
|
-
c += 1;
|
|
539
|
-
continue;
|
|
540
|
-
}
|
|
541
|
-
if (s.push(a), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
|
|
542
|
-
}
|
|
543
|
-
return this.materializeIndicesResult(s);
|
|
544
|
-
}
|
|
545
|
-
searchAllFieldsIndexedInCandidates(e, t, n, r, i) {
|
|
546
|
-
let a = [];
|
|
547
|
-
for (let n of this.flatIndexes.keys()) {
|
|
548
|
-
let r = this.getResolvedFlatIndex(n);
|
|
549
|
-
if (!r) continue;
|
|
550
|
-
let i = s(r.ngramMap, t, r.normalizedValues, e);
|
|
551
|
-
i !== null && a.push(i.matches);
|
|
552
|
-
}
|
|
553
|
-
if (a.length === 0) return {
|
|
554
|
-
items: [],
|
|
555
|
-
indices: []
|
|
556
|
-
};
|
|
557
|
-
let o = [], c = 0;
|
|
558
|
-
for (let e = 0; e < i.length; e++) {
|
|
559
|
-
let t = i[e];
|
|
560
|
-
if (r !== null && !r[t]) continue;
|
|
561
|
-
let s = !1;
|
|
562
|
-
for (let e = 0; e < a.length; e++) if (a[e](t)) {
|
|
563
|
-
s = !0;
|
|
564
|
-
break;
|
|
565
|
-
}
|
|
566
|
-
if (s) {
|
|
567
|
-
if (c < n.offset) {
|
|
568
|
-
c += 1;
|
|
569
|
-
continue;
|
|
570
|
-
}
|
|
571
|
-
if (o.push(t), c += 1, this.hasReachedWindowLimit(n, o.length)) break;
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
return this.materializeIndicesResult(o);
|
|
575
|
-
}
|
|
576
|
-
searchField(e, t, n) {
|
|
577
|
-
let r = this.normalizeQuery(t), i = this.normalizeSearchWindow(n), a = `field "${e}"`;
|
|
578
|
-
if (i.limit === 0) return [];
|
|
579
|
-
if (!r || r.length < this.minQueryLength) return this.sliceItems(this.dataset, i);
|
|
580
|
-
r.length >= 2 && this.ensureConfiguredIndexesReady();
|
|
581
|
-
let o = this.shouldTrackPreviousResult(i), { indices: s, lookup: c } = this.getSearchSource(r), l = this.nestedCollection.hasField(e), u, d = () => (u === void 0 && (u = r.length >= 2 ? this.getQueryGrams(r) : null), u);
|
|
582
|
-
if (s !== null) {
|
|
583
|
-
let t = d();
|
|
584
|
-
if (l) {
|
|
585
|
-
if (t !== null && this.nestedCollection.hasIndexes()) {
|
|
586
|
-
this.recordIndexedQuery();
|
|
587
|
-
let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, c, s, i.take), a = this.collectItemsFromIndices(n, i);
|
|
588
|
-
return this.persistSearchResult(a, r, o), a.items;
|
|
589
|
-
}
|
|
590
|
-
this.recordFallbackQuery(a);
|
|
591
|
-
let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
|
|
592
|
-
return this.persistSearchResult(n, r, o), n.items;
|
|
593
|
-
}
|
|
594
|
-
if (t !== null && this.flatIndexes.has(e)) {
|
|
595
|
-
this.recordIndexedQuery();
|
|
596
|
-
let n = this.searchFieldWithPreparedQuery(e, r, t, i, c, s);
|
|
597
|
-
return this.persistSearchResult(n, r, o), n.items;
|
|
598
|
-
}
|
|
599
|
-
this.recordFallbackQuery(a);
|
|
600
|
-
let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
|
|
601
|
-
return this.persistSearchResult(n, r, o), n.items;
|
|
602
|
-
}
|
|
603
|
-
if (l) {
|
|
604
|
-
if (r.length >= 2 && this.nestedCollection.hasIndexes()) {
|
|
605
|
-
let t = this.getQueryGrams(r);
|
|
606
|
-
if (t === null) return [];
|
|
607
|
-
this.recordIndexedQuery();
|
|
608
|
-
let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, null, null, i.take), a = this.collectItemsFromIndices(n, i);
|
|
609
|
-
return this.persistSearchResult(a, r, o), a.items;
|
|
610
|
-
}
|
|
611
|
-
this.recordFallbackQuery(a);
|
|
612
|
-
let t = this.searchLinearSingleField(this.dataset, e, r, null, i);
|
|
613
|
-
return this.persistSearchResult(t, r, o), t.items;
|
|
614
|
-
}
|
|
615
|
-
this.flatIndexes.size || this.warnAboutFallback(a, r, this.indexedFields.size > 0 ? `field "${e}" is not backed by an active index` : "no indexed fields are configured");
|
|
616
|
-
let f = d();
|
|
617
|
-
if (this.flatIndexes.size > 0 && f !== null) {
|
|
618
|
-
if (!this.flatIndexes.has(e)) return [];
|
|
619
|
-
this.recordIndexedQuery();
|
|
620
|
-
let t = this.searchFieldWithPreparedQuery(e, r, f, i);
|
|
621
|
-
return this.persistSearchResult(t, r, o), t.items;
|
|
622
|
-
}
|
|
623
|
-
this.recordFallbackQuery(a);
|
|
624
|
-
let p = this.searchLinearSingleField(this.dataset, e, r, null, i);
|
|
625
|
-
return this.persistSearchResult(p, r, o), p.items;
|
|
626
|
-
}
|
|
627
|
-
searchFieldWithPreparedQuery(e, t, n, r, i = null, a = null) {
|
|
628
|
-
let o = this.searchFieldWithPreparedQueryIndices(e, t, n, i, a, r.take);
|
|
629
|
-
return this.collectItemsFromIndices(o, r);
|
|
630
|
-
}
|
|
631
|
-
searchFieldWithPreparedQueryIndices(e, t, n, r = null, i = null, a = Infinity) {
|
|
632
|
-
let o = this.getResolvedFlatIndex(e);
|
|
633
|
-
if (!o) return [];
|
|
634
|
-
let { ngramMap: s, normalizedValues: c } = o;
|
|
635
|
-
return i === null ? f(s, n, c, t, {
|
|
636
|
-
restrictionLookup: r,
|
|
637
|
-
take: a
|
|
638
|
-
}) : p(s, n, c, t, {
|
|
639
|
-
candidateIndices: i,
|
|
640
|
-
restrictionLookup: r,
|
|
641
|
-
take: a
|
|
642
|
-
});
|
|
643
|
-
}
|
|
644
|
-
getIndexedFieldsList() {
|
|
645
|
-
return this.cachedIndexedFieldsList === null && (this.cachedIndexedFieldsList = Array.from(this.indexedFields)), this.cachedIndexedFieldsList;
|
|
646
|
-
}
|
|
647
|
-
materializeIndicesResult(e) {
|
|
648
|
-
let t = [];
|
|
649
|
-
for (let n = 0; n < e.length; n++) {
|
|
650
|
-
let r = this.dataset[e[n]];
|
|
651
|
-
r && t.push(r);
|
|
652
|
-
}
|
|
653
|
-
return {
|
|
654
|
-
items: t,
|
|
655
|
-
indices: e
|
|
656
|
-
};
|
|
657
|
-
}
|
|
658
|
-
getLinearSearchFields(e) {
|
|
659
|
-
let t = this.getIndexedFieldsList();
|
|
660
|
-
return t.length > 0 ? t : (this.cachedLinearSearchFieldsList === null && (this.cachedLinearSearchFieldsList = e.length ? Object.keys(e[0]).filter((t) => typeof e[0][t] == "string") : []), this.cachedLinearSearchFieldsList);
|
|
661
|
-
}
|
|
662
|
-
buildNormalizedValuesOnly(e, t) {
|
|
663
|
-
let n = Array(e.length);
|
|
664
|
-
for (let r = 0, i = e.length; r < i; r++) {
|
|
665
|
-
let i = e[r][t];
|
|
666
|
-
typeof i == "string" && (n[r] = i.toLowerCase());
|
|
667
|
-
}
|
|
668
|
-
return this.normalizedValuesCache.set(t, n), n;
|
|
669
|
-
}
|
|
670
|
-
buildCombinedNormalizedValues(e, t) {
|
|
671
|
-
let n = Array(e.length);
|
|
672
|
-
for (let r = 0; r < e.length; r++) n[r] = this.buildCombinedNormalizedValue(e[r], t);
|
|
673
|
-
return this.combinedNormalizedValuesCache = {
|
|
674
|
-
fieldsKey: t.join("\0"),
|
|
675
|
-
values: n
|
|
676
|
-
}, n;
|
|
677
|
-
}
|
|
678
|
-
buildCombinedNormalizedValue(e, t) {
|
|
679
|
-
let n = "";
|
|
680
|
-
for (let r = 0; r < t.length; r++) {
|
|
681
|
-
let i = e[t[r]];
|
|
682
|
-
typeof i == "string" && (n && (n += "\n"), n += i.toLowerCase());
|
|
683
|
-
}
|
|
684
|
-
return n;
|
|
685
|
-
}
|
|
686
|
-
getCombinedNormalizedValues(e, t) {
|
|
687
|
-
if (e !== this.dataset) return null;
|
|
688
|
-
let n = t.join("\0");
|
|
689
|
-
return this.combinedNormalizedValuesCache?.fieldsKey === n ? this.combinedNormalizedValuesCache.values : this.buildCombinedNormalizedValues(e, t);
|
|
690
|
-
}
|
|
691
|
-
invalidateNormalizedValuesCacheEntry(e, t) {
|
|
692
|
-
for (let [n, r] of this.normalizedValuesCache) {
|
|
693
|
-
let i = t[n];
|
|
694
|
-
r[e] = typeof i == "string" ? i.toLowerCase() : "";
|
|
695
|
-
}
|
|
696
|
-
if (this.combinedNormalizedValuesCache !== null) {
|
|
697
|
-
let n = this.combinedNormalizedValuesCache.fieldsKey.split("\0");
|
|
698
|
-
this.combinedNormalizedValuesCache.values[e] = this.buildCombinedNormalizedValue(t, n);
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
searchLinearAllFields(e, t, n, r) {
|
|
702
|
-
if (!e.length) return {
|
|
703
|
-
items: [],
|
|
704
|
-
indices: []
|
|
705
|
-
};
|
|
706
|
-
let i = this.getLinearSearchFields(e), a = e === this.dataset, o = i.length, s = Array(o);
|
|
707
|
-
for (let t = 0; t < o; t++) {
|
|
708
|
-
let n = i[t], r = this.flatIndexes.get(n);
|
|
709
|
-
if (r) s[t] = r.normalizedValues;
|
|
710
|
-
else {
|
|
711
|
-
let r = this.normalizedValuesCache.get(n);
|
|
712
|
-
r ? s[t] = r : a ? s[t] = this.buildNormalizedValuesOnly(e, n) : s[t] = null;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
let c = this.nestedCollection.hasRegisteredFields(), l = c ? null : this.getCombinedNormalizedValues(e, i), u = !c && s.every((e) => e !== null), d = [], f = 0;
|
|
716
|
-
if (n !== null) {
|
|
717
|
-
let e = this.dataset;
|
|
718
|
-
if (l !== null) {
|
|
719
|
-
for (let e = 0; e < n.length; e++) {
|
|
720
|
-
let i = n[e];
|
|
721
|
-
if (l[i]?.includes(t)) {
|
|
722
|
-
if (f < r.offset) {
|
|
723
|
-
f += 1;
|
|
724
|
-
continue;
|
|
725
|
-
}
|
|
726
|
-
if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
return this.materializeIndicesResult(d);
|
|
730
|
-
}
|
|
731
|
-
if (u) {
|
|
732
|
-
for (let e = 0; e < n.length; e++) {
|
|
733
|
-
let i = n[e], a = !1;
|
|
734
|
-
for (let e = 0; e < o; e++) {
|
|
735
|
-
let n = s[e][i];
|
|
736
|
-
if (n && n.includes(t)) {
|
|
737
|
-
a = !0;
|
|
738
|
-
break;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
if (a) {
|
|
742
|
-
if (f < r.offset) {
|
|
743
|
-
f += 1;
|
|
744
|
-
continue;
|
|
745
|
-
}
|
|
746
|
-
if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
return this.materializeIndicesResult(d);
|
|
750
|
-
}
|
|
751
|
-
for (let a = 0; a < n.length; a++) {
|
|
752
|
-
let l = n[a], u = e[l], p = !1;
|
|
753
|
-
for (let e = 0; e < o; e++) {
|
|
754
|
-
let n = s[e];
|
|
755
|
-
if (n) {
|
|
756
|
-
let e = n[l];
|
|
757
|
-
if (e && e.includes(t)) {
|
|
758
|
-
p = !0;
|
|
759
|
-
break;
|
|
760
|
-
}
|
|
761
|
-
} else {
|
|
762
|
-
let n = u[i[e]];
|
|
763
|
-
if (typeof n == "string" && n.toLowerCase().includes(t)) {
|
|
764
|
-
p = !0;
|
|
765
|
-
break;
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
if (!p && c && (p = this.nestedCollection.matchesAnyField(u, t)), p) {
|
|
770
|
-
if (f < r.offset) {
|
|
771
|
-
f += 1;
|
|
772
|
-
continue;
|
|
773
|
-
}
|
|
774
|
-
if (d.push(l), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
return this.materializeIndicesResult(d);
|
|
778
|
-
}
|
|
779
|
-
if (l !== null) {
|
|
780
|
-
for (let n = 0; n < e.length; n++) if (l[n]?.includes(t)) {
|
|
781
|
-
if (f < r.offset) {
|
|
782
|
-
f += 1;
|
|
783
|
-
continue;
|
|
784
|
-
}
|
|
785
|
-
if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
|
|
786
|
-
}
|
|
787
|
-
return this.materializeIndicesResult(d);
|
|
788
|
-
}
|
|
789
|
-
if (u) {
|
|
790
|
-
for (let n = 0; n < e.length; n++) {
|
|
791
|
-
let e = !1;
|
|
792
|
-
for (let r = 0; r < o; r++) {
|
|
793
|
-
let i = s[r][n];
|
|
794
|
-
if (i && i.includes(t)) {
|
|
795
|
-
e = !0;
|
|
796
|
-
break;
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
if (e) {
|
|
800
|
-
if (f < r.offset) {
|
|
801
|
-
f += 1;
|
|
802
|
-
continue;
|
|
803
|
-
}
|
|
804
|
-
if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
return this.materializeIndicesResult(d);
|
|
808
|
-
}
|
|
809
|
-
for (let n = 0; n < e.length; n++) {
|
|
810
|
-
let a = e[n], l = !1;
|
|
811
|
-
for (let e = 0; e < o; e++) {
|
|
812
|
-
let r = s[e];
|
|
813
|
-
if (r) {
|
|
814
|
-
let e = r[n];
|
|
815
|
-
if (e && e.includes(t)) {
|
|
816
|
-
l = !0;
|
|
817
|
-
break;
|
|
818
|
-
}
|
|
819
|
-
} else {
|
|
820
|
-
let n = a[i[e]];
|
|
821
|
-
if (typeof n == "string" && n.toLowerCase().includes(t)) {
|
|
822
|
-
l = !0;
|
|
823
|
-
break;
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
if (!l && c && (l = this.nestedCollection.matchesAnyField(a, t)), l) {
|
|
828
|
-
if (f < r.offset) {
|
|
829
|
-
f += 1;
|
|
830
|
-
continue;
|
|
831
|
-
}
|
|
832
|
-
if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
return this.materializeIndicesResult(d);
|
|
836
|
-
}
|
|
837
|
-
searchLinearSingleField(e, t, n, r, i) {
|
|
838
|
-
if (!e.length) return {
|
|
839
|
-
items: [],
|
|
840
|
-
indices: []
|
|
841
|
-
};
|
|
842
|
-
if (this.nestedCollection.hasField(t)) {
|
|
843
|
-
let a = this.nestedCollection.searchFieldLinearIndices(e, t, n, r ?? void 0);
|
|
844
|
-
return this.collectItemsFromIndices(a, i);
|
|
845
|
-
}
|
|
846
|
-
let a = [], o = this.flatIndexes.get(t), s = o ? o.normalizedValues : this.normalizedValuesCache.get(t) ?? (e === this.dataset ? this.buildNormalizedValuesOnly(e, t) : null), c = 0;
|
|
847
|
-
if (r !== null) {
|
|
848
|
-
let e = this.dataset;
|
|
849
|
-
if (s) {
|
|
850
|
-
for (let e = 0; e < r.length; e++) {
|
|
851
|
-
let t = r[e];
|
|
852
|
-
if (s[t]?.includes(n)) {
|
|
853
|
-
if (c < i.offset) {
|
|
854
|
-
c += 1;
|
|
855
|
-
continue;
|
|
856
|
-
}
|
|
857
|
-
if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
|
|
858
|
-
}
|
|
859
|
-
}
|
|
860
|
-
return this.materializeIndicesResult(a);
|
|
861
|
-
}
|
|
862
|
-
for (let o = 0; o < r.length; o++) {
|
|
863
|
-
let s = r[o], l = e[s];
|
|
864
|
-
if (typeof l[t] == "string" && l[t].toLowerCase().includes(n)) {
|
|
865
|
-
if (c < i.offset) {
|
|
866
|
-
c += 1;
|
|
867
|
-
continue;
|
|
868
|
-
}
|
|
869
|
-
if (a.push(s), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
return this.materializeIndicesResult(a);
|
|
873
|
-
}
|
|
874
|
-
if (s) {
|
|
875
|
-
for (let t = 0; t < e.length; t++) if (s[t]?.includes(n)) {
|
|
876
|
-
if (c < i.offset) {
|
|
877
|
-
c += 1;
|
|
878
|
-
continue;
|
|
879
|
-
}
|
|
880
|
-
if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
|
|
881
|
-
}
|
|
882
|
-
return this.materializeIndicesResult(a);
|
|
883
|
-
}
|
|
884
|
-
for (let r = 0; r < e.length; r++) if (typeof e[r][t] == "string" && e[r][t].toLowerCase().includes(n)) {
|
|
885
|
-
if (c < i.offset) {
|
|
886
|
-
c += 1;
|
|
887
|
-
continue;
|
|
888
|
-
}
|
|
889
|
-
if (a.push(r), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
|
|
890
|
-
}
|
|
891
|
-
return this.materializeIndicesResult(a);
|
|
892
|
-
}
|
|
893
|
-
clearIndexes() {
|
|
894
|
-
return this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this;
|
|
895
|
-
}
|
|
896
|
-
getOriginData() {
|
|
897
|
-
return this.state.getOriginData();
|
|
898
|
-
}
|
|
899
|
-
data(e) {
|
|
900
|
-
return this.state.data(e), this;
|
|
901
|
-
}
|
|
902
|
-
add(e) {
|
|
903
|
-
return this.state.add(e), this;
|
|
904
|
-
}
|
|
905
|
-
update(e) {
|
|
906
|
-
return this.state.update(e), this;
|
|
907
|
-
}
|
|
908
|
-
applyAddedItems(e, t) {
|
|
909
|
-
if (e.length === 0) return this;
|
|
910
|
-
for (let n of this.indexedFields) this.addItemsToField(n, e, t);
|
|
911
|
-
return this.nestedCollection.addItems(e, t), this;
|
|
912
|
-
}
|
|
913
|
-
clearData() {
|
|
914
|
-
return this.state.clearData(), this;
|
|
915
|
-
}
|
|
916
|
-
handleStateMutation(e) {
|
|
917
|
-
if (this.shouldDeferMutationIndexUpdates() && (e.type === "add" || e.type === "update")) {
|
|
918
|
-
this.markDeferredMutationState();
|
|
919
|
-
return;
|
|
920
|
-
}
|
|
921
|
-
switch (e.type) {
|
|
922
|
-
case "add":
|
|
923
|
-
this.applyAddedItems(e.items, e.startIndex);
|
|
924
|
-
for (let [t, n] of this.normalizedValuesCache) for (let r = 0; r < e.items.length; r++) {
|
|
925
|
-
let i = e.items[r][t];
|
|
926
|
-
n[e.startIndex + r] = typeof i == "string" ? i.toLowerCase() : "";
|
|
927
|
-
}
|
|
928
|
-
this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
|
|
929
|
-
return;
|
|
930
|
-
case "update":
|
|
931
|
-
this.applyUpdatedItem(e.index, e.previousItem, e.nextItem), this.invalidateNormalizedValuesCacheEntry(e.index, e.nextItem), this.clearPreviousSearchState();
|
|
932
|
-
return;
|
|
933
|
-
case "data":
|
|
934
|
-
this.runtime.deferredMutationVersion = null, this.rebuildConfiguredIndexes(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
|
|
935
|
-
return;
|
|
936
|
-
case "clearData":
|
|
937
|
-
this.runtime.deferredMutationVersion = null, this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
|
|
938
|
-
return;
|
|
939
|
-
case "remove":
|
|
940
|
-
this.applyRemovedItem(e.removedItem, e.removedIndex, e.movedItem, e.movedFromIndex), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
|
|
941
|
-
return;
|
|
942
|
-
case "removeMany":
|
|
943
|
-
for (let t = 0; t < e.entries.length; t++) {
|
|
944
|
-
let n = e.entries[t];
|
|
945
|
-
this.applyRemovedItem(n.removedItem, n.removedIndex, n.movedItem, n.movedFromIndex);
|
|
946
|
-
}
|
|
947
|
-
this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
|
|
948
|
-
return;
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
addItemsToField(e, t, n) {
|
|
952
|
-
let r = this.flatIndexes.get(e);
|
|
953
|
-
if (!r) return;
|
|
954
|
-
let { ngramMap: i, normalizedValues: a } = r;
|
|
955
|
-
for (let r = 0; r < t.length; r++) {
|
|
956
|
-
let o = t[r][e];
|
|
957
|
-
if (typeof o != "string") continue;
|
|
958
|
-
let s = o.toLowerCase(), c = n + r;
|
|
959
|
-
a[c] = s, d(i, s, c);
|
|
960
|
-
}
|
|
961
|
-
r.version = this.state.getMutationVersion();
|
|
962
|
-
}
|
|
963
|
-
applyUpdatedItem(e, t, n) {
|
|
964
|
-
for (let r of this.indexedFields) this.updateIndexedField(r, e, t, n);
|
|
965
|
-
this.nestedCollection.updateItem(n, t, e);
|
|
966
|
-
}
|
|
967
|
-
applyRemovedItem(e, t, n, r) {
|
|
968
|
-
for (let i of this.indexedFields) this.removeIndexedFieldValue(i, e, t), n !== null && r !== null && this.moveIndexedFieldValue(i, n, r, t);
|
|
969
|
-
this.nestedCollection.removeItem(e, t), n !== null && r !== null && this.nestedCollection.moveItem(n, r, t);
|
|
970
|
-
}
|
|
971
|
-
updateIndexedField(e, t, n, r) {
|
|
972
|
-
let i = this.flatIndexes.get(e);
|
|
973
|
-
if (!i) return;
|
|
974
|
-
if (n[e] === r[e]) {
|
|
975
|
-
i.version = this.state.getMutationVersion();
|
|
976
|
-
return;
|
|
977
|
-
}
|
|
978
|
-
let { ngramMap: a, normalizedValues: o } = i, s = this.getNormalizedFieldValue(n, e);
|
|
979
|
-
s && m(a, s, t);
|
|
980
|
-
let c = this.getNormalizedFieldValue(r, e);
|
|
981
|
-
if (!c) {
|
|
982
|
-
delete o[t], i.version = this.state.getMutationVersion();
|
|
983
|
-
return;
|
|
984
|
-
}
|
|
985
|
-
o[t] = c, d(a, c, t), i.version = this.state.getMutationVersion();
|
|
986
|
-
}
|
|
987
|
-
removeIndexedFieldValue(e, t, n) {
|
|
988
|
-
let r = this.flatIndexes.get(e);
|
|
989
|
-
if (!r) return;
|
|
990
|
-
let { ngramMap: i, normalizedValues: a } = r, o = this.getNormalizedFieldValue(t, e);
|
|
991
|
-
o && m(i, o, n), delete a[n], r.version = this.state.getMutationVersion();
|
|
992
|
-
}
|
|
993
|
-
moveIndexedFieldValue(e, t, n, r) {
|
|
994
|
-
if (n === r) return;
|
|
995
|
-
let i = this.flatIndexes.get(e);
|
|
996
|
-
if (!i) return;
|
|
997
|
-
let { ngramMap: a, normalizedValues: o } = i, s = o[n] ?? this.getNormalizedFieldValue(t, e);
|
|
998
|
-
if (!s) {
|
|
999
|
-
delete o[n], i.version = this.state.getMutationVersion();
|
|
1000
|
-
return;
|
|
1001
|
-
}
|
|
1002
|
-
m(a, s, n), d(a, s, r), o[r] = s, delete o[n], i.version = this.state.getMutationVersion();
|
|
1003
|
-
}
|
|
1004
|
-
getNormalizedFieldValue(e, t) {
|
|
1005
|
-
let n = e[t];
|
|
1006
|
-
return typeof n == "string" ? n.toLowerCase() : null;
|
|
1007
|
-
}
|
|
1008
|
-
};
|
|
1009
|
-
//#endregion
|
|
1010
|
-
export { _ as TextSearchEngine };
|
|
1
|
+
import{a as e,i as t,n as s}from"../chunks/constants-BXQI1M5E.mjs";import{n as i}from"../chunks/internal-COJC3Ik3.mjs";function n(e,t,s,i){const n=function(e,t){const s=[];for(const i of t){const t=e.get(i);if(!t)return null;s.push(t)}return function(e){for(let t=0;t<e.length-1;t++){let s=t;for(let i=t+1;i<e.length;i++)e[i].size<e[s].size&&(s=i);if(s!==t){const i=e[t];e[t]=e[s],e[s]=i}}}(s),s}(e,t);if(null===n)return null;const r=n[0];return{smallestPostingList:r,matches:e=>r.has(e)&&function(e,t){for(let s=0;s<t.length;s++)if(!t[s].has(e))return!1;return!0}(e,n)&&Boolean(s[e]?.includes(i))}}function r(e,t){const s=e.get(t);if(s)return s;const i=/* @__PURE__ */new Set;return e.set(t,i),i}function l(e,t,s){const i=Math.min(3,t.length);for(let n=2;n<=i;n++){const i=t.length-n;for(let l=0;l<=i;l++)r(e,t.substring(l,l+n)).add(s)}}function a(e,t,s,i,r={}){const{restrictionLookup:l=null,take:a=Number.POSITIVE_INFINITY}=r,o=n(e,t,s,i);if(null===o)return[];const d=[];for(const n of o.smallestPostingList)if((null===l||l[n])&&o.matches(n)&&(d.push(n),d.length>=a))break;return d}function o(e,t,s,i,r){const{candidateIndices:l,restrictionLookup:a=null,take:o=Number.POSITIVE_INFINITY}=r;if(0===l.length)return[];const d=n(e,t,s,i);if(null===d)return[];const h=[];if(null===a||l.length<=d.smallestPostingList.size){for(let e=0;e<l.length;e++){const t=l[e];if(d.matches(t)&&(h.push(t),h.length>=o))break}return h}for(const n of d.smallestPostingList)if(a[n]&&d.matches(n)&&(h.push(n),h.length>=o))break;return h}function d(e,t,s){const i=Math.min(3,t.length);for(let n=2;n<=i;n++){const i=t.length-n;for(let r=0;r<=i;r++){const i=t.substring(r,r+n),l=e.get(i);l&&(l.delete(s),0===l.size&&e.delete(i))}}}var h=class{constructor(e={ngramIndexes:/* @__PURE__ */new Map,normalizedFieldValues:/* @__PURE__ */new Map}){this.storage=e,this.registeredFields=/* @__PURE__ */new Set,this.fieldDescriptors=/* @__PURE__ */new Map}registerFields(e){if(e?.length)for(const t of e)this.registerField(t)}hasRegisteredFields(){return this.registeredFields.size>0}hasField(e){return this.registeredFields.has(e)}hasIndexes(){return this.storage.ngramIndexes.size>0}clearIndexes(){this.storage.ngramIndexes.clear(),this.storage.normalizedFieldValues.clear()}buildIndexes(e){this.clearIndexes();for(const t of this.registeredFields)this.buildIndex(e,t)}addItems(e,t){if(0!==e.length&&0!==this.storage.ngramIndexes.size)for(const s of this.storage.ngramIndexes.keys())this.addItemsToField(s,e,t)}updateItem(e,t,s){if(0!==this.storage.ngramIndexes.size)for(const i of this.storage.ngramIndexes.keys())this.updateItemInField(i,e,t,s)}removeItem(e,t){if(0!==this.storage.ngramIndexes.size)for(const s of this.storage.ngramIndexes.keys())this.removeItemFromField(s,e,t)}moveItem(e,t,s){if(0!==this.storage.ngramIndexes.size&&t!==s)for(const i of this.storage.ngramIndexes.keys())this.moveItemForField(i,e,t,s)}searchAllIndexedFieldIndices(e,t,s,i){const n=/* @__PURE__ */new Set,r=[];for(const l of this.storage.ngramIndexes.keys())for(const a of this.searchIndexedFieldIndices(l,e,t,s,i))n.has(a)||(n.add(a),r.push(a));return r}searchIndexedField(e,t,s,i,n,r,l=Number.POSITIVE_INFINITY){const a=this.searchIndexedFieldIndices(t,s,i,n,r,l),o=[];for(let d=0;d<a.length;d++){const t=e[a[d]];t&&o.push(t)}return o}searchIndexedFieldIndices(e,t,s,i,n,r=Number.POSITIVE_INFINITY){const l=this.storage.ngramIndexes.get(e);if(!l)return[];const d=this.storage.normalizedFieldValues.get(e)??[];return null!=n?o(l,s,d,t,{candidateIndices:n,restrictionLookup:i,take:r}):a(l,s,d,t,{restrictionLookup:i,take:r})}searchFieldLinear(e,t,s){const i=this.searchFieldLinearIndices(e,t,s),n=[];for(let r=0;r<i.length;r++){const t=e[i[r]];t&&n.push(t)}return n}searchFieldLinearIndices(e,t,s,i){const n=this.fieldDescriptors.get(t);if(!n)return[];const{collectionKey:r,nestedKey:l}=n,a=[];if(i){for(let t=0;t<i.length;t++){const n=i[t],o=e[n][r];if(!Array.isArray(o))continue;let d=!1;for(let e=0;e<o.length;e++){const t=o[e][l];if("string"==typeof t&&t.toLowerCase().includes(s)){d=!0;break}}d&&a.push(n)}return a}for(let o=0;o<e.length;o++){const t=e[o][r];if(!Array.isArray(t))continue;let i=!1;for(let e=0;e<t.length;e++){const n=t[e][l];if("string"==typeof n&&n.toLowerCase().includes(s)){i=!0;break}}i&&a.push(o)}return a}matchesAnyField(e,t){for(const s of this.registeredFields){const i=this.fieldDescriptors.get(s);if(!i)continue;const{collectionKey:n,nestedKey:r}=i,l=e[n];if(Array.isArray(l))for(let e=0;e<l.length;e++){const s=l[e][r];if("string"==typeof s&&s.toLowerCase().includes(t))return!0}}return!1}registerField(e){const t=i(e);t&&(this.registeredFields.add(e),this.fieldDescriptors.set(e,t))}getNormalizedValues(e){const t=this.storage.normalizedFieldValues.get(e);if(t)return t;const s=[];return this.storage.normalizedFieldValues.set(e,s),s}buildIndex(e,t){const s=this.fieldDescriptors.get(t);if(!s)return;const{collectionKey:i,nestedKey:n}=s,r=/* @__PURE__ */new Map,a=new Array(e.length);for(let o=0,d=e.length;o<d;o++){const t=e[o][i];if(!Array.isArray(t))continue;const s=[];for(let e=0;e<t.length;e++){const i=t[e][n];"string"==typeof i&&s.push(i.toLowerCase())}if(0===s.length)continue;const d=s.join("\n");a[o]=d,l(r,d,o)}this.storage.ngramIndexes.set(t,r),this.storage.normalizedFieldValues.set(t,a)}addItemsToField(e,t,s){const i=this.fieldDescriptors.get(e),n=this.storage.ngramIndexes.get(e);if(!i||!n)return;const r=this.getNormalizedValues(e),{collectionKey:a,nestedKey:o}=i;for(let d=0;d<t.length;d++){const e=t[d][a];if(!Array.isArray(e))continue;const i=[],h=s+d;for(let t=0;t<e.length;t++){const s=e[t][o];"string"==typeof s&&i.push(s.toLowerCase())}if(0===i.length)continue;const u=i.join("\n");r[h]=u,l(n,u,h)}}updateItemInField(e,t,s,i){const n=this.storage.ngramIndexes.get(e);if(!n)return;const r=this.getNormalizedValues(e),a=this.getNormalizedItemValue(e,s),o=this.getNormalizedItemValue(e,t);a!==o&&(a&&d(n,a,i),o?(r[i]=o,l(n,o,i)):delete r[i])}removeItemFromField(e,t,s){const i=this.storage.ngramIndexes.get(e);if(!i)return;const n=this.getNormalizedValues(e),r=n[s]??this.getNormalizedItemValue(e,t);r&&d(i,r,s),delete n[s]}moveItemForField(e,t,s,i){const n=this.storage.ngramIndexes.get(e);if(!n)return;const r=this.getNormalizedValues(e),a=r[s]??this.getNormalizedItemValue(e,t);a?(d(n,a,s),l(n,a,i),r[i]=a,delete r[s]):delete r[s]}getNormalizedItemValue(e,t){const s=this.fieldDescriptors.get(e);if(!s)return null;const{collectionKey:i,nestedKey:n}=s,r=t[i];if(!Array.isArray(r))return null;const l=[];for(let a=0;a<r.length;a++){const e=r[a][n];"string"==typeof e&&l.push(e.toLowerCase())}return 0===l.length?null:l.join("\n")}},u=()=>({indexedFields:/* @__PURE__ */new Set,flatIndexes:/* @__PURE__ */new Map,nestedStorage:{ngramIndexes:/* @__PURE__ */new Map,normalizedFieldValues:/* @__PURE__ */new Map},deferredMutationVersion:null,filterByPreviousResult:!1,previousResultIndices:null,previousResultLookup:null,previousQuery:null,stats:{totalQueries:0,indexedQueries:0,fallbackQueries:0,fallbackFields:/* @__PURE__ */new Map}}),c=class{constructor(t={}){this.cachedIndexedFieldsList=null,this.cachedLinearSearchFieldsList=null,this.emittedWarningKeys=/* @__PURE__ */new Set,this.warnings=[],this.normalizedValuesCache=/* @__PURE__ */new Map,this.combinedNormalizedValuesCache=null,this.minQueryLength=t.minQueryLength??1,this.silent=t.silent??!1,this.state=t.state??new e(t.data??[]),this.namespace=this.state.createNamespace("search"),this.nestedCollection=new h(this.runtime.nestedStorage),this.nestedCollection.registerFields(t.nestedFields),this.state.subscribe(e=>this.handleStateMutation(e)),t.filterByPreviousResult&&(this.runtime.filterByPreviousResult=!0);const s=t.fields?.length,i=this.nestedCollection.hasRegisteredFields();if(s)for(const e of t.fields)this.indexedFields.add(e);this.dataset.length>0&&(s||i)&&this.rebuildConfiguredIndexes()}get dataset(){return this.state.getOriginData()}get runtime(){return this.state.getOrCreateScopedValue(this.namespace,"runtime",u)}get flatIndexes(){return this.runtime.flatIndexes}get indexedFields(){return this.runtime.indexedFields}shouldDeferMutationIndexUpdates(){return!0===this.state.getScopedValue(t,s)}markDeferredMutationState(){this.runtime.deferredMutationVersion=this.state.getMutationVersion(),this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.cachedIndexedFieldsList=null,this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,this.clearPreviousSearchState()}ensureConfiguredIndexesReady(){null!==this.runtime.deferredMutationVersion&&(this.runtime.deferredMutationVersion=null,this.dataset.length>0&&(this.indexedFields.size>0||this.nestedCollection.hasRegisteredFields())&&this.rebuildConfiguredIndexes())}rebuildConfiguredIndexes(){this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.cachedIndexedFieldsList=null,this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null;for(const e of this.indexedFields)this.buildIndexFromData(this.dataset,e);this.nestedCollection.hasRegisteredFields()&&this.nestedCollection.buildIndexes(this.dataset)}buildIndexFromData(e,t){const s=this.state.getMutationVersion(),i=/* @__PURE__ */new Map,n=new Array(e.length);for(let r=0,a=e.length;r<a;r++){const s=e[r][t];if("string"!=typeof s)continue;const a=s.toLowerCase();n[r]=a,l(i,a,r)}this.flatIndexes.set(t,{ngramMap:i,normalizedValues:n,version:s})}search(e,t,s){return"string"==typeof t?this.searchField(e,t,s):this.searchAll(e,t)}searchAll(e,t){return this.searchAllFields(e,t)}normalizeQuery(e){return e.trim().toLowerCase()}normalizeSearchWindow(e){const t=Math.max(0,Math.trunc(e?.offset??0)),s=e?.limit,i=void 0===s?Number.POSITIVE_INFINITY:Math.max(0,Math.trunc(s));return{offset:t,limit:i,take:Number.isFinite(i)?t+i:Number.POSITIVE_INFINITY,hasWindow:t>0||Number.isFinite(i)}}shouldTrackPreviousResult(e){return!e.hasWindow}hasReachedWindowLimit(e,t){return Number.isFinite(e.limit)&&t>=e.limit}sliceItems(e,t){return t.hasWindow?e.slice(t.offset,t.take):e}collectItemsFromIndices(e,t){const s=t.hasWindow&&e.length>0?e.slice(t.offset,t.take):e,i=[];for(let n=0;n<s.length;n++){const e=this.dataset[s[n]];e&&i.push(e)}return{items:i,indices:s}}createLookup(e){const t=new Uint8Array(this.dataset.length);for(let s=0;s<e.length;s++)t[e[s]]=1;return t}getRestrictionLookup(e){const t=this.runtime.previousResultLookup;if(null!==t&&this.runtime.previousResultIndices===e&&t.length===this.dataset.length)return t;const s=this.createLookup(e);return this.runtime.previousResultLookup=s,s}getSearchSource(e){const{runtime:t}=this;return t.filterByPreviousResult?null!==t.previousQuery&&null!==t.previousResultIndices&&e.includes(t.previousQuery)?{indices:t.previousResultIndices,lookup:this.getRestrictionLookup(t.previousResultIndices)}:(t.previousResultIndices=null,t.previousResultLookup=null,t.previousQuery=null,{indices:null,lookup:null}):{indices:null,lookup:null}}saveSearchResult(e,t){const{runtime:s}=this;s.filterByPreviousResult&&(s.previousResultIndices=e,s.previousResultLookup=null,s.previousQuery=t)}persistSearchResult(e,t,s){s&&this.saveSearchResult(e.indices,t)}resetSearchState(){return this.clearPreviousSearchState(),this}getWarnings(){return[...this.warnings]}getStats(){const{totalQueries:e,indexedQueries:t,fallbackQueries:s,fallbackFields:i}=this.runtime.stats;return{totalQueries:e,indexedQueries:t,fallbackQueries:s,fallbackRate:0===e?0:s/e,fallbackFields:Object.fromEntries(i)}}resetStats(){const{stats:e}=this.runtime;return e.totalQueries=0,e.indexedQueries=0,e.fallbackQueries=0,e.fallbackFields.clear(),this}clearPreviousSearchState(){const{runtime:e}=this;e.previousResultIndices=null,e.previousResultLookup=null,e.previousQuery=null}recordIndexedQuery(){const{stats:e}=this.runtime;e.totalQueries+=1,e.indexedQueries+=1}recordFallbackQuery(e){const{stats:t}=this.runtime;t.totalQueries+=1,t.fallbackQueries+=1,t.fallbackFields.set(e,(t.fallbackFields.get(e)??0)+1)}shouldEmitFallbackWarning(e){return!this.silent&&e.length>=2}warnAboutFallback(e,t,s){if(!this.shouldEmitFallbackWarning(t))return;const i=`${e}\0${t}\0${s}`;if(this.emittedWarningKeys.has(i))return;this.emittedWarningKeys.add(i);const n=`[TextSearchEngine] warn: query "${t}" on ${e} used linear fallback. ${s}. Add the field(s) to the index schema to enable indexed search.`;this.warnings.push(n),"undefined"!=typeof process&&"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.warn(n)}getResolvedFlatIndex(e){const t=this.state.getMutationVersion();let s=this.flatIndexes.get(e);return s&&s.version!==t&&this.dataset.length>0&&(this.buildIndexFromData(this.dataset,e),s=this.flatIndexes.get(e)),s}getQueryGrams(e){const t=function(e){const t=function(e){const t=Math.min(3,Math.max(2,e.length)),s=e.length-t+1,i=new Array(s);for(let n=0;n<s;n++)i[n]=e.substring(n,n+t);return i}(e);if(t.length<=12)return new Set(t);const s=/* @__PURE__ */new Set,i=t.length-1;for(let n=0;n<=11;n++){const e=Math.round(n*i/11);s.add(t[e])}return s}(e);return t.size>0?t:null}searchAllFields(e,t){const s=this.normalizeQuery(e),i=this.normalizeSearchWindow(t),n="all fields";if(0===i.limit)return[];if(!s||s.length<this.minQueryLength)return this.sliceItems(this.dataset,i);s.length>=2&&this.ensureConfiguredIndexesReady();const r=this.shouldTrackPreviousResult(i),{indices:l,lookup:a}=this.getSearchSource(s);let o;const d=()=>(void 0!==o||(o=s.length>=2?this.getQueryGrams(s):null),o);if(null!==l){const e=d();if(null!==e&&(this.flatIndexes.size>0||this.nestedCollection.hasIndexes())){this.recordIndexedQuery();const t=this.searchAllFieldsIndexed(s,e,i,a,l);return this.persistSearchResult(t,s,r),t.items}this.recordFallbackQuery(n);const t=this.searchLinearAllFields(this.dataset,s,l,i);return this.persistSearchResult(t,s,r),t.items}if(!this.flatIndexes.size&&!this.nestedCollection.hasIndexes()){this.warnAboutFallback(n,s,this.indexedFields.size>0?"configured indexes are not currently built":"no indexed fields are configured"),this.recordFallbackQuery(n);const e=this.searchLinearAllFields(this.dataset,s,null,i);return this.persistSearchResult(e,s,r),e.items}const h=d();if(null===h){this.recordFallbackQuery(n);const e=this.searchLinearAllFields(this.dataset,s,null,i);return this.persistSearchResult(e,s,r),e.items}this.recordIndexedQuery();const u=this.searchAllFieldsIndexed(s,h,i,null);return this.persistSearchResult(u,s,r),u.items}searchAllFieldsIndexed(e,t,s,i,n=null){if(null!==n&&!this.nestedCollection.hasRegisteredFields()&&this.flatIndexes.size>0)return this.searchAllFieldsIndexedInCandidates(e,t,s,i,n);const r=this.dataset,l=new Uint8Array(r.length),a=[];let o=0;for(const d of this.flatIndexes.keys()){const r=this.searchFieldWithPreparedQueryIndices(d,e,t,i,n);for(let e=0;e<r.length;e++){const t=r[e];if(!l[t])if(l[t]=1,o<s.offset)o+=1;else if(a.push(t),o+=1,this.hasReachedWindowLimit(s,a.length))return this.materializeIndicesResult(a)}}for(const d of this.nestedCollection.searchAllIndexedFieldIndices(e,t,i,n))if(!l[d])if(l[d]=1,o<s.offset)o+=1;else if(a.push(d),o+=1,this.hasReachedWindowLimit(s,a.length))return this.materializeIndicesResult(a);return this.materializeIndicesResult(a)}searchAllFieldsIndexedInCandidates(e,t,s,i,r){const l=[];for(const d of this.flatIndexes.keys()){const s=this.getResolvedFlatIndex(d);if(!s)continue;const i=n(s.ngramMap,t,s.normalizedValues,e);null!==i&&l.push(i.matches)}if(0===l.length)return{items:[],indices:[]};const a=[];let o=0;for(let n=0;n<r.length;n++){const e=r[n];if(null!==i&&!i[e])continue;let t=!1;for(let s=0;s<l.length;s++)if(l[s](e)){t=!0;break}if(t)if(o<s.offset)o+=1;else if(a.push(e),o+=1,this.hasReachedWindowLimit(s,a.length))break}return this.materializeIndicesResult(a)}searchField(e,t,s){const i=this.normalizeQuery(t),n=this.normalizeSearchWindow(s),r=`field "${e}"`;if(0===n.limit)return[];if(!i||i.length<this.minQueryLength)return this.sliceItems(this.dataset,n);i.length>=2&&this.ensureConfiguredIndexesReady();const l=this.shouldTrackPreviousResult(n),{indices:a,lookup:o}=this.getSearchSource(i),d=this.nestedCollection.hasField(e);let h;const u=()=>(void 0!==h||(h=i.length>=2?this.getQueryGrams(i):null),h);if(null!==a){const t=u();if(d){if(null!==t&&this.nestedCollection.hasIndexes()){this.recordIndexedQuery();const s=this.nestedCollection.searchIndexedFieldIndices(e,i,t,o,a,n.take),r=this.collectItemsFromIndices(s,n);return this.persistSearchResult(r,i,l),r.items}this.recordFallbackQuery(r);const s=this.searchLinearSingleField(this.dataset,e,i,a,n);return this.persistSearchResult(s,i,l),s.items}if(null!==t&&this.flatIndexes.has(e)){this.recordIndexedQuery();const s=this.searchFieldWithPreparedQuery(e,i,t,n,o,a);return this.persistSearchResult(s,i,l),s.items}this.recordFallbackQuery(r);const s=this.searchLinearSingleField(this.dataset,e,i,a,n);return this.persistSearchResult(s,i,l),s.items}if(d){if(i.length>=2&&this.nestedCollection.hasIndexes()){const t=this.getQueryGrams(i);if(null===t)return[];this.recordIndexedQuery();const s=this.nestedCollection.searchIndexedFieldIndices(e,i,t,null,null,n.take),r=this.collectItemsFromIndices(s,n);return this.persistSearchResult(r,i,l),r.items}this.recordFallbackQuery(r);const t=this.searchLinearSingleField(this.dataset,e,i,null,n);return this.persistSearchResult(t,i,l),t.items}this.flatIndexes.size||this.warnAboutFallback(r,i,this.indexedFields.size>0?`field "${e}" is not backed by an active index`:"no indexed fields are configured");const c=u();if(this.flatIndexes.size>0&&null!==c){if(!this.flatIndexes.has(e))return[];this.recordIndexedQuery();const t=this.searchFieldWithPreparedQuery(e,i,c,n);return this.persistSearchResult(t,i,l),t.items}this.recordFallbackQuery(r);const f=this.searchLinearSingleField(this.dataset,e,i,null,n);return this.persistSearchResult(f,i,l),f.items}searchFieldWithPreparedQuery(e,t,s,i,n=null,r=null){const l=this.searchFieldWithPreparedQueryIndices(e,t,s,n,r,i.take);return this.collectItemsFromIndices(l,i)}searchFieldWithPreparedQueryIndices(e,t,s,i=null,n=null,r=Number.POSITIVE_INFINITY){const l=this.getResolvedFlatIndex(e);if(!l)return[];const{ngramMap:d,normalizedValues:h}=l;return null!==n?o(d,s,h,t,{candidateIndices:n,restrictionLookup:i,take:r}):a(d,s,h,t,{restrictionLookup:i,take:r})}getIndexedFieldsList(){return null===this.cachedIndexedFieldsList&&(this.cachedIndexedFieldsList=Array.from(this.indexedFields)),this.cachedIndexedFieldsList}materializeIndicesResult(e){const t=[];for(let s=0;s<e.length;s++){const i=this.dataset[e[s]];i&&t.push(i)}return{items:t,indices:e}}getLinearSearchFields(e){const t=this.getIndexedFieldsList();return t.length>0?t:(null!==this.cachedLinearSearchFieldsList||(this.cachedLinearSearchFieldsList=e.length?Object.keys(e[0]).filter(t=>"string"==typeof e[0][t]):[]),this.cachedLinearSearchFieldsList)}buildNormalizedValuesOnly(e,t){const s=new Array(e.length);for(let i=0,n=e.length;i<n;i++){const n=e[i][t];"string"==typeof n&&(s[i]=n.toLowerCase())}return this.normalizedValuesCache.set(t,s),s}buildCombinedNormalizedValues(e,t){const s=new Array(e.length);for(let i=0;i<e.length;i++)s[i]=this.buildCombinedNormalizedValue(e[i],t);return this.combinedNormalizedValuesCache={fieldsKey:t.join("\0"),values:s},s}buildCombinedNormalizedValue(e,t){let s="";for(let i=0;i<t.length;i++){const n=e[t[i]];"string"==typeof n&&(s&&(s+="\n"),s+=n.toLowerCase())}return s}getCombinedNormalizedValues(e,t){if(e!==this.dataset)return null;const s=t.join("\0");return this.combinedNormalizedValuesCache?.fieldsKey===s?this.combinedNormalizedValuesCache.values:this.buildCombinedNormalizedValues(e,t)}invalidateNormalizedValuesCacheEntry(e,t){for(const[s,i]of this.normalizedValuesCache){const n=t[s];i[e]="string"==typeof n?n.toLowerCase():""}if(null!==this.combinedNormalizedValuesCache){const s=this.combinedNormalizedValuesCache.fieldsKey.split("\0");this.combinedNormalizedValuesCache.values[e]=this.buildCombinedNormalizedValue(t,s)}}searchLinearAllFields(e,t,s,i){if(!e.length)return{items:[],indices:[]};const n=this.getLinearSearchFields(e),r=e===this.dataset,l=n.length,a=new Array(l);for(let f=0;f<l;f++){const t=n[f],s=this.flatIndexes.get(t);if(s)a[f]=s.normalizedValues;else{const s=this.normalizedValuesCache.get(t);a[f]=s||(r?this.buildNormalizedValuesOnly(e,t):null)}}const o=this.nestedCollection.hasRegisteredFields(),d=o?null:this.getCombinedNormalizedValues(e,n),h=!o&&a.every(e=>null!==e),u=[];let c=0;if(null!==s){const e=this.dataset;if(null!==d){for(let e=0;e<s.length;e++){const n=s[e];if(d[n]?.includes(t))if(c<i.offset)c+=1;else if(u.push(n),c+=1,this.hasReachedWindowLimit(i,u.length))break}return this.materializeIndicesResult(u)}if(h){for(let e=0;e<s.length;e++){const n=s[e];let r=!1;for(let e=0;e<l;e++){const s=a[e][n];if(s&&s.includes(t)){r=!0;break}}if(r)if(c<i.offset)c+=1;else if(u.push(n),c+=1,this.hasReachedWindowLimit(i,u.length))break}return this.materializeIndicesResult(u)}for(let r=0;r<s.length;r++){const d=s[r],h=e[d];let f=!1;for(let e=0;e<l;e++){const s=a[e];if(s){const e=s[d];if(e&&e.includes(t)){f=!0;break}}else{const s=h[n[e]];if("string"==typeof s&&s.toLowerCase().includes(t)){f=!0;break}}}if(!f&&o&&(f=this.nestedCollection.matchesAnyField(h,t)),f)if(c<i.offset)c+=1;else if(u.push(d),c+=1,this.hasReachedWindowLimit(i,u.length))break}return this.materializeIndicesResult(u)}if(null!==d){for(let s=0;s<e.length;s++)if(d[s]?.includes(t))if(c<i.offset)c+=1;else if(u.push(s),c+=1,this.hasReachedWindowLimit(i,u.length))break;return this.materializeIndicesResult(u)}if(h){for(let s=0;s<e.length;s++){let e=!1;for(let i=0;i<l;i++){const n=a[i][s];if(n&&n.includes(t)){e=!0;break}}if(e)if(c<i.offset)c+=1;else if(u.push(s),c+=1,this.hasReachedWindowLimit(i,u.length))break}return this.materializeIndicesResult(u)}for(let f=0;f<e.length;f++){const s=e[f];let r=!1;for(let e=0;e<l;e++){const i=a[e];if(i){const e=i[f];if(e&&e.includes(t)){r=!0;break}}else{const i=s[n[e]];if("string"==typeof i&&i.toLowerCase().includes(t)){r=!0;break}}}if(!r&&o&&(r=this.nestedCollection.matchesAnyField(s,t)),r)if(c<i.offset)c+=1;else if(u.push(f),c+=1,this.hasReachedWindowLimit(i,u.length))break}return this.materializeIndicesResult(u)}searchLinearSingleField(e,t,s,i,n){if(!e.length)return{items:[],indices:[]};if(this.nestedCollection.hasField(t)){const r=this.nestedCollection.searchFieldLinearIndices(e,t,s,i??void 0);return this.collectItemsFromIndices(r,n)}const r=[],l=this.flatIndexes.get(t),a=l?l.normalizedValues:this.normalizedValuesCache.get(t)??(e===this.dataset?this.buildNormalizedValuesOnly(e,t):null);let o=0;if(null!==i){const e=this.dataset;if(a){for(let e=0;e<i.length;e++){const t=i[e];if(a[t]?.includes(s))if(o<n.offset)o+=1;else if(r.push(t),o+=1,this.hasReachedWindowLimit(n,r.length))break}return this.materializeIndicesResult(r)}for(let l=0;l<i.length;l++){const a=i[l],d=e[a];if("string"==typeof d[t]&&d[t].toLowerCase().includes(s))if(o<n.offset)o+=1;else if(r.push(a),o+=1,this.hasReachedWindowLimit(n,r.length))break}return this.materializeIndicesResult(r)}if(a){for(let t=0;t<e.length;t++)if(a[t]?.includes(s))if(o<n.offset)o+=1;else if(r.push(t),o+=1,this.hasReachedWindowLimit(n,r.length))break;return this.materializeIndicesResult(r)}for(let d=0;d<e.length;d++)if("string"==typeof e[d][t]&&e[d][t].toLowerCase().includes(s))if(o<n.offset)o+=1;else if(r.push(d),o+=1,this.hasReachedWindowLimit(n,r.length))break;return this.materializeIndicesResult(r)}clearIndexes(){return this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,this}getOriginData(){return this.state.getOriginData()}data(e){return this.state.data(e),this}add(e){return this.state.add(e),this}update(e){return this.state.update(e),this}applyAddedItems(e,t){if(0===e.length)return this;for(const s of this.indexedFields)this.addItemsToField(s,e,t);return this.nestedCollection.addItems(e,t),this}clearData(){return this.state.clearData(),this}handleStateMutation(e){if(!this.shouldDeferMutationIndexUpdates()||"add"!==e.type&&"update"!==e.type)switch(e.type){case"add":this.applyAddedItems(e.items,e.startIndex);for(const[t,s]of this.normalizedValuesCache)for(let i=0;i<e.items.length;i++){const n=e.items[i][t];s[e.startIndex+i]="string"==typeof n?n.toLowerCase():""}return this.combinedNormalizedValuesCache=null,void this.clearPreviousSearchState();case"update":return this.applyUpdatedItem(e.index,e.previousItem,e.nextItem),this.invalidateNormalizedValuesCacheEntry(e.index,e.nextItem),void this.clearPreviousSearchState();case"data":return this.runtime.deferredMutationVersion=null,this.rebuildConfiguredIndexes(),this.combinedNormalizedValuesCache=null,void this.clearPreviousSearchState();case"clearData":return this.runtime.deferredMutationVersion=null,this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,void this.clearPreviousSearchState();case"remove":return this.applyRemovedItem(e.removedItem,e.removedIndex,e.movedItem,e.movedFromIndex),this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,void this.clearPreviousSearchState();case"removeMany":for(let t=0;t<e.entries.length;t++){const s=e.entries[t];this.applyRemovedItem(s.removedItem,s.removedIndex,s.movedItem,s.movedFromIndex)}return this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,void this.clearPreviousSearchState()}else this.markDeferredMutationState()}addItemsToField(e,t,s){const i=this.flatIndexes.get(e);if(!i)return;const{ngramMap:n,normalizedValues:r}=i;for(let a=0;a<t.length;a++){const i=t[a][e];if("string"!=typeof i)continue;const o=i.toLowerCase(),d=s+a;r[d]=o,l(n,o,d)}i.version=this.state.getMutationVersion()}applyUpdatedItem(e,t,s){for(const i of this.indexedFields)this.updateIndexedField(i,e,t,s);this.nestedCollection.updateItem(s,t,e)}applyRemovedItem(e,t,s,i){for(const n of this.indexedFields)this.removeIndexedFieldValue(n,e,t),null!==s&&null!==i&&this.moveIndexedFieldValue(n,s,i,t);this.nestedCollection.removeItem(e,t),null!==s&&null!==i&&this.nestedCollection.moveItem(s,i,t)}updateIndexedField(e,t,s,i){const n=this.flatIndexes.get(e);if(!n)return;if(s[e]===i[e])return void(n.version=this.state.getMutationVersion());const{ngramMap:r,normalizedValues:a}=n,o=this.getNormalizedFieldValue(s,e);o&&d(r,o,t);const h=this.getNormalizedFieldValue(i,e);if(!h)return delete a[t],void(n.version=this.state.getMutationVersion());a[t]=h,l(r,h,t),n.version=this.state.getMutationVersion()}removeIndexedFieldValue(e,t,s){const i=this.flatIndexes.get(e);if(!i)return;const{ngramMap:n,normalizedValues:r}=i,l=this.getNormalizedFieldValue(t,e);l&&d(n,l,s),delete r[s],i.version=this.state.getMutationVersion()}moveIndexedFieldValue(e,t,s,i){if(s===i)return;const n=this.flatIndexes.get(e);if(!n)return;const{ngramMap:r,normalizedValues:a}=n,o=a[s]??this.getNormalizedFieldValue(t,e);if(!o)return delete a[s],void(n.version=this.state.getMutationVersion());d(r,o,s),l(r,o,i),a[i]=o,delete a[s],n.version=this.state.getMutationVersion()}getNormalizedFieldValue(e,t){const s=e[t];return"string"==typeof s?s.toLowerCase():null}};export{c as TextSearchEngine};
|