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