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