cortex
aien-dev/cortex
Brought by drakestapleton
| 1 | note ===== fts.om : SQLite 3.45.0 FTS5 replica for cortex-rs search_entities ===== |
| 2 | note Owner FtsEngine. Globals q*, functions fts_*. |
| 3 | note Replicates, for the table |
| 4 | note entities_fts USING fts5(id UNINDEXED, canonical_name, content, space_slug UNINDEXED) |
| 5 | note and the statement |
| 6 | note SELECT .. COALESCE(bm25(entities_fts), 5.0) AS bm25_rank FROM entities e |
| 7 | note JOIN entities_fts f ON f.id = e.id WHERE entities_fts MATCH ?1 AND e.space_slug = ?2 |
| 8 | note AND e.retracted = 0 ORDER BY bm25_rank ASC LIMIT ?3 |
| 9 | note what SQLite 3.45.0 (libsqlite3-sys 0.28.0, gcc -O3 as built into cortex-rs) returns: |
| 10 | note the FTS5 expression grammar (Lemon tables of fts5parse.y), the parse actions of |
| 11 | note fts5_expr.c, the node iteration engine (TERM, STRING/NEAR, AND, OR, NOT, colsets, ^, *), |
| 12 | note the position lists the xInst API exposes, bm25 with k1=1.2 b=0.75 in the exact floating |
| 13 | note operation order (pairwise vector loop, fused last term) of fts5Bm25Function, the plan |
| 14 | note SCAN f (rowid ascending) + SEARCH e + temp b-tree ORDER BY with LIMIT (stable on ties). |
| 15 | note |
| 16 | note API (callers write the input globals, then call) |
| 17 | note fts_init once (idempotent; also run by fts_put/fts_search) |
| 18 | note fts_put doc row -> bool add FTS row `row` (the entities_fts rowid) for doc slot `doc` |
| 19 | note (< 262144, the store's EMAX), columns canonical_name = qnm[0..qnml), |
| 20 | note content = qct[0..qctl). A doc slot already present is deleted |
| 21 | note first (cortex-rs does DELETE then INSERT). false: row already |
| 22 | note used by another doc, or a capacity limit (see below) was hit. |
| 23 | note qgr[0] names the row's group (the caller's space_slug, |
| 24 | note < 262144; false otherwise). |
| 25 | note fts_del doc remove the FTS row of doc slot `doc` (no-op when absent) |
| 26 | note fts_reset drop every row |
| 27 | note fts_search -> ulong MATCH string qq[0..qql) (SQLite reads it up to the first NUL), |
| 28 | note join filter qok[doc] = 1 when entities e (e.id = f.id) has the |
| 29 | note wanted space_slug and retracted = 0, LIMIT qlim. Returns the |
| 30 | note row count n; qrd[0..n) doc slots and qrb[0..n) bm25_rank in |
| 31 | note SQL output order. qerr = 1 when SQLite would raise an error |
| 32 | note (syntax error, unknown column, ...; cortex-rs then sees 0 rows), |
| 33 | note 2 when a capacity limit of this replica was hit (0 rows). |
| 34 | note Only rows of group qgsel[0] are returned, and qok is read only |
| 35 | note for them: the caller sets qok for the rows of that group |
| 36 | note (fts_gfirst / fts_gnext walk them) and nothing else. |
| 37 | note fts_gfirst g / fts_gnext doc the doc slots of group g in ascending rowid (NONE at the end) |
| 38 | note fts_icheck -> ulong mismatches between the incremental index and a rebuild (0) |
| 39 | note fts_rows / fts_tokens nRow and total token count (the bm25 table statistics) |
| 40 | note Capacities: 262144 doc slots, 16777216 token words, 2097152 distinct terms (16 MiB of |
| 41 | note term bytes), 8388608 posting entries per query batch, 65536 query terms/phrases/nodes, |
| 42 | note 262144 result rows (qlim is clamped to that). |
| 43 | note generated by tests/fts/gen_parse_tables.py from sqlite3.c sha256 7956a38f236a6be6c0bb30c96ba4f85f19e5a69f6beb6d2c62c9d246972a6775 (SQLite 3.45.0) |
| 44 | const QPA ulong 0 |
| 45 | const QPL ulong 105 |
| 46 | const QPS ulong 226 |
| 47 | const QPR ulong 261 |
| 48 | const QPD ulong 279 |
| 49 | const QPH ulong 314 |
| 50 | const QPN ulong 342 |
| 51 | const QPT ulong 370 |
| 52 | text qptx "81 20 96 6 28 99 98 26 26 18 96 6 28 17 98 56 26 19 96 6 28 14 98 14 26 31 92 96 6 28 108 98 25 26 21 96 6 28 78 98 58 26 29 96 6 28 107 98 22 26 24 16 12 11 1 13 13 24 16 23 11 33 34 13 97 8 27 32 98 7 26 3 4 5 3 4 5 3 83 4 5 3 63 5 3 62 12 2 86 13 9 30 10 10 54 57 75 78 78 53 57 15 82 82 71 16 17 18 19 20 22 22 24 24 17 18 19 20 7 22 9 24 17 18 19 20 9 22 9 24 13 17 18 19 20 26 22 24 24 17 18 19 20 15 22 9 24 17 18 19 20 26 22 21 24 6 7 9 9 10 12 12 6 7 21 9 24 25 12 18 5 20 14 22 5 24 3 1 2 3 1 2 3 0 1 2 3 11 2 3 11 9 10 5 12 23 24 10 10 8 9 9 15 15 8 9 9 27 27 11 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 44 44 44 44 44 44 51 77 43 12 14 83 82 14 23 23 31 31 71 74 78 81 86 91 6 53 53 60 64 68 53 87 92 53 93 84 92 100 109 117 125 146 83 83 137 167 104 104 108 104 120 127 138 80 80 80 80 80 80 95 80 80 105 80 110 110 80 110 110 80 80 80 80 80 91 80 80 80 101 100 80 80 90 103 80 80 104 80 16 20 20 20 20 21 21 17 17 17 17 17 17 19 19 18 18 22 22 22 23 23 25 25 24 24 26 26 1 4 3 1 2 2 1 3 3 3 5 3 1 1 2 1 3 1 2 5 1 2 0 2 4 2 1 0" |
| 53 | text qcolx "id canonical_name content space_slug" |
| 54 | const QD ulong 262144 |
| 55 | const QTKN ulong 16777216 |
| 56 | const QTM ulong 2097152 |
| 57 | const QTH ulong 4194304 |
| 58 | const QTB ulong 16777216 |
| 59 | const QE ulong 8388608 |
| 60 | const QA ulong 4194304 |
| 61 | const QX ulong 65536 |
| 62 | const QR ulong 262144 |
| 63 | note --- storage --- |
| 64 | global qdrow ulong QD |
| 65 | global qdst uint QD |
| 66 | global qsr uint QD |
| 67 | global qsn ulong 1 |
| 68 | global qtot ulong 1 |
| 69 | global qtk uint QTKN |
| 70 | global qtkn ulong 1 |
| 71 | global qtkd ulong 1 |
| 72 | global qtb uchar QTB |
| 73 | global qtbn ulong 1 |
| 74 | global qtmo uint QTM |
| 75 | global qtml uint QTM |
| 76 | global qtmf uint QTM |
| 77 | global qtmc ulong 1 |
| 78 | global qth uint QTH |
| 79 | global qtmk ulong QTM |
| 80 | global qmkl uint QTM |
| 81 | global qmkn ulong 1 |
| 82 | global qini ulong 1 |
| 83 | global qtkf ulong 1 |
| 84 | note --- API --- |
| 85 | global qq uchar 1048576 |
| 86 | global qql ulong 1 |
| 87 | global qok uchar QD |
| 88 | global qlim ulong 1 |
| 89 | global qrd ulong QR |
| 90 | global qrb f64 QR |
| 91 | global qrn ulong 1 |
| 92 | global qerr ulong 1 |
| 93 | note --- query: patterns, terms, phrases, nearsets, nodes --- |
| 94 | global qqe ulong 1 |
| 95 | global qxb uchar 1048576 |
| 96 | global qxbn ulong 1 |
| 97 | global qpb ulong QX |
| 98 | global qpl ulong QX |
| 99 | global qpp ulong QX |
| 100 | global qpe0 ulong QX |
| 101 | global qpe1 ulong QX |
| 102 | global qpcn ulong QX |
| 103 | global qpn ulong 1 |
| 104 | global qxo ulong QX |
| 105 | global qxl ulong QX |
| 106 | global qxp ulong QX |
| 107 | global qxf ulong QX |
| 108 | global qxr ulong QX |
| 109 | global qxm ulong QX |
| 110 | global qxe ulong QX |
| 111 | global qxw ulong QX |
| 112 | global qxs ulong QX |
| 113 | global qxt ulong QX |
| 114 | global qxc ulong QX |
| 115 | global qxn ulong 1 |
| 116 | global qht0 ulong QX |
| 117 | global qhn ulong QX |
| 118 | global qhnd ulong QX |
| 119 | global qhsel ulong QX |
| 120 | global qhs ulong QX |
| 121 | global qhc ulong QX |
| 122 | global qhcn ulong 1 |
| 123 | global qyd long QX |
| 124 | global qycs ulong QX |
| 125 | global qyp0 ulong QX |
| 126 | global qyk ulong QX |
| 127 | global qyn ulong 1 |
| 128 | global qyph ulong QX |
| 129 | global qyphn ulong 1 |
| 130 | global qot ulong QX |
| 131 | global qox ulong QX |
| 132 | global qoe ulong QX |
| 133 | global qonm ulong QX |
| 134 | global qow ulong QX |
| 135 | global qoy ulong QX |
| 136 | global qoh ulong QX |
| 137 | global qoc ulong QX |
| 138 | global qocs ulong QX |
| 139 | global qocn ulong 1 |
| 140 | global qkid ulong QX |
| 141 | global qkidn ulong 1 |
| 142 | global qap ulong QX |
| 143 | global qapn ulong 1 |
| 144 | global qexpr ulong 1 |
| 145 | global qroot ulong 1 |
| 146 | note parser stack (fts5YYSTACKDEPTH 100) and current token |
| 147 | global qss ulong 100 |
| 148 | global qsm ulong 100 |
| 149 | global qsv ulong 100 |
| 150 | global qsw ulong 100 |
| 151 | global qst ulong 1 |
| 152 | global qtkp ulong 1 |
| 153 | global qtkl ulong 1 |
| 154 | global qptab ulong QPT |
| 155 | note posting entries (rowpos, encoded position (col-1)<<31|offset) and the poslist arena |
| 156 | global qer uint QE |
| 157 | global qep uint QE |
| 158 | global qpa uint 8388608 |
| 159 | global qpan ulong 1 |
| 160 | global qpah ulong 1 |
| 161 | note readers (by term for phrase matching, by phrase for NEAR) |
| 162 | global qrdi ulong QX |
| 163 | global qrde ulong QX |
| 164 | global qrdp long QX |
| 165 | global qlki ulong QX |
| 166 | global qlke ulong QX |
| 167 | global qlkp long QX |
| 168 | global qlkl long QX |
| 169 | global qlko ulong QX |
| 170 | global qlkc ulong QX |
| 171 | global qlkv long QX |
| 172 | note bm25 |
| 173 | global qidf f64 QX |
| 174 | global qfrq f64 QX |
| 175 | global qidok ulong 1 |
| 176 | global qavg f64 1 |
| 177 | global qcln ulong QX |
| 178 | note ---- incremental index (kept by fts_put and fts_del; fts_icheck compares it with a rebuild) ---- |
| 179 | note Term lists: one node per (term, row) pair, prepended when the row is put. A node holds the |
| 180 | note doc slot and the slot's generation (qgen, bumped by every delete), so a deleted row's nodes |
| 181 | note are skipped without unlinking. The node pool has the size of the token pool (a live pair |
| 182 | note needs a token there), so fts_lbuild (run when the pool is full, half garbage, or after a |
| 183 | note generation wraps) always makes room: the index has no capacity of its own. |
| 184 | note qdf: the number of rows that hold each term (the bm25 hit count of a one-term phrase). |
| 185 | note Groups: the rows of each group (qgr at put time) in a doubly linked list in ascending rowid, |
| 186 | note qgc rows each. qlv: 0 nodes used, 1 garbage nodes, 2 rebuild pending, 3 the most tokens one |
| 187 | note row has had since the last rebuild (an upper bound, used by the arena guard of fts_route). |
| 188 | global qln uint QTKN |
| 189 | global qld uint QTKN |
| 190 | global qlh uint QTM |
| 191 | global qdf uint QTM |
| 192 | global qgen uint QD |
| 193 | global qlv ulong 4 |
| 194 | global qdg uint QD |
| 195 | global qgp uint QD |
| 196 | global qgn uint QD |
| 197 | global qgh uint QD |
| 198 | global qgt uint QD |
| 199 | global qgc uint QD |
| 200 | global qgr ulong 1 |
| 201 | global qgsel ulong 1 |
| 202 | note candidate rows of one search, ascending rowid (posting entries hold an index into qcd); |
| 203 | note qcs is the sort scratch, qcm marks rows already taken |
| 204 | global qcd uint QD |
| 205 | global qcs uint QD |
| 206 | global qcn ulong 1 |
| 207 | global qcm uchar QD |
| 208 | note qfb: phrases whose hit count is counted by running their clone |
| 209 | global qfb uchar QX |
| 210 | note qrte: 0 choose; 1 scan the group, 2 term lists, 3 every row (the plan before the index); |
| 211 | note 4 choose but never take the term-count loop (fts_ortop); set only by tests. |
| 212 | note qrst: route of the last search (1, 2, 3), its candidate count, 1 when fts_ortop ran |
| 213 | global qrte ulong 1 |
| 214 | global qrst ulong 3 |
| 215 | global qkbad ulong 1 |
| 216 | note fts_ortop: term slot of each phrase (QX when the dictionary lacks it), counts per term slot, |
| 217 | note the slots counted in the current row |
| 218 | global qotk ulong QX |
| 219 | global qotc ulong QX |
| 220 | global qotu ulong QX |
| 221 | fn fts_init to void |
| 222 | if eq get qini 0 1 |
| 223 | ret |
| 224 | end |
| 225 | uf_init |
| 226 | flog_init |
| 227 | k = ulong 0 |
| 228 | v = ulong 0 |
| 229 | dg = false |
| 230 | for i ulong len qptx |
| 231 | c = ulong get qptx i |
| 232 | if and ge c 48 le c 57 |
| 233 | v = add mul v 10 sub c 48 |
| 234 | dg = true |
| 235 | else |
| 236 | if dg |
| 237 | put qptab k v |
| 238 | k = add k 1 |
| 239 | end |
| 240 | v = ulong 0 |
| 241 | dg = false |
| 242 | end |
| 243 | end |
| 244 | if dg |
| 245 | put qptab k v |
| 246 | k = add k 1 |
| 247 | end |
| 248 | if ne k QPT |
| 249 | trap 9 |
| 250 | end |
| 251 | put qini 0 1 |
| 252 | end |
| 253 | fn fts_rows to ulong |
| 254 | ret get qsn 0 |
| 255 | end |
| 256 | fn fts_tokens to ulong |
| 257 | ret get qtot 0 |
| 258 | end |
| 259 | note byte i of a key: s 0 = qtf (token just folded), s 1 = qxb at offset o (query term) |
| 260 | fn fts_kb s ulong o ulong i ulong to ulong |
| 261 | if eq s 0 |
| 262 | ret ulong get qtf i |
| 263 | end |
| 264 | ret ulong get qxb add o i |
| 265 | end |
| 266 | fn fts_hash s ulong o ulong n ulong to ulong |
| 267 | h = ulong 14695981039346656037 |
| 268 | for i ulong n |
| 269 | h = mul xor h fts_kb s o i 1099511628211 |
| 270 | end |
| 271 | ret h |
| 272 | end |
| 273 | note true when term t equals the key |
| 274 | fn fts_teq t ulong s ulong o ulong n ulong to bool |
| 275 | if ne ulong get qtml t n |
| 276 | ret false |
| 277 | end |
| 278 | b = ulong get qtmo t |
| 279 | for i ulong n |
| 280 | if ne ulong get qtb add b i fts_kb s o i |
| 281 | ret false |
| 282 | end |
| 283 | end |
| 284 | ret true |
| 285 | end |
| 286 | note term id of the key, or QTM when absent; with ins, add it (QTM when the dictionary is full) |
| 287 | fn fts_tid s ulong o ulong n ulong ins bool to ulong |
| 288 | h = and fts_hash s o n sub QTH 1 |
| 289 | while true bound 999999 |
| 290 | e = ulong get qth h |
| 291 | if eq e 0 |
| 292 | break |
| 293 | end |
| 294 | if fts_teq sub e 1 s o n |
| 295 | ret sub e 1 |
| 296 | end |
| 297 | h = and add h 1 sub QTH 1 |
| 298 | end |
| 299 | if not ins |
| 300 | ret QTM |
| 301 | end |
| 302 | t = get qtmc 0 |
| 303 | b = get qtbn 0 |
| 304 | if or ge t sub QTM 1 gt add b n QTB |
| 305 | ret QTM |
| 306 | end |
| 307 | for i ulong n |
| 308 | put qtb add b i uchar fts_kb s o i |
| 309 | end |
| 310 | put qtmo t uint b |
| 311 | put qtml t uint n |
| 312 | put qtmf t 0 |
| 313 | put qtbn 0 add b n |
| 314 | put qtmc 0 add t 1 |
| 315 | put qth h uint add t 1 |
| 316 | ret t |
| 317 | end |
| 318 | note --- big loops: every loop that may pass 1,000,000 times is split as 17 x 999999 --- |
| 319 | note move n words of qtk from src to dst (dst <= src) |
| 320 | fn fts_mvtk dst ulong src ulong n ulong to void |
| 321 | i = ulong 0 |
| 322 | for a ulong 17 |
| 323 | if ge i n |
| 324 | ret |
| 325 | end |
| 326 | m = sub n i |
| 327 | if gt m 999999 |
| 328 | m = ulong 999999 |
| 329 | end |
| 330 | for j ulong m |
| 331 | put qtk add dst i get qtk add src i |
| 332 | i = add i 1 |
| 333 | end |
| 334 | end |
| 335 | end |
| 336 | fn fts_rlen p ulong to ulong |
| 337 | a = ulong get qtk add p 1 |
| 338 | ret add add 3 a ulong get qtk add p 2 |
| 339 | end |
| 340 | note compact the token pool: regions [doc, n1, n2, tokens] in start order, dead doc = 2^32-1 |
| 341 | fn fts_cmp to void |
| 342 | p = ulong 0 |
| 343 | w = ulong 0 |
| 344 | e = get qtkn 0 |
| 345 | for a ulong 17 |
| 346 | for j ulong 999999 |
| 347 | if ge p e |
| 348 | put qtkn 0 w |
| 349 | put qtkd 0 0 |
| 350 | ret |
| 351 | end |
| 352 | d = ulong get qtk p |
| 353 | ln = fts_rlen p |
| 354 | if ne d 4294967295 |
| 355 | if ne w p |
| 356 | fts_mvtk w p ln |
| 357 | end |
| 358 | put qdst d uint add w 1 |
| 359 | w = add w ln |
| 360 | end |
| 361 | p = add p ln |
| 362 | end |
| 363 | end |
| 364 | trap 9 |
| 365 | end |
| 366 | note first sorted index whose rowid is >= row |
| 367 | fn fts_spos row ulong to ulong |
| 368 | lo = ulong 0 |
| 369 | hi = get qsn 0 |
| 370 | while lt lo hi bound 64 |
| 371 | m = div add lo hi 2 |
| 372 | if lt get qdrow ulong get qsr m row |
| 373 | lo = add m 1 |
| 374 | else |
| 375 | hi = m |
| 376 | end |
| 377 | end |
| 378 | ret lo |
| 379 | end |
| 380 | fn fts_sins p ulong doc ulong to void |
| 381 | n = get qsn 0 |
| 382 | i = n |
| 383 | for a ulong 17 |
| 384 | for j ulong 999999 |
| 385 | if le i p |
| 386 | put qsr p uint doc |
| 387 | put qsn 0 add n 1 |
| 388 | ret |
| 389 | end |
| 390 | put qsr i get qsr sub i 1 |
| 391 | i = sub i 1 |
| 392 | end |
| 393 | end |
| 394 | trap 9 |
| 395 | end |
| 396 | fn fts_sdel p ulong to void |
| 397 | n = sub get qsn 0 1 |
| 398 | i = p |
| 399 | for a ulong 17 |
| 400 | for j ulong 999999 |
| 401 | if ge i n |
| 402 | put qsn 0 n |
| 403 | ret |
| 404 | end |
| 405 | put qsr i get qsr add i 1 |
| 406 | i = add i 1 |
| 407 | end |
| 408 | end |
| 409 | trap 9 |
| 410 | end |
| 411 | fn fts_reset to void |
| 412 | fts_init |
| 413 | for a ulong 17 |
| 414 | for j ulong 999999 |
| 415 | if eq get qsn 0 0 |
| 416 | put qtkn 0 0 |
| 417 | put qtkd 0 0 |
| 418 | put qtot 0 0 |
| 419 | fts_lclear |
| 420 | ret |
| 421 | end |
| 422 | fts_del ulong get qsr sub get qsn 0 1 |
| 423 | end |
| 424 | end |
| 425 | end |
| 426 | fn fts_del doc ulong to void |
| 427 | if ge doc QD |
| 428 | ret |
| 429 | end |
| 430 | s1 = ulong get qdst doc |
| 431 | if eq s1 0 |
| 432 | ret |
| 433 | end |
| 434 | fts_ldel doc |
| 435 | fts_gdel doc |
| 436 | s = sub s1 1 |
| 437 | n1 = ulong get qtk add s 1 |
| 438 | n2 = ulong get qtk add s 2 |
| 439 | nt = add n1 n2 |
| 440 | fts_unref add s 3 nt |
| 441 | put qtk s 4294967295 |
| 442 | put qtkd 0 add get qtkd 0 add 3 nt |
| 443 | put qtot 0 sub get qtot 0 nt |
| 444 | p = fts_spos get qdrow doc |
| 445 | fts_sdel p |
| 446 | put qdst doc 0 |
| 447 | end |
| 448 | fn fts_unref s ulong n ulong to void |
| 449 | i = ulong 0 |
| 450 | for a ulong 17 |
| 451 | for j ulong 999999 |
| 452 | if ge i n |
| 453 | ret |
| 454 | end |
| 455 | t = ulong get qtk add s i |
| 456 | put qtmf t uint sub ulong get qtmf t 1 |
| 457 | i = add i 1 |
| 458 | end |
| 459 | end |
| 460 | end |
| 461 | note tokenize source src (n bytes) into qtk from w; returns the count, or 2^64-1 on a full dictionary |
| 462 | fn fts_tokcol src ulong n ulong w ulong to ulong |
| 463 | uf_start src n |
| 464 | k = ulong 0 |
| 465 | for a ulong 17 |
| 466 | for j ulong 999999 |
| 467 | if not uf_next |
| 468 | ret k |
| 469 | end |
| 470 | m = get qtfn 0 |
| 471 | if gt m 32768 |
| 472 | m = ulong 32768 |
| 473 | end |
| 474 | t = fts_tid 0 0 m true |
| 475 | if eq t QTM |
| 476 | put qtkf 0 k |
| 477 | ret 18446744073709551615 |
| 478 | end |
| 479 | put qtmf t uint add ulong get qtmf t 1 |
| 480 | put qtk add w k uint t |
| 481 | k = add k 1 |
| 482 | end |
| 483 | end |
| 484 | trap 9 |
| 485 | end |
| 486 | fn fts_put doc ulong row ulong to bool |
| 487 | fts_init |
| 488 | if ge doc QD |
| 489 | ret false |
| 490 | end |
| 491 | if ge get qgr 0 QD |
| 492 | ret false |
| 493 | end |
| 494 | fts_del doc |
| 495 | p = fts_spos row |
| 496 | if lt p get qsn 0 |
| 497 | if eq get qdrow ulong get qsr p row |
| 498 | ret false |
| 499 | end |
| 500 | end |
| 501 | nd = div add get qnml 0 1 2 |
| 502 | nd = add nd div add get qctl 0 1 2 |
| 503 | nd = add nd 3 |
| 504 | if gt add get qtkn 0 nd QTKN |
| 505 | fts_cmp |
| 506 | if gt add get qtkn 0 nd QTKN |
| 507 | ret false |
| 508 | end |
| 509 | end |
| 510 | s = get qtkn 0 |
| 511 | put qtk s uint doc |
| 512 | n1 = fts_tokcol 1 get qnml 0 add s 3 |
| 513 | if eq n1 18446744073709551615 |
| 514 | ret fts_putfail s get qtkf 0 |
| 515 | end |
| 516 | n2 = fts_tokcol 0 get qctl 0 add add s 3 n1 |
| 517 | if eq n2 18446744073709551615 |
| 518 | ret fts_putfail s add n1 get qtkf 0 |
| 519 | end |
| 520 | put qtk add s 1 uint n1 |
| 521 | put qtk add s 2 uint n2 |
| 522 | put qtkn 0 add add s 3 add n1 n2 |
| 523 | put qdst doc uint add s 1 |
| 524 | put qdrow doc row |
| 525 | fts_sins p doc |
| 526 | put qtot 0 add get qtot 0 add n1 n2 |
| 527 | fts_ladd doc |
| 528 | fts_gadd doc row |
| 529 | ret true |
| 530 | end |
| 531 | note undo a partly tokenized row (n tokens already counted in qtmf) |
| 532 | fn fts_putfail s ulong n ulong to bool |
| 533 | fts_unref add s 3 n |
| 534 | ret false |
| 535 | end |
| 536 | note ---------------- MATCH expression: lexer (fts5ExprGetToken) ---------------- |
| 537 | fn fts_qc i ulong to ulong |
| 538 | if lt i get qqe 0 |
| 539 | ret ulong get qq i |
| 540 | end |
| 541 | ret 0 |
| 542 | end |
| 543 | note sqlite3Fts5IsBareword |
| 544 | fn fts_bare c ulong to bool |
| 545 | if ge c 128 |
| 546 | ret true |
| 547 | end |
| 548 | if or eq c 26 eq c 95 |
| 549 | ret true |
| 550 | end |
| 551 | if and ge c 48 le c 57 |
| 552 | ret true |
| 553 | end |
| 554 | if and ge c 65 le c 90 |
| 555 | ret true |
| 556 | end |
| 557 | ret and ge c 97 le c 122 |
| 558 | end |
| 559 | fn fts_bad to bool |
| 560 | ret ne get qerr 0 0 |
| 561 | end |
| 562 | fn fts_seterr to void |
| 563 | if not fts_bad |
| 564 | put qerr 0 1 |
| 565 | end |
| 566 | end |
| 567 | note next token from position qtkp+qtkl; sets qtkp, qtkl; returns the FTS5 token code |
| 568 | fn fts_lex to ulong |
| 569 | z = add get qtkp 0 get qtkl 0 |
| 570 | for a ulong 2 |
| 571 | dn = false |
| 572 | for j ulong 999999 |
| 573 | c = fts_qc z |
| 574 | if or or eq c 32 eq c 9 or eq c 10 eq c 13 |
| 575 | z = add z 1 |
| 576 | else |
| 577 | dn = true |
| 578 | break |
| 579 | end |
| 580 | end |
| 581 | if dn |
| 582 | break |
| 583 | end |
| 584 | end |
| 585 | put qtkp 0 z |
| 586 | put qtkl 0 1 |
| 587 | c = fts_qc z |
| 588 | if eq c 40 |
| 589 | ret 10 |
| 590 | end |
| 591 | if eq c 41 |
| 592 | ret 11 |
| 593 | end |
| 594 | if eq c 123 |
| 595 | ret 7 |
| 596 | end |
| 597 | if eq c 125 |
| 598 | ret 8 |
| 599 | end |
| 600 | if eq c 58 |
| 601 | ret 5 |
| 602 | end |
| 603 | if eq c 44 |
| 604 | ret 13 |
| 605 | end |
| 606 | if eq c 43 |
| 607 | ret 14 |
| 608 | end |
| 609 | if eq c 42 |
| 610 | ret 15 |
| 611 | end |
| 612 | if eq c 45 |
| 613 | ret 6 |
| 614 | end |
| 615 | if eq c 94 |
| 616 | ret 12 |
| 617 | end |
| 618 | if eq c 0 |
| 619 | ret 0 |
| 620 | end |
| 621 | if eq c 34 |
| 622 | ret fts_lexq z |
| 623 | end |
| 624 | if not fts_bare c |
| 625 | fts_seterr |
| 626 | ret 0 |
| 627 | end |
| 628 | ret fts_lexw z |
| 629 | end |
| 630 | fn fts_lexq z ulong to ulong |
| 631 | z2 = add z 1 |
| 632 | for a ulong 2 |
| 633 | for j ulong 999999 |
| 634 | c = fts_qc z2 |
| 635 | if eq c 34 |
| 636 | z2 = add z2 1 |
| 637 | if ne fts_qc z2 34 |
| 638 | put qtkl 0 sub z2 z |
| 639 | ret 9 |
| 640 | end |
| 641 | end |
| 642 | if eq fts_qc z2 0 |
| 643 | fts_seterr |
| 644 | ret 0 |
| 645 | end |
| 646 | z2 = add z2 1 |
| 647 | end |
| 648 | end |
| 649 | trap 9 |
| 650 | end |
| 651 | fn fts_lexw z ulong to ulong |
| 652 | z2 = add z 1 |
| 653 | for a ulong 2 |
| 654 | dn = false |
| 655 | for j ulong 999999 |
| 656 | if not fts_bare fts_qc z2 |
| 657 | dn = true |
| 658 | break |
| 659 | end |
| 660 | z2 = add z2 1 |
| 661 | end |
| 662 | if dn |
| 663 | break |
| 664 | end |
| 665 | end |
| 666 | n = sub z2 z |
| 667 | put qtkl 0 n |
| 668 | if eq n 2 |
| 669 | if and eq fts_qc z 79 eq fts_qc add z 1 82 |
| 670 | ret 1 |
| 671 | end |
| 672 | end |
| 673 | if eq n 3 |
| 674 | c0 = fts_qc z |
| 675 | c1 = fts_qc add z 1 |
| 676 | c2 = fts_qc add z 2 |
| 677 | if and and eq c0 78 eq c1 79 eq c2 84 |
| 678 | ret 3 |
| 679 | end |
| 680 | if and and eq c0 65 eq c1 78 eq c2 68 |
| 681 | ret 2 |
| 682 | end |
| 683 | end |
| 684 | ret 9 |
| 685 | end |
| 686 | note ---------------- parse actions (fts5_expr.c) ---------------- |
| 687 | note dequote token qq[p..p+n) into qqt (sqlite3Fts5Dequote); returns the length |
| 688 | fn fts_deq p ulong n ulong to ulong |
| 689 | if ne fts_qc p 34 |
| 690 | for i ulong n |
| 691 | put qqt i get qq add p i |
| 692 | end |
| 693 | ret n |
| 694 | end |
| 695 | i = ulong 1 |
| 696 | o = ulong 0 |
| 697 | while lt i n bound 999999 |
| 698 | c = fts_qc add p i |
| 699 | if eq c 34 |
| 700 | if ge add i 1 n |
| 701 | ret o |
| 702 | end |
| 703 | if ne fts_qc add add p i 1 34 |
| 704 | ret o |
| 705 | end |
| 706 | i = add i 2 |
| 707 | put qqt o 34 |
| 708 | o = add o 1 |
| 709 | else |
| 710 | put qqt o uchar c |
| 711 | o = add o 1 |
| 712 | i = add i 1 |
| 713 | end |
| 714 | end |
| 715 | ret o |
| 716 | end |
| 717 | fn fts_lower c ulong to ulong |
| 718 | if and ge c 65 le c 90 |
| 719 | ret add c 32 |
| 720 | end |
| 721 | ret c |
| 722 | end |
| 723 | note column index of qqt[0..n) by sqlite3_stricmp, 4 when unknown |
| 724 | fn fts_colix n ulong to ulong |
| 725 | for k ulong 4 |
| 726 | o = ulong 0 |
| 727 | m = ulong 2 |
| 728 | if eq k 1 |
| 729 | o = ulong 3 |
| 730 | m = ulong 14 |
| 731 | end |
| 732 | if eq k 2 |
| 733 | o = ulong 18 |
| 734 | m = ulong 7 |
| 735 | end |
| 736 | if eq k 3 |
| 737 | o = ulong 26 |
| 738 | m = ulong 10 |
| 739 | end |
| 740 | if eq m n |
| 741 | eq2 = true |
| 742 | for i ulong n |
| 743 | if ne fts_lower ulong get qqt i ulong get qcolx add o i |
| 744 | eq2 = false |
| 745 | end |
| 746 | end |
| 747 | if eq2 |
| 748 | ret k |
| 749 | end |
| 750 | end |
| 751 | end |
| 752 | ret 4 |
| 753 | end |
| 754 | note colset value: 0 = NULL, 256 + bit mask over columns 0..3 |
| 755 | fn fts_colset cs ulong p ulong n ulong to ulong |
| 756 | if fts_bad |
| 757 | ret 0 |
| 758 | end |
| 759 | k = fts_colix fts_deq p n |
| 760 | if eq k 4 |
| 761 | fts_seterr |
| 762 | ret 0 |
| 763 | end |
| 764 | v = cs |
| 765 | if eq v 0 |
| 766 | v = ulong 256 |
| 767 | end |
| 768 | ret or v shl ulong 1 k |
| 769 | end |
| 770 | fn fts_csinv cs ulong to ulong |
| 771 | if fts_bad |
| 772 | ret 0 |
| 773 | end |
| 774 | ret or 256 and not cs 15 |
| 775 | end |
| 776 | note new phrase id (1-based) with no terms; terms start at the end of the term pool |
| 777 | fn fts_newph to ulong |
| 778 | h = get qhcn 0 |
| 779 | if ge h QX |
| 780 | put qerr 0 2 |
| 781 | ret 0 |
| 782 | end |
| 783 | put qht0 h get qxn 0 |
| 784 | put qhn h 0 |
| 785 | put qhnd h 0 |
| 786 | put qhsel h 0 |
| 787 | put qhs h 0 |
| 788 | put qhc h 0 |
| 789 | put qhcn 0 add h 1 |
| 790 | ret add h 1 |
| 791 | end |
| 792 | note append the token in qtf to phrase ph (1-based); terms of ph stay contiguous at the pool end |
| 793 | fn fts_addterm ph ulong to void |
| 794 | h = sub ph 1 |
| 795 | t0 = get qht0 h |
| 796 | n = get qhn h |
| 797 | if ne add t0 n get qxn 0 |
| 798 | fts_mvph h |
| 799 | t0 = get qht0 h |
| 800 | end |
| 801 | t = get qxn 0 |
| 802 | m = get qtfn 0 |
| 803 | if gt m 32768 |
| 804 | m = ulong 32768 |
| 805 | end |
| 806 | b = get qxbn 0 |
| 807 | if or ge t QX gt add b m 1048576 |
| 808 | put qerr 0 2 |
| 809 | ret |
| 810 | end |
| 811 | for i ulong m |
| 812 | put qxb add b i get qtf i |
| 813 | end |
| 814 | put qxbn 0 add b m |
| 815 | put qxo t b |
| 816 | put qxl t m |
| 817 | put qxf t 0 |
| 818 | put qxr t 0 |
| 819 | put qxn 0 add t 1 |
| 820 | put qhn h add n 1 |
| 821 | end |
| 822 | note copy the terms of phrase h to the end of the term pool |
| 823 | fn fts_mvph h ulong to void |
| 824 | t0 = get qht0 h |
| 825 | n = get qhn h |
| 826 | t = get qxn 0 |
| 827 | if gt add t n QX |
| 828 | put qerr 0 2 |
| 829 | ret |
| 830 | end |
| 831 | for i ulong n |
| 832 | put qxo add t i get qxo add t0 i |
| 833 | put qxl add t i get qxl add t0 i |
| 834 | put qxf add t i get qxf add t0 i |
| 835 | put qxr add t i get qxr add t0 i |
| 836 | end |
| 837 | put qht0 h t |
| 838 | put qxn 0 add t n |
| 839 | end |
| 840 | note sqlite3Fts5ParseTerm |
| 841 | fn fts_term app ulong p ulong n ulong pre ulong to ulong |
| 842 | if fts_bad |
| 843 | ret 0 |
| 844 | end |
| 845 | m = fts_deq p n |
| 846 | uf_start 2 m |
| 847 | ph = app |
| 848 | for a ulong 2 |
| 849 | dn = false |
| 850 | for j ulong 999999 |
| 851 | if not uf_next |
| 852 | dn = true |
| 853 | break |
| 854 | end |
| 855 | if eq ph 0 |
| 856 | ph = fts_newph |
| 857 | if eq ph 0 |
| 858 | ret 0 |
| 859 | end |
| 860 | end |
| 861 | fts_addterm ph |
| 862 | if fts_bad |
| 863 | ret 0 |
| 864 | end |
| 865 | end |
| 866 | if dn |
| 867 | break |
| 868 | end |
| 869 | end |
| 870 | if eq app 0 |
| 871 | k = get qapn 0 |
| 872 | if ge k QX |
| 873 | put qerr 0 2 |
| 874 | ret 0 |
| 875 | end |
| 876 | put qapn 0 add k 1 |
| 877 | if eq ph 0 |
| 878 | ph = fts_newph |
| 879 | if eq ph 0 |
| 880 | ret 0 |
| 881 | end |
| 882 | end |
| 883 | end |
| 884 | put qap sub get qapn 0 1 ph |
| 885 | fts_setpre sub ph 1 pre |
| 886 | ret ph |
| 887 | end |
| 888 | fn fts_setpre h ulong pre ulong to void |
| 889 | n = get qhn h |
| 890 | if gt n 0 |
| 891 | put qxr add get qht0 h sub n 1 pre |
| 892 | end |
| 893 | end |
| 894 | fn fts_caret ph ulong to void |
| 895 | if ne ph 0 |
| 896 | if gt get qhn sub ph 1 0 |
| 897 | put qxf get qht0 sub ph 1 1 |
| 898 | end |
| 899 | end |
| 900 | end |
| 901 | note sqlite3Fts5ParseNearset |
| 902 | fn fts_near ny ulong ph ulong to ulong |
| 903 | if fts_bad |
| 904 | ret 0 |
| 905 | end |
| 906 | if eq ph 0 |
| 907 | ret ny |
| 908 | end |
| 909 | y = ny |
| 910 | if eq y 0 |
| 911 | k = get qyn 0 |
| 912 | if ge k QX |
| 913 | put qerr 0 2 |
| 914 | ret 0 |
| 915 | end |
| 916 | put qyd k 0 |
| 917 | put qycs k 0 |
| 918 | put qyp0 k get qyphn 0 |
| 919 | put qyk k 0 |
| 920 | put qyn 0 add k 1 |
| 921 | y = add k 1 |
| 922 | else |
| 923 | fts_mvny sub y 1 |
| 924 | if fts_bad |
| 925 | ret 0 |
| 926 | end |
| 927 | end |
| 928 | ret fts_near2 sub y 1 ph |
| 929 | end |
| 930 | fn fts_near2 y ulong ph0 ulong to ulong |
| 931 | ph = ph0 |
| 932 | k = get qyk y |
| 933 | b = get qyp0 y |
| 934 | if gt k 0 |
| 935 | last = get qyph add b sub k 1 |
| 936 | if eq get qhn sub ph 1 0 |
| 937 | k = sub k 1 |
| 938 | put qapn 0 sub get qapn 0 1 |
| 939 | ph = last |
| 940 | else |
| 941 | if eq get qhn sub last 1 0 |
| 942 | put qap sub get qapn 0 2 ph |
| 943 | put qapn 0 sub get qapn 0 1 |
| 944 | k = sub k 1 |
| 945 | end |
| 946 | end |
| 947 | end |
| 948 | if ge add b k QX |
| 949 | put qerr 0 2 |
| 950 | ret 0 |
| 951 | end |
| 952 | put qyph add b k ph |
| 953 | put qyk y add k 1 |
| 954 | put qyphn 0 add add b k 1 |
| 955 | ret add y 1 |
| 956 | end |
| 957 | note keep the phrase list of nearset y at the end of the pool |
| 958 | fn fts_mvny y ulong to void |
| 959 | b = get qyp0 y |
| 960 | k = get qyk y |
| 961 | e = get qyphn 0 |
| 962 | if eq add b k e |
| 963 | ret |
| 964 | end |
| 965 | if gt add e k QX |
| 966 | put qerr 0 2 |
| 967 | ret |
| 968 | end |
| 969 | for i ulong k |
| 970 | put qyph add e i get qyph add b i |
| 971 | end |
| 972 | put qyp0 y e |
| 973 | put qyphn 0 add e k |
| 974 | end |
| 975 | fn fts_pnear p ulong n ulong to void |
| 976 | if ne n 4 |
| 977 | fts_seterr |
| 978 | ret |
| 979 | end |
| 980 | if or or ne fts_qc p 78 ne fts_qc add p 1 69 or ne fts_qc add p 2 65 ne fts_qc add p 3 82 |
| 981 | fts_seterr |
| 982 | end |
| 983 | end |
| 984 | note sqlite3Fts5ParseSetDistance (int arithmetic wraps as the compiled C does) |
| 985 | fn fts_dist ny ulong p ulong n ulong to void |
| 986 | if eq ny 0 |
| 987 | ret |
| 988 | end |
| 989 | v = ulong 10 |
| 990 | if gt n 0 |
| 991 | v = ulong 0 |
| 992 | for i ulong n |
| 993 | c = fts_qc add p i |
| 994 | if or lt c 48 gt c 57 |
| 995 | fts_seterr |
| 996 | ret |
| 997 | end |
| 998 | v = and add mul v 10 sub c 48 4294967295 |
| 999 | end |
| 1000 | end |
| 1001 | d = long v |
| 1002 | if ge v 2147483648 |
| 1003 | d = sub d 4294967296 |
| 1004 | end |
| 1005 | put qyd sub ny 1 d |
| 1006 | end |
| 1007 | note ---------------- expression nodes (sqlite3Fts5ParseNode and friends) ---------------- |
| 1008 | note node types: 0 EOF, 1 OR, 2 AND, 3 NOT, 4 TERM, 9 STRING |
| 1009 | fn fts_newnode ty ulong ny ulong nch ulong to ulong |
| 1010 | o = get qocn 0 |
| 1011 | c = get qkidn 0 |
| 1012 | if or ge o QX gt add c nch QX |
| 1013 | put qerr 0 2 |
| 1014 | ret 0 |
| 1015 | end |
| 1016 | put qot o ty |
| 1017 | put qox o 1 |
| 1018 | put qoe o 0 |
| 1019 | put qonm o 0 |
| 1020 | put qow o 0 |
| 1021 | put qoy o ny |
| 1022 | put qoh o 0 |
| 1023 | put qoc o 0 |
| 1024 | put qocs o c |
| 1025 | put qkidn 0 add c nch |
| 1026 | put qocn 0 add o 1 |
| 1027 | ret add o 1 |
| 1028 | end |
| 1029 | note fts5ExprAssignXNext for a STRING node |
| 1030 | fn fts_xnext o ulong to void |
| 1031 | y = sub get qoy o 1 |
| 1032 | if eq get qyk y 1 |
| 1033 | h = sub get qyph get qyp0 y 1 |
| 1034 | if eq get qhn h 1 |
| 1035 | if eq get qxf get qht0 h 0 |
| 1036 | put qot o 4 |
| 1037 | end |
| 1038 | end |
| 1039 | end |
| 1040 | end |
| 1041 | fn fts_child o ulong i ulong to ulong |
| 1042 | ret get qkid add get qocs o i |
| 1043 | end |
| 1044 | fn fts_addch p ulong sub0 ulong to void |
| 1045 | o = sub p 1 |
| 1046 | s = sub sub0 1 |
| 1047 | ii = get qoc o |
| 1048 | ty = get qot o |
| 1049 | if and ne ty 3 eq get qot s ty |
| 1050 | b = add get qocs o ii |
| 1051 | for i ulong get qoc s |
| 1052 | put qkid add b i fts_child s i |
| 1053 | end |
| 1054 | put qoc o add ii get qoc s |
| 1055 | else |
| 1056 | put qkid add get qocs o ii sub0 |
| 1057 | put qoc o add ii 1 |
| 1058 | end |
| 1059 | fts_hgt o ii |
| 1060 | end |
| 1061 | fn fts_hgt o ulong ii ulong to void |
| 1062 | for i ulong sub get qoc o ii |
| 1063 | ch = sub fts_child o add ii i 1 |
| 1064 | hh = add get qoh ch 1 |
| 1065 | if gt hh get qoh o |
| 1066 | put qoh o hh |
| 1067 | end |
| 1068 | end |
| 1069 | end |
| 1070 | fn fts_node ty ulong l ulong r ulong ny ulong to ulong |
| 1071 | if fts_bad |
| 1072 | ret 0 |
| 1073 | end |
| 1074 | if eq ty 9 |
| 1075 | if eq ny 0 |
| 1076 | ret 0 |
| 1077 | end |
| 1078 | ret fts_strnode ny |
| 1079 | end |
| 1080 | if eq l 0 |
| 1081 | ret r |
| 1082 | end |
| 1083 | if eq r 0 |
| 1084 | ret l |
| 1085 | end |
| 1086 | nch = ulong 2 |
| 1087 | if ne ty 3 |
| 1088 | if eq get qot sub l 1 ty |
| 1089 | nch = add nch sub get qoc sub l 1 1 |
| 1090 | end |
| 1091 | if eq get qot sub r 1 ty |
| 1092 | nch = add nch sub get qoc sub r 1 1 |
| 1093 | end |
| 1094 | end |
| 1095 | n = fts_newnode ty 0 nch |
| 1096 | if eq n 0 |
| 1097 | ret 0 |
| 1098 | end |
| 1099 | fts_addch n l |
| 1100 | fts_addch n r |
| 1101 | if gt get qoh sub n 1 256 |
| 1102 | fts_seterr |
| 1103 | ret 0 |
| 1104 | end |
| 1105 | ret n |
| 1106 | end |
| 1107 | fn fts_strnode ny ulong to ulong |
| 1108 | n = fts_newnode 9 ny 0 |
| 1109 | if eq n 0 |
| 1110 | ret 0 |
| 1111 | end |
| 1112 | o = sub n 1 |
| 1113 | fts_xnext o |
| 1114 | y = sub ny 1 |
| 1115 | b = get qyp0 y |
| 1116 | for i ulong get qyk y |
| 1117 | h = sub get qyph add b i 1 |
| 1118 | put qhnd h n |
| 1119 | if eq get qhn h 0 |
| 1120 | put qox o 0 |
| 1121 | put qot o 0 |
| 1122 | end |
| 1123 | end |
| 1124 | ret n |
| 1125 | end |
| 1126 | note sqlite3Fts5ParseImplicitAnd |
| 1127 | fn fts_iand l ulong r ulong to ulong |
| 1128 | if fts_bad |
| 1129 | ret 0 |
| 1130 | end |
| 1131 | pv = l |
| 1132 | if eq get qot sub l 1 2 |
| 1133 | pv = fts_child sub l 1 sub get qoc sub l 1 1 |
| 1134 | end |
| 1135 | if eq get qot sub r 1 0 |
| 1136 | put qapn 0 sub get qapn 0 1 |
| 1137 | ret l |
| 1138 | end |
| 1139 | if eq get qot sub pv 1 0 |
| 1140 | rt = r |
| 1141 | if ne pv l |
| 1142 | put qkid add get qocs sub l 1 sub get qoc sub l 1 1 r |
| 1143 | rt = l |
| 1144 | end |
| 1145 | nr = get qyk sub get qoy sub r 1 1 |
| 1146 | ix = sub sub get qapn 0 1 nr |
| 1147 | for i ulong nr |
| 1148 | put qap add ix i get qap add add ix i 1 |
| 1149 | end |
| 1150 | put qapn 0 sub get qapn 0 1 |
| 1151 | ret rt |
| 1152 | end |
| 1153 | ret fts_node 2 l r 0 |
| 1154 | end |
| 1155 | note sqlite3Fts5ParseSetColset (recursive part) |
| 1156 | fn fts_setcs n ulong cs ulong to void |
| 1157 | if fts_bad |
| 1158 | ret |
| 1159 | end |
| 1160 | o = sub n 1 |
| 1161 | ty = get qot o |
| 1162 | if or eq ty 9 eq ty 4 |
| 1163 | y = sub get qoy o 1 |
| 1164 | old = get qycs y |
| 1165 | if ne old 0 |
| 1166 | m = and and old cs 15 |
| 1167 | put qycs y or 256 m |
| 1168 | if eq m 0 |
| 1169 | put qot o 0 |
| 1170 | put qox o 0 |
| 1171 | end |
| 1172 | else |
| 1173 | put qycs y cs |
| 1174 | end |
| 1175 | ret |
| 1176 | end |
| 1177 | for i ulong get qoc o |
| 1178 | fts_setcs fts_child o i cs |
| 1179 | end |
| 1180 | end |
| 1181 | note ---------------- Lemon LALR driver (sqlite3Fts5Parser) ---------------- |
| 1182 | fn fts_ptab i ulong to ulong |
| 1183 | ret get qptab i |
| 1184 | end |
| 1185 | fn fts_fshift look ulong st ulong to ulong |
| 1186 | if gt st 34 |
| 1187 | ret st |
| 1188 | end |
| 1189 | i = add fts_ptab add QPS st look |
| 1190 | if ne fts_ptab add QPL i look |
| 1191 | ret fts_ptab add QPD st |
| 1192 | end |
| 1193 | ret fts_ptab add QPA i |
| 1194 | end |
| 1195 | fn fts_freduce st ulong look ulong to ulong |
| 1196 | i = sub add fts_ptab add QPR st look 100 |
| 1197 | ret fts_ptab add QPA i |
| 1198 | end |
| 1199 | fn fts_shift ns ulong mj ulong to void |
| 1200 | t = add get qst 0 1 |
| 1201 | if gt t 99 |
| 1202 | fts_seterr |
| 1203 | ret |
| 1204 | end |
| 1205 | s = ns |
| 1206 | if gt s 34 |
| 1207 | s = add s 31 |
| 1208 | end |
| 1209 | put qst 0 t |
| 1210 | put qss t s |
| 1211 | put qsm t mj |
| 1212 | put qsv t get qtkp 0 |
| 1213 | put qsw t get qtkl 0 |
| 1214 | end |
| 1215 | note feed one token; mirrors sqlite3Fts5Parser for a grammar without an error symbol |
| 1216 | fn fts_lp mj ulong to void |
| 1217 | act = get qss get qst 0 |
| 1218 | for j ulong 999999 |
| 1219 | act = fts_fshift mj act |
| 1220 | if ge act 83 |
| 1221 | rl = sub act 83 |
| 1222 | if eq fts_ptab add QPN rl 0 |
| 1223 | if ge get qst 0 99 |
| 1224 | fts_seterr |
| 1225 | ret |
| 1226 | end |
| 1227 | end |
| 1228 | act = fts_reduce rl |
| 1229 | else |
| 1230 | if le act 79 |
| 1231 | fts_shift act mj |
| 1232 | ret |
| 1233 | end |
| 1234 | if eq act 81 |
| 1235 | put qst 0 sub get qst 0 1 |
| 1236 | ret |
| 1237 | end |
| 1238 | fts_seterr |
| 1239 | ret |
| 1240 | end |
| 1241 | end |
| 1242 | trap 9 |
| 1243 | end |
| 1244 | note reduce by rule rl; returns the new state |
| 1245 | fn fts_reduce rl ulong to ulong |
| 1246 | m = get qst 0 |
| 1247 | n = fts_ptab add QPN rl |
| 1248 | r = sub add m 1 n |
| 1249 | v = fts_ract rl m |
| 1250 | if ne rl 0 |
| 1251 | put qsv r v |
| 1252 | end |
| 1253 | if eq rl 22 |
| 1254 | put qsw r 0 |
| 1255 | end |
| 1256 | if eq rl 23 |
| 1257 | put qsw r get qsw m |
| 1258 | end |
| 1259 | gt0 = fts_ptab add QPH rl |
| 1260 | act = fts_freduce get qss sub m n gt0 |
| 1261 | put qst 0 r |
| 1262 | put qss r act |
| 1263 | put qsm r gt0 |
| 1264 | ret act |
| 1265 | end |
| 1266 | fn fts_sv i ulong to ulong |
| 1267 | ret get qsv i |
| 1268 | end |
| 1269 | fn fts_sw i ulong to ulong |
| 1270 | ret get qsw i |
| 1271 | end |
| 1272 | note the reduce actions; returns the value for the result slot |
| 1273 | fn fts_ract rl ulong m ulong to ulong |
| 1274 | if eq rl 0 |
| 1275 | put qexpr 0 fts_sv m |
| 1276 | ret 0 |
| 1277 | end |
| 1278 | if eq rl 1 |
| 1279 | ret fts_csinv fts_sv sub m 1 |
| 1280 | end |
| 1281 | if eq rl 2 |
| 1282 | ret fts_sv sub m 1 |
| 1283 | end |
| 1284 | if or eq rl 3 eq rl 6 |
| 1285 | ret fts_colset 0 fts_sv m fts_sw m |
| 1286 | end |
| 1287 | if eq rl 4 |
| 1288 | ret fts_csinv fts_colset 0 fts_sv m fts_sw m |
| 1289 | end |
| 1290 | if eq rl 5 |
| 1291 | ret fts_colset fts_sv sub m 1 fts_sv m fts_sw m |
| 1292 | end |
| 1293 | if eq rl 7 |
| 1294 | ret fts_node 2 fts_sv sub m 2 fts_sv m 0 |
| 1295 | end |
| 1296 | if eq rl 8 |
| 1297 | ret fts_node 1 fts_sv sub m 2 fts_sv m 0 |
| 1298 | end |
| 1299 | if eq rl 9 |
| 1300 | ret fts_node 3 fts_sv sub m 2 fts_sv m 0 |
| 1301 | end |
| 1302 | ret fts_ract2 rl m |
| 1303 | end |
| 1304 | fn fts_ract2 rl ulong m ulong to ulong |
| 1305 | if eq rl 10 |
| 1306 | fts_setcs2 fts_sv sub m 1 fts_sv sub m 4 |
| 1307 | ret fts_sv sub m 1 |
| 1308 | end |
| 1309 | if eq rl 11 |
| 1310 | ret fts_sv sub m 1 |
| 1311 | end |
| 1312 | if or eq rl 12 eq rl 13 |
| 1313 | ret fts_sv m |
| 1314 | end |
| 1315 | if eq rl 14 |
| 1316 | ret fts_iand fts_sv sub m 1 fts_sv m |
| 1317 | end |
| 1318 | if eq rl 15 |
| 1319 | ret fts_node 9 0 0 fts_sv m |
| 1320 | end |
| 1321 | if eq rl 16 |
| 1322 | n = fts_node 9 0 0 fts_sv m |
| 1323 | fts_setcs2 n fts_sv sub m 2 |
| 1324 | ret n |
| 1325 | end |
| 1326 | if or eq rl 17 eq rl 20 |
| 1327 | ret fts_near 0 fts_sv m |
| 1328 | end |
| 1329 | if eq rl 18 |
| 1330 | fts_caret fts_sv m |
| 1331 | ret fts_near 0 fts_sv m |
| 1332 | end |
| 1333 | if eq rl 19 |
| 1334 | fts_pnear fts_sv sub m 4 fts_sw sub m 4 |
| 1335 | fts_dist fts_sv sub m 2 fts_sv sub m 1 fts_sw sub m 1 |
| 1336 | ret fts_sv sub m 2 |
| 1337 | end |
| 1338 | if eq rl 21 |
| 1339 | ret fts_near fts_sv sub m 1 fts_sv m |
| 1340 | end |
| 1341 | if eq rl 22 |
| 1342 | ret 0 |
| 1343 | end |
| 1344 | if eq rl 23 |
| 1345 | ret fts_sv m |
| 1346 | end |
| 1347 | if eq rl 24 |
| 1348 | ret fts_term fts_sv sub m 3 fts_sv sub m 1 fts_sw sub m 1 fts_sv m |
| 1349 | end |
| 1350 | if eq rl 25 |
| 1351 | ret fts_term 0 fts_sv sub m 1 fts_sw sub m 1 fts_sv m |
| 1352 | end |
| 1353 | if eq rl 26 |
| 1354 | ret 1 |
| 1355 | end |
| 1356 | ret 0 |
| 1357 | end |
| 1358 | fn fts_setcs2 n ulong cs ulong to void |
| 1359 | if ne n 0 |
| 1360 | fts_setcs n cs |
| 1361 | end |
| 1362 | end |
| 1363 | note parse qq[0..qqe); leaves qexpr (root node id or 0) and qerr |
| 1364 | fn fts_parse to void |
| 1365 | put qst 0 0 |
| 1366 | put qss 0 0 |
| 1367 | put qsm 0 0 |
| 1368 | put qtkp 0 0 |
| 1369 | put qtkl 0 0 |
| 1370 | for a ulong 2 |
| 1371 | for j ulong 999999 |
| 1372 | t = fts_lex |
| 1373 | fts_lp t |
| 1374 | if or fts_bad eq t 0 |
| 1375 | ret |
| 1376 | end |
| 1377 | end |
| 1378 | end |
| 1379 | trap 9 |
| 1380 | end |
| 1381 | note ---------------- posting entries and term iterators (Fts5IndexIter) ---------------- |
| 1382 | note decode an encoded position: SQLite's (column << 32) | offset |
| 1383 | fn fts_dec e ulong to long |
| 1384 | ret long add shl add 1 shr e 31 32 and e 2147483647 |
| 1385 | end |
| 1386 | fn fts_enc p long to ulong |
| 1387 | u = ulong p |
| 1388 | ret or shl sub shr u 32 1 31 and u 4294967295 |
| 1389 | end |
| 1390 | fn fts_pos sel ulong i ulong to long |
| 1391 | if eq sel 0 |
| 1392 | ret fts_dec ulong get qep i |
| 1393 | end |
| 1394 | ret fts_dec ulong get qpa i |
| 1395 | end |
| 1396 | note end of the run of entries for one row starting at s; col-1 count into qkc1 |
| 1397 | global qkc1 ulong 1 |
| 1398 | fn fts_runend s ulong e ulong to ulong |
| 1399 | r = get qer s |
| 1400 | k = s |
| 1401 | c1 = ulong 0 |
| 1402 | for a ulong 9 |
| 1403 | for j ulong 999999 |
| 1404 | if or ge k e ne get qer k r |
| 1405 | put qkc1 0 c1 |
| 1406 | ret k |
| 1407 | end |
| 1408 | if lt get qep k 2147483648 |
| 1409 | c1 = add c1 1 |
| 1410 | end |
| 1411 | k = add k 1 |
| 1412 | end |
| 1413 | end |
| 1414 | trap 9 |
| 1415 | end |
| 1416 | note position iterator t on the first run at or after qxs with a nonempty column selection |
| 1417 | fn fts_irun t ulong to void |
| 1418 | p = get qxp t |
| 1419 | e = get qpe1 p |
| 1420 | m = get qxm t |
| 1421 | s = get qxs t |
| 1422 | for a ulong 9 |
| 1423 | for j ulong 999999 |
| 1424 | if ge s e |
| 1425 | put qxe t 1 |
| 1426 | ret |
| 1427 | end |
| 1428 | k = fts_runend s e |
| 1429 | c1 = get qkc1 0 |
| 1430 | sel = ulong 0 |
| 1431 | if ne and m 1 0 |
| 1432 | sel = c1 |
| 1433 | end |
| 1434 | if ne and m 2 0 |
| 1435 | sel = add sel sub sub k s c1 |
| 1436 | end |
| 1437 | if gt sel 0 |
| 1438 | put qxs t s |
| 1439 | put qxt t k |
| 1440 | put qxc t c1 |
| 1441 | put qxw t ulong get qer s |
| 1442 | put qxe t 0 |
| 1443 | ret |
| 1444 | end |
| 1445 | s = k |
| 1446 | end |
| 1447 | end |
| 1448 | trap 9 |
| 1449 | end |
| 1450 | fn fts_iopen t ulong to void |
| 1451 | put qxs t get qpe0 get qxp t |
| 1452 | put qxe t 0 |
| 1453 | fts_irun t |
| 1454 | end |
| 1455 | fn fts_inext t ulong to void |
| 1456 | if ne get qxe t 0 |
| 1457 | ret |
| 1458 | end |
| 1459 | put qxs t get qxt t |
| 1460 | fts_irun t |
| 1461 | end |
| 1462 | note sqlite3Fts5IterNextFrom: advance at least once, then until rowid >= x |
| 1463 | fn fts_inextf t ulong x ulong to void |
| 1464 | for a ulong 9 |
| 1465 | for j ulong 999999 |
| 1466 | fts_inext t |
| 1467 | if or ne get qxe t 0 ge get qxw t x |
| 1468 | ret |
| 1469 | end |
| 1470 | end |
| 1471 | end |
| 1472 | end |
| 1473 | note the iterator's current position list: returns the first entry index, count in qkc1 |
| 1474 | fn fts_idata t ulong to ulong |
| 1475 | m = get qxm t |
| 1476 | s = get qxs t |
| 1477 | c1 = get qxc t |
| 1478 | if eq m 1 |
| 1479 | put qkc1 0 c1 |
| 1480 | ret s |
| 1481 | end |
| 1482 | if eq m 2 |
| 1483 | put qkc1 0 sub sub get qxt t s c1 |
| 1484 | ret add s c1 |
| 1485 | end |
| 1486 | put qkc1 0 sub get qxt t s |
| 1487 | ret s |
| 1488 | end |
| 1489 | note ---------------- position-list arena (two halves, copying compaction) ---------------- |
| 1490 | fn fts_alloc n ulong to ulong |
| 1491 | if gt add get qpan 0 n QA |
| 1492 | fts_acmp |
| 1493 | if gt add get qpan 0 n QA |
| 1494 | put qerr 0 2 |
| 1495 | ret 0 |
| 1496 | end |
| 1497 | end |
| 1498 | b = add get qpah 0 get qpan 0 |
| 1499 | put qpan 0 add get qpan 0 n |
| 1500 | ret b |
| 1501 | end |
| 1502 | fn fts_acmp to void |
| 1503 | nh = ulong 0 |
| 1504 | if eq get qpah 0 0 |
| 1505 | nh = QA |
| 1506 | end |
| 1507 | w = ulong 0 |
| 1508 | for h ulong get qhcn 0 |
| 1509 | if eq get qhsel h 1 |
| 1510 | s = get qhs h |
| 1511 | c = get qhc h |
| 1512 | if gt add w c QA |
| 1513 | put qerr 0 2 |
| 1514 | ret |
| 1515 | end |
| 1516 | d = add nh w |
| 1517 | fts_cpa d s c |
| 1518 | put qhs h d |
| 1519 | w = add w c |
| 1520 | end |
| 1521 | end |
| 1522 | put qpah 0 nh |
| 1523 | put qpan 0 w |
| 1524 | end |
| 1525 | fn fts_cpa d ulong s ulong c ulong to void |
| 1526 | i = ulong 0 |
| 1527 | for a ulong 9 |
| 1528 | for j ulong 999999 |
| 1529 | if ge i c |
| 1530 | ret |
| 1531 | end |
| 1532 | put qpa add d i get qpa add s i |
| 1533 | i = add i 1 |
| 1534 | end |
| 1535 | end |
| 1536 | end |
| 1537 | note ---------------- phrase matching (fts5ExprPhraseIsMatch) ---------------- |
| 1538 | note reader over term t's iterator data; false when empty |
| 1539 | fn fts_rinit t ulong to bool |
| 1540 | s = fts_idata t |
| 1541 | c = get qkc1 0 |
| 1542 | put qrdi t s |
| 1543 | put qrde t add s c |
| 1544 | if eq c 0 |
| 1545 | ret false |
| 1546 | end |
| 1547 | put qrdp t fts_pos 0 s |
| 1548 | ret true |
| 1549 | end |
| 1550 | note advance reader t; true at EOF |
| 1551 | fn fts_rnext t ulong to bool |
| 1552 | i = add get qrdi t 1 |
| 1553 | put qrdi t i |
| 1554 | if ge i get qrde t |
| 1555 | put qrdp t -1 |
| 1556 | ret true |
| 1557 | end |
| 1558 | put qrdp t fts_pos 0 i |
| 1559 | ret false |
| 1560 | end |
| 1561 | fn fts_pmatch h ulong to bool |
| 1562 | t0 = get qht0 h |
| 1563 | nt = get qhn h |
| 1564 | s = fts_idata t0 |
| 1565 | cap = get qkc1 0 |
| 1566 | d = fts_alloc cap |
| 1567 | if fts_bad |
| 1568 | ret false |
| 1569 | end |
| 1570 | put qhsel h 1 |
| 1571 | put qhs h d |
| 1572 | put qhc h 0 |
| 1573 | for i ulong nt |
| 1574 | if not fts_rinit add t0 i |
| 1575 | ret false |
| 1576 | end |
| 1577 | end |
| 1578 | fts_pm2 h t0 nt d |
| 1579 | ret gt get qhc h 0 |
| 1580 | end |
| 1581 | global qpip long 1 |
| 1582 | fn fts_pm2 h ulong t0 ulong nt ulong d ulong to void |
| 1583 | bf = ne get qxf t0 0 |
| 1584 | for a ulong 9 |
| 1585 | for j ulong 999999 |
| 1586 | put qpip 0 get qrdp t0 |
| 1587 | for b ulong 9 |
| 1588 | r = ulong 1 |
| 1589 | for k ulong 999999 |
| 1590 | r = fts_pstep t0 nt |
| 1591 | if eq r 2 |
| 1592 | ret |
| 1593 | end |
| 1594 | if eq r 0 |
| 1595 | break |
| 1596 | end |
| 1597 | end |
| 1598 | if eq r 0 |
| 1599 | break |
| 1600 | end |
| 1601 | end |
| 1602 | ip = get qpip 0 |
| 1603 | if or not bf eq and ulong ip 2147483647 0 |
| 1604 | c = get qhc h |
| 1605 | put qpa add d c uint fts_enc ip |
| 1606 | put qhc h add c 1 |
| 1607 | end |
| 1608 | for i ulong nt |
| 1609 | if fts_rnext add t0 i |
| 1610 | ret |
| 1611 | end |
| 1612 | end |
| 1613 | end |
| 1614 | end |
| 1615 | end |
| 1616 | note one pass of the phrase alignment loop: 0 aligned at qpip, 1 not yet, 2 a reader hit EOF |
| 1617 | fn fts_pstep t0 ulong nt ulong to ulong |
| 1618 | ip = get qpip 0 |
| 1619 | mt = true |
| 1620 | for i ulong nt |
| 1621 | t = add t0 i |
| 1622 | adj = add ip long i |
| 1623 | if ne get qrdp t adj |
| 1624 | mt = false |
| 1625 | if fts_radv t adj |
| 1626 | ret 2 |
| 1627 | end |
| 1628 | if gt get qrdp t adj |
| 1629 | ip = sub get qrdp t long i |
| 1630 | end |
| 1631 | end |
| 1632 | end |
| 1633 | put qpip 0 ip |
| 1634 | if mt |
| 1635 | ret 0 |
| 1636 | end |
| 1637 | ret 1 |
| 1638 | end |
| 1639 | note advance reader t while its position is below adj; true at EOF |
| 1640 | fn fts_radv t ulong adj long to bool |
| 1641 | for a ulong 9 |
| 1642 | for j ulong 999999 |
| 1643 | if ge get qrdp t adj |
| 1644 | ret false |
| 1645 | end |
| 1646 | if fts_rnext t |
| 1647 | ret true |
| 1648 | end |
| 1649 | end |
| 1650 | end |
| 1651 | trap 9 |
| 1652 | end |
| 1653 | note ---------------- NEAR (fts5ExprNearIsMatch, lookahead readers by phrase slot) ---------------- |
| 1654 | global qlkb ulong QX |
| 1655 | fn fts_lknext i ulong to bool |
| 1656 | p = get qlkl i |
| 1657 | put qlkp i p |
| 1658 | k = get qlki i |
| 1659 | if ge k get qlke i |
| 1660 | put qlkl i 4611686018427387904 |
| 1661 | else |
| 1662 | put qlkl i fts_pos get qlko i k |
| 1663 | put qlki i add k 1 |
| 1664 | end |
| 1665 | ret eq get qlkp i 4611686018427387904 |
| 1666 | end |
| 1667 | fn fts_nmatch y ulong to bool |
| 1668 | np = get qyk y |
| 1669 | b = get qyp0 y |
| 1670 | tot = ulong 0 |
| 1671 | for i ulong np |
| 1672 | tot = add tot get qhc sub get qyph add b i 1 |
| 1673 | end |
| 1674 | d = fts_alloc tot |
| 1675 | if fts_bad |
| 1676 | ret false |
| 1677 | end |
| 1678 | for i ulong np |
| 1679 | d = fts_lkinit i sub get qyph add b i 1 d |
| 1680 | end |
| 1681 | fts_nm2 y np b |
| 1682 | for i ulong np |
| 1683 | h = sub get qyph add b i 1 |
| 1684 | put qhsel h 1 |
| 1685 | put qhs h get qlkb i |
| 1686 | put qhc h get qlkc i |
| 1687 | end |
| 1688 | ret gt get qlkc 0 0 |
| 1689 | end |
| 1690 | note lookahead reader i over phrase h's list; output region at d; returns the next region |
| 1691 | fn fts_lkinit i ulong h ulong d ulong to ulong |
| 1692 | put qlko i get qhsel h |
| 1693 | s = get qhs h |
| 1694 | put qlki i s |
| 1695 | put qlke i add s get qhc h |
| 1696 | put qlkl i 0 |
| 1697 | z = fts_lknext i |
| 1698 | z = fts_lknext i |
| 1699 | put qlkb i d |
| 1700 | put qlkc i 0 |
| 1701 | put qlkv i 0 |
| 1702 | ret add d get qhc h |
| 1703 | end |
| 1704 | global qnmx long 1 |
| 1705 | fn fts_nm2 y ulong np ulong b ulong to void |
| 1706 | for a ulong 9 |
| 1707 | for j ulong 999999 |
| 1708 | put qnmx 0 get qlkp 0 |
| 1709 | for c ulong 9 |
| 1710 | r = ulong 1 |
| 1711 | for k ulong 999999 |
| 1712 | r = fts_nstep y np b |
| 1713 | if eq r 2 |
| 1714 | ret |
| 1715 | end |
| 1716 | if eq r 0 |
| 1717 | break |
| 1718 | end |
| 1719 | end |
| 1720 | if eq r 0 |
| 1721 | break |
| 1722 | end |
| 1723 | end |
| 1724 | fts_nemit np |
| 1725 | if fts_lknext fts_nadv np |
| 1726 | ret |
| 1727 | end |
| 1728 | end |
| 1729 | end |
| 1730 | end |
| 1731 | note one pass of the NEAR window loop: 0 matched, 1 not yet, 2 a reader hit EOF |
| 1732 | fn fts_nstep y ulong np ulong b ulong to ulong |
| 1733 | nn = get qyd y |
| 1734 | imax = get qnmx 0 |
| 1735 | mt = true |
| 1736 | for i ulong np |
| 1737 | h = sub get qyph add b i 1 |
| 1738 | imin = sub imax long get qhn h |
| 1739 | imin = sub imin nn |
| 1740 | pp = get qlkp i |
| 1741 | if or lt pp imin gt pp imax |
| 1742 | mt = false |
| 1743 | if fts_lkadv i imin |
| 1744 | ret 2 |
| 1745 | end |
| 1746 | if gt get qlkp i imax |
| 1747 | imax = get qlkp i |
| 1748 | end |
| 1749 | end |
| 1750 | end |
| 1751 | put qnmx 0 imax |
| 1752 | if mt |
| 1753 | ret 0 |
| 1754 | end |
| 1755 | ret 1 |
| 1756 | end |
| 1757 | fn fts_nemit np ulong to void |
| 1758 | for i ulong np |
| 1759 | ip = get qlkp i |
| 1760 | c2 = get qlkc i |
| 1761 | if or eq c2 0 ne ip get qlkv i |
| 1762 | put qpa add get qlkb i c2 uint fts_enc ip |
| 1763 | put qlkc i add c2 1 |
| 1764 | put qlkv i ip |
| 1765 | end |
| 1766 | end |
| 1767 | end |
| 1768 | fn fts_nadv np ulong to ulong |
| 1769 | iadv = ulong 0 |
| 1770 | imn = get qlkl 0 |
| 1771 | for i ulong np |
| 1772 | if lt get qlkl i imn |
| 1773 | imn = get qlkl i |
| 1774 | iadv = i |
| 1775 | end |
| 1776 | end |
| 1777 | ret iadv |
| 1778 | end |
| 1779 | fn fts_lkadv i ulong imin long to bool |
| 1780 | for a ulong 9 |
| 1781 | for j ulong 999999 |
| 1782 | if ge get qlkp i imin |
| 1783 | ret false |
| 1784 | end |
| 1785 | if fts_lknext i |
| 1786 | ret true |
| 1787 | end |
| 1788 | end |
| 1789 | end |
| 1790 | trap 9 |
| 1791 | end |
| 1792 | note ---------------- nodes (fts5ExprNode* engine, ascending rowid) ---------------- |
| 1793 | fn fts_ph y ulong i ulong to ulong |
| 1794 | ret sub get qyph add get qyp0 y i 1 |
| 1795 | end |
| 1796 | fn fts_mask y ulong to ulong |
| 1797 | cs = get qycs y |
| 1798 | if eq cs 0 |
| 1799 | ret 3 |
| 1800 | end |
| 1801 | ret and shr cs 1 3 |
| 1802 | end |
| 1803 | note fts5ExprNearTest; true on a match |
| 1804 | fn fts_ntest2 o ulong to bool |
| 1805 | y = sub get qoy o 1 |
| 1806 | np = get qyk y |
| 1807 | i = ulong 0 |
| 1808 | while lt i np bound 999999 |
| 1809 | if not fts_nph y fts_ph y i |
| 1810 | break |
| 1811 | end |
| 1812 | if fts_bad |
| 1813 | ret false |
| 1814 | end |
| 1815 | i = add i 1 |
| 1816 | end |
| 1817 | if ne i np |
| 1818 | ret false |
| 1819 | end |
| 1820 | if eq np 1 |
| 1821 | ret true |
| 1822 | end |
| 1823 | ret fts_nmatch y |
| 1824 | end |
| 1825 | note one phrase of fts5ExprNearTest: false when it does not match |
| 1826 | fn fts_nph y ulong h ulong to bool |
| 1827 | t0 = get qht0 h |
| 1828 | if gt get qhn h 1 |
| 1829 | ret fts_pmatch h |
| 1830 | end |
| 1831 | if or ne get qycs y 0 ne get qxf t0 0 |
| 1832 | ret fts_pmatch h |
| 1833 | end |
| 1834 | s = fts_idata t0 |
| 1835 | put qhsel h 0 |
| 1836 | put qhs h s |
| 1837 | put qhc h get qkc1 0 |
| 1838 | ret true |
| 1839 | end |
| 1840 | note fts5ExprNearInitAll |
| 1841 | fn fts_ninit o ulong to void |
| 1842 | y = sub get qoy o 1 |
| 1843 | m = fts_mask y |
| 1844 | for i ulong get qyk y |
| 1845 | if not fts_phopen fts_ph y i m |
| 1846 | put qoe o 1 |
| 1847 | ret |
| 1848 | end |
| 1849 | end |
| 1850 | put qoe o 0 |
| 1851 | end |
| 1852 | note open the iterators of phrase h with column mask m; false when one matches no row |
| 1853 | fn fts_phopen h ulong m ulong to bool |
| 1854 | n = get qhn h |
| 1855 | if eq n 0 |
| 1856 | ret false |
| 1857 | end |
| 1858 | t0 = get qht0 h |
| 1859 | for k ulong n |
| 1860 | t = add t0 k |
| 1861 | put qxm t m |
| 1862 | fts_iopen t |
| 1863 | if ne get qxe t 0 |
| 1864 | ret false |
| 1865 | end |
| 1866 | end |
| 1867 | ret true |
| 1868 | end |
| 1869 | fn fts_t0 o ulong to ulong |
| 1870 | ret get qht0 fts_ph sub get qoy o 1 0 |
| 1871 | end |
| 1872 | note fts5ExprNodeTest_STRING |
| 1873 | global qslast ulong 1 |
| 1874 | fn fts_tstr o ulong to void |
| 1875 | y = sub get qoy o 1 |
| 1876 | put qslast 0 get qxw fts_t0 o |
| 1877 | for a ulong 9 |
| 1878 | for j ulong 999999 |
| 1879 | r = ulong 0 |
| 1880 | for i ulong get qyk y |
| 1881 | v = fts_sph fts_ph y i |
| 1882 | if eq v 2 |
| 1883 | put qoe o 1 |
| 1884 | ret |
| 1885 | end |
| 1886 | if eq v 1 |
| 1887 | r = ulong 1 |
| 1888 | end |
| 1889 | end |
| 1890 | if eq r 0 |
| 1891 | put qow o get qslast 0 |
| 1892 | if fts_ntest2 o |
| 1893 | put qonm o 0 |
| 1894 | else |
| 1895 | put qonm o 1 |
| 1896 | end |
| 1897 | ret |
| 1898 | end |
| 1899 | end |
| 1900 | end |
| 1901 | trap 9 |
| 1902 | end |
| 1903 | note advance the iterators of phrase h towards qslast: 0 all there, 1 moved, 2 EOF |
| 1904 | fn fts_sph h ulong to ulong |
| 1905 | last = get qslast 0 |
| 1906 | t0 = get qht0 h |
| 1907 | r = ulong 0 |
| 1908 | for k ulong get qhn h |
| 1909 | t = add t0 k |
| 1910 | w = get qxw t |
| 1911 | e = get qxe t |
| 1912 | if and ne w last eq e 0 |
| 1913 | r = ulong 1 |
| 1914 | if gt last get qxw t |
| 1915 | fts_inextf t last |
| 1916 | if ne get qxe t 0 |
| 1917 | ret 2 |
| 1918 | end |
| 1919 | end |
| 1920 | last = get qxw t |
| 1921 | end |
| 1922 | end |
| 1923 | put qslast 0 last |
| 1924 | ret r |
| 1925 | end |
| 1926 | fn fts_tterm o ulong to void |
| 1927 | h = fts_ph sub get qoy o 1 0 |
| 1928 | t = get qht0 h |
| 1929 | s = fts_idata t |
| 1930 | c = get qkc1 0 |
| 1931 | put qhsel h 0 |
| 1932 | put qhs h s |
| 1933 | put qhc h c |
| 1934 | put qow o get qxw t |
| 1935 | if eq c 0 |
| 1936 | put qonm o 1 |
| 1937 | else |
| 1938 | put qonm o 0 |
| 1939 | end |
| 1940 | end |
| 1941 | fn fts_nxterm o ulong bfv bool x ulong to void |
| 1942 | t = fts_t0 o |
| 1943 | if bfv |
| 1944 | fts_inextf t x |
| 1945 | else |
| 1946 | fts_inext t |
| 1947 | end |
| 1948 | if eq get qxe t 0 |
| 1949 | fts_tterm o |
| 1950 | else |
| 1951 | put qoe o 1 |
| 1952 | put qonm o 0 |
| 1953 | end |
| 1954 | end |
| 1955 | fn fts_nxstr o ulong bfv bool x ulong to void |
| 1956 | put qonm o 0 |
| 1957 | t = fts_t0 o |
| 1958 | if bfv |
| 1959 | fts_inextf t x |
| 1960 | else |
| 1961 | fts_inext t |
| 1962 | end |
| 1963 | put qoe o get qxe t |
| 1964 | if eq get qoe o 0 |
| 1965 | fts_tstr o |
| 1966 | end |
| 1967 | end |
| 1968 | note fts5NodeCompare: 0 less, 1 equal, 2 greater |
| 1969 | fn fts_ncmp a ulong b ulong to ulong |
| 1970 | if ne get qoe b 0 |
| 1971 | ret 0 |
| 1972 | end |
| 1973 | if ne get qoe a 0 |
| 1974 | ret 2 |
| 1975 | end |
| 1976 | wa = get qow a |
| 1977 | wb = get qow b |
| 1978 | if lt wa wb |
| 1979 | ret 0 |
| 1980 | end |
| 1981 | if gt wa wb |
| 1982 | ret 2 |
| 1983 | end |
| 1984 | ret 1 |
| 1985 | end |
| 1986 | fn fts_tor o ulong to void |
| 1987 | nx = sub fts_child o 0 1 |
| 1988 | for i ulong sub get qoc o 1 |
| 1989 | c = sub fts_child o add i 1 1 |
| 1990 | cm = fts_ncmp nx c |
| 1991 | if eq cm 1 |
| 1992 | if eq get qonm c 0 |
| 1993 | cm = ulong 2 |
| 1994 | end |
| 1995 | end |
| 1996 | if eq cm 2 |
| 1997 | nx = c |
| 1998 | end |
| 1999 | end |
| 2000 | put qow o get qow nx |
| 2001 | put qoe o get qoe nx |
| 2002 | put qonm o get qonm nx |
| 2003 | end |
| 2004 | fn fts_nxor o ulong bfv bool x ulong to void |
| 2005 | last = get qow o |
| 2006 | for i ulong get qoc o |
| 2007 | c = sub fts_child o i 1 |
| 2008 | if eq get qoe c 0 |
| 2009 | w = get qow c |
| 2010 | mv = eq w last |
| 2011 | if bfv |
| 2012 | if lt w x |
| 2013 | mv = true |
| 2014 | end |
| 2015 | end |
| 2016 | if mv |
| 2017 | fts_nnext c bfv x |
| 2018 | end |
| 2019 | end |
| 2020 | end |
| 2021 | fts_tor o |
| 2022 | end |
| 2023 | fn fts_seteof o ulong to void |
| 2024 | put qoe o 1 |
| 2025 | put qonm o 0 |
| 2026 | for i ulong get qoc o |
| 2027 | fts_seteof sub fts_child o i 1 |
| 2028 | end |
| 2029 | end |
| 2030 | fn fts_zero o ulong to void |
| 2031 | ty = get qot o |
| 2032 | if or eq ty 9 eq ty 4 |
| 2033 | y = sub get qoy o 1 |
| 2034 | for i ulong get qyk y |
| 2035 | put qhc fts_ph y i 0 |
| 2036 | end |
| 2037 | ret |
| 2038 | end |
| 2039 | for i ulong get qoc o |
| 2040 | fts_zero sub fts_child o i 1 |
| 2041 | end |
| 2042 | end |
| 2043 | fn fts_tand o ulong to void |
| 2044 | last = get qow o |
| 2045 | for a ulong 9 |
| 2046 | for j ulong 999999 |
| 2047 | put qonm o 0 |
| 2048 | mt = true |
| 2049 | for i ulong get qoc o |
| 2050 | c = sub fts_child o i 1 |
| 2051 | if gt last get qow c |
| 2052 | fts_nnext c true last |
| 2053 | end |
| 2054 | if ne get qoe c 0 |
| 2055 | fts_seteof o |
| 2056 | mt = true |
| 2057 | break |
| 2058 | end |
| 2059 | if ne last get qow c |
| 2060 | mt = false |
| 2061 | last = get qow c |
| 2062 | end |
| 2063 | if ne get qonm c 0 |
| 2064 | put qonm o 1 |
| 2065 | end |
| 2066 | end |
| 2067 | if mt |
| 2068 | if and ne get qonm o 0 ne o get qroot 0 |
| 2069 | fts_zero o |
| 2070 | end |
| 2071 | put qow o last |
| 2072 | ret |
| 2073 | end |
| 2074 | end |
| 2075 | end |
| 2076 | trap 9 |
| 2077 | end |
| 2078 | fn fts_tnot o ulong to void |
| 2079 | p1 = sub fts_child o 0 1 |
| 2080 | p2 = sub fts_child o 1 1 |
| 2081 | for a ulong 9 |
| 2082 | dn = false |
| 2083 | for j ulong 999999 |
| 2084 | if ne get qoe p1 0 |
| 2085 | dn = true |
| 2086 | break |
| 2087 | end |
| 2088 | cm = fts_ncmp p1 p2 |
| 2089 | if eq cm 2 |
| 2090 | fts_nnext p2 true get qow p1 |
| 2091 | cm = fts_ncmp p1 p2 |
| 2092 | end |
| 2093 | if or ne cm 1 ne get qonm p2 0 |
| 2094 | dn = true |
| 2095 | break |
| 2096 | end |
| 2097 | fts_nnext p1 false 0 |
| 2098 | end |
| 2099 | if dn |
| 2100 | break |
| 2101 | end |
| 2102 | end |
| 2103 | put qoe o get qoe p1 |
| 2104 | put qonm o get qonm p1 |
| 2105 | put qow o get qow p1 |
| 2106 | if ne get qoe p1 0 |
| 2107 | fts_zero p2 |
| 2108 | end |
| 2109 | end |
| 2110 | fn fts_nnext o ulong bfv bool x ulong to void |
| 2111 | ty = get qot o |
| 2112 | if eq ty 4 |
| 2113 | fts_nxterm o bfv x |
| 2114 | ret |
| 2115 | end |
| 2116 | if eq ty 9 |
| 2117 | fts_nxstr o bfv x |
| 2118 | ret |
| 2119 | end |
| 2120 | if eq ty 1 |
| 2121 | fts_nxor o bfv x |
| 2122 | ret |
| 2123 | end |
| 2124 | if eq ty 2 |
| 2125 | fts_nnext sub fts_child o 0 1 bfv x |
| 2126 | fts_tand o |
| 2127 | ret |
| 2128 | end |
| 2129 | if eq ty 3 |
| 2130 | fts_nnext sub fts_child o 0 1 bfv x |
| 2131 | fts_tnot o |
| 2132 | end |
| 2133 | end |
| 2134 | fn fts_ntest o ulong to void |
| 2135 | if ne get qoe o 0 |
| 2136 | ret |
| 2137 | end |
| 2138 | ty = get qot o |
| 2139 | if eq ty 9 |
| 2140 | fts_tstr o |
| 2141 | ret |
| 2142 | end |
| 2143 | if eq ty 4 |
| 2144 | fts_tterm o |
| 2145 | ret |
| 2146 | end |
| 2147 | if eq ty 2 |
| 2148 | fts_tand o |
| 2149 | ret |
| 2150 | end |
| 2151 | if eq ty 1 |
| 2152 | fts_tor o |
| 2153 | ret |
| 2154 | end |
| 2155 | if eq ty 3 |
| 2156 | fts_tnot o |
| 2157 | end |
| 2158 | end |
| 2159 | fn fts_nfirst o ulong to void |
| 2160 | put qoe o 0 |
| 2161 | put qonm o 0 |
| 2162 | ty = get qot o |
| 2163 | if or eq ty 9 eq ty 4 |
| 2164 | fts_ninit o |
| 2165 | else |
| 2166 | if eq get qox o 0 |
| 2167 | put qoe o 1 |
| 2168 | else |
| 2169 | ne1 = ulong 0 |
| 2170 | for i ulong get qoc o |
| 2171 | c = sub fts_child o i 1 |
| 2172 | fts_nfirst c |
| 2173 | ne1 = add ne1 get qoe c |
| 2174 | end |
| 2175 | put qow o get qow sub fts_child o 0 1 |
| 2176 | if and eq ty 2 gt ne1 0 |
| 2177 | fts_seteof o |
| 2178 | end |
| 2179 | if and eq ty 1 eq ne1 get qoc o |
| 2180 | fts_seteof o |
| 2181 | end |
| 2182 | if eq ty 3 |
| 2183 | put qoe o get qoe sub fts_child o 0 1 |
| 2184 | end |
| 2185 | end |
| 2186 | end |
| 2187 | fts_ntest o |
| 2188 | end |
| 2189 | note sqlite3Fts5ExprFirst / sqlite3Fts5ExprNext (skip rows the root flags as no-match) |
| 2190 | fn fts_skipnm o ulong to void |
| 2191 | for a ulong 9 |
| 2192 | for j ulong 999999 |
| 2193 | if or eq get qonm o 0 ne get qoe o 0 |
| 2194 | ret |
| 2195 | end |
| 2196 | fts_nnext o false 0 |
| 2197 | end |
| 2198 | end |
| 2199 | trap 9 |
| 2200 | end |
| 2201 | note ---------------- patterns (distinct term/prefix keys) and posting entries ---------------- |
| 2202 | fn fts_patof t ulong to ulong |
| 2203 | for p ulong get qpn 0 |
| 2204 | if fts_pateq p t |
| 2205 | ret p |
| 2206 | end |
| 2207 | end |
| 2208 | p = get qpn 0 |
| 2209 | put qpb p get qxo t |
| 2210 | put qpl p get qxl t |
| 2211 | put qpp p get qxr t |
| 2212 | put qpn 0 add p 1 |
| 2213 | ret p |
| 2214 | end |
| 2215 | fn fts_pateq p ulong t ulong to bool |
| 2216 | n = get qxl t |
| 2217 | if ne get qpl p n |
| 2218 | ret false |
| 2219 | end |
| 2220 | if ne get qpp p get qxr t |
| 2221 | ret false |
| 2222 | end |
| 2223 | b = get qpb p |
| 2224 | o = get qxo t |
| 2225 | for i ulong n |
| 2226 | if ne get qxb add b i get qxb add o i |
| 2227 | ret false |
| 2228 | end |
| 2229 | end |
| 2230 | ret true |
| 2231 | end |
| 2232 | note mark the term ids matching pattern p with bit bt; returns the occurrence count |
| 2233 | fn fts_markp p ulong bt ulong to ulong |
| 2234 | o = get qpb p |
| 2235 | n = get qpl p |
| 2236 | if eq get qpp p 0 |
| 2237 | t = fts_tid 1 o n false |
| 2238 | if eq t QTM |
| 2239 | ret 0 |
| 2240 | end |
| 2241 | fts_mark1 t bt |
| 2242 | ret ulong get qtmf t |
| 2243 | end |
| 2244 | cn = ulong 0 |
| 2245 | nt = get qtmc 0 |
| 2246 | i = ulong 0 |
| 2247 | for a ulong 3 |
| 2248 | for j ulong 999999 |
| 2249 | if ge i nt |
| 2250 | ret cn |
| 2251 | end |
| 2252 | if ge ulong get qtml i n |
| 2253 | if fts_teqp i o n |
| 2254 | fts_mark1 i bt |
| 2255 | cn = add cn ulong get qtmf i |
| 2256 | end |
| 2257 | end |
| 2258 | i = add i 1 |
| 2259 | end |
| 2260 | end |
| 2261 | ret cn |
| 2262 | end |
| 2263 | fn fts_teqp t ulong o ulong n ulong to bool |
| 2264 | b = ulong get qtmo t |
| 2265 | for i ulong n |
| 2266 | if ne get qtb add b i get qxb add o i |
| 2267 | ret false |
| 2268 | end |
| 2269 | end |
| 2270 | ret true |
| 2271 | end |
| 2272 | fn fts_mark1 t ulong bt ulong to void |
| 2273 | m = get qtmk t |
| 2274 | if eq m 0 |
| 2275 | k = get qmkn 0 |
| 2276 | put qmkl k uint t |
| 2277 | put qmkn 0 add k 1 |
| 2278 | end |
| 2279 | put qtmk t or m shl ulong 1 bt |
| 2280 | end |
| 2281 | fn fts_unmark to void |
| 2282 | n = get qmkn 0 |
| 2283 | i = ulong 0 |
| 2284 | for a ulong 3 |
| 2285 | for j ulong 999999 |
| 2286 | if ge i n |
| 2287 | put qmkn 0 0 |
| 2288 | ret |
| 2289 | end |
| 2290 | put qtmk ulong get qmkl i 0 |
| 2291 | i = add i 1 |
| 2292 | end |
| 2293 | end |
| 2294 | end |
| 2295 | note build posting entries for patterns p0..p1-1 (at most 64): one scan of every candidate row |
| 2296 | fn fts_batch p0 ulong p1 ulong to void |
| 2297 | w = ulong 0 |
| 2298 | for k ulong sub p1 p0 |
| 2299 | p = add p0 k |
| 2300 | put qpe0 p w |
| 2301 | put qpe1 p w |
| 2302 | w = add w get qpcn p |
| 2303 | end |
| 2304 | for k ulong sub p1 p0 |
| 2305 | z = fts_markp add p0 k k |
| 2306 | end |
| 2307 | n = get qcn 0 |
| 2308 | r = ulong 0 |
| 2309 | for a ulong 5 |
| 2310 | for j ulong 999999 |
| 2311 | if ge r n |
| 2312 | fts_unmark |
| 2313 | ret |
| 2314 | end |
| 2315 | fts_scanrow r p0 |
| 2316 | r = add r 1 |
| 2317 | end |
| 2318 | end |
| 2319 | fts_unmark |
| 2320 | end |
| 2321 | fn fts_scanrow r ulong p0 ulong to void |
| 2322 | s = sub ulong get qdst ulong get qcd r 1 |
| 2323 | n1 = ulong get qtk add s 1 |
| 2324 | nt = add n1 ulong get qtk add s 2 |
| 2325 | fts_scandoc r add s 3 n1 nt p0 |
| 2326 | end |
| 2327 | fn fts_scandoc r ulong s ulong n1 ulong nt ulong p0 ulong to void |
| 2328 | i = ulong 0 |
| 2329 | for a ulong 17 |
| 2330 | for j ulong 999999 |
| 2331 | if ge i nt |
| 2332 | ret |
| 2333 | end |
| 2334 | m = get qtmk ulong get qtk add s i |
| 2335 | if ne m 0 |
| 2336 | fts_emit r i n1 m p0 |
| 2337 | end |
| 2338 | i = add i 1 |
| 2339 | end |
| 2340 | end |
| 2341 | end |
| 2342 | fn fts_emit r ulong i ulong n1 ulong m ulong p0 ulong to void |
| 2343 | e = i |
| 2344 | if ge i n1 |
| 2345 | e = or 2147483648 sub i n1 |
| 2346 | end |
| 2347 | for b ulong 64 |
| 2348 | if ne and shr m b 1 0 |
| 2349 | p = add p0 b |
| 2350 | w = get qpe1 p |
| 2351 | put qer w uint r |
| 2352 | put qep w uint e |
| 2353 | put qpe1 p add w 1 |
| 2354 | end |
| 2355 | end |
| 2356 | end |
| 2357 | note count pattern occurrences (store-wide: an upper bound on the entries of any row set) |
| 2358 | fn fts_pcount to void |
| 2359 | for p ulong get qpn 0 |
| 2360 | put qpcn p fts_markp p 0 |
| 2361 | fts_unmark |
| 2362 | end |
| 2363 | end |
| 2364 | note posting entries of the candidate rows qcd[0..qcn), in batches of at most 64 patterns and |
| 2365 | note QE entries (fts_pcount first: a pattern above QE is the capacity error 2) |
| 2366 | fn fts_postings to void |
| 2367 | np = get qpn 0 |
| 2368 | p0 = ulong 0 |
| 2369 | while lt p0 np bound 999999 |
| 2370 | if gt get qpcn p0 QE |
| 2371 | put qerr 0 2 |
| 2372 | ret |
| 2373 | end |
| 2374 | p1 = add p0 1 |
| 2375 | tot = get qpcn p0 |
| 2376 | while lt p1 np bound 64 |
| 2377 | if or ge sub p1 p0 64 gt add tot get qpcn p1 QE |
| 2378 | break |
| 2379 | end |
| 2380 | tot = add tot get qpcn p1 |
| 2381 | p1 = add p1 1 |
| 2382 | end |
| 2383 | fts_batch p0 p1 |
| 2384 | p0 = p1 |
| 2385 | end |
| 2386 | end |
| 2387 | note ---------------- bm25 (fts5Bm25Function, k1 1.2, b 0.75) ---------------- |
| 2388 | note clone phrase h (sqlite3Fts5ExprClonePhrase); returns the clone's node (0-based) |
| 2389 | fn fts_clone h ulong to ulong |
| 2390 | h2 = fts_newph |
| 2391 | if eq h2 0 |
| 2392 | ret 0 |
| 2393 | end |
| 2394 | t0 = get qht0 h |
| 2395 | nt = get qhn h |
| 2396 | if gt add get qxn 0 nt QX |
| 2397 | put qerr 0 2 |
| 2398 | ret 0 |
| 2399 | end |
| 2400 | for i ulong nt |
| 2401 | fts_cpterm add t0 i |
| 2402 | end |
| 2403 | put qhn sub h2 1 nt |
| 2404 | ny = fts_near 0 h2 |
| 2405 | if eq ny 0 |
| 2406 | ret 0 |
| 2407 | end |
| 2408 | oy = get qoy sub get qhnd h 1 |
| 2409 | put qycs sub ny 1 get qycs sub oy 1 |
| 2410 | ty = ulong 9 |
| 2411 | if eq nt 1 |
| 2412 | if eq get qxf t0 0 |
| 2413 | ty = ulong 4 |
| 2414 | end |
| 2415 | end |
| 2416 | n = fts_newnode ty ny 0 |
| 2417 | if eq n 0 |
| 2418 | ret 0 |
| 2419 | end |
| 2420 | put qhnd sub h2 1 n |
| 2421 | ret sub n 1 |
| 2422 | end |
| 2423 | fn fts_cpterm s ulong to void |
| 2424 | t = get qxn 0 |
| 2425 | put qxo t get qxo s |
| 2426 | put qxl t get qxl s |
| 2427 | put qxf t get qxf s |
| 2428 | put qxr t get qxr s |
| 2429 | put qxn 0 add t 1 |
| 2430 | end |
| 2431 | fn fts_nhit o ulong to ulong |
| 2432 | fts_nfirst o |
| 2433 | fts_skipnm o |
| 2434 | c = ulong 0 |
| 2435 | for a ulong 9 |
| 2436 | for j ulong 999999 |
| 2437 | if ne get qoe o 0 |
| 2438 | ret c |
| 2439 | end |
| 2440 | c = add c 1 |
| 2441 | fts_nnext o false 0 |
| 2442 | fts_skipnm o |
| 2443 | end |
| 2444 | end |
| 2445 | ret c |
| 2446 | end |
| 2447 | fn fts_idf to void |
| 2448 | nr = get qsn 0 |
| 2449 | put qavg 0 div f64 long get qtot 0 f64 long nr |
| 2450 | for i ulong get qapn 0 |
| 2451 | put qidf i fts_idf1 nr fts_nhit get qcln i |
| 2452 | end |
| 2453 | put qidok 0 1 |
| 2454 | end |
| 2455 | fn fts_idf1 nr ulong nh ulong to f64 |
| 2456 | a = add f64 sub long nr long nh 0.5 |
| 2457 | b = add f64 long nh 0.5 |
| 2458 | v = flog div a b |
| 2459 | if le v 0.0 |
| 2460 | ret 1.0e-6 |
| 2461 | end |
| 2462 | ret v |
| 2463 | end |
| 2464 | note phrase i's instance count at the current root row (sqlite3Fts5ExprPoslist) |
| 2465 | fn fts_freq i ulong to f64 |
| 2466 | h = sub get qap i 1 |
| 2467 | o = sub get qhnd h 1 |
| 2468 | if ne get qoe o 0 |
| 2469 | ret 0.0 |
| 2470 | end |
| 2471 | rw = get qow get qroot 0 |
| 2472 | if eq get qow o rw |
| 2473 | ret f64 get qhc h |
| 2474 | end |
| 2475 | ret 0.0 |
| 2476 | end |
| 2477 | fn fts_bm25 dt ulong to f64 |
| 2478 | np = get qapn 0 |
| 2479 | if eq np 0 |
| 2480 | ret bitcast f64 ulong 9223372036854775808 |
| 2481 | end |
| 2482 | kk = div mul f64 dt 0.75 get qavg 0 |
| 2483 | kk = mul add kk 0.25 bitcast f64 ulong 4608083138725491507 |
| 2484 | s = 0.0f64 |
| 2485 | i = ulong 0 |
| 2486 | while lt add i 1 np bound 999999 |
| 2487 | u0 = fts_bterm kk i |
| 2488 | u1 = fts_bterm kk add i 1 |
| 2489 | s = add s u0 |
| 2490 | s = add u1 s |
| 2491 | i = add i 2 |
| 2492 | end |
| 2493 | if lt i np |
| 2494 | f = get qfrq i |
| 2495 | t = div mul f bitcast f64 ulong 4612136378390124954 add kk f |
| 2496 | s = flog_fma t get qidf i s |
| 2497 | end |
| 2498 | ret neg s |
| 2499 | end |
| 2500 | note one lane of the vectorised loop: (f * 2.2) / (K + f) * idf, each operation rounded |
| 2501 | fn fts_bterm kk f64 i ulong to f64 |
| 2502 | f = get qfrq i |
| 2503 | t = div mul f bitcast f64 ulong 4612136378390124954 add kk f |
| 2504 | ret mul t get qidf i |
| 2505 | end |
| 2506 | note temp b-tree ORDER BY bm25_rank with LIMIT: stable on ties, keep at most lim rows |
| 2507 | fn fts_sort b f64 doc ulong lim ulong to void |
| 2508 | n = get qrn 0 |
| 2509 | if eq n lim |
| 2510 | if le get qrb sub n 1 b |
| 2511 | ret |
| 2512 | end |
| 2513 | n = sub n 1 |
| 2514 | end |
| 2515 | p = n |
| 2516 | while gt p 0 bound 999999 |
| 2517 | if le get qrb sub p 1 b |
| 2518 | break |
| 2519 | end |
| 2520 | put qrb p get qrb sub p 1 |
| 2521 | put qrd p get qrd sub p 1 |
| 2522 | p = sub p 1 |
| 2523 | end |
| 2524 | put qrb p b |
| 2525 | put qrd p doc |
| 2526 | put qrn 0 add n 1 |
| 2527 | end |
| 2528 | note ---------------- the search ---------------- |
| 2529 | fn fts_qreset to void |
| 2530 | put qerr 0 0 |
| 2531 | put qrn 0 0 |
| 2532 | put qrst 0 0 |
| 2533 | put qrst 2 0 |
| 2534 | put qxn 0 0 |
| 2535 | put qxbn 0 0 |
| 2536 | put qhcn 0 0 |
| 2537 | put qyn 0 0 |
| 2538 | put qyphn 0 0 |
| 2539 | put qocn 0 0 |
| 2540 | put qkidn 0 0 |
| 2541 | put qapn 0 0 |
| 2542 | put qexpr 0 0 |
| 2543 | put qpn 0 0 |
| 2544 | put qpan 0 0 |
| 2545 | put qpah 0 0 |
| 2546 | put qidok 0 0 |
| 2547 | put qmkn 0 0 |
| 2548 | end |
| 2549 | note effective MATCH length: up to the first NUL |
| 2550 | fn fts_qlen to ulong |
| 2551 | n = get qql 0 |
| 2552 | if gt n 1048576 |
| 2553 | n = ulong 1048576 |
| 2554 | end |
| 2555 | i = ulong 0 |
| 2556 | for a ulong 2 |
| 2557 | for j ulong 999999 |
| 2558 | if ge i n |
| 2559 | ret n |
| 2560 | end |
| 2561 | if eq get qq i 0 |
| 2562 | ret i |
| 2563 | end |
| 2564 | i = add i 1 |
| 2565 | end |
| 2566 | end |
| 2567 | ret n |
| 2568 | end |
| 2569 | note fts5SpecialMatch for MATCH strings starting with '*': "reads"/"id" give no joined row, others error |
| 2570 | fn fts_special to void |
| 2571 | z = ulong 1 |
| 2572 | while eq fts_qc z 32 bound 999999 |
| 2573 | z = add z 1 |
| 2574 | end |
| 2575 | n = ulong 0 |
| 2576 | while fts_spc add z n bound 999999 |
| 2577 | n = add n 1 |
| 2578 | end |
| 2579 | if eq n 5 |
| 2580 | if and and fts_ci z 114 fts_ci add z 1 101 fts_ci add z 2 97 |
| 2581 | if and fts_ci add z 3 100 fts_ci add z 4 115 |
| 2582 | ret |
| 2583 | end |
| 2584 | end |
| 2585 | end |
| 2586 | if eq n 2 |
| 2587 | if and fts_ci z 105 fts_ci add z 1 100 |
| 2588 | ret |
| 2589 | end |
| 2590 | end |
| 2591 | fts_seterr |
| 2592 | end |
| 2593 | fn fts_spc i ulong to bool |
| 2594 | c = fts_qc i |
| 2595 | ret and ne c 0 ne c 32 |
| 2596 | end |
| 2597 | fn fts_ci i ulong ch ulong to bool |
| 2598 | ret eq fts_lower fts_qc i ch |
| 2599 | end |
| 2600 | fn fts_search to ulong |
| 2601 | fts_init |
| 2602 | fts_qreset |
| 2603 | lim = get qlim 0 |
| 2604 | if ge lim 9223372036854775808 |
| 2605 | put qerr 0 1 |
| 2606 | ret 0 |
| 2607 | end |
| 2608 | if gt lim QR |
| 2609 | lim = QR |
| 2610 | end |
| 2611 | if eq lim 0 |
| 2612 | ret 0 |
| 2613 | end |
| 2614 | put qqe 0 fts_qlen |
| 2615 | if gt get qqe 0 0 |
| 2616 | if eq get qq 0 42 |
| 2617 | fts_special |
| 2618 | ret 0 |
| 2619 | end |
| 2620 | end |
| 2621 | fts_parse |
| 2622 | if fts_bad |
| 2623 | ret 0 |
| 2624 | end |
| 2625 | ret fts_run lim |
| 2626 | end |
| 2627 | fn fts_run lim ulong to ulong |
| 2628 | rt = get qexpr 0 |
| 2629 | if eq rt 0 |
| 2630 | rt = fts_newnode 0 0 0 |
| 2631 | if eq rt 0 |
| 2632 | ret 0 |
| 2633 | end |
| 2634 | put qox sub rt 1 0 |
| 2635 | end |
| 2636 | put qroot 0 sub rt 1 |
| 2637 | for i ulong get qapn 0 |
| 2638 | put qcln i fts_clone sub get qap i 1 |
| 2639 | end |
| 2640 | for t ulong get qxn 0 |
| 2641 | put qxp t fts_patof t |
| 2642 | end |
| 2643 | fts_pcount |
| 2644 | np = get qpn 0 |
| 2645 | for p ulong np |
| 2646 | if gt get qpcn p QE |
| 2647 | put qerr 0 2 |
| 2648 | ret 0 |
| 2649 | end |
| 2650 | end |
| 2651 | if ne get qlv 2 0 |
| 2652 | fts_lbuild |
| 2653 | end |
| 2654 | if not fts_plan |
| 2655 | ret 0 |
| 2656 | end |
| 2657 | put qrst 2 0 |
| 2658 | if fts_orok |
| 2659 | put qrst 2 1 |
| 2660 | fts_ortop lim |
| 2661 | ret get qrn 0 |
| 2662 | end |
| 2663 | fts_postings |
| 2664 | if fts_bad |
| 2665 | ret 0 |
| 2666 | end |
| 2667 | o = get qroot 0 |
| 2668 | fts_nfirst o |
| 2669 | fts_skipnm o |
| 2670 | for a ulong 5 |
| 2671 | for j ulong 999999 |
| 2672 | if or ne get qoe o 0 fts_bad |
| 2673 | if fts_bad |
| 2674 | put qrn 0 0 |
| 2675 | end |
| 2676 | ret get qrn 0 |
| 2677 | end |
| 2678 | fts_row lim |
| 2679 | fts_nnext o false 0 |
| 2680 | fts_skipnm o |
| 2681 | end |
| 2682 | end |
| 2683 | ret get qrn 0 |
| 2684 | end |
| 2685 | fn fts_row lim ulong to void |
| 2686 | o = get qroot 0 |
| 2687 | doc = ulong get qcd get qow o |
| 2688 | if eq get qidok 0 0 |
| 2689 | fts_idf |
| 2690 | end |
| 2691 | for i ulong get qapn 0 |
| 2692 | put qfrq i fts_freq i |
| 2693 | end |
| 2694 | s = sub ulong get qdst doc 1 |
| 2695 | b = fts_bm25 add ulong get qtk add s 1 ulong get qtk add s 2 |
| 2696 | if ne b b |
| 2697 | b = 5.0 |
| 2698 | end |
| 2699 | if eq get qok doc 1 |
| 2700 | if eq ulong get qdg doc get qgsel 0 |
| 2701 | fts_sort b doc lim |
| 2702 | end |
| 2703 | end |
| 2704 | end |
| 2705 | note ---------------- the plan: which rows the query runs over ---------------- |
| 2706 | note Rows the query runs over, into qcd: 1 (scan the group's list) or 2 (the term lists of each |
| 2707 | note phrase's driver term) give the group's rows with qok 1 that can match, then the bm25 |
| 2708 | note statistics are computed before the row loop (fts_idfall); 3 is every row of the table with |
| 2709 | note the statistics computed at the first matching row, which is how SQLite runs it. The answer |
| 2710 | note is the same: a row matches or not on its own tokens, rows keep their rowid order, idf uses |
| 2711 | note store-wide counts, and fts_route takes 3 whenever a row outside the candidates could make |
| 2712 | note the arena overflow (the capacity error SQLite's plan would then raise). false: no rows. |
| 2713 | fn fts_plan to bool |
| 2714 | md = fts_route |
| 2715 | put qrst 0 md |
| 2716 | if eq md 3 |
| 2717 | fts_call |
| 2718 | put qrst 1 get qcn 0 |
| 2719 | ret true |
| 2720 | end |
| 2721 | fts_cmain md |
| 2722 | put qrst 1 get qcn 0 |
| 2723 | if eq get qcn 0 0 |
| 2724 | ret false |
| 2725 | end |
| 2726 | if fts_idfall |
| 2727 | fts_cmain md |
| 2728 | end |
| 2729 | if fts_bad |
| 2730 | put qerr 0 0 |
| 2731 | put qidok 0 0 |
| 2732 | put qpan 0 0 |
| 2733 | put qpah 0 0 |
| 2734 | for h ulong get qhcn 0 |
| 2735 | put qhsel h 0 |
| 2736 | end |
| 2737 | put qrst 0 3 |
| 2738 | fts_call |
| 2739 | put qrst 1 get qcn 0 |
| 2740 | end |
| 2741 | ret true |
| 2742 | end |
| 2743 | fn fts_route to ulong |
| 2744 | r = get qrte 0 |
| 2745 | if eq r 3 |
| 2746 | ret 3 |
| 2747 | end |
| 2748 | if gt mul mul 2 get qhcn 0 get qlv 3 QA |
| 2749 | ret 3 |
| 2750 | end |
| 2751 | g = get qgsel 0 |
| 2752 | gs = ulong 0 |
| 2753 | if lt g QD |
| 2754 | gs = ulong get qgc g |
| 2755 | end |
| 2756 | tc = ulong 0 |
| 2757 | inf = false |
| 2758 | for i ulong get qapn 0 |
| 2759 | h = sub get qap i 1 |
| 2760 | if gt get qhn h 0 |
| 2761 | x = fts_drv h |
| 2762 | if eq x QX |
| 2763 | inf = true |
| 2764 | else |
| 2765 | tc = add tc fts_xdf x |
| 2766 | end |
| 2767 | end |
| 2768 | end |
| 2769 | if or eq r 1 inf |
| 2770 | ret 1 |
| 2771 | end |
| 2772 | if or eq r 2 lt tc gs |
| 2773 | ret 2 |
| 2774 | end |
| 2775 | ret 1 |
| 2776 | end |
| 2777 | note term id of query term x (QTM when the dictionary does not hold it) and its row count |
| 2778 | fn fts_xtid x ulong to ulong |
| 2779 | ret fts_tid 1 get qxo x get qxl x false |
| 2780 | end |
| 2781 | fn fts_xdf x ulong to ulong |
| 2782 | t = fts_xtid x |
| 2783 | if eq t QTM |
| 2784 | ret 0 |
| 2785 | end |
| 2786 | ret ulong get qdf t |
| 2787 | end |
| 2788 | note driver of phrase h: its non-prefix term held by the fewest rows (QX when all are prefixes); |
| 2789 | note a row that matches the phrase holds it |
| 2790 | fn fts_drv h ulong to ulong |
| 2791 | t0 = get qht0 h |
| 2792 | b = QX |
| 2793 | bd = ulong 0 |
| 2794 | for k ulong get qhn h |
| 2795 | x = add t0 k |
| 2796 | if eq get qxr x 0 |
| 2797 | d = fts_xdf x |
| 2798 | if or eq b QX lt d bd |
| 2799 | b = x |
| 2800 | bd = d |
| 2801 | end |
| 2802 | end |
| 2803 | end |
| 2804 | ret b |
| 2805 | end |
| 2806 | note candidates of the query in group qgsel with qok 1, ascending rowid (md 1 group, 2 terms) |
| 2807 | fn fts_cmain md ulong to void |
| 2808 | put qcn 0 0 |
| 2809 | g = get qgsel 0 |
| 2810 | if ge g QD |
| 2811 | ret |
| 2812 | end |
| 2813 | if eq md 1 |
| 2814 | fts_cgrp g |
| 2815 | ret |
| 2816 | end |
| 2817 | for i ulong get qapn 0 |
| 2818 | h = sub get qap i 1 |
| 2819 | if gt get qhn h 0 |
| 2820 | fts_clist fts_xtid fts_drv h g true |
| 2821 | end |
| 2822 | end |
| 2823 | fts_cfin |
| 2824 | end |
| 2825 | note every row, ascending rowid |
| 2826 | fn fts_call to void |
| 2827 | n = get qsn 0 |
| 2828 | r = ulong 0 |
| 2829 | for a ulong 4611686018427387904 |
| 2830 | if ge r n |
| 2831 | break |
| 2832 | end |
| 2833 | put qcd r get qsr r |
| 2834 | r = add r 1 |
| 2835 | end |
| 2836 | put qcn 0 n |
| 2837 | end |
| 2838 | fn fts_cgrp g ulong to void |
| 2839 | p = ulong get qgh g |
| 2840 | for a ulong 4611686018427387904 |
| 2841 | if eq p 0 |
| 2842 | ret |
| 2843 | end |
| 2844 | doc = sub p 1 |
| 2845 | if eq get qok doc 1 |
| 2846 | n = get qcn 0 |
| 2847 | put qcd n uint doc |
| 2848 | put qcn 0 add n 1 |
| 2849 | end |
| 2850 | p = ulong get qgn doc |
| 2851 | end |
| 2852 | end |
| 2853 | note the live rows of term t's list (filt: only group g with qok 1), each once |
| 2854 | fn fts_clist t ulong g ulong filt bool to void |
| 2855 | if eq t QTM |
| 2856 | ret |
| 2857 | end |
| 2858 | k = ulong get qlh t |
| 2859 | for a ulong 4611686018427387904 |
| 2860 | if eq k 0 |
| 2861 | ret |
| 2862 | end |
| 2863 | v = ulong get qld sub k 1 |
| 2864 | doc = and v 262143 |
| 2865 | if fts_lok doc v |
| 2866 | if fts_cok doc g filt |
| 2867 | fts_cadd doc |
| 2868 | end |
| 2869 | end |
| 2870 | k = ulong get qln sub k 1 |
| 2871 | end |
| 2872 | end |
| 2873 | note filt: the row is in group g and its qok is 1 |
| 2874 | fn fts_cok doc ulong g ulong filt bool to bool |
| 2875 | if not filt |
| 2876 | ret true |
| 2877 | end |
| 2878 | if ne ulong get qdg doc g |
| 2879 | ret false |
| 2880 | end |
| 2881 | ret eq get qok doc 1 |
| 2882 | end |
| 2883 | note node value v (doc | generation << 18) names the live row of doc |
| 2884 | fn fts_lok doc ulong v ulong to bool |
| 2885 | if eq get qdst doc 0 |
| 2886 | ret false |
| 2887 | end |
| 2888 | ret eq shr v 18 ulong get qgen doc |
| 2889 | end |
| 2890 | fn fts_cadd doc ulong to void |
| 2891 | if eq get qcm doc 0 |
| 2892 | put qcm doc 1 |
| 2893 | n = get qcn 0 |
| 2894 | put qcd n uint doc |
| 2895 | put qcn 0 add n 1 |
| 2896 | end |
| 2897 | end |
| 2898 | fn fts_cunm to void |
| 2899 | n = get qcn 0 |
| 2900 | k = ulong 0 |
| 2901 | for a ulong 4611686018427387904 |
| 2902 | if ge k n |
| 2903 | ret |
| 2904 | end |
| 2905 | put qcm ulong get qcd k 0 |
| 2906 | k = add k 1 |
| 2907 | end |
| 2908 | end |
| 2909 | note clear the marks and put the candidates in ascending rowid |
| 2910 | fn fts_cfin to void |
| 2911 | fts_cunm |
| 2912 | n = get qcn 0 |
| 2913 | if lt n 2 |
| 2914 | ret |
| 2915 | end |
| 2916 | up = true |
| 2917 | dn = true |
| 2918 | k = ulong 1 |
| 2919 | for a ulong 4611686018427387904 |
| 2920 | if ge k n |
| 2921 | break |
| 2922 | end |
| 2923 | x = fts_crow 0 sub k 1 |
| 2924 | y = fts_crow 0 k |
| 2925 | if gt x y |
| 2926 | up = false |
| 2927 | else |
| 2928 | dn = false |
| 2929 | end |
| 2930 | k = add k 1 |
| 2931 | end |
| 2932 | if up |
| 2933 | ret |
| 2934 | end |
| 2935 | if dn |
| 2936 | fts_crev n |
| 2937 | ret |
| 2938 | end |
| 2939 | fts_csort n |
| 2940 | end |
| 2941 | fn fts_crev n ulong to void |
| 2942 | i = ulong 0 |
| 2943 | j = sub n 1 |
| 2944 | for a ulong 4611686018427387904 |
| 2945 | if ge i j |
| 2946 | ret |
| 2947 | end |
| 2948 | x = get qcd i |
| 2949 | put qcd i get qcd j |
| 2950 | put qcd j x |
| 2951 | i = add i 1 |
| 2952 | j = sub j 1 |
| 2953 | end |
| 2954 | end |
| 2955 | note rowid of candidate i of array s (0 qcd, 1 qcs) |
| 2956 | fn fts_crow s ulong i ulong to ulong |
| 2957 | if eq s 0 |
| 2958 | ret get qdrow ulong get qcd i |
| 2959 | end |
| 2960 | ret get qdrow ulong get qcs i |
| 2961 | end |
| 2962 | fn fts_cget s ulong i ulong to uint |
| 2963 | if eq s 0 |
| 2964 | ret get qcd i |
| 2965 | end |
| 2966 | ret get qcs i |
| 2967 | end |
| 2968 | fn fts_cput s ulong i ulong v uint to void |
| 2969 | if eq s 0 |
| 2970 | put qcd i v |
| 2971 | ret |
| 2972 | end |
| 2973 | put qcs i v |
| 2974 | end |
| 2975 | note bottom-up merge sort of qcd[0..n) by rowid (rowids are distinct) |
| 2976 | fn fts_csort n ulong to void |
| 2977 | s = ulong 0 |
| 2978 | w = ulong 1 |
| 2979 | while lt w n bound 64 |
| 2980 | i = ulong 0 |
| 2981 | for a ulong 4611686018427387904 |
| 2982 | if ge i n |
| 2983 | break |
| 2984 | end |
| 2985 | m = add i w |
| 2986 | if gt m n |
| 2987 | m = n |
| 2988 | end |
| 2989 | e = add m w |
| 2990 | if gt e n |
| 2991 | e = n |
| 2992 | end |
| 2993 | fts_cmerge s i m e |
| 2994 | i = e |
| 2995 | end |
| 2996 | s = xor s 1 |
| 2997 | w = mul w 2 |
| 2998 | end |
| 2999 | if eq s 1 |
| 3000 | for k ulong 4611686018427387904 |
| 3001 | if ge k n |
| 3002 | break |
| 3003 | end |
| 3004 | put qcd k get qcs k |
| 3005 | end |
| 3006 | end |
| 3007 | end |
| 3008 | fn fts_cmerge s ulong i0 ulong m ulong e ulong to void |
| 3009 | d = xor s 1 |
| 3010 | i = i0 |
| 3011 | j = m |
| 3012 | k = i0 |
| 3013 | for a ulong 4611686018427387904 |
| 3014 | if ge k e |
| 3015 | ret |
| 3016 | end |
| 3017 | ti = lt i m |
| 3018 | if and ti lt j e |
| 3019 | ti = lt fts_crow s i fts_crow s j |
| 3020 | end |
| 3021 | if ti |
| 3022 | fts_cput d k fts_cget s i |
| 3023 | i = add i 1 |
| 3024 | else |
| 3025 | fts_cput d k fts_cget s j |
| 3026 | j = add j 1 |
| 3027 | end |
| 3028 | k = add k 1 |
| 3029 | end |
| 3030 | end |
| 3031 | note bm25 statistics before the row loop: qavg and each phrase's idf. A one-term phrase with no |
| 3032 | note prefix, caret or column filter matches exactly the rows that hold the term, so its hit count |
| 3033 | note is qdf. The others are counted by running their clone over the rows that hold their driver |
| 3034 | note term (all rows when a phrase has only prefix terms). true when qcd was used for that. |
| 3035 | fn fts_idfall to bool |
| 3036 | nr = get qsn 0 |
| 3037 | put qavg 0 div f64 long get qtot 0 f64 long nr |
| 3038 | np = get qapn 0 |
| 3039 | nf = ulong 0 |
| 3040 | for i ulong np |
| 3041 | h = sub get qap i 1 |
| 3042 | put qfb i 0 |
| 3043 | if fts_simple h |
| 3044 | put qidf i fts_idf1 nr fts_hdf h |
| 3045 | else |
| 3046 | put qfb i 1 |
| 3047 | nf = add nf 1 |
| 3048 | end |
| 3049 | end |
| 3050 | if eq nf 0 |
| 3051 | put qidok 0 1 |
| 3052 | ret false |
| 3053 | end |
| 3054 | fts_cglob |
| 3055 | fts_postings |
| 3056 | if fts_bad |
| 3057 | ret true |
| 3058 | end |
| 3059 | for i ulong np |
| 3060 | if eq get qfb i 1 |
| 3061 | put qidf i fts_idf1 nr fts_nhit get qcln i |
| 3062 | end |
| 3063 | end |
| 3064 | put qidok 0 1 |
| 3065 | ret true |
| 3066 | end |
| 3067 | fn fts_simple h ulong to bool |
| 3068 | n = get qhn h |
| 3069 | if eq n 0 |
| 3070 | ret true |
| 3071 | end |
| 3072 | if ne n 1 |
| 3073 | ret false |
| 3074 | end |
| 3075 | t0 = get qht0 h |
| 3076 | if or ne get qxr t0 0 ne get qxf t0 0 |
| 3077 | ret false |
| 3078 | end |
| 3079 | y = get qoy sub get qhnd h 1 |
| 3080 | ret eq get qycs sub y 1 0 |
| 3081 | end |
| 3082 | fn fts_hdf h ulong to ulong |
| 3083 | if eq get qhn h 0 |
| 3084 | ret 0 |
| 3085 | end |
| 3086 | ret fts_xdf get qht0 h |
| 3087 | end |
| 3088 | note rows for the counted phrases (qfb), every group and no qok filter, ascending rowid |
| 3089 | fn fts_cglob to void |
| 3090 | put qcn 0 0 |
| 3091 | all = false |
| 3092 | for i ulong get qapn 0 |
| 3093 | if eq get qfb i 1 |
| 3094 | h = sub get qap i 1 |
| 3095 | if gt get qhn h 0 |
| 3096 | x = fts_drv h |
| 3097 | if eq x QX |
| 3098 | all = true |
| 3099 | else |
| 3100 | fts_clist fts_xtid x 0 false |
| 3101 | end |
| 3102 | end |
| 3103 | end |
| 3104 | end |
| 3105 | if all |
| 3106 | fts_cunm |
| 3107 | fts_call |
| 3108 | ret |
| 3109 | end |
| 3110 | fts_cfin |
| 3111 | end |
| 3112 | note ---------------- index upkeep ---------------- |
| 3113 | note first token word of row doc's region and its token count |
| 3114 | fn fts_rtok doc ulong to ulong |
| 3115 | ret add ulong get qdst doc 2 |
| 3116 | end |
| 3117 | fn fts_rcnt doc ulong to ulong |
| 3118 | s = sub ulong get qdst doc 1 |
| 3119 | ret add ulong get qtk add s 1 ulong get qtk add s 2 |
| 3120 | end |
| 3121 | note walk the distinct terms of row doc; md bits: 1 add a list node, 2 count it in qdf, 4 uncount; |
| 3122 | note returns the number of distinct terms |
| 3123 | fn fts_lwalk doc ulong md ulong to ulong |
| 3124 | s = fts_rtok doc |
| 3125 | n = fts_rcnt doc |
| 3126 | v = uint or doc shl ulong get qgen doc 18 |
| 3127 | c = ulong 0 |
| 3128 | i = ulong 0 |
| 3129 | for a ulong 4611686018427387904 |
| 3130 | if ge i n |
| 3131 | break |
| 3132 | end |
| 3133 | t = ulong get qtk add s i |
| 3134 | if eq get qtmk t 0 |
| 3135 | fts_mark1 t 0 |
| 3136 | c = add c 1 |
| 3137 | fts_lstep t v md |
| 3138 | end |
| 3139 | i = add i 1 |
| 3140 | end |
| 3141 | fts_unmark |
| 3142 | ret c |
| 3143 | end |
| 3144 | fn fts_lstep t ulong v uint md ulong to void |
| 3145 | if ne and md 4 0 |
| 3146 | put qdf t uint sub ulong get qdf t 1 |
| 3147 | end |
| 3148 | if ne and md 2 0 |
| 3149 | put qdf t uint add ulong get qdf t 1 |
| 3150 | end |
| 3151 | if ne and md 1 0 |
| 3152 | k = get qlv 0 |
| 3153 | put qld k v |
| 3154 | put qln k get qlh t |
| 3155 | put qlh t uint add k 1 |
| 3156 | put qlv 0 add k 1 |
| 3157 | end |
| 3158 | end |
| 3159 | note a row was put: count its terms, add its nodes (or rebuild when the pool is full, half |
| 3160 | note garbage, or a rebuild is pending) |
| 3161 | fn fts_ladd doc ulong to void |
| 3162 | n = fts_rcnt doc |
| 3163 | if gt n get qlv 3 |
| 3164 | put qlv 3 n |
| 3165 | end |
| 3166 | u = get qlv 0 |
| 3167 | g = get qlv 1 |
| 3168 | full = gt add u n QTKN |
| 3169 | junk = and gt g 1048576 gt mul g 2 u |
| 3170 | if or or full junk ne get qlv 2 0 |
| 3171 | z = fts_lwalk doc 2 |
| 3172 | fts_lbuild |
| 3173 | ret |
| 3174 | end |
| 3175 | z2 = fts_lwalk doc 3 |
| 3176 | end |
| 3177 | note a row is deleted: uncount its terms; its nodes turn to garbage by the generation bump |
| 3178 | fn fts_ldel doc ulong to void |
| 3179 | c = fts_lwalk doc 4 |
| 3180 | put qlv 1 add get qlv 1 c |
| 3181 | g = add ulong get qgen doc 1 |
| 3182 | if ge g 16384 |
| 3183 | g = ulong 0 |
| 3184 | put qlv 2 1 |
| 3185 | end |
| 3186 | put qgen doc uint g |
| 3187 | end |
| 3188 | fn fts_lclear to void |
| 3189 | nt = get qtmc 0 |
| 3190 | k = ulong 0 |
| 3191 | for a ulong 4611686018427387904 |
| 3192 | if ge k nt |
| 3193 | break |
| 3194 | end |
| 3195 | put qlh k 0 |
| 3196 | k = add k 1 |
| 3197 | end |
| 3198 | put qlv 0 0 |
| 3199 | put qlv 1 0 |
| 3200 | put qlv 2 0 |
| 3201 | put qlv 3 0 |
| 3202 | end |
| 3203 | note rebuild every term list from the rows (qdf is not touched: it is kept by put and delete) |
| 3204 | fn fts_lbuild to void |
| 3205 | fts_lclear |
| 3206 | m = ulong 0 |
| 3207 | n = get qsn 0 |
| 3208 | r = ulong 0 |
| 3209 | for a ulong 4611686018427387904 |
| 3210 | if ge r n |
| 3211 | break |
| 3212 | end |
| 3213 | doc = ulong get qsr r |
| 3214 | z = fts_lwalk doc 1 |
| 3215 | x = fts_rcnt doc |
| 3216 | if gt x m |
| 3217 | m = x |
| 3218 | end |
| 3219 | r = add r 1 |
| 3220 | end |
| 3221 | put qlv 3 m |
| 3222 | end |
| 3223 | note link row doc into group qgr after the last row with a smaller rowid |
| 3224 | fn fts_gadd doc ulong row ulong to void |
| 3225 | g = get qgr 0 |
| 3226 | put qdg doc uint g |
| 3227 | p = ulong get qgt g |
| 3228 | for a ulong 4611686018427387904 |
| 3229 | if eq p 0 |
| 3230 | break |
| 3231 | end |
| 3232 | if lt get qdrow sub p 1 row |
| 3233 | break |
| 3234 | end |
| 3235 | p = ulong get qgp sub p 1 |
| 3236 | end |
| 3237 | x = ulong 0 |
| 3238 | if eq p 0 |
| 3239 | x = ulong get qgh g |
| 3240 | put qgh g uint add doc 1 |
| 3241 | else |
| 3242 | x = ulong get qgn sub p 1 |
| 3243 | put qgn sub p 1 uint add doc 1 |
| 3244 | end |
| 3245 | put qgp doc uint p |
| 3246 | put qgn doc uint x |
| 3247 | if eq x 0 |
| 3248 | put qgt g uint add doc 1 |
| 3249 | else |
| 3250 | put qgp sub x 1 uint add doc 1 |
| 3251 | end |
| 3252 | put qgc g uint add ulong get qgc g 1 |
| 3253 | end |
| 3254 | fn fts_gdel doc ulong to void |
| 3255 | g = ulong get qdg doc |
| 3256 | p = ulong get qgp doc |
| 3257 | x = ulong get qgn doc |
| 3258 | if eq p 0 |
| 3259 | put qgh g uint x |
| 3260 | else |
| 3261 | put qgn sub p 1 uint x |
| 3262 | end |
| 3263 | if eq x 0 |
| 3264 | put qgt g uint p |
| 3265 | else |
| 3266 | put qgp sub x 1 uint p |
| 3267 | end |
| 3268 | put qgc g uint sub ulong get qgc g 1 |
| 3269 | put qgp doc 0 |
| 3270 | put qgn doc 0 |
| 3271 | end |
| 3272 | fn fts_gfirst g ulong to ulong |
| 3273 | if ge g QD |
| 3274 | ret 18446744073709551615 |
| 3275 | end |
| 3276 | p = ulong get qgh g |
| 3277 | if eq p 0 |
| 3278 | ret 18446744073709551615 |
| 3279 | end |
| 3280 | ret sub p 1 |
| 3281 | end |
| 3282 | fn fts_gnext doc ulong to ulong |
| 3283 | x = ulong get qgn doc |
| 3284 | if eq x 0 |
| 3285 | ret 18446744073709551615 |
| 3286 | end |
| 3287 | ret sub x 1 |
| 3288 | end |
| 3289 | note ---------------- consistency check ---------------- |
| 3290 | fn fts_icheck to ulong |
| 3291 | fts_init |
| 3292 | if ne get qlv 2 0 |
| 3293 | fts_lbuild |
| 3294 | end |
| 3295 | put qkbad 0 0 |
| 3296 | tot = ulong 0 |
| 3297 | nt = get qtmc 0 |
| 3298 | t = ulong 0 |
| 3299 | for a ulong 4611686018427387904 |
| 3300 | if ge t nt |
| 3301 | break |
| 3302 | end |
| 3303 | c = fts_kcheck t |
| 3304 | if ne c ulong get qdf t |
| 3305 | put qkbad 0 add get qkbad 0 1 |
| 3306 | end |
| 3307 | tot = add tot c |
| 3308 | t = add t 1 |
| 3309 | end |
| 3310 | pairs = ulong 0 |
| 3311 | n = get qsn 0 |
| 3312 | r = ulong 0 |
| 3313 | for a2 ulong 4611686018427387904 |
| 3314 | if ge r n |
| 3315 | break |
| 3316 | end |
| 3317 | doc = ulong get qsr r |
| 3318 | pairs = add pairs fts_lwalk doc 0 |
| 3319 | if gt fts_rcnt doc get qlv 3 |
| 3320 | put qkbad 0 add get qkbad 0 1 |
| 3321 | end |
| 3322 | r = add r 1 |
| 3323 | end |
| 3324 | if ne tot pairs |
| 3325 | put qkbad 0 add get qkbad 0 1 |
| 3326 | end |
| 3327 | ret add get qkbad 0 fts_gcheck |
| 3328 | end |
| 3329 | note live nodes of term t's list; each must name a different row that holds t |
| 3330 | fn fts_kcheck t ulong to ulong |
| 3331 | put qcn 0 0 |
| 3332 | c = ulong 0 |
| 3333 | k = ulong get qlh t |
| 3334 | for a ulong 4611686018427387904 |
| 3335 | if eq k 0 |
| 3336 | break |
| 3337 | end |
| 3338 | v = ulong get qld sub k 1 |
| 3339 | doc = and v 262143 |
| 3340 | if fts_lok doc v |
| 3341 | if eq get qcm doc 1 |
| 3342 | put qkbad 0 add get qkbad 0 1 |
| 3343 | else |
| 3344 | fts_cadd doc |
| 3345 | c = add c 1 |
| 3346 | if not fts_rhas doc t |
| 3347 | put qkbad 0 add get qkbad 0 1 |
| 3348 | end |
| 3349 | end |
| 3350 | end |
| 3351 | k = ulong get qln sub k 1 |
| 3352 | end |
| 3353 | fts_cunm |
| 3354 | ret c |
| 3355 | end |
| 3356 | fn fts_rhas doc ulong t ulong to bool |
| 3357 | s = fts_rtok doc |
| 3358 | n = fts_rcnt doc |
| 3359 | i = ulong 0 |
| 3360 | for a ulong 4611686018427387904 |
| 3361 | if ge i n |
| 3362 | ret false |
| 3363 | end |
| 3364 | if eq ulong get qtk add s i t |
| 3365 | ret true |
| 3366 | end |
| 3367 | i = add i 1 |
| 3368 | end |
| 3369 | ret false |
| 3370 | end |
| 3371 | note every group list: linked both ways, ascending rowid, live rows of that group, counted |
| 3372 | fn fts_gcheck to ulong |
| 3373 | bad = ulong 0 |
| 3374 | tot = ulong 0 |
| 3375 | for g ulong 262144 |
| 3376 | if or ne get qgh g 0 ne get qgc g 0 |
| 3377 | bad = add bad fts_g1 g |
| 3378 | tot = add tot ulong get qgc g |
| 3379 | end |
| 3380 | if ne get qgt g 0 |
| 3381 | if eq get qgh g 0 |
| 3382 | bad = add bad 1 |
| 3383 | end |
| 3384 | end |
| 3385 | end |
| 3386 | if ne tot get qsn 0 |
| 3387 | bad = add bad 1 |
| 3388 | end |
| 3389 | ret bad |
| 3390 | end |
| 3391 | fn fts_g1 g ulong to ulong |
| 3392 | bad = ulong 0 |
| 3393 | p = ulong 0 |
| 3394 | x = ulong get qgh g |
| 3395 | c = ulong 0 |
| 3396 | for a ulong 4611686018427387904 |
| 3397 | if eq x 0 |
| 3398 | break |
| 3399 | end |
| 3400 | if gt c get qsn 0 |
| 3401 | ret add bad 1 |
| 3402 | end |
| 3403 | doc = sub x 1 |
| 3404 | bad = add bad fts_g1row doc g p |
| 3405 | c = add c 1 |
| 3406 | p = x |
| 3407 | x = ulong get qgn doc |
| 3408 | end |
| 3409 | if ne ulong get qgt g p |
| 3410 | bad = add bad 1 |
| 3411 | end |
| 3412 | if ne c ulong get qgc g |
| 3413 | bad = add bad 1 |
| 3414 | end |
| 3415 | ret bad |
| 3416 | end |
| 3417 | note row doc of group g's list, after p (previous doc + 1, 0 at the head) |
| 3418 | fn fts_g1row doc ulong g ulong p ulong to ulong |
| 3419 | bad = ulong 0 |
| 3420 | if or eq get qdst doc 0 ne ulong get qdg doc g |
| 3421 | bad = add bad 1 |
| 3422 | end |
| 3423 | if ne ulong get qgp doc p |
| 3424 | bad = add bad 1 |
| 3425 | end |
| 3426 | if gt p 0 |
| 3427 | if le get qdrow doc get qdrow sub p 1 |
| 3428 | bad = add bad 1 |
| 3429 | end |
| 3430 | end |
| 3431 | ret bad |
| 3432 | end |
| 3433 | note ---------------- the term-count loop ---------------- |
| 3434 | note A query that is one-term phrases joined by OR (what Brain read-before sends), each with no |
| 3435 | note prefix, caret or column filter, matches a row exactly when the row holds one of the terms, |
| 3436 | note and phrase i's instance count there is the number of times its term occurs in the row (a |
| 3437 | note TERM node with every column selected). fts_ortop gets those counts with one pass over each |
| 3438 | note candidate row's tokens and scores and keeps the rows as fts_row does, in the same order with |
| 3439 | note the same bm25 operations, instead of building posting entries and running the node engine. |
| 3440 | fn fts_orok to bool |
| 3441 | if or eq get qrte 0 4 eq get qrst 0 3 |
| 3442 | ret false |
| 3443 | end |
| 3444 | if eq get qexpr 0 0 |
| 3445 | ret false |
| 3446 | end |
| 3447 | ret fts_oknode get qroot 0 |
| 3448 | end |
| 3449 | note node o is OR over such nodes, or a TERM node of a one-term phrase without filters |
| 3450 | fn fts_oknode o ulong to bool |
| 3451 | ty = get qot o |
| 3452 | if eq ty 1 |
| 3453 | for i ulong get qoc o |
| 3454 | if not fts_oknode sub fts_child o i 1 |
| 3455 | ret false |
| 3456 | end |
| 3457 | end |
| 3458 | ret true |
| 3459 | end |
| 3460 | if ne ty 4 |
| 3461 | ret false |
| 3462 | end |
| 3463 | y = sub get qoy o 1 |
| 3464 | if ne get qyk y 1 |
| 3465 | ret false |
| 3466 | end |
| 3467 | if ne get qycs y 0 |
| 3468 | ret false |
| 3469 | end |
| 3470 | h = fts_ph y 0 |
| 3471 | if ne get qhn h 1 |
| 3472 | ret false |
| 3473 | end |
| 3474 | t0 = get qht0 h |
| 3475 | ret and eq get qxr t0 0 eq get qxf t0 0 |
| 3476 | end |
| 3477 | fn fts_ortop lim ulong to void |
| 3478 | np = get qapn 0 |
| 3479 | nk = ulong 0 |
| 3480 | for i ulong np |
| 3481 | t = fts_xtid get qht0 sub get qap i 1 |
| 3482 | k = QX |
| 3483 | if ne t QTM |
| 3484 | m = get qtmk t |
| 3485 | if eq m 0 |
| 3486 | nk = add nk 1 |
| 3487 | m = nk |
| 3488 | put qtmk t m |
| 3489 | put qotc sub m 1 0 |
| 3490 | j = get qmkn 0 |
| 3491 | put qmkl j uint t |
| 3492 | put qmkn 0 add j 1 |
| 3493 | end |
| 3494 | k = sub m 1 |
| 3495 | end |
| 3496 | put qotk i k |
| 3497 | end |
| 3498 | n = get qcn 0 |
| 3499 | c = ulong 0 |
| 3500 | for a ulong 4611686018427387904 |
| 3501 | if ge c n |
| 3502 | break |
| 3503 | end |
| 3504 | fts_orrow ulong get qcd c lim |
| 3505 | c = add c 1 |
| 3506 | end |
| 3507 | fts_unmark |
| 3508 | end |
| 3509 | note count the query terms in row doc; when one occurs, score the row and keep it |
| 3510 | fn fts_orrow doc ulong lim ulong to void |
| 3511 | s = fts_rtok doc |
| 3512 | n = fts_rcnt doc |
| 3513 | u = ulong 0 |
| 3514 | i = ulong 0 |
| 3515 | for a ulong 4611686018427387904 |
| 3516 | if ge i n |
| 3517 | break |
| 3518 | end |
| 3519 | m = get qtmk ulong get qtk add s i |
| 3520 | if ne m 0 |
| 3521 | k = sub m 1 |
| 3522 | v = get qotc k |
| 3523 | if eq v 0 |
| 3524 | put qotu u k |
| 3525 | u = add u 1 |
| 3526 | end |
| 3527 | put qotc k add v 1 |
| 3528 | end |
| 3529 | i = add i 1 |
| 3530 | end |
| 3531 | if eq u 0 |
| 3532 | ret |
| 3533 | end |
| 3534 | fts_orscore doc n lim |
| 3535 | for j ulong u |
| 3536 | put qotc get qotu j 0 |
| 3537 | end |
| 3538 | end |
| 3539 | fn fts_orscore doc ulong dt ulong lim ulong to void |
| 3540 | for i ulong get qapn 0 |
| 3541 | k = get qotk i |
| 3542 | f = 0.0f64 |
| 3543 | if ne k QX |
| 3544 | f = f64 get qotc k |
| 3545 | end |
| 3546 | put qfrq i f |
| 3547 | end |
| 3548 | b = fts_bm25 dt |
| 3549 | if ne b b |
| 3550 | b = 5.0 |
| 3551 | end |
| 3552 | if eq get qok doc 1 |
| 3553 | if eq ulong get qdg doc get qgsel 0 |
| 3554 | fts_sort b doc lim |
| 3555 | end |
| 3556 | end |
| 3557 | end |