@minnowdb/core 0.7.0 → 0.7.1
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/block-format/block.js +254 -325
- package/dist/block-format/checksum.js +30 -52
- package/dist/block-format/codecs.js +112 -125
- package/dist/block-format/column.js +187 -205
- package/dist/block-format/index.js +5 -1
- package/dist/block-format/physical.js +371 -387
- package/dist/block-format/types.js +4 -1
- package/dist/block-format/unicode.js +30 -39
- package/dist/date-value.js +67 -78
- package/dist/engine/artifact-cache.js +106 -116
- package/dist/engine/batch.js +36 -44
- package/dist/engine/buffered-writer.js +156 -158
- package/dist/engine/byte-estimates.js +20 -22
- package/dist/engine/cache-limits.js +24 -25
- package/dist/engine/cancellation.js +5 -3
- package/dist/engine/catalog.js +64 -73
- package/dist/engine/client.js +715 -780
- package/dist/engine/database.js +15941 -19313
- package/dist/engine/defaults.js +104 -111
- package/dist/engine/errors.js +113 -128
- package/dist/engine/fts.js +233 -280
- package/dist/engine/group-index.js +287 -321
- package/dist/engine/index.js +23 -12
- package/dist/engine/join-index.js +177 -186
- package/dist/engine/keyed-live.js +168 -173
- package/dist/engine/live-api.js +0 -1
- package/dist/engine/live-equal.js +32 -37
- package/dist/engine/live.js +603 -647
- package/dist/engine/memory.js +109 -111
- package/dist/engine/optimizer.d.ts +19 -1
- package/dist/engine/optimizer.js +2633 -2934
- package/dist/engine/point-read.js +122 -182
- package/dist/engine/query-api.js +12 -3
- package/dist/engine/query-cache.js +61 -69
- package/dist/engine/query.d.ts +6 -0
- package/dist/engine/query.js +8503 -9980
- package/dist/engine/result-wire.js +221 -252
- package/dist/engine/schema-wire.js +97 -112
- package/dist/engine/schema.js +1099 -1254
- package/dist/engine/sort-keys.js +315 -389
- package/dist/engine/sql-domains.js +646 -769
- package/dist/engine/sql-driver.js +0 -1
- package/dist/engine/sql-functions.js +1031 -1125
- package/dist/engine/sql-json.js +199 -218
- package/dist/engine/sql-semantics.js +572 -639
- package/dist/engine/typed-live.js +271 -293
- package/dist/engine/vector.js +4484 -5145
- package/dist/engine/worker-host.d.ts +8 -63
- package/dist/engine/worker-host.js +28 -1130
- package/dist/engine/worker-indexeddb.d.ts +1 -0
- package/dist/engine/worker-indexeddb.js +3 -0
- package/dist/engine/worker-memory.d.ts +1 -0
- package/dist/engine/worker-memory.js +3 -0
- package/dist/engine/worker-opfs.d.ts +1 -0
- package/dist/engine/worker-opfs.js +3 -0
- package/dist/engine/worker-server.d.ts +93 -0
- package/dist/engine/worker-server.js +999 -0
- package/dist/engine/worker-store-indexeddb.d.ts +2 -0
- package/dist/engine/worker-store-indexeddb.js +11 -0
- package/dist/engine/worker-store-memory.d.ts +2 -0
- package/dist/engine/worker-store-memory.js +6 -0
- package/dist/engine/worker-store-opfs.d.ts +2 -0
- package/dist/engine/worker-store-opfs.js +9 -0
- package/dist/engine/worker.js +0 -12
- package/dist/engine/write-block-planner.js +93 -100
- package/dist/plan/index.js +15 -16
- package/dist/plan/model.d.ts +6 -0
- package/dist/plan/model.js +20 -27
- package/dist/storage/index.js +0 -12
- package/dist/storage/indexeddb.js +11776 -13171
- package/dist/storage/memory.js +1198 -1241
- package/dist/storage/opfs/files.js +238 -295
- package/dist/storage/opfs/index.js +9 -3
- package/dist/storage/opfs/leader.js +4386 -4856
- package/dist/storage/opfs/rpc.js +223 -259
- package/dist/storage/opfs/snapshot-ledger.js +219 -260
- package/dist/storage/opfs/store.js +884 -1003
- package/dist/storage/persistence.js +59 -74
- package/dist/storage/snapshot-stream.js +788 -826
- package/dist/storage/snapshot.js +36 -37
- package/dist/storage/toolkit/extents.js +444 -520
- package/dist/storage/toolkit/index.js +28 -29
- package/dist/storage/toolkit/record-core.js +5792 -6377
- package/dist/storage/toolkit/sync-file.js +34 -42
- package/dist/storage/toolkit/wal.js +92 -127
- package/dist/storage/toolkit/wire.js +232 -256
- package/dist/storage/types.js +2948 -3250
- package/dist/testing/block-store-conformance.js +1556 -1629
- package/dist/testing/index.js +310 -291
- package/dist/testing/opfs-shim.js +280 -312
- package/dist/testing/simulator.js +496 -534
- package/dist/testing/sqllogictest.js +425 -365
- package/dist/transactions/index.js +1464 -1712
- package/dist/worker-protocol/index.js +70 -69
- package/package.json +20 -2
package/dist/engine/fts.js
CHANGED
|
@@ -1,300 +1,253 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Full-text search primitives: the tokenizer, the query grammar, and the BM25 scorer.
|
|
3
|
-
*
|
|
4
|
-
* The tokenizer is deliberately hand-rolled rather than `Intl.Segmenter`: segmentation output
|
|
5
|
-
* must be identical across browsers, ICU versions, and time, because phase-2 persists tokens in
|
|
6
|
-
* a multi-tab shared index — an environment-varying tokenizer would silently split one logical
|
|
7
|
-
* index into two vocabularies. Any change to tokenization must bump `FTS_TOKENIZER_VERSION`;
|
|
8
|
-
* a persisted index stamped with another version reads as absent and rebuilds.
|
|
9
|
-
*
|
|
10
|
-
* The scorer takes only integers (term frequency, document length, document frequency, document
|
|
11
|
-
* count, total token count), so the row and columnar executors produce bit-identical scores no
|
|
12
|
-
* matter what order they accumulate in. Keep it that way: never feed it a running float.
|
|
13
|
-
*/
|
|
14
1
|
import { assertWellFormedString } from "../block-format/unicode.js";
|
|
15
2
|
import { MAX_CACHEABLE_TEXT_CHARACTERS, MAX_SQL_PATTERN_CHARACTERS } from "./cache-limits.js";
|
|
16
3
|
import { externalSqlDomainValue } from "./sql-domains.js";
|
|
17
|
-
import { MAX_FTS_QUERY_TERMS, MAX_FTS_TOKENS_PER_DOCUMENT, MAX_INDEXED_STRING_CHARACTERS
|
|
18
|
-
|
|
19
|
-
/** BM25 constants — the standard defaults. */
|
|
4
|
+
import { MAX_FTS_QUERY_TERMS, MAX_FTS_TOKENS_PER_DOCUMENT, MAX_INDEXED_STRING_CHARACTERS } from "../storage/types.js";
|
|
5
|
+
const FTS_TOKENIZER_VERSION = 1;
|
|
20
6
|
const BM25_K1 = 1.2;
|
|
21
7
|
const BM25_B = 0.75;
|
|
22
8
|
const MAX_TOKEN_LENGTH = 32;
|
|
23
9
|
const TOKEN_RUN = /[\p{L}\p{N}]+/gu;
|
|
24
10
|
const QUERY_WHITESPACE = /\s/u;
|
|
25
|
-
// Script_Extensions rather than Script: shared marks like the katakana prolonged sound mark
|
|
26
|
-
// (U+30FC, script Common) must stay inside the kana run they extend.
|
|
27
11
|
const CJK = /[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}]/u;
|
|
28
12
|
function pushToken(tokens, token) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
13
|
+
if (tokens.length >= MAX_FTS_TOKENS_PER_DOCUMENT)
|
|
14
|
+
return;
|
|
15
|
+
tokens.push(token.length > MAX_TOKEN_LENGTH ? token.slice(0, MAX_TOKEN_LENGTH) : token);
|
|
32
16
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Splits one normalized run into tokens: contiguous CJK subsequences emit character bigrams
|
|
35
|
-
* (a single character when alone), everything else stays one token. Bigrams give usable CJK
|
|
36
|
-
* search without a dictionary while staying fully deterministic.
|
|
37
|
-
*/
|
|
38
17
|
function emitRun(tokens, run) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
for (let index = 0; index < tokens.length; index += 1) {
|
|
101
|
-
const term = tokens[index];
|
|
102
|
-
if (term === undefined)
|
|
103
|
-
continue;
|
|
104
|
-
terms.push({ term, prefix: prefix && index === tokens.length - 1 });
|
|
105
|
-
// Every caller rejects above 32. Stop before adversarial whitespace can allocate a terms
|
|
106
|
-
// array proportional to the input; the 33rd item is all validation needs to see.
|
|
107
|
-
if (terms.length > MAX_FTS_QUERY_TERMS)
|
|
108
|
-
return terms;
|
|
109
|
-
}
|
|
18
|
+
if (!CJK.test(run)) {
|
|
19
|
+
pushToken(tokens, run);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const characters = Array.from(run);
|
|
23
|
+
let start = 0;
|
|
24
|
+
while (start < characters.length) {
|
|
25
|
+
const isCjk = CJK.test(characters[start] ?? "");
|
|
26
|
+
let end = start + 1;
|
|
27
|
+
while (end < characters.length && CJK.test(characters[end] ?? "") === isCjk)
|
|
28
|
+
end += 1;
|
|
29
|
+
if (isCjk) {
|
|
30
|
+
if (end - start === 1)
|
|
31
|
+
pushToken(tokens, characters[start] ?? "");
|
|
32
|
+
for (let index = start; index < end - 1; index += 1) {
|
|
33
|
+
pushToken(tokens, (characters[index] ?? "") + (characters[index + 1] ?? ""));
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
pushToken(tokens, characters.slice(start, end).join(""));
|
|
37
|
+
}
|
|
38
|
+
start = end;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function tokenize(text) {
|
|
42
|
+
if (text.length > MAX_INDEXED_STRING_CHARACTERS) {
|
|
43
|
+
throw new RangeError(`Full-text values cannot exceed ${String(MAX_INDEXED_STRING_CHARACTERS)} characters`);
|
|
44
|
+
}
|
|
45
|
+
assertWellFormedString(text, "Full-text value");
|
|
46
|
+
const tokens = [];
|
|
47
|
+
const normalized = text.normalize("NFKC").toLowerCase();
|
|
48
|
+
for (const match of normalized.matchAll(TOKEN_RUN)) {
|
|
49
|
+
emitRun(tokens, match[0]);
|
|
50
|
+
if (tokens.length >= MAX_FTS_TOKENS_PER_DOCUMENT)
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
return tokens;
|
|
54
|
+
}
|
|
55
|
+
function tokenizeQuery(query) {
|
|
56
|
+
if (query.length > MAX_SQL_PATTERN_CHARACTERS) {
|
|
57
|
+
throw new RangeError(`Full-text queries cannot exceed ${String(MAX_SQL_PATTERN_CHARACTERS)} characters`);
|
|
58
|
+
}
|
|
59
|
+
assertWellFormedString(query, "Full-text query");
|
|
60
|
+
const terms = [];
|
|
61
|
+
let cursor = 0;
|
|
62
|
+
while (cursor < query.length) {
|
|
63
|
+
while (cursor < query.length && QUERY_WHITESPACE.test(query[cursor] ?? ""))
|
|
64
|
+
cursor += 1;
|
|
65
|
+
const start = cursor;
|
|
66
|
+
while (cursor < query.length && !QUERY_WHITESPACE.test(query[cursor] ?? ""))
|
|
67
|
+
cursor += 1;
|
|
68
|
+
if (cursor === start)
|
|
69
|
+
continue;
|
|
70
|
+
const prefix = query[cursor - 1] === "*";
|
|
71
|
+
const tokens = tokenize(query.slice(start, prefix ? cursor - 1 : cursor));
|
|
72
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
73
|
+
const term = tokens[index];
|
|
74
|
+
if (term === void 0)
|
|
75
|
+
continue;
|
|
76
|
+
terms.push({ term, prefix: prefix && index === tokens.length - 1 });
|
|
77
|
+
if (terms.length > MAX_FTS_QUERY_TERMS)
|
|
78
|
+
return terms;
|
|
110
79
|
}
|
|
111
|
-
|
|
80
|
+
}
|
|
81
|
+
return terms;
|
|
112
82
|
}
|
|
113
83
|
function termMatches(token, term) {
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const cached = queryTermsCache.get(query);
|
|
124
|
-
if (cached !== undefined)
|
|
125
|
-
return cached;
|
|
126
|
-
const terms = tokenizeQuery(query);
|
|
127
|
-
if (query.length > MAX_CACHEABLE_TEXT_CHARACTERS)
|
|
128
|
-
return terms;
|
|
129
|
-
if (queryTermsCache.size >= 128)
|
|
130
|
-
queryTermsCache.clear();
|
|
131
|
-
queryTermsCache.set(query, terms);
|
|
84
|
+
return term.prefix ? token.startsWith(term.term) : token === term.term;
|
|
85
|
+
}
|
|
86
|
+
const queryTermsCache = /* @__PURE__ */ new Map();
|
|
87
|
+
function cachedQueryTerms(query) {
|
|
88
|
+
const cached = queryTermsCache.get(query);
|
|
89
|
+
if (cached !== void 0)
|
|
90
|
+
return cached;
|
|
91
|
+
const terms = tokenizeQuery(query);
|
|
92
|
+
if (query.length > MAX_CACHEABLE_TEXT_CHARACTERS)
|
|
132
93
|
return terms;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
94
|
+
if (queryTermsCache.size >= 128)
|
|
95
|
+
queryTermsCache.clear();
|
|
96
|
+
queryTermsCache.set(query, terms);
|
|
97
|
+
return terms;
|
|
98
|
+
}
|
|
99
|
+
function validateFtsQuery(query) {
|
|
100
|
+
if (cachedQueryTerms(query).length > MAX_FTS_QUERY_TERMS) {
|
|
101
|
+
throw new TypeError(`Full-text queries support at most ${String(MAX_FTS_QUERY_TERMS)} terms`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function termsMask(tokens, terms) {
|
|
105
|
+
let mask = 0;
|
|
106
|
+
for (const token of tokens) {
|
|
107
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
108
|
+
const term = terms[index];
|
|
109
|
+
if (term !== void 0 && (mask & 1 << index) === 0 && termMatches(token, term)) {
|
|
110
|
+
mask |= 1 << index;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return mask;
|
|
115
|
+
}
|
|
116
|
+
function fullTermsMask(termCount) {
|
|
117
|
+
return termCount >= 32 ? -1 : (1 << termCount) - 1;
|
|
118
|
+
}
|
|
119
|
+
function ftsMatchTruth(terms, values) {
|
|
120
|
+
let anyPresent = false;
|
|
121
|
+
const present = new Array(terms.length).fill(false);
|
|
122
|
+
let satisfied = 0;
|
|
123
|
+
for (const value of values) {
|
|
124
|
+
const rendered = renderDocumentValue(value);
|
|
125
|
+
if (rendered === void 0)
|
|
126
|
+
continue;
|
|
127
|
+
anyPresent = true;
|
|
128
|
+
if (terms.length === 0 || satisfied === terms.length)
|
|
129
|
+
continue;
|
|
130
|
+
for (const token of tokenize(rendered)) {
|
|
131
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
132
|
+
const term = terms[index];
|
|
133
|
+
if (term !== void 0 && present[index] !== true && termMatches(token, term)) {
|
|
134
|
+
present[index] = true;
|
|
135
|
+
satisfied += 1;
|
|
149
136
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return frequencies;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* One term's BM25 contribution. Integer inputs only — see the module comment. `df` of zero
|
|
207
|
-
* contributes nothing (the term appears in no document, so no row scores on it).
|
|
208
|
-
*/
|
|
209
|
-
export function bm25Score(tf, docLength, df, docCount, totalTokens) {
|
|
210
|
-
if (tf === 0 || df === 0 || docCount === 0)
|
|
211
|
-
return 0;
|
|
212
|
-
const idf = Math.log(1 + (docCount - df + 0.5) / (df + 0.5));
|
|
213
|
-
const averageLength = totalTokens / docCount;
|
|
214
|
-
const normalized = (tf * (BM25_K1 + 1)) /
|
|
215
|
-
(tf +
|
|
216
|
-
BM25_K1 * (1 - BM25_B + (BM25_B * docLength) / (averageLength === 0 ? 1 : averageLength)));
|
|
217
|
-
return idf * normalized;
|
|
218
|
-
}
|
|
219
|
-
/** Sums the contributions of every query term for one document. */
|
|
220
|
-
export function bm25DocumentScore(frequencies, docLength, stats) {
|
|
221
|
-
let score = 0;
|
|
137
|
+
}
|
|
138
|
+
if (satisfied === terms.length)
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (!anyPresent)
|
|
143
|
+
return null;
|
|
144
|
+
return terms.length > 0 && satisfied === terms.length;
|
|
145
|
+
}
|
|
146
|
+
function termFrequencies(tokens, terms) {
|
|
147
|
+
const frequencies = new Array(terms.length).fill(0);
|
|
148
|
+
for (const token of tokens) {
|
|
149
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
150
|
+
const term = terms[index];
|
|
151
|
+
if (term !== void 0 && termMatches(token, term)) {
|
|
152
|
+
frequencies[index] = (frequencies[index] ?? 0) + 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return frequencies;
|
|
157
|
+
}
|
|
158
|
+
function bm25Score(tf, docLength, df, docCount, totalTokens) {
|
|
159
|
+
if (tf === 0 || df === 0 || docCount === 0)
|
|
160
|
+
return 0;
|
|
161
|
+
const idf = Math.log(1 + (docCount - df + 0.5) / (df + 0.5));
|
|
162
|
+
const averageLength = totalTokens / docCount;
|
|
163
|
+
const normalized = tf * (BM25_K1 + 1) / (tf + BM25_K1 * (1 - BM25_B + BM25_B * docLength / (averageLength === 0 ? 1 : averageLength)));
|
|
164
|
+
return idf * normalized;
|
|
165
|
+
}
|
|
166
|
+
function bm25DocumentScore(frequencies, docLength, stats) {
|
|
167
|
+
let score = 0;
|
|
168
|
+
for (let index = 0; index < frequencies.length; index += 1) {
|
|
169
|
+
score += bm25Score(frequencies[index] ?? 0, docLength, stats.dfByTerm[index] ?? 0, stats.docCount, stats.totalTokens);
|
|
170
|
+
}
|
|
171
|
+
return score;
|
|
172
|
+
}
|
|
173
|
+
class FtsStatsAccumulator {
|
|
174
|
+
#terms;
|
|
175
|
+
#stats;
|
|
176
|
+
constructor(terms) {
|
|
177
|
+
this.#terms = terms;
|
|
178
|
+
this.#stats = {
|
|
179
|
+
docCount: 0,
|
|
180
|
+
totalTokens: 0,
|
|
181
|
+
dfByTerm: new Array(terms.length).fill(0)
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
addDocument(tokens) {
|
|
185
|
+
this.#stats.docCount += 1;
|
|
186
|
+
this.#stats.totalTokens += tokens.length;
|
|
187
|
+
const frequencies = termFrequencies(tokens, this.#terms);
|
|
222
188
|
for (let index = 0; index < frequencies.length; index += 1) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
this.#stats =
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
let anyPresent = false;
|
|
271
|
-
let docLength = 0;
|
|
272
|
-
const frequencies = new Array(terms.length).fill(0);
|
|
273
|
-
for (const value of values) {
|
|
274
|
-
const rendered = renderDocumentValue(value);
|
|
275
|
-
if (rendered === undefined)
|
|
276
|
-
continue;
|
|
277
|
-
anyPresent = true;
|
|
278
|
-
const tokens = tokenize(rendered);
|
|
279
|
-
docLength += tokens.length;
|
|
280
|
-
const partial = termFrequencies(tokens, terms);
|
|
281
|
-
for (let index = 0; index < terms.length; index += 1) {
|
|
282
|
-
frequencies[index] = (frequencies[index] ?? 0) + (partial[index] ?? 0);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
if (!anyPresent)
|
|
286
|
-
return null;
|
|
287
|
-
return bm25DocumentScore(frequencies, docLength, stats);
|
|
288
|
-
}
|
|
289
|
-
/** Renders one cell for the document, per column type. Booleans are excluded from documents. */
|
|
290
|
-
export function renderDocumentValue(value) {
|
|
291
|
-
value = externalSqlDomainValue(value);
|
|
292
|
-
if (value === null || value === undefined || typeof value === "boolean")
|
|
293
|
-
return undefined;
|
|
294
|
-
if (value instanceof Date)
|
|
295
|
-
return dateIsoString(value);
|
|
296
|
-
if (typeof value === "number")
|
|
297
|
-
return String(value);
|
|
298
|
-
return typeof value === "string" ? value : undefined;
|
|
189
|
+
if ((frequencies[index] ?? 0) > 0) {
|
|
190
|
+
this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
addDocumentCounts(termMask, length) {
|
|
195
|
+
this.#stats.docCount += 1;
|
|
196
|
+
this.#stats.totalTokens += length;
|
|
197
|
+
for (let index = 0; index < this.#terms.length; index += 1) {
|
|
198
|
+
if ((termMask & 1 << index) !== 0) {
|
|
199
|
+
this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
get stats() {
|
|
204
|
+
return this.#stats;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function ftsBm25Row(terms, values, stats) {
|
|
208
|
+
let anyPresent = false;
|
|
209
|
+
let docLength = 0;
|
|
210
|
+
const frequencies = new Array(terms.length).fill(0);
|
|
211
|
+
for (const value of values) {
|
|
212
|
+
const rendered = renderDocumentValue(value);
|
|
213
|
+
if (rendered === void 0)
|
|
214
|
+
continue;
|
|
215
|
+
anyPresent = true;
|
|
216
|
+
const tokens = tokenize(rendered);
|
|
217
|
+
docLength += tokens.length;
|
|
218
|
+
const partial = termFrequencies(tokens, terms);
|
|
219
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
220
|
+
frequencies[index] = (frequencies[index] ?? 0) + (partial[index] ?? 0);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (!anyPresent)
|
|
224
|
+
return null;
|
|
225
|
+
return bm25DocumentScore(frequencies, docLength, stats);
|
|
226
|
+
}
|
|
227
|
+
function renderDocumentValue(value) {
|
|
228
|
+
value = externalSqlDomainValue(value);
|
|
229
|
+
if (value === null || value === void 0 || typeof value === "boolean")
|
|
230
|
+
return void 0;
|
|
231
|
+
if (value instanceof Date)
|
|
232
|
+
return dateIsoString(value);
|
|
233
|
+
if (typeof value === "number")
|
|
234
|
+
return String(value);
|
|
235
|
+
return typeof value === "string" ? value : void 0;
|
|
299
236
|
}
|
|
300
237
|
import { dateIsoString } from "../date-value.js";
|
|
238
|
+
export {
|
|
239
|
+
FTS_TOKENIZER_VERSION,
|
|
240
|
+
FtsStatsAccumulator,
|
|
241
|
+
bm25DocumentScore,
|
|
242
|
+
bm25Score,
|
|
243
|
+
cachedQueryTerms,
|
|
244
|
+
ftsBm25Row,
|
|
245
|
+
ftsMatchTruth,
|
|
246
|
+
fullTermsMask,
|
|
247
|
+
renderDocumentValue,
|
|
248
|
+
termFrequencies,
|
|
249
|
+
termsMask,
|
|
250
|
+
tokenize,
|
|
251
|
+
tokenizeQuery,
|
|
252
|
+
validateFtsQuery
|
|
253
|
+
};
|