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