| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2006 Oct 10 | 2 ** 2006 Oct 10 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 ** A docid is the unique integer identifier for a single document. | 63 ** A docid is the unique integer identifier for a single document. |
| 64 ** A position is the index of a word within the document. The first | 64 ** A position is the index of a word within the document. The first |
| 65 ** word of the document has a position of 0. | 65 ** word of the document has a position of 0. |
| 66 ** | 66 ** |
| 67 ** FTS3 used to optionally store character offsets using a compile-time | 67 ** FTS3 used to optionally store character offsets using a compile-time |
| 68 ** option. But that functionality is no longer supported. | 68 ** option. But that functionality is no longer supported. |
| 69 ** | 69 ** |
| 70 ** A doclist is stored like this: | 70 ** A doclist is stored like this: |
| 71 ** | 71 ** |
| 72 ** array { | 72 ** array { |
| 73 ** varint docid; | 73 ** varint docid; (delta from previous doclist) |
| 74 ** array { (position list for column 0) | 74 ** array { (position list for column 0) |
| 75 ** varint position; (2 more than the delta from previous position) | 75 ** varint position; (2 more than the delta from previous position) |
| 76 ** } | 76 ** } |
| 77 ** array { | 77 ** array { |
| 78 ** varint POS_COLUMN; (marks start of position list for new column) | 78 ** varint POS_COLUMN; (marks start of position list for new column) |
| 79 ** varint column; (index of new column) | 79 ** varint column; (index of new column) |
| 80 ** array { | 80 ** array { |
| 81 ** varint position; (2 more than the delta from previous position) | 81 ** varint position; (2 more than the delta from previous position) |
| 82 ** } | 82 ** } |
| 83 ** } | 83 ** } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 94 ** 2 for the first position. Example: | 94 ** 2 for the first position. Example: |
| 95 ** | 95 ** |
| 96 ** label: A B C D E F G H I J K | 96 ** label: A B C D E F G H I J K |
| 97 ** value: 123 5 9 1 1 14 35 0 234 72 0 | 97 ** value: 123 5 9 1 1 14 35 0 234 72 0 |
| 98 ** | 98 ** |
| 99 ** The 123 value is the first docid. For column zero in this document | 99 ** The 123 value is the first docid. For column zero in this document |
| 100 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1 | 100 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1 |
| 101 ** at D signals the start of a new column; the 1 at E indicates that the | 101 ** at D signals the start of a new column; the 1 at E indicates that the |
| 102 ** new column is column number 1. There are two positions at 12 and 45 | 102 ** new column is column number 1. There are two positions at 12 and 45 |
| 103 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The | 103 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The |
| 104 ** 234 at I is the next docid. It has one position 72 (72-2) and then | 104 ** 234 at I is the delta to next docid (357). It has one position 70 |
| 105 ** terminates with the 0 at K. | 105 ** (72-2) and then terminates with the 0 at K. |
| 106 ** | 106 ** |
| 107 ** A "position-list" is the list of positions for multiple columns for | 107 ** A "position-list" is the list of positions for multiple columns for |
| 108 ** a single docid. A "column-list" is the set of positions for a single | 108 ** a single docid. A "column-list" is the set of positions for a single |
| 109 ** column. Hence, a position-list consists of one or more column-lists, | 109 ** column. Hence, a position-list consists of one or more column-lists, |
| 110 ** a document record consists of a docid followed by a position-list and | 110 ** a document record consists of a docid followed by a position-list and |
| 111 ** a doclist consists of one or more document records. | 111 ** a doclist consists of one or more document records. |
| 112 ** | 112 ** |
| 113 ** A bare doclist omits the position information, becoming an | 113 ** A bare doclist omits the position information, becoming an |
| 114 ** array of varint-encoded docids. | 114 ** array of varint-encoded docids. |
| 115 ** | 115 ** |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 **** Handling of deletions and updates **** | 279 **** Handling of deletions and updates **** |
| 280 ** Since we're using a segmented structure, with no docid-oriented | 280 ** Since we're using a segmented structure, with no docid-oriented |
| 281 ** index into the term index, we clearly cannot simply update the term | 281 ** index into the term index, we clearly cannot simply update the term |
| 282 ** index when a document is deleted or updated. For deletions, we | 282 ** index when a document is deleted or updated. For deletions, we |
| 283 ** write an empty doclist (varint(docid) varint(POS_END)), for updates | 283 ** write an empty doclist (varint(docid) varint(POS_END)), for updates |
| 284 ** we simply write the new doclist. Segment merges overwrite older | 284 ** we simply write the new doclist. Segment merges overwrite older |
| 285 ** data for a particular docid with newer data, so deletes or updates | 285 ** data for a particular docid with newer data, so deletes or updates |
| 286 ** will eventually overtake the earlier data and knock it out. The | 286 ** will eventually overtake the earlier data and knock it out. The |
| 287 ** query logic likewise merges doclists so that newer data knocks out | 287 ** query logic likewise merges doclists so that newer data knocks out |
| 288 ** older data. | 288 ** older data. |
| 289 ** | |
| 290 ** TODO(shess) Provide a VACUUM type operation to clear out all | |
| 291 ** deletions and duplications. This would basically be a forced merge | |
| 292 ** into a single segment. | |
| 293 */ | 289 */ |
| 294 #define CHROMIUM_FTS3_CHANGES 1 | 290 #define CHROMIUM_FTS3_CHANGES 1 |
| 295 | 291 |
| 292 #include "fts3Int.h" |
| 296 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) | 293 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 297 | 294 |
| 298 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) | 295 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) |
| 299 # define SQLITE_CORE 1 | 296 # define SQLITE_CORE 1 |
| 300 #endif | 297 #endif |
| 301 | 298 |
| 302 #include "fts3Int.h" | |
| 303 | |
| 304 #include <assert.h> | 299 #include <assert.h> |
| 305 #include <stdlib.h> | 300 #include <stdlib.h> |
| 306 #include <stddef.h> | 301 #include <stddef.h> |
| 307 #include <stdio.h> | 302 #include <stdio.h> |
| 308 #include <string.h> | 303 #include <string.h> |
| 309 #include <stdarg.h> | 304 #include <stdarg.h> |
| 310 | 305 |
| 311 #include "fts3.h" | 306 #include "fts3.h" |
| 312 #ifndef SQLITE_CORE | 307 #ifndef SQLITE_CORE |
| 313 # include "sqlite3ext.h" | 308 # include "sqlite3ext.h" |
| 314 SQLITE_EXTENSION_INIT1 | 309 SQLITE_EXTENSION_INIT1 |
| 315 #endif | 310 #endif |
| 316 | 311 |
| 312 static int fts3EvalNext(Fts3Cursor *pCsr); |
| 313 static int fts3EvalStart(Fts3Cursor *pCsr); |
| 314 static int fts3TermSegReaderCursor( |
| 315 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **); |
| 316 |
| 317 /* | 317 /* |
| 318 ** Write a 64-bit variable-length integer to memory starting at p[0]. | 318 ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 319 ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes. | 319 ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes. |
| 320 ** The number of bytes written is returned. | 320 ** The number of bytes written is returned. |
| 321 */ | 321 */ |
| 322 int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){ | 322 int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){ |
| 323 unsigned char *q = (unsigned char *) p; | 323 unsigned char *q = (unsigned char *) p; |
| 324 sqlite_uint64 vu = v; | 324 sqlite_uint64 vu = v; |
| 325 do{ | 325 do{ |
| 326 *q++ = (unsigned char) ((vu & 0x7f) | 0x80); | 326 *q++ = (unsigned char) ((vu & 0x7f) | 0x80); |
| 327 vu >>= 7; | 327 vu >>= 7; |
| 328 }while( vu!=0 ); | 328 }while( vu!=0 ); |
| 329 q[-1] &= 0x7f; /* turn off high bit in final byte */ | 329 q[-1] &= 0x7f; /* turn off high bit in final byte */ |
| 330 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX ); | 330 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX ); |
| 331 return (int) (q - (unsigned char *)p); | 331 return (int) (q - (unsigned char *)p); |
| 332 } | 332 } |
| 333 | 333 |
| 334 #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \ |
| 335 v = (v & mask1) | ( (*ptr++) << shift ); \ |
| 336 if( (v & mask2)==0 ){ var = v; return ret; } |
| 337 #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \ |
| 338 v = (*ptr++); \ |
| 339 if( (v & mask2)==0 ){ var = v; return ret; } |
| 340 |
| 334 /* | 341 /* |
| 335 ** Read a 64-bit variable-length integer from memory starting at p[0]. | 342 ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 336 ** Return the number of bytes read, or 0 on error. | 343 ** Return the number of bytes read, or 0 on error. |
| 337 ** The value is stored in *v. | 344 ** The value is stored in *v. |
| 338 */ | 345 */ |
| 339 int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){ | 346 int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){ |
| 340 const unsigned char *q = (const unsigned char *) p; | 347 const char *pStart = p; |
| 341 sqlite_uint64 x = 0, y = 1; | 348 u32 a; |
| 342 while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){ | 349 u64 b; |
| 343 x += y * (*q++ & 0x7f); | 350 int shift; |
| 344 y <<= 7; | 351 |
| 352 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1); |
| 353 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2); |
| 354 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3); |
| 355 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4); |
| 356 b = (a & 0x0FFFFFFF ); |
| 357 |
| 358 for(shift=28; shift<=63; shift+=7){ |
| 359 u64 c = *p++; |
| 360 b += (c&0x7F) << shift; |
| 361 if( (c & 0x80)==0 ) break; |
| 345 } | 362 } |
| 346 x += y * (*q++); | 363 *v = b; |
| 347 *v = (sqlite_int64) x; | 364 return (int)(p - pStart); |
| 348 return (int) (q - (unsigned char *)p); | |
| 349 } | 365 } |
| 350 | 366 |
| 351 /* | 367 /* |
| 352 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a | 368 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a |
| 353 ** 32-bit integer before it is returned. | 369 ** 32-bit integer before it is returned. |
| 354 */ | 370 */ |
| 355 int sqlite3Fts3GetVarint32(const char *p, int *pi){ | 371 int sqlite3Fts3GetVarint32(const char *p, int *pi){ |
| 356 sqlite_int64 i; | 372 u32 a; |
| 357 int ret = sqlite3Fts3GetVarint(p, &i); | 373 |
| 358 *pi = (int) i; | 374 #ifndef fts3GetVarint32 |
| 359 return ret; | 375 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1); |
| 376 #else |
| 377 a = (*p++); |
| 378 assert( a & 0x80 ); |
| 379 #endif |
| 380 |
| 381 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *pi, 2); |
| 382 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *pi, 3); |
| 383 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4); |
| 384 a = (a & 0x0FFFFFFF ); |
| 385 *pi = (int)(a | ((u32)(*p & 0x0F) << 28)); |
| 386 return 5; |
| 360 } | 387 } |
| 361 | 388 |
| 362 /* | 389 /* |
| 363 ** Return the number of bytes required to encode v as a varint | 390 ** Return the number of bytes required to encode v as a varint |
| 364 */ | 391 */ |
| 365 int sqlite3Fts3VarintLen(sqlite3_uint64 v){ | 392 int sqlite3Fts3VarintLen(sqlite3_uint64 v){ |
| 366 int i = 0; | 393 int i = 0; |
| 367 do{ | 394 do{ |
| 368 i++; | 395 i++; |
| 369 v >>= 7; | 396 v >>= 7; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 ** to the first byte past the end of the varint. Add the value of the varint | 441 ** to the first byte past the end of the varint. Add the value of the varint |
| 415 ** to *pVal. | 442 ** to *pVal. |
| 416 */ | 443 */ |
| 417 static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){ | 444 static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){ |
| 418 sqlite3_int64 iVal; | 445 sqlite3_int64 iVal; |
| 419 *pp += sqlite3Fts3GetVarint(*pp, &iVal); | 446 *pp += sqlite3Fts3GetVarint(*pp, &iVal); |
| 420 *pVal += iVal; | 447 *pVal += iVal; |
| 421 } | 448 } |
| 422 | 449 |
| 423 /* | 450 /* |
| 424 ** As long as *pp has not reached its end (pEnd), then do the same | 451 ** When this function is called, *pp points to the first byte following a |
| 425 ** as fts3GetDeltaVarint(): read a single varint and add it to *pVal. | 452 ** varint that is part of a doclist (or position-list, or any other list |
| 426 ** But if we have reached the end of the varint, just set *pp=0 and | 453 ** of varints). This function moves *pp to point to the start of that varint, |
| 427 ** leave *pVal unchanged. | 454 ** and sets *pVal by the varint value. |
| 455 ** |
| 456 ** Argument pStart points to the first byte of the doclist that the |
| 457 ** varint is part of. |
| 428 */ | 458 */ |
| 429 static void fts3GetDeltaVarint2(char **pp, char *pEnd, sqlite3_int64 *pVal){ | 459 static void fts3GetReverseVarint( |
| 430 if( *pp>=pEnd ){ | 460 char **pp, |
| 431 *pp = 0; | 461 char *pStart, |
| 432 }else{ | 462 sqlite3_int64 *pVal |
| 433 fts3GetDeltaVarint(pp, pVal); | 463 ){ |
| 434 } | 464 sqlite3_int64 iVal; |
| 465 char *p; |
| 466 |
| 467 /* Pointer p now points at the first byte past the varint we are |
| 468 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is |
| 469 ** clear on character p[-1]. */ |
| 470 for(p = (*pp)-2; p>=pStart && *p&0x80; p--); |
| 471 p++; |
| 472 *pp = p; |
| 473 |
| 474 sqlite3Fts3GetVarint(p, &iVal); |
| 475 *pVal = iVal; |
| 435 } | 476 } |
| 436 | 477 |
| 437 /* | 478 /* |
| 438 ** The xDisconnect() virtual table method. | 479 ** The xDisconnect() virtual table method. |
| 439 */ | 480 */ |
| 440 static int fts3DisconnectMethod(sqlite3_vtab *pVtab){ | 481 static int fts3DisconnectMethod(sqlite3_vtab *pVtab){ |
| 441 Fts3Table *p = (Fts3Table *)pVtab; | 482 Fts3Table *p = (Fts3Table *)pVtab; |
| 442 int i; | 483 int i; |
| 443 | 484 |
| 444 assert( p->nPendingData==0 ); | 485 assert( p->nPendingData==0 ); |
| 445 assert( p->pSegments==0 ); | 486 assert( p->pSegments==0 ); |
| 446 | 487 |
| 447 /* Free any prepared statements held */ | 488 /* Free any prepared statements held */ |
| 448 for(i=0; i<SizeofArray(p->aStmt); i++){ | 489 for(i=0; i<SizeofArray(p->aStmt); i++){ |
| 449 sqlite3_finalize(p->aStmt[i]); | 490 sqlite3_finalize(p->aStmt[i]); |
| 450 } | 491 } |
| 451 sqlite3_free(p->zSegmentsTbl); | 492 sqlite3_free(p->zSegmentsTbl); |
| 452 sqlite3_free(p->zReadExprlist); | 493 sqlite3_free(p->zReadExprlist); |
| 453 sqlite3_free(p->zWriteExprlist); | 494 sqlite3_free(p->zWriteExprlist); |
| 495 sqlite3_free(p->zContentTbl); |
| 496 sqlite3_free(p->zLanguageid); |
| 454 | 497 |
| 455 /* Invoke the tokenizer destructor to free the tokenizer. */ | 498 /* Invoke the tokenizer destructor to free the tokenizer. */ |
| 456 p->pTokenizer->pModule->xDestroy(p->pTokenizer); | 499 p->pTokenizer->pModule->xDestroy(p->pTokenizer); |
| 457 | 500 |
| 458 sqlite3_free(p); | 501 sqlite3_free(p); |
| 459 return SQLITE_OK; | 502 return SQLITE_OK; |
| 460 } | 503 } |
| 461 | 504 |
| 462 /* | 505 /* |
| 463 ** Construct one or more SQL statements from the format string given | 506 ** Construct one or more SQL statements from the format string given |
| (...skipping 19 matching lines...) Expand all Loading... |
| 483 }else{ | 526 }else{ |
| 484 *pRc = sqlite3_exec(db, zSql, 0, 0, 0); | 527 *pRc = sqlite3_exec(db, zSql, 0, 0, 0); |
| 485 sqlite3_free(zSql); | 528 sqlite3_free(zSql); |
| 486 } | 529 } |
| 487 } | 530 } |
| 488 | 531 |
| 489 /* | 532 /* |
| 490 ** The xDestroy() virtual table method. | 533 ** The xDestroy() virtual table method. |
| 491 */ | 534 */ |
| 492 static int fts3DestroyMethod(sqlite3_vtab *pVtab){ | 535 static int fts3DestroyMethod(sqlite3_vtab *pVtab){ |
| 536 Fts3Table *p = (Fts3Table *)pVtab; |
| 493 int rc = SQLITE_OK; /* Return code */ | 537 int rc = SQLITE_OK; /* Return code */ |
| 494 Fts3Table *p = (Fts3Table *)pVtab; | 538 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */ |
| 495 sqlite3 *db = p->db; | 539 sqlite3 *db = p->db; /* Database handle */ |
| 496 | 540 |
| 497 /* Drop the shadow tables */ | 541 /* Drop the shadow tables */ |
| 498 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName); | 542 if( p->zContentTbl==0 ){ |
| 499 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName); | 543 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName); |
| 500 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName); | 544 } |
| 501 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName); | 545 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName); |
| 502 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName); | 546 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName); |
| 547 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName); |
| 548 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName); |
| 503 | 549 |
| 504 /* If everything has worked, invoke fts3DisconnectMethod() to free the | 550 /* If everything has worked, invoke fts3DisconnectMethod() to free the |
| 505 ** memory associated with the Fts3Table structure and return SQLITE_OK. | 551 ** memory associated with the Fts3Table structure and return SQLITE_OK. |
| 506 ** Otherwise, return an SQLite error code. | 552 ** Otherwise, return an SQLite error code. |
| 507 */ | 553 */ |
| 508 return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc); | 554 return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc); |
| 509 } | 555 } |
| 510 | 556 |
| 511 | 557 |
| 512 /* | 558 /* |
| 513 ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table | 559 ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table |
| 514 ** passed as the first argument. This is done as part of the xConnect() | 560 ** passed as the first argument. This is done as part of the xConnect() |
| 515 ** and xCreate() methods. | 561 ** and xCreate() methods. |
| 516 ** | 562 ** |
| 517 ** If *pRc is non-zero when this function is called, it is a no-op. | 563 ** If *pRc is non-zero when this function is called, it is a no-op. |
| 518 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc | 564 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc |
| 519 ** before returning. | 565 ** before returning. |
| 520 */ | 566 */ |
| 521 static void fts3DeclareVtab(int *pRc, Fts3Table *p){ | 567 static void fts3DeclareVtab(int *pRc, Fts3Table *p){ |
| 522 if( *pRc==SQLITE_OK ){ | 568 if( *pRc==SQLITE_OK ){ |
| 523 int i; /* Iterator variable */ | 569 int i; /* Iterator variable */ |
| 524 int rc; /* Return code */ | 570 int rc; /* Return code */ |
| 525 char *zSql; /* SQL statement passed to declare_vtab() */ | 571 char *zSql; /* SQL statement passed to declare_vtab() */ |
| 526 char *zCols; /* List of user defined columns */ | 572 char *zCols; /* List of user defined columns */ |
| 573 const char *zLanguageid; |
| 574 |
| 575 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid"); |
| 576 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1); |
| 527 | 577 |
| 528 /* Create a list of user columns for the virtual table */ | 578 /* Create a list of user columns for the virtual table */ |
| 529 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]); | 579 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]); |
| 530 for(i=1; zCols && i<p->nColumn; i++){ | 580 for(i=1; zCols && i<p->nColumn; i++){ |
| 531 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]); | 581 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]); |
| 532 } | 582 } |
| 533 | 583 |
| 534 /* Create the whole "CREATE TABLE" statement to pass to SQLite */ | 584 /* Create the whole "CREATE TABLE" statement to pass to SQLite */ |
| 535 zSql = sqlite3_mprintf( | 585 zSql = sqlite3_mprintf( |
| 536 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName | 586 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)", |
| 587 zCols, p->zName, zLanguageid |
| 537 ); | 588 ); |
| 538 if( !zCols || !zSql ){ | 589 if( !zCols || !zSql ){ |
| 539 rc = SQLITE_NOMEM; | 590 rc = SQLITE_NOMEM; |
| 540 }else{ | 591 }else{ |
| 541 rc = sqlite3_declare_vtab(p->db, zSql); | 592 rc = sqlite3_declare_vtab(p->db, zSql); |
| 542 } | 593 } |
| 543 | 594 |
| 544 sqlite3_free(zSql); | 595 sqlite3_free(zSql); |
| 545 sqlite3_free(zCols); | 596 sqlite3_free(zCols); |
| 546 *pRc = rc; | 597 *pRc = rc; |
| 547 } | 598 } |
| 548 } | 599 } |
| 549 | 600 |
| 550 /* | 601 /* |
| 602 ** Create the %_stat table if it does not already exist. |
| 603 */ |
| 604 void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){ |
| 605 fts3DbExec(pRc, p->db, |
| 606 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'" |
| 607 "(id INTEGER PRIMARY KEY, value BLOB);", |
| 608 p->zDb, p->zName |
| 609 ); |
| 610 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1; |
| 611 } |
| 612 |
| 613 /* |
| 551 ** Create the backing store tables (%_content, %_segments and %_segdir) | 614 ** Create the backing store tables (%_content, %_segments and %_segdir) |
| 552 ** required by the FTS3 table passed as the only argument. This is done | 615 ** required by the FTS3 table passed as the only argument. This is done |
| 553 ** as part of the vtab xCreate() method. | 616 ** as part of the vtab xCreate() method. |
| 554 ** | 617 ** |
| 555 ** If the p->bHasDocsize boolean is true (indicating that this is an | 618 ** If the p->bHasDocsize boolean is true (indicating that this is an |
| 556 ** FTS4 table, not an FTS3 table) then also create the %_docsize and | 619 ** FTS4 table, not an FTS3 table) then also create the %_docsize and |
| 557 ** %_stat tables required by FTS4. | 620 ** %_stat tables required by FTS4. |
| 558 */ | 621 */ |
| 559 static int fts3CreateTables(Fts3Table *p){ | 622 static int fts3CreateTables(Fts3Table *p){ |
| 560 int rc = SQLITE_OK; /* Return code */ | 623 int rc = SQLITE_OK; /* Return code */ |
| 561 int i; /* Iterator variable */ | 624 int i; /* Iterator variable */ |
| 562 char *zContentCols; /* Columns of %_content table */ | |
| 563 sqlite3 *db = p->db; /* The database connection */ | 625 sqlite3 *db = p->db; /* The database connection */ |
| 564 | 626 |
| 565 /* Create a list of user columns for the content table */ | 627 if( p->zContentTbl==0 ){ |
| 566 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY"); | 628 const char *zLanguageid = p->zLanguageid; |
| 567 for(i=0; zContentCols && i<p->nColumn; i++){ | 629 char *zContentCols; /* Columns of %_content table */ |
| 568 char *z = p->azColumn[i]; | 630 |
| 569 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z); | 631 /* Create a list of user columns for the content table */ |
| 632 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY"); |
| 633 for(i=0; zContentCols && i<p->nColumn; i++){ |
| 634 char *z = p->azColumn[i]; |
| 635 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z); |
| 636 } |
| 637 if( zLanguageid && zContentCols ){ |
| 638 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid); |
| 639 } |
| 640 if( zContentCols==0 ) rc = SQLITE_NOMEM; |
| 641 |
| 642 /* Create the content table */ |
| 643 fts3DbExec(&rc, db, |
| 644 "CREATE TABLE %Q.'%q_content'(%s)", |
| 645 p->zDb, p->zName, zContentCols |
| 646 ); |
| 647 sqlite3_free(zContentCols); |
| 570 } | 648 } |
| 571 if( zContentCols==0 ) rc = SQLITE_NOMEM; | |
| 572 | 649 |
| 573 /* Create the content table */ | |
| 574 fts3DbExec(&rc, db, | |
| 575 "CREATE TABLE %Q.'%q_content'(%s)", | |
| 576 p->zDb, p->zName, zContentCols | |
| 577 ); | |
| 578 sqlite3_free(zContentCols); | |
| 579 /* Create other tables */ | 650 /* Create other tables */ |
| 580 fts3DbExec(&rc, db, | 651 fts3DbExec(&rc, db, |
| 581 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);", | 652 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);", |
| 582 p->zDb, p->zName | 653 p->zDb, p->zName |
| 583 ); | 654 ); |
| 584 fts3DbExec(&rc, db, | 655 fts3DbExec(&rc, db, |
| 585 "CREATE TABLE %Q.'%q_segdir'(" | 656 "CREATE TABLE %Q.'%q_segdir'(" |
| 586 "level INTEGER," | 657 "level INTEGER," |
| 587 "idx INTEGER," | 658 "idx INTEGER," |
| 588 "start_block INTEGER," | 659 "start_block INTEGER," |
| 589 "leaves_end_block INTEGER," | 660 "leaves_end_block INTEGER," |
| 590 "end_block INTEGER," | 661 "end_block INTEGER," |
| 591 "root BLOB," | 662 "root BLOB," |
| 592 "PRIMARY KEY(level, idx)" | 663 "PRIMARY KEY(level, idx)" |
| 593 ");", | 664 ");", |
| 594 p->zDb, p->zName | 665 p->zDb, p->zName |
| 595 ); | 666 ); |
| 596 if( p->bHasDocsize ){ | 667 if( p->bHasDocsize ){ |
| 597 fts3DbExec(&rc, db, | 668 fts3DbExec(&rc, db, |
| 598 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);", | 669 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);", |
| 599 p->zDb, p->zName | 670 p->zDb, p->zName |
| 600 ); | 671 ); |
| 601 } | 672 } |
| 673 assert( p->bHasStat==p->bFts4 ); |
| 602 if( p->bHasStat ){ | 674 if( p->bHasStat ){ |
| 603 fts3DbExec(&rc, db, | 675 sqlite3Fts3CreateStatTable(&rc, p); |
| 604 "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);", | |
| 605 p->zDb, p->zName | |
| 606 ); | |
| 607 } | 676 } |
| 608 return rc; | 677 return rc; |
| 609 } | 678 } |
| 610 | 679 |
| 611 /* | 680 /* |
| 612 ** Store the current database page-size in bytes in p->nPgsz. | 681 ** Store the current database page-size in bytes in p->nPgsz. |
| 613 ** | 682 ** |
| 614 ** If *pRc is non-zero when this function is called, it is a no-op. | 683 ** If *pRc is non-zero when this function is called, it is a no-op. |
| 615 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc | 684 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc |
| 616 ** before returning. | 685 ** before returning. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 int *pRc, /* IN/OUT: Error code */ | 747 int *pRc, /* IN/OUT: Error code */ |
| 679 char **pz, /* IN/OUT: Pointer to string buffer */ | 748 char **pz, /* IN/OUT: Pointer to string buffer */ |
| 680 const char *zFormat, /* Printf format string to append */ | 749 const char *zFormat, /* Printf format string to append */ |
| 681 ... /* Arguments for printf format string */ | 750 ... /* Arguments for printf format string */ |
| 682 ){ | 751 ){ |
| 683 if( *pRc==SQLITE_OK ){ | 752 if( *pRc==SQLITE_OK ){ |
| 684 va_list ap; | 753 va_list ap; |
| 685 char *z; | 754 char *z; |
| 686 va_start(ap, zFormat); | 755 va_start(ap, zFormat); |
| 687 z = sqlite3_vmprintf(zFormat, ap); | 756 z = sqlite3_vmprintf(zFormat, ap); |
| 757 va_end(ap); |
| 688 if( z && *pz ){ | 758 if( z && *pz ){ |
| 689 char *z2 = sqlite3_mprintf("%s%s", *pz, z); | 759 char *z2 = sqlite3_mprintf("%s%s", *pz, z); |
| 690 sqlite3_free(z); | 760 sqlite3_free(z); |
| 691 z = z2; | 761 z = z2; |
| 692 } | 762 } |
| 693 if( z==0 ) *pRc = SQLITE_NOMEM; | 763 if( z==0 ) *pRc = SQLITE_NOMEM; |
| 694 sqlite3_free(*pz); | 764 sqlite3_free(*pz); |
| 695 *pz = z; | 765 *pz = z; |
| 696 } | 766 } |
| 697 } | 767 } |
| 698 | 768 |
| 699 /* | 769 /* |
| 700 ** Return a copy of input string zInput enclosed in double-quotes (") and | 770 ** Return a copy of input string zInput enclosed in double-quotes (") and |
| 701 ** with all double quote characters escaped. For example: | 771 ** with all double quote characters escaped. For example: |
| 702 ** | 772 ** |
| 703 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\"" | 773 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\"" |
| 704 ** | 774 ** |
| 705 ** The pointer returned points to memory obtained from sqlite3_malloc(). It | 775 ** The pointer returned points to memory obtained from sqlite3_malloc(). It |
| 706 ** is the callers responsibility to call sqlite3_free() to release this | 776 ** is the callers responsibility to call sqlite3_free() to release this |
| 707 ** memory. | 777 ** memory. |
| 708 */ | 778 */ |
| 709 static char *fts3QuoteId(char const *zInput){ | 779 static char *fts3QuoteId(char const *zInput){ |
| 710 int nRet; | 780 int nRet; |
| 711 char *zRet; | 781 char *zRet; |
| 712 nRet = 2 + strlen(zInput)*2 + 1; | 782 nRet = 2 + (int)strlen(zInput)*2 + 1; |
| 713 zRet = sqlite3_malloc(nRet); | 783 zRet = sqlite3_malloc(nRet); |
| 714 if( zRet ){ | 784 if( zRet ){ |
| 715 int i; | 785 int i; |
| 716 char *z = zRet; | 786 char *z = zRet; |
| 717 *(z++) = '"'; | 787 *(z++) = '"'; |
| 718 for(i=0; zInput[i]; i++){ | 788 for(i=0; zInput[i]; i++){ |
| 719 if( zInput[i]=='"' ) *(z++) = '"'; | 789 if( zInput[i]=='"' ) *(z++) = '"'; |
| 720 *(z++) = zInput[i]; | 790 *(z++) = zInput[i]; |
| 721 } | 791 } |
| 722 *(z++) = '"'; | 792 *(z++) = '"'; |
| 723 *(z++) = '\0'; | 793 *(z++) = '\0'; |
| 724 } | 794 } |
| 725 return zRet; | 795 return zRet; |
| 726 } | 796 } |
| 727 | 797 |
| 728 /* | 798 /* |
| 729 ** Return a list of comma separated SQL expressions that could be used | 799 ** Return a list of comma separated SQL expressions and a FROM clause that |
| 730 ** in a SELECT statement such as the following: | 800 ** could be used in a SELECT statement such as the following: |
| 731 ** | 801 ** |
| 732 ** SELECT <list of expressions> FROM %_content AS x ... | 802 ** SELECT <list of expressions> FROM %_content AS x ... |
| 733 ** | 803 ** |
| 734 ** to return the docid, followed by each column of text data in order | 804 ** to return the docid, followed by each column of text data in order |
| 735 ** from left to write. If parameter zFunc is not NULL, then instead of | 805 ** from left to write. If parameter zFunc is not NULL, then instead of |
| 736 ** being returned directly each column of text data is passed to an SQL | 806 ** being returned directly each column of text data is passed to an SQL |
| 737 ** function named zFunc first. For example, if zFunc is "unzip" and the | 807 ** function named zFunc first. For example, if zFunc is "unzip" and the |
| 738 ** table has the three user-defined columns "a", "b", and "c", the following | 808 ** table has the three user-defined columns "a", "b", and "c", the following |
| 739 ** string is returned: | 809 ** string is returned: |
| 740 ** | 810 ** |
| 741 ** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')" | 811 ** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x" |
| 742 ** | 812 ** |
| 743 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It | 813 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It |
| 744 ** is the responsibility of the caller to eventually free it. | 814 ** is the responsibility of the caller to eventually free it. |
| 745 ** | 815 ** |
| 746 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and | 816 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and |
| 747 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered | 817 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered |
| 748 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If | 818 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If |
| 749 ** no error occurs, *pRc is left unmodified. | 819 ** no error occurs, *pRc is left unmodified. |
| 750 */ | 820 */ |
| 751 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){ | 821 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){ |
| 752 char *zRet = 0; | 822 char *zRet = 0; |
| 753 char *zFree = 0; | 823 char *zFree = 0; |
| 754 char *zFunction; | 824 char *zFunction; |
| 755 int i; | 825 int i; |
| 756 | 826 |
| 757 if( !zFunc ){ | 827 if( p->zContentTbl==0 ){ |
| 758 zFunction = ""; | 828 if( !zFunc ){ |
| 829 zFunction = ""; |
| 830 }else{ |
| 831 zFree = zFunction = fts3QuoteId(zFunc); |
| 832 } |
| 833 fts3Appendf(pRc, &zRet, "docid"); |
| 834 for(i=0; i<p->nColumn; i++){ |
| 835 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]); |
| 836 } |
| 837 if( p->zLanguageid ){ |
| 838 fts3Appendf(pRc, &zRet, ", x.%Q", "langid"); |
| 839 } |
| 840 sqlite3_free(zFree); |
| 759 }else{ | 841 }else{ |
| 760 zFree = zFunction = fts3QuoteId(zFunc); | 842 fts3Appendf(pRc, &zRet, "rowid"); |
| 843 for(i=0; i<p->nColumn; i++){ |
| 844 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]); |
| 845 } |
| 846 if( p->zLanguageid ){ |
| 847 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid); |
| 848 } |
| 761 } | 849 } |
| 762 fts3Appendf(pRc, &zRet, "docid"); | 850 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x", |
| 763 for(i=0; i<p->nColumn; i++){ | 851 p->zDb, |
| 764 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]); | 852 (p->zContentTbl ? p->zContentTbl : p->zName), |
| 765 } | 853 (p->zContentTbl ? "" : "_content") |
| 766 sqlite3_free(zFree); | 854 ); |
| 767 return zRet; | 855 return zRet; |
| 768 } | 856 } |
| 769 | 857 |
| 770 /* | 858 /* |
| 771 ** Return a list of N comma separated question marks, where N is the number | 859 ** Return a list of N comma separated question marks, where N is the number |
| 772 ** of columns in the %_content table (one for the docid plus one for each | 860 ** of columns in the %_content table (one for the docid plus one for each |
| 773 ** user-defined text column). | 861 ** user-defined text column). |
| 774 ** | 862 ** |
| 775 ** If argument zFunc is not NULL, then all but the first question mark | 863 ** If argument zFunc is not NULL, then all but the first question mark |
| 776 ** is preceded by zFunc and an open bracket, and followed by a closed | 864 ** is preceded by zFunc and an open bracket, and followed by a closed |
| (...skipping 18 matching lines...) Expand all Loading... |
| 795 | 883 |
| 796 if( !zFunc ){ | 884 if( !zFunc ){ |
| 797 zFunction = ""; | 885 zFunction = ""; |
| 798 }else{ | 886 }else{ |
| 799 zFree = zFunction = fts3QuoteId(zFunc); | 887 zFree = zFunction = fts3QuoteId(zFunc); |
| 800 } | 888 } |
| 801 fts3Appendf(pRc, &zRet, "?"); | 889 fts3Appendf(pRc, &zRet, "?"); |
| 802 for(i=0; i<p->nColumn; i++){ | 890 for(i=0; i<p->nColumn; i++){ |
| 803 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction); | 891 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction); |
| 804 } | 892 } |
| 893 if( p->zLanguageid ){ |
| 894 fts3Appendf(pRc, &zRet, ", ?"); |
| 895 } |
| 805 sqlite3_free(zFree); | 896 sqlite3_free(zFree); |
| 806 return zRet; | 897 return zRet; |
| 807 } | 898 } |
| 808 | 899 |
| 809 /* | 900 /* |
| 901 ** This function interprets the string at (*pp) as a non-negative integer |
| 902 ** value. It reads the integer and sets *pnOut to the value read, then |
| 903 ** sets *pp to point to the byte immediately following the last byte of |
| 904 ** the integer value. |
| 905 ** |
| 906 ** Only decimal digits ('0'..'9') may be part of an integer value. |
| 907 ** |
| 908 ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and |
| 909 ** the output value undefined. Otherwise SQLITE_OK is returned. |
| 910 ** |
| 911 ** This function is used when parsing the "prefix=" FTS4 parameter. |
| 912 */ |
| 913 static int fts3GobbleInt(const char **pp, int *pnOut){ |
| 914 const char *p; /* Iterator pointer */ |
| 915 int nInt = 0; /* Output value */ |
| 916 |
| 917 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ |
| 918 nInt = nInt * 10 + (p[0] - '0'); |
| 919 } |
| 920 if( p==*pp ) return SQLITE_ERROR; |
| 921 *pnOut = nInt; |
| 922 *pp = p; |
| 923 return SQLITE_OK; |
| 924 } |
| 925 |
| 926 /* |
| 927 ** This function is called to allocate an array of Fts3Index structures |
| 928 ** representing the indexes maintained by the current FTS table. FTS tables |
| 929 ** always maintain the main "terms" index, but may also maintain one or |
| 930 ** more "prefix" indexes, depending on the value of the "prefix=" parameter |
| 931 ** (if any) specified as part of the CREATE VIRTUAL TABLE statement. |
| 932 ** |
| 933 ** Argument zParam is passed the value of the "prefix=" option if one was |
| 934 ** specified, or NULL otherwise. |
| 935 ** |
| 936 ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to |
| 937 ** the allocated array. *pnIndex is set to the number of elements in the |
| 938 ** array. If an error does occur, an SQLite error code is returned. |
| 939 ** |
| 940 ** Regardless of whether or not an error is returned, it is the responsibility |
| 941 ** of the caller to call sqlite3_free() on the output array to free it. |
| 942 */ |
| 943 static int fts3PrefixParameter( |
| 944 const char *zParam, /* ABC in prefix=ABC parameter to parse */ |
| 945 int *pnIndex, /* OUT: size of *apIndex[] array */ |
| 946 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */ |
| 947 ){ |
| 948 struct Fts3Index *aIndex; /* Allocated array */ |
| 949 int nIndex = 1; /* Number of entries in array */ |
| 950 |
| 951 if( zParam && zParam[0] ){ |
| 952 const char *p; |
| 953 nIndex++; |
| 954 for(p=zParam; *p; p++){ |
| 955 if( *p==',' ) nIndex++; |
| 956 } |
| 957 } |
| 958 |
| 959 aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex); |
| 960 *apIndex = aIndex; |
| 961 *pnIndex = nIndex; |
| 962 if( !aIndex ){ |
| 963 return SQLITE_NOMEM; |
| 964 } |
| 965 |
| 966 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex); |
| 967 if( zParam ){ |
| 968 const char *p = zParam; |
| 969 int i; |
| 970 for(i=1; i<nIndex; i++){ |
| 971 int nPrefix; |
| 972 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR; |
| 973 aIndex[i].nPrefix = nPrefix; |
| 974 p++; |
| 975 } |
| 976 } |
| 977 |
| 978 return SQLITE_OK; |
| 979 } |
| 980 |
| 981 /* |
| 982 ** This function is called when initializing an FTS4 table that uses the |
| 983 ** content=xxx option. It determines the number of and names of the columns |
| 984 ** of the new FTS4 table. |
| 985 ** |
| 986 ** The third argument passed to this function is the value passed to the |
| 987 ** config=xxx option (i.e. "xxx"). This function queries the database for |
| 988 ** a table of that name. If found, the output variables are populated |
| 989 ** as follows: |
| 990 ** |
| 991 ** *pnCol: Set to the number of columns table xxx has, |
| 992 ** |
| 993 ** *pnStr: Set to the total amount of space required to store a copy |
| 994 ** of each columns name, including the nul-terminator. |
| 995 ** |
| 996 ** *pazCol: Set to point to an array of *pnCol strings. Each string is |
| 997 ** the name of the corresponding column in table xxx. The array |
| 998 ** and its contents are allocated using a single allocation. It |
| 999 ** is the responsibility of the caller to free this allocation |
| 1000 ** by eventually passing the *pazCol value to sqlite3_free(). |
| 1001 ** |
| 1002 ** If the table cannot be found, an error code is returned and the output |
| 1003 ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is |
| 1004 ** returned (and the output variables are undefined). |
| 1005 */ |
| 1006 static int fts3ContentColumns( |
| 1007 sqlite3 *db, /* Database handle */ |
| 1008 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */ |
| 1009 const char *zTbl, /* Name of content table */ |
| 1010 const char ***pazCol, /* OUT: Malloc'd array of column names */ |
| 1011 int *pnCol, /* OUT: Size of array *pazCol */ |
| 1012 int *pnStr /* OUT: Bytes of string content */ |
| 1013 ){ |
| 1014 int rc = SQLITE_OK; /* Return code */ |
| 1015 char *zSql; /* "SELECT *" statement on zTbl */ |
| 1016 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */ |
| 1017 |
| 1018 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl); |
| 1019 if( !zSql ){ |
| 1020 rc = SQLITE_NOMEM; |
| 1021 }else{ |
| 1022 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 1023 } |
| 1024 sqlite3_free(zSql); |
| 1025 |
| 1026 if( rc==SQLITE_OK ){ |
| 1027 const char **azCol; /* Output array */ |
| 1028 int nStr = 0; /* Size of all column names (incl. 0x00) */ |
| 1029 int nCol; /* Number of table columns */ |
| 1030 int i; /* Used to iterate through columns */ |
| 1031 |
| 1032 /* Loop through the returned columns. Set nStr to the number of bytes of |
| 1033 ** space required to store a copy of each column name, including the |
| 1034 ** nul-terminator byte. */ |
| 1035 nCol = sqlite3_column_count(pStmt); |
| 1036 for(i=0; i<nCol; i++){ |
| 1037 const char *zCol = sqlite3_column_name(pStmt, i); |
| 1038 nStr += (int)strlen(zCol) + 1; |
| 1039 } |
| 1040 |
| 1041 /* Allocate and populate the array to return. */ |
| 1042 azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr); |
| 1043 if( azCol==0 ){ |
| 1044 rc = SQLITE_NOMEM; |
| 1045 }else{ |
| 1046 char *p = (char *)&azCol[nCol]; |
| 1047 for(i=0; i<nCol; i++){ |
| 1048 const char *zCol = sqlite3_column_name(pStmt, i); |
| 1049 int n = (int)strlen(zCol)+1; |
| 1050 memcpy(p, zCol, n); |
| 1051 azCol[i] = p; |
| 1052 p += n; |
| 1053 } |
| 1054 } |
| 1055 sqlite3_finalize(pStmt); |
| 1056 |
| 1057 /* Set the output variables. */ |
| 1058 *pnCol = nCol; |
| 1059 *pnStr = nStr; |
| 1060 *pazCol = azCol; |
| 1061 } |
| 1062 |
| 1063 return rc; |
| 1064 } |
| 1065 |
| 1066 /* |
| 810 ** This function is the implementation of both the xConnect and xCreate | 1067 ** This function is the implementation of both the xConnect and xCreate |
| 811 ** methods of the FTS3 virtual table. | 1068 ** methods of the FTS3 virtual table. |
| 812 ** | 1069 ** |
| 813 ** The argv[] array contains the following: | 1070 ** The argv[] array contains the following: |
| 814 ** | 1071 ** |
| 815 ** argv[0] -> module name ("fts3" or "fts4") | 1072 ** argv[0] -> module name ("fts3" or "fts4") |
| 816 ** argv[1] -> database name | 1073 ** argv[1] -> database name |
| 817 ** argv[2] -> table name | 1074 ** argv[2] -> table name |
| 818 ** argv[...] -> "column name" and other module argument fields. | 1075 ** argv[...] -> "column name" and other module argument fields. |
| 819 */ | 1076 */ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 831 int rc = SQLITE_OK; /* Return code */ | 1088 int rc = SQLITE_OK; /* Return code */ |
| 832 int i; /* Iterator variable */ | 1089 int i; /* Iterator variable */ |
| 833 int nByte; /* Size of allocation used for *p */ | 1090 int nByte; /* Size of allocation used for *p */ |
| 834 int iCol; /* Column index */ | 1091 int iCol; /* Column index */ |
| 835 int nString = 0; /* Bytes required to hold all column names */ | 1092 int nString = 0; /* Bytes required to hold all column names */ |
| 836 int nCol = 0; /* Number of columns in the FTS table */ | 1093 int nCol = 0; /* Number of columns in the FTS table */ |
| 837 char *zCsr; /* Space for holding column names */ | 1094 char *zCsr; /* Space for holding column names */ |
| 838 int nDb; /* Bytes required to hold database name */ | 1095 int nDb; /* Bytes required to hold database name */ |
| 839 int nName; /* Bytes required to hold table name */ | 1096 int nName; /* Bytes required to hold table name */ |
| 840 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */ | 1097 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */ |
| 841 int bNoDocsize = 0; /* True to omit %_docsize table */ | |
| 842 const char **aCol; /* Array of column names */ | 1098 const char **aCol; /* Array of column names */ |
| 843 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */ | 1099 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */ |
| 844 | 1100 |
| 845 char *zCompress = 0; | 1101 int nIndex; /* Size of aIndex[] array */ |
| 846 char *zUncompress = 0; | 1102 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */ |
| 1103 |
| 1104 /* The results of parsing supported FTS4 key=value options: */ |
| 1105 int bNoDocsize = 0; /* True to omit %_docsize table */ |
| 1106 int bDescIdx = 0; /* True to store descending indexes */ |
| 1107 char *zPrefix = 0; /* Prefix parameter value (or NULL) */ |
| 1108 char *zCompress = 0; /* compress=? parameter (or NULL) */ |
| 1109 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */ |
| 1110 char *zContent = 0; /* content=? parameter (or NULL) */ |
| 1111 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */ |
| 1112 char **azNotindexed = 0; /* The set of notindexed= columns */ |
| 1113 int nNotindexed = 0; /* Size of azNotindexed[] array */ |
| 847 | 1114 |
| 848 assert( strlen(argv[0])==4 ); | 1115 assert( strlen(argv[0])==4 ); |
| 849 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4) | 1116 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4) |
| 850 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4) | 1117 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4) |
| 851 ); | 1118 ); |
| 852 | 1119 |
| 853 nDb = (int)strlen(argv[1]) + 1; | 1120 nDb = (int)strlen(argv[1]) + 1; |
| 854 nName = (int)strlen(argv[2]) + 1; | 1121 nName = (int)strlen(argv[2]) + 1; |
| 855 | 1122 |
| 856 aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) ); | 1123 nByte = sizeof(const char *) * (argc-2); |
| 857 if( !aCol ) return SQLITE_NOMEM; | 1124 aCol = (const char **)sqlite3_malloc(nByte); |
| 858 memset((void *)aCol, 0, sizeof(const char *) * (argc-2)); | 1125 if( aCol ){ |
| 1126 memset((void*)aCol, 0, nByte); |
| 1127 azNotindexed = (char **)sqlite3_malloc(nByte); |
| 1128 } |
| 1129 if( azNotindexed ){ |
| 1130 memset(azNotindexed, 0, nByte); |
| 1131 } |
| 1132 if( !aCol || !azNotindexed ){ |
| 1133 rc = SQLITE_NOMEM; |
| 1134 goto fts3_init_out; |
| 1135 } |
| 859 | 1136 |
| 860 /* Loop through all of the arguments passed by the user to the FTS3/4 | 1137 /* Loop through all of the arguments passed by the user to the FTS3/4 |
| 861 ** module (i.e. all the column names and special arguments). This loop | 1138 ** module (i.e. all the column names and special arguments). This loop |
| 862 ** does the following: | 1139 ** does the following: |
| 863 ** | 1140 ** |
| 864 ** + Figures out the number of columns the FTSX table will have, and | 1141 ** + Figures out the number of columns the FTSX table will have, and |
| 865 ** the number of bytes of space that must be allocated to store copies | 1142 ** the number of bytes of space that must be allocated to store copies |
| 866 ** of the column names. | 1143 ** of the column names. |
| 867 ** | 1144 ** |
| 868 ** + If there is a tokenizer specification included in the arguments, | 1145 ** + If there is a tokenizer specification included in the arguments, |
| 869 ** initializes the tokenizer pTokenizer. | 1146 ** initializes the tokenizer pTokenizer. |
| 870 */ | 1147 */ |
| 871 for(i=3; rc==SQLITE_OK && i<argc; i++){ | 1148 for(i=3; rc==SQLITE_OK && i<argc; i++){ |
| 872 char const *z = argv[i]; | 1149 char const *z = argv[i]; |
| 873 int nKey; | 1150 int nKey; |
| 874 char *zVal; | 1151 char *zVal; |
| 875 | 1152 |
| 876 /* Check if this is a tokenizer specification */ | 1153 /* Check if this is a tokenizer specification */ |
| 877 if( !pTokenizer | 1154 if( !pTokenizer |
| 878 && strlen(z)>8 | 1155 && strlen(z)>8 |
| 879 && 0==sqlite3_strnicmp(z, "tokenize", 8) | 1156 && 0==sqlite3_strnicmp(z, "tokenize", 8) |
| 880 && 0==sqlite3Fts3IsIdChar(z[8]) | 1157 && 0==sqlite3Fts3IsIdChar(z[8]) |
| 881 ){ | 1158 ){ |
| 882 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr); | 1159 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr); |
| 883 } | 1160 } |
| 884 | 1161 |
| 885 /* Check if it is an FTS4 special argument. */ | 1162 /* Check if it is an FTS4 special argument. */ |
| 886 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){ | 1163 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){ |
| 1164 struct Fts4Option { |
| 1165 const char *zOpt; |
| 1166 int nOpt; |
| 1167 } aFts4Opt[] = { |
| 1168 { "matchinfo", 9 }, /* 0 -> MATCHINFO */ |
| 1169 { "prefix", 6 }, /* 1 -> PREFIX */ |
| 1170 { "compress", 8 }, /* 2 -> COMPRESS */ |
| 1171 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */ |
| 1172 { "order", 5 }, /* 4 -> ORDER */ |
| 1173 { "content", 7 }, /* 5 -> CONTENT */ |
| 1174 { "languageid", 10 }, /* 6 -> LANGUAGEID */ |
| 1175 { "notindexed", 10 } /* 7 -> NOTINDEXED */ |
| 1176 }; |
| 1177 |
| 1178 int iOpt; |
| 887 if( !zVal ){ | 1179 if( !zVal ){ |
| 888 rc = SQLITE_NOMEM; | 1180 rc = SQLITE_NOMEM; |
| 889 goto fts3_init_out; | 1181 }else{ |
| 1182 for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){ |
| 1183 struct Fts4Option *pOp = &aFts4Opt[iOpt]; |
| 1184 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){ |
| 1185 break; |
| 1186 } |
| 1187 } |
| 1188 if( iOpt==SizeofArray(aFts4Opt) ){ |
| 1189 *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z); |
| 1190 rc = SQLITE_ERROR; |
| 1191 }else{ |
| 1192 switch( iOpt ){ |
| 1193 case 0: /* MATCHINFO */ |
| 1194 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){ |
| 1195 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal); |
| 1196 rc = SQLITE_ERROR; |
| 1197 } |
| 1198 bNoDocsize = 1; |
| 1199 break; |
| 1200 |
| 1201 case 1: /* PREFIX */ |
| 1202 sqlite3_free(zPrefix); |
| 1203 zPrefix = zVal; |
| 1204 zVal = 0; |
| 1205 break; |
| 1206 |
| 1207 case 2: /* COMPRESS */ |
| 1208 sqlite3_free(zCompress); |
| 1209 zCompress = zVal; |
| 1210 zVal = 0; |
| 1211 break; |
| 1212 |
| 1213 case 3: /* UNCOMPRESS */ |
| 1214 sqlite3_free(zUncompress); |
| 1215 zUncompress = zVal; |
| 1216 zVal = 0; |
| 1217 break; |
| 1218 |
| 1219 case 4: /* ORDER */ |
| 1220 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) |
| 1221 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) |
| 1222 ){ |
| 1223 *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal); |
| 1224 rc = SQLITE_ERROR; |
| 1225 } |
| 1226 bDescIdx = (zVal[0]=='d' || zVal[0]=='D'); |
| 1227 break; |
| 1228 |
| 1229 case 5: /* CONTENT */ |
| 1230 sqlite3_free(zContent); |
| 1231 zContent = zVal; |
| 1232 zVal = 0; |
| 1233 break; |
| 1234 |
| 1235 case 6: /* LANGUAGEID */ |
| 1236 assert( iOpt==6 ); |
| 1237 sqlite3_free(zLanguageid); |
| 1238 zLanguageid = zVal; |
| 1239 zVal = 0; |
| 1240 break; |
| 1241 |
| 1242 case 7: /* NOTINDEXED */ |
| 1243 azNotindexed[nNotindexed++] = zVal; |
| 1244 zVal = 0; |
| 1245 break; |
| 1246 } |
| 1247 } |
| 1248 sqlite3_free(zVal); |
| 890 } | 1249 } |
| 891 if( nKey==9 && 0==sqlite3_strnicmp(z, "matchinfo", 9) ){ | |
| 892 if( strlen(zVal)==4 && 0==sqlite3_strnicmp(zVal, "fts3", 4) ){ | |
| 893 bNoDocsize = 1; | |
| 894 }else{ | |
| 895 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal); | |
| 896 rc = SQLITE_ERROR; | |
| 897 } | |
| 898 }else if( nKey==8 && 0==sqlite3_strnicmp(z, "compress", 8) ){ | |
| 899 zCompress = zVal; | |
| 900 zVal = 0; | |
| 901 }else if( nKey==10 && 0==sqlite3_strnicmp(z, "uncompress", 10) ){ | |
| 902 zUncompress = zVal; | |
| 903 zVal = 0; | |
| 904 }else{ | |
| 905 *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z); | |
| 906 rc = SQLITE_ERROR; | |
| 907 } | |
| 908 sqlite3_free(zVal); | |
| 909 } | 1250 } |
| 910 | 1251 |
| 911 /* Otherwise, the argument is a column name. */ | 1252 /* Otherwise, the argument is a column name. */ |
| 912 else { | 1253 else { |
| 913 nString += (int)(strlen(z) + 1); | 1254 nString += (int)(strlen(z) + 1); |
| 914 aCol[nCol++] = z; | 1255 aCol[nCol++] = z; |
| 915 } | 1256 } |
| 916 } | 1257 } |
| 1258 |
| 1259 /* If a content=xxx option was specified, the following: |
| 1260 ** |
| 1261 ** 1. Ignore any compress= and uncompress= options. |
| 1262 ** |
| 1263 ** 2. If no column names were specified as part of the CREATE VIRTUAL |
| 1264 ** TABLE statement, use all columns from the content table. |
| 1265 */ |
| 1266 if( rc==SQLITE_OK && zContent ){ |
| 1267 sqlite3_free(zCompress); |
| 1268 sqlite3_free(zUncompress); |
| 1269 zCompress = 0; |
| 1270 zUncompress = 0; |
| 1271 if( nCol==0 ){ |
| 1272 sqlite3_free((void*)aCol); |
| 1273 aCol = 0; |
| 1274 rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString); |
| 1275 |
| 1276 /* If a languageid= option was specified, remove the language id |
| 1277 ** column from the aCol[] array. */ |
| 1278 if( rc==SQLITE_OK && zLanguageid ){ |
| 1279 int j; |
| 1280 for(j=0; j<nCol; j++){ |
| 1281 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){ |
| 1282 int k; |
| 1283 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1]; |
| 1284 nCol--; |
| 1285 break; |
| 1286 } |
| 1287 } |
| 1288 } |
| 1289 } |
| 1290 } |
| 917 if( rc!=SQLITE_OK ) goto fts3_init_out; | 1291 if( rc!=SQLITE_OK ) goto fts3_init_out; |
| 918 | 1292 |
| 919 if( nCol==0 ){ | 1293 if( nCol==0 ){ |
| 920 assert( nString==0 ); | 1294 assert( nString==0 ); |
| 921 aCol[0] = "content"; | 1295 aCol[0] = "content"; |
| 922 nString = 8; | 1296 nString = 8; |
| 923 nCol = 1; | 1297 nCol = 1; |
| 924 } | 1298 } |
| 925 | 1299 |
| 926 if( pTokenizer==0 ){ | 1300 if( pTokenizer==0 ){ |
| 927 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr); | 1301 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr); |
| 928 if( rc!=SQLITE_OK ) goto fts3_init_out; | 1302 if( rc!=SQLITE_OK ) goto fts3_init_out; |
| 929 } | 1303 } |
| 930 assert( pTokenizer ); | 1304 assert( pTokenizer ); |
| 931 | 1305 |
| 1306 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex); |
| 1307 if( rc==SQLITE_ERROR ){ |
| 1308 assert( zPrefix ); |
| 1309 *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix); |
| 1310 } |
| 1311 if( rc!=SQLITE_OK ) goto fts3_init_out; |
| 932 | 1312 |
| 933 /* Allocate and populate the Fts3Table structure. */ | 1313 /* Allocate and populate the Fts3Table structure. */ |
| 934 nByte = sizeof(Fts3Table) + /* Fts3Table */ | 1314 nByte = sizeof(Fts3Table) + /* Fts3Table */ |
| 935 nCol * sizeof(char *) + /* azColumn */ | 1315 nCol * sizeof(char *) + /* azColumn */ |
| 1316 nIndex * sizeof(struct Fts3Index) + /* aIndex */ |
| 1317 nCol * sizeof(u8) + /* abNotindexed */ |
| 936 nName + /* zName */ | 1318 nName + /* zName */ |
| 937 nDb + /* zDb */ | 1319 nDb + /* zDb */ |
| 938 nString; /* Space for azColumn strings */ | 1320 nString; /* Space for azColumn strings */ |
| 939 p = (Fts3Table*)sqlite3_malloc(nByte); | 1321 p = (Fts3Table*)sqlite3_malloc(nByte); |
| 940 if( p==0 ){ | 1322 if( p==0 ){ |
| 941 rc = SQLITE_NOMEM; | 1323 rc = SQLITE_NOMEM; |
| 942 goto fts3_init_out; | 1324 goto fts3_init_out; |
| 943 } | 1325 } |
| 944 memset(p, 0, nByte); | 1326 memset(p, 0, nByte); |
| 945 p->db = db; | 1327 p->db = db; |
| 946 p->nColumn = nCol; | 1328 p->nColumn = nCol; |
| 947 p->nPendingData = 0; | 1329 p->nPendingData = 0; |
| 948 p->azColumn = (char **)&p[1]; | 1330 p->azColumn = (char **)&p[1]; |
| 949 p->pTokenizer = pTokenizer; | 1331 p->pTokenizer = pTokenizer; |
| 950 p->nNodeSize = 1000; | |
| 951 p->nMaxPendingData = FTS3_MAX_PENDING_DATA; | 1332 p->nMaxPendingData = FTS3_MAX_PENDING_DATA; |
| 952 p->bHasDocsize = (isFts4 && bNoDocsize==0); | 1333 p->bHasDocsize = (isFts4 && bNoDocsize==0); |
| 953 p->bHasStat = isFts4; | 1334 p->bHasStat = isFts4; |
| 954 fts3HashInit(&p->pendingTerms, FTS3_HASH_STRING, 1); | 1335 p->bFts4 = isFts4; |
| 1336 p->bDescIdx = bDescIdx; |
| 1337 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */ |
| 1338 p->zContentTbl = zContent; |
| 1339 p->zLanguageid = zLanguageid; |
| 1340 zContent = 0; |
| 1341 zLanguageid = 0; |
| 1342 TESTONLY( p->inTransaction = -1 ); |
| 1343 TESTONLY( p->mxSavepoint = -1 ); |
| 1344 |
| 1345 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol]; |
| 1346 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex); |
| 1347 p->nIndex = nIndex; |
| 1348 for(i=0; i<nIndex; i++){ |
| 1349 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1); |
| 1350 } |
| 1351 p->abNotindexed = (u8 *)&p->aIndex[nIndex]; |
| 955 | 1352 |
| 956 /* Fill in the zName and zDb fields of the vtab structure. */ | 1353 /* Fill in the zName and zDb fields of the vtab structure. */ |
| 957 zCsr = (char *)&p->azColumn[nCol]; | 1354 zCsr = (char *)&p->abNotindexed[nCol]; |
| 958 p->zName = zCsr; | 1355 p->zName = zCsr; |
| 959 memcpy(zCsr, argv[2], nName); | 1356 memcpy(zCsr, argv[2], nName); |
| 960 zCsr += nName; | 1357 zCsr += nName; |
| 961 p->zDb = zCsr; | 1358 p->zDb = zCsr; |
| 962 memcpy(zCsr, argv[1], nDb); | 1359 memcpy(zCsr, argv[1], nDb); |
| 963 zCsr += nDb; | 1360 zCsr += nDb; |
| 964 | 1361 |
| 965 /* Fill in the azColumn array */ | 1362 /* Fill in the azColumn array */ |
| 966 for(iCol=0; iCol<nCol; iCol++){ | 1363 for(iCol=0; iCol<nCol; iCol++){ |
| 967 char *z; | 1364 char *z; |
| 968 int n; | 1365 int n = 0; |
| 969 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n); | 1366 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n); |
| 970 memcpy(zCsr, z, n); | 1367 memcpy(zCsr, z, n); |
| 971 zCsr[n] = '\0'; | 1368 zCsr[n] = '\0'; |
| 972 sqlite3Fts3Dequote(zCsr); | 1369 sqlite3Fts3Dequote(zCsr); |
| 973 p->azColumn[iCol] = zCsr; | 1370 p->azColumn[iCol] = zCsr; |
| 974 zCsr += n+1; | 1371 zCsr += n+1; |
| 975 assert( zCsr <= &((char *)p)[nByte] ); | 1372 assert( zCsr <= &((char *)p)[nByte] ); |
| 976 } | 1373 } |
| 977 | 1374 |
| 978 if( (zCompress==0)!=(zUncompress==0) ){ | 1375 /* Fill in the abNotindexed array */ |
| 1376 for(iCol=0; iCol<nCol; iCol++){ |
| 1377 int n = (int)strlen(p->azColumn[iCol]); |
| 1378 for(i=0; i<nNotindexed; i++){ |
| 1379 char *zNot = azNotindexed[i]; |
| 1380 if( zNot && n==(int)strlen(zNot) |
| 1381 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) |
| 1382 ){ |
| 1383 p->abNotindexed[iCol] = 1; |
| 1384 sqlite3_free(zNot); |
| 1385 azNotindexed[i] = 0; |
| 1386 } |
| 1387 } |
| 1388 } |
| 1389 for(i=0; i<nNotindexed; i++){ |
| 1390 if( azNotindexed[i] ){ |
| 1391 *pzErr = sqlite3_mprintf("no such column: %s", azNotindexed[i]); |
| 1392 rc = SQLITE_ERROR; |
| 1393 } |
| 1394 } |
| 1395 |
| 1396 if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){ |
| 979 char const *zMiss = (zCompress==0 ? "compress" : "uncompress"); | 1397 char const *zMiss = (zCompress==0 ? "compress" : "uncompress"); |
| 980 rc = SQLITE_ERROR; | 1398 rc = SQLITE_ERROR; |
| 981 *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss); | 1399 *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss); |
| 982 } | 1400 } |
| 983 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc); | 1401 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc); |
| 984 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc); | 1402 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc); |
| 985 if( rc!=SQLITE_OK ) goto fts3_init_out; | 1403 if( rc!=SQLITE_OK ) goto fts3_init_out; |
| 986 | 1404 |
| 987 /* If this is an xCreate call, create the underlying tables in the | 1405 /* If this is an xCreate call, create the underlying tables in the |
| 988 ** database. TODO: For xConnect(), it could verify that said tables exist. | 1406 ** database. TODO: For xConnect(), it could verify that said tables exist. |
| 989 */ | 1407 */ |
| 990 if( isCreate ){ | 1408 if( isCreate ){ |
| 991 rc = fts3CreateTables(p); | 1409 rc = fts3CreateTables(p); |
| 992 } | 1410 } |
| 993 | 1411 |
| 1412 /* Check to see if a legacy fts3 table has been "upgraded" by the |
| 1413 ** addition of a %_stat table so that it can use incremental merge. |
| 1414 */ |
| 1415 if( !isFts4 && !isCreate ){ |
| 1416 p->bHasStat = 2; |
| 1417 } |
| 1418 |
| 994 /* Figure out the page-size for the database. This is required in order to | 1419 /* Figure out the page-size for the database. This is required in order to |
| 995 ** estimate the cost of loading large doclists from the database (see | 1420 ** estimate the cost of loading large doclists from the database. */ |
| 996 ** function sqlite3Fts3SegReaderCost() for details). | |
| 997 */ | |
| 998 fts3DatabasePageSize(&rc, p); | 1421 fts3DatabasePageSize(&rc, p); |
| 1422 p->nNodeSize = p->nPgsz-35; |
| 999 | 1423 |
| 1000 /* Declare the table schema to SQLite. */ | 1424 /* Declare the table schema to SQLite. */ |
| 1001 fts3DeclareVtab(&rc, p); | 1425 fts3DeclareVtab(&rc, p); |
| 1002 | 1426 |
| 1003 fts3_init_out: | 1427 fts3_init_out: |
| 1428 sqlite3_free(zPrefix); |
| 1429 sqlite3_free(aIndex); |
| 1004 sqlite3_free(zCompress); | 1430 sqlite3_free(zCompress); |
| 1005 sqlite3_free(zUncompress); | 1431 sqlite3_free(zUncompress); |
| 1432 sqlite3_free(zContent); |
| 1433 sqlite3_free(zLanguageid); |
| 1434 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]); |
| 1006 sqlite3_free((void *)aCol); | 1435 sqlite3_free((void *)aCol); |
| 1436 sqlite3_free((void *)azNotindexed); |
| 1007 if( rc!=SQLITE_OK ){ | 1437 if( rc!=SQLITE_OK ){ |
| 1008 if( p ){ | 1438 if( p ){ |
| 1009 fts3DisconnectMethod((sqlite3_vtab *)p); | 1439 fts3DisconnectMethod((sqlite3_vtab *)p); |
| 1010 }else if( pTokenizer ){ | 1440 }else if( pTokenizer ){ |
| 1011 pTokenizer->pModule->xDestroy(pTokenizer); | 1441 pTokenizer->pModule->xDestroy(pTokenizer); |
| 1012 } | 1442 } |
| 1013 }else{ | 1443 }else{ |
| 1444 assert( p->pSegments==0 ); |
| 1014 *ppVTab = &p->base; | 1445 *ppVTab = &p->base; |
| 1015 } | 1446 } |
| 1016 return rc; | 1447 return rc; |
| 1017 } | 1448 } |
| 1018 | 1449 |
| 1019 /* | 1450 /* |
| 1020 ** The xConnect() and xCreate() methods for the virtual table. All the | 1451 ** The xConnect() and xCreate() methods for the virtual table. All the |
| 1021 ** work is done in function fts3InitVtab(). | 1452 ** work is done in function fts3InitVtab(). |
| 1022 */ | 1453 */ |
| 1023 static int fts3ConnectMethod( | 1454 static int fts3ConnectMethod( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1034 sqlite3 *db, /* Database connection */ | 1465 sqlite3 *db, /* Database connection */ |
| 1035 void *pAux, /* Pointer to tokenizer hash table */ | 1466 void *pAux, /* Pointer to tokenizer hash table */ |
| 1036 int argc, /* Number of elements in argv array */ | 1467 int argc, /* Number of elements in argv array */ |
| 1037 const char * const *argv, /* xCreate/xConnect argument array */ | 1468 const char * const *argv, /* xCreate/xConnect argument array */ |
| 1038 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ | 1469 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ |
| 1039 char **pzErr /* OUT: sqlite3_malloc'd error message */ | 1470 char **pzErr /* OUT: sqlite3_malloc'd error message */ |
| 1040 ){ | 1471 ){ |
| 1041 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr); | 1472 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr); |
| 1042 } | 1473 } |
| 1043 | 1474 |
| 1475 /* |
| 1476 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this |
| 1477 ** extension is currently being used by a version of SQLite too old to |
| 1478 ** support estimatedRows. In that case this function is a no-op. |
| 1479 */ |
| 1480 static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){ |
| 1481 #if SQLITE_VERSION_NUMBER>=3008002 |
| 1482 if( sqlite3_libversion_number()>=3008002 ){ |
| 1483 pIdxInfo->estimatedRows = nRow; |
| 1484 } |
| 1485 #endif |
| 1486 } |
| 1487 |
| 1044 /* | 1488 /* |
| 1045 ** Implementation of the xBestIndex method for FTS3 tables. There | 1489 ** Implementation of the xBestIndex method for FTS3 tables. There |
| 1046 ** are three possible strategies, in order of preference: | 1490 ** are three possible strategies, in order of preference: |
| 1047 ** | 1491 ** |
| 1048 ** 1. Direct lookup by rowid or docid. | 1492 ** 1. Direct lookup by rowid or docid. |
| 1049 ** 2. Full-text search using a MATCH operator on a non-docid column. | 1493 ** 2. Full-text search using a MATCH operator on a non-docid column. |
| 1050 ** 3. Linear scan of %_content table. | 1494 ** 3. Linear scan of %_content table. |
| 1051 */ | 1495 */ |
| 1052 static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ | 1496 static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ |
| 1053 Fts3Table *p = (Fts3Table *)pVTab; | 1497 Fts3Table *p = (Fts3Table *)pVTab; |
| 1054 int i; /* Iterator variable */ | 1498 int i; /* Iterator variable */ |
| 1055 int iCons = -1; /* Index of constraint to use */ | 1499 int iCons = -1; /* Index of constraint to use */ |
| 1056 | 1500 |
| 1501 int iLangidCons = -1; /* Index of langid=x constraint, if present */ |
| 1502 int iDocidGe = -1; /* Index of docid>=x constraint, if present */ |
| 1503 int iDocidLe = -1; /* Index of docid<=x constraint, if present */ |
| 1504 int iIdx; |
| 1505 |
| 1057 /* By default use a full table scan. This is an expensive option, | 1506 /* By default use a full table scan. This is an expensive option, |
| 1058 ** so search through the constraints to see if a more efficient | 1507 ** so search through the constraints to see if a more efficient |
| 1059 ** strategy is possible. | 1508 ** strategy is possible. |
| 1060 */ | 1509 */ |
| 1061 pInfo->idxNum = FTS3_FULLSCAN_SEARCH; | 1510 pInfo->idxNum = FTS3_FULLSCAN_SEARCH; |
| 1062 pInfo->estimatedCost = 500000; | 1511 pInfo->estimatedCost = 5000000; |
| 1063 for(i=0; i<pInfo->nConstraint; i++){ | 1512 for(i=0; i<pInfo->nConstraint; i++){ |
| 1513 int bDocid; /* True if this constraint is on docid */ |
| 1064 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; | 1514 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; |
| 1065 if( pCons->usable==0 ) continue; | 1515 if( pCons->usable==0 ){ |
| 1516 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){ |
| 1517 /* There exists an unusable MATCH constraint. This means that if |
| 1518 ** the planner does elect to use the results of this call as part |
| 1519 ** of the overall query plan the user will see an "unable to use |
| 1520 ** function MATCH in the requested context" error. To discourage |
| 1521 ** this, return a very high cost here. */ |
| 1522 pInfo->idxNum = FTS3_FULLSCAN_SEARCH; |
| 1523 pInfo->estimatedCost = 1e50; |
| 1524 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50); |
| 1525 return SQLITE_OK; |
| 1526 } |
| 1527 continue; |
| 1528 } |
| 1529 |
| 1530 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1); |
| 1066 | 1531 |
| 1067 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ | 1532 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ |
| 1068 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ | 1533 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){ |
| 1069 && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 ) | |
| 1070 ){ | |
| 1071 pInfo->idxNum = FTS3_DOCID_SEARCH; | 1534 pInfo->idxNum = FTS3_DOCID_SEARCH; |
| 1072 pInfo->estimatedCost = 1.0; | 1535 pInfo->estimatedCost = 1.0; |
| 1073 iCons = i; | 1536 iCons = i; |
| 1074 } | 1537 } |
| 1075 | 1538 |
| 1076 /* A MATCH constraint. Use a full-text search. | 1539 /* A MATCH constraint. Use a full-text search. |
| 1077 ** | 1540 ** |
| 1078 ** If there is more than one MATCH constraint available, use the first | 1541 ** If there is more than one MATCH constraint available, use the first |
| 1079 ** one encountered. If there is both a MATCH constraint and a direct | 1542 ** one encountered. If there is both a MATCH constraint and a direct |
| 1080 ** rowid/docid lookup, prefer the MATCH strategy. This is done even | 1543 ** rowid/docid lookup, prefer the MATCH strategy. This is done even |
| 1081 ** though the rowid/docid lookup is faster than a MATCH query, selecting | 1544 ** though the rowid/docid lookup is faster than a MATCH query, selecting |
| 1082 ** it would lead to an "unable to use function MATCH in the requested | 1545 ** it would lead to an "unable to use function MATCH in the requested |
| 1083 ** context" error. | 1546 ** context" error. |
| 1084 */ | 1547 */ |
| 1085 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH | 1548 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH |
| 1086 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn | 1549 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn |
| 1087 ){ | 1550 ){ |
| 1088 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn; | 1551 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn; |
| 1089 pInfo->estimatedCost = 2.0; | 1552 pInfo->estimatedCost = 2.0; |
| 1090 iCons = i; | 1553 iCons = i; |
| 1091 break; | 1554 } |
| 1555 |
| 1556 /* Equality constraint on the langid column */ |
| 1557 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ |
| 1558 && pCons->iColumn==p->nColumn + 2 |
| 1559 ){ |
| 1560 iLangidCons = i; |
| 1561 } |
| 1562 |
| 1563 if( bDocid ){ |
| 1564 switch( pCons->op ){ |
| 1565 case SQLITE_INDEX_CONSTRAINT_GE: |
| 1566 case SQLITE_INDEX_CONSTRAINT_GT: |
| 1567 iDocidGe = i; |
| 1568 break; |
| 1569 |
| 1570 case SQLITE_INDEX_CONSTRAINT_LE: |
| 1571 case SQLITE_INDEX_CONSTRAINT_LT: |
| 1572 iDocidLe = i; |
| 1573 break; |
| 1574 } |
| 1092 } | 1575 } |
| 1093 } | 1576 } |
| 1094 | 1577 |
| 1578 iIdx = 1; |
| 1095 if( iCons>=0 ){ | 1579 if( iCons>=0 ){ |
| 1096 pInfo->aConstraintUsage[iCons].argvIndex = 1; | 1580 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++; |
| 1097 pInfo->aConstraintUsage[iCons].omit = 1; | 1581 pInfo->aConstraintUsage[iCons].omit = 1; |
| 1098 } | 1582 } |
| 1583 if( iLangidCons>=0 ){ |
| 1584 pInfo->idxNum |= FTS3_HAVE_LANGID; |
| 1585 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++; |
| 1586 } |
| 1587 if( iDocidGe>=0 ){ |
| 1588 pInfo->idxNum |= FTS3_HAVE_DOCID_GE; |
| 1589 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++; |
| 1590 } |
| 1591 if( iDocidLe>=0 ){ |
| 1592 pInfo->idxNum |= FTS3_HAVE_DOCID_LE; |
| 1593 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++; |
| 1594 } |
| 1595 |
| 1596 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or |
| 1597 ** docid) order. Both ascending and descending are possible. |
| 1598 */ |
| 1599 if( pInfo->nOrderBy==1 ){ |
| 1600 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0]; |
| 1601 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){ |
| 1602 if( pOrder->desc ){ |
| 1603 pInfo->idxStr = "DESC"; |
| 1604 }else{ |
| 1605 pInfo->idxStr = "ASC"; |
| 1606 } |
| 1607 pInfo->orderByConsumed = 1; |
| 1608 } |
| 1609 } |
| 1610 |
| 1611 assert( p->pSegments==0 ); |
| 1099 return SQLITE_OK; | 1612 return SQLITE_OK; |
| 1100 } | 1613 } |
| 1101 | 1614 |
| 1102 /* | 1615 /* |
| 1103 ** Implementation of xOpen method. | 1616 ** Implementation of xOpen method. |
| 1104 */ | 1617 */ |
| 1105 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ | 1618 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ |
| 1106 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */ | 1619 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */ |
| 1107 | 1620 |
| 1108 UNUSED_PARAMETER(pVTab); | 1621 UNUSED_PARAMETER(pVTab); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1124 ** on the xClose method of the virtual table interface. | 1637 ** on the xClose method of the virtual table interface. |
| 1125 */ | 1638 */ |
| 1126 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){ | 1639 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){ |
| 1127 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; | 1640 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; |
| 1128 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); | 1641 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 1129 sqlite3_finalize(pCsr->pStmt); | 1642 sqlite3_finalize(pCsr->pStmt); |
| 1130 sqlite3Fts3ExprFree(pCsr->pExpr); | 1643 sqlite3Fts3ExprFree(pCsr->pExpr); |
| 1131 sqlite3Fts3FreeDeferredTokens(pCsr); | 1644 sqlite3Fts3FreeDeferredTokens(pCsr); |
| 1132 sqlite3_free(pCsr->aDoclist); | 1645 sqlite3_free(pCsr->aDoclist); |
| 1133 sqlite3_free(pCsr->aMatchinfo); | 1646 sqlite3_free(pCsr->aMatchinfo); |
| 1647 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 1134 sqlite3_free(pCsr); | 1648 sqlite3_free(pCsr); |
| 1135 return SQLITE_OK; | 1649 return SQLITE_OK; |
| 1136 } | 1650 } |
| 1137 | 1651 |
| 1138 /* | 1652 /* |
| 1653 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then |
| 1654 ** compose and prepare an SQL statement of the form: |
| 1655 ** |
| 1656 ** "SELECT <columns> FROM %_content WHERE rowid = ?" |
| 1657 ** |
| 1658 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to |
| 1659 ** it. If an error occurs, return an SQLite error code. |
| 1660 ** |
| 1661 ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK. |
| 1662 */ |
| 1663 static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){ |
| 1664 int rc = SQLITE_OK; |
| 1665 if( pCsr->pStmt==0 ){ |
| 1666 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; |
| 1667 char *zSql; |
| 1668 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist); |
| 1669 if( !zSql ) return SQLITE_NOMEM; |
| 1670 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); |
| 1671 sqlite3_free(zSql); |
| 1672 } |
| 1673 *ppStmt = pCsr->pStmt; |
| 1674 return rc; |
| 1675 } |
| 1676 |
| 1677 /* |
| 1139 ** Position the pCsr->pStmt statement so that it is on the row | 1678 ** Position the pCsr->pStmt statement so that it is on the row |
| 1140 ** of the %_content table that contains the last match. Return | 1679 ** of the %_content table that contains the last match. Return |
| 1141 ** SQLITE_OK on success. | 1680 ** SQLITE_OK on success. |
| 1142 */ | 1681 */ |
| 1143 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ | 1682 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ |
| 1683 int rc = SQLITE_OK; |
| 1144 if( pCsr->isRequireSeek ){ | 1684 if( pCsr->isRequireSeek ){ |
| 1145 pCsr->isRequireSeek = 0; | 1685 sqlite3_stmt *pStmt = 0; |
| 1146 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); | 1686 |
| 1147 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ | 1687 rc = fts3CursorSeekStmt(pCsr, &pStmt); |
| 1148 return SQLITE_OK; | 1688 if( rc==SQLITE_OK ){ |
| 1149 }else{ | 1689 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); |
| 1150 int rc = sqlite3_reset(pCsr->pStmt); | 1690 pCsr->isRequireSeek = 0; |
| 1151 if( rc==SQLITE_OK ){ | 1691 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ |
| 1152 /* If no row was found and no error has occured, then the %_content | 1692 return SQLITE_OK; |
| 1153 ** table is missing a row that is present in the full-text index. | 1693 }else{ |
| 1154 ** The data structures are corrupt. | 1694 rc = sqlite3_reset(pCsr->pStmt); |
| 1155 */ | 1695 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){ |
| 1156 rc = SQLITE_CORRUPT; | 1696 /* If no row was found and no error has occurred, then the %_content |
| 1697 ** table is missing a row that is present in the full-text index. |
| 1698 ** The data structures are corrupt. */ |
| 1699 rc = FTS_CORRUPT_VTAB; |
| 1700 pCsr->isEof = 1; |
| 1701 } |
| 1157 } | 1702 } |
| 1158 pCsr->isEof = 1; | |
| 1159 if( pContext ){ | |
| 1160 sqlite3_result_error_code(pContext, rc); | |
| 1161 } | |
| 1162 return rc; | |
| 1163 } | 1703 } |
| 1164 }else{ | |
| 1165 return SQLITE_OK; | |
| 1166 } | 1704 } |
| 1705 |
| 1706 if( rc!=SQLITE_OK && pContext ){ |
| 1707 sqlite3_result_error_code(pContext, rc); |
| 1708 } |
| 1709 return rc; |
| 1167 } | 1710 } |
| 1168 | 1711 |
| 1169 /* | 1712 /* |
| 1170 ** This function is used to process a single interior node when searching | 1713 ** This function is used to process a single interior node when searching |
| 1171 ** a b-tree for a term or term prefix. The node data is passed to this | 1714 ** a b-tree for a term or term prefix. The node data is passed to this |
| 1172 ** function via the zNode/nNode parameters. The term to search for is | 1715 ** function via the zNode/nNode parameters. The term to search for is |
| 1173 ** passed in zTerm/nTerm. | 1716 ** passed in zTerm/nTerm. |
| 1174 ** | 1717 ** |
| 1175 ** If piFirst is not NULL, then this function sets *piFirst to the blockid | 1718 ** If piFirst is not NULL, then this function sets *piFirst to the blockid |
| 1176 ** of the child node that heads the sub-tree that may contain the term. | 1719 ** of the child node that heads the sub-tree that may contain the term. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1206 ** root node, then the buffer comes from a SELECT statement. SQLite does | 1749 ** root node, then the buffer comes from a SELECT statement. SQLite does |
| 1207 ** not make this guarantee explicitly, but in practice there are always | 1750 ** not make this guarantee explicitly, but in practice there are always |
| 1208 ** either more than 20 bytes of allocated space following the nNode bytes of | 1751 ** either more than 20 bytes of allocated space following the nNode bytes of |
| 1209 ** contents, or two zero bytes. Or, if the node is read from the %_segments | 1752 ** contents, or two zero bytes. Or, if the node is read from the %_segments |
| 1210 ** table, then there are always 20 bytes of zeroed padding following the | 1753 ** table, then there are always 20 bytes of zeroed padding following the |
| 1211 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). | 1754 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). |
| 1212 */ | 1755 */ |
| 1213 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); | 1756 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 1214 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); | 1757 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 1215 if( zCsr>zEnd ){ | 1758 if( zCsr>zEnd ){ |
| 1216 return SQLITE_CORRUPT; | 1759 return FTS_CORRUPT_VTAB; |
| 1217 } | 1760 } |
| 1218 | 1761 |
| 1219 while( zCsr<zEnd && (piFirst || piLast) ){ | 1762 while( zCsr<zEnd && (piFirst || piLast) ){ |
| 1220 int cmp; /* memcmp() result */ | 1763 int cmp; /* memcmp() result */ |
| 1221 int nSuffix; /* Size of term suffix */ | 1764 int nSuffix; /* Size of term suffix */ |
| 1222 int nPrefix = 0; /* Size of term prefix */ | 1765 int nPrefix = 0; /* Size of term prefix */ |
| 1223 int nBuffer; /* Total term size */ | 1766 int nBuffer; /* Total term size */ |
| 1224 | 1767 |
| 1225 /* Load the next term on the node into zBuffer. Use realloc() to expand | 1768 /* Load the next term on the node into zBuffer. Use realloc() to expand |
| 1226 ** the size of zBuffer if required. */ | 1769 ** the size of zBuffer if required. */ |
| 1227 if( !isFirstTerm ){ | 1770 if( !isFirstTerm ){ |
| 1228 zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix); | 1771 zCsr += fts3GetVarint32(zCsr, &nPrefix); |
| 1229 } | 1772 } |
| 1230 isFirstTerm = 0; | 1773 isFirstTerm = 0; |
| 1231 zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix); | 1774 zCsr += fts3GetVarint32(zCsr, &nSuffix); |
| 1232 | 1775 |
| 1233 /* NOTE(shess): Previous code checked for negative nPrefix and | 1776 /* NOTE(shess): Previous code checked for negative nPrefix and |
| 1234 ** nSuffix and suffix overrunning zEnd. Additionally corrupt if | 1777 ** nSuffix and suffix overrunning zEnd. Additionally corrupt if |
| 1235 ** the prefix is longer than the previous term, or if the suffix | 1778 ** the prefix is longer than the previous term, or if the suffix |
| 1236 ** causes overflow. | 1779 ** causes overflow. |
| 1237 */ | 1780 */ |
| 1238 if( nPrefix<0 || nSuffix<0 /* || nPrefix>nBuffer */ | 1781 if( nPrefix<0 || nSuffix<0 /* || nPrefix>nBuffer */ |
| 1239 || &zCsr[nSuffix]<zCsr || &zCsr[nSuffix]>zEnd ){ | 1782 || &zCsr[nSuffix]<zCsr || &zCsr[nSuffix]>zEnd ){ |
| 1240 rc = SQLITE_CORRUPT; | 1783 rc = SQLITE_CORRUPT; |
| 1241 goto finish_scan; | 1784 goto finish_scan; |
| 1242 } | 1785 } |
| 1243 if( nPrefix+nSuffix>nAlloc ){ | 1786 if( nPrefix+nSuffix>nAlloc ){ |
| 1244 char *zNew; | 1787 char *zNew; |
| 1245 nAlloc = (nPrefix+nSuffix) * 2; | 1788 nAlloc = (nPrefix+nSuffix) * 2; |
| 1246 zNew = (char *)sqlite3_realloc(zBuffer, nAlloc); | 1789 zNew = (char *)sqlite3_realloc(zBuffer, nAlloc); |
| 1247 if( !zNew ){ | 1790 if( !zNew ){ |
| 1248 rc = SQLITE_NOMEM; | 1791 rc = SQLITE_NOMEM; |
| 1249 goto finish_scan; | 1792 goto finish_scan; |
| 1250 } | 1793 } |
| 1251 zBuffer = zNew; | 1794 zBuffer = zNew; |
| 1252 } | 1795 } |
| 1796 assert( zBuffer ); |
| 1253 memcpy(&zBuffer[nPrefix], zCsr, nSuffix); | 1797 memcpy(&zBuffer[nPrefix], zCsr, nSuffix); |
| 1254 nBuffer = nPrefix + nSuffix; | 1798 nBuffer = nPrefix + nSuffix; |
| 1255 zCsr += nSuffix; | 1799 zCsr += nSuffix; |
| 1256 | 1800 |
| 1257 /* Compare the term we are searching for with the term just loaded from | 1801 /* Compare the term we are searching for with the term just loaded from |
| 1258 ** the interior node. If the specified term is greater than or equal | 1802 ** the interior node. If the specified term is greater than or equal |
| 1259 ** to the term from the interior node, then all terms on the sub-tree | 1803 ** to the term from the interior node, then all terms on the sub-tree |
| 1260 ** headed by node iChild are smaller than zTerm. No need to search | 1804 ** headed by node iChild are smaller than zTerm. No need to search |
| 1261 ** iChild. | 1805 ** iChild. |
| 1262 ** | 1806 ** |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 const char *zNode, /* Buffer containing segment interior node */ | 1858 const char *zNode, /* Buffer containing segment interior node */ |
| 1315 int nNode, /* Size of buffer at zNode */ | 1859 int nNode, /* Size of buffer at zNode */ |
| 1316 sqlite3_int64 *piLeaf, /* Selected leaf node */ | 1860 sqlite3_int64 *piLeaf, /* Selected leaf node */ |
| 1317 sqlite3_int64 *piLeaf2 /* Selected leaf node */ | 1861 sqlite3_int64 *piLeaf2 /* Selected leaf node */ |
| 1318 ){ | 1862 ){ |
| 1319 int rc; /* Return code */ | 1863 int rc; /* Return code */ |
| 1320 int iHeight; /* Height of this node in tree */ | 1864 int iHeight; /* Height of this node in tree */ |
| 1321 | 1865 |
| 1322 assert( piLeaf || piLeaf2 ); | 1866 assert( piLeaf || piLeaf2 ); |
| 1323 | 1867 |
| 1324 sqlite3Fts3GetVarint32(zNode, &iHeight); | 1868 fts3GetVarint32(zNode, &iHeight); |
| 1325 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); | 1869 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); |
| 1326 assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); | 1870 assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); |
| 1327 | 1871 |
| 1328 if( rc==SQLITE_OK && iHeight>1 ){ | 1872 if( rc==SQLITE_OK && iHeight>1 ){ |
| 1329 char *zBlob = 0; /* Blob read from %_segments table */ | 1873 char *zBlob = 0; /* Blob read from %_segments table */ |
| 1330 int nBlob; /* Size of zBlob in bytes */ | 1874 int nBlob; /* Size of zBlob in bytes */ |
| 1331 | 1875 |
| 1332 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){ | 1876 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){ |
| 1333 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob); | 1877 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0); |
| 1334 if( rc==SQLITE_OK ){ | 1878 if( rc==SQLITE_OK ){ |
| 1335 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0); | 1879 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0); |
| 1336 } | 1880 } |
| 1337 sqlite3_free(zBlob); | 1881 sqlite3_free(zBlob); |
| 1338 piLeaf = 0; | 1882 piLeaf = 0; |
| 1339 zBlob = 0; | 1883 zBlob = 0; |
| 1340 } | 1884 } |
| 1341 | 1885 |
| 1342 if( rc==SQLITE_OK ){ | 1886 if( rc==SQLITE_OK ){ |
| 1343 rc = sqlite3Fts3ReadBlock(p, piLeaf ? *piLeaf : *piLeaf2, &zBlob, &nBlob); | 1887 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0); |
| 1344 } | 1888 } |
| 1345 if( rc==SQLITE_OK ){ | 1889 if( rc==SQLITE_OK ){ |
| 1346 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2); | 1890 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2); |
| 1347 } | 1891 } |
| 1348 sqlite3_free(zBlob); | 1892 sqlite3_free(zBlob); |
| 1349 } | 1893 } |
| 1350 | 1894 |
| 1351 return rc; | 1895 return rc; |
| 1352 } | 1896 } |
| 1353 | 1897 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1516 char **pp2 /* Right input list */ | 2060 char **pp2 /* Right input list */ |
| 1517 ){ | 2061 ){ |
| 1518 char *p = *pp; | 2062 char *p = *pp; |
| 1519 char *p1 = *pp1; | 2063 char *p1 = *pp1; |
| 1520 char *p2 = *pp2; | 2064 char *p2 = *pp2; |
| 1521 | 2065 |
| 1522 while( *p1 || *p2 ){ | 2066 while( *p1 || *p2 ){ |
| 1523 int iCol1; /* The current column index in pp1 */ | 2067 int iCol1; /* The current column index in pp1 */ |
| 1524 int iCol2; /* The current column index in pp2 */ | 2068 int iCol2; /* The current column index in pp2 */ |
| 1525 | 2069 |
| 1526 if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1); | 2070 if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1); |
| 1527 else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; | 2071 else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; |
| 1528 else iCol1 = 0; | 2072 else iCol1 = 0; |
| 1529 | 2073 |
| 1530 if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2); | 2074 if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2); |
| 1531 else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; | 2075 else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; |
| 1532 else iCol2 = 0; | 2076 else iCol2 = 0; |
| 1533 | 2077 |
| 1534 if( iCol1==iCol2 ){ | 2078 if( iCol1==iCol2 ){ |
| 1535 sqlite3_int64 i1 = 0; /* Last position from pp1 */ | 2079 sqlite3_int64 i1 = 0; /* Last position from pp1 */ |
| 1536 sqlite3_int64 i2 = 0; /* Last position from pp2 */ | 2080 sqlite3_int64 i2 = 0; /* Last position from pp2 */ |
| 1537 sqlite3_int64 iPrev = 0; | 2081 sqlite3_int64 iPrev = 0; |
| 1538 int n = fts3PutColNumber(&p, iCol1); | 2082 int n = fts3PutColNumber(&p, iCol1); |
| 1539 p1 += n; | 2083 p1 += n; |
| 1540 p2 += n; | 2084 p2 += n; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1571 } | 2115 } |
| 1572 } | 2116 } |
| 1573 | 2117 |
| 1574 *p++ = POS_END; | 2118 *p++ = POS_END; |
| 1575 *pp = p; | 2119 *pp = p; |
| 1576 *pp1 = p1 + 1; | 2120 *pp1 = p1 + 1; |
| 1577 *pp2 = p2 + 1; | 2121 *pp2 = p2 + 1; |
| 1578 } | 2122 } |
| 1579 | 2123 |
| 1580 /* | 2124 /* |
| 1581 ** nToken==1 searches for adjacent positions. | |
| 1582 ** | |
| 1583 ** This function is used to merge two position lists into one. When it is | 2125 ** This function is used to merge two position lists into one. When it is |
| 1584 ** called, *pp1 and *pp2 must both point to position lists. A position-list is | 2126 ** called, *pp1 and *pp2 must both point to position lists. A position-list is |
| 1585 ** the part of a doclist that follows each document id. For example, if a row | 2127 ** the part of a doclist that follows each document id. For example, if a row |
| 1586 ** contains: | 2128 ** contains: |
| 1587 ** | 2129 ** |
| 1588 ** 'a b c'|'x y z'|'a b b a' | 2130 ** 'a b c'|'x y z'|'a b b a' |
| 1589 ** | 2131 ** |
| 1590 ** Then the position list for this row for token 'b' would consist of: | 2132 ** Then the position list for this row for token 'b' would consist of: |
| 1591 ** | 2133 ** |
| 1592 ** 0x02 0x01 0x02 0x03 0x03 0x00 | 2134 ** 0x02 0x01 0x02 0x03 0x03 0x00 |
| 1593 ** | 2135 ** |
| 1594 ** When this function returns, both *pp1 and *pp2 are left pointing to the | 2136 ** When this function returns, both *pp1 and *pp2 are left pointing to the |
| 1595 ** byte following the 0x00 terminator of their respective position lists. | 2137 ** byte following the 0x00 terminator of their respective position lists. |
| 1596 ** | 2138 ** |
| 1597 ** If isSaveLeft is 0, an entry is added to the output position list for | 2139 ** If isSaveLeft is 0, an entry is added to the output position list for |
| 1598 ** each position in *pp2 for which there exists one or more positions in | 2140 ** each position in *pp2 for which there exists one or more positions in |
| 1599 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e. | 2141 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e. |
| 1600 ** when the *pp1 token appears before the *pp2 token, but not more than nToken | 2142 ** when the *pp1 token appears before the *pp2 token, but not more than nToken |
| 1601 ** slots before it. | 2143 ** slots before it. |
| 2144 ** |
| 2145 ** e.g. nToken==1 searches for adjacent positions. |
| 1602 */ | 2146 */ |
| 1603 static int fts3PoslistPhraseMerge( | 2147 static int fts3PoslistPhraseMerge( |
| 1604 char **pp, /* IN/OUT: Preallocated output buffer */ | 2148 char **pp, /* IN/OUT: Preallocated output buffer */ |
| 1605 int nToken, /* Maximum difference in token positions */ | 2149 int nToken, /* Maximum difference in token positions */ |
| 1606 int isSaveLeft, /* Save the left position */ | 2150 int isSaveLeft, /* Save the left position */ |
| 1607 int isExact, /* If *pp1 is exactly nTokens before *pp2 */ | 2151 int isExact, /* If *pp1 is exactly nTokens before *pp2 */ |
| 1608 char **pp1, /* IN/OUT: Left input list */ | 2152 char **pp1, /* IN/OUT: Left input list */ |
| 1609 char **pp2 /* IN/OUT: Right input list */ | 2153 char **pp2 /* IN/OUT: Right input list */ |
| 1610 ){ | 2154 ){ |
| 1611 char *p = (pp ? *pp : 0); | 2155 char *p = *pp; |
| 1612 char *p1 = *pp1; | 2156 char *p1 = *pp1; |
| 1613 char *p2 = *pp2; | 2157 char *p2 = *pp2; |
| 1614 int iCol1 = 0; | 2158 int iCol1 = 0; |
| 1615 int iCol2 = 0; | 2159 int iCol2 = 0; |
| 1616 | 2160 |
| 1617 /* Never set both isSaveLeft and isExact for the same invocation. */ | 2161 /* Never set both isSaveLeft and isExact for the same invocation. */ |
| 1618 assert( isSaveLeft==0 || isExact==0 ); | 2162 assert( isSaveLeft==0 || isExact==0 ); |
| 1619 | 2163 |
| 1620 assert( *p1!=0 && *p2!=0 ); | 2164 assert( p!=0 && *p1!=0 && *p2!=0 ); |
| 1621 if( *p1==POS_COLUMN ){ | 2165 if( *p1==POS_COLUMN ){ |
| 1622 p1++; | 2166 p1++; |
| 1623 p1 += sqlite3Fts3GetVarint32(p1, &iCol1); | 2167 p1 += fts3GetVarint32(p1, &iCol1); |
| 1624 } | 2168 } |
| 1625 if( *p2==POS_COLUMN ){ | 2169 if( *p2==POS_COLUMN ){ |
| 1626 p2++; | 2170 p2++; |
| 1627 p2 += sqlite3Fts3GetVarint32(p2, &iCol2); | 2171 p2 += fts3GetVarint32(p2, &iCol2); |
| 1628 } | 2172 } |
| 1629 | 2173 |
| 1630 while( 1 ){ | 2174 while( 1 ){ |
| 1631 if( iCol1==iCol2 ){ | 2175 if( iCol1==iCol2 ){ |
| 1632 char *pSave = p; | 2176 char *pSave = p; |
| 1633 sqlite3_int64 iPrev = 0; | 2177 sqlite3_int64 iPrev = 0; |
| 1634 sqlite3_int64 iPos1 = 0; | 2178 sqlite3_int64 iPos1 = 0; |
| 1635 sqlite3_int64 iPos2 = 0; | 2179 sqlite3_int64 iPos2 = 0; |
| 1636 | 2180 |
| 1637 if( pp && iCol1 ){ | 2181 if( iCol1 ){ |
| 1638 *p++ = POS_COLUMN; | 2182 *p++ = POS_COLUMN; |
| 1639 p += sqlite3Fts3PutVarint(p, iCol1); | 2183 p += sqlite3Fts3PutVarint(p, iCol1); |
| 1640 } | 2184 } |
| 1641 | 2185 |
| 1642 assert( *p1!=POS_END && *p1!=POS_COLUMN ); | 2186 assert( *p1!=POS_END && *p1!=POS_COLUMN ); |
| 1643 assert( *p2!=POS_END && *p2!=POS_COLUMN ); | 2187 assert( *p2!=POS_END && *p2!=POS_COLUMN ); |
| 1644 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; | 2188 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; |
| 1645 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; | 2189 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; |
| 1646 | 2190 |
| 1647 while( 1 ){ | 2191 while( 1 ){ |
| 1648 if( iPos2==iPos1+nToken | 2192 if( iPos2==iPos1+nToken |
| 1649 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) | 2193 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) |
| 1650 ){ | 2194 ){ |
| 1651 sqlite3_int64 iSave; | 2195 sqlite3_int64 iSave; |
| 1652 if( !pp ){ | |
| 1653 fts3PoslistCopy(0, &p2); | |
| 1654 fts3PoslistCopy(0, &p1); | |
| 1655 *pp1 = p1; | |
| 1656 *pp2 = p2; | |
| 1657 return 1; | |
| 1658 } | |
| 1659 iSave = isSaveLeft ? iPos1 : iPos2; | 2196 iSave = isSaveLeft ? iPos1 : iPos2; |
| 1660 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2; | 2197 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2; |
| 1661 pSave = 0; | 2198 pSave = 0; |
| 2199 assert( p ); |
| 1662 } | 2200 } |
| 1663 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){ | 2201 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){ |
| 1664 if( (*p2&0xFE)==0 ) break; | 2202 if( (*p2&0xFE)==0 ) break; |
| 1665 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; | 2203 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; |
| 1666 }else{ | 2204 }else{ |
| 1667 if( (*p1&0xFE)==0 ) break; | 2205 if( (*p1&0xFE)==0 ) break; |
| 1668 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; | 2206 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; |
| 1669 } | 2207 } |
| 1670 } | 2208 } |
| 1671 | 2209 |
| 1672 if( pSave ){ | 2210 if( pSave ){ |
| 1673 assert( pp && p ); | 2211 assert( pp && p ); |
| 1674 p = pSave; | 2212 p = pSave; |
| 1675 } | 2213 } |
| 1676 | 2214 |
| 1677 fts3ColumnlistCopy(0, &p1); | 2215 fts3ColumnlistCopy(0, &p1); |
| 1678 fts3ColumnlistCopy(0, &p2); | 2216 fts3ColumnlistCopy(0, &p2); |
| 1679 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 ); | 2217 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 ); |
| 1680 if( 0==*p1 || 0==*p2 ) break; | 2218 if( 0==*p1 || 0==*p2 ) break; |
| 1681 | 2219 |
| 1682 p1++; | 2220 p1++; |
| 1683 p1 += sqlite3Fts3GetVarint32(p1, &iCol1); | 2221 p1 += fts3GetVarint32(p1, &iCol1); |
| 1684 p2++; | 2222 p2++; |
| 1685 p2 += sqlite3Fts3GetVarint32(p2, &iCol2); | 2223 p2 += fts3GetVarint32(p2, &iCol2); |
| 1686 } | 2224 } |
| 1687 | 2225 |
| 1688 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of | 2226 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of |
| 1689 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the | 2227 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the |
| 1690 ** end of the position list, or the 0x01 that precedes the next | 2228 ** end of the position list, or the 0x01 that precedes the next |
| 1691 ** column-number in the position list. | 2229 ** column-number in the position list. |
| 1692 */ | 2230 */ |
| 1693 else if( iCol1<iCol2 ){ | 2231 else if( iCol1<iCol2 ){ |
| 1694 fts3ColumnlistCopy(0, &p1); | 2232 fts3ColumnlistCopy(0, &p1); |
| 1695 if( 0==*p1 ) break; | 2233 if( 0==*p1 ) break; |
| 1696 p1++; | 2234 p1++; |
| 1697 p1 += sqlite3Fts3GetVarint32(p1, &iCol1); | 2235 p1 += fts3GetVarint32(p1, &iCol1); |
| 1698 }else{ | 2236 }else{ |
| 1699 fts3ColumnlistCopy(0, &p2); | 2237 fts3ColumnlistCopy(0, &p2); |
| 1700 if( 0==*p2 ) break; | 2238 if( 0==*p2 ) break; |
| 1701 p2++; | 2239 p2++; |
| 1702 p2 += sqlite3Fts3GetVarint32(p2, &iCol2); | 2240 p2 += fts3GetVarint32(p2, &iCol2); |
| 1703 } | 2241 } |
| 1704 } | 2242 } |
| 1705 | 2243 |
| 1706 fts3PoslistCopy(0, &p2); | 2244 fts3PoslistCopy(0, &p2); |
| 1707 fts3PoslistCopy(0, &p1); | 2245 fts3PoslistCopy(0, &p1); |
| 1708 *pp1 = p1; | 2246 *pp1 = p1; |
| 1709 *pp2 = p2; | 2247 *pp2 = p2; |
| 1710 if( !pp || *pp==p ){ | 2248 if( *pp==p ){ |
| 1711 return 0; | 2249 return 0; |
| 1712 } | 2250 } |
| 1713 *p++ = 0x00; | 2251 *p++ = 0x00; |
| 1714 *pp = p; | 2252 *pp = p; |
| 1715 return 1; | 2253 return 1; |
| 1716 } | 2254 } |
| 1717 | 2255 |
| 1718 /* | 2256 /* |
| 1719 ** Merge two position-lists as required by the NEAR operator. | 2257 ** Merge two position-lists as required by the NEAR operator. The argument |
| 2258 ** position lists correspond to the left and right phrases of an expression |
| 2259 ** like: |
| 2260 ** |
| 2261 ** "phrase 1" NEAR "phrase number 2" |
| 2262 ** |
| 2263 ** Position list *pp1 corresponds to the left-hand side of the NEAR |
| 2264 ** expression and *pp2 to the right. As usual, the indexes in the position |
| 2265 ** lists are the offsets of the last token in each phrase (tokens "1" and "2" |
| 2266 ** in the example above). |
| 2267 ** |
| 2268 ** The output position list - written to *pp - is a copy of *pp2 with those |
| 2269 ** entries that are not sufficiently NEAR entries in *pp1 removed. |
| 1720 */ | 2270 */ |
| 1721 static int fts3PoslistNearMerge( | 2271 static int fts3PoslistNearMerge( |
| 1722 char **pp, /* Output buffer */ | 2272 char **pp, /* Output buffer */ |
| 1723 char *aTmp, /* Temporary buffer space */ | 2273 char *aTmp, /* Temporary buffer space */ |
| 1724 int nRight, /* Maximum difference in token positions */ | 2274 int nRight, /* Maximum difference in token positions */ |
| 1725 int nLeft, /* Maximum difference in token positions */ | 2275 int nLeft, /* Maximum difference in token positions */ |
| 1726 char **pp1, /* IN/OUT: Left input list */ | 2276 char **pp1, /* IN/OUT: Left input list */ |
| 1727 char **pp2 /* IN/OUT: Right input list */ | 2277 char **pp2 /* IN/OUT: Right input list */ |
| 1728 ){ | 2278 ){ |
| 1729 char *p1 = *pp1; | 2279 char *p1 = *pp1; |
| 1730 char *p2 = *pp2; | 2280 char *p2 = *pp2; |
| 1731 | 2281 |
| 1732 if( !pp ){ | 2282 char *pTmp1 = aTmp; |
| 1733 if( fts3PoslistPhraseMerge(0, nRight, 0, 0, pp1, pp2) ) return 1; | 2283 char *pTmp2; |
| 1734 *pp1 = p1; | 2284 char *aTmp2; |
| 1735 *pp2 = p2; | 2285 int res = 1; |
| 1736 return fts3PoslistPhraseMerge(0, nLeft, 0, 0, pp2, pp1); | 2286 |
| 2287 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2); |
| 2288 aTmp2 = pTmp2 = pTmp1; |
| 2289 *pp1 = p1; |
| 2290 *pp2 = p2; |
| 2291 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1); |
| 2292 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){ |
| 2293 fts3PoslistMerge(pp, &aTmp, &aTmp2); |
| 2294 }else if( pTmp1!=aTmp ){ |
| 2295 fts3PoslistCopy(pp, &aTmp); |
| 2296 }else if( pTmp2!=aTmp2 ){ |
| 2297 fts3PoslistCopy(pp, &aTmp2); |
| 1737 }else{ | 2298 }else{ |
| 1738 char *pTmp1 = aTmp; | 2299 res = 0; |
| 1739 char *pTmp2; | 2300 } |
| 1740 char *aTmp2; | 2301 |
| 1741 int res = 1; | 2302 return res; |
| 1742 | 2303 } |
| 1743 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2); | 2304 |
| 1744 aTmp2 = pTmp2 = pTmp1; | 2305 /* |
| 1745 *pp1 = p1; | 2306 ** An instance of this function is used to merge together the (potentially |
| 1746 *pp2 = p2; | 2307 ** large number of) doclists for each term that matches a prefix query. |
| 1747 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1); | 2308 ** See function fts3TermSelectMerge() for details. |
| 1748 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){ | 2309 */ |
| 1749 fts3PoslistMerge(pp, &aTmp, &aTmp2); | 2310 typedef struct TermSelect TermSelect; |
| 1750 }else if( pTmp1!=aTmp ){ | 2311 struct TermSelect { |
| 1751 fts3PoslistCopy(pp, &aTmp); | 2312 char *aaOutput[16]; /* Malloc'd output buffers */ |
| 1752 }else if( pTmp2!=aTmp2 ){ | 2313 int anOutput[16]; /* Size each output buffer in bytes */ |
| 1753 fts3PoslistCopy(pp, &aTmp2); | 2314 }; |
| 2315 |
| 2316 /* |
| 2317 ** This function is used to read a single varint from a buffer. Parameter |
| 2318 ** pEnd points 1 byte past the end of the buffer. When this function is |
| 2319 ** called, if *pp points to pEnd or greater, then the end of the buffer |
| 2320 ** has been reached. In this case *pp is set to 0 and the function returns. |
| 2321 ** |
| 2322 ** If *pp does not point to or past pEnd, then a single varint is read |
| 2323 ** from *pp. *pp is then set to point 1 byte past the end of the read varint. |
| 2324 ** |
| 2325 ** If bDescIdx is false, the value read is added to *pVal before returning. |
| 2326 ** If it is true, the value read is subtracted from *pVal before this |
| 2327 ** function returns. |
| 2328 */ |
| 2329 static void fts3GetDeltaVarint3( |
| 2330 char **pp, /* IN/OUT: Point to read varint from */ |
| 2331 char *pEnd, /* End of buffer */ |
| 2332 int bDescIdx, /* True if docids are descending */ |
| 2333 sqlite3_int64 *pVal /* IN/OUT: Integer value */ |
| 2334 ){ |
| 2335 if( *pp>=pEnd ){ |
| 2336 *pp = 0; |
| 2337 }else{ |
| 2338 sqlite3_int64 iVal; |
| 2339 *pp += sqlite3Fts3GetVarint(*pp, &iVal); |
| 2340 if( bDescIdx ){ |
| 2341 *pVal -= iVal; |
| 1754 }else{ | 2342 }else{ |
| 1755 res = 0; | 2343 *pVal += iVal; |
| 1756 } | 2344 } |
| 1757 | 2345 } |
| 1758 return res; | 2346 } |
| 1759 } | 2347 |
| 1760 } | 2348 /* |
| 1761 | 2349 ** This function is used to write a single varint to a buffer. The varint |
| 1762 /* | 2350 ** is written to *pp. Before returning, *pp is set to point 1 byte past the |
| 1763 ** Values that may be used as the first parameter to fts3DoclistMerge(). | 2351 ** end of the value written. |
| 1764 */ | 2352 ** |
| 1765 #define MERGE_NOT 2 /* D + D -> D */ | 2353 ** If *pbFirst is zero when this function is called, the value written to |
| 1766 #define MERGE_AND 3 /* D + D -> D */ | 2354 ** the buffer is that of parameter iVal. |
| 1767 #define MERGE_OR 4 /* D + D -> D */ | 2355 ** |
| 1768 #define MERGE_POS_OR 5 /* P + P -> P */ | 2356 ** If *pbFirst is non-zero when this function is called, then the value |
| 1769 #define MERGE_PHRASE 6 /* P + P -> D */ | 2357 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal) |
| 1770 #define MERGE_POS_PHRASE 7 /* P + P -> P */ | 2358 ** (if bDescIdx is non-zero). |
| 1771 #define MERGE_NEAR 8 /* P + P -> D */ | 2359 ** |
| 1772 #define MERGE_POS_NEAR 9 /* P + P -> P */ | 2360 ** Before returning, this function always sets *pbFirst to 1 and *piPrev |
| 1773 | 2361 ** to the value of parameter iVal. |
| 1774 /* | 2362 */ |
| 1775 ** Merge the two doclists passed in buffer a1 (size n1 bytes) and a2 | 2363 static void fts3PutDeltaVarint3( |
| 1776 ** (size n2 bytes). The output is written to pre-allocated buffer aBuffer, | 2364 char **pp, /* IN/OUT: Output pointer */ |
| 1777 ** which is guaranteed to be large enough to hold the results. The number | 2365 int bDescIdx, /* True for descending docids */ |
| 1778 ** of bytes written to aBuffer is stored in *pnBuffer before returning. | 2366 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */ |
| 1779 ** | 2367 int *pbFirst, /* IN/OUT: True after first int written */ |
| 1780 ** If successful, SQLITE_OK is returned. Otherwise, if a malloc error | 2368 sqlite3_int64 iVal /* Write this value to the list */ |
| 1781 ** occurs while allocating a temporary buffer as part of the merge operation, | 2369 ){ |
| 1782 ** SQLITE_NOMEM is returned. | 2370 sqlite3_int64 iWrite; |
| 1783 */ | 2371 if( bDescIdx==0 || *pbFirst==0 ){ |
| 1784 static int fts3DoclistMerge( | 2372 iWrite = iVal - *piPrev; |
| 1785 int mergetype, /* One of the MERGE_XXX constants */ | 2373 }else{ |
| 1786 int nParam1, /* Used by MERGE_NEAR and MERGE_POS_NEAR */ | 2374 iWrite = *piPrev - iVal; |
| 1787 int nParam2, /* Used by MERGE_NEAR and MERGE_POS_NEAR */ | 2375 } |
| 1788 char *aBuffer, /* Pre-allocated output buffer */ | 2376 assert( *pbFirst || *piPrev==0 ); |
| 1789 int *pnBuffer, /* OUT: Bytes written to aBuffer */ | 2377 assert( *pbFirst==0 || iWrite>0 ); |
| 1790 char *a1, /* Buffer containing first doclist */ | 2378 *pp += sqlite3Fts3PutVarint(*pp, iWrite); |
| 1791 int n1, /* Size of buffer a1 */ | 2379 *piPrev = iVal; |
| 1792 char *a2, /* Buffer containing second doclist */ | 2380 *pbFirst = 1; |
| 1793 int n2, /* Size of buffer a2 */ | 2381 } |
| 1794 int *pnDoc /* OUT: Number of docids in output */ | 2382 |
| 2383 |
| 2384 /* |
| 2385 ** This macro is used by various functions that merge doclists. The two |
| 2386 ** arguments are 64-bit docid values. If the value of the stack variable |
| 2387 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2). |
| 2388 ** Otherwise, (i2-i1). |
| 2389 ** |
| 2390 ** Using this makes it easier to write code that can merge doclists that are |
| 2391 ** sorted in either ascending or descending order. |
| 2392 */ |
| 2393 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2)) |
| 2394 |
| 2395 /* |
| 2396 ** This function does an "OR" merge of two doclists (output contains all |
| 2397 ** positions contained in either argument doclist). If the docids in the |
| 2398 ** input doclists are sorted in ascending order, parameter bDescDoclist |
| 2399 ** should be false. If they are sorted in ascending order, it should be |
| 2400 ** passed a non-zero value. |
| 2401 ** |
| 2402 ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer |
| 2403 ** containing the output doclist and SQLITE_OK is returned. In this case |
| 2404 ** *pnOut is set to the number of bytes in the output doclist. |
| 2405 ** |
| 2406 ** If an error occurs, an SQLite error code is returned. The output values |
| 2407 ** are undefined in this case. |
| 2408 */ |
| 2409 static int fts3DoclistOrMerge( |
| 2410 int bDescDoclist, /* True if arguments are desc */ |
| 2411 char *a1, int n1, /* First doclist */ |
| 2412 char *a2, int n2, /* Second doclist */ |
| 2413 char **paOut, int *pnOut /* OUT: Malloc'd doclist */ |
| 1795 ){ | 2414 ){ |
| 1796 sqlite3_int64 i1 = 0; | 2415 sqlite3_int64 i1 = 0; |
| 1797 sqlite3_int64 i2 = 0; | 2416 sqlite3_int64 i2 = 0; |
| 1798 sqlite3_int64 iPrev = 0; | 2417 sqlite3_int64 iPrev = 0; |
| 1799 | 2418 char *pEnd1 = &a1[n1]; |
| 1800 char *p = aBuffer; | 2419 char *pEnd2 = &a2[n2]; |
| 1801 char *p1 = a1; | 2420 char *p1 = a1; |
| 1802 char *p2 = a2; | 2421 char *p2 = a2; |
| 1803 char *pEnd1 = &a1[n1]; | 2422 char *p; |
| 1804 char *pEnd2 = &a2[n2]; | 2423 char *aOut; |
| 1805 int nDoc = 0; | 2424 int bFirstOut = 0; |
| 1806 | 2425 |
| 1807 assert( mergetype==MERGE_OR || mergetype==MERGE_POS_OR | 2426 *paOut = 0; |
| 1808 || mergetype==MERGE_AND || mergetype==MERGE_NOT | 2427 *pnOut = 0; |
| 1809 || mergetype==MERGE_PHRASE || mergetype==MERGE_POS_PHRASE | 2428 |
| 1810 || mergetype==MERGE_NEAR || mergetype==MERGE_POS_NEAR | 2429 /* Allocate space for the output. Both the input and output doclists |
| 1811 ); | 2430 ** are delta encoded. If they are in ascending order (bDescDoclist==0), |
| 1812 | 2431 ** then the first docid in each list is simply encoded as a varint. For |
| 1813 if( !aBuffer ){ | 2432 ** each subsequent docid, the varint stored is the difference between the |
| 1814 *pnBuffer = 0; | 2433 ** current and previous docid (a positive number - since the list is in |
| 1815 return SQLITE_NOMEM; | 2434 ** ascending order). |
| 1816 } | 2435 ** |
| 1817 | 2436 ** The first docid written to the output is therefore encoded using the |
| 1818 /* Read the first docid from each doclist */ | 2437 ** same number of bytes as it is in whichever of the input lists it is |
| 1819 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | 2438 ** read from. And each subsequent docid read from the same input list |
| 1820 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | 2439 ** consumes either the same or less bytes as it did in the input (since |
| 1821 | 2440 ** the difference between it and the previous value in the output must |
| 1822 switch( mergetype ){ | 2441 ** be a positive value less than or equal to the delta value read from |
| 1823 case MERGE_OR: | 2442 ** the input list). The same argument applies to all but the first docid |
| 1824 case MERGE_POS_OR: | 2443 ** read from the 'other' list. And to the contents of all position lists |
| 1825 while( p1 || p2 ){ | 2444 ** that will be copied and merged from the input to the output. |
| 1826 if( p2 && p1 && i1==i2 ){ | 2445 ** |
| 1827 fts3PutDeltaVarint(&p, &iPrev, i1); | 2446 ** However, if the first docid copied to the output is a negative number, |
| 1828 if( mergetype==MERGE_POS_OR ) fts3PoslistMerge(&p, &p1, &p2); | 2447 ** then the encoding of the first docid from the 'other' input list may |
| 1829 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | 2448 ** be larger in the output than it was in the input (since the delta value |
| 1830 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | 2449 ** may be a larger positive integer than the actual docid). |
| 1831 }else if( !p2 || (p1 && i1<i2) ){ | 2450 ** |
| 1832 fts3PutDeltaVarint(&p, &iPrev, i1); | 2451 ** The space required to store the output is therefore the sum of the |
| 1833 if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p1); | 2452 ** sizes of the two inputs, plus enough space for exactly one of the input |
| 1834 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | 2453 ** docids to grow. |
| 1835 }else{ | 2454 ** |
| 1836 fts3PutDeltaVarint(&p, &iPrev, i2); | 2455 ** A symetric argument may be made if the doclists are in descending |
| 1837 if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p2); | 2456 ** order. |
| 1838 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | 2457 */ |
| 1839 } | 2458 aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1); |
| 2459 if( !aOut ) return SQLITE_NOMEM; |
| 2460 |
| 2461 p = aOut; |
| 2462 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1); |
| 2463 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2); |
| 2464 while( p1 || p2 ){ |
| 2465 sqlite3_int64 iDiff = DOCID_CMP(i1, i2); |
| 2466 |
| 2467 if( p2 && p1 && iDiff==0 ){ |
| 2468 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1); |
| 2469 fts3PoslistMerge(&p, &p1, &p2); |
| 2470 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); |
| 2471 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); |
| 2472 }else if( !p2 || (p1 && iDiff<0) ){ |
| 2473 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1); |
| 2474 fts3PoslistCopy(&p, &p1); |
| 2475 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); |
| 2476 }else{ |
| 2477 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2); |
| 2478 fts3PoslistCopy(&p, &p2); |
| 2479 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); |
| 2480 } |
| 2481 } |
| 2482 |
| 2483 *paOut = aOut; |
| 2484 *pnOut = (int)(p-aOut); |
| 2485 assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 ); |
| 2486 return SQLITE_OK; |
| 2487 } |
| 2488 |
| 2489 /* |
| 2490 ** This function does a "phrase" merge of two doclists. In a phrase merge, |
| 2491 ** the output contains a copy of each position from the right-hand input |
| 2492 ** doclist for which there is a position in the left-hand input doclist |
| 2493 ** exactly nDist tokens before it. |
| 2494 ** |
| 2495 ** If the docids in the input doclists are sorted in ascending order, |
| 2496 ** parameter bDescDoclist should be false. If they are sorted in ascending |
| 2497 ** order, it should be passed a non-zero value. |
| 2498 ** |
| 2499 ** The right-hand input doclist is overwritten by this function. |
| 2500 */ |
| 2501 static void fts3DoclistPhraseMerge( |
| 2502 int bDescDoclist, /* True if arguments are desc */ |
| 2503 int nDist, /* Distance from left to right (1=adjacent) */ |
| 2504 char *aLeft, int nLeft, /* Left doclist */ |
| 2505 char *aRight, int *pnRight /* IN/OUT: Right/output doclist */ |
| 2506 ){ |
| 2507 sqlite3_int64 i1 = 0; |
| 2508 sqlite3_int64 i2 = 0; |
| 2509 sqlite3_int64 iPrev = 0; |
| 2510 char *pEnd1 = &aLeft[nLeft]; |
| 2511 char *pEnd2 = &aRight[*pnRight]; |
| 2512 char *p1 = aLeft; |
| 2513 char *p2 = aRight; |
| 2514 char *p; |
| 2515 int bFirstOut = 0; |
| 2516 char *aOut = aRight; |
| 2517 |
| 2518 assert( nDist>0 ); |
| 2519 |
| 2520 p = aOut; |
| 2521 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1); |
| 2522 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2); |
| 2523 |
| 2524 while( p1 && p2 ){ |
| 2525 sqlite3_int64 iDiff = DOCID_CMP(i1, i2); |
| 2526 if( iDiff==0 ){ |
| 2527 char *pSave = p; |
| 2528 sqlite3_int64 iPrevSave = iPrev; |
| 2529 int bFirstOutSave = bFirstOut; |
| 2530 |
| 2531 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1); |
| 2532 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){ |
| 2533 p = pSave; |
| 2534 iPrev = iPrevSave; |
| 2535 bFirstOut = bFirstOutSave; |
| 1840 } | 2536 } |
| 1841 break; | 2537 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); |
| 1842 | 2538 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); |
| 1843 case MERGE_AND: | 2539 }else if( iDiff<0 ){ |
| 1844 while( p1 && p2 ){ | 2540 fts3PoslistCopy(0, &p1); |
| 1845 if( i1==i2 ){ | 2541 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); |
| 1846 fts3PutDeltaVarint(&p, &iPrev, i1); | 2542 }else{ |
| 1847 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | 2543 fts3PoslistCopy(0, &p2); |
| 1848 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | 2544 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); |
| 1849 nDoc++; | 2545 } |
| 1850 }else if( i1<i2 ){ | 2546 } |
| 1851 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | 2547 |
| 1852 }else{ | 2548 *pnRight = (int)(p - aOut); |
| 1853 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | 2549 } |
| 1854 } | 2550 |
| 2551 /* |
| 2552 ** Argument pList points to a position list nList bytes in size. This |
| 2553 ** function checks to see if the position list contains any entries for |
| 2554 ** a token in position 0 (of any column). If so, it writes argument iDelta |
| 2555 ** to the output buffer pOut, followed by a position list consisting only |
| 2556 ** of the entries from pList at position 0, and terminated by an 0x00 byte. |
| 2557 ** The value returned is the number of bytes written to pOut (if any). |
| 2558 */ |
| 2559 int sqlite3Fts3FirstFilter( |
| 2560 sqlite3_int64 iDelta, /* Varint that may be written to pOut */ |
| 2561 char *pList, /* Position list (no 0x00 term) */ |
| 2562 int nList, /* Size of pList in bytes */ |
| 2563 char *pOut /* Write output here */ |
| 2564 ){ |
| 2565 int nOut = 0; |
| 2566 int bWritten = 0; /* True once iDelta has been written */ |
| 2567 char *p = pList; |
| 2568 char *pEnd = &pList[nList]; |
| 2569 |
| 2570 if( *p!=0x01 ){ |
| 2571 if( *p==0x02 ){ |
| 2572 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta); |
| 2573 pOut[nOut++] = 0x02; |
| 2574 bWritten = 1; |
| 2575 } |
| 2576 fts3ColumnlistCopy(0, &p); |
| 2577 } |
| 2578 |
| 2579 while( p<pEnd && *p==0x01 ){ |
| 2580 sqlite3_int64 iCol; |
| 2581 p++; |
| 2582 p += sqlite3Fts3GetVarint(p, &iCol); |
| 2583 if( *p==0x02 ){ |
| 2584 if( bWritten==0 ){ |
| 2585 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta); |
| 2586 bWritten = 1; |
| 1855 } | 2587 } |
| 1856 break; | 2588 pOut[nOut++] = 0x01; |
| 1857 | 2589 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol); |
| 1858 case MERGE_NOT: | 2590 pOut[nOut++] = 0x02; |
| 1859 while( p1 ){ | 2591 } |
| 1860 if( p2 && i1==i2 ){ | 2592 fts3ColumnlistCopy(0, &p); |
| 1861 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | 2593 } |
| 1862 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | 2594 if( bWritten ){ |
| 1863 }else if( !p2 || i1<i2 ){ | 2595 pOut[nOut++] = 0x00; |
| 1864 fts3PutDeltaVarint(&p, &iPrev, i1); | 2596 } |
| 1865 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | 2597 |
| 1866 }else{ | 2598 return nOut; |
| 1867 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | 2599 } |
| 1868 } | 2600 |
| 1869 } | |
| 1870 break; | |
| 1871 | |
| 1872 case MERGE_POS_PHRASE: | |
| 1873 case MERGE_PHRASE: { | |
| 1874 char **ppPos = (mergetype==MERGE_PHRASE ? 0 : &p); | |
| 1875 while( p1 && p2 ){ | |
| 1876 if( i1==i2 ){ | |
| 1877 char *pSave = p; | |
| 1878 sqlite3_int64 iPrevSave = iPrev; | |
| 1879 fts3PutDeltaVarint(&p, &iPrev, i1); | |
| 1880 if( 0==fts3PoslistPhraseMerge(ppPos, nParam1, 0, 1, &p1, &p2) ){ | |
| 1881 p = pSave; | |
| 1882 iPrev = iPrevSave; | |
| 1883 }else{ | |
| 1884 nDoc++; | |
| 1885 } | |
| 1886 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | |
| 1887 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | |
| 1888 }else if( i1<i2 ){ | |
| 1889 fts3PoslistCopy(0, &p1); | |
| 1890 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | |
| 1891 }else{ | |
| 1892 fts3PoslistCopy(0, &p2); | |
| 1893 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | |
| 1894 } | |
| 1895 } | |
| 1896 break; | |
| 1897 } | |
| 1898 | |
| 1899 default: assert( mergetype==MERGE_POS_NEAR || mergetype==MERGE_NEAR ); { | |
| 1900 char *aTmp = 0; | |
| 1901 char **ppPos = 0; | |
| 1902 | |
| 1903 if( mergetype==MERGE_POS_NEAR ){ | |
| 1904 ppPos = &p; | |
| 1905 aTmp = sqlite3_malloc(2*(n1+n2+1)); | |
| 1906 if( !aTmp ){ | |
| 1907 return SQLITE_NOMEM; | |
| 1908 } | |
| 1909 } | |
| 1910 | |
| 1911 while( p1 && p2 ){ | |
| 1912 if( i1==i2 ){ | |
| 1913 char *pSave = p; | |
| 1914 sqlite3_int64 iPrevSave = iPrev; | |
| 1915 fts3PutDeltaVarint(&p, &iPrev, i1); | |
| 1916 | |
| 1917 if( !fts3PoslistNearMerge(ppPos, aTmp, nParam1, nParam2, &p1, &p2) ){ | |
| 1918 iPrev = iPrevSave; | |
| 1919 p = pSave; | |
| 1920 } | |
| 1921 | |
| 1922 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | |
| 1923 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | |
| 1924 }else if( i1<i2 ){ | |
| 1925 fts3PoslistCopy(0, &p1); | |
| 1926 fts3GetDeltaVarint2(&p1, pEnd1, &i1); | |
| 1927 }else{ | |
| 1928 fts3PoslistCopy(0, &p2); | |
| 1929 fts3GetDeltaVarint2(&p2, pEnd2, &i2); | |
| 1930 } | |
| 1931 } | |
| 1932 sqlite3_free(aTmp); | |
| 1933 break; | |
| 1934 } | |
| 1935 } | |
| 1936 | |
| 1937 if( pnDoc ) *pnDoc = nDoc; | |
| 1938 *pnBuffer = (int)(p-aBuffer); | |
| 1939 return SQLITE_OK; | |
| 1940 } | |
| 1941 | |
| 1942 /* | |
| 1943 ** A pointer to an instance of this structure is used as the context | |
| 1944 ** argument to sqlite3Fts3SegReaderIterate() | |
| 1945 */ | |
| 1946 typedef struct TermSelect TermSelect; | |
| 1947 struct TermSelect { | |
| 1948 int isReqPos; | |
| 1949 char *aaOutput[16]; /* Malloc'd output buffer */ | |
| 1950 int anOutput[16]; /* Size of output in bytes */ | |
| 1951 }; | |
| 1952 | 2601 |
| 1953 /* | 2602 /* |
| 1954 ** Merge all doclists in the TermSelect.aaOutput[] array into a single | 2603 ** Merge all doclists in the TermSelect.aaOutput[] array into a single |
| 1955 ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all | 2604 ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all |
| 1956 ** other doclists (except the aaOutput[0] one) and return SQLITE_OK. | 2605 ** other doclists (except the aaOutput[0] one) and return SQLITE_OK. |
| 1957 ** | 2606 ** |
| 1958 ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is | 2607 ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is |
| 1959 ** the responsibility of the caller to free any doclists left in the | 2608 ** the responsibility of the caller to free any doclists left in the |
| 1960 ** TermSelect.aaOutput[] array. | 2609 ** TermSelect.aaOutput[] array. |
| 1961 */ | 2610 */ |
| 1962 static int fts3TermSelectMerge(TermSelect *pTS){ | 2611 static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){ |
| 1963 int mergetype = (pTS->isReqPos ? MERGE_POS_OR : MERGE_OR); | |
| 1964 char *aOut = 0; | 2612 char *aOut = 0; |
| 1965 int nOut = 0; | 2613 int nOut = 0; |
| 1966 int i; | 2614 int i; |
| 1967 | 2615 |
| 1968 /* Loop through the doclists in the aaOutput[] array. Merge them all | 2616 /* Loop through the doclists in the aaOutput[] array. Merge them all |
| 1969 ** into a single doclist. | 2617 ** into a single doclist. |
| 1970 */ | 2618 */ |
| 1971 for(i=0; i<SizeofArray(pTS->aaOutput); i++){ | 2619 for(i=0; i<SizeofArray(pTS->aaOutput); i++){ |
| 1972 if( pTS->aaOutput[i] ){ | 2620 if( pTS->aaOutput[i] ){ |
| 1973 if( !aOut ){ | 2621 if( !aOut ){ |
| 1974 aOut = pTS->aaOutput[i]; | 2622 aOut = pTS->aaOutput[i]; |
| 1975 nOut = pTS->anOutput[i]; | 2623 nOut = pTS->anOutput[i]; |
| 1976 pTS->aaOutput[i] = 0; | 2624 pTS->aaOutput[i] = 0; |
| 1977 }else{ | 2625 }else{ |
| 1978 int nNew = nOut + pTS->anOutput[i]; | 2626 int nNew; |
| 1979 char *aNew = sqlite3_malloc(nNew); | 2627 char *aNew; |
| 1980 if( !aNew ){ | 2628 |
| 2629 int rc = fts3DoclistOrMerge(p->bDescIdx, |
| 2630 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew |
| 2631 ); |
| 2632 if( rc!=SQLITE_OK ){ |
| 1981 sqlite3_free(aOut); | 2633 sqlite3_free(aOut); |
| 1982 return SQLITE_NOMEM; | 2634 return rc; |
| 1983 } | 2635 } |
| 1984 fts3DoclistMerge(mergetype, 0, 0, | 2636 |
| 1985 aNew, &nNew, pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, 0 | |
| 1986 ); | |
| 1987 sqlite3_free(pTS->aaOutput[i]); | 2637 sqlite3_free(pTS->aaOutput[i]); |
| 1988 sqlite3_free(aOut); | 2638 sqlite3_free(aOut); |
| 1989 pTS->aaOutput[i] = 0; | 2639 pTS->aaOutput[i] = 0; |
| 1990 aOut = aNew; | 2640 aOut = aNew; |
| 1991 nOut = nNew; | 2641 nOut = nNew; |
| 1992 } | 2642 } |
| 1993 } | 2643 } |
| 1994 } | 2644 } |
| 1995 | 2645 |
| 1996 pTS->aaOutput[0] = aOut; | 2646 pTS->aaOutput[0] = aOut; |
| 1997 pTS->anOutput[0] = nOut; | 2647 pTS->anOutput[0] = nOut; |
| 1998 return SQLITE_OK; | 2648 return SQLITE_OK; |
| 1999 } | 2649 } |
| 2000 | 2650 |
| 2001 /* | 2651 /* |
| 2002 ** This function is used as the sqlite3Fts3SegReaderIterate() callback when | 2652 ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed |
| 2003 ** querying the full-text index for a doclist associated with a term or | 2653 ** as the first argument. The merge is an "OR" merge (see function |
| 2004 ** term-prefix. | 2654 ** fts3DoclistOrMerge() for details). |
| 2655 ** |
| 2656 ** This function is called with the doclist for each term that matches |
| 2657 ** a queried prefix. It merges all these doclists into one, the doclist |
| 2658 ** for the specified prefix. Since there can be a very large number of |
| 2659 ** doclists to merge, the merging is done pair-wise using the TermSelect |
| 2660 ** object. |
| 2661 ** |
| 2662 ** This function returns SQLITE_OK if the merge is successful, or an |
| 2663 ** SQLite error code (SQLITE_NOMEM) if an error occurs. |
| 2005 */ | 2664 */ |
| 2006 static int fts3TermSelectCb( | 2665 static int fts3TermSelectMerge( |
| 2007 Fts3Table *p, /* Virtual table object */ | 2666 Fts3Table *p, /* FTS table handle */ |
| 2008 void *pContext, /* Pointer to TermSelect structure */ | 2667 TermSelect *pTS, /* TermSelect object to merge into */ |
| 2009 char *zTerm, | 2668 char *aDoclist, /* Pointer to doclist */ |
| 2010 int nTerm, | 2669 int nDoclist /* Size of aDoclist in bytes */ |
| 2011 char *aDoclist, | |
| 2012 int nDoclist | |
| 2013 ){ | 2670 ){ |
| 2014 TermSelect *pTS = (TermSelect *)pContext; | |
| 2015 | |
| 2016 UNUSED_PARAMETER(p); | |
| 2017 UNUSED_PARAMETER(zTerm); | |
| 2018 UNUSED_PARAMETER(nTerm); | |
| 2019 | |
| 2020 if( pTS->aaOutput[0]==0 ){ | 2671 if( pTS->aaOutput[0]==0 ){ |
| 2021 /* If this is the first term selected, copy the doclist to the output | 2672 /* If this is the first term selected, copy the doclist to the output |
| 2022 ** buffer using memcpy(). TODO: Add a way to transfer control of the | 2673 ** buffer using memcpy(). */ |
| 2023 ** aDoclist buffer from the caller so as to avoid the memcpy(). | |
| 2024 */ | |
| 2025 pTS->aaOutput[0] = sqlite3_malloc(nDoclist); | 2674 pTS->aaOutput[0] = sqlite3_malloc(nDoclist); |
| 2026 pTS->anOutput[0] = nDoclist; | 2675 pTS->anOutput[0] = nDoclist; |
| 2027 if( pTS->aaOutput[0] ){ | 2676 if( pTS->aaOutput[0] ){ |
| 2028 memcpy(pTS->aaOutput[0], aDoclist, nDoclist); | 2677 memcpy(pTS->aaOutput[0], aDoclist, nDoclist); |
| 2029 }else{ | 2678 }else{ |
| 2030 return SQLITE_NOMEM; | 2679 return SQLITE_NOMEM; |
| 2031 } | 2680 } |
| 2032 }else{ | 2681 }else{ |
| 2033 int mergetype = (pTS->isReqPos ? MERGE_POS_OR : MERGE_OR); | |
| 2034 char *aMerge = aDoclist; | 2682 char *aMerge = aDoclist; |
| 2035 int nMerge = nDoclist; | 2683 int nMerge = nDoclist; |
| 2036 int iOut; | 2684 int iOut; |
| 2037 | 2685 |
| 2038 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){ | 2686 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){ |
| 2039 char *aNew; | |
| 2040 int nNew; | |
| 2041 if( pTS->aaOutput[iOut]==0 ){ | 2687 if( pTS->aaOutput[iOut]==0 ){ |
| 2042 assert( iOut>0 ); | 2688 assert( iOut>0 ); |
| 2043 pTS->aaOutput[iOut] = aMerge; | 2689 pTS->aaOutput[iOut] = aMerge; |
| 2044 pTS->anOutput[iOut] = nMerge; | 2690 pTS->anOutput[iOut] = nMerge; |
| 2045 break; | 2691 break; |
| 2046 } | 2692 }else{ |
| 2693 char *aNew; |
| 2694 int nNew; |
| 2047 | 2695 |
| 2048 nNew = nMerge + pTS->anOutput[iOut]; | 2696 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge, |
| 2049 aNew = sqlite3_malloc(nNew); | 2697 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew |
| 2050 if( !aNew ){ | 2698 ); |
| 2051 if( aMerge!=aDoclist ){ | 2699 if( rc!=SQLITE_OK ){ |
| 2052 sqlite3_free(aMerge); | 2700 if( aMerge!=aDoclist ) sqlite3_free(aMerge); |
| 2701 return rc; |
| 2053 } | 2702 } |
| 2054 return SQLITE_NOMEM; | |
| 2055 } | |
| 2056 fts3DoclistMerge(mergetype, 0, 0, aNew, &nNew, | |
| 2057 pTS->aaOutput[iOut], pTS->anOutput[iOut], aMerge, nMerge, 0 | |
| 2058 ); | |
| 2059 | 2703 |
| 2060 if( iOut>0 ) sqlite3_free(aMerge); | 2704 if( aMerge!=aDoclist ) sqlite3_free(aMerge); |
| 2061 sqlite3_free(pTS->aaOutput[iOut]); | 2705 sqlite3_free(pTS->aaOutput[iOut]); |
| 2062 pTS->aaOutput[iOut] = 0; | 2706 pTS->aaOutput[iOut] = 0; |
| 2063 | 2707 |
| 2064 aMerge = aNew; | 2708 aMerge = aNew; |
| 2065 nMerge = nNew; | 2709 nMerge = nNew; |
| 2066 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){ | 2710 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){ |
| 2067 pTS->aaOutput[iOut] = aMerge; | 2711 pTS->aaOutput[iOut] = aMerge; |
| 2068 pTS->anOutput[iOut] = nMerge; | 2712 pTS->anOutput[iOut] = nMerge; |
| 2713 } |
| 2069 } | 2714 } |
| 2070 } | 2715 } |
| 2071 } | 2716 } |
| 2072 return SQLITE_OK; | 2717 return SQLITE_OK; |
| 2073 } | 2718 } |
| 2074 | 2719 |
| 2075 static int fts3DeferredTermSelect( | 2720 /* |
| 2076 Fts3DeferredToken *pToken, /* Phrase token */ | 2721 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array. |
| 2077 int isTermPos, /* True to include positions */ | 2722 */ |
| 2078 int *pnOut, /* OUT: Size of list */ | 2723 static int fts3SegReaderCursorAppend( |
| 2079 char **ppOut /* OUT: Body of list */ | 2724 Fts3MultiSegReader *pCsr, |
| 2725 Fts3SegReader *pNew |
| 2080 ){ | 2726 ){ |
| 2081 char *aSource; | 2727 if( (pCsr->nSegment%16)==0 ){ |
| 2082 int nSource; | 2728 Fts3SegReader **apNew; |
| 2083 | 2729 int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*); |
| 2084 aSource = sqlite3Fts3DeferredDoclist(pToken, &nSource); | 2730 apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte); |
| 2085 if( !aSource ){ | 2731 if( !apNew ){ |
| 2086 *pnOut = 0; | 2732 sqlite3Fts3SegReaderFree(pNew); |
| 2087 *ppOut = 0; | 2733 return SQLITE_NOMEM; |
| 2088 }else if( isTermPos ){ | 2734 } |
| 2089 *ppOut = sqlite3_malloc(nSource); | 2735 pCsr->apSegment = apNew; |
| 2090 if( !*ppOut ) return SQLITE_NOMEM; | |
| 2091 memcpy(*ppOut, aSource, nSource); | |
| 2092 *pnOut = nSource; | |
| 2093 }else{ | |
| 2094 sqlite3_int64 docid; | |
| 2095 *pnOut = sqlite3Fts3GetVarint(aSource, &docid); | |
| 2096 *ppOut = sqlite3_malloc(*pnOut); | |
| 2097 if( !*ppOut ) return SQLITE_NOMEM; | |
| 2098 sqlite3Fts3PutVarint(*ppOut, docid); | |
| 2099 } | 2736 } |
| 2100 | 2737 pCsr->apSegment[pCsr->nSegment++] = pNew; |
| 2101 return SQLITE_OK; | 2738 return SQLITE_OK; |
| 2102 } | 2739 } |
| 2103 | 2740 |
| 2104 int sqlite3Fts3SegReaderCursor( | 2741 /* |
| 2742 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the |
| 2743 ** 8th argument. |
| 2744 ** |
| 2745 ** This function returns SQLITE_OK if successful, or an SQLite error code |
| 2746 ** otherwise. |
| 2747 */ |
| 2748 static int fts3SegReaderCursor( |
| 2105 Fts3Table *p, /* FTS3 table handle */ | 2749 Fts3Table *p, /* FTS3 table handle */ |
| 2750 int iLangid, /* Language id */ |
| 2751 int iIndex, /* Index to search (from 0 to p->nIndex-1) */ |
| 2106 int iLevel, /* Level of segments to scan */ | 2752 int iLevel, /* Level of segments to scan */ |
| 2107 const char *zTerm, /* Term to query for */ | 2753 const char *zTerm, /* Term to query for */ |
| 2108 int nTerm, /* Size of zTerm in bytes */ | 2754 int nTerm, /* Size of zTerm in bytes */ |
| 2109 int isPrefix, /* True for a prefix search */ | 2755 int isPrefix, /* True for a prefix search */ |
| 2110 int isScan, /* True to scan from zTerm to EOF */ | 2756 int isScan, /* True to scan from zTerm to EOF */ |
| 2111 Fts3SegReaderCursor *pCsr /* Cursor object to populate */ | 2757 Fts3MultiSegReader *pCsr /* Cursor object to populate */ |
| 2112 ){ | 2758 ){ |
| 2113 int rc = SQLITE_OK; | 2759 int rc = SQLITE_OK; /* Error code */ |
| 2114 int rc2; | 2760 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */ |
| 2115 int iAge = 0; | 2761 int rc2; /* Result of sqlite3_reset() */ |
| 2116 sqlite3_stmt *pStmt = 0; | |
| 2117 Fts3SegReader *pPending = 0; | |
| 2118 | 2762 |
| 2119 assert( iLevel==FTS3_SEGCURSOR_ALL | 2763 /* If iLevel is less than 0 and this is not a scan, include a seg-reader |
| 2120 || iLevel==FTS3_SEGCURSOR_PENDING | 2764 ** for the pending-terms. If this is a scan, then this call must be being |
| 2121 || iLevel>=0 | 2765 ** made by an fts4aux module, not an FTS table. In this case calling |
| 2122 ); | 2766 ** Fts3SegReaderPending might segfault, as the data structures used by |
| 2123 assert( FTS3_SEGCURSOR_PENDING<0 ); | 2767 ** fts4aux are not completely populated. So it's easiest to filter these |
| 2124 assert( FTS3_SEGCURSOR_ALL<0 ); | 2768 ** calls out here. */ |
| 2125 assert( iLevel==FTS3_SEGCURSOR_ALL || (zTerm==0 && isPrefix==1) ); | 2769 if( iLevel<0 && p->aIndex ){ |
| 2126 assert( isPrefix==0 || isScan==0 ); | 2770 Fts3SegReader *pSeg = 0; |
| 2127 | 2771 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg); |
| 2128 | 2772 if( rc==SQLITE_OK && pSeg ){ |
| 2129 memset(pCsr, 0, sizeof(Fts3SegReaderCursor)); | 2773 rc = fts3SegReaderCursorAppend(pCsr, pSeg); |
| 2130 | |
| 2131 /* If iLevel is less than 0, include a seg-reader for the pending-terms. */ | |
| 2132 assert( isScan==0 || fts3HashCount(&p->pendingTerms)==0 ); | |
| 2133 if( iLevel<0 && isScan==0 ){ | |
| 2134 rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pPending); | |
| 2135 if( rc==SQLITE_OK && pPending ){ | |
| 2136 int nByte = (sizeof(Fts3SegReader *) * 16); | |
| 2137 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte); | |
| 2138 if( pCsr->apSegment==0 ){ | |
| 2139 rc = SQLITE_NOMEM; | |
| 2140 }else{ | |
| 2141 pCsr->apSegment[0] = pPending; | |
| 2142 pCsr->nSegment = 1; | |
| 2143 pPending = 0; | |
| 2144 } | |
| 2145 } | 2774 } |
| 2146 } | 2775 } |
| 2147 | 2776 |
| 2148 if( iLevel!=FTS3_SEGCURSOR_PENDING ){ | 2777 if( iLevel!=FTS3_SEGCURSOR_PENDING ){ |
| 2149 if( rc==SQLITE_OK ){ | 2778 if( rc==SQLITE_OK ){ |
| 2150 rc = sqlite3Fts3AllSegdirs(p, iLevel, &pStmt); | 2779 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt); |
| 2151 } | 2780 } |
| 2781 |
| 2152 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ | 2782 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ |
| 2783 Fts3SegReader *pSeg = 0; |
| 2153 | 2784 |
| 2154 /* Read the values returned by the SELECT into local variables. */ | 2785 /* Read the values returned by the SELECT into local variables. */ |
| 2155 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1); | 2786 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1); |
| 2156 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2); | 2787 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2); |
| 2157 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3); | 2788 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3); |
| 2158 int nRoot = sqlite3_column_bytes(pStmt, 4); | 2789 int nRoot = sqlite3_column_bytes(pStmt, 4); |
| 2159 char const *zRoot = sqlite3_column_blob(pStmt, 4); | 2790 char const *zRoot = sqlite3_column_blob(pStmt, 4); |
| 2160 | 2791 |
| 2161 /* If nSegment is a multiple of 16 the array needs to be extended. */ | |
| 2162 if( (pCsr->nSegment%16)==0 ){ | |
| 2163 Fts3SegReader **apNew; | |
| 2164 int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*); | |
| 2165 apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte); | |
| 2166 if( !apNew ){ | |
| 2167 rc = SQLITE_NOMEM; | |
| 2168 goto finished; | |
| 2169 } | |
| 2170 pCsr->apSegment = apNew; | |
| 2171 } | |
| 2172 | |
| 2173 /* If zTerm is not NULL, and this segment is not stored entirely on its | 2792 /* If zTerm is not NULL, and this segment is not stored entirely on its |
| 2174 ** root node, the range of leaves scanned can be reduced. Do this. */ | 2793 ** root node, the range of leaves scanned can be reduced. Do this. */ |
| 2175 if( iStartBlock && zTerm ){ | 2794 if( iStartBlock && zTerm ){ |
| 2176 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0); | 2795 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0); |
| 2177 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi); | 2796 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi); |
| 2178 if( rc!=SQLITE_OK ) goto finished; | 2797 if( rc!=SQLITE_OK ) goto finished; |
| 2179 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock; | 2798 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock; |
| 2180 } | 2799 } |
| 2181 | 2800 |
| 2182 rc = sqlite3Fts3SegReaderNew(iAge, iStartBlock, iLeavesEndBlock, | 2801 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1, |
| 2183 iEndBlock, zRoot, nRoot, &pCsr->apSegment[pCsr->nSegment] | 2802 (isPrefix==0 && isScan==0), |
| 2803 iStartBlock, iLeavesEndBlock, |
| 2804 iEndBlock, zRoot, nRoot, &pSeg |
| 2184 ); | 2805 ); |
| 2185 if( rc!=SQLITE_OK ) goto finished; | 2806 if( rc!=SQLITE_OK ) goto finished; |
| 2186 pCsr->nSegment++; | 2807 rc = fts3SegReaderCursorAppend(pCsr, pSeg); |
| 2187 iAge++; | |
| 2188 } | 2808 } |
| 2189 } | 2809 } |
| 2190 | 2810 |
| 2191 finished: | 2811 finished: |
| 2192 rc2 = sqlite3_reset(pStmt); | 2812 rc2 = sqlite3_reset(pStmt); |
| 2193 if( rc==SQLITE_DONE ) rc = rc2; | 2813 if( rc==SQLITE_DONE ) rc = rc2; |
| 2194 sqlite3Fts3SegReaderFree(pPending); | |
| 2195 | 2814 |
| 2196 return rc; | 2815 return rc; |
| 2197 } | 2816 } |
| 2198 | 2817 |
| 2818 /* |
| 2819 ** Set up a cursor object for iterating through a full-text index or a |
| 2820 ** single level therein. |
| 2821 */ |
| 2822 int sqlite3Fts3SegReaderCursor( |
| 2823 Fts3Table *p, /* FTS3 table handle */ |
| 2824 int iLangid, /* Language-id to search */ |
| 2825 int iIndex, /* Index to search (from 0 to p->nIndex-1) */ |
| 2826 int iLevel, /* Level of segments to scan */ |
| 2827 const char *zTerm, /* Term to query for */ |
| 2828 int nTerm, /* Size of zTerm in bytes */ |
| 2829 int isPrefix, /* True for a prefix search */ |
| 2830 int isScan, /* True to scan from zTerm to EOF */ |
| 2831 Fts3MultiSegReader *pCsr /* Cursor object to populate */ |
| 2832 ){ |
| 2833 assert( iIndex>=0 && iIndex<p->nIndex ); |
| 2834 assert( iLevel==FTS3_SEGCURSOR_ALL |
| 2835 || iLevel==FTS3_SEGCURSOR_PENDING |
| 2836 || iLevel>=0 |
| 2837 ); |
| 2838 assert( iLevel<FTS3_SEGDIR_MAXLEVEL ); |
| 2839 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 ); |
| 2840 assert( isPrefix==0 || isScan==0 ); |
| 2199 | 2841 |
| 2842 memset(pCsr, 0, sizeof(Fts3MultiSegReader)); |
| 2843 return fts3SegReaderCursor( |
| 2844 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr |
| 2845 ); |
| 2846 } |
| 2847 |
| 2848 /* |
| 2849 ** In addition to its current configuration, have the Fts3MultiSegReader |
| 2850 ** passed as the 4th argument also scan the doclist for term zTerm/nTerm. |
| 2851 ** |
| 2852 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. |
| 2853 */ |
| 2854 static int fts3SegReaderCursorAddZero( |
| 2855 Fts3Table *p, /* FTS virtual table handle */ |
| 2856 int iLangid, |
| 2857 const char *zTerm, /* Term to scan doclist of */ |
| 2858 int nTerm, /* Number of bytes in zTerm */ |
| 2859 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */ |
| 2860 ){ |
| 2861 return fts3SegReaderCursor(p, |
| 2862 iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr |
| 2863 ); |
| 2864 } |
| 2865 |
| 2866 /* |
| 2867 ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or, |
| 2868 ** if isPrefix is true, to scan the doclist for all terms for which |
| 2869 ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write |
| 2870 ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return |
| 2871 ** an SQLite error code. |
| 2872 ** |
| 2873 ** It is the responsibility of the caller to free this object by eventually |
| 2874 ** passing it to fts3SegReaderCursorFree() |
| 2875 ** |
| 2876 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. |
| 2877 ** Output parameter *ppSegcsr is set to 0 if an error occurs. |
| 2878 */ |
| 2200 static int fts3TermSegReaderCursor( | 2879 static int fts3TermSegReaderCursor( |
| 2201 Fts3Cursor *pCsr, /* Virtual table cursor handle */ | 2880 Fts3Cursor *pCsr, /* Virtual table cursor handle */ |
| 2202 const char *zTerm, /* Term to query for */ | 2881 const char *zTerm, /* Term to query for */ |
| 2203 int nTerm, /* Size of zTerm in bytes */ | 2882 int nTerm, /* Size of zTerm in bytes */ |
| 2204 int isPrefix, /* True for a prefix search */ | 2883 int isPrefix, /* True for a prefix search */ |
| 2205 Fts3SegReaderCursor **ppSegcsr /* OUT: Allocated seg-reader cursor */ | 2884 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */ |
| 2206 ){ | 2885 ){ |
| 2207 Fts3SegReaderCursor *pSegcsr; /* Object to allocate and return */ | 2886 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */ |
| 2208 int rc = SQLITE_NOMEM; /* Return code */ | 2887 int rc = SQLITE_NOMEM; /* Return code */ |
| 2209 | 2888 |
| 2210 pSegcsr = sqlite3_malloc(sizeof(Fts3SegReaderCursor)); | 2889 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader)); |
| 2211 if( pSegcsr ){ | 2890 if( pSegcsr ){ |
| 2891 int i; |
| 2892 int bFound = 0; /* True once an index has been found */ |
| 2212 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; | 2893 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; |
| 2213 int i; | 2894 |
| 2214 int nCost = 0; | 2895 if( isPrefix ){ |
| 2215 rc = sqlite3Fts3SegReaderCursor( | 2896 for(i=1; bFound==0 && i<p->nIndex; i++){ |
| 2216 p, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr); | 2897 if( p->aIndex[i].nPrefix==nTerm ){ |
| 2217 | 2898 bFound = 1; |
| 2218 for(i=0; rc==SQLITE_OK && i<pSegcsr->nSegment; i++){ | 2899 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, |
| 2219 rc = sqlite3Fts3SegReaderCost(pCsr, pSegcsr->apSegment[i], &nCost); | 2900 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr |
| 2901 ); |
| 2902 pSegcsr->bLookup = 1; |
| 2903 } |
| 2904 } |
| 2905 |
| 2906 for(i=1; bFound==0 && i<p->nIndex; i++){ |
| 2907 if( p->aIndex[i].nPrefix==nTerm+1 ){ |
| 2908 bFound = 1; |
| 2909 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, |
| 2910 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr |
| 2911 ); |
| 2912 if( rc==SQLITE_OK ){ |
| 2913 rc = fts3SegReaderCursorAddZero( |
| 2914 p, pCsr->iLangid, zTerm, nTerm, pSegcsr |
| 2915 ); |
| 2916 } |
| 2917 } |
| 2918 } |
| 2220 } | 2919 } |
| 2221 pSegcsr->nCost = nCost; | 2920 |
| 2921 if( bFound==0 ){ |
| 2922 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, |
| 2923 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr |
| 2924 ); |
| 2925 pSegcsr->bLookup = !isPrefix; |
| 2926 } |
| 2222 } | 2927 } |
| 2223 | 2928 |
| 2224 *ppSegcsr = pSegcsr; | 2929 *ppSegcsr = pSegcsr; |
| 2225 return rc; | 2930 return rc; |
| 2226 } | 2931 } |
| 2227 | 2932 |
| 2228 static void fts3SegReaderCursorFree(Fts3SegReaderCursor *pSegcsr){ | 2933 /* |
| 2934 ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor(). |
| 2935 */ |
| 2936 static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){ |
| 2229 sqlite3Fts3SegReaderFinish(pSegcsr); | 2937 sqlite3Fts3SegReaderFinish(pSegcsr); |
| 2230 sqlite3_free(pSegcsr); | 2938 sqlite3_free(pSegcsr); |
| 2231 } | 2939 } |
| 2232 | 2940 |
| 2233 /* | 2941 /* |
| 2234 ** This function retreives the doclist for the specified term (or term | 2942 ** This function retrieves the doclist for the specified term (or term |
| 2235 ** prefix) from the database. | 2943 ** prefix) from the database. |
| 2236 ** | |
| 2237 ** The returned doclist may be in one of two formats, depending on the | |
| 2238 ** value of parameter isReqPos. If isReqPos is zero, then the doclist is | |
| 2239 ** a sorted list of delta-compressed docids (a bare doclist). If isReqPos | |
| 2240 ** is non-zero, then the returned list is in the same format as is stored | |
| 2241 ** in the database without the found length specifier at the start of on-disk | |
| 2242 ** doclists. | |
| 2243 */ | 2944 */ |
| 2244 static int fts3TermSelect( | 2945 static int fts3TermSelect( |
| 2245 Fts3Table *p, /* Virtual table handle */ | 2946 Fts3Table *p, /* Virtual table handle */ |
| 2246 Fts3PhraseToken *pTok, /* Token to query for */ | 2947 Fts3PhraseToken *pTok, /* Token to query for */ |
| 2247 int iColumn, /* Column to query (or -ve for all columns) */ | 2948 int iColumn, /* Column to query (or -ve for all columns) */ |
| 2248 int isReqPos, /* True to include position lists in output */ | |
| 2249 int *pnOut, /* OUT: Size of buffer at *ppOut */ | 2949 int *pnOut, /* OUT: Size of buffer at *ppOut */ |
| 2250 char **ppOut /* OUT: Malloced result buffer */ | 2950 char **ppOut /* OUT: Malloced result buffer */ |
| 2251 ){ | 2951 ){ |
| 2252 int rc; /* Return code */ | 2952 int rc; /* Return code */ |
| 2253 Fts3SegReaderCursor *pSegcsr; /* Seg-reader cursor for this term */ | 2953 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */ |
| 2254 TermSelect tsc; /* Context object for fts3TermSelectCb() */ | 2954 TermSelect tsc; /* Object for pair-wise doclist merging */ |
| 2255 Fts3SegFilter filter; /* Segment term filter configuration */ | 2955 Fts3SegFilter filter; /* Segment term filter configuration */ |
| 2256 | 2956 |
| 2257 pSegcsr = pTok->pSegcsr; | 2957 pSegcsr = pTok->pSegcsr; |
| 2258 memset(&tsc, 0, sizeof(TermSelect)); | 2958 memset(&tsc, 0, sizeof(TermSelect)); |
| 2259 tsc.isReqPos = isReqPos; | |
| 2260 | 2959 |
| 2261 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | 2960 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS |
| 2262 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0) | 2961 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0) |
| 2263 | (isReqPos ? FTS3_SEGMENT_REQUIRE_POS : 0) | 2962 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0) |
| 2264 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0); | 2963 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0); |
| 2265 filter.iCol = iColumn; | 2964 filter.iCol = iColumn; |
| 2266 filter.zTerm = pTok->z; | 2965 filter.zTerm = pTok->z; |
| 2267 filter.nTerm = pTok->n; | 2966 filter.nTerm = pTok->n; |
| 2268 | 2967 |
| 2269 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter); | 2968 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter); |
| 2270 while( SQLITE_OK==rc | 2969 while( SQLITE_OK==rc |
| 2271 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) | 2970 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) |
| 2272 ){ | 2971 ){ |
| 2273 rc = fts3TermSelectCb(p, (void *)&tsc, | 2972 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist); |
| 2274 pSegcsr->zTerm, pSegcsr->nTerm, pSegcsr->aDoclist, pSegcsr->nDoclist | |
| 2275 ); | |
| 2276 } | 2973 } |
| 2277 | 2974 |
| 2278 if( rc==SQLITE_OK ){ | 2975 if( rc==SQLITE_OK ){ |
| 2279 rc = fts3TermSelectMerge(&tsc); | 2976 rc = fts3TermSelectFinishMerge(p, &tsc); |
| 2280 } | 2977 } |
| 2281 if( rc==SQLITE_OK ){ | 2978 if( rc==SQLITE_OK ){ |
| 2282 *ppOut = tsc.aaOutput[0]; | 2979 *ppOut = tsc.aaOutput[0]; |
| 2283 *pnOut = tsc.anOutput[0]; | 2980 *pnOut = tsc.anOutput[0]; |
| 2284 }else{ | 2981 }else{ |
| 2285 int i; | 2982 int i; |
| 2286 for(i=0; i<SizeofArray(tsc.aaOutput); i++){ | 2983 for(i=0; i<SizeofArray(tsc.aaOutput); i++){ |
| 2287 sqlite3_free(tsc.aaOutput[i]); | 2984 sqlite3_free(tsc.aaOutput[i]); |
| 2288 } | 2985 } |
| 2289 } | 2986 } |
| 2290 | 2987 |
| 2291 fts3SegReaderCursorFree(pSegcsr); | 2988 fts3SegReaderCursorFree(pSegcsr); |
| 2292 pTok->pSegcsr = 0; | 2989 pTok->pSegcsr = 0; |
| 2293 return rc; | 2990 return rc; |
| 2294 } | 2991 } |
| 2295 | 2992 |
| 2296 /* | 2993 /* |
| 2297 ** This function counts the total number of docids in the doclist stored | 2994 ** This function counts the total number of docids in the doclist stored |
| 2298 ** in buffer aList[], size nList bytes. | 2995 ** in buffer aList[], size nList bytes. |
| 2299 ** | 2996 ** |
| 2300 ** If the isPoslist argument is true, then it is assumed that the doclist | 2997 ** If the isPoslist argument is true, then it is assumed that the doclist |
| 2301 ** contains a position-list following each docid. Otherwise, it is assumed | 2998 ** contains a position-list following each docid. Otherwise, it is assumed |
| 2302 ** that the doclist is simply a list of docids stored as delta encoded | 2999 ** that the doclist is simply a list of docids stored as delta encoded |
| 2303 ** varints. | 3000 ** varints. |
| 2304 */ | 3001 */ |
| 2305 static int fts3DoclistCountDocids(int isPoslist, char *aList, int nList){ | 3002 static int fts3DoclistCountDocids(char *aList, int nList){ |
| 2306 int nDoc = 0; /* Return value */ | 3003 int nDoc = 0; /* Return value */ |
| 2307 if( aList ){ | 3004 if( aList ){ |
| 2308 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */ | 3005 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */ |
| 2309 char *p = aList; /* Cursor */ | 3006 char *p = aList; /* Cursor */ |
| 2310 if( !isPoslist ){ | 3007 while( p<aEnd ){ |
| 2311 /* The number of docids in the list is the same as the number of | 3008 nDoc++; |
| 2312 ** varints. In FTS3 a varint consists of a single byte with the 0x80 | 3009 while( (*p++)&0x80 ); /* Skip docid varint */ |
| 2313 ** bit cleared and zero or more bytes with the 0x80 bit set. So to | 3010 fts3PoslistCopy(0, &p); /* Skip over position list */ |
| 2314 ** count the varints in the buffer, just count the number of bytes | |
| 2315 ** with the 0x80 bit clear. */ | |
| 2316 while( p<aEnd ) nDoc += (((*p++)&0x80)==0); | |
| 2317 }else{ | |
| 2318 while( p<aEnd ){ | |
| 2319 nDoc++; | |
| 2320 while( (*p++)&0x80 ); /* Skip docid varint */ | |
| 2321 fts3PoslistCopy(0, &p); /* Skip over position list */ | |
| 2322 } | |
| 2323 } | 3011 } |
| 2324 } | 3012 } |
| 2325 | 3013 |
| 2326 return nDoc; | 3014 return nDoc; |
| 2327 } | 3015 } |
| 2328 | 3016 |
| 2329 /* | 3017 /* |
| 2330 ** Call sqlite3Fts3DeferToken() for each token in the expression pExpr. | |
| 2331 */ | |
| 2332 static int fts3DeferExpression(Fts3Cursor *pCsr, Fts3Expr *pExpr){ | |
| 2333 int rc = SQLITE_OK; | |
| 2334 if( pExpr ){ | |
| 2335 rc = fts3DeferExpression(pCsr, pExpr->pLeft); | |
| 2336 if( rc==SQLITE_OK ){ | |
| 2337 rc = fts3DeferExpression(pCsr, pExpr->pRight); | |
| 2338 } | |
| 2339 if( pExpr->eType==FTSQUERY_PHRASE ){ | |
| 2340 int iCol = pExpr->pPhrase->iColumn; | |
| 2341 int i; | |
| 2342 for(i=0; rc==SQLITE_OK && i<pExpr->pPhrase->nToken; i++){ | |
| 2343 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i]; | |
| 2344 if( pToken->pDeferred==0 ){ | |
| 2345 rc = sqlite3Fts3DeferToken(pCsr, pToken, iCol); | |
| 2346 } | |
| 2347 } | |
| 2348 } | |
| 2349 } | |
| 2350 return rc; | |
| 2351 } | |
| 2352 | |
| 2353 /* | |
| 2354 ** This function removes the position information from a doclist. When | |
| 2355 ** called, buffer aList (size *pnList bytes) contains a doclist that includes | |
| 2356 ** position information. This function removes the position information so | |
| 2357 ** that aList contains only docids, and adjusts *pnList to reflect the new | |
| 2358 ** (possibly reduced) size of the doclist. | |
| 2359 */ | |
| 2360 static void fts3DoclistStripPositions( | |
| 2361 char *aList, /* IN/OUT: Buffer containing doclist */ | |
| 2362 int *pnList /* IN/OUT: Size of doclist in bytes */ | |
| 2363 ){ | |
| 2364 if( aList ){ | |
| 2365 char *aEnd = &aList[*pnList]; /* Pointer to one byte after EOF */ | |
| 2366 char *p = aList; /* Input cursor */ | |
| 2367 char *pOut = aList; /* Output cursor */ | |
| 2368 | |
| 2369 while( p<aEnd ){ | |
| 2370 sqlite3_int64 delta; | |
| 2371 p += sqlite3Fts3GetVarint(p, &delta); | |
| 2372 fts3PoslistCopy(0, &p); | |
| 2373 pOut += sqlite3Fts3PutVarint(pOut, delta); | |
| 2374 } | |
| 2375 | |
| 2376 *pnList = (int)(pOut - aList); | |
| 2377 } | |
| 2378 } | |
| 2379 | |
| 2380 /* | |
| 2381 ** Return a DocList corresponding to the phrase *pPhrase. | |
| 2382 ** | |
| 2383 ** If this function returns SQLITE_OK, but *pnOut is set to a negative value, | |
| 2384 ** then no tokens in the phrase were looked up in the full-text index. This | |
| 2385 ** is only possible when this function is called from within xFilter(). The | |
| 2386 ** caller should assume that all documents match the phrase. The actual | |
| 2387 ** filtering will take place in xNext(). | |
| 2388 */ | |
| 2389 static int fts3PhraseSelect( | |
| 2390 Fts3Cursor *pCsr, /* Virtual table cursor handle */ | |
| 2391 Fts3Phrase *pPhrase, /* Phrase to return a doclist for */ | |
| 2392 int isReqPos, /* True if output should contain positions */ | |
| 2393 char **paOut, /* OUT: Pointer to malloc'd result buffer */ | |
| 2394 int *pnOut /* OUT: Size of buffer at *paOut */ | |
| 2395 ){ | |
| 2396 char *pOut = 0; | |
| 2397 int nOut = 0; | |
| 2398 int rc = SQLITE_OK; | |
| 2399 int ii; | |
| 2400 int iCol = pPhrase->iColumn; | |
| 2401 int isTermPos = (pPhrase->nToken>1 || isReqPos); | |
| 2402 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; | |
| 2403 int isFirst = 1; | |
| 2404 | |
| 2405 int iPrevTok = 0; | |
| 2406 int nDoc = 0; | |
| 2407 | |
| 2408 /* If this is an xFilter() evaluation, create a segment-reader for each | |
| 2409 ** phrase token. Or, if this is an xNext() or snippet/offsets/matchinfo | |
| 2410 ** evaluation, only create segment-readers if there are no Fts3DeferredToken | |
| 2411 ** objects attached to the phrase-tokens. | |
| 2412 */ | |
| 2413 for(ii=0; ii<pPhrase->nToken; ii++){ | |
| 2414 Fts3PhraseToken *pTok = &pPhrase->aToken[ii]; | |
| 2415 if( pTok->pSegcsr==0 ){ | |
| 2416 if( (pCsr->eEvalmode==FTS3_EVAL_FILTER) | |
| 2417 || (pCsr->eEvalmode==FTS3_EVAL_NEXT && pCsr->pDeferred==0) | |
| 2418 || (pCsr->eEvalmode==FTS3_EVAL_MATCHINFO && pTok->bFulltext) | |
| 2419 ){ | |
| 2420 rc = fts3TermSegReaderCursor( | |
| 2421 pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr | |
| 2422 ); | |
| 2423 if( rc!=SQLITE_OK ) return rc; | |
| 2424 } | |
| 2425 } | |
| 2426 } | |
| 2427 | |
| 2428 for(ii=0; ii<pPhrase->nToken; ii++){ | |
| 2429 Fts3PhraseToken *pTok; /* Token to find doclist for */ | |
| 2430 int iTok = 0; /* The token being queried this iteration */ | |
| 2431 char *pList = 0; /* Pointer to token doclist */ | |
| 2432 int nList = 0; /* Size of buffer at pList */ | |
| 2433 | |
| 2434 /* Select a token to process. If this is an xFilter() call, then tokens | |
| 2435 ** are processed in order from least to most costly. Otherwise, tokens | |
| 2436 ** are processed in the order in which they occur in the phrase. | |
| 2437 */ | |
| 2438 if( pCsr->eEvalmode==FTS3_EVAL_MATCHINFO ){ | |
| 2439 assert( isReqPos ); | |
| 2440 iTok = ii; | |
| 2441 pTok = &pPhrase->aToken[iTok]; | |
| 2442 if( pTok->bFulltext==0 ) continue; | |
| 2443 }else if( pCsr->eEvalmode==FTS3_EVAL_NEXT || isReqPos ){ | |
| 2444 iTok = ii; | |
| 2445 pTok = &pPhrase->aToken[iTok]; | |
| 2446 }else{ | |
| 2447 int nMinCost = 0x7FFFFFFF; | |
| 2448 int jj; | |
| 2449 | |
| 2450 /* Find the remaining token with the lowest cost. */ | |
| 2451 for(jj=0; jj<pPhrase->nToken; jj++){ | |
| 2452 Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[jj].pSegcsr; | |
| 2453 if( pSegcsr && pSegcsr->nCost<nMinCost ){ | |
| 2454 iTok = jj; | |
| 2455 nMinCost = pSegcsr->nCost; | |
| 2456 } | |
| 2457 } | |
| 2458 pTok = &pPhrase->aToken[iTok]; | |
| 2459 | |
| 2460 /* This branch is taken if it is determined that loading the doclist | |
| 2461 ** for the next token would require more IO than loading all documents | |
| 2462 ** currently identified by doclist pOut/nOut. No further doclists will | |
| 2463 ** be loaded from the full-text index for this phrase. | |
| 2464 */ | |
| 2465 if( nMinCost>nDoc && ii>0 ){ | |
| 2466 rc = fts3DeferExpression(pCsr, pCsr->pExpr); | |
| 2467 break; | |
| 2468 } | |
| 2469 } | |
| 2470 | |
| 2471 if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){ | |
| 2472 rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList); | |
| 2473 }else{ | |
| 2474 if( pTok->pSegcsr ){ | |
| 2475 rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList); | |
| 2476 } | |
| 2477 pTok->bFulltext = 1; | |
| 2478 } | |
| 2479 assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pSegcsr==0 ); | |
| 2480 if( rc!=SQLITE_OK ) break; | |
| 2481 | |
| 2482 if( isFirst ){ | |
| 2483 pOut = pList; | |
| 2484 nOut = nList; | |
| 2485 if( pCsr->eEvalmode==FTS3_EVAL_FILTER && pPhrase->nToken>1 ){ | |
| 2486 nDoc = fts3DoclistCountDocids(1, pOut, nOut); | |
| 2487 } | |
| 2488 isFirst = 0; | |
| 2489 iPrevTok = iTok; | |
| 2490 }else{ | |
| 2491 /* Merge the new term list and the current output. */ | |
| 2492 char *aLeft, *aRight; | |
| 2493 int nLeft, nRight; | |
| 2494 int nDist; | |
| 2495 int mt; | |
| 2496 | |
| 2497 /* If this is the final token of the phrase, and positions were not | |
| 2498 ** requested by the caller, use MERGE_PHRASE instead of POS_PHRASE. | |
| 2499 ** This drops the position information from the output list. | |
| 2500 */ | |
| 2501 mt = MERGE_POS_PHRASE; | |
| 2502 if( ii==pPhrase->nToken-1 && !isReqPos ) mt = MERGE_PHRASE; | |
| 2503 | |
| 2504 assert( iPrevTok!=iTok ); | |
| 2505 if( iPrevTok<iTok ){ | |
| 2506 aLeft = pOut; | |
| 2507 nLeft = nOut; | |
| 2508 aRight = pList; | |
| 2509 nRight = nList; | |
| 2510 nDist = iTok-iPrevTok; | |
| 2511 iPrevTok = iTok; | |
| 2512 }else{ | |
| 2513 aRight = pOut; | |
| 2514 nRight = nOut; | |
| 2515 aLeft = pList; | |
| 2516 nLeft = nList; | |
| 2517 nDist = iPrevTok-iTok; | |
| 2518 } | |
| 2519 pOut = aRight; | |
| 2520 fts3DoclistMerge( | |
| 2521 mt, nDist, 0, pOut, &nOut, aLeft, nLeft, aRight, nRight, &nDoc | |
| 2522 ); | |
| 2523 sqlite3_free(aLeft); | |
| 2524 } | |
| 2525 assert( nOut==0 || pOut!=0 ); | |
| 2526 } | |
| 2527 | |
| 2528 if( rc==SQLITE_OK ){ | |
| 2529 if( ii!=pPhrase->nToken ){ | |
| 2530 assert( pCsr->eEvalmode==FTS3_EVAL_FILTER && isReqPos==0 ); | |
| 2531 fts3DoclistStripPositions(pOut, &nOut); | |
| 2532 } | |
| 2533 *paOut = pOut; | |
| 2534 *pnOut = nOut; | |
| 2535 }else{ | |
| 2536 sqlite3_free(pOut); | |
| 2537 } | |
| 2538 return rc; | |
| 2539 } | |
| 2540 | |
| 2541 /* | |
| 2542 ** This function merges two doclists according to the requirements of a | |
| 2543 ** NEAR operator. | |
| 2544 ** | |
| 2545 ** Both input doclists must include position information. The output doclist | |
| 2546 ** includes position information if the first argument to this function | |
| 2547 ** is MERGE_POS_NEAR, or does not if it is MERGE_NEAR. | |
| 2548 */ | |
| 2549 static int fts3NearMerge( | |
| 2550 int mergetype, /* MERGE_POS_NEAR or MERGE_NEAR */ | |
| 2551 int nNear, /* Parameter to NEAR operator */ | |
| 2552 int nTokenLeft, /* Number of tokens in LHS phrase arg */ | |
| 2553 char *aLeft, /* Doclist for LHS (incl. positions) */ | |
| 2554 int nLeft, /* Size of LHS doclist in bytes */ | |
| 2555 int nTokenRight, /* As nTokenLeft */ | |
| 2556 char *aRight, /* As aLeft */ | |
| 2557 int nRight, /* As nRight */ | |
| 2558 char **paOut, /* OUT: Results of merge (malloced) */ | |
| 2559 int *pnOut /* OUT: Sized of output buffer */ | |
| 2560 ){ | |
| 2561 char *aOut; /* Buffer to write output doclist to */ | |
| 2562 int rc; /* Return code */ | |
| 2563 | |
| 2564 assert( mergetype==MERGE_POS_NEAR || MERGE_NEAR ); | |
| 2565 | |
| 2566 aOut = sqlite3_malloc(nLeft+nRight+1); | |
| 2567 if( aOut==0 ){ | |
| 2568 rc = SQLITE_NOMEM; | |
| 2569 }else{ | |
| 2570 rc = fts3DoclistMerge(mergetype, nNear+nTokenRight, nNear+nTokenLeft, | |
| 2571 aOut, pnOut, aLeft, nLeft, aRight, nRight, 0 | |
| 2572 ); | |
| 2573 if( rc!=SQLITE_OK ){ | |
| 2574 sqlite3_free(aOut); | |
| 2575 aOut = 0; | |
| 2576 } | |
| 2577 } | |
| 2578 | |
| 2579 *paOut = aOut; | |
| 2580 return rc; | |
| 2581 } | |
| 2582 | |
| 2583 /* | |
| 2584 ** This function is used as part of the processing for the snippet() and | |
| 2585 ** offsets() functions. | |
| 2586 ** | |
| 2587 ** Both pLeft and pRight are expression nodes of type FTSQUERY_PHRASE. Both | |
| 2588 ** have their respective doclists (including position information) loaded | |
| 2589 ** in Fts3Expr.aDoclist/nDoclist. This function removes all entries from | |
| 2590 ** each doclist that are not within nNear tokens of a corresponding entry | |
| 2591 ** in the other doclist. | |
| 2592 */ | |
| 2593 int sqlite3Fts3ExprNearTrim(Fts3Expr *pLeft, Fts3Expr *pRight, int nNear){ | |
| 2594 int rc; /* Return code */ | |
| 2595 | |
| 2596 assert( pLeft->eType==FTSQUERY_PHRASE ); | |
| 2597 assert( pRight->eType==FTSQUERY_PHRASE ); | |
| 2598 assert( pLeft->isLoaded && pRight->isLoaded ); | |
| 2599 | |
| 2600 if( pLeft->aDoclist==0 || pRight->aDoclist==0 ){ | |
| 2601 sqlite3_free(pLeft->aDoclist); | |
| 2602 sqlite3_free(pRight->aDoclist); | |
| 2603 pRight->aDoclist = 0; | |
| 2604 pLeft->aDoclist = 0; | |
| 2605 rc = SQLITE_OK; | |
| 2606 }else{ | |
| 2607 char *aOut; /* Buffer in which to assemble new doclist */ | |
| 2608 int nOut; /* Size of buffer aOut in bytes */ | |
| 2609 | |
| 2610 rc = fts3NearMerge(MERGE_POS_NEAR, nNear, | |
| 2611 pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist, | |
| 2612 pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist, | |
| 2613 &aOut, &nOut | |
| 2614 ); | |
| 2615 if( rc!=SQLITE_OK ) return rc; | |
| 2616 sqlite3_free(pRight->aDoclist); | |
| 2617 pRight->aDoclist = aOut; | |
| 2618 pRight->nDoclist = nOut; | |
| 2619 | |
| 2620 rc = fts3NearMerge(MERGE_POS_NEAR, nNear, | |
| 2621 pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist, | |
| 2622 pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist, | |
| 2623 &aOut, &nOut | |
| 2624 ); | |
| 2625 sqlite3_free(pLeft->aDoclist); | |
| 2626 pLeft->aDoclist = aOut; | |
| 2627 pLeft->nDoclist = nOut; | |
| 2628 } | |
| 2629 return rc; | |
| 2630 } | |
| 2631 | |
| 2632 | |
| 2633 /* | |
| 2634 ** Allocate an Fts3SegReaderArray for each token in the expression pExpr. | |
| 2635 ** The allocated objects are stored in the Fts3PhraseToken.pArray member | |
| 2636 ** variables of each token structure. | |
| 2637 */ | |
| 2638 static int fts3ExprAllocateSegReaders( | |
| 2639 Fts3Cursor *pCsr, /* FTS3 table */ | |
| 2640 Fts3Expr *pExpr, /* Expression to create seg-readers for */ | |
| 2641 int *pnExpr /* OUT: Number of AND'd expressions */ | |
| 2642 ){ | |
| 2643 int rc = SQLITE_OK; /* Return code */ | |
| 2644 | |
| 2645 assert( pCsr->eEvalmode==FTS3_EVAL_FILTER ); | |
| 2646 if( pnExpr && pExpr->eType!=FTSQUERY_AND ){ | |
| 2647 (*pnExpr)++; | |
| 2648 pnExpr = 0; | |
| 2649 } | |
| 2650 | |
| 2651 if( pExpr->eType==FTSQUERY_PHRASE ){ | |
| 2652 Fts3Phrase *pPhrase = pExpr->pPhrase; | |
| 2653 int ii; | |
| 2654 | |
| 2655 for(ii=0; rc==SQLITE_OK && ii<pPhrase->nToken; ii++){ | |
| 2656 Fts3PhraseToken *pTok = &pPhrase->aToken[ii]; | |
| 2657 if( pTok->pSegcsr==0 ){ | |
| 2658 rc = fts3TermSegReaderCursor( | |
| 2659 pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr | |
| 2660 ); | |
| 2661 } | |
| 2662 } | |
| 2663 }else{ | |
| 2664 rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pLeft, pnExpr); | |
| 2665 if( rc==SQLITE_OK ){ | |
| 2666 rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pRight, pnExpr); | |
| 2667 } | |
| 2668 } | |
| 2669 return rc; | |
| 2670 } | |
| 2671 | |
| 2672 /* | |
| 2673 ** Free the Fts3SegReaderArray objects associated with each token in the | |
| 2674 ** expression pExpr. In other words, this function frees the resources | |
| 2675 ** allocated by fts3ExprAllocateSegReaders(). | |
| 2676 */ | |
| 2677 static void fts3ExprFreeSegReaders(Fts3Expr *pExpr){ | |
| 2678 if( pExpr ){ | |
| 2679 Fts3Phrase *pPhrase = pExpr->pPhrase; | |
| 2680 if( pPhrase ){ | |
| 2681 int kk; | |
| 2682 for(kk=0; kk<pPhrase->nToken; kk++){ | |
| 2683 fts3SegReaderCursorFree(pPhrase->aToken[kk].pSegcsr); | |
| 2684 pPhrase->aToken[kk].pSegcsr = 0; | |
| 2685 } | |
| 2686 } | |
| 2687 fts3ExprFreeSegReaders(pExpr->pLeft); | |
| 2688 fts3ExprFreeSegReaders(pExpr->pRight); | |
| 2689 } | |
| 2690 } | |
| 2691 | |
| 2692 /* | |
| 2693 ** Return the sum of the costs of all tokens in the expression pExpr. This | |
| 2694 ** function must be called after Fts3SegReaderArrays have been allocated | |
| 2695 ** for all tokens using fts3ExprAllocateSegReaders(). | |
| 2696 */ | |
| 2697 static int fts3ExprCost(Fts3Expr *pExpr){ | |
| 2698 int nCost; /* Return value */ | |
| 2699 if( pExpr->eType==FTSQUERY_PHRASE ){ | |
| 2700 Fts3Phrase *pPhrase = pExpr->pPhrase; | |
| 2701 int ii; | |
| 2702 nCost = 0; | |
| 2703 for(ii=0; ii<pPhrase->nToken; ii++){ | |
| 2704 Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[ii].pSegcsr; | |
| 2705 if( pSegcsr ) nCost += pSegcsr->nCost; | |
| 2706 } | |
| 2707 }else{ | |
| 2708 nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight); | |
| 2709 } | |
| 2710 return nCost; | |
| 2711 } | |
| 2712 | |
| 2713 /* | |
| 2714 ** The following is a helper function (and type) for fts3EvalExpr(). It | |
| 2715 ** must be called after Fts3SegReaders have been allocated for every token | |
| 2716 ** in the expression. See the context it is called from in fts3EvalExpr() | |
| 2717 ** for further explanation. | |
| 2718 */ | |
| 2719 typedef struct ExprAndCost ExprAndCost; | |
| 2720 struct ExprAndCost { | |
| 2721 Fts3Expr *pExpr; | |
| 2722 int nCost; | |
| 2723 }; | |
| 2724 static void fts3ExprAssignCosts( | |
| 2725 Fts3Expr *pExpr, /* Expression to create seg-readers for */ | |
| 2726 ExprAndCost **ppExprCost /* OUT: Write to *ppExprCost */ | |
| 2727 ){ | |
| 2728 if( pExpr->eType==FTSQUERY_AND ){ | |
| 2729 fts3ExprAssignCosts(pExpr->pLeft, ppExprCost); | |
| 2730 fts3ExprAssignCosts(pExpr->pRight, ppExprCost); | |
| 2731 }else{ | |
| 2732 (*ppExprCost)->pExpr = pExpr; | |
| 2733 (*ppExprCost)->nCost = fts3ExprCost(pExpr); | |
| 2734 (*ppExprCost)++; | |
| 2735 } | |
| 2736 } | |
| 2737 | |
| 2738 /* | |
| 2739 ** Evaluate the full-text expression pExpr against FTS3 table pTab. Store | |
| 2740 ** the resulting doclist in *paOut and *pnOut. This routine mallocs for | |
| 2741 ** the space needed to store the output. The caller is responsible for | |
| 2742 ** freeing the space when it has finished. | |
| 2743 ** | |
| 2744 ** This function is called in two distinct contexts: | |
| 2745 ** | |
| 2746 ** * From within the virtual table xFilter() method. In this case, the | |
| 2747 ** output doclist contains entries for all rows in the table, based on | |
| 2748 ** data read from the full-text index. | |
| 2749 ** | |
| 2750 ** In this case, if the query expression contains one or more tokens that | |
| 2751 ** are very common, then the returned doclist may contain a superset of | |
| 2752 ** the documents that actually match the expression. | |
| 2753 ** | |
| 2754 ** * From within the virtual table xNext() method. This call is only made | |
| 2755 ** if the call from within xFilter() found that there were very common | |
| 2756 ** tokens in the query expression and did return a superset of the | |
| 2757 ** matching documents. In this case the returned doclist contains only | |
| 2758 ** entries that correspond to the current row of the table. Instead of | |
| 2759 ** reading the data for each token from the full-text index, the data is | |
| 2760 ** already available in-memory in the Fts3PhraseToken.pDeferred structures. | |
| 2761 ** See fts3EvalDeferred() for how it gets there. | |
| 2762 ** | |
| 2763 ** In the first case above, Fts3Cursor.doDeferred==0. In the second (if it is | |
| 2764 ** required) Fts3Cursor.doDeferred==1. | |
| 2765 ** | |
| 2766 ** If the SQLite invokes the snippet(), offsets() or matchinfo() function | |
| 2767 ** as part of a SELECT on an FTS3 table, this function is called on each | |
| 2768 ** individual phrase expression in the query. If there were very common tokens | |
| 2769 ** found in the xFilter() call, then this function is called once for phrase | |
| 2770 ** for each row visited, and the returned doclist contains entries for the | |
| 2771 ** current row only. Otherwise, if there were no very common tokens, then this | |
| 2772 ** function is called once only for each phrase in the query and the returned | |
| 2773 ** doclist contains entries for all rows of the table. | |
| 2774 ** | |
| 2775 ** Fts3Cursor.doDeferred==1 when this function is called on phrases as a | |
| 2776 ** result of a snippet(), offsets() or matchinfo() invocation. | |
| 2777 */ | |
| 2778 static int fts3EvalExpr( | |
| 2779 Fts3Cursor *p, /* Virtual table cursor handle */ | |
| 2780 Fts3Expr *pExpr, /* Parsed fts3 expression */ | |
| 2781 char **paOut, /* OUT: Pointer to malloc'd result buffer */ | |
| 2782 int *pnOut, /* OUT: Size of buffer at *paOut */ | |
| 2783 int isReqPos /* Require positions in output buffer */ | |
| 2784 ){ | |
| 2785 int rc = SQLITE_OK; /* Return code */ | |
| 2786 | |
| 2787 /* Zero the output parameters. */ | |
| 2788 *paOut = 0; | |
| 2789 *pnOut = 0; | |
| 2790 | |
| 2791 if( pExpr ){ | |
| 2792 assert( pExpr->eType==FTSQUERY_NEAR || pExpr->eType==FTSQUERY_OR | |
| 2793 || pExpr->eType==FTSQUERY_AND || pExpr->eType==FTSQUERY_NOT | |
| 2794 || pExpr->eType==FTSQUERY_PHRASE | |
| 2795 ); | |
| 2796 assert( pExpr->eType==FTSQUERY_PHRASE || isReqPos==0 ); | |
| 2797 | |
| 2798 if( pExpr->eType==FTSQUERY_PHRASE ){ | |
| 2799 rc = fts3PhraseSelect(p, pExpr->pPhrase, | |
| 2800 isReqPos || (pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR), | |
| 2801 paOut, pnOut | |
| 2802 ); | |
| 2803 fts3ExprFreeSegReaders(pExpr); | |
| 2804 }else if( p->eEvalmode==FTS3_EVAL_FILTER && pExpr->eType==FTSQUERY_AND ){ | |
| 2805 ExprAndCost *aExpr = 0; /* Array of AND'd expressions and costs */ | |
| 2806 int nExpr = 0; /* Size of aExpr[] */ | |
| 2807 char *aRet = 0; /* Doclist to return to caller */ | |
| 2808 int nRet = 0; /* Length of aRet[] in bytes */ | |
| 2809 int nDoc = 0x7FFFFFFF; | |
| 2810 | |
| 2811 assert( !isReqPos ); | |
| 2812 | |
| 2813 rc = fts3ExprAllocateSegReaders(p, pExpr, &nExpr); | |
| 2814 if( rc==SQLITE_OK ){ | |
| 2815 assert( nExpr>1 ); | |
| 2816 aExpr = sqlite3_malloc(sizeof(ExprAndCost) * nExpr); | |
| 2817 if( !aExpr ) rc = SQLITE_NOMEM; | |
| 2818 } | |
| 2819 if( rc==SQLITE_OK ){ | |
| 2820 int ii; /* Used to iterate through expressions */ | |
| 2821 | |
| 2822 fts3ExprAssignCosts(pExpr, &aExpr); | |
| 2823 aExpr -= nExpr; | |
| 2824 for(ii=0; ii<nExpr; ii++){ | |
| 2825 char *aNew; | |
| 2826 int nNew; | |
| 2827 int jj; | |
| 2828 ExprAndCost *pBest = 0; | |
| 2829 | |
| 2830 for(jj=0; jj<nExpr; jj++){ | |
| 2831 ExprAndCost *pCand = &aExpr[jj]; | |
| 2832 if( pCand->pExpr && (pBest==0 || pCand->nCost<pBest->nCost) ){ | |
| 2833 pBest = pCand; | |
| 2834 } | |
| 2835 } | |
| 2836 | |
| 2837 if( pBest->nCost>nDoc ){ | |
| 2838 rc = fts3DeferExpression(p, p->pExpr); | |
| 2839 break; | |
| 2840 }else{ | |
| 2841 rc = fts3EvalExpr(p, pBest->pExpr, &aNew, &nNew, 0); | |
| 2842 if( rc!=SQLITE_OK ) break; | |
| 2843 pBest->pExpr = 0; | |
| 2844 if( ii==0 ){ | |
| 2845 aRet = aNew; | |
| 2846 nRet = nNew; | |
| 2847 nDoc = fts3DoclistCountDocids(0, aRet, nRet); | |
| 2848 }else{ | |
| 2849 fts3DoclistMerge( | |
| 2850 MERGE_AND, 0, 0, aRet, &nRet, aRet, nRet, aNew, nNew, &nDoc | |
| 2851 ); | |
| 2852 sqlite3_free(aNew); | |
| 2853 } | |
| 2854 } | |
| 2855 } | |
| 2856 } | |
| 2857 | |
| 2858 if( rc==SQLITE_OK ){ | |
| 2859 *paOut = aRet; | |
| 2860 *pnOut = nRet; | |
| 2861 }else{ | |
| 2862 assert( *paOut==0 ); | |
| 2863 sqlite3_free(aRet); | |
| 2864 } | |
| 2865 sqlite3_free(aExpr); | |
| 2866 fts3ExprFreeSegReaders(pExpr); | |
| 2867 | |
| 2868 }else{ | |
| 2869 char *aLeft; | |
| 2870 char *aRight; | |
| 2871 int nLeft; | |
| 2872 int nRight; | |
| 2873 | |
| 2874 assert( pExpr->eType==FTSQUERY_NEAR | |
| 2875 || pExpr->eType==FTSQUERY_OR | |
| 2876 || pExpr->eType==FTSQUERY_NOT | |
| 2877 || (pExpr->eType==FTSQUERY_AND && p->eEvalmode==FTS3_EVAL_NEXT) | |
| 2878 ); | |
| 2879 | |
| 2880 if( 0==(rc = fts3EvalExpr(p, pExpr->pRight, &aRight, &nRight, isReqPos)) | |
| 2881 && 0==(rc = fts3EvalExpr(p, pExpr->pLeft, &aLeft, &nLeft, isReqPos)) | |
| 2882 ){ | |
| 2883 switch( pExpr->eType ){ | |
| 2884 case FTSQUERY_NEAR: { | |
| 2885 Fts3Expr *pLeft; | |
| 2886 Fts3Expr *pRight; | |
| 2887 int mergetype = MERGE_NEAR; | |
| 2888 if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){ | |
| 2889 mergetype = MERGE_POS_NEAR; | |
| 2890 } | |
| 2891 pLeft = pExpr->pLeft; | |
| 2892 while( pLeft->eType==FTSQUERY_NEAR ){ | |
| 2893 pLeft=pLeft->pRight; | |
| 2894 } | |
| 2895 pRight = pExpr->pRight; | |
| 2896 assert( pRight->eType==FTSQUERY_PHRASE ); | |
| 2897 assert( pLeft->eType==FTSQUERY_PHRASE ); | |
| 2898 | |
| 2899 rc = fts3NearMerge(mergetype, pExpr->nNear, | |
| 2900 pLeft->pPhrase->nToken, aLeft, nLeft, | |
| 2901 pRight->pPhrase->nToken, aRight, nRight, | |
| 2902 paOut, pnOut | |
| 2903 ); | |
| 2904 sqlite3_free(aLeft); | |
| 2905 break; | |
| 2906 } | |
| 2907 | |
| 2908 case FTSQUERY_OR: { | |
| 2909 /* Allocate a buffer for the output. The maximum size is the | |
| 2910 ** sum of the sizes of the two input buffers. The +1 term is | |
| 2911 ** so that a buffer of zero bytes is never allocated - this can | |
| 2912 ** cause fts3DoclistMerge() to incorrectly return SQLITE_NOMEM. | |
| 2913 */ | |
| 2914 char *aBuffer = sqlite3_malloc(nRight+nLeft+1); | |
| 2915 rc = fts3DoclistMerge(MERGE_OR, 0, 0, aBuffer, pnOut, | |
| 2916 aLeft, nLeft, aRight, nRight, 0 | |
| 2917 ); | |
| 2918 *paOut = aBuffer; | |
| 2919 sqlite3_free(aLeft); | |
| 2920 break; | |
| 2921 } | |
| 2922 | |
| 2923 default: { | |
| 2924 assert( FTSQUERY_NOT==MERGE_NOT && FTSQUERY_AND==MERGE_AND ); | |
| 2925 fts3DoclistMerge(pExpr->eType, 0, 0, aLeft, pnOut, | |
| 2926 aLeft, nLeft, aRight, nRight, 0 | |
| 2927 ); | |
| 2928 *paOut = aLeft; | |
| 2929 break; | |
| 2930 } | |
| 2931 } | |
| 2932 } | |
| 2933 sqlite3_free(aRight); | |
| 2934 } | |
| 2935 } | |
| 2936 | |
| 2937 assert( rc==SQLITE_OK || *paOut==0 ); | |
| 2938 return rc; | |
| 2939 } | |
| 2940 | |
| 2941 /* | |
| 2942 ** This function is called from within xNext() for each row visited by | |
| 2943 ** an FTS3 query. If evaluating the FTS3 query expression within xFilter() | |
| 2944 ** was able to determine the exact set of matching rows, this function sets | |
| 2945 ** *pbRes to true and returns SQLITE_IO immediately. | |
| 2946 ** | |
| 2947 ** Otherwise, if evaluating the query expression within xFilter() returned a | |
| 2948 ** superset of the matching documents instead of an exact set (this happens | |
| 2949 ** when the query includes very common tokens and it is deemed too expensive to | |
| 2950 ** load their doclists from disk), this function tests if the current row | |
| 2951 ** really does match the FTS3 query. | |
| 2952 ** | |
| 2953 ** If an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK | |
| 2954 ** is returned and *pbRes is set to true if the current row matches the | |
| 2955 ** FTS3 query (and should be included in the results returned to SQLite), or | |
| 2956 ** false otherwise. | |
| 2957 */ | |
| 2958 static int fts3EvalDeferred( | |
| 2959 Fts3Cursor *pCsr, /* FTS3 cursor pointing at row to test */ | |
| 2960 int *pbRes /* OUT: Set to true if row is a match */ | |
| 2961 ){ | |
| 2962 int rc = SQLITE_OK; | |
| 2963 if( pCsr->pDeferred==0 ){ | |
| 2964 *pbRes = 1; | |
| 2965 }else{ | |
| 2966 rc = fts3CursorSeek(0, pCsr); | |
| 2967 if( rc==SQLITE_OK ){ | |
| 2968 sqlite3Fts3FreeDeferredDoclists(pCsr); | |
| 2969 rc = sqlite3Fts3CacheDeferredDoclists(pCsr); | |
| 2970 } | |
| 2971 if( rc==SQLITE_OK ){ | |
| 2972 char *a = 0; | |
| 2973 int n = 0; | |
| 2974 rc = fts3EvalExpr(pCsr, pCsr->pExpr, &a, &n, 0); | |
| 2975 assert( n>=0 ); | |
| 2976 *pbRes = (n>0); | |
| 2977 sqlite3_free(a); | |
| 2978 } | |
| 2979 } | |
| 2980 return rc; | |
| 2981 } | |
| 2982 | |
| 2983 /* | |
| 2984 ** Advance the cursor to the next row in the %_content table that | 3018 ** Advance the cursor to the next row in the %_content table that |
| 2985 ** matches the search criteria. For a MATCH search, this will be | 3019 ** matches the search criteria. For a MATCH search, this will be |
| 2986 ** the next row that matches. For a full-table scan, this will be | 3020 ** the next row that matches. For a full-table scan, this will be |
| 2987 ** simply the next row in the %_content table. For a docid lookup, | 3021 ** simply the next row in the %_content table. For a docid lookup, |
| 2988 ** this routine simply sets the EOF flag. | 3022 ** this routine simply sets the EOF flag. |
| 2989 ** | 3023 ** |
| 2990 ** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned | 3024 ** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned |
| 2991 ** even if we reach end-of-file. The fts3EofMethod() will be called | 3025 ** even if we reach end-of-file. The fts3EofMethod() will be called |
| 2992 ** subsequently to determine whether or not an EOF was hit. | 3026 ** subsequently to determine whether or not an EOF was hit. |
| 2993 */ | 3027 */ |
| 2994 static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ | 3028 static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ |
| 2995 int res; | 3029 int rc; |
| 2996 int rc = SQLITE_OK; /* Return code */ | |
| 2997 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; | 3030 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; |
| 2998 | 3031 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){ |
| 2999 pCsr->eEvalmode = FTS3_EVAL_NEXT; | 3032 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){ |
| 3000 do { | 3033 pCsr->isEof = 1; |
| 3001 if( pCsr->aDoclist==0 ){ | 3034 rc = sqlite3_reset(pCsr->pStmt); |
| 3002 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){ | 3035 }else{ |
| 3003 pCsr->isEof = 1; | |
| 3004 rc = sqlite3_reset(pCsr->pStmt); | |
| 3005 break; | |
| 3006 } | |
| 3007 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0); | 3036 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0); |
| 3008 }else{ | 3037 rc = SQLITE_OK; |
| 3009 if( pCsr->pNextId>=&pCsr->aDoclist[pCsr->nDoclist] ){ | 3038 } |
| 3010 pCsr->isEof = 1; | 3039 }else{ |
| 3011 break; | 3040 rc = fts3EvalNext((Fts3Cursor *)pCursor); |
| 3012 } | 3041 } |
| 3013 sqlite3_reset(pCsr->pStmt); | 3042 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 3014 fts3GetDeltaVarint(&pCsr->pNextId, &pCsr->iPrevId); | |
| 3015 pCsr->isRequireSeek = 1; | |
| 3016 pCsr->isMatchinfoNeeded = 1; | |
| 3017 } | |
| 3018 }while( SQLITE_OK==(rc = fts3EvalDeferred(pCsr, &res)) && res==0 ); | |
| 3019 | |
| 3020 return rc; | 3043 return rc; |
| 3021 } | 3044 } |
| 3022 | 3045 |
| 3023 /* | 3046 /* |
| 3047 ** The following are copied from sqliteInt.h. |
| 3048 ** |
| 3049 ** Constants for the largest and smallest possible 64-bit signed integers. |
| 3050 ** These macros are designed to work correctly on both 32-bit and 64-bit |
| 3051 ** compilers. |
| 3052 */ |
| 3053 #ifndef SQLITE_AMALGAMATION |
| 3054 # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) |
| 3055 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) |
| 3056 #endif |
| 3057 |
| 3058 /* |
| 3059 ** If the numeric type of argument pVal is "integer", then return it |
| 3060 ** converted to a 64-bit signed integer. Otherwise, return a copy of |
| 3061 ** the second parameter, iDefault. |
| 3062 */ |
| 3063 static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){ |
| 3064 if( pVal ){ |
| 3065 int eType = sqlite3_value_numeric_type(pVal); |
| 3066 if( eType==SQLITE_INTEGER ){ |
| 3067 return sqlite3_value_int64(pVal); |
| 3068 } |
| 3069 } |
| 3070 return iDefault; |
| 3071 } |
| 3072 |
| 3073 /* |
| 3024 ** This is the xFilter interface for the virtual table. See | 3074 ** This is the xFilter interface for the virtual table. See |
| 3025 ** the virtual table xFilter method documentation for additional | 3075 ** the virtual table xFilter method documentation for additional |
| 3026 ** information. | 3076 ** information. |
| 3027 ** | 3077 ** |
| 3028 ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against | 3078 ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against |
| 3029 ** the %_content table. | 3079 ** the %_content table. |
| 3030 ** | 3080 ** |
| 3031 ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry | 3081 ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry |
| 3032 ** in the %_content table. | 3082 ** in the %_content table. |
| 3033 ** | 3083 ** |
| 3034 ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The | 3084 ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The |
| 3035 ** column on the left-hand side of the MATCH operator is column | 3085 ** column on the left-hand side of the MATCH operator is column |
| 3036 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand | 3086 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand |
| 3037 ** side of the MATCH operator. | 3087 ** side of the MATCH operator. |
| 3038 */ | 3088 */ |
| 3039 static int fts3FilterMethod( | 3089 static int fts3FilterMethod( |
| 3040 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ | 3090 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ |
| 3041 int idxNum, /* Strategy index */ | 3091 int idxNum, /* Strategy index */ |
| 3042 const char *idxStr, /* Unused */ | 3092 const char *idxStr, /* Unused */ |
| 3043 int nVal, /* Number of elements in apVal */ | 3093 int nVal, /* Number of elements in apVal */ |
| 3044 sqlite3_value **apVal /* Arguments for the indexing scheme */ | 3094 sqlite3_value **apVal /* Arguments for the indexing scheme */ |
| 3045 ){ | 3095 ){ |
| 3046 const char *azSql[] = { | 3096 int rc; |
| 3047 "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?", /* non-full-scan */ | |
| 3048 "SELECT %s FROM %Q.'%q_content' AS x ", /* full-scan */ | |
| 3049 }; | |
| 3050 int rc; /* Return code */ | |
| 3051 char *zSql; /* SQL statement used to access %_content */ | 3097 char *zSql; /* SQL statement used to access %_content */ |
| 3098 int eSearch; |
| 3052 Fts3Table *p = (Fts3Table *)pCursor->pVtab; | 3099 Fts3Table *p = (Fts3Table *)pCursor->pVtab; |
| 3053 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; | 3100 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; |
| 3054 | 3101 |
| 3102 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */ |
| 3103 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */ |
| 3104 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */ |
| 3105 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */ |
| 3106 int iIdx; |
| 3107 |
| 3055 UNUSED_PARAMETER(idxStr); | 3108 UNUSED_PARAMETER(idxStr); |
| 3056 UNUSED_PARAMETER(nVal); | 3109 UNUSED_PARAMETER(nVal); |
| 3057 | 3110 |
| 3058 assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) ); | 3111 eSearch = (idxNum & 0x0000FFFF); |
| 3059 assert( nVal==0 || nVal==1 ); | 3112 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) ); |
| 3060 assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) ); | |
| 3061 assert( p->pSegments==0 ); | 3113 assert( p->pSegments==0 ); |
| 3062 | 3114 |
| 3115 /* Collect arguments into local variables */ |
| 3116 iIdx = 0; |
| 3117 if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++]; |
| 3118 if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++]; |
| 3119 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++]; |
| 3120 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++]; |
| 3121 assert( iIdx==nVal ); |
| 3122 |
| 3063 /* In case the cursor has been used before, clear it now. */ | 3123 /* In case the cursor has been used before, clear it now. */ |
| 3064 sqlite3_finalize(pCsr->pStmt); | 3124 sqlite3_finalize(pCsr->pStmt); |
| 3065 sqlite3_free(pCsr->aDoclist); | 3125 sqlite3_free(pCsr->aDoclist); |
| 3126 sqlite3_free(pCsr->aMatchinfo); |
| 3066 sqlite3Fts3ExprFree(pCsr->pExpr); | 3127 sqlite3Fts3ExprFree(pCsr->pExpr); |
| 3067 memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); | 3128 memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); |
| 3068 | 3129 |
| 3069 if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){ | 3130 /* Set the lower and upper bounds on docids to return */ |
| 3070 int iCol = idxNum-FTS3_FULLTEXT_SEARCH; | 3131 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64); |
| 3071 const char *zQuery = (const char *)sqlite3_value_text(apVal[0]); | 3132 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64); |
| 3072 | 3133 |
| 3073 if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ | 3134 if( idxStr ){ |
| 3135 pCsr->bDesc = (idxStr[0]=='D'); |
| 3136 }else{ |
| 3137 pCsr->bDesc = p->bDescIdx; |
| 3138 } |
| 3139 pCsr->eSearch = (i16)eSearch; |
| 3140 |
| 3141 if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){ |
| 3142 int iCol = eSearch-FTS3_FULLTEXT_SEARCH; |
| 3143 const char *zQuery = (const char *)sqlite3_value_text(pCons); |
| 3144 |
| 3145 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){ |
| 3074 return SQLITE_NOMEM; | 3146 return SQLITE_NOMEM; |
| 3075 } | 3147 } |
| 3076 | 3148 |
| 3077 rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn, | 3149 pCsr->iLangid = 0; |
| 3078 iCol, zQuery, -1, &pCsr->pExpr | 3150 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid); |
| 3151 |
| 3152 assert( p->base.zErrMsg==0 ); |
| 3153 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid, |
| 3154 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr, |
| 3155 &p->base.zErrMsg |
| 3079 ); | 3156 ); |
| 3080 if( rc!=SQLITE_OK ){ | 3157 if( rc!=SQLITE_OK ){ |
| 3081 if( rc==SQLITE_ERROR ){ | |
| 3082 p->base.zErrMsg = sqlite3_mprintf("malformed MATCH expression: [%s]", | |
| 3083 zQuery); | |
| 3084 } | |
| 3085 return rc; | 3158 return rc; |
| 3086 } | 3159 } |
| 3087 | 3160 |
| 3088 rc = sqlite3Fts3ReadLock(p); | 3161 rc = fts3EvalStart(pCsr); |
| 3089 if( rc!=SQLITE_OK ) return rc; | |
| 3090 | |
| 3091 rc = fts3EvalExpr(pCsr, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0); | |
| 3092 sqlite3Fts3SegmentsClose(p); | 3162 sqlite3Fts3SegmentsClose(p); |
| 3093 if( rc!=SQLITE_OK ) return rc; | 3163 if( rc!=SQLITE_OK ) return rc; |
| 3094 pCsr->pNextId = pCsr->aDoclist; | 3164 pCsr->pNextId = pCsr->aDoclist; |
| 3095 pCsr->iPrevId = 0; | 3165 pCsr->iPrevId = 0; |
| 3096 } | 3166 } |
| 3097 | 3167 |
| 3098 /* Compile a SELECT statement for this cursor. For a full-table-scan, the | 3168 /* Compile a SELECT statement for this cursor. For a full-table-scan, the |
| 3099 ** statement loops through all rows of the %_content table. For a | 3169 ** statement loops through all rows of the %_content table. For a |
| 3100 ** full-text query or docid lookup, the statement retrieves a single | 3170 ** full-text query or docid lookup, the statement retrieves a single |
| 3101 ** row by docid. | 3171 ** row by docid. |
| 3102 */ | 3172 */ |
| 3103 zSql = (char *)azSql[idxNum==FTS3_FULLSCAN_SEARCH]; | 3173 if( eSearch==FTS3_FULLSCAN_SEARCH ){ |
| 3104 zSql = sqlite3_mprintf(zSql, p->zReadExprlist, p->zDb, p->zName); | 3174 zSql = sqlite3_mprintf( |
| 3105 if( !zSql ){ | 3175 "SELECT %s ORDER BY rowid %s", |
| 3106 rc = SQLITE_NOMEM; | 3176 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC") |
| 3107 }else{ | 3177 ); |
| 3108 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); | 3178 if( zSql ){ |
| 3109 sqlite3_free(zSql); | 3179 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); |
| 3110 } | 3180 sqlite3_free(zSql); |
| 3111 if( rc==SQLITE_OK && idxNum==FTS3_DOCID_SEARCH ){ | 3181 }else{ |
| 3112 rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]); | 3182 rc = SQLITE_NOMEM; |
| 3113 } | 3183 } |
| 3114 pCsr->eSearch = (i16)idxNum; | 3184 }else if( eSearch==FTS3_DOCID_SEARCH ){ |
| 3115 | 3185 rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt); |
| 3186 if( rc==SQLITE_OK ){ |
| 3187 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons); |
| 3188 } |
| 3189 } |
| 3116 if( rc!=SQLITE_OK ) return rc; | 3190 if( rc!=SQLITE_OK ) return rc; |
| 3191 |
| 3117 return fts3NextMethod(pCursor); | 3192 return fts3NextMethod(pCursor); |
| 3118 } | 3193 } |
| 3119 | 3194 |
| 3120 /* | 3195 /* |
| 3121 ** This is the xEof method of the virtual table. SQLite calls this | 3196 ** This is the xEof method of the virtual table. SQLite calls this |
| 3122 ** routine to find out if it has reached the end of a result set. | 3197 ** routine to find out if it has reached the end of a result set. |
| 3123 */ | 3198 */ |
| 3124 static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){ | 3199 static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){ |
| 3125 return ((Fts3Cursor *)pCursor)->isEof; | 3200 return ((Fts3Cursor *)pCursor)->isEof; |
| 3126 } | 3201 } |
| 3127 | 3202 |
| 3128 /* | 3203 /* |
| 3129 ** This is the xRowid method. The SQLite core calls this routine to | 3204 ** This is the xRowid method. The SQLite core calls this routine to |
| 3130 ** retrieve the rowid for the current row of the result set. fts3 | 3205 ** retrieve the rowid for the current row of the result set. fts3 |
| 3131 ** exposes %_content.docid as the rowid for the virtual table. The | 3206 ** exposes %_content.docid as the rowid for the virtual table. The |
| 3132 ** rowid should be written to *pRowid. | 3207 ** rowid should be written to *pRowid. |
| 3133 */ | 3208 */ |
| 3134 static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ | 3209 static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ |
| 3135 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; | 3210 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; |
| 3136 if( pCsr->aDoclist ){ | 3211 *pRowid = pCsr->iPrevId; |
| 3137 *pRowid = pCsr->iPrevId; | |
| 3138 }else{ | |
| 3139 /* This branch runs if the query is implemented using a full-table scan | |
| 3140 ** (not using the full-text index). In this case grab the rowid from the | |
| 3141 ** SELECT statement. | |
| 3142 */ | |
| 3143 assert( pCsr->isRequireSeek==0 ); | |
| 3144 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); | |
| 3145 } | |
| 3146 return SQLITE_OK; | 3212 return SQLITE_OK; |
| 3147 } | 3213 } |
| 3148 | 3214 |
| 3149 /* | 3215 /* |
| 3150 ** This is the xColumn method, called by SQLite to request a value from | 3216 ** This is the xColumn method, called by SQLite to request a value from |
| 3151 ** the row that the supplied cursor currently points to. | 3217 ** the row that the supplied cursor currently points to. |
| 3218 ** |
| 3219 ** If: |
| 3220 ** |
| 3221 ** (iCol < p->nColumn) -> The value of the iCol'th user column. |
| 3222 ** (iCol == p->nColumn) -> Magic column with the same name as the table. |
| 3223 ** (iCol == p->nColumn+1) -> Docid column |
| 3224 ** (iCol == p->nColumn+2) -> Langid column |
| 3152 */ | 3225 */ |
| 3153 static int fts3ColumnMethod( | 3226 static int fts3ColumnMethod( |
| 3154 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ | 3227 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ |
| 3155 sqlite3_context *pContext, /* Context for sqlite3_result_xxx() calls */ | 3228 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ |
| 3156 int iCol /* Index of column to read value from */ | 3229 int iCol /* Index of column to read value from */ |
| 3157 ){ | 3230 ){ |
| 3158 int rc; /* Return Code */ | 3231 int rc = SQLITE_OK; /* Return Code */ |
| 3159 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; | 3232 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; |
| 3160 Fts3Table *p = (Fts3Table *)pCursor->pVtab; | 3233 Fts3Table *p = (Fts3Table *)pCursor->pVtab; |
| 3161 | 3234 |
| 3162 /* The column value supplied by SQLite must be in range. */ | 3235 /* The column value supplied by SQLite must be in range. */ |
| 3163 assert( iCol>=0 && iCol<=p->nColumn+1 ); | 3236 assert( iCol>=0 && iCol<=p->nColumn+2 ); |
| 3164 | 3237 |
| 3165 if( iCol==p->nColumn+1 ){ | 3238 if( iCol==p->nColumn+1 ){ |
| 3166 /* This call is a request for the "docid" column. Since "docid" is an | 3239 /* This call is a request for the "docid" column. Since "docid" is an |
| 3167 ** alias for "rowid", use the xRowid() method to obtain the value. | 3240 ** alias for "rowid", use the xRowid() method to obtain the value. |
| 3168 */ | 3241 */ |
| 3169 sqlite3_int64 iRowid; | 3242 sqlite3_result_int64(pCtx, pCsr->iPrevId); |
| 3170 rc = fts3RowidMethod(pCursor, &iRowid); | |
| 3171 sqlite3_result_int64(pContext, iRowid); | |
| 3172 }else if( iCol==p->nColumn ){ | 3243 }else if( iCol==p->nColumn ){ |
| 3173 /* The extra column whose name is the same as the table. | 3244 /* The extra column whose name is the same as the table. |
| 3174 ** Return a blob which is a pointer to the cursor. | 3245 ** Return a blob which is a pointer to the cursor. */ |
| 3175 */ | 3246 sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT); |
| 3176 sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT); | 3247 }else if( iCol==p->nColumn+2 && pCsr->pExpr ){ |
| 3177 rc = SQLITE_OK; | 3248 sqlite3_result_int64(pCtx, pCsr->iLangid); |
| 3178 }else{ | 3249 }else{ |
| 3250 /* The requested column is either a user column (one that contains |
| 3251 ** indexed data), or the language-id column. */ |
| 3179 rc = fts3CursorSeek(0, pCsr); | 3252 rc = fts3CursorSeek(0, pCsr); |
| 3253 |
| 3180 if( rc==SQLITE_OK ){ | 3254 if( rc==SQLITE_OK ){ |
| 3181 sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1)); | 3255 if( iCol==p->nColumn+2 ){ |
| 3182 } | 3256 int iLangid = 0; |
| 3183 } | 3257 if( p->zLanguageid ){ |
| 3258 iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1); |
| 3259 } |
| 3260 sqlite3_result_int(pCtx, iLangid); |
| 3261 }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){ |
| 3262 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1)); |
| 3263 } |
| 3264 } |
| 3265 } |
| 3266 |
| 3267 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 3184 return rc; | 3268 return rc; |
| 3185 } | 3269 } |
| 3186 | 3270 |
| 3187 /* | 3271 /* |
| 3188 ** This function is the implementation of the xUpdate callback used by | 3272 ** This function is the implementation of the xUpdate callback used by |
| 3189 ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be | 3273 ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be |
| 3190 ** inserted, updated or deleted. | 3274 ** inserted, updated or deleted. |
| 3191 */ | 3275 */ |
| 3192 static int fts3UpdateMethod( | 3276 static int fts3UpdateMethod( |
| 3193 sqlite3_vtab *pVtab, /* Virtual table handle */ | 3277 sqlite3_vtab *pVtab, /* Virtual table handle */ |
| 3194 int nArg, /* Size of argument array */ | 3278 int nArg, /* Size of argument array */ |
| 3195 sqlite3_value **apVal, /* Array of arguments */ | 3279 sqlite3_value **apVal, /* Array of arguments */ |
| 3196 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ | 3280 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ |
| 3197 ){ | 3281 ){ |
| 3198 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid); | 3282 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid); |
| 3199 } | 3283 } |
| 3200 | 3284 |
| 3201 /* | 3285 /* |
| 3202 ** Implementation of xSync() method. Flush the contents of the pending-terms | 3286 ** Implementation of xSync() method. Flush the contents of the pending-terms |
| 3203 ** hash-table to the database. | 3287 ** hash-table to the database. |
| 3204 */ | 3288 */ |
| 3205 static int fts3SyncMethod(sqlite3_vtab *pVtab){ | 3289 static int fts3SyncMethod(sqlite3_vtab *pVtab){ |
| 3206 int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab); | 3290 |
| 3207 sqlite3Fts3SegmentsClose((Fts3Table *)pVtab); | 3291 /* Following an incremental-merge operation, assuming that the input |
| 3292 ** segments are not completely consumed (the usual case), they are updated |
| 3293 ** in place to remove the entries that have already been merged. This |
| 3294 ** involves updating the leaf block that contains the smallest unmerged |
| 3295 ** entry and each block (if any) between the leaf and the root node. So |
| 3296 ** if the height of the input segment b-trees is N, and input segments |
| 3297 ** are merged eight at a time, updating the input segments at the end |
| 3298 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually |
| 3299 ** small - often between 0 and 2. So the overhead of the incremental |
| 3300 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead |
| 3301 ** dwarfing the actual productive work accomplished, the incremental merge |
| 3302 ** is only attempted if it will write at least 64 leaf blocks. Hence |
| 3303 ** nMinMerge. |
| 3304 ** |
| 3305 ** Of course, updating the input segments also involves deleting a bunch |
| 3306 ** of blocks from the segments table. But this is not considered overhead |
| 3307 ** as it would also be required by a crisis-merge that used the same input |
| 3308 ** segments. |
| 3309 */ |
| 3310 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */ |
| 3311 |
| 3312 Fts3Table *p = (Fts3Table*)pVtab; |
| 3313 int rc = sqlite3Fts3PendingTermsFlush(p); |
| 3314 |
| 3315 if( rc==SQLITE_OK |
| 3316 && p->nLeafAdd>(nMinMerge/16) |
| 3317 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff |
| 3318 ){ |
| 3319 int mxLevel = 0; /* Maximum relative level value in db */ |
| 3320 int A; /* Incr-merge parameter A */ |
| 3321 |
| 3322 rc = sqlite3Fts3MaxLevel(p, &mxLevel); |
| 3323 assert( rc==SQLITE_OK || mxLevel==0 ); |
| 3324 A = p->nLeafAdd * mxLevel; |
| 3325 A += (A/2); |
| 3326 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge); |
| 3327 } |
| 3328 sqlite3Fts3SegmentsClose(p); |
| 3208 return rc; | 3329 return rc; |
| 3209 } | 3330 } |
| 3210 | 3331 |
| 3211 /* | 3332 /* |
| 3212 ** Implementation of xBegin() method. This is a no-op. | 3333 ** If it is currently unknown whether or not the FTS table has an %_stat |
| 3334 ** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat |
| 3335 ** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code |
| 3336 ** if an error occurs. |
| 3337 */ |
| 3338 static int fts3SetHasStat(Fts3Table *p){ |
| 3339 int rc = SQLITE_OK; |
| 3340 if( p->bHasStat==2 ){ |
| 3341 const char *zFmt ="SELECT 1 FROM %Q.sqlite_master WHERE tbl_name='%q_stat'"; |
| 3342 char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName); |
| 3343 if( zSql ){ |
| 3344 sqlite3_stmt *pStmt = 0; |
| 3345 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3346 if( rc==SQLITE_OK ){ |
| 3347 int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW); |
| 3348 rc = sqlite3_finalize(pStmt); |
| 3349 if( rc==SQLITE_OK ) p->bHasStat = bHasStat; |
| 3350 } |
| 3351 sqlite3_free(zSql); |
| 3352 }else{ |
| 3353 rc = SQLITE_NOMEM; |
| 3354 } |
| 3355 } |
| 3356 return rc; |
| 3357 } |
| 3358 |
| 3359 /* |
| 3360 ** Implementation of xBegin() method. |
| 3213 */ | 3361 */ |
| 3214 static int fts3BeginMethod(sqlite3_vtab *pVtab){ | 3362 static int fts3BeginMethod(sqlite3_vtab *pVtab){ |
| 3363 Fts3Table *p = (Fts3Table*)pVtab; |
| 3215 UNUSED_PARAMETER(pVtab); | 3364 UNUSED_PARAMETER(pVtab); |
| 3216 assert( ((Fts3Table *)pVtab)->nPendingData==0 ); | 3365 assert( p->pSegments==0 ); |
| 3217 return SQLITE_OK; | 3366 assert( p->nPendingData==0 ); |
| 3367 assert( p->inTransaction!=1 ); |
| 3368 TESTONLY( p->inTransaction = 1 ); |
| 3369 TESTONLY( p->mxSavepoint = -1; ); |
| 3370 p->nLeafAdd = 0; |
| 3371 return fts3SetHasStat(p); |
| 3218 } | 3372 } |
| 3219 | 3373 |
| 3220 /* | 3374 /* |
| 3221 ** Implementation of xCommit() method. This is a no-op. The contents of | 3375 ** Implementation of xCommit() method. This is a no-op. The contents of |
| 3222 ** the pending-terms hash-table have already been flushed into the database | 3376 ** the pending-terms hash-table have already been flushed into the database |
| 3223 ** by fts3SyncMethod(). | 3377 ** by fts3SyncMethod(). |
| 3224 */ | 3378 */ |
| 3225 static int fts3CommitMethod(sqlite3_vtab *pVtab){ | 3379 static int fts3CommitMethod(sqlite3_vtab *pVtab){ |
| 3380 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab ); |
| 3226 UNUSED_PARAMETER(pVtab); | 3381 UNUSED_PARAMETER(pVtab); |
| 3227 assert( ((Fts3Table *)pVtab)->nPendingData==0 ); | 3382 assert( p->nPendingData==0 ); |
| 3383 assert( p->inTransaction!=0 ); |
| 3384 assert( p->pSegments==0 ); |
| 3385 TESTONLY( p->inTransaction = 0 ); |
| 3386 TESTONLY( p->mxSavepoint = -1; ); |
| 3228 return SQLITE_OK; | 3387 return SQLITE_OK; |
| 3229 } | 3388 } |
| 3230 | 3389 |
| 3231 /* | 3390 /* |
| 3232 ** Implementation of xRollback(). Discard the contents of the pending-terms | 3391 ** Implementation of xRollback(). Discard the contents of the pending-terms |
| 3233 ** hash-table. Any changes made to the database are reverted by SQLite. | 3392 ** hash-table. Any changes made to the database are reverted by SQLite. |
| 3234 */ | 3393 */ |
| 3235 static int fts3RollbackMethod(sqlite3_vtab *pVtab){ | 3394 static int fts3RollbackMethod(sqlite3_vtab *pVtab){ |
| 3236 sqlite3Fts3PendingTermsClear((Fts3Table *)pVtab); | 3395 Fts3Table *p = (Fts3Table*)pVtab; |
| 3396 sqlite3Fts3PendingTermsClear(p); |
| 3397 assert( p->inTransaction!=0 ); |
| 3398 TESTONLY( p->inTransaction = 0 ); |
| 3399 TESTONLY( p->mxSavepoint = -1; ); |
| 3237 return SQLITE_OK; | 3400 return SQLITE_OK; |
| 3238 } | 3401 } |
| 3239 | 3402 |
| 3240 /* | 3403 /* |
| 3241 ** Load the doclist associated with expression pExpr to pExpr->aDoclist. | 3404 ** When called, *ppPoslist must point to the byte immediately following the |
| 3242 ** The loaded doclist contains positions as well as the document ids. | 3405 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function |
| 3243 ** This is used by the matchinfo(), snippet() and offsets() auxillary | 3406 ** moves *ppPoslist so that it instead points to the first byte of the |
| 3244 ** functions. | 3407 ** same position list. |
| 3245 */ | 3408 */ |
| 3246 int sqlite3Fts3ExprLoadDoclist(Fts3Cursor *pCsr, Fts3Expr *pExpr){ | 3409 static void fts3ReversePoslist(char *pStart, char **ppPoslist){ |
| 3247 int rc; | 3410 char *p = &(*ppPoslist)[-2]; |
| 3248 assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase ); | 3411 char c = 0; |
| 3249 assert( pCsr->eEvalmode==FTS3_EVAL_NEXT ); | 3412 |
| 3250 rc = fts3EvalExpr(pCsr, pExpr, &pExpr->aDoclist, &pExpr->nDoclist, 1); | 3413 while( p>pStart && (c=*p--)==0 ); |
| 3251 return rc; | 3414 while( p>pStart && (*p & 0x80) | c ){ |
| 3252 } | 3415 c = *p--; |
| 3253 | 3416 } |
| 3254 int sqlite3Fts3ExprLoadFtDoclist( | 3417 if( p>pStart ){ p = &p[2]; } |
| 3255 Fts3Cursor *pCsr, | 3418 while( *p++&0x80 ); |
| 3256 Fts3Expr *pExpr, | 3419 *ppPoslist = p; |
| 3257 char **paDoclist, | |
| 3258 int *pnDoclist | |
| 3259 ){ | |
| 3260 int rc; | |
| 3261 assert( pCsr->eEvalmode==FTS3_EVAL_NEXT ); | |
| 3262 assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase ); | |
| 3263 pCsr->eEvalmode = FTS3_EVAL_MATCHINFO; | |
| 3264 rc = fts3EvalExpr(pCsr, pExpr, paDoclist, pnDoclist, 1); | |
| 3265 pCsr->eEvalmode = FTS3_EVAL_NEXT; | |
| 3266 return rc; | |
| 3267 } | |
| 3268 | |
| 3269 /* | |
| 3270 ** After ExprLoadDoclist() (see above) has been called, this function is | |
| 3271 ** used to iterate/search through the position lists that make up the doclist | |
| 3272 ** stored in pExpr->aDoclist. | |
| 3273 */ | |
| 3274 char *sqlite3Fts3FindPositions( | |
| 3275 Fts3Expr *pExpr, /* Access this expressions doclist */ | |
| 3276 sqlite3_int64 iDocid, /* Docid associated with requested pos-list */ | |
| 3277 int iCol /* Column of requested pos-list */ | |
| 3278 ){ | |
| 3279 assert( pExpr->isLoaded ); | |
| 3280 if( pExpr->aDoclist ){ | |
| 3281 char *pEnd = &pExpr->aDoclist[pExpr->nDoclist]; | |
| 3282 char *pCsr; | |
| 3283 | |
| 3284 if( pExpr->pCurrent==0 ){ | |
| 3285 pExpr->pCurrent = pExpr->aDoclist; | |
| 3286 pExpr->iCurrent = 0; | |
| 3287 pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent,&pExpr->iCurrent); | |
| 3288 } | |
| 3289 pCsr = pExpr->pCurrent; | |
| 3290 assert( pCsr ); | |
| 3291 | |
| 3292 while( pCsr<pEnd ){ | |
| 3293 if( pExpr->iCurrent<iDocid ){ | |
| 3294 fts3PoslistCopy(0, &pCsr); | |
| 3295 if( pCsr<pEnd ){ | |
| 3296 fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent); | |
| 3297 } | |
| 3298 pExpr->pCurrent = pCsr; | |
| 3299 }else{ | |
| 3300 if( pExpr->iCurrent==iDocid ){ | |
| 3301 int iThis = 0; | |
| 3302 if( iCol<0 ){ | |
| 3303 /* If iCol is negative, return a pointer to the start of the | |
| 3304 ** position-list (instead of a pointer to the start of a list | |
| 3305 ** of offsets associated with a specific column). | |
| 3306 */ | |
| 3307 return pCsr; | |
| 3308 } | |
| 3309 while( iThis<iCol ){ | |
| 3310 fts3ColumnlistCopy(0, &pCsr); | |
| 3311 if( *pCsr==0x00 ) return 0; | |
| 3312 pCsr++; | |
| 3313 pCsr += sqlite3Fts3GetVarint32(pCsr, &iThis); | |
| 3314 } | |
| 3315 if( iCol==iThis && (*pCsr&0xFE) ) return pCsr; | |
| 3316 } | |
| 3317 return 0; | |
| 3318 } | |
| 3319 } | |
| 3320 } | |
| 3321 | |
| 3322 return 0; | |
| 3323 } | 3420 } |
| 3324 | 3421 |
| 3325 /* | 3422 /* |
| 3326 ** Helper function used by the implementation of the overloaded snippet(), | 3423 ** Helper function used by the implementation of the overloaded snippet(), |
| 3327 ** offsets() and optimize() SQL functions. | 3424 ** offsets() and optimize() SQL functions. |
| 3328 ** | 3425 ** |
| 3329 ** If the value passed as the third argument is a blob of size | 3426 ** If the value passed as the third argument is a blob of size |
| 3330 ** sizeof(Fts3Cursor*), then the blob contents are copied to the | 3427 ** sizeof(Fts3Cursor*), then the blob contents are copied to the |
| 3331 ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error | 3428 ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error |
| 3332 ** message is written to context pContext and SQLITE_ERROR returned. The | 3429 ** message is written to context pContext and SQLITE_ERROR returned. The |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3513 ** Implementation of FTS3 xRename method. Rename an fts3 table. | 3610 ** Implementation of FTS3 xRename method. Rename an fts3 table. |
| 3514 */ | 3611 */ |
| 3515 static int fts3RenameMethod( | 3612 static int fts3RenameMethod( |
| 3516 sqlite3_vtab *pVtab, /* Virtual table handle */ | 3613 sqlite3_vtab *pVtab, /* Virtual table handle */ |
| 3517 const char *zName /* New name of table */ | 3614 const char *zName /* New name of table */ |
| 3518 ){ | 3615 ){ |
| 3519 Fts3Table *p = (Fts3Table *)pVtab; | 3616 Fts3Table *p = (Fts3Table *)pVtab; |
| 3520 sqlite3 *db = p->db; /* Database connection */ | 3617 sqlite3 *db = p->db; /* Database connection */ |
| 3521 int rc; /* Return Code */ | 3618 int rc; /* Return Code */ |
| 3522 | 3619 |
| 3523 rc = sqlite3Fts3PendingTermsFlush(p); | 3620 /* At this point it must be known if the %_stat table exists or not. |
| 3524 if( rc!=SQLITE_OK ){ | 3621 ** So bHasStat may not be 2. */ |
| 3525 return rc; | 3622 rc = fts3SetHasStat(p); |
| 3623 |
| 3624 /* As it happens, the pending terms table is always empty here. This is |
| 3625 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction |
| 3626 ** always opens a savepoint transaction. And the xSavepoint() method |
| 3627 ** flushes the pending terms table. But leave the (no-op) call to |
| 3628 ** PendingTermsFlush() in in case that changes. |
| 3629 */ |
| 3630 assert( p->nPendingData==0 ); |
| 3631 if( rc==SQLITE_OK ){ |
| 3632 rc = sqlite3Fts3PendingTermsFlush(p); |
| 3526 } | 3633 } |
| 3527 | 3634 |
| 3528 fts3DbExec(&rc, db, | 3635 if( p->zContentTbl==0 ){ |
| 3529 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", | 3636 fts3DbExec(&rc, db, |
| 3530 p->zDb, p->zName, zName | 3637 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", |
| 3531 ); | 3638 p->zDb, p->zName, zName |
| 3639 ); |
| 3640 } |
| 3641 |
| 3532 if( p->bHasDocsize ){ | 3642 if( p->bHasDocsize ){ |
| 3533 fts3DbExec(&rc, db, | 3643 fts3DbExec(&rc, db, |
| 3534 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';", | 3644 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';", |
| 3535 p->zDb, p->zName, zName | 3645 p->zDb, p->zName, zName |
| 3536 ); | 3646 ); |
| 3537 } | 3647 } |
| 3538 if( p->bHasStat ){ | 3648 if( p->bHasStat ){ |
| 3539 fts3DbExec(&rc, db, | 3649 fts3DbExec(&rc, db, |
| 3540 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';", | 3650 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';", |
| 3541 p->zDb, p->zName, zName | 3651 p->zDb, p->zName, zName |
| 3542 ); | 3652 ); |
| 3543 } | 3653 } |
| 3544 fts3DbExec(&rc, db, | 3654 fts3DbExec(&rc, db, |
| 3545 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';", | 3655 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';", |
| 3546 p->zDb, p->zName, zName | 3656 p->zDb, p->zName, zName |
| 3547 ); | 3657 ); |
| 3548 fts3DbExec(&rc, db, | 3658 fts3DbExec(&rc, db, |
| 3549 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';", | 3659 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';", |
| 3550 p->zDb, p->zName, zName | 3660 p->zDb, p->zName, zName |
| 3551 ); | 3661 ); |
| 3552 return rc; | 3662 return rc; |
| 3553 } | 3663 } |
| 3554 | 3664 |
| 3665 /* |
| 3666 ** The xSavepoint() method. |
| 3667 ** |
| 3668 ** Flush the contents of the pending-terms table to disk. |
| 3669 */ |
| 3670 static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){ |
| 3671 int rc = SQLITE_OK; |
| 3672 UNUSED_PARAMETER(iSavepoint); |
| 3673 assert( ((Fts3Table *)pVtab)->inTransaction ); |
| 3674 assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint ); |
| 3675 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint ); |
| 3676 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){ |
| 3677 rc = fts3SyncMethod(pVtab); |
| 3678 } |
| 3679 return rc; |
| 3680 } |
| 3681 |
| 3682 /* |
| 3683 ** The xRelease() method. |
| 3684 ** |
| 3685 ** This is a no-op. |
| 3686 */ |
| 3687 static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){ |
| 3688 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab ); |
| 3689 UNUSED_PARAMETER(iSavepoint); |
| 3690 UNUSED_PARAMETER(pVtab); |
| 3691 assert( p->inTransaction ); |
| 3692 assert( p->mxSavepoint >= iSavepoint ); |
| 3693 TESTONLY( p->mxSavepoint = iSavepoint-1 ); |
| 3694 return SQLITE_OK; |
| 3695 } |
| 3696 |
| 3697 /* |
| 3698 ** The xRollbackTo() method. |
| 3699 ** |
| 3700 ** Discard the contents of the pending terms table. |
| 3701 */ |
| 3702 static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){ |
| 3703 Fts3Table *p = (Fts3Table*)pVtab; |
| 3704 UNUSED_PARAMETER(iSavepoint); |
| 3705 assert( p->inTransaction ); |
| 3706 assert( p->mxSavepoint >= iSavepoint ); |
| 3707 TESTONLY( p->mxSavepoint = iSavepoint ); |
| 3708 sqlite3Fts3PendingTermsClear(p); |
| 3709 return SQLITE_OK; |
| 3710 } |
| 3711 |
| 3555 static const sqlite3_module fts3Module = { | 3712 static const sqlite3_module fts3Module = { |
| 3556 /* iVersion */ 0, | 3713 /* iVersion */ 2, |
| 3557 /* xCreate */ fts3CreateMethod, | 3714 /* xCreate */ fts3CreateMethod, |
| 3558 /* xConnect */ fts3ConnectMethod, | 3715 /* xConnect */ fts3ConnectMethod, |
| 3559 /* xBestIndex */ fts3BestIndexMethod, | 3716 /* xBestIndex */ fts3BestIndexMethod, |
| 3560 /* xDisconnect */ fts3DisconnectMethod, | 3717 /* xDisconnect */ fts3DisconnectMethod, |
| 3561 /* xDestroy */ fts3DestroyMethod, | 3718 /* xDestroy */ fts3DestroyMethod, |
| 3562 /* xOpen */ fts3OpenMethod, | 3719 /* xOpen */ fts3OpenMethod, |
| 3563 /* xClose */ fts3CloseMethod, | 3720 /* xClose */ fts3CloseMethod, |
| 3564 /* xFilter */ fts3FilterMethod, | 3721 /* xFilter */ fts3FilterMethod, |
| 3565 /* xNext */ fts3NextMethod, | 3722 /* xNext */ fts3NextMethod, |
| 3566 /* xEof */ fts3EofMethod, | 3723 /* xEof */ fts3EofMethod, |
| 3567 /* xColumn */ fts3ColumnMethod, | 3724 /* xColumn */ fts3ColumnMethod, |
| 3568 /* xRowid */ fts3RowidMethod, | 3725 /* xRowid */ fts3RowidMethod, |
| 3569 /* xUpdate */ fts3UpdateMethod, | 3726 /* xUpdate */ fts3UpdateMethod, |
| 3570 /* xBegin */ fts3BeginMethod, | 3727 /* xBegin */ fts3BeginMethod, |
| 3571 /* xSync */ fts3SyncMethod, | 3728 /* xSync */ fts3SyncMethod, |
| 3572 /* xCommit */ fts3CommitMethod, | 3729 /* xCommit */ fts3CommitMethod, |
| 3573 /* xRollback */ fts3RollbackMethod, | 3730 /* xRollback */ fts3RollbackMethod, |
| 3574 /* xFindFunction */ fts3FindFunctionMethod, | 3731 /* xFindFunction */ fts3FindFunctionMethod, |
| 3575 /* xRename */ fts3RenameMethod, | 3732 /* xRename */ fts3RenameMethod, |
| 3733 /* xSavepoint */ fts3SavepointMethod, |
| 3734 /* xRelease */ fts3ReleaseMethod, |
| 3735 /* xRollbackTo */ fts3RollbackToMethod, |
| 3576 }; | 3736 }; |
| 3577 | 3737 |
| 3578 /* | 3738 /* |
| 3579 ** This function is registered as the module destructor (called when an | 3739 ** This function is registered as the module destructor (called when an |
| 3580 ** FTS3 enabled database connection is closed). It frees the memory | 3740 ** FTS3 enabled database connection is closed). It frees the memory |
| 3581 ** allocated for the tokenizer hash table. | 3741 ** allocated for the tokenizer hash table. |
| 3582 */ | 3742 */ |
| 3583 static void hashDestroy(void *p){ | 3743 static void hashDestroy(void *p){ |
| 3584 Fts3Hash *pHash = (Fts3Hash *)p; | 3744 Fts3Hash *pHash = (Fts3Hash *)p; |
| 3585 sqlite3Fts3HashClear(pHash); | 3745 sqlite3Fts3HashClear(pHash); |
| 3586 sqlite3_free(pHash); | 3746 sqlite3_free(pHash); |
| 3587 } | 3747 } |
| 3588 | 3748 |
| 3589 /* | 3749 /* |
| 3590 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are | 3750 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are |
| 3591 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c | 3751 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c |
| 3592 ** respectively. The following three forward declarations are for functions | 3752 ** respectively. The following three forward declarations are for functions |
| 3593 ** declared in these files used to retrieve the respective implementations. | 3753 ** declared in these files used to retrieve the respective implementations. |
| 3594 ** | 3754 ** |
| 3595 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed | 3755 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed |
| 3596 ** to by the argument to point to the "simple" tokenizer implementation. | 3756 ** to by the argument to point to the "simple" tokenizer implementation. |
| 3597 ** And so on. | 3757 ** And so on. |
| 3598 */ | 3758 */ |
| 3599 void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); | 3759 void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
| 3600 void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); | 3760 void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
| 3761 #ifndef SQLITE_DISABLE_FTS3_UNICODE |
| 3762 void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule); |
| 3763 #endif |
| 3601 #ifdef SQLITE_ENABLE_ICU | 3764 #ifdef SQLITE_ENABLE_ICU |
| 3602 void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); | 3765 void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
| 3603 #endif | 3766 #endif |
| 3604 | 3767 |
| 3605 /* | 3768 /* |
| 3606 ** Initialise the fts3 extension. If this extension is built as part | 3769 ** Initialize the fts3 extension. If this extension is built as part |
| 3607 ** of the sqlite library, then this function is called directly by | 3770 ** of the sqlite library, then this function is called directly by |
| 3608 ** SQLite. If fts3 is built as a dynamically loadable extension, this | 3771 ** SQLite. If fts3 is built as a dynamically loadable extension, this |
| 3609 ** function is called by the sqlite3_extension_init() entry point. | 3772 ** function is called by the sqlite3_extension_init() entry point. |
| 3610 */ | 3773 */ |
| 3611 int sqlite3Fts3Init(sqlite3 *db){ | 3774 int sqlite3Fts3Init(sqlite3 *db){ |
| 3612 int rc = SQLITE_OK; | 3775 int rc = SQLITE_OK; |
| 3613 Fts3Hash *pHash = 0; | 3776 Fts3Hash *pHash = 0; |
| 3614 const sqlite3_tokenizer_module *pSimple = 0; | 3777 const sqlite3_tokenizer_module *pSimple = 0; |
| 3615 const sqlite3_tokenizer_module *pPorter = 0; | 3778 const sqlite3_tokenizer_module *pPorter = 0; |
| 3779 #ifndef SQLITE_DISABLE_FTS3_UNICODE |
| 3780 const sqlite3_tokenizer_module *pUnicode = 0; |
| 3781 #endif |
| 3616 | 3782 |
| 3617 #ifdef SQLITE_ENABLE_ICU | 3783 #ifdef SQLITE_ENABLE_ICU |
| 3618 const sqlite3_tokenizer_module *pIcu = 0; | 3784 const sqlite3_tokenizer_module *pIcu = 0; |
| 3619 sqlite3Fts3IcuTokenizerModule(&pIcu); | 3785 sqlite3Fts3IcuTokenizerModule(&pIcu); |
| 3620 #endif | 3786 #endif |
| 3621 | 3787 |
| 3788 #ifndef SQLITE_DISABLE_FTS3_UNICODE |
| 3789 sqlite3Fts3UnicodeTokenizer(&pUnicode); |
| 3790 #endif |
| 3791 |
| 3792 #ifdef SQLITE_TEST |
| 3793 rc = sqlite3Fts3InitTerm(db); |
| 3794 if( rc!=SQLITE_OK ) return rc; |
| 3795 #endif |
| 3796 |
| 3622 rc = sqlite3Fts3InitAux(db); | 3797 rc = sqlite3Fts3InitAux(db); |
| 3623 if( rc!=SQLITE_OK ) return rc; | 3798 if( rc!=SQLITE_OK ) return rc; |
| 3624 | 3799 |
| 3625 sqlite3Fts3SimpleTokenizerModule(&pSimple); | 3800 sqlite3Fts3SimpleTokenizerModule(&pSimple); |
| 3626 sqlite3Fts3PorterTokenizerModule(&pPorter); | 3801 sqlite3Fts3PorterTokenizerModule(&pPorter); |
| 3627 | 3802 |
| 3628 /* Allocate and initialise the hash-table used to store tokenizers. */ | 3803 /* Allocate and initialize the hash-table used to store tokenizers. */ |
| 3629 pHash = sqlite3_malloc(sizeof(Fts3Hash)); | 3804 pHash = sqlite3_malloc(sizeof(Fts3Hash)); |
| 3630 if( !pHash ){ | 3805 if( !pHash ){ |
| 3631 rc = SQLITE_NOMEM; | 3806 rc = SQLITE_NOMEM; |
| 3632 }else{ | 3807 }else{ |
| 3633 sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); | 3808 sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); |
| 3634 } | 3809 } |
| 3635 | 3810 |
| 3636 /* Load the built-in tokenizers into the hash table */ | 3811 /* Load the built-in tokenizers into the hash table */ |
| 3637 if( rc==SQLITE_OK ){ | 3812 if( rc==SQLITE_OK ){ |
| 3638 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) | 3813 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) |
| 3639 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) | 3814 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) |
| 3815 |
| 3816 #ifndef SQLITE_DISABLE_FTS3_UNICODE |
| 3817 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) |
| 3818 #endif |
| 3640 #ifdef SQLITE_ENABLE_ICU | 3819 #ifdef SQLITE_ENABLE_ICU |
| 3641 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) | 3820 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) |
| 3642 #endif | 3821 #endif |
| 3643 ){ | 3822 ){ |
| 3644 rc = SQLITE_NOMEM; | 3823 rc = SQLITE_NOMEM; |
| 3645 } | 3824 } |
| 3646 } | 3825 } |
| 3647 | 3826 |
| 3648 #ifdef SQLITE_TEST | 3827 #ifdef SQLITE_TEST |
| 3649 if( rc==SQLITE_OK ){ | 3828 if( rc==SQLITE_OK ){ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 3664 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) | 3843 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) |
| 3665 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1)) | 3844 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1)) |
| 3666 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1)) | 3845 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1)) |
| 3667 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2)) | 3846 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2)) |
| 3668 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1)) | 3847 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1)) |
| 3669 ){ | 3848 ){ |
| 3670 rc = sqlite3_create_module_v2( | 3849 rc = sqlite3_create_module_v2( |
| 3671 db, "fts3", &fts3Module, (void *)pHash, hashDestroy | 3850 db, "fts3", &fts3Module, (void *)pHash, hashDestroy |
| 3672 ); | 3851 ); |
| 3673 #if CHROMIUM_FTS3_CHANGES && !SQLITE_TEST | 3852 #if CHROMIUM_FTS3_CHANGES && !SQLITE_TEST |
| 3674 /* Disable fts4 pending review. */ | 3853 /* Disable fts4 and tokenizer vtab pending review. */ |
| 3675 #else | 3854 #else |
| 3676 if( rc==SQLITE_OK ){ | 3855 if( rc==SQLITE_OK ){ |
| 3677 rc = sqlite3_create_module_v2( | 3856 rc = sqlite3_create_module_v2( |
| 3678 db, "fts4", &fts3Module, (void *)pHash, 0 | 3857 db, "fts4", &fts3Module, (void *)pHash, 0 |
| 3679 ); | 3858 ); |
| 3680 } | 3859 } |
| 3860 if( rc==SQLITE_OK ){ |
| 3861 rc = sqlite3Fts3InitTok(db, (void *)pHash); |
| 3862 } |
| 3681 #endif | 3863 #endif |
| 3682 return rc; | 3864 return rc; |
| 3683 } | 3865 } |
| 3684 | 3866 |
| 3867 |
| 3685 /* An error has occurred. Delete the hash table and return the error code. */ | 3868 /* An error has occurred. Delete the hash table and return the error code. */ |
| 3686 assert( rc!=SQLITE_OK ); | 3869 assert( rc!=SQLITE_OK ); |
| 3687 if( pHash ){ | 3870 if( pHash ){ |
| 3688 sqlite3Fts3HashClear(pHash); | 3871 sqlite3Fts3HashClear(pHash); |
| 3689 sqlite3_free(pHash); | 3872 sqlite3_free(pHash); |
| 3690 } | 3873 } |
| 3691 return rc; | 3874 return rc; |
| 3692 } | 3875 } |
| 3693 | 3876 |
| 3877 /* |
| 3878 ** Allocate an Fts3MultiSegReader for each token in the expression headed |
| 3879 ** by pExpr. |
| 3880 ** |
| 3881 ** An Fts3SegReader object is a cursor that can seek or scan a range of |
| 3882 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple |
| 3883 ** Fts3SegReader objects internally to provide an interface to seek or scan |
| 3884 ** within the union of all segments of a b-tree. Hence the name. |
| 3885 ** |
| 3886 ** If the allocated Fts3MultiSegReader just seeks to a single entry in a |
| 3887 ** segment b-tree (if the term is not a prefix or it is a prefix for which |
| 3888 ** there exists prefix b-tree of the right length) then it may be traversed |
| 3889 ** and merged incrementally. Otherwise, it has to be merged into an in-memory |
| 3890 ** doclist and then traversed. |
| 3891 */ |
| 3892 static void fts3EvalAllocateReaders( |
| 3893 Fts3Cursor *pCsr, /* FTS cursor handle */ |
| 3894 Fts3Expr *pExpr, /* Allocate readers for this expression */ |
| 3895 int *pnToken, /* OUT: Total number of tokens in phrase. */ |
| 3896 int *pnOr, /* OUT: Total number of OR nodes in expr. */ |
| 3897 int *pRc /* IN/OUT: Error code */ |
| 3898 ){ |
| 3899 if( pExpr && SQLITE_OK==*pRc ){ |
| 3900 if( pExpr->eType==FTSQUERY_PHRASE ){ |
| 3901 int i; |
| 3902 int nToken = pExpr->pPhrase->nToken; |
| 3903 *pnToken += nToken; |
| 3904 for(i=0; i<nToken; i++){ |
| 3905 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i]; |
| 3906 int rc = fts3TermSegReaderCursor(pCsr, |
| 3907 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr |
| 3908 ); |
| 3909 if( rc!=SQLITE_OK ){ |
| 3910 *pRc = rc; |
| 3911 return; |
| 3912 } |
| 3913 } |
| 3914 assert( pExpr->pPhrase->iDoclistToken==0 ); |
| 3915 pExpr->pPhrase->iDoclistToken = -1; |
| 3916 }else{ |
| 3917 *pnOr += (pExpr->eType==FTSQUERY_OR); |
| 3918 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc); |
| 3919 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc); |
| 3920 } |
| 3921 } |
| 3922 } |
| 3923 |
| 3924 /* |
| 3925 ** Arguments pList/nList contain the doclist for token iToken of phrase p. |
| 3926 ** It is merged into the main doclist stored in p->doclist.aAll/nAll. |
| 3927 ** |
| 3928 ** This function assumes that pList points to a buffer allocated using |
| 3929 ** sqlite3_malloc(). This function takes responsibility for eventually |
| 3930 ** freeing the buffer. |
| 3931 */ |
| 3932 static void fts3EvalPhraseMergeToken( |
| 3933 Fts3Table *pTab, /* FTS Table pointer */ |
| 3934 Fts3Phrase *p, /* Phrase to merge pList/nList into */ |
| 3935 int iToken, /* Token pList/nList corresponds to */ |
| 3936 char *pList, /* Pointer to doclist */ |
| 3937 int nList /* Number of bytes in pList */ |
| 3938 ){ |
| 3939 assert( iToken!=p->iDoclistToken ); |
| 3940 |
| 3941 if( pList==0 ){ |
| 3942 sqlite3_free(p->doclist.aAll); |
| 3943 p->doclist.aAll = 0; |
| 3944 p->doclist.nAll = 0; |
| 3945 } |
| 3946 |
| 3947 else if( p->iDoclistToken<0 ){ |
| 3948 p->doclist.aAll = pList; |
| 3949 p->doclist.nAll = nList; |
| 3950 } |
| 3951 |
| 3952 else if( p->doclist.aAll==0 ){ |
| 3953 sqlite3_free(pList); |
| 3954 } |
| 3955 |
| 3956 else { |
| 3957 char *pLeft; |
| 3958 char *pRight; |
| 3959 int nLeft; |
| 3960 int nRight; |
| 3961 int nDiff; |
| 3962 |
| 3963 if( p->iDoclistToken<iToken ){ |
| 3964 pLeft = p->doclist.aAll; |
| 3965 nLeft = p->doclist.nAll; |
| 3966 pRight = pList; |
| 3967 nRight = nList; |
| 3968 nDiff = iToken - p->iDoclistToken; |
| 3969 }else{ |
| 3970 pRight = p->doclist.aAll; |
| 3971 nRight = p->doclist.nAll; |
| 3972 pLeft = pList; |
| 3973 nLeft = nList; |
| 3974 nDiff = p->iDoclistToken - iToken; |
| 3975 } |
| 3976 |
| 3977 fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight); |
| 3978 sqlite3_free(pLeft); |
| 3979 p->doclist.aAll = pRight; |
| 3980 p->doclist.nAll = nRight; |
| 3981 } |
| 3982 |
| 3983 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken; |
| 3984 } |
| 3985 |
| 3986 /* |
| 3987 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist |
| 3988 ** does not take deferred tokens into account. |
| 3989 ** |
| 3990 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. |
| 3991 */ |
| 3992 static int fts3EvalPhraseLoad( |
| 3993 Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 3994 Fts3Phrase *p /* Phrase object */ |
| 3995 ){ |
| 3996 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 3997 int iToken; |
| 3998 int rc = SQLITE_OK; |
| 3999 |
| 4000 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){ |
| 4001 Fts3PhraseToken *pToken = &p->aToken[iToken]; |
| 4002 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 ); |
| 4003 |
| 4004 if( pToken->pSegcsr ){ |
| 4005 int nThis = 0; |
| 4006 char *pThis = 0; |
| 4007 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis); |
| 4008 if( rc==SQLITE_OK ){ |
| 4009 fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis); |
| 4010 } |
| 4011 } |
| 4012 assert( pToken->pSegcsr==0 ); |
| 4013 } |
| 4014 |
| 4015 return rc; |
| 4016 } |
| 4017 |
| 4018 /* |
| 4019 ** This function is called on each phrase after the position lists for |
| 4020 ** any deferred tokens have been loaded into memory. It updates the phrases |
| 4021 ** current position list to include only those positions that are really |
| 4022 ** instances of the phrase (after considering deferred tokens). If this |
| 4023 ** means that the phrase does not appear in the current row, doclist.pList |
| 4024 ** and doclist.nList are both zeroed. |
| 4025 ** |
| 4026 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. |
| 4027 */ |
| 4028 static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ |
| 4029 int iToken; /* Used to iterate through phrase tokens */ |
| 4030 char *aPoslist = 0; /* Position list for deferred tokens */ |
| 4031 int nPoslist = 0; /* Number of bytes in aPoslist */ |
| 4032 int iPrev = -1; /* Token number of previous deferred token */ |
| 4033 |
| 4034 assert( pPhrase->doclist.bFreeList==0 ); |
| 4035 |
| 4036 for(iToken=0; iToken<pPhrase->nToken; iToken++){ |
| 4037 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken]; |
| 4038 Fts3DeferredToken *pDeferred = pToken->pDeferred; |
| 4039 |
| 4040 if( pDeferred ){ |
| 4041 char *pList; |
| 4042 int nList; |
| 4043 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList); |
| 4044 if( rc!=SQLITE_OK ) return rc; |
| 4045 |
| 4046 if( pList==0 ){ |
| 4047 sqlite3_free(aPoslist); |
| 4048 pPhrase->doclist.pList = 0; |
| 4049 pPhrase->doclist.nList = 0; |
| 4050 return SQLITE_OK; |
| 4051 |
| 4052 }else if( aPoslist==0 ){ |
| 4053 aPoslist = pList; |
| 4054 nPoslist = nList; |
| 4055 |
| 4056 }else{ |
| 4057 char *aOut = pList; |
| 4058 char *p1 = aPoslist; |
| 4059 char *p2 = aOut; |
| 4060 |
| 4061 assert( iPrev>=0 ); |
| 4062 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2); |
| 4063 sqlite3_free(aPoslist); |
| 4064 aPoslist = pList; |
| 4065 nPoslist = (int)(aOut - aPoslist); |
| 4066 if( nPoslist==0 ){ |
| 4067 sqlite3_free(aPoslist); |
| 4068 pPhrase->doclist.pList = 0; |
| 4069 pPhrase->doclist.nList = 0; |
| 4070 return SQLITE_OK; |
| 4071 } |
| 4072 } |
| 4073 iPrev = iToken; |
| 4074 } |
| 4075 } |
| 4076 |
| 4077 if( iPrev>=0 ){ |
| 4078 int nMaxUndeferred = pPhrase->iDoclistToken; |
| 4079 if( nMaxUndeferred<0 ){ |
| 4080 pPhrase->doclist.pList = aPoslist; |
| 4081 pPhrase->doclist.nList = nPoslist; |
| 4082 pPhrase->doclist.iDocid = pCsr->iPrevId; |
| 4083 pPhrase->doclist.bFreeList = 1; |
| 4084 }else{ |
| 4085 int nDistance; |
| 4086 char *p1; |
| 4087 char *p2; |
| 4088 char *aOut; |
| 4089 |
| 4090 if( nMaxUndeferred>iPrev ){ |
| 4091 p1 = aPoslist; |
| 4092 p2 = pPhrase->doclist.pList; |
| 4093 nDistance = nMaxUndeferred - iPrev; |
| 4094 }else{ |
| 4095 p1 = pPhrase->doclist.pList; |
| 4096 p2 = aPoslist; |
| 4097 nDistance = iPrev - nMaxUndeferred; |
| 4098 } |
| 4099 |
| 4100 aOut = (char *)sqlite3_malloc(nPoslist+8); |
| 4101 if( !aOut ){ |
| 4102 sqlite3_free(aPoslist); |
| 4103 return SQLITE_NOMEM; |
| 4104 } |
| 4105 |
| 4106 pPhrase->doclist.pList = aOut; |
| 4107 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){ |
| 4108 pPhrase->doclist.bFreeList = 1; |
| 4109 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList); |
| 4110 }else{ |
| 4111 sqlite3_free(aOut); |
| 4112 pPhrase->doclist.pList = 0; |
| 4113 pPhrase->doclist.nList = 0; |
| 4114 } |
| 4115 sqlite3_free(aPoslist); |
| 4116 } |
| 4117 } |
| 4118 |
| 4119 return SQLITE_OK; |
| 4120 } |
| 4121 |
| 4122 /* |
| 4123 ** Maximum number of tokens a phrase may have to be considered for the |
| 4124 ** incremental doclists strategy. |
| 4125 */ |
| 4126 #define MAX_INCR_PHRASE_TOKENS 4 |
| 4127 |
| 4128 /* |
| 4129 ** This function is called for each Fts3Phrase in a full-text query |
| 4130 ** expression to initialize the mechanism for returning rows. Once this |
| 4131 ** function has been called successfully on an Fts3Phrase, it may be |
| 4132 ** used with fts3EvalPhraseNext() to iterate through the matching docids. |
| 4133 ** |
| 4134 ** If parameter bOptOk is true, then the phrase may (or may not) use the |
| 4135 ** incremental loading strategy. Otherwise, the entire doclist is loaded into |
| 4136 ** memory within this call. |
| 4137 ** |
| 4138 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. |
| 4139 */ |
| 4140 static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){ |
| 4141 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 4142 int rc = SQLITE_OK; /* Error code */ |
| 4143 int i; |
| 4144 |
| 4145 /* Determine if doclists may be loaded from disk incrementally. This is |
| 4146 ** possible if the bOptOk argument is true, the FTS doclists will be |
| 4147 ** scanned in forward order, and the phrase consists of |
| 4148 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first" |
| 4149 ** tokens or prefix tokens that cannot use a prefix-index. */ |
| 4150 int bHaveIncr = 0; |
| 4151 int bIncrOk = (bOptOk |
| 4152 && pCsr->bDesc==pTab->bDescIdx |
| 4153 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0 |
| 4154 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0 |
| 4155 #ifdef SQLITE_TEST |
| 4156 && pTab->bNoIncrDoclist==0 |
| 4157 #endif |
| 4158 ); |
| 4159 for(i=0; bIncrOk==1 && i<p->nToken; i++){ |
| 4160 Fts3PhraseToken *pToken = &p->aToken[i]; |
| 4161 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){ |
| 4162 bIncrOk = 0; |
| 4163 } |
| 4164 if( pToken->pSegcsr ) bHaveIncr = 1; |
| 4165 } |
| 4166 |
| 4167 if( bIncrOk && bHaveIncr ){ |
| 4168 /* Use the incremental approach. */ |
| 4169 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn); |
| 4170 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){ |
| 4171 Fts3PhraseToken *pToken = &p->aToken[i]; |
| 4172 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr; |
| 4173 if( pSegcsr ){ |
| 4174 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n); |
| 4175 } |
| 4176 } |
| 4177 p->bIncr = 1; |
| 4178 }else{ |
| 4179 /* Load the full doclist for the phrase into memory. */ |
| 4180 rc = fts3EvalPhraseLoad(pCsr, p); |
| 4181 p->bIncr = 0; |
| 4182 } |
| 4183 |
| 4184 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr ); |
| 4185 return rc; |
| 4186 } |
| 4187 |
| 4188 /* |
| 4189 ** This function is used to iterate backwards (from the end to start) |
| 4190 ** through doclists. It is used by this module to iterate through phrase |
| 4191 ** doclists in reverse and by the fts3_write.c module to iterate through |
| 4192 ** pending-terms lists when writing to databases with "order=desc". |
| 4193 ** |
| 4194 ** The doclist may be sorted in ascending (parameter bDescIdx==0) or |
| 4195 ** descending (parameter bDescIdx==1) order of docid. Regardless, this |
| 4196 ** function iterates from the end of the doclist to the beginning. |
| 4197 */ |
| 4198 void sqlite3Fts3DoclistPrev( |
| 4199 int bDescIdx, /* True if the doclist is desc */ |
| 4200 char *aDoclist, /* Pointer to entire doclist */ |
| 4201 int nDoclist, /* Length of aDoclist in bytes */ |
| 4202 char **ppIter, /* IN/OUT: Iterator pointer */ |
| 4203 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */ |
| 4204 int *pnList, /* OUT: List length pointer */ |
| 4205 u8 *pbEof /* OUT: End-of-file flag */ |
| 4206 ){ |
| 4207 char *p = *ppIter; |
| 4208 |
| 4209 assert( nDoclist>0 ); |
| 4210 assert( *pbEof==0 ); |
| 4211 assert( p || *piDocid==0 ); |
| 4212 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) ); |
| 4213 |
| 4214 if( p==0 ){ |
| 4215 sqlite3_int64 iDocid = 0; |
| 4216 char *pNext = 0; |
| 4217 char *pDocid = aDoclist; |
| 4218 char *pEnd = &aDoclist[nDoclist]; |
| 4219 int iMul = 1; |
| 4220 |
| 4221 while( pDocid<pEnd ){ |
| 4222 sqlite3_int64 iDelta; |
| 4223 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta); |
| 4224 iDocid += (iMul * iDelta); |
| 4225 pNext = pDocid; |
| 4226 fts3PoslistCopy(0, &pDocid); |
| 4227 while( pDocid<pEnd && *pDocid==0 ) pDocid++; |
| 4228 iMul = (bDescIdx ? -1 : 1); |
| 4229 } |
| 4230 |
| 4231 *pnList = (int)(pEnd - pNext); |
| 4232 *ppIter = pNext; |
| 4233 *piDocid = iDocid; |
| 4234 }else{ |
| 4235 int iMul = (bDescIdx ? -1 : 1); |
| 4236 sqlite3_int64 iDelta; |
| 4237 fts3GetReverseVarint(&p, aDoclist, &iDelta); |
| 4238 *piDocid -= (iMul * iDelta); |
| 4239 |
| 4240 if( p==aDoclist ){ |
| 4241 *pbEof = 1; |
| 4242 }else{ |
| 4243 char *pSave = p; |
| 4244 fts3ReversePoslist(aDoclist, &p); |
| 4245 *pnList = (int)(pSave - p); |
| 4246 } |
| 4247 *ppIter = p; |
| 4248 } |
| 4249 } |
| 4250 |
| 4251 /* |
| 4252 ** Iterate forwards through a doclist. |
| 4253 */ |
| 4254 void sqlite3Fts3DoclistNext( |
| 4255 int bDescIdx, /* True if the doclist is desc */ |
| 4256 char *aDoclist, /* Pointer to entire doclist */ |
| 4257 int nDoclist, /* Length of aDoclist in bytes */ |
| 4258 char **ppIter, /* IN/OUT: Iterator pointer */ |
| 4259 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */ |
| 4260 u8 *pbEof /* OUT: End-of-file flag */ |
| 4261 ){ |
| 4262 char *p = *ppIter; |
| 4263 |
| 4264 assert( nDoclist>0 ); |
| 4265 assert( *pbEof==0 ); |
| 4266 assert( p || *piDocid==0 ); |
| 4267 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) ); |
| 4268 |
| 4269 if( p==0 ){ |
| 4270 p = aDoclist; |
| 4271 p += sqlite3Fts3GetVarint(p, piDocid); |
| 4272 }else{ |
| 4273 fts3PoslistCopy(0, &p); |
| 4274 if( p>=&aDoclist[nDoclist] ){ |
| 4275 *pbEof = 1; |
| 4276 }else{ |
| 4277 sqlite3_int64 iVar; |
| 4278 p += sqlite3Fts3GetVarint(p, &iVar); |
| 4279 *piDocid += ((bDescIdx ? -1 : 1) * iVar); |
| 4280 } |
| 4281 } |
| 4282 |
| 4283 *ppIter = p; |
| 4284 } |
| 4285 |
| 4286 /* |
| 4287 ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof |
| 4288 ** to true if EOF is reached. |
| 4289 */ |
| 4290 static void fts3EvalDlPhraseNext( |
| 4291 Fts3Table *pTab, |
| 4292 Fts3Doclist *pDL, |
| 4293 u8 *pbEof |
| 4294 ){ |
| 4295 char *pIter; /* Used to iterate through aAll */ |
| 4296 char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */ |
| 4297 |
| 4298 if( pDL->pNextDocid ){ |
| 4299 pIter = pDL->pNextDocid; |
| 4300 }else{ |
| 4301 pIter = pDL->aAll; |
| 4302 } |
| 4303 |
| 4304 if( pIter>=pEnd ){ |
| 4305 /* We have already reached the end of this doclist. EOF. */ |
| 4306 *pbEof = 1; |
| 4307 }else{ |
| 4308 sqlite3_int64 iDelta; |
| 4309 pIter += sqlite3Fts3GetVarint(pIter, &iDelta); |
| 4310 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){ |
| 4311 pDL->iDocid += iDelta; |
| 4312 }else{ |
| 4313 pDL->iDocid -= iDelta; |
| 4314 } |
| 4315 pDL->pList = pIter; |
| 4316 fts3PoslistCopy(0, &pIter); |
| 4317 pDL->nList = (int)(pIter - pDL->pList); |
| 4318 |
| 4319 /* pIter now points just past the 0x00 that terminates the position- |
| 4320 ** list for document pDL->iDocid. However, if this position-list was |
| 4321 ** edited in place by fts3EvalNearTrim(), then pIter may not actually |
| 4322 ** point to the start of the next docid value. The following line deals |
| 4323 ** with this case by advancing pIter past the zero-padding added by |
| 4324 ** fts3EvalNearTrim(). */ |
| 4325 while( pIter<pEnd && *pIter==0 ) pIter++; |
| 4326 |
| 4327 pDL->pNextDocid = pIter; |
| 4328 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter ); |
| 4329 *pbEof = 0; |
| 4330 } |
| 4331 } |
| 4332 |
| 4333 /* |
| 4334 ** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext(). |
| 4335 */ |
| 4336 typedef struct TokenDoclist TokenDoclist; |
| 4337 struct TokenDoclist { |
| 4338 int bIgnore; |
| 4339 sqlite3_int64 iDocid; |
| 4340 char *pList; |
| 4341 int nList; |
| 4342 }; |
| 4343 |
| 4344 /* |
| 4345 ** Token pToken is an incrementally loaded token that is part of a |
| 4346 ** multi-token phrase. Advance it to the next matching document in the |
| 4347 ** database and populate output variable *p with the details of the new |
| 4348 ** entry. Or, if the iterator has reached EOF, set *pbEof to true. |
| 4349 ** |
| 4350 ** If an error occurs, return an SQLite error code. Otherwise, return |
| 4351 ** SQLITE_OK. |
| 4352 */ |
| 4353 static int incrPhraseTokenNext( |
| 4354 Fts3Table *pTab, /* Virtual table handle */ |
| 4355 Fts3Phrase *pPhrase, /* Phrase to advance token of */ |
| 4356 int iToken, /* Specific token to advance */ |
| 4357 TokenDoclist *p, /* OUT: Docid and doclist for new entry */ |
| 4358 u8 *pbEof /* OUT: True if iterator is at EOF */ |
| 4359 ){ |
| 4360 int rc = SQLITE_OK; |
| 4361 |
| 4362 if( pPhrase->iDoclistToken==iToken ){ |
| 4363 assert( p->bIgnore==0 ); |
| 4364 assert( pPhrase->aToken[iToken].pSegcsr==0 ); |
| 4365 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof); |
| 4366 p->pList = pPhrase->doclist.pList; |
| 4367 p->nList = pPhrase->doclist.nList; |
| 4368 p->iDocid = pPhrase->doclist.iDocid; |
| 4369 }else{ |
| 4370 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken]; |
| 4371 assert( pToken->pDeferred==0 ); |
| 4372 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 ); |
| 4373 if( pToken->pSegcsr ){ |
| 4374 assert( p->bIgnore==0 ); |
| 4375 rc = sqlite3Fts3MsrIncrNext( |
| 4376 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList |
| 4377 ); |
| 4378 if( p->pList==0 ) *pbEof = 1; |
| 4379 }else{ |
| 4380 p->bIgnore = 1; |
| 4381 } |
| 4382 } |
| 4383 |
| 4384 return rc; |
| 4385 } |
| 4386 |
| 4387 |
| 4388 /* |
| 4389 ** The phrase iterator passed as the second argument: |
| 4390 ** |
| 4391 ** * features at least one token that uses an incremental doclist, and |
| 4392 ** |
| 4393 ** * does not contain any deferred tokens. |
| 4394 ** |
| 4395 ** Advance it to the next matching documnent in the database and populate |
| 4396 ** the Fts3Doclist.pList and nList fields. |
| 4397 ** |
| 4398 ** If there is no "next" entry and no error occurs, then *pbEof is set to |
| 4399 ** 1 before returning. Otherwise, if no error occurs and the iterator is |
| 4400 ** successfully advanced, *pbEof is set to 0. |
| 4401 ** |
| 4402 ** If an error occurs, return an SQLite error code. Otherwise, return |
| 4403 ** SQLITE_OK. |
| 4404 */ |
| 4405 static int fts3EvalIncrPhraseNext( |
| 4406 Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 4407 Fts3Phrase *p, /* Phrase object to advance to next docid */ |
| 4408 u8 *pbEof /* OUT: Set to 1 if EOF */ |
| 4409 ){ |
| 4410 int rc = SQLITE_OK; |
| 4411 Fts3Doclist *pDL = &p->doclist; |
| 4412 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 4413 u8 bEof = 0; |
| 4414 |
| 4415 /* This is only called if it is guaranteed that the phrase has at least |
| 4416 ** one incremental token. In which case the bIncr flag is set. */ |
| 4417 assert( p->bIncr==1 ); |
| 4418 |
| 4419 if( p->nToken==1 && p->bIncr ){ |
| 4420 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, |
| 4421 &pDL->iDocid, &pDL->pList, &pDL->nList |
| 4422 ); |
| 4423 if( pDL->pList==0 ) bEof = 1; |
| 4424 }else{ |
| 4425 int bDescDoclist = pCsr->bDesc; |
| 4426 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS]; |
| 4427 |
| 4428 memset(a, 0, sizeof(a)); |
| 4429 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS ); |
| 4430 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS ); |
| 4431 |
| 4432 while( bEof==0 ){ |
| 4433 int bMaxSet = 0; |
| 4434 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */ |
| 4435 int i; /* Used to iterate through tokens */ |
| 4436 |
| 4437 /* Advance the iterator for each token in the phrase once. */ |
| 4438 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){ |
| 4439 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof); |
| 4440 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){ |
| 4441 iMax = a[i].iDocid; |
| 4442 bMaxSet = 1; |
| 4443 } |
| 4444 } |
| 4445 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) ); |
| 4446 assert( rc!=SQLITE_OK || bMaxSet ); |
| 4447 |
| 4448 /* Keep advancing iterators until they all point to the same document */ |
| 4449 for(i=0; i<p->nToken; i++){ |
| 4450 while( rc==SQLITE_OK && bEof==0 |
| 4451 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0 |
| 4452 ){ |
| 4453 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof); |
| 4454 if( DOCID_CMP(a[i].iDocid, iMax)>0 ){ |
| 4455 iMax = a[i].iDocid; |
| 4456 i = 0; |
| 4457 } |
| 4458 } |
| 4459 } |
| 4460 |
| 4461 /* Check if the current entries really are a phrase match */ |
| 4462 if( bEof==0 ){ |
| 4463 int nList = 0; |
| 4464 int nByte = a[p->nToken-1].nList; |
| 4465 char *aDoclist = sqlite3_malloc(nByte+1); |
| 4466 if( !aDoclist ) return SQLITE_NOMEM; |
| 4467 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1); |
| 4468 |
| 4469 for(i=0; i<(p->nToken-1); i++){ |
| 4470 if( a[i].bIgnore==0 ){ |
| 4471 char *pL = a[i].pList; |
| 4472 char *pR = aDoclist; |
| 4473 char *pOut = aDoclist; |
| 4474 int nDist = p->nToken-1-i; |
| 4475 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR); |
| 4476 if( res==0 ) break; |
| 4477 nList = (int)(pOut - aDoclist); |
| 4478 } |
| 4479 } |
| 4480 if( i==(p->nToken-1) ){ |
| 4481 pDL->iDocid = iMax; |
| 4482 pDL->pList = aDoclist; |
| 4483 pDL->nList = nList; |
| 4484 pDL->bFreeList = 1; |
| 4485 break; |
| 4486 } |
| 4487 sqlite3_free(aDoclist); |
| 4488 } |
| 4489 } |
| 4490 } |
| 4491 |
| 4492 *pbEof = bEof; |
| 4493 return rc; |
| 4494 } |
| 4495 |
| 4496 /* |
| 4497 ** Attempt to move the phrase iterator to point to the next matching docid. |
| 4498 ** If an error occurs, return an SQLite error code. Otherwise, return |
| 4499 ** SQLITE_OK. |
| 4500 ** |
| 4501 ** If there is no "next" entry and no error occurs, then *pbEof is set to |
| 4502 ** 1 before returning. Otherwise, if no error occurs and the iterator is |
| 4503 ** successfully advanced, *pbEof is set to 0. |
| 4504 */ |
| 4505 static int fts3EvalPhraseNext( |
| 4506 Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 4507 Fts3Phrase *p, /* Phrase object to advance to next docid */ |
| 4508 u8 *pbEof /* OUT: Set to 1 if EOF */ |
| 4509 ){ |
| 4510 int rc = SQLITE_OK; |
| 4511 Fts3Doclist *pDL = &p->doclist; |
| 4512 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 4513 |
| 4514 if( p->bIncr ){ |
| 4515 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof); |
| 4516 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){ |
| 4517 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll, |
| 4518 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof |
| 4519 ); |
| 4520 pDL->pList = pDL->pNextDocid; |
| 4521 }else{ |
| 4522 fts3EvalDlPhraseNext(pTab, pDL, pbEof); |
| 4523 } |
| 4524 |
| 4525 return rc; |
| 4526 } |
| 4527 |
| 4528 /* |
| 4529 ** |
| 4530 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
| 4531 ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the |
| 4532 ** expression. Also the Fts3Expr.bDeferred variable is set to true for any |
| 4533 ** expressions for which all descendent tokens are deferred. |
| 4534 ** |
| 4535 ** If parameter bOptOk is zero, then it is guaranteed that the |
| 4536 ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for |
| 4537 ** each phrase in the expression (subject to deferred token processing). |
| 4538 ** Or, if bOptOk is non-zero, then one or more tokens within the expression |
| 4539 ** may be loaded incrementally, meaning doclist.aAll/nAll is not available. |
| 4540 ** |
| 4541 ** If an error occurs within this function, *pRc is set to an SQLite error |
| 4542 ** code before returning. |
| 4543 */ |
| 4544 static void fts3EvalStartReaders( |
| 4545 Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 4546 Fts3Expr *pExpr, /* Expression to initialize phrases in */ |
| 4547 int *pRc /* IN/OUT: Error code */ |
| 4548 ){ |
| 4549 if( pExpr && SQLITE_OK==*pRc ){ |
| 4550 if( pExpr->eType==FTSQUERY_PHRASE ){ |
| 4551 int i; |
| 4552 int nToken = pExpr->pPhrase->nToken; |
| 4553 for(i=0; i<nToken; i++){ |
| 4554 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break; |
| 4555 } |
| 4556 pExpr->bDeferred = (i==nToken); |
| 4557 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase); |
| 4558 }else{ |
| 4559 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc); |
| 4560 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc); |
| 4561 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred); |
| 4562 } |
| 4563 } |
| 4564 } |
| 4565 |
| 4566 /* |
| 4567 ** An array of the following structures is assembled as part of the process |
| 4568 ** of selecting tokens to defer before the query starts executing (as part |
| 4569 ** of the xFilter() method). There is one element in the array for each |
| 4570 ** token in the FTS expression. |
| 4571 ** |
| 4572 ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong |
| 4573 ** to phrases that are connected only by AND and NEAR operators (not OR or |
| 4574 ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered |
| 4575 ** separately. The root of a tokens AND/NEAR cluster is stored in |
| 4576 ** Fts3TokenAndCost.pRoot. |
| 4577 */ |
| 4578 typedef struct Fts3TokenAndCost Fts3TokenAndCost; |
| 4579 struct Fts3TokenAndCost { |
| 4580 Fts3Phrase *pPhrase; /* The phrase the token belongs to */ |
| 4581 int iToken; /* Position of token in phrase */ |
| 4582 Fts3PhraseToken *pToken; /* The token itself */ |
| 4583 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */ |
| 4584 int nOvfl; /* Number of overflow pages to load doclist */ |
| 4585 int iCol; /* The column the token must match */ |
| 4586 }; |
| 4587 |
| 4588 /* |
| 4589 ** This function is used to populate an allocated Fts3TokenAndCost array. |
| 4590 ** |
| 4591 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
| 4592 ** Otherwise, if an error occurs during execution, *pRc is set to an |
| 4593 ** SQLite error code. |
| 4594 */ |
| 4595 static void fts3EvalTokenCosts( |
| 4596 Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 4597 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */ |
| 4598 Fts3Expr *pExpr, /* Expression to consider */ |
| 4599 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */ |
| 4600 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */ |
| 4601 int *pRc /* IN/OUT: Error code */ |
| 4602 ){ |
| 4603 if( *pRc==SQLITE_OK ){ |
| 4604 if( pExpr->eType==FTSQUERY_PHRASE ){ |
| 4605 Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 4606 int i; |
| 4607 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){ |
| 4608 Fts3TokenAndCost *pTC = (*ppTC)++; |
| 4609 pTC->pPhrase = pPhrase; |
| 4610 pTC->iToken = i; |
| 4611 pTC->pRoot = pRoot; |
| 4612 pTC->pToken = &pPhrase->aToken[i]; |
| 4613 pTC->iCol = pPhrase->iColumn; |
| 4614 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl); |
| 4615 } |
| 4616 }else if( pExpr->eType!=FTSQUERY_NOT ){ |
| 4617 assert( pExpr->eType==FTSQUERY_OR |
| 4618 || pExpr->eType==FTSQUERY_AND |
| 4619 || pExpr->eType==FTSQUERY_NEAR |
| 4620 ); |
| 4621 assert( pExpr->pLeft && pExpr->pRight ); |
| 4622 if( pExpr->eType==FTSQUERY_OR ){ |
| 4623 pRoot = pExpr->pLeft; |
| 4624 **ppOr = pRoot; |
| 4625 (*ppOr)++; |
| 4626 } |
| 4627 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc); |
| 4628 if( pExpr->eType==FTSQUERY_OR ){ |
| 4629 pRoot = pExpr->pRight; |
| 4630 **ppOr = pRoot; |
| 4631 (*ppOr)++; |
| 4632 } |
| 4633 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc); |
| 4634 } |
| 4635 } |
| 4636 } |
| 4637 |
| 4638 /* |
| 4639 ** Determine the average document (row) size in pages. If successful, |
| 4640 ** write this value to *pnPage and return SQLITE_OK. Otherwise, return |
| 4641 ** an SQLite error code. |
| 4642 ** |
| 4643 ** The average document size in pages is calculated by first calculating |
| 4644 ** determining the average size in bytes, B. If B is less than the amount |
| 4645 ** of data that will fit on a single leaf page of an intkey table in |
| 4646 ** this database, then the average docsize is 1. Otherwise, it is 1 plus |
| 4647 ** the number of overflow pages consumed by a record B bytes in size. |
| 4648 */ |
| 4649 static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){ |
| 4650 if( pCsr->nRowAvg==0 ){ |
| 4651 /* The average document size, which is required to calculate the cost |
| 4652 ** of each doclist, has not yet been determined. Read the required |
| 4653 ** data from the %_stat table to calculate it. |
| 4654 ** |
| 4655 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 |
| 4656 ** varints, where nCol is the number of columns in the FTS3 table. |
| 4657 ** The first varint is the number of documents currently stored in |
| 4658 ** the table. The following nCol varints contain the total amount of |
| 4659 ** data stored in all rows of each column of the table, from left |
| 4660 ** to right. |
| 4661 */ |
| 4662 int rc; |
| 4663 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab; |
| 4664 sqlite3_stmt *pStmt; |
| 4665 sqlite3_int64 nDoc = 0; |
| 4666 sqlite3_int64 nByte = 0; |
| 4667 const char *pEnd; |
| 4668 const char *a; |
| 4669 |
| 4670 rc = sqlite3Fts3SelectDoctotal(p, &pStmt); |
| 4671 if( rc!=SQLITE_OK ) return rc; |
| 4672 a = sqlite3_column_blob(pStmt, 0); |
| 4673 assert( a ); |
| 4674 |
| 4675 pEnd = &a[sqlite3_column_bytes(pStmt, 0)]; |
| 4676 a += sqlite3Fts3GetVarint(a, &nDoc); |
| 4677 while( a<pEnd ){ |
| 4678 a += sqlite3Fts3GetVarint(a, &nByte); |
| 4679 } |
| 4680 if( nDoc==0 || nByte==0 ){ |
| 4681 sqlite3_reset(pStmt); |
| 4682 return FTS_CORRUPT_VTAB; |
| 4683 } |
| 4684 |
| 4685 pCsr->nDoc = nDoc; |
| 4686 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz); |
| 4687 assert( pCsr->nRowAvg>0 ); |
| 4688 rc = sqlite3_reset(pStmt); |
| 4689 if( rc!=SQLITE_OK ) return rc; |
| 4690 } |
| 4691 |
| 4692 *pnPage = pCsr->nRowAvg; |
| 4693 return SQLITE_OK; |
| 4694 } |
| 4695 |
| 4696 /* |
| 4697 ** This function is called to select the tokens (if any) that will be |
| 4698 ** deferred. The array aTC[] has already been populated when this is |
| 4699 ** called. |
| 4700 ** |
| 4701 ** This function is called once for each AND/NEAR cluster in the |
| 4702 ** expression. Each invocation determines which tokens to defer within |
| 4703 ** the cluster with root node pRoot. See comments above the definition |
| 4704 ** of struct Fts3TokenAndCost for more details. |
| 4705 ** |
| 4706 ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken() |
| 4707 ** called on each token to defer. Otherwise, an SQLite error code is |
| 4708 ** returned. |
| 4709 */ |
| 4710 static int fts3EvalSelectDeferred( |
| 4711 Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 4712 Fts3Expr *pRoot, /* Consider tokens with this root node */ |
| 4713 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */ |
| 4714 int nTC /* Number of entries in aTC[] */ |
| 4715 ){ |
| 4716 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 4717 int nDocSize = 0; /* Number of pages per doc loaded */ |
| 4718 int rc = SQLITE_OK; /* Return code */ |
| 4719 int ii; /* Iterator variable for various purposes */ |
| 4720 int nOvfl = 0; /* Total overflow pages used by doclists */ |
| 4721 int nToken = 0; /* Total number of tokens in cluster */ |
| 4722 |
| 4723 int nMinEst = 0; /* The minimum count for any phrase so far. */ |
| 4724 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */ |
| 4725 |
| 4726 /* Tokens are never deferred for FTS tables created using the content=xxx |
| 4727 ** option. The reason being that it is not guaranteed that the content |
| 4728 ** table actually contains the same data as the index. To prevent this from |
| 4729 ** causing any problems, the deferred token optimization is completely |
| 4730 ** disabled for content=xxx tables. */ |
| 4731 if( pTab->zContentTbl ){ |
| 4732 return SQLITE_OK; |
| 4733 } |
| 4734 |
| 4735 /* Count the tokens in this AND/NEAR cluster. If none of the doclists |
| 4736 ** associated with the tokens spill onto overflow pages, or if there is |
| 4737 ** only 1 token, exit early. No tokens to defer in this case. */ |
| 4738 for(ii=0; ii<nTC; ii++){ |
| 4739 if( aTC[ii].pRoot==pRoot ){ |
| 4740 nOvfl += aTC[ii].nOvfl; |
| 4741 nToken++; |
| 4742 } |
| 4743 } |
| 4744 if( nOvfl==0 || nToken<2 ) return SQLITE_OK; |
| 4745 |
| 4746 /* Obtain the average docsize (in pages). */ |
| 4747 rc = fts3EvalAverageDocsize(pCsr, &nDocSize); |
| 4748 assert( rc!=SQLITE_OK || nDocSize>0 ); |
| 4749 |
| 4750 |
| 4751 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order |
| 4752 ** of the number of overflow pages that will be loaded by the pager layer |
| 4753 ** to retrieve the entire doclist for the token from the full-text index. |
| 4754 ** Load the doclists for tokens that are either: |
| 4755 ** |
| 4756 ** a. The cheapest token in the entire query (i.e. the one visited by the |
| 4757 ** first iteration of this loop), or |
| 4758 ** |
| 4759 ** b. Part of a multi-token phrase. |
| 4760 ** |
| 4761 ** After each token doclist is loaded, merge it with the others from the |
| 4762 ** same phrase and count the number of documents that the merged doclist |
| 4763 ** contains. Set variable "nMinEst" to the smallest number of documents in |
| 4764 ** any phrase doclist for which 1 or more token doclists have been loaded. |
| 4765 ** Let nOther be the number of other phrases for which it is certain that |
| 4766 ** one or more tokens will not be deferred. |
| 4767 ** |
| 4768 ** Then, for each token, defer it if loading the doclist would result in |
| 4769 ** loading N or more overflow pages into memory, where N is computed as: |
| 4770 ** |
| 4771 ** (nMinEst + 4^nOther - 1) / (4^nOther) |
| 4772 */ |
| 4773 for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){ |
| 4774 int iTC; /* Used to iterate through aTC[] array. */ |
| 4775 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */ |
| 4776 |
| 4777 /* Set pTC to point to the cheapest remaining token. */ |
| 4778 for(iTC=0; iTC<nTC; iTC++){ |
| 4779 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot |
| 4780 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl) |
| 4781 ){ |
| 4782 pTC = &aTC[iTC]; |
| 4783 } |
| 4784 } |
| 4785 assert( pTC ); |
| 4786 |
| 4787 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){ |
| 4788 /* The number of overflow pages to load for this (and therefore all |
| 4789 ** subsequent) tokens is greater than the estimated number of pages |
| 4790 ** that will be loaded if all subsequent tokens are deferred. |
| 4791 */ |
| 4792 Fts3PhraseToken *pToken = pTC->pToken; |
| 4793 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol); |
| 4794 fts3SegReaderCursorFree(pToken->pSegcsr); |
| 4795 pToken->pSegcsr = 0; |
| 4796 }else{ |
| 4797 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the |
| 4798 ** for-loop. Except, limit the value to 2^24 to prevent it from |
| 4799 ** overflowing the 32-bit integer it is stored in. */ |
| 4800 if( ii<12 ) nLoad4 = nLoad4*4; |
| 4801 |
| 4802 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){ |
| 4803 /* Either this is the cheapest token in the entire query, or it is |
| 4804 ** part of a multi-token phrase. Either way, the entire doclist will |
| 4805 ** (eventually) be loaded into memory. It may as well be now. */ |
| 4806 Fts3PhraseToken *pToken = pTC->pToken; |
| 4807 int nList = 0; |
| 4808 char *pList = 0; |
| 4809 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList); |
| 4810 assert( rc==SQLITE_OK || pList==0 ); |
| 4811 if( rc==SQLITE_OK ){ |
| 4812 int nCount; |
| 4813 fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList); |
| 4814 nCount = fts3DoclistCountDocids( |
| 4815 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll |
| 4816 ); |
| 4817 if( ii==0 || nCount<nMinEst ) nMinEst = nCount; |
| 4818 } |
| 4819 } |
| 4820 } |
| 4821 pTC->pToken = 0; |
| 4822 } |
| 4823 |
| 4824 return rc; |
| 4825 } |
| 4826 |
| 4827 /* |
| 4828 ** This function is called from within the xFilter method. It initializes |
| 4829 ** the full-text query currently stored in pCsr->pExpr. To iterate through |
| 4830 ** the results of a query, the caller does: |
| 4831 ** |
| 4832 ** fts3EvalStart(pCsr); |
| 4833 ** while( 1 ){ |
| 4834 ** fts3EvalNext(pCsr); |
| 4835 ** if( pCsr->bEof ) break; |
| 4836 ** ... return row pCsr->iPrevId to the caller ... |
| 4837 ** } |
| 4838 */ |
| 4839 static int fts3EvalStart(Fts3Cursor *pCsr){ |
| 4840 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 4841 int rc = SQLITE_OK; |
| 4842 int nToken = 0; |
| 4843 int nOr = 0; |
| 4844 |
| 4845 /* Allocate a MultiSegReader for each token in the expression. */ |
| 4846 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc); |
| 4847 |
| 4848 /* Determine which, if any, tokens in the expression should be deferred. */ |
| 4849 #ifndef SQLITE_DISABLE_FTS4_DEFERRED |
| 4850 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){ |
| 4851 Fts3TokenAndCost *aTC; |
| 4852 Fts3Expr **apOr; |
| 4853 aTC = (Fts3TokenAndCost *)sqlite3_malloc( |
| 4854 sizeof(Fts3TokenAndCost) * nToken |
| 4855 + sizeof(Fts3Expr *) * nOr * 2 |
| 4856 ); |
| 4857 apOr = (Fts3Expr **)&aTC[nToken]; |
| 4858 |
| 4859 if( !aTC ){ |
| 4860 rc = SQLITE_NOMEM; |
| 4861 }else{ |
| 4862 int ii; |
| 4863 Fts3TokenAndCost *pTC = aTC; |
| 4864 Fts3Expr **ppOr = apOr; |
| 4865 |
| 4866 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc); |
| 4867 nToken = (int)(pTC-aTC); |
| 4868 nOr = (int)(ppOr-apOr); |
| 4869 |
| 4870 if( rc==SQLITE_OK ){ |
| 4871 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken); |
| 4872 for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){ |
| 4873 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken); |
| 4874 } |
| 4875 } |
| 4876 |
| 4877 sqlite3_free(aTC); |
| 4878 } |
| 4879 } |
| 4880 #endif |
| 4881 |
| 4882 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc); |
| 4883 return rc; |
| 4884 } |
| 4885 |
| 4886 /* |
| 4887 ** Invalidate the current position list for phrase pPhrase. |
| 4888 */ |
| 4889 static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){ |
| 4890 if( pPhrase->doclist.bFreeList ){ |
| 4891 sqlite3_free(pPhrase->doclist.pList); |
| 4892 } |
| 4893 pPhrase->doclist.pList = 0; |
| 4894 pPhrase->doclist.nList = 0; |
| 4895 pPhrase->doclist.bFreeList = 0; |
| 4896 } |
| 4897 |
| 4898 /* |
| 4899 ** This function is called to edit the position list associated with |
| 4900 ** the phrase object passed as the fifth argument according to a NEAR |
| 4901 ** condition. For example: |
| 4902 ** |
| 4903 ** abc NEAR/5 "def ghi" |
| 4904 ** |
| 4905 ** Parameter nNear is passed the NEAR distance of the expression (5 in |
| 4906 ** the example above). When this function is called, *paPoslist points to |
| 4907 ** the position list, and *pnToken is the number of phrase tokens in, the |
| 4908 ** phrase on the other side of the NEAR operator to pPhrase. For example, |
| 4909 ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to |
| 4910 ** the position list associated with phrase "abc". |
| 4911 ** |
| 4912 ** All positions in the pPhrase position list that are not sufficiently |
| 4913 ** close to a position in the *paPoslist position list are removed. If this |
| 4914 ** leaves 0 positions, zero is returned. Otherwise, non-zero. |
| 4915 ** |
| 4916 ** Before returning, *paPoslist is set to point to the position lsit |
| 4917 ** associated with pPhrase. And *pnToken is set to the number of tokens in |
| 4918 ** pPhrase. |
| 4919 */ |
| 4920 static int fts3EvalNearTrim( |
| 4921 int nNear, /* NEAR distance. As in "NEAR/nNear". */ |
| 4922 char *aTmp, /* Temporary space to use */ |
| 4923 char **paPoslist, /* IN/OUT: Position list */ |
| 4924 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */ |
| 4925 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */ |
| 4926 ){ |
| 4927 int nParam1 = nNear + pPhrase->nToken; |
| 4928 int nParam2 = nNear + *pnToken; |
| 4929 int nNew; |
| 4930 char *p2; |
| 4931 char *pOut; |
| 4932 int res; |
| 4933 |
| 4934 assert( pPhrase->doclist.pList ); |
| 4935 |
| 4936 p2 = pOut = pPhrase->doclist.pList; |
| 4937 res = fts3PoslistNearMerge( |
| 4938 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2 |
| 4939 ); |
| 4940 if( res ){ |
| 4941 nNew = (int)(pOut - pPhrase->doclist.pList) - 1; |
| 4942 assert( pPhrase->doclist.pList[nNew]=='\0' ); |
| 4943 assert( nNew<=pPhrase->doclist.nList && nNew>0 ); |
| 4944 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew); |
| 4945 pPhrase->doclist.nList = nNew; |
| 4946 *paPoslist = pPhrase->doclist.pList; |
| 4947 *pnToken = pPhrase->nToken; |
| 4948 } |
| 4949 |
| 4950 return res; |
| 4951 } |
| 4952 |
| 4953 /* |
| 4954 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called. |
| 4955 ** Otherwise, it advances the expression passed as the second argument to |
| 4956 ** point to the next matching row in the database. Expressions iterate through |
| 4957 ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero, |
| 4958 ** or descending if it is non-zero. |
| 4959 ** |
| 4960 ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if |
| 4961 ** successful, the following variables in pExpr are set: |
| 4962 ** |
| 4963 ** Fts3Expr.bEof (non-zero if EOF - there is no next row) |
| 4964 ** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row) |
| 4965 ** |
| 4966 ** If the expression is of type FTSQUERY_PHRASE, and the expression is not |
| 4967 ** at EOF, then the following variables are populated with the position list |
| 4968 ** for the phrase for the visited row: |
| 4969 ** |
| 4970 ** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes) |
| 4971 ** FTs3Expr.pPhrase->doclist.pList (pointer to position list) |
| 4972 ** |
| 4973 ** It says above that this function advances the expression to the next |
| 4974 ** matching row. This is usually true, but there are the following exceptions: |
| 4975 ** |
| 4976 ** 1. Deferred tokens are not taken into account. If a phrase consists |
| 4977 ** entirely of deferred tokens, it is assumed to match every row in |
| 4978 ** the db. In this case the position-list is not populated at all. |
| 4979 ** |
| 4980 ** Or, if a phrase contains one or more deferred tokens and one or |
| 4981 ** more non-deferred tokens, then the expression is advanced to the |
| 4982 ** next possible match, considering only non-deferred tokens. In other |
| 4983 ** words, if the phrase is "A B C", and "B" is deferred, the expression |
| 4984 ** is advanced to the next row that contains an instance of "A * C", |
| 4985 ** where "*" may match any single token. The position list in this case |
| 4986 ** is populated as for "A * C" before returning. |
| 4987 ** |
| 4988 ** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is |
| 4989 ** advanced to point to the next row that matches "x AND y". |
| 4990 ** |
| 4991 ** See fts3EvalTestDeferredAndNear() for details on testing if a row is |
| 4992 ** really a match, taking into account deferred tokens and NEAR operators. |
| 4993 */ |
| 4994 static void fts3EvalNextRow( |
| 4995 Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 4996 Fts3Expr *pExpr, /* Expr. to advance to next matching row */ |
| 4997 int *pRc /* IN/OUT: Error code */ |
| 4998 ){ |
| 4999 if( *pRc==SQLITE_OK ){ |
| 5000 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */ |
| 5001 assert( pExpr->bEof==0 ); |
| 5002 pExpr->bStart = 1; |
| 5003 |
| 5004 switch( pExpr->eType ){ |
| 5005 case FTSQUERY_NEAR: |
| 5006 case FTSQUERY_AND: { |
| 5007 Fts3Expr *pLeft = pExpr->pLeft; |
| 5008 Fts3Expr *pRight = pExpr->pRight; |
| 5009 assert( !pLeft->bDeferred || !pRight->bDeferred ); |
| 5010 |
| 5011 if( pLeft->bDeferred ){ |
| 5012 /* LHS is entirely deferred. So we assume it matches every row. |
| 5013 ** Advance the RHS iterator to find the next row visited. */ |
| 5014 fts3EvalNextRow(pCsr, pRight, pRc); |
| 5015 pExpr->iDocid = pRight->iDocid; |
| 5016 pExpr->bEof = pRight->bEof; |
| 5017 }else if( pRight->bDeferred ){ |
| 5018 /* RHS is entirely deferred. So we assume it matches every row. |
| 5019 ** Advance the LHS iterator to find the next row visited. */ |
| 5020 fts3EvalNextRow(pCsr, pLeft, pRc); |
| 5021 pExpr->iDocid = pLeft->iDocid; |
| 5022 pExpr->bEof = pLeft->bEof; |
| 5023 }else{ |
| 5024 /* Neither the RHS or LHS are deferred. */ |
| 5025 fts3EvalNextRow(pCsr, pLeft, pRc); |
| 5026 fts3EvalNextRow(pCsr, pRight, pRc); |
| 5027 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){ |
| 5028 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid); |
| 5029 if( iDiff==0 ) break; |
| 5030 if( iDiff<0 ){ |
| 5031 fts3EvalNextRow(pCsr, pLeft, pRc); |
| 5032 }else{ |
| 5033 fts3EvalNextRow(pCsr, pRight, pRc); |
| 5034 } |
| 5035 } |
| 5036 pExpr->iDocid = pLeft->iDocid; |
| 5037 pExpr->bEof = (pLeft->bEof || pRight->bEof); |
| 5038 } |
| 5039 break; |
| 5040 } |
| 5041 |
| 5042 case FTSQUERY_OR: { |
| 5043 Fts3Expr *pLeft = pExpr->pLeft; |
| 5044 Fts3Expr *pRight = pExpr->pRight; |
| 5045 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid); |
| 5046 |
| 5047 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid ); |
| 5048 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid ); |
| 5049 |
| 5050 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){ |
| 5051 fts3EvalNextRow(pCsr, pLeft, pRc); |
| 5052 }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){ |
| 5053 fts3EvalNextRow(pCsr, pRight, pRc); |
| 5054 }else{ |
| 5055 fts3EvalNextRow(pCsr, pLeft, pRc); |
| 5056 fts3EvalNextRow(pCsr, pRight, pRc); |
| 5057 } |
| 5058 |
| 5059 pExpr->bEof = (pLeft->bEof && pRight->bEof); |
| 5060 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid); |
| 5061 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){ |
| 5062 pExpr->iDocid = pLeft->iDocid; |
| 5063 }else{ |
| 5064 pExpr->iDocid = pRight->iDocid; |
| 5065 } |
| 5066 |
| 5067 break; |
| 5068 } |
| 5069 |
| 5070 case FTSQUERY_NOT: { |
| 5071 Fts3Expr *pLeft = pExpr->pLeft; |
| 5072 Fts3Expr *pRight = pExpr->pRight; |
| 5073 |
| 5074 if( pRight->bStart==0 ){ |
| 5075 fts3EvalNextRow(pCsr, pRight, pRc); |
| 5076 assert( *pRc!=SQLITE_OK || pRight->bStart ); |
| 5077 } |
| 5078 |
| 5079 fts3EvalNextRow(pCsr, pLeft, pRc); |
| 5080 if( pLeft->bEof==0 ){ |
| 5081 while( !*pRc |
| 5082 && !pRight->bEof |
| 5083 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0 |
| 5084 ){ |
| 5085 fts3EvalNextRow(pCsr, pRight, pRc); |
| 5086 } |
| 5087 } |
| 5088 pExpr->iDocid = pLeft->iDocid; |
| 5089 pExpr->bEof = pLeft->bEof; |
| 5090 break; |
| 5091 } |
| 5092 |
| 5093 default: { |
| 5094 Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 5095 fts3EvalInvalidatePoslist(pPhrase); |
| 5096 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof); |
| 5097 pExpr->iDocid = pPhrase->doclist.iDocid; |
| 5098 break; |
| 5099 } |
| 5100 } |
| 5101 } |
| 5102 } |
| 5103 |
| 5104 /* |
| 5105 ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR |
| 5106 ** cluster, then this function returns 1 immediately. |
| 5107 ** |
| 5108 ** Otherwise, it checks if the current row really does match the NEAR |
| 5109 ** expression, using the data currently stored in the position lists |
| 5110 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression. |
| 5111 ** |
| 5112 ** If the current row is a match, the position list associated with each |
| 5113 ** phrase in the NEAR expression is edited in place to contain only those |
| 5114 ** phrase instances sufficiently close to their peers to satisfy all NEAR |
| 5115 ** constraints. In this case it returns 1. If the NEAR expression does not |
| 5116 ** match the current row, 0 is returned. The position lists may or may not |
| 5117 ** be edited if 0 is returned. |
| 5118 */ |
| 5119 static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){ |
| 5120 int res = 1; |
| 5121 |
| 5122 /* The following block runs if pExpr is the root of a NEAR query. |
| 5123 ** For example, the query: |
| 5124 ** |
| 5125 ** "w" NEAR "x" NEAR "y" NEAR "z" |
| 5126 ** |
| 5127 ** which is represented in tree form as: |
| 5128 ** |
| 5129 ** | |
| 5130 ** +--NEAR--+ <-- root of NEAR query |
| 5131 ** | | |
| 5132 ** +--NEAR--+ "z" |
| 5133 ** | | |
| 5134 ** +--NEAR--+ "y" |
| 5135 ** | | |
| 5136 ** "w" "x" |
| 5137 ** |
| 5138 ** The right-hand child of a NEAR node is always a phrase. The |
| 5139 ** left-hand child may be either a phrase or a NEAR node. There are |
| 5140 ** no exceptions to this - it's the way the parser in fts3_expr.c works. |
| 5141 */ |
| 5142 if( *pRc==SQLITE_OK |
| 5143 && pExpr->eType==FTSQUERY_NEAR |
| 5144 && pExpr->bEof==0 |
| 5145 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR) |
| 5146 ){ |
| 5147 Fts3Expr *p; |
| 5148 int nTmp = 0; /* Bytes of temp space */ |
| 5149 char *aTmp; /* Temp space for PoslistNearMerge() */ |
| 5150 |
| 5151 /* Allocate temporary working space. */ |
| 5152 for(p=pExpr; p->pLeft; p=p->pLeft){ |
| 5153 nTmp += p->pRight->pPhrase->doclist.nList; |
| 5154 } |
| 5155 nTmp += p->pPhrase->doclist.nList; |
| 5156 if( nTmp==0 ){ |
| 5157 res = 0; |
| 5158 }else{ |
| 5159 aTmp = sqlite3_malloc(nTmp*2); |
| 5160 if( !aTmp ){ |
| 5161 *pRc = SQLITE_NOMEM; |
| 5162 res = 0; |
| 5163 }else{ |
| 5164 char *aPoslist = p->pPhrase->doclist.pList; |
| 5165 int nToken = p->pPhrase->nToken; |
| 5166 |
| 5167 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){ |
| 5168 Fts3Phrase *pPhrase = p->pRight->pPhrase; |
| 5169 int nNear = p->nNear; |
| 5170 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); |
| 5171 } |
| 5172 |
| 5173 aPoslist = pExpr->pRight->pPhrase->doclist.pList; |
| 5174 nToken = pExpr->pRight->pPhrase->nToken; |
| 5175 for(p=pExpr->pLeft; p && res; p=p->pLeft){ |
| 5176 int nNear; |
| 5177 Fts3Phrase *pPhrase; |
| 5178 assert( p->pParent && p->pParent->pLeft==p ); |
| 5179 nNear = p->pParent->nNear; |
| 5180 pPhrase = ( |
| 5181 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase |
| 5182 ); |
| 5183 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); |
| 5184 } |
| 5185 } |
| 5186 |
| 5187 sqlite3_free(aTmp); |
| 5188 } |
| 5189 } |
| 5190 |
| 5191 return res; |
| 5192 } |
| 5193 |
| 5194 /* |
| 5195 ** This function is a helper function for fts3EvalTestDeferredAndNear(). |
| 5196 ** Assuming no error occurs or has occurred, It returns non-zero if the |
| 5197 ** expression passed as the second argument matches the row that pCsr |
| 5198 ** currently points to, or zero if it does not. |
| 5199 ** |
| 5200 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
| 5201 ** If an error occurs during execution of this function, *pRc is set to |
| 5202 ** the appropriate SQLite error code. In this case the returned value is |
| 5203 ** undefined. |
| 5204 */ |
| 5205 static int fts3EvalTestExpr( |
| 5206 Fts3Cursor *pCsr, /* FTS cursor handle */ |
| 5207 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */ |
| 5208 int *pRc /* IN/OUT: Error code */ |
| 5209 ){ |
| 5210 int bHit = 1; /* Return value */ |
| 5211 if( *pRc==SQLITE_OK ){ |
| 5212 switch( pExpr->eType ){ |
| 5213 case FTSQUERY_NEAR: |
| 5214 case FTSQUERY_AND: |
| 5215 bHit = ( |
| 5216 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc) |
| 5217 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc) |
| 5218 && fts3EvalNearTest(pExpr, pRc) |
| 5219 ); |
| 5220 |
| 5221 /* If the NEAR expression does not match any rows, zero the doclist for |
| 5222 ** all phrases involved in the NEAR. This is because the snippet(), |
| 5223 ** offsets() and matchinfo() functions are not supposed to recognize |
| 5224 ** any instances of phrases that are part of unmatched NEAR queries. |
| 5225 ** For example if this expression: |
| 5226 ** |
| 5227 ** ... MATCH 'a OR (b NEAR c)' |
| 5228 ** |
| 5229 ** is matched against a row containing: |
| 5230 ** |
| 5231 ** 'a b d e' |
| 5232 ** |
| 5233 ** then any snippet() should ony highlight the "a" term, not the "b" |
| 5234 ** (as "b" is part of a non-matching NEAR clause). |
| 5235 */ |
| 5236 if( bHit==0 |
| 5237 && pExpr->eType==FTSQUERY_NEAR |
| 5238 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR) |
| 5239 ){ |
| 5240 Fts3Expr *p; |
| 5241 for(p=pExpr; p->pPhrase==0; p=p->pLeft){ |
| 5242 if( p->pRight->iDocid==pCsr->iPrevId ){ |
| 5243 fts3EvalInvalidatePoslist(p->pRight->pPhrase); |
| 5244 } |
| 5245 } |
| 5246 if( p->iDocid==pCsr->iPrevId ){ |
| 5247 fts3EvalInvalidatePoslist(p->pPhrase); |
| 5248 } |
| 5249 } |
| 5250 |
| 5251 break; |
| 5252 |
| 5253 case FTSQUERY_OR: { |
| 5254 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc); |
| 5255 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc); |
| 5256 bHit = bHit1 || bHit2; |
| 5257 break; |
| 5258 } |
| 5259 |
| 5260 case FTSQUERY_NOT: |
| 5261 bHit = ( |
| 5262 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc) |
| 5263 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc) |
| 5264 ); |
| 5265 break; |
| 5266 |
| 5267 default: { |
| 5268 #ifndef SQLITE_DISABLE_FTS4_DEFERRED |
| 5269 if( pCsr->pDeferred |
| 5270 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred) |
| 5271 ){ |
| 5272 Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 5273 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 ); |
| 5274 if( pExpr->bDeferred ){ |
| 5275 fts3EvalInvalidatePoslist(pPhrase); |
| 5276 } |
| 5277 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase); |
| 5278 bHit = (pPhrase->doclist.pList!=0); |
| 5279 pExpr->iDocid = pCsr->iPrevId; |
| 5280 }else |
| 5281 #endif |
| 5282 { |
| 5283 bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId); |
| 5284 } |
| 5285 break; |
| 5286 } |
| 5287 } |
| 5288 } |
| 5289 return bHit; |
| 5290 } |
| 5291 |
| 5292 /* |
| 5293 ** This function is called as the second part of each xNext operation when |
| 5294 ** iterating through the results of a full-text query. At this point the |
| 5295 ** cursor points to a row that matches the query expression, with the |
| 5296 ** following caveats: |
| 5297 ** |
| 5298 ** * Up until this point, "NEAR" operators in the expression have been |
| 5299 ** treated as "AND". |
| 5300 ** |
| 5301 ** * Deferred tokens have not yet been considered. |
| 5302 ** |
| 5303 ** If *pRc is not SQLITE_OK when this function is called, it immediately |
| 5304 ** returns 0. Otherwise, it tests whether or not after considering NEAR |
| 5305 ** operators and deferred tokens the current row is still a match for the |
| 5306 ** expression. It returns 1 if both of the following are true: |
| 5307 ** |
| 5308 ** 1. *pRc is SQLITE_OK when this function returns, and |
| 5309 ** |
| 5310 ** 2. After scanning the current FTS table row for the deferred tokens, |
| 5311 ** it is determined that the row does *not* match the query. |
| 5312 ** |
| 5313 ** Or, if no error occurs and it seems the current row does match the FTS |
| 5314 ** query, return 0. |
| 5315 */ |
| 5316 static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){ |
| 5317 int rc = *pRc; |
| 5318 int bMiss = 0; |
| 5319 if( rc==SQLITE_OK ){ |
| 5320 |
| 5321 /* If there are one or more deferred tokens, load the current row into |
| 5322 ** memory and scan it to determine the position list for each deferred |
| 5323 ** token. Then, see if this row is really a match, considering deferred |
| 5324 ** tokens and NEAR operators (neither of which were taken into account |
| 5325 ** earlier, by fts3EvalNextRow()). |
| 5326 */ |
| 5327 if( pCsr->pDeferred ){ |
| 5328 rc = fts3CursorSeek(0, pCsr); |
| 5329 if( rc==SQLITE_OK ){ |
| 5330 rc = sqlite3Fts3CacheDeferredDoclists(pCsr); |
| 5331 } |
| 5332 } |
| 5333 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc)); |
| 5334 |
| 5335 /* Free the position-lists accumulated for each deferred token above. */ |
| 5336 sqlite3Fts3FreeDeferredDoclists(pCsr); |
| 5337 *pRc = rc; |
| 5338 } |
| 5339 return (rc==SQLITE_OK && bMiss); |
| 5340 } |
| 5341 |
| 5342 /* |
| 5343 ** Advance to the next document that matches the FTS expression in |
| 5344 ** Fts3Cursor.pExpr. |
| 5345 */ |
| 5346 static int fts3EvalNext(Fts3Cursor *pCsr){ |
| 5347 int rc = SQLITE_OK; /* Return Code */ |
| 5348 Fts3Expr *pExpr = pCsr->pExpr; |
| 5349 assert( pCsr->isEof==0 ); |
| 5350 if( pExpr==0 ){ |
| 5351 pCsr->isEof = 1; |
| 5352 }else{ |
| 5353 do { |
| 5354 if( pCsr->isRequireSeek==0 ){ |
| 5355 sqlite3_reset(pCsr->pStmt); |
| 5356 } |
| 5357 assert( sqlite3_data_count(pCsr->pStmt)==0 ); |
| 5358 fts3EvalNextRow(pCsr, pExpr, &rc); |
| 5359 pCsr->isEof = pExpr->bEof; |
| 5360 pCsr->isRequireSeek = 1; |
| 5361 pCsr->isMatchinfoNeeded = 1; |
| 5362 pCsr->iPrevId = pExpr->iDocid; |
| 5363 }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) ); |
| 5364 } |
| 5365 |
| 5366 /* Check if the cursor is past the end of the docid range specified |
| 5367 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */ |
| 5368 if( rc==SQLITE_OK && ( |
| 5369 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid) |
| 5370 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid) |
| 5371 )){ |
| 5372 pCsr->isEof = 1; |
| 5373 } |
| 5374 |
| 5375 return rc; |
| 5376 } |
| 5377 |
| 5378 /* |
| 5379 ** Restart interation for expression pExpr so that the next call to |
| 5380 ** fts3EvalNext() visits the first row. Do not allow incremental |
| 5381 ** loading or merging of phrase doclists for this iteration. |
| 5382 ** |
| 5383 ** If *pRc is other than SQLITE_OK when this function is called, it is |
| 5384 ** a no-op. If an error occurs within this function, *pRc is set to an |
| 5385 ** SQLite error code before returning. |
| 5386 */ |
| 5387 static void fts3EvalRestart( |
| 5388 Fts3Cursor *pCsr, |
| 5389 Fts3Expr *pExpr, |
| 5390 int *pRc |
| 5391 ){ |
| 5392 if( pExpr && *pRc==SQLITE_OK ){ |
| 5393 Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 5394 |
| 5395 if( pPhrase ){ |
| 5396 fts3EvalInvalidatePoslist(pPhrase); |
| 5397 if( pPhrase->bIncr ){ |
| 5398 int i; |
| 5399 for(i=0; i<pPhrase->nToken; i++){ |
| 5400 Fts3PhraseToken *pToken = &pPhrase->aToken[i]; |
| 5401 assert( pToken->pDeferred==0 ); |
| 5402 if( pToken->pSegcsr ){ |
| 5403 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr); |
| 5404 } |
| 5405 } |
| 5406 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase); |
| 5407 } |
| 5408 pPhrase->doclist.pNextDocid = 0; |
| 5409 pPhrase->doclist.iDocid = 0; |
| 5410 } |
| 5411 |
| 5412 pExpr->iDocid = 0; |
| 5413 pExpr->bEof = 0; |
| 5414 pExpr->bStart = 0; |
| 5415 |
| 5416 fts3EvalRestart(pCsr, pExpr->pLeft, pRc); |
| 5417 fts3EvalRestart(pCsr, pExpr->pRight, pRc); |
| 5418 } |
| 5419 } |
| 5420 |
| 5421 /* |
| 5422 ** After allocating the Fts3Expr.aMI[] array for each phrase in the |
| 5423 ** expression rooted at pExpr, the cursor iterates through all rows matched |
| 5424 ** by pExpr, calling this function for each row. This function increments |
| 5425 ** the values in Fts3Expr.aMI[] according to the position-list currently |
| 5426 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase |
| 5427 ** expression nodes. |
| 5428 */ |
| 5429 static void fts3EvalUpdateCounts(Fts3Expr *pExpr){ |
| 5430 if( pExpr ){ |
| 5431 Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 5432 if( pPhrase && pPhrase->doclist.pList ){ |
| 5433 int iCol = 0; |
| 5434 char *p = pPhrase->doclist.pList; |
| 5435 |
| 5436 assert( *p ); |
| 5437 while( 1 ){ |
| 5438 u8 c = 0; |
| 5439 int iCnt = 0; |
| 5440 while( 0xFE & (*p | c) ){ |
| 5441 if( (c&0x80)==0 ) iCnt++; |
| 5442 c = *p++ & 0x80; |
| 5443 } |
| 5444 |
| 5445 /* aMI[iCol*3 + 1] = Number of occurrences |
| 5446 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance |
| 5447 */ |
| 5448 pExpr->aMI[iCol*3 + 1] += iCnt; |
| 5449 pExpr->aMI[iCol*3 + 2] += (iCnt>0); |
| 5450 if( *p==0x00 ) break; |
| 5451 p++; |
| 5452 p += fts3GetVarint32(p, &iCol); |
| 5453 } |
| 5454 } |
| 5455 |
| 5456 fts3EvalUpdateCounts(pExpr->pLeft); |
| 5457 fts3EvalUpdateCounts(pExpr->pRight); |
| 5458 } |
| 5459 } |
| 5460 |
| 5461 /* |
| 5462 ** Expression pExpr must be of type FTSQUERY_PHRASE. |
| 5463 ** |
| 5464 ** If it is not already allocated and populated, this function allocates and |
| 5465 ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part |
| 5466 ** of a NEAR expression, then it also allocates and populates the same array |
| 5467 ** for all other phrases that are part of the NEAR expression. |
| 5468 ** |
| 5469 ** SQLITE_OK is returned if the aMI[] array is successfully allocated and |
| 5470 ** populated. Otherwise, if an error occurs, an SQLite error code is returned. |
| 5471 */ |
| 5472 static int fts3EvalGatherStats( |
| 5473 Fts3Cursor *pCsr, /* Cursor object */ |
| 5474 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */ |
| 5475 ){ |
| 5476 int rc = SQLITE_OK; /* Return code */ |
| 5477 |
| 5478 assert( pExpr->eType==FTSQUERY_PHRASE ); |
| 5479 if( pExpr->aMI==0 ){ |
| 5480 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 5481 Fts3Expr *pRoot; /* Root of NEAR expression */ |
| 5482 Fts3Expr *p; /* Iterator used for several purposes */ |
| 5483 |
| 5484 sqlite3_int64 iPrevId = pCsr->iPrevId; |
| 5485 sqlite3_int64 iDocid; |
| 5486 u8 bEof; |
| 5487 |
| 5488 /* Find the root of the NEAR expression */ |
| 5489 pRoot = pExpr; |
| 5490 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){ |
| 5491 pRoot = pRoot->pParent; |
| 5492 } |
| 5493 iDocid = pRoot->iDocid; |
| 5494 bEof = pRoot->bEof; |
| 5495 assert( pRoot->bStart ); |
| 5496 |
| 5497 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */ |
| 5498 for(p=pRoot; p; p=p->pLeft){ |
| 5499 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight); |
| 5500 assert( pE->aMI==0 ); |
| 5501 pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32)); |
| 5502 if( !pE->aMI ) return SQLITE_NOMEM; |
| 5503 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32)); |
| 5504 } |
| 5505 |
| 5506 fts3EvalRestart(pCsr, pRoot, &rc); |
| 5507 |
| 5508 while( pCsr->isEof==0 && rc==SQLITE_OK ){ |
| 5509 |
| 5510 do { |
| 5511 /* Ensure the %_content statement is reset. */ |
| 5512 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt); |
| 5513 assert( sqlite3_data_count(pCsr->pStmt)==0 ); |
| 5514 |
| 5515 /* Advance to the next document */ |
| 5516 fts3EvalNextRow(pCsr, pRoot, &rc); |
| 5517 pCsr->isEof = pRoot->bEof; |
| 5518 pCsr->isRequireSeek = 1; |
| 5519 pCsr->isMatchinfoNeeded = 1; |
| 5520 pCsr->iPrevId = pRoot->iDocid; |
| 5521 }while( pCsr->isEof==0 |
| 5522 && pRoot->eType==FTSQUERY_NEAR |
| 5523 && fts3EvalTestDeferredAndNear(pCsr, &rc) |
| 5524 ); |
| 5525 |
| 5526 if( rc==SQLITE_OK && pCsr->isEof==0 ){ |
| 5527 fts3EvalUpdateCounts(pRoot); |
| 5528 } |
| 5529 } |
| 5530 |
| 5531 pCsr->isEof = 0; |
| 5532 pCsr->iPrevId = iPrevId; |
| 5533 |
| 5534 if( bEof ){ |
| 5535 pRoot->bEof = bEof; |
| 5536 }else{ |
| 5537 /* Caution: pRoot may iterate through docids in ascending or descending |
| 5538 ** order. For this reason, even though it seems more defensive, the |
| 5539 ** do loop can not be written: |
| 5540 ** |
| 5541 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK ); |
| 5542 */ |
| 5543 fts3EvalRestart(pCsr, pRoot, &rc); |
| 5544 do { |
| 5545 fts3EvalNextRow(pCsr, pRoot, &rc); |
| 5546 assert( pRoot->bEof==0 ); |
| 5547 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK ); |
| 5548 fts3EvalTestDeferredAndNear(pCsr, &rc); |
| 5549 } |
| 5550 } |
| 5551 return rc; |
| 5552 } |
| 5553 |
| 5554 /* |
| 5555 ** This function is used by the matchinfo() module to query a phrase |
| 5556 ** expression node for the following information: |
| 5557 ** |
| 5558 ** 1. The total number of occurrences of the phrase in each column of |
| 5559 ** the FTS table (considering all rows), and |
| 5560 ** |
| 5561 ** 2. For each column, the number of rows in the table for which the |
| 5562 ** column contains at least one instance of the phrase. |
| 5563 ** |
| 5564 ** If no error occurs, SQLITE_OK is returned and the values for each column |
| 5565 ** written into the array aiOut as follows: |
| 5566 ** |
| 5567 ** aiOut[iCol*3 + 1] = Number of occurrences |
| 5568 ** aiOut[iCol*3 + 2] = Number of rows containing at least one instance |
| 5569 ** |
| 5570 ** Caveats: |
| 5571 ** |
| 5572 ** * If a phrase consists entirely of deferred tokens, then all output |
| 5573 ** values are set to the number of documents in the table. In other |
| 5574 ** words we assume that very common tokens occur exactly once in each |
| 5575 ** column of each row of the table. |
| 5576 ** |
| 5577 ** * If a phrase contains some deferred tokens (and some non-deferred |
| 5578 ** tokens), count the potential occurrence identified by considering |
| 5579 ** the non-deferred tokens instead of actual phrase occurrences. |
| 5580 ** |
| 5581 ** * If the phrase is part of a NEAR expression, then only phrase instances |
| 5582 ** that meet the NEAR constraint are included in the counts. |
| 5583 */ |
| 5584 int sqlite3Fts3EvalPhraseStats( |
| 5585 Fts3Cursor *pCsr, /* FTS cursor handle */ |
| 5586 Fts3Expr *pExpr, /* Phrase expression */ |
| 5587 u32 *aiOut /* Array to write results into (see above) */ |
| 5588 ){ |
| 5589 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 5590 int rc = SQLITE_OK; |
| 5591 int iCol; |
| 5592 |
| 5593 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){ |
| 5594 assert( pCsr->nDoc>0 ); |
| 5595 for(iCol=0; iCol<pTab->nColumn; iCol++){ |
| 5596 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc; |
| 5597 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc; |
| 5598 } |
| 5599 }else{ |
| 5600 rc = fts3EvalGatherStats(pCsr, pExpr); |
| 5601 if( rc==SQLITE_OK ){ |
| 5602 assert( pExpr->aMI ); |
| 5603 for(iCol=0; iCol<pTab->nColumn; iCol++){ |
| 5604 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1]; |
| 5605 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2]; |
| 5606 } |
| 5607 } |
| 5608 } |
| 5609 |
| 5610 return rc; |
| 5611 } |
| 5612 |
| 5613 /* |
| 5614 ** The expression pExpr passed as the second argument to this function |
| 5615 ** must be of type FTSQUERY_PHRASE. |
| 5616 ** |
| 5617 ** The returned value is either NULL or a pointer to a buffer containing |
| 5618 ** a position-list indicating the occurrences of the phrase in column iCol |
| 5619 ** of the current row. |
| 5620 ** |
| 5621 ** More specifically, the returned buffer contains 1 varint for each |
| 5622 ** occurrence of the phrase in the column, stored using the normal (delta+2) |
| 5623 ** compression and is terminated by either an 0x01 or 0x00 byte. For example, |
| 5624 ** if the requested column contains "a b X c d X X" and the position-list |
| 5625 ** for 'X' is requested, the buffer returned may contain: |
| 5626 ** |
| 5627 ** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00 |
| 5628 ** |
| 5629 ** This function works regardless of whether or not the phrase is deferred, |
| 5630 ** incremental, or neither. |
| 5631 */ |
| 5632 int sqlite3Fts3EvalPhrasePoslist( |
| 5633 Fts3Cursor *pCsr, /* FTS3 cursor object */ |
| 5634 Fts3Expr *pExpr, /* Phrase to return doclist for */ |
| 5635 int iCol, /* Column to return position list for */ |
| 5636 char **ppOut /* OUT: Pointer to position list */ |
| 5637 ){ |
| 5638 Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 5639 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 5640 char *pIter; |
| 5641 int iThis; |
| 5642 sqlite3_int64 iDocid; |
| 5643 |
| 5644 /* If this phrase is applies specifically to some column other than |
| 5645 ** column iCol, return a NULL pointer. */ |
| 5646 *ppOut = 0; |
| 5647 assert( iCol>=0 && iCol<pTab->nColumn ); |
| 5648 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){ |
| 5649 return SQLITE_OK; |
| 5650 } |
| 5651 |
| 5652 iDocid = pExpr->iDocid; |
| 5653 pIter = pPhrase->doclist.pList; |
| 5654 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){ |
| 5655 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */ |
| 5656 int iMul; /* +1 if csr dir matches index dir, else -1 */ |
| 5657 int bOr = 0; |
| 5658 u8 bEof = 0; |
| 5659 u8 bTreeEof = 0; |
| 5660 Fts3Expr *p; /* Used to iterate from pExpr to root */ |
| 5661 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */ |
| 5662 |
| 5663 /* Check if this phrase descends from an OR expression node. If not, |
| 5664 ** return NULL. Otherwise, the entry that corresponds to docid |
| 5665 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the |
| 5666 ** tree that the node is part of has been marked as EOF, but the node |
| 5667 ** itself is not EOF, then it may point to an earlier entry. */ |
| 5668 pNear = pExpr; |
| 5669 for(p=pExpr->pParent; p; p=p->pParent){ |
| 5670 if( p->eType==FTSQUERY_OR ) bOr = 1; |
| 5671 if( p->eType==FTSQUERY_NEAR ) pNear = p; |
| 5672 if( p->bEof ) bTreeEof = 1; |
| 5673 } |
| 5674 if( bOr==0 ) return SQLITE_OK; |
| 5675 |
| 5676 /* This is the descendent of an OR node. In this case we cannot use |
| 5677 ** an incremental phrase. Load the entire doclist for the phrase |
| 5678 ** into memory in this case. */ |
| 5679 if( pPhrase->bIncr ){ |
| 5680 int rc = SQLITE_OK; |
| 5681 int bEofSave = pExpr->bEof; |
| 5682 fts3EvalRestart(pCsr, pExpr, &rc); |
| 5683 while( rc==SQLITE_OK && !pExpr->bEof ){ |
| 5684 fts3EvalNextRow(pCsr, pExpr, &rc); |
| 5685 if( bEofSave==0 && pExpr->iDocid==iDocid ) break; |
| 5686 } |
| 5687 pIter = pPhrase->doclist.pList; |
| 5688 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 ); |
| 5689 if( rc!=SQLITE_OK ) return rc; |
| 5690 } |
| 5691 |
| 5692 iMul = ((pCsr->bDesc==bDescDoclist) ? 1 : -1); |
| 5693 while( bTreeEof==1 |
| 5694 && pNear->bEof==0 |
| 5695 && (DOCID_CMP(pNear->iDocid, pCsr->iPrevId) * iMul)<0 |
| 5696 ){ |
| 5697 int rc = SQLITE_OK; |
| 5698 fts3EvalNextRow(pCsr, pExpr, &rc); |
| 5699 if( rc!=SQLITE_OK ) return rc; |
| 5700 iDocid = pExpr->iDocid; |
| 5701 pIter = pPhrase->doclist.pList; |
| 5702 } |
| 5703 |
| 5704 bEof = (pPhrase->doclist.nAll==0); |
| 5705 assert( bDescDoclist==0 || bDescDoclist==1 ); |
| 5706 assert( pCsr->bDesc==0 || pCsr->bDesc==1 ); |
| 5707 |
| 5708 if( bEof==0 ){ |
| 5709 if( pCsr->bDesc==bDescDoclist ){ |
| 5710 int dummy; |
| 5711 if( pNear->bEof ){ |
| 5712 /* This expression is already at EOF. So position it to point to the |
| 5713 ** last entry in the doclist at pPhrase->doclist.aAll[]. Variable |
| 5714 ** iDocid is already set for this entry, so all that is required is |
| 5715 ** to set pIter to point to the first byte of the last position-list |
| 5716 ** in the doclist. |
| 5717 ** |
| 5718 ** It would also be correct to set pIter and iDocid to zero. In |
| 5719 ** this case, the first call to sqltie3Fts4DoclistPrev() below |
| 5720 ** would also move the iterator to point to the last entry in the |
| 5721 ** doclist. However, this is expensive, as to do so it has to |
| 5722 ** iterate through the entire doclist from start to finish (since |
| 5723 ** it does not know the docid for the last entry). */ |
| 5724 pIter = &pPhrase->doclist.aAll[pPhrase->doclist.nAll-1]; |
| 5725 fts3ReversePoslist(pPhrase->doclist.aAll, &pIter); |
| 5726 } |
| 5727 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){ |
| 5728 sqlite3Fts3DoclistPrev( |
| 5729 bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, |
| 5730 &pIter, &iDocid, &dummy, &bEof |
| 5731 ); |
| 5732 } |
| 5733 }else{ |
| 5734 if( pNear->bEof ){ |
| 5735 pIter = 0; |
| 5736 iDocid = 0; |
| 5737 } |
| 5738 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){ |
| 5739 sqlite3Fts3DoclistNext( |
| 5740 bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, |
| 5741 &pIter, &iDocid, &bEof |
| 5742 ); |
| 5743 } |
| 5744 } |
| 5745 } |
| 5746 |
| 5747 if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0; |
| 5748 } |
| 5749 if( pIter==0 ) return SQLITE_OK; |
| 5750 |
| 5751 if( *pIter==0x01 ){ |
| 5752 pIter++; |
| 5753 pIter += fts3GetVarint32(pIter, &iThis); |
| 5754 }else{ |
| 5755 iThis = 0; |
| 5756 } |
| 5757 while( iThis<iCol ){ |
| 5758 fts3ColumnlistCopy(0, &pIter); |
| 5759 if( *pIter==0x00 ) return 0; |
| 5760 pIter++; |
| 5761 pIter += fts3GetVarint32(pIter, &iThis); |
| 5762 } |
| 5763 |
| 5764 *ppOut = ((iCol==iThis)?pIter:0); |
| 5765 return SQLITE_OK; |
| 5766 } |
| 5767 |
| 5768 /* |
| 5769 ** Free all components of the Fts3Phrase structure that were allocated by |
| 5770 ** the eval module. Specifically, this means to free: |
| 5771 ** |
| 5772 ** * the contents of pPhrase->doclist, and |
| 5773 ** * any Fts3MultiSegReader objects held by phrase tokens. |
| 5774 */ |
| 5775 void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){ |
| 5776 if( pPhrase ){ |
| 5777 int i; |
| 5778 sqlite3_free(pPhrase->doclist.aAll); |
| 5779 fts3EvalInvalidatePoslist(pPhrase); |
| 5780 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist)); |
| 5781 for(i=0; i<pPhrase->nToken; i++){ |
| 5782 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr); |
| 5783 pPhrase->aToken[i].pSegcsr = 0; |
| 5784 } |
| 5785 } |
| 5786 } |
| 5787 |
| 5788 |
| 5789 /* |
| 5790 ** Return SQLITE_CORRUPT_VTAB. |
| 5791 */ |
| 5792 #ifdef SQLITE_DEBUG |
| 5793 int sqlite3Fts3Corrupt(){ |
| 5794 return SQLITE_CORRUPT_VTAB; |
| 5795 } |
| 5796 #endif |
| 5797 |
| 3694 #if !SQLITE_CORE | 5798 #if !SQLITE_CORE |
| 3695 int sqlite3_extension_init( | 5799 /* |
| 5800 ** Initialize API pointer table, if required. |
| 5801 */ |
| 5802 #ifdef _WIN32 |
| 5803 __declspec(dllexport) |
| 5804 #endif |
| 5805 int sqlite3_fts3_init( |
| 3696 sqlite3 *db, | 5806 sqlite3 *db, |
| 3697 char **pzErrMsg, | 5807 char **pzErrMsg, |
| 3698 const sqlite3_api_routines *pApi | 5808 const sqlite3_api_routines *pApi |
| 3699 ){ | 5809 ){ |
| 3700 SQLITE_EXTENSION_INIT2(pApi) | 5810 SQLITE_EXTENSION_INIT2(pApi) |
| 3701 return sqlite3Fts3Init(db); | 5811 return sqlite3Fts3Init(db); |
| 3702 } | 5812 } |
| 3703 #endif | 5813 #endif |
| 3704 | 5814 |
| 3705 #endif | 5815 #endif |
| OLD | NEW |