| 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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 ** we simply write the new doclist. Segment merges overwrite older | 264 ** we simply write the new doclist. Segment merges overwrite older |
| 265 ** data for a particular docid with newer data, so deletes or updates | 265 ** data for a particular docid with newer data, so deletes or updates |
| 266 ** will eventually overtake the earlier data and knock it out. The | 266 ** will eventually overtake the earlier data and knock it out. The |
| 267 ** query logic likewise merges doclists so that newer data knocks out | 267 ** query logic likewise merges doclists so that newer data knocks out |
| 268 ** older data. | 268 ** older data. |
| 269 ** | 269 ** |
| 270 ** TODO(shess) Provide a VACUUM type operation to clear out all | 270 ** TODO(shess) Provide a VACUUM type operation to clear out all |
| 271 ** deletions and duplications. This would basically be a forced merge | 271 ** deletions and duplications. This would basically be a forced merge |
| 272 ** into a single segment. | 272 ** into a single segment. |
| 273 */ | 273 */ |
| 274 #define CHROMIUM_FTS3_CHANGES 1 |
| 274 | 275 |
| 275 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) | 276 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 276 | 277 |
| 277 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) | 278 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) |
| 278 # define SQLITE_CORE 1 | 279 # define SQLITE_CORE 1 |
| 279 #endif | 280 #endif |
| 280 | 281 |
| 281 #include <assert.h> | 282 #include <assert.h> |
| 282 #include <stdlib.h> | 283 #include <stdlib.h> |
| 283 #include <stdio.h> | 284 #include <stdio.h> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 306 ** Put the query functions last because they're likely to reference | 307 ** Put the query functions last because they're likely to reference |
| 307 ** typedefs or functions from the table update section. | 308 ** typedefs or functions from the table update section. |
| 308 */ | 309 */ |
| 309 | 310 |
| 310 #if 0 | 311 #if 0 |
| 311 # define FTSTRACE(A) printf A; fflush(stdout) | 312 # define FTSTRACE(A) printf A; fflush(stdout) |
| 312 #else | 313 #else |
| 313 # define FTSTRACE(A) | 314 # define FTSTRACE(A) |
| 314 #endif | 315 #endif |
| 315 | 316 |
| 317 #if 0 |
| 318 /* Useful to set breakpoints. See main.c sqlite3Corrupt(). */ |
| 319 static int fts3Corrupt(void){ |
| 320 return SQLITE_CORRUPT; |
| 321 } |
| 322 # define SQLITE_CORRUPT_BKPT fts3Corrupt() |
| 323 #else |
| 324 # define SQLITE_CORRUPT_BKPT SQLITE_CORRUPT |
| 325 #endif |
| 326 |
| 316 /* It is not safe to call isspace(), tolower(), or isalnum() on | 327 /* It is not safe to call isspace(), tolower(), or isalnum() on |
| 317 ** hi-bit-set characters. This is the same solution used in the | 328 ** hi-bit-set characters. This is the same solution used in the |
| 318 ** tokenizer. | 329 ** tokenizer. |
| 319 */ | 330 */ |
| 320 /* TODO(shess) The snippet-generation code should be using the | 331 /* TODO(shess) The snippet-generation code should be using the |
| 321 ** tokenizer-generated tokens rather than doing its own local | 332 ** tokenizer-generated tokens rather than doing its own local |
| 322 ** tokenization. | 333 ** tokenization. |
| 323 */ | 334 */ |
| 324 /* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */ | 335 /* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */ |
| 325 static int safe_isspace(char c){ | 336 static int safe_isspace(char c){ |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 vu >>= 7; | 405 vu >>= 7; |
| 395 }while( vu!=0 ); | 406 }while( vu!=0 ); |
| 396 q[-1] &= 0x7f; /* turn off high bit in final byte */ | 407 q[-1] &= 0x7f; /* turn off high bit in final byte */ |
| 397 assert( q - (unsigned char *)p <= VARINT_MAX ); | 408 assert( q - (unsigned char *)p <= VARINT_MAX ); |
| 398 return (int) (q - (unsigned char *)p); | 409 return (int) (q - (unsigned char *)p); |
| 399 } | 410 } |
| 400 | 411 |
| 401 /* Read a 64-bit variable-length integer from memory starting at p[0]. | 412 /* Read a 64-bit variable-length integer from memory starting at p[0]. |
| 402 * Return the number of bytes read, or 0 on error. | 413 * Return the number of bytes read, or 0 on error. |
| 403 * The value is stored in *v. */ | 414 * The value is stored in *v. */ |
| 404 static int fts3GetVarint(const char *p, sqlite_int64 *v){ | 415 static int fts3GetVarintSafe(const char *p, sqlite_int64 *v, int max){ |
| 405 const unsigned char *q = (const unsigned char *) p; | 416 const unsigned char *q = (const unsigned char *) p; |
| 406 sqlite_uint64 x = 0, y = 1; | 417 sqlite_uint64 x = 0, y = 1; |
| 407 while( (*q & 0x80) == 0x80 ){ | 418 if( max>VARINT_MAX ) max = VARINT_MAX; |
| 419 while( max && (*q & 0x80) == 0x80 ){ |
| 420 max--; |
| 408 x += y * (*q++ & 0x7f); | 421 x += y * (*q++ & 0x7f); |
| 409 y <<= 7; | 422 y <<= 7; |
| 410 if( q - (unsigned char *)p >= VARINT_MAX ){ /* bad data */ | 423 } |
| 411 assert( 0 ); | 424 if( !max ){ |
| 412 return 0; | 425 assert( 0 ); |
| 413 } | 426 return 0; /* tried to read too much; bad data */ |
| 414 } | 427 } |
| 415 x += y * (*q++); | 428 x += y * (*q++); |
| 416 *v = (sqlite_int64) x; | 429 *v = (sqlite_int64) x; |
| 417 return (int) (q - (unsigned char *)p); | 430 return (int) (q - (unsigned char *)p); |
| 418 } | 431 } |
| 419 | 432 |
| 420 static int fts3GetVarint32(const char *p, int *pi){ | 433 static int fts3GetVarint(const char *p, sqlite_int64 *v){ |
| 434 return fts3GetVarintSafe(p, v, VARINT_MAX); |
| 435 } |
| 436 |
| 437 static int fts3GetVarint32Safe(const char *p, int *pi, int max){ |
| 421 sqlite_int64 i; | 438 sqlite_int64 i; |
| 422 int ret = fts3GetVarint(p, &i); | 439 int ret = fts3GetVarintSafe(p, &i, max); |
| 440 if( !ret ) return ret; |
| 423 *pi = (int) i; | 441 *pi = (int) i; |
| 424 assert( *pi==i ); | 442 assert( *pi==i ); |
| 425 return ret; | 443 return ret; |
| 426 } | 444 } |
| 427 | 445 |
| 446 static int fts3GetVarint32(const char* p, int *pi){ |
| 447 return fts3GetVarint32Safe(p, pi, VARINT_MAX); |
| 448 } |
| 449 |
| 428 /*******************************************************************/ | 450 /*******************************************************************/ |
| 429 /* DataBuffer is used to collect data into a buffer in piecemeal | 451 /* DataBuffer is used to collect data into a buffer in piecemeal |
| 430 ** fashion. It implements the usual distinction between amount of | 452 ** fashion. It implements the usual distinction between amount of |
| 431 ** data currently stored (nData) and buffer capacity (nCapacity). | 453 ** data currently stored (nData) and buffer capacity (nCapacity). |
| 432 ** | 454 ** |
| 433 ** dataBufferInit - create a buffer with given initial capacity. | 455 ** dataBufferInit - create a buffer with given initial capacity. |
| 434 ** dataBufferReset - forget buffer's data, retaining capacity. | 456 ** dataBufferReset - forget buffer's data, retaining capacity. |
| 435 ** dataBufferDestroy - free buffer's data. | 457 ** dataBufferDestroy - free buffer's data. |
| 436 ** dataBufferSwap - swap contents of two buffers. | 458 ** dataBufferSwap - swap contents of two buffers. |
| 437 ** dataBufferExpand - expand capacity without adding data. | 459 ** dataBufferExpand - expand capacity without adding data. |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 DocListType iType; | 608 DocListType iType; |
| 587 const char *pData; | 609 const char *pData; |
| 588 int nData; | 610 int nData; |
| 589 | 611 |
| 590 sqlite_int64 iDocid; | 612 sqlite_int64 iDocid; |
| 591 int nElement; | 613 int nElement; |
| 592 } DLReader; | 614 } DLReader; |
| 593 | 615 |
| 594 static int dlrAtEnd(DLReader *pReader){ | 616 static int dlrAtEnd(DLReader *pReader){ |
| 595 assert( pReader->nData>=0 ); | 617 assert( pReader->nData>=0 ); |
| 596 return pReader->nData==0; | 618 return pReader->nData<=0; |
| 597 } | 619 } |
| 598 static sqlite_int64 dlrDocid(DLReader *pReader){ | 620 static sqlite_int64 dlrDocid(DLReader *pReader){ |
| 599 assert( !dlrAtEnd(pReader) ); | 621 assert( !dlrAtEnd(pReader) ); |
| 600 return pReader->iDocid; | 622 return pReader->iDocid; |
| 601 } | 623 } |
| 602 static const char *dlrDocData(DLReader *pReader){ | 624 static const char *dlrDocData(DLReader *pReader){ |
| 603 assert( !dlrAtEnd(pReader) ); | 625 assert( !dlrAtEnd(pReader) ); |
| 604 return pReader->pData; | 626 return pReader->pData; |
| 605 } | 627 } |
| 606 static int dlrDocDataBytes(DLReader *pReader){ | 628 static int dlrDocDataBytes(DLReader *pReader){ |
| 607 assert( !dlrAtEnd(pReader) ); | 629 assert( !dlrAtEnd(pReader) ); |
| 608 return pReader->nElement; | 630 return pReader->nElement; |
| 609 } | 631 } |
| 610 static int dlrAllDataBytes(DLReader *pReader){ | 632 static int dlrAllDataBytes(DLReader *pReader){ |
| 611 assert( !dlrAtEnd(pReader) ); | 633 assert( !dlrAtEnd(pReader) ); |
| 612 return pReader->nData; | 634 return pReader->nData; |
| 613 } | 635 } |
| 614 /* TODO(shess) Consider adding a field to track iDocid varint length | 636 /* TODO(shess) Consider adding a field to track iDocid varint length |
| 615 ** to make these two functions faster. This might matter (a tiny bit) | 637 ** to make these two functions faster. This might matter (a tiny bit) |
| 616 ** for queries. | 638 ** for queries. |
| 617 */ | 639 */ |
| 618 static const char *dlrPosData(DLReader *pReader){ | 640 static const char *dlrPosData(DLReader *pReader){ |
| 619 sqlite_int64 iDummy; | 641 sqlite_int64 iDummy; |
| 620 int n = fts3GetVarint(pReader->pData, &iDummy); | 642 int n = fts3GetVarintSafe(pReader->pData, &iDummy, pReader->nElement); |
| 643 if( !n ) return NULL; |
| 621 assert( !dlrAtEnd(pReader) ); | 644 assert( !dlrAtEnd(pReader) ); |
| 622 return pReader->pData+n; | 645 return pReader->pData+n; |
| 623 } | 646 } |
| 624 static int dlrPosDataLen(DLReader *pReader){ | 647 static int dlrPosDataLen(DLReader *pReader){ |
| 625 sqlite_int64 iDummy; | 648 sqlite_int64 iDummy; |
| 626 int n = fts3GetVarint(pReader->pData, &iDummy); | 649 int n = fts3GetVarint(pReader->pData, &iDummy); |
| 627 assert( !dlrAtEnd(pReader) ); | 650 assert( !dlrAtEnd(pReader) ); |
| 628 return pReader->nElement-n; | 651 return pReader->nElement-n; |
| 629 } | 652 } |
| 630 static void dlrStep(DLReader *pReader){ | 653 static int dlrStep(DLReader *pReader){ |
| 631 assert( !dlrAtEnd(pReader) ); | 654 assert( !dlrAtEnd(pReader) ); |
| 632 | 655 |
| 633 /* Skip past current doclist element. */ | 656 /* Skip past current doclist element. */ |
| 634 assert( pReader->nElement<=pReader->nData ); | 657 assert( pReader->nElement<=pReader->nData ); |
| 635 pReader->pData += pReader->nElement; | 658 pReader->pData += pReader->nElement; |
| 636 pReader->nData -= pReader->nElement; | 659 pReader->nData -= pReader->nElement; |
| 637 | 660 |
| 638 /* If there is more data, read the next doclist element. */ | 661 /* If there is more data, read the next doclist element. */ |
| 639 if( pReader->nData!=0 ){ | 662 if( pReader->nData>0 ){ |
| 640 sqlite_int64 iDocidDelta; | 663 sqlite_int64 iDocidDelta; |
| 641 int iDummy, n = fts3GetVarint(pReader->pData, &iDocidDelta); | 664 int nTotal = 0; |
| 665 int iDummy, n = fts3GetVarintSafe(pReader->pData, &iDocidDelta, pReader->nDa
ta); |
| 666 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 667 nTotal += n; |
| 642 pReader->iDocid += iDocidDelta; | 668 pReader->iDocid += iDocidDelta; |
| 643 if( pReader->iType>=DL_POSITIONS ){ | 669 if( pReader->iType>=DL_POSITIONS ){ |
| 644 assert( n<pReader->nData ); | |
| 645 while( 1 ){ | 670 while( 1 ){ |
| 646 n += fts3GetVarint32(pReader->pData+n, &iDummy); | 671 n = fts3GetVarint32Safe(pReader->pData+nTotal, &iDummy, pReader->nData-n
Total); |
| 647 assert( n<=pReader->nData ); | 672 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 673 nTotal += n; |
| 648 if( iDummy==POS_END ) break; | 674 if( iDummy==POS_END ) break; |
| 649 if( iDummy==POS_COLUMN ){ | 675 if( iDummy==POS_COLUMN ){ |
| 650 n += fts3GetVarint32(pReader->pData+n, &iDummy); | 676 n = fts3GetVarint32Safe(pReader->pData+nTotal, &iDummy, pReader->nData
-nTotal); |
| 651 assert( n<pReader->nData ); | 677 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 678 nTotal += n; |
| 652 }else if( pReader->iType==DL_POSITIONS_OFFSETS ){ | 679 }else if( pReader->iType==DL_POSITIONS_OFFSETS ){ |
| 653 n += fts3GetVarint32(pReader->pData+n, &iDummy); | 680 n = fts3GetVarint32Safe(pReader->pData+nTotal, &iDummy, pReader->nData
-nTotal); |
| 654 n += fts3GetVarint32(pReader->pData+n, &iDummy); | 681 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 655 assert( n<pReader->nData ); | 682 nTotal += n; |
| 683 n = fts3GetVarint32Safe(pReader->pData+nTotal, &iDummy, pReader->nData
-nTotal); |
| 684 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 685 nTotal += n; |
| 656 } | 686 } |
| 657 } | 687 } |
| 658 } | 688 } |
| 659 pReader->nElement = n; | 689 pReader->nElement = nTotal; |
| 660 assert( pReader->nElement<=pReader->nData ); | 690 assert( pReader->nElement<=pReader->nData ); |
| 661 } | 691 } |
| 692 return SQLITE_OK; |
| 662 } | 693 } |
| 663 static void dlrInit(DLReader *pReader, DocListType iType, | 694 static void dlrDestroy(DLReader *pReader){ |
| 664 const char *pData, int nData){ | 695 SCRAMBLE(pReader); |
| 696 } |
| 697 static int dlrInit(DLReader *pReader, DocListType iType, |
| 698 const char *pData, int nData){ |
| 699 int rc; |
| 665 assert( pData!=NULL && nData!=0 ); | 700 assert( pData!=NULL && nData!=0 ); |
| 666 pReader->iType = iType; | 701 pReader->iType = iType; |
| 667 pReader->pData = pData; | 702 pReader->pData = pData; |
| 668 pReader->nData = nData; | 703 pReader->nData = nData; |
| 669 pReader->nElement = 0; | 704 pReader->nElement = 0; |
| 670 pReader->iDocid = 0; | 705 pReader->iDocid = 0; |
| 671 | 706 |
| 672 /* Load the first element's data. There must be a first element. */ | 707 /* Load the first element's data. There must be a first element. */ |
| 673 dlrStep(pReader); | 708 rc = dlrStep(pReader); |
| 674 } | 709 if( rc!=SQLITE_OK ) dlrDestroy(pReader); |
| 675 static void dlrDestroy(DLReader *pReader){ | 710 return rc; |
| 676 SCRAMBLE(pReader); | |
| 677 } | 711 } |
| 678 | 712 |
| 679 #ifndef NDEBUG | 713 #ifndef NDEBUG |
| 680 /* Verify that the doclist can be validly decoded. Also returns the | 714 /* Verify that the doclist can be validly decoded. Also returns the |
| 681 ** last docid found because it is convenient in other assertions for | 715 ** last docid found because it is convenient in other assertions for |
| 682 ** DLWriter. | 716 ** DLWriter. |
| 683 */ | 717 */ |
| 684 static void docListValidate(DocListType iType, const char *pData, int nData, | 718 static void docListValidate(DocListType iType, const char *pData, int nData, |
| 685 sqlite_int64 *pLastDocid){ | 719 sqlite_int64 *pLastDocid){ |
| 686 sqlite_int64 iPrevDocid = 0; | 720 sqlite_int64 iPrevDocid = 0; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 ** | 787 ** |
| 754 ** iLastDocid is the final docid in the doclist in pData. It is | 788 ** iLastDocid is the final docid in the doclist in pData. It is |
| 755 ** needed to create the new iPrevDocid for future delta-encoding. The | 789 ** needed to create the new iPrevDocid for future delta-encoding. The |
| 756 ** code could decode the passed doclist to recreate iLastDocid, but | 790 ** code could decode the passed doclist to recreate iLastDocid, but |
| 757 ** the only current user (docListMerge) already has decoded this | 791 ** the only current user (docListMerge) already has decoded this |
| 758 ** information. | 792 ** information. |
| 759 */ | 793 */ |
| 760 /* TODO(shess) This has become just a helper for docListMerge. | 794 /* TODO(shess) This has become just a helper for docListMerge. |
| 761 ** Consider a refactor to make this cleaner. | 795 ** Consider a refactor to make this cleaner. |
| 762 */ | 796 */ |
| 763 static void dlwAppend(DLWriter *pWriter, | 797 static int dlwAppend(DLWriter *pWriter, |
| 764 const char *pData, int nData, | 798 const char *pData, int nData, |
| 765 sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){ | 799 sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){ |
| 766 sqlite_int64 iDocid = 0; | 800 sqlite_int64 iDocid = 0; |
| 767 char c[VARINT_MAX]; | 801 char c[VARINT_MAX]; |
| 768 int nFirstOld, nFirstNew; /* Old and new varint len of first docid. */ | 802 int nFirstOld, nFirstNew; /* Old and new varint len of first docid. */ |
| 769 #ifndef NDEBUG | 803 #ifndef NDEBUG |
| 770 sqlite_int64 iLastDocidDelta; | 804 sqlite_int64 iLastDocidDelta; |
| 771 #endif | 805 #endif |
| 772 | 806 |
| 773 /* Recode the initial docid as delta from iPrevDocid. */ | 807 /* Recode the initial docid as delta from iPrevDocid. */ |
| 774 nFirstOld = fts3GetVarint(pData, &iDocid); | 808 nFirstOld = fts3GetVarintSafe(pData, &iDocid, nData); |
| 809 if( !nFirstOld ) return SQLITE_CORRUPT_BKPT; |
| 775 assert( nFirstOld<nData || (nFirstOld==nData && pWriter->iType==DL_DOCIDS) ); | 810 assert( nFirstOld<nData || (nFirstOld==nData && pWriter->iType==DL_DOCIDS) ); |
| 776 nFirstNew = fts3PutVarint(c, iFirstDocid-pWriter->iPrevDocid); | 811 nFirstNew = fts3PutVarint(c, iFirstDocid-pWriter->iPrevDocid); |
| 777 | 812 |
| 778 /* Verify that the incoming doclist is valid AND that it ends with | 813 /* Verify that the incoming doclist is valid AND that it ends with |
| 779 ** the expected docid. This is essential because we'll trust this | 814 ** the expected docid. This is essential because we'll trust this |
| 780 ** docid in future delta-encoding. | 815 ** docid in future delta-encoding. |
| 781 */ | 816 */ |
| 782 ASSERT_VALID_DOCLIST(pWriter->iType, pData, nData, &iLastDocidDelta); | 817 ASSERT_VALID_DOCLIST(pWriter->iType, pData, nData, &iLastDocidDelta); |
| 783 assert( iLastDocid==iFirstDocid-iDocid+iLastDocidDelta ); | 818 assert( iLastDocid==iFirstDocid-iDocid+iLastDocidDelta ); |
| 784 | 819 |
| 785 /* Append recoded initial docid and everything else. Rest of docids | 820 /* Append recoded initial docid and everything else. Rest of docids |
| 786 ** should have been delta-encoded from previous initial docid. | 821 ** should have been delta-encoded from previous initial docid. |
| 787 */ | 822 */ |
| 788 if( nFirstOld<nData ){ | 823 if( nFirstOld<nData ){ |
| 789 dataBufferAppend2(pWriter->b, c, nFirstNew, | 824 dataBufferAppend2(pWriter->b, c, nFirstNew, |
| 790 pData+nFirstOld, nData-nFirstOld); | 825 pData+nFirstOld, nData-nFirstOld); |
| 791 }else{ | 826 }else{ |
| 792 dataBufferAppend(pWriter->b, c, nFirstNew); | 827 dataBufferAppend(pWriter->b, c, nFirstNew); |
| 793 } | 828 } |
| 794 pWriter->iPrevDocid = iLastDocid; | 829 pWriter->iPrevDocid = iLastDocid; |
| 830 return SQLITE_OK; |
| 795 } | 831 } |
| 796 static void dlwCopy(DLWriter *pWriter, DLReader *pReader){ | 832 static int dlwCopy(DLWriter *pWriter, DLReader *pReader){ |
| 797 dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader), | 833 return dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader), |
| 798 dlrDocid(pReader), dlrDocid(pReader)); | 834 dlrDocid(pReader), dlrDocid(pReader)); |
| 799 } | 835 } |
| 800 static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){ | 836 static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){ |
| 801 char c[VARINT_MAX]; | 837 char c[VARINT_MAX]; |
| 802 int n = fts3PutVarint(c, iDocid-pWriter->iPrevDocid); | 838 int n = fts3PutVarint(c, iDocid-pWriter->iPrevDocid); |
| 803 | 839 |
| 804 /* Docids must ascend. */ | 840 /* Docids must ascend. */ |
| 805 assert( !pWriter->has_iPrevDocid || iDocid>pWriter->iPrevDocid ); | 841 assert( !pWriter->has_iPrevDocid || iDocid>pWriter->iPrevDocid ); |
| 806 assert( pWriter->iType==DL_DOCIDS ); | 842 assert( pWriter->iType==DL_DOCIDS ); |
| 807 | 843 |
| 808 dataBufferAppend(pWriter->b, c, n); | 844 dataBufferAppend(pWriter->b, c, n); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 return pReader->iPosition; | 885 return pReader->iPosition; |
| 850 } | 886 } |
| 851 static int plrStartOffset(PLReader *pReader){ | 887 static int plrStartOffset(PLReader *pReader){ |
| 852 assert( !plrAtEnd(pReader) ); | 888 assert( !plrAtEnd(pReader) ); |
| 853 return pReader->iStartOffset; | 889 return pReader->iStartOffset; |
| 854 } | 890 } |
| 855 static int plrEndOffset(PLReader *pReader){ | 891 static int plrEndOffset(PLReader *pReader){ |
| 856 assert( !plrAtEnd(pReader) ); | 892 assert( !plrAtEnd(pReader) ); |
| 857 return pReader->iEndOffset; | 893 return pReader->iEndOffset; |
| 858 } | 894 } |
| 859 static void plrStep(PLReader *pReader){ | 895 static int plrStep(PLReader *pReader){ |
| 860 int i, n; | 896 int i, n, nTotal = 0; |
| 861 | 897 |
| 862 assert( !plrAtEnd(pReader) ); | 898 assert( !plrAtEnd(pReader) ); |
| 863 | 899 |
| 864 if( pReader->nData==0 ){ | 900 if( pReader->nData<=0 ){ |
| 865 pReader->pData = NULL; | 901 pReader->pData = NULL; |
| 866 return; | 902 return SQLITE_OK; |
| 867 } | 903 } |
| 868 | 904 |
| 869 n = fts3GetVarint32(pReader->pData, &i); | 905 n = fts3GetVarint32Safe(pReader->pData, &i, pReader->nData); |
| 906 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 907 nTotal += n; |
| 870 if( i==POS_COLUMN ){ | 908 if( i==POS_COLUMN ){ |
| 871 n += fts3GetVarint32(pReader->pData+n, &pReader->iColumn); | 909 n = fts3GetVarint32Safe(pReader->pData+nTotal, &pReader->iColumn, pReader->n
Data-nTotal); |
| 910 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 911 nTotal += n; |
| 872 pReader->iPosition = 0; | 912 pReader->iPosition = 0; |
| 873 pReader->iStartOffset = 0; | 913 pReader->iStartOffset = 0; |
| 874 n += fts3GetVarint32(pReader->pData+n, &i); | 914 n = fts3GetVarint32Safe(pReader->pData+nTotal, &i, pReader->nData-nTotal); |
| 915 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 916 nTotal += n; |
| 875 } | 917 } |
| 876 /* Should never see adjacent column changes. */ | 918 /* Should never see adjacent column changes. */ |
| 877 assert( i!=POS_COLUMN ); | 919 assert( i!=POS_COLUMN ); |
| 878 | 920 |
| 879 if( i==POS_END ){ | 921 if( i==POS_END ){ |
| 922 assert( nTotal<=pReader->nData ); |
| 880 pReader->nData = 0; | 923 pReader->nData = 0; |
| 881 pReader->pData = NULL; | 924 pReader->pData = NULL; |
| 882 return; | 925 return SQLITE_OK; |
| 883 } | 926 } |
| 884 | 927 |
| 885 pReader->iPosition += i-POS_BASE; | 928 pReader->iPosition += i-POS_BASE; |
| 886 if( pReader->iType==DL_POSITIONS_OFFSETS ){ | 929 if( pReader->iType==DL_POSITIONS_OFFSETS ){ |
| 887 n += fts3GetVarint32(pReader->pData+n, &i); | 930 n = fts3GetVarint32Safe(pReader->pData+nTotal, &i, pReader->nData-nTotal); |
| 931 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 932 nTotal += n; |
| 888 pReader->iStartOffset += i; | 933 pReader->iStartOffset += i; |
| 889 n += fts3GetVarint32(pReader->pData+n, &i); | 934 n = fts3GetVarint32Safe(pReader->pData+nTotal, &i, pReader->nData-nTotal); |
| 935 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 936 nTotal += n; |
| 890 pReader->iEndOffset = pReader->iStartOffset+i; | 937 pReader->iEndOffset = pReader->iStartOffset+i; |
| 891 } | 938 } |
| 892 assert( n<=pReader->nData ); | 939 assert( nTotal<=pReader->nData ); |
| 893 pReader->pData += n; | 940 pReader->pData += nTotal; |
| 894 pReader->nData -= n; | 941 pReader->nData -= nTotal; |
| 942 return SQLITE_OK; |
| 895 } | 943 } |
| 896 | 944 |
| 897 static void plrInit(PLReader *pReader, DLReader *pDLReader){ | 945 static void plrDestroy(PLReader *pReader){ |
| 946 SCRAMBLE(pReader); |
| 947 } |
| 948 static int plrInit(PLReader *pReader, DLReader *pDLReader){ |
| 949 int rc; |
| 898 pReader->pData = dlrPosData(pDLReader); | 950 pReader->pData = dlrPosData(pDLReader); |
| 899 pReader->nData = dlrPosDataLen(pDLReader); | 951 pReader->nData = dlrPosDataLen(pDLReader); |
| 900 pReader->iType = pDLReader->iType; | 952 pReader->iType = pDLReader->iType; |
| 901 pReader->iColumn = 0; | 953 pReader->iColumn = 0; |
| 902 pReader->iPosition = 0; | 954 pReader->iPosition = 0; |
| 903 pReader->iStartOffset = 0; | 955 pReader->iStartOffset = 0; |
| 904 pReader->iEndOffset = 0; | 956 pReader->iEndOffset = 0; |
| 905 plrStep(pReader); | 957 rc = plrStep(pReader); |
| 906 } | 958 if( rc!=SQLITE_OK ) plrDestroy(pReader); |
| 907 static void plrDestroy(PLReader *pReader){ | 959 return rc; |
| 908 SCRAMBLE(pReader); | |
| 909 } | 960 } |
| 910 | 961 |
| 911 /*******************************************************************/ | 962 /*******************************************************************/ |
| 912 /* PLWriter is used in constructing a document's position list. As a | 963 /* PLWriter is used in constructing a document's position list. As a |
| 913 ** convenience, if iType is DL_DOCIDS, PLWriter becomes a no-op. | 964 ** convenience, if iType is DL_DOCIDS, PLWriter becomes a no-op. |
| 914 ** PLWriter writes to the associated DLWriter's buffer. | 965 ** PLWriter writes to the associated DLWriter's buffer. |
| 915 ** | 966 ** |
| 916 ** plwInit - init for writing a document's poslist. | 967 ** plwInit - init for writing a document's poslist. |
| 917 ** plwDestroy - clear a writer. | 968 ** plwDestroy - clear a writer. |
| 918 ** plwAdd - append position and offset information. | 969 ** plwAdd - append position and offset information. |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1084 /* Copy the doclist data of iType in pData/nData into *out, trimming | 1135 /* Copy the doclist data of iType in pData/nData into *out, trimming |
| 1085 ** unnecessary data as we go. Only columns matching iColumn are | 1136 ** unnecessary data as we go. Only columns matching iColumn are |
| 1086 ** copied, all columns copied if iColumn is -1. Elements with no | 1137 ** copied, all columns copied if iColumn is -1. Elements with no |
| 1087 ** matching columns are dropped. The output is an iOutType doclist. | 1138 ** matching columns are dropped. The output is an iOutType doclist. |
| 1088 */ | 1139 */ |
| 1089 /* NOTE(shess) This code is only valid after all doclists are merged. | 1140 /* NOTE(shess) This code is only valid after all doclists are merged. |
| 1090 ** If this is run before merges, then doclist items which represent | 1141 ** If this is run before merges, then doclist items which represent |
| 1091 ** deletion will be trimmed, and will thus not effect a deletion | 1142 ** deletion will be trimmed, and will thus not effect a deletion |
| 1092 ** during the merge. | 1143 ** during the merge. |
| 1093 */ | 1144 */ |
| 1094 static void docListTrim(DocListType iType, const char *pData, int nData, | 1145 static int docListTrim(DocListType iType, const char *pData, int nData, |
| 1095 int iColumn, DocListType iOutType, DataBuffer *out){ | 1146 int iColumn, DocListType iOutType, DataBuffer *out){ |
| 1096 DLReader dlReader; | 1147 DLReader dlReader; |
| 1097 DLWriter dlWriter; | 1148 DLWriter dlWriter; |
| 1149 int rc; |
| 1098 | 1150 |
| 1099 assert( iOutType<=iType ); | 1151 assert( iOutType<=iType ); |
| 1100 | 1152 |
| 1101 dlrInit(&dlReader, iType, pData, nData); | 1153 rc = dlrInit(&dlReader, iType, pData, nData); |
| 1154 if( rc!=SQLITE_OK ) return rc; |
| 1102 dlwInit(&dlWriter, iOutType, out); | 1155 dlwInit(&dlWriter, iOutType, out); |
| 1103 | 1156 |
| 1104 while( !dlrAtEnd(&dlReader) ){ | 1157 while( !dlrAtEnd(&dlReader) ){ |
| 1105 PLReader plReader; | 1158 PLReader plReader; |
| 1106 PLWriter plWriter; | 1159 PLWriter plWriter; |
| 1107 int match = 0; | 1160 int match = 0; |
| 1108 | 1161 |
| 1109 plrInit(&plReader, &dlReader); | 1162 rc = plrInit(&plReader, &dlReader); |
| 1163 if( rc!=SQLITE_OK ) break; |
| 1110 | 1164 |
| 1111 while( !plrAtEnd(&plReader) ){ | 1165 while( !plrAtEnd(&plReader) ){ |
| 1112 if( iColumn==-1 || plrColumn(&plReader)==iColumn ){ | 1166 if( iColumn==-1 || plrColumn(&plReader)==iColumn ){ |
| 1113 if( !match ){ | 1167 if( !match ){ |
| 1114 plwInit(&plWriter, &dlWriter, dlrDocid(&dlReader)); | 1168 plwInit(&plWriter, &dlWriter, dlrDocid(&dlReader)); |
| 1115 match = 1; | 1169 match = 1; |
| 1116 } | 1170 } |
| 1117 plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader), | 1171 plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader), |
| 1118 plrStartOffset(&plReader), plrEndOffset(&plReader)); | 1172 plrStartOffset(&plReader), plrEndOffset(&plReader)); |
| 1119 } | 1173 } |
| 1120 plrStep(&plReader); | 1174 rc = plrStep(&plReader); |
| 1175 if( rc!=SQLITE_OK ){ |
| 1176 plrDestroy(&plReader); |
| 1177 goto err; |
| 1178 } |
| 1121 } | 1179 } |
| 1122 if( match ){ | 1180 if( match ){ |
| 1123 plwTerminate(&plWriter); | 1181 plwTerminate(&plWriter); |
| 1124 plwDestroy(&plWriter); | 1182 plwDestroy(&plWriter); |
| 1125 } | 1183 } |
| 1126 | 1184 |
| 1127 plrDestroy(&plReader); | 1185 plrDestroy(&plReader); |
| 1128 dlrStep(&dlReader); | 1186 rc = dlrStep(&dlReader); |
| 1187 if( rc!=SQLITE_OK ) break; |
| 1129 } | 1188 } |
| 1189 err: |
| 1130 dlwDestroy(&dlWriter); | 1190 dlwDestroy(&dlWriter); |
| 1131 dlrDestroy(&dlReader); | 1191 dlrDestroy(&dlReader); |
| 1192 return rc; |
| 1132 } | 1193 } |
| 1133 | 1194 |
| 1134 /* Used by docListMerge() to keep doclists in the ascending order by | 1195 /* Used by docListMerge() to keep doclists in the ascending order by |
| 1135 ** docid, then ascending order by age (so the newest comes first). | 1196 ** docid, then ascending order by age (so the newest comes first). |
| 1136 */ | 1197 */ |
| 1137 typedef struct OrderedDLReader { | 1198 typedef struct OrderedDLReader { |
| 1138 DLReader *pReader; | 1199 DLReader *pReader; |
| 1139 | 1200 |
| 1140 /* TODO(shess) If we assume that docListMerge pReaders is ordered by | 1201 /* TODO(shess) If we assume that docListMerge pReaders is ordered by |
| 1141 ** age (which we do), then we could use pReader comparisons to break | 1202 ** age (which we do), then we could use pReader comparisons to break |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1178 } | 1239 } |
| 1179 | 1240 |
| 1180 /* Given an array of doclist readers, merge their doclist elements | 1241 /* Given an array of doclist readers, merge their doclist elements |
| 1181 ** into out in sorted order (by docid), dropping elements from older | 1242 ** into out in sorted order (by docid), dropping elements from older |
| 1182 ** readers when there is a duplicate docid. pReaders is assumed to be | 1243 ** readers when there is a duplicate docid. pReaders is assumed to be |
| 1183 ** ordered by age, oldest first. | 1244 ** ordered by age, oldest first. |
| 1184 */ | 1245 */ |
| 1185 /* TODO(shess) nReaders must be <= MERGE_COUNT. This should probably | 1246 /* TODO(shess) nReaders must be <= MERGE_COUNT. This should probably |
| 1186 ** be fixed. | 1247 ** be fixed. |
| 1187 */ | 1248 */ |
| 1188 static void docListMerge(DataBuffer *out, | 1249 static int docListMerge(DataBuffer *out, |
| 1189 DLReader *pReaders, int nReaders){ | 1250 DLReader *pReaders, int nReaders){ |
| 1190 OrderedDLReader readers[MERGE_COUNT]; | 1251 OrderedDLReader readers[MERGE_COUNT]; |
| 1191 DLWriter writer; | 1252 DLWriter writer; |
| 1192 int i, n; | 1253 int i, n; |
| 1193 const char *pStart = 0; | 1254 const char *pStart = 0; |
| 1194 int nStart = 0; | 1255 int nStart = 0; |
| 1195 sqlite_int64 iFirstDocid = 0, iLastDocid = 0; | 1256 sqlite_int64 iFirstDocid = 0, iLastDocid = 0; |
| 1257 int rc = SQLITE_OK; |
| 1196 | 1258 |
| 1197 assert( nReaders>0 ); | 1259 assert( nReaders>0 ); |
| 1198 if( nReaders==1 ){ | 1260 if( nReaders==1 ){ |
| 1199 dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders)); | 1261 dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders)); |
| 1200 return; | 1262 return SQLITE_OK; |
| 1201 } | 1263 } |
| 1202 | 1264 |
| 1203 assert( nReaders<=MERGE_COUNT ); | 1265 assert( nReaders<=MERGE_COUNT ); |
| 1204 n = 0; | 1266 n = 0; |
| 1205 for(i=0; i<nReaders; i++){ | 1267 for(i=0; i<nReaders; i++){ |
| 1206 assert( pReaders[i].iType==pReaders[0].iType ); | 1268 assert( pReaders[i].iType==pReaders[0].iType ); |
| 1207 readers[i].pReader = pReaders+i; | 1269 readers[i].pReader = pReaders+i; |
| 1208 readers[i].idx = i; | 1270 readers[i].idx = i; |
| 1209 n += dlrAllDataBytes(&pReaders[i]); | 1271 n += dlrAllDataBytes(&pReaders[i]); |
| 1210 } | 1272 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1223 sqlite_int64 iDocid = dlrDocid(readers[0].pReader); | 1285 sqlite_int64 iDocid = dlrDocid(readers[0].pReader); |
| 1224 | 1286 |
| 1225 /* If this is a continuation of the current buffer to copy, extend | 1287 /* If this is a continuation of the current buffer to copy, extend |
| 1226 ** that buffer. memcpy() seems to be more efficient if it has a | 1288 ** that buffer. memcpy() seems to be more efficient if it has a |
| 1227 ** lots of data to copy. | 1289 ** lots of data to copy. |
| 1228 */ | 1290 */ |
| 1229 if( dlrDocData(readers[0].pReader)==pStart+nStart ){ | 1291 if( dlrDocData(readers[0].pReader)==pStart+nStart ){ |
| 1230 nStart += dlrDocDataBytes(readers[0].pReader); | 1292 nStart += dlrDocDataBytes(readers[0].pReader); |
| 1231 }else{ | 1293 }else{ |
| 1232 if( pStart!=0 ){ | 1294 if( pStart!=0 ){ |
| 1233 dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); | 1295 rc = dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); |
| 1296 if( rc!=SQLITE_OK ) goto err; |
| 1234 } | 1297 } |
| 1235 pStart = dlrDocData(readers[0].pReader); | 1298 pStart = dlrDocData(readers[0].pReader); |
| 1236 nStart = dlrDocDataBytes(readers[0].pReader); | 1299 nStart = dlrDocDataBytes(readers[0].pReader); |
| 1237 iFirstDocid = iDocid; | 1300 iFirstDocid = iDocid; |
| 1238 } | 1301 } |
| 1239 iLastDocid = iDocid; | 1302 iLastDocid = iDocid; |
| 1240 dlrStep(readers[0].pReader); | 1303 rc = dlrStep(readers[0].pReader); |
| 1304 if( rc!= SQLITE_OK ) goto err; |
| 1241 | 1305 |
| 1242 /* Drop all of the older elements with the same docid. */ | 1306 /* Drop all of the older elements with the same docid. */ |
| 1243 for(i=1; i<nReaders && | 1307 for(i=1; i<nReaders && |
| 1244 !dlrAtEnd(readers[i].pReader) && | 1308 !dlrAtEnd(readers[i].pReader) && |
| 1245 dlrDocid(readers[i].pReader)==iDocid; i++){ | 1309 dlrDocid(readers[i].pReader)==iDocid; i++){ |
| 1246 dlrStep(readers[i].pReader); | 1310 rc = dlrStep(readers[i].pReader); |
| 1311 if( rc!=SQLITE_OK ) goto err; |
| 1247 } | 1312 } |
| 1248 | 1313 |
| 1249 /* Get the readers back into order. */ | 1314 /* Get the readers back into order. */ |
| 1250 while( i-->0 ){ | 1315 while( i-->0 ){ |
| 1251 orderedDLReaderReorder(readers+i, nReaders-i); | 1316 orderedDLReaderReorder(readers+i, nReaders-i); |
| 1252 } | 1317 } |
| 1253 } | 1318 } |
| 1254 | 1319 |
| 1255 /* Copy over any remaining elements. */ | 1320 /* Copy over any remaining elements. */ |
| 1256 if( nStart>0 ) dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); | 1321 if( nStart>0 ) rc = dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid
); |
| 1322 err: |
| 1257 dlwDestroy(&writer); | 1323 dlwDestroy(&writer); |
| 1324 return rc; |
| 1258 } | 1325 } |
| 1259 | 1326 |
| 1260 /* Helper function for posListUnion(). Compares the current position | 1327 /* Helper function for posListUnion(). Compares the current position |
| 1261 ** between left and right, returning as standard C idiom of <0 if | 1328 ** between left and right, returning as standard C idiom of <0 if |
| 1262 ** left<right, >0 if left>right, and 0 if left==right. "End" always | 1329 ** left<right, >0 if left>right, and 0 if left==right. "End" always |
| 1263 ** compares greater. | 1330 ** compares greater. |
| 1264 */ | 1331 */ |
| 1265 static int posListCmp(PLReader *pLeft, PLReader *pRight){ | 1332 static int posListCmp(PLReader *pLeft, PLReader *pRight){ |
| 1266 assert( pLeft->iType==pRight->iType ); | 1333 assert( pLeft->iType==pRight->iType ); |
| 1267 if( pLeft->iType==DL_DOCIDS ) return 0; | 1334 if( pLeft->iType==DL_DOCIDS ) return 0; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1283 if( plrEndOffset(pLeft)>plrEndOffset(pRight) ) return 1; | 1350 if( plrEndOffset(pLeft)>plrEndOffset(pRight) ) return 1; |
| 1284 | 1351 |
| 1285 return 0; | 1352 return 0; |
| 1286 } | 1353 } |
| 1287 | 1354 |
| 1288 /* Write the union of position lists in pLeft and pRight to pOut. | 1355 /* Write the union of position lists in pLeft and pRight to pOut. |
| 1289 ** "Union" in this case meaning "All unique position tuples". Should | 1356 ** "Union" in this case meaning "All unique position tuples". Should |
| 1290 ** work with any doclist type, though both inputs and the output | 1357 ** work with any doclist type, though both inputs and the output |
| 1291 ** should be the same type. | 1358 ** should be the same type. |
| 1292 */ | 1359 */ |
| 1293 static void posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){ | 1360 static int posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){ |
| 1294 PLReader left, right; | 1361 PLReader left, right; |
| 1295 PLWriter writer; | 1362 PLWriter writer; |
| 1363 int rc; |
| 1296 | 1364 |
| 1297 assert( dlrDocid(pLeft)==dlrDocid(pRight) ); | 1365 assert( dlrDocid(pLeft)==dlrDocid(pRight) ); |
| 1298 assert( pLeft->iType==pRight->iType ); | 1366 assert( pLeft->iType==pRight->iType ); |
| 1299 assert( pLeft->iType==pOut->iType ); | 1367 assert( pLeft->iType==pOut->iType ); |
| 1300 | 1368 |
| 1301 plrInit(&left, pLeft); | 1369 rc = plrInit(&left, pLeft); |
| 1302 plrInit(&right, pRight); | 1370 if( rc!=SQLITE_OK ) return rc; |
| 1371 rc = plrInit(&right, pRight); |
| 1372 if( rc!=SQLITE_OK ){ |
| 1373 plrDestroy(&left); |
| 1374 return rc; |
| 1375 } |
| 1303 plwInit(&writer, pOut, dlrDocid(pLeft)); | 1376 plwInit(&writer, pOut, dlrDocid(pLeft)); |
| 1304 | 1377 |
| 1305 while( !plrAtEnd(&left) || !plrAtEnd(&right) ){ | 1378 while( !plrAtEnd(&left) || !plrAtEnd(&right) ){ |
| 1306 int c = posListCmp(&left, &right); | 1379 int c = posListCmp(&left, &right); |
| 1307 if( c<0 ){ | 1380 if( c<0 ){ |
| 1308 plwCopy(&writer, &left); | 1381 plwCopy(&writer, &left); |
| 1309 plrStep(&left); | 1382 rc = plrStep(&left); |
| 1383 if( rc!=SQLITE_OK ) break; |
| 1310 }else if( c>0 ){ | 1384 }else if( c>0 ){ |
| 1311 plwCopy(&writer, &right); | 1385 plwCopy(&writer, &right); |
| 1312 plrStep(&right); | 1386 rc = plrStep(&right); |
| 1387 if( rc!=SQLITE_OK ) break; |
| 1313 }else{ | 1388 }else{ |
| 1314 plwCopy(&writer, &left); | 1389 plwCopy(&writer, &left); |
| 1315 plrStep(&left); | 1390 rc = plrStep(&left); |
| 1316 plrStep(&right); | 1391 if( rc!=SQLITE_OK ) break; |
| 1392 rc = plrStep(&right); |
| 1393 if( rc!=SQLITE_OK ) break; |
| 1317 } | 1394 } |
| 1318 } | 1395 } |
| 1319 | 1396 |
| 1320 plwTerminate(&writer); | 1397 plwTerminate(&writer); |
| 1321 plwDestroy(&writer); | 1398 plwDestroy(&writer); |
| 1322 plrDestroy(&left); | 1399 plrDestroy(&left); |
| 1323 plrDestroy(&right); | 1400 plrDestroy(&right); |
| 1401 return rc; |
| 1324 } | 1402 } |
| 1325 | 1403 |
| 1326 /* Write the union of doclists in pLeft and pRight to pOut. For | 1404 /* Write the union of doclists in pLeft and pRight to pOut. For |
| 1327 ** docids in common between the inputs, the union of the position | 1405 ** docids in common between the inputs, the union of the position |
| 1328 ** lists is written. Inputs and outputs are always type DL_DEFAULT. | 1406 ** lists is written. Inputs and outputs are always type DL_DEFAULT. |
| 1329 */ | 1407 */ |
| 1330 static void docListUnion( | 1408 static int docListUnion( |
| 1331 const char *pLeft, int nLeft, | 1409 const char *pLeft, int nLeft, |
| 1332 const char *pRight, int nRight, | 1410 const char *pRight, int nRight, |
| 1333 DataBuffer *pOut /* Write the combined doclist here */ | 1411 DataBuffer *pOut /* Write the combined doclist here */ |
| 1334 ){ | 1412 ){ |
| 1335 DLReader left, right; | 1413 DLReader left, right; |
| 1336 DLWriter writer; | 1414 DLWriter writer; |
| 1415 int rc; |
| 1337 | 1416 |
| 1338 if( nLeft==0 ){ | 1417 if( nLeft==0 ){ |
| 1339 if( nRight!=0) dataBufferAppend(pOut, pRight, nRight); | 1418 if( nRight!=0) dataBufferAppend(pOut, pRight, nRight); |
| 1340 return; | 1419 return SQLITE_OK; |
| 1341 } | 1420 } |
| 1342 if( nRight==0 ){ | 1421 if( nRight==0 ){ |
| 1343 dataBufferAppend(pOut, pLeft, nLeft); | 1422 dataBufferAppend(pOut, pLeft, nLeft); |
| 1344 return; | 1423 return SQLITE_OK; |
| 1345 } | 1424 } |
| 1346 | 1425 |
| 1347 dlrInit(&left, DL_DEFAULT, pLeft, nLeft); | 1426 rc = dlrInit(&left, DL_DEFAULT, pLeft, nLeft); |
| 1348 dlrInit(&right, DL_DEFAULT, pRight, nRight); | 1427 if( rc!=SQLITE_OK ) return rc; |
| 1428 rc = dlrInit(&right, DL_DEFAULT, pRight, nRight); |
| 1429 if( rc!=SQLITE_OK){ |
| 1430 dlrDestroy(&left); |
| 1431 return rc; |
| 1432 } |
| 1349 dlwInit(&writer, DL_DEFAULT, pOut); | 1433 dlwInit(&writer, DL_DEFAULT, pOut); |
| 1350 | 1434 |
| 1351 while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ | 1435 while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ |
| 1352 if( dlrAtEnd(&right) ){ | 1436 if( dlrAtEnd(&right) ){ |
| 1353 dlwCopy(&writer, &left); | 1437 rc = dlwCopy(&writer, &left); |
| 1354 dlrStep(&left); | 1438 if( rc!=SQLITE_OK) break; |
| 1439 rc = dlrStep(&left); |
| 1440 if( rc!=SQLITE_OK) break; |
| 1355 }else if( dlrAtEnd(&left) ){ | 1441 }else if( dlrAtEnd(&left) ){ |
| 1356 dlwCopy(&writer, &right); | 1442 rc = dlwCopy(&writer, &right); |
| 1357 dlrStep(&right); | 1443 if( rc!=SQLITE_OK ) break; |
| 1444 rc = dlrStep(&right); |
| 1445 if( rc!=SQLITE_OK ) break; |
| 1358 }else if( dlrDocid(&left)<dlrDocid(&right) ){ | 1446 }else if( dlrDocid(&left)<dlrDocid(&right) ){ |
| 1359 dlwCopy(&writer, &left); | 1447 rc = dlwCopy(&writer, &left); |
| 1360 dlrStep(&left); | 1448 if( rc!=SQLITE_OK ) break; |
| 1449 rc = dlrStep(&left); |
| 1450 if( rc!=SQLITE_OK ) break; |
| 1361 }else if( dlrDocid(&left)>dlrDocid(&right) ){ | 1451 }else if( dlrDocid(&left)>dlrDocid(&right) ){ |
| 1362 dlwCopy(&writer, &right); | 1452 rc = dlwCopy(&writer, &right); |
| 1363 dlrStep(&right); | 1453 if( rc!=SQLITE_OK ) break; |
| 1454 rc = dlrStep(&right); |
| 1455 if( rc!=SQLITE_OK ) break; |
| 1364 }else{ | 1456 }else{ |
| 1365 posListUnion(&left, &right, &writer); | 1457 rc = posListUnion(&left, &right, &writer); |
| 1366 dlrStep(&left); | 1458 if( rc!=SQLITE_OK ) break; |
| 1367 dlrStep(&right); | 1459 rc = dlrStep(&left); |
| 1460 if( rc!=SQLITE_OK ) break; |
| 1461 rc = dlrStep(&right); |
| 1462 if( rc!=SQLITE_OK ) break; |
| 1368 } | 1463 } |
| 1369 } | 1464 } |
| 1370 | 1465 |
| 1371 dlrDestroy(&left); | 1466 dlrDestroy(&left); |
| 1372 dlrDestroy(&right); | 1467 dlrDestroy(&right); |
| 1373 dlwDestroy(&writer); | 1468 dlwDestroy(&writer); |
| 1469 return rc; |
| 1374 } | 1470 } |
| 1375 | 1471 |
| 1376 /* | 1472 /* |
| 1377 ** This function is used as part of the implementation of phrase and | 1473 ** This function is used as part of the implementation of phrase and |
| 1378 ** NEAR matching. | 1474 ** NEAR matching. |
| 1379 ** | 1475 ** |
| 1380 ** pLeft and pRight are DLReaders positioned to the same docid in | 1476 ** pLeft and pRight are DLReaders positioned to the same docid in |
| 1381 ** lists of type DL_POSITION. This function writes an entry to the | 1477 ** lists of type DL_POSITION. This function writes an entry to the |
| 1382 ** DLWriter pOut for each position in pRight that is less than | 1478 ** DLWriter pOut for each position in pRight that is less than |
| 1383 ** (nNear+1) greater (but not equal to or smaller) than a position | 1479 ** (nNear+1) greater (but not equal to or smaller) than a position |
| 1384 ** in pLeft. For example, if nNear is 0, and the positions contained | 1480 ** in pLeft. For example, if nNear is 0, and the positions contained |
| 1385 ** by pLeft and pRight are: | 1481 ** by pLeft and pRight are: |
| 1386 ** | 1482 ** |
| 1387 ** pLeft: 5 10 15 20 | 1483 ** pLeft: 5 10 15 20 |
| 1388 ** pRight: 6 9 17 21 | 1484 ** pRight: 6 9 17 21 |
| 1389 ** | 1485 ** |
| 1390 ** then the docid is added to pOut. If pOut is of type DL_POSITIONS, | 1486 ** then the docid is added to pOut. If pOut is of type DL_POSITIONS, |
| 1391 ** then a positionids "6" and "21" are also added to pOut. | 1487 ** then a positionids "6" and "21" are also added to pOut. |
| 1392 ** | 1488 ** |
| 1393 ** If boolean argument isSaveLeft is true, then positionids are copied | 1489 ** If boolean argument isSaveLeft is true, then positionids are copied |
| 1394 ** from pLeft instead of pRight. In the example above, the positions "5" | 1490 ** from pLeft instead of pRight. In the example above, the positions "5" |
| 1395 ** and "20" would be added instead of "6" and "21". | 1491 ** and "20" would be added instead of "6" and "21". |
| 1396 */ | 1492 */ |
| 1397 static void posListPhraseMerge( | 1493 static int posListPhraseMerge( |
| 1398 DLReader *pLeft, | 1494 DLReader *pLeft, |
| 1399 DLReader *pRight, | 1495 DLReader *pRight, |
| 1400 int nNear, | 1496 int nNear, |
| 1401 int isSaveLeft, | 1497 int isSaveLeft, |
| 1402 DLWriter *pOut | 1498 DLWriter *pOut |
| 1403 ){ | 1499 ){ |
| 1404 PLReader left, right; | 1500 PLReader left, right; |
| 1405 PLWriter writer; | 1501 PLWriter writer; |
| 1406 int match = 0; | 1502 int match = 0; |
| 1503 int rc; |
| 1407 | 1504 |
| 1408 assert( dlrDocid(pLeft)==dlrDocid(pRight) ); | 1505 assert( dlrDocid(pLeft)==dlrDocid(pRight) ); |
| 1409 assert( pOut->iType!=DL_POSITIONS_OFFSETS ); | 1506 assert( pOut->iType!=DL_POSITIONS_OFFSETS ); |
| 1410 | 1507 |
| 1411 plrInit(&left, pLeft); | 1508 rc = plrInit(&left, pLeft); |
| 1412 plrInit(&right, pRight); | 1509 if( rc!=SQLITE_OK ) return rc; |
| 1510 rc = plrInit(&right, pRight); |
| 1511 if( rc!=SQLITE_OK ){ |
| 1512 plrDestroy(&left); |
| 1513 return rc; |
| 1514 } |
| 1413 | 1515 |
| 1414 while( !plrAtEnd(&left) && !plrAtEnd(&right) ){ | 1516 while( !plrAtEnd(&left) && !plrAtEnd(&right) ){ |
| 1415 if( plrColumn(&left)<plrColumn(&right) ){ | 1517 if( plrColumn(&left)<plrColumn(&right) ){ |
| 1416 plrStep(&left); | 1518 rc = plrStep(&left); |
| 1519 if( rc!=SQLITE_OK ) break; |
| 1417 }else if( plrColumn(&left)>plrColumn(&right) ){ | 1520 }else if( plrColumn(&left)>plrColumn(&right) ){ |
| 1418 plrStep(&right); | 1521 rc = plrStep(&right); |
| 1522 if( rc!=SQLITE_OK ) break; |
| 1419 }else if( plrPosition(&left)>=plrPosition(&right) ){ | 1523 }else if( plrPosition(&left)>=plrPosition(&right) ){ |
| 1420 plrStep(&right); | 1524 rc = plrStep(&right); |
| 1525 if( rc!=SQLITE_OK ) break; |
| 1421 }else{ | 1526 }else{ |
| 1422 if( (plrPosition(&right)-plrPosition(&left))<=(nNear+1) ){ | 1527 if( (plrPosition(&right)-plrPosition(&left))<=(nNear+1) ){ |
| 1423 if( !match ){ | 1528 if( !match ){ |
| 1424 plwInit(&writer, pOut, dlrDocid(pLeft)); | 1529 plwInit(&writer, pOut, dlrDocid(pLeft)); |
| 1425 match = 1; | 1530 match = 1; |
| 1426 } | 1531 } |
| 1427 if( !isSaveLeft ){ | 1532 if( !isSaveLeft ){ |
| 1428 plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0); | 1533 plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0); |
| 1429 }else{ | 1534 }else{ |
| 1430 plwAdd(&writer, plrColumn(&left), plrPosition(&left), 0, 0); | 1535 plwAdd(&writer, plrColumn(&left), plrPosition(&left), 0, 0); |
| 1431 } | 1536 } |
| 1432 plrStep(&right); | 1537 rc = plrStep(&right); |
| 1538 if( rc!=SQLITE_OK ) break; |
| 1433 }else{ | 1539 }else{ |
| 1434 plrStep(&left); | 1540 rc = plrStep(&left); |
| 1541 if( rc!=SQLITE_OK ) break; |
| 1435 } | 1542 } |
| 1436 } | 1543 } |
| 1437 } | 1544 } |
| 1438 | 1545 |
| 1439 if( match ){ | 1546 if( match ){ |
| 1440 plwTerminate(&writer); | 1547 plwTerminate(&writer); |
| 1441 plwDestroy(&writer); | 1548 plwDestroy(&writer); |
| 1442 } | 1549 } |
| 1443 | 1550 |
| 1444 plrDestroy(&left); | 1551 plrDestroy(&left); |
| 1445 plrDestroy(&right); | 1552 plrDestroy(&right); |
| 1553 return rc; |
| 1446 } | 1554 } |
| 1447 | 1555 |
| 1448 /* | 1556 /* |
| 1449 ** Compare the values pointed to by the PLReaders passed as arguments. | 1557 ** Compare the values pointed to by the PLReaders passed as arguments. |
| 1450 ** Return -1 if the value pointed to by pLeft is considered less than | 1558 ** Return -1 if the value pointed to by pLeft is considered less than |
| 1451 ** the value pointed to by pRight, +1 if it is considered greater | 1559 ** the value pointed to by pRight, +1 if it is considered greater |
| 1452 ** than it, or 0 if it is equal. i.e. | 1560 ** than it, or 0 if it is equal. i.e. |
| 1453 ** | 1561 ** |
| 1454 ** (*pLeft - *pRight) | 1562 ** (*pLeft - *pRight) |
| 1455 ** | 1563 ** |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1489 ** combined. For example: | 1597 ** combined. For example: |
| 1490 ** | 1598 ** |
| 1491 ** Query syntax nPhrase | 1599 ** Query syntax nPhrase |
| 1492 ** ------------------------------------ | 1600 ** ------------------------------------ |
| 1493 ** "A B C" NEAR "D E" 5 | 1601 ** "A B C" NEAR "D E" 5 |
| 1494 ** A NEAR B 2 | 1602 ** A NEAR B 2 |
| 1495 ** | 1603 ** |
| 1496 ** iType controls the type of data written to pOut. If iType is | 1604 ** iType controls the type of data written to pOut. If iType is |
| 1497 ** DL_POSITIONS, the positions are those from pRight. | 1605 ** DL_POSITIONS, the positions are those from pRight. |
| 1498 */ | 1606 */ |
| 1499 static void docListPhraseMerge( | 1607 static int docListPhraseMerge( |
| 1500 const char *pLeft, int nLeft, | 1608 const char *pLeft, int nLeft, |
| 1501 const char *pRight, int nRight, | 1609 const char *pRight, int nRight, |
| 1502 int nNear, /* 0 for a phrase merge, non-zero for a NEAR merge */ | 1610 int nNear, /* 0 for a phrase merge, non-zero for a NEAR merge */ |
| 1503 int nPhrase, /* Number of tokens in left+right operands to NEAR */ | 1611 int nPhrase, /* Number of tokens in left+right operands to NEAR */ |
| 1504 DocListType iType, /* Type of doclist to write to pOut */ | 1612 DocListType iType, /* Type of doclist to write to pOut */ |
| 1505 DataBuffer *pOut /* Write the combined doclist here */ | 1613 DataBuffer *pOut /* Write the combined doclist here */ |
| 1506 ){ | 1614 ){ |
| 1507 DLReader left, right; | 1615 DLReader left, right; |
| 1508 DLWriter writer; | 1616 DLWriter writer; |
| 1617 int rc; |
| 1509 | 1618 |
| 1510 if( nLeft==0 || nRight==0 ) return; | 1619 /* These two buffers are used in the 'while', but are declared here |
| 1620 ** to simplify error-handling. |
| 1621 */ |
| 1622 DataBuffer one = {0, 0, 0}; |
| 1623 DataBuffer two = {0, 0, 0}; |
| 1624 |
| 1625 if( nLeft==0 || nRight==0 ) return SQLITE_OK; |
| 1511 | 1626 |
| 1512 assert( iType!=DL_POSITIONS_OFFSETS ); | 1627 assert( iType!=DL_POSITIONS_OFFSETS ); |
| 1513 | 1628 |
| 1514 dlrInit(&left, DL_POSITIONS, pLeft, nLeft); | 1629 rc = dlrInit(&left, DL_POSITIONS, pLeft, nLeft); |
| 1515 dlrInit(&right, DL_POSITIONS, pRight, nRight); | 1630 if( rc!=SQLITE_OK ) return rc; |
| 1631 rc = dlrInit(&right, DL_POSITIONS, pRight, nRight); |
| 1632 if( rc!=SQLITE_OK ){ |
| 1633 dlrDestroy(&left); |
| 1634 return rc; |
| 1635 } |
| 1516 dlwInit(&writer, iType, pOut); | 1636 dlwInit(&writer, iType, pOut); |
| 1517 | 1637 |
| 1518 while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){ | 1638 while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){ |
| 1519 if( dlrDocid(&left)<dlrDocid(&right) ){ | 1639 if( dlrDocid(&left)<dlrDocid(&right) ){ |
| 1520 dlrStep(&left); | 1640 rc = dlrStep(&left); |
| 1641 if( rc!=SQLITE_OK ) goto err; |
| 1521 }else if( dlrDocid(&right)<dlrDocid(&left) ){ | 1642 }else if( dlrDocid(&right)<dlrDocid(&left) ){ |
| 1522 dlrStep(&right); | 1643 rc = dlrStep(&right); |
| 1644 if( rc!=SQLITE_OK ) goto err; |
| 1523 }else{ | 1645 }else{ |
| 1524 if( nNear==0 ){ | 1646 if( nNear==0 ){ |
| 1525 posListPhraseMerge(&left, &right, 0, 0, &writer); | 1647 rc = posListPhraseMerge(&left, &right, 0, 0, &writer); |
| 1648 if( rc!=SQLITE_OK ) goto err; |
| 1526 }else{ | 1649 }else{ |
| 1527 /* This case occurs when two terms (simple terms or phrases) are | 1650 /* This case occurs when two terms (simple terms or phrases) are |
| 1528 * connected by a NEAR operator, span (nNear+1). i.e. | 1651 * connected by a NEAR operator, span (nNear+1). i.e. |
| 1529 * | 1652 * |
| 1530 * '"terrible company" NEAR widget' | 1653 * '"terrible company" NEAR widget' |
| 1531 */ | 1654 */ |
| 1532 DataBuffer one = {0, 0, 0}; | |
| 1533 DataBuffer two = {0, 0, 0}; | |
| 1534 | |
| 1535 DLWriter dlwriter2; | 1655 DLWriter dlwriter2; |
| 1536 DLReader dr1 = {0, 0, 0, 0, 0}; | 1656 DLReader dr1 = {0, 0, 0, 0, 0}; |
| 1537 DLReader dr2 = {0, 0, 0, 0, 0}; | 1657 DLReader dr2 = {0, 0, 0, 0, 0}; |
| 1538 | 1658 |
| 1539 dlwInit(&dlwriter2, iType, &one); | 1659 dlwInit(&dlwriter2, iType, &one); |
| 1540 posListPhraseMerge(&right, &left, nNear-3+nPhrase, 1, &dlwriter2); | 1660 rc = posListPhraseMerge(&right, &left, nNear-3+nPhrase, 1, &dlwriter2); |
| 1661 if( rc!=SQLITE_OK ) goto err; |
| 1541 dlwInit(&dlwriter2, iType, &two); | 1662 dlwInit(&dlwriter2, iType, &two); |
| 1542 posListPhraseMerge(&left, &right, nNear-1, 0, &dlwriter2); | 1663 rc = posListPhraseMerge(&left, &right, nNear-1, 0, &dlwriter2); |
| 1664 if( rc!=SQLITE_OK ) goto err; |
| 1543 | 1665 |
| 1544 if( one.nData) dlrInit(&dr1, iType, one.pData, one.nData); | 1666 if( one.nData){ |
| 1545 if( two.nData) dlrInit(&dr2, iType, two.pData, two.nData); | 1667 rc = dlrInit(&dr1, iType, one.pData, one.nData); |
| 1668 if( rc!=SQLITE_OK ) goto err; |
| 1669 } |
| 1670 if( two.nData){ |
| 1671 rc = dlrInit(&dr2, iType, two.pData, two.nData); |
| 1672 if( rc!=SQLITE_OK ) goto err; |
| 1673 } |
| 1546 | 1674 |
| 1547 if( !dlrAtEnd(&dr1) || !dlrAtEnd(&dr2) ){ | 1675 if( !dlrAtEnd(&dr1) || !dlrAtEnd(&dr2) ){ |
| 1548 PLReader pr1 = {0}; | 1676 PLReader pr1 = {0}; |
| 1549 PLReader pr2 = {0}; | 1677 PLReader pr2 = {0}; |
| 1550 | 1678 |
| 1551 PLWriter plwriter; | 1679 PLWriter plwriter; |
| 1552 plwInit(&plwriter, &writer, dlrDocid(dlrAtEnd(&dr1)?&dr2:&dr1)); | 1680 plwInit(&plwriter, &writer, dlrDocid(dlrAtEnd(&dr1)?&dr2:&dr1)); |
| 1553 | 1681 |
| 1554 if( one.nData ) plrInit(&pr1, &dr1); | 1682 if( one.nData ){ |
| 1555 if( two.nData ) plrInit(&pr2, &dr2); | 1683 rc = plrInit(&pr1, &dr1); |
| 1684 if( rc!=SQLITE_OK ) goto err; |
| 1685 } |
| 1686 if( two.nData ){ |
| 1687 rc = plrInit(&pr2, &dr2); |
| 1688 if( rc!=SQLITE_OK ) goto err; |
| 1689 } |
| 1556 while( !plrAtEnd(&pr1) || !plrAtEnd(&pr2) ){ | 1690 while( !plrAtEnd(&pr1) || !plrAtEnd(&pr2) ){ |
| 1557 int iCompare = plrCompare(&pr1, &pr2); | 1691 int iCompare = plrCompare(&pr1, &pr2); |
| 1558 switch( iCompare ){ | 1692 switch( iCompare ){ |
| 1559 case -1: | 1693 case -1: |
| 1560 plwCopy(&plwriter, &pr1); | 1694 plwCopy(&plwriter, &pr1); |
| 1561 plrStep(&pr1); | 1695 rc = plrStep(&pr1); |
| 1696 if( rc!=SQLITE_OK ) goto err; |
| 1562 break; | 1697 break; |
| 1563 case 1: | 1698 case 1: |
| 1564 plwCopy(&plwriter, &pr2); | 1699 plwCopy(&plwriter, &pr2); |
| 1565 plrStep(&pr2); | 1700 rc = plrStep(&pr2); |
| 1701 if( rc!=SQLITE_OK ) goto err; |
| 1566 break; | 1702 break; |
| 1567 case 0: | 1703 case 0: |
| 1568 plwCopy(&plwriter, &pr1); | 1704 plwCopy(&plwriter, &pr1); |
| 1569 plrStep(&pr1); | 1705 rc = plrStep(&pr1); |
| 1570 plrStep(&pr2); | 1706 if( rc!=SQLITE_OK ) goto err; |
| 1707 rc = plrStep(&pr2); |
| 1708 if( rc!=SQLITE_OK ) goto err; |
| 1571 break; | 1709 break; |
| 1572 } | 1710 } |
| 1573 } | 1711 } |
| 1574 plwTerminate(&plwriter); | 1712 plwTerminate(&plwriter); |
| 1575 } | 1713 } |
| 1576 dataBufferDestroy(&one); | 1714 dataBufferReset(&one); |
| 1577 dataBufferDestroy(&two); | 1715 dataBufferReset(&two); |
| 1578 } | 1716 } |
| 1579 dlrStep(&left); | 1717 rc = dlrStep(&left); |
| 1580 dlrStep(&right); | 1718 if( rc!=SQLITE_OK ) goto err; |
| 1581 } | 1719 rc = dlrStep(&right); |
| 1582 } | 1720 if( rc!=SQLITE_OK ) goto err; |
| 1583 | 1721 } |
| 1584 dlrDestroy(&left); | 1722 } |
| 1585 dlrDestroy(&right); | 1723 |
| 1586 dlwDestroy(&writer); | 1724 err: |
| 1725 dataBufferDestroy(&one); |
| 1726 dataBufferDestroy(&two); |
| 1727 dlrDestroy(&left); |
| 1728 dlrDestroy(&right); |
| 1729 dlwDestroy(&writer); |
| 1730 return rc; |
| 1587 } | 1731 } |
| 1588 | 1732 |
| 1589 /* We have two DL_DOCIDS doclists: pLeft and pRight. | 1733 /* We have two DL_DOCIDS doclists: pLeft and pRight. |
| 1590 ** Write the intersection of these two doclists into pOut as a | 1734 ** Write the intersection of these two doclists into pOut as a |
| 1591 ** DL_DOCIDS doclist. | 1735 ** DL_DOCIDS doclist. |
| 1592 */ | 1736 */ |
| 1593 static void docListAndMerge( | 1737 static int docListAndMerge( |
| 1594 const char *pLeft, int nLeft, | 1738 const char *pLeft, int nLeft, |
| 1595 const char *pRight, int nRight, | 1739 const char *pRight, int nRight, |
| 1596 DataBuffer *pOut /* Write the combined doclist here */ | 1740 DataBuffer *pOut /* Write the combined doclist here */ |
| 1597 ){ | 1741 ){ |
| 1598 DLReader left, right; | 1742 DLReader left, right; |
| 1599 DLWriter writer; | 1743 DLWriter writer; |
| 1600 | 1744 int rc; |
| 1601 if( nLeft==0 || nRight==0 ) return; | 1745 |
| 1602 | 1746 if( nLeft==0 || nRight==0 ) return SQLITE_OK; |
| 1603 dlrInit(&left, DL_DOCIDS, pLeft, nLeft); | 1747 |
| 1604 dlrInit(&right, DL_DOCIDS, pRight, nRight); | 1748 rc = dlrInit(&left, DL_DOCIDS, pLeft, nLeft); |
| 1749 if( rc!=SQLITE_OK ) return rc; |
| 1750 rc = dlrInit(&right, DL_DOCIDS, pRight, nRight); |
| 1751 if( rc!=SQLITE_OK ){ |
| 1752 dlrDestroy(&left); |
| 1753 return rc; |
| 1754 } |
| 1605 dlwInit(&writer, DL_DOCIDS, pOut); | 1755 dlwInit(&writer, DL_DOCIDS, pOut); |
| 1606 | 1756 |
| 1607 while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){ | 1757 while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){ |
| 1608 if( dlrDocid(&left)<dlrDocid(&right) ){ | 1758 if( dlrDocid(&left)<dlrDocid(&right) ){ |
| 1609 dlrStep(&left); | 1759 rc = dlrStep(&left); |
| 1760 if( rc!=SQLITE_OK ) break; |
| 1610 }else if( dlrDocid(&right)<dlrDocid(&left) ){ | 1761 }else if( dlrDocid(&right)<dlrDocid(&left) ){ |
| 1611 dlrStep(&right); | 1762 rc = dlrStep(&right); |
| 1763 if( rc!=SQLITE_OK ) break; |
| 1612 }else{ | 1764 }else{ |
| 1613 dlwAdd(&writer, dlrDocid(&left)); | 1765 dlwAdd(&writer, dlrDocid(&left)); |
| 1614 dlrStep(&left); | 1766 rc = dlrStep(&left); |
| 1615 dlrStep(&right); | 1767 if( rc!=SQLITE_OK ) break; |
| 1616 } | 1768 rc = dlrStep(&right); |
| 1617 } | 1769 if( rc!=SQLITE_OK ) break; |
| 1618 | 1770 } |
| 1619 dlrDestroy(&left); | 1771 } |
| 1620 dlrDestroy(&right); | 1772 |
| 1621 dlwDestroy(&writer); | 1773 dlrDestroy(&left); |
| 1774 dlrDestroy(&right); |
| 1775 dlwDestroy(&writer); |
| 1776 return rc; |
| 1622 } | 1777 } |
| 1623 | 1778 |
| 1624 /* We have two DL_DOCIDS doclists: pLeft and pRight. | 1779 /* We have two DL_DOCIDS doclists: pLeft and pRight. |
| 1625 ** Write the union of these two doclists into pOut as a | 1780 ** Write the union of these two doclists into pOut as a |
| 1626 ** DL_DOCIDS doclist. | 1781 ** DL_DOCIDS doclist. |
| 1627 */ | 1782 */ |
| 1628 static void docListOrMerge( | 1783 static int docListOrMerge( |
| 1629 const char *pLeft, int nLeft, | 1784 const char *pLeft, int nLeft, |
| 1630 const char *pRight, int nRight, | 1785 const char *pRight, int nRight, |
| 1631 DataBuffer *pOut /* Write the combined doclist here */ | 1786 DataBuffer *pOut /* Write the combined doclist here */ |
| 1632 ){ | 1787 ){ |
| 1633 DLReader left, right; | 1788 DLReader left, right; |
| 1634 DLWriter writer; | 1789 DLWriter writer; |
| 1790 int rc; |
| 1635 | 1791 |
| 1636 if( nLeft==0 ){ | 1792 if( nLeft==0 ){ |
| 1637 if( nRight!=0 ) dataBufferAppend(pOut, pRight, nRight); | 1793 if( nRight!=0 ) dataBufferAppend(pOut, pRight, nRight); |
| 1638 return; | 1794 return SQLITE_OK; |
| 1639 } | 1795 } |
| 1640 if( nRight==0 ){ | 1796 if( nRight==0 ){ |
| 1641 dataBufferAppend(pOut, pLeft, nLeft); | 1797 dataBufferAppend(pOut, pLeft, nLeft); |
| 1642 return; | 1798 return SQLITE_OK; |
| 1643 } | 1799 } |
| 1644 | 1800 |
| 1645 dlrInit(&left, DL_DOCIDS, pLeft, nLeft); | 1801 rc = dlrInit(&left, DL_DOCIDS, pLeft, nLeft); |
| 1646 dlrInit(&right, DL_DOCIDS, pRight, nRight); | 1802 if( rc!=SQLITE_OK ) return rc; |
| 1803 rc = dlrInit(&right, DL_DOCIDS, pRight, nRight); |
| 1804 if( rc!=SQLITE_OK ){ |
| 1805 dlrDestroy(&left); |
| 1806 return rc; |
| 1807 } |
| 1647 dlwInit(&writer, DL_DOCIDS, pOut); | 1808 dlwInit(&writer, DL_DOCIDS, pOut); |
| 1648 | 1809 |
| 1649 while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ | 1810 while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ |
| 1650 if( dlrAtEnd(&right) ){ | 1811 if( dlrAtEnd(&right) ){ |
| 1651 dlwAdd(&writer, dlrDocid(&left)); | 1812 dlwAdd(&writer, dlrDocid(&left)); |
| 1652 dlrStep(&left); | 1813 rc = dlrStep(&left); |
| 1814 if( rc!=SQLITE_OK ) break; |
| 1653 }else if( dlrAtEnd(&left) ){ | 1815 }else if( dlrAtEnd(&left) ){ |
| 1654 dlwAdd(&writer, dlrDocid(&right)); | 1816 dlwAdd(&writer, dlrDocid(&right)); |
| 1655 dlrStep(&right); | 1817 rc = dlrStep(&right); |
| 1818 if( rc!=SQLITE_OK ) break; |
| 1656 }else if( dlrDocid(&left)<dlrDocid(&right) ){ | 1819 }else if( dlrDocid(&left)<dlrDocid(&right) ){ |
| 1657 dlwAdd(&writer, dlrDocid(&left)); | 1820 dlwAdd(&writer, dlrDocid(&left)); |
| 1658 dlrStep(&left); | 1821 rc = dlrStep(&left); |
| 1822 if( rc!=SQLITE_OK ) break; |
| 1659 }else if( dlrDocid(&right)<dlrDocid(&left) ){ | 1823 }else if( dlrDocid(&right)<dlrDocid(&left) ){ |
| 1660 dlwAdd(&writer, dlrDocid(&right)); | 1824 dlwAdd(&writer, dlrDocid(&right)); |
| 1661 dlrStep(&right); | 1825 rc = dlrStep(&right); |
| 1826 if( rc!=SQLITE_OK ) break; |
| 1662 }else{ | 1827 }else{ |
| 1663 dlwAdd(&writer, dlrDocid(&left)); | 1828 dlwAdd(&writer, dlrDocid(&left)); |
| 1664 dlrStep(&left); | 1829 rc = dlrStep(&left); |
| 1665 dlrStep(&right); | 1830 if( rc!=SQLITE_OK ) break; |
| 1666 } | 1831 rc = dlrStep(&right); |
| 1667 } | 1832 if( rc!=SQLITE_OK ) break; |
| 1668 | 1833 } |
| 1669 dlrDestroy(&left); | 1834 } |
| 1670 dlrDestroy(&right); | 1835 |
| 1671 dlwDestroy(&writer); | 1836 dlrDestroy(&left); |
| 1837 dlrDestroy(&right); |
| 1838 dlwDestroy(&writer); |
| 1839 return rc; |
| 1672 } | 1840 } |
| 1673 | 1841 |
| 1674 /* We have two DL_DOCIDS doclists: pLeft and pRight. | 1842 /* We have two DL_DOCIDS doclists: pLeft and pRight. |
| 1675 ** Write into pOut as DL_DOCIDS doclist containing all documents that | 1843 ** Write into pOut as DL_DOCIDS doclist containing all documents that |
| 1676 ** occur in pLeft but not in pRight. | 1844 ** occur in pLeft but not in pRight. |
| 1677 */ | 1845 */ |
| 1678 static void docListExceptMerge( | 1846 static int docListExceptMerge( |
| 1679 const char *pLeft, int nLeft, | 1847 const char *pLeft, int nLeft, |
| 1680 const char *pRight, int nRight, | 1848 const char *pRight, int nRight, |
| 1681 DataBuffer *pOut /* Write the combined doclist here */ | 1849 DataBuffer *pOut /* Write the combined doclist here */ |
| 1682 ){ | 1850 ){ |
| 1683 DLReader left, right; | 1851 DLReader left, right; |
| 1684 DLWriter writer; | 1852 DLWriter writer; |
| 1685 | 1853 int rc; |
| 1686 if( nLeft==0 ) return; | 1854 |
| 1855 if( nLeft==0 ) return SQLITE_OK; |
| 1687 if( nRight==0 ){ | 1856 if( nRight==0 ){ |
| 1688 dataBufferAppend(pOut, pLeft, nLeft); | 1857 dataBufferAppend(pOut, pLeft, nLeft); |
| 1689 return; | 1858 return SQLITE_OK; |
| 1690 } | 1859 } |
| 1691 | 1860 |
| 1692 dlrInit(&left, DL_DOCIDS, pLeft, nLeft); | 1861 rc = dlrInit(&left, DL_DOCIDS, pLeft, nLeft); |
| 1693 dlrInit(&right, DL_DOCIDS, pRight, nRight); | 1862 if( rc!=SQLITE_OK ) return rc; |
| 1863 rc = dlrInit(&right, DL_DOCIDS, pRight, nRight); |
| 1864 if( rc!=SQLITE_OK ){ |
| 1865 dlrDestroy(&left); |
| 1866 return rc; |
| 1867 } |
| 1694 dlwInit(&writer, DL_DOCIDS, pOut); | 1868 dlwInit(&writer, DL_DOCIDS, pOut); |
| 1695 | 1869 |
| 1696 while( !dlrAtEnd(&left) ){ | 1870 while( !dlrAtEnd(&left) ){ |
| 1697 while( !dlrAtEnd(&right) && dlrDocid(&right)<dlrDocid(&left) ){ | 1871 while( !dlrAtEnd(&right) && dlrDocid(&right)<dlrDocid(&left) ){ |
| 1698 dlrStep(&right); | 1872 rc = dlrStep(&right); |
| 1873 if( rc!=SQLITE_OK ) goto err; |
| 1699 } | 1874 } |
| 1700 if( dlrAtEnd(&right) || dlrDocid(&left)<dlrDocid(&right) ){ | 1875 if( dlrAtEnd(&right) || dlrDocid(&left)<dlrDocid(&right) ){ |
| 1701 dlwAdd(&writer, dlrDocid(&left)); | 1876 dlwAdd(&writer, dlrDocid(&left)); |
| 1702 } | 1877 } |
| 1703 dlrStep(&left); | 1878 rc = dlrStep(&left); |
| 1704 } | 1879 if( rc!=SQLITE_OK ) break; |
| 1705 | 1880 } |
| 1706 dlrDestroy(&left); | 1881 |
| 1707 dlrDestroy(&right); | 1882 err: |
| 1708 dlwDestroy(&writer); | 1883 dlrDestroy(&left); |
| 1884 dlrDestroy(&right); |
| 1885 dlwDestroy(&writer); |
| 1886 return rc; |
| 1709 } | 1887 } |
| 1710 | 1888 |
| 1711 static char *string_dup_n(const char *s, int n){ | 1889 static char *string_dup_n(const char *s, int n){ |
| 1712 char *str = sqlite3_malloc(n + 1); | 1890 char *str = sqlite3_malloc(n + 1); |
| 1713 memcpy(str, s, n); | 1891 memcpy(str, s, n); |
| 1714 str[n] = '\0'; | 1892 str[n] = '\0'; |
| 1715 return str; | 1893 return str; |
| 1716 } | 1894 } |
| 1717 | 1895 |
| 1718 /* Duplicate a string; the caller must free() the returned string. | 1896 /* Duplicate a string; the caller must free() the returned string. |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1851 | 2029 |
| 1852 /* BLOCK_INSERT */ | 2030 /* BLOCK_INSERT */ |
| 1853 "insert into %_segments (blockid, block) values (null, ?)", | 2031 "insert into %_segments (blockid, block) values (null, ?)", |
| 1854 /* BLOCK_SELECT */ "select block from %_segments where blockid = ?", | 2032 /* BLOCK_SELECT */ "select block from %_segments where blockid = ?", |
| 1855 /* BLOCK_DELETE */ "delete from %_segments where blockid between ? and ?", | 2033 /* BLOCK_DELETE */ "delete from %_segments where blockid between ? and ?", |
| 1856 /* BLOCK_DELETE_ALL */ "delete from %_segments", | 2034 /* BLOCK_DELETE_ALL */ "delete from %_segments", |
| 1857 | 2035 |
| 1858 /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?", | 2036 /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?", |
| 1859 /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)", | 2037 /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)", |
| 1860 /* SEGDIR_SELECT_LEVEL */ | 2038 /* SEGDIR_SELECT_LEVEL */ |
| 1861 "select start_block, leaves_end_block, root from %_segdir " | 2039 "select start_block, leaves_end_block, root, idx from %_segdir " |
| 1862 " where level = ? order by idx", | 2040 " where level = ? order by idx", |
| 1863 /* SEGDIR_SPAN */ | 2041 /* SEGDIR_SPAN */ |
| 1864 "select min(start_block), max(end_block) from %_segdir " | 2042 "select min(start_block), max(end_block) from %_segdir " |
| 1865 " where level = ? and start_block <> 0", | 2043 " where level = ? and start_block <> 0", |
| 1866 /* SEGDIR_DELETE */ "delete from %_segdir where level = ?", | 2044 /* SEGDIR_DELETE */ "delete from %_segdir where level = ?", |
| 1867 | 2045 |
| 1868 /* NOTE(shess): The first three results of the following two | 2046 /* NOTE(shess): The first three results of the following two |
| 1869 ** statements must match. | 2047 ** statements must match. |
| 1870 */ | 2048 */ |
| 1871 /* SEGDIR_SELECT_SEGMENT */ | 2049 /* SEGDIR_SELECT_SEGMENT */ |
| (...skipping 1801 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3673 } | 3851 } |
| 3674 } else { /* full-text query */ | 3852 } else { /* full-text query */ |
| 3675 rc = sqlite3_reset(c->pStmt); | 3853 rc = sqlite3_reset(c->pStmt); |
| 3676 if( rc!=SQLITE_OK ) return rc; | 3854 if( rc!=SQLITE_OK ) return rc; |
| 3677 | 3855 |
| 3678 if( c->result.nData==0 || dlrAtEnd(&c->reader) ){ | 3856 if( c->result.nData==0 || dlrAtEnd(&c->reader) ){ |
| 3679 c->eof = 1; | 3857 c->eof = 1; |
| 3680 return SQLITE_OK; | 3858 return SQLITE_OK; |
| 3681 } | 3859 } |
| 3682 rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader)); | 3860 rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader)); |
| 3683 dlrStep(&c->reader); | 3861 if( rc!=SQLITE_OK ) return rc; |
| 3862 rc = dlrStep(&c->reader); |
| 3684 if( rc!=SQLITE_OK ) return rc; | 3863 if( rc!=SQLITE_OK ) return rc; |
| 3685 /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ | 3864 /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ |
| 3686 rc = sqlite3_step(c->pStmt); | 3865 rc = sqlite3_step(c->pStmt); |
| 3687 if( rc==SQLITE_ROW ){ /* the case we expect */ | 3866 if( rc==SQLITE_ROW ){ /* the case we expect */ |
| 3688 c->eof = 0; | 3867 c->eof = 0; |
| 3689 return SQLITE_OK; | 3868 return SQLITE_OK; |
| 3690 } | 3869 } |
| 3691 /* an error occurred; abort */ | 3870 /* Corrupt if the index refers to missing document. */ |
| 3692 return rc==SQLITE_DONE ? SQLITE_ERROR : rc; | 3871 if( rc==SQLITE_DONE ) return SQLITE_CORRUPT_BKPT; |
| 3872 |
| 3873 return rc; |
| 3693 } | 3874 } |
| 3694 } | 3875 } |
| 3695 | 3876 |
| 3696 | 3877 |
| 3697 /* TODO(shess) If we pushed LeafReader to the top of the file, or to | 3878 /* TODO(shess) If we pushed LeafReader to the top of the file, or to |
| 3698 ** another file, term_select() could be pushed above | 3879 ** another file, term_select() could be pushed above |
| 3699 ** docListOfTerm(). | 3880 ** docListOfTerm(). |
| 3700 */ | 3881 */ |
| 3701 static int termSelect(fulltext_vtab *v, int iColumn, | 3882 static int termSelect(fulltext_vtab *v, int iColumn, |
| 3702 const char *pTerm, int nTerm, int isPrefix, | 3883 const char *pTerm, int nTerm, int isPrefix, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 3732 rc = termSelect(pTab, iCol, p->z, p->n, p->isPrefix, eType, &tmp); | 3913 rc = termSelect(pTab, iCol, p->z, p->n, p->isPrefix, eType, &tmp); |
| 3733 if( rc==SQLITE_OK ){ | 3914 if( rc==SQLITE_OK ){ |
| 3734 if( ii==0 ){ | 3915 if( ii==0 ){ |
| 3735 *pResult = tmp; | 3916 *pResult = tmp; |
| 3736 }else{ | 3917 }else{ |
| 3737 DataBuffer res = *pResult; | 3918 DataBuffer res = *pResult; |
| 3738 dataBufferInit(pResult, 0); | 3919 dataBufferInit(pResult, 0); |
| 3739 if( ii==(pPhrase->nToken-1) ){ | 3920 if( ii==(pPhrase->nToken-1) ){ |
| 3740 eType = eListType; | 3921 eType = eListType; |
| 3741 } | 3922 } |
| 3742 docListPhraseMerge( | 3923 rc = docListPhraseMerge( |
| 3743 res.pData, res.nData, tmp.pData, tmp.nData, 0, 0, eType, pResult | 3924 res.pData, res.nData, tmp.pData, tmp.nData, 0, 0, eType, pResult |
| 3744 ); | 3925 ); |
| 3745 dataBufferDestroy(&res); | 3926 dataBufferDestroy(&res); |
| 3746 dataBufferDestroy(&tmp); | 3927 dataBufferDestroy(&tmp); |
| 3928 if( rc!= SQLITE_OK ) return rc; |
| 3747 } | 3929 } |
| 3748 } | 3930 } |
| 3749 } | 3931 } |
| 3750 | 3932 |
| 3751 return rc; | 3933 return rc; |
| 3752 } | 3934 } |
| 3753 | 3935 |
| 3754 /* | 3936 /* |
| 3755 ** Evaluate the full-text expression pExpr against fts3 table pTab. Write | 3937 ** Evaluate the full-text expression pExpr against fts3 table pTab. Write |
| 3756 ** the results into pRes. | 3938 ** the results into pRes. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3791 if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){ | 3973 if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){ |
| 3792 eType = DL_POSITIONS; | 3974 eType = DL_POSITIONS; |
| 3793 } | 3975 } |
| 3794 pLeft = pExpr->pLeft; | 3976 pLeft = pExpr->pLeft; |
| 3795 while( pLeft->eType==FTSQUERY_NEAR ){ | 3977 while( pLeft->eType==FTSQUERY_NEAR ){ |
| 3796 pLeft=pLeft->pRight; | 3978 pLeft=pLeft->pRight; |
| 3797 } | 3979 } |
| 3798 assert( pExpr->pRight->eType==FTSQUERY_PHRASE ); | 3980 assert( pExpr->pRight->eType==FTSQUERY_PHRASE ); |
| 3799 assert( pLeft->eType==FTSQUERY_PHRASE ); | 3981 assert( pLeft->eType==FTSQUERY_PHRASE ); |
| 3800 nToken = pLeft->pPhrase->nToken + pExpr->pRight->pPhrase->nToken; | 3982 nToken = pLeft->pPhrase->nToken + pExpr->pRight->pPhrase->nToken; |
| 3801 docListPhraseMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, | 3983 rc = docListPhraseMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, |
| 3802 pExpr->nNear+1, nToken, eType, pRes | 3984 pExpr->nNear+1, nToken, eType, pRes |
| 3803 ); | 3985 ); |
| 3804 break; | 3986 break; |
| 3805 } | 3987 } |
| 3806 case FTSQUERY_NOT: { | 3988 case FTSQUERY_NOT: { |
| 3807 docListExceptMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData,pRes); | 3989 rc = docListExceptMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData,p
Res); |
| 3808 break; | 3990 break; |
| 3809 } | 3991 } |
| 3810 case FTSQUERY_AND: { | 3992 case FTSQUERY_AND: { |
| 3811 docListAndMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, pRes); | 3993 rc = docListAndMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, pRe
s); |
| 3812 break; | 3994 break; |
| 3813 } | 3995 } |
| 3814 case FTSQUERY_OR: { | 3996 case FTSQUERY_OR: { |
| 3815 docListOrMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, pRes); | 3997 rc = docListOrMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, pRes
); |
| 3816 break; | 3998 break; |
| 3817 } | 3999 } |
| 3818 } | 4000 } |
| 3819 } | 4001 } |
| 3820 dataBufferDestroy(&lhs); | 4002 dataBufferDestroy(&lhs); |
| 3821 dataBufferDestroy(&rhs); | 4003 dataBufferDestroy(&rhs); |
| 3822 } | 4004 } |
| 3823 } | 4005 } |
| 3824 | 4006 |
| 3825 return rc; | 4007 return rc; |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4462 DataBuffer term; /* previous term, for decoding term delta. */ | 4644 DataBuffer term; /* previous term, for decoding term delta. */ |
| 4463 | 4645 |
| 4464 sqlite_int64 iBlockid; | 4646 sqlite_int64 iBlockid; |
| 4465 } InteriorReader; | 4647 } InteriorReader; |
| 4466 | 4648 |
| 4467 static void interiorReaderDestroy(InteriorReader *pReader){ | 4649 static void interiorReaderDestroy(InteriorReader *pReader){ |
| 4468 dataBufferDestroy(&pReader->term); | 4650 dataBufferDestroy(&pReader->term); |
| 4469 SCRAMBLE(pReader); | 4651 SCRAMBLE(pReader); |
| 4470 } | 4652 } |
| 4471 | 4653 |
| 4472 /* TODO(shess) The assertions are great, but what if we're in NDEBUG | 4654 static int interiorReaderInit(const char *pData, int nData, |
| 4473 ** and the blob is empty or otherwise contains suspect data? | 4655 InteriorReader *pReader){ |
| 4474 */ | |
| 4475 static void interiorReaderInit(const char *pData, int nData, | |
| 4476 InteriorReader *pReader){ | |
| 4477 int n, nTerm; | 4656 int n, nTerm; |
| 4478 | 4657 |
| 4479 /* Require at least the leading flag byte */ | 4658 /* These conditions are checked and met by the callers. */ |
| 4480 assert( nData>0 ); | 4659 assert( nData>0 ); |
| 4481 assert( pData[0]!='\0' ); | 4660 assert( pData[0]!='\0' ); |
| 4482 | 4661 |
| 4483 CLEAR(pReader); | 4662 CLEAR(pReader); |
| 4484 | 4663 |
| 4485 /* Decode the base blockid, and set the cursor to the first term. */ | 4664 /* Decode the base blockid, and set the cursor to the first term. */ |
| 4486 n = fts3GetVarint(pData+1, &pReader->iBlockid); | 4665 n = fts3GetVarintSafe(pData+1, &pReader->iBlockid, nData-1); |
| 4487 assert( 1+n<=nData ); | 4666 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 4488 pReader->pData = pData+1+n; | 4667 pReader->pData = pData+1+n; |
| 4489 pReader->nData = nData-(1+n); | 4668 pReader->nData = nData-(1+n); |
| 4490 | 4669 |
| 4491 /* A single-child interior node (such as when a leaf node was too | 4670 /* A single-child interior node (such as when a leaf node was too |
| 4492 ** large for the segment directory) won't have any terms. | 4671 ** large for the segment directory) won't have any terms. |
| 4493 ** Otherwise, decode the first term. | 4672 ** Otherwise, decode the first term. |
| 4494 */ | 4673 */ |
| 4495 if( pReader->nData==0 ){ | 4674 if( pReader->nData==0 ){ |
| 4496 dataBufferInit(&pReader->term, 0); | 4675 dataBufferInit(&pReader->term, 0); |
| 4497 }else{ | 4676 }else{ |
| 4498 n = fts3GetVarint32(pReader->pData, &nTerm); | 4677 n = fts3GetVarint32Safe(pReader->pData, &nTerm, pReader->nData); |
| 4678 if( !n || nTerm<0 || nTerm>pReader->nData-n) return SQLITE_CORRUPT_BKPT; |
| 4499 dataBufferInit(&pReader->term, nTerm); | 4679 dataBufferInit(&pReader->term, nTerm); |
| 4500 dataBufferReplace(&pReader->term, pReader->pData+n, nTerm); | 4680 dataBufferReplace(&pReader->term, pReader->pData+n, nTerm); |
| 4501 assert( n+nTerm<=pReader->nData ); | |
| 4502 pReader->pData += n+nTerm; | 4681 pReader->pData += n+nTerm; |
| 4503 pReader->nData -= n+nTerm; | 4682 pReader->nData -= n+nTerm; |
| 4504 } | 4683 } |
| 4684 return SQLITE_OK; |
| 4505 } | 4685 } |
| 4506 | 4686 |
| 4507 static int interiorReaderAtEnd(InteriorReader *pReader){ | 4687 static int interiorReaderAtEnd(InteriorReader *pReader){ |
| 4508 return pReader->term.nData==0; | 4688 return pReader->term.nData<=0; |
| 4509 } | 4689 } |
| 4510 | 4690 |
| 4511 static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){ | 4691 static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){ |
| 4512 return pReader->iBlockid; | 4692 return pReader->iBlockid; |
| 4513 } | 4693 } |
| 4514 | 4694 |
| 4515 static int interiorReaderTermBytes(InteriorReader *pReader){ | 4695 static int interiorReaderTermBytes(InteriorReader *pReader){ |
| 4516 assert( !interiorReaderAtEnd(pReader) ); | 4696 assert( !interiorReaderAtEnd(pReader) ); |
| 4517 return pReader->term.nData; | 4697 return pReader->term.nData; |
| 4518 } | 4698 } |
| 4519 static const char *interiorReaderTerm(InteriorReader *pReader){ | 4699 static const char *interiorReaderTerm(InteriorReader *pReader){ |
| 4520 assert( !interiorReaderAtEnd(pReader) ); | 4700 assert( !interiorReaderAtEnd(pReader) ); |
| 4521 return pReader->term.pData; | 4701 return pReader->term.pData; |
| 4522 } | 4702 } |
| 4523 | 4703 |
| 4524 /* Step forward to the next term in the node. */ | 4704 /* Step forward to the next term in the node. */ |
| 4525 static void interiorReaderStep(InteriorReader *pReader){ | 4705 static int interiorReaderStep(InteriorReader *pReader){ |
| 4526 assert( !interiorReaderAtEnd(pReader) ); | 4706 assert( !interiorReaderAtEnd(pReader) ); |
| 4527 | 4707 |
| 4528 /* If the last term has been read, signal eof, else construct the | 4708 /* If the last term has been read, signal eof, else construct the |
| 4529 ** next term. | 4709 ** next term. |
| 4530 */ | 4710 */ |
| 4531 if( pReader->nData==0 ){ | 4711 if( pReader->nData==0 ){ |
| 4532 dataBufferReset(&pReader->term); | 4712 dataBufferReset(&pReader->term); |
| 4533 }else{ | 4713 }else{ |
| 4534 int n, nPrefix, nSuffix; | 4714 int n, nPrefix, nSuffix; |
| 4535 | 4715 |
| 4536 n = fts3GetVarint32(pReader->pData, &nPrefix); | 4716 n = fts3GetVarint32Safe(pReader->pData, &nPrefix, pReader->nData); |
| 4537 n += fts3GetVarint32(pReader->pData+n, &nSuffix); | 4717 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 4718 pReader->nData -= n; |
| 4719 pReader->pData += n; |
| 4720 n += fts3GetVarint32Safe(pReader->pData, &nSuffix, pReader->nData); |
| 4721 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 4722 pReader->nData -= n; |
| 4723 pReader->pData += n; |
| 4724 if( nSuffix<0 || nSuffix>pReader->nData ) return SQLITE_CORRUPT_BKPT; |
| 4725 if( nPrefix<0 || nPrefix>pReader->term.nData ) return SQLITE_CORRUPT_BKPT; |
| 4538 | 4726 |
| 4539 /* Truncate the current term and append suffix data. */ | 4727 /* Truncate the current term and append suffix data. */ |
| 4540 pReader->term.nData = nPrefix; | 4728 pReader->term.nData = nPrefix; |
| 4541 dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); | 4729 dataBufferAppend(&pReader->term, pReader->pData, nSuffix); |
| 4542 | 4730 |
| 4543 assert( n+nSuffix<=pReader->nData ); | 4731 pReader->pData += nSuffix; |
| 4544 pReader->pData += n+nSuffix; | 4732 pReader->nData -= nSuffix; |
| 4545 pReader->nData -= n+nSuffix; | |
| 4546 } | 4733 } |
| 4547 pReader->iBlockid++; | 4734 pReader->iBlockid++; |
| 4735 return SQLITE_OK; |
| 4548 } | 4736 } |
| 4549 | 4737 |
| 4550 /* Compare the current term to pTerm[nTerm], returning strcmp-style | 4738 /* Compare the current term to pTerm[nTerm], returning strcmp-style |
| 4551 ** results. If isPrefix, equality means equal through nTerm bytes. | 4739 ** results. If isPrefix, equality means equal through nTerm bytes. |
| 4552 */ | 4740 */ |
| 4553 static int interiorReaderTermCmp(InteriorReader *pReader, | 4741 static int interiorReaderTermCmp(InteriorReader *pReader, |
| 4554 const char *pTerm, int nTerm, int isPrefix){ | 4742 const char *pTerm, int nTerm, int isPrefix){ |
| 4555 const char *pReaderTerm = interiorReaderTerm(pReader); | 4743 const char *pReaderTerm = interiorReaderTerm(pReader); |
| 4556 int nReaderTerm = interiorReaderTermBytes(pReader); | 4744 int nReaderTerm = interiorReaderTermBytes(pReader); |
| 4557 int c, n = nReaderTerm<nTerm ? nReaderTerm : nTerm; | 4745 int c, n = nReaderTerm<nTerm ? nReaderTerm : nTerm; |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4909 | 5097 |
| 4910 /* Estimate the length of the merged doclist so we can leave space | 5098 /* Estimate the length of the merged doclist so we can leave space |
| 4911 ** to encode it. | 5099 ** to encode it. |
| 4912 */ | 5100 */ |
| 4913 for(i=0, nData=0; i<nReaders; i++){ | 5101 for(i=0, nData=0; i<nReaders; i++){ |
| 4914 nData += dlrAllDataBytes(&pReaders[i]); | 5102 nData += dlrAllDataBytes(&pReaders[i]); |
| 4915 } | 5103 } |
| 4916 n = fts3PutVarint(c, nData); | 5104 n = fts3PutVarint(c, nData); |
| 4917 dataBufferAppend(&pWriter->data, c, n); | 5105 dataBufferAppend(&pWriter->data, c, n); |
| 4918 | 5106 |
| 4919 docListMerge(&pWriter->data, pReaders, nReaders); | 5107 rc = docListMerge(&pWriter->data, pReaders, nReaders); |
| 5108 if( rc!=SQLITE_OK ) return rc; |
| 4920 ASSERT_VALID_DOCLIST(DL_DEFAULT, | 5109 ASSERT_VALID_DOCLIST(DL_DEFAULT, |
| 4921 pWriter->data.pData+iDoclistData+n, | 5110 pWriter->data.pData+iDoclistData+n, |
| 4922 pWriter->data.nData-iDoclistData-n, NULL); | 5111 pWriter->data.nData-iDoclistData-n, NULL); |
| 4923 | 5112 |
| 4924 /* The actual amount of doclist data at this point could be smaller | 5113 /* The actual amount of doclist data at this point could be smaller |
| 4925 ** than the length we encoded. Additionally, the space required to | 5114 ** than the length we encoded. Additionally, the space required to |
| 4926 ** encode this length could be smaller. For small doclists, this is | 5115 ** encode this length could be smaller. For small doclists, this is |
| 4927 ** not a big deal, we can just use memmove() to adjust things. | 5116 ** not a big deal, we can just use memmove() to adjust things. |
| 4928 */ | 5117 */ |
| 4929 nActualData = pWriter->data.nData-(iDoclistData+n); | 5118 nActualData = pWriter->data.nData-(iDoclistData+n); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5019 */ | 5208 */ |
| 5020 /* TODO(shess) Revise writeZeroSegment() so that doclists are | 5209 /* TODO(shess) Revise writeZeroSegment() so that doclists are |
| 5021 ** constructed directly in pWriter->data. | 5210 ** constructed directly in pWriter->data. |
| 5022 */ | 5211 */ |
| 5023 static int leafWriterStep(fulltext_vtab *v, LeafWriter *pWriter, | 5212 static int leafWriterStep(fulltext_vtab *v, LeafWriter *pWriter, |
| 5024 const char *pTerm, int nTerm, | 5213 const char *pTerm, int nTerm, |
| 5025 const char *pData, int nData){ | 5214 const char *pData, int nData){ |
| 5026 int rc; | 5215 int rc; |
| 5027 DLReader reader; | 5216 DLReader reader; |
| 5028 | 5217 |
| 5029 dlrInit(&reader, DL_DEFAULT, pData, nData); | 5218 rc = dlrInit(&reader, DL_DEFAULT, pData, nData); |
| 5219 if( rc!=SQLITE_OK ) return rc; |
| 5030 rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1); | 5220 rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1); |
| 5031 dlrDestroy(&reader); | 5221 dlrDestroy(&reader); |
| 5032 | 5222 |
| 5033 return rc; | 5223 return rc; |
| 5034 } | 5224 } |
| 5035 | 5225 |
| 5036 | 5226 |
| 5037 /****************************************************************/ | 5227 /****************************************************************/ |
| 5038 /* LeafReader is used to iterate over an individual leaf node. */ | 5228 /* LeafReader is used to iterate over an individual leaf node. */ |
| 5039 typedef struct LeafReader { | 5229 typedef struct LeafReader { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 5064 /* Access the doclist data for the current term. */ | 5254 /* Access the doclist data for the current term. */ |
| 5065 static int leafReaderDataBytes(LeafReader *pReader){ | 5255 static int leafReaderDataBytes(LeafReader *pReader){ |
| 5066 int nData; | 5256 int nData; |
| 5067 assert( pReader->term.nData>0 ); | 5257 assert( pReader->term.nData>0 ); |
| 5068 fts3GetVarint32(pReader->pData, &nData); | 5258 fts3GetVarint32(pReader->pData, &nData); |
| 5069 return nData; | 5259 return nData; |
| 5070 } | 5260 } |
| 5071 static const char *leafReaderData(LeafReader *pReader){ | 5261 static const char *leafReaderData(LeafReader *pReader){ |
| 5072 int n, nData; | 5262 int n, nData; |
| 5073 assert( pReader->term.nData>0 ); | 5263 assert( pReader->term.nData>0 ); |
| 5074 n = fts3GetVarint32(pReader->pData, &nData); | 5264 n = fts3GetVarint32Safe(pReader->pData, &nData, pReader->nData); |
| 5265 if( !n || nData>pReader->nData-n ) return NULL; |
| 5075 return pReader->pData+n; | 5266 return pReader->pData+n; |
| 5076 } | 5267 } |
| 5077 | 5268 |
| 5078 static void leafReaderInit(const char *pData, int nData, | 5269 static int leafReaderInit(const char *pData, int nData, |
| 5079 LeafReader *pReader){ | 5270 LeafReader *pReader){ |
| 5080 int nTerm, n; | 5271 int nTerm, n; |
| 5081 | 5272 |
| 5273 /* All callers check this precondition. */ |
| 5082 assert( nData>0 ); | 5274 assert( nData>0 ); |
| 5083 assert( pData[0]=='\0' ); | 5275 assert( pData[0]=='\0' ); |
| 5084 | 5276 |
| 5085 CLEAR(pReader); | 5277 CLEAR(pReader); |
| 5086 | 5278 |
| 5087 /* Read the first term, skipping the header byte. */ | 5279 /* Read the first term, skipping the header byte. */ |
| 5088 n = fts3GetVarint32(pData+1, &nTerm); | 5280 n = fts3GetVarint32Safe(pData+1, &nTerm, nData-1); |
| 5281 if( !n || nTerm<0 || nTerm>nData-1-n ) return SQLITE_CORRUPT_BKPT; |
| 5089 dataBufferInit(&pReader->term, nTerm); | 5282 dataBufferInit(&pReader->term, nTerm); |
| 5090 dataBufferReplace(&pReader->term, pData+1+n, nTerm); | 5283 dataBufferReplace(&pReader->term, pData+1+n, nTerm); |
| 5091 | 5284 |
| 5092 /* Position after the first term. */ | 5285 /* Position after the first term. */ |
| 5093 assert( 1+n+nTerm<nData ); | |
| 5094 pReader->pData = pData+1+n+nTerm; | 5286 pReader->pData = pData+1+n+nTerm; |
| 5095 pReader->nData = nData-1-n-nTerm; | 5287 pReader->nData = nData-1-n-nTerm; |
| 5288 return SQLITE_OK; |
| 5096 } | 5289 } |
| 5097 | 5290 |
| 5098 /* Step the reader forward to the next term. */ | 5291 /* Step the reader forward to the next term. */ |
| 5099 static void leafReaderStep(LeafReader *pReader){ | 5292 static int leafReaderStep(LeafReader *pReader){ |
| 5100 int n, nData, nPrefix, nSuffix; | 5293 int n, nData, nPrefix, nSuffix; |
| 5101 assert( !leafReaderAtEnd(pReader) ); | 5294 assert( !leafReaderAtEnd(pReader) ); |
| 5102 | 5295 |
| 5103 /* Skip previous entry's data block. */ | 5296 /* Skip previous entry's data block. */ |
| 5104 n = fts3GetVarint32(pReader->pData, &nData); | 5297 n = fts3GetVarint32Safe(pReader->pData, &nData, pReader->nData); |
| 5105 assert( n+nData<=pReader->nData ); | 5298 if( !n || nData<0 || nData>pReader->nData-n ) return SQLITE_CORRUPT_BKPT; |
| 5106 pReader->pData += n+nData; | 5299 pReader->pData += n+nData; |
| 5107 pReader->nData -= n+nData; | 5300 pReader->nData -= n+nData; |
| 5108 | 5301 |
| 5109 if( !leafReaderAtEnd(pReader) ){ | 5302 if( !leafReaderAtEnd(pReader) ){ |
| 5110 /* Construct the new term using a prefix from the old term plus a | 5303 /* Construct the new term using a prefix from the old term plus a |
| 5111 ** suffix from the leaf data. | 5304 ** suffix from the leaf data. |
| 5112 */ | 5305 */ |
| 5113 n = fts3GetVarint32(pReader->pData, &nPrefix); | 5306 n = fts3GetVarint32Safe(pReader->pData, &nPrefix, pReader->nData); |
| 5114 n += fts3GetVarint32(pReader->pData+n, &nSuffix); | 5307 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 5115 assert( n+nSuffix<pReader->nData ); | 5308 pReader->nData -= n; |
| 5309 pReader->pData += n; |
| 5310 n = fts3GetVarint32Safe(pReader->pData, &nSuffix, pReader->nData); |
| 5311 if( !n ) return SQLITE_CORRUPT_BKPT; |
| 5312 pReader->nData -= n; |
| 5313 pReader->pData += n; |
| 5314 if( nSuffix<0 || nSuffix>pReader->nData ) return SQLITE_CORRUPT_BKPT; |
| 5315 if( nPrefix<0 || nPrefix>pReader->term.nData ) return SQLITE_CORRUPT_BKPT; |
| 5116 pReader->term.nData = nPrefix; | 5316 pReader->term.nData = nPrefix; |
| 5117 dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); | 5317 dataBufferAppend(&pReader->term, pReader->pData, nSuffix); |
| 5118 | 5318 |
| 5119 pReader->pData += n+nSuffix; | 5319 pReader->pData += nSuffix; |
| 5120 pReader->nData -= n+nSuffix; | 5320 pReader->nData -= nSuffix; |
| 5121 } | 5321 } |
| 5322 return SQLITE_OK; |
| 5122 } | 5323 } |
| 5123 | 5324 |
| 5124 /* strcmp-style comparison of pReader's current term against pTerm. | 5325 /* strcmp-style comparison of pReader's current term against pTerm. |
| 5125 ** If isPrefix, equality means equal through nTerm bytes. | 5326 ** If isPrefix, equality means equal through nTerm bytes. |
| 5126 */ | 5327 */ |
| 5127 static int leafReaderTermCmp(LeafReader *pReader, | 5328 static int leafReaderTermCmp(LeafReader *pReader, |
| 5128 const char *pTerm, int nTerm, int isPrefix){ | 5329 const char *pTerm, int nTerm, int isPrefix){ |
| 5129 int c, n = pReader->term.nData<nTerm ? pReader->term.nData : nTerm; | 5330 int c, n = pReader->term.nData<nTerm ? pReader->term.nData : nTerm; |
| 5130 if( n==0 ){ | 5331 if( n==0 ){ |
| 5131 if( pReader->term.nData>0 ) return -1; | 5332 if( pReader->term.nData>0 ) return -1; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5215 int idx, | 5416 int idx, |
| 5216 sqlite_int64 iStartBlockid, | 5417 sqlite_int64 iStartBlockid, |
| 5217 sqlite_int64 iEndBlockid, | 5418 sqlite_int64 iEndBlockid, |
| 5218 const char *pRootData, int nRootData, | 5419 const char *pRootData, int nRootData, |
| 5219 LeavesReader *pReader){ | 5420 LeavesReader *pReader){ |
| 5220 CLEAR(pReader); | 5421 CLEAR(pReader); |
| 5221 pReader->idx = idx; | 5422 pReader->idx = idx; |
| 5222 | 5423 |
| 5223 dataBufferInit(&pReader->rootData, 0); | 5424 dataBufferInit(&pReader->rootData, 0); |
| 5224 if( iStartBlockid==0 ){ | 5425 if( iStartBlockid==0 ){ |
| 5426 int rc; |
| 5427 /* Corrupt if this can't be a leaf node. */ |
| 5428 if( pRootData==NULL || nRootData<1 || pRootData[0]!='\0' ){ |
| 5429 return SQLITE_CORRUPT_BKPT; |
| 5430 } |
| 5225 /* Entire leaf level fit in root data. */ | 5431 /* Entire leaf level fit in root data. */ |
| 5226 dataBufferReplace(&pReader->rootData, pRootData, nRootData); | 5432 dataBufferReplace(&pReader->rootData, pRootData, nRootData); |
| 5227 leafReaderInit(pReader->rootData.pData, pReader->rootData.nData, | 5433 rc = leafReaderInit(pReader->rootData.pData, pReader->rootData.nData, |
| 5228 &pReader->leafReader); | 5434 &pReader->leafReader); |
| 5435 if( rc!=SQLITE_OK ){ |
| 5436 dataBufferDestroy(&pReader->rootData); |
| 5437 return rc; |
| 5438 } |
| 5229 }else{ | 5439 }else{ |
| 5230 sqlite3_stmt *s; | 5440 sqlite3_stmt *s; |
| 5231 int rc = sql_get_leaf_statement(v, idx, &s); | 5441 int rc = sql_get_leaf_statement(v, idx, &s); |
| 5232 if( rc!=SQLITE_OK ) return rc; | 5442 if( rc!=SQLITE_OK ) return rc; |
| 5233 | 5443 |
| 5234 rc = sqlite3_bind_int64(s, 1, iStartBlockid); | 5444 rc = sqlite3_bind_int64(s, 1, iStartBlockid); |
| 5235 if( rc!=SQLITE_OK ) return rc; | 5445 if( rc!=SQLITE_OK ) goto err; |
| 5236 | 5446 |
| 5237 rc = sqlite3_bind_int64(s, 2, iEndBlockid); | 5447 rc = sqlite3_bind_int64(s, 2, iEndBlockid); |
| 5238 if( rc!=SQLITE_OK ) return rc; | 5448 if( rc!=SQLITE_OK ) goto err; |
| 5239 | 5449 |
| 5240 rc = sqlite3_step(s); | 5450 rc = sqlite3_step(s); |
| 5451 |
| 5452 /* Corrupt if interior node referenced missing leaf node. */ |
| 5241 if( rc==SQLITE_DONE ){ | 5453 if( rc==SQLITE_DONE ){ |
| 5242 pReader->eof = 1; | 5454 rc = SQLITE_CORRUPT_BKPT; |
| 5243 return SQLITE_OK; | 5455 goto err; |
| 5244 } | 5456 } |
| 5245 if( rc!=SQLITE_ROW ) return rc; | |
| 5246 | 5457 |
| 5247 pReader->pStmt = s; | 5458 if( rc!=SQLITE_ROW ) goto err; |
| 5248 leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), | 5459 rc = SQLITE_OK; |
| 5249 sqlite3_column_bytes(pReader->pStmt, 0), | 5460 |
| 5250 &pReader->leafReader); | 5461 /* Corrupt if leaf data isn't a blob. */ |
| 5462 if( sqlite3_column_type(s, 0)!=SQLITE_BLOB ){ |
| 5463 rc = SQLITE_CORRUPT_BKPT; |
| 5464 }else{ |
| 5465 const char *pLeafData = sqlite3_column_blob(s, 0); |
| 5466 int nLeafData = sqlite3_column_bytes(s, 0); |
| 5467 |
| 5468 /* Corrupt if this can't be a leaf node. */ |
| 5469 if( pLeafData==NULL || nLeafData<1 || pLeafData[0]!='\0' ){ |
| 5470 rc = SQLITE_CORRUPT_BKPT; |
| 5471 }else{ |
| 5472 rc = leafReaderInit(pLeafData, nLeafData, &pReader->leafReader); |
| 5473 } |
| 5474 } |
| 5475 |
| 5476 err: |
| 5477 if( rc!=SQLITE_OK ){ |
| 5478 if( idx==-1 ){ |
| 5479 sqlite3_finalize(s); |
| 5480 }else{ |
| 5481 sqlite3_reset(s); |
| 5482 } |
| 5483 return rc; |
| 5484 } |
| 5251 } | 5485 } |
| 5252 return SQLITE_OK; | 5486 return SQLITE_OK; |
| 5253 } | 5487 } |
| 5254 | 5488 |
| 5255 /* Step the current leaf forward to the next term. If we reach the | 5489 /* Step the current leaf forward to the next term. If we reach the |
| 5256 ** end of the current leaf, step forward to the next leaf block. | 5490 ** end of the current leaf, step forward to the next leaf block. |
| 5257 */ | 5491 */ |
| 5258 static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){ | 5492 static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){ |
| 5493 int rc; |
| 5259 assert( !leavesReaderAtEnd(pReader) ); | 5494 assert( !leavesReaderAtEnd(pReader) ); |
| 5260 leafReaderStep(&pReader->leafReader); | 5495 rc = leafReaderStep(&pReader->leafReader); |
| 5496 if( rc!=SQLITE_OK ) return rc; |
| 5261 | 5497 |
| 5262 if( leafReaderAtEnd(&pReader->leafReader) ){ | 5498 if( leafReaderAtEnd(&pReader->leafReader) ){ |
| 5263 int rc; | |
| 5264 if( pReader->rootData.pData ){ | 5499 if( pReader->rootData.pData ){ |
| 5265 pReader->eof = 1; | 5500 pReader->eof = 1; |
| 5266 return SQLITE_OK; | 5501 return SQLITE_OK; |
| 5267 } | 5502 } |
| 5268 rc = sqlite3_step(pReader->pStmt); | 5503 rc = sqlite3_step(pReader->pStmt); |
| 5269 if( rc!=SQLITE_ROW ){ | 5504 if( rc!=SQLITE_ROW ){ |
| 5270 pReader->eof = 1; | 5505 pReader->eof = 1; |
| 5271 return rc==SQLITE_DONE ? SQLITE_OK : rc; | 5506 return rc==SQLITE_DONE ? SQLITE_OK : rc; |
| 5272 } | 5507 } |
| 5273 leafReaderDestroy(&pReader->leafReader); | 5508 |
| 5274 leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), | 5509 /* Corrupt if leaf data isn't a blob. */ |
| 5275 sqlite3_column_bytes(pReader->pStmt, 0), | 5510 if( sqlite3_column_type(pReader->pStmt, 0)!=SQLITE_BLOB ){ |
| 5276 &pReader->leafReader); | 5511 return SQLITE_CORRUPT_BKPT; |
| 5512 }else{ |
| 5513 LeafReader tmp; |
| 5514 const char *pLeafData = sqlite3_column_blob(pReader->pStmt, 0); |
| 5515 int nLeafData = sqlite3_column_bytes(pReader->pStmt, 0); |
| 5516 |
| 5517 /* Corrupt if this can't be a leaf node. */ |
| 5518 if( pLeafData==NULL || nLeafData<1 || pLeafData[0]!='\0' ){ |
| 5519 return SQLITE_CORRUPT_BKPT; |
| 5520 } |
| 5521 |
| 5522 rc = leafReaderInit(pLeafData, nLeafData, &tmp); |
| 5523 if( rc!=SQLITE_OK ) return rc; |
| 5524 leafReaderDestroy(&pReader->leafReader); |
| 5525 pReader->leafReader = tmp; |
| 5526 } |
| 5277 } | 5527 } |
| 5278 return SQLITE_OK; | 5528 return SQLITE_OK; |
| 5279 } | 5529 } |
| 5280 | 5530 |
| 5281 /* Order LeavesReaders by their term, ignoring idx. Readers at eof | 5531 /* Order LeavesReaders by their term, ignoring idx. Readers at eof |
| 5282 ** always sort to the end. | 5532 ** always sort to the end. |
| 5283 */ | 5533 */ |
| 5284 static int leavesReaderTermCmp(LeavesReader *lr1, LeavesReader *lr2){ | 5534 static int leavesReaderTermCmp(LeavesReader *lr1, LeavesReader *lr2){ |
| 5285 if( leavesReaderAtEnd(lr1) ){ | 5535 if( leavesReaderAtEnd(lr1) ){ |
| 5286 if( leavesReaderAtEnd(lr2) ) return 0; | 5536 if( leavesReaderAtEnd(lr2) ) return 0; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5327 | 5577 |
| 5328 rc = sqlite3_bind_int(s, 1, iLevel); | 5578 rc = sqlite3_bind_int(s, 1, iLevel); |
| 5329 if( rc!=SQLITE_OK ) return rc; | 5579 if( rc!=SQLITE_OK ) return rc; |
| 5330 | 5580 |
| 5331 i = 0; | 5581 i = 0; |
| 5332 while( (rc = sqlite3_step(s))==SQLITE_ROW ){ | 5582 while( (rc = sqlite3_step(s))==SQLITE_ROW ){ |
| 5333 sqlite_int64 iStart = sqlite3_column_int64(s, 0); | 5583 sqlite_int64 iStart = sqlite3_column_int64(s, 0); |
| 5334 sqlite_int64 iEnd = sqlite3_column_int64(s, 1); | 5584 sqlite_int64 iEnd = sqlite3_column_int64(s, 1); |
| 5335 const char *pRootData = sqlite3_column_blob(s, 2); | 5585 const char *pRootData = sqlite3_column_blob(s, 2); |
| 5336 int nRootData = sqlite3_column_bytes(s, 2); | 5586 int nRootData = sqlite3_column_bytes(s, 2); |
| 5587 sqlite_int64 iIndex = sqlite3_column_int64(s, 3); |
| 5337 | 5588 |
| 5338 assert( i<MERGE_COUNT ); | 5589 /* Corrupt if we get back different types than we stored. */ |
| 5590 /* Also corrupt if the index is not sequential starting at 0. */ |
| 5591 if( sqlite3_column_type(s, 0)!=SQLITE_INTEGER || |
| 5592 sqlite3_column_type(s, 1)!=SQLITE_INTEGER || |
| 5593 sqlite3_column_type(s, 2)!=SQLITE_BLOB || |
| 5594 i!=iIndex || |
| 5595 i>=MERGE_COUNT ){ |
| 5596 rc = SQLITE_CORRUPT_BKPT; |
| 5597 break; |
| 5598 } |
| 5599 |
| 5339 rc = leavesReaderInit(v, i, iStart, iEnd, pRootData, nRootData, | 5600 rc = leavesReaderInit(v, i, iStart, iEnd, pRootData, nRootData, |
| 5340 &pReaders[i]); | 5601 &pReaders[i]); |
| 5341 if( rc!=SQLITE_OK ) break; | 5602 if( rc!=SQLITE_OK ) break; |
| 5342 | 5603 |
| 5343 i++; | 5604 i++; |
| 5344 } | 5605 } |
| 5345 if( rc!=SQLITE_DONE ){ | 5606 if( rc!=SQLITE_DONE ){ |
| 5346 while( i-->0 ){ | 5607 while( i-->0 ){ |
| 5347 leavesReaderDestroy(&pReaders[i]); | 5608 leavesReaderDestroy(&pReaders[i]); |
| 5348 } | 5609 } |
| 5610 sqlite3_reset(s); /* So we don't leave a lock. */ |
| 5349 return rc; | 5611 return rc; |
| 5350 } | 5612 } |
| 5351 | 5613 |
| 5352 *piReaders = i; | 5614 *piReaders = i; |
| 5353 | 5615 |
| 5354 /* Leave our results sorted by term, then age. */ | 5616 /* Leave our results sorted by term, then age. */ |
| 5355 while( i-- ){ | 5617 while( i-- ){ |
| 5356 leavesReaderReorder(pReaders+i, *piReaders-i); | 5618 leavesReaderReorder(pReaders+i, *piReaders-i); |
| 5357 } | 5619 } |
| 5358 return SQLITE_OK; | 5620 return SQLITE_OK; |
| 5359 } | 5621 } |
| 5360 | 5622 |
| 5361 /* Merge doclists from pReaders[nReaders] into a single doclist, which | 5623 /* Merge doclists from pReaders[nReaders] into a single doclist, which |
| 5362 ** is written to pWriter. Assumes pReaders is ordered oldest to | 5624 ** is written to pWriter. Assumes pReaders is ordered oldest to |
| 5363 ** newest. | 5625 ** newest. |
| 5364 */ | 5626 */ |
| 5365 /* TODO(shess) Consider putting this inline in segmentMerge(). */ | 5627 /* TODO(shess) Consider putting this inline in segmentMerge(). */ |
| 5366 static int leavesReadersMerge(fulltext_vtab *v, | 5628 static int leavesReadersMerge(fulltext_vtab *v, |
| 5367 LeavesReader *pReaders, int nReaders, | 5629 LeavesReader *pReaders, int nReaders, |
| 5368 LeafWriter *pWriter){ | 5630 LeafWriter *pWriter){ |
| 5369 DLReader dlReaders[MERGE_COUNT]; | 5631 DLReader dlReaders[MERGE_COUNT]; |
| 5370 const char *pTerm = leavesReaderTerm(pReaders); | 5632 const char *pTerm = leavesReaderTerm(pReaders); |
| 5371 int i, nTerm = leavesReaderTermBytes(pReaders); | 5633 int i, nTerm = leavesReaderTermBytes(pReaders); |
| 5634 int rc; |
| 5372 | 5635 |
| 5373 assert( nReaders<=MERGE_COUNT ); | 5636 assert( nReaders<=MERGE_COUNT ); |
| 5374 | 5637 |
| 5375 for(i=0; i<nReaders; i++){ | 5638 for(i=0; i<nReaders; i++){ |
| 5376 dlrInit(&dlReaders[i], DL_DEFAULT, | 5639 const char *pData = leavesReaderData(pReaders+i); |
| 5377 leavesReaderData(pReaders+i), | 5640 if( pData==NULL ){ |
| 5378 leavesReaderDataBytes(pReaders+i)); | 5641 rc = SQLITE_CORRUPT_BKPT; |
| 5642 break; |
| 5643 } |
| 5644 rc = dlrInit(&dlReaders[i], DL_DEFAULT, |
| 5645 pData, |
| 5646 leavesReaderDataBytes(pReaders+i)); |
| 5647 if( rc!=SQLITE_OK ) break; |
| 5648 } |
| 5649 if( rc!=SQLITE_OK ){ |
| 5650 while( i-->0 ){ |
| 5651 dlrDestroy(&dlReaders[i]); |
| 5652 } |
| 5653 return rc; |
| 5379 } | 5654 } |
| 5380 | 5655 |
| 5381 return leafWriterStepMerge(v, pWriter, pTerm, nTerm, dlReaders, nReaders); | 5656 return leafWriterStepMerge(v, pWriter, pTerm, nTerm, dlReaders, nReaders); |
| 5382 } | 5657 } |
| 5383 | 5658 |
| 5384 /* Forward ref due to mutual recursion with segdirNextIndex(). */ | 5659 /* Forward ref due to mutual recursion with segdirNextIndex(). */ |
| 5385 static int segmentMerge(fulltext_vtab *v, int iLevel); | 5660 static int segmentMerge(fulltext_vtab *v, int iLevel); |
| 5386 | 5661 |
| 5387 /* Put the next available index at iLevel into *pidx. If iLevel | 5662 /* Put the next available index at iLevel into *pidx. If iLevel |
| 5388 ** already has MERGE_COUNT segments, they are merged to a higher | 5663 ** already has MERGE_COUNT segments, they are merged to a higher |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5422 if( rc!=SQLITE_OK ) return rc; | 5697 if( rc!=SQLITE_OK ) return rc; |
| 5423 | 5698 |
| 5424 /* TODO(shess) This assumes that we'll always see exactly | 5699 /* TODO(shess) This assumes that we'll always see exactly |
| 5425 ** MERGE_COUNT segments to merge at a given level. That will be | 5700 ** MERGE_COUNT segments to merge at a given level. That will be |
| 5426 ** broken if we allow the developer to request preemptive or | 5701 ** broken if we allow the developer to request preemptive or |
| 5427 ** deferred merging. | 5702 ** deferred merging. |
| 5428 */ | 5703 */ |
| 5429 memset(&lrs, '\0', sizeof(lrs)); | 5704 memset(&lrs, '\0', sizeof(lrs)); |
| 5430 rc = leavesReadersInit(v, iLevel, lrs, &i); | 5705 rc = leavesReadersInit(v, iLevel, lrs, &i); |
| 5431 if( rc!=SQLITE_OK ) return rc; | 5706 if( rc!=SQLITE_OK ) return rc; |
| 5432 assert( i==MERGE_COUNT ); | |
| 5433 | 5707 |
| 5434 leafWriterInit(iLevel+1, idx, &writer); | 5708 leafWriterInit(iLevel+1, idx, &writer); |
| 5435 | 5709 |
| 5710 if( i!=MERGE_COUNT ){ |
| 5711 rc = SQLITE_CORRUPT_BKPT; |
| 5712 goto err; |
| 5713 } |
| 5714 |
| 5436 /* Since leavesReaderReorder() pushes readers at eof to the end, | 5715 /* Since leavesReaderReorder() pushes readers at eof to the end, |
| 5437 ** when the first reader is empty, all will be empty. | 5716 ** when the first reader is empty, all will be empty. |
| 5438 */ | 5717 */ |
| 5439 while( !leavesReaderAtEnd(lrs) ){ | 5718 while( !leavesReaderAtEnd(lrs) ){ |
| 5440 /* Figure out how many readers share their next term. */ | 5719 /* Figure out how many readers share their next term. */ |
| 5441 for(i=1; i<MERGE_COUNT && !leavesReaderAtEnd(lrs+i); i++){ | 5720 for(i=1; i<MERGE_COUNT && !leavesReaderAtEnd(lrs+i); i++){ |
| 5442 if( 0!=leavesReaderTermCmp(lrs, lrs+i) ) break; | 5721 if( 0!=leavesReaderTermCmp(lrs, lrs+i) ) break; |
| 5443 } | 5722 } |
| 5444 | 5723 |
| 5445 rc = leavesReadersMerge(v, lrs, i, &writer); | 5724 rc = leavesReadersMerge(v, lrs, i, &writer); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 5468 | 5747 |
| 5469 err: | 5748 err: |
| 5470 for(i=0; i<MERGE_COUNT; i++){ | 5749 for(i=0; i<MERGE_COUNT; i++){ |
| 5471 leavesReaderDestroy(&lrs[i]); | 5750 leavesReaderDestroy(&lrs[i]); |
| 5472 } | 5751 } |
| 5473 leafWriterDestroy(&writer); | 5752 leafWriterDestroy(&writer); |
| 5474 return rc; | 5753 return rc; |
| 5475 } | 5754 } |
| 5476 | 5755 |
| 5477 /* Accumulate the union of *acc and *pData into *acc. */ | 5756 /* Accumulate the union of *acc and *pData into *acc. */ |
| 5478 static void docListAccumulateUnion(DataBuffer *acc, | 5757 static int docListAccumulateUnion(DataBuffer *acc, |
| 5479 const char *pData, int nData) { | 5758 const char *pData, int nData) { |
| 5480 DataBuffer tmp = *acc; | 5759 DataBuffer tmp = *acc; |
| 5760 int rc; |
| 5481 dataBufferInit(acc, tmp.nData+nData); | 5761 dataBufferInit(acc, tmp.nData+nData); |
| 5482 docListUnion(tmp.pData, tmp.nData, pData, nData, acc); | 5762 rc = docListUnion(tmp.pData, tmp.nData, pData, nData, acc); |
| 5483 dataBufferDestroy(&tmp); | 5763 dataBufferDestroy(&tmp); |
| 5764 return rc; |
| 5484 } | 5765 } |
| 5485 | 5766 |
| 5486 /* TODO(shess) It might be interesting to explore different merge | 5767 /* TODO(shess) It might be interesting to explore different merge |
| 5487 ** strategies, here. For instance, since this is a sorted merge, we | 5768 ** strategies, here. For instance, since this is a sorted merge, we |
| 5488 ** could easily merge many doclists in parallel. With some | 5769 ** could easily merge many doclists in parallel. With some |
| 5489 ** comprehension of the storage format, we could merge all of the | 5770 ** comprehension of the storage format, we could merge all of the |
| 5490 ** doclists within a leaf node directly from the leaf node's storage. | 5771 ** doclists within a leaf node directly from the leaf node's storage. |
| 5491 ** It may be worthwhile to merge smaller doclists before larger | 5772 ** It may be worthwhile to merge smaller doclists before larger |
| 5492 ** doclists, since they can be traversed more quickly - but the | 5773 ** doclists, since they can be traversed more quickly - but the |
| 5493 ** results may have less overlap, making them more expensive in a | 5774 ** results may have less overlap, making them more expensive in a |
| (...skipping 21 matching lines...) Expand all Loading... |
| 5515 for(rc=SQLITE_OK; rc==SQLITE_OK && !leavesReaderAtEnd(pReader); | 5796 for(rc=SQLITE_OK; rc==SQLITE_OK && !leavesReaderAtEnd(pReader); |
| 5516 rc=leavesReaderStep(v, pReader)){ | 5797 rc=leavesReaderStep(v, pReader)){ |
| 5517 /* TODO(shess) Really want leavesReaderTermCmp(), but that name is | 5798 /* TODO(shess) Really want leavesReaderTermCmp(), but that name is |
| 5518 ** already taken to compare the terms of two LeavesReaders. Think | 5799 ** already taken to compare the terms of two LeavesReaders. Think |
| 5519 ** on a better name. [Meanwhile, break encapsulation rather than | 5800 ** on a better name. [Meanwhile, break encapsulation rather than |
| 5520 ** use a confusing name.] | 5801 ** use a confusing name.] |
| 5521 */ | 5802 */ |
| 5522 int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix); | 5803 int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix); |
| 5523 if( c>0 ) break; /* Past any possible matches. */ | 5804 if( c>0 ) break; /* Past any possible matches. */ |
| 5524 if( c==0 ){ | 5805 if( c==0 ){ |
| 5806 int iBuffer, nData; |
| 5525 const char *pData = leavesReaderData(pReader); | 5807 const char *pData = leavesReaderData(pReader); |
| 5526 int iBuffer, nData = leavesReaderDataBytes(pReader); | 5808 if( pData==NULL ){ |
| 5809 rc = SQLITE_CORRUPT_BKPT; |
| 5810 break; |
| 5811 } |
| 5812 nData = leavesReaderDataBytes(pReader); |
| 5527 | 5813 |
| 5528 /* Find the first empty buffer. */ | 5814 /* Find the first empty buffer. */ |
| 5529 for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){ | 5815 for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){ |
| 5530 if( 0==pBuffers[iBuffer].nData ) break; | 5816 if( 0==pBuffers[iBuffer].nData ) break; |
| 5531 } | 5817 } |
| 5532 | 5818 |
| 5533 /* Out of buffers, add an empty one. */ | 5819 /* Out of buffers, add an empty one. */ |
| 5534 if( iBuffer==nBuffers ){ | 5820 if( iBuffer==nBuffers ){ |
| 5535 if( nBuffers==nMaxBuffers ){ | 5821 if( nBuffers==nMaxBuffers ){ |
| 5536 DataBuffer *p; | 5822 DataBuffer *p; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 5562 dataBufferReplace(&(pBuffers[0]), pData, nData); | 5848 dataBufferReplace(&(pBuffers[0]), pData, nData); |
| 5563 }else{ | 5849 }else{ |
| 5564 /* pAcc is the empty buffer the merged data will end up in. */ | 5850 /* pAcc is the empty buffer the merged data will end up in. */ |
| 5565 DataBuffer *pAcc = &(pBuffers[iBuffer]); | 5851 DataBuffer *pAcc = &(pBuffers[iBuffer]); |
| 5566 DataBuffer *p = &(pBuffers[0]); | 5852 DataBuffer *p = &(pBuffers[0]); |
| 5567 | 5853 |
| 5568 /* Handle position 0 specially to avoid need to prime pAcc | 5854 /* Handle position 0 specially to avoid need to prime pAcc |
| 5569 ** with pData/nData. | 5855 ** with pData/nData. |
| 5570 */ | 5856 */ |
| 5571 dataBufferSwap(p, pAcc); | 5857 dataBufferSwap(p, pAcc); |
| 5572 docListAccumulateUnion(pAcc, pData, nData); | 5858 rc = docListAccumulateUnion(pAcc, pData, nData); |
| 5859 if( rc!=SQLITE_OK ) goto err; |
| 5573 | 5860 |
| 5574 /* Accumulate remaining doclists into pAcc. */ | 5861 /* Accumulate remaining doclists into pAcc. */ |
| 5575 for(++p; p<pAcc; ++p){ | 5862 for(++p; p<pAcc; ++p){ |
| 5576 docListAccumulateUnion(pAcc, p->pData, p->nData); | 5863 rc = docListAccumulateUnion(pAcc, p->pData, p->nData); |
| 5864 if( rc!=SQLITE_OK ) goto err; |
| 5577 | 5865 |
| 5578 /* dataBufferReset() could allow a large doclist to blow up | 5866 /* dataBufferReset() could allow a large doclist to blow up |
| 5579 ** our memory requirements. | 5867 ** our memory requirements. |
| 5580 */ | 5868 */ |
| 5581 if( p->nCapacity<1024 ){ | 5869 if( p->nCapacity<1024 ){ |
| 5582 dataBufferReset(p); | 5870 dataBufferReset(p); |
| 5583 }else{ | 5871 }else{ |
| 5584 dataBufferDestroy(p); | 5872 dataBufferDestroy(p); |
| 5585 dataBufferInit(p, 0); | 5873 dataBufferInit(p, 0); |
| 5586 } | 5874 } |
| 5587 } | 5875 } |
| 5588 } | 5876 } |
| 5589 } | 5877 } |
| 5590 } | 5878 } |
| 5591 | 5879 |
| 5592 /* Union all the doclists together into *out. */ | 5880 /* Union all the doclists together into *out. */ |
| 5593 /* TODO(shess) What if *out is big? Sigh. */ | 5881 /* TODO(shess) What if *out is big? Sigh. */ |
| 5594 if( rc==SQLITE_OK && nBuffers>0 ){ | 5882 if( rc==SQLITE_OK && nBuffers>0 ){ |
| 5595 int iBuffer; | 5883 int iBuffer; |
| 5596 for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){ | 5884 for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){ |
| 5597 if( pBuffers[iBuffer].nData>0 ){ | 5885 if( pBuffers[iBuffer].nData>0 ){ |
| 5598 if( out->nData==0 ){ | 5886 if( out->nData==0 ){ |
| 5599 dataBufferSwap(out, &(pBuffers[iBuffer])); | 5887 dataBufferSwap(out, &(pBuffers[iBuffer])); |
| 5600 }else{ | 5888 }else{ |
| 5601 docListAccumulateUnion(out, pBuffers[iBuffer].pData, | 5889 rc = docListAccumulateUnion(out, pBuffers[iBuffer].pData, |
| 5602 pBuffers[iBuffer].nData); | 5890 pBuffers[iBuffer].nData); |
| 5891 if( rc!=SQLITE_OK ) break; |
| 5603 } | 5892 } |
| 5604 } | 5893 } |
| 5605 } | 5894 } |
| 5606 } | 5895 } |
| 5607 | 5896 |
| 5897 err: |
| 5608 while( nBuffers-- ){ | 5898 while( nBuffers-- ){ |
| 5609 dataBufferDestroy(&(pBuffers[nBuffers])); | 5899 dataBufferDestroy(&(pBuffers[nBuffers])); |
| 5610 } | 5900 } |
| 5611 if( pBuffers!=NULL ) sqlite3_free(pBuffers); | 5901 if( pBuffers!=NULL ) sqlite3_free(pBuffers); |
| 5612 | 5902 |
| 5613 return rc; | 5903 return rc; |
| 5614 } | 5904 } |
| 5615 | 5905 |
| 5616 /* Call loadSegmentLeavesInt() with pData/nData as input. */ | 5906 /* Call loadSegmentLeavesInt() with pData/nData as input. */ |
| 5617 static int loadSegmentLeaf(fulltext_vtab *v, const char *pData, int nData, | 5907 static int loadSegmentLeaf(fulltext_vtab *v, const char *pData, int nData, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5656 ** nodes which could include pTerm/nTerm/isPrefix. Note that the | 5946 ** nodes which could include pTerm/nTerm/isPrefix. Note that the |
| 5657 ** interior node terms logically come between the blocks, so there is | 5947 ** interior node terms logically come between the blocks, so there is |
| 5658 ** one more blockid than there are terms (that block contains terms >= | 5948 ** one more blockid than there are terms (that block contains terms >= |
| 5659 ** the last interior-node term). | 5949 ** the last interior-node term). |
| 5660 */ | 5950 */ |
| 5661 /* TODO(shess) The calling code may already know that the end child is | 5951 /* TODO(shess) The calling code may already know that the end child is |
| 5662 ** not worth calculating, because the end may be in a later sibling | 5952 ** not worth calculating, because the end may be in a later sibling |
| 5663 ** node. Consider whether breaking symmetry is worthwhile. I suspect | 5953 ** node. Consider whether breaking symmetry is worthwhile. I suspect |
| 5664 ** it is not worthwhile. | 5954 ** it is not worthwhile. |
| 5665 */ | 5955 */ |
| 5666 static void getChildrenContaining(const char *pData, int nData, | 5956 static int getChildrenContaining(const char *pData, int nData, |
| 5667 const char *pTerm, int nTerm, int isPrefix, | 5957 const char *pTerm, int nTerm, int isPrefix, |
| 5668 sqlite_int64 *piStartChild, | 5958 sqlite_int64 *piStartChild, |
| 5669 sqlite_int64 *piEndChild){ | 5959 sqlite_int64 *piEndChild){ |
| 5670 InteriorReader reader; | 5960 InteriorReader reader; |
| 5961 int rc; |
| 5671 | 5962 |
| 5672 assert( nData>1 ); | 5963 assert( nData>1 ); |
| 5673 assert( *pData!='\0' ); | 5964 assert( *pData!='\0' ); |
| 5674 interiorReaderInit(pData, nData, &reader); | 5965 rc = interiorReaderInit(pData, nData, &reader); |
| 5966 if( rc!=SQLITE_OK ) return rc; |
| 5675 | 5967 |
| 5676 /* Scan for the first child which could contain pTerm/nTerm. */ | 5968 /* Scan for the first child which could contain pTerm/nTerm. */ |
| 5677 while( !interiorReaderAtEnd(&reader) ){ | 5969 while( !interiorReaderAtEnd(&reader) ){ |
| 5678 if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break; | 5970 if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break; |
| 5679 interiorReaderStep(&reader); | 5971 rc = interiorReaderStep(&reader); |
| 5972 if( rc!=SQLITE_OK ){ |
| 5973 interiorReaderDestroy(&reader); |
| 5974 return rc; |
| 5975 } |
| 5680 } | 5976 } |
| 5681 *piStartChild = interiorReaderCurrentBlockid(&reader); | 5977 *piStartChild = interiorReaderCurrentBlockid(&reader); |
| 5682 | 5978 |
| 5683 /* Keep scanning to find a term greater than our term, using prefix | 5979 /* Keep scanning to find a term greater than our term, using prefix |
| 5684 ** comparison if indicated. If isPrefix is false, this will be the | 5980 ** comparison if indicated. If isPrefix is false, this will be the |
| 5685 ** same blockid as the starting block. | 5981 ** same blockid as the starting block. |
| 5686 */ | 5982 */ |
| 5687 while( !interiorReaderAtEnd(&reader) ){ | 5983 while( !interiorReaderAtEnd(&reader) ){ |
| 5688 if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break; | 5984 if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break; |
| 5689 interiorReaderStep(&reader); | 5985 rc = interiorReaderStep(&reader); |
| 5986 if( rc!=SQLITE_OK ){ |
| 5987 interiorReaderDestroy(&reader); |
| 5988 return rc; |
| 5989 } |
| 5690 } | 5990 } |
| 5691 *piEndChild = interiorReaderCurrentBlockid(&reader); | 5991 *piEndChild = interiorReaderCurrentBlockid(&reader); |
| 5692 | 5992 |
| 5693 interiorReaderDestroy(&reader); | 5993 interiorReaderDestroy(&reader); |
| 5694 | 5994 |
| 5695 /* Children must ascend, and if !prefix, both must be the same. */ | 5995 /* Children must ascend, and if !prefix, both must be the same. */ |
| 5696 assert( *piEndChild>=*piStartChild ); | 5996 assert( *piEndChild>=*piStartChild ); |
| 5697 assert( isPrefix || *piStartChild==*piEndChild ); | 5997 assert( isPrefix || *piStartChild==*piEndChild ); |
| 5998 return rc; |
| 5698 } | 5999 } |
| 5699 | 6000 |
| 5700 /* Read block at iBlockid and pass it with other params to | 6001 /* Read block at iBlockid and pass it with other params to |
| 5701 ** getChildrenContaining(). | 6002 ** getChildrenContaining(). |
| 5702 */ | 6003 */ |
| 5703 static int loadAndGetChildrenContaining( | 6004 static int loadAndGetChildrenContaining( |
| 5704 fulltext_vtab *v, | 6005 fulltext_vtab *v, |
| 5705 sqlite_int64 iBlockid, | 6006 sqlite_int64 iBlockid, |
| 5706 const char *pTerm, int nTerm, int isPrefix, | 6007 const char *pTerm, int nTerm, int isPrefix, |
| 5707 sqlite_int64 *piStartChild, sqlite_int64 *piEndChild | 6008 sqlite_int64 *piStartChild, sqlite_int64 *piEndChild |
| 5708 ){ | 6009 ){ |
| 5709 sqlite3_stmt *s = NULL; | 6010 sqlite3_stmt *s = NULL; |
| 5710 int rc; | 6011 int rc; |
| 5711 | 6012 |
| 5712 assert( iBlockid!=0 ); | 6013 assert( iBlockid!=0 ); |
| 5713 assert( pTerm!=NULL ); | 6014 assert( pTerm!=NULL ); |
| 5714 assert( nTerm!=0 ); /* TODO(shess) Why not allow this? */ | 6015 assert( nTerm!=0 ); /* TODO(shess) Why not allow this? */ |
| 5715 assert( piStartChild!=NULL ); | 6016 assert( piStartChild!=NULL ); |
| 5716 assert( piEndChild!=NULL ); | 6017 assert( piEndChild!=NULL ); |
| 5717 | 6018 |
| 5718 rc = sql_get_statement(v, BLOCK_SELECT_STMT, &s); | 6019 rc = sql_get_statement(v, BLOCK_SELECT_STMT, &s); |
| 5719 if( rc!=SQLITE_OK ) return rc; | 6020 if( rc!=SQLITE_OK ) return rc; |
| 5720 | 6021 |
| 5721 rc = sqlite3_bind_int64(s, 1, iBlockid); | 6022 rc = sqlite3_bind_int64(s, 1, iBlockid); |
| 5722 if( rc!=SQLITE_OK ) return rc; | 6023 if( rc!=SQLITE_OK ) return rc; |
| 5723 | 6024 |
| 5724 rc = sqlite3_step(s); | 6025 rc = sqlite3_step(s); |
| 5725 if( rc==SQLITE_DONE ) return SQLITE_ERROR; | 6026 /* Corrupt if interior node references missing child node. */ |
| 6027 if( rc==SQLITE_DONE ) return SQLITE_CORRUPT_BKPT; |
| 5726 if( rc!=SQLITE_ROW ) return rc; | 6028 if( rc!=SQLITE_ROW ) return rc; |
| 5727 | 6029 |
| 5728 getChildrenContaining(sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0), | 6030 /* Corrupt if child node isn't a blob. */ |
| 5729 pTerm, nTerm, isPrefix, piStartChild, piEndChild); | 6031 if( sqlite3_column_type(s, 0)!=SQLITE_BLOB ){ |
| 6032 sqlite3_reset(s); /* So we don't leave a lock. */ |
| 6033 return SQLITE_CORRUPT_BKPT; |
| 6034 }else{ |
| 6035 const char *pData = sqlite3_column_blob(s, 0); |
| 6036 int nData = sqlite3_column_bytes(s, 0); |
| 6037 |
| 6038 /* Corrupt if child is not a valid interior node. */ |
| 6039 if( pData==NULL || nData<1 || pData[0]=='\0' ){ |
| 6040 sqlite3_reset(s); /* So we don't leave a lock. */ |
| 6041 return SQLITE_CORRUPT_BKPT; |
| 6042 } |
| 6043 |
| 6044 rc = getChildrenContaining(pData, nData, pTerm, nTerm, |
| 6045 isPrefix, piStartChild, piEndChild); |
| 6046 if( rc!=SQLITE_OK ){ |
| 6047 sqlite3_reset(s); |
| 6048 return rc; |
| 6049 } |
| 6050 } |
| 5730 | 6051 |
| 5731 /* We expect only one row. We must execute another sqlite3_step() | 6052 /* We expect only one row. We must execute another sqlite3_step() |
| 5732 * to complete the iteration; otherwise the table will remain | 6053 * to complete the iteration; otherwise the table will remain |
| 5733 * locked. */ | 6054 * locked. */ |
| 5734 rc = sqlite3_step(s); | 6055 rc = sqlite3_step(s); |
| 5735 if( rc==SQLITE_ROW ) return SQLITE_ERROR; | 6056 if( rc==SQLITE_ROW ) return SQLITE_ERROR; |
| 5736 if( rc!=SQLITE_DONE ) return rc; | 6057 if( rc!=SQLITE_DONE ) return rc; |
| 5737 | 6058 |
| 5738 return SQLITE_OK; | 6059 return SQLITE_OK; |
| 5739 } | 6060 } |
| 5740 | 6061 |
| 5741 /* Traverse the tree represented by pData[nData] looking for | 6062 /* Traverse the tree represented by pData[nData] looking for |
| 5742 ** pTerm[nTerm], placing its doclist into *out. This is internal to | 6063 ** pTerm[nTerm], placing its doclist into *out. This is internal to |
| 5743 ** loadSegment() to make error-handling cleaner. | 6064 ** loadSegment() to make error-handling cleaner. |
| 5744 */ | 6065 */ |
| 5745 static int loadSegmentInt(fulltext_vtab *v, const char *pData, int nData, | 6066 static int loadSegmentInt(fulltext_vtab *v, const char *pData, int nData, |
| 5746 sqlite_int64 iLeavesEnd, | 6067 sqlite_int64 iLeavesEnd, |
| 5747 const char *pTerm, int nTerm, int isPrefix, | 6068 const char *pTerm, int nTerm, int isPrefix, |
| 5748 DataBuffer *out){ | 6069 DataBuffer *out){ |
| 5749 /* Special case where root is a leaf. */ | 6070 /* Special case where root is a leaf. */ |
| 5750 if( *pData=='\0' ){ | 6071 if( *pData=='\0' ){ |
| 5751 return loadSegmentLeaf(v, pData, nData, pTerm, nTerm, isPrefix, out); | 6072 return loadSegmentLeaf(v, pData, nData, pTerm, nTerm, isPrefix, out); |
| 5752 }else{ | 6073 }else{ |
| 5753 int rc; | 6074 int rc; |
| 5754 sqlite_int64 iStartChild, iEndChild; | 6075 sqlite_int64 iStartChild, iEndChild; |
| 5755 | 6076 |
| 5756 /* Process pData as an interior node, then loop down the tree | 6077 /* Process pData as an interior node, then loop down the tree |
| 5757 ** until we find the set of leaf nodes to scan for the term. | 6078 ** until we find the set of leaf nodes to scan for the term. |
| 5758 */ | 6079 */ |
| 5759 getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix, | 6080 rc = getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix, |
| 5760 &iStartChild, &iEndChild); | 6081 &iStartChild, &iEndChild); |
| 6082 if( rc!=SQLITE_OK ) return rc; |
| 5761 while( iStartChild>iLeavesEnd ){ | 6083 while( iStartChild>iLeavesEnd ){ |
| 5762 sqlite_int64 iNextStart, iNextEnd; | 6084 sqlite_int64 iNextStart, iNextEnd; |
| 5763 rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix, | 6085 rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix, |
| 5764 &iNextStart, &iNextEnd); | 6086 &iNextStart, &iNextEnd); |
| 5765 if( rc!=SQLITE_OK ) return rc; | 6087 if( rc!=SQLITE_OK ) return rc; |
| 5766 | 6088 |
| 5767 /* If we've branched, follow the end branch, too. */ | 6089 /* If we've branched, follow the end branch, too. */ |
| 5768 if( iStartChild!=iEndChild ){ | 6090 if( iStartChild!=iEndChild ){ |
| 5769 sqlite_int64 iDummy; | 6091 sqlite_int64 iDummy; |
| 5770 rc = loadAndGetChildrenContaining(v, iEndChild, pTerm, nTerm, isPrefix, | 6092 rc = loadAndGetChildrenContaining(v, iEndChild, pTerm, nTerm, isPrefix, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5802 ** within a segment, with pairwise merges across segments, or across | 6124 ** within a segment, with pairwise merges across segments, or across |
| 5803 ** all segments at once. | 6125 ** all segments at once. |
| 5804 */ | 6126 */ |
| 5805 static int loadSegment(fulltext_vtab *v, const char *pData, int nData, | 6127 static int loadSegment(fulltext_vtab *v, const char *pData, int nData, |
| 5806 sqlite_int64 iLeavesEnd, | 6128 sqlite_int64 iLeavesEnd, |
| 5807 const char *pTerm, int nTerm, int isPrefix, | 6129 const char *pTerm, int nTerm, int isPrefix, |
| 5808 DataBuffer *out){ | 6130 DataBuffer *out){ |
| 5809 DataBuffer result; | 6131 DataBuffer result; |
| 5810 int rc; | 6132 int rc; |
| 5811 | 6133 |
| 5812 assert( nData>1 ); | 6134 /* Corrupt if segment root can't be valid. */ |
| 6135 if( pData==NULL || nData<1 ) return SQLITE_CORRUPT_BKPT; |
| 5813 | 6136 |
| 5814 /* This code should never be called with buffered updates. */ | 6137 /* This code should never be called with buffered updates. */ |
| 5815 assert( v->nPendingData<0 ); | 6138 assert( v->nPendingData<0 ); |
| 5816 | 6139 |
| 5817 dataBufferInit(&result, 0); | 6140 dataBufferInit(&result, 0); |
| 5818 rc = loadSegmentInt(v, pData, nData, iLeavesEnd, | 6141 rc = loadSegmentInt(v, pData, nData, iLeavesEnd, |
| 5819 pTerm, nTerm, isPrefix, &result); | 6142 pTerm, nTerm, isPrefix, &result); |
| 5820 if( rc==SQLITE_OK && result.nData>0 ){ | 6143 if( rc==SQLITE_OK && result.nData>0 ){ |
| 5821 if( out->nData==0 ){ | 6144 if( out->nData==0 ){ |
| 5822 DataBuffer tmp = *out; | 6145 DataBuffer tmp = *out; |
| 5823 *out = result; | 6146 *out = result; |
| 5824 result = tmp; | 6147 result = tmp; |
| 5825 }else{ | 6148 }else{ |
| 5826 DataBuffer merged; | 6149 DataBuffer merged; |
| 5827 DLReader readers[2]; | 6150 DLReader readers[2]; |
| 5828 | 6151 |
| 5829 dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData); | 6152 rc = dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData); |
| 5830 dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData); | 6153 if( rc==SQLITE_OK ){ |
| 5831 dataBufferInit(&merged, out->nData+result.nData); | 6154 rc = dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData); |
| 5832 docListMerge(&merged, readers, 2); | 6155 if( rc==SQLITE_OK ){ |
| 5833 dataBufferDestroy(out); | 6156 dataBufferInit(&merged, out->nData+result.nData); |
| 5834 *out = merged; | 6157 rc = docListMerge(&merged, readers, 2); |
| 5835 dlrDestroy(&readers[0]); | 6158 dataBufferDestroy(out); |
| 5836 dlrDestroy(&readers[1]); | 6159 *out = merged; |
| 6160 dlrDestroy(&readers[1]); |
| 6161 } |
| 6162 dlrDestroy(&readers[0]); |
| 6163 } |
| 5837 } | 6164 } |
| 5838 } | 6165 } |
| 6166 |
| 5839 dataBufferDestroy(&result); | 6167 dataBufferDestroy(&result); |
| 5840 return rc; | 6168 return rc; |
| 5841 } | 6169 } |
| 5842 | 6170 |
| 5843 /* Scan the database and merge together the posting lists for the term | 6171 /* Scan the database and merge together the posting lists for the term |
| 5844 ** into *out. | 6172 ** into *out. |
| 5845 */ | 6173 */ |
| 5846 static int termSelect( | 6174 static int termSelect( |
| 5847 fulltext_vtab *v, | 6175 fulltext_vtab *v, |
| 5848 int iColumn, | 6176 int iColumn, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 5862 dataBufferInit(&doclist, 0); | 6190 dataBufferInit(&doclist, 0); |
| 5863 dataBufferInit(out, 0); | 6191 dataBufferInit(out, 0); |
| 5864 | 6192 |
| 5865 /* Traverse the segments from oldest to newest so that newer doclist | 6193 /* Traverse the segments from oldest to newest so that newer doclist |
| 5866 ** elements for given docids overwrite older elements. | 6194 ** elements for given docids overwrite older elements. |
| 5867 */ | 6195 */ |
| 5868 while( (rc = sqlite3_step(s))==SQLITE_ROW ){ | 6196 while( (rc = sqlite3_step(s))==SQLITE_ROW ){ |
| 5869 const char *pData = sqlite3_column_blob(s, 2); | 6197 const char *pData = sqlite3_column_blob(s, 2); |
| 5870 const int nData = sqlite3_column_bytes(s, 2); | 6198 const int nData = sqlite3_column_bytes(s, 2); |
| 5871 const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1); | 6199 const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1); |
| 6200 |
| 6201 /* Corrupt if we get back different types than we stored. */ |
| 6202 if( sqlite3_column_type(s, 1)!=SQLITE_INTEGER || |
| 6203 sqlite3_column_type(s, 2)!=SQLITE_BLOB ){ |
| 6204 rc = SQLITE_CORRUPT_BKPT; |
| 6205 goto err; |
| 6206 } |
| 6207 |
| 5872 rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix, | 6208 rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix, |
| 5873 &doclist); | 6209 &doclist); |
| 5874 if( rc!=SQLITE_OK ) goto err; | 6210 if( rc!=SQLITE_OK ) goto err; |
| 5875 } | 6211 } |
| 5876 if( rc==SQLITE_DONE ){ | 6212 if( rc==SQLITE_DONE ){ |
| 6213 rc = SQLITE_OK; |
| 5877 if( doclist.nData!=0 ){ | 6214 if( doclist.nData!=0 ){ |
| 5878 /* TODO(shess) The old term_select_all() code applied the column | 6215 /* TODO(shess) The old term_select_all() code applied the column |
| 5879 ** restrict as we merged segments, leading to smaller buffers. | 6216 ** restrict as we merged segments, leading to smaller buffers. |
| 5880 ** This is probably worthwhile to bring back, once the new storage | 6217 ** This is probably worthwhile to bring back, once the new storage |
| 5881 ** system is checked in. | 6218 ** system is checked in. |
| 5882 */ | 6219 */ |
| 5883 if( iColumn==v->nColumn) iColumn = -1; | 6220 if( iColumn==v->nColumn) iColumn = -1; |
| 5884 docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, | 6221 rc = docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, |
| 5885 iColumn, iType, out); | 6222 iColumn, iType, out); |
| 5886 } | 6223 } |
| 5887 rc = SQLITE_OK; | |
| 5888 } | 6224 } |
| 5889 | 6225 |
| 5890 err: | 6226 err: |
| 6227 sqlite3_reset(s); /* So we don't leave a lock. */ |
| 5891 dataBufferDestroy(&doclist); | 6228 dataBufferDestroy(&doclist); |
| 5892 return rc; | 6229 return rc; |
| 5893 } | 6230 } |
| 5894 | 6231 |
| 5895 /****************************************************************/ | 6232 /****************************************************************/ |
| 5896 /* Used to hold hashtable data for sorting. */ | 6233 /* Used to hold hashtable data for sorting. */ |
| 5897 typedef struct TermData { | 6234 typedef struct TermData { |
| 5898 const char *pTerm; | 6235 const char *pTerm; |
| 5899 int nTerm; | 6236 int nTerm; |
| 5900 DLCollector *pCollector; | 6237 DLCollector *pCollector; |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6243 /* optimize() helper function. Put the readers in order and iterate | 6580 /* optimize() helper function. Put the readers in order and iterate |
| 6244 ** through them, merging doclists for matching terms into pWriter. | 6581 ** through them, merging doclists for matching terms into pWriter. |
| 6245 ** Returns SQLITE_OK on success, or the SQLite error code which | 6582 ** Returns SQLITE_OK on success, or the SQLite error code which |
| 6246 ** prevented success. | 6583 ** prevented success. |
| 6247 */ | 6584 */ |
| 6248 static int optimizeInternal(fulltext_vtab *v, | 6585 static int optimizeInternal(fulltext_vtab *v, |
| 6249 OptLeavesReader *readers, int nReaders, | 6586 OptLeavesReader *readers, int nReaders, |
| 6250 LeafWriter *pWriter){ | 6587 LeafWriter *pWriter){ |
| 6251 int i, rc = SQLITE_OK; | 6588 int i, rc = SQLITE_OK; |
| 6252 DataBuffer doclist, merged, tmp; | 6589 DataBuffer doclist, merged, tmp; |
| 6590 const char *pData; |
| 6253 | 6591 |
| 6254 /* Order the readers. */ | 6592 /* Order the readers. */ |
| 6255 i = nReaders; | 6593 i = nReaders; |
| 6256 while( i-- > 0 ){ | 6594 while( i-- > 0 ){ |
| 6257 optLeavesReaderReorder(&readers[i], nReaders-i); | 6595 optLeavesReaderReorder(&readers[i], nReaders-i); |
| 6258 } | 6596 } |
| 6259 | 6597 |
| 6260 dataBufferInit(&doclist, LEAF_MAX); | 6598 dataBufferInit(&doclist, LEAF_MAX); |
| 6261 dataBufferInit(&merged, LEAF_MAX); | 6599 dataBufferInit(&merged, LEAF_MAX); |
| 6262 | 6600 |
| 6263 /* Exhausted readers bubble to the end, so when the first reader is | 6601 /* Exhausted readers bubble to the end, so when the first reader is |
| 6264 ** at eof, all are at eof. | 6602 ** at eof, all are at eof. |
| 6265 */ | 6603 */ |
| 6266 while( !optLeavesReaderAtEnd(&readers[0]) ){ | 6604 while( !optLeavesReaderAtEnd(&readers[0]) ){ |
| 6267 | 6605 |
| 6268 /* Figure out how many readers share the next term. */ | 6606 /* Figure out how many readers share the next term. */ |
| 6269 for(i=1; i<nReaders && !optLeavesReaderAtEnd(&readers[i]); i++){ | 6607 for(i=1; i<nReaders && !optLeavesReaderAtEnd(&readers[i]); i++){ |
| 6270 if( 0!=optLeavesReaderTermCmp(&readers[0], &readers[i]) ) break; | 6608 if( 0!=optLeavesReaderTermCmp(&readers[0], &readers[i]) ) break; |
| 6271 } | 6609 } |
| 6272 | 6610 |
| 6611 pData = optLeavesReaderData(&readers[0]); |
| 6612 if( pData==NULL ){ |
| 6613 rc = SQLITE_CORRUPT_BKPT; |
| 6614 break; |
| 6615 } |
| 6616 |
| 6273 /* Special-case for no merge. */ | 6617 /* Special-case for no merge. */ |
| 6274 if( i==1 ){ | 6618 if( i==1 ){ |
| 6275 /* Trim deletions from the doclist. */ | 6619 /* Trim deletions from the doclist. */ |
| 6276 dataBufferReset(&merged); | 6620 dataBufferReset(&merged); |
| 6277 docListTrim(DL_DEFAULT, | 6621 rc = docListTrim(DL_DEFAULT, pData, |
| 6278 optLeavesReaderData(&readers[0]), | 6622 optLeavesReaderDataBytes(&readers[0]), |
| 6279 optLeavesReaderDataBytes(&readers[0]), | 6623 -1, DL_DEFAULT, &merged); |
| 6280 -1, DL_DEFAULT, &merged); | 6624 if( rc!=SQLITE_OK ) break; |
| 6281 }else{ | 6625 }else{ |
| 6282 DLReader dlReaders[MERGE_COUNT]; | 6626 DLReader dlReaders[MERGE_COUNT]; |
| 6283 int iReader, nReaders; | 6627 int iReader, nReaders; |
| 6284 | 6628 |
| 6285 /* Prime the pipeline with the first reader's doclist. After | 6629 /* Prime the pipeline with the first reader's doclist. After |
| 6286 ** one pass index 0 will reference the accumulated doclist. | 6630 ** one pass index 0 will reference the accumulated doclist. |
| 6287 */ | 6631 */ |
| 6288 dlrInit(&dlReaders[0], DL_DEFAULT, | 6632 rc = dlrInit(&dlReaders[0], DL_DEFAULT, |
| 6289 optLeavesReaderData(&readers[0]), | 6633 pData, |
| 6290 optLeavesReaderDataBytes(&readers[0])); | 6634 optLeavesReaderDataBytes(&readers[0])); |
| 6635 if( rc!=SQLITE_OK ) break; |
| 6291 iReader = 1; | 6636 iReader = 1; |
| 6292 | 6637 |
| 6293 assert( iReader<i ); /* Must execute the loop at least once. */ | 6638 assert( iReader<i ); /* Must execute the loop at least once. */ |
| 6294 while( iReader<i ){ | 6639 while( iReader<i ){ |
| 6295 /* Merge 16 inputs per pass. */ | 6640 /* Merge 16 inputs per pass. */ |
| 6296 for( nReaders=1; iReader<i && nReaders<MERGE_COUNT; | 6641 for( nReaders=1; iReader<i && nReaders<MERGE_COUNT; |
| 6297 iReader++, nReaders++ ){ | 6642 iReader++, nReaders++ ){ |
| 6298 dlrInit(&dlReaders[nReaders], DL_DEFAULT, | 6643 pData = optLeavesReaderData(&readers[iReader]); |
| 6299 optLeavesReaderData(&readers[iReader]), | 6644 if( pData==NULL ){ |
| 6300 optLeavesReaderDataBytes(&readers[iReader])); | 6645 rc = SQLITE_CORRUPT_BKPT; |
| 6646 break; |
| 6647 } |
| 6648 rc = dlrInit(&dlReaders[nReaders], DL_DEFAULT, pData, |
| 6649 optLeavesReaderDataBytes(&readers[iReader])); |
| 6650 if( rc!=SQLITE_OK ) break; |
| 6301 } | 6651 } |
| 6302 | 6652 |
| 6303 /* Merge doclists and swap result into accumulator. */ | 6653 /* Merge doclists and swap result into accumulator. */ |
| 6304 dataBufferReset(&merged); | 6654 if( rc==SQLITE_OK ){ |
| 6305 docListMerge(&merged, dlReaders, nReaders); | 6655 dataBufferReset(&merged); |
| 6306 tmp = merged; | 6656 rc = docListMerge(&merged, dlReaders, nReaders); |
| 6307 merged = doclist; | 6657 tmp = merged; |
| 6308 doclist = tmp; | 6658 merged = doclist; |
| 6659 doclist = tmp; |
| 6660 } |
| 6309 | 6661 |
| 6310 while( nReaders-- > 0 ){ | 6662 while( nReaders-- > 0 ){ |
| 6311 dlrDestroy(&dlReaders[nReaders]); | 6663 dlrDestroy(&dlReaders[nReaders]); |
| 6312 } | 6664 } |
| 6313 | 6665 |
| 6666 if( rc!=SQLITE_OK ) goto err; |
| 6667 |
| 6314 /* Accumulated doclist to reader 0 for next pass. */ | 6668 /* Accumulated doclist to reader 0 for next pass. */ |
| 6315 dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData); | 6669 rc = dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData); |
| 6670 if( rc!=SQLITE_OK ) goto err; |
| 6316 } | 6671 } |
| 6317 | 6672 |
| 6318 /* Destroy reader that was left in the pipeline. */ | 6673 /* Destroy reader that was left in the pipeline. */ |
| 6319 dlrDestroy(&dlReaders[0]); | 6674 dlrDestroy(&dlReaders[0]); |
| 6320 | 6675 |
| 6321 /* Trim deletions from the doclist. */ | 6676 /* Trim deletions from the doclist. */ |
| 6322 dataBufferReset(&merged); | 6677 dataBufferReset(&merged); |
| 6323 docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, | 6678 rc = docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, |
| 6324 -1, DL_DEFAULT, &merged); | 6679 -1, DL_DEFAULT, &merged); |
| 6680 if( rc!=SQLITE_OK ) goto err; |
| 6325 } | 6681 } |
| 6326 | 6682 |
| 6327 /* Only pass doclists with hits (skip if all hits deleted). */ | 6683 /* Only pass doclists with hits (skip if all hits deleted). */ |
| 6328 if( merged.nData>0 ){ | 6684 if( merged.nData>0 ){ |
| 6329 rc = leafWriterStep(v, pWriter, | 6685 rc = leafWriterStep(v, pWriter, |
| 6330 optLeavesReaderTerm(&readers[0]), | 6686 optLeavesReaderTerm(&readers[0]), |
| 6331 optLeavesReaderTermBytes(&readers[0]), | 6687 optLeavesReaderTermBytes(&readers[0]), |
| 6332 merged.pData, merged.nData); | 6688 merged.pData, merged.nData); |
| 6333 if( rc!=SQLITE_OK ) goto err; | 6689 if( rc!=SQLITE_OK ) goto err; |
| 6334 } | 6690 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6394 */ | 6750 */ |
| 6395 leafWriterInit(iMaxLevel, 0, &writer); | 6751 leafWriterInit(iMaxLevel, 0, &writer); |
| 6396 | 6752 |
| 6397 i = 0; | 6753 i = 0; |
| 6398 while( (rc = sqlite3_step(s))==SQLITE_ROW ){ | 6754 while( (rc = sqlite3_step(s))==SQLITE_ROW ){ |
| 6399 sqlite_int64 iStart = sqlite3_column_int64(s, 0); | 6755 sqlite_int64 iStart = sqlite3_column_int64(s, 0); |
| 6400 sqlite_int64 iEnd = sqlite3_column_int64(s, 1); | 6756 sqlite_int64 iEnd = sqlite3_column_int64(s, 1); |
| 6401 const char *pRootData = sqlite3_column_blob(s, 2); | 6757 const char *pRootData = sqlite3_column_blob(s, 2); |
| 6402 int nRootData = sqlite3_column_bytes(s, 2); | 6758 int nRootData = sqlite3_column_bytes(s, 2); |
| 6403 | 6759 |
| 6760 /* Corrupt if we get back different types than we stored. */ |
| 6761 if( sqlite3_column_type(s, 0)!=SQLITE_INTEGER || |
| 6762 sqlite3_column_type(s, 1)!=SQLITE_INTEGER || |
| 6763 sqlite3_column_type(s, 2)!=SQLITE_BLOB ){ |
| 6764 rc = SQLITE_CORRUPT_BKPT; |
| 6765 break; |
| 6766 } |
| 6767 |
| 6404 assert( i<nReaders ); | 6768 assert( i<nReaders ); |
| 6405 rc = leavesReaderInit(v, -1, iStart, iEnd, pRootData, nRootData, | 6769 rc = leavesReaderInit(v, -1, iStart, iEnd, pRootData, nRootData, |
| 6406 &readers[i].reader); | 6770 &readers[i].reader); |
| 6407 if( rc!=SQLITE_OK ) break; | 6771 if( rc!=SQLITE_OK ) break; |
| 6408 | 6772 |
| 6409 readers[i].segment = i; | 6773 readers[i].segment = i; |
| 6410 i++; | 6774 i++; |
| 6411 } | 6775 } |
| 6412 | 6776 |
| 6413 /* If we managed to successfully read them all, optimize them. */ | 6777 /* If we managed to successfully read them all, optimize them. */ |
| 6414 if( rc==SQLITE_DONE ){ | 6778 if( rc==SQLITE_DONE ){ |
| 6415 assert( i==nReaders ); | 6779 assert( i==nReaders ); |
| 6416 rc = optimizeInternal(v, readers, nReaders, &writer); | 6780 rc = optimizeInternal(v, readers, nReaders, &writer); |
| 6781 }else{ |
| 6782 sqlite3_reset(s); /* So we don't leave a lock. */ |
| 6417 } | 6783 } |
| 6418 | 6784 |
| 6419 while( i-- > 0 ){ | 6785 while( i-- > 0 ){ |
| 6420 leavesReaderDestroy(&readers[i].reader); | 6786 leavesReaderDestroy(&readers[i].reader); |
| 6421 } | 6787 } |
| 6422 sqlite3_free(readers); | 6788 sqlite3_free(readers); |
| 6423 | 6789 |
| 6424 /* If we've successfully gotten to here, delete the old segments | 6790 /* If we've successfully gotten to here, delete the old segments |
| 6425 ** and flush the interior structure of the new segment. | 6791 ** and flush the interior structure of the new segment. |
| 6426 */ | 6792 */ |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6470 ** iStartBlockid and iEndBlockid, inclusive, or by the contents of | 6836 ** iStartBlockid and iEndBlockid, inclusive, or by the contents of |
| 6471 ** pRootData if iStartBlockid is 0 (in which case the entire segment | 6837 ** pRootData if iStartBlockid is 0 (in which case the entire segment |
| 6472 ** fit in a leaf). | 6838 ** fit in a leaf). |
| 6473 */ | 6839 */ |
| 6474 static int collectSegmentTerms(fulltext_vtab *v, sqlite3_stmt *s, | 6840 static int collectSegmentTerms(fulltext_vtab *v, sqlite3_stmt *s, |
| 6475 fts3Hash *pTerms){ | 6841 fts3Hash *pTerms){ |
| 6476 const sqlite_int64 iStartBlockid = sqlite3_column_int64(s, 0); | 6842 const sqlite_int64 iStartBlockid = sqlite3_column_int64(s, 0); |
| 6477 const sqlite_int64 iEndBlockid = sqlite3_column_int64(s, 1); | 6843 const sqlite_int64 iEndBlockid = sqlite3_column_int64(s, 1); |
| 6478 const char *pRootData = sqlite3_column_blob(s, 2); | 6844 const char *pRootData = sqlite3_column_blob(s, 2); |
| 6479 const int nRootData = sqlite3_column_bytes(s, 2); | 6845 const int nRootData = sqlite3_column_bytes(s, 2); |
| 6846 int rc; |
| 6480 LeavesReader reader; | 6847 LeavesReader reader; |
| 6481 int rc = leavesReaderInit(v, 0, iStartBlockid, iEndBlockid, | 6848 |
| 6482 pRootData, nRootData, &reader); | 6849 /* Corrupt if we get back different types than we stored. */ |
| 6850 if( sqlite3_column_type(s, 0)!=SQLITE_INTEGER || |
| 6851 sqlite3_column_type(s, 1)!=SQLITE_INTEGER || |
| 6852 sqlite3_column_type(s, 2)!=SQLITE_BLOB ){ |
| 6853 return SQLITE_CORRUPT_BKPT; |
| 6854 } |
| 6855 |
| 6856 rc = leavesReaderInit(v, 0, iStartBlockid, iEndBlockid, |
| 6857 pRootData, nRootData, &reader); |
| 6483 if( rc!=SQLITE_OK ) return rc; | 6858 if( rc!=SQLITE_OK ) return rc; |
| 6484 | 6859 |
| 6485 while( rc==SQLITE_OK && !leavesReaderAtEnd(&reader) ){ | 6860 while( rc==SQLITE_OK && !leavesReaderAtEnd(&reader) ){ |
| 6486 const char *pTerm = leavesReaderTerm(&reader); | 6861 const char *pTerm = leavesReaderTerm(&reader); |
| 6487 const int nTerm = leavesReaderTermBytes(&reader); | 6862 const int nTerm = leavesReaderTermBytes(&reader); |
| 6488 void *oldValue = sqlite3Fts3HashFind(pTerms, pTerm, nTerm); | 6863 void *oldValue = sqlite3Fts3HashFind(pTerms, pTerm, nTerm); |
| 6489 void *newValue = (void *)((char *)oldValue+1); | 6864 void *newValue = (void *)((char *)oldValue+1); |
| 6490 | 6865 |
| 6491 /* From the comment before sqlite3Fts3HashInsert in fts3_hash.c, | 6866 /* From the comment before sqlite3Fts3HashInsert in fts3_hash.c, |
| 6492 ** the data value passed is returned in case of malloc failure. | 6867 ** the data value passed is returned in case of malloc failure. |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6634 } | 7009 } |
| 6635 } | 7010 } |
| 6636 | 7011 |
| 6637 /* Expand the DL_DEFAULT doclist in pData into a text result in | 7012 /* Expand the DL_DEFAULT doclist in pData into a text result in |
| 6638 ** pContext. | 7013 ** pContext. |
| 6639 */ | 7014 */ |
| 6640 static void createDoclistResult(sqlite3_context *pContext, | 7015 static void createDoclistResult(sqlite3_context *pContext, |
| 6641 const char *pData, int nData){ | 7016 const char *pData, int nData){ |
| 6642 DataBuffer dump; | 7017 DataBuffer dump; |
| 6643 DLReader dlReader; | 7018 DLReader dlReader; |
| 7019 int rc; |
| 6644 | 7020 |
| 6645 assert( pData!=NULL && nData>0 ); | 7021 assert( pData!=NULL && nData>0 ); |
| 6646 | 7022 |
| 7023 rc = dlrInit(&dlReader, DL_DEFAULT, pData, nData); |
| 7024 if( rc!=SQLITE_OK ) return rc; |
| 6647 dataBufferInit(&dump, 0); | 7025 dataBufferInit(&dump, 0); |
| 6648 dlrInit(&dlReader, DL_DEFAULT, pData, nData); | 7026 for( ; rc==SQLITE_OK && !dlrAtEnd(&dlReader); rc = dlrStep(&dlReader) ){ |
| 6649 for( ; !dlrAtEnd(&dlReader); dlrStep(&dlReader) ){ | |
| 6650 char buf[256]; | 7027 char buf[256]; |
| 6651 PLReader plReader; | 7028 PLReader plReader; |
| 6652 | 7029 |
| 6653 plrInit(&plReader, &dlReader); | 7030 rc = plrInit(&plReader, &dlReader); |
| 7031 if( rc!=SQLITE_OK ) break; |
| 6654 if( DL_DEFAULT==DL_DOCIDS || plrAtEnd(&plReader) ){ | 7032 if( DL_DEFAULT==DL_DOCIDS || plrAtEnd(&plReader) ){ |
| 6655 sqlite3_snprintf(sizeof(buf), buf, "[%lld] ", dlrDocid(&dlReader)); | 7033 sqlite3_snprintf(sizeof(buf), buf, "[%lld] ", dlrDocid(&dlReader)); |
| 6656 dataBufferAppend(&dump, buf, strlen(buf)); | 7034 dataBufferAppend(&dump, buf, strlen(buf)); |
| 6657 }else{ | 7035 }else{ |
| 6658 int iColumn = plrColumn(&plReader); | 7036 int iColumn = plrColumn(&plReader); |
| 6659 | 7037 |
| 6660 sqlite3_snprintf(sizeof(buf), buf, "[%lld %d[", | 7038 sqlite3_snprintf(sizeof(buf), buf, "[%lld %d[", |
| 6661 dlrDocid(&dlReader), iColumn); | 7039 dlrDocid(&dlReader), iColumn); |
| 6662 dataBufferAppend(&dump, buf, strlen(buf)); | 7040 dataBufferAppend(&dump, buf, strlen(buf)); |
| 6663 | 7041 |
| 6664 for( ; !plrAtEnd(&plReader); plrStep(&plReader) ){ | 7042 for( ; !plrAtEnd(&plReader); rc = plrStep(&plReader) ){ |
| 7043 if( rc!=SQLITE_OK ) break; |
| 6665 if( plrColumn(&plReader)!=iColumn ){ | 7044 if( plrColumn(&plReader)!=iColumn ){ |
| 6666 iColumn = plrColumn(&plReader); | 7045 iColumn = plrColumn(&plReader); |
| 6667 sqlite3_snprintf(sizeof(buf), buf, "] %d[", iColumn); | 7046 sqlite3_snprintf(sizeof(buf), buf, "] %d[", iColumn); |
| 6668 assert( dump.nData>0 ); | 7047 assert( dump.nData>0 ); |
| 6669 dump.nData--; /* Overwrite trailing space. */ | 7048 dump.nData--; /* Overwrite trailing space. */ |
| 6670 assert( dump.pData[dump.nData]==' '); | 7049 assert( dump.pData[dump.nData]==' '); |
| 6671 dataBufferAppend(&dump, buf, strlen(buf)); | 7050 dataBufferAppend(&dump, buf, strlen(buf)); |
| 6672 } | 7051 } |
| 6673 if( DL_DEFAULT==DL_POSITIONS_OFFSETS ){ | 7052 if( DL_DEFAULT==DL_POSITIONS_OFFSETS ){ |
| 6674 sqlite3_snprintf(sizeof(buf), buf, "%d,%d,%d ", | 7053 sqlite3_snprintf(sizeof(buf), buf, "%d,%d,%d ", |
| 6675 plrPosition(&plReader), | 7054 plrPosition(&plReader), |
| 6676 plrStartOffset(&plReader), plrEndOffset(&plReader)); | 7055 plrStartOffset(&plReader), plrEndOffset(&plReader)); |
| 6677 }else if( DL_DEFAULT==DL_POSITIONS ){ | 7056 }else if( DL_DEFAULT==DL_POSITIONS ){ |
| 6678 sqlite3_snprintf(sizeof(buf), buf, "%d ", plrPosition(&plReader)); | 7057 sqlite3_snprintf(sizeof(buf), buf, "%d ", plrPosition(&plReader)); |
| 6679 }else{ | 7058 }else{ |
| 6680 assert( NULL=="Unhandled DL_DEFAULT value"); | 7059 assert( NULL=="Unhandled DL_DEFAULT value"); |
| 6681 } | 7060 } |
| 6682 dataBufferAppend(&dump, buf, strlen(buf)); | 7061 dataBufferAppend(&dump, buf, strlen(buf)); |
| 6683 } | 7062 } |
| 6684 plrDestroy(&plReader); | 7063 plrDestroy(&plReader); |
| 7064 if( rc!= SQLITE_OK ) break; |
| 6685 | 7065 |
| 6686 assert( dump.nData>0 ); | 7066 assert( dump.nData>0 ); |
| 6687 dump.nData--; /* Overwrite trailing space. */ | 7067 dump.nData--; /* Overwrite trailing space. */ |
| 6688 assert( dump.pData[dump.nData]==' '); | 7068 assert( dump.pData[dump.nData]==' '); |
| 6689 dataBufferAppend(&dump, "]] ", 3); | 7069 dataBufferAppend(&dump, "]] ", 3); |
| 6690 } | 7070 } |
| 6691 } | 7071 } |
| 6692 dlrDestroy(&dlReader); | 7072 dlrDestroy(&dlReader); |
| 7073 if( rc!=SQLITE_OK ){ |
| 7074 dataBufferDestroy(&dump); |
| 7075 return rc; |
| 7076 } |
| 6693 | 7077 |
| 6694 assert( dump.nData>0 ); | 7078 assert( dump.nData>0 ); |
| 6695 dump.nData--; /* Overwrite trailing space. */ | 7079 dump.nData--; /* Overwrite trailing space. */ |
| 6696 assert( dump.pData[dump.nData]==' '); | 7080 assert( dump.pData[dump.nData]==' '); |
| 6697 dump.pData[dump.nData] = '\0'; | 7081 dump.pData[dump.nData] = '\0'; |
| 6698 assert( dump.nData>0 ); | 7082 assert( dump.nData>0 ); |
| 6699 | 7083 |
| 6700 /* Passes ownership of dump's buffer to pContext. */ | 7084 /* Passes ownership of dump's buffer to pContext. */ |
| 6701 sqlite3_result_text(pContext, dump.pData, dump.nData, sqlite3_free); | 7085 sqlite3_result_text(pContext, dump.pData, dump.nData, sqlite3_free); |
| 6702 dump.pData = NULL; | 7086 dump.pData = NULL; |
| 6703 dump.nData = dump.nCapacity = 0; | 7087 dump.nData = dump.nCapacity = 0; |
| 7088 return SQLITE_OK; |
| 6704 } | 7089 } |
| 6705 | 7090 |
| 6706 /* Implements dump_doclist() for use in inspecting the fts3 index from | 7091 /* Implements dump_doclist() for use in inspecting the fts3 index from |
| 6707 ** tests. TEXT result containing a string representation of the | 7092 ** tests. TEXT result containing a string representation of the |
| 6708 ** doclist for the indicated term. dump_doclist(t, term, level, idx) | 7093 ** doclist for the indicated term. dump_doclist(t, term, level, idx) |
| 6709 ** dumps the doclist for term from the segment specified by level, idx | 7094 ** dumps the doclist for term from the segment specified by level, idx |
| 6710 ** (in %_segdir), while dump_doclist(t, term) dumps the logical | 7095 ** (in %_segdir), while dump_doclist(t, term) dumps the logical |
| 6711 ** doclist for the term across all segments. The per-segment doclist | 7096 ** doclist for the term across all segments. The per-segment doclist |
| 6712 ** can contain deletions, while the full-index doclist will not | 7097 ** can contain deletions, while the full-index doclist will not |
| 6713 ** (deletions are omitted). | 7098 ** (deletions are omitted). |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6980 | 7365 |
| 6981 #ifdef SQLITE_TEST | 7366 #ifdef SQLITE_TEST |
| 6982 sqlite3Fts3ExprInitTestInterface(db); | 7367 sqlite3Fts3ExprInitTestInterface(db); |
| 6983 #endif | 7368 #endif |
| 6984 | 7369 |
| 6985 /* Create the virtual table wrapper around the hash-table and overload | 7370 /* Create the virtual table wrapper around the hash-table and overload |
| 6986 ** the two scalar functions. If this is successful, register the | 7371 ** the two scalar functions. If this is successful, register the |
| 6987 ** module with sqlite. | 7372 ** module with sqlite. |
| 6988 */ | 7373 */ |
| 6989 if( SQLITE_OK==rc | 7374 if( SQLITE_OK==rc |
| 7375 #if CHROMIUM_FTS3_CHANGES && !SQLITE_TEST |
| 7376 /* fts3_tokenizer() disabled for security reasons. */ |
| 7377 #else |
| 6990 && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) | 7378 && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) |
| 7379 #endif |
| 6991 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) | 7380 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) |
| 6992 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1)) | 7381 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1)) |
| 6993 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", -1)) | 7382 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", -1)) |
| 6994 #ifdef SQLITE_TEST | 7383 #ifdef SQLITE_TEST |
| 6995 && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_terms", -1)) | 7384 && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_terms", -1)) |
| 6996 && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_doclist", -1)) | 7385 && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_doclist", -1)) |
| 6997 #endif | 7386 #endif |
| 6998 ){ | 7387 ){ |
| 6999 return sqlite3_create_module_v2( | 7388 return sqlite3_create_module_v2( |
| 7000 db, "fts3", &fts3Module, (void *)pHash, hashDestroy | 7389 db, "fts3", &fts3Module, (void *)pHash, hashDestroy |
| (...skipping 14 matching lines...) Expand all Loading... |
| 7015 sqlite3 *db, | 7404 sqlite3 *db, |
| 7016 char **pzErrMsg, | 7405 char **pzErrMsg, |
| 7017 const sqlite3_api_routines *pApi | 7406 const sqlite3_api_routines *pApi |
| 7018 ){ | 7407 ){ |
| 7019 SQLITE_EXTENSION_INIT2(pApi) | 7408 SQLITE_EXTENSION_INIT2(pApi) |
| 7020 return sqlite3Fts3Init(db); | 7409 return sqlite3Fts3Init(db); |
| 7021 } | 7410 } |
| 7022 #endif | 7411 #endif |
| 7023 | 7412 |
| 7024 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ | 7413 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |
| OLD | NEW |