modernc.org/cc@v1.0.1/v2/testdata/_sqlite/ext/fts3/fts3_write.c (about)

     1  /*
     2  ** 2009 Oct 23
     3  **
     4  ** The author disclaims copyright to this source code.  In place of
     5  ** a legal notice, here is a blessing:
     6  **
     7  **    May you do good and not evil.
     8  **    May you find forgiveness for yourself and forgive others.
     9  **    May you share freely, never taking more than you give.
    10  **
    11  ******************************************************************************
    12  **
    13  ** This file is part of the SQLite FTS3 extension module. Specifically,
    14  ** this file contains code to insert, update and delete rows from FTS3
    15  ** tables. It also contains code to merge FTS3 b-tree segments. Some
    16  ** of the sub-routines used to merge segments are also used by the query 
    17  ** code in fts3.c.
    18  */
    19  
    20  #include "fts3Int.h"
    21  #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
    22  
    23  #include <string.h>
    24  #include <assert.h>
    25  #include <stdlib.h>
    26  
    27  
    28  #define FTS_MAX_APPENDABLE_HEIGHT 16
    29  
    30  /*
    31  ** When full-text index nodes are loaded from disk, the buffer that they
    32  ** are loaded into has the following number of bytes of padding at the end 
    33  ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
    34  ** of 920 bytes is allocated for it.
    35  **
    36  ** This means that if we have a pointer into a buffer containing node data,
    37  ** it is always safe to read up to two varints from it without risking an
    38  ** overread, even if the node data is corrupted.
    39  */
    40  #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
    41  
    42  /*
    43  ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
    44  ** memory incrementally instead of all at once. This can be a big performance
    45  ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
    46  ** method before retrieving all query results (as may happen, for example,
    47  ** if a query has a LIMIT clause).
    48  **
    49  ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD 
    50  ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
    51  ** The code is written so that the hard lower-limit for each of these values 
    52  ** is 1. Clearly such small values would be inefficient, but can be useful 
    53  ** for testing purposes.
    54  **
    55  ** If this module is built with SQLITE_TEST defined, these constants may
    56  ** be overridden at runtime for testing purposes. File fts3_test.c contains
    57  ** a Tcl interface to read and write the values.
    58  */
    59  #ifdef SQLITE_TEST
    60  int test_fts3_node_chunksize = (4*1024);
    61  int test_fts3_node_chunk_threshold = (4*1024)*4;
    62  # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
    63  # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
    64  #else
    65  # define FTS3_NODE_CHUNKSIZE (4*1024) 
    66  # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
    67  #endif
    68  
    69  /*
    70  ** The two values that may be meaningfully bound to the :1 parameter in
    71  ** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
    72  */
    73  #define FTS_STAT_DOCTOTAL      0
    74  #define FTS_STAT_INCRMERGEHINT 1
    75  #define FTS_STAT_AUTOINCRMERGE 2
    76  
    77  /*
    78  ** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
    79  ** and incremental merge operation that takes place. This is used for 
    80  ** debugging FTS only, it should not usually be turned on in production
    81  ** systems.
    82  */
    83  #ifdef FTS3_LOG_MERGES
    84  static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
    85    sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
    86  }
    87  #else
    88  #define fts3LogMerge(x, y)
    89  #endif
    90  
    91  
    92  typedef struct PendingList PendingList;
    93  typedef struct SegmentNode SegmentNode;
    94  typedef struct SegmentWriter SegmentWriter;
    95  
    96  /*
    97  ** An instance of the following data structure is used to build doclists
    98  ** incrementally. See function fts3PendingListAppend() for details.
    99  */
   100  struct PendingList {
   101    int nData;
   102    char *aData;
   103    int nSpace;
   104    sqlite3_int64 iLastDocid;
   105    sqlite3_int64 iLastCol;
   106    sqlite3_int64 iLastPos;
   107  };
   108  
   109  
   110  /*
   111  ** Each cursor has a (possibly empty) linked list of the following objects.
   112  */
   113  struct Fts3DeferredToken {
   114    Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
   115    int iCol;                       /* Column token must occur in */
   116    Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
   117    PendingList *pList;             /* Doclist is assembled here */
   118  };
   119  
   120  /*
   121  ** An instance of this structure is used to iterate through the terms on
   122  ** a contiguous set of segment b-tree leaf nodes. Although the details of
   123  ** this structure are only manipulated by code in this file, opaque handles
   124  ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
   125  ** terms when querying the full-text index. See functions:
   126  **
   127  **   sqlite3Fts3SegReaderNew()
   128  **   sqlite3Fts3SegReaderFree()
   129  **   sqlite3Fts3SegReaderIterate()
   130  **
   131  ** Methods used to manipulate Fts3SegReader structures:
   132  **
   133  **   fts3SegReaderNext()
   134  **   fts3SegReaderFirstDocid()
   135  **   fts3SegReaderNextDocid()
   136  */
   137  struct Fts3SegReader {
   138    int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
   139    u8 bLookup;                     /* True for a lookup only */
   140    u8 rootOnly;                    /* True for a root-only reader */
   141  
   142    sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
   143    sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
   144    sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
   145    sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
   146  
   147    char *aNode;                    /* Pointer to node data (or NULL) */
   148    int nNode;                      /* Size of buffer at aNode (or 0) */
   149    int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
   150    sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
   151  
   152    Fts3HashElem **ppNextElem;
   153  
   154    /* Variables set by fts3SegReaderNext(). These may be read directly
   155    ** by the caller. They are valid from the time SegmentReaderNew() returns
   156    ** until SegmentReaderNext() returns something other than SQLITE_OK
   157    ** (i.e. SQLITE_DONE).
   158    */
   159    int nTerm;                      /* Number of bytes in current term */
   160    char *zTerm;                    /* Pointer to current term */
   161    int nTermAlloc;                 /* Allocated size of zTerm buffer */
   162    char *aDoclist;                 /* Pointer to doclist of current entry */
   163    int nDoclist;                   /* Size of doclist in current entry */
   164  
   165    /* The following variables are used by fts3SegReaderNextDocid() to iterate 
   166    ** through the current doclist (aDoclist/nDoclist).
   167    */
   168    char *pOffsetList;
   169    int nOffsetList;                /* For descending pending seg-readers only */
   170    sqlite3_int64 iDocid;
   171  };
   172  
   173  #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
   174  #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
   175  
   176  /*
   177  ** An instance of this structure is used to create a segment b-tree in the
   178  ** database. The internal details of this type are only accessed by the
   179  ** following functions:
   180  **
   181  **   fts3SegWriterAdd()
   182  **   fts3SegWriterFlush()
   183  **   fts3SegWriterFree()
   184  */
   185  struct SegmentWriter {
   186    SegmentNode *pTree;             /* Pointer to interior tree structure */
   187    sqlite3_int64 iFirst;           /* First slot in %_segments written */
   188    sqlite3_int64 iFree;            /* Next free slot in %_segments */
   189    char *zTerm;                    /* Pointer to previous term buffer */
   190    int nTerm;                      /* Number of bytes in zTerm */
   191    int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
   192    char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
   193    int nSize;                      /* Size of allocation at aData */
   194    int nData;                      /* Bytes of data in aData */
   195    char *aData;                    /* Pointer to block from malloc() */
   196    i64 nLeafData;                  /* Number of bytes of leaf data written */
   197  };
   198  
   199  /*
   200  ** Type SegmentNode is used by the following three functions to create
   201  ** the interior part of the segment b+-tree structures (everything except
   202  ** the leaf nodes). These functions and type are only ever used by code
   203  ** within the fts3SegWriterXXX() family of functions described above.
   204  **
   205  **   fts3NodeAddTerm()
   206  **   fts3NodeWrite()
   207  **   fts3NodeFree()
   208  **
   209  ** When a b+tree is written to the database (either as a result of a merge
   210  ** or the pending-terms table being flushed), leaves are written into the 
   211  ** database file as soon as they are completely populated. The interior of
   212  ** the tree is assembled in memory and written out only once all leaves have
   213  ** been populated and stored. This is Ok, as the b+-tree fanout is usually
   214  ** very large, meaning that the interior of the tree consumes relatively 
   215  ** little memory.
   216  */
   217  struct SegmentNode {
   218    SegmentNode *pParent;           /* Parent node (or NULL for root node) */
   219    SegmentNode *pRight;            /* Pointer to right-sibling */
   220    SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
   221    int nEntry;                     /* Number of terms written to node so far */
   222    char *zTerm;                    /* Pointer to previous term buffer */
   223    int nTerm;                      /* Number of bytes in zTerm */
   224    int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
   225    char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
   226    int nData;                      /* Bytes of valid data so far */
   227    char *aData;                    /* Node data */
   228  };
   229  
   230  /*
   231  ** Valid values for the second argument to fts3SqlStmt().
   232  */
   233  #define SQL_DELETE_CONTENT             0
   234  #define SQL_IS_EMPTY                   1
   235  #define SQL_DELETE_ALL_CONTENT         2 
   236  #define SQL_DELETE_ALL_SEGMENTS        3
   237  #define SQL_DELETE_ALL_SEGDIR          4
   238  #define SQL_DELETE_ALL_DOCSIZE         5
   239  #define SQL_DELETE_ALL_STAT            6
   240  #define SQL_SELECT_CONTENT_BY_ROWID    7
   241  #define SQL_NEXT_SEGMENT_INDEX         8
   242  #define SQL_INSERT_SEGMENTS            9
   243  #define SQL_NEXT_SEGMENTS_ID          10
   244  #define SQL_INSERT_SEGDIR             11
   245  #define SQL_SELECT_LEVEL              12
   246  #define SQL_SELECT_LEVEL_RANGE        13
   247  #define SQL_SELECT_LEVEL_COUNT        14
   248  #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
   249  #define SQL_DELETE_SEGDIR_LEVEL       16
   250  #define SQL_DELETE_SEGMENTS_RANGE     17
   251  #define SQL_CONTENT_INSERT            18
   252  #define SQL_DELETE_DOCSIZE            19
   253  #define SQL_REPLACE_DOCSIZE           20
   254  #define SQL_SELECT_DOCSIZE            21
   255  #define SQL_SELECT_STAT               22
   256  #define SQL_REPLACE_STAT              23
   257  
   258  #define SQL_SELECT_ALL_PREFIX_LEVEL   24
   259  #define SQL_DELETE_ALL_TERMS_SEGDIR   25
   260  #define SQL_DELETE_SEGDIR_RANGE       26
   261  #define SQL_SELECT_ALL_LANGID         27
   262  #define SQL_FIND_MERGE_LEVEL          28
   263  #define SQL_MAX_LEAF_NODE_ESTIMATE    29
   264  #define SQL_DELETE_SEGDIR_ENTRY       30
   265  #define SQL_SHIFT_SEGDIR_ENTRY        31
   266  #define SQL_SELECT_SEGDIR             32
   267  #define SQL_CHOMP_SEGDIR              33
   268  #define SQL_SEGMENT_IS_APPENDABLE     34
   269  #define SQL_SELECT_INDEXES            35
   270  #define SQL_SELECT_MXLEVEL            36
   271  
   272  #define SQL_SELECT_LEVEL_RANGE2       37
   273  #define SQL_UPDATE_LEVEL_IDX          38
   274  #define SQL_UPDATE_LEVEL              39
   275  
   276  /*
   277  ** This function is used to obtain an SQLite prepared statement handle
   278  ** for the statement identified by the second argument. If successful,
   279  ** *pp is set to the requested statement handle and SQLITE_OK returned.
   280  ** Otherwise, an SQLite error code is returned and *pp is set to 0.
   281  **
   282  ** If argument apVal is not NULL, then it must point to an array with
   283  ** at least as many entries as the requested statement has bound 
   284  ** parameters. The values are bound to the statements parameters before
   285  ** returning.
   286  */
   287  static int fts3SqlStmt(
   288    Fts3Table *p,                   /* Virtual table handle */
   289    int eStmt,                      /* One of the SQL_XXX constants above */
   290    sqlite3_stmt **pp,              /* OUT: Statement handle */
   291    sqlite3_value **apVal           /* Values to bind to statement */
   292  ){
   293    const char *azSql[] = {
   294  /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
   295  /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
   296  /* 2  */  "DELETE FROM %Q.'%q_content'",
   297  /* 3  */  "DELETE FROM %Q.'%q_segments'",
   298  /* 4  */  "DELETE FROM %Q.'%q_segdir'",
   299  /* 5  */  "DELETE FROM %Q.'%q_docsize'",
   300  /* 6  */  "DELETE FROM %Q.'%q_stat'",
   301  /* 7  */  "SELECT %s WHERE rowid=?",
   302  /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
   303  /* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
   304  /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
   305  /* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
   306  
   307            /* Return segments in order from oldest to newest.*/ 
   308  /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
   309              "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
   310  /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
   311              "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
   312              "ORDER BY level DESC, idx ASC",
   313  
   314  /* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
   315  /* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
   316  
   317  /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
   318  /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
   319  /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
   320  /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
   321  /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
   322  /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
   323  /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
   324  /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
   325  /* 24 */  "",
   326  /* 25 */  "",
   327  
   328  /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
   329  /* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
   330  
   331  /* This statement is used to determine which level to read the input from
   332  ** when performing an incremental merge. It returns the absolute level number
   333  ** of the oldest level in the db that contains at least ? segments. Or,
   334  ** if no level in the FTS index contains more than ? segments, the statement
   335  ** returns zero rows.  */
   336  /* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
   337           "  GROUP BY level HAVING cnt>=?"
   338           "  ORDER BY (level %% 1024) ASC LIMIT 1",
   339  
   340  /* Estimate the upper limit on the number of leaf nodes in a new segment
   341  ** created by merging the oldest :2 segments from absolute level :1. See 
   342  ** function sqlite3Fts3Incrmerge() for details.  */
   343  /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
   344           "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
   345  
   346  /* SQL_DELETE_SEGDIR_ENTRY
   347  **   Delete the %_segdir entry on absolute level :1 with index :2.  */
   348  /* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
   349  
   350  /* SQL_SHIFT_SEGDIR_ENTRY
   351  **   Modify the idx value for the segment with idx=:3 on absolute level :2
   352  **   to :1.  */
   353  /* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
   354  
   355  /* SQL_SELECT_SEGDIR
   356  **   Read a single entry from the %_segdir table. The entry from absolute 
   357  **   level :1 with index value :2.  */
   358  /* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
   359              "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
   360  
   361  /* SQL_CHOMP_SEGDIR
   362  **   Update the start_block (:1) and root (:2) fields of the %_segdir
   363  **   entry located on absolute level :3 with index :4.  */
   364  /* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
   365              "WHERE level = ? AND idx = ?",
   366  
   367  /* SQL_SEGMENT_IS_APPENDABLE
   368  **   Return a single row if the segment with end_block=? is appendable. Or
   369  **   no rows otherwise.  */
   370  /* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
   371  
   372  /* SQL_SELECT_INDEXES
   373  **   Return the list of valid segment indexes for absolute level ?  */
   374  /* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
   375  
   376  /* SQL_SELECT_MXLEVEL
   377  **   Return the largest relative level in the FTS index or indexes.  */
   378  /* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
   379  
   380            /* Return segments in order from oldest to newest.*/ 
   381  /* 37 */  "SELECT level, idx, end_block "
   382              "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
   383              "ORDER BY level DESC, idx ASC",
   384  
   385            /* Update statements used while promoting segments */
   386  /* 38 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
   387              "WHERE level=? AND idx=?",
   388  /* 39 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
   389  
   390    };
   391    int rc = SQLITE_OK;
   392    sqlite3_stmt *pStmt;
   393  
   394    assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
   395    assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
   396    
   397    pStmt = p->aStmt[eStmt];
   398    if( !pStmt ){
   399      char *zSql;
   400      if( eStmt==SQL_CONTENT_INSERT ){
   401        zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
   402      }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
   403        zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
   404      }else{
   405        zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
   406      }
   407      if( !zSql ){
   408        rc = SQLITE_NOMEM;
   409      }else{
   410        rc = sqlite3_prepare_v3(p->db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
   411                                &pStmt, NULL);
   412        sqlite3_free(zSql);
   413        assert( rc==SQLITE_OK || pStmt==0 );
   414        p->aStmt[eStmt] = pStmt;
   415      }
   416    }
   417    if( apVal ){
   418      int i;
   419      int nParam = sqlite3_bind_parameter_count(pStmt);
   420      for(i=0; rc==SQLITE_OK && i<nParam; i++){
   421        rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
   422      }
   423    }
   424    *pp = pStmt;
   425    return rc;
   426  }
   427  
   428  
   429  static int fts3SelectDocsize(
   430    Fts3Table *pTab,                /* FTS3 table handle */
   431    sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
   432    sqlite3_stmt **ppStmt           /* OUT: Statement handle */
   433  ){
   434    sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
   435    int rc;                         /* Return code */
   436  
   437    rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
   438    if( rc==SQLITE_OK ){
   439      sqlite3_bind_int64(pStmt, 1, iDocid);
   440      rc = sqlite3_step(pStmt);
   441      if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
   442        rc = sqlite3_reset(pStmt);
   443        if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
   444        pStmt = 0;
   445      }else{
   446        rc = SQLITE_OK;
   447      }
   448    }
   449  
   450    *ppStmt = pStmt;
   451    return rc;
   452  }
   453  
   454  int sqlite3Fts3SelectDoctotal(
   455    Fts3Table *pTab,                /* Fts3 table handle */
   456    sqlite3_stmt **ppStmt           /* OUT: Statement handle */
   457  ){
   458    sqlite3_stmt *pStmt = 0;
   459    int rc;
   460    rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
   461    if( rc==SQLITE_OK ){
   462      sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
   463      if( sqlite3_step(pStmt)!=SQLITE_ROW
   464       || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
   465      ){
   466        rc = sqlite3_reset(pStmt);
   467        if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
   468        pStmt = 0;
   469      }
   470    }
   471    *ppStmt = pStmt;
   472    return rc;
   473  }
   474  
   475  int sqlite3Fts3SelectDocsize(
   476    Fts3Table *pTab,                /* Fts3 table handle */
   477    sqlite3_int64 iDocid,           /* Docid to read size data for */
   478    sqlite3_stmt **ppStmt           /* OUT: Statement handle */
   479  ){
   480    return fts3SelectDocsize(pTab, iDocid, ppStmt);
   481  }
   482  
   483  /*
   484  ** Similar to fts3SqlStmt(). Except, after binding the parameters in
   485  ** array apVal[] to the SQL statement identified by eStmt, the statement
   486  ** is executed.
   487  **
   488  ** Returns SQLITE_OK if the statement is successfully executed, or an
   489  ** SQLite error code otherwise.
   490  */
   491  static void fts3SqlExec(
   492    int *pRC,                /* Result code */
   493    Fts3Table *p,            /* The FTS3 table */
   494    int eStmt,               /* Index of statement to evaluate */
   495    sqlite3_value **apVal    /* Parameters to bind */
   496  ){
   497    sqlite3_stmt *pStmt;
   498    int rc;
   499    if( *pRC ) return;
   500    rc = fts3SqlStmt(p, eStmt, &pStmt, apVal); 
   501    if( rc==SQLITE_OK ){
   502      sqlite3_step(pStmt);
   503      rc = sqlite3_reset(pStmt);
   504    }
   505    *pRC = rc;
   506  }
   507  
   508  
   509  /*
   510  ** This function ensures that the caller has obtained an exclusive 
   511  ** shared-cache table-lock on the %_segdir table. This is required before 
   512  ** writing data to the fts3 table. If this lock is not acquired first, then
   513  ** the caller may end up attempting to take this lock as part of committing
   514  ** a transaction, causing SQLite to return SQLITE_LOCKED or 
   515  ** LOCKED_SHAREDCACHEto a COMMIT command.
   516  **
   517  ** It is best to avoid this because if FTS3 returns any error when 
   518  ** committing a transaction, the whole transaction will be rolled back. 
   519  ** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. 
   520  ** It can still happen if the user locks the underlying tables directly 
   521  ** instead of accessing them via FTS.
   522  */
   523  static int fts3Writelock(Fts3Table *p){
   524    int rc = SQLITE_OK;
   525    
   526    if( p->nPendingData==0 ){
   527      sqlite3_stmt *pStmt;
   528      rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
   529      if( rc==SQLITE_OK ){
   530        sqlite3_bind_null(pStmt, 1);
   531        sqlite3_step(pStmt);
   532        rc = sqlite3_reset(pStmt);
   533      }
   534    }
   535  
   536    return rc;
   537  }
   538  
   539  /*
   540  ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
   541  ** Within each language id, a separate index is maintained to store the
   542  ** document terms, and each configured prefix size (configured the FTS 
   543  ** "prefix=" option). And each index consists of multiple levels ("relative
   544  ** levels").
   545  **
   546  ** All three of these values (the language id, the specific index and the
   547  ** level within the index) are encoded in 64-bit integer values stored
   548  ** in the %_segdir table on disk. This function is used to convert three
   549  ** separate component values into the single 64-bit integer value that
   550  ** can be used to query the %_segdir table.
   551  **
   552  ** Specifically, each language-id/index combination is allocated 1024 
   553  ** 64-bit integer level values ("absolute levels"). The main terms index
   554  ** for language-id 0 is allocate values 0-1023. The first prefix index
   555  ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
   556  ** Language 1 indexes are allocated immediately following language 0.
   557  **
   558  ** So, for a system with nPrefix prefix indexes configured, the block of
   559  ** absolute levels that corresponds to language-id iLangid and index 
   560  ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
   561  */
   562  static sqlite3_int64 getAbsoluteLevel(
   563    Fts3Table *p,                   /* FTS3 table handle */
   564    int iLangid,                    /* Language id */
   565    int iIndex,                     /* Index in p->aIndex[] */
   566    int iLevel                      /* Level of segments */
   567  ){
   568    sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
   569    assert( iLangid>=0 );
   570    assert( p->nIndex>0 );
   571    assert( iIndex>=0 && iIndex<p->nIndex );
   572  
   573    iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
   574    return iBase + iLevel;
   575  }
   576  
   577  /*
   578  ** Set *ppStmt to a statement handle that may be used to iterate through
   579  ** all rows in the %_segdir table, from oldest to newest. If successful,
   580  ** return SQLITE_OK. If an error occurs while preparing the statement, 
   581  ** return an SQLite error code.
   582  **
   583  ** There is only ever one instance of this SQL statement compiled for
   584  ** each FTS3 table.
   585  **
   586  ** The statement returns the following columns from the %_segdir table:
   587  **
   588  **   0: idx
   589  **   1: start_block
   590  **   2: leaves_end_block
   591  **   3: end_block
   592  **   4: root
   593  */
   594  int sqlite3Fts3AllSegdirs(
   595    Fts3Table *p,                   /* FTS3 table */
   596    int iLangid,                    /* Language being queried */
   597    int iIndex,                     /* Index for p->aIndex[] */
   598    int iLevel,                     /* Level to select (relative level) */
   599    sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
   600  ){
   601    int rc;
   602    sqlite3_stmt *pStmt = 0;
   603  
   604    assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
   605    assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
   606    assert( iIndex>=0 && iIndex<p->nIndex );
   607  
   608    if( iLevel<0 ){
   609      /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
   610      rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
   611      if( rc==SQLITE_OK ){ 
   612        sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
   613        sqlite3_bind_int64(pStmt, 2, 
   614            getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
   615        );
   616      }
   617    }else{
   618      /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
   619      rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
   620      if( rc==SQLITE_OK ){ 
   621        sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
   622      }
   623    }
   624    *ppStmt = pStmt;
   625    return rc;
   626  }
   627  
   628  
   629  /*
   630  ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
   631  ** if successful, or an SQLite error code otherwise.
   632  **
   633  ** This function also serves to allocate the PendingList structure itself.
   634  ** For example, to create a new PendingList structure containing two
   635  ** varints:
   636  **
   637  **   PendingList *p = 0;
   638  **   fts3PendingListAppendVarint(&p, 1);
   639  **   fts3PendingListAppendVarint(&p, 2);
   640  */
   641  static int fts3PendingListAppendVarint(
   642    PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
   643    sqlite3_int64 i                 /* Value to append to data */
   644  ){
   645    PendingList *p = *pp;
   646  
   647    /* Allocate or grow the PendingList as required. */
   648    if( !p ){
   649      p = sqlite3_malloc(sizeof(*p) + 100);
   650      if( !p ){
   651        return SQLITE_NOMEM;
   652      }
   653      p->nSpace = 100;
   654      p->aData = (char *)&p[1];
   655      p->nData = 0;
   656    }
   657    else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
   658      int nNew = p->nSpace * 2;
   659      p = sqlite3_realloc(p, sizeof(*p) + nNew);
   660      if( !p ){
   661        sqlite3_free(*pp);
   662        *pp = 0;
   663        return SQLITE_NOMEM;
   664      }
   665      p->nSpace = nNew;
   666      p->aData = (char *)&p[1];
   667    }
   668  
   669    /* Append the new serialized varint to the end of the list. */
   670    p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
   671    p->aData[p->nData] = '\0';
   672    *pp = p;
   673    return SQLITE_OK;
   674  }
   675  
   676  /*
   677  ** Add a docid/column/position entry to a PendingList structure. Non-zero
   678  ** is returned if the structure is sqlite3_realloced as part of adding
   679  ** the entry. Otherwise, zero.
   680  **
   681  ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
   682  ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
   683  ** it is set to SQLITE_OK.
   684  */
   685  static int fts3PendingListAppend(
   686    PendingList **pp,               /* IN/OUT: PendingList structure */
   687    sqlite3_int64 iDocid,           /* Docid for entry to add */
   688    sqlite3_int64 iCol,             /* Column for entry to add */
   689    sqlite3_int64 iPos,             /* Position of term for entry to add */
   690    int *pRc                        /* OUT: Return code */
   691  ){
   692    PendingList *p = *pp;
   693    int rc = SQLITE_OK;
   694  
   695    assert( !p || p->iLastDocid<=iDocid );
   696  
   697    if( !p || p->iLastDocid!=iDocid ){
   698      sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
   699      if( p ){
   700        assert( p->nData<p->nSpace );
   701        assert( p->aData[p->nData]==0 );
   702        p->nData++;
   703      }
   704      if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
   705        goto pendinglistappend_out;
   706      }
   707      p->iLastCol = -1;
   708      p->iLastPos = 0;
   709      p->iLastDocid = iDocid;
   710    }
   711    if( iCol>0 && p->iLastCol!=iCol ){
   712      if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
   713       || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
   714      ){
   715        goto pendinglistappend_out;
   716      }
   717      p->iLastCol = iCol;
   718      p->iLastPos = 0;
   719    }
   720    if( iCol>=0 ){
   721      assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
   722      rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
   723      if( rc==SQLITE_OK ){
   724        p->iLastPos = iPos;
   725      }
   726    }
   727  
   728   pendinglistappend_out:
   729    *pRc = rc;
   730    if( p!=*pp ){
   731      *pp = p;
   732      return 1;
   733    }
   734    return 0;
   735  }
   736  
   737  /*
   738  ** Free a PendingList object allocated by fts3PendingListAppend().
   739  */
   740  static void fts3PendingListDelete(PendingList *pList){
   741    sqlite3_free(pList);
   742  }
   743  
   744  /*
   745  ** Add an entry to one of the pending-terms hash tables.
   746  */
   747  static int fts3PendingTermsAddOne(
   748    Fts3Table *p,
   749    int iCol,
   750    int iPos,
   751    Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
   752    const char *zToken,
   753    int nToken
   754  ){
   755    PendingList *pList;
   756    int rc = SQLITE_OK;
   757  
   758    pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
   759    if( pList ){
   760      p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
   761    }
   762    if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
   763      if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
   764        /* Malloc failed while inserting the new entry. This can only 
   765        ** happen if there was no previous entry for this token.
   766        */
   767        assert( 0==fts3HashFind(pHash, zToken, nToken) );
   768        sqlite3_free(pList);
   769        rc = SQLITE_NOMEM;
   770      }
   771    }
   772    if( rc==SQLITE_OK ){
   773      p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
   774    }
   775    return rc;
   776  }
   777  
   778  /*
   779  ** Tokenize the nul-terminated string zText and add all tokens to the
   780  ** pending-terms hash-table. The docid used is that currently stored in
   781  ** p->iPrevDocid, and the column is specified by argument iCol.
   782  **
   783  ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
   784  */
   785  static int fts3PendingTermsAdd(
   786    Fts3Table *p,                   /* Table into which text will be inserted */
   787    int iLangid,                    /* Language id to use */
   788    const char *zText,              /* Text of document to be inserted */
   789    int iCol,                       /* Column into which text is being inserted */
   790    u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
   791  ){
   792    int rc;
   793    int iStart = 0;
   794    int iEnd = 0;
   795    int iPos = 0;
   796    int nWord = 0;
   797  
   798    char const *zToken;
   799    int nToken = 0;
   800  
   801    sqlite3_tokenizer *pTokenizer = p->pTokenizer;
   802    sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
   803    sqlite3_tokenizer_cursor *pCsr;
   804    int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
   805        const char**,int*,int*,int*,int*);
   806  
   807    assert( pTokenizer && pModule );
   808  
   809    /* If the user has inserted a NULL value, this function may be called with
   810    ** zText==0. In this case, add zero token entries to the hash table and 
   811    ** return early. */
   812    if( zText==0 ){
   813      *pnWord = 0;
   814      return SQLITE_OK;
   815    }
   816  
   817    rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
   818    if( rc!=SQLITE_OK ){
   819      return rc;
   820    }
   821  
   822    xNext = pModule->xNext;
   823    while( SQLITE_OK==rc
   824        && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
   825    ){
   826      int i;
   827      if( iPos>=nWord ) nWord = iPos+1;
   828  
   829      /* Positions cannot be negative; we use -1 as a terminator internally.
   830      ** Tokens must have a non-zero length.
   831      */
   832      if( iPos<0 || !zToken || nToken<=0 ){
   833        rc = SQLITE_ERROR;
   834        break;
   835      }
   836  
   837      /* Add the term to the terms index */
   838      rc = fts3PendingTermsAddOne(
   839          p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
   840      );
   841      
   842      /* Add the term to each of the prefix indexes that it is not too 
   843      ** short for. */
   844      for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
   845        struct Fts3Index *pIndex = &p->aIndex[i];
   846        if( nToken<pIndex->nPrefix ) continue;
   847        rc = fts3PendingTermsAddOne(
   848            p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
   849        );
   850      }
   851    }
   852  
   853    pModule->xClose(pCsr);
   854    *pnWord += nWord;
   855    return (rc==SQLITE_DONE ? SQLITE_OK : rc);
   856  }
   857  
   858  /* 
   859  ** Calling this function indicates that subsequent calls to 
   860  ** fts3PendingTermsAdd() are to add term/position-list pairs for the
   861  ** contents of the document with docid iDocid.
   862  */
   863  static int fts3PendingTermsDocid(
   864    Fts3Table *p,                   /* Full-text table handle */
   865    int bDelete,                    /* True if this op is a delete */
   866    int iLangid,                    /* Language id of row being written */
   867    sqlite_int64 iDocid             /* Docid of row being written */
   868  ){
   869    assert( iLangid>=0 );
   870    assert( bDelete==1 || bDelete==0 );
   871  
   872    /* TODO(shess) Explore whether partially flushing the buffer on
   873    ** forced-flush would provide better performance.  I suspect that if
   874    ** we ordered the doclists by size and flushed the largest until the
   875    ** buffer was half empty, that would let the less frequent terms
   876    ** generate longer doclists.
   877    */
   878    if( iDocid<p->iPrevDocid 
   879     || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
   880     || p->iPrevLangid!=iLangid
   881     || p->nPendingData>p->nMaxPendingData 
   882    ){
   883      int rc = sqlite3Fts3PendingTermsFlush(p);
   884      if( rc!=SQLITE_OK ) return rc;
   885    }
   886    p->iPrevDocid = iDocid;
   887    p->iPrevLangid = iLangid;
   888    p->bPrevDelete = bDelete;
   889    return SQLITE_OK;
   890  }
   891  
   892  /*
   893  ** Discard the contents of the pending-terms hash tables. 
   894  */
   895  void sqlite3Fts3PendingTermsClear(Fts3Table *p){
   896    int i;
   897    for(i=0; i<p->nIndex; i++){
   898      Fts3HashElem *pElem;
   899      Fts3Hash *pHash = &p->aIndex[i].hPending;
   900      for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
   901        PendingList *pList = (PendingList *)fts3HashData(pElem);
   902        fts3PendingListDelete(pList);
   903      }
   904      fts3HashClear(pHash);
   905    }
   906    p->nPendingData = 0;
   907  }
   908  
   909  /*
   910  ** This function is called by the xUpdate() method as part of an INSERT
   911  ** operation. It adds entries for each term in the new record to the
   912  ** pendingTerms hash table.
   913  **
   914  ** Argument apVal is the same as the similarly named argument passed to
   915  ** fts3InsertData(). Parameter iDocid is the docid of the new row.
   916  */
   917  static int fts3InsertTerms(
   918    Fts3Table *p, 
   919    int iLangid, 
   920    sqlite3_value **apVal, 
   921    u32 *aSz
   922  ){
   923    int i;                          /* Iterator variable */
   924    for(i=2; i<p->nColumn+2; i++){
   925      int iCol = i-2;
   926      if( p->abNotindexed[iCol]==0 ){
   927        const char *zText = (const char *)sqlite3_value_text(apVal[i]);
   928        int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
   929        if( rc!=SQLITE_OK ){
   930          return rc;
   931        }
   932        aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
   933      }
   934    }
   935    return SQLITE_OK;
   936  }
   937  
   938  /*
   939  ** This function is called by the xUpdate() method for an INSERT operation.
   940  ** The apVal parameter is passed a copy of the apVal argument passed by
   941  ** SQLite to the xUpdate() method. i.e:
   942  **
   943  **   apVal[0]                Not used for INSERT.
   944  **   apVal[1]                rowid
   945  **   apVal[2]                Left-most user-defined column
   946  **   ...
   947  **   apVal[p->nColumn+1]     Right-most user-defined column
   948  **   apVal[p->nColumn+2]     Hidden column with same name as table
   949  **   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
   950  **   apVal[p->nColumn+4]     Hidden languageid column
   951  */
   952  static int fts3InsertData(
   953    Fts3Table *p,                   /* Full-text table */
   954    sqlite3_value **apVal,          /* Array of values to insert */
   955    sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
   956  ){
   957    int rc;                         /* Return code */
   958    sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
   959  
   960    if( p->zContentTbl ){
   961      sqlite3_value *pRowid = apVal[p->nColumn+3];
   962      if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
   963        pRowid = apVal[1];
   964      }
   965      if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
   966        return SQLITE_CONSTRAINT;
   967      }
   968      *piDocid = sqlite3_value_int64(pRowid);
   969      return SQLITE_OK;
   970    }
   971  
   972    /* Locate the statement handle used to insert data into the %_content
   973    ** table. The SQL for this statement is:
   974    **
   975    **   INSERT INTO %_content VALUES(?, ?, ?, ...)
   976    **
   977    ** The statement features N '?' variables, where N is the number of user
   978    ** defined columns in the FTS3 table, plus one for the docid field.
   979    */
   980    rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
   981    if( rc==SQLITE_OK && p->zLanguageid ){
   982      rc = sqlite3_bind_int(
   983          pContentInsert, p->nColumn+2, 
   984          sqlite3_value_int(apVal[p->nColumn+4])
   985      );
   986    }
   987    if( rc!=SQLITE_OK ) return rc;
   988  
   989    /* There is a quirk here. The users INSERT statement may have specified
   990    ** a value for the "rowid" field, for the "docid" field, or for both.
   991    ** Which is a problem, since "rowid" and "docid" are aliases for the
   992    ** same value. For example:
   993    **
   994    **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
   995    **
   996    ** In FTS3, this is an error. It is an error to specify non-NULL values
   997    ** for both docid and some other rowid alias.
   998    */
   999    if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
  1000      if( SQLITE_NULL==sqlite3_value_type(apVal[0])
  1001       && SQLITE_NULL!=sqlite3_value_type(apVal[1])
  1002      ){
  1003        /* A rowid/docid conflict. */
  1004        return SQLITE_ERROR;
  1005      }
  1006      rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
  1007      if( rc!=SQLITE_OK ) return rc;
  1008    }
  1009  
  1010    /* Execute the statement to insert the record. Set *piDocid to the 
  1011    ** new docid value. 
  1012    */
  1013    sqlite3_step(pContentInsert);
  1014    rc = sqlite3_reset(pContentInsert);
  1015  
  1016    *piDocid = sqlite3_last_insert_rowid(p->db);
  1017    return rc;
  1018  }
  1019  
  1020  
  1021  
  1022  /*
  1023  ** Remove all data from the FTS3 table. Clear the hash table containing
  1024  ** pending terms.
  1025  */
  1026  static int fts3DeleteAll(Fts3Table *p, int bContent){
  1027    int rc = SQLITE_OK;             /* Return code */
  1028  
  1029    /* Discard the contents of the pending-terms hash table. */
  1030    sqlite3Fts3PendingTermsClear(p);
  1031  
  1032    /* Delete everything from the shadow tables. Except, leave %_content as
  1033    ** is if bContent is false.  */
  1034    assert( p->zContentTbl==0 || bContent==0 );
  1035    if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
  1036    fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
  1037    fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
  1038    if( p->bHasDocsize ){
  1039      fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
  1040    }
  1041    if( p->bHasStat ){
  1042      fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
  1043    }
  1044    return rc;
  1045  }
  1046  
  1047  /*
  1048  **
  1049  */
  1050  static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
  1051    int iLangid = 0;
  1052    if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
  1053    return iLangid;
  1054  }
  1055  
  1056  /*
  1057  ** The first element in the apVal[] array is assumed to contain the docid
  1058  ** (an integer) of a row about to be deleted. Remove all terms from the
  1059  ** full-text index.
  1060  */
  1061  static void fts3DeleteTerms( 
  1062    int *pRC,               /* Result code */
  1063    Fts3Table *p,           /* The FTS table to delete from */
  1064    sqlite3_value *pRowid,  /* The docid to be deleted */
  1065    u32 *aSz,               /* Sizes of deleted document written here */
  1066    int *pbFound            /* OUT: Set to true if row really does exist */
  1067  ){
  1068    int rc;
  1069    sqlite3_stmt *pSelect;
  1070  
  1071    assert( *pbFound==0 );
  1072    if( *pRC ) return;
  1073    rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
  1074    if( rc==SQLITE_OK ){
  1075      if( SQLITE_ROW==sqlite3_step(pSelect) ){
  1076        int i;
  1077        int iLangid = langidFromSelect(p, pSelect);
  1078        i64 iDocid = sqlite3_column_int64(pSelect, 0);
  1079        rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
  1080        for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
  1081          int iCol = i-1;
  1082          if( p->abNotindexed[iCol]==0 ){
  1083            const char *zText = (const char *)sqlite3_column_text(pSelect, i);
  1084            rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
  1085            aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
  1086          }
  1087        }
  1088        if( rc!=SQLITE_OK ){
  1089          sqlite3_reset(pSelect);
  1090          *pRC = rc;
  1091          return;
  1092        }
  1093        *pbFound = 1;
  1094      }
  1095      rc = sqlite3_reset(pSelect);
  1096    }else{
  1097      sqlite3_reset(pSelect);
  1098    }
  1099    *pRC = rc;
  1100  }
  1101  
  1102  /*
  1103  ** Forward declaration to account for the circular dependency between
  1104  ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
  1105  */
  1106  static int fts3SegmentMerge(Fts3Table *, int, int, int);
  1107  
  1108  /* 
  1109  ** This function allocates a new level iLevel index in the segdir table.
  1110  ** Usually, indexes are allocated within a level sequentially starting
  1111  ** with 0, so the allocated index is one greater than the value returned
  1112  ** by:
  1113  **
  1114  **   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
  1115  **
  1116  ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
  1117  ** level, they are merged into a single level (iLevel+1) segment and the 
  1118  ** allocated index is 0.
  1119  **
  1120  ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
  1121  ** returned. Otherwise, an SQLite error code is returned.
  1122  */
  1123  static int fts3AllocateSegdirIdx(
  1124    Fts3Table *p, 
  1125    int iLangid,                    /* Language id */
  1126    int iIndex,                     /* Index for p->aIndex */
  1127    int iLevel, 
  1128    int *piIdx
  1129  ){
  1130    int rc;                         /* Return Code */
  1131    sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
  1132    int iNext = 0;                  /* Result of query pNextIdx */
  1133  
  1134    assert( iLangid>=0 );
  1135    assert( p->nIndex>=1 );
  1136  
  1137    /* Set variable iNext to the next available segdir index at level iLevel. */
  1138    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
  1139    if( rc==SQLITE_OK ){
  1140      sqlite3_bind_int64(
  1141          pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
  1142      );
  1143      if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
  1144        iNext = sqlite3_column_int(pNextIdx, 0);
  1145      }
  1146      rc = sqlite3_reset(pNextIdx);
  1147    }
  1148  
  1149    if( rc==SQLITE_OK ){
  1150      /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
  1151      ** full, merge all segments in level iLevel into a single iLevel+1
  1152      ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
  1153      ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
  1154      */
  1155      if( iNext>=FTS3_MERGE_COUNT ){
  1156        fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
  1157        rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
  1158        *piIdx = 0;
  1159      }else{
  1160        *piIdx = iNext;
  1161      }
  1162    }
  1163  
  1164    return rc;
  1165  }
  1166  
  1167  /*
  1168  ** The %_segments table is declared as follows:
  1169  **
  1170  **   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
  1171  **
  1172  ** This function reads data from a single row of the %_segments table. The
  1173  ** specific row is identified by the iBlockid parameter. If paBlob is not
  1174  ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
  1175  ** with the contents of the blob stored in the "block" column of the 
  1176  ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
  1177  ** to the size of the blob in bytes before returning.
  1178  **
  1179  ** If an error occurs, or the table does not contain the specified row,
  1180  ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
  1181  ** paBlob is non-NULL, then it is the responsibility of the caller to
  1182  ** eventually free the returned buffer.
  1183  **
  1184  ** This function may leave an open sqlite3_blob* handle in the
  1185  ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
  1186  ** to this function. The handle may be closed by calling the
  1187  ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
  1188  ** performance improvement, but the blob handle should always be closed
  1189  ** before control is returned to the user (to prevent a lock being held
  1190  ** on the database file for longer than necessary). Thus, any virtual table
  1191  ** method (xFilter etc.) that may directly or indirectly call this function
  1192  ** must call sqlite3Fts3SegmentsClose() before returning.
  1193  */
  1194  int sqlite3Fts3ReadBlock(
  1195    Fts3Table *p,                   /* FTS3 table handle */
  1196    sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
  1197    char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
  1198    int *pnBlob,                    /* OUT: Size of blob data */
  1199    int *pnLoad                     /* OUT: Bytes actually loaded */
  1200  ){
  1201    int rc;                         /* Return code */
  1202  
  1203    /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
  1204    assert( pnBlob );
  1205  
  1206    if( p->pSegments ){
  1207      rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
  1208    }else{
  1209      if( 0==p->zSegmentsTbl ){
  1210        p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
  1211        if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
  1212      }
  1213      rc = sqlite3_blob_open(
  1214         p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
  1215      );
  1216    }
  1217  
  1218    if( rc==SQLITE_OK ){
  1219      int nByte = sqlite3_blob_bytes(p->pSegments);
  1220      *pnBlob = nByte;
  1221      if( paBlob ){
  1222        char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
  1223        if( !aByte ){
  1224          rc = SQLITE_NOMEM;
  1225        }else{
  1226          if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
  1227            nByte = FTS3_NODE_CHUNKSIZE;
  1228            *pnLoad = nByte;
  1229          }
  1230          rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
  1231          memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
  1232          if( rc!=SQLITE_OK ){
  1233            sqlite3_free(aByte);
  1234            aByte = 0;
  1235          }
  1236        }
  1237        *paBlob = aByte;
  1238      }
  1239    }
  1240  
  1241    return rc;
  1242  }
  1243  
  1244  /*
  1245  ** Close the blob handle at p->pSegments, if it is open. See comments above
  1246  ** the sqlite3Fts3ReadBlock() function for details.
  1247  */
  1248  void sqlite3Fts3SegmentsClose(Fts3Table *p){
  1249    sqlite3_blob_close(p->pSegments);
  1250    p->pSegments = 0;
  1251  }
  1252      
  1253  static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
  1254    int nRead;                      /* Number of bytes to read */
  1255    int rc;                         /* Return code */
  1256  
  1257    nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
  1258    rc = sqlite3_blob_read(
  1259        pReader->pBlob, 
  1260        &pReader->aNode[pReader->nPopulate],
  1261        nRead,
  1262        pReader->nPopulate
  1263    );
  1264  
  1265    if( rc==SQLITE_OK ){
  1266      pReader->nPopulate += nRead;
  1267      memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
  1268      if( pReader->nPopulate==pReader->nNode ){
  1269        sqlite3_blob_close(pReader->pBlob);
  1270        pReader->pBlob = 0;
  1271        pReader->nPopulate = 0;
  1272      }
  1273    }
  1274    return rc;
  1275  }
  1276  
  1277  static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
  1278    int rc = SQLITE_OK;
  1279    assert( !pReader->pBlob 
  1280         || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
  1281    );
  1282    while( pReader->pBlob && rc==SQLITE_OK 
  1283       &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
  1284    ){
  1285      rc = fts3SegReaderIncrRead(pReader);
  1286    }
  1287    return rc;
  1288  }
  1289  
  1290  /*
  1291  ** Set an Fts3SegReader cursor to point at EOF.
  1292  */
  1293  static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
  1294    if( !fts3SegReaderIsRootOnly(pSeg) ){
  1295      sqlite3_free(pSeg->aNode);
  1296      sqlite3_blob_close(pSeg->pBlob);
  1297      pSeg->pBlob = 0;
  1298    }
  1299    pSeg->aNode = 0;
  1300  }
  1301  
  1302  /*
  1303  ** Move the iterator passed as the first argument to the next term in the
  1304  ** segment. If successful, SQLITE_OK is returned. If there is no next term,
  1305  ** SQLITE_DONE. Otherwise, an SQLite error code.
  1306  */
  1307  static int fts3SegReaderNext(
  1308    Fts3Table *p, 
  1309    Fts3SegReader *pReader,
  1310    int bIncr
  1311  ){
  1312    int rc;                         /* Return code of various sub-routines */
  1313    char *pNext;                    /* Cursor variable */
  1314    int nPrefix;                    /* Number of bytes in term prefix */
  1315    int nSuffix;                    /* Number of bytes in term suffix */
  1316  
  1317    if( !pReader->aDoclist ){
  1318      pNext = pReader->aNode;
  1319    }else{
  1320      pNext = &pReader->aDoclist[pReader->nDoclist];
  1321    }
  1322  
  1323    if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
  1324  
  1325      if( fts3SegReaderIsPending(pReader) ){
  1326        Fts3HashElem *pElem = *(pReader->ppNextElem);
  1327        sqlite3_free(pReader->aNode);
  1328        pReader->aNode = 0;
  1329        if( pElem ){
  1330          char *aCopy;
  1331          PendingList *pList = (PendingList *)fts3HashData(pElem);
  1332          int nCopy = pList->nData+1;
  1333          pReader->zTerm = (char *)fts3HashKey(pElem);
  1334          pReader->nTerm = fts3HashKeysize(pElem);
  1335          aCopy = (char*)sqlite3_malloc(nCopy);
  1336          if( !aCopy ) return SQLITE_NOMEM;
  1337          memcpy(aCopy, pList->aData, nCopy);
  1338          pReader->nNode = pReader->nDoclist = nCopy;
  1339          pReader->aNode = pReader->aDoclist = aCopy;
  1340          pReader->ppNextElem++;
  1341          assert( pReader->aNode );
  1342        }
  1343        return SQLITE_OK;
  1344      }
  1345  
  1346      fts3SegReaderSetEof(pReader);
  1347  
  1348      /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
  1349      ** blocks have already been traversed.  */
  1350      assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
  1351      if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
  1352        return SQLITE_OK;
  1353      }
  1354  
  1355      rc = sqlite3Fts3ReadBlock(
  1356          p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode, 
  1357          (bIncr ? &pReader->nPopulate : 0)
  1358      );
  1359      if( rc!=SQLITE_OK ) return rc;
  1360      assert( pReader->pBlob==0 );
  1361      if( bIncr && pReader->nPopulate<pReader->nNode ){
  1362        pReader->pBlob = p->pSegments;
  1363        p->pSegments = 0;
  1364      }
  1365      pNext = pReader->aNode;
  1366    }
  1367  
  1368    assert( !fts3SegReaderIsPending(pReader) );
  1369  
  1370    rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
  1371    if( rc!=SQLITE_OK ) return rc;
  1372    
  1373    /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
  1374    ** safe (no risk of overread) even if the node data is corrupted. */
  1375    pNext += fts3GetVarint32(pNext, &nPrefix);
  1376    pNext += fts3GetVarint32(pNext, &nSuffix);
  1377    if( nPrefix<0 || nSuffix<=0 
  1378     || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
  1379    ){
  1380      return FTS_CORRUPT_VTAB;
  1381    }
  1382  
  1383    if( nPrefix+nSuffix>pReader->nTermAlloc ){
  1384      int nNew = (nPrefix+nSuffix)*2;
  1385      char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
  1386      if( !zNew ){
  1387        return SQLITE_NOMEM;
  1388      }
  1389      pReader->zTerm = zNew;
  1390      pReader->nTermAlloc = nNew;
  1391    }
  1392  
  1393    rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
  1394    if( rc!=SQLITE_OK ) return rc;
  1395  
  1396    memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
  1397    pReader->nTerm = nPrefix+nSuffix;
  1398    pNext += nSuffix;
  1399    pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
  1400    pReader->aDoclist = pNext;
  1401    pReader->pOffsetList = 0;
  1402  
  1403    /* Check that the doclist does not appear to extend past the end of the
  1404    ** b-tree node. And that the final byte of the doclist is 0x00. If either 
  1405    ** of these statements is untrue, then the data structure is corrupt.
  1406    */
  1407    if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
  1408     || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
  1409    ){
  1410      return FTS_CORRUPT_VTAB;
  1411    }
  1412    return SQLITE_OK;
  1413  }
  1414  
  1415  /*
  1416  ** Set the SegReader to point to the first docid in the doclist associated
  1417  ** with the current term.
  1418  */
  1419  static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
  1420    int rc = SQLITE_OK;
  1421    assert( pReader->aDoclist );
  1422    assert( !pReader->pOffsetList );
  1423    if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
  1424      u8 bEof = 0;
  1425      pReader->iDocid = 0;
  1426      pReader->nOffsetList = 0;
  1427      sqlite3Fts3DoclistPrev(0,
  1428          pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList, 
  1429          &pReader->iDocid, &pReader->nOffsetList, &bEof
  1430      );
  1431    }else{
  1432      rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
  1433      if( rc==SQLITE_OK ){
  1434        int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
  1435        pReader->pOffsetList = &pReader->aDoclist[n];
  1436      }
  1437    }
  1438    return rc;
  1439  }
  1440  
  1441  /*
  1442  ** Advance the SegReader to point to the next docid in the doclist
  1443  ** associated with the current term.
  1444  ** 
  1445  ** If arguments ppOffsetList and pnOffsetList are not NULL, then 
  1446  ** *ppOffsetList is set to point to the first column-offset list
  1447  ** in the doclist entry (i.e. immediately past the docid varint).
  1448  ** *pnOffsetList is set to the length of the set of column-offset
  1449  ** lists, not including the nul-terminator byte. For example:
  1450  */
  1451  static int fts3SegReaderNextDocid(
  1452    Fts3Table *pTab,
  1453    Fts3SegReader *pReader,         /* Reader to advance to next docid */
  1454    char **ppOffsetList,            /* OUT: Pointer to current position-list */
  1455    int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
  1456  ){
  1457    int rc = SQLITE_OK;
  1458    char *p = pReader->pOffsetList;
  1459    char c = 0;
  1460  
  1461    assert( p );
  1462  
  1463    if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
  1464      /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
  1465      ** Pending-terms doclists are always built up in ascending order, so
  1466      ** we have to iterate through them backwards here. */
  1467      u8 bEof = 0;
  1468      if( ppOffsetList ){
  1469        *ppOffsetList = pReader->pOffsetList;
  1470        *pnOffsetList = pReader->nOffsetList - 1;
  1471      }
  1472      sqlite3Fts3DoclistPrev(0,
  1473          pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
  1474          &pReader->nOffsetList, &bEof
  1475      );
  1476      if( bEof ){
  1477        pReader->pOffsetList = 0;
  1478      }else{
  1479        pReader->pOffsetList = p;
  1480      }
  1481    }else{
  1482      char *pEnd = &pReader->aDoclist[pReader->nDoclist];
  1483  
  1484      /* Pointer p currently points at the first byte of an offset list. The
  1485      ** following block advances it to point one byte past the end of
  1486      ** the same offset list. */
  1487      while( 1 ){
  1488    
  1489        /* The following line of code (and the "p++" below the while() loop) is
  1490        ** normally all that is required to move pointer p to the desired 
  1491        ** position. The exception is if this node is being loaded from disk
  1492        ** incrementally and pointer "p" now points to the first byte past
  1493        ** the populated part of pReader->aNode[].
  1494        */
  1495        while( *p | c ) c = *p++ & 0x80;
  1496        assert( *p==0 );
  1497    
  1498        if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
  1499        rc = fts3SegReaderIncrRead(pReader);
  1500        if( rc!=SQLITE_OK ) return rc;
  1501      }
  1502      p++;
  1503    
  1504      /* If required, populate the output variables with a pointer to and the
  1505      ** size of the previous offset-list.
  1506      */
  1507      if( ppOffsetList ){
  1508        *ppOffsetList = pReader->pOffsetList;
  1509        *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
  1510      }
  1511  
  1512      /* List may have been edited in place by fts3EvalNearTrim() */
  1513      while( p<pEnd && *p==0 ) p++;
  1514    
  1515      /* If there are no more entries in the doclist, set pOffsetList to
  1516      ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
  1517      ** Fts3SegReader.pOffsetList to point to the next offset list before
  1518      ** returning.
  1519      */
  1520      if( p>=pEnd ){
  1521        pReader->pOffsetList = 0;
  1522      }else{
  1523        rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
  1524        if( rc==SQLITE_OK ){
  1525          sqlite3_int64 iDelta;
  1526          pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
  1527          if( pTab->bDescIdx ){
  1528            pReader->iDocid -= iDelta;
  1529          }else{
  1530            pReader->iDocid += iDelta;
  1531          }
  1532        }
  1533      }
  1534    }
  1535  
  1536    return SQLITE_OK;
  1537  }
  1538  
  1539  
  1540  int sqlite3Fts3MsrOvfl(
  1541    Fts3Cursor *pCsr, 
  1542    Fts3MultiSegReader *pMsr,
  1543    int *pnOvfl
  1544  ){
  1545    Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
  1546    int nOvfl = 0;
  1547    int ii;
  1548    int rc = SQLITE_OK;
  1549    int pgsz = p->nPgsz;
  1550  
  1551    assert( p->bFts4 );
  1552    assert( pgsz>0 );
  1553  
  1554    for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
  1555      Fts3SegReader *pReader = pMsr->apSegment[ii];
  1556      if( !fts3SegReaderIsPending(pReader) 
  1557       && !fts3SegReaderIsRootOnly(pReader) 
  1558      ){
  1559        sqlite3_int64 jj;
  1560        for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
  1561          int nBlob;
  1562          rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
  1563          if( rc!=SQLITE_OK ) break;
  1564          if( (nBlob+35)>pgsz ){
  1565            nOvfl += (nBlob + 34)/pgsz;
  1566          }
  1567        }
  1568      }
  1569    }
  1570    *pnOvfl = nOvfl;
  1571    return rc;
  1572  }
  1573  
  1574  /*
  1575  ** Free all allocations associated with the iterator passed as the 
  1576  ** second argument.
  1577  */
  1578  void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
  1579    if( pReader ){
  1580      if( !fts3SegReaderIsPending(pReader) ){
  1581        sqlite3_free(pReader->zTerm);
  1582      }
  1583      if( !fts3SegReaderIsRootOnly(pReader) ){
  1584        sqlite3_free(pReader->aNode);
  1585      }
  1586      sqlite3_blob_close(pReader->pBlob);
  1587    }
  1588    sqlite3_free(pReader);
  1589  }
  1590  
  1591  /*
  1592  ** Allocate a new SegReader object.
  1593  */
  1594  int sqlite3Fts3SegReaderNew(
  1595    int iAge,                       /* Segment "age". */
  1596    int bLookup,                    /* True for a lookup only */
  1597    sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
  1598    sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
  1599    sqlite3_int64 iEndBlock,        /* Final block of segment */
  1600    const char *zRoot,              /* Buffer containing root node */
  1601    int nRoot,                      /* Size of buffer containing root node */
  1602    Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
  1603  ){
  1604    Fts3SegReader *pReader;         /* Newly allocated SegReader object */
  1605    int nExtra = 0;                 /* Bytes to allocate segment root node */
  1606  
  1607    assert( iStartLeaf<=iEndLeaf );
  1608    if( iStartLeaf==0 ){
  1609      nExtra = nRoot + FTS3_NODE_PADDING;
  1610    }
  1611  
  1612    pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
  1613    if( !pReader ){
  1614      return SQLITE_NOMEM;
  1615    }
  1616    memset(pReader, 0, sizeof(Fts3SegReader));
  1617    pReader->iIdx = iAge;
  1618    pReader->bLookup = bLookup!=0;
  1619    pReader->iStartBlock = iStartLeaf;
  1620    pReader->iLeafEndBlock = iEndLeaf;
  1621    pReader->iEndBlock = iEndBlock;
  1622  
  1623    if( nExtra ){
  1624      /* The entire segment is stored in the root node. */
  1625      pReader->aNode = (char *)&pReader[1];
  1626      pReader->rootOnly = 1;
  1627      pReader->nNode = nRoot;
  1628      memcpy(pReader->aNode, zRoot, nRoot);
  1629      memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
  1630    }else{
  1631      pReader->iCurrentBlock = iStartLeaf-1;
  1632    }
  1633    *ppReader = pReader;
  1634    return SQLITE_OK;
  1635  }
  1636  
  1637  /*
  1638  ** This is a comparison function used as a qsort() callback when sorting
  1639  ** an array of pending terms by term. This occurs as part of flushing
  1640  ** the contents of the pending-terms hash table to the database.
  1641  */
  1642  static int SQLITE_CDECL fts3CompareElemByTerm(
  1643    const void *lhs,
  1644    const void *rhs
  1645  ){
  1646    char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
  1647    char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
  1648    int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
  1649    int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
  1650  
  1651    int n = (n1<n2 ? n1 : n2);
  1652    int c = memcmp(z1, z2, n);
  1653    if( c==0 ){
  1654      c = n1 - n2;
  1655    }
  1656    return c;
  1657  }
  1658  
  1659  /*
  1660  ** This function is used to allocate an Fts3SegReader that iterates through
  1661  ** a subset of the terms stored in the Fts3Table.pendingTerms array.
  1662  **
  1663  ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
  1664  ** through each term in the pending-terms table. Or, if isPrefixIter is
  1665  ** non-zero, it iterates through each term and its prefixes. For example, if
  1666  ** the pending terms hash table contains the terms "sqlite", "mysql" and
  1667  ** "firebird", then the iterator visits the following 'terms' (in the order
  1668  ** shown):
  1669  **
  1670  **   f fi fir fire fireb firebi firebir firebird
  1671  **   m my mys mysq mysql
  1672  **   s sq sql sqli sqlit sqlite
  1673  **
  1674  ** Whereas if isPrefixIter is zero, the terms visited are:
  1675  **
  1676  **   firebird mysql sqlite
  1677  */
  1678  int sqlite3Fts3SegReaderPending(
  1679    Fts3Table *p,                   /* Virtual table handle */
  1680    int iIndex,                     /* Index for p->aIndex */
  1681    const char *zTerm,              /* Term to search for */
  1682    int nTerm,                      /* Size of buffer zTerm */
  1683    int bPrefix,                    /* True for a prefix iterator */
  1684    Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
  1685  ){
  1686    Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
  1687    Fts3HashElem *pE;               /* Iterator variable */
  1688    Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
  1689    int nElem = 0;                  /* Size of array at aElem */
  1690    int rc = SQLITE_OK;             /* Return Code */
  1691    Fts3Hash *pHash;
  1692  
  1693    pHash = &p->aIndex[iIndex].hPending;
  1694    if( bPrefix ){
  1695      int nAlloc = 0;               /* Size of allocated array at aElem */
  1696  
  1697      for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
  1698        char *zKey = (char *)fts3HashKey(pE);
  1699        int nKey = fts3HashKeysize(pE);
  1700        if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
  1701          if( nElem==nAlloc ){
  1702            Fts3HashElem **aElem2;
  1703            nAlloc += 16;
  1704            aElem2 = (Fts3HashElem **)sqlite3_realloc(
  1705                aElem, nAlloc*sizeof(Fts3HashElem *)
  1706            );
  1707            if( !aElem2 ){
  1708              rc = SQLITE_NOMEM;
  1709              nElem = 0;
  1710              break;
  1711            }
  1712            aElem = aElem2;
  1713          }
  1714  
  1715          aElem[nElem++] = pE;
  1716        }
  1717      }
  1718  
  1719      /* If more than one term matches the prefix, sort the Fts3HashElem
  1720      ** objects in term order using qsort(). This uses the same comparison
  1721      ** callback as is used when flushing terms to disk.
  1722      */
  1723      if( nElem>1 ){
  1724        qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
  1725      }
  1726  
  1727    }else{
  1728      /* The query is a simple term lookup that matches at most one term in
  1729      ** the index. All that is required is a straight hash-lookup. 
  1730      **
  1731      ** Because the stack address of pE may be accessed via the aElem pointer
  1732      ** below, the "Fts3HashElem *pE" must be declared so that it is valid
  1733      ** within this entire function, not just this "else{...}" block.
  1734      */
  1735      pE = fts3HashFindElem(pHash, zTerm, nTerm);
  1736      if( pE ){
  1737        aElem = &pE;
  1738        nElem = 1;
  1739      }
  1740    }
  1741  
  1742    if( nElem>0 ){
  1743      int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
  1744      pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
  1745      if( !pReader ){
  1746        rc = SQLITE_NOMEM;
  1747      }else{
  1748        memset(pReader, 0, nByte);
  1749        pReader->iIdx = 0x7FFFFFFF;
  1750        pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
  1751        memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
  1752      }
  1753    }
  1754  
  1755    if( bPrefix ){
  1756      sqlite3_free(aElem);
  1757    }
  1758    *ppReader = pReader;
  1759    return rc;
  1760  }
  1761  
  1762  /*
  1763  ** Compare the entries pointed to by two Fts3SegReader structures. 
  1764  ** Comparison is as follows:
  1765  **
  1766  **   1) EOF is greater than not EOF.
  1767  **
  1768  **   2) The current terms (if any) are compared using memcmp(). If one
  1769  **      term is a prefix of another, the longer term is considered the
  1770  **      larger.
  1771  **
  1772  **   3) By segment age. An older segment is considered larger.
  1773  */
  1774  static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
  1775    int rc;
  1776    if( pLhs->aNode && pRhs->aNode ){
  1777      int rc2 = pLhs->nTerm - pRhs->nTerm;
  1778      if( rc2<0 ){
  1779        rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
  1780      }else{
  1781        rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
  1782      }
  1783      if( rc==0 ){
  1784        rc = rc2;
  1785      }
  1786    }else{
  1787      rc = (pLhs->aNode==0) - (pRhs->aNode==0);
  1788    }
  1789    if( rc==0 ){
  1790      rc = pRhs->iIdx - pLhs->iIdx;
  1791    }
  1792    assert( rc!=0 );
  1793    return rc;
  1794  }
  1795  
  1796  /*
  1797  ** A different comparison function for SegReader structures. In this
  1798  ** version, it is assumed that each SegReader points to an entry in
  1799  ** a doclist for identical terms. Comparison is made as follows:
  1800  **
  1801  **   1) EOF (end of doclist in this case) is greater than not EOF.
  1802  **
  1803  **   2) By current docid.
  1804  **
  1805  **   3) By segment age. An older segment is considered larger.
  1806  */
  1807  static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
  1808    int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
  1809    if( rc==0 ){
  1810      if( pLhs->iDocid==pRhs->iDocid ){
  1811        rc = pRhs->iIdx - pLhs->iIdx;
  1812      }else{
  1813        rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
  1814      }
  1815    }
  1816    assert( pLhs->aNode && pRhs->aNode );
  1817    return rc;
  1818  }
  1819  static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
  1820    int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
  1821    if( rc==0 ){
  1822      if( pLhs->iDocid==pRhs->iDocid ){
  1823        rc = pRhs->iIdx - pLhs->iIdx;
  1824      }else{
  1825        rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
  1826      }
  1827    }
  1828    assert( pLhs->aNode && pRhs->aNode );
  1829    return rc;
  1830  }
  1831  
  1832  /*
  1833  ** Compare the term that the Fts3SegReader object passed as the first argument
  1834  ** points to with the term specified by arguments zTerm and nTerm. 
  1835  **
  1836  ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
  1837  ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
  1838  ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
  1839  */
  1840  static int fts3SegReaderTermCmp(
  1841    Fts3SegReader *pSeg,            /* Segment reader object */
  1842    const char *zTerm,              /* Term to compare to */
  1843    int nTerm                       /* Size of term zTerm in bytes */
  1844  ){
  1845    int res = 0;
  1846    if( pSeg->aNode ){
  1847      if( pSeg->nTerm>nTerm ){
  1848        res = memcmp(pSeg->zTerm, zTerm, nTerm);
  1849      }else{
  1850        res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
  1851      }
  1852      if( res==0 ){
  1853        res = pSeg->nTerm-nTerm;
  1854      }
  1855    }
  1856    return res;
  1857  }
  1858  
  1859  /*
  1860  ** Argument apSegment is an array of nSegment elements. It is known that
  1861  ** the final (nSegment-nSuspect) members are already in sorted order
  1862  ** (according to the comparison function provided). This function shuffles
  1863  ** the array around until all entries are in sorted order.
  1864  */
  1865  static void fts3SegReaderSort(
  1866    Fts3SegReader **apSegment,                     /* Array to sort entries of */
  1867    int nSegment,                                  /* Size of apSegment array */
  1868    int nSuspect,                                  /* Unsorted entry count */
  1869    int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
  1870  ){
  1871    int i;                          /* Iterator variable */
  1872  
  1873    assert( nSuspect<=nSegment );
  1874  
  1875    if( nSuspect==nSegment ) nSuspect--;
  1876    for(i=nSuspect-1; i>=0; i--){
  1877      int j;
  1878      for(j=i; j<(nSegment-1); j++){
  1879        Fts3SegReader *pTmp;
  1880        if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
  1881        pTmp = apSegment[j+1];
  1882        apSegment[j+1] = apSegment[j];
  1883        apSegment[j] = pTmp;
  1884      }
  1885    }
  1886  
  1887  #ifndef NDEBUG
  1888    /* Check that the list really is sorted now. */
  1889    for(i=0; i<(nSuspect-1); i++){
  1890      assert( xCmp(apSegment[i], apSegment[i+1])<0 );
  1891    }
  1892  #endif
  1893  }
  1894  
  1895  /* 
  1896  ** Insert a record into the %_segments table.
  1897  */
  1898  static int fts3WriteSegment(
  1899    Fts3Table *p,                   /* Virtual table handle */
  1900    sqlite3_int64 iBlock,           /* Block id for new block */
  1901    char *z,                        /* Pointer to buffer containing block data */
  1902    int n                           /* Size of buffer z in bytes */
  1903  ){
  1904    sqlite3_stmt *pStmt;
  1905    int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
  1906    if( rc==SQLITE_OK ){
  1907      sqlite3_bind_int64(pStmt, 1, iBlock);
  1908      sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
  1909      sqlite3_step(pStmt);
  1910      rc = sqlite3_reset(pStmt);
  1911    }
  1912    return rc;
  1913  }
  1914  
  1915  /*
  1916  ** Find the largest relative level number in the table. If successful, set
  1917  ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
  1918  ** set *pnMax to zero and return an SQLite error code.
  1919  */
  1920  int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
  1921    int rc;
  1922    int mxLevel = 0;
  1923    sqlite3_stmt *pStmt = 0;
  1924  
  1925    rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
  1926    if( rc==SQLITE_OK ){
  1927      if( SQLITE_ROW==sqlite3_step(pStmt) ){
  1928        mxLevel = sqlite3_column_int(pStmt, 0);
  1929      }
  1930      rc = sqlite3_reset(pStmt);
  1931    }
  1932    *pnMax = mxLevel;
  1933    return rc;
  1934  }
  1935  
  1936  /* 
  1937  ** Insert a record into the %_segdir table.
  1938  */
  1939  static int fts3WriteSegdir(
  1940    Fts3Table *p,                   /* Virtual table handle */
  1941    sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
  1942    int iIdx,                       /* Value for "idx" field */
  1943    sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
  1944    sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
  1945    sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
  1946    sqlite3_int64 nLeafData,        /* Bytes of leaf data in segment */
  1947    char *zRoot,                    /* Blob value for "root" field */
  1948    int nRoot                       /* Number of bytes in buffer zRoot */
  1949  ){
  1950    sqlite3_stmt *pStmt;
  1951    int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
  1952    if( rc==SQLITE_OK ){
  1953      sqlite3_bind_int64(pStmt, 1, iLevel);
  1954      sqlite3_bind_int(pStmt, 2, iIdx);
  1955      sqlite3_bind_int64(pStmt, 3, iStartBlock);
  1956      sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
  1957      if( nLeafData==0 ){
  1958        sqlite3_bind_int64(pStmt, 5, iEndBlock);
  1959      }else{
  1960        char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
  1961        if( !zEnd ) return SQLITE_NOMEM;
  1962        sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
  1963      }
  1964      sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
  1965      sqlite3_step(pStmt);
  1966      rc = sqlite3_reset(pStmt);
  1967    }
  1968    return rc;
  1969  }
  1970  
  1971  /*
  1972  ** Return the size of the common prefix (if any) shared by zPrev and
  1973  ** zNext, in bytes. For example, 
  1974  **
  1975  **   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
  1976  **   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
  1977  **   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
  1978  */
  1979  static int fts3PrefixCompress(
  1980    const char *zPrev,              /* Buffer containing previous term */
  1981    int nPrev,                      /* Size of buffer zPrev in bytes */
  1982    const char *zNext,              /* Buffer containing next term */
  1983    int nNext                       /* Size of buffer zNext in bytes */
  1984  ){
  1985    int n;
  1986    UNUSED_PARAMETER(nNext);
  1987    for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
  1988    return n;
  1989  }
  1990  
  1991  /*
  1992  ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
  1993  ** (according to memcmp) than the previous term.
  1994  */
  1995  static int fts3NodeAddTerm(
  1996    Fts3Table *p,                   /* Virtual table handle */
  1997    SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */ 
  1998    int isCopyTerm,                 /* True if zTerm/nTerm is transient */
  1999    const char *zTerm,              /* Pointer to buffer containing term */
  2000    int nTerm                       /* Size of term in bytes */
  2001  ){
  2002    SegmentNode *pTree = *ppTree;
  2003    int rc;
  2004    SegmentNode *pNew;
  2005  
  2006    /* First try to append the term to the current node. Return early if 
  2007    ** this is possible.
  2008    */
  2009    if( pTree ){
  2010      int nData = pTree->nData;     /* Current size of node in bytes */
  2011      int nReq = nData;             /* Required space after adding zTerm */
  2012      int nPrefix;                  /* Number of bytes of prefix compression */
  2013      int nSuffix;                  /* Suffix length */
  2014  
  2015      nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
  2016      nSuffix = nTerm-nPrefix;
  2017  
  2018      nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
  2019      if( nReq<=p->nNodeSize || !pTree->zTerm ){
  2020  
  2021        if( nReq>p->nNodeSize ){
  2022          /* An unusual case: this is the first term to be added to the node
  2023          ** and the static node buffer (p->nNodeSize bytes) is not large
  2024          ** enough. Use a separately malloced buffer instead This wastes
  2025          ** p->nNodeSize bytes, but since this scenario only comes about when
  2026          ** the database contain two terms that share a prefix of almost 2KB, 
  2027          ** this is not expected to be a serious problem. 
  2028          */
  2029          assert( pTree->aData==(char *)&pTree[1] );
  2030          pTree->aData = (char *)sqlite3_malloc(nReq);
  2031          if( !pTree->aData ){
  2032            return SQLITE_NOMEM;
  2033          }
  2034        }
  2035  
  2036        if( pTree->zTerm ){
  2037          /* There is no prefix-length field for first term in a node */
  2038          nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
  2039        }
  2040  
  2041        nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
  2042        memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
  2043        pTree->nData = nData + nSuffix;
  2044        pTree->nEntry++;
  2045  
  2046        if( isCopyTerm ){
  2047          if( pTree->nMalloc<nTerm ){
  2048            char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
  2049            if( !zNew ){
  2050              return SQLITE_NOMEM;
  2051            }
  2052            pTree->nMalloc = nTerm*2;
  2053            pTree->zMalloc = zNew;
  2054          }
  2055          pTree->zTerm = pTree->zMalloc;
  2056          memcpy(pTree->zTerm, zTerm, nTerm);
  2057          pTree->nTerm = nTerm;
  2058        }else{
  2059          pTree->zTerm = (char *)zTerm;
  2060          pTree->nTerm = nTerm;
  2061        }
  2062        return SQLITE_OK;
  2063      }
  2064    }
  2065  
  2066    /* If control flows to here, it was not possible to append zTerm to the
  2067    ** current node. Create a new node (a right-sibling of the current node).
  2068    ** If this is the first node in the tree, the term is added to it.
  2069    **
  2070    ** Otherwise, the term is not added to the new node, it is left empty for
  2071    ** now. Instead, the term is inserted into the parent of pTree. If pTree 
  2072    ** has no parent, one is created here.
  2073    */
  2074    pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
  2075    if( !pNew ){
  2076      return SQLITE_NOMEM;
  2077    }
  2078    memset(pNew, 0, sizeof(SegmentNode));
  2079    pNew->nData = 1 + FTS3_VARINT_MAX;
  2080    pNew->aData = (char *)&pNew[1];
  2081  
  2082    if( pTree ){
  2083      SegmentNode *pParent = pTree->pParent;
  2084      rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
  2085      if( pTree->pParent==0 ){
  2086        pTree->pParent = pParent;
  2087      }
  2088      pTree->pRight = pNew;
  2089      pNew->pLeftmost = pTree->pLeftmost;
  2090      pNew->pParent = pParent;
  2091      pNew->zMalloc = pTree->zMalloc;
  2092      pNew->nMalloc = pTree->nMalloc;
  2093      pTree->zMalloc = 0;
  2094    }else{
  2095      pNew->pLeftmost = pNew;
  2096      rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); 
  2097    }
  2098  
  2099    *ppTree = pNew;
  2100    return rc;
  2101  }
  2102  
  2103  /*
  2104  ** Helper function for fts3NodeWrite().
  2105  */
  2106  static int fts3TreeFinishNode(
  2107    SegmentNode *pTree, 
  2108    int iHeight, 
  2109    sqlite3_int64 iLeftChild
  2110  ){
  2111    int nStart;
  2112    assert( iHeight>=1 && iHeight<128 );
  2113    nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
  2114    pTree->aData[nStart] = (char)iHeight;
  2115    sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
  2116    return nStart;
  2117  }
  2118  
  2119  /*
  2120  ** Write the buffer for the segment node pTree and all of its peers to the
  2121  ** database. Then call this function recursively to write the parent of 
  2122  ** pTree and its peers to the database. 
  2123  **
  2124  ** Except, if pTree is a root node, do not write it to the database. Instead,
  2125  ** set output variables *paRoot and *pnRoot to contain the root node.
  2126  **
  2127  ** If successful, SQLITE_OK is returned and output variable *piLast is
  2128  ** set to the largest blockid written to the database (or zero if no
  2129  ** blocks were written to the db). Otherwise, an SQLite error code is 
  2130  ** returned.
  2131  */
  2132  static int fts3NodeWrite(
  2133    Fts3Table *p,                   /* Virtual table handle */
  2134    SegmentNode *pTree,             /* SegmentNode handle */
  2135    int iHeight,                    /* Height of this node in tree */
  2136    sqlite3_int64 iLeaf,            /* Block id of first leaf node */
  2137    sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
  2138    sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
  2139    char **paRoot,                  /* OUT: Data for root node */
  2140    int *pnRoot                     /* OUT: Size of root node in bytes */
  2141  ){
  2142    int rc = SQLITE_OK;
  2143  
  2144    if( !pTree->pParent ){
  2145      /* Root node of the tree. */
  2146      int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
  2147      *piLast = iFree-1;
  2148      *pnRoot = pTree->nData - nStart;
  2149      *paRoot = &pTree->aData[nStart];
  2150    }else{
  2151      SegmentNode *pIter;
  2152      sqlite3_int64 iNextFree = iFree;
  2153      sqlite3_int64 iNextLeaf = iLeaf;
  2154      for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
  2155        int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
  2156        int nWrite = pIter->nData - nStart;
  2157    
  2158        rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
  2159        iNextFree++;
  2160        iNextLeaf += (pIter->nEntry+1);
  2161      }
  2162      if( rc==SQLITE_OK ){
  2163        assert( iNextLeaf==iFree );
  2164        rc = fts3NodeWrite(
  2165            p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
  2166        );
  2167      }
  2168    }
  2169  
  2170    return rc;
  2171  }
  2172  
  2173  /*
  2174  ** Free all memory allocations associated with the tree pTree.
  2175  */
  2176  static void fts3NodeFree(SegmentNode *pTree){
  2177    if( pTree ){
  2178      SegmentNode *p = pTree->pLeftmost;
  2179      fts3NodeFree(p->pParent);
  2180      while( p ){
  2181        SegmentNode *pRight = p->pRight;
  2182        if( p->aData!=(char *)&p[1] ){
  2183          sqlite3_free(p->aData);
  2184        }
  2185        assert( pRight==0 || p->zMalloc==0 );
  2186        sqlite3_free(p->zMalloc);
  2187        sqlite3_free(p);
  2188        p = pRight;
  2189      }
  2190    }
  2191  }
  2192  
  2193  /*
  2194  ** Add a term to the segment being constructed by the SegmentWriter object
  2195  ** *ppWriter. When adding the first term to a segment, *ppWriter should
  2196  ** be passed NULL. This function will allocate a new SegmentWriter object
  2197  ** and return it via the input/output variable *ppWriter in this case.
  2198  **
  2199  ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
  2200  */
  2201  static int fts3SegWriterAdd(
  2202    Fts3Table *p,                   /* Virtual table handle */
  2203    SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */ 
  2204    int isCopyTerm,                 /* True if buffer zTerm must be copied */
  2205    const char *zTerm,              /* Pointer to buffer containing term */
  2206    int nTerm,                      /* Size of term in bytes */
  2207    const char *aDoclist,           /* Pointer to buffer containing doclist */
  2208    int nDoclist                    /* Size of doclist in bytes */
  2209  ){
  2210    int nPrefix;                    /* Size of term prefix in bytes */
  2211    int nSuffix;                    /* Size of term suffix in bytes */
  2212    int nReq;                       /* Number of bytes required on leaf page */
  2213    int nData;
  2214    SegmentWriter *pWriter = *ppWriter;
  2215  
  2216    if( !pWriter ){
  2217      int rc;
  2218      sqlite3_stmt *pStmt;
  2219  
  2220      /* Allocate the SegmentWriter structure */
  2221      pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
  2222      if( !pWriter ) return SQLITE_NOMEM;
  2223      memset(pWriter, 0, sizeof(SegmentWriter));
  2224      *ppWriter = pWriter;
  2225  
  2226      /* Allocate a buffer in which to accumulate data */
  2227      pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
  2228      if( !pWriter->aData ) return SQLITE_NOMEM;
  2229      pWriter->nSize = p->nNodeSize;
  2230  
  2231      /* Find the next free blockid in the %_segments table */
  2232      rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
  2233      if( rc!=SQLITE_OK ) return rc;
  2234      if( SQLITE_ROW==sqlite3_step(pStmt) ){
  2235        pWriter->iFree = sqlite3_column_int64(pStmt, 0);
  2236        pWriter->iFirst = pWriter->iFree;
  2237      }
  2238      rc = sqlite3_reset(pStmt);
  2239      if( rc!=SQLITE_OK ) return rc;
  2240    }
  2241    nData = pWriter->nData;
  2242  
  2243    nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
  2244    nSuffix = nTerm-nPrefix;
  2245  
  2246    /* Figure out how many bytes are required by this new entry */
  2247    nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
  2248      sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
  2249      nSuffix +                               /* Term suffix */
  2250      sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
  2251      nDoclist;                               /* Doclist data */
  2252  
  2253    if( nData>0 && nData+nReq>p->nNodeSize ){
  2254      int rc;
  2255  
  2256      /* The current leaf node is full. Write it out to the database. */
  2257      rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
  2258      if( rc!=SQLITE_OK ) return rc;
  2259      p->nLeafAdd++;
  2260  
  2261      /* Add the current term to the interior node tree. The term added to
  2262      ** the interior tree must:
  2263      **
  2264      **   a) be greater than the largest term on the leaf node just written
  2265      **      to the database (still available in pWriter->zTerm), and
  2266      **
  2267      **   b) be less than or equal to the term about to be added to the new
  2268      **      leaf node (zTerm/nTerm).
  2269      **
  2270      ** In other words, it must be the prefix of zTerm 1 byte longer than
  2271      ** the common prefix (if any) of zTerm and pWriter->zTerm.
  2272      */
  2273      assert( nPrefix<nTerm );
  2274      rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
  2275      if( rc!=SQLITE_OK ) return rc;
  2276  
  2277      nData = 0;
  2278      pWriter->nTerm = 0;
  2279  
  2280      nPrefix = 0;
  2281      nSuffix = nTerm;
  2282      nReq = 1 +                              /* varint containing prefix size */
  2283        sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
  2284        nTerm +                               /* Term suffix */
  2285        sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
  2286        nDoclist;                             /* Doclist data */
  2287    }
  2288  
  2289    /* Increase the total number of bytes written to account for the new entry. */
  2290    pWriter->nLeafData += nReq;
  2291  
  2292    /* If the buffer currently allocated is too small for this entry, realloc
  2293    ** the buffer to make it large enough.
  2294    */
  2295    if( nReq>pWriter->nSize ){
  2296      char *aNew = sqlite3_realloc(pWriter->aData, nReq);
  2297      if( !aNew ) return SQLITE_NOMEM;
  2298      pWriter->aData = aNew;
  2299      pWriter->nSize = nReq;
  2300    }
  2301    assert( nData+nReq<=pWriter->nSize );
  2302  
  2303    /* Append the prefix-compressed term and doclist to the buffer. */
  2304    nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
  2305    nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
  2306    memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
  2307    nData += nSuffix;
  2308    nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
  2309    memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
  2310    pWriter->nData = nData + nDoclist;
  2311  
  2312    /* Save the current term so that it can be used to prefix-compress the next.
  2313    ** If the isCopyTerm parameter is true, then the buffer pointed to by
  2314    ** zTerm is transient, so take a copy of the term data. Otherwise, just
  2315    ** store a copy of the pointer.
  2316    */
  2317    if( isCopyTerm ){
  2318      if( nTerm>pWriter->nMalloc ){
  2319        char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
  2320        if( !zNew ){
  2321          return SQLITE_NOMEM;
  2322        }
  2323        pWriter->nMalloc = nTerm*2;
  2324        pWriter->zMalloc = zNew;
  2325        pWriter->zTerm = zNew;
  2326      }
  2327      assert( pWriter->zTerm==pWriter->zMalloc );
  2328      memcpy(pWriter->zTerm, zTerm, nTerm);
  2329    }else{
  2330      pWriter->zTerm = (char *)zTerm;
  2331    }
  2332    pWriter->nTerm = nTerm;
  2333  
  2334    return SQLITE_OK;
  2335  }
  2336  
  2337  /*
  2338  ** Flush all data associated with the SegmentWriter object pWriter to the
  2339  ** database. This function must be called after all terms have been added
  2340  ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
  2341  ** returned. Otherwise, an SQLite error code.
  2342  */
  2343  static int fts3SegWriterFlush(
  2344    Fts3Table *p,                   /* Virtual table handle */
  2345    SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
  2346    sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
  2347    int iIdx                        /* Value for 'idx' column of %_segdir */
  2348  ){
  2349    int rc;                         /* Return code */
  2350    if( pWriter->pTree ){
  2351      sqlite3_int64 iLast = 0;      /* Largest block id written to database */
  2352      sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
  2353      char *zRoot = NULL;           /* Pointer to buffer containing root node */
  2354      int nRoot = 0;                /* Size of buffer zRoot */
  2355  
  2356      iLastLeaf = pWriter->iFree;
  2357      rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
  2358      if( rc==SQLITE_OK ){
  2359        rc = fts3NodeWrite(p, pWriter->pTree, 1,
  2360            pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
  2361      }
  2362      if( rc==SQLITE_OK ){
  2363        rc = fts3WriteSegdir(p, iLevel, iIdx, 
  2364            pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
  2365      }
  2366    }else{
  2367      /* The entire tree fits on the root node. Write it to the segdir table. */
  2368      rc = fts3WriteSegdir(p, iLevel, iIdx, 
  2369          0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
  2370    }
  2371    p->nLeafAdd++;
  2372    return rc;
  2373  }
  2374  
  2375  /*
  2376  ** Release all memory held by the SegmentWriter object passed as the 
  2377  ** first argument.
  2378  */
  2379  static void fts3SegWriterFree(SegmentWriter *pWriter){
  2380    if( pWriter ){
  2381      sqlite3_free(pWriter->aData);
  2382      sqlite3_free(pWriter->zMalloc);
  2383      fts3NodeFree(pWriter->pTree);
  2384      sqlite3_free(pWriter);
  2385    }
  2386  }
  2387  
  2388  /*
  2389  ** The first value in the apVal[] array is assumed to contain an integer.
  2390  ** This function tests if there exist any documents with docid values that
  2391  ** are different from that integer. i.e. if deleting the document with docid
  2392  ** pRowid would mean the FTS3 table were empty.
  2393  **
  2394  ** If successful, *pisEmpty is set to true if the table is empty except for
  2395  ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
  2396  ** error occurs, an SQLite error code is returned.
  2397  */
  2398  static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
  2399    sqlite3_stmt *pStmt;
  2400    int rc;
  2401    if( p->zContentTbl ){
  2402      /* If using the content=xxx option, assume the table is never empty */
  2403      *pisEmpty = 0;
  2404      rc = SQLITE_OK;
  2405    }else{
  2406      rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
  2407      if( rc==SQLITE_OK ){
  2408        if( SQLITE_ROW==sqlite3_step(pStmt) ){
  2409          *pisEmpty = sqlite3_column_int(pStmt, 0);
  2410        }
  2411        rc = sqlite3_reset(pStmt);
  2412      }
  2413    }
  2414    return rc;
  2415  }
  2416  
  2417  /*
  2418  ** Set *pnMax to the largest segment level in the database for the index
  2419  ** iIndex.
  2420  **
  2421  ** Segment levels are stored in the 'level' column of the %_segdir table.
  2422  **
  2423  ** Return SQLITE_OK if successful, or an SQLite error code if not.
  2424  */
  2425  static int fts3SegmentMaxLevel(
  2426    Fts3Table *p, 
  2427    int iLangid,
  2428    int iIndex, 
  2429    sqlite3_int64 *pnMax
  2430  ){
  2431    sqlite3_stmt *pStmt;
  2432    int rc;
  2433    assert( iIndex>=0 && iIndex<p->nIndex );
  2434  
  2435    /* Set pStmt to the compiled version of:
  2436    **
  2437    **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
  2438    **
  2439    ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
  2440    */
  2441    rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
  2442    if( rc!=SQLITE_OK ) return rc;
  2443    sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
  2444    sqlite3_bind_int64(pStmt, 2, 
  2445        getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
  2446    );
  2447    if( SQLITE_ROW==sqlite3_step(pStmt) ){
  2448      *pnMax = sqlite3_column_int64(pStmt, 0);
  2449    }
  2450    return sqlite3_reset(pStmt);
  2451  }
  2452  
  2453  /*
  2454  ** iAbsLevel is an absolute level that may be assumed to exist within
  2455  ** the database. This function checks if it is the largest level number
  2456  ** within its index. Assuming no error occurs, *pbMax is set to 1 if
  2457  ** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
  2458  ** is returned. If an error occurs, an error code is returned and the
  2459  ** final value of *pbMax is undefined.
  2460  */
  2461  static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
  2462  
  2463    /* Set pStmt to the compiled version of:
  2464    **
  2465    **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
  2466    **
  2467    ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
  2468    */
  2469    sqlite3_stmt *pStmt;
  2470    int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
  2471    if( rc!=SQLITE_OK ) return rc;
  2472    sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
  2473    sqlite3_bind_int64(pStmt, 2, 
  2474        ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
  2475    );
  2476  
  2477    *pbMax = 0;
  2478    if( SQLITE_ROW==sqlite3_step(pStmt) ){
  2479      *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
  2480    }
  2481    return sqlite3_reset(pStmt);
  2482  }
  2483  
  2484  /*
  2485  ** Delete all entries in the %_segments table associated with the segment
  2486  ** opened with seg-reader pSeg. This function does not affect the contents
  2487  ** of the %_segdir table.
  2488  */
  2489  static int fts3DeleteSegment(
  2490    Fts3Table *p,                   /* FTS table handle */
  2491    Fts3SegReader *pSeg             /* Segment to delete */
  2492  ){
  2493    int rc = SQLITE_OK;             /* Return code */
  2494    if( pSeg->iStartBlock ){
  2495      sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
  2496      rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
  2497      if( rc==SQLITE_OK ){
  2498        sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
  2499        sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
  2500        sqlite3_step(pDelete);
  2501        rc = sqlite3_reset(pDelete);
  2502      }
  2503    }
  2504    return rc;
  2505  }
  2506  
  2507  /*
  2508  ** This function is used after merging multiple segments into a single large
  2509  ** segment to delete the old, now redundant, segment b-trees. Specifically,
  2510  ** it:
  2511  ** 
  2512  **   1) Deletes all %_segments entries for the segments associated with 
  2513  **      each of the SegReader objects in the array passed as the third 
  2514  **      argument, and
  2515  **
  2516  **   2) deletes all %_segdir entries with level iLevel, or all %_segdir
  2517  **      entries regardless of level if (iLevel<0).
  2518  **
  2519  ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
  2520  */
  2521  static int fts3DeleteSegdir(
  2522    Fts3Table *p,                   /* Virtual table handle */
  2523    int iLangid,                    /* Language id */
  2524    int iIndex,                     /* Index for p->aIndex */
  2525    int iLevel,                     /* Level of %_segdir entries to delete */
  2526    Fts3SegReader **apSegment,      /* Array of SegReader objects */
  2527    int nReader                     /* Size of array apSegment */
  2528  ){
  2529    int rc = SQLITE_OK;             /* Return Code */
  2530    int i;                          /* Iterator variable */
  2531    sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
  2532  
  2533    for(i=0; rc==SQLITE_OK && i<nReader; i++){
  2534      rc = fts3DeleteSegment(p, apSegment[i]);
  2535    }
  2536    if( rc!=SQLITE_OK ){
  2537      return rc;
  2538    }
  2539  
  2540    assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
  2541    if( iLevel==FTS3_SEGCURSOR_ALL ){
  2542      rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
  2543      if( rc==SQLITE_OK ){
  2544        sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
  2545        sqlite3_bind_int64(pDelete, 2, 
  2546            getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
  2547        );
  2548      }
  2549    }else{
  2550      rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
  2551      if( rc==SQLITE_OK ){
  2552        sqlite3_bind_int64(
  2553            pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
  2554        );
  2555      }
  2556    }
  2557  
  2558    if( rc==SQLITE_OK ){
  2559      sqlite3_step(pDelete);
  2560      rc = sqlite3_reset(pDelete);
  2561    }
  2562  
  2563    return rc;
  2564  }
  2565  
  2566  /*
  2567  ** When this function is called, buffer *ppList (size *pnList bytes) contains 
  2568  ** a position list that may (or may not) feature multiple columns. This
  2569  ** function adjusts the pointer *ppList and the length *pnList so that they
  2570  ** identify the subset of the position list that corresponds to column iCol.
  2571  **
  2572  ** If there are no entries in the input position list for column iCol, then
  2573  ** *pnList is set to zero before returning.
  2574  **
  2575  ** If parameter bZero is non-zero, then any part of the input list following
  2576  ** the end of the output list is zeroed before returning.
  2577  */
  2578  static void fts3ColumnFilter(
  2579    int iCol,                       /* Column to filter on */
  2580    int bZero,                      /* Zero out anything following *ppList */
  2581    char **ppList,                  /* IN/OUT: Pointer to position list */
  2582    int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
  2583  ){
  2584    char *pList = *ppList;
  2585    int nList = *pnList;
  2586    char *pEnd = &pList[nList];
  2587    int iCurrent = 0;
  2588    char *p = pList;
  2589  
  2590    assert( iCol>=0 );
  2591    while( 1 ){
  2592      char c = 0;
  2593      while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
  2594    
  2595      if( iCol==iCurrent ){
  2596        nList = (int)(p - pList);
  2597        break;
  2598      }
  2599  
  2600      nList -= (int)(p - pList);
  2601      pList = p;
  2602      if( nList==0 ){
  2603        break;
  2604      }
  2605      p = &pList[1];
  2606      p += fts3GetVarint32(p, &iCurrent);
  2607    }
  2608  
  2609    if( bZero && &pList[nList]!=pEnd ){
  2610      memset(&pList[nList], 0, pEnd - &pList[nList]);
  2611    }
  2612    *ppList = pList;
  2613    *pnList = nList;
  2614  }
  2615  
  2616  /*
  2617  ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
  2618  ** existing data). Grow the buffer if required.
  2619  **
  2620  ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
  2621  ** trying to resize the buffer, return SQLITE_NOMEM.
  2622  */
  2623  static int fts3MsrBufferData(
  2624    Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
  2625    char *pList,
  2626    int nList
  2627  ){
  2628    if( nList>pMsr->nBuffer ){
  2629      char *pNew;
  2630      pMsr->nBuffer = nList*2;
  2631      pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
  2632      if( !pNew ) return SQLITE_NOMEM;
  2633      pMsr->aBuffer = pNew;
  2634    }
  2635  
  2636    memcpy(pMsr->aBuffer, pList, nList);
  2637    return SQLITE_OK;
  2638  }
  2639  
  2640  int sqlite3Fts3MsrIncrNext(
  2641    Fts3Table *p,                   /* Virtual table handle */
  2642    Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
  2643    sqlite3_int64 *piDocid,         /* OUT: Docid value */
  2644    char **paPoslist,               /* OUT: Pointer to position list */
  2645    int *pnPoslist                  /* OUT: Size of position list in bytes */
  2646  ){
  2647    int nMerge = pMsr->nAdvance;
  2648    Fts3SegReader **apSegment = pMsr->apSegment;
  2649    int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
  2650      p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
  2651    );
  2652  
  2653    if( nMerge==0 ){
  2654      *paPoslist = 0;
  2655      return SQLITE_OK;
  2656    }
  2657  
  2658    while( 1 ){
  2659      Fts3SegReader *pSeg;
  2660      pSeg = pMsr->apSegment[0];
  2661  
  2662      if( pSeg->pOffsetList==0 ){
  2663        *paPoslist = 0;
  2664        break;
  2665      }else{
  2666        int rc;
  2667        char *pList;
  2668        int nList;
  2669        int j;
  2670        sqlite3_int64 iDocid = apSegment[0]->iDocid;
  2671  
  2672        rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
  2673        j = 1;
  2674        while( rc==SQLITE_OK 
  2675          && j<nMerge
  2676          && apSegment[j]->pOffsetList
  2677          && apSegment[j]->iDocid==iDocid
  2678        ){
  2679          rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
  2680          j++;
  2681        }
  2682        if( rc!=SQLITE_OK ) return rc;
  2683        fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
  2684  
  2685        if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
  2686          rc = fts3MsrBufferData(pMsr, pList, nList+1);
  2687          if( rc!=SQLITE_OK ) return rc;
  2688          assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
  2689          pList = pMsr->aBuffer;
  2690        }
  2691  
  2692        if( pMsr->iColFilter>=0 ){
  2693          fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
  2694        }
  2695  
  2696        if( nList>0 ){
  2697          *paPoslist = pList;
  2698          *piDocid = iDocid;
  2699          *pnPoslist = nList;
  2700          break;
  2701        }
  2702      }
  2703    }
  2704  
  2705    return SQLITE_OK;
  2706  }
  2707  
  2708  static int fts3SegReaderStart(
  2709    Fts3Table *p,                   /* Virtual table handle */
  2710    Fts3MultiSegReader *pCsr,       /* Cursor object */
  2711    const char *zTerm,              /* Term searched for (or NULL) */
  2712    int nTerm                       /* Length of zTerm in bytes */
  2713  ){
  2714    int i;
  2715    int nSeg = pCsr->nSegment;
  2716  
  2717    /* If the Fts3SegFilter defines a specific term (or term prefix) to search 
  2718    ** for, then advance each segment iterator until it points to a term of
  2719    ** equal or greater value than the specified term. This prevents many
  2720    ** unnecessary merge/sort operations for the case where single segment
  2721    ** b-tree leaf nodes contain more than one term.
  2722    */
  2723    for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
  2724      int res = 0;
  2725      Fts3SegReader *pSeg = pCsr->apSegment[i];
  2726      do {
  2727        int rc = fts3SegReaderNext(p, pSeg, 0);
  2728        if( rc!=SQLITE_OK ) return rc;
  2729      }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
  2730  
  2731      if( pSeg->bLookup && res!=0 ){
  2732        fts3SegReaderSetEof(pSeg);
  2733      }
  2734    }
  2735    fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
  2736  
  2737    return SQLITE_OK;
  2738  }
  2739  
  2740  int sqlite3Fts3SegReaderStart(
  2741    Fts3Table *p,                   /* Virtual table handle */
  2742    Fts3MultiSegReader *pCsr,       /* Cursor object */
  2743    Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
  2744  ){
  2745    pCsr->pFilter = pFilter;
  2746    return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
  2747  }
  2748  
  2749  int sqlite3Fts3MsrIncrStart(
  2750    Fts3Table *p,                   /* Virtual table handle */
  2751    Fts3MultiSegReader *pCsr,       /* Cursor object */
  2752    int iCol,                       /* Column to match on. */
  2753    const char *zTerm,              /* Term to iterate through a doclist for */
  2754    int nTerm                       /* Number of bytes in zTerm */
  2755  ){
  2756    int i;
  2757    int rc;
  2758    int nSegment = pCsr->nSegment;
  2759    int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
  2760      p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
  2761    );
  2762  
  2763    assert( pCsr->pFilter==0 );
  2764    assert( zTerm && nTerm>0 );
  2765  
  2766    /* Advance each segment iterator until it points to the term zTerm/nTerm. */
  2767    rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
  2768    if( rc!=SQLITE_OK ) return rc;
  2769  
  2770    /* Determine how many of the segments actually point to zTerm/nTerm. */
  2771    for(i=0; i<nSegment; i++){
  2772      Fts3SegReader *pSeg = pCsr->apSegment[i];
  2773      if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
  2774        break;
  2775      }
  2776    }
  2777    pCsr->nAdvance = i;
  2778  
  2779    /* Advance each of the segments to point to the first docid. */
  2780    for(i=0; i<pCsr->nAdvance; i++){
  2781      rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
  2782      if( rc!=SQLITE_OK ) return rc;
  2783    }
  2784    fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
  2785  
  2786    assert( iCol<0 || iCol<p->nColumn );
  2787    pCsr->iColFilter = iCol;
  2788  
  2789    return SQLITE_OK;
  2790  }
  2791  
  2792  /*
  2793  ** This function is called on a MultiSegReader that has been started using
  2794  ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
  2795  ** have been made. Calling this function puts the MultiSegReader in such
  2796  ** a state that if the next two calls are:
  2797  **
  2798  **   sqlite3Fts3SegReaderStart()
  2799  **   sqlite3Fts3SegReaderStep()
  2800  **
  2801  ** then the entire doclist for the term is available in 
  2802  ** MultiSegReader.aDoclist/nDoclist.
  2803  */
  2804  int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
  2805    int i;                          /* Used to iterate through segment-readers */
  2806  
  2807    assert( pCsr->zTerm==0 );
  2808    assert( pCsr->nTerm==0 );
  2809    assert( pCsr->aDoclist==0 );
  2810    assert( pCsr->nDoclist==0 );
  2811  
  2812    pCsr->nAdvance = 0;
  2813    pCsr->bRestart = 1;
  2814    for(i=0; i<pCsr->nSegment; i++){
  2815      pCsr->apSegment[i]->pOffsetList = 0;
  2816      pCsr->apSegment[i]->nOffsetList = 0;
  2817      pCsr->apSegment[i]->iDocid = 0;
  2818    }
  2819  
  2820    return SQLITE_OK;
  2821  }
  2822  
  2823  
  2824  int sqlite3Fts3SegReaderStep(
  2825    Fts3Table *p,                   /* Virtual table handle */
  2826    Fts3MultiSegReader *pCsr        /* Cursor object */
  2827  ){
  2828    int rc = SQLITE_OK;
  2829  
  2830    int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
  2831    int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
  2832    int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
  2833    int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
  2834    int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
  2835    int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
  2836  
  2837    Fts3SegReader **apSegment = pCsr->apSegment;
  2838    int nSegment = pCsr->nSegment;
  2839    Fts3SegFilter *pFilter = pCsr->pFilter;
  2840    int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
  2841      p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
  2842    );
  2843  
  2844    if( pCsr->nSegment==0 ) return SQLITE_OK;
  2845  
  2846    do {
  2847      int nMerge;
  2848      int i;
  2849    
  2850      /* Advance the first pCsr->nAdvance entries in the apSegment[] array
  2851      ** forward. Then sort the list in order of current term again.  
  2852      */
  2853      for(i=0; i<pCsr->nAdvance; i++){
  2854        Fts3SegReader *pSeg = apSegment[i];
  2855        if( pSeg->bLookup ){
  2856          fts3SegReaderSetEof(pSeg);
  2857        }else{
  2858          rc = fts3SegReaderNext(p, pSeg, 0);
  2859        }
  2860        if( rc!=SQLITE_OK ) return rc;
  2861      }
  2862      fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
  2863      pCsr->nAdvance = 0;
  2864  
  2865      /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
  2866      assert( rc==SQLITE_OK );
  2867      if( apSegment[0]->aNode==0 ) break;
  2868  
  2869      pCsr->nTerm = apSegment[0]->nTerm;
  2870      pCsr->zTerm = apSegment[0]->zTerm;
  2871  
  2872      /* If this is a prefix-search, and if the term that apSegment[0] points
  2873      ** to does not share a suffix with pFilter->zTerm/nTerm, then all 
  2874      ** required callbacks have been made. In this case exit early.
  2875      **
  2876      ** Similarly, if this is a search for an exact match, and the first term
  2877      ** of segment apSegment[0] is not a match, exit early.
  2878      */
  2879      if( pFilter->zTerm && !isScan ){
  2880        if( pCsr->nTerm<pFilter->nTerm 
  2881         || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
  2882         || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) 
  2883        ){
  2884          break;
  2885        }
  2886      }
  2887  
  2888      nMerge = 1;
  2889      while( nMerge<nSegment 
  2890          && apSegment[nMerge]->aNode
  2891          && apSegment[nMerge]->nTerm==pCsr->nTerm 
  2892          && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
  2893      ){
  2894        nMerge++;
  2895      }
  2896  
  2897      assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
  2898      if( nMerge==1 
  2899       && !isIgnoreEmpty 
  2900       && !isFirst 
  2901       && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
  2902      ){
  2903        pCsr->nDoclist = apSegment[0]->nDoclist;
  2904        if( fts3SegReaderIsPending(apSegment[0]) ){
  2905          rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
  2906          pCsr->aDoclist = pCsr->aBuffer;
  2907        }else{
  2908          pCsr->aDoclist = apSegment[0]->aDoclist;
  2909        }
  2910        if( rc==SQLITE_OK ) rc = SQLITE_ROW;
  2911      }else{
  2912        int nDoclist = 0;           /* Size of doclist */
  2913        sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
  2914  
  2915        /* The current term of the first nMerge entries in the array
  2916        ** of Fts3SegReader objects is the same. The doclists must be merged
  2917        ** and a single term returned with the merged doclist.
  2918        */
  2919        for(i=0; i<nMerge; i++){
  2920          fts3SegReaderFirstDocid(p, apSegment[i]);
  2921        }
  2922        fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
  2923        while( apSegment[0]->pOffsetList ){
  2924          int j;                    /* Number of segments that share a docid */
  2925          char *pList = 0;
  2926          int nList = 0;
  2927          int nByte;
  2928          sqlite3_int64 iDocid = apSegment[0]->iDocid;
  2929          fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
  2930          j = 1;
  2931          while( j<nMerge
  2932              && apSegment[j]->pOffsetList
  2933              && apSegment[j]->iDocid==iDocid
  2934          ){
  2935            fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
  2936            j++;
  2937          }
  2938  
  2939          if( isColFilter ){
  2940            fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
  2941          }
  2942  
  2943          if( !isIgnoreEmpty || nList>0 ){
  2944  
  2945            /* Calculate the 'docid' delta value to write into the merged 
  2946            ** doclist. */
  2947            sqlite3_int64 iDelta;
  2948            if( p->bDescIdx && nDoclist>0 ){
  2949              iDelta = iPrev - iDocid;
  2950            }else{
  2951              iDelta = iDocid - iPrev;
  2952            }
  2953            assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
  2954            assert( nDoclist>0 || iDelta==iDocid );
  2955  
  2956            nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
  2957            if( nDoclist+nByte>pCsr->nBuffer ){
  2958              char *aNew;
  2959              pCsr->nBuffer = (nDoclist+nByte)*2;
  2960              aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
  2961              if( !aNew ){
  2962                return SQLITE_NOMEM;
  2963              }
  2964              pCsr->aBuffer = aNew;
  2965            }
  2966  
  2967            if( isFirst ){
  2968              char *a = &pCsr->aBuffer[nDoclist];
  2969              int nWrite;
  2970             
  2971              nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
  2972              if( nWrite ){
  2973                iPrev = iDocid;
  2974                nDoclist += nWrite;
  2975              }
  2976            }else{
  2977              nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
  2978              iPrev = iDocid;
  2979              if( isRequirePos ){
  2980                memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
  2981                nDoclist += nList;
  2982                pCsr->aBuffer[nDoclist++] = '\0';
  2983              }
  2984            }
  2985          }
  2986  
  2987          fts3SegReaderSort(apSegment, nMerge, j, xCmp);
  2988        }
  2989        if( nDoclist>0 ){
  2990          pCsr->aDoclist = pCsr->aBuffer;
  2991          pCsr->nDoclist = nDoclist;
  2992          rc = SQLITE_ROW;
  2993        }
  2994      }
  2995      pCsr->nAdvance = nMerge;
  2996    }while( rc==SQLITE_OK );
  2997  
  2998    return rc;
  2999  }
  3000  
  3001  
  3002  void sqlite3Fts3SegReaderFinish(
  3003    Fts3MultiSegReader *pCsr       /* Cursor object */
  3004  ){
  3005    if( pCsr ){
  3006      int i;
  3007      for(i=0; i<pCsr->nSegment; i++){
  3008        sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
  3009      }
  3010      sqlite3_free(pCsr->apSegment);
  3011      sqlite3_free(pCsr->aBuffer);
  3012  
  3013      pCsr->nSegment = 0;
  3014      pCsr->apSegment = 0;
  3015      pCsr->aBuffer = 0;
  3016    }
  3017  }
  3018  
  3019  /*
  3020  ** Decode the "end_block" field, selected by column iCol of the SELECT 
  3021  ** statement passed as the first argument. 
  3022  **
  3023  ** The "end_block" field may contain either an integer, or a text field
  3024  ** containing the text representation of two non-negative integers separated 
  3025  ** by one or more space (0x20) characters. In the first case, set *piEndBlock 
  3026  ** to the integer value and *pnByte to zero before returning. In the second, 
  3027  ** set *piEndBlock to the first value and *pnByte to the second.
  3028  */
  3029  static void fts3ReadEndBlockField(
  3030    sqlite3_stmt *pStmt, 
  3031    int iCol, 
  3032    i64 *piEndBlock,
  3033    i64 *pnByte
  3034  ){
  3035    const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
  3036    if( zText ){
  3037      int i;
  3038      int iMul = 1;
  3039      i64 iVal = 0;
  3040      for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
  3041        iVal = iVal*10 + (zText[i] - '0');
  3042      }
  3043      *piEndBlock = iVal;
  3044      while( zText[i]==' ' ) i++;
  3045      iVal = 0;
  3046      if( zText[i]=='-' ){
  3047        i++;
  3048        iMul = -1;
  3049      }
  3050      for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
  3051        iVal = iVal*10 + (zText[i] - '0');
  3052      }
  3053      *pnByte = (iVal * (i64)iMul);
  3054    }
  3055  }
  3056  
  3057  
  3058  /*
  3059  ** A segment of size nByte bytes has just been written to absolute level
  3060  ** iAbsLevel. Promote any segments that should be promoted as a result.
  3061  */
  3062  static int fts3PromoteSegments(
  3063    Fts3Table *p,                   /* FTS table handle */
  3064    sqlite3_int64 iAbsLevel,        /* Absolute level just updated */
  3065    sqlite3_int64 nByte             /* Size of new segment at iAbsLevel */
  3066  ){
  3067    int rc = SQLITE_OK;
  3068    sqlite3_stmt *pRange;
  3069  
  3070    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
  3071  
  3072    if( rc==SQLITE_OK ){
  3073      int bOk = 0;
  3074      i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
  3075      i64 nLimit = (nByte*3)/2;
  3076  
  3077      /* Loop through all entries in the %_segdir table corresponding to 
  3078      ** segments in this index on levels greater than iAbsLevel. If there is
  3079      ** at least one such segment, and it is possible to determine that all 
  3080      ** such segments are smaller than nLimit bytes in size, they will be 
  3081      ** promoted to level iAbsLevel.  */
  3082      sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
  3083      sqlite3_bind_int64(pRange, 2, iLast);
  3084      while( SQLITE_ROW==sqlite3_step(pRange) ){
  3085        i64 nSize = 0, dummy;
  3086        fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
  3087        if( nSize<=0 || nSize>nLimit ){
  3088          /* If nSize==0, then the %_segdir.end_block field does not not 
  3089          ** contain a size value. This happens if it was written by an
  3090          ** old version of FTS. In this case it is not possible to determine
  3091          ** the size of the segment, and so segment promotion does not
  3092          ** take place.  */
  3093          bOk = 0;
  3094          break;
  3095        }
  3096        bOk = 1;
  3097      }
  3098      rc = sqlite3_reset(pRange);
  3099  
  3100      if( bOk ){
  3101        int iIdx = 0;
  3102        sqlite3_stmt *pUpdate1 = 0;
  3103        sqlite3_stmt *pUpdate2 = 0;
  3104  
  3105        if( rc==SQLITE_OK ){
  3106          rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
  3107        }
  3108        if( rc==SQLITE_OK ){
  3109          rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
  3110        }
  3111  
  3112        if( rc==SQLITE_OK ){
  3113  
  3114          /* Loop through all %_segdir entries for segments in this index with
  3115          ** levels equal to or greater than iAbsLevel. As each entry is visited,
  3116          ** updated it to set (level = -1) and (idx = N), where N is 0 for the
  3117          ** oldest segment in the range, 1 for the next oldest, and so on.
  3118          **
  3119          ** In other words, move all segments being promoted to level -1,
  3120          ** setting the "idx" fields as appropriate to keep them in the same
  3121          ** order. The contents of level -1 (which is never used, except
  3122          ** transiently here), will be moved back to level iAbsLevel below.  */
  3123          sqlite3_bind_int64(pRange, 1, iAbsLevel);
  3124          while( SQLITE_ROW==sqlite3_step(pRange) ){
  3125            sqlite3_bind_int(pUpdate1, 1, iIdx++);
  3126            sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
  3127            sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
  3128            sqlite3_step(pUpdate1);
  3129            rc = sqlite3_reset(pUpdate1);
  3130            if( rc!=SQLITE_OK ){
  3131              sqlite3_reset(pRange);
  3132              break;
  3133            }
  3134          }
  3135        }
  3136        if( rc==SQLITE_OK ){
  3137          rc = sqlite3_reset(pRange);
  3138        }
  3139  
  3140        /* Move level -1 to level iAbsLevel */
  3141        if( rc==SQLITE_OK ){
  3142          sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
  3143          sqlite3_step(pUpdate2);
  3144          rc = sqlite3_reset(pUpdate2);
  3145        }
  3146      }
  3147    }
  3148  
  3149  
  3150    return rc;
  3151  }
  3152  
  3153  /*
  3154  ** Merge all level iLevel segments in the database into a single 
  3155  ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
  3156  ** single segment with a level equal to the numerically largest level 
  3157  ** currently present in the database.
  3158  **
  3159  ** If this function is called with iLevel<0, but there is only one
  3160  ** segment in the database, SQLITE_DONE is returned immediately. 
  3161  ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, 
  3162  ** an SQLite error code is returned.
  3163  */
  3164  static int fts3SegmentMerge(
  3165    Fts3Table *p, 
  3166    int iLangid,                    /* Language id to merge */
  3167    int iIndex,                     /* Index in p->aIndex[] to merge */
  3168    int iLevel                      /* Level to merge */
  3169  ){
  3170    int rc;                         /* Return code */
  3171    int iIdx = 0;                   /* Index of new segment */
  3172    sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
  3173    SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
  3174    Fts3SegFilter filter;           /* Segment term filter condition */
  3175    Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
  3176    int bIgnoreEmpty = 0;           /* True to ignore empty segments */
  3177    i64 iMaxLevel = 0;              /* Max level number for this index/langid */
  3178  
  3179    assert( iLevel==FTS3_SEGCURSOR_ALL
  3180         || iLevel==FTS3_SEGCURSOR_PENDING
  3181         || iLevel>=0
  3182    );
  3183    assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
  3184    assert( iIndex>=0 && iIndex<p->nIndex );
  3185  
  3186    rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
  3187    if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
  3188  
  3189    if( iLevel!=FTS3_SEGCURSOR_PENDING ){
  3190      rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
  3191      if( rc!=SQLITE_OK ) goto finished;
  3192    }
  3193  
  3194    if( iLevel==FTS3_SEGCURSOR_ALL ){
  3195      /* This call is to merge all segments in the database to a single
  3196      ** segment. The level of the new segment is equal to the numerically
  3197      ** greatest segment level currently present in the database for this
  3198      ** index. The idx of the new segment is always 0.  */
  3199      if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
  3200        rc = SQLITE_DONE;
  3201        goto finished;
  3202      }
  3203      iNewLevel = iMaxLevel;
  3204      bIgnoreEmpty = 1;
  3205  
  3206    }else{
  3207      /* This call is to merge all segments at level iLevel. find the next
  3208      ** available segment index at level iLevel+1. The call to
  3209      ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to 
  3210      ** a single iLevel+2 segment if necessary.  */
  3211      assert( FTS3_SEGCURSOR_PENDING==-1 );
  3212      iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
  3213      rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
  3214      bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
  3215    }
  3216    if( rc!=SQLITE_OK ) goto finished;
  3217  
  3218    assert( csr.nSegment>0 );
  3219    assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
  3220    assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
  3221  
  3222    memset(&filter, 0, sizeof(Fts3SegFilter));
  3223    filter.flags = FTS3_SEGMENT_REQUIRE_POS;
  3224    filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
  3225  
  3226    rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
  3227    while( SQLITE_OK==rc ){
  3228      rc = sqlite3Fts3SegReaderStep(p, &csr);
  3229      if( rc!=SQLITE_ROW ) break;
  3230      rc = fts3SegWriterAdd(p, &pWriter, 1, 
  3231          csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
  3232    }
  3233    if( rc!=SQLITE_OK ) goto finished;
  3234    assert( pWriter || bIgnoreEmpty );
  3235  
  3236    if( iLevel!=FTS3_SEGCURSOR_PENDING ){
  3237      rc = fts3DeleteSegdir(
  3238          p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
  3239      );
  3240      if( rc!=SQLITE_OK ) goto finished;
  3241    }
  3242    if( pWriter ){
  3243      rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
  3244      if( rc==SQLITE_OK ){
  3245        if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
  3246          rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
  3247        }
  3248      }
  3249    }
  3250  
  3251   finished:
  3252    fts3SegWriterFree(pWriter);
  3253    sqlite3Fts3SegReaderFinish(&csr);
  3254    return rc;
  3255  }
  3256  
  3257  
  3258  /* 
  3259  ** Flush the contents of pendingTerms to level 0 segments. 
  3260  */
  3261  int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
  3262    int rc = SQLITE_OK;
  3263    int i;
  3264          
  3265    for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
  3266      rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
  3267      if( rc==SQLITE_DONE ) rc = SQLITE_OK;
  3268    }
  3269    sqlite3Fts3PendingTermsClear(p);
  3270  
  3271    /* Determine the auto-incr-merge setting if unknown.  If enabled,
  3272    ** estimate the number of leaf blocks of content to be written
  3273    */
  3274    if( rc==SQLITE_OK && p->bHasStat
  3275     && p->nAutoincrmerge==0xff && p->nLeafAdd>0
  3276    ){
  3277      sqlite3_stmt *pStmt = 0;
  3278      rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
  3279      if( rc==SQLITE_OK ){
  3280        sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
  3281        rc = sqlite3_step(pStmt);
  3282        if( rc==SQLITE_ROW ){
  3283          p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
  3284          if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
  3285        }else if( rc==SQLITE_DONE ){
  3286          p->nAutoincrmerge = 0;
  3287        }
  3288        rc = sqlite3_reset(pStmt);
  3289      }
  3290    }
  3291    return rc;
  3292  }
  3293  
  3294  /*
  3295  ** Encode N integers as varints into a blob.
  3296  */
  3297  static void fts3EncodeIntArray(
  3298    int N,             /* The number of integers to encode */
  3299    u32 *a,            /* The integer values */
  3300    char *zBuf,        /* Write the BLOB here */
  3301    int *pNBuf         /* Write number of bytes if zBuf[] used here */
  3302  ){
  3303    int i, j;
  3304    for(i=j=0; i<N; i++){
  3305      j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
  3306    }
  3307    *pNBuf = j;
  3308  }
  3309  
  3310  /*
  3311  ** Decode a blob of varints into N integers
  3312  */
  3313  static void fts3DecodeIntArray(
  3314    int N,             /* The number of integers to decode */
  3315    u32 *a,            /* Write the integer values */
  3316    const char *zBuf,  /* The BLOB containing the varints */
  3317    int nBuf           /* size of the BLOB */
  3318  ){
  3319    int i, j;
  3320    UNUSED_PARAMETER(nBuf);
  3321    for(i=j=0; i<N; i++){
  3322      sqlite3_int64 x;
  3323      j += sqlite3Fts3GetVarint(&zBuf[j], &x);
  3324      assert(j<=nBuf);
  3325      a[i] = (u32)(x & 0xffffffff);
  3326    }
  3327  }
  3328  
  3329  /*
  3330  ** Insert the sizes (in tokens) for each column of the document
  3331  ** with docid equal to p->iPrevDocid.  The sizes are encoded as
  3332  ** a blob of varints.
  3333  */
  3334  static void fts3InsertDocsize(
  3335    int *pRC,                       /* Result code */
  3336    Fts3Table *p,                   /* Table into which to insert */
  3337    u32 *aSz                        /* Sizes of each column, in tokens */
  3338  ){
  3339    char *pBlob;             /* The BLOB encoding of the document size */
  3340    int nBlob;               /* Number of bytes in the BLOB */
  3341    sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
  3342    int rc;                  /* Result code from subfunctions */
  3343  
  3344    if( *pRC ) return;
  3345    pBlob = sqlite3_malloc( 10*p->nColumn );
  3346    if( pBlob==0 ){
  3347      *pRC = SQLITE_NOMEM;
  3348      return;
  3349    }
  3350    fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
  3351    rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
  3352    if( rc ){
  3353      sqlite3_free(pBlob);
  3354      *pRC = rc;
  3355      return;
  3356    }
  3357    sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
  3358    sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
  3359    sqlite3_step(pStmt);
  3360    *pRC = sqlite3_reset(pStmt);
  3361  }
  3362  
  3363  /*
  3364  ** Record 0 of the %_stat table contains a blob consisting of N varints,
  3365  ** where N is the number of user defined columns in the fts3 table plus
  3366  ** two. If nCol is the number of user defined columns, then values of the 
  3367  ** varints are set as follows:
  3368  **
  3369  **   Varint 0:       Total number of rows in the table.
  3370  **
  3371  **   Varint 1..nCol: For each column, the total number of tokens stored in
  3372  **                   the column for all rows of the table.
  3373  **
  3374  **   Varint 1+nCol:  The total size, in bytes, of all text values in all
  3375  **                   columns of all rows of the table.
  3376  **
  3377  */
  3378  static void fts3UpdateDocTotals(
  3379    int *pRC,                       /* The result code */
  3380    Fts3Table *p,                   /* Table being updated */
  3381    u32 *aSzIns,                    /* Size increases */
  3382    u32 *aSzDel,                    /* Size decreases */
  3383    int nChng                       /* Change in the number of documents */
  3384  ){
  3385    char *pBlob;             /* Storage for BLOB written into %_stat */
  3386    int nBlob;               /* Size of BLOB written into %_stat */
  3387    u32 *a;                  /* Array of integers that becomes the BLOB */
  3388    sqlite3_stmt *pStmt;     /* Statement for reading and writing */
  3389    int i;                   /* Loop counter */
  3390    int rc;                  /* Result code from subfunctions */
  3391  
  3392    const int nStat = p->nColumn+2;
  3393  
  3394    if( *pRC ) return;
  3395    a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
  3396    if( a==0 ){
  3397      *pRC = SQLITE_NOMEM;
  3398      return;
  3399    }
  3400    pBlob = (char*)&a[nStat];
  3401    rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
  3402    if( rc ){
  3403      sqlite3_free(a);
  3404      *pRC = rc;
  3405      return;
  3406    }
  3407    sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
  3408    if( sqlite3_step(pStmt)==SQLITE_ROW ){
  3409      fts3DecodeIntArray(nStat, a,
  3410           sqlite3_column_blob(pStmt, 0),
  3411           sqlite3_column_bytes(pStmt, 0));
  3412    }else{
  3413      memset(a, 0, sizeof(u32)*(nStat) );
  3414    }
  3415    rc = sqlite3_reset(pStmt);
  3416    if( rc!=SQLITE_OK ){
  3417      sqlite3_free(a);
  3418      *pRC = rc;
  3419      return;
  3420    }
  3421    if( nChng<0 && a[0]<(u32)(-nChng) ){
  3422      a[0] = 0;
  3423    }else{
  3424      a[0] += nChng;
  3425    }
  3426    for(i=0; i<p->nColumn+1; i++){
  3427      u32 x = a[i+1];
  3428      if( x+aSzIns[i] < aSzDel[i] ){
  3429        x = 0;
  3430      }else{
  3431        x = x + aSzIns[i] - aSzDel[i];
  3432      }
  3433      a[i+1] = x;
  3434    }
  3435    fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
  3436    rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
  3437    if( rc ){
  3438      sqlite3_free(a);
  3439      *pRC = rc;
  3440      return;
  3441    }
  3442    sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
  3443    sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
  3444    sqlite3_step(pStmt);
  3445    *pRC = sqlite3_reset(pStmt);
  3446    sqlite3_free(a);
  3447  }
  3448  
  3449  /*
  3450  ** Merge the entire database so that there is one segment for each 
  3451  ** iIndex/iLangid combination.
  3452  */
  3453  static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
  3454    int bSeenDone = 0;
  3455    int rc;
  3456    sqlite3_stmt *pAllLangid = 0;
  3457  
  3458    rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
  3459    if( rc==SQLITE_OK ){
  3460      int rc2;
  3461      sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
  3462      sqlite3_bind_int(pAllLangid, 2, p->nIndex);
  3463      while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
  3464        int i;
  3465        int iLangid = sqlite3_column_int(pAllLangid, 0);
  3466        for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
  3467          rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
  3468          if( rc==SQLITE_DONE ){
  3469            bSeenDone = 1;
  3470            rc = SQLITE_OK;
  3471          }
  3472        }
  3473      }
  3474      rc2 = sqlite3_reset(pAllLangid);
  3475      if( rc==SQLITE_OK ) rc = rc2;
  3476    }
  3477  
  3478    sqlite3Fts3SegmentsClose(p);
  3479    sqlite3Fts3PendingTermsClear(p);
  3480  
  3481    return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
  3482  }
  3483  
  3484  /*
  3485  ** This function is called when the user executes the following statement:
  3486  **
  3487  **     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
  3488  **
  3489  ** The entire FTS index is discarded and rebuilt. If the table is one 
  3490  ** created using the content=xxx option, then the new index is based on
  3491  ** the current contents of the xxx table. Otherwise, it is rebuilt based
  3492  ** on the contents of the %_content table.
  3493  */
  3494  static int fts3DoRebuild(Fts3Table *p){
  3495    int rc;                         /* Return Code */
  3496  
  3497    rc = fts3DeleteAll(p, 0);
  3498    if( rc==SQLITE_OK ){
  3499      u32 *aSz = 0;
  3500      u32 *aSzIns = 0;
  3501      u32 *aSzDel = 0;
  3502      sqlite3_stmt *pStmt = 0;
  3503      int nEntry = 0;
  3504  
  3505      /* Compose and prepare an SQL statement to loop through the content table */
  3506      char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
  3507      if( !zSql ){
  3508        rc = SQLITE_NOMEM;
  3509      }else{
  3510        rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  3511        sqlite3_free(zSql);
  3512      }
  3513  
  3514      if( rc==SQLITE_OK ){
  3515        int nByte = sizeof(u32) * (p->nColumn+1)*3;
  3516        aSz = (u32 *)sqlite3_malloc(nByte);
  3517        if( aSz==0 ){
  3518          rc = SQLITE_NOMEM;
  3519        }else{
  3520          memset(aSz, 0, nByte);
  3521          aSzIns = &aSz[p->nColumn+1];
  3522          aSzDel = &aSzIns[p->nColumn+1];
  3523        }
  3524      }
  3525  
  3526      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  3527        int iCol;
  3528        int iLangid = langidFromSelect(p, pStmt);
  3529        rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
  3530        memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
  3531        for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
  3532          if( p->abNotindexed[iCol]==0 ){
  3533            const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
  3534            rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
  3535            aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
  3536          }
  3537        }
  3538        if( p->bHasDocsize ){
  3539          fts3InsertDocsize(&rc, p, aSz);
  3540        }
  3541        if( rc!=SQLITE_OK ){
  3542          sqlite3_finalize(pStmt);
  3543          pStmt = 0;
  3544        }else{
  3545          nEntry++;
  3546          for(iCol=0; iCol<=p->nColumn; iCol++){
  3547            aSzIns[iCol] += aSz[iCol];
  3548          }
  3549        }
  3550      }
  3551      if( p->bFts4 ){
  3552        fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
  3553      }
  3554      sqlite3_free(aSz);
  3555  
  3556      if( pStmt ){
  3557        int rc2 = sqlite3_finalize(pStmt);
  3558        if( rc==SQLITE_OK ){
  3559          rc = rc2;
  3560        }
  3561      }
  3562    }
  3563  
  3564    return rc;
  3565  }
  3566  
  3567  
  3568  /*
  3569  ** This function opens a cursor used to read the input data for an 
  3570  ** incremental merge operation. Specifically, it opens a cursor to scan
  3571  ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute 
  3572  ** level iAbsLevel.
  3573  */
  3574  static int fts3IncrmergeCsr(
  3575    Fts3Table *p,                   /* FTS3 table handle */
  3576    sqlite3_int64 iAbsLevel,        /* Absolute level to open */
  3577    int nSeg,                       /* Number of segments to merge */
  3578    Fts3MultiSegReader *pCsr        /* Cursor object to populate */
  3579  ){
  3580    int rc;                         /* Return Code */
  3581    sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */  
  3582    int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
  3583  
  3584    /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
  3585    memset(pCsr, 0, sizeof(*pCsr));
  3586    nByte = sizeof(Fts3SegReader *) * nSeg;
  3587    pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
  3588  
  3589    if( pCsr->apSegment==0 ){
  3590      rc = SQLITE_NOMEM;
  3591    }else{
  3592      memset(pCsr->apSegment, 0, nByte);
  3593      rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
  3594    }
  3595    if( rc==SQLITE_OK ){
  3596      int i;
  3597      int rc2;
  3598      sqlite3_bind_int64(pStmt, 1, iAbsLevel);
  3599      assert( pCsr->nSegment==0 );
  3600      for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
  3601        rc = sqlite3Fts3SegReaderNew(i, 0,
  3602            sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
  3603            sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
  3604            sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
  3605            sqlite3_column_blob(pStmt, 4),         /* segdir.root */
  3606            sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
  3607            &pCsr->apSegment[i]
  3608        );
  3609        pCsr->nSegment++;
  3610      }
  3611      rc2 = sqlite3_reset(pStmt);
  3612      if( rc==SQLITE_OK ) rc = rc2;
  3613    }
  3614  
  3615    return rc;
  3616  }
  3617  
  3618  typedef struct IncrmergeWriter IncrmergeWriter;
  3619  typedef struct NodeWriter NodeWriter;
  3620  typedef struct Blob Blob;
  3621  typedef struct NodeReader NodeReader;
  3622  
  3623  /*
  3624  ** An instance of the following structure is used as a dynamic buffer
  3625  ** to build up nodes or other blobs of data in.
  3626  **
  3627  ** The function blobGrowBuffer() is used to extend the allocation.
  3628  */
  3629  struct Blob {
  3630    char *a;                        /* Pointer to allocation */
  3631    int n;                          /* Number of valid bytes of data in a[] */
  3632    int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
  3633  };
  3634  
  3635  /*
  3636  ** This structure is used to build up buffers containing segment b-tree 
  3637  ** nodes (blocks).
  3638  */
  3639  struct NodeWriter {
  3640    sqlite3_int64 iBlock;           /* Current block id */
  3641    Blob key;                       /* Last key written to the current block */
  3642    Blob block;                     /* Current block image */
  3643  };
  3644  
  3645  /*
  3646  ** An object of this type contains the state required to create or append
  3647  ** to an appendable b-tree segment.
  3648  */
  3649  struct IncrmergeWriter {
  3650    int nLeafEst;                   /* Space allocated for leaf blocks */
  3651    int nWork;                      /* Number of leaf pages flushed */
  3652    sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
  3653    int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
  3654    sqlite3_int64 iStart;           /* Block number of first allocated block */
  3655    sqlite3_int64 iEnd;             /* Block number of last allocated block */
  3656    sqlite3_int64 nLeafData;        /* Bytes of leaf page data so far */
  3657    u8 bNoLeafData;                 /* If true, store 0 for segment size */
  3658    NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
  3659  };
  3660  
  3661  /*
  3662  ** An object of the following type is used to read data from a single
  3663  ** FTS segment node. See the following functions:
  3664  **
  3665  **     nodeReaderInit()
  3666  **     nodeReaderNext()
  3667  **     nodeReaderRelease()
  3668  */
  3669  struct NodeReader {
  3670    const char *aNode;
  3671    int nNode;
  3672    int iOff;                       /* Current offset within aNode[] */
  3673  
  3674    /* Output variables. Containing the current node entry. */
  3675    sqlite3_int64 iChild;           /* Pointer to child node */
  3676    Blob term;                      /* Current term */
  3677    const char *aDoclist;           /* Pointer to doclist */
  3678    int nDoclist;                   /* Size of doclist in bytes */
  3679  };
  3680  
  3681  /*
  3682  ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  3683  ** Otherwise, if the allocation at pBlob->a is not already at least nMin
  3684  ** bytes in size, extend (realloc) it to be so.
  3685  **
  3686  ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
  3687  ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
  3688  ** to reflect the new size of the pBlob->a[] buffer.
  3689  */
  3690  static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
  3691    if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
  3692      int nAlloc = nMin;
  3693      char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
  3694      if( a ){
  3695        pBlob->nAlloc = nAlloc;
  3696        pBlob->a = a;
  3697      }else{
  3698        *pRc = SQLITE_NOMEM;
  3699      }
  3700    }
  3701  }
  3702  
  3703  /*
  3704  ** Attempt to advance the node-reader object passed as the first argument to
  3705  ** the next entry on the node. 
  3706  **
  3707  ** Return an error code if an error occurs (SQLITE_NOMEM is possible). 
  3708  ** Otherwise return SQLITE_OK. If there is no next entry on the node
  3709  ** (e.g. because the current entry is the last) set NodeReader->aNode to
  3710  ** NULL to indicate EOF. Otherwise, populate the NodeReader structure output 
  3711  ** variables for the new entry.
  3712  */
  3713  static int nodeReaderNext(NodeReader *p){
  3714    int bFirst = (p->term.n==0);    /* True for first term on the node */
  3715    int nPrefix = 0;                /* Bytes to copy from previous term */
  3716    int nSuffix = 0;                /* Bytes to append to the prefix */
  3717    int rc = SQLITE_OK;             /* Return code */
  3718  
  3719    assert( p->aNode );
  3720    if( p->iChild && bFirst==0 ) p->iChild++;
  3721    if( p->iOff>=p->nNode ){
  3722      /* EOF */
  3723      p->aNode = 0;
  3724    }else{
  3725      if( bFirst==0 ){
  3726        p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
  3727      }
  3728      p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
  3729  
  3730      blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
  3731      if( rc==SQLITE_OK ){
  3732        memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
  3733        p->term.n = nPrefix+nSuffix;
  3734        p->iOff += nSuffix;
  3735        if( p->iChild==0 ){
  3736          p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
  3737          p->aDoclist = &p->aNode[p->iOff];
  3738          p->iOff += p->nDoclist;
  3739        }
  3740      }
  3741    }
  3742  
  3743    assert( p->iOff<=p->nNode );
  3744  
  3745    return rc;
  3746  }
  3747  
  3748  /*
  3749  ** Release all dynamic resources held by node-reader object *p.
  3750  */
  3751  static void nodeReaderRelease(NodeReader *p){
  3752    sqlite3_free(p->term.a);
  3753  }
  3754  
  3755  /*
  3756  ** Initialize a node-reader object to read the node in buffer aNode/nNode.
  3757  **
  3758  ** If successful, SQLITE_OK is returned and the NodeReader object set to 
  3759  ** point to the first entry on the node (if any). Otherwise, an SQLite
  3760  ** error code is returned.
  3761  */
  3762  static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
  3763    memset(p, 0, sizeof(NodeReader));
  3764    p->aNode = aNode;
  3765    p->nNode = nNode;
  3766  
  3767    /* Figure out if this is a leaf or an internal node. */
  3768    if( p->aNode[0] ){
  3769      /* An internal node. */
  3770      p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
  3771    }else{
  3772      p->iOff = 1;
  3773    }
  3774  
  3775    return nodeReaderNext(p);
  3776  }
  3777  
  3778  /*
  3779  ** This function is called while writing an FTS segment each time a leaf o
  3780  ** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
  3781  ** to be greater than the largest key on the node just written, but smaller
  3782  ** than or equal to the first key that will be written to the next leaf
  3783  ** node.
  3784  **
  3785  ** The block id of the leaf node just written to disk may be found in
  3786  ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
  3787  */
  3788  static int fts3IncrmergePush(
  3789    Fts3Table *p,                   /* Fts3 table handle */
  3790    IncrmergeWriter *pWriter,       /* Writer object */
  3791    const char *zTerm,              /* Term to write to internal node */
  3792    int nTerm                       /* Bytes at zTerm */
  3793  ){
  3794    sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
  3795    int iLayer;
  3796  
  3797    assert( nTerm>0 );
  3798    for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
  3799      sqlite3_int64 iNextPtr = 0;
  3800      NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
  3801      int rc = SQLITE_OK;
  3802      int nPrefix;
  3803      int nSuffix;
  3804      int nSpace;
  3805  
  3806      /* Figure out how much space the key will consume if it is written to
  3807      ** the current node of layer iLayer. Due to the prefix compression, 
  3808      ** the space required changes depending on which node the key is to
  3809      ** be added to.  */
  3810      nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
  3811      nSuffix = nTerm - nPrefix;
  3812      nSpace  = sqlite3Fts3VarintLen(nPrefix);
  3813      nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
  3814  
  3815      if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){ 
  3816        /* If the current node of layer iLayer contains zero keys, or if adding
  3817        ** the key to it will not cause it to grow to larger than nNodeSize 
  3818        ** bytes in size, write the key here.  */
  3819  
  3820        Blob *pBlk = &pNode->block;
  3821        if( pBlk->n==0 ){
  3822          blobGrowBuffer(pBlk, p->nNodeSize, &rc);
  3823          if( rc==SQLITE_OK ){
  3824            pBlk->a[0] = (char)iLayer;
  3825            pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
  3826          }
  3827        }
  3828        blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
  3829        blobGrowBuffer(&pNode->key, nTerm, &rc);
  3830  
  3831        if( rc==SQLITE_OK ){
  3832          if( pNode->key.n ){
  3833            pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
  3834          }
  3835          pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
  3836          memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
  3837          pBlk->n += nSuffix;
  3838  
  3839          memcpy(pNode->key.a, zTerm, nTerm);
  3840          pNode->key.n = nTerm;
  3841        }
  3842      }else{
  3843        /* Otherwise, flush the current node of layer iLayer to disk.
  3844        ** Then allocate a new, empty sibling node. The key will be written
  3845        ** into the parent of this node. */
  3846        rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
  3847  
  3848        assert( pNode->block.nAlloc>=p->nNodeSize );
  3849        pNode->block.a[0] = (char)iLayer;
  3850        pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
  3851  
  3852        iNextPtr = pNode->iBlock;
  3853        pNode->iBlock++;
  3854        pNode->key.n = 0;
  3855      }
  3856  
  3857      if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
  3858      iPtr = iNextPtr;
  3859    }
  3860  
  3861    assert( 0 );
  3862    return 0;
  3863  }
  3864  
  3865  /*
  3866  ** Append a term and (optionally) doclist to the FTS segment node currently
  3867  ** stored in blob *pNode. The node need not contain any terms, but the
  3868  ** header must be written before this function is called.
  3869  **
  3870  ** A node header is a single 0x00 byte for a leaf node, or a height varint
  3871  ** followed by the left-hand-child varint for an internal node.
  3872  **
  3873  ** The term to be appended is passed via arguments zTerm/nTerm. For a 
  3874  ** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
  3875  ** node, both aDoclist and nDoclist must be passed 0.
  3876  **
  3877  ** If the size of the value in blob pPrev is zero, then this is the first
  3878  ** term written to the node. Otherwise, pPrev contains a copy of the 
  3879  ** previous term. Before this function returns, it is updated to contain a
  3880  ** copy of zTerm/nTerm.
  3881  **
  3882  ** It is assumed that the buffer associated with pNode is already large
  3883  ** enough to accommodate the new entry. The buffer associated with pPrev
  3884  ** is extended by this function if requrired.
  3885  **
  3886  ** If an error (i.e. OOM condition) occurs, an SQLite error code is
  3887  ** returned. Otherwise, SQLITE_OK.
  3888  */
  3889  static int fts3AppendToNode(
  3890    Blob *pNode,                    /* Current node image to append to */
  3891    Blob *pPrev,                    /* Buffer containing previous term written */
  3892    const char *zTerm,              /* New term to write */
  3893    int nTerm,                      /* Size of zTerm in bytes */
  3894    const char *aDoclist,           /* Doclist (or NULL) to write */
  3895    int nDoclist                    /* Size of aDoclist in bytes */ 
  3896  ){
  3897    int rc = SQLITE_OK;             /* Return code */
  3898    int bFirst = (pPrev->n==0);     /* True if this is the first term written */
  3899    int nPrefix;                    /* Size of term prefix in bytes */
  3900    int nSuffix;                    /* Size of term suffix in bytes */
  3901  
  3902    /* Node must have already been started. There must be a doclist for a
  3903    ** leaf node, and there must not be a doclist for an internal node.  */
  3904    assert( pNode->n>0 );
  3905    assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
  3906  
  3907    blobGrowBuffer(pPrev, nTerm, &rc);
  3908    if( rc!=SQLITE_OK ) return rc;
  3909  
  3910    nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
  3911    nSuffix = nTerm - nPrefix;
  3912    memcpy(pPrev->a, zTerm, nTerm);
  3913    pPrev->n = nTerm;
  3914  
  3915    if( bFirst==0 ){
  3916      pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
  3917    }
  3918    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
  3919    memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
  3920    pNode->n += nSuffix;
  3921  
  3922    if( aDoclist ){
  3923      pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
  3924      memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
  3925      pNode->n += nDoclist;
  3926    }
  3927  
  3928    assert( pNode->n<=pNode->nAlloc );
  3929  
  3930    return SQLITE_OK;
  3931  }
  3932  
  3933  /*
  3934  ** Append the current term and doclist pointed to by cursor pCsr to the
  3935  ** appendable b-tree segment opened for writing by pWriter.
  3936  **
  3937  ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
  3938  */
  3939  static int fts3IncrmergeAppend(
  3940    Fts3Table *p,                   /* Fts3 table handle */
  3941    IncrmergeWriter *pWriter,       /* Writer object */
  3942    Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
  3943  ){
  3944    const char *zTerm = pCsr->zTerm;
  3945    int nTerm = pCsr->nTerm;
  3946    const char *aDoclist = pCsr->aDoclist;
  3947    int nDoclist = pCsr->nDoclist;
  3948    int rc = SQLITE_OK;           /* Return code */
  3949    int nSpace;                   /* Total space in bytes required on leaf */
  3950    int nPrefix;                  /* Size of prefix shared with previous term */
  3951    int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
  3952    NodeWriter *pLeaf;            /* Object used to write leaf nodes */
  3953  
  3954    pLeaf = &pWriter->aNodeWriter[0];
  3955    nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
  3956    nSuffix = nTerm - nPrefix;
  3957  
  3958    nSpace  = sqlite3Fts3VarintLen(nPrefix);
  3959    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
  3960    nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
  3961  
  3962    /* If the current block is not empty, and if adding this term/doclist
  3963    ** to the current block would make it larger than Fts3Table.nNodeSize
  3964    ** bytes, write this block out to the database. */
  3965    if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
  3966      rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
  3967      pWriter->nWork++;
  3968  
  3969      /* Add the current term to the parent node. The term added to the 
  3970      ** parent must:
  3971      **
  3972      **   a) be greater than the largest term on the leaf node just written
  3973      **      to the database (still available in pLeaf->key), and
  3974      **
  3975      **   b) be less than or equal to the term about to be added to the new
  3976      **      leaf node (zTerm/nTerm).
  3977      **
  3978      ** In other words, it must be the prefix of zTerm 1 byte longer than
  3979      ** the common prefix (if any) of zTerm and pWriter->zTerm.
  3980      */
  3981      if( rc==SQLITE_OK ){
  3982        rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
  3983      }
  3984  
  3985      /* Advance to the next output block */
  3986      pLeaf->iBlock++;
  3987      pLeaf->key.n = 0;
  3988      pLeaf->block.n = 0;
  3989  
  3990      nSuffix = nTerm;
  3991      nSpace  = 1;
  3992      nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
  3993      nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
  3994    }
  3995  
  3996    pWriter->nLeafData += nSpace;
  3997    blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
  3998    if( rc==SQLITE_OK ){
  3999      if( pLeaf->block.n==0 ){
  4000        pLeaf->block.n = 1;
  4001        pLeaf->block.a[0] = '\0';
  4002      }
  4003      rc = fts3AppendToNode(
  4004          &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
  4005      );
  4006    }
  4007  
  4008    return rc;
  4009  }
  4010  
  4011  /*
  4012  ** This function is called to release all dynamic resources held by the
  4013  ** merge-writer object pWriter, and if no error has occurred, to flush
  4014  ** all outstanding node buffers held by pWriter to disk.
  4015  **
  4016  ** If *pRc is not SQLITE_OK when this function is called, then no attempt
  4017  ** is made to write any data to disk. Instead, this function serves only
  4018  ** to release outstanding resources.
  4019  **
  4020  ** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
  4021  ** flushing buffers to disk, *pRc is set to an SQLite error code before
  4022  ** returning.
  4023  */
  4024  static void fts3IncrmergeRelease(
  4025    Fts3Table *p,                   /* FTS3 table handle */
  4026    IncrmergeWriter *pWriter,       /* Merge-writer object */
  4027    int *pRc                        /* IN/OUT: Error code */
  4028  ){
  4029    int i;                          /* Used to iterate through non-root layers */
  4030    int iRoot;                      /* Index of root in pWriter->aNodeWriter */
  4031    NodeWriter *pRoot;              /* NodeWriter for root node */
  4032    int rc = *pRc;                  /* Error code */
  4033  
  4034    /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment 
  4035    ** root node. If the segment fits entirely on a single leaf node, iRoot
  4036    ** will be set to 0. If the root node is the parent of the leaves, iRoot
  4037    ** will be 1. And so on.  */
  4038    for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
  4039      NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
  4040      if( pNode->block.n>0 ) break;
  4041      assert( *pRc || pNode->block.nAlloc==0 );
  4042      assert( *pRc || pNode->key.nAlloc==0 );
  4043      sqlite3_free(pNode->block.a);
  4044      sqlite3_free(pNode->key.a);
  4045    }
  4046  
  4047    /* Empty output segment. This is a no-op. */
  4048    if( iRoot<0 ) return;
  4049  
  4050    /* The entire output segment fits on a single node. Normally, this means
  4051    ** the node would be stored as a blob in the "root" column of the %_segdir
  4052    ** table. However, this is not permitted in this case. The problem is that 
  4053    ** space has already been reserved in the %_segments table, and so the 
  4054    ** start_block and end_block fields of the %_segdir table must be populated. 
  4055    ** And, by design or by accident, released versions of FTS cannot handle 
  4056    ** segments that fit entirely on the root node with start_block!=0.
  4057    **
  4058    ** Instead, create a synthetic root node that contains nothing but a 
  4059    ** pointer to the single content node. So that the segment consists of a
  4060    ** single leaf and a single interior (root) node.
  4061    **
  4062    ** Todo: Better might be to defer allocating space in the %_segments 
  4063    ** table until we are sure it is needed.
  4064    */
  4065    if( iRoot==0 ){
  4066      Blob *pBlock = &pWriter->aNodeWriter[1].block;
  4067      blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
  4068      if( rc==SQLITE_OK ){
  4069        pBlock->a[0] = 0x01;
  4070        pBlock->n = 1 + sqlite3Fts3PutVarint(
  4071            &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
  4072        );
  4073      }
  4074      iRoot = 1;
  4075    }
  4076    pRoot = &pWriter->aNodeWriter[iRoot];
  4077  
  4078    /* Flush all currently outstanding nodes to disk. */
  4079    for(i=0; i<iRoot; i++){
  4080      NodeWriter *pNode = &pWriter->aNodeWriter[i];
  4081      if( pNode->block.n>0 && rc==SQLITE_OK ){
  4082        rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
  4083      }
  4084      sqlite3_free(pNode->block.a);
  4085      sqlite3_free(pNode->key.a);
  4086    }
  4087  
  4088    /* Write the %_segdir record. */
  4089    if( rc==SQLITE_OK ){
  4090      rc = fts3WriteSegdir(p, 
  4091          pWriter->iAbsLevel+1,               /* level */
  4092          pWriter->iIdx,                      /* idx */
  4093          pWriter->iStart,                    /* start_block */
  4094          pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
  4095          pWriter->iEnd,                      /* end_block */
  4096          (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0),   /* end_block */
  4097          pRoot->block.a, pRoot->block.n      /* root */
  4098      );
  4099    }
  4100    sqlite3_free(pRoot->block.a);
  4101    sqlite3_free(pRoot->key.a);
  4102  
  4103    *pRc = rc;
  4104  }
  4105  
  4106  /*
  4107  ** Compare the term in buffer zLhs (size in bytes nLhs) with that in
  4108  ** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
  4109  ** the other, it is considered to be smaller than the other.
  4110  **
  4111  ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
  4112  ** if it is greater.
  4113  */
  4114  static int fts3TermCmp(
  4115    const char *zLhs, int nLhs,     /* LHS of comparison */
  4116    const char *zRhs, int nRhs      /* RHS of comparison */
  4117  ){
  4118    int nCmp = MIN(nLhs, nRhs);
  4119    int res;
  4120  
  4121    res = memcmp(zLhs, zRhs, nCmp);
  4122    if( res==0 ) res = nLhs - nRhs;
  4123  
  4124    return res;
  4125  }
  4126  
  4127  
  4128  /*
  4129  ** Query to see if the entry in the %_segments table with blockid iEnd is 
  4130  ** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
  4131  ** returning. Otherwise, set *pbRes to 0. 
  4132  **
  4133  ** Or, if an error occurs while querying the database, return an SQLite 
  4134  ** error code. The final value of *pbRes is undefined in this case.
  4135  **
  4136  ** This is used to test if a segment is an "appendable" segment. If it
  4137  ** is, then a NULL entry has been inserted into the %_segments table
  4138  ** with blockid %_segdir.end_block.
  4139  */
  4140  static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
  4141    int bRes = 0;                   /* Result to set *pbRes to */
  4142    sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
  4143    int rc;                         /* Return code */
  4144  
  4145    rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
  4146    if( rc==SQLITE_OK ){
  4147      sqlite3_bind_int64(pCheck, 1, iEnd);
  4148      if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
  4149      rc = sqlite3_reset(pCheck);
  4150    }
  4151    
  4152    *pbRes = bRes;
  4153    return rc;
  4154  }
  4155  
  4156  /*
  4157  ** This function is called when initializing an incremental-merge operation.
  4158  ** It checks if the existing segment with index value iIdx at absolute level 
  4159  ** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
  4160  ** merge-writer object *pWriter is initialized to write to it.
  4161  **
  4162  ** An existing segment can be appended to by an incremental merge if:
  4163  **
  4164  **   * It was initially created as an appendable segment (with all required
  4165  **     space pre-allocated), and
  4166  **
  4167  **   * The first key read from the input (arguments zKey and nKey) is 
  4168  **     greater than the largest key currently stored in the potential
  4169  **     output segment.
  4170  */
  4171  static int fts3IncrmergeLoad(
  4172    Fts3Table *p,                   /* Fts3 table handle */
  4173    sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
  4174    int iIdx,                       /* Index of candidate output segment */
  4175    const char *zKey,               /* First key to write */
  4176    int nKey,                       /* Number of bytes in nKey */
  4177    IncrmergeWriter *pWriter        /* Populate this object */
  4178  ){
  4179    int rc;                         /* Return code */
  4180    sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
  4181  
  4182    rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
  4183    if( rc==SQLITE_OK ){
  4184      sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
  4185      sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
  4186      sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
  4187      const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
  4188      int nRoot = 0;                /* Size of aRoot[] in bytes */
  4189      int rc2;                      /* Return code from sqlite3_reset() */
  4190      int bAppendable = 0;          /* Set to true if segment is appendable */
  4191  
  4192      /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
  4193      sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
  4194      sqlite3_bind_int(pSelect, 2, iIdx);
  4195      if( sqlite3_step(pSelect)==SQLITE_ROW ){
  4196        iStart = sqlite3_column_int64(pSelect, 1);
  4197        iLeafEnd = sqlite3_column_int64(pSelect, 2);
  4198        fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
  4199        if( pWriter->nLeafData<0 ){
  4200          pWriter->nLeafData = pWriter->nLeafData * -1;
  4201        }
  4202        pWriter->bNoLeafData = (pWriter->nLeafData==0);
  4203        nRoot = sqlite3_column_bytes(pSelect, 4);
  4204        aRoot = sqlite3_column_blob(pSelect, 4);
  4205      }else{
  4206        return sqlite3_reset(pSelect);
  4207      }
  4208  
  4209      /* Check for the zero-length marker in the %_segments table */
  4210      rc = fts3IsAppendable(p, iEnd, &bAppendable);
  4211  
  4212      /* Check that zKey/nKey is larger than the largest key the candidate */
  4213      if( rc==SQLITE_OK && bAppendable ){
  4214        char *aLeaf = 0;
  4215        int nLeaf = 0;
  4216  
  4217        rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
  4218        if( rc==SQLITE_OK ){
  4219          NodeReader reader;
  4220          for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
  4221              rc==SQLITE_OK && reader.aNode;
  4222              rc = nodeReaderNext(&reader)
  4223          ){
  4224            assert( reader.aNode );
  4225          }
  4226          if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
  4227            bAppendable = 0;
  4228          }
  4229          nodeReaderRelease(&reader);
  4230        }
  4231        sqlite3_free(aLeaf);
  4232      }
  4233  
  4234      if( rc==SQLITE_OK && bAppendable ){
  4235        /* It is possible to append to this segment. Set up the IncrmergeWriter
  4236        ** object to do so.  */
  4237        int i;
  4238        int nHeight = (int)aRoot[0];
  4239        NodeWriter *pNode;
  4240  
  4241        pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
  4242        pWriter->iStart = iStart;
  4243        pWriter->iEnd = iEnd;
  4244        pWriter->iAbsLevel = iAbsLevel;
  4245        pWriter->iIdx = iIdx;
  4246  
  4247        for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
  4248          pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
  4249        }
  4250  
  4251        pNode = &pWriter->aNodeWriter[nHeight];
  4252        pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
  4253        blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
  4254        if( rc==SQLITE_OK ){
  4255          memcpy(pNode->block.a, aRoot, nRoot);
  4256          pNode->block.n = nRoot;
  4257        }
  4258  
  4259        for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
  4260          NodeReader reader;
  4261          pNode = &pWriter->aNodeWriter[i];
  4262  
  4263          rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
  4264          while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
  4265          blobGrowBuffer(&pNode->key, reader.term.n, &rc);
  4266          if( rc==SQLITE_OK ){
  4267            memcpy(pNode->key.a, reader.term.a, reader.term.n);
  4268            pNode->key.n = reader.term.n;
  4269            if( i>0 ){
  4270              char *aBlock = 0;
  4271              int nBlock = 0;
  4272              pNode = &pWriter->aNodeWriter[i-1];
  4273              pNode->iBlock = reader.iChild;
  4274              rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
  4275              blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
  4276              if( rc==SQLITE_OK ){
  4277                memcpy(pNode->block.a, aBlock, nBlock);
  4278                pNode->block.n = nBlock;
  4279              }
  4280              sqlite3_free(aBlock);
  4281            }
  4282          }
  4283          nodeReaderRelease(&reader);
  4284        }
  4285      }
  4286  
  4287      rc2 = sqlite3_reset(pSelect);
  4288      if( rc==SQLITE_OK ) rc = rc2;
  4289    }
  4290  
  4291    return rc;
  4292  }
  4293  
  4294  /*
  4295  ** Determine the largest segment index value that exists within absolute
  4296  ** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
  4297  ** one before returning SQLITE_OK. Or, if there are no segments at all 
  4298  ** within level iAbsLevel, set *piIdx to zero.
  4299  **
  4300  ** If an error occurs, return an SQLite error code. The final value of
  4301  ** *piIdx is undefined in this case.
  4302  */
  4303  static int fts3IncrmergeOutputIdx( 
  4304    Fts3Table *p,                   /* FTS Table handle */
  4305    sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
  4306    int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
  4307  ){
  4308    int rc;
  4309    sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
  4310  
  4311    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
  4312    if( rc==SQLITE_OK ){
  4313      sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
  4314      sqlite3_step(pOutputIdx);
  4315      *piIdx = sqlite3_column_int(pOutputIdx, 0);
  4316      rc = sqlite3_reset(pOutputIdx);
  4317    }
  4318  
  4319    return rc;
  4320  }
  4321  
  4322  /* 
  4323  ** Allocate an appendable output segment on absolute level iAbsLevel+1
  4324  ** with idx value iIdx.
  4325  **
  4326  ** In the %_segdir table, a segment is defined by the values in three
  4327  ** columns:
  4328  **
  4329  **     start_block
  4330  **     leaves_end_block
  4331  **     end_block
  4332  **
  4333  ** When an appendable segment is allocated, it is estimated that the
  4334  ** maximum number of leaf blocks that may be required is the sum of the
  4335  ** number of leaf blocks consumed by the input segments, plus the number
  4336  ** of input segments, multiplied by two. This value is stored in stack 
  4337  ** variable nLeafEst.
  4338  **
  4339  ** A total of 16*nLeafEst blocks are allocated when an appendable segment
  4340  ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
  4341  ** array of leaf nodes starts at the first block allocated. The array
  4342  ** of interior nodes that are parents of the leaf nodes start at block
  4343  ** (start_block + (1 + end_block - start_block) / 16). And so on.
  4344  **
  4345  ** In the actual code below, the value "16" is replaced with the 
  4346  ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
  4347  */
  4348  static int fts3IncrmergeWriter( 
  4349    Fts3Table *p,                   /* Fts3 table handle */
  4350    sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
  4351    int iIdx,                       /* Index of new output segment */
  4352    Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
  4353    IncrmergeWriter *pWriter        /* Populate this object */
  4354  ){
  4355    int rc;                         /* Return Code */
  4356    int i;                          /* Iterator variable */
  4357    int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
  4358    sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
  4359    sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
  4360  
  4361    /* Calculate nLeafEst. */
  4362    rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
  4363    if( rc==SQLITE_OK ){
  4364      sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
  4365      sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
  4366      if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
  4367        nLeafEst = sqlite3_column_int(pLeafEst, 0);
  4368      }
  4369      rc = sqlite3_reset(pLeafEst);
  4370    }
  4371    if( rc!=SQLITE_OK ) return rc;
  4372  
  4373    /* Calculate the first block to use in the output segment */
  4374    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
  4375    if( rc==SQLITE_OK ){
  4376      if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
  4377        pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
  4378        pWriter->iEnd = pWriter->iStart - 1;
  4379        pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
  4380      }
  4381      rc = sqlite3_reset(pFirstBlock);
  4382    }
  4383    if( rc!=SQLITE_OK ) return rc;
  4384  
  4385    /* Insert the marker in the %_segments table to make sure nobody tries
  4386    ** to steal the space just allocated. This is also used to identify 
  4387    ** appendable segments.  */
  4388    rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
  4389    if( rc!=SQLITE_OK ) return rc;
  4390  
  4391    pWriter->iAbsLevel = iAbsLevel;
  4392    pWriter->nLeafEst = nLeafEst;
  4393    pWriter->iIdx = iIdx;
  4394  
  4395    /* Set up the array of NodeWriter objects */
  4396    for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
  4397      pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
  4398    }
  4399    return SQLITE_OK;
  4400  }
  4401  
  4402  /*
  4403  ** Remove an entry from the %_segdir table. This involves running the 
  4404  ** following two statements:
  4405  **
  4406  **   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
  4407  **   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
  4408  **
  4409  ** The DELETE statement removes the specific %_segdir level. The UPDATE 
  4410  ** statement ensures that the remaining segments have contiguously allocated
  4411  ** idx values.
  4412  */
  4413  static int fts3RemoveSegdirEntry(
  4414    Fts3Table *p,                   /* FTS3 table handle */
  4415    sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
  4416    int iIdx                        /* Index of %_segdir entry to delete */
  4417  ){
  4418    int rc;                         /* Return code */
  4419    sqlite3_stmt *pDelete = 0;      /* DELETE statement */
  4420  
  4421    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
  4422    if( rc==SQLITE_OK ){
  4423      sqlite3_bind_int64(pDelete, 1, iAbsLevel);
  4424      sqlite3_bind_int(pDelete, 2, iIdx);
  4425      sqlite3_step(pDelete);
  4426      rc = sqlite3_reset(pDelete);
  4427    }
  4428  
  4429    return rc;
  4430  }
  4431  
  4432  /*
  4433  ** One or more segments have just been removed from absolute level iAbsLevel.
  4434  ** Update the 'idx' values of the remaining segments in the level so that
  4435  ** the idx values are a contiguous sequence starting from 0.
  4436  */
  4437  static int fts3RepackSegdirLevel(
  4438    Fts3Table *p,                   /* FTS3 table handle */
  4439    sqlite3_int64 iAbsLevel         /* Absolute level to repack */
  4440  ){
  4441    int rc;                         /* Return code */
  4442    int *aIdx = 0;                  /* Array of remaining idx values */
  4443    int nIdx = 0;                   /* Valid entries in aIdx[] */
  4444    int nAlloc = 0;                 /* Allocated size of aIdx[] */
  4445    int i;                          /* Iterator variable */
  4446    sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
  4447    sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
  4448  
  4449    rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
  4450    if( rc==SQLITE_OK ){
  4451      int rc2;
  4452      sqlite3_bind_int64(pSelect, 1, iAbsLevel);
  4453      while( SQLITE_ROW==sqlite3_step(pSelect) ){
  4454        if( nIdx>=nAlloc ){
  4455          int *aNew;
  4456          nAlloc += 16;
  4457          aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
  4458          if( !aNew ){
  4459            rc = SQLITE_NOMEM;
  4460            break;
  4461          }
  4462          aIdx = aNew;
  4463        }
  4464        aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
  4465      }
  4466      rc2 = sqlite3_reset(pSelect);
  4467      if( rc==SQLITE_OK ) rc = rc2;
  4468    }
  4469  
  4470    if( rc==SQLITE_OK ){
  4471      rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
  4472    }
  4473    if( rc==SQLITE_OK ){
  4474      sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
  4475    }
  4476  
  4477    assert( p->bIgnoreSavepoint==0 );
  4478    p->bIgnoreSavepoint = 1;
  4479    for(i=0; rc==SQLITE_OK && i<nIdx; i++){
  4480      if( aIdx[i]!=i ){
  4481        sqlite3_bind_int(pUpdate, 3, aIdx[i]);
  4482        sqlite3_bind_int(pUpdate, 1, i);
  4483        sqlite3_step(pUpdate);
  4484        rc = sqlite3_reset(pUpdate);
  4485      }
  4486    }
  4487    p->bIgnoreSavepoint = 0;
  4488  
  4489    sqlite3_free(aIdx);
  4490    return rc;
  4491  }
  4492  
  4493  static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
  4494    pNode->a[0] = (char)iHeight;
  4495    if( iChild ){
  4496      assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
  4497      pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
  4498    }else{
  4499      assert( pNode->nAlloc>=1 );
  4500      pNode->n = 1;
  4501    }
  4502  }
  4503  
  4504  /*
  4505  ** The first two arguments are a pointer to and the size of a segment b-tree
  4506  ** node. The node may be a leaf or an internal node.
  4507  **
  4508  ** This function creates a new node image in blob object *pNew by copying
  4509  ** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
  4510  ** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
  4511  */
  4512  static int fts3TruncateNode(
  4513    const char *aNode,              /* Current node image */
  4514    int nNode,                      /* Size of aNode in bytes */
  4515    Blob *pNew,                     /* OUT: Write new node image here */
  4516    const char *zTerm,              /* Omit all terms smaller than this */
  4517    int nTerm,                      /* Size of zTerm in bytes */
  4518    sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
  4519  ){
  4520    NodeReader reader;              /* Reader object */
  4521    Blob prev = {0, 0, 0};          /* Previous term written to new node */
  4522    int rc = SQLITE_OK;             /* Return code */
  4523    int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
  4524  
  4525    /* Allocate required output space */
  4526    blobGrowBuffer(pNew, nNode, &rc);
  4527    if( rc!=SQLITE_OK ) return rc;
  4528    pNew->n = 0;
  4529  
  4530    /* Populate new node buffer */
  4531    for(rc = nodeReaderInit(&reader, aNode, nNode); 
  4532        rc==SQLITE_OK && reader.aNode; 
  4533        rc = nodeReaderNext(&reader)
  4534    ){
  4535      if( pNew->n==0 ){
  4536        int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
  4537        if( res<0 || (bLeaf==0 && res==0) ) continue;
  4538        fts3StartNode(pNew, (int)aNode[0], reader.iChild);
  4539        *piBlock = reader.iChild;
  4540      }
  4541      rc = fts3AppendToNode(
  4542          pNew, &prev, reader.term.a, reader.term.n,
  4543          reader.aDoclist, reader.nDoclist
  4544      );
  4545      if( rc!=SQLITE_OK ) break;
  4546    }
  4547    if( pNew->n==0 ){
  4548      fts3StartNode(pNew, (int)aNode[0], reader.iChild);
  4549      *piBlock = reader.iChild;
  4550    }
  4551    assert( pNew->n<=pNew->nAlloc );
  4552  
  4553    nodeReaderRelease(&reader);
  4554    sqlite3_free(prev.a);
  4555    return rc;
  4556  }
  4557  
  4558  /*
  4559  ** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute 
  4560  ** level iAbsLevel. This may involve deleting entries from the %_segments
  4561  ** table, and modifying existing entries in both the %_segments and %_segdir
  4562  ** tables.
  4563  **
  4564  ** SQLITE_OK is returned if the segment is updated successfully. Or an
  4565  ** SQLite error code otherwise.
  4566  */
  4567  static int fts3TruncateSegment(
  4568    Fts3Table *p,                   /* FTS3 table handle */
  4569    sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
  4570    int iIdx,                       /* Index within level of segment to modify */
  4571    const char *zTerm,              /* Remove terms smaller than this */
  4572    int nTerm                      /* Number of bytes in buffer zTerm */
  4573  ){
  4574    int rc = SQLITE_OK;             /* Return code */
  4575    Blob root = {0,0,0};            /* New root page image */
  4576    Blob block = {0,0,0};           /* Buffer used for any other block */
  4577    sqlite3_int64 iBlock = 0;       /* Block id */
  4578    sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
  4579    sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
  4580    sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
  4581  
  4582    rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
  4583    if( rc==SQLITE_OK ){
  4584      int rc2;                      /* sqlite3_reset() return code */
  4585      sqlite3_bind_int64(pFetch, 1, iAbsLevel);
  4586      sqlite3_bind_int(pFetch, 2, iIdx);
  4587      if( SQLITE_ROW==sqlite3_step(pFetch) ){
  4588        const char *aRoot = sqlite3_column_blob(pFetch, 4);
  4589        int nRoot = sqlite3_column_bytes(pFetch, 4);
  4590        iOldStart = sqlite3_column_int64(pFetch, 1);
  4591        rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
  4592      }
  4593      rc2 = sqlite3_reset(pFetch);
  4594      if( rc==SQLITE_OK ) rc = rc2;
  4595    }
  4596  
  4597    while( rc==SQLITE_OK && iBlock ){
  4598      char *aBlock = 0;
  4599      int nBlock = 0;
  4600      iNewStart = iBlock;
  4601  
  4602      rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
  4603      if( rc==SQLITE_OK ){
  4604        rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
  4605      }
  4606      if( rc==SQLITE_OK ){
  4607        rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
  4608      }
  4609      sqlite3_free(aBlock);
  4610    }
  4611  
  4612    /* Variable iNewStart now contains the first valid leaf node. */
  4613    if( rc==SQLITE_OK && iNewStart ){
  4614      sqlite3_stmt *pDel = 0;
  4615      rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
  4616      if( rc==SQLITE_OK ){
  4617        sqlite3_bind_int64(pDel, 1, iOldStart);
  4618        sqlite3_bind_int64(pDel, 2, iNewStart-1);
  4619        sqlite3_step(pDel);
  4620        rc = sqlite3_reset(pDel);
  4621      }
  4622    }
  4623  
  4624    if( rc==SQLITE_OK ){
  4625      sqlite3_stmt *pChomp = 0;
  4626      rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
  4627      if( rc==SQLITE_OK ){
  4628        sqlite3_bind_int64(pChomp, 1, iNewStart);
  4629        sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
  4630        sqlite3_bind_int64(pChomp, 3, iAbsLevel);
  4631        sqlite3_bind_int(pChomp, 4, iIdx);
  4632        sqlite3_step(pChomp);
  4633        rc = sqlite3_reset(pChomp);
  4634      }
  4635    }
  4636  
  4637    sqlite3_free(root.a);
  4638    sqlite3_free(block.a);
  4639    return rc;
  4640  }
  4641  
  4642  /*
  4643  ** This function is called after an incrmental-merge operation has run to
  4644  ** merge (or partially merge) two or more segments from absolute level
  4645  ** iAbsLevel.
  4646  **
  4647  ** Each input segment is either removed from the db completely (if all of
  4648  ** its data was copied to the output segment by the incrmerge operation)
  4649  ** or modified in place so that it no longer contains those entries that
  4650  ** have been duplicated in the output segment.
  4651  */
  4652  static int fts3IncrmergeChomp(
  4653    Fts3Table *p,                   /* FTS table handle */
  4654    sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
  4655    Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
  4656    int *pnRem                      /* Number of segments not deleted */
  4657  ){
  4658    int i;
  4659    int nRem = 0;
  4660    int rc = SQLITE_OK;
  4661  
  4662    for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
  4663      Fts3SegReader *pSeg = 0;
  4664      int j;
  4665  
  4666      /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
  4667      ** somewhere in the pCsr->apSegment[] array.  */
  4668      for(j=0; ALWAYS(j<pCsr->nSegment); j++){
  4669        pSeg = pCsr->apSegment[j];
  4670        if( pSeg->iIdx==i ) break;
  4671      }
  4672      assert( j<pCsr->nSegment && pSeg->iIdx==i );
  4673  
  4674      if( pSeg->aNode==0 ){
  4675        /* Seg-reader is at EOF. Remove the entire input segment. */
  4676        rc = fts3DeleteSegment(p, pSeg);
  4677        if( rc==SQLITE_OK ){
  4678          rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
  4679        }
  4680        *pnRem = 0;
  4681      }else{
  4682        /* The incremental merge did not copy all the data from this 
  4683        ** segment to the upper level. The segment is modified in place
  4684        ** so that it contains no keys smaller than zTerm/nTerm. */ 
  4685        const char *zTerm = pSeg->zTerm;
  4686        int nTerm = pSeg->nTerm;
  4687        rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
  4688        nRem++;
  4689      }
  4690    }
  4691  
  4692    if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
  4693      rc = fts3RepackSegdirLevel(p, iAbsLevel);
  4694    }
  4695  
  4696    *pnRem = nRem;
  4697    return rc;
  4698  }
  4699  
  4700  /*
  4701  ** Store an incr-merge hint in the database.
  4702  */
  4703  static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
  4704    sqlite3_stmt *pReplace = 0;
  4705    int rc;                         /* Return code */
  4706  
  4707    rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
  4708    if( rc==SQLITE_OK ){
  4709      sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
  4710      sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
  4711      sqlite3_step(pReplace);
  4712      rc = sqlite3_reset(pReplace);
  4713    }
  4714  
  4715    return rc;
  4716  }
  4717  
  4718  /*
  4719  ** Load an incr-merge hint from the database. The incr-merge hint, if one 
  4720  ** exists, is stored in the rowid==1 row of the %_stat table.
  4721  **
  4722  ** If successful, populate blob *pHint with the value read from the %_stat
  4723  ** table and return SQLITE_OK. Otherwise, if an error occurs, return an
  4724  ** SQLite error code.
  4725  */
  4726  static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
  4727    sqlite3_stmt *pSelect = 0;
  4728    int rc;
  4729  
  4730    pHint->n = 0;
  4731    rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
  4732    if( rc==SQLITE_OK ){
  4733      int rc2;
  4734      sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
  4735      if( SQLITE_ROW==sqlite3_step(pSelect) ){
  4736        const char *aHint = sqlite3_column_blob(pSelect, 0);
  4737        int nHint = sqlite3_column_bytes(pSelect, 0);
  4738        if( aHint ){
  4739          blobGrowBuffer(pHint, nHint, &rc);
  4740          if( rc==SQLITE_OK ){
  4741            memcpy(pHint->a, aHint, nHint);
  4742            pHint->n = nHint;
  4743          }
  4744        }
  4745      }
  4746      rc2 = sqlite3_reset(pSelect);
  4747      if( rc==SQLITE_OK ) rc = rc2;
  4748    }
  4749  
  4750    return rc;
  4751  }
  4752  
  4753  /*
  4754  ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  4755  ** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
  4756  ** consists of two varints, the absolute level number of the input segments 
  4757  ** and the number of input segments.
  4758  **
  4759  ** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
  4760  ** set *pRc to an SQLite error code before returning.
  4761  */
  4762  static void fts3IncrmergeHintPush(
  4763    Blob *pHint,                    /* Hint blob to append to */
  4764    i64 iAbsLevel,                  /* First varint to store in hint */
  4765    int nInput,                     /* Second varint to store in hint */
  4766    int *pRc                        /* IN/OUT: Error code */
  4767  ){
  4768    blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
  4769    if( *pRc==SQLITE_OK ){
  4770      pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
  4771      pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
  4772    }
  4773  }
  4774  
  4775  /*
  4776  ** Read the last entry (most recently pushed) from the hint blob *pHint
  4777  ** and then remove the entry. Write the two values read to *piAbsLevel and 
  4778  ** *pnInput before returning.
  4779  **
  4780  ** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
  4781  ** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
  4782  */
  4783  static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
  4784    const int nHint = pHint->n;
  4785    int i;
  4786  
  4787    i = pHint->n-2;
  4788    while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
  4789    while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
  4790  
  4791    pHint->n = i;
  4792    i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
  4793    i += fts3GetVarint32(&pHint->a[i], pnInput);
  4794    if( i!=nHint ) return FTS_CORRUPT_VTAB;
  4795  
  4796    return SQLITE_OK;
  4797  }
  4798  
  4799  
  4800  /*
  4801  ** Attempt an incremental merge that writes nMerge leaf blocks.
  4802  **
  4803  ** Incremental merges happen nMin segments at a time. The segments 
  4804  ** to be merged are the nMin oldest segments (the ones with the smallest 
  4805  ** values for the _segdir.idx field) in the highest level that contains 
  4806  ** at least nMin segments. Multiple merges might occur in an attempt to 
  4807  ** write the quota of nMerge leaf blocks.
  4808  */
  4809  int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
  4810    int rc;                         /* Return code */
  4811    int nRem = nMerge;              /* Number of leaf pages yet to  be written */
  4812    Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
  4813    Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
  4814    IncrmergeWriter *pWriter;       /* Writer object */
  4815    int nSeg = 0;                   /* Number of input segments */
  4816    sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
  4817    Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
  4818    int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
  4819  
  4820    /* Allocate space for the cursor, filter and writer objects */
  4821    const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
  4822    pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
  4823    if( !pWriter ) return SQLITE_NOMEM;
  4824    pFilter = (Fts3SegFilter *)&pWriter[1];
  4825    pCsr = (Fts3MultiSegReader *)&pFilter[1];
  4826  
  4827    rc = fts3IncrmergeHintLoad(p, &hint);
  4828    while( rc==SQLITE_OK && nRem>0 ){
  4829      const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
  4830      sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
  4831      int bUseHint = 0;             /* True if attempting to append */
  4832      int iIdx = 0;                 /* Largest idx in level (iAbsLevel+1) */
  4833  
  4834      /* Search the %_segdir table for the absolute level with the smallest
  4835      ** relative level number that contains at least nMin segments, if any.
  4836      ** If one is found, set iAbsLevel to the absolute level number and
  4837      ** nSeg to nMin. If no level with at least nMin segments can be found, 
  4838      ** set nSeg to -1.
  4839      */
  4840      rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
  4841      sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin));
  4842      if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
  4843        iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
  4844        nSeg = sqlite3_column_int(pFindLevel, 1);
  4845        assert( nSeg>=2 );
  4846      }else{
  4847        nSeg = -1;
  4848      }
  4849      rc = sqlite3_reset(pFindLevel);
  4850  
  4851      /* If the hint read from the %_stat table is not empty, check if the
  4852      ** last entry in it specifies a relative level smaller than or equal
  4853      ** to the level identified by the block above (if any). If so, this 
  4854      ** iteration of the loop will work on merging at the hinted level.
  4855      */
  4856      if( rc==SQLITE_OK && hint.n ){
  4857        int nHint = hint.n;
  4858        sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
  4859        int nHintSeg = 0;                     /* Hint number of segments */
  4860  
  4861        rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
  4862        if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
  4863          iAbsLevel = iHintAbsLevel;
  4864          nSeg = nHintSeg;
  4865          bUseHint = 1;
  4866          bDirtyHint = 1;
  4867        }else{
  4868          /* This undoes the effect of the HintPop() above - so that no entry
  4869          ** is removed from the hint blob.  */
  4870          hint.n = nHint;
  4871        }
  4872      }
  4873  
  4874      /* If nSeg is less that zero, then there is no level with at least
  4875      ** nMin segments and no hint in the %_stat table. No work to do.
  4876      ** Exit early in this case.  */
  4877      if( nSeg<0 ) break;
  4878  
  4879      /* Open a cursor to iterate through the contents of the oldest nSeg 
  4880      ** indexes of absolute level iAbsLevel. If this cursor is opened using 
  4881      ** the 'hint' parameters, it is possible that there are less than nSeg
  4882      ** segments available in level iAbsLevel. In this case, no work is
  4883      ** done on iAbsLevel - fall through to the next iteration of the loop 
  4884      ** to start work on some other level.  */
  4885      memset(pWriter, 0, nAlloc);
  4886      pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
  4887  
  4888      if( rc==SQLITE_OK ){
  4889        rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
  4890        assert( bUseHint==1 || bUseHint==0 );
  4891        if( iIdx==0 || (bUseHint && iIdx==1) ){
  4892          int bIgnore = 0;
  4893          rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
  4894          if( bIgnore ){
  4895            pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
  4896          }
  4897        }
  4898      }
  4899  
  4900      if( rc==SQLITE_OK ){
  4901        rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
  4902      }
  4903      if( SQLITE_OK==rc && pCsr->nSegment==nSeg
  4904       && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
  4905       && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
  4906      ){
  4907        if( bUseHint && iIdx>0 ){
  4908          const char *zKey = pCsr->zTerm;
  4909          int nKey = pCsr->nTerm;
  4910          rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
  4911        }else{
  4912          rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
  4913        }
  4914  
  4915        if( rc==SQLITE_OK && pWriter->nLeafEst ){
  4916          fts3LogMerge(nSeg, iAbsLevel);
  4917          do {
  4918            rc = fts3IncrmergeAppend(p, pWriter, pCsr);
  4919            if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
  4920            if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
  4921          }while( rc==SQLITE_ROW );
  4922  
  4923          /* Update or delete the input segments */
  4924          if( rc==SQLITE_OK ){
  4925            nRem -= (1 + pWriter->nWork);
  4926            rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
  4927            if( nSeg!=0 ){
  4928              bDirtyHint = 1;
  4929              fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
  4930            }
  4931          }
  4932        }
  4933  
  4934        if( nSeg!=0 ){
  4935          pWriter->nLeafData = pWriter->nLeafData * -1;
  4936        }
  4937        fts3IncrmergeRelease(p, pWriter, &rc);
  4938        if( nSeg==0 && pWriter->bNoLeafData==0 ){
  4939          fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
  4940        }
  4941      }
  4942  
  4943      sqlite3Fts3SegReaderFinish(pCsr);
  4944    }
  4945  
  4946    /* Write the hint values into the %_stat table for the next incr-merger */
  4947    if( bDirtyHint && rc==SQLITE_OK ){
  4948      rc = fts3IncrmergeHintStore(p, &hint);
  4949    }
  4950  
  4951    sqlite3_free(pWriter);
  4952    sqlite3_free(hint.a);
  4953    return rc;
  4954  }
  4955  
  4956  /*
  4957  ** Convert the text beginning at *pz into an integer and return
  4958  ** its value.  Advance *pz to point to the first character past
  4959  ** the integer.
  4960  **
  4961  ** This function used for parameters to merge= and incrmerge=
  4962  ** commands. 
  4963  */
  4964  static int fts3Getint(const char **pz){
  4965    const char *z = *pz;
  4966    int i = 0;
  4967    while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
  4968    *pz = z;
  4969    return i;
  4970  }
  4971  
  4972  /*
  4973  ** Process statements of the form:
  4974  **
  4975  **    INSERT INTO table(table) VALUES('merge=A,B');
  4976  **
  4977  ** A and B are integers that decode to be the number of leaf pages
  4978  ** written for the merge, and the minimum number of segments on a level
  4979  ** before it will be selected for a merge, respectively.
  4980  */
  4981  static int fts3DoIncrmerge(
  4982    Fts3Table *p,                   /* FTS3 table handle */
  4983    const char *zParam              /* Nul-terminated string containing "A,B" */
  4984  ){
  4985    int rc;
  4986    int nMin = (FTS3_MERGE_COUNT / 2);
  4987    int nMerge = 0;
  4988    const char *z = zParam;
  4989  
  4990    /* Read the first integer value */
  4991    nMerge = fts3Getint(&z);
  4992  
  4993    /* If the first integer value is followed by a ',',  read the second
  4994    ** integer value. */
  4995    if( z[0]==',' && z[1]!='\0' ){
  4996      z++;
  4997      nMin = fts3Getint(&z);
  4998    }
  4999  
  5000    if( z[0]!='\0' || nMin<2 ){
  5001      rc = SQLITE_ERROR;
  5002    }else{
  5003      rc = SQLITE_OK;
  5004      if( !p->bHasStat ){
  5005        assert( p->bFts4==0 );
  5006        sqlite3Fts3CreateStatTable(&rc, p);
  5007      }
  5008      if( rc==SQLITE_OK ){
  5009        rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
  5010      }
  5011      sqlite3Fts3SegmentsClose(p);
  5012    }
  5013    return rc;
  5014  }
  5015  
  5016  /*
  5017  ** Process statements of the form:
  5018  **
  5019  **    INSERT INTO table(table) VALUES('automerge=X');
  5020  **
  5021  ** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
  5022  ** turn it on.  The setting is persistent.
  5023  */
  5024  static int fts3DoAutoincrmerge(
  5025    Fts3Table *p,                   /* FTS3 table handle */
  5026    const char *zParam              /* Nul-terminated string containing boolean */
  5027  ){
  5028    int rc = SQLITE_OK;
  5029    sqlite3_stmt *pStmt = 0;
  5030    p->nAutoincrmerge = fts3Getint(&zParam);
  5031    if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
  5032      p->nAutoincrmerge = 8;
  5033    }
  5034    if( !p->bHasStat ){
  5035      assert( p->bFts4==0 );
  5036      sqlite3Fts3CreateStatTable(&rc, p);
  5037      if( rc ) return rc;
  5038    }
  5039    rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
  5040    if( rc ) return rc;
  5041    sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
  5042    sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
  5043    sqlite3_step(pStmt);
  5044    rc = sqlite3_reset(pStmt);
  5045    return rc;
  5046  }
  5047  
  5048  /*
  5049  ** Return a 64-bit checksum for the FTS index entry specified by the
  5050  ** arguments to this function.
  5051  */
  5052  static u64 fts3ChecksumEntry(
  5053    const char *zTerm,              /* Pointer to buffer containing term */
  5054    int nTerm,                      /* Size of zTerm in bytes */
  5055    int iLangid,                    /* Language id for current row */
  5056    int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
  5057    i64 iDocid,                     /* Docid for current row. */
  5058    int iCol,                       /* Column number */
  5059    int iPos                        /* Position */
  5060  ){
  5061    int i;
  5062    u64 ret = (u64)iDocid;
  5063  
  5064    ret += (ret<<3) + iLangid;
  5065    ret += (ret<<3) + iIndex;
  5066    ret += (ret<<3) + iCol;
  5067    ret += (ret<<3) + iPos;
  5068    for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
  5069  
  5070    return ret;
  5071  }
  5072  
  5073  /*
  5074  ** Return a checksum of all entries in the FTS index that correspond to
  5075  ** language id iLangid. The checksum is calculated by XORing the checksums
  5076  ** of each individual entry (see fts3ChecksumEntry()) together.
  5077  **
  5078  ** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
  5079  ** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
  5080  ** return value is undefined in this case.
  5081  */
  5082  static u64 fts3ChecksumIndex(
  5083    Fts3Table *p,                   /* FTS3 table handle */
  5084    int iLangid,                    /* Language id to return cksum for */
  5085    int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
  5086    int *pRc                        /* OUT: Return code */
  5087  ){
  5088    Fts3SegFilter filter;
  5089    Fts3MultiSegReader csr;
  5090    int rc;
  5091    u64 cksum = 0;
  5092  
  5093    assert( *pRc==SQLITE_OK );
  5094  
  5095    memset(&filter, 0, sizeof(filter));
  5096    memset(&csr, 0, sizeof(csr));
  5097    filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
  5098    filter.flags |= FTS3_SEGMENT_SCAN;
  5099  
  5100    rc = sqlite3Fts3SegReaderCursor(
  5101        p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
  5102    );
  5103    if( rc==SQLITE_OK ){
  5104      rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
  5105    }
  5106  
  5107    if( rc==SQLITE_OK ){
  5108      while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
  5109        char *pCsr = csr.aDoclist;
  5110        char *pEnd = &pCsr[csr.nDoclist];
  5111  
  5112        i64 iDocid = 0;
  5113        i64 iCol = 0;
  5114        i64 iPos = 0;
  5115  
  5116        pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
  5117        while( pCsr<pEnd ){
  5118          i64 iVal = 0;
  5119          pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
  5120          if( pCsr<pEnd ){
  5121            if( iVal==0 || iVal==1 ){
  5122              iCol = 0;
  5123              iPos = 0;
  5124              if( iVal ){
  5125                pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
  5126              }else{
  5127                pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
  5128                iDocid += iVal;
  5129              }
  5130            }else{
  5131              iPos += (iVal - 2);
  5132              cksum = cksum ^ fts3ChecksumEntry(
  5133                  csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
  5134                  (int)iCol, (int)iPos
  5135              );
  5136            }
  5137          }
  5138        }
  5139      }
  5140    }
  5141    sqlite3Fts3SegReaderFinish(&csr);
  5142  
  5143    *pRc = rc;
  5144    return cksum;
  5145  }
  5146  
  5147  /*
  5148  ** Check if the contents of the FTS index match the current contents of the
  5149  ** content table. If no error occurs and the contents do match, set *pbOk
  5150  ** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
  5151  ** to false before returning.
  5152  **
  5153  ** If an error occurs (e.g. an OOM or IO error), return an SQLite error 
  5154  ** code. The final value of *pbOk is undefined in this case.
  5155  */
  5156  static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
  5157    int rc = SQLITE_OK;             /* Return code */
  5158    u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
  5159    u64 cksum2 = 0;                 /* Checksum based on %_content contents */
  5160    sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
  5161  
  5162    /* This block calculates the checksum according to the FTS index. */
  5163    rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
  5164    if( rc==SQLITE_OK ){
  5165      int rc2;
  5166      sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
  5167      sqlite3_bind_int(pAllLangid, 2, p->nIndex);
  5168      while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
  5169        int iLangid = sqlite3_column_int(pAllLangid, 0);
  5170        int i;
  5171        for(i=0; i<p->nIndex; i++){
  5172          cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
  5173        }
  5174      }
  5175      rc2 = sqlite3_reset(pAllLangid);
  5176      if( rc==SQLITE_OK ) rc = rc2;
  5177    }
  5178  
  5179    /* This block calculates the checksum according to the %_content table */
  5180    if( rc==SQLITE_OK ){
  5181      sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
  5182      sqlite3_stmt *pStmt = 0;
  5183      char *zSql;
  5184     
  5185      zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
  5186      if( !zSql ){
  5187        rc = SQLITE_NOMEM;
  5188      }else{
  5189        rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  5190        sqlite3_free(zSql);
  5191      }
  5192  
  5193      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  5194        i64 iDocid = sqlite3_column_int64(pStmt, 0);
  5195        int iLang = langidFromSelect(p, pStmt);
  5196        int iCol;
  5197  
  5198        for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
  5199          if( p->abNotindexed[iCol]==0 ){
  5200            const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
  5201            int nText = sqlite3_column_bytes(pStmt, iCol+1);
  5202            sqlite3_tokenizer_cursor *pT = 0;
  5203  
  5204            rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
  5205            while( rc==SQLITE_OK ){
  5206              char const *zToken;       /* Buffer containing token */
  5207              int nToken = 0;           /* Number of bytes in token */
  5208              int iDum1 = 0, iDum2 = 0; /* Dummy variables */
  5209              int iPos = 0;             /* Position of token in zText */
  5210  
  5211              rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
  5212              if( rc==SQLITE_OK ){
  5213                int i;
  5214                cksum2 = cksum2 ^ fts3ChecksumEntry(
  5215                    zToken, nToken, iLang, 0, iDocid, iCol, iPos
  5216                );
  5217                for(i=1; i<p->nIndex; i++){
  5218                  if( p->aIndex[i].nPrefix<=nToken ){
  5219                    cksum2 = cksum2 ^ fts3ChecksumEntry(
  5220                        zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
  5221                    );
  5222                  }
  5223                }
  5224              }
  5225            }
  5226            if( pT ) pModule->xClose(pT);
  5227            if( rc==SQLITE_DONE ) rc = SQLITE_OK;
  5228          }
  5229        }
  5230      }
  5231  
  5232      sqlite3_finalize(pStmt);
  5233    }
  5234  
  5235    *pbOk = (cksum1==cksum2);
  5236    return rc;
  5237  }
  5238  
  5239  /*
  5240  ** Run the integrity-check. If no error occurs and the current contents of
  5241  ** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
  5242  ** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
  5243  **
  5244  ** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite 
  5245  ** error code.
  5246  **
  5247  ** The integrity-check works as follows. For each token and indexed token
  5248  ** prefix in the document set, a 64-bit checksum is calculated (by code
  5249  ** in fts3ChecksumEntry()) based on the following:
  5250  **
  5251  **     + The index number (0 for the main index, 1 for the first prefix
  5252  **       index etc.),
  5253  **     + The token (or token prefix) text itself, 
  5254  **     + The language-id of the row it appears in,
  5255  **     + The docid of the row it appears in,
  5256  **     + The column it appears in, and
  5257  **     + The tokens position within that column.
  5258  **
  5259  ** The checksums for all entries in the index are XORed together to create
  5260  ** a single checksum for the entire index.
  5261  **
  5262  ** The integrity-check code calculates the same checksum in two ways:
  5263  **
  5264  **     1. By scanning the contents of the FTS index, and 
  5265  **     2. By scanning and tokenizing the content table.
  5266  **
  5267  ** If the two checksums are identical, the integrity-check is deemed to have
  5268  ** passed.
  5269  */
  5270  static int fts3DoIntegrityCheck(
  5271    Fts3Table *p                    /* FTS3 table handle */
  5272  ){
  5273    int rc;
  5274    int bOk = 0;
  5275    rc = fts3IntegrityCheck(p, &bOk);
  5276    if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
  5277    return rc;
  5278  }
  5279  
  5280  /*
  5281  ** Handle a 'special' INSERT of the form:
  5282  **
  5283  **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
  5284  **
  5285  ** Argument pVal contains the result of <expr>. Currently the only 
  5286  ** meaningful value to insert is the text 'optimize'.
  5287  */
  5288  static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
  5289    int rc;                         /* Return Code */
  5290    const char *zVal = (const char *)sqlite3_value_text(pVal);
  5291    int nVal = sqlite3_value_bytes(pVal);
  5292  
  5293    if( !zVal ){
  5294      return SQLITE_NOMEM;
  5295    }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
  5296      rc = fts3DoOptimize(p, 0);
  5297    }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
  5298      rc = fts3DoRebuild(p);
  5299    }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
  5300      rc = fts3DoIntegrityCheck(p);
  5301    }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
  5302      rc = fts3DoIncrmerge(p, &zVal[6]);
  5303    }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
  5304      rc = fts3DoAutoincrmerge(p, &zVal[10]);
  5305  #ifdef SQLITE_TEST
  5306    }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
  5307      p->nNodeSize = atoi(&zVal[9]);
  5308      rc = SQLITE_OK;
  5309    }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
  5310      p->nMaxPendingData = atoi(&zVal[11]);
  5311      rc = SQLITE_OK;
  5312    }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
  5313      p->bNoIncrDoclist = atoi(&zVal[21]);
  5314      rc = SQLITE_OK;
  5315  #endif
  5316    }else{
  5317      rc = SQLITE_ERROR;
  5318    }
  5319  
  5320    return rc;
  5321  }
  5322  
  5323  #ifndef SQLITE_DISABLE_FTS4_DEFERRED
  5324  /*
  5325  ** Delete all cached deferred doclists. Deferred doclists are cached
  5326  ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
  5327  */
  5328  void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
  5329    Fts3DeferredToken *pDef;
  5330    for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
  5331      fts3PendingListDelete(pDef->pList);
  5332      pDef->pList = 0;
  5333    }
  5334  }
  5335  
  5336  /*
  5337  ** Free all entries in the pCsr->pDeffered list. Entries are added to 
  5338  ** this list using sqlite3Fts3DeferToken().
  5339  */
  5340  void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
  5341    Fts3DeferredToken *pDef;
  5342    Fts3DeferredToken *pNext;
  5343    for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
  5344      pNext = pDef->pNext;
  5345      fts3PendingListDelete(pDef->pList);
  5346      sqlite3_free(pDef);
  5347    }
  5348    pCsr->pDeferred = 0;
  5349  }
  5350  
  5351  /*
  5352  ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
  5353  ** based on the row that pCsr currently points to.
  5354  **
  5355  ** A deferred-doclist is like any other doclist with position information
  5356  ** included, except that it only contains entries for a single row of the
  5357  ** table, not for all rows.
  5358  */
  5359  int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
  5360    int rc = SQLITE_OK;             /* Return code */
  5361    if( pCsr->pDeferred ){
  5362      int i;                        /* Used to iterate through table columns */
  5363      sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
  5364      Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
  5365    
  5366      Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  5367      sqlite3_tokenizer *pT = p->pTokenizer;
  5368      sqlite3_tokenizer_module const *pModule = pT->pModule;
  5369     
  5370      assert( pCsr->isRequireSeek==0 );
  5371      iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
  5372    
  5373      for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
  5374        if( p->abNotindexed[i]==0 ){
  5375          const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
  5376          sqlite3_tokenizer_cursor *pTC = 0;
  5377  
  5378          rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
  5379          while( rc==SQLITE_OK ){
  5380            char const *zToken;       /* Buffer containing token */
  5381            int nToken = 0;           /* Number of bytes in token */
  5382            int iDum1 = 0, iDum2 = 0; /* Dummy variables */
  5383            int iPos = 0;             /* Position of token in zText */
  5384  
  5385            rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
  5386            for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
  5387              Fts3PhraseToken *pPT = pDef->pToken;
  5388              if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
  5389                  && (pPT->bFirst==0 || iPos==0)
  5390                  && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
  5391                  && (0==memcmp(zToken, pPT->z, pPT->n))
  5392                ){
  5393                fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
  5394              }
  5395            }
  5396          }
  5397          if( pTC ) pModule->xClose(pTC);
  5398          if( rc==SQLITE_DONE ) rc = SQLITE_OK;
  5399        }
  5400      }
  5401  
  5402      for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
  5403        if( pDef->pList ){
  5404          rc = fts3PendingListAppendVarint(&pDef->pList, 0);
  5405        }
  5406      }
  5407    }
  5408  
  5409    return rc;
  5410  }
  5411  
  5412  int sqlite3Fts3DeferredTokenList(
  5413    Fts3DeferredToken *p, 
  5414    char **ppData, 
  5415    int *pnData
  5416  ){
  5417    char *pRet;
  5418    int nSkip;
  5419    sqlite3_int64 dummy;
  5420  
  5421    *ppData = 0;
  5422    *pnData = 0;
  5423  
  5424    if( p->pList==0 ){
  5425      return SQLITE_OK;
  5426    }
  5427  
  5428    pRet = (char *)sqlite3_malloc(p->pList->nData);
  5429    if( !pRet ) return SQLITE_NOMEM;
  5430  
  5431    nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
  5432    *pnData = p->pList->nData - nSkip;
  5433    *ppData = pRet;
  5434    
  5435    memcpy(pRet, &p->pList->aData[nSkip], *pnData);
  5436    return SQLITE_OK;
  5437  }
  5438  
  5439  /*
  5440  ** Add an entry for token pToken to the pCsr->pDeferred list.
  5441  */
  5442  int sqlite3Fts3DeferToken(
  5443    Fts3Cursor *pCsr,               /* Fts3 table cursor */
  5444    Fts3PhraseToken *pToken,        /* Token to defer */
  5445    int iCol                        /* Column that token must appear in (or -1) */
  5446  ){
  5447    Fts3DeferredToken *pDeferred;
  5448    pDeferred = sqlite3_malloc(sizeof(*pDeferred));
  5449    if( !pDeferred ){
  5450      return SQLITE_NOMEM;
  5451    }
  5452    memset(pDeferred, 0, sizeof(*pDeferred));
  5453    pDeferred->pToken = pToken;
  5454    pDeferred->pNext = pCsr->pDeferred; 
  5455    pDeferred->iCol = iCol;
  5456    pCsr->pDeferred = pDeferred;
  5457  
  5458    assert( pToken->pDeferred==0 );
  5459    pToken->pDeferred = pDeferred;
  5460  
  5461    return SQLITE_OK;
  5462  }
  5463  #endif
  5464  
  5465  /*
  5466  ** SQLite value pRowid contains the rowid of a row that may or may not be
  5467  ** present in the FTS3 table. If it is, delete it and adjust the contents
  5468  ** of subsiduary data structures accordingly.
  5469  */
  5470  static int fts3DeleteByRowid(
  5471    Fts3Table *p, 
  5472    sqlite3_value *pRowid, 
  5473    int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
  5474    u32 *aSzDel
  5475  ){
  5476    int rc = SQLITE_OK;             /* Return code */
  5477    int bFound = 0;                 /* True if *pRowid really is in the table */
  5478  
  5479    fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
  5480    if( bFound && rc==SQLITE_OK ){
  5481      int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
  5482      rc = fts3IsEmpty(p, pRowid, &isEmpty);
  5483      if( rc==SQLITE_OK ){
  5484        if( isEmpty ){
  5485          /* Deleting this row means the whole table is empty. In this case
  5486          ** delete the contents of all three tables and throw away any
  5487          ** data in the pendingTerms hash table.  */
  5488          rc = fts3DeleteAll(p, 1);
  5489          *pnChng = 0;
  5490          memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
  5491        }else{
  5492          *pnChng = *pnChng - 1;
  5493          if( p->zContentTbl==0 ){
  5494            fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
  5495          }
  5496          if( p->bHasDocsize ){
  5497            fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
  5498          }
  5499        }
  5500      }
  5501    }
  5502  
  5503    return rc;
  5504  }
  5505  
  5506  /*
  5507  ** This function does the work for the xUpdate method of FTS3 virtual
  5508  ** tables. The schema of the virtual table being:
  5509  **
  5510  **     CREATE TABLE <table name>( 
  5511  **       <user columns>,
  5512  **       <table name> HIDDEN, 
  5513  **       docid HIDDEN, 
  5514  **       <langid> HIDDEN
  5515  **     );
  5516  **
  5517  ** 
  5518  */
  5519  int sqlite3Fts3UpdateMethod(
  5520    sqlite3_vtab *pVtab,            /* FTS3 vtab object */
  5521    int nArg,                       /* Size of argument array */
  5522    sqlite3_value **apVal,          /* Array of arguments */
  5523    sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
  5524  ){
  5525    Fts3Table *p = (Fts3Table *)pVtab;
  5526    int rc = SQLITE_OK;             /* Return Code */
  5527    int isRemove = 0;               /* True for an UPDATE or DELETE */
  5528    u32 *aSzIns = 0;                /* Sizes of inserted documents */
  5529    u32 *aSzDel = 0;                /* Sizes of deleted documents */
  5530    int nChng = 0;                  /* Net change in number of documents */
  5531    int bInsertDone = 0;
  5532  
  5533    /* At this point it must be known if the %_stat table exists or not.
  5534    ** So bHasStat may not be 2.  */
  5535    assert( p->bHasStat==0 || p->bHasStat==1 );
  5536  
  5537    assert( p->pSegments==0 );
  5538    assert( 
  5539        nArg==1                     /* DELETE operations */
  5540     || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
  5541    );
  5542  
  5543    /* Check for a "special" INSERT operation. One of the form:
  5544    **
  5545    **   INSERT INTO xyz(xyz) VALUES('command');
  5546    */
  5547    if( nArg>1 
  5548     && sqlite3_value_type(apVal[0])==SQLITE_NULL 
  5549     && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL 
  5550    ){
  5551      rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
  5552      goto update_out;
  5553    }
  5554  
  5555    if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
  5556      rc = SQLITE_CONSTRAINT;
  5557      goto update_out;
  5558    }
  5559  
  5560    /* Allocate space to hold the change in document sizes */
  5561    aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
  5562    if( aSzDel==0 ){
  5563      rc = SQLITE_NOMEM;
  5564      goto update_out;
  5565    }
  5566    aSzIns = &aSzDel[p->nColumn+1];
  5567    memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
  5568  
  5569    rc = fts3Writelock(p);
  5570    if( rc!=SQLITE_OK ) goto update_out;
  5571  
  5572    /* If this is an INSERT operation, or an UPDATE that modifies the rowid
  5573    ** value, then this operation requires constraint handling.
  5574    **
  5575    ** If the on-conflict mode is REPLACE, this means that the existing row
  5576    ** should be deleted from the database before inserting the new row. Or,
  5577    ** if the on-conflict mode is other than REPLACE, then this method must
  5578    ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
  5579    ** modify the database file.
  5580    */
  5581    if( nArg>1 && p->zContentTbl==0 ){
  5582      /* Find the value object that holds the new rowid value. */
  5583      sqlite3_value *pNewRowid = apVal[3+p->nColumn];
  5584      if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
  5585        pNewRowid = apVal[1];
  5586      }
  5587  
  5588      if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && ( 
  5589          sqlite3_value_type(apVal[0])==SQLITE_NULL
  5590       || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
  5591      )){
  5592        /* The new rowid is not NULL (in this case the rowid will be
  5593        ** automatically assigned and there is no chance of a conflict), and 
  5594        ** the statement is either an INSERT or an UPDATE that modifies the
  5595        ** rowid column. So if the conflict mode is REPLACE, then delete any
  5596        ** existing row with rowid=pNewRowid. 
  5597        **
  5598        ** Or, if the conflict mode is not REPLACE, insert the new record into 
  5599        ** the %_content table. If we hit the duplicate rowid constraint (or any
  5600        ** other error) while doing so, return immediately.
  5601        **
  5602        ** This branch may also run if pNewRowid contains a value that cannot
  5603        ** be losslessly converted to an integer. In this case, the eventual 
  5604        ** call to fts3InsertData() (either just below or further on in this
  5605        ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is 
  5606        ** invoked, it will delete zero rows (since no row will have
  5607        ** docid=$pNewRowid if $pNewRowid is not an integer value).
  5608        */
  5609        if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
  5610          rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
  5611        }else{
  5612          rc = fts3InsertData(p, apVal, pRowid);
  5613          bInsertDone = 1;
  5614        }
  5615      }
  5616    }
  5617    if( rc!=SQLITE_OK ){
  5618      goto update_out;
  5619    }
  5620  
  5621    /* If this is a DELETE or UPDATE operation, remove the old record. */
  5622    if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  5623      assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
  5624      rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
  5625      isRemove = 1;
  5626    }
  5627    
  5628    /* If this is an INSERT or UPDATE operation, insert the new record. */
  5629    if( nArg>1 && rc==SQLITE_OK ){
  5630      int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
  5631      if( bInsertDone==0 ){
  5632        rc = fts3InsertData(p, apVal, pRowid);
  5633        if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
  5634          rc = FTS_CORRUPT_VTAB;
  5635        }
  5636      }
  5637      if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
  5638        rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
  5639      }
  5640      if( rc==SQLITE_OK ){
  5641        assert( p->iPrevDocid==*pRowid );
  5642        rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
  5643      }
  5644      if( p->bHasDocsize ){
  5645        fts3InsertDocsize(&rc, p, aSzIns);
  5646      }
  5647      nChng++;
  5648    }
  5649  
  5650    if( p->bFts4 ){
  5651      fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
  5652    }
  5653  
  5654   update_out:
  5655    sqlite3_free(aSzDel);
  5656    sqlite3Fts3SegmentsClose(p);
  5657    return rc;
  5658  }
  5659  
  5660  /* 
  5661  ** Flush any data in the pending-terms hash table to disk. If successful,
  5662  ** merge all segments in the database (including the new segment, if 
  5663  ** there was any data to flush) into a single segment. 
  5664  */
  5665  int sqlite3Fts3Optimize(Fts3Table *p){
  5666    int rc;
  5667    rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
  5668    if( rc==SQLITE_OK ){
  5669      rc = fts3DoOptimize(p, 1);
  5670      if( rc==SQLITE_OK || rc==SQLITE_DONE ){
  5671        int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
  5672        if( rc2!=SQLITE_OK ) rc = rc2;
  5673      }else{
  5674        sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
  5675        sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
  5676      }
  5677    }
  5678    sqlite3Fts3SegmentsClose(p);
  5679    return rc;
  5680  }
  5681  
  5682  #endif