Escape non-ASCII character from an ICU extension comment.
[sqlite.git] / ext / fts3 / fts3_write.c
blob3ff481b0b081cdc70a18ee89b70cbb0a6189941e
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.
11 ******************************************************************************
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.
20 #include "fts3Int.h"
21 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
23 #include <string.h>
24 #include <assert.h>
25 #include <stdlib.h>
28 #define FTS_MAX_APPENDABLE_HEIGHT 16
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.
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.
40 #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
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).
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.
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.
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
70 ** The two values that may be meaningfully bound to the :1 parameter in
71 ** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
73 #define FTS_STAT_DOCTOTAL 0
74 #define FTS_STAT_INCRMERGEHINT 1
75 #define FTS_STAT_AUTOINCRMERGE 2
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.
83 #ifdef FTS3_LOG_MERGES
84 static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
85 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
87 #else
88 #define fts3LogMerge(x, y)
89 #endif
92 typedef struct PendingList PendingList;
93 typedef struct SegmentNode SegmentNode;
94 typedef struct SegmentWriter SegmentWriter;
97 ** An instance of the following data structure is used to build doclists
98 ** incrementally. See function fts3PendingListAppend() for details.
100 struct PendingList {
101 int nData;
102 char *aData;
103 int nSpace;
104 sqlite3_int64 iLastDocid;
105 sqlite3_int64 iLastCol;
106 sqlite3_int64 iLastPos;
111 ** Each cursor has a (possibly empty) linked list of the following objects.
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 */
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:
127 ** sqlite3Fts3SegReaderNew()
128 ** sqlite3Fts3SegReaderFree()
129 ** sqlite3Fts3SegReaderIterate()
131 ** Methods used to manipulate Fts3SegReader structures:
133 ** fts3SegReaderNext()
134 ** fts3SegReaderFirstDocid()
135 ** fts3SegReaderNextDocid()
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 */
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) */
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 */
152 Fts3HashElem **ppNextElem;
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).
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 */
165 /* The following variables are used by fts3SegReaderNextDocid() to iterate
166 ** through the current doclist (aDoclist/nDoclist).
168 char *pOffsetList;
169 int nOffsetList; /* For descending pending seg-readers only */
170 sqlite3_int64 iDocid;
173 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
174 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
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:
181 ** fts3SegWriterAdd()
182 ** fts3SegWriterFlush()
183 ** fts3SegWriterFree()
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 */
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.
205 ** fts3NodeAddTerm()
206 ** fts3NodeWrite()
207 ** fts3NodeFree()
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.
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 */
231 ** Valid values for the second argument to fts3SqlStmt().
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
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
272 #define SQL_SELECT_LEVEL_RANGE2 37
273 #define SQL_UPDATE_LEVEL_IDX 38
274 #define SQL_UPDATE_LEVEL 39
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.
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.
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 */
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(?,?,?,?,?,?)",
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",
314 /* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
315 /* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
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 */ "",
328 /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
329 /* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
331 /* This statement is used to determine which level to read the input from
332 ** when performing an incremental merge. It returns the absolute level number
333 ** of the oldest level in the db that contains at least ? segments. Or,
334 ** if no level in the FTS index contains more than ? segments, the statement
335 ** returns zero rows. */
336 /* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
337 " GROUP BY level HAVING cnt>=?"
338 " ORDER BY (level %% 1024) ASC LIMIT 1",
340 /* Estimate the upper limit on the number of leaf nodes in a new segment
341 ** created by merging the oldest :2 segments from absolute level :1. See
342 ** function sqlite3Fts3Incrmerge() for details. */
343 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
344 " FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
346 /* SQL_DELETE_SEGDIR_ENTRY
347 ** Delete the %_segdir entry on absolute level :1 with index :2. */
348 /* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
350 /* SQL_SHIFT_SEGDIR_ENTRY
351 ** Modify the idx value for the segment with idx=:3 on absolute level :2
352 ** to :1. */
353 /* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
355 /* SQL_SELECT_SEGDIR
356 ** Read a single entry from the %_segdir table. The entry from absolute
357 ** level :1 with index value :2. */
358 /* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
359 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
361 /* SQL_CHOMP_SEGDIR
362 ** Update the start_block (:1) and root (:2) fields of the %_segdir
363 ** entry located on absolute level :3 with index :4. */
364 /* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
365 "WHERE level = ? AND idx = ?",
367 /* SQL_SEGMENT_IS_APPENDABLE
368 ** Return a single row if the segment with end_block=? is appendable. Or
369 ** no rows otherwise. */
370 /* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
372 /* SQL_SELECT_INDEXES
373 ** Return the list of valid segment indexes for absolute level ? */
374 /* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
376 /* SQL_SELECT_MXLEVEL
377 ** Return the largest relative level in the FTS index or indexes. */
378 /* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
380 /* Return segments in order from oldest to newest.*/
381 /* 37 */ "SELECT level, idx, end_block "
382 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
383 "ORDER BY level DESC, idx ASC",
385 /* Update statements used while promoting segments */
386 /* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
387 "WHERE level=? AND idx=?",
388 /* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
391 int rc = SQLITE_OK;
392 sqlite3_stmt *pStmt;
394 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
395 assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
397 pStmt = p->aStmt[eStmt];
398 if( !pStmt ){
399 char *zSql;
400 if( eStmt==SQL_CONTENT_INSERT ){
401 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
402 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
403 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
404 }else{
405 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
407 if( !zSql ){
408 rc = SQLITE_NOMEM;
409 }else{
410 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
411 sqlite3_free(zSql);
412 assert( rc==SQLITE_OK || pStmt==0 );
413 p->aStmt[eStmt] = pStmt;
416 if( apVal ){
417 int i;
418 int nParam = sqlite3_bind_parameter_count(pStmt);
419 for(i=0; rc==SQLITE_OK && i<nParam; i++){
420 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
423 *pp = pStmt;
424 return rc;
428 static int fts3SelectDocsize(
429 Fts3Table *pTab, /* FTS3 table handle */
430 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
431 sqlite3_stmt **ppStmt /* OUT: Statement handle */
433 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
434 int rc; /* Return code */
436 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
437 if( rc==SQLITE_OK ){
438 sqlite3_bind_int64(pStmt, 1, iDocid);
439 rc = sqlite3_step(pStmt);
440 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
441 rc = sqlite3_reset(pStmt);
442 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
443 pStmt = 0;
444 }else{
445 rc = SQLITE_OK;
449 *ppStmt = pStmt;
450 return rc;
453 int sqlite3Fts3SelectDoctotal(
454 Fts3Table *pTab, /* Fts3 table handle */
455 sqlite3_stmt **ppStmt /* OUT: Statement handle */
457 sqlite3_stmt *pStmt = 0;
458 int rc;
459 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
460 if( rc==SQLITE_OK ){
461 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
462 if( sqlite3_step(pStmt)!=SQLITE_ROW
463 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
465 rc = sqlite3_reset(pStmt);
466 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
467 pStmt = 0;
470 *ppStmt = pStmt;
471 return rc;
474 int sqlite3Fts3SelectDocsize(
475 Fts3Table *pTab, /* Fts3 table handle */
476 sqlite3_int64 iDocid, /* Docid to read size data for */
477 sqlite3_stmt **ppStmt /* OUT: Statement handle */
479 return fts3SelectDocsize(pTab, iDocid, ppStmt);
483 ** Similar to fts3SqlStmt(). Except, after binding the parameters in
484 ** array apVal[] to the SQL statement identified by eStmt, the statement
485 ** is executed.
487 ** Returns SQLITE_OK if the statement is successfully executed, or an
488 ** SQLite error code otherwise.
490 static void fts3SqlExec(
491 int *pRC, /* Result code */
492 Fts3Table *p, /* The FTS3 table */
493 int eStmt, /* Index of statement to evaluate */
494 sqlite3_value **apVal /* Parameters to bind */
496 sqlite3_stmt *pStmt;
497 int rc;
498 if( *pRC ) return;
499 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
500 if( rc==SQLITE_OK ){
501 sqlite3_step(pStmt);
502 rc = sqlite3_reset(pStmt);
504 *pRC = rc;
509 ** This function ensures that the caller has obtained an exclusive
510 ** shared-cache table-lock on the %_segdir table. This is required before
511 ** writing data to the fts3 table. If this lock is not acquired first, then
512 ** the caller may end up attempting to take this lock as part of committing
513 ** a transaction, causing SQLite to return SQLITE_LOCKED or
514 ** LOCKED_SHAREDCACHEto a COMMIT command.
516 ** It is best to avoid this because if FTS3 returns any error when
517 ** committing a transaction, the whole transaction will be rolled back.
518 ** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
519 ** It can still happen if the user locks the underlying tables directly
520 ** instead of accessing them via FTS.
522 static int fts3Writelock(Fts3Table *p){
523 int rc = SQLITE_OK;
525 if( p->nPendingData==0 ){
526 sqlite3_stmt *pStmt;
527 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
528 if( rc==SQLITE_OK ){
529 sqlite3_bind_null(pStmt, 1);
530 sqlite3_step(pStmt);
531 rc = sqlite3_reset(pStmt);
535 return rc;
539 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
540 ** Within each language id, a separate index is maintained to store the
541 ** document terms, and each configured prefix size (configured the FTS
542 ** "prefix=" option). And each index consists of multiple levels ("relative
543 ** levels").
545 ** All three of these values (the language id, the specific index and the
546 ** level within the index) are encoded in 64-bit integer values stored
547 ** in the %_segdir table on disk. This function is used to convert three
548 ** separate component values into the single 64-bit integer value that
549 ** can be used to query the %_segdir table.
551 ** Specifically, each language-id/index combination is allocated 1024
552 ** 64-bit integer level values ("absolute levels"). The main terms index
553 ** for language-id 0 is allocate values 0-1023. The first prefix index
554 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
555 ** Language 1 indexes are allocated immediately following language 0.
557 ** So, for a system with nPrefix prefix indexes configured, the block of
558 ** absolute levels that corresponds to language-id iLangid and index
559 ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
561 static sqlite3_int64 getAbsoluteLevel(
562 Fts3Table *p, /* FTS3 table handle */
563 int iLangid, /* Language id */
564 int iIndex, /* Index in p->aIndex[] */
565 int iLevel /* Level of segments */
567 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
568 assert( iLangid>=0 );
569 assert( p->nIndex>0 );
570 assert( iIndex>=0 && iIndex<p->nIndex );
572 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
573 return iBase + iLevel;
577 ** Set *ppStmt to a statement handle that may be used to iterate through
578 ** all rows in the %_segdir table, from oldest to newest. If successful,
579 ** return SQLITE_OK. If an error occurs while preparing the statement,
580 ** return an SQLite error code.
582 ** There is only ever one instance of this SQL statement compiled for
583 ** each FTS3 table.
585 ** The statement returns the following columns from the %_segdir table:
587 ** 0: idx
588 ** 1: start_block
589 ** 2: leaves_end_block
590 ** 3: end_block
591 ** 4: root
593 int sqlite3Fts3AllSegdirs(
594 Fts3Table *p, /* FTS3 table */
595 int iLangid, /* Language being queried */
596 int iIndex, /* Index for p->aIndex[] */
597 int iLevel, /* Level to select (relative level) */
598 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
600 int rc;
601 sqlite3_stmt *pStmt = 0;
603 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
604 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
605 assert( iIndex>=0 && iIndex<p->nIndex );
607 if( iLevel<0 ){
608 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
609 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
610 if( rc==SQLITE_OK ){
611 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
612 sqlite3_bind_int64(pStmt, 2,
613 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
616 }else{
617 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
618 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
619 if( rc==SQLITE_OK ){
620 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
623 *ppStmt = pStmt;
624 return rc;
629 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
630 ** if successful, or an SQLite error code otherwise.
632 ** This function also serves to allocate the PendingList structure itself.
633 ** For example, to create a new PendingList structure containing two
634 ** varints:
636 ** PendingList *p = 0;
637 ** fts3PendingListAppendVarint(&p, 1);
638 ** fts3PendingListAppendVarint(&p, 2);
640 static int fts3PendingListAppendVarint(
641 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
642 sqlite3_int64 i /* Value to append to data */
644 PendingList *p = *pp;
646 /* Allocate or grow the PendingList as required. */
647 if( !p ){
648 p = sqlite3_malloc(sizeof(*p) + 100);
649 if( !p ){
650 return SQLITE_NOMEM;
652 p->nSpace = 100;
653 p->aData = (char *)&p[1];
654 p->nData = 0;
656 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
657 int nNew = p->nSpace * 2;
658 p = sqlite3_realloc(p, sizeof(*p) + nNew);
659 if( !p ){
660 sqlite3_free(*pp);
661 *pp = 0;
662 return SQLITE_NOMEM;
664 p->nSpace = nNew;
665 p->aData = (char *)&p[1];
668 /* Append the new serialized varint to the end of the list. */
669 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
670 p->aData[p->nData] = '\0';
671 *pp = p;
672 return SQLITE_OK;
676 ** Add a docid/column/position entry to a PendingList structure. Non-zero
677 ** is returned if the structure is sqlite3_realloced as part of adding
678 ** the entry. Otherwise, zero.
680 ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
681 ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
682 ** it is set to SQLITE_OK.
684 static int fts3PendingListAppend(
685 PendingList **pp, /* IN/OUT: PendingList structure */
686 sqlite3_int64 iDocid, /* Docid for entry to add */
687 sqlite3_int64 iCol, /* Column for entry to add */
688 sqlite3_int64 iPos, /* Position of term for entry to add */
689 int *pRc /* OUT: Return code */
691 PendingList *p = *pp;
692 int rc = SQLITE_OK;
694 assert( !p || p->iLastDocid<=iDocid );
696 if( !p || p->iLastDocid!=iDocid ){
697 sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
698 if( p ){
699 assert( p->nData<p->nSpace );
700 assert( p->aData[p->nData]==0 );
701 p->nData++;
703 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
704 goto pendinglistappend_out;
706 p->iLastCol = -1;
707 p->iLastPos = 0;
708 p->iLastDocid = iDocid;
710 if( iCol>0 && p->iLastCol!=iCol ){
711 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
712 || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
714 goto pendinglistappend_out;
716 p->iLastCol = iCol;
717 p->iLastPos = 0;
719 if( iCol>=0 ){
720 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
721 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
722 if( rc==SQLITE_OK ){
723 p->iLastPos = iPos;
727 pendinglistappend_out:
728 *pRc = rc;
729 if( p!=*pp ){
730 *pp = p;
731 return 1;
733 return 0;
737 ** Free a PendingList object allocated by fts3PendingListAppend().
739 static void fts3PendingListDelete(PendingList *pList){
740 sqlite3_free(pList);
744 ** Add an entry to one of the pending-terms hash tables.
746 static int fts3PendingTermsAddOne(
747 Fts3Table *p,
748 int iCol,
749 int iPos,
750 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
751 const char *zToken,
752 int nToken
754 PendingList *pList;
755 int rc = SQLITE_OK;
757 pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
758 if( pList ){
759 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
761 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
762 if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
763 /* Malloc failed while inserting the new entry. This can only
764 ** happen if there was no previous entry for this token.
766 assert( 0==fts3HashFind(pHash, zToken, nToken) );
767 sqlite3_free(pList);
768 rc = SQLITE_NOMEM;
771 if( rc==SQLITE_OK ){
772 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
774 return rc;
778 ** Tokenize the nul-terminated string zText and add all tokens to the
779 ** pending-terms hash-table. The docid used is that currently stored in
780 ** p->iPrevDocid, and the column is specified by argument iCol.
782 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
784 static int fts3PendingTermsAdd(
785 Fts3Table *p, /* Table into which text will be inserted */
786 int iLangid, /* Language id to use */
787 const char *zText, /* Text of document to be inserted */
788 int iCol, /* Column into which text is being inserted */
789 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
791 int rc;
792 int iStart = 0;
793 int iEnd = 0;
794 int iPos = 0;
795 int nWord = 0;
797 char const *zToken;
798 int nToken = 0;
800 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
801 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
802 sqlite3_tokenizer_cursor *pCsr;
803 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
804 const char**,int*,int*,int*,int*);
806 assert( pTokenizer && pModule );
808 /* If the user has inserted a NULL value, this function may be called with
809 ** zText==0. In this case, add zero token entries to the hash table and
810 ** return early. */
811 if( zText==0 ){
812 *pnWord = 0;
813 return SQLITE_OK;
816 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
817 if( rc!=SQLITE_OK ){
818 return rc;
821 xNext = pModule->xNext;
822 while( SQLITE_OK==rc
823 && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
825 int i;
826 if( iPos>=nWord ) nWord = iPos+1;
828 /* Positions cannot be negative; we use -1 as a terminator internally.
829 ** Tokens must have a non-zero length.
831 if( iPos<0 || !zToken || nToken<=0 ){
832 rc = SQLITE_ERROR;
833 break;
836 /* Add the term to the terms index */
837 rc = fts3PendingTermsAddOne(
838 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
841 /* Add the term to each of the prefix indexes that it is not too
842 ** short for. */
843 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
844 struct Fts3Index *pIndex = &p->aIndex[i];
845 if( nToken<pIndex->nPrefix ) continue;
846 rc = fts3PendingTermsAddOne(
847 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
852 pModule->xClose(pCsr);
853 *pnWord += nWord;
854 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
858 ** Calling this function indicates that subsequent calls to
859 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
860 ** contents of the document with docid iDocid.
862 static int fts3PendingTermsDocid(
863 Fts3Table *p, /* Full-text table handle */
864 int bDelete, /* True if this op is a delete */
865 int iLangid, /* Language id of row being written */
866 sqlite_int64 iDocid /* Docid of row being written */
868 assert( iLangid>=0 );
869 assert( bDelete==1 || bDelete==0 );
871 /* TODO(shess) Explore whether partially flushing the buffer on
872 ** forced-flush would provide better performance. I suspect that if
873 ** we ordered the doclists by size and flushed the largest until the
874 ** buffer was half empty, that would let the less frequent terms
875 ** generate longer doclists.
877 if( iDocid<p->iPrevDocid
878 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
879 || p->iPrevLangid!=iLangid
880 || p->nPendingData>p->nMaxPendingData
882 int rc = sqlite3Fts3PendingTermsFlush(p);
883 if( rc!=SQLITE_OK ) return rc;
885 p->iPrevDocid = iDocid;
886 p->iPrevLangid = iLangid;
887 p->bPrevDelete = bDelete;
888 return SQLITE_OK;
892 ** Discard the contents of the pending-terms hash tables.
894 void sqlite3Fts3PendingTermsClear(Fts3Table *p){
895 int i;
896 for(i=0; i<p->nIndex; i++){
897 Fts3HashElem *pElem;
898 Fts3Hash *pHash = &p->aIndex[i].hPending;
899 for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
900 PendingList *pList = (PendingList *)fts3HashData(pElem);
901 fts3PendingListDelete(pList);
903 fts3HashClear(pHash);
905 p->nPendingData = 0;
909 ** This function is called by the xUpdate() method as part of an INSERT
910 ** operation. It adds entries for each term in the new record to the
911 ** pendingTerms hash table.
913 ** Argument apVal is the same as the similarly named argument passed to
914 ** fts3InsertData(). Parameter iDocid is the docid of the new row.
916 static int fts3InsertTerms(
917 Fts3Table *p,
918 int iLangid,
919 sqlite3_value **apVal,
920 u32 *aSz
922 int i; /* Iterator variable */
923 for(i=2; i<p->nColumn+2; i++){
924 int iCol = i-2;
925 if( p->abNotindexed[iCol]==0 ){
926 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
927 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
928 if( rc!=SQLITE_OK ){
929 return rc;
931 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
934 return SQLITE_OK;
938 ** This function is called by the xUpdate() method for an INSERT operation.
939 ** The apVal parameter is passed a copy of the apVal argument passed by
940 ** SQLite to the xUpdate() method. i.e:
942 ** apVal[0] Not used for INSERT.
943 ** apVal[1] rowid
944 ** apVal[2] Left-most user-defined column
945 ** ...
946 ** apVal[p->nColumn+1] Right-most user-defined column
947 ** apVal[p->nColumn+2] Hidden column with same name as table
948 ** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
949 ** apVal[p->nColumn+4] Hidden languageid column
951 static int fts3InsertData(
952 Fts3Table *p, /* Full-text table */
953 sqlite3_value **apVal, /* Array of values to insert */
954 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
956 int rc; /* Return code */
957 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
959 if( p->zContentTbl ){
960 sqlite3_value *pRowid = apVal[p->nColumn+3];
961 if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
962 pRowid = apVal[1];
964 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
965 return SQLITE_CONSTRAINT;
967 *piDocid = sqlite3_value_int64(pRowid);
968 return SQLITE_OK;
971 /* Locate the statement handle used to insert data into the %_content
972 ** table. The SQL for this statement is:
974 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
976 ** The statement features N '?' variables, where N is the number of user
977 ** defined columns in the FTS3 table, plus one for the docid field.
979 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
980 if( rc==SQLITE_OK && p->zLanguageid ){
981 rc = sqlite3_bind_int(
982 pContentInsert, p->nColumn+2,
983 sqlite3_value_int(apVal[p->nColumn+4])
986 if( rc!=SQLITE_OK ) return rc;
988 /* There is a quirk here. The users INSERT statement may have specified
989 ** a value for the "rowid" field, for the "docid" field, or for both.
990 ** Which is a problem, since "rowid" and "docid" are aliases for the
991 ** same value. For example:
993 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
995 ** In FTS3, this is an error. It is an error to specify non-NULL values
996 ** for both docid and some other rowid alias.
998 if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
999 if( SQLITE_NULL==sqlite3_value_type(apVal[0])
1000 && SQLITE_NULL!=sqlite3_value_type(apVal[1])
1002 /* A rowid/docid conflict. */
1003 return SQLITE_ERROR;
1005 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
1006 if( rc!=SQLITE_OK ) return rc;
1009 /* Execute the statement to insert the record. Set *piDocid to the
1010 ** new docid value.
1012 sqlite3_step(pContentInsert);
1013 rc = sqlite3_reset(pContentInsert);
1015 *piDocid = sqlite3_last_insert_rowid(p->db);
1016 return rc;
1022 ** Remove all data from the FTS3 table. Clear the hash table containing
1023 ** pending terms.
1025 static int fts3DeleteAll(Fts3Table *p, int bContent){
1026 int rc = SQLITE_OK; /* Return code */
1028 /* Discard the contents of the pending-terms hash table. */
1029 sqlite3Fts3PendingTermsClear(p);
1031 /* Delete everything from the shadow tables. Except, leave %_content as
1032 ** is if bContent is false. */
1033 assert( p->zContentTbl==0 || bContent==0 );
1034 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
1035 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
1036 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
1037 if( p->bHasDocsize ){
1038 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
1040 if( p->bHasStat ){
1041 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
1043 return rc;
1049 static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
1050 int iLangid = 0;
1051 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
1052 return iLangid;
1056 ** The first element in the apVal[] array is assumed to contain the docid
1057 ** (an integer) of a row about to be deleted. Remove all terms from the
1058 ** full-text index.
1060 static void fts3DeleteTerms(
1061 int *pRC, /* Result code */
1062 Fts3Table *p, /* The FTS table to delete from */
1063 sqlite3_value *pRowid, /* The docid to be deleted */
1064 u32 *aSz, /* Sizes of deleted document written here */
1065 int *pbFound /* OUT: Set to true if row really does exist */
1067 int rc;
1068 sqlite3_stmt *pSelect;
1070 assert( *pbFound==0 );
1071 if( *pRC ) return;
1072 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
1073 if( rc==SQLITE_OK ){
1074 if( SQLITE_ROW==sqlite3_step(pSelect) ){
1075 int i;
1076 int iLangid = langidFromSelect(p, pSelect);
1077 i64 iDocid = sqlite3_column_int64(pSelect, 0);
1078 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
1079 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
1080 int iCol = i-1;
1081 if( p->abNotindexed[iCol]==0 ){
1082 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
1083 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
1084 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
1087 if( rc!=SQLITE_OK ){
1088 sqlite3_reset(pSelect);
1089 *pRC = rc;
1090 return;
1092 *pbFound = 1;
1094 rc = sqlite3_reset(pSelect);
1095 }else{
1096 sqlite3_reset(pSelect);
1098 *pRC = rc;
1102 ** Forward declaration to account for the circular dependency between
1103 ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
1105 static int fts3SegmentMerge(Fts3Table *, int, int, int);
1108 ** This function allocates a new level iLevel index in the segdir table.
1109 ** Usually, indexes are allocated within a level sequentially starting
1110 ** with 0, so the allocated index is one greater than the value returned
1111 ** by:
1113 ** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
1115 ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
1116 ** level, they are merged into a single level (iLevel+1) segment and the
1117 ** allocated index is 0.
1119 ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
1120 ** returned. Otherwise, an SQLite error code is returned.
1122 static int fts3AllocateSegdirIdx(
1123 Fts3Table *p,
1124 int iLangid, /* Language id */
1125 int iIndex, /* Index for p->aIndex */
1126 int iLevel,
1127 int *piIdx
1129 int rc; /* Return Code */
1130 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
1131 int iNext = 0; /* Result of query pNextIdx */
1133 assert( iLangid>=0 );
1134 assert( p->nIndex>=1 );
1136 /* Set variable iNext to the next available segdir index at level iLevel. */
1137 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
1138 if( rc==SQLITE_OK ){
1139 sqlite3_bind_int64(
1140 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
1142 if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
1143 iNext = sqlite3_column_int(pNextIdx, 0);
1145 rc = sqlite3_reset(pNextIdx);
1148 if( rc==SQLITE_OK ){
1149 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
1150 ** full, merge all segments in level iLevel into a single iLevel+1
1151 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
1152 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
1154 if( iNext>=FTS3_MERGE_COUNT ){
1155 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
1156 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
1157 *piIdx = 0;
1158 }else{
1159 *piIdx = iNext;
1163 return rc;
1167 ** The %_segments table is declared as follows:
1169 ** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
1171 ** This function reads data from a single row of the %_segments table. The
1172 ** specific row is identified by the iBlockid parameter. If paBlob is not
1173 ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
1174 ** with the contents of the blob stored in the "block" column of the
1175 ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
1176 ** to the size of the blob in bytes before returning.
1178 ** If an error occurs, or the table does not contain the specified row,
1179 ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
1180 ** paBlob is non-NULL, then it is the responsibility of the caller to
1181 ** eventually free the returned buffer.
1183 ** This function may leave an open sqlite3_blob* handle in the
1184 ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
1185 ** to this function. The handle may be closed by calling the
1186 ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
1187 ** performance improvement, but the blob handle should always be closed
1188 ** before control is returned to the user (to prevent a lock being held
1189 ** on the database file for longer than necessary). Thus, any virtual table
1190 ** method (xFilter etc.) that may directly or indirectly call this function
1191 ** must call sqlite3Fts3SegmentsClose() before returning.
1193 int sqlite3Fts3ReadBlock(
1194 Fts3Table *p, /* FTS3 table handle */
1195 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
1196 char **paBlob, /* OUT: Blob data in malloc'd buffer */
1197 int *pnBlob, /* OUT: Size of blob data */
1198 int *pnLoad /* OUT: Bytes actually loaded */
1200 int rc; /* Return code */
1202 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
1203 assert( pnBlob );
1205 if( p->pSegments ){
1206 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
1207 }else{
1208 if( 0==p->zSegmentsTbl ){
1209 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
1210 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
1212 rc = sqlite3_blob_open(
1213 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
1217 if( rc==SQLITE_OK ){
1218 int nByte = sqlite3_blob_bytes(p->pSegments);
1219 *pnBlob = nByte;
1220 if( paBlob ){
1221 char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
1222 if( !aByte ){
1223 rc = SQLITE_NOMEM;
1224 }else{
1225 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
1226 nByte = FTS3_NODE_CHUNKSIZE;
1227 *pnLoad = nByte;
1229 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
1230 memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
1231 if( rc!=SQLITE_OK ){
1232 sqlite3_free(aByte);
1233 aByte = 0;
1236 *paBlob = aByte;
1240 return rc;
1244 ** Close the blob handle at p->pSegments, if it is open. See comments above
1245 ** the sqlite3Fts3ReadBlock() function for details.
1247 void sqlite3Fts3SegmentsClose(Fts3Table *p){
1248 sqlite3_blob_close(p->pSegments);
1249 p->pSegments = 0;
1252 static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
1253 int nRead; /* Number of bytes to read */
1254 int rc; /* Return code */
1256 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
1257 rc = sqlite3_blob_read(
1258 pReader->pBlob,
1259 &pReader->aNode[pReader->nPopulate],
1260 nRead,
1261 pReader->nPopulate
1264 if( rc==SQLITE_OK ){
1265 pReader->nPopulate += nRead;
1266 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
1267 if( pReader->nPopulate==pReader->nNode ){
1268 sqlite3_blob_close(pReader->pBlob);
1269 pReader->pBlob = 0;
1270 pReader->nPopulate = 0;
1273 return rc;
1276 static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
1277 int rc = SQLITE_OK;
1278 assert( !pReader->pBlob
1279 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
1281 while( pReader->pBlob && rc==SQLITE_OK
1282 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
1284 rc = fts3SegReaderIncrRead(pReader);
1286 return rc;
1290 ** Set an Fts3SegReader cursor to point at EOF.
1292 static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
1293 if( !fts3SegReaderIsRootOnly(pSeg) ){
1294 sqlite3_free(pSeg->aNode);
1295 sqlite3_blob_close(pSeg->pBlob);
1296 pSeg->pBlob = 0;
1298 pSeg->aNode = 0;
1302 ** Move the iterator passed as the first argument to the next term in the
1303 ** segment. If successful, SQLITE_OK is returned. If there is no next term,
1304 ** SQLITE_DONE. Otherwise, an SQLite error code.
1306 static int fts3SegReaderNext(
1307 Fts3Table *p,
1308 Fts3SegReader *pReader,
1309 int bIncr
1311 int rc; /* Return code of various sub-routines */
1312 char *pNext; /* Cursor variable */
1313 int nPrefix; /* Number of bytes in term prefix */
1314 int nSuffix; /* Number of bytes in term suffix */
1316 if( !pReader->aDoclist ){
1317 pNext = pReader->aNode;
1318 }else{
1319 pNext = &pReader->aDoclist[pReader->nDoclist];
1322 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
1324 if( fts3SegReaderIsPending(pReader) ){
1325 Fts3HashElem *pElem = *(pReader->ppNextElem);
1326 sqlite3_free(pReader->aNode);
1327 pReader->aNode = 0;
1328 if( pElem ){
1329 char *aCopy;
1330 PendingList *pList = (PendingList *)fts3HashData(pElem);
1331 int nCopy = pList->nData+1;
1332 pReader->zTerm = (char *)fts3HashKey(pElem);
1333 pReader->nTerm = fts3HashKeysize(pElem);
1334 aCopy = (char*)sqlite3_malloc(nCopy);
1335 if( !aCopy ) return SQLITE_NOMEM;
1336 memcpy(aCopy, pList->aData, nCopy);
1337 pReader->nNode = pReader->nDoclist = nCopy;
1338 pReader->aNode = pReader->aDoclist = aCopy;
1339 pReader->ppNextElem++;
1340 assert( pReader->aNode );
1342 return SQLITE_OK;
1345 fts3SegReaderSetEof(pReader);
1347 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
1348 ** blocks have already been traversed. */
1349 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
1350 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
1351 return SQLITE_OK;
1354 rc = sqlite3Fts3ReadBlock(
1355 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
1356 (bIncr ? &pReader->nPopulate : 0)
1358 if( rc!=SQLITE_OK ) return rc;
1359 assert( pReader->pBlob==0 );
1360 if( bIncr && pReader->nPopulate<pReader->nNode ){
1361 pReader->pBlob = p->pSegments;
1362 p->pSegments = 0;
1364 pNext = pReader->aNode;
1367 assert( !fts3SegReaderIsPending(pReader) );
1369 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
1370 if( rc!=SQLITE_OK ) return rc;
1372 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
1373 ** safe (no risk of overread) even if the node data is corrupted. */
1374 pNext += fts3GetVarint32(pNext, &nPrefix);
1375 pNext += fts3GetVarint32(pNext, &nSuffix);
1376 if( nPrefix<0 || nSuffix<=0
1377 || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
1379 return FTS_CORRUPT_VTAB;
1382 if( nPrefix+nSuffix>pReader->nTermAlloc ){
1383 int nNew = (nPrefix+nSuffix)*2;
1384 char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
1385 if( !zNew ){
1386 return SQLITE_NOMEM;
1388 pReader->zTerm = zNew;
1389 pReader->nTermAlloc = nNew;
1392 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
1393 if( rc!=SQLITE_OK ) return rc;
1395 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
1396 pReader->nTerm = nPrefix+nSuffix;
1397 pNext += nSuffix;
1398 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
1399 pReader->aDoclist = pNext;
1400 pReader->pOffsetList = 0;
1402 /* Check that the doclist does not appear to extend past the end of the
1403 ** b-tree node. And that the final byte of the doclist is 0x00. If either
1404 ** of these statements is untrue, then the data structure is corrupt.
1406 if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
1407 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
1409 return FTS_CORRUPT_VTAB;
1411 return SQLITE_OK;
1415 ** Set the SegReader to point to the first docid in the doclist associated
1416 ** with the current term.
1418 static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
1419 int rc = SQLITE_OK;
1420 assert( pReader->aDoclist );
1421 assert( !pReader->pOffsetList );
1422 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
1423 u8 bEof = 0;
1424 pReader->iDocid = 0;
1425 pReader->nOffsetList = 0;
1426 sqlite3Fts3DoclistPrev(0,
1427 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
1428 &pReader->iDocid, &pReader->nOffsetList, &bEof
1430 }else{
1431 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
1432 if( rc==SQLITE_OK ){
1433 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
1434 pReader->pOffsetList = &pReader->aDoclist[n];
1437 return rc;
1441 ** Advance the SegReader to point to the next docid in the doclist
1442 ** associated with the current term.
1444 ** If arguments ppOffsetList and pnOffsetList are not NULL, then
1445 ** *ppOffsetList is set to point to the first column-offset list
1446 ** in the doclist entry (i.e. immediately past the docid varint).
1447 ** *pnOffsetList is set to the length of the set of column-offset
1448 ** lists, not including the nul-terminator byte. For example:
1450 static int fts3SegReaderNextDocid(
1451 Fts3Table *pTab,
1452 Fts3SegReader *pReader, /* Reader to advance to next docid */
1453 char **ppOffsetList, /* OUT: Pointer to current position-list */
1454 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
1456 int rc = SQLITE_OK;
1457 char *p = pReader->pOffsetList;
1458 char c = 0;
1460 assert( p );
1462 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
1463 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
1464 ** Pending-terms doclists are always built up in ascending order, so
1465 ** we have to iterate through them backwards here. */
1466 u8 bEof = 0;
1467 if( ppOffsetList ){
1468 *ppOffsetList = pReader->pOffsetList;
1469 *pnOffsetList = pReader->nOffsetList - 1;
1471 sqlite3Fts3DoclistPrev(0,
1472 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
1473 &pReader->nOffsetList, &bEof
1475 if( bEof ){
1476 pReader->pOffsetList = 0;
1477 }else{
1478 pReader->pOffsetList = p;
1480 }else{
1481 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
1483 /* Pointer p currently points at the first byte of an offset list. The
1484 ** following block advances it to point one byte past the end of
1485 ** the same offset list. */
1486 while( 1 ){
1488 /* The following line of code (and the "p++" below the while() loop) is
1489 ** normally all that is required to move pointer p to the desired
1490 ** position. The exception is if this node is being loaded from disk
1491 ** incrementally and pointer "p" now points to the first byte past
1492 ** the populated part of pReader->aNode[].
1494 while( *p | c ) c = *p++ & 0x80;
1495 assert( *p==0 );
1497 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
1498 rc = fts3SegReaderIncrRead(pReader);
1499 if( rc!=SQLITE_OK ) return rc;
1501 p++;
1503 /* If required, populate the output variables with a pointer to and the
1504 ** size of the previous offset-list.
1506 if( ppOffsetList ){
1507 *ppOffsetList = pReader->pOffsetList;
1508 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
1511 /* List may have been edited in place by fts3EvalNearTrim() */
1512 while( p<pEnd && *p==0 ) p++;
1514 /* If there are no more entries in the doclist, set pOffsetList to
1515 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
1516 ** Fts3SegReader.pOffsetList to point to the next offset list before
1517 ** returning.
1519 if( p>=pEnd ){
1520 pReader->pOffsetList = 0;
1521 }else{
1522 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
1523 if( rc==SQLITE_OK ){
1524 sqlite3_int64 iDelta;
1525 pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
1526 if( pTab->bDescIdx ){
1527 pReader->iDocid -= iDelta;
1528 }else{
1529 pReader->iDocid += iDelta;
1535 return SQLITE_OK;
1539 int sqlite3Fts3MsrOvfl(
1540 Fts3Cursor *pCsr,
1541 Fts3MultiSegReader *pMsr,
1542 int *pnOvfl
1544 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
1545 int nOvfl = 0;
1546 int ii;
1547 int rc = SQLITE_OK;
1548 int pgsz = p->nPgsz;
1550 assert( p->bFts4 );
1551 assert( pgsz>0 );
1553 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
1554 Fts3SegReader *pReader = pMsr->apSegment[ii];
1555 if( !fts3SegReaderIsPending(pReader)
1556 && !fts3SegReaderIsRootOnly(pReader)
1558 sqlite3_int64 jj;
1559 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
1560 int nBlob;
1561 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
1562 if( rc!=SQLITE_OK ) break;
1563 if( (nBlob+35)>pgsz ){
1564 nOvfl += (nBlob + 34)/pgsz;
1569 *pnOvfl = nOvfl;
1570 return rc;
1574 ** Free all allocations associated with the iterator passed as the
1575 ** second argument.
1577 void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
1578 if( pReader ){
1579 if( !fts3SegReaderIsPending(pReader) ){
1580 sqlite3_free(pReader->zTerm);
1582 if( !fts3SegReaderIsRootOnly(pReader) ){
1583 sqlite3_free(pReader->aNode);
1585 sqlite3_blob_close(pReader->pBlob);
1587 sqlite3_free(pReader);
1591 ** Allocate a new SegReader object.
1593 int sqlite3Fts3SegReaderNew(
1594 int iAge, /* Segment "age". */
1595 int bLookup, /* True for a lookup only */
1596 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
1597 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
1598 sqlite3_int64 iEndBlock, /* Final block of segment */
1599 const char *zRoot, /* Buffer containing root node */
1600 int nRoot, /* Size of buffer containing root node */
1601 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
1603 Fts3SegReader *pReader; /* Newly allocated SegReader object */
1604 int nExtra = 0; /* Bytes to allocate segment root node */
1606 assert( iStartLeaf<=iEndLeaf );
1607 if( iStartLeaf==0 ){
1608 nExtra = nRoot + FTS3_NODE_PADDING;
1611 pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
1612 if( !pReader ){
1613 return SQLITE_NOMEM;
1615 memset(pReader, 0, sizeof(Fts3SegReader));
1616 pReader->iIdx = iAge;
1617 pReader->bLookup = bLookup!=0;
1618 pReader->iStartBlock = iStartLeaf;
1619 pReader->iLeafEndBlock = iEndLeaf;
1620 pReader->iEndBlock = iEndBlock;
1622 if( nExtra ){
1623 /* The entire segment is stored in the root node. */
1624 pReader->aNode = (char *)&pReader[1];
1625 pReader->rootOnly = 1;
1626 pReader->nNode = nRoot;
1627 memcpy(pReader->aNode, zRoot, nRoot);
1628 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
1629 }else{
1630 pReader->iCurrentBlock = iStartLeaf-1;
1632 *ppReader = pReader;
1633 return SQLITE_OK;
1637 ** This is a comparison function used as a qsort() callback when sorting
1638 ** an array of pending terms by term. This occurs as part of flushing
1639 ** the contents of the pending-terms hash table to the database.
1641 static int SQLITE_CDECL fts3CompareElemByTerm(
1642 const void *lhs,
1643 const void *rhs
1645 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
1646 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
1647 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
1648 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
1650 int n = (n1<n2 ? n1 : n2);
1651 int c = memcmp(z1, z2, n);
1652 if( c==0 ){
1653 c = n1 - n2;
1655 return c;
1659 ** This function is used to allocate an Fts3SegReader that iterates through
1660 ** a subset of the terms stored in the Fts3Table.pendingTerms array.
1662 ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
1663 ** through each term in the pending-terms table. Or, if isPrefixIter is
1664 ** non-zero, it iterates through each term and its prefixes. For example, if
1665 ** the pending terms hash table contains the terms "sqlite", "mysql" and
1666 ** "firebird", then the iterator visits the following 'terms' (in the order
1667 ** shown):
1669 ** f fi fir fire fireb firebi firebir firebird
1670 ** m my mys mysq mysql
1671 ** s sq sql sqli sqlit sqlite
1673 ** Whereas if isPrefixIter is zero, the terms visited are:
1675 ** firebird mysql sqlite
1677 int sqlite3Fts3SegReaderPending(
1678 Fts3Table *p, /* Virtual table handle */
1679 int iIndex, /* Index for p->aIndex */
1680 const char *zTerm, /* Term to search for */
1681 int nTerm, /* Size of buffer zTerm */
1682 int bPrefix, /* True for a prefix iterator */
1683 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
1685 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
1686 Fts3HashElem *pE; /* Iterator variable */
1687 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
1688 int nElem = 0; /* Size of array at aElem */
1689 int rc = SQLITE_OK; /* Return Code */
1690 Fts3Hash *pHash;
1692 pHash = &p->aIndex[iIndex].hPending;
1693 if( bPrefix ){
1694 int nAlloc = 0; /* Size of allocated array at aElem */
1696 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
1697 char *zKey = (char *)fts3HashKey(pE);
1698 int nKey = fts3HashKeysize(pE);
1699 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
1700 if( nElem==nAlloc ){
1701 Fts3HashElem **aElem2;
1702 nAlloc += 16;
1703 aElem2 = (Fts3HashElem **)sqlite3_realloc(
1704 aElem, nAlloc*sizeof(Fts3HashElem *)
1706 if( !aElem2 ){
1707 rc = SQLITE_NOMEM;
1708 nElem = 0;
1709 break;
1711 aElem = aElem2;
1714 aElem[nElem++] = pE;
1718 /* If more than one term matches the prefix, sort the Fts3HashElem
1719 ** objects in term order using qsort(). This uses the same comparison
1720 ** callback as is used when flushing terms to disk.
1722 if( nElem>1 ){
1723 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
1726 }else{
1727 /* The query is a simple term lookup that matches at most one term in
1728 ** the index. All that is required is a straight hash-lookup.
1730 ** Because the stack address of pE may be accessed via the aElem pointer
1731 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
1732 ** within this entire function, not just this "else{...}" block.
1734 pE = fts3HashFindElem(pHash, zTerm, nTerm);
1735 if( pE ){
1736 aElem = &pE;
1737 nElem = 1;
1741 if( nElem>0 ){
1742 int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
1743 pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
1744 if( !pReader ){
1745 rc = SQLITE_NOMEM;
1746 }else{
1747 memset(pReader, 0, nByte);
1748 pReader->iIdx = 0x7FFFFFFF;
1749 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
1750 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
1754 if( bPrefix ){
1755 sqlite3_free(aElem);
1757 *ppReader = pReader;
1758 return rc;
1762 ** Compare the entries pointed to by two Fts3SegReader structures.
1763 ** Comparison is as follows:
1765 ** 1) EOF is greater than not EOF.
1767 ** 2) The current terms (if any) are compared using memcmp(). If one
1768 ** term is a prefix of another, the longer term is considered the
1769 ** larger.
1771 ** 3) By segment age. An older segment is considered larger.
1773 static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
1774 int rc;
1775 if( pLhs->aNode && pRhs->aNode ){
1776 int rc2 = pLhs->nTerm - pRhs->nTerm;
1777 if( rc2<0 ){
1778 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
1779 }else{
1780 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
1782 if( rc==0 ){
1783 rc = rc2;
1785 }else{
1786 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
1788 if( rc==0 ){
1789 rc = pRhs->iIdx - pLhs->iIdx;
1791 assert( rc!=0 );
1792 return rc;
1796 ** A different comparison function for SegReader structures. In this
1797 ** version, it is assumed that each SegReader points to an entry in
1798 ** a doclist for identical terms. Comparison is made as follows:
1800 ** 1) EOF (end of doclist in this case) is greater than not EOF.
1802 ** 2) By current docid.
1804 ** 3) By segment age. An older segment is considered larger.
1806 static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
1807 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
1808 if( rc==0 ){
1809 if( pLhs->iDocid==pRhs->iDocid ){
1810 rc = pRhs->iIdx - pLhs->iIdx;
1811 }else{
1812 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
1815 assert( pLhs->aNode && pRhs->aNode );
1816 return rc;
1818 static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
1819 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
1820 if( rc==0 ){
1821 if( pLhs->iDocid==pRhs->iDocid ){
1822 rc = pRhs->iIdx - pLhs->iIdx;
1823 }else{
1824 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
1827 assert( pLhs->aNode && pRhs->aNode );
1828 return rc;
1832 ** Compare the term that the Fts3SegReader object passed as the first argument
1833 ** points to with the term specified by arguments zTerm and nTerm.
1835 ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
1836 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
1837 ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
1839 static int fts3SegReaderTermCmp(
1840 Fts3SegReader *pSeg, /* Segment reader object */
1841 const char *zTerm, /* Term to compare to */
1842 int nTerm /* Size of term zTerm in bytes */
1844 int res = 0;
1845 if( pSeg->aNode ){
1846 if( pSeg->nTerm>nTerm ){
1847 res = memcmp(pSeg->zTerm, zTerm, nTerm);
1848 }else{
1849 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
1851 if( res==0 ){
1852 res = pSeg->nTerm-nTerm;
1855 return res;
1859 ** Argument apSegment is an array of nSegment elements. It is known that
1860 ** the final (nSegment-nSuspect) members are already in sorted order
1861 ** (according to the comparison function provided). This function shuffles
1862 ** the array around until all entries are in sorted order.
1864 static void fts3SegReaderSort(
1865 Fts3SegReader **apSegment, /* Array to sort entries of */
1866 int nSegment, /* Size of apSegment array */
1867 int nSuspect, /* Unsorted entry count */
1868 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
1870 int i; /* Iterator variable */
1872 assert( nSuspect<=nSegment );
1874 if( nSuspect==nSegment ) nSuspect--;
1875 for(i=nSuspect-1; i>=0; i--){
1876 int j;
1877 for(j=i; j<(nSegment-1); j++){
1878 Fts3SegReader *pTmp;
1879 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
1880 pTmp = apSegment[j+1];
1881 apSegment[j+1] = apSegment[j];
1882 apSegment[j] = pTmp;
1886 #ifndef NDEBUG
1887 /* Check that the list really is sorted now. */
1888 for(i=0; i<(nSuspect-1); i++){
1889 assert( xCmp(apSegment[i], apSegment[i+1])<0 );
1891 #endif
1895 ** Insert a record into the %_segments table.
1897 static int fts3WriteSegment(
1898 Fts3Table *p, /* Virtual table handle */
1899 sqlite3_int64 iBlock, /* Block id for new block */
1900 char *z, /* Pointer to buffer containing block data */
1901 int n /* Size of buffer z in bytes */
1903 sqlite3_stmt *pStmt;
1904 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
1905 if( rc==SQLITE_OK ){
1906 sqlite3_bind_int64(pStmt, 1, iBlock);
1907 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
1908 sqlite3_step(pStmt);
1909 rc = sqlite3_reset(pStmt);
1911 return rc;
1915 ** Find the largest relative level number in the table. If successful, set
1916 ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
1917 ** set *pnMax to zero and return an SQLite error code.
1919 int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
1920 int rc;
1921 int mxLevel = 0;
1922 sqlite3_stmt *pStmt = 0;
1924 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
1925 if( rc==SQLITE_OK ){
1926 if( SQLITE_ROW==sqlite3_step(pStmt) ){
1927 mxLevel = sqlite3_column_int(pStmt, 0);
1929 rc = sqlite3_reset(pStmt);
1931 *pnMax = mxLevel;
1932 return rc;
1936 ** Insert a record into the %_segdir table.
1938 static int fts3WriteSegdir(
1939 Fts3Table *p, /* Virtual table handle */
1940 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
1941 int iIdx, /* Value for "idx" field */
1942 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
1943 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
1944 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
1945 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
1946 char *zRoot, /* Blob value for "root" field */
1947 int nRoot /* Number of bytes in buffer zRoot */
1949 sqlite3_stmt *pStmt;
1950 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
1951 if( rc==SQLITE_OK ){
1952 sqlite3_bind_int64(pStmt, 1, iLevel);
1953 sqlite3_bind_int(pStmt, 2, iIdx);
1954 sqlite3_bind_int64(pStmt, 3, iStartBlock);
1955 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
1956 if( nLeafData==0 ){
1957 sqlite3_bind_int64(pStmt, 5, iEndBlock);
1958 }else{
1959 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
1960 if( !zEnd ) return SQLITE_NOMEM;
1961 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
1963 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
1964 sqlite3_step(pStmt);
1965 rc = sqlite3_reset(pStmt);
1967 return rc;
1971 ** Return the size of the common prefix (if any) shared by zPrev and
1972 ** zNext, in bytes. For example,
1974 ** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
1975 ** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
1976 ** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
1978 static int fts3PrefixCompress(
1979 const char *zPrev, /* Buffer containing previous term */
1980 int nPrev, /* Size of buffer zPrev in bytes */
1981 const char *zNext, /* Buffer containing next term */
1982 int nNext /* Size of buffer zNext in bytes */
1984 int n;
1985 UNUSED_PARAMETER(nNext);
1986 for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
1987 return n;
1991 ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
1992 ** (according to memcmp) than the previous term.
1994 static int fts3NodeAddTerm(
1995 Fts3Table *p, /* Virtual table handle */
1996 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
1997 int isCopyTerm, /* True if zTerm/nTerm is transient */
1998 const char *zTerm, /* Pointer to buffer containing term */
1999 int nTerm /* Size of term in bytes */
2001 SegmentNode *pTree = *ppTree;
2002 int rc;
2003 SegmentNode *pNew;
2005 /* First try to append the term to the current node. Return early if
2006 ** this is possible.
2008 if( pTree ){
2009 int nData = pTree->nData; /* Current size of node in bytes */
2010 int nReq = nData; /* Required space after adding zTerm */
2011 int nPrefix; /* Number of bytes of prefix compression */
2012 int nSuffix; /* Suffix length */
2014 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
2015 nSuffix = nTerm-nPrefix;
2017 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
2018 if( nReq<=p->nNodeSize || !pTree->zTerm ){
2020 if( nReq>p->nNodeSize ){
2021 /* An unusual case: this is the first term to be added to the node
2022 ** and the static node buffer (p->nNodeSize bytes) is not large
2023 ** enough. Use a separately malloced buffer instead This wastes
2024 ** p->nNodeSize bytes, but since this scenario only comes about when
2025 ** the database contain two terms that share a prefix of almost 2KB,
2026 ** this is not expected to be a serious problem.
2028 assert( pTree->aData==(char *)&pTree[1] );
2029 pTree->aData = (char *)sqlite3_malloc(nReq);
2030 if( !pTree->aData ){
2031 return SQLITE_NOMEM;
2035 if( pTree->zTerm ){
2036 /* There is no prefix-length field for first term in a node */
2037 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
2040 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
2041 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
2042 pTree->nData = nData + nSuffix;
2043 pTree->nEntry++;
2045 if( isCopyTerm ){
2046 if( pTree->nMalloc<nTerm ){
2047 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
2048 if( !zNew ){
2049 return SQLITE_NOMEM;
2051 pTree->nMalloc = nTerm*2;
2052 pTree->zMalloc = zNew;
2054 pTree->zTerm = pTree->zMalloc;
2055 memcpy(pTree->zTerm, zTerm, nTerm);
2056 pTree->nTerm = nTerm;
2057 }else{
2058 pTree->zTerm = (char *)zTerm;
2059 pTree->nTerm = nTerm;
2061 return SQLITE_OK;
2065 /* If control flows to here, it was not possible to append zTerm to the
2066 ** current node. Create a new node (a right-sibling of the current node).
2067 ** If this is the first node in the tree, the term is added to it.
2069 ** Otherwise, the term is not added to the new node, it is left empty for
2070 ** now. Instead, the term is inserted into the parent of pTree. If pTree
2071 ** has no parent, one is created here.
2073 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
2074 if( !pNew ){
2075 return SQLITE_NOMEM;
2077 memset(pNew, 0, sizeof(SegmentNode));
2078 pNew->nData = 1 + FTS3_VARINT_MAX;
2079 pNew->aData = (char *)&pNew[1];
2081 if( pTree ){
2082 SegmentNode *pParent = pTree->pParent;
2083 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
2084 if( pTree->pParent==0 ){
2085 pTree->pParent = pParent;
2087 pTree->pRight = pNew;
2088 pNew->pLeftmost = pTree->pLeftmost;
2089 pNew->pParent = pParent;
2090 pNew->zMalloc = pTree->zMalloc;
2091 pNew->nMalloc = pTree->nMalloc;
2092 pTree->zMalloc = 0;
2093 }else{
2094 pNew->pLeftmost = pNew;
2095 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
2098 *ppTree = pNew;
2099 return rc;
2103 ** Helper function for fts3NodeWrite().
2105 static int fts3TreeFinishNode(
2106 SegmentNode *pTree,
2107 int iHeight,
2108 sqlite3_int64 iLeftChild
2110 int nStart;
2111 assert( iHeight>=1 && iHeight<128 );
2112 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
2113 pTree->aData[nStart] = (char)iHeight;
2114 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
2115 return nStart;
2119 ** Write the buffer for the segment node pTree and all of its peers to the
2120 ** database. Then call this function recursively to write the parent of
2121 ** pTree and its peers to the database.
2123 ** Except, if pTree is a root node, do not write it to the database. Instead,
2124 ** set output variables *paRoot and *pnRoot to contain the root node.
2126 ** If successful, SQLITE_OK is returned and output variable *piLast is
2127 ** set to the largest blockid written to the database (or zero if no
2128 ** blocks were written to the db). Otherwise, an SQLite error code is
2129 ** returned.
2131 static int fts3NodeWrite(
2132 Fts3Table *p, /* Virtual table handle */
2133 SegmentNode *pTree, /* SegmentNode handle */
2134 int iHeight, /* Height of this node in tree */
2135 sqlite3_int64 iLeaf, /* Block id of first leaf node */
2136 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
2137 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
2138 char **paRoot, /* OUT: Data for root node */
2139 int *pnRoot /* OUT: Size of root node in bytes */
2141 int rc = SQLITE_OK;
2143 if( !pTree->pParent ){
2144 /* Root node of the tree. */
2145 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
2146 *piLast = iFree-1;
2147 *pnRoot = pTree->nData - nStart;
2148 *paRoot = &pTree->aData[nStart];
2149 }else{
2150 SegmentNode *pIter;
2151 sqlite3_int64 iNextFree = iFree;
2152 sqlite3_int64 iNextLeaf = iLeaf;
2153 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
2154 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
2155 int nWrite = pIter->nData - nStart;
2157 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
2158 iNextFree++;
2159 iNextLeaf += (pIter->nEntry+1);
2161 if( rc==SQLITE_OK ){
2162 assert( iNextLeaf==iFree );
2163 rc = fts3NodeWrite(
2164 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
2169 return rc;
2173 ** Free all memory allocations associated with the tree pTree.
2175 static void fts3NodeFree(SegmentNode *pTree){
2176 if( pTree ){
2177 SegmentNode *p = pTree->pLeftmost;
2178 fts3NodeFree(p->pParent);
2179 while( p ){
2180 SegmentNode *pRight = p->pRight;
2181 if( p->aData!=(char *)&p[1] ){
2182 sqlite3_free(p->aData);
2184 assert( pRight==0 || p->zMalloc==0 );
2185 sqlite3_free(p->zMalloc);
2186 sqlite3_free(p);
2187 p = pRight;
2193 ** Add a term to the segment being constructed by the SegmentWriter object
2194 ** *ppWriter. When adding the first term to a segment, *ppWriter should
2195 ** be passed NULL. This function will allocate a new SegmentWriter object
2196 ** and return it via the input/output variable *ppWriter in this case.
2198 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
2200 static int fts3SegWriterAdd(
2201 Fts3Table *p, /* Virtual table handle */
2202 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
2203 int isCopyTerm, /* True if buffer zTerm must be copied */
2204 const char *zTerm, /* Pointer to buffer containing term */
2205 int nTerm, /* Size of term in bytes */
2206 const char *aDoclist, /* Pointer to buffer containing doclist */
2207 int nDoclist /* Size of doclist in bytes */
2209 int nPrefix; /* Size of term prefix in bytes */
2210 int nSuffix; /* Size of term suffix in bytes */
2211 int nReq; /* Number of bytes required on leaf page */
2212 int nData;
2213 SegmentWriter *pWriter = *ppWriter;
2215 if( !pWriter ){
2216 int rc;
2217 sqlite3_stmt *pStmt;
2219 /* Allocate the SegmentWriter structure */
2220 pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
2221 if( !pWriter ) return SQLITE_NOMEM;
2222 memset(pWriter, 0, sizeof(SegmentWriter));
2223 *ppWriter = pWriter;
2225 /* Allocate a buffer in which to accumulate data */
2226 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
2227 if( !pWriter->aData ) return SQLITE_NOMEM;
2228 pWriter->nSize = p->nNodeSize;
2230 /* Find the next free blockid in the %_segments table */
2231 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
2232 if( rc!=SQLITE_OK ) return rc;
2233 if( SQLITE_ROW==sqlite3_step(pStmt) ){
2234 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
2235 pWriter->iFirst = pWriter->iFree;
2237 rc = sqlite3_reset(pStmt);
2238 if( rc!=SQLITE_OK ) return rc;
2240 nData = pWriter->nData;
2242 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
2243 nSuffix = nTerm-nPrefix;
2245 /* Figure out how many bytes are required by this new entry */
2246 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
2247 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
2248 nSuffix + /* Term suffix */
2249 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
2250 nDoclist; /* Doclist data */
2252 if( nData>0 && nData+nReq>p->nNodeSize ){
2253 int rc;
2255 /* The current leaf node is full. Write it out to the database. */
2256 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
2257 if( rc!=SQLITE_OK ) return rc;
2258 p->nLeafAdd++;
2260 /* Add the current term to the interior node tree. The term added to
2261 ** the interior tree must:
2263 ** a) be greater than the largest term on the leaf node just written
2264 ** to the database (still available in pWriter->zTerm), and
2266 ** b) be less than or equal to the term about to be added to the new
2267 ** leaf node (zTerm/nTerm).
2269 ** In other words, it must be the prefix of zTerm 1 byte longer than
2270 ** the common prefix (if any) of zTerm and pWriter->zTerm.
2272 assert( nPrefix<nTerm );
2273 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
2274 if( rc!=SQLITE_OK ) return rc;
2276 nData = 0;
2277 pWriter->nTerm = 0;
2279 nPrefix = 0;
2280 nSuffix = nTerm;
2281 nReq = 1 + /* varint containing prefix size */
2282 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
2283 nTerm + /* Term suffix */
2284 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
2285 nDoclist; /* Doclist data */
2288 /* Increase the total number of bytes written to account for the new entry. */
2289 pWriter->nLeafData += nReq;
2291 /* If the buffer currently allocated is too small for this entry, realloc
2292 ** the buffer to make it large enough.
2294 if( nReq>pWriter->nSize ){
2295 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
2296 if( !aNew ) return SQLITE_NOMEM;
2297 pWriter->aData = aNew;
2298 pWriter->nSize = nReq;
2300 assert( nData+nReq<=pWriter->nSize );
2302 /* Append the prefix-compressed term and doclist to the buffer. */
2303 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
2304 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
2305 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
2306 nData += nSuffix;
2307 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
2308 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
2309 pWriter->nData = nData + nDoclist;
2311 /* Save the current term so that it can be used to prefix-compress the next.
2312 ** If the isCopyTerm parameter is true, then the buffer pointed to by
2313 ** zTerm is transient, so take a copy of the term data. Otherwise, just
2314 ** store a copy of the pointer.
2316 if( isCopyTerm ){
2317 if( nTerm>pWriter->nMalloc ){
2318 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
2319 if( !zNew ){
2320 return SQLITE_NOMEM;
2322 pWriter->nMalloc = nTerm*2;
2323 pWriter->zMalloc = zNew;
2324 pWriter->zTerm = zNew;
2326 assert( pWriter->zTerm==pWriter->zMalloc );
2327 memcpy(pWriter->zTerm, zTerm, nTerm);
2328 }else{
2329 pWriter->zTerm = (char *)zTerm;
2331 pWriter->nTerm = nTerm;
2333 return SQLITE_OK;
2337 ** Flush all data associated with the SegmentWriter object pWriter to the
2338 ** database. This function must be called after all terms have been added
2339 ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
2340 ** returned. Otherwise, an SQLite error code.
2342 static int fts3SegWriterFlush(
2343 Fts3Table *p, /* Virtual table handle */
2344 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
2345 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
2346 int iIdx /* Value for 'idx' column of %_segdir */
2348 int rc; /* Return code */
2349 if( pWriter->pTree ){
2350 sqlite3_int64 iLast = 0; /* Largest block id written to database */
2351 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
2352 char *zRoot = NULL; /* Pointer to buffer containing root node */
2353 int nRoot = 0; /* Size of buffer zRoot */
2355 iLastLeaf = pWriter->iFree;
2356 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
2357 if( rc==SQLITE_OK ){
2358 rc = fts3NodeWrite(p, pWriter->pTree, 1,
2359 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
2361 if( rc==SQLITE_OK ){
2362 rc = fts3WriteSegdir(p, iLevel, iIdx,
2363 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
2365 }else{
2366 /* The entire tree fits on the root node. Write it to the segdir table. */
2367 rc = fts3WriteSegdir(p, iLevel, iIdx,
2368 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
2370 p->nLeafAdd++;
2371 return rc;
2375 ** Release all memory held by the SegmentWriter object passed as the
2376 ** first argument.
2378 static void fts3SegWriterFree(SegmentWriter *pWriter){
2379 if( pWriter ){
2380 sqlite3_free(pWriter->aData);
2381 sqlite3_free(pWriter->zMalloc);
2382 fts3NodeFree(pWriter->pTree);
2383 sqlite3_free(pWriter);
2388 ** The first value in the apVal[] array is assumed to contain an integer.
2389 ** This function tests if there exist any documents with docid values that
2390 ** are different from that integer. i.e. if deleting the document with docid
2391 ** pRowid would mean the FTS3 table were empty.
2393 ** If successful, *pisEmpty is set to true if the table is empty except for
2394 ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
2395 ** error occurs, an SQLite error code is returned.
2397 static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
2398 sqlite3_stmt *pStmt;
2399 int rc;
2400 if( p->zContentTbl ){
2401 /* If using the content=xxx option, assume the table is never empty */
2402 *pisEmpty = 0;
2403 rc = SQLITE_OK;
2404 }else{
2405 rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
2406 if( rc==SQLITE_OK ){
2407 if( SQLITE_ROW==sqlite3_step(pStmt) ){
2408 *pisEmpty = sqlite3_column_int(pStmt, 0);
2410 rc = sqlite3_reset(pStmt);
2413 return rc;
2417 ** Set *pnMax to the largest segment level in the database for the index
2418 ** iIndex.
2420 ** Segment levels are stored in the 'level' column of the %_segdir table.
2422 ** Return SQLITE_OK if successful, or an SQLite error code if not.
2424 static int fts3SegmentMaxLevel(
2425 Fts3Table *p,
2426 int iLangid,
2427 int iIndex,
2428 sqlite3_int64 *pnMax
2430 sqlite3_stmt *pStmt;
2431 int rc;
2432 assert( iIndex>=0 && iIndex<p->nIndex );
2434 /* Set pStmt to the compiled version of:
2436 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
2438 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
2440 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
2441 if( rc!=SQLITE_OK ) return rc;
2442 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
2443 sqlite3_bind_int64(pStmt, 2,
2444 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
2446 if( SQLITE_ROW==sqlite3_step(pStmt) ){
2447 *pnMax = sqlite3_column_int64(pStmt, 0);
2449 return sqlite3_reset(pStmt);
2453 ** iAbsLevel is an absolute level that may be assumed to exist within
2454 ** the database. This function checks if it is the largest level number
2455 ** within its index. Assuming no error occurs, *pbMax is set to 1 if
2456 ** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
2457 ** is returned. If an error occurs, an error code is returned and the
2458 ** final value of *pbMax is undefined.
2460 static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
2462 /* Set pStmt to the compiled version of:
2464 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
2466 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
2468 sqlite3_stmt *pStmt;
2469 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
2470 if( rc!=SQLITE_OK ) return rc;
2471 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
2472 sqlite3_bind_int64(pStmt, 2,
2473 ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
2476 *pbMax = 0;
2477 if( SQLITE_ROW==sqlite3_step(pStmt) ){
2478 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
2480 return sqlite3_reset(pStmt);
2484 ** Delete all entries in the %_segments table associated with the segment
2485 ** opened with seg-reader pSeg. This function does not affect the contents
2486 ** of the %_segdir table.
2488 static int fts3DeleteSegment(
2489 Fts3Table *p, /* FTS table handle */
2490 Fts3SegReader *pSeg /* Segment to delete */
2492 int rc = SQLITE_OK; /* Return code */
2493 if( pSeg->iStartBlock ){
2494 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
2495 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
2496 if( rc==SQLITE_OK ){
2497 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
2498 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
2499 sqlite3_step(pDelete);
2500 rc = sqlite3_reset(pDelete);
2503 return rc;
2507 ** This function is used after merging multiple segments into a single large
2508 ** segment to delete the old, now redundant, segment b-trees. Specifically,
2509 ** it:
2511 ** 1) Deletes all %_segments entries for the segments associated with
2512 ** each of the SegReader objects in the array passed as the third
2513 ** argument, and
2515 ** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
2516 ** entries regardless of level if (iLevel<0).
2518 ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
2520 static int fts3DeleteSegdir(
2521 Fts3Table *p, /* Virtual table handle */
2522 int iLangid, /* Language id */
2523 int iIndex, /* Index for p->aIndex */
2524 int iLevel, /* Level of %_segdir entries to delete */
2525 Fts3SegReader **apSegment, /* Array of SegReader objects */
2526 int nReader /* Size of array apSegment */
2528 int rc = SQLITE_OK; /* Return Code */
2529 int i; /* Iterator variable */
2530 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
2532 for(i=0; rc==SQLITE_OK && i<nReader; i++){
2533 rc = fts3DeleteSegment(p, apSegment[i]);
2535 if( rc!=SQLITE_OK ){
2536 return rc;
2539 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
2540 if( iLevel==FTS3_SEGCURSOR_ALL ){
2541 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
2542 if( rc==SQLITE_OK ){
2543 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
2544 sqlite3_bind_int64(pDelete, 2,
2545 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
2548 }else{
2549 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
2550 if( rc==SQLITE_OK ){
2551 sqlite3_bind_int64(
2552 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
2557 if( rc==SQLITE_OK ){
2558 sqlite3_step(pDelete);
2559 rc = sqlite3_reset(pDelete);
2562 return rc;
2566 ** When this function is called, buffer *ppList (size *pnList bytes) contains
2567 ** a position list that may (or may not) feature multiple columns. This
2568 ** function adjusts the pointer *ppList and the length *pnList so that they
2569 ** identify the subset of the position list that corresponds to column iCol.
2571 ** If there are no entries in the input position list for column iCol, then
2572 ** *pnList is set to zero before returning.
2574 ** If parameter bZero is non-zero, then any part of the input list following
2575 ** the end of the output list is zeroed before returning.
2577 static void fts3ColumnFilter(
2578 int iCol, /* Column to filter on */
2579 int bZero, /* Zero out anything following *ppList */
2580 char **ppList, /* IN/OUT: Pointer to position list */
2581 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
2583 char *pList = *ppList;
2584 int nList = *pnList;
2585 char *pEnd = &pList[nList];
2586 int iCurrent = 0;
2587 char *p = pList;
2589 assert( iCol>=0 );
2590 while( 1 ){
2591 char c = 0;
2592 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
2594 if( iCol==iCurrent ){
2595 nList = (int)(p - pList);
2596 break;
2599 nList -= (int)(p - pList);
2600 pList = p;
2601 if( nList==0 ){
2602 break;
2604 p = &pList[1];
2605 p += fts3GetVarint32(p, &iCurrent);
2608 if( bZero && &pList[nList]!=pEnd ){
2609 memset(&pList[nList], 0, pEnd - &pList[nList]);
2611 *ppList = pList;
2612 *pnList = nList;
2616 ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
2617 ** existing data). Grow the buffer if required.
2619 ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
2620 ** trying to resize the buffer, return SQLITE_NOMEM.
2622 static int fts3MsrBufferData(
2623 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
2624 char *pList,
2625 int nList
2627 if( nList>pMsr->nBuffer ){
2628 char *pNew;
2629 pMsr->nBuffer = nList*2;
2630 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
2631 if( !pNew ) return SQLITE_NOMEM;
2632 pMsr->aBuffer = pNew;
2635 memcpy(pMsr->aBuffer, pList, nList);
2636 return SQLITE_OK;
2639 int sqlite3Fts3MsrIncrNext(
2640 Fts3Table *p, /* Virtual table handle */
2641 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
2642 sqlite3_int64 *piDocid, /* OUT: Docid value */
2643 char **paPoslist, /* OUT: Pointer to position list */
2644 int *pnPoslist /* OUT: Size of position list in bytes */
2646 int nMerge = pMsr->nAdvance;
2647 Fts3SegReader **apSegment = pMsr->apSegment;
2648 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
2649 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
2652 if( nMerge==0 ){
2653 *paPoslist = 0;
2654 return SQLITE_OK;
2657 while( 1 ){
2658 Fts3SegReader *pSeg;
2659 pSeg = pMsr->apSegment[0];
2661 if( pSeg->pOffsetList==0 ){
2662 *paPoslist = 0;
2663 break;
2664 }else{
2665 int rc;
2666 char *pList;
2667 int nList;
2668 int j;
2669 sqlite3_int64 iDocid = apSegment[0]->iDocid;
2671 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
2672 j = 1;
2673 while( rc==SQLITE_OK
2674 && j<nMerge
2675 && apSegment[j]->pOffsetList
2676 && apSegment[j]->iDocid==iDocid
2678 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
2679 j++;
2681 if( rc!=SQLITE_OK ) return rc;
2682 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
2684 if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
2685 rc = fts3MsrBufferData(pMsr, pList, nList+1);
2686 if( rc!=SQLITE_OK ) return rc;
2687 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
2688 pList = pMsr->aBuffer;
2691 if( pMsr->iColFilter>=0 ){
2692 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
2695 if( nList>0 ){
2696 *paPoslist = pList;
2697 *piDocid = iDocid;
2698 *pnPoslist = nList;
2699 break;
2704 return SQLITE_OK;
2707 static int fts3SegReaderStart(
2708 Fts3Table *p, /* Virtual table handle */
2709 Fts3MultiSegReader *pCsr, /* Cursor object */
2710 const char *zTerm, /* Term searched for (or NULL) */
2711 int nTerm /* Length of zTerm in bytes */
2713 int i;
2714 int nSeg = pCsr->nSegment;
2716 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
2717 ** for, then advance each segment iterator until it points to a term of
2718 ** equal or greater value than the specified term. This prevents many
2719 ** unnecessary merge/sort operations for the case where single segment
2720 ** b-tree leaf nodes contain more than one term.
2722 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
2723 int res = 0;
2724 Fts3SegReader *pSeg = pCsr->apSegment[i];
2725 do {
2726 int rc = fts3SegReaderNext(p, pSeg, 0);
2727 if( rc!=SQLITE_OK ) return rc;
2728 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
2730 if( pSeg->bLookup && res!=0 ){
2731 fts3SegReaderSetEof(pSeg);
2734 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
2736 return SQLITE_OK;
2739 int sqlite3Fts3SegReaderStart(
2740 Fts3Table *p, /* Virtual table handle */
2741 Fts3MultiSegReader *pCsr, /* Cursor object */
2742 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
2744 pCsr->pFilter = pFilter;
2745 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
2748 int sqlite3Fts3MsrIncrStart(
2749 Fts3Table *p, /* Virtual table handle */
2750 Fts3MultiSegReader *pCsr, /* Cursor object */
2751 int iCol, /* Column to match on. */
2752 const char *zTerm, /* Term to iterate through a doclist for */
2753 int nTerm /* Number of bytes in zTerm */
2755 int i;
2756 int rc;
2757 int nSegment = pCsr->nSegment;
2758 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
2759 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
2762 assert( pCsr->pFilter==0 );
2763 assert( zTerm && nTerm>0 );
2765 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
2766 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
2767 if( rc!=SQLITE_OK ) return rc;
2769 /* Determine how many of the segments actually point to zTerm/nTerm. */
2770 for(i=0; i<nSegment; i++){
2771 Fts3SegReader *pSeg = pCsr->apSegment[i];
2772 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
2773 break;
2776 pCsr->nAdvance = i;
2778 /* Advance each of the segments to point to the first docid. */
2779 for(i=0; i<pCsr->nAdvance; i++){
2780 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
2781 if( rc!=SQLITE_OK ) return rc;
2783 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
2785 assert( iCol<0 || iCol<p->nColumn );
2786 pCsr->iColFilter = iCol;
2788 return SQLITE_OK;
2792 ** This function is called on a MultiSegReader that has been started using
2793 ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
2794 ** have been made. Calling this function puts the MultiSegReader in such
2795 ** a state that if the next two calls are:
2797 ** sqlite3Fts3SegReaderStart()
2798 ** sqlite3Fts3SegReaderStep()
2800 ** then the entire doclist for the term is available in
2801 ** MultiSegReader.aDoclist/nDoclist.
2803 int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
2804 int i; /* Used to iterate through segment-readers */
2806 assert( pCsr->zTerm==0 );
2807 assert( pCsr->nTerm==0 );
2808 assert( pCsr->aDoclist==0 );
2809 assert( pCsr->nDoclist==0 );
2811 pCsr->nAdvance = 0;
2812 pCsr->bRestart = 1;
2813 for(i=0; i<pCsr->nSegment; i++){
2814 pCsr->apSegment[i]->pOffsetList = 0;
2815 pCsr->apSegment[i]->nOffsetList = 0;
2816 pCsr->apSegment[i]->iDocid = 0;
2819 return SQLITE_OK;
2823 int sqlite3Fts3SegReaderStep(
2824 Fts3Table *p, /* Virtual table handle */
2825 Fts3MultiSegReader *pCsr /* Cursor object */
2827 int rc = SQLITE_OK;
2829 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
2830 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
2831 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
2832 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
2833 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
2834 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
2836 Fts3SegReader **apSegment = pCsr->apSegment;
2837 int nSegment = pCsr->nSegment;
2838 Fts3SegFilter *pFilter = pCsr->pFilter;
2839 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
2840 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
2843 if( pCsr->nSegment==0 ) return SQLITE_OK;
2845 do {
2846 int nMerge;
2847 int i;
2849 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
2850 ** forward. Then sort the list in order of current term again.
2852 for(i=0; i<pCsr->nAdvance; i++){
2853 Fts3SegReader *pSeg = apSegment[i];
2854 if( pSeg->bLookup ){
2855 fts3SegReaderSetEof(pSeg);
2856 }else{
2857 rc = fts3SegReaderNext(p, pSeg, 0);
2859 if( rc!=SQLITE_OK ) return rc;
2861 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
2862 pCsr->nAdvance = 0;
2864 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
2865 assert( rc==SQLITE_OK );
2866 if( apSegment[0]->aNode==0 ) break;
2868 pCsr->nTerm = apSegment[0]->nTerm;
2869 pCsr->zTerm = apSegment[0]->zTerm;
2871 /* If this is a prefix-search, and if the term that apSegment[0] points
2872 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
2873 ** required callbacks have been made. In this case exit early.
2875 ** Similarly, if this is a search for an exact match, and the first term
2876 ** of segment apSegment[0] is not a match, exit early.
2878 if( pFilter->zTerm && !isScan ){
2879 if( pCsr->nTerm<pFilter->nTerm
2880 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
2881 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
2883 break;
2887 nMerge = 1;
2888 while( nMerge<nSegment
2889 && apSegment[nMerge]->aNode
2890 && apSegment[nMerge]->nTerm==pCsr->nTerm
2891 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
2893 nMerge++;
2896 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
2897 if( nMerge==1
2898 && !isIgnoreEmpty
2899 && !isFirst
2900 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
2902 pCsr->nDoclist = apSegment[0]->nDoclist;
2903 if( fts3SegReaderIsPending(apSegment[0]) ){
2904 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
2905 pCsr->aDoclist = pCsr->aBuffer;
2906 }else{
2907 pCsr->aDoclist = apSegment[0]->aDoclist;
2909 if( rc==SQLITE_OK ) rc = SQLITE_ROW;
2910 }else{
2911 int nDoclist = 0; /* Size of doclist */
2912 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
2914 /* The current term of the first nMerge entries in the array
2915 ** of Fts3SegReader objects is the same. The doclists must be merged
2916 ** and a single term returned with the merged doclist.
2918 for(i=0; i<nMerge; i++){
2919 fts3SegReaderFirstDocid(p, apSegment[i]);
2921 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
2922 while( apSegment[0]->pOffsetList ){
2923 int j; /* Number of segments that share a docid */
2924 char *pList = 0;
2925 int nList = 0;
2926 int nByte;
2927 sqlite3_int64 iDocid = apSegment[0]->iDocid;
2928 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
2929 j = 1;
2930 while( j<nMerge
2931 && apSegment[j]->pOffsetList
2932 && apSegment[j]->iDocid==iDocid
2934 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
2935 j++;
2938 if( isColFilter ){
2939 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
2942 if( !isIgnoreEmpty || nList>0 ){
2944 /* Calculate the 'docid' delta value to write into the merged
2945 ** doclist. */
2946 sqlite3_int64 iDelta;
2947 if( p->bDescIdx && nDoclist>0 ){
2948 iDelta = iPrev - iDocid;
2949 }else{
2950 iDelta = iDocid - iPrev;
2952 assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
2953 assert( nDoclist>0 || iDelta==iDocid );
2955 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
2956 if( nDoclist+nByte>pCsr->nBuffer ){
2957 char *aNew;
2958 pCsr->nBuffer = (nDoclist+nByte)*2;
2959 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
2960 if( !aNew ){
2961 return SQLITE_NOMEM;
2963 pCsr->aBuffer = aNew;
2966 if( isFirst ){
2967 char *a = &pCsr->aBuffer[nDoclist];
2968 int nWrite;
2970 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
2971 if( nWrite ){
2972 iPrev = iDocid;
2973 nDoclist += nWrite;
2975 }else{
2976 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
2977 iPrev = iDocid;
2978 if( isRequirePos ){
2979 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
2980 nDoclist += nList;
2981 pCsr->aBuffer[nDoclist++] = '\0';
2986 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
2988 if( nDoclist>0 ){
2989 pCsr->aDoclist = pCsr->aBuffer;
2990 pCsr->nDoclist = nDoclist;
2991 rc = SQLITE_ROW;
2994 pCsr->nAdvance = nMerge;
2995 }while( rc==SQLITE_OK );
2997 return rc;
3001 void sqlite3Fts3SegReaderFinish(
3002 Fts3MultiSegReader *pCsr /* Cursor object */
3004 if( pCsr ){
3005 int i;
3006 for(i=0; i<pCsr->nSegment; i++){
3007 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
3009 sqlite3_free(pCsr->apSegment);
3010 sqlite3_free(pCsr->aBuffer);
3012 pCsr->nSegment = 0;
3013 pCsr->apSegment = 0;
3014 pCsr->aBuffer = 0;
3019 ** Decode the "end_block" field, selected by column iCol of the SELECT
3020 ** statement passed as the first argument.
3022 ** The "end_block" field may contain either an integer, or a text field
3023 ** containing the text representation of two non-negative integers separated
3024 ** by one or more space (0x20) characters. In the first case, set *piEndBlock
3025 ** to the integer value and *pnByte to zero before returning. In the second,
3026 ** set *piEndBlock to the first value and *pnByte to the second.
3028 static void fts3ReadEndBlockField(
3029 sqlite3_stmt *pStmt,
3030 int iCol,
3031 i64 *piEndBlock,
3032 i64 *pnByte
3034 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
3035 if( zText ){
3036 int i;
3037 int iMul = 1;
3038 i64 iVal = 0;
3039 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
3040 iVal = iVal*10 + (zText[i] - '0');
3042 *piEndBlock = iVal;
3043 while( zText[i]==' ' ) i++;
3044 iVal = 0;
3045 if( zText[i]=='-' ){
3046 i++;
3047 iMul = -1;
3049 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
3050 iVal = iVal*10 + (zText[i] - '0');
3052 *pnByte = (iVal * (i64)iMul);
3058 ** A segment of size nByte bytes has just been written to absolute level
3059 ** iAbsLevel. Promote any segments that should be promoted as a result.
3061 static int fts3PromoteSegments(
3062 Fts3Table *p, /* FTS table handle */
3063 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
3064 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
3066 int rc = SQLITE_OK;
3067 sqlite3_stmt *pRange;
3069 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
3071 if( rc==SQLITE_OK ){
3072 int bOk = 0;
3073 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
3074 i64 nLimit = (nByte*3)/2;
3076 /* Loop through all entries in the %_segdir table corresponding to
3077 ** segments in this index on levels greater than iAbsLevel. If there is
3078 ** at least one such segment, and it is possible to determine that all
3079 ** such segments are smaller than nLimit bytes in size, they will be
3080 ** promoted to level iAbsLevel. */
3081 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
3082 sqlite3_bind_int64(pRange, 2, iLast);
3083 while( SQLITE_ROW==sqlite3_step(pRange) ){
3084 i64 nSize = 0, dummy;
3085 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
3086 if( nSize<=0 || nSize>nLimit ){
3087 /* If nSize==0, then the %_segdir.end_block field does not not
3088 ** contain a size value. This happens if it was written by an
3089 ** old version of FTS. In this case it is not possible to determine
3090 ** the size of the segment, and so segment promotion does not
3091 ** take place. */
3092 bOk = 0;
3093 break;
3095 bOk = 1;
3097 rc = sqlite3_reset(pRange);
3099 if( bOk ){
3100 int iIdx = 0;
3101 sqlite3_stmt *pUpdate1 = 0;
3102 sqlite3_stmt *pUpdate2 = 0;
3104 if( rc==SQLITE_OK ){
3105 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
3107 if( rc==SQLITE_OK ){
3108 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
3111 if( rc==SQLITE_OK ){
3113 /* Loop through all %_segdir entries for segments in this index with
3114 ** levels equal to or greater than iAbsLevel. As each entry is visited,
3115 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
3116 ** oldest segment in the range, 1 for the next oldest, and so on.
3118 ** In other words, move all segments being promoted to level -1,
3119 ** setting the "idx" fields as appropriate to keep them in the same
3120 ** order. The contents of level -1 (which is never used, except
3121 ** transiently here), will be moved back to level iAbsLevel below. */
3122 sqlite3_bind_int64(pRange, 1, iAbsLevel);
3123 while( SQLITE_ROW==sqlite3_step(pRange) ){
3124 sqlite3_bind_int(pUpdate1, 1, iIdx++);
3125 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
3126 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
3127 sqlite3_step(pUpdate1);
3128 rc = sqlite3_reset(pUpdate1);
3129 if( rc!=SQLITE_OK ){
3130 sqlite3_reset(pRange);
3131 break;
3135 if( rc==SQLITE_OK ){
3136 rc = sqlite3_reset(pRange);
3139 /* Move level -1 to level iAbsLevel */
3140 if( rc==SQLITE_OK ){
3141 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
3142 sqlite3_step(pUpdate2);
3143 rc = sqlite3_reset(pUpdate2);
3149 return rc;
3153 ** Merge all level iLevel segments in the database into a single
3154 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
3155 ** single segment with a level equal to the numerically largest level
3156 ** currently present in the database.
3158 ** If this function is called with iLevel<0, but there is only one
3159 ** segment in the database, SQLITE_DONE is returned immediately.
3160 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
3161 ** an SQLite error code is returned.
3163 static int fts3SegmentMerge(
3164 Fts3Table *p,
3165 int iLangid, /* Language id to merge */
3166 int iIndex, /* Index in p->aIndex[] to merge */
3167 int iLevel /* Level to merge */
3169 int rc; /* Return code */
3170 int iIdx = 0; /* Index of new segment */
3171 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
3172 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
3173 Fts3SegFilter filter; /* Segment term filter condition */
3174 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
3175 int bIgnoreEmpty = 0; /* True to ignore empty segments */
3176 i64 iMaxLevel = 0; /* Max level number for this index/langid */
3178 assert( iLevel==FTS3_SEGCURSOR_ALL
3179 || iLevel==FTS3_SEGCURSOR_PENDING
3180 || iLevel>=0
3182 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
3183 assert( iIndex>=0 && iIndex<p->nIndex );
3185 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
3186 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
3188 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
3189 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
3190 if( rc!=SQLITE_OK ) goto finished;
3193 if( iLevel==FTS3_SEGCURSOR_ALL ){
3194 /* This call is to merge all segments in the database to a single
3195 ** segment. The level of the new segment is equal to the numerically
3196 ** greatest segment level currently present in the database for this
3197 ** index. The idx of the new segment is always 0. */
3198 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
3199 rc = SQLITE_DONE;
3200 goto finished;
3202 iNewLevel = iMaxLevel;
3203 bIgnoreEmpty = 1;
3205 }else{
3206 /* This call is to merge all segments at level iLevel. find the next
3207 ** available segment index at level iLevel+1. The call to
3208 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
3209 ** a single iLevel+2 segment if necessary. */
3210 assert( FTS3_SEGCURSOR_PENDING==-1 );
3211 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
3212 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
3213 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
3215 if( rc!=SQLITE_OK ) goto finished;
3217 assert( csr.nSegment>0 );
3218 assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
3219 assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
3221 memset(&filter, 0, sizeof(Fts3SegFilter));
3222 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
3223 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
3225 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
3226 while( SQLITE_OK==rc ){
3227 rc = sqlite3Fts3SegReaderStep(p, &csr);
3228 if( rc!=SQLITE_ROW ) break;
3229 rc = fts3SegWriterAdd(p, &pWriter, 1,
3230 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
3232 if( rc!=SQLITE_OK ) goto finished;
3233 assert( pWriter || bIgnoreEmpty );
3235 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
3236 rc = fts3DeleteSegdir(
3237 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
3239 if( rc!=SQLITE_OK ) goto finished;
3241 if( pWriter ){
3242 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
3243 if( rc==SQLITE_OK ){
3244 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
3245 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
3250 finished:
3251 fts3SegWriterFree(pWriter);
3252 sqlite3Fts3SegReaderFinish(&csr);
3253 return rc;
3258 ** Flush the contents of pendingTerms to level 0 segments.
3260 int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
3261 int rc = SQLITE_OK;
3262 int i;
3264 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
3265 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
3266 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
3268 sqlite3Fts3PendingTermsClear(p);
3270 /* Determine the auto-incr-merge setting if unknown. If enabled,
3271 ** estimate the number of leaf blocks of content to be written
3273 if( rc==SQLITE_OK && p->bHasStat
3274 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
3276 sqlite3_stmt *pStmt = 0;
3277 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
3278 if( rc==SQLITE_OK ){
3279 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
3280 rc = sqlite3_step(pStmt);
3281 if( rc==SQLITE_ROW ){
3282 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
3283 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
3284 }else if( rc==SQLITE_DONE ){
3285 p->nAutoincrmerge = 0;
3287 rc = sqlite3_reset(pStmt);
3290 return rc;
3294 ** Encode N integers as varints into a blob.
3296 static void fts3EncodeIntArray(
3297 int N, /* The number of integers to encode */
3298 u32 *a, /* The integer values */
3299 char *zBuf, /* Write the BLOB here */
3300 int *pNBuf /* Write number of bytes if zBuf[] used here */
3302 int i, j;
3303 for(i=j=0; i<N; i++){
3304 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
3306 *pNBuf = j;
3310 ** Decode a blob of varints into N integers
3312 static void fts3DecodeIntArray(
3313 int N, /* The number of integers to decode */
3314 u32 *a, /* Write the integer values */
3315 const char *zBuf, /* The BLOB containing the varints */
3316 int nBuf /* size of the BLOB */
3318 int i, j;
3319 UNUSED_PARAMETER(nBuf);
3320 for(i=j=0; i<N; i++){
3321 sqlite3_int64 x;
3322 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
3323 assert(j<=nBuf);
3324 a[i] = (u32)(x & 0xffffffff);
3329 ** Insert the sizes (in tokens) for each column of the document
3330 ** with docid equal to p->iPrevDocid. The sizes are encoded as
3331 ** a blob of varints.
3333 static void fts3InsertDocsize(
3334 int *pRC, /* Result code */
3335 Fts3Table *p, /* Table into which to insert */
3336 u32 *aSz /* Sizes of each column, in tokens */
3338 char *pBlob; /* The BLOB encoding of the document size */
3339 int nBlob; /* Number of bytes in the BLOB */
3340 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
3341 int rc; /* Result code from subfunctions */
3343 if( *pRC ) return;
3344 pBlob = sqlite3_malloc( 10*p->nColumn );
3345 if( pBlob==0 ){
3346 *pRC = SQLITE_NOMEM;
3347 return;
3349 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
3350 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
3351 if( rc ){
3352 sqlite3_free(pBlob);
3353 *pRC = rc;
3354 return;
3356 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
3357 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
3358 sqlite3_step(pStmt);
3359 *pRC = sqlite3_reset(pStmt);
3363 ** Record 0 of the %_stat table contains a blob consisting of N varints,
3364 ** where N is the number of user defined columns in the fts3 table plus
3365 ** two. If nCol is the number of user defined columns, then values of the
3366 ** varints are set as follows:
3368 ** Varint 0: Total number of rows in the table.
3370 ** Varint 1..nCol: For each column, the total number of tokens stored in
3371 ** the column for all rows of the table.
3373 ** Varint 1+nCol: The total size, in bytes, of all text values in all
3374 ** columns of all rows of the table.
3377 static void fts3UpdateDocTotals(
3378 int *pRC, /* The result code */
3379 Fts3Table *p, /* Table being updated */
3380 u32 *aSzIns, /* Size increases */
3381 u32 *aSzDel, /* Size decreases */
3382 int nChng /* Change in the number of documents */
3384 char *pBlob; /* Storage for BLOB written into %_stat */
3385 int nBlob; /* Size of BLOB written into %_stat */
3386 u32 *a; /* Array of integers that becomes the BLOB */
3387 sqlite3_stmt *pStmt; /* Statement for reading and writing */
3388 int i; /* Loop counter */
3389 int rc; /* Result code from subfunctions */
3391 const int nStat = p->nColumn+2;
3393 if( *pRC ) return;
3394 a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
3395 if( a==0 ){
3396 *pRC = SQLITE_NOMEM;
3397 return;
3399 pBlob = (char*)&a[nStat];
3400 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
3401 if( rc ){
3402 sqlite3_free(a);
3403 *pRC = rc;
3404 return;
3406 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
3407 if( sqlite3_step(pStmt)==SQLITE_ROW ){
3408 fts3DecodeIntArray(nStat, a,
3409 sqlite3_column_blob(pStmt, 0),
3410 sqlite3_column_bytes(pStmt, 0));
3411 }else{
3412 memset(a, 0, sizeof(u32)*(nStat) );
3414 rc = sqlite3_reset(pStmt);
3415 if( rc!=SQLITE_OK ){
3416 sqlite3_free(a);
3417 *pRC = rc;
3418 return;
3420 if( nChng<0 && a[0]<(u32)(-nChng) ){
3421 a[0] = 0;
3422 }else{
3423 a[0] += nChng;
3425 for(i=0; i<p->nColumn+1; i++){
3426 u32 x = a[i+1];
3427 if( x+aSzIns[i] < aSzDel[i] ){
3428 x = 0;
3429 }else{
3430 x = x + aSzIns[i] - aSzDel[i];
3432 a[i+1] = x;
3434 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
3435 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
3436 if( rc ){
3437 sqlite3_free(a);
3438 *pRC = rc;
3439 return;
3441 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
3442 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
3443 sqlite3_step(pStmt);
3444 *pRC = sqlite3_reset(pStmt);
3445 sqlite3_free(a);
3449 ** Merge the entire database so that there is one segment for each
3450 ** iIndex/iLangid combination.
3452 static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
3453 int bSeenDone = 0;
3454 int rc;
3455 sqlite3_stmt *pAllLangid = 0;
3457 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
3458 if( rc==SQLITE_OK ){
3459 int rc2;
3460 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
3461 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
3462 while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
3463 int i;
3464 int iLangid = sqlite3_column_int(pAllLangid, 0);
3465 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
3466 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
3467 if( rc==SQLITE_DONE ){
3468 bSeenDone = 1;
3469 rc = SQLITE_OK;
3473 rc2 = sqlite3_reset(pAllLangid);
3474 if( rc==SQLITE_OK ) rc = rc2;
3477 sqlite3Fts3SegmentsClose(p);
3478 sqlite3Fts3PendingTermsClear(p);
3480 return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
3484 ** This function is called when the user executes the following statement:
3486 ** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
3488 ** The entire FTS index is discarded and rebuilt. If the table is one
3489 ** created using the content=xxx option, then the new index is based on
3490 ** the current contents of the xxx table. Otherwise, it is rebuilt based
3491 ** on the contents of the %_content table.
3493 static int fts3DoRebuild(Fts3Table *p){
3494 int rc; /* Return Code */
3496 rc = fts3DeleteAll(p, 0);
3497 if( rc==SQLITE_OK ){
3498 u32 *aSz = 0;
3499 u32 *aSzIns = 0;
3500 u32 *aSzDel = 0;
3501 sqlite3_stmt *pStmt = 0;
3502 int nEntry = 0;
3504 /* Compose and prepare an SQL statement to loop through the content table */
3505 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
3506 if( !zSql ){
3507 rc = SQLITE_NOMEM;
3508 }else{
3509 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3510 sqlite3_free(zSql);
3513 if( rc==SQLITE_OK ){
3514 int nByte = sizeof(u32) * (p->nColumn+1)*3;
3515 aSz = (u32 *)sqlite3_malloc(nByte);
3516 if( aSz==0 ){
3517 rc = SQLITE_NOMEM;
3518 }else{
3519 memset(aSz, 0, nByte);
3520 aSzIns = &aSz[p->nColumn+1];
3521 aSzDel = &aSzIns[p->nColumn+1];
3525 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
3526 int iCol;
3527 int iLangid = langidFromSelect(p, pStmt);
3528 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
3529 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
3530 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
3531 if( p->abNotindexed[iCol]==0 ){
3532 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
3533 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
3534 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
3537 if( p->bHasDocsize ){
3538 fts3InsertDocsize(&rc, p, aSz);
3540 if( rc!=SQLITE_OK ){
3541 sqlite3_finalize(pStmt);
3542 pStmt = 0;
3543 }else{
3544 nEntry++;
3545 for(iCol=0; iCol<=p->nColumn; iCol++){
3546 aSzIns[iCol] += aSz[iCol];
3550 if( p->bFts4 ){
3551 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
3553 sqlite3_free(aSz);
3555 if( pStmt ){
3556 int rc2 = sqlite3_finalize(pStmt);
3557 if( rc==SQLITE_OK ){
3558 rc = rc2;
3563 return rc;
3568 ** This function opens a cursor used to read the input data for an
3569 ** incremental merge operation. Specifically, it opens a cursor to scan
3570 ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
3571 ** level iAbsLevel.
3573 static int fts3IncrmergeCsr(
3574 Fts3Table *p, /* FTS3 table handle */
3575 sqlite3_int64 iAbsLevel, /* Absolute level to open */
3576 int nSeg, /* Number of segments to merge */
3577 Fts3MultiSegReader *pCsr /* Cursor object to populate */
3579 int rc; /* Return Code */
3580 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
3581 int nByte; /* Bytes allocated at pCsr->apSegment[] */
3583 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
3584 memset(pCsr, 0, sizeof(*pCsr));
3585 nByte = sizeof(Fts3SegReader *) * nSeg;
3586 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
3588 if( pCsr->apSegment==0 ){
3589 rc = SQLITE_NOMEM;
3590 }else{
3591 memset(pCsr->apSegment, 0, nByte);
3592 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
3594 if( rc==SQLITE_OK ){
3595 int i;
3596 int rc2;
3597 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
3598 assert( pCsr->nSegment==0 );
3599 for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
3600 rc = sqlite3Fts3SegReaderNew(i, 0,
3601 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
3602 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
3603 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
3604 sqlite3_column_blob(pStmt, 4), /* segdir.root */
3605 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
3606 &pCsr->apSegment[i]
3608 pCsr->nSegment++;
3610 rc2 = sqlite3_reset(pStmt);
3611 if( rc==SQLITE_OK ) rc = rc2;
3614 return rc;
3617 typedef struct IncrmergeWriter IncrmergeWriter;
3618 typedef struct NodeWriter NodeWriter;
3619 typedef struct Blob Blob;
3620 typedef struct NodeReader NodeReader;
3623 ** An instance of the following structure is used as a dynamic buffer
3624 ** to build up nodes or other blobs of data in.
3626 ** The function blobGrowBuffer() is used to extend the allocation.
3628 struct Blob {
3629 char *a; /* Pointer to allocation */
3630 int n; /* Number of valid bytes of data in a[] */
3631 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
3635 ** This structure is used to build up buffers containing segment b-tree
3636 ** nodes (blocks).
3638 struct NodeWriter {
3639 sqlite3_int64 iBlock; /* Current block id */
3640 Blob key; /* Last key written to the current block */
3641 Blob block; /* Current block image */
3645 ** An object of this type contains the state required to create or append
3646 ** to an appendable b-tree segment.
3648 struct IncrmergeWriter {
3649 int nLeafEst; /* Space allocated for leaf blocks */
3650 int nWork; /* Number of leaf pages flushed */
3651 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
3652 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
3653 sqlite3_int64 iStart; /* Block number of first allocated block */
3654 sqlite3_int64 iEnd; /* Block number of last allocated block */
3655 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
3656 u8 bNoLeafData; /* If true, store 0 for segment size */
3657 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
3661 ** An object of the following type is used to read data from a single
3662 ** FTS segment node. See the following functions:
3664 ** nodeReaderInit()
3665 ** nodeReaderNext()
3666 ** nodeReaderRelease()
3668 struct NodeReader {
3669 const char *aNode;
3670 int nNode;
3671 int iOff; /* Current offset within aNode[] */
3673 /* Output variables. Containing the current node entry. */
3674 sqlite3_int64 iChild; /* Pointer to child node */
3675 Blob term; /* Current term */
3676 const char *aDoclist; /* Pointer to doclist */
3677 int nDoclist; /* Size of doclist in bytes */
3681 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
3682 ** Otherwise, if the allocation at pBlob->a is not already at least nMin
3683 ** bytes in size, extend (realloc) it to be so.
3685 ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
3686 ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
3687 ** to reflect the new size of the pBlob->a[] buffer.
3689 static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
3690 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
3691 int nAlloc = nMin;
3692 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
3693 if( a ){
3694 pBlob->nAlloc = nAlloc;
3695 pBlob->a = a;
3696 }else{
3697 *pRc = SQLITE_NOMEM;
3703 ** Attempt to advance the node-reader object passed as the first argument to
3704 ** the next entry on the node.
3706 ** Return an error code if an error occurs (SQLITE_NOMEM is possible).
3707 ** Otherwise return SQLITE_OK. If there is no next entry on the node
3708 ** (e.g. because the current entry is the last) set NodeReader->aNode to
3709 ** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
3710 ** variables for the new entry.
3712 static int nodeReaderNext(NodeReader *p){
3713 int bFirst = (p->term.n==0); /* True for first term on the node */
3714 int nPrefix = 0; /* Bytes to copy from previous term */
3715 int nSuffix = 0; /* Bytes to append to the prefix */
3716 int rc = SQLITE_OK; /* Return code */
3718 assert( p->aNode );
3719 if( p->iChild && bFirst==0 ) p->iChild++;
3720 if( p->iOff>=p->nNode ){
3721 /* EOF */
3722 p->aNode = 0;
3723 }else{
3724 if( bFirst==0 ){
3725 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
3727 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
3729 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
3730 if( rc==SQLITE_OK ){
3731 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
3732 p->term.n = nPrefix+nSuffix;
3733 p->iOff += nSuffix;
3734 if( p->iChild==0 ){
3735 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
3736 p->aDoclist = &p->aNode[p->iOff];
3737 p->iOff += p->nDoclist;
3742 assert( p->iOff<=p->nNode );
3744 return rc;
3748 ** Release all dynamic resources held by node-reader object *p.
3750 static void nodeReaderRelease(NodeReader *p){
3751 sqlite3_free(p->term.a);
3755 ** Initialize a node-reader object to read the node in buffer aNode/nNode.
3757 ** If successful, SQLITE_OK is returned and the NodeReader object set to
3758 ** point to the first entry on the node (if any). Otherwise, an SQLite
3759 ** error code is returned.
3761 static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
3762 memset(p, 0, sizeof(NodeReader));
3763 p->aNode = aNode;
3764 p->nNode = nNode;
3766 /* Figure out if this is a leaf or an internal node. */
3767 if( p->aNode[0] ){
3768 /* An internal node. */
3769 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
3770 }else{
3771 p->iOff = 1;
3774 return nodeReaderNext(p);
3778 ** This function is called while writing an FTS segment each time a leaf o
3779 ** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
3780 ** to be greater than the largest key on the node just written, but smaller
3781 ** than or equal to the first key that will be written to the next leaf
3782 ** node.
3784 ** The block id of the leaf node just written to disk may be found in
3785 ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
3787 static int fts3IncrmergePush(
3788 Fts3Table *p, /* Fts3 table handle */
3789 IncrmergeWriter *pWriter, /* Writer object */
3790 const char *zTerm, /* Term to write to internal node */
3791 int nTerm /* Bytes at zTerm */
3793 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
3794 int iLayer;
3796 assert( nTerm>0 );
3797 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
3798 sqlite3_int64 iNextPtr = 0;
3799 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
3800 int rc = SQLITE_OK;
3801 int nPrefix;
3802 int nSuffix;
3803 int nSpace;
3805 /* Figure out how much space the key will consume if it is written to
3806 ** the current node of layer iLayer. Due to the prefix compression,
3807 ** the space required changes depending on which node the key is to
3808 ** be added to. */
3809 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
3810 nSuffix = nTerm - nPrefix;
3811 nSpace = sqlite3Fts3VarintLen(nPrefix);
3812 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
3814 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
3815 /* If the current node of layer iLayer contains zero keys, or if adding
3816 ** the key to it will not cause it to grow to larger than nNodeSize
3817 ** bytes in size, write the key here. */
3819 Blob *pBlk = &pNode->block;
3820 if( pBlk->n==0 ){
3821 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
3822 if( rc==SQLITE_OK ){
3823 pBlk->a[0] = (char)iLayer;
3824 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
3827 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
3828 blobGrowBuffer(&pNode->key, nTerm, &rc);
3830 if( rc==SQLITE_OK ){
3831 if( pNode->key.n ){
3832 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
3834 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
3835 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
3836 pBlk->n += nSuffix;
3838 memcpy(pNode->key.a, zTerm, nTerm);
3839 pNode->key.n = nTerm;
3841 }else{
3842 /* Otherwise, flush the current node of layer iLayer to disk.
3843 ** Then allocate a new, empty sibling node. The key will be written
3844 ** into the parent of this node. */
3845 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
3847 assert( pNode->block.nAlloc>=p->nNodeSize );
3848 pNode->block.a[0] = (char)iLayer;
3849 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
3851 iNextPtr = pNode->iBlock;
3852 pNode->iBlock++;
3853 pNode->key.n = 0;
3856 if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
3857 iPtr = iNextPtr;
3860 assert( 0 );
3861 return 0;
3865 ** Append a term and (optionally) doclist to the FTS segment node currently
3866 ** stored in blob *pNode. The node need not contain any terms, but the
3867 ** header must be written before this function is called.
3869 ** A node header is a single 0x00 byte for a leaf node, or a height varint
3870 ** followed by the left-hand-child varint for an internal node.
3872 ** The term to be appended is passed via arguments zTerm/nTerm. For a
3873 ** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
3874 ** node, both aDoclist and nDoclist must be passed 0.
3876 ** If the size of the value in blob pPrev is zero, then this is the first
3877 ** term written to the node. Otherwise, pPrev contains a copy of the
3878 ** previous term. Before this function returns, it is updated to contain a
3879 ** copy of zTerm/nTerm.
3881 ** It is assumed that the buffer associated with pNode is already large
3882 ** enough to accommodate the new entry. The buffer associated with pPrev
3883 ** is extended by this function if requrired.
3885 ** If an error (i.e. OOM condition) occurs, an SQLite error code is
3886 ** returned. Otherwise, SQLITE_OK.
3888 static int fts3AppendToNode(
3889 Blob *pNode, /* Current node image to append to */
3890 Blob *pPrev, /* Buffer containing previous term written */
3891 const char *zTerm, /* New term to write */
3892 int nTerm, /* Size of zTerm in bytes */
3893 const char *aDoclist, /* Doclist (or NULL) to write */
3894 int nDoclist /* Size of aDoclist in bytes */
3896 int rc = SQLITE_OK; /* Return code */
3897 int bFirst = (pPrev->n==0); /* True if this is the first term written */
3898 int nPrefix; /* Size of term prefix in bytes */
3899 int nSuffix; /* Size of term suffix in bytes */
3901 /* Node must have already been started. There must be a doclist for a
3902 ** leaf node, and there must not be a doclist for an internal node. */
3903 assert( pNode->n>0 );
3904 assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
3906 blobGrowBuffer(pPrev, nTerm, &rc);
3907 if( rc!=SQLITE_OK ) return rc;
3909 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
3910 nSuffix = nTerm - nPrefix;
3911 memcpy(pPrev->a, zTerm, nTerm);
3912 pPrev->n = nTerm;
3914 if( bFirst==0 ){
3915 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
3917 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
3918 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
3919 pNode->n += nSuffix;
3921 if( aDoclist ){
3922 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
3923 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
3924 pNode->n += nDoclist;
3927 assert( pNode->n<=pNode->nAlloc );
3929 return SQLITE_OK;
3933 ** Append the current term and doclist pointed to by cursor pCsr to the
3934 ** appendable b-tree segment opened for writing by pWriter.
3936 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
3938 static int fts3IncrmergeAppend(
3939 Fts3Table *p, /* Fts3 table handle */
3940 IncrmergeWriter *pWriter, /* Writer object */
3941 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
3943 const char *zTerm = pCsr->zTerm;
3944 int nTerm = pCsr->nTerm;
3945 const char *aDoclist = pCsr->aDoclist;
3946 int nDoclist = pCsr->nDoclist;
3947 int rc = SQLITE_OK; /* Return code */
3948 int nSpace; /* Total space in bytes required on leaf */
3949 int nPrefix; /* Size of prefix shared with previous term */
3950 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
3951 NodeWriter *pLeaf; /* Object used to write leaf nodes */
3953 pLeaf = &pWriter->aNodeWriter[0];
3954 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
3955 nSuffix = nTerm - nPrefix;
3957 nSpace = sqlite3Fts3VarintLen(nPrefix);
3958 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
3959 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
3961 /* If the current block is not empty, and if adding this term/doclist
3962 ** to the current block would make it larger than Fts3Table.nNodeSize
3963 ** bytes, write this block out to the database. */
3964 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
3965 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
3966 pWriter->nWork++;
3968 /* Add the current term to the parent node. The term added to the
3969 ** parent must:
3971 ** a) be greater than the largest term on the leaf node just written
3972 ** to the database (still available in pLeaf->key), and
3974 ** b) be less than or equal to the term about to be added to the new
3975 ** leaf node (zTerm/nTerm).
3977 ** In other words, it must be the prefix of zTerm 1 byte longer than
3978 ** the common prefix (if any) of zTerm and pWriter->zTerm.
3980 if( rc==SQLITE_OK ){
3981 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
3984 /* Advance to the next output block */
3985 pLeaf->iBlock++;
3986 pLeaf->key.n = 0;
3987 pLeaf->block.n = 0;
3989 nSuffix = nTerm;
3990 nSpace = 1;
3991 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
3992 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
3995 pWriter->nLeafData += nSpace;
3996 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
3997 if( rc==SQLITE_OK ){
3998 if( pLeaf->block.n==0 ){
3999 pLeaf->block.n = 1;
4000 pLeaf->block.a[0] = '\0';
4002 rc = fts3AppendToNode(
4003 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
4007 return rc;
4011 ** This function is called to release all dynamic resources held by the
4012 ** merge-writer object pWriter, and if no error has occurred, to flush
4013 ** all outstanding node buffers held by pWriter to disk.
4015 ** If *pRc is not SQLITE_OK when this function is called, then no attempt
4016 ** is made to write any data to disk. Instead, this function serves only
4017 ** to release outstanding resources.
4019 ** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
4020 ** flushing buffers to disk, *pRc is set to an SQLite error code before
4021 ** returning.
4023 static void fts3IncrmergeRelease(
4024 Fts3Table *p, /* FTS3 table handle */
4025 IncrmergeWriter *pWriter, /* Merge-writer object */
4026 int *pRc /* IN/OUT: Error code */
4028 int i; /* Used to iterate through non-root layers */
4029 int iRoot; /* Index of root in pWriter->aNodeWriter */
4030 NodeWriter *pRoot; /* NodeWriter for root node */
4031 int rc = *pRc; /* Error code */
4033 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
4034 ** root node. If the segment fits entirely on a single leaf node, iRoot
4035 ** will be set to 0. If the root node is the parent of the leaves, iRoot
4036 ** will be 1. And so on. */
4037 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
4038 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
4039 if( pNode->block.n>0 ) break;
4040 assert( *pRc || pNode->block.nAlloc==0 );
4041 assert( *pRc || pNode->key.nAlloc==0 );
4042 sqlite3_free(pNode->block.a);
4043 sqlite3_free(pNode->key.a);
4046 /* Empty output segment. This is a no-op. */
4047 if( iRoot<0 ) return;
4049 /* The entire output segment fits on a single node. Normally, this means
4050 ** the node would be stored as a blob in the "root" column of the %_segdir
4051 ** table. However, this is not permitted in this case. The problem is that
4052 ** space has already been reserved in the %_segments table, and so the
4053 ** start_block and end_block fields of the %_segdir table must be populated.
4054 ** And, by design or by accident, released versions of FTS cannot handle
4055 ** segments that fit entirely on the root node with start_block!=0.
4057 ** Instead, create a synthetic root node that contains nothing but a
4058 ** pointer to the single content node. So that the segment consists of a
4059 ** single leaf and a single interior (root) node.
4061 ** Todo: Better might be to defer allocating space in the %_segments
4062 ** table until we are sure it is needed.
4064 if( iRoot==0 ){
4065 Blob *pBlock = &pWriter->aNodeWriter[1].block;
4066 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
4067 if( rc==SQLITE_OK ){
4068 pBlock->a[0] = 0x01;
4069 pBlock->n = 1 + sqlite3Fts3PutVarint(
4070 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
4073 iRoot = 1;
4075 pRoot = &pWriter->aNodeWriter[iRoot];
4077 /* Flush all currently outstanding nodes to disk. */
4078 for(i=0; i<iRoot; i++){
4079 NodeWriter *pNode = &pWriter->aNodeWriter[i];
4080 if( pNode->block.n>0 && rc==SQLITE_OK ){
4081 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
4083 sqlite3_free(pNode->block.a);
4084 sqlite3_free(pNode->key.a);
4087 /* Write the %_segdir record. */
4088 if( rc==SQLITE_OK ){
4089 rc = fts3WriteSegdir(p,
4090 pWriter->iAbsLevel+1, /* level */
4091 pWriter->iIdx, /* idx */
4092 pWriter->iStart, /* start_block */
4093 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
4094 pWriter->iEnd, /* end_block */
4095 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
4096 pRoot->block.a, pRoot->block.n /* root */
4099 sqlite3_free(pRoot->block.a);
4100 sqlite3_free(pRoot->key.a);
4102 *pRc = rc;
4106 ** Compare the term in buffer zLhs (size in bytes nLhs) with that in
4107 ** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
4108 ** the other, it is considered to be smaller than the other.
4110 ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
4111 ** if it is greater.
4113 static int fts3TermCmp(
4114 const char *zLhs, int nLhs, /* LHS of comparison */
4115 const char *zRhs, int nRhs /* RHS of comparison */
4117 int nCmp = MIN(nLhs, nRhs);
4118 int res;
4120 res = memcmp(zLhs, zRhs, nCmp);
4121 if( res==0 ) res = nLhs - nRhs;
4123 return res;
4128 ** Query to see if the entry in the %_segments table with blockid iEnd is
4129 ** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
4130 ** returning. Otherwise, set *pbRes to 0.
4132 ** Or, if an error occurs while querying the database, return an SQLite
4133 ** error code. The final value of *pbRes is undefined in this case.
4135 ** This is used to test if a segment is an "appendable" segment. If it
4136 ** is, then a NULL entry has been inserted into the %_segments table
4137 ** with blockid %_segdir.end_block.
4139 static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
4140 int bRes = 0; /* Result to set *pbRes to */
4141 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
4142 int rc; /* Return code */
4144 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
4145 if( rc==SQLITE_OK ){
4146 sqlite3_bind_int64(pCheck, 1, iEnd);
4147 if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
4148 rc = sqlite3_reset(pCheck);
4151 *pbRes = bRes;
4152 return rc;
4156 ** This function is called when initializing an incremental-merge operation.
4157 ** It checks if the existing segment with index value iIdx at absolute level
4158 ** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
4159 ** merge-writer object *pWriter is initialized to write to it.
4161 ** An existing segment can be appended to by an incremental merge if:
4163 ** * It was initially created as an appendable segment (with all required
4164 ** space pre-allocated), and
4166 ** * The first key read from the input (arguments zKey and nKey) is
4167 ** greater than the largest key currently stored in the potential
4168 ** output segment.
4170 static int fts3IncrmergeLoad(
4171 Fts3Table *p, /* Fts3 table handle */
4172 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
4173 int iIdx, /* Index of candidate output segment */
4174 const char *zKey, /* First key to write */
4175 int nKey, /* Number of bytes in nKey */
4176 IncrmergeWriter *pWriter /* Populate this object */
4178 int rc; /* Return code */
4179 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
4181 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
4182 if( rc==SQLITE_OK ){
4183 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
4184 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
4185 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
4186 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
4187 int nRoot = 0; /* Size of aRoot[] in bytes */
4188 int rc2; /* Return code from sqlite3_reset() */
4189 int bAppendable = 0; /* Set to true if segment is appendable */
4191 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
4192 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
4193 sqlite3_bind_int(pSelect, 2, iIdx);
4194 if( sqlite3_step(pSelect)==SQLITE_ROW ){
4195 iStart = sqlite3_column_int64(pSelect, 1);
4196 iLeafEnd = sqlite3_column_int64(pSelect, 2);
4197 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
4198 if( pWriter->nLeafData<0 ){
4199 pWriter->nLeafData = pWriter->nLeafData * -1;
4201 pWriter->bNoLeafData = (pWriter->nLeafData==0);
4202 nRoot = sqlite3_column_bytes(pSelect, 4);
4203 aRoot = sqlite3_column_blob(pSelect, 4);
4204 }else{
4205 return sqlite3_reset(pSelect);
4208 /* Check for the zero-length marker in the %_segments table */
4209 rc = fts3IsAppendable(p, iEnd, &bAppendable);
4211 /* Check that zKey/nKey is larger than the largest key the candidate */
4212 if( rc==SQLITE_OK && bAppendable ){
4213 char *aLeaf = 0;
4214 int nLeaf = 0;
4216 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
4217 if( rc==SQLITE_OK ){
4218 NodeReader reader;
4219 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
4220 rc==SQLITE_OK && reader.aNode;
4221 rc = nodeReaderNext(&reader)
4223 assert( reader.aNode );
4225 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
4226 bAppendable = 0;
4228 nodeReaderRelease(&reader);
4230 sqlite3_free(aLeaf);
4233 if( rc==SQLITE_OK && bAppendable ){
4234 /* It is possible to append to this segment. Set up the IncrmergeWriter
4235 ** object to do so. */
4236 int i;
4237 int nHeight = (int)aRoot[0];
4238 NodeWriter *pNode;
4240 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
4241 pWriter->iStart = iStart;
4242 pWriter->iEnd = iEnd;
4243 pWriter->iAbsLevel = iAbsLevel;
4244 pWriter->iIdx = iIdx;
4246 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
4247 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
4250 pNode = &pWriter->aNodeWriter[nHeight];
4251 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
4252 blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
4253 if( rc==SQLITE_OK ){
4254 memcpy(pNode->block.a, aRoot, nRoot);
4255 pNode->block.n = nRoot;
4258 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
4259 NodeReader reader;
4260 pNode = &pWriter->aNodeWriter[i];
4262 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
4263 while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
4264 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
4265 if( rc==SQLITE_OK ){
4266 memcpy(pNode->key.a, reader.term.a, reader.term.n);
4267 pNode->key.n = reader.term.n;
4268 if( i>0 ){
4269 char *aBlock = 0;
4270 int nBlock = 0;
4271 pNode = &pWriter->aNodeWriter[i-1];
4272 pNode->iBlock = reader.iChild;
4273 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
4274 blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
4275 if( rc==SQLITE_OK ){
4276 memcpy(pNode->block.a, aBlock, nBlock);
4277 pNode->block.n = nBlock;
4279 sqlite3_free(aBlock);
4282 nodeReaderRelease(&reader);
4286 rc2 = sqlite3_reset(pSelect);
4287 if( rc==SQLITE_OK ) rc = rc2;
4290 return rc;
4294 ** Determine the largest segment index value that exists within absolute
4295 ** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
4296 ** one before returning SQLITE_OK. Or, if there are no segments at all
4297 ** within level iAbsLevel, set *piIdx to zero.
4299 ** If an error occurs, return an SQLite error code. The final value of
4300 ** *piIdx is undefined in this case.
4302 static int fts3IncrmergeOutputIdx(
4303 Fts3Table *p, /* FTS Table handle */
4304 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
4305 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
4307 int rc;
4308 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
4310 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
4311 if( rc==SQLITE_OK ){
4312 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
4313 sqlite3_step(pOutputIdx);
4314 *piIdx = sqlite3_column_int(pOutputIdx, 0);
4315 rc = sqlite3_reset(pOutputIdx);
4318 return rc;
4322 ** Allocate an appendable output segment on absolute level iAbsLevel+1
4323 ** with idx value iIdx.
4325 ** In the %_segdir table, a segment is defined by the values in three
4326 ** columns:
4328 ** start_block
4329 ** leaves_end_block
4330 ** end_block
4332 ** When an appendable segment is allocated, it is estimated that the
4333 ** maximum number of leaf blocks that may be required is the sum of the
4334 ** number of leaf blocks consumed by the input segments, plus the number
4335 ** of input segments, multiplied by two. This value is stored in stack
4336 ** variable nLeafEst.
4338 ** A total of 16*nLeafEst blocks are allocated when an appendable segment
4339 ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
4340 ** array of leaf nodes starts at the first block allocated. The array
4341 ** of interior nodes that are parents of the leaf nodes start at block
4342 ** (start_block + (1 + end_block - start_block) / 16). And so on.
4344 ** In the actual code below, the value "16" is replaced with the
4345 ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
4347 static int fts3IncrmergeWriter(
4348 Fts3Table *p, /* Fts3 table handle */
4349 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
4350 int iIdx, /* Index of new output segment */
4351 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
4352 IncrmergeWriter *pWriter /* Populate this object */
4354 int rc; /* Return Code */
4355 int i; /* Iterator variable */
4356 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
4357 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
4358 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
4360 /* Calculate nLeafEst. */
4361 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
4362 if( rc==SQLITE_OK ){
4363 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
4364 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
4365 if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
4366 nLeafEst = sqlite3_column_int(pLeafEst, 0);
4368 rc = sqlite3_reset(pLeafEst);
4370 if( rc!=SQLITE_OK ) return rc;
4372 /* Calculate the first block to use in the output segment */
4373 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
4374 if( rc==SQLITE_OK ){
4375 if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
4376 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
4377 pWriter->iEnd = pWriter->iStart - 1;
4378 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
4380 rc = sqlite3_reset(pFirstBlock);
4382 if( rc!=SQLITE_OK ) return rc;
4384 /* Insert the marker in the %_segments table to make sure nobody tries
4385 ** to steal the space just allocated. This is also used to identify
4386 ** appendable segments. */
4387 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
4388 if( rc!=SQLITE_OK ) return rc;
4390 pWriter->iAbsLevel = iAbsLevel;
4391 pWriter->nLeafEst = nLeafEst;
4392 pWriter->iIdx = iIdx;
4394 /* Set up the array of NodeWriter objects */
4395 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
4396 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
4398 return SQLITE_OK;
4402 ** Remove an entry from the %_segdir table. This involves running the
4403 ** following two statements:
4405 ** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
4406 ** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
4408 ** The DELETE statement removes the specific %_segdir level. The UPDATE
4409 ** statement ensures that the remaining segments have contiguously allocated
4410 ** idx values.
4412 static int fts3RemoveSegdirEntry(
4413 Fts3Table *p, /* FTS3 table handle */
4414 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
4415 int iIdx /* Index of %_segdir entry to delete */
4417 int rc; /* Return code */
4418 sqlite3_stmt *pDelete = 0; /* DELETE statement */
4420 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
4421 if( rc==SQLITE_OK ){
4422 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
4423 sqlite3_bind_int(pDelete, 2, iIdx);
4424 sqlite3_step(pDelete);
4425 rc = sqlite3_reset(pDelete);
4428 return rc;
4432 ** One or more segments have just been removed from absolute level iAbsLevel.
4433 ** Update the 'idx' values of the remaining segments in the level so that
4434 ** the idx values are a contiguous sequence starting from 0.
4436 static int fts3RepackSegdirLevel(
4437 Fts3Table *p, /* FTS3 table handle */
4438 sqlite3_int64 iAbsLevel /* Absolute level to repack */
4440 int rc; /* Return code */
4441 int *aIdx = 0; /* Array of remaining idx values */
4442 int nIdx = 0; /* Valid entries in aIdx[] */
4443 int nAlloc = 0; /* Allocated size of aIdx[] */
4444 int i; /* Iterator variable */
4445 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
4446 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
4448 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
4449 if( rc==SQLITE_OK ){
4450 int rc2;
4451 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
4452 while( SQLITE_ROW==sqlite3_step(pSelect) ){
4453 if( nIdx>=nAlloc ){
4454 int *aNew;
4455 nAlloc += 16;
4456 aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
4457 if( !aNew ){
4458 rc = SQLITE_NOMEM;
4459 break;
4461 aIdx = aNew;
4463 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
4465 rc2 = sqlite3_reset(pSelect);
4466 if( rc==SQLITE_OK ) rc = rc2;
4469 if( rc==SQLITE_OK ){
4470 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
4472 if( rc==SQLITE_OK ){
4473 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
4476 assert( p->bIgnoreSavepoint==0 );
4477 p->bIgnoreSavepoint = 1;
4478 for(i=0; rc==SQLITE_OK && i<nIdx; i++){
4479 if( aIdx[i]!=i ){
4480 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
4481 sqlite3_bind_int(pUpdate, 1, i);
4482 sqlite3_step(pUpdate);
4483 rc = sqlite3_reset(pUpdate);
4486 p->bIgnoreSavepoint = 0;
4488 sqlite3_free(aIdx);
4489 return rc;
4492 static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
4493 pNode->a[0] = (char)iHeight;
4494 if( iChild ){
4495 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
4496 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
4497 }else{
4498 assert( pNode->nAlloc>=1 );
4499 pNode->n = 1;
4504 ** The first two arguments are a pointer to and the size of a segment b-tree
4505 ** node. The node may be a leaf or an internal node.
4507 ** This function creates a new node image in blob object *pNew by copying
4508 ** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
4509 ** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
4511 static int fts3TruncateNode(
4512 const char *aNode, /* Current node image */
4513 int nNode, /* Size of aNode in bytes */
4514 Blob *pNew, /* OUT: Write new node image here */
4515 const char *zTerm, /* Omit all terms smaller than this */
4516 int nTerm, /* Size of zTerm in bytes */
4517 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
4519 NodeReader reader; /* Reader object */
4520 Blob prev = {0, 0, 0}; /* Previous term written to new node */
4521 int rc = SQLITE_OK; /* Return code */
4522 int bLeaf = aNode[0]=='\0'; /* True for a leaf node */
4524 /* Allocate required output space */
4525 blobGrowBuffer(pNew, nNode, &rc);
4526 if( rc!=SQLITE_OK ) return rc;
4527 pNew->n = 0;
4529 /* Populate new node buffer */
4530 for(rc = nodeReaderInit(&reader, aNode, nNode);
4531 rc==SQLITE_OK && reader.aNode;
4532 rc = nodeReaderNext(&reader)
4534 if( pNew->n==0 ){
4535 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
4536 if( res<0 || (bLeaf==0 && res==0) ) continue;
4537 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
4538 *piBlock = reader.iChild;
4540 rc = fts3AppendToNode(
4541 pNew, &prev, reader.term.a, reader.term.n,
4542 reader.aDoclist, reader.nDoclist
4544 if( rc!=SQLITE_OK ) break;
4546 if( pNew->n==0 ){
4547 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
4548 *piBlock = reader.iChild;
4550 assert( pNew->n<=pNew->nAlloc );
4552 nodeReaderRelease(&reader);
4553 sqlite3_free(prev.a);
4554 return rc;
4558 ** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
4559 ** level iAbsLevel. This may involve deleting entries from the %_segments
4560 ** table, and modifying existing entries in both the %_segments and %_segdir
4561 ** tables.
4563 ** SQLITE_OK is returned if the segment is updated successfully. Or an
4564 ** SQLite error code otherwise.
4566 static int fts3TruncateSegment(
4567 Fts3Table *p, /* FTS3 table handle */
4568 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
4569 int iIdx, /* Index within level of segment to modify */
4570 const char *zTerm, /* Remove terms smaller than this */
4571 int nTerm /* Number of bytes in buffer zTerm */
4573 int rc = SQLITE_OK; /* Return code */
4574 Blob root = {0,0,0}; /* New root page image */
4575 Blob block = {0,0,0}; /* Buffer used for any other block */
4576 sqlite3_int64 iBlock = 0; /* Block id */
4577 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
4578 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
4579 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
4581 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
4582 if( rc==SQLITE_OK ){
4583 int rc2; /* sqlite3_reset() return code */
4584 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
4585 sqlite3_bind_int(pFetch, 2, iIdx);
4586 if( SQLITE_ROW==sqlite3_step(pFetch) ){
4587 const char *aRoot = sqlite3_column_blob(pFetch, 4);
4588 int nRoot = sqlite3_column_bytes(pFetch, 4);
4589 iOldStart = sqlite3_column_int64(pFetch, 1);
4590 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
4592 rc2 = sqlite3_reset(pFetch);
4593 if( rc==SQLITE_OK ) rc = rc2;
4596 while( rc==SQLITE_OK && iBlock ){
4597 char *aBlock = 0;
4598 int nBlock = 0;
4599 iNewStart = iBlock;
4601 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
4602 if( rc==SQLITE_OK ){
4603 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
4605 if( rc==SQLITE_OK ){
4606 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
4608 sqlite3_free(aBlock);
4611 /* Variable iNewStart now contains the first valid leaf node. */
4612 if( rc==SQLITE_OK && iNewStart ){
4613 sqlite3_stmt *pDel = 0;
4614 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
4615 if( rc==SQLITE_OK ){
4616 sqlite3_bind_int64(pDel, 1, iOldStart);
4617 sqlite3_bind_int64(pDel, 2, iNewStart-1);
4618 sqlite3_step(pDel);
4619 rc = sqlite3_reset(pDel);
4623 if( rc==SQLITE_OK ){
4624 sqlite3_stmt *pChomp = 0;
4625 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
4626 if( rc==SQLITE_OK ){
4627 sqlite3_bind_int64(pChomp, 1, iNewStart);
4628 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
4629 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
4630 sqlite3_bind_int(pChomp, 4, iIdx);
4631 sqlite3_step(pChomp);
4632 rc = sqlite3_reset(pChomp);
4636 sqlite3_free(root.a);
4637 sqlite3_free(block.a);
4638 return rc;
4642 ** This function is called after an incrmental-merge operation has run to
4643 ** merge (or partially merge) two or more segments from absolute level
4644 ** iAbsLevel.
4646 ** Each input segment is either removed from the db completely (if all of
4647 ** its data was copied to the output segment by the incrmerge operation)
4648 ** or modified in place so that it no longer contains those entries that
4649 ** have been duplicated in the output segment.
4651 static int fts3IncrmergeChomp(
4652 Fts3Table *p, /* FTS table handle */
4653 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
4654 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
4655 int *pnRem /* Number of segments not deleted */
4657 int i;
4658 int nRem = 0;
4659 int rc = SQLITE_OK;
4661 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
4662 Fts3SegReader *pSeg = 0;
4663 int j;
4665 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
4666 ** somewhere in the pCsr->apSegment[] array. */
4667 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
4668 pSeg = pCsr->apSegment[j];
4669 if( pSeg->iIdx==i ) break;
4671 assert( j<pCsr->nSegment && pSeg->iIdx==i );
4673 if( pSeg->aNode==0 ){
4674 /* Seg-reader is at EOF. Remove the entire input segment. */
4675 rc = fts3DeleteSegment(p, pSeg);
4676 if( rc==SQLITE_OK ){
4677 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
4679 *pnRem = 0;
4680 }else{
4681 /* The incremental merge did not copy all the data from this
4682 ** segment to the upper level. The segment is modified in place
4683 ** so that it contains no keys smaller than zTerm/nTerm. */
4684 const char *zTerm = pSeg->zTerm;
4685 int nTerm = pSeg->nTerm;
4686 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
4687 nRem++;
4691 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
4692 rc = fts3RepackSegdirLevel(p, iAbsLevel);
4695 *pnRem = nRem;
4696 return rc;
4700 ** Store an incr-merge hint in the database.
4702 static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
4703 sqlite3_stmt *pReplace = 0;
4704 int rc; /* Return code */
4706 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
4707 if( rc==SQLITE_OK ){
4708 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
4709 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
4710 sqlite3_step(pReplace);
4711 rc = sqlite3_reset(pReplace);
4714 return rc;
4718 ** Load an incr-merge hint from the database. The incr-merge hint, if one
4719 ** exists, is stored in the rowid==1 row of the %_stat table.
4721 ** If successful, populate blob *pHint with the value read from the %_stat
4722 ** table and return SQLITE_OK. Otherwise, if an error occurs, return an
4723 ** SQLite error code.
4725 static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
4726 sqlite3_stmt *pSelect = 0;
4727 int rc;
4729 pHint->n = 0;
4730 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
4731 if( rc==SQLITE_OK ){
4732 int rc2;
4733 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
4734 if( SQLITE_ROW==sqlite3_step(pSelect) ){
4735 const char *aHint = sqlite3_column_blob(pSelect, 0);
4736 int nHint = sqlite3_column_bytes(pSelect, 0);
4737 if( aHint ){
4738 blobGrowBuffer(pHint, nHint, &rc);
4739 if( rc==SQLITE_OK ){
4740 memcpy(pHint->a, aHint, nHint);
4741 pHint->n = nHint;
4745 rc2 = sqlite3_reset(pSelect);
4746 if( rc==SQLITE_OK ) rc = rc2;
4749 return rc;
4753 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
4754 ** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
4755 ** consists of two varints, the absolute level number of the input segments
4756 ** and the number of input segments.
4758 ** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
4759 ** set *pRc to an SQLite error code before returning.
4761 static void fts3IncrmergeHintPush(
4762 Blob *pHint, /* Hint blob to append to */
4763 i64 iAbsLevel, /* First varint to store in hint */
4764 int nInput, /* Second varint to store in hint */
4765 int *pRc /* IN/OUT: Error code */
4767 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
4768 if( *pRc==SQLITE_OK ){
4769 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
4770 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
4775 ** Read the last entry (most recently pushed) from the hint blob *pHint
4776 ** and then remove the entry. Write the two values read to *piAbsLevel and
4777 ** *pnInput before returning.
4779 ** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
4780 ** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
4782 static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
4783 const int nHint = pHint->n;
4784 int i;
4786 i = pHint->n-2;
4787 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
4788 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
4790 pHint->n = i;
4791 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
4792 i += fts3GetVarint32(&pHint->a[i], pnInput);
4793 if( i!=nHint ) return FTS_CORRUPT_VTAB;
4795 return SQLITE_OK;
4800 ** Attempt an incremental merge that writes nMerge leaf blocks.
4802 ** Incremental merges happen nMin segments at a time. The segments
4803 ** to be merged are the nMin oldest segments (the ones with the smallest
4804 ** values for the _segdir.idx field) in the highest level that contains
4805 ** at least nMin segments. Multiple merges might occur in an attempt to
4806 ** write the quota of nMerge leaf blocks.
4808 int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
4809 int rc; /* Return code */
4810 int nRem = nMerge; /* Number of leaf pages yet to be written */
4811 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
4812 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
4813 IncrmergeWriter *pWriter; /* Writer object */
4814 int nSeg = 0; /* Number of input segments */
4815 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
4816 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
4817 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
4819 /* Allocate space for the cursor, filter and writer objects */
4820 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
4821 pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
4822 if( !pWriter ) return SQLITE_NOMEM;
4823 pFilter = (Fts3SegFilter *)&pWriter[1];
4824 pCsr = (Fts3MultiSegReader *)&pFilter[1];
4826 rc = fts3IncrmergeHintLoad(p, &hint);
4827 while( rc==SQLITE_OK && nRem>0 ){
4828 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
4829 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
4830 int bUseHint = 0; /* True if attempting to append */
4831 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
4833 /* Search the %_segdir table for the absolute level with the smallest
4834 ** relative level number that contains at least nMin segments, if any.
4835 ** If one is found, set iAbsLevel to the absolute level number and
4836 ** nSeg to nMin. If no level with at least nMin segments can be found,
4837 ** set nSeg to -1.
4839 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
4840 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin));
4841 if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
4842 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
4843 nSeg = sqlite3_column_int(pFindLevel, 1);
4844 assert( nSeg>=2 );
4845 }else{
4846 nSeg = -1;
4848 rc = sqlite3_reset(pFindLevel);
4850 /* If the hint read from the %_stat table is not empty, check if the
4851 ** last entry in it specifies a relative level smaller than or equal
4852 ** to the level identified by the block above (if any). If so, this
4853 ** iteration of the loop will work on merging at the hinted level.
4855 if( rc==SQLITE_OK && hint.n ){
4856 int nHint = hint.n;
4857 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
4858 int nHintSeg = 0; /* Hint number of segments */
4860 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
4861 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
4862 iAbsLevel = iHintAbsLevel;
4863 nSeg = nHintSeg;
4864 bUseHint = 1;
4865 bDirtyHint = 1;
4866 }else{
4867 /* This undoes the effect of the HintPop() above - so that no entry
4868 ** is removed from the hint blob. */
4869 hint.n = nHint;
4873 /* If nSeg is less that zero, then there is no level with at least
4874 ** nMin segments and no hint in the %_stat table. No work to do.
4875 ** Exit early in this case. */
4876 if( nSeg<0 ) break;
4878 /* Open a cursor to iterate through the contents of the oldest nSeg
4879 ** indexes of absolute level iAbsLevel. If this cursor is opened using
4880 ** the 'hint' parameters, it is possible that there are less than nSeg
4881 ** segments available in level iAbsLevel. In this case, no work is
4882 ** done on iAbsLevel - fall through to the next iteration of the loop
4883 ** to start work on some other level. */
4884 memset(pWriter, 0, nAlloc);
4885 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
4887 if( rc==SQLITE_OK ){
4888 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
4889 assert( bUseHint==1 || bUseHint==0 );
4890 if( iIdx==0 || (bUseHint && iIdx==1) ){
4891 int bIgnore = 0;
4892 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
4893 if( bIgnore ){
4894 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
4899 if( rc==SQLITE_OK ){
4900 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
4902 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
4903 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
4904 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
4906 if( bUseHint && iIdx>0 ){
4907 const char *zKey = pCsr->zTerm;
4908 int nKey = pCsr->nTerm;
4909 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
4910 }else{
4911 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
4914 if( rc==SQLITE_OK && pWriter->nLeafEst ){
4915 fts3LogMerge(nSeg, iAbsLevel);
4916 do {
4917 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
4918 if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
4919 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
4920 }while( rc==SQLITE_ROW );
4922 /* Update or delete the input segments */
4923 if( rc==SQLITE_OK ){
4924 nRem -= (1 + pWriter->nWork);
4925 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
4926 if( nSeg!=0 ){
4927 bDirtyHint = 1;
4928 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
4933 if( nSeg!=0 ){
4934 pWriter->nLeafData = pWriter->nLeafData * -1;
4936 fts3IncrmergeRelease(p, pWriter, &rc);
4937 if( nSeg==0 && pWriter->bNoLeafData==0 ){
4938 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
4942 sqlite3Fts3SegReaderFinish(pCsr);
4945 /* Write the hint values into the %_stat table for the next incr-merger */
4946 if( bDirtyHint && rc==SQLITE_OK ){
4947 rc = fts3IncrmergeHintStore(p, &hint);
4950 sqlite3_free(pWriter);
4951 sqlite3_free(hint.a);
4952 return rc;
4956 ** Convert the text beginning at *pz into an integer and return
4957 ** its value. Advance *pz to point to the first character past
4958 ** the integer.
4960 static int fts3Getint(const char **pz){
4961 const char *z = *pz;
4962 int i = 0;
4963 while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
4964 *pz = z;
4965 return i;
4969 ** Process statements of the form:
4971 ** INSERT INTO table(table) VALUES('merge=A,B');
4973 ** A and B are integers that decode to be the number of leaf pages
4974 ** written for the merge, and the minimum number of segments on a level
4975 ** before it will be selected for a merge, respectively.
4977 static int fts3DoIncrmerge(
4978 Fts3Table *p, /* FTS3 table handle */
4979 const char *zParam /* Nul-terminated string containing "A,B" */
4981 int rc;
4982 int nMin = (FTS3_MERGE_COUNT / 2);
4983 int nMerge = 0;
4984 const char *z = zParam;
4986 /* Read the first integer value */
4987 nMerge = fts3Getint(&z);
4989 /* If the first integer value is followed by a ',', read the second
4990 ** integer value. */
4991 if( z[0]==',' && z[1]!='\0' ){
4992 z++;
4993 nMin = fts3Getint(&z);
4996 if( z[0]!='\0' || nMin<2 ){
4997 rc = SQLITE_ERROR;
4998 }else{
4999 rc = SQLITE_OK;
5000 if( !p->bHasStat ){
5001 assert( p->bFts4==0 );
5002 sqlite3Fts3CreateStatTable(&rc, p);
5004 if( rc==SQLITE_OK ){
5005 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
5007 sqlite3Fts3SegmentsClose(p);
5009 return rc;
5013 ** Process statements of the form:
5015 ** INSERT INTO table(table) VALUES('automerge=X');
5017 ** where X is an integer. X==0 means to turn automerge off. X!=0 means
5018 ** turn it on. The setting is persistent.
5020 static int fts3DoAutoincrmerge(
5021 Fts3Table *p, /* FTS3 table handle */
5022 const char *zParam /* Nul-terminated string containing boolean */
5024 int rc = SQLITE_OK;
5025 sqlite3_stmt *pStmt = 0;
5026 p->nAutoincrmerge = fts3Getint(&zParam);
5027 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
5028 p->nAutoincrmerge = 8;
5030 if( !p->bHasStat ){
5031 assert( p->bFts4==0 );
5032 sqlite3Fts3CreateStatTable(&rc, p);
5033 if( rc ) return rc;
5035 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
5036 if( rc ) return rc;
5037 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
5038 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
5039 sqlite3_step(pStmt);
5040 rc = sqlite3_reset(pStmt);
5041 return rc;
5045 ** Return a 64-bit checksum for the FTS index entry specified by the
5046 ** arguments to this function.
5048 static u64 fts3ChecksumEntry(
5049 const char *zTerm, /* Pointer to buffer containing term */
5050 int nTerm, /* Size of zTerm in bytes */
5051 int iLangid, /* Language id for current row */
5052 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
5053 i64 iDocid, /* Docid for current row. */
5054 int iCol, /* Column number */
5055 int iPos /* Position */
5057 int i;
5058 u64 ret = (u64)iDocid;
5060 ret += (ret<<3) + iLangid;
5061 ret += (ret<<3) + iIndex;
5062 ret += (ret<<3) + iCol;
5063 ret += (ret<<3) + iPos;
5064 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
5066 return ret;
5070 ** Return a checksum of all entries in the FTS index that correspond to
5071 ** language id iLangid. The checksum is calculated by XORing the checksums
5072 ** of each individual entry (see fts3ChecksumEntry()) together.
5074 ** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
5075 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
5076 ** return value is undefined in this case.
5078 static u64 fts3ChecksumIndex(
5079 Fts3Table *p, /* FTS3 table handle */
5080 int iLangid, /* Language id to return cksum for */
5081 int iIndex, /* Index to cksum (0..p->nIndex-1) */
5082 int *pRc /* OUT: Return code */
5084 Fts3SegFilter filter;
5085 Fts3MultiSegReader csr;
5086 int rc;
5087 u64 cksum = 0;
5089 assert( *pRc==SQLITE_OK );
5091 memset(&filter, 0, sizeof(filter));
5092 memset(&csr, 0, sizeof(csr));
5093 filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
5094 filter.flags |= FTS3_SEGMENT_SCAN;
5096 rc = sqlite3Fts3SegReaderCursor(
5097 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
5099 if( rc==SQLITE_OK ){
5100 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
5103 if( rc==SQLITE_OK ){
5104 while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
5105 char *pCsr = csr.aDoclist;
5106 char *pEnd = &pCsr[csr.nDoclist];
5108 i64 iDocid = 0;
5109 i64 iCol = 0;
5110 i64 iPos = 0;
5112 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
5113 while( pCsr<pEnd ){
5114 i64 iVal = 0;
5115 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
5116 if( pCsr<pEnd ){
5117 if( iVal==0 || iVal==1 ){
5118 iCol = 0;
5119 iPos = 0;
5120 if( iVal ){
5121 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
5122 }else{
5123 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
5124 iDocid += iVal;
5126 }else{
5127 iPos += (iVal - 2);
5128 cksum = cksum ^ fts3ChecksumEntry(
5129 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
5130 (int)iCol, (int)iPos
5137 sqlite3Fts3SegReaderFinish(&csr);
5139 *pRc = rc;
5140 return cksum;
5144 ** Check if the contents of the FTS index match the current contents of the
5145 ** content table. If no error occurs and the contents do match, set *pbOk
5146 ** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
5147 ** to false before returning.
5149 ** If an error occurs (e.g. an OOM or IO error), return an SQLite error
5150 ** code. The final value of *pbOk is undefined in this case.
5152 static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
5153 int rc = SQLITE_OK; /* Return code */
5154 u64 cksum1 = 0; /* Checksum based on FTS index contents */
5155 u64 cksum2 = 0; /* Checksum based on %_content contents */
5156 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
5158 /* This block calculates the checksum according to the FTS index. */
5159 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
5160 if( rc==SQLITE_OK ){
5161 int rc2;
5162 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
5163 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
5164 while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
5165 int iLangid = sqlite3_column_int(pAllLangid, 0);
5166 int i;
5167 for(i=0; i<p->nIndex; i++){
5168 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
5171 rc2 = sqlite3_reset(pAllLangid);
5172 if( rc==SQLITE_OK ) rc = rc2;
5175 /* This block calculates the checksum according to the %_content table */
5176 if( rc==SQLITE_OK ){
5177 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
5178 sqlite3_stmt *pStmt = 0;
5179 char *zSql;
5181 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
5182 if( !zSql ){
5183 rc = SQLITE_NOMEM;
5184 }else{
5185 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5186 sqlite3_free(zSql);
5189 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
5190 i64 iDocid = sqlite3_column_int64(pStmt, 0);
5191 int iLang = langidFromSelect(p, pStmt);
5192 int iCol;
5194 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
5195 if( p->abNotindexed[iCol]==0 ){
5196 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
5197 int nText = sqlite3_column_bytes(pStmt, iCol+1);
5198 sqlite3_tokenizer_cursor *pT = 0;
5200 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
5201 while( rc==SQLITE_OK ){
5202 char const *zToken; /* Buffer containing token */
5203 int nToken = 0; /* Number of bytes in token */
5204 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
5205 int iPos = 0; /* Position of token in zText */
5207 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
5208 if( rc==SQLITE_OK ){
5209 int i;
5210 cksum2 = cksum2 ^ fts3ChecksumEntry(
5211 zToken, nToken, iLang, 0, iDocid, iCol, iPos
5213 for(i=1; i<p->nIndex; i++){
5214 if( p->aIndex[i].nPrefix<=nToken ){
5215 cksum2 = cksum2 ^ fts3ChecksumEntry(
5216 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
5222 if( pT ) pModule->xClose(pT);
5223 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
5228 sqlite3_finalize(pStmt);
5231 *pbOk = (cksum1==cksum2);
5232 return rc;
5236 ** Run the integrity-check. If no error occurs and the current contents of
5237 ** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
5238 ** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
5240 ** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
5241 ** error code.
5243 ** The integrity-check works as follows. For each token and indexed token
5244 ** prefix in the document set, a 64-bit checksum is calculated (by code
5245 ** in fts3ChecksumEntry()) based on the following:
5247 ** + The index number (0 for the main index, 1 for the first prefix
5248 ** index etc.),
5249 ** + The token (or token prefix) text itself,
5250 ** + The language-id of the row it appears in,
5251 ** + The docid of the row it appears in,
5252 ** + The column it appears in, and
5253 ** + The tokens position within that column.
5255 ** The checksums for all entries in the index are XORed together to create
5256 ** a single checksum for the entire index.
5258 ** The integrity-check code calculates the same checksum in two ways:
5260 ** 1. By scanning the contents of the FTS index, and
5261 ** 2. By scanning and tokenizing the content table.
5263 ** If the two checksums are identical, the integrity-check is deemed to have
5264 ** passed.
5266 static int fts3DoIntegrityCheck(
5267 Fts3Table *p /* FTS3 table handle */
5269 int rc;
5270 int bOk = 0;
5271 rc = fts3IntegrityCheck(p, &bOk);
5272 if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
5273 return rc;
5277 ** Handle a 'special' INSERT of the form:
5279 ** "INSERT INTO tbl(tbl) VALUES(<expr>)"
5281 ** Argument pVal contains the result of <expr>. Currently the only
5282 ** meaningful value to insert is the text 'optimize'.
5284 static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
5285 int rc; /* Return Code */
5286 const char *zVal = (const char *)sqlite3_value_text(pVal);
5287 int nVal = sqlite3_value_bytes(pVal);
5289 if( !zVal ){
5290 return SQLITE_NOMEM;
5291 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
5292 rc = fts3DoOptimize(p, 0);
5293 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
5294 rc = fts3DoRebuild(p);
5295 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
5296 rc = fts3DoIntegrityCheck(p);
5297 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
5298 rc = fts3DoIncrmerge(p, &zVal[6]);
5299 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
5300 rc = fts3DoAutoincrmerge(p, &zVal[10]);
5301 #ifdef SQLITE_TEST
5302 }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
5303 p->nNodeSize = atoi(&zVal[9]);
5304 rc = SQLITE_OK;
5305 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
5306 p->nMaxPendingData = atoi(&zVal[11]);
5307 rc = SQLITE_OK;
5308 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
5309 p->bNoIncrDoclist = atoi(&zVal[21]);
5310 rc = SQLITE_OK;
5311 #endif
5312 }else{
5313 rc = SQLITE_ERROR;
5316 return rc;
5319 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
5321 ** Delete all cached deferred doclists. Deferred doclists are cached
5322 ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
5324 void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
5325 Fts3DeferredToken *pDef;
5326 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
5327 fts3PendingListDelete(pDef->pList);
5328 pDef->pList = 0;
5333 ** Free all entries in the pCsr->pDeffered list. Entries are added to
5334 ** this list using sqlite3Fts3DeferToken().
5336 void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
5337 Fts3DeferredToken *pDef;
5338 Fts3DeferredToken *pNext;
5339 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
5340 pNext = pDef->pNext;
5341 fts3PendingListDelete(pDef->pList);
5342 sqlite3_free(pDef);
5344 pCsr->pDeferred = 0;
5348 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
5349 ** based on the row that pCsr currently points to.
5351 ** A deferred-doclist is like any other doclist with position information
5352 ** included, except that it only contains entries for a single row of the
5353 ** table, not for all rows.
5355 int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
5356 int rc = SQLITE_OK; /* Return code */
5357 if( pCsr->pDeferred ){
5358 int i; /* Used to iterate through table columns */
5359 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
5360 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
5362 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
5363 sqlite3_tokenizer *pT = p->pTokenizer;
5364 sqlite3_tokenizer_module const *pModule = pT->pModule;
5366 assert( pCsr->isRequireSeek==0 );
5367 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
5369 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
5370 if( p->abNotindexed[i]==0 ){
5371 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
5372 sqlite3_tokenizer_cursor *pTC = 0;
5374 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
5375 while( rc==SQLITE_OK ){
5376 char const *zToken; /* Buffer containing token */
5377 int nToken = 0; /* Number of bytes in token */
5378 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
5379 int iPos = 0; /* Position of token in zText */
5381 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
5382 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
5383 Fts3PhraseToken *pPT = pDef->pToken;
5384 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
5385 && (pPT->bFirst==0 || iPos==0)
5386 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
5387 && (0==memcmp(zToken, pPT->z, pPT->n))
5389 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
5393 if( pTC ) pModule->xClose(pTC);
5394 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
5398 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
5399 if( pDef->pList ){
5400 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
5405 return rc;
5408 int sqlite3Fts3DeferredTokenList(
5409 Fts3DeferredToken *p,
5410 char **ppData,
5411 int *pnData
5413 char *pRet;
5414 int nSkip;
5415 sqlite3_int64 dummy;
5417 *ppData = 0;
5418 *pnData = 0;
5420 if( p->pList==0 ){
5421 return SQLITE_OK;
5424 pRet = (char *)sqlite3_malloc(p->pList->nData);
5425 if( !pRet ) return SQLITE_NOMEM;
5427 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
5428 *pnData = p->pList->nData - nSkip;
5429 *ppData = pRet;
5431 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
5432 return SQLITE_OK;
5436 ** Add an entry for token pToken to the pCsr->pDeferred list.
5438 int sqlite3Fts3DeferToken(
5439 Fts3Cursor *pCsr, /* Fts3 table cursor */
5440 Fts3PhraseToken *pToken, /* Token to defer */
5441 int iCol /* Column that token must appear in (or -1) */
5443 Fts3DeferredToken *pDeferred;
5444 pDeferred = sqlite3_malloc(sizeof(*pDeferred));
5445 if( !pDeferred ){
5446 return SQLITE_NOMEM;
5448 memset(pDeferred, 0, sizeof(*pDeferred));
5449 pDeferred->pToken = pToken;
5450 pDeferred->pNext = pCsr->pDeferred;
5451 pDeferred->iCol = iCol;
5452 pCsr->pDeferred = pDeferred;
5454 assert( pToken->pDeferred==0 );
5455 pToken->pDeferred = pDeferred;
5457 return SQLITE_OK;
5459 #endif
5462 ** SQLite value pRowid contains the rowid of a row that may or may not be
5463 ** present in the FTS3 table. If it is, delete it and adjust the contents
5464 ** of subsiduary data structures accordingly.
5466 static int fts3DeleteByRowid(
5467 Fts3Table *p,
5468 sqlite3_value *pRowid,
5469 int *pnChng, /* IN/OUT: Decrement if row is deleted */
5470 u32 *aSzDel
5472 int rc = SQLITE_OK; /* Return code */
5473 int bFound = 0; /* True if *pRowid really is in the table */
5475 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
5476 if( bFound && rc==SQLITE_OK ){
5477 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
5478 rc = fts3IsEmpty(p, pRowid, &isEmpty);
5479 if( rc==SQLITE_OK ){
5480 if( isEmpty ){
5481 /* Deleting this row means the whole table is empty. In this case
5482 ** delete the contents of all three tables and throw away any
5483 ** data in the pendingTerms hash table. */
5484 rc = fts3DeleteAll(p, 1);
5485 *pnChng = 0;
5486 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
5487 }else{
5488 *pnChng = *pnChng - 1;
5489 if( p->zContentTbl==0 ){
5490 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
5492 if( p->bHasDocsize ){
5493 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
5499 return rc;
5503 ** This function does the work for the xUpdate method of FTS3 virtual
5504 ** tables. The schema of the virtual table being:
5506 ** CREATE TABLE <table name>(
5507 ** <user columns>,
5508 ** <table name> HIDDEN,
5509 ** docid HIDDEN,
5510 ** <langid> HIDDEN
5511 ** );
5515 int sqlite3Fts3UpdateMethod(
5516 sqlite3_vtab *pVtab, /* FTS3 vtab object */
5517 int nArg, /* Size of argument array */
5518 sqlite3_value **apVal, /* Array of arguments */
5519 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
5521 Fts3Table *p = (Fts3Table *)pVtab;
5522 int rc = SQLITE_OK; /* Return Code */
5523 int isRemove = 0; /* True for an UPDATE or DELETE */
5524 u32 *aSzIns = 0; /* Sizes of inserted documents */
5525 u32 *aSzDel = 0; /* Sizes of deleted documents */
5526 int nChng = 0; /* Net change in number of documents */
5527 int bInsertDone = 0;
5529 /* At this point it must be known if the %_stat table exists or not.
5530 ** So bHasStat may not be 2. */
5531 assert( p->bHasStat==0 || p->bHasStat==1 );
5533 assert( p->pSegments==0 );
5534 assert(
5535 nArg==1 /* DELETE operations */
5536 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
5539 /* Check for a "special" INSERT operation. One of the form:
5541 ** INSERT INTO xyz(xyz) VALUES('command');
5543 if( nArg>1
5544 && sqlite3_value_type(apVal[0])==SQLITE_NULL
5545 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
5547 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
5548 goto update_out;
5551 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
5552 rc = SQLITE_CONSTRAINT;
5553 goto update_out;
5556 /* Allocate space to hold the change in document sizes */
5557 aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
5558 if( aSzDel==0 ){
5559 rc = SQLITE_NOMEM;
5560 goto update_out;
5562 aSzIns = &aSzDel[p->nColumn+1];
5563 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
5565 rc = fts3Writelock(p);
5566 if( rc!=SQLITE_OK ) goto update_out;
5568 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
5569 ** value, then this operation requires constraint handling.
5571 ** If the on-conflict mode is REPLACE, this means that the existing row
5572 ** should be deleted from the database before inserting the new row. Or,
5573 ** if the on-conflict mode is other than REPLACE, then this method must
5574 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
5575 ** modify the database file.
5577 if( nArg>1 && p->zContentTbl==0 ){
5578 /* Find the value object that holds the new rowid value. */
5579 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
5580 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
5581 pNewRowid = apVal[1];
5584 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
5585 sqlite3_value_type(apVal[0])==SQLITE_NULL
5586 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
5588 /* The new rowid is not NULL (in this case the rowid will be
5589 ** automatically assigned and there is no chance of a conflict), and
5590 ** the statement is either an INSERT or an UPDATE that modifies the
5591 ** rowid column. So if the conflict mode is REPLACE, then delete any
5592 ** existing row with rowid=pNewRowid.
5594 ** Or, if the conflict mode is not REPLACE, insert the new record into
5595 ** the %_content table. If we hit the duplicate rowid constraint (or any
5596 ** other error) while doing so, return immediately.
5598 ** This branch may also run if pNewRowid contains a value that cannot
5599 ** be losslessly converted to an integer. In this case, the eventual
5600 ** call to fts3InsertData() (either just below or further on in this
5601 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
5602 ** invoked, it will delete zero rows (since no row will have
5603 ** docid=$pNewRowid if $pNewRowid is not an integer value).
5605 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
5606 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
5607 }else{
5608 rc = fts3InsertData(p, apVal, pRowid);
5609 bInsertDone = 1;
5613 if( rc!=SQLITE_OK ){
5614 goto update_out;
5617 /* If this is a DELETE or UPDATE operation, remove the old record. */
5618 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5619 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
5620 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
5621 isRemove = 1;
5624 /* If this is an INSERT or UPDATE operation, insert the new record. */
5625 if( nArg>1 && rc==SQLITE_OK ){
5626 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
5627 if( bInsertDone==0 ){
5628 rc = fts3InsertData(p, apVal, pRowid);
5629 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
5630 rc = FTS_CORRUPT_VTAB;
5633 if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
5634 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
5636 if( rc==SQLITE_OK ){
5637 assert( p->iPrevDocid==*pRowid );
5638 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
5640 if( p->bHasDocsize ){
5641 fts3InsertDocsize(&rc, p, aSzIns);
5643 nChng++;
5646 if( p->bFts4 ){
5647 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
5650 update_out:
5651 sqlite3_free(aSzDel);
5652 sqlite3Fts3SegmentsClose(p);
5653 return rc;
5657 ** Flush any data in the pending-terms hash table to disk. If successful,
5658 ** merge all segments in the database (including the new segment, if
5659 ** there was any data to flush) into a single segment.
5661 int sqlite3Fts3Optimize(Fts3Table *p){
5662 int rc;
5663 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
5664 if( rc==SQLITE_OK ){
5665 rc = fts3DoOptimize(p, 1);
5666 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
5667 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
5668 if( rc2!=SQLITE_OK ) rc = rc2;
5669 }else{
5670 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
5671 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
5674 sqlite3Fts3SegmentsClose(p);
5675 return rc;
5678 #endif