Update contrib.
     1 /* fts1 has a design flaw which can lead to database corruption (see
 
     2 ** below).  It is recommended not to use it any longer, instead use
 
     3 ** fts3 (or higher).  If you believe that your use of fts1 is safe,
 
     4 ** add -DSQLITE_ENABLE_BROKEN_FTS1=1 to your CFLAGS.
 
     6 #if (!defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)) \
 
     7         && !defined(SQLITE_ENABLE_BROKEN_FTS1)
 
     8 #error fts1 has a design flaw and has been deprecated.
 
    10 /* The flaw is that fts1 uses the content table's unaliased rowid as
 
    11 ** the unique docid.  fts1 embeds the rowid in the index it builds,
 
    12 ** and expects the rowid to not change.  The SQLite VACUUM operation
 
    13 ** will renumber such rowids, thereby breaking fts1.  If you are using
 
    14 ** fts1 in a system which has disabled VACUUM, then you can continue
 
    15 ** to use it safely.  Note that PRAGMA auto_vacuum does NOT disable
 
    16 ** VACUUM, though systems using auto_vacuum are unlikely to invoke
 
    19 ** fts1 should be safe even across VACUUM if you only insert documents
 
    23 /* The author disclaims copyright to this source code.
 
    25  * This is an SQLite module implementing full-text search.
 
    29 ** The code in this file is only compiled if:
 
    31 **     * The FTS1 module is being built as an extension
 
    32 **       (in which case SQLITE_CORE is not defined), or
 
    34 **     * The FTS1 module is being built into the core of
 
    35 **       SQLite (in which case SQLITE_ENABLE_FTS1 is defined).
 
    37 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)
 
    39 #if defined(SQLITE_ENABLE_FTS1) && !defined(SQLITE_CORE)
 
    40 # define SQLITE_CORE 1
 
    50 #include "fts1_hash.h"
 
    51 #include "fts1_tokenizer.h"
 
    53 #include "sqlite3ext.h"
 
    54 SQLITE_EXTENSION_INIT1
 
    58 # define TRACE(A)  printf A; fflush(stdout)
 
    63 /* utility functions */
 
    65 typedef struct StringBuffer {
 
    66   int len;      /* length, not including null terminator */
 
    67   int alloced;  /* Space allocated for s[] */ 
 
    68   char *s;      /* Content of the string */
 
    71 static void initStringBuffer(StringBuffer *sb){
 
    78 static void nappend(StringBuffer *sb, const char *zFrom, int nFrom){
 
    79   if( sb->len + nFrom >= sb->alloced ){
 
    80     sb->alloced = sb->len + nFrom + 100;
 
    81     sb->s = realloc(sb->s, sb->alloced+1);
 
    87   memcpy(sb->s + sb->len, zFrom, nFrom);
 
    91 static void append(StringBuffer *sb, const char *zFrom){
 
    92   nappend(sb, zFrom, strlen(zFrom));
 
    95 /* We encode variable-length integers in little-endian order using seven bits
 
    96  * per byte as follows:
 
    99 **         A = 0xxxxxxx    7 bits of data and one flag bit
 
   100 **         B = 1xxxxxxx    7 bits of data and one flag bit
 
   108 /* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */
 
   109 #define VARINT_MAX 10
 
   111 /* Write a 64-bit variable-length integer to memory starting at p[0].
 
   112  * The length of data written will be between 1 and VARINT_MAX bytes.
 
   113  * The number of bytes written is returned. */
 
   114 static int putVarint(char *p, sqlite_int64 v){
 
   115   unsigned char *q = (unsigned char *) p;
 
   116   sqlite_uint64 vu = v;
 
   118     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
 
   121   q[-1] &= 0x7f;  /* turn off high bit in final byte */
 
   122   assert( q - (unsigned char *)p <= VARINT_MAX );
 
   123   return (int) (q - (unsigned char *)p);
 
   126 /* Read a 64-bit variable-length integer from memory starting at p[0].
 
   127  * Return the number of bytes read, or 0 on error.
 
   128  * The value is stored in *v. */
 
   129 static int getVarint(const char *p, sqlite_int64 *v){
 
   130   const unsigned char *q = (const unsigned char *) p;
 
   131   sqlite_uint64 x = 0, y = 1;
 
   132   while( (*q & 0x80) == 0x80 ){
 
   133     x += y * (*q++ & 0x7f);
 
   135     if( q - (unsigned char *)p >= VARINT_MAX ){  /* bad data */
 
   141   *v = (sqlite_int64) x;
 
   142   return (int) (q - (unsigned char *)p);
 
   145 static int getVarint32(const char *p, int *pi){
 
   147  int ret = getVarint(p, &i);
 
   153 /*** Document lists ***
 
   155  * A document list holds a sorted list of varint-encoded document IDs.
 
   157  * A doclist with type DL_POSITIONS_OFFSETS is stored like this:
 
   162  *     varint position;     (delta from previous position plus POS_BASE)
 
   163  *     varint startOffset;  (delta from previous startOffset)
 
   164  *     varint endOffset;    (delta from startOffset)
 
   168  * Here, array { X } means zero or more occurrences of X, adjacent in memory.
 
   170  * A position list may hold positions for text in multiple columns.  A position
 
   171  * POS_COLUMN is followed by a varint containing the index of the column for
 
   172  * following positions in the list.  Any positions appearing before any
 
   173  * occurrences of POS_COLUMN are for column 0.
 
   175  * A doclist with type DL_POSITIONS is like the above, but holds only docids
 
   176  * and positions without offset information.
 
   178  * A doclist with type DL_DOCIDS is like the above, but holds only docids
 
   179  * without positions or offset information.
 
   181  * On disk, every document list has positions and offsets, so we don't bother
 
   182  * to serialize a doclist's type.
 
   184  * We don't yet delta-encode document IDs; doing so will probably be a
 
   187  * NOTE(shess) I've thought of a slightly (1%) better offset encoding.
 
   188  * After the first offset, estimate the next offset by using the
 
   189  * current token position and the previous token position and offset,
 
   190  * offset to handle some variance.  So the estimate would be
 
   191  * (iPosition*w->iStartOffset/w->iPosition-64), which is delta-encoded
 
   192  * as normal.  Offsets more than 64 chars from the estimate are
 
   193  * encoded as the delta to the previous start offset + 128.  An
 
   194  * additional tiny increment can be gained by using the end offset of
 
   195  * the previous token to make the estimate a tiny bit more precise.
 
   198 /* It is not safe to call isspace(), tolower(), or isalnum() on
 
   199 ** hi-bit-set characters.  This is the same solution used in the
 
   202 /* TODO(shess) The snippet-generation code should be using the
 
   203 ** tokenizer-generated tokens rather than doing its own local
 
   206 /* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */
 
   207 static int safe_isspace(char c){
 
   208   return (c&0x80)==0 ? isspace(c) : 0;
 
   210 static int safe_tolower(char c){
 
   211   return (c&0x80)==0 ? tolower(c) : c;
 
   213 static int safe_isalnum(char c){
 
   214   return (c&0x80)==0 ? isalnum(c) : 0;
 
   217 typedef enum DocListType {
 
   218   DL_DOCIDS,              /* docids only */
 
   219   DL_POSITIONS,           /* docids + positions */
 
   220   DL_POSITIONS_OFFSETS    /* docids + positions + offsets */
 
   224 ** By default, only positions and not offsets are stored in the doclists.
 
   225 ** To change this so that offsets are stored too, compile with
 
   227 **          -DDL_DEFAULT=DL_POSITIONS_OFFSETS
 
   231 # define DL_DEFAULT DL_POSITIONS
 
   234 typedef struct DocList {
 
   238   int iLastColumn;    /* the last column written */
 
   239   int iLastPos;       /* the last position written */
 
   240   int iLastOffset;    /* the last start offset written */
 
   244   POS_END = 0,        /* end of this position list */
 
   245   POS_COLUMN,         /* followed by new column number */
 
   249 /* Initialize a new DocList to hold the given data. */
 
   250 static void docListInit(DocList *d, DocListType iType,
 
   251                         const char *pData, int nData){
 
   254     d->pData = malloc(nData);
 
   255     memcpy(d->pData, pData, nData);
 
   261   d->iLastPos = d->iLastOffset = 0;
 
   264 /* Create a new dynamically-allocated DocList. */
 
   265 static DocList *docListNew(DocListType iType){
 
   266   DocList *d = (DocList *) malloc(sizeof(DocList));
 
   267   docListInit(d, iType, 0, 0);
 
   271 static void docListDestroy(DocList *d){
 
   274   memset(d, 0x55, sizeof(*d));
 
   278 static void docListDelete(DocList *d){
 
   283 static char *docListEnd(DocList *d){
 
   284   return d->pData + d->nData;
 
   287 /* Append a varint to a DocList's data. */
 
   288 static void appendVarint(DocList *d, sqlite_int64 i){
 
   290   int n = putVarint(c, i);
 
   291   d->pData = realloc(d->pData, d->nData + n);
 
   292   memcpy(d->pData + d->nData, c, n);
 
   296 static void docListAddDocid(DocList *d, sqlite_int64 iDocid){
 
   297   appendVarint(d, iDocid);
 
   298   if( d->iType>=DL_POSITIONS ){
 
   299     appendVarint(d, POS_END);  /* initially empty position list */
 
   301     d->iLastPos = d->iLastOffset = 0;
 
   305 /* helper function for docListAddPos and docListAddPosOffset */
 
   306 static void addPos(DocList *d, int iColumn, int iPos){
 
   307   assert( d->nData>0 );
 
   308   --d->nData;  /* remove previous terminator */
 
   309   if( iColumn!=d->iLastColumn ){
 
   310     assert( iColumn>d->iLastColumn );
 
   311     appendVarint(d, POS_COLUMN);
 
   312     appendVarint(d, iColumn);
 
   313     d->iLastColumn = iColumn;
 
   314     d->iLastPos = d->iLastOffset = 0;
 
   316   assert( iPos>=d->iLastPos );
 
   317   appendVarint(d, iPos-d->iLastPos+POS_BASE);
 
   321 /* Add a position to the last position list in a doclist. */
 
   322 static void docListAddPos(DocList *d, int iColumn, int iPos){
 
   323   assert( d->iType==DL_POSITIONS );
 
   324   addPos(d, iColumn, iPos);
 
   325   appendVarint(d, POS_END);  /* add new terminator */
 
   329 ** Add a position and starting and ending offsets to a doclist.
 
   331 ** If the doclist is setup to handle only positions, then insert
 
   332 ** the position only and ignore the offsets.
 
   334 static void docListAddPosOffset(
 
   335   DocList *d,             /* Doclist under construction */
 
   336   int iColumn,            /* Column the inserted term is part of */
 
   337   int iPos,               /* Position of the inserted term */
 
   338   int iStartOffset,       /* Starting offset of inserted term */
 
   339   int iEndOffset          /* Ending offset of inserted term */
 
   341   assert( d->iType>=DL_POSITIONS );
 
   342   addPos(d, iColumn, iPos);
 
   343   if( d->iType==DL_POSITIONS_OFFSETS ){
 
   344     assert( iStartOffset>=d->iLastOffset );
 
   345     appendVarint(d, iStartOffset-d->iLastOffset);
 
   346     d->iLastOffset = iStartOffset;
 
   347     assert( iEndOffset>=iStartOffset );
 
   348     appendVarint(d, iEndOffset-iStartOffset);
 
   350   appendVarint(d, POS_END);  /* add new terminator */
 
   354 ** A DocListReader object is a cursor into a doclist.  Initialize
 
   355 ** the cursor to the beginning of the doclist by calling readerInit().
 
   361 **      skipPositionList()
 
   364 ** to read information out of the doclist.  When we reach the end
 
   365 ** of the doclist, atEnd() returns TRUE.
 
   367 typedef struct DocListReader {
 
   368   DocList *pDoclist;  /* The document list we are stepping through */
 
   369   char *p;            /* Pointer to next unread byte in the doclist */
 
   371   int iLastPos;  /* the last position read, or -1 when not in a position list */
 
   375 ** Initialize the DocListReader r to point to the beginning of pDoclist.
 
   377 static void readerInit(DocListReader *r, DocList *pDoclist){
 
   378   r->pDoclist = pDoclist;
 
   379   if( pDoclist!=NULL ){
 
   380     r->p = pDoclist->pData;
 
   387 ** Return TRUE if we have reached then end of pReader and there is
 
   388 ** nothing else left to read.
 
   390 static int atEnd(DocListReader *pReader){
 
   391   return pReader->pDoclist==0 || (pReader->p >= docListEnd(pReader->pDoclist));
 
   394 /* Peek at the next docid without advancing the read pointer. 
 
   396 static sqlite_int64 peekDocid(DocListReader *pReader){
 
   398   assert( !atEnd(pReader) );
 
   399   assert( pReader->iLastPos==-1 );
 
   400   getVarint(pReader->p, &ret);
 
   404 /* Read the next docid.   See also nextDocid().
 
   406 static sqlite_int64 readDocid(DocListReader *pReader){
 
   408   assert( !atEnd(pReader) );
 
   409   assert( pReader->iLastPos==-1 );
 
   410   pReader->p += getVarint(pReader->p, &ret);
 
   411   if( pReader->pDoclist->iType>=DL_POSITIONS ){
 
   412     pReader->iLastColumn = 0;
 
   413     pReader->iLastPos = 0;
 
   418 /* Read the next position and column index from a position list.
 
   419  * Returns the position, or -1 at the end of the list. */
 
   420 static int readPosition(DocListReader *pReader, int *iColumn){
 
   422   int iType = pReader->pDoclist->iType;
 
   424   if( pReader->iLastPos==-1 ){
 
   427   assert( !atEnd(pReader) );
 
   429   if( iType<DL_POSITIONS ){
 
   432   pReader->p += getVarint32(pReader->p, &i);
 
   434     pReader->iLastColumn = pReader->iLastPos = -1;
 
   439     pReader->p += getVarint32(pReader->p, &pReader->iLastColumn);
 
   440     pReader->iLastPos = 0;
 
   441     pReader->p += getVarint32(pReader->p, &i);
 
   442     assert( i>=POS_BASE );
 
   444   pReader->iLastPos += ((int) i)-POS_BASE;
 
   445   if( iType>=DL_POSITIONS_OFFSETS ){
 
   446     /* Skip over offsets, ignoring them for now. */
 
   448     pReader->p += getVarint32(pReader->p, &iStart);
 
   449     pReader->p += getVarint32(pReader->p, &iEnd);
 
   451   *iColumn = pReader->iLastColumn;
 
   452   return pReader->iLastPos;
 
   455 /* Skip past the end of a position list. */
 
   456 static void skipPositionList(DocListReader *pReader){
 
   457   DocList *p = pReader->pDoclist;
 
   458   if( p && p->iType>=DL_POSITIONS ){
 
   460     while( readPosition(pReader, &iColumn)!=-1 ){}
 
   464 /* Skip over a docid, including its position list if the doclist has
 
   466 static void skipDocument(DocListReader *pReader){
 
   468   skipPositionList(pReader);
 
   471 /* Skip past all docids which are less than [iDocid].  Returns 1 if a docid
 
   472  * matching [iDocid] was found.  */
 
   473 static int skipToDocid(DocListReader *pReader, sqlite_int64 iDocid){
 
   475   while( !atEnd(pReader) && (d=peekDocid(pReader))<iDocid ){
 
   476     skipDocument(pReader);
 
   478   return !atEnd(pReader) && d==iDocid;
 
   481 /* Return the first document in a document list.
 
   483 static sqlite_int64 firstDocid(DocList *d){
 
   486   return readDocid(&r);
 
   491 ** This routine is used for debugging purpose only.
 
   493 ** Write the content of a doclist to standard output.
 
   495 static void printDoclist(DocList *p){
 
   497   const char *zSep = "";
 
   501     sqlite_int64 docid = readDocid(&r);
 
   503       skipPositionList(&r);
 
   506     printf("%s%lld", zSep, docid);
 
   508     if( p->iType>=DL_POSITIONS ){
 
   510       const char *zDiv = "";
 
   512       while( (iPos = readPosition(&r, &iCol))>=0 ){
 
   513         printf("%s%d:%d", zDiv, iCol, iPos);
 
   522 #endif /* SQLITE_DEBUG */
 
   524 /* Trim the given doclist to contain only positions in column
 
   525  * [iRestrictColumn]. */
 
   526 static void docListRestrictColumn(DocList *in, int iRestrictColumn){
 
   530   assert( in->iType>=DL_POSITIONS );
 
   532   docListInit(&out, DL_POSITIONS, NULL, 0);
 
   535     sqlite_int64 iDocid = readDocid(&r);
 
   538     docListAddDocid(&out, iDocid);
 
   539     while( (iPos = readPosition(&r, &iColumn)) != -1 ){
 
   540       if( iColumn==iRestrictColumn ){
 
   541         docListAddPos(&out, iColumn, iPos);
 
   550 /* Trim the given doclist by discarding any docids without any remaining
 
   552 static void docListDiscardEmpty(DocList *in) {
 
   556   /* TODO: It would be nice to implement this operation in place; that
 
   557    * could save a significant amount of memory in queries with long doclists. */
 
   558   assert( in->iType>=DL_POSITIONS );
 
   560   docListInit(&out, DL_POSITIONS, NULL, 0);
 
   563     sqlite_int64 iDocid = readDocid(&r);
 
   566     while( (iPos = readPosition(&r, &iColumn)) != -1 ){
 
   568         docListAddDocid(&out, iDocid);
 
   571       docListAddPos(&out, iColumn, iPos);
 
   579 /* Helper function for docListUpdate() and docListAccumulate().
 
   580 ** Splices a doclist element into the doclist represented by r,
 
   581 ** leaving r pointing after the newly spliced element.
 
   583 static void docListSpliceElement(DocListReader *r, sqlite_int64 iDocid,
 
   584                                  const char *pSource, int nSource){
 
   585   DocList *d = r->pDoclist;
 
   589   found = skipToDocid(r, iDocid);
 
   591   /* Describe slice in d to place pSource/nSource. */
 
   595     nTarget = r->p-pTarget;
 
   600   /* The sense of the following is that there are three possibilities.
 
   601   ** If nTarget==nSource, we should not move any memory nor realloc.
 
   602   ** If nTarget>nSource, trim target and realloc.
 
   603   ** If nTarget<nSource, realloc then expand target.
 
   605   if( nTarget>nSource ){
 
   606     memmove(pTarget+nSource, pTarget+nTarget, docListEnd(d)-(pTarget+nTarget));
 
   608   if( nTarget!=nSource ){
 
   609     int iDoclist = pTarget-d->pData;
 
   610     d->pData = realloc(d->pData, d->nData+nSource-nTarget);
 
   611     pTarget = d->pData+iDoclist;
 
   613   if( nTarget<nSource ){
 
   614     memmove(pTarget+nSource, pTarget+nTarget, docListEnd(d)-(pTarget+nTarget));
 
   617   memcpy(pTarget, pSource, nSource);
 
   618   d->nData += nSource-nTarget;
 
   619   r->p = pTarget+nSource;
 
   622 /* Insert/update pUpdate into the doclist. */
 
   623 static void docListUpdate(DocList *d, DocList *pUpdate){
 
   624   DocListReader reader;
 
   626   assert( d!=NULL && pUpdate!=NULL );
 
   627   assert( d->iType==pUpdate->iType);
 
   629   readerInit(&reader, d);
 
   630   docListSpliceElement(&reader, firstDocid(pUpdate),
 
   631                        pUpdate->pData, pUpdate->nData);
 
   634 /* Propagate elements from pUpdate to pAcc, overwriting elements with
 
   637 static void docListAccumulate(DocList *pAcc, DocList *pUpdate){
 
   638   DocListReader accReader, updateReader;
 
   640   /* Handle edge cases where one doclist is empty. */
 
   641   assert( pAcc!=NULL );
 
   642   if( pUpdate==NULL || pUpdate->nData==0 ) return;
 
   643   if( pAcc->nData==0 ){
 
   644     pAcc->pData = malloc(pUpdate->nData);
 
   645     memcpy(pAcc->pData, pUpdate->pData, pUpdate->nData);
 
   646     pAcc->nData = pUpdate->nData;
 
   650   readerInit(&accReader, pAcc);
 
   651   readerInit(&updateReader, pUpdate);
 
   653   while( !atEnd(&updateReader) ){
 
   654     char *pSource = updateReader.p;
 
   655     sqlite_int64 iDocid = readDocid(&updateReader);
 
   656     skipPositionList(&updateReader);
 
   657     docListSpliceElement(&accReader, iDocid, pSource, updateReader.p-pSource);
 
   662 ** Read the next docid off of pIn.  Return 0 if we reach the end.
 
   664 * TODO: This assumes that docids are never 0, but they may actually be 0 since
 
   665 * users can choose docids when inserting into a full-text table.  Fix this.
 
   667 static sqlite_int64 nextDocid(DocListReader *pIn){
 
   668   skipPositionList(pIn);
 
   669   return atEnd(pIn) ? 0 : readDocid(pIn);
 
   673 ** pLeft and pRight are two DocListReaders that are pointing to
 
   674 ** positions lists of the same document: iDocid. 
 
   676 ** If there are no instances in pLeft or pRight where the position
 
   677 ** of pLeft is one less than the position of pRight, then this
 
   678 ** routine adds nothing to pOut.
 
   680 ** If there are one or more instances where positions from pLeft
 
   681 ** are exactly one less than positions from pRight, then add a new
 
   682 ** document record to pOut.  If pOut wants to hold positions, then
 
   683 ** include the positions from pRight that are one more than a
 
   684 ** position in pLeft.  In other words:  pRight.iPos==pLeft.iPos+1.
 
   686 ** pLeft and pRight are left pointing at the next document record.
 
   688 static void mergePosList(
 
   689   DocListReader *pLeft,    /* Left position list */
 
   690   DocListReader *pRight,   /* Right position list */
 
   691   sqlite_int64 iDocid,     /* The docid from pLeft and pRight */
 
   692   DocList *pOut            /* Write the merged document record here */
 
   694   int iLeftCol, iLeftPos = readPosition(pLeft, &iLeftCol);
 
   695   int iRightCol, iRightPos = readPosition(pRight, &iRightCol);
 
   698   /* Loop until we've reached the end of both position lists. */
 
   699   while( iLeftPos!=-1 && iRightPos!=-1 ){
 
   700     if( iLeftCol==iRightCol && iLeftPos+1==iRightPos ){
 
   702         docListAddDocid(pOut, iDocid);
 
   705       if( pOut->iType>=DL_POSITIONS ){
 
   706         docListAddPos(pOut, iRightCol, iRightPos);
 
   708       iLeftPos = readPosition(pLeft, &iLeftCol);
 
   709       iRightPos = readPosition(pRight, &iRightCol);
 
   710     }else if( iRightCol<iLeftCol ||
 
   711               (iRightCol==iLeftCol && iRightPos<iLeftPos+1) ){
 
   712       iRightPos = readPosition(pRight, &iRightCol);
 
   714       iLeftPos = readPosition(pLeft, &iLeftCol);
 
   717   if( iLeftPos>=0 ) skipPositionList(pLeft);
 
   718   if( iRightPos>=0 ) skipPositionList(pRight);
 
   721 /* We have two doclists:  pLeft and pRight.
 
   722 ** Write the phrase intersection of these two doclists into pOut.
 
   724 ** A phrase intersection means that two documents only match
 
   725 ** if pLeft.iPos+1==pRight.iPos.
 
   727 ** The output pOut may or may not contain positions.  If pOut
 
   728 ** does contain positions, they are the positions of pRight.
 
   730 static void docListPhraseMerge(
 
   731   DocList *pLeft,    /* Doclist resulting from the words on the left */
 
   732   DocList *pRight,   /* Doclist for the next word to the right */
 
   733   DocList *pOut      /* Write the combined doclist here */
 
   735   DocListReader left, right;
 
   736   sqlite_int64 docidLeft, docidRight;
 
   738   readerInit(&left, pLeft);
 
   739   readerInit(&right, pRight);
 
   740   docidLeft = nextDocid(&left);
 
   741   docidRight = nextDocid(&right);
 
   743   while( docidLeft>0 && docidRight>0 ){
 
   744     if( docidLeft<docidRight ){
 
   745       docidLeft = nextDocid(&left);
 
   746     }else if( docidRight<docidLeft ){
 
   747       docidRight = nextDocid(&right);
 
   749       mergePosList(&left, &right, docidLeft, pOut);
 
   750       docidLeft = nextDocid(&left);
 
   751       docidRight = nextDocid(&right);
 
   756 /* We have two doclists:  pLeft and pRight.
 
   757 ** Write the intersection of these two doclists into pOut.
 
   758 ** Only docids are matched.  Position information is ignored.
 
   760 ** The output pOut never holds positions.
 
   762 static void docListAndMerge(
 
   763   DocList *pLeft,    /* Doclist resulting from the words on the left */
 
   764   DocList *pRight,   /* Doclist for the next word to the right */
 
   765   DocList *pOut      /* Write the combined doclist here */
 
   767   DocListReader left, right;
 
   768   sqlite_int64 docidLeft, docidRight;
 
   770   assert( pOut->iType<DL_POSITIONS );
 
   772   readerInit(&left, pLeft);
 
   773   readerInit(&right, pRight);
 
   774   docidLeft = nextDocid(&left);
 
   775   docidRight = nextDocid(&right);
 
   777   while( docidLeft>0 && docidRight>0 ){
 
   778     if( docidLeft<docidRight ){
 
   779       docidLeft = nextDocid(&left);
 
   780     }else if( docidRight<docidLeft ){
 
   781       docidRight = nextDocid(&right);
 
   783       docListAddDocid(pOut, docidLeft);
 
   784       docidLeft = nextDocid(&left);
 
   785       docidRight = nextDocid(&right);
 
   790 /* We have two doclists:  pLeft and pRight.
 
   791 ** Write the union of these two doclists into pOut.
 
   792 ** Only docids are matched.  Position information is ignored.
 
   794 ** The output pOut never holds positions.
 
   796 static void docListOrMerge(
 
   797   DocList *pLeft,    /* Doclist resulting from the words on the left */
 
   798   DocList *pRight,   /* Doclist for the next word to the right */
 
   799   DocList *pOut      /* Write the combined doclist here */
 
   801   DocListReader left, right;
 
   802   sqlite_int64 docidLeft, docidRight, priorLeft;
 
   804   readerInit(&left, pLeft);
 
   805   readerInit(&right, pRight);
 
   806   docidLeft = nextDocid(&left);
 
   807   docidRight = nextDocid(&right);
 
   809   while( docidLeft>0 && docidRight>0 ){
 
   810     if( docidLeft<=docidRight ){
 
   811       docListAddDocid(pOut, docidLeft);
 
   813       docListAddDocid(pOut, docidRight);
 
   815     priorLeft = docidLeft;
 
   816     if( docidLeft<=docidRight ){
 
   817       docidLeft = nextDocid(&left);
 
   819     if( docidRight>0 && docidRight<=priorLeft ){
 
   820       docidRight = nextDocid(&right);
 
   823   while( docidLeft>0 ){
 
   824     docListAddDocid(pOut, docidLeft);
 
   825     docidLeft = nextDocid(&left);
 
   827   while( docidRight>0 ){
 
   828     docListAddDocid(pOut, docidRight);
 
   829     docidRight = nextDocid(&right);
 
   833 /* We have two doclists:  pLeft and pRight.
 
   834 ** Write into pOut all documents that occur in pLeft but not
 
   837 ** Only docids are matched.  Position information is ignored.
 
   839 ** The output pOut never holds positions.
 
   841 static void docListExceptMerge(
 
   842   DocList *pLeft,    /* Doclist resulting from the words on the left */
 
   843   DocList *pRight,   /* Doclist for the next word to the right */
 
   844   DocList *pOut      /* Write the combined doclist here */
 
   846   DocListReader left, right;
 
   847   sqlite_int64 docidLeft, docidRight, priorLeft;
 
   849   readerInit(&left, pLeft);
 
   850   readerInit(&right, pRight);
 
   851   docidLeft = nextDocid(&left);
 
   852   docidRight = nextDocid(&right);
 
   854   while( docidLeft>0 && docidRight>0 ){
 
   855     priorLeft = docidLeft;
 
   856     if( docidLeft<docidRight ){
 
   857       docListAddDocid(pOut, docidLeft);
 
   859     if( docidLeft<=docidRight ){
 
   860       docidLeft = nextDocid(&left);
 
   862     if( docidRight>0 && docidRight<=priorLeft ){
 
   863       docidRight = nextDocid(&right);
 
   866   while( docidLeft>0 ){
 
   867     docListAddDocid(pOut, docidLeft);
 
   868     docidLeft = nextDocid(&left);
 
   872 static char *string_dup_n(const char *s, int n){
 
   873   char *str = malloc(n + 1);
 
   879 /* Duplicate a string; the caller must free() the returned string.
 
   880  * (We don't use strdup() since it is not part of the standard C library and
 
   881  * may not be available everywhere.) */
 
   882 static char *string_dup(const char *s){
 
   883   return string_dup_n(s, strlen(s));
 
   886 /* Format a string, replacing each occurrence of the % character with
 
   887  * zDb.zName.  This may be more convenient than sqlite_mprintf()
 
   888  * when one string is used repeatedly in a format string.
 
   889  * The caller must free() the returned string. */
 
   890 static char *string_format(const char *zFormat,
 
   891                            const char *zDb, const char *zName){
 
   894   size_t nDb = strlen(zDb);
 
   895   size_t nName = strlen(zName);
 
   896   size_t nFullTableName = nDb+1+nName;
 
   900   /* first compute length needed */
 
   901   for(p = zFormat ; *p ; ++p){
 
   902     len += (*p=='%' ? nFullTableName : 1);
 
   904   len += 1;  /* for null terminator */
 
   906   r = result = malloc(len);
 
   907   for(p = zFormat; *p; ++p){
 
   912       memcpy(r, zName, nName);
 
   919   assert( r == result + len );
 
   923 static int sql_exec(sqlite3 *db, const char *zDb, const char *zName,
 
   924                     const char *zFormat){
 
   925   char *zCommand = string_format(zFormat, zDb, zName);
 
   927   TRACE(("FTS1 sql: %s\n", zCommand));
 
   928   rc = sqlite3_exec(db, zCommand, NULL, 0, NULL);
 
   933 static int sql_prepare(sqlite3 *db, const char *zDb, const char *zName,
 
   934                        sqlite3_stmt **ppStmt, const char *zFormat){
 
   935   char *zCommand = string_format(zFormat, zDb, zName);
 
   937   TRACE(("FTS1 prepare: %s\n", zCommand));
 
   938   rc = sqlite3_prepare(db, zCommand, -1, ppStmt, NULL);
 
   943 /* end utility functions */
 
   945 /* Forward reference */
 
   946 typedef struct fulltext_vtab fulltext_vtab;
 
   948 /* A single term in a query is represented by an instances of
 
   949 ** the following structure.
 
   951 typedef struct QueryTerm {
 
   952   short int nPhrase; /* How many following terms are part of the same phrase */
 
   953   short int iPhrase; /* This is the i-th term of a phrase. */
 
   954   short int iColumn; /* Column of the index that must match this term */
 
   955   signed char isOr;  /* this term is preceded by "OR" */
 
   956   signed char isNot; /* this term is preceded by "-" */
 
   957   char *pTerm;       /* text of the term.  '\000' terminated.  malloced */
 
   958   int nTerm;         /* Number of bytes in pTerm[] */
 
   962 /* A query string is parsed into a Query structure.
 
   964  * We could, in theory, allow query strings to be complicated
 
   965  * nested expressions with precedence determined by parentheses.
 
   966  * But none of the major search engines do this.  (Perhaps the
 
   967  * feeling is that an parenthesized expression is two complex of
 
   968  * an idea for the average user to grasp.)  Taking our lead from
 
   969  * the major search engines, we will allow queries to be a list
 
   970  * of terms (with an implied AND operator) or phrases in double-quotes,
 
   971  * with a single optional "-" before each non-phrase term to designate
 
   972  * negation and an optional OR connector.
 
   974  * OR binds more tightly than the implied AND, which is what the
 
   975  * major search engines seem to do.  So, for example:
 
   977  *    [one two OR three]     ==>    one AND (two OR three)
 
   978  *    [one OR two three]     ==>    (one OR two) AND three
 
   980  * A "-" before a term matches all entries that lack that term.
 
   981  * The "-" must occur immediately before the term with in intervening
 
   982  * space.  This is how the search engines do it.
 
   984  * A NOT term cannot be the right-hand operand of an OR.  If this
 
   985  * occurs in the query string, the NOT is ignored:
 
   987  *    [one OR -two]          ==>    one OR two
 
   990 typedef struct Query {
 
   991   fulltext_vtab *pFts;  /* The full text index */
 
   992   int nTerms;           /* Number of terms in the query */
 
   993   QueryTerm *pTerms;    /* Array of terms.  Space obtained from malloc() */
 
   994   int nextIsOr;         /* Set the isOr flag on the next inserted term */
 
   995   int nextColumn;       /* Next word parsed must be in this column */
 
   996   int dfltColumn;       /* The default column */
 
  1001 ** An instance of the following structure keeps track of generated
 
  1002 ** matching-word offset information and snippets.
 
  1004 typedef struct Snippet {
 
  1005   int nMatch;     /* Total number of matches */
 
  1006   int nAlloc;     /* Space allocated for aMatch[] */
 
  1007   struct snippetMatch { /* One entry for each matching term */
 
  1008     char snStatus;       /* Status flag for use while constructing snippets */
 
  1009     short int iCol;      /* The column that contains the match */
 
  1010     short int iTerm;     /* The index in Query.pTerms[] of the matching term */
 
  1011     short int nByte;     /* Number of bytes in the term */
 
  1012     int iStart;          /* The offset to the first character of the term */
 
  1013   } *aMatch;      /* Points to space obtained from malloc */
 
  1014   char *zOffset;  /* Text rendering of aMatch[] */
 
  1015   int nOffset;    /* strlen(zOffset) */
 
  1016   char *zSnippet; /* Snippet text */
 
  1017   int nSnippet;   /* strlen(zSnippet) */
 
  1021 typedef enum QueryType {
 
  1022   QUERY_GENERIC,   /* table scan */
 
  1023   QUERY_ROWID,     /* lookup by rowid */
 
  1024   QUERY_FULLTEXT   /* QUERY_FULLTEXT + [i] is a full-text search for column i*/
 
  1027 /* TODO(shess) CHUNK_MAX controls how much data we allow in segment 0
 
  1028 ** before we start aggregating into larger segments.  Lower CHUNK_MAX
 
  1029 ** means that for a given input we have more individual segments per
 
  1030 ** term, which means more rows in the table and a bigger index (due to
 
  1031 ** both more rows and bigger rowids).  But it also reduces the average
 
  1032 ** cost of adding new elements to the segment 0 doclist, and it seems
 
  1033 ** to reduce the number of pages read and written during inserts.  256
 
  1034 ** was chosen by measuring insertion times for a certain input (first
 
  1035 ** 10k documents of Enron corpus), though including query performance
 
  1036 ** in the decision may argue for a larger value.
 
  1038 #define CHUNK_MAX 256
 
  1040 typedef enum fulltext_statement {
 
  1041   CONTENT_INSERT_STMT,
 
  1042   CONTENT_SELECT_STMT,
 
  1043   CONTENT_UPDATE_STMT,
 
  1044   CONTENT_DELETE_STMT,
 
  1047   TERM_SELECT_ALL_STMT,
 
  1052   MAX_STMT                     /* Always at end! */
 
  1053 } fulltext_statement;
 
  1055 /* These must exactly match the enum above. */
 
  1056 /* TODO(adam): Is there some risk that a statement (in particular,
 
  1057 ** pTermSelectStmt) will be used in two cursors at once, e.g.  if a
 
  1058 ** query joins a virtual table to itself?  If so perhaps we should
 
  1059 ** move some of these to the cursor object.
 
  1061 static const char *const fulltext_zStatement[MAX_STMT] = {
 
  1062   /* CONTENT_INSERT */ NULL,  /* generated in contentInsertStatement() */
 
  1063   /* CONTENT_SELECT */ "select * from %_content where rowid = ?",
 
  1064   /* CONTENT_UPDATE */ NULL,  /* generated in contentUpdateStatement() */
 
  1065   /* CONTENT_DELETE */ "delete from %_content where rowid = ?",
 
  1068   "select rowid, doclist from %_term where term = ? and segment = ?",
 
  1069   /* TERM_SELECT_ALL */
 
  1070   "select doclist from %_term where term = ? order by segment",
 
  1072   "insert into %_term (rowid, term, segment, doclist) values (?, ?, ?, ?)",
 
  1073   /* TERM_UPDATE */ "update %_term set doclist = ? where rowid = ?",
 
  1074   /* TERM_DELETE */ "delete from %_term where rowid = ?",
 
  1078 ** A connection to a fulltext index is an instance of the following
 
  1079 ** structure.  The xCreate and xConnect methods create an instance
 
  1080 ** of this structure and xDestroy and xDisconnect free that instance.
 
  1081 ** All other methods receive a pointer to the structure as one of their
 
  1084 struct fulltext_vtab {
 
  1085   sqlite3_vtab base;               /* Base class used by SQLite core */
 
  1086   sqlite3 *db;                     /* The database connection */
 
  1087   const char *zDb;                 /* logical database name */
 
  1088   const char *zName;               /* virtual table name */
 
  1089   int nColumn;                     /* number of columns in virtual table */
 
  1090   char **azColumn;                 /* column names.  malloced */
 
  1091   char **azContentColumn;          /* column names in content table; malloced */
 
  1092   sqlite3_tokenizer *pTokenizer;   /* tokenizer for inserts and queries */
 
  1094   /* Precompiled statements which we keep as long as the table is
 
  1097   sqlite3_stmt *pFulltextStatements[MAX_STMT];
 
  1101 ** When the core wants to do a query, it create a cursor using a
 
  1102 ** call to xOpen.  This structure is an instance of a cursor.  It
 
  1103 ** is destroyed by xClose.
 
  1105 typedef struct fulltext_cursor {
 
  1106   sqlite3_vtab_cursor base;        /* Base class used by SQLite core */
 
  1107   QueryType iCursorType;           /* Copy of sqlite3_index_info.idxNum */
 
  1108   sqlite3_stmt *pStmt;             /* Prepared statement in use by the cursor */
 
  1109   int eof;                         /* True if at End Of Results */
 
  1110   Query q;                         /* Parsed query string */
 
  1111   Snippet snippet;                 /* Cached snippet for the current row */
 
  1112   int iColumn;                     /* Column being searched */
 
  1113   DocListReader result;  /* used when iCursorType == QUERY_FULLTEXT */ 
 
  1116 static struct fulltext_vtab *cursor_vtab(fulltext_cursor *c){
 
  1117   return (fulltext_vtab *) c->base.pVtab;
 
  1120 static const sqlite3_module fulltextModule;   /* forward declaration */
 
  1122 /* Append a list of strings separated by commas to a StringBuffer. */
 
  1123 static void appendList(StringBuffer *sb, int nString, char **azString){
 
  1125   for(i=0; i<nString; ++i){
 
  1126     if( i>0 ) append(sb, ", ");
 
  1127     append(sb, azString[i]);
 
  1131 /* Return a dynamically generated statement of the form
 
  1132  *   insert into %_content (rowid, ...) values (?, ...)
 
  1134 static const char *contentInsertStatement(fulltext_vtab *v){
 
  1138   initStringBuffer(&sb);
 
  1139   append(&sb, "insert into %_content (rowid, ");
 
  1140   appendList(&sb, v->nColumn, v->azContentColumn);
 
  1141   append(&sb, ") values (?");
 
  1142   for(i=0; i<v->nColumn; ++i)
 
  1148 /* Return a dynamically generated statement of the form
 
  1149  *   update %_content set [col_0] = ?, [col_1] = ?, ...
 
  1152 static const char *contentUpdateStatement(fulltext_vtab *v){
 
  1156   initStringBuffer(&sb);
 
  1157   append(&sb, "update %_content set ");
 
  1158   for(i=0; i<v->nColumn; ++i) {
 
  1162     append(&sb, v->azContentColumn[i]);
 
  1163     append(&sb, " = ?");
 
  1165   append(&sb, " where rowid = ?");
 
  1169 /* Puts a freshly-prepared statement determined by iStmt in *ppStmt.
 
  1170 ** If the indicated statement has never been prepared, it is prepared
 
  1171 ** and cached, otherwise the cached version is reset.
 
  1173 static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt,
 
  1174                              sqlite3_stmt **ppStmt){
 
  1175   assert( iStmt<MAX_STMT );
 
  1176   if( v->pFulltextStatements[iStmt]==NULL ){
 
  1180       case CONTENT_INSERT_STMT:
 
  1181         zStmt = contentInsertStatement(v); break;
 
  1182       case CONTENT_UPDATE_STMT:
 
  1183         zStmt = contentUpdateStatement(v); break;
 
  1185         zStmt = fulltext_zStatement[iStmt];
 
  1187     rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt],
 
  1189     if( zStmt != fulltext_zStatement[iStmt]) free((void *) zStmt);
 
  1190     if( rc!=SQLITE_OK ) return rc;
 
  1192     int rc = sqlite3_reset(v->pFulltextStatements[iStmt]);
 
  1193     if( rc!=SQLITE_OK ) return rc;
 
  1196   *ppStmt = v->pFulltextStatements[iStmt];
 
  1200 /* Step the indicated statement, handling errors SQLITE_BUSY (by
 
  1201 ** retrying) and SQLITE_SCHEMA (by re-preparing and transferring
 
  1202 ** bindings to the new statement).
 
  1203 ** TODO(adam): We should extend this function so that it can work with
 
  1204 ** statements declared locally, not only globally cached statements.
 
  1206 static int sql_step_statement(fulltext_vtab *v, fulltext_statement iStmt,
 
  1207                               sqlite3_stmt **ppStmt){
 
  1209   sqlite3_stmt *s = *ppStmt;
 
  1210   assert( iStmt<MAX_STMT );
 
  1211   assert( s==v->pFulltextStatements[iStmt] );
 
  1213   while( (rc=sqlite3_step(s))!=SQLITE_DONE && rc!=SQLITE_ROW ){
 
  1214     if( rc==SQLITE_BUSY ) continue;
 
  1215     if( rc!=SQLITE_ERROR ) return rc;
 
  1217     /* If an SQLITE_SCHEMA error has occured, then finalizing this
 
  1218      * statement is going to delete the fulltext_vtab structure. If
 
  1219      * the statement just executed is in the pFulltextStatements[]
 
  1220      * array, it will be finalized twice. So remove it before
 
  1221      * calling sqlite3_finalize().
 
  1223     v->pFulltextStatements[iStmt] = NULL;
 
  1224     rc = sqlite3_finalize(s);
 
  1230   sqlite3_finalize(s);
 
  1234 /* Like sql_step_statement(), but convert SQLITE_DONE to SQLITE_OK.
 
  1235 ** Useful for statements like UPDATE, where we expect no results.
 
  1237 static int sql_single_step_statement(fulltext_vtab *v,
 
  1238                                      fulltext_statement iStmt,
 
  1239                                      sqlite3_stmt **ppStmt){
 
  1240   int rc = sql_step_statement(v, iStmt, ppStmt);
 
  1241   return (rc==SQLITE_DONE) ? SQLITE_OK : rc;
 
  1244 /* insert into %_content (rowid, ...) values ([rowid], [pValues]) */
 
  1245 static int content_insert(fulltext_vtab *v, sqlite3_value *rowid,
 
  1246                           sqlite3_value **pValues){
 
  1249   int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s);
 
  1250   if( rc!=SQLITE_OK ) return rc;
 
  1252   rc = sqlite3_bind_value(s, 1, rowid);
 
  1253   if( rc!=SQLITE_OK ) return rc;
 
  1255   for(i=0; i<v->nColumn; ++i){
 
  1256     rc = sqlite3_bind_value(s, 2+i, pValues[i]);
 
  1257     if( rc!=SQLITE_OK ) return rc;
 
  1260   return sql_single_step_statement(v, CONTENT_INSERT_STMT, &s);
 
  1263 /* update %_content set col0 = pValues[0], col1 = pValues[1], ...
 
  1264  *                  where rowid = [iRowid] */
 
  1265 static int content_update(fulltext_vtab *v, sqlite3_value **pValues,
 
  1266                           sqlite_int64 iRowid){
 
  1269   int rc = sql_get_statement(v, CONTENT_UPDATE_STMT, &s);
 
  1270   if( rc!=SQLITE_OK ) return rc;
 
  1272   for(i=0; i<v->nColumn; ++i){
 
  1273     rc = sqlite3_bind_value(s, 1+i, pValues[i]);
 
  1274     if( rc!=SQLITE_OK ) return rc;
 
  1277   rc = sqlite3_bind_int64(s, 1+v->nColumn, iRowid);
 
  1278   if( rc!=SQLITE_OK ) return rc;
 
  1280   return sql_single_step_statement(v, CONTENT_UPDATE_STMT, &s);
 
  1283 static void freeStringArray(int nString, const char **pString){
 
  1286   for (i=0 ; i < nString ; ++i) {
 
  1287     if( pString[i]!=NULL ) free((void *) pString[i]);
 
  1289   free((void *) pString);
 
  1292 /* select * from %_content where rowid = [iRow]
 
  1293  * The caller must delete the returned array and all strings in it.
 
  1294  * null fields will be NULL in the returned array.
 
  1296  * TODO: Perhaps we should return pointer/length strings here for consistency
 
  1297  * with other code which uses pointer/length. */
 
  1298 static int content_select(fulltext_vtab *v, sqlite_int64 iRow,
 
  1299                           const char ***pValues){
 
  1301   const char **values;
 
  1307   rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s);
 
  1308   if( rc!=SQLITE_OK ) return rc;
 
  1310   rc = sqlite3_bind_int64(s, 1, iRow);
 
  1311   if( rc!=SQLITE_OK ) return rc;
 
  1313   rc = sql_step_statement(v, CONTENT_SELECT_STMT, &s);
 
  1314   if( rc!=SQLITE_ROW ) return rc;
 
  1316   values = (const char **) malloc(v->nColumn * sizeof(const char *));
 
  1317   for(i=0; i<v->nColumn; ++i){
 
  1318     if( sqlite3_column_type(s, i)==SQLITE_NULL ){
 
  1321       values[i] = string_dup((char*)sqlite3_column_text(s, i));
 
  1325   /* We expect only one row.  We must execute another sqlite3_step()
 
  1326    * to complete the iteration; otherwise the table will remain locked. */
 
  1327   rc = sqlite3_step(s);
 
  1328   if( rc==SQLITE_DONE ){
 
  1333   freeStringArray(v->nColumn, values);
 
  1337 /* delete from %_content where rowid = [iRow ] */
 
  1338 static int content_delete(fulltext_vtab *v, sqlite_int64 iRow){
 
  1340   int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s);
 
  1341   if( rc!=SQLITE_OK ) return rc;
 
  1343   rc = sqlite3_bind_int64(s, 1, iRow);
 
  1344   if( rc!=SQLITE_OK ) return rc;
 
  1346   return sql_single_step_statement(v, CONTENT_DELETE_STMT, &s);
 
  1349 /* select rowid, doclist from %_term
 
  1350  *  where term = [pTerm] and segment = [iSegment]
 
  1351  * If found, returns SQLITE_ROW; the caller must free the
 
  1352  * returned doclist.  If no rows found, returns SQLITE_DONE. */
 
  1353 static int term_select(fulltext_vtab *v, const char *pTerm, int nTerm,
 
  1355                        sqlite_int64 *rowid, DocList *out){
 
  1357   int rc = sql_get_statement(v, TERM_SELECT_STMT, &s);
 
  1358   if( rc!=SQLITE_OK ) return rc;
 
  1360   rc = sqlite3_bind_text(s, 1, pTerm, nTerm, SQLITE_STATIC);
 
  1361   if( rc!=SQLITE_OK ) return rc;
 
  1363   rc = sqlite3_bind_int(s, 2, iSegment);
 
  1364   if( rc!=SQLITE_OK ) return rc;
 
  1366   rc = sql_step_statement(v, TERM_SELECT_STMT, &s);
 
  1367   if( rc!=SQLITE_ROW ) return rc;
 
  1369   *rowid = sqlite3_column_int64(s, 0);
 
  1370   docListInit(out, DL_DEFAULT,
 
  1371               sqlite3_column_blob(s, 1), sqlite3_column_bytes(s, 1));
 
  1373   /* We expect only one row.  We must execute another sqlite3_step()
 
  1374    * to complete the iteration; otherwise the table will remain locked. */
 
  1375   rc = sqlite3_step(s);
 
  1376   return rc==SQLITE_DONE ? SQLITE_ROW : rc;
 
  1379 /* Load the segment doclists for term pTerm and merge them in
 
  1380 ** appropriate order into out.  Returns SQLITE_OK if successful.  If
 
  1381 ** there are no segments for pTerm, successfully returns an empty
 
  1384 ** Each document consists of 1 or more "columns".  The number of
 
  1385 ** columns is v->nColumn.  If iColumn==v->nColumn, then return
 
  1386 ** position information about all columns.  If iColumn<v->nColumn,
 
  1387 ** then only return position information about the iColumn-th column
 
  1388 ** (where the first column is 0).
 
  1390 static int term_select_all(
 
  1391   fulltext_vtab *v,     /* The fulltext index we are querying against */
 
  1392   int iColumn,          /* If <nColumn, only look at the iColumn-th column */
 
  1393   const char *pTerm,    /* The term whose posting lists we want */
 
  1394   int nTerm,            /* Number of bytes in pTerm */
 
  1395   DocList *out          /* Write the resulting doclist here */
 
  1399   int rc = sql_get_statement(v, TERM_SELECT_ALL_STMT, &s);
 
  1400   if( rc!=SQLITE_OK ) return rc;
 
  1402   rc = sqlite3_bind_text(s, 1, pTerm, nTerm, SQLITE_STATIC);
 
  1403   if( rc!=SQLITE_OK ) return rc;
 
  1405   docListInit(&doclist, DL_DEFAULT, 0, 0);
 
  1407   /* TODO(shess) Handle schema and busy errors. */
 
  1408   while( (rc=sql_step_statement(v, TERM_SELECT_ALL_STMT, &s))==SQLITE_ROW ){
 
  1411     /* TODO(shess) If we processed doclists from oldest to newest, we
 
  1412     ** could skip the malloc() involved with the following call.  For
 
  1413     ** now, I'd rather keep this logic similar to index_insert_term().
 
  1414     ** We could additionally drop elements when we see deletes, but
 
  1415     ** that would require a distinct version of docListAccumulate().
 
  1417     docListInit(&old, DL_DEFAULT,
 
  1418                 sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0));
 
  1420     if( iColumn<v->nColumn ){   /* querying a single column */
 
  1421       docListRestrictColumn(&old, iColumn);
 
  1424     /* doclist contains the newer data, so write it over old.  Then
 
  1425     ** steal accumulated result for doclist.
 
  1427     docListAccumulate(&old, &doclist);
 
  1428     docListDestroy(&doclist);
 
  1431   if( rc!=SQLITE_DONE ){
 
  1432     docListDestroy(&doclist);
 
  1436   docListDiscardEmpty(&doclist);
 
  1441 /* insert into %_term (rowid, term, segment, doclist)
 
  1442                values ([piRowid], [pTerm], [iSegment], [doclist])
 
  1443 ** Lets sqlite select rowid if piRowid is NULL, else uses *piRowid.
 
  1445 ** NOTE(shess) piRowid is IN, with values of "space of int64" plus
 
  1446 ** null, it is not used to pass data back to the caller.
 
  1448 static int term_insert(fulltext_vtab *v, sqlite_int64 *piRowid,
 
  1449                        const char *pTerm, int nTerm,
 
  1450                        int iSegment, DocList *doclist){
 
  1452   int rc = sql_get_statement(v, TERM_INSERT_STMT, &s);
 
  1453   if( rc!=SQLITE_OK ) return rc;
 
  1455   if( piRowid==NULL ){
 
  1456     rc = sqlite3_bind_null(s, 1);
 
  1458     rc = sqlite3_bind_int64(s, 1, *piRowid);
 
  1460   if( rc!=SQLITE_OK ) return rc;
 
  1462   rc = sqlite3_bind_text(s, 2, pTerm, nTerm, SQLITE_STATIC);
 
  1463   if( rc!=SQLITE_OK ) return rc;
 
  1465   rc = sqlite3_bind_int(s, 3, iSegment);
 
  1466   if( rc!=SQLITE_OK ) return rc;
 
  1468   rc = sqlite3_bind_blob(s, 4, doclist->pData, doclist->nData, SQLITE_STATIC);
 
  1469   if( rc!=SQLITE_OK ) return rc;
 
  1471   return sql_single_step_statement(v, TERM_INSERT_STMT, &s);
 
  1474 /* update %_term set doclist = [doclist] where rowid = [rowid] */
 
  1475 static int term_update(fulltext_vtab *v, sqlite_int64 rowid,
 
  1478   int rc = sql_get_statement(v, TERM_UPDATE_STMT, &s);
 
  1479   if( rc!=SQLITE_OK ) return rc;
 
  1481   rc = sqlite3_bind_blob(s, 1, doclist->pData, doclist->nData, SQLITE_STATIC);
 
  1482   if( rc!=SQLITE_OK ) return rc;
 
  1484   rc = sqlite3_bind_int64(s, 2, rowid);
 
  1485   if( rc!=SQLITE_OK ) return rc;
 
  1487   return sql_single_step_statement(v, TERM_UPDATE_STMT, &s);
 
  1490 static int term_delete(fulltext_vtab *v, sqlite_int64 rowid){
 
  1492   int rc = sql_get_statement(v, TERM_DELETE_STMT, &s);
 
  1493   if( rc!=SQLITE_OK ) return rc;
 
  1495   rc = sqlite3_bind_int64(s, 1, rowid);
 
  1496   if( rc!=SQLITE_OK ) return rc;
 
  1498   return sql_single_step_statement(v, TERM_DELETE_STMT, &s);
 
  1502 ** Free the memory used to contain a fulltext_vtab structure.
 
  1504 static void fulltext_vtab_destroy(fulltext_vtab *v){
 
  1507   TRACE(("FTS1 Destroy %p\n", v));
 
  1508   for( iStmt=0; iStmt<MAX_STMT; iStmt++ ){
 
  1509     if( v->pFulltextStatements[iStmt]!=NULL ){
 
  1510       sqlite3_finalize(v->pFulltextStatements[iStmt]);
 
  1511       v->pFulltextStatements[iStmt] = NULL;
 
  1515   if( v->pTokenizer!=NULL ){
 
  1516     v->pTokenizer->pModule->xDestroy(v->pTokenizer);
 
  1517     v->pTokenizer = NULL;
 
  1521   for(i = 0; i < v->nColumn; ++i) {
 
  1522     sqlite3_free(v->azContentColumn[i]);
 
  1524   free(v->azContentColumn);
 
  1529 ** Token types for parsing the arguments to xConnect or xCreate.
 
  1531 #define TOKEN_EOF         0    /* End of file */
 
  1532 #define TOKEN_SPACE       1    /* Any kind of whitespace */
 
  1533 #define TOKEN_ID          2    /* An identifier */
 
  1534 #define TOKEN_STRING      3    /* A string literal */
 
  1535 #define TOKEN_PUNCT       4    /* A single punctuation character */
 
  1538 ** If X is a character that can be used in an identifier then
 
  1539 ** IdChar(X) will be true.  Otherwise it is false.
 
  1541 ** For ASCII, any character with the high-order bit set is
 
  1542 ** allowed in an identifier.  For 7-bit characters, 
 
  1543 ** sqlite3IsIdChar[X] must be 1.
 
  1545 ** Ticket #1066.  the SQL standard does not allow '$' in the
 
  1546 ** middle of identfiers.  But many SQL implementations do. 
 
  1547 ** SQLite will allow '$' in identifiers for compatibility.
 
  1548 ** But the feature is undocumented.
 
  1550 static const char isIdChar[] = {
 
  1551 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
 
  1552     0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
 
  1553     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
 
  1554     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
 
  1555     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
 
  1556     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
 
  1557     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
 
  1559 #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20]))
 
  1563 ** Return the length of the token that begins at z[0]. 
 
  1564 ** Store the token type in *tokenType before returning.
 
  1566 static int getToken(const char *z, int *tokenType){
 
  1570       *tokenType = TOKEN_EOF;
 
  1573     case ' ': case '\t': case '\n': case '\f': case '\r': {
 
  1574       for(i=1; safe_isspace(z[i]); i++){}
 
  1575       *tokenType = TOKEN_SPACE;
 
  1582       for(i=1; (c=z[i])!=0; i++){
 
  1584           if( z[i+1]==delim ){
 
  1591       *tokenType = TOKEN_STRING;
 
  1595       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
 
  1596       *tokenType = TOKEN_ID;
 
  1603       for(i=1; IdChar(z[i]); i++){}
 
  1604       *tokenType = TOKEN_ID;
 
  1608   *tokenType = TOKEN_PUNCT;
 
  1613 ** A token extracted from a string is an instance of the following
 
  1616 typedef struct Token {
 
  1617   const char *z;       /* Pointer to token text.  Not '\000' terminated */
 
  1618   short int n;         /* Length of the token text in bytes. */
 
  1622 ** Given a input string (which is really one of the argv[] parameters
 
  1623 ** passed into xConnect or xCreate) split the string up into tokens.
 
  1624 ** Return an array of pointers to '\000' terminated strings, one string
 
  1625 ** for each non-whitespace token.
 
  1627 ** The returned array is terminated by a single NULL pointer.
 
  1629 ** Space to hold the returned array is obtained from a single
 
  1630 ** malloc and should be freed by passing the return value to free().
 
  1631 ** The individual strings within the token list are all a part of
 
  1632 ** the single memory allocation and will all be freed at once.
 
  1634 static char **tokenizeString(const char *z, int *pnToken){
 
  1636   Token *aToken = malloc( strlen(z) * sizeof(aToken[0]) );
 
  1643     n = getToken(z, &e);
 
  1644     if( e!=TOKEN_SPACE ){
 
  1645       aToken[nToken].z = z;
 
  1646       aToken[nToken].n = n;
 
  1652   azToken = (char**)malloc( nToken*sizeof(char*) + totalSize );
 
  1653   zCopy = (char*)&azToken[nToken];
 
  1655   for(i=0; i<nToken; i++){
 
  1658     memcpy(zCopy, aToken[i].z, n);
 
  1662   azToken[nToken] = 0;
 
  1669 ** Convert an SQL-style quoted string into a normal string by removing
 
  1670 ** the quote characters.  The conversion is done in-place.  If the
 
  1671 ** input does not begin with a quote character, then this routine
 
  1676 **     "abc"   becomes   abc
 
  1677 **     'xyz'   becomes   xyz
 
  1678 **     [pqr]   becomes   pqr
 
  1679 **     `mno`   becomes   mno
 
  1681 static void dequoteString(char *z){
 
  1689     case '`':   break;                /* For MySQL compatibility */
 
  1690     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
 
  1693   for(i=1, j=0; z[i]; i++){
 
  1695       if( z[i+1]==quote ){
 
  1709 ** The input azIn is a NULL-terminated list of tokens.  Remove the first
 
  1710 ** token and all punctuation tokens.  Remove the quotes from
 
  1711 ** around string literal tokens.
 
  1715 **     input:      tokenize chinese ( 'simplifed' , 'mixed' )
 
  1716 **     output:     chinese simplifed mixed
 
  1720 **     input:      delimiters ( '[' , ']' , '...' )
 
  1723 static void tokenListToIdList(char **azIn){
 
  1726     for(i=0, j=-1; azIn[i]; i++){
 
  1727       if( safe_isalnum(azIn[i][0]) || azIn[i][1] ){
 
  1728         dequoteString(azIn[i]);
 
  1741 ** Find the first alphanumeric token in the string zIn.  Null-terminate
 
  1742 ** this token.  Remove any quotation marks.  And return a pointer to
 
  1745 static char *firstToken(char *zIn, char **pzTail){
 
  1748     n = getToken(zIn, &ttype);
 
  1749     if( ttype==TOKEN_SPACE ){
 
  1751     }else if( ttype==TOKEN_EOF ){
 
  1764 /* Return true if...
 
  1766 **   *  s begins with the string t, ignoring case
 
  1767 **   *  s is longer than t
 
  1768 **   *  The first character of s beyond t is not a alphanumeric
 
  1770 ** Ignore leading space in *s.
 
  1772 ** To put it another way, return true if the first token of
 
  1775 static int startsWith(const char *s, const char *t){
 
  1776   while( safe_isspace(*s) ){ s++; }
 
  1778     if( safe_tolower(*s++)!=safe_tolower(*t++) ) return 0;
 
  1780   return *s!='_' && !safe_isalnum(*s);
 
  1784 ** An instance of this structure defines the "spec" of a
 
  1785 ** full text index.  This structure is populated by parseSpec
 
  1786 ** and use by fulltextConnect and fulltextCreate.
 
  1788 typedef struct TableSpec {
 
  1789   const char *zDb;         /* Logical database name */
 
  1790   const char *zName;       /* Name of the full-text index */
 
  1791   int nColumn;             /* Number of columns to be indexed */
 
  1792   char **azColumn;         /* Original names of columns to be indexed */
 
  1793   char **azContentColumn;  /* Column names for %_content */
 
  1794   char **azTokenizer;      /* Name of tokenizer and its arguments */
 
  1798 ** Reclaim all of the memory used by a TableSpec
 
  1800 static void clearTableSpec(TableSpec *p) {
 
  1802   free(p->azContentColumn);
 
  1803   free(p->azTokenizer);
 
  1806 /* Parse a CREATE VIRTUAL TABLE statement, which looks like this:
 
  1808  * CREATE VIRTUAL TABLE email
 
  1809  *        USING fts1(subject, body, tokenize mytokenizer(myarg))
 
  1811  * We return parsed information in a TableSpec structure.
 
  1814 static int parseSpec(TableSpec *pSpec, int argc, const char *const*argv,
 
  1819   const char *zTokenizer = 0;    /* argv[] entry describing the tokenizer */
 
  1822   /* Current interface:
 
  1823   ** argv[0] - module name
 
  1824   ** argv[1] - database name
 
  1825   ** argv[2] - table name
 
  1826   ** argv[3..] - columns, optionally followed by tokenizer specification
 
  1827   **             and snippet delimiters specification.
 
  1830   /* Make a copy of the complete argv[][] array in a single allocation.
 
  1831   ** The argv[][] array is read-only and transient.  We can write to the
 
  1832   ** copy in order to modify things and the copy is persistent.
 
  1834   memset(pSpec, 0, sizeof(*pSpec));
 
  1835   for(i=n=0; i<argc; i++){
 
  1836     n += strlen(argv[i]) + 1;
 
  1838   azArg = malloc( sizeof(char*)*argc + n );
 
  1840     return SQLITE_NOMEM;
 
  1842   z = (char*)&azArg[argc];
 
  1843   for(i=0; i<argc; i++){
 
  1849   /* Identify the column names and the tokenizer and delimiter arguments
 
  1850   ** in the argv[][] array.
 
  1852   pSpec->zDb = azArg[1];
 
  1853   pSpec->zName = azArg[2];
 
  1855   pSpec->azColumn = azArg;
 
  1856   zTokenizer = "tokenize simple";
 
  1857   for(i=3; i<argc; ++i){
 
  1858     if( startsWith(azArg[i],"tokenize") ){
 
  1859       zTokenizer = azArg[i];
 
  1861       z = azArg[pSpec->nColumn] = firstToken(azArg[i], &zDummy);
 
  1865   if( pSpec->nColumn==0 ){
 
  1866     azArg[0] = "content";
 
  1871   ** Construct the list of content column names.
 
  1873   ** Each content column name will be of the form cNNAAAA
 
  1874   ** where NN is the column number and AAAA is the sanitized
 
  1875   ** column name.  "sanitized" means that special characters are
 
  1876   ** converted to "_".  The cNN prefix guarantees that all column
 
  1877   ** names are unique.
 
  1879   ** The AAAA suffix is not strictly necessary.  It is included
 
  1880   ** for the convenience of people who might examine the generated
 
  1881   ** %_content table and wonder what the columns are used for.
 
  1883   pSpec->azContentColumn = malloc( pSpec->nColumn * sizeof(char *) );
 
  1884   if( pSpec->azContentColumn==0 ){
 
  1885     clearTableSpec(pSpec);
 
  1886     return SQLITE_NOMEM;
 
  1888   for(i=0; i<pSpec->nColumn; i++){
 
  1890     pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]);
 
  1891     for (p = pSpec->azContentColumn[i]; *p ; ++p) {
 
  1892       if( !safe_isalnum(*p) ) *p = '_';
 
  1897   ** Parse the tokenizer specification string.
 
  1899   pSpec->azTokenizer = tokenizeString(zTokenizer, &n);
 
  1900   tokenListToIdList(pSpec->azTokenizer);
 
  1906 ** Generate a CREATE TABLE statement that describes the schema of
 
  1907 ** the virtual table.  Return a pointer to this schema string.
 
  1909 ** Space is obtained from sqlite3_mprintf() and should be freed
 
  1910 ** using sqlite3_free().
 
  1912 static char *fulltextSchema(
 
  1913   int nColumn,                  /* Number of columns */
 
  1914   const char *const* azColumn,  /* List of columns */
 
  1915   const char *zTableName        /* Name of the table */
 
  1918   char *zSchema, *zNext;
 
  1919   const char *zSep = "(";
 
  1920   zSchema = sqlite3_mprintf("CREATE TABLE x");
 
  1921   for(i=0; i<nColumn; i++){
 
  1922     zNext = sqlite3_mprintf("%s%s%Q", zSchema, zSep, azColumn[i]);
 
  1923     sqlite3_free(zSchema);
 
  1927   zNext = sqlite3_mprintf("%s,%Q)", zSchema, zTableName);
 
  1928   sqlite3_free(zSchema);
 
  1933 ** Build a new sqlite3_vtab structure that will describe the
 
  1934 ** fulltext index defined by spec.
 
  1936 static int constructVtab(
 
  1937   sqlite3 *db,              /* The SQLite database connection */
 
  1938   TableSpec *spec,          /* Parsed spec information from parseSpec() */
 
  1939   sqlite3_vtab **ppVTab,    /* Write the resulting vtab structure here */
 
  1940   char **pzErr              /* Write any error message here */
 
  1944   fulltext_vtab *v = 0;
 
  1945   const sqlite3_tokenizer_module *m = NULL;
 
  1948   v = (fulltext_vtab *) malloc(sizeof(fulltext_vtab));
 
  1949   if( v==0 ) return SQLITE_NOMEM;
 
  1950   memset(v, 0, sizeof(*v));
 
  1951   /* sqlite will initialize v->base */
 
  1953   v->zDb = spec->zDb;       /* Freed when azColumn is freed */
 
  1954   v->zName = spec->zName;   /* Freed when azColumn is freed */
 
  1955   v->nColumn = spec->nColumn;
 
  1956   v->azContentColumn = spec->azContentColumn;
 
  1957   spec->azContentColumn = 0;
 
  1958   v->azColumn = spec->azColumn;
 
  1961   if( spec->azTokenizer==0 ){
 
  1962     return SQLITE_NOMEM;
 
  1964   /* TODO(shess) For now, add new tokenizers as else if clauses. */
 
  1965   if( spec->azTokenizer[0]==0 || startsWith(spec->azTokenizer[0], "simple") ){
 
  1966     sqlite3Fts1SimpleTokenizerModule(&m);
 
  1967   }else if( startsWith(spec->azTokenizer[0], "porter") ){
 
  1968     sqlite3Fts1PorterTokenizerModule(&m);
 
  1970     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]);
 
  1974   for(n=0; spec->azTokenizer[n]; n++){}
 
  1976     rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1],
 
  1979     rc = m->xCreate(0, 0, &v->pTokenizer);
 
  1981   if( rc!=SQLITE_OK ) goto err;
 
  1982   v->pTokenizer->pModule = m;
 
  1984   /* TODO: verify the existence of backing tables foo_content, foo_term */
 
  1986   schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn,
 
  1988   rc = sqlite3_declare_vtab(db, schema);
 
  1989   sqlite3_free(schema);
 
  1990   if( rc!=SQLITE_OK ) goto err;
 
  1992   memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements));
 
  1995   TRACE(("FTS1 Connect %p\n", v));
 
  2000   fulltext_vtab_destroy(v);
 
  2004 static int fulltextConnect(
 
  2007   int argc, const char *const*argv,
 
  2008   sqlite3_vtab **ppVTab,
 
  2012   int rc = parseSpec(&spec, argc, argv, pzErr);
 
  2013   if( rc!=SQLITE_OK ) return rc;
 
  2015   rc = constructVtab(db, &spec, ppVTab, pzErr);
 
  2016   clearTableSpec(&spec);
 
  2020   /* The %_content table holds the text of each document, with
 
  2021   ** the rowid used as the docid.
 
  2023   ** The %_term table maps each term to a document list blob
 
  2024   ** containing elements sorted by ascending docid, each element
 
  2027   **   docid varint-encoded
 
  2029   **     position+1 varint-encoded as delta from previous position
 
  2030   **     start offset varint-encoded as delta from previous start offset
 
  2031   **     end offset varint-encoded as delta from start offset
 
  2033   ** The sentinel position of 0 indicates the end of the token list.
 
  2035   ** Additionally, doclist blobs are chunked into multiple segments,
 
  2036   ** using segment to order the segments.  New elements are added to
 
  2037   ** the segment at segment 0, until it exceeds CHUNK_MAX.  Then
 
  2038   ** segment 0 is deleted, and the doclist is inserted at segment 1.
 
  2039   ** If there is already a doclist at segment 1, the segment 0 doclist
 
  2040   ** is merged with it, the segment 1 doclist is deleted, and the
 
  2041   ** merged doclist is inserted at segment 2, repeating those
 
  2042   ** operations until an insert succeeds.
 
  2044   ** Since this structure doesn't allow us to update elements in place
 
  2045   ** in case of deletion or update, these are simply written to
 
  2046   ** segment 0 (with an empty token list in case of deletion), with
 
  2047   ** docListAccumulate() taking care to retain lower-segment
 
  2048   ** information in preference to higher-segment information.
 
  2050   /* TODO(shess) Provide a VACUUM type operation which both removes
 
  2051   ** deleted elements which are no longer necessary, and duplicated
 
  2052   ** elements.  I suspect this will probably not be necessary in
 
  2053   ** practice, though.
 
  2055 static int fulltextCreate(sqlite3 *db, void *pAux,
 
  2056                           int argc, const char * const *argv,
 
  2057                           sqlite3_vtab **ppVTab, char **pzErr){
 
  2060   StringBuffer schema;
 
  2061   TRACE(("FTS1 Create\n"));
 
  2063   rc = parseSpec(&spec, argc, argv, pzErr);
 
  2064   if( rc!=SQLITE_OK ) return rc;
 
  2066   initStringBuffer(&schema);
 
  2067   append(&schema, "CREATE TABLE %_content(");
 
  2068   appendList(&schema, spec.nColumn, spec.azContentColumn);
 
  2069   append(&schema, ")");
 
  2070   rc = sql_exec(db, spec.zDb, spec.zName, schema.s);
 
  2072   if( rc!=SQLITE_OK ) goto out;
 
  2074   rc = sql_exec(db, spec.zDb, spec.zName,
 
  2075     "create table %_term(term text, segment integer, doclist blob, "
 
  2076                         "primary key(term, segment));");
 
  2077   if( rc!=SQLITE_OK ) goto out;
 
  2079   rc = constructVtab(db, &spec, ppVTab, pzErr);
 
  2082   clearTableSpec(&spec);
 
  2086 /* Decide how to handle an SQL query. */
 
  2087 static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
 
  2089   TRACE(("FTS1 BestIndex\n"));
 
  2091   for(i=0; i<pInfo->nConstraint; ++i){
 
  2092     const struct sqlite3_index_constraint *pConstraint;
 
  2093     pConstraint = &pInfo->aConstraint[i];
 
  2094     if( pConstraint->usable ) {
 
  2095       if( pConstraint->iColumn==-1 &&
 
  2096           pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
 
  2097         pInfo->idxNum = QUERY_ROWID;      /* lookup by rowid */
 
  2098         TRACE(("FTS1 QUERY_ROWID\n"));
 
  2099       } else if( pConstraint->iColumn>=0 &&
 
  2100                  pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
 
  2101         /* full-text search */
 
  2102         pInfo->idxNum = QUERY_FULLTEXT + pConstraint->iColumn;
 
  2103         TRACE(("FTS1 QUERY_FULLTEXT %d\n", pConstraint->iColumn));
 
  2106       pInfo->aConstraintUsage[i].argvIndex = 1;
 
  2107       pInfo->aConstraintUsage[i].omit = 1;
 
  2109       /* An arbitrary value for now.
 
  2110        * TODO: Perhaps rowid matches should be considered cheaper than
 
  2111        * full-text searches. */
 
  2112       pInfo->estimatedCost = 1.0;   
 
  2117   pInfo->idxNum = QUERY_GENERIC;
 
  2121 static int fulltextDisconnect(sqlite3_vtab *pVTab){
 
  2122   TRACE(("FTS1 Disconnect %p\n", pVTab));
 
  2123   fulltext_vtab_destroy((fulltext_vtab *)pVTab);
 
  2127 static int fulltextDestroy(sqlite3_vtab *pVTab){
 
  2128   fulltext_vtab *v = (fulltext_vtab *)pVTab;
 
  2131   TRACE(("FTS1 Destroy %p\n", pVTab));
 
  2132   rc = sql_exec(v->db, v->zDb, v->zName,
 
  2133                 "drop table if exists %_content;"
 
  2134                 "drop table if exists %_term;"
 
  2136   if( rc!=SQLITE_OK ) return rc;
 
  2138   fulltext_vtab_destroy((fulltext_vtab *)pVTab);
 
  2142 static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
 
  2145   c = (fulltext_cursor *) calloc(sizeof(fulltext_cursor), 1);
 
  2146   /* sqlite will initialize c->base */
 
  2147   *ppCursor = &c->base;
 
  2148   TRACE(("FTS1 Open %p: %p\n", pVTab, c));
 
  2154 /* Free all of the dynamically allocated memory held by *q
 
  2156 static void queryClear(Query *q){
 
  2158   for(i = 0; i < q->nTerms; ++i){
 
  2159     free(q->pTerms[i].pTerm);
 
  2162   memset(q, 0, sizeof(*q));
 
  2165 /* Free all of the dynamically allocated memory held by the
 
  2168 static void snippetClear(Snippet *p){
 
  2172   memset(p, 0, sizeof(*p));
 
  2175 ** Append a single entry to the p->aMatch[] log.
 
  2177 static void snippetAppendMatch(
 
  2178   Snippet *p,               /* Append the entry to this snippet */
 
  2179   int iCol, int iTerm,      /* The column and query term */
 
  2180   int iStart, int nByte     /* Offset and size of the match */
 
  2183   struct snippetMatch *pMatch;
 
  2184   if( p->nMatch+1>=p->nAlloc ){
 
  2185     p->nAlloc = p->nAlloc*2 + 10;
 
  2186     p->aMatch = realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) );
 
  2194   pMatch = &p->aMatch[i];
 
  2195   pMatch->iCol = iCol;
 
  2196   pMatch->iTerm = iTerm;
 
  2197   pMatch->iStart = iStart;
 
  2198   pMatch->nByte = nByte;
 
  2202 ** Sizing information for the circular buffer used in snippetOffsetsOfColumn()
 
  2204 #define FTS1_ROTOR_SZ   (32)
 
  2205 #define FTS1_ROTOR_MASK (FTS1_ROTOR_SZ-1)
 
  2208 ** Add entries to pSnippet->aMatch[] for every match that occurs against
 
  2209 ** document zDoc[0..nDoc-1] which is stored in column iColumn.
 
  2211 static void snippetOffsetsOfColumn(
 
  2218   const sqlite3_tokenizer_module *pTModule;  /* The tokenizer module */
 
  2219   sqlite3_tokenizer *pTokenizer;             /* The specific tokenizer */
 
  2220   sqlite3_tokenizer_cursor *pTCursor;        /* Tokenizer cursor */
 
  2221   fulltext_vtab *pVtab;                /* The full text index */
 
  2222   int nColumn;                         /* Number of columns in the index */
 
  2223   const QueryTerm *aTerm;              /* Query string terms */
 
  2224   int nTerm;                           /* Number of query string terms */  
 
  2225   int i, j;                            /* Loop counters */
 
  2226   int rc;                              /* Return code */
 
  2227   unsigned int match, prevMatch;       /* Phrase search bitmasks */
 
  2228   const char *zToken;                  /* Next token from the tokenizer */
 
  2229   int nToken;                          /* Size of zToken */
 
  2230   int iBegin, iEnd, iPos;              /* Offsets of beginning and end */
 
  2232   /* The following variables keep a circular buffer of the last
 
  2234   unsigned int iRotor = 0;             /* Index of current token */
 
  2235   int iRotorBegin[FTS1_ROTOR_SZ];      /* Beginning offset of token */
 
  2236   int iRotorLen[FTS1_ROTOR_SZ];        /* Length of token */
 
  2238   pVtab = pQuery->pFts;
 
  2239   nColumn = pVtab->nColumn;
 
  2240   pTokenizer = pVtab->pTokenizer;
 
  2241   pTModule = pTokenizer->pModule;
 
  2242   rc = pTModule->xOpen(pTokenizer, zDoc, nDoc, &pTCursor);
 
  2244   pTCursor->pTokenizer = pTokenizer;
 
  2245   aTerm = pQuery->pTerms;
 
  2246   nTerm = pQuery->nTerms;
 
  2247   if( nTerm>=FTS1_ROTOR_SZ ){
 
  2248     nTerm = FTS1_ROTOR_SZ - 1;
 
  2252     rc = pTModule->xNext(pTCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos);
 
  2254     iRotorBegin[iRotor&FTS1_ROTOR_MASK] = iBegin;
 
  2255     iRotorLen[iRotor&FTS1_ROTOR_MASK] = iEnd-iBegin;
 
  2257     for(i=0; i<nTerm; i++){
 
  2259       iCol = aTerm[i].iColumn;
 
  2260       if( iCol>=0 && iCol<nColumn && iCol!=iColumn ) continue;
 
  2261       if( aTerm[i].nTerm!=nToken ) continue;
 
  2262       if( memcmp(aTerm[i].pTerm, zToken, nToken) ) continue;
 
  2263       if( aTerm[i].iPhrase>1 && (prevMatch & (1<<i))==0 ) continue;
 
  2265       if( i==nTerm-1 || aTerm[i+1].iPhrase==1 ){
 
  2266         for(j=aTerm[i].iPhrase-1; j>=0; j--){
 
  2267           int k = (iRotor-j) & FTS1_ROTOR_MASK;
 
  2268           snippetAppendMatch(pSnippet, iColumn, i-j,
 
  2269                 iRotorBegin[k], iRotorLen[k]);
 
  2273     prevMatch = match<<1;
 
  2276   pTModule->xClose(pTCursor);  
 
  2281 ** Compute all offsets for the current row of the query.  
 
  2282 ** If the offsets have already been computed, this routine is a no-op.
 
  2284 static void snippetAllOffsets(fulltext_cursor *p){
 
  2288   fulltext_vtab *pFts;
 
  2290   if( p->snippet.nMatch ) return;
 
  2291   if( p->q.nTerms==0 ) return;
 
  2293   nColumn = pFts->nColumn;
 
  2294   iColumn = p->iCursorType - QUERY_FULLTEXT;
 
  2295   if( iColumn<0 || iColumn>=nColumn ){
 
  2302   for(i=iFirst; i<=iLast; i++){
 
  2305     zDoc = (const char*)sqlite3_column_text(p->pStmt, i+1);
 
  2306     nDoc = sqlite3_column_bytes(p->pStmt, i+1);
 
  2307     snippetOffsetsOfColumn(&p->q, &p->snippet, i, zDoc, nDoc);
 
  2312 ** Convert the information in the aMatch[] array of the snippet
 
  2313 ** into the string zOffset[0..nOffset-1].
 
  2315 static void snippetOffsetText(Snippet *p){
 
  2320   if( p->zOffset ) return;
 
  2321   initStringBuffer(&sb);
 
  2322   for(i=0; i<p->nMatch; i++){
 
  2323     struct snippetMatch *pMatch = &p->aMatch[i];
 
  2325     sqlite3_snprintf(sizeof(zBuf)-1, &zBuf[cnt>0], "%d %d %d %d",
 
  2326         pMatch->iCol, pMatch->iTerm, pMatch->iStart, pMatch->nByte);
 
  2331   p->nOffset = sb.len;
 
  2335 ** zDoc[0..nDoc-1] is phrase of text.  aMatch[0..nMatch-1] are a set
 
  2336 ** of matching words some of which might be in zDoc.  zDoc is column
 
  2339 ** iBreak is suggested spot in zDoc where we could begin or end an
 
  2340 ** excerpt.  Return a value similar to iBreak but possibly adjusted
 
  2341 ** to be a little left or right so that the break point is better.
 
  2343 static int wordBoundary(
 
  2344   int iBreak,                   /* The suggested break point */
 
  2345   const char *zDoc,             /* Document text */
 
  2346   int nDoc,                     /* Number of bytes in zDoc[] */
 
  2347   struct snippetMatch *aMatch,  /* Matching words */
 
  2348   int nMatch,                   /* Number of entries in aMatch[] */
 
  2349   int iCol                      /* The column number for zDoc[] */
 
  2355   if( iBreak>=nDoc-10 ){
 
  2358   for(i=0; i<nMatch && aMatch[i].iCol<iCol; i++){}
 
  2359   while( i<nMatch && aMatch[i].iStart+aMatch[i].nByte<iBreak ){ i++; }
 
  2361     if( aMatch[i].iStart<iBreak+10 ){
 
  2362       return aMatch[i].iStart;
 
  2364     if( i>0 && aMatch[i-1].iStart+aMatch[i-1].nByte>=iBreak ){
 
  2365       return aMatch[i-1].iStart;
 
  2368   for(i=1; i<=10; i++){
 
  2369     if( safe_isspace(zDoc[iBreak-i]) ){
 
  2370       return iBreak - i + 1;
 
  2372     if( safe_isspace(zDoc[iBreak+i]) ){
 
  2373       return iBreak + i + 1;
 
  2380 ** If the StringBuffer does not end in white space, add a single
 
  2381 ** space character to the end.
 
  2383 static void appendWhiteSpace(StringBuffer *p){
 
  2384   if( p->len==0 ) return;
 
  2385   if( safe_isspace(p->s[p->len-1]) ) return;
 
  2390 ** Remove white space from teh end of the StringBuffer
 
  2392 static void trimWhiteSpace(StringBuffer *p){
 
  2393   while( p->len>0 && safe_isspace(p->s[p->len-1]) ){
 
  2401 ** Allowed values for Snippet.aMatch[].snStatus
 
  2403 #define SNIPPET_IGNORE  0   /* It is ok to omit this match from the snippet */
 
  2404 #define SNIPPET_DESIRED 1   /* We want to include this match in the snippet */
 
  2407 ** Generate the text of a snippet.
 
  2409 static void snippetText(
 
  2410   fulltext_cursor *pCursor,   /* The cursor we need the snippet for */
 
  2411   const char *zStartMark,     /* Markup to appear before each match */
 
  2412   const char *zEndMark,       /* Markup to appear after each match */
 
  2413   const char *zEllipsis       /* Ellipsis mark */
 
  2416   struct snippetMatch *aMatch;
 
  2426   int tailEllipsis = 0;
 
  2430   free(pCursor->snippet.zSnippet);
 
  2431   pCursor->snippet.zSnippet = 0;
 
  2432   aMatch = pCursor->snippet.aMatch;
 
  2433   nMatch = pCursor->snippet.nMatch;
 
  2434   initStringBuffer(&sb);
 
  2436   for(i=0; i<nMatch; i++){
 
  2437     aMatch[i].snStatus = SNIPPET_IGNORE;
 
  2440   for(i=0; i<pCursor->q.nTerms; i++){
 
  2441     for(j=0; j<nMatch; j++){
 
  2442       if( aMatch[j].iTerm==i ){
 
  2443         aMatch[j].snStatus = SNIPPET_DESIRED;
 
  2453   for(i=0; i<nMatch && nDesired>0; i++){
 
  2454     if( aMatch[i].snStatus!=SNIPPET_DESIRED ) continue;
 
  2456     iCol = aMatch[i].iCol;
 
  2457     zDoc = (const char*)sqlite3_column_text(pCursor->pStmt, iCol+1);
 
  2458     nDoc = sqlite3_column_bytes(pCursor->pStmt, iCol+1);
 
  2459     iStart = aMatch[i].iStart - 40;
 
  2460     iStart = wordBoundary(iStart, zDoc, nDoc, aMatch, nMatch, iCol);
 
  2464     if( iCol==tailCol && iStart<=tailOffset+20 ){
 
  2465       iStart = tailOffset;
 
  2467     if( (iCol!=tailCol && tailCol>=0) || iStart!=tailOffset ){
 
  2468       trimWhiteSpace(&sb);
 
  2469       appendWhiteSpace(&sb);
 
  2470       append(&sb, zEllipsis);
 
  2471       appendWhiteSpace(&sb);
 
  2473     iEnd = aMatch[i].iStart + aMatch[i].nByte + 40;
 
  2474     iEnd = wordBoundary(iEnd, zDoc, nDoc, aMatch, nMatch, iCol);
 
  2475     if( iEnd>=nDoc-10 ){
 
  2481     while( iMatch<nMatch && aMatch[iMatch].iCol<iCol ){ iMatch++; }
 
  2482     while( iStart<iEnd ){
 
  2483       while( iMatch<nMatch && aMatch[iMatch].iStart<iStart
 
  2484              && aMatch[iMatch].iCol<=iCol ){
 
  2487       if( iMatch<nMatch && aMatch[iMatch].iStart<iEnd
 
  2488              && aMatch[iMatch].iCol==iCol ){
 
  2489         nappend(&sb, &zDoc[iStart], aMatch[iMatch].iStart - iStart);
 
  2490         iStart = aMatch[iMatch].iStart;
 
  2491         append(&sb, zStartMark);
 
  2492         nappend(&sb, &zDoc[iStart], aMatch[iMatch].nByte);
 
  2493         append(&sb, zEndMark);
 
  2494         iStart += aMatch[iMatch].nByte;
 
  2495         for(j=iMatch+1; j<nMatch; j++){
 
  2496           if( aMatch[j].iTerm==aMatch[iMatch].iTerm
 
  2497               && aMatch[j].snStatus==SNIPPET_DESIRED ){
 
  2499             aMatch[j].snStatus = SNIPPET_IGNORE;
 
  2503         nappend(&sb, &zDoc[iStart], iEnd - iStart);
 
  2510   trimWhiteSpace(&sb);
 
  2512     appendWhiteSpace(&sb);
 
  2513     append(&sb, zEllipsis);
 
  2515   pCursor->snippet.zSnippet = sb.s;
 
  2516   pCursor->snippet.nSnippet = sb.len;  
 
  2521 ** Close the cursor.  For additional information see the documentation
 
  2522 ** on the xClose method of the virtual table interface.
 
  2524 static int fulltextClose(sqlite3_vtab_cursor *pCursor){
 
  2525   fulltext_cursor *c = (fulltext_cursor *) pCursor;
 
  2526   TRACE(("FTS1 Close %p\n", c));
 
  2527   sqlite3_finalize(c->pStmt);
 
  2529   snippetClear(&c->snippet);
 
  2530   if( c->result.pDoclist!=NULL ){
 
  2531     docListDelete(c->result.pDoclist);
 
  2537 static int fulltextNext(sqlite3_vtab_cursor *pCursor){
 
  2538   fulltext_cursor *c = (fulltext_cursor *) pCursor;
 
  2539   sqlite_int64 iDocid;
 
  2542   TRACE(("FTS1 Next %p\n", pCursor));
 
  2543   snippetClear(&c->snippet);
 
  2544   if( c->iCursorType < QUERY_FULLTEXT ){
 
  2545     /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */
 
  2546     rc = sqlite3_step(c->pStmt);
 
  2558   } else {  /* full-text query */
 
  2559     rc = sqlite3_reset(c->pStmt);
 
  2560     if( rc!=SQLITE_OK ) return rc;
 
  2562     iDocid = nextDocid(&c->result);
 
  2567     rc = sqlite3_bind_int64(c->pStmt, 1, iDocid);
 
  2568     if( rc!=SQLITE_OK ) return rc;
 
  2569     /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */
 
  2570     rc = sqlite3_step(c->pStmt);
 
  2571     if( rc==SQLITE_ROW ){   /* the case we expect */
 
  2575     /* an error occurred; abort */
 
  2576     return rc==SQLITE_DONE ? SQLITE_ERROR : rc;
 
  2581 /* Return a DocList corresponding to the query term *pTerm.  If *pTerm
 
  2582 ** is the first term of a phrase query, go ahead and evaluate the phrase
 
  2583 ** query and return the doclist for the entire phrase query.
 
  2585 ** The result is stored in pTerm->doclist.
 
  2587 static int docListOfTerm(
 
  2588   fulltext_vtab *v,     /* The full text index */
 
  2589   int iColumn,          /* column to restrict to.  No restrition if >=nColumn */
 
  2590   QueryTerm *pQTerm,    /* Term we are looking for, or 1st term of a phrase */
 
  2591   DocList **ppResult    /* Write the result here */
 
  2593   DocList *pLeft, *pRight, *pNew;
 
  2596   pLeft = docListNew(DL_POSITIONS);
 
  2597   rc = term_select_all(v, iColumn, pQTerm->pTerm, pQTerm->nTerm, pLeft);
 
  2599     docListDelete(pLeft);
 
  2602   for(i=1; i<=pQTerm->nPhrase; i++){
 
  2603     pRight = docListNew(DL_POSITIONS);
 
  2604     rc = term_select_all(v, iColumn, pQTerm[i].pTerm, pQTerm[i].nTerm, pRight);
 
  2606       docListDelete(pLeft);
 
  2609     pNew = docListNew(i<pQTerm->nPhrase ? DL_POSITIONS : DL_DOCIDS);
 
  2610     docListPhraseMerge(pLeft, pRight, pNew);
 
  2611     docListDelete(pLeft);
 
  2612     docListDelete(pRight);
 
  2619 /* Add a new term pTerm[0..nTerm-1] to the query *q.
 
  2621 static void queryAdd(Query *q, const char *pTerm, int nTerm){
 
  2624   q->pTerms = realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0]));
 
  2629   t = &q->pTerms[q->nTerms - 1];
 
  2630   memset(t, 0, sizeof(*t));
 
  2631   t->pTerm = malloc(nTerm+1);
 
  2632   memcpy(t->pTerm, pTerm, nTerm);
 
  2633   t->pTerm[nTerm] = 0;
 
  2635   t->isOr = q->nextIsOr;
 
  2637   t->iColumn = q->nextColumn;
 
  2638   q->nextColumn = q->dfltColumn;
 
  2642 ** Check to see if the string zToken[0...nToken-1] matches any
 
  2643 ** column name in the virtual table.   If it does,
 
  2644 ** return the zero-indexed column number.  If not, return -1.
 
  2646 static int checkColumnSpecifier(
 
  2647   fulltext_vtab *pVtab,    /* The virtual table */
 
  2648   const char *zToken,      /* Text of the token */
 
  2649   int nToken               /* Number of characters in the token */
 
  2652   for(i=0; i<pVtab->nColumn; i++){
 
  2653     if( memcmp(pVtab->azColumn[i], zToken, nToken)==0
 
  2654         && pVtab->azColumn[i][nToken]==0 ){
 
  2662 ** Parse the text at pSegment[0..nSegment-1].  Add additional terms
 
  2663 ** to the query being assemblied in pQuery.
 
  2665 ** inPhrase is true if pSegment[0..nSegement-1] is contained within
 
  2666 ** double-quotes.  If inPhrase is true, then the first term
 
  2667 ** is marked with the number of terms in the phrase less one and
 
  2668 ** OR and "-" syntax is ignored.  If inPhrase is false, then every
 
  2669 ** term found is marked with nPhrase=0 and OR and "-" syntax is significant.
 
  2671 static int tokenizeSegment(
 
  2672   sqlite3_tokenizer *pTokenizer,          /* The tokenizer to use */
 
  2673   const char *pSegment, int nSegment,     /* Query expression being parsed */
 
  2674   int inPhrase,                           /* True if within "..." */
 
  2675   Query *pQuery                           /* Append results here */
 
  2677   const sqlite3_tokenizer_module *pModule = pTokenizer->pModule;
 
  2678   sqlite3_tokenizer_cursor *pCursor;
 
  2679   int firstIndex = pQuery->nTerms;
 
  2683   int rc = pModule->xOpen(pTokenizer, pSegment, nSegment, &pCursor);
 
  2684   if( rc!=SQLITE_OK ) return rc;
 
  2685   pCursor->pTokenizer = pTokenizer;
 
  2689     int nToken, iBegin, iEnd, iPos;
 
  2691     rc = pModule->xNext(pCursor,
 
  2693                         &iBegin, &iEnd, &iPos);
 
  2694     if( rc!=SQLITE_OK ) break;
 
  2696         pSegment[iEnd]==':' &&
 
  2697          (iCol = checkColumnSpecifier(pQuery->pFts, pToken, nToken))>=0 ){
 
  2698       pQuery->nextColumn = iCol;
 
  2701     if( !inPhrase && pQuery->nTerms>0 && nToken==2
 
  2702          && pSegment[iBegin]=='O' && pSegment[iBegin+1]=='R' ){
 
  2703       pQuery->nextIsOr = 1;
 
  2706     queryAdd(pQuery, pToken, nToken);
 
  2707     if( !inPhrase && iBegin>0 && pSegment[iBegin-1]=='-' ){
 
  2708       pQuery->pTerms[pQuery->nTerms-1].isNot = 1;
 
  2710     pQuery->pTerms[pQuery->nTerms-1].iPhrase = nTerm;
 
  2716   if( inPhrase && pQuery->nTerms>firstIndex ){
 
  2717     pQuery->pTerms[firstIndex].nPhrase = pQuery->nTerms - firstIndex - 1;
 
  2720   return pModule->xClose(pCursor);
 
  2723 /* Parse a query string, yielding a Query object pQuery.
 
  2725 ** The calling function will need to queryClear() to clean up
 
  2726 ** the dynamically allocated memory held by pQuery.
 
  2728 static int parseQuery(
 
  2729   fulltext_vtab *v,        /* The fulltext index */
 
  2730   const char *zInput,      /* Input text of the query string */
 
  2731   int nInput,              /* Size of the input text */
 
  2732   int dfltColumn,          /* Default column of the index to match against */
 
  2733   Query *pQuery            /* Write the parse results here. */
 
  2735   int iInput, inPhrase = 0;
 
  2737   if( zInput==0 ) nInput = 0;
 
  2738   if( nInput<0 ) nInput = strlen(zInput);
 
  2740   pQuery->pTerms = NULL;
 
  2741   pQuery->nextIsOr = 0;
 
  2742   pQuery->nextColumn = dfltColumn;
 
  2743   pQuery->dfltColumn = dfltColumn;
 
  2746   for(iInput=0; iInput<nInput; ++iInput){
 
  2748     for(i=iInput; i<nInput && zInput[i]!='"'; ++i){}
 
  2750       tokenizeSegment(v->pTokenizer, zInput+iInput, i-iInput, inPhrase,
 
  2755       assert( zInput[i]=='"' );
 
  2756       inPhrase = !inPhrase;
 
  2761     /* unmatched quote */
 
  2763     return SQLITE_ERROR;
 
  2768 /* Perform a full-text query using the search expression in
 
  2769 ** zInput[0..nInput-1].  Return a list of matching documents
 
  2772 ** Queries must match column iColumn.  Or if iColumn>=nColumn
 
  2773 ** they are allowed to match against any column.
 
  2775 static int fulltextQuery(
 
  2776   fulltext_vtab *v,      /* The full text index */
 
  2777   int iColumn,           /* Match against this column by default */
 
  2778   const char *zInput,    /* The query string */
 
  2779   int nInput,            /* Number of bytes in zInput[] */
 
  2780   DocList **pResult,     /* Write the result doclist here */
 
  2781   Query *pQuery          /* Put parsed query string here */
 
  2784   DocList *pLeft = NULL;
 
  2785   DocList *pRight, *pNew, *pOr;
 
  2789   rc = parseQuery(v, zInput, nInput, iColumn, pQuery);
 
  2790   if( rc!=SQLITE_OK ) return rc;
 
  2792   /* Merge AND terms. */
 
  2793   aTerm = pQuery->pTerms;
 
  2794   for(i = 0; i<pQuery->nTerms; i=iNext){
 
  2795     if( aTerm[i].isNot ){
 
  2796       /* Handle all NOT terms in a separate pass */
 
  2798       iNext = i + aTerm[i].nPhrase+1;
 
  2801     iNext = i + aTerm[i].nPhrase + 1;
 
  2802     rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &pRight);
 
  2807     while( iNext<pQuery->nTerms && aTerm[iNext].isOr ){
 
  2808       rc = docListOfTerm(v, aTerm[iNext].iColumn, &aTerm[iNext], &pOr);
 
  2809       iNext += aTerm[iNext].nPhrase + 1;
 
  2814       pNew = docListNew(DL_DOCIDS);
 
  2815       docListOrMerge(pRight, pOr, pNew);
 
  2816       docListDelete(pRight);
 
  2823       pNew = docListNew(DL_DOCIDS);
 
  2824       docListAndMerge(pLeft, pRight, pNew);
 
  2825       docListDelete(pRight);
 
  2826       docListDelete(pLeft);
 
  2831   if( nNot && pLeft==0 ){
 
  2832     /* We do not yet know how to handle a query of only NOT terms */
 
  2833     return SQLITE_ERROR;
 
  2836   /* Do the EXCEPT terms */
 
  2837   for(i=0; i<pQuery->nTerms;  i += aTerm[i].nPhrase + 1){
 
  2838     if( !aTerm[i].isNot ) continue;
 
  2839     rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &pRight);
 
  2842       docListDelete(pLeft);
 
  2845     pNew = docListNew(DL_DOCIDS);
 
  2846     docListExceptMerge(pLeft, pRight, pNew);
 
  2847     docListDelete(pRight);
 
  2848     docListDelete(pLeft);
 
  2857 ** This is the xFilter interface for the virtual table.  See
 
  2858 ** the virtual table xFilter method documentation for additional
 
  2861 ** If idxNum==QUERY_GENERIC then do a full table scan against
 
  2862 ** the %_content table.
 
  2864 ** If idxNum==QUERY_ROWID then do a rowid lookup for a single entry
 
  2865 ** in the %_content table.
 
  2867 ** If idxNum>=QUERY_FULLTEXT then use the full text index.  The
 
  2868 ** column on the left-hand side of the MATCH operator is column
 
  2869 ** number idxNum-QUERY_FULLTEXT, 0 indexed.  argv[0] is the right-hand
 
  2870 ** side of the MATCH operator.
 
  2872 /* TODO(shess) Upgrade the cursor initialization and destruction to
 
  2873 ** account for fulltextFilter() being called multiple times on the
 
  2874 ** same cursor.  The current solution is very fragile.  Apply fix to
 
  2875 ** fts2 as appropriate.
 
  2877 static int fulltextFilter(
 
  2878   sqlite3_vtab_cursor *pCursor,     /* The cursor used for this query */
 
  2879   int idxNum, const char *idxStr,   /* Which indexing scheme to use */
 
  2880   int argc, sqlite3_value **argv    /* Arguments for the indexing scheme */
 
  2882   fulltext_cursor *c = (fulltext_cursor *) pCursor;
 
  2883   fulltext_vtab *v = cursor_vtab(c);
 
  2887   TRACE(("FTS1 Filter %p\n",pCursor));
 
  2889   zSql = sqlite3_mprintf("select rowid, * from %%_content %s",
 
  2890                           idxNum==QUERY_GENERIC ? "" : "where rowid=?");
 
  2891   sqlite3_finalize(c->pStmt);
 
  2892   rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql);
 
  2894   if( rc!=SQLITE_OK ) return rc;
 
  2896   c->iCursorType = idxNum;
 
  2902       rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0]));
 
  2903       if( rc!=SQLITE_OK ) return rc;
 
  2906     default:   /* full-text search */
 
  2908       const char *zQuery = (const char *)sqlite3_value_text(argv[0]);
 
  2910       assert( idxNum<=QUERY_FULLTEXT+v->nColumn);
 
  2913       rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &pResult, &c->q);
 
  2914       if( rc!=SQLITE_OK ) return rc;
 
  2915       if( c->result.pDoclist!=NULL ) docListDelete(c->result.pDoclist);
 
  2916       readerInit(&c->result, pResult);
 
  2921   return fulltextNext(pCursor);
 
  2924 /* This is the xEof method of the virtual table.  The SQLite core
 
  2925 ** calls this routine to find out if it has reached the end of
 
  2926 ** a query's results set.
 
  2928 static int fulltextEof(sqlite3_vtab_cursor *pCursor){
 
  2929   fulltext_cursor *c = (fulltext_cursor *) pCursor;
 
  2933 /* This is the xColumn method of the virtual table.  The SQLite
 
  2934 ** core calls this method during a query when it needs the value
 
  2935 ** of a column from the virtual table.  This method needs to use
 
  2936 ** one of the sqlite3_result_*() routines to store the requested
 
  2937 ** value back in the pContext.
 
  2939 static int fulltextColumn(sqlite3_vtab_cursor *pCursor,
 
  2940                           sqlite3_context *pContext, int idxCol){
 
  2941   fulltext_cursor *c = (fulltext_cursor *) pCursor;
 
  2942   fulltext_vtab *v = cursor_vtab(c);
 
  2944   if( idxCol<v->nColumn ){
 
  2945     sqlite3_value *pVal = sqlite3_column_value(c->pStmt, idxCol+1);
 
  2946     sqlite3_result_value(pContext, pVal);
 
  2947   }else if( idxCol==v->nColumn ){
 
  2948     /* The extra column whose name is the same as the table.
 
  2949     ** Return a blob which is a pointer to the cursor
 
  2951     sqlite3_result_blob(pContext, &c, sizeof(c), SQLITE_TRANSIENT);
 
  2956 /* This is the xRowid method.  The SQLite core calls this routine to
 
  2957 ** retrive the rowid for the current row of the result set.  The
 
  2958 ** rowid should be written to *pRowid.
 
  2960 static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
 
  2961   fulltext_cursor *c = (fulltext_cursor *) pCursor;
 
  2963   *pRowid = sqlite3_column_int64(c->pStmt, 0);
 
  2967 /* Add all terms in [zText] to the given hash table.  If [iColumn] > 0,
 
  2968  * we also store positions and offsets in the hash table using the given
 
  2970 static int buildTerms(fulltext_vtab *v, fts1Hash *terms, sqlite_int64 iDocid,
 
  2971                       const char *zText, int iColumn){
 
  2972   sqlite3_tokenizer *pTokenizer = v->pTokenizer;
 
  2973   sqlite3_tokenizer_cursor *pCursor;
 
  2976   int iStartOffset, iEndOffset, iPosition;
 
  2979   rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor);
 
  2980   if( rc!=SQLITE_OK ) return rc;
 
  2982   pCursor->pTokenizer = pTokenizer;
 
  2983   while( SQLITE_OK==pTokenizer->pModule->xNext(pCursor,
 
  2984                                                &pToken, &nTokenBytes,
 
  2985                                                &iStartOffset, &iEndOffset,
 
  2989     /* Positions can't be negative; we use -1 as a terminator internally. */
 
  2991       pTokenizer->pModule->xClose(pCursor);
 
  2992       return SQLITE_ERROR;
 
  2995     p = fts1HashFind(terms, pToken, nTokenBytes);
 
  2997       p = docListNew(DL_DEFAULT);
 
  2998       docListAddDocid(p, iDocid);
 
  2999       fts1HashInsert(terms, pToken, nTokenBytes, p);
 
  3002       docListAddPosOffset(p, iColumn, iPosition, iStartOffset, iEndOffset);
 
  3006   /* TODO(shess) Check return?  Should this be able to cause errors at
 
  3007   ** this point?  Actually, same question about sqlite3_finalize(),
 
  3008   ** though one could argue that failure there means that the data is
 
  3009   ** not durable.  *ponder*
 
  3011   pTokenizer->pModule->xClose(pCursor);
 
  3015 /* Update the %_terms table to map the term [pTerm] to the given rowid. */
 
  3016 static int index_insert_term(fulltext_vtab *v, const char *pTerm, int nTerm,
 
  3018   sqlite_int64 iIndexRow;
 
  3020   int iSegment = 0, rc;
 
  3022   rc = term_select(v, pTerm, nTerm, iSegment, &iIndexRow, &doclist);
 
  3023   if( rc==SQLITE_DONE ){
 
  3024     docListInit(&doclist, DL_DEFAULT, 0, 0);
 
  3025     docListUpdate(&doclist, d);
 
  3026     /* TODO(shess) Consider length(doclist)>CHUNK_MAX? */
 
  3027     rc = term_insert(v, NULL, pTerm, nTerm, iSegment, &doclist);
 
  3030   if( rc!=SQLITE_ROW ) return SQLITE_ERROR;
 
  3032   docListUpdate(&doclist, d);
 
  3033   if( doclist.nData<=CHUNK_MAX ){
 
  3034     rc = term_update(v, iIndexRow, &doclist);
 
  3038   /* Doclist doesn't fit, delete what's there, and accumulate
 
  3041   rc = term_delete(v, iIndexRow);
 
  3042   if( rc!=SQLITE_OK ) goto err;
 
  3044   /* Try to insert the doclist into a higher segment bucket.  On
 
  3045   ** failure, accumulate existing doclist with the doclist from that
 
  3046   ** bucket, and put results in the next bucket.
 
  3049   while( (rc=term_insert(v, &iIndexRow, pTerm, nTerm, iSegment,
 
  3050                          &doclist))!=SQLITE_OK ){
 
  3051     sqlite_int64 iSegmentRow;
 
  3055     /* Retain old error in case the term_insert() error was really an
 
  3056     ** error rather than a bounced insert.
 
  3058     rc2 = term_select(v, pTerm, nTerm, iSegment, &iSegmentRow, &old);
 
  3059     if( rc2!=SQLITE_ROW ) goto err;
 
  3061     rc = term_delete(v, iSegmentRow);
 
  3062     if( rc!=SQLITE_OK ) goto err;
 
  3064     /* Reusing lowest-number deleted row keeps the index smaller. */
 
  3065     if( iSegmentRow<iIndexRow ) iIndexRow = iSegmentRow;
 
  3067     /* doclist contains the newer data, so accumulate it over old.
 
  3068     ** Then steal accumulated data for doclist.
 
  3070     docListAccumulate(&old, &doclist);
 
  3071     docListDestroy(&doclist);
 
  3078   docListDestroy(&doclist);
 
  3082 /* Add doclists for all terms in [pValues] to the hash table [terms]. */
 
  3083 static int insertTerms(fulltext_vtab *v, fts1Hash *terms, sqlite_int64 iRowid,
 
  3084                 sqlite3_value **pValues){
 
  3086   for(i = 0; i < v->nColumn ; ++i){
 
  3087     char *zText = (char*)sqlite3_value_text(pValues[i]);
 
  3088     int rc = buildTerms(v, terms, iRowid, zText, i);
 
  3089     if( rc!=SQLITE_OK ) return rc;
 
  3094 /* Add empty doclists for all terms in the given row's content to the hash
 
  3095  * table [pTerms]. */
 
  3096 static int deleteTerms(fulltext_vtab *v, fts1Hash *pTerms, sqlite_int64 iRowid){
 
  3097   const char **pValues;
 
  3100   int rc = content_select(v, iRowid, &pValues);
 
  3101   if( rc!=SQLITE_OK ) return rc;
 
  3103   for(i = 0 ; i < v->nColumn; ++i) {
 
  3104     rc = buildTerms(v, pTerms, iRowid, pValues[i], -1);
 
  3105     if( rc!=SQLITE_OK ) break;
 
  3108   freeStringArray(v->nColumn, pValues);
 
  3112 /* Insert a row into the %_content table; set *piRowid to be the ID of the
 
  3113  * new row.  Fill [pTerms] with new doclists for the %_term table. */
 
  3114 static int index_insert(fulltext_vtab *v, sqlite3_value *pRequestRowid,
 
  3115                         sqlite3_value **pValues,
 
  3116                         sqlite_int64 *piRowid, fts1Hash *pTerms){
 
  3119   rc = content_insert(v, pRequestRowid, pValues);  /* execute an SQL INSERT */
 
  3120   if( rc!=SQLITE_OK ) return rc;
 
  3121   *piRowid = sqlite3_last_insert_rowid(v->db);
 
  3122   return insertTerms(v, pTerms, *piRowid, pValues);
 
  3125 /* Delete a row from the %_content table; fill [pTerms] with empty doclists
 
  3126  * to be written to the %_term table. */
 
  3127 static int index_delete(fulltext_vtab *v, sqlite_int64 iRow, fts1Hash *pTerms){
 
  3128   int rc = deleteTerms(v, pTerms, iRow);
 
  3129   if( rc!=SQLITE_OK ) return rc;
 
  3130   return content_delete(v, iRow);  /* execute an SQL DELETE */
 
  3133 /* Update a row in the %_content table; fill [pTerms] with new doclists for the
 
  3135 static int index_update(fulltext_vtab *v, sqlite_int64 iRow,
 
  3136                         sqlite3_value **pValues, fts1Hash *pTerms){
 
  3137   /* Generate an empty doclist for each term that previously appeared in this
 
  3139   int rc = deleteTerms(v, pTerms, iRow);
 
  3140   if( rc!=SQLITE_OK ) return rc;
 
  3142   rc = content_update(v, pValues, iRow);  /* execute an SQL UPDATE */
 
  3143   if( rc!=SQLITE_OK ) return rc;
 
  3145   /* Now add positions for terms which appear in the updated row. */
 
  3146   return insertTerms(v, pTerms, iRow, pValues);
 
  3149 /* This function implements the xUpdate callback; it is the top-level entry
 
  3150  * point for inserting, deleting or updating a row in a full-text table. */
 
  3151 static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg,
 
  3152                    sqlite_int64 *pRowid){
 
  3153   fulltext_vtab *v = (fulltext_vtab *) pVtab;
 
  3154   fts1Hash terms;   /* maps term string -> PosList */
 
  3158   TRACE(("FTS1 Update %p\n", pVtab));
 
  3160   fts1HashInit(&terms, FTS1_HASH_STRING, 1);
 
  3163     rc = index_delete(v, sqlite3_value_int64(ppArg[0]), &terms);
 
  3164   } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){
 
  3166      * ppArg[0] = old rowid
 
  3167      * ppArg[1] = new rowid
 
  3168      * ppArg[2..2+v->nColumn-1] = values
 
  3169      * ppArg[2+v->nColumn] = value for magic column (we ignore this)
 
  3171     sqlite_int64 rowid = sqlite3_value_int64(ppArg[0]);
 
  3172     if( sqlite3_value_type(ppArg[1]) != SQLITE_INTEGER ||
 
  3173       sqlite3_value_int64(ppArg[1]) != rowid ){
 
  3174       rc = SQLITE_ERROR;  /* we don't allow changing the rowid */
 
  3176       assert( nArg==2+v->nColumn+1);
 
  3177       rc = index_update(v, rowid, &ppArg[2], &terms);
 
  3181      * ppArg[1] = requested rowid
 
  3182      * ppArg[2..2+v->nColumn-1] = values
 
  3183      * ppArg[2+v->nColumn] = value for magic column (we ignore this)
 
  3185     assert( nArg==2+v->nColumn+1);
 
  3186     rc = index_insert(v, ppArg[1], &ppArg[2], pRowid, &terms);
 
  3189   if( rc==SQLITE_OK ){
 
  3190     /* Write updated doclists to disk. */
 
  3191     for(e=fts1HashFirst(&terms); e; e=fts1HashNext(e)){
 
  3192       DocList *p = fts1HashData(e);
 
  3193       rc = index_insert_term(v, fts1HashKey(e), fts1HashKeysize(e), p);
 
  3194       if( rc!=SQLITE_OK ) break;
 
  3199   for(e=fts1HashFirst(&terms); e; e=fts1HashNext(e)){
 
  3200     DocList *p = fts1HashData(e);
 
  3203   fts1HashClear(&terms);
 
  3209 ** Implementation of the snippet() function for FTS1
 
  3211 static void snippetFunc(
 
  3212   sqlite3_context *pContext,
 
  3214   sqlite3_value **argv
 
  3216   fulltext_cursor *pCursor;
 
  3217   if( argc<1 ) return;
 
  3218   if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
 
  3219       sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
 
  3220     sqlite3_result_error(pContext, "illegal first argument to html_snippet",-1);
 
  3222     const char *zStart = "<b>";
 
  3223     const char *zEnd = "</b>";
 
  3224     const char *zEllipsis = "<b>...</b>";
 
  3225     memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
 
  3227       zStart = (const char*)sqlite3_value_text(argv[1]);
 
  3229         zEnd = (const char*)sqlite3_value_text(argv[2]);
 
  3231           zEllipsis = (const char*)sqlite3_value_text(argv[3]);
 
  3235     snippetAllOffsets(pCursor);
 
  3236     snippetText(pCursor, zStart, zEnd, zEllipsis);
 
  3237     sqlite3_result_text(pContext, pCursor->snippet.zSnippet,
 
  3238                         pCursor->snippet.nSnippet, SQLITE_STATIC);
 
  3243 ** Implementation of the offsets() function for FTS1
 
  3245 static void snippetOffsetsFunc(
 
  3246   sqlite3_context *pContext,
 
  3248   sqlite3_value **argv
 
  3250   fulltext_cursor *pCursor;
 
  3251   if( argc<1 ) return;
 
  3252   if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
 
  3253       sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
 
  3254     sqlite3_result_error(pContext, "illegal first argument to offsets",-1);
 
  3256     memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
 
  3257     snippetAllOffsets(pCursor);
 
  3258     snippetOffsetText(&pCursor->snippet);
 
  3259     sqlite3_result_text(pContext,
 
  3260                         pCursor->snippet.zOffset, pCursor->snippet.nOffset,
 
  3266 ** This routine implements the xFindFunction method for the FTS1
 
  3269 static int fulltextFindFunction(
 
  3270   sqlite3_vtab *pVtab,
 
  3273   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
 
  3276   if( strcmp(zName,"snippet")==0 ){
 
  3277     *pxFunc = snippetFunc;
 
  3279   }else if( strcmp(zName,"offsets")==0 ){
 
  3280     *pxFunc = snippetOffsetsFunc;
 
  3287 ** Rename an fts1 table.
 
  3289 static int fulltextRename(
 
  3290   sqlite3_vtab *pVtab,
 
  3293   fulltext_vtab *p = (fulltext_vtab *)pVtab;
 
  3294   int rc = SQLITE_NOMEM;
 
  3295   char *zSql = sqlite3_mprintf(
 
  3296     "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';"
 
  3297     "ALTER TABLE %Q.'%q_term' RENAME TO '%q_term';"
 
  3298     , p->zDb, p->zName, zName
 
  3299     , p->zDb, p->zName, zName
 
  3302     rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
 
  3308 static const sqlite3_module fulltextModule = {
 
  3310   /* xCreate       */ fulltextCreate,
 
  3311   /* xConnect      */ fulltextConnect,
 
  3312   /* xBestIndex    */ fulltextBestIndex,
 
  3313   /* xDisconnect   */ fulltextDisconnect,
 
  3314   /* xDestroy      */ fulltextDestroy,
 
  3315   /* xOpen         */ fulltextOpen,
 
  3316   /* xClose        */ fulltextClose,
 
  3317   /* xFilter       */ fulltextFilter,
 
  3318   /* xNext         */ fulltextNext,
 
  3319   /* xEof          */ fulltextEof,
 
  3320   /* xColumn       */ fulltextColumn,
 
  3321   /* xRowid        */ fulltextRowid,
 
  3322   /* xUpdate       */ fulltextUpdate,
 
  3327   /* xFindFunction */ fulltextFindFunction,
 
  3328   /* xRename       */ fulltextRename,
 
  3331 int sqlite3Fts1Init(sqlite3 *db){
 
  3332   sqlite3_overload_function(db, "snippet", -1);
 
  3333   sqlite3_overload_function(db, "offsets", -1);
 
  3334   return sqlite3_create_module(db, "fts1", &fulltextModule, 0);
 
  3338 int sqlite3_extension_init(sqlite3 *db, char **pzErrMsg,
 
  3339                            const sqlite3_api_routines *pApi){
 
  3340   SQLITE_EXTENSION_INIT2(pApi)
 
  3341   return sqlite3Fts1Init(db);
 
  3345 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */