Debian Codegraph @

ext/fts5/fts5_index.c

raw
1 /*
2 ** 2014 May 31
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** Low level access to the FTS index stored in the database file. The
14 ** routines in this file file implement all read and write access to the
15 ** %_data table. Other parts of the system access this functionality via
16 ** the interface defined in fts5Int.h.
17 */
18
19
20 #include "fts5Int.h"
21
22 /*
23 ** Overview:
24 **
25 ** The %_data table contains all the FTS indexes for an FTS5 virtual table.
26 ** As well as the main term index, there may be up to 31 prefix indexes.
27 ** The format is similar to FTS3/4, except that:
28 **
29 ** * all segment b-tree leaf data is stored in fixed size page records
30 ** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
31 ** taken to ensure it is possible to iterate in either direction through
32 ** the entries in a doclist, or to seek to a specific entry within a
33 ** doclist, without loading it into memory.
34 **
35 ** * large doclists that span many pages have associated "doclist index"
36 ** records that contain a copy of the first rowid on each page spanned by
37 ** the doclist. This is used to speed up seek operations, and merges of
38 ** large doclists with very small doclists.
39 **
40 ** * extra fields in the "structure record" record the state of ongoing
41 ** incremental merge operations.
42 **
43 */
44
45
46 #define FTS5_OPT_WORK_UNIT 1000 /* Number of leaf pages per optimize step */
47 #define FTS5_WORK_UNIT 64 /* Number of leaf pages in unit of work */
48
49 #define FTS5_MIN_DLIDX_SIZE 4 /* Add dlidx if this many empty pages */
50
51 #define FTS5_MAIN_PREFIX '0'
52
53 #if FTS5_MAX_PREFIX_INDEXES > 31
54 # error "FTS5_MAX_PREFIX_INDEXES is too large"
55 #endif
56
57 #define FTS5_MAX_LEVEL 64
58
59 /*
60 ** There are two versions of the format used for the structure record:
61 **
62 ** 1. the legacy format, that may be read by all fts5 versions, and
63 **
64 ** 2. the V2 format, which is used by contentless_delete=1 databases.
65 **
66 ** Both begin with a 4-byte "configuration cookie" value. Then, a legacy
67 ** format structure record contains a varint - the number of levels in
68 ** the structure. Whereas a V2 structure record contains the constant
69 ** 4 bytes [0xff 0x00 0x00 0x01]. This is unambiguous as the value of a
70 ** varint has to be at least 16256 to begin with "0xFF". And the default
71 ** maximum number of levels is 64.
72 **
73 ** See below for more on structure record formats.
74 */
75 #define FTS5_STRUCTURE_V2 "\xFF\x00\x00\x01"
76
77 /*
78 ** Details:
79 **
80 ** The %_data table managed by this module,
81 **
82 ** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
83 **
84 ** , contains the following 6 types of records. See the comments surrounding
85 ** the FTS5_*_ROWID macros below for a description of how %_data rowids are
86 ** assigned to each fo them.
87 **
88 ** 1. Structure Records:
89 **
90 ** The set of segments that make up an index - the index structure - are
91 ** recorded in a single record within the %_data table. The record consists
92 ** of a single 32-bit configuration cookie value followed by a list of
93 ** SQLite varints.
94 **
95 ** If the structure record is a V2 record, the configuration cookie is
96 ** followed by the following 4 bytes: [0xFF 0x00 0x00 0x01].
97 **
98 ** Next, the record continues with three varints:
99 **
100 ** + number of levels,
101 ** + total number of segments on all levels,
102 ** + value of write counter.
103 **
104 ** Then, for each level from 0 to nMax:
105 **
106 ** + number of input segments in ongoing merge.
107 ** + total number of segments in level.
108 ** + for each segment from oldest to newest:
109 ** + segment id (always > 0)
110 ** + first leaf page number (often 1, always greater than 0)
111 ** + final leaf page number
112 **
113 ** Then, for V2 structures only:
114 **
115 ** + lower origin counter value,
116 ** + upper origin counter value,
117 ** + the number of tombstone hash pages.
118 **
119 ** 2. The Averages Record:
120 **
121 ** A single record within the %_data table. The data is a list of varints.
122 ** The first value is the number of rows in the index. Then, for each column
123 ** from left to right, the total number of tokens in the column for all
124 ** rows of the table.
125 **
126 ** 3. Segment leaves:
127 **
128 ** TERM/DOCLIST FORMAT:
129 **
130 ** Most of each segment leaf is taken up by term/doclist data. The
131 ** general format of term/doclist, starting with the first term
132 ** on the leaf page, is:
133 **
134 ** varint : size of first term
135 ** blob: first term data
136 ** doclist: first doclist
137 ** zero-or-more {
138 ** varint: number of bytes in common with previous term
139 ** varint: number of bytes of new term data (nNew)
140 ** blob: nNew bytes of new term data
141 ** doclist: next doclist
142 ** }
143 **
144 ** doclist format:
145 **
146 ** varint: first rowid
147 ** poslist: first poslist
148 ** zero-or-more {
149 ** varint: rowid delta (always > 0)
150 ** poslist: next poslist
151 ** }
152 **
153 ** poslist format:
154 **
155 ** varint: size of poslist in bytes multiplied by 2, not including
156 ** this field. Plus 1 if this entry carries the "delete" flag.
157 ** collist: collist for column 0
158 ** zero-or-more {
159 ** 0x01 byte
160 ** varint: column number (I)
161 ** collist: collist for column I
162 ** }
163 **
164 ** collist format:
165 **
166 ** varint: first offset + 2
167 ** zero-or-more {
168 ** varint: offset delta + 2
169 ** }
170 **
171 ** PAGE FORMAT
172 **
173 ** Each leaf page begins with a 4-byte header containing 2 16-bit
174 ** unsigned integer fields in big-endian format. They are:
175 **
176 ** * The byte offset of the first rowid on the page, if it exists
177 ** and occurs before the first term (otherwise 0).
178 **
179 ** * The byte offset of the start of the page footer. If the page
180 ** footer is 0 bytes in size, then this field is the same as the
181 ** size of the leaf page in bytes.
182 **
183 ** The page footer consists of a single varint for each term located
184 ** on the page. Each varint is the byte offset of the current term
185 ** within the page, delta-compressed against the previous value. In
186 ** other words, the first varint in the footer is the byte offset of
187 ** the first term, the second is the byte offset of the second less that
188 ** of the first, and so on.
189 **
190 ** The term/doclist format described above is accurate if the entire
191 ** term/doclist data fits on a single leaf page. If this is not the case,
192 ** the format is changed in two ways:
193 **
194 ** + if the first rowid on a page occurs before the first term, it
195 ** is stored as a literal value:
196 **
197 ** varint: first rowid
198 **
199 ** + the first term on each page is stored in the same way as the
200 ** very first term of the segment:
201 **
202 ** varint : size of first term
203 ** blob: first term data
204 **
205 ** 5. Segment doclist indexes:
206 **
207 ** Doclist indexes are themselves b-trees, however they usually consist of
208 ** a single leaf record only. The format of each doclist index leaf page
209 ** is:
210 **
211 ** * Flags byte. Bits are:
212 ** 0x01: Clear if leaf is also the root page, otherwise set.
213 **
214 ** * Page number of fts index leaf page. As a varint.
215 **
216 ** * First rowid on page indicated by previous field. As a varint.
217 **
218 ** * A list of varints, one for each subsequent termless page. A
219 ** positive delta if the termless page contains at least one rowid,
220 ** or an 0x00 byte otherwise.
221 **
222 ** Internal doclist index nodes are:
223 **
224 ** * Flags byte. Bits are:
225 ** 0x01: Clear for root page, otherwise set.
226 **
227 ** * Page number of first child page. As a varint.
228 **
229 ** * Copy of first rowid on page indicated by previous field. As a varint.
230 **
231 ** * A list of delta-encoded varints - the first rowid on each subsequent
232 ** child page.
233 **
234 ** 6. Tombstone Hash Page
235 **
236 ** These records are only ever present in contentless_delete=1 tables.
237 ** There are zero or more of these associated with each segment. They
238 ** are used to store the tombstone rowids for rows contained in the
239 ** associated segments.
240 **
241 ** The set of nHashPg tombstone hash pages associated with a single
242 ** segment together form a single hash table containing tombstone rowids.
243 ** To find the page of the hash on which a key might be stored:
244 **
245 ** iPg = (rowid % nHashPg)
246 **
247 ** Then, within page iPg, which has nSlot slots:
248 **
249 ** iSlot = (rowid / nHashPg) % nSlot
250 **
251 ** Each tombstone hash page begins with an 8 byte header:
252 **
253 ** 1-byte: Key-size (the size in bytes of each slot). Either 4 or 8.
254 ** 1-byte: rowid-0-tombstone flag. This flag is only valid on the
255 ** first tombstone hash page for each segment (iPg=0). If set,
256 ** the hash table contains rowid 0. If clear, it does not.
257 ** Rowid 0 is handled specially.
258 ** 2-bytes: unused.
259 ** 4-bytes: Big-endian integer containing number of entries on page.
260 **
261 ** Following this are nSlot 4 or 8 byte slots (depending on the key-size
262 ** in the first byte of the page header). The number of slots may be
263 ** determined based on the size of the page record and the key-size:
264 **
265 ** nSlot = (nByte - 8) / key-size
266 */
267
268 /*
269 ** Rowids for the averages and structure records in the %_data table.
270 */
271 #define FTS5_AVERAGES_ROWID 1 /* Rowid used for the averages record */
272 #define FTS5_STRUCTURE_ROWID 10 /* The structure record */
273
274 /*
275 ** Macros determining the rowids used by segment leaves and dlidx leaves
276 ** and nodes. All nodes and leaves are stored in the %_data table with large
277 ** positive rowids.
278 **
279 ** Each segment has a unique non-zero 16-bit id.
280 **
281 ** The rowid for each segment leaf is found by passing the segment id and
282 ** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
283 ** sequentially starting from 1.
284 */
285 #define FTS5_DATA_ID_B 16 /* Max seg id number 65535 */
286 #define FTS5_DATA_DLI_B 1 /* Doclist-index flag (1 bit) */
287 #define FTS5_DATA_HEIGHT_B 5 /* Max dlidx tree height of 32 */
288 #define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
289
290 #define fts5_dri(segid, dlidx, height, pgno) ( \
291 ((i64)(segid) << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) + \
292 ((i64)(dlidx) << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) + \
293 ((i64)(height) << (FTS5_DATA_PAGE_B)) + \
294 ((i64)(pgno)) \
295 )
296
297 #define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno)
298 #define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
299 #define FTS5_TOMBSTONE_ROWID(segid,ipg) fts5_dri(segid+(1<<16), 0, 0, ipg)
300
301 #ifdef SQLITE_DEBUG
302 int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
303 #endif
304
305
306 /*
307 ** Each time a blob is read from the %_data table, it is padded with this
308 ** many zero bytes. This makes it easier to decode the various record formats
309 ** without overreading if the records are corrupt.
310 */
311 #define FTS5_DATA_ZERO_PADDING 8
312 #define FTS5_DATA_PADDING 20
313
314 typedef struct Fts5Data Fts5Data;
315 typedef struct Fts5DlidxIter Fts5DlidxIter;
316 typedef struct Fts5DlidxLvl Fts5DlidxLvl;
317 typedef struct Fts5DlidxWriter Fts5DlidxWriter;
318 typedef struct Fts5Iter Fts5Iter;
319 typedef struct Fts5PageWriter Fts5PageWriter;
320 typedef struct Fts5SegIter Fts5SegIter;
321 typedef struct Fts5DoclistIter Fts5DoclistIter;
322 typedef struct Fts5SegWriter Fts5SegWriter;
323 typedef struct Fts5Structure Fts5Structure;
324 typedef struct Fts5StructureLevel Fts5StructureLevel;
325 typedef struct Fts5StructureSegment Fts5StructureSegment;
326 typedef struct Fts5TokenDataIter Fts5TokenDataIter;
327 typedef struct Fts5TokenDataMap Fts5TokenDataMap;
328 typedef struct Fts5TombstoneArray Fts5TombstoneArray;
329
330 struct Fts5Data {
331 u8 *p; /* Pointer to buffer containing record */
332 int nn; /* Size of record in bytes */
333 int szLeaf; /* Size of leaf without page-index */
334 };
335
336 /*
337 ** One object per %_data table.
338 **
339 ** nContentlessDelete:
340 ** The number of contentless delete operations since the most recent
341 ** call to fts5IndexFlush() or fts5IndexDiscardData(). This is tracked
342 ** so that extra auto-merge work can be done by fts5IndexFlush() to
343 ** account for the delete operations.
344 */
345 struct Fts5Index {
346 Fts5Config *pConfig; /* Virtual table configuration */
347 char *zDataTbl; /* Name of %_data table */
348 int nWorkUnit; /* Leaf pages in a "unit" of work */
349
350 /*
351 ** Variables related to the accumulation of tokens and doclists within the
352 ** in-memory hash tables before they are flushed to disk.
353 */
354 Fts5Hash *pHash; /* Hash table for in-memory data */
355 int nPendingData; /* Current bytes of pending data */
356 i64 iWriteRowid; /* Rowid for current doc being written */
357 int bDelete; /* Current write is a delete */
358 int nContentlessDelete; /* Number of contentless delete ops */
359 int nPendingRow; /* Number of INSERT in hash table */
360
361 /* Error state. */
362 int rc; /* Current error code */
363 int flushRc;
364
365 /* State used by the fts5DataXXX() functions. */
366 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
367 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
368 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
369 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
370 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=?" */
371 sqlite3_stmt *pIdxSelect;
372 sqlite3_stmt *pIdxNextSelect;
373 int nRead; /* Total number of blocks read */
374
375 sqlite3_stmt *pDeleteFromIdx;
376
377 sqlite3_stmt *pDataVersion;
378 i64 iStructVersion; /* data_version when pStruct read */
379 Fts5Structure *pStruct; /* Current db structure (or NULL) */
380 };
381
382 struct Fts5DoclistIter {
383 u8 *aEof; /* Pointer to 1 byte past end of doclist */
384
385 /* Output variables. aPoslist==0 at EOF */
386 i64 iRowid;
387 u8 *aPoslist;
388 int nPoslist;
389 int nSize;
390 };
391
392 /*
393 ** The contents of the "structure" record for each index are represented
394 ** using an Fts5Structure record in memory. Which uses instances of the
395 ** other Fts5StructureXXX types as components.
396 **
397 ** nOriginCntr:
398 ** This value is set to non-zero for structure records created for
399 ** contentlessdelete=1 tables only. In that case it represents the
400 ** origin value to apply to the next top-level segment created.
401 */
402 struct Fts5StructureSegment {
403 int iSegid; /* Segment id */
404 int pgnoFirst; /* First leaf page number in segment */
405 int pgnoLast; /* Last leaf page number in segment */
406
407 /* contentlessdelete=1 tables only: */
408 u64 iOrigin1;
409 u64 iOrigin2;
410 int nPgTombstone; /* Number of tombstone hash table pages */
411 u64 nEntryTombstone; /* Number of tombstone entries that "count" */
412 u64 nEntry; /* Number of rows in this segment */
413 };
414 struct Fts5StructureLevel {
415 int nMerge; /* Number of segments in incr-merge */
416 int nSeg; /* Total number of segments on level */
417 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
418 };
419 struct Fts5Structure {
420 int nRef; /* Object reference count */
421 u64 nWriteCounter; /* Total leaves written to level 0 */
422 u64 nOriginCntr; /* Origin value for next top-level segment */
423 int nSegment; /* Total segments in this structure */
424 int nLevel; /* Number of levels in this index */
425 Fts5StructureLevel aLevel[FLEXARRAY]; /* Array of nLevel level objects */
426 };
427
428 /* Size (in bytes) of an Fts5Structure object holding up to N levels */
429 #define SZ_FTS5STRUCTURE(N) \
430 (offsetof(Fts5Structure,aLevel) + (N)*sizeof(Fts5StructureLevel))
431
432 /*
433 ** An object of type Fts5SegWriter is used to write to segments.
434 */
435 struct Fts5PageWriter {
436 int pgno; /* Page number for this page */
437 int iPrevPgidx; /* Previous value written into pgidx */
438 Fts5Buffer buf; /* Buffer containing leaf data */
439 Fts5Buffer pgidx; /* Buffer containing page-index */
440 Fts5Buffer term; /* Buffer containing previous term on page */
441 };
442 struct Fts5DlidxWriter {
443 int pgno; /* Page number for this page */
444 int bPrevValid; /* True if iPrev is valid */
445 i64 iPrev; /* Previous rowid value written to page */
446 Fts5Buffer buf; /* Buffer containing page data */
447 };
448 struct Fts5SegWriter {
449 int iSegid; /* Segid to write to */
450 Fts5PageWriter writer; /* PageWriter object */
451 i64 iPrevRowid; /* Previous rowid written to current leaf */
452 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
453 u8 bFirstRowidInPage; /* True if next rowid is first in page */
454 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
455 u8 bFirstTermInPage; /* True if next term will be first in leaf */
456 int nLeafWritten; /* Number of leaf pages written */
457 int nEmpty; /* Number of contiguous term-less nodes */
458
459 int nDlidx; /* Allocated size of aDlidx[] array */
460 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
461
462 /* Values to insert into the %_idx table */
463 Fts5Buffer btterm; /* Next term to insert into %_idx table */
464 int iBtPage; /* Page number corresponding to btterm */
465 };
466
467 typedef struct Fts5CResult Fts5CResult;
468 struct Fts5CResult {
469 u16 iFirst; /* aSeg[] index of firstest iterator */
470 u8 bTermEq; /* True if the terms are equal */
471 };
472
473 /*
474 ** Object for iterating through a single segment, visiting each term/rowid
475 ** pair in the segment.
476 **
477 ** pSeg:
478 ** The segment to iterate through.
479 **
480 ** iLeafPgno:
481 ** Current leaf page number within segment.
482 **
483 ** iLeafOffset:
484 ** Byte offset within the current leaf that is the first byte of the
485 ** position list data (one byte passed the position-list size field).
486 **
487 ** pLeaf:
488 ** Buffer containing current leaf page data. Set to NULL at EOF.
489 **
490 ** iTermLeafPgno, iTermLeafOffset:
491 ** Leaf page number containing the last term read from the segment. And
492 ** the offset immediately following the term data.
493 **
494 ** flags:
495 ** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
496 **
497 ** FTS5_SEGITER_ONETERM:
498 ** If set, set the iterator to point to EOF after the current doclist
499 ** has been exhausted. Do not proceed to the next term in the segment.
500 **
501 ** FTS5_SEGITER_REVERSE:
502 ** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
503 ** it is set, iterate through rowid in descending order instead of the
504 ** default ascending order.
505 **
506 ** iRowidOffset/nRowidOffset/aRowidOffset:
507 ** These are used if the FTS5_SEGITER_REVERSE flag is set.
508 **
509 ** For each rowid on the page corresponding to the current term, the
510 ** corresponding aRowidOffset[] entry is set to the byte offset of the
511 ** start of the "position-list-size" field within the page.
512 **
513 ** iTermIdx:
514 ** Index of current term on iTermLeafPgno.
515 **
516 ** apTombstone/nTombstone:
517 ** These are used for contentless_delete=1 tables only. When the cursor
518 ** is first allocated, the apTombstone[] array is allocated so that it
519 ** is large enough for all tombstones hash pages associated with the
520 ** segment. The pages themselves are loaded lazily from the database as
521 ** they are required.
522 */
523 struct Fts5SegIter {
524 Fts5StructureSegment *pSeg; /* Segment to iterate through */
525 int flags; /* Mask of configuration flags */
526 int iLeafPgno; /* Current leaf page number */
527 Fts5Data *pLeaf; /* Current leaf data */
528 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
529 i64 iLeafOffset; /* Byte offset within current leaf */
530 Fts5TombstoneArray *pTombArray; /* Array of tombstone pages */
531
532 /* Next method */
533 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
534
535 /* The page and offset from which the current term was read. The offset
536 ** is the offset of the first rowid in the current doclist. */
537 int iTermLeafPgno;
538 int iTermLeafOffset;
539
540 int iPgidxOff; /* Next offset in pgidx */
541 int iEndofDoclist;
542
543 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
544 int iRowidOffset; /* Current entry in aRowidOffset[] */
545 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
546 int *aRowidOffset; /* Array of offset to rowid fields */
547
548 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
549
550 /* Variables populated based on current entry. */
551 Fts5Buffer term; /* Current term */
552 i64 iRowid; /* Current rowid */
553 int nPos; /* Number of bytes in current position list */
554 u8 bDel; /* True if the delete flag is set */
555 };
556
557 static int fts5IndexCorruptRowid(Fts5Index *pIdx, i64 iRowid){
558 pIdx->rc = FTS5_CORRUPT;
559 sqlite3Fts5ConfigErrmsg(pIdx->pConfig,
560 "fts5: corruption found reading blob %lld from table \"%s\"",
561 iRowid, pIdx->pConfig->zName
562 );
563 return SQLITE_CORRUPT_VTAB;
564 }
565 #define FTS5_CORRUPT_ROWID(pIdx, iRowid) fts5IndexCorruptRowid(pIdx, iRowid)
566
567 static int fts5IndexCorruptIter(Fts5Index *pIdx, Fts5SegIter *pIter){
568 pIdx->rc = FTS5_CORRUPT;
569 sqlite3Fts5ConfigErrmsg(pIdx->pConfig,
570 "fts5: corruption on page %d, segment %d, table \"%s\"",
571 pIter->iLeafPgno, pIter->pSeg->iSegid, pIdx->pConfig->zName
572 );
573 return SQLITE_CORRUPT_VTAB;
574 }
575 #define FTS5_CORRUPT_ITER(pIdx, pIter) fts5IndexCorruptIter(pIdx, pIter)
576
577 static int fts5IndexCorruptIdx(Fts5Index *pIdx){
578 pIdx->rc = FTS5_CORRUPT;
579 sqlite3Fts5ConfigErrmsg(pIdx->pConfig,
580 "fts5: corruption in table \"%s\"", pIdx->pConfig->zName
581 );
582 return SQLITE_CORRUPT_VTAB;
583 }
584 #define FTS5_CORRUPT_IDX(pIdx) fts5IndexCorruptIdx(pIdx)
585
586
587 /*
588 ** Array of tombstone pages. Reference counted.
589 */
590 struct Fts5TombstoneArray {
591 int nRef; /* Number of pointers to this object */
592 int nTombstone;
593 Fts5Data *apTombstone[FLEXARRAY]; /* Array of tombstone pages */
594 };
595
596 /* Size (in bytes) of an Fts5TombstoneArray holding up to N tombstones */
597 #define SZ_FTS5TOMBSTONEARRAY(N) \
598 (offsetof(Fts5TombstoneArray,apTombstone)+(N)*sizeof(Fts5Data*))
599
600 /*
601 ** Argument is a pointer to an Fts5Data structure that contains a
602 ** leaf page.
603 */
604 #define ASSERT_SZLEAF_OK(x) assert( \
605 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
606 )
607
608 #define FTS5_SEGITER_ONETERM 0x01
609 #define FTS5_SEGITER_REVERSE 0x02
610
611 /*
612 ** Argument is a pointer to an Fts5Data structure that contains a leaf
613 ** page. This macro evaluates to true if the leaf contains no terms, or
614 ** false if it contains at least one term.
615 */
616 #define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
617
618 #define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
619
620 #define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
621
622 /*
623 ** Object for iterating through the merged results of one or more segments,
624 ** visiting each term/rowid pair in the merged data.
625 **
626 ** nSeg is always a power of two greater than or equal to the number of
627 ** segments that this object is merging data from. Both the aSeg[] and
628 ** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
629 ** with zeroed objects - these are handled as if they were iterators opened
630 ** on empty segments.
631 **
632 ** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
633 ** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
634 ** comparison in this context is the index of the iterator that currently
635 ** points to the smaller term/rowid combination. Iterators at EOF are
636 ** considered to be greater than all other iterators.
637 **
638 ** aFirst[1] contains the index in aSeg[] of the iterator that points to
639 ** the smallest key overall. aFirst[0] is unused.
640 **
641 ** poslist:
642 ** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
643 ** There is no way to tell if this is populated or not.
644 **
645 ** pColset:
646 ** If not NULL, points to an object containing a set of column indices.
647 ** Only matches that occur in one of these columns will be returned.
648 ** The Fts5Iter does not own the Fts5Colset object, and so it is not
649 ** freed when the iterator is closed - it is owned by the upper layer.
650 */
651 struct Fts5Iter {
652 Fts5IndexIter base; /* Base class containing output vars */
653 Fts5TokenDataIter *pTokenDataIter;
654
655 Fts5Index *pIndex; /* Index that owns this iterator */
656 Fts5Buffer poslist; /* Buffer containing current poslist */
657 Fts5Colset *pColset; /* Restrict matches to these columns */
658
659 /* Invoked to set output variables. */
660 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
661
662 int nSeg; /* Size of aSeg[] array */
663 int bRev; /* True to iterate in reverse order */
664 u8 bSkipEmpty; /* True to skip deleted entries */
665
666 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
667 Fts5CResult *aFirst; /* Current merge state (see above) */
668 Fts5SegIter aSeg[FLEXARRAY]; /* Array of segment iterators */
669 };
670
671 /* Size (in bytes) of an Fts5Iter object holding up to N segment iterators */
672 #define SZ_FTS5ITER(N) (offsetof(Fts5Iter,aSeg)+(N)*sizeof(Fts5SegIter))
673
674 /*
675 ** An instance of the following type is used to iterate through the contents
676 ** of a doclist-index record.
677 **
678 ** pData:
679 ** Record containing the doclist-index data.
680 **
681 ** bEof:
682 ** Set to true once iterator has reached EOF.
683 **
684 ** iOff:
685 ** Set to the current offset within record pData.
686 */
687 struct Fts5DlidxLvl {
688 Fts5Data *pData; /* Data for current page of this level */
689 int iOff; /* Current offset into pData */
690 int bEof; /* At EOF already */
691 int iFirstOff; /* Used by reverse iterators */
692
693 /* Output variables */
694 int iLeafPgno; /* Page number of current leaf page */
695 i64 iRowid; /* First rowid on leaf iLeafPgno */
696 };
697 struct Fts5DlidxIter {
698 int nLvl;
699 int iSegid;
700 Fts5DlidxLvl aLvl[FLEXARRAY];
701 };
702
703 /* Size (in bytes) of an Fts5DlidxIter object with up to N levels */
704 #define SZ_FTS5DLIDXITER(N) \
705 (offsetof(Fts5DlidxIter,aLvl)+(N)*sizeof(Fts5DlidxLvl))
706
707 static void fts5PutU16(u8 *aOut, u16 iVal){
708 aOut[0] = (iVal>>8);
709 aOut[1] = (iVal&0xFF);
710 }
711
712 static u16 fts5GetU16(const u8 *aIn){
713 return ((u16)aIn[0] << 8) + aIn[1];
714 }
715
716 /*
717 ** The only argument points to a buffer at least 8 bytes in size. This
718 ** function interprets the first 8 bytes of the buffer as a 64-bit big-endian
719 ** unsigned integer and returns the result.
720 */
721 static u64 fts5GetU64(u8 *a){
722 return ((u64)a[0] << 56)
723 + ((u64)a[1] << 48)
724 + ((u64)a[2] << 40)
725 + ((u64)a[3] << 32)
726 + ((u64)a[4] << 24)
727 + ((u64)a[5] << 16)
728 + ((u64)a[6] << 8)
729 + ((u64)a[7] << 0);
730 }
731
732 /*
733 ** The only argument points to a buffer at least 4 bytes in size. This
734 ** function interprets the first 4 bytes of the buffer as a 32-bit big-endian
735 ** unsigned integer and returns the result.
736 */
737 static u32 fts5GetU32(const u8 *a){
738 return ((u32)a[0] << 24)
739 + ((u32)a[1] << 16)
740 + ((u32)a[2] << 8)
741 + ((u32)a[3] << 0);
742 }
743
744 /*
745 ** Write iVal, formated as a 64-bit big-endian unsigned integer, to the
746 ** buffer indicated by the first argument.
747 */
748 static void fts5PutU64(u8 *a, u64 iVal){
749 a[0] = ((iVal >> 56) & 0xFF);
750 a[1] = ((iVal >> 48) & 0xFF);
751 a[2] = ((iVal >> 40) & 0xFF);
752 a[3] = ((iVal >> 32) & 0xFF);
753 a[4] = ((iVal >> 24) & 0xFF);
754 a[5] = ((iVal >> 16) & 0xFF);
755 a[6] = ((iVal >> 8) & 0xFF);
756 a[7] = ((iVal >> 0) & 0xFF);
757 }
758
759 /*
760 ** Write iVal, formated as a 32-bit big-endian unsigned integer, to the
761 ** buffer indicated by the first argument.
762 */
763 static void fts5PutU32(u8 *a, u32 iVal){
764 a[0] = ((iVal >> 24) & 0xFF);
765 a[1] = ((iVal >> 16) & 0xFF);
766 a[2] = ((iVal >> 8) & 0xFF);
767 a[3] = ((iVal >> 0) & 0xFF);
768 }
769
770 /*
771 ** Allocate and return a buffer at least nByte bytes in size.
772 **
773 ** If an OOM error is encountered, return NULL and set the error code in
774 ** the Fts5Index handle passed as the first argument.
775 */
776 static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
777 return sqlite3Fts5MallocZero(&p->rc, nByte);
778 }
779
780 /*
781 ** Compare the contents of the pLeft buffer with the pRight/nRight blob.
782 **
783 ** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
784 ** +ve if pRight is smaller than pLeft. In other words:
785 **
786 ** res = *pLeft - *pRight
787 */
788 #ifdef SQLITE_DEBUG
789 static int fts5BufferCompareBlob(
790 Fts5Buffer *pLeft, /* Left hand side of comparison */
791 const u8 *pRight, int nRight /* Right hand side of comparison */
792 ){
793 int nCmp = MIN(pLeft->n, nRight);
794 int res = memcmp(pLeft->p, pRight, nCmp);
795 return (res==0 ? (pLeft->n - nRight) : res);
796 }
797 #endif
798
799 /*
800 ** Compare the contents of the two buffers using memcmp(). If one buffer
801 ** is a prefix of the other, it is considered the lesser.
802 **
803 ** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
804 ** +ve if pRight is smaller than pLeft. In other words:
805 **
806 ** res = *pLeft - *pRight
807 */
808 static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
809 int nCmp, res;
810 nCmp = MIN(pLeft->n, pRight->n);
811 assert( nCmp<=0 || pLeft->p!=0 );
812 assert( nCmp<=0 || pRight->p!=0 );
813 res = fts5Memcmp(pLeft->p, pRight->p, nCmp);
814 return (res==0 ? (pLeft->n - pRight->n) : res);
815 }
816
817 static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
818 int ret;
819 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
820 return ret;
821 }
822
823 /*
824 ** Close the read-only blob handle, if it is open.
825 */
826 static void fts5IndexCloseReader(Fts5Index *p){
827 if( p->pReader ){
828 int rc;
829 sqlite3_blob *pReader = p->pReader;
830 p->pReader = 0;
831 rc = sqlite3_blob_close(pReader);
832 if( p->rc==SQLITE_OK ) p->rc = rc;
833 }
834 }
835
836 /*
837 ** Retrieve a record from the %_data table.
838 **
839 ** If an error occurs, NULL is returned and an error left in the
840 ** Fts5Index object.
841 */
842 static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
843 Fts5Data *pRet = 0;
844 if( p->rc==SQLITE_OK ){
845 int rc = SQLITE_OK;
846
847 if( p->pReader ){
848 /* This call may return SQLITE_ABORT if there has been a savepoint
849 ** rollback since it was last used. In this case a new blob handle
850 ** is required. */
851 sqlite3_blob *pBlob = p->pReader;
852 p->pReader = 0;
853 rc = sqlite3_blob_reopen(pBlob, iRowid);
854 assert( p->pReader==0 );
855 p->pReader = pBlob;
856 if( rc!=SQLITE_OK ){
857 fts5IndexCloseReader(p);
858 }
859 if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
860 }
861
862 /* If the blob handle is not open at this point, open it and seek
863 ** to the requested entry. */
864 if( p->pReader==0 && rc==SQLITE_OK ){
865 Fts5Config *pConfig = p->pConfig;
866 rc = sqlite3_blob_open(pConfig->db,
867 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
868 );
869 }
870
871 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
872 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
873 ** All the reasons those functions might return SQLITE_ERROR - missing
874 ** table, missing row, non-blob/text in block column - indicate
875 ** backing store corruption. */
876 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT_ROWID(p, iRowid);
877
878 if( rc==SQLITE_OK ){
879 u8 *aOut = 0; /* Read blob data into this buffer */
880 i64 nByte = sqlite3_blob_bytes(p->pReader);
881 i64 szData = (sizeof(Fts5Data) + 7) & ~7;
882 i64 nAlloc = szData + nByte + FTS5_DATA_PADDING;
883 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
884 if( pRet ){
885 pRet->nn = nByte;
886 aOut = pRet->p = (u8*)pRet + szData;
887 }else{
888 rc = SQLITE_NOMEM;
889 }
890
891 if( rc==SQLITE_OK ){
892 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
893 }
894 if( rc!=SQLITE_OK ){
895 sqlite3_free(pRet);
896 pRet = 0;
897 }else{
898 /* TODO1: Fix this */
899 pRet->p[nByte] = 0x00;
900 pRet->p[nByte+1] = 0x00;
901 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
902 }
903 }
904 p->rc = rc;
905 p->nRead++;
906 }
907
908 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
909 assert( pRet==0 || EIGHT_BYTE_ALIGNMENT( pRet->p ) );
910 return pRet;
911 }
912
913
914 /*
915 ** Release a reference to data record returned by an earlier call to
916 ** fts5DataRead().
917 */
918 static void fts5DataRelease(Fts5Data *pData){
919 sqlite3_free(pData);
920 }
921
922 static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
923 Fts5Data *pRet = fts5DataRead(p, iRowid);
924 if( pRet ){
925 if( pRet->szLeaf<4 || pRet->szLeaf>pRet->nn ){
926 FTS5_CORRUPT_ROWID(p, iRowid);
927 fts5DataRelease(pRet);
928 pRet = 0;
929 }
930 }
931 return pRet;
932 }
933
934 static int fts5IndexPrepareStmt(
935 Fts5Index *p,
936 sqlite3_stmt **ppStmt,
937 char *zSql
938 ){
939 if( p->rc==SQLITE_OK ){
940 if( zSql ){
941 int rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
942 SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB,
943 ppStmt, 0);
944 /* If this prepare() call fails with SQLITE_ERROR, then one of the
945 ** %_idx or %_data tables has been removed or modified. Call this
946 ** corruption. */
947 p->rc = (rc==SQLITE_ERROR ? SQLITE_CORRUPT : rc);
948 }else{
949 p->rc = SQLITE_NOMEM;
950 }
951 }
952 sqlite3_free(zSql);
953 return p->rc;
954 }
955
956
957 /*
958 ** INSERT OR REPLACE a record into the %_data table.
959 */
960 static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
961 if( p->rc!=SQLITE_OK ) return;
962
963 if( p->pWriter==0 ){
964 Fts5Config *pConfig = p->pConfig;
965 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
966 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
967 pConfig->zDb, pConfig->zName
968 ));
969 if( p->rc ) return;
970 }
971
972 sqlite3_bind_int64(p->pWriter, 1, iRowid);
973 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
974 sqlite3_step(p->pWriter);
975 p->rc = sqlite3_reset(p->pWriter);
976 sqlite3_bind_null(p->pWriter, 2);
977 }
978
979 /*
980 ** Execute the following SQL:
981 **
982 ** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
983 */
984 static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
985 if( p->rc!=SQLITE_OK ) return;
986
987 if( p->pDeleter==0 ){
988 Fts5Config *pConfig = p->pConfig;
989 char *zSql = sqlite3_mprintf(
990 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
991 pConfig->zDb, pConfig->zName
992 );
993 if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
994 }
995
996 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
997 sqlite3_bind_int64(p->pDeleter, 2, iLast);
998 sqlite3_step(p->pDeleter);
999 p->rc = sqlite3_reset(p->pDeleter);
1000 }
1001
1002 /*
1003 ** Remove all records associated with segment iSegid.
1004 */
1005 static void fts5DataRemoveSegment(Fts5Index *p, Fts5StructureSegment *pSeg){
1006 int iSegid = pSeg->iSegid;
1007 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
1008 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
1009 fts5DataDelete(p, iFirst, iLast);
1010
1011 if( pSeg->nPgTombstone ){
1012 i64 iTomb1 = FTS5_TOMBSTONE_ROWID(iSegid, 0);
1013 i64 iTomb2 = FTS5_TOMBSTONE_ROWID(iSegid, pSeg->nPgTombstone-1);
1014 fts5DataDelete(p, iTomb1, iTomb2);
1015 }
1016 if( p->pIdxDeleter==0 ){
1017 Fts5Config *pConfig = p->pConfig;
1018 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
1019 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
1020 pConfig->zDb, pConfig->zName
1021 ));
1022 }
1023 if( p->rc==SQLITE_OK ){
1024 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
1025 sqlite3_step(p->pIdxDeleter);
1026 p->rc = sqlite3_reset(p->pIdxDeleter);
1027 }
1028 }
1029
1030 /*
1031 ** Release a reference to an Fts5Structure object returned by an earlier
1032 ** call to fts5StructureRead() or fts5StructureDecode().
1033 */
1034 static void fts5StructureRelease(Fts5Structure *pStruct){
1035 if( pStruct && 0>=(--pStruct->nRef) ){
1036 int i;
1037 assert( pStruct->nRef==0 );
1038 for(i=0; i<pStruct->nLevel; i++){
1039 sqlite3_free(pStruct->aLevel[i].aSeg);
1040 }
1041 sqlite3_free(pStruct);
1042 }
1043 }
1044
1045 static void fts5StructureRef(Fts5Structure *pStruct){
1046 pStruct->nRef++;
1047 }
1048
1049 void *sqlite3Fts5StructureRef(Fts5Index *p){
1050 fts5StructureRef(p->pStruct);
1051 return (void*)p->pStruct;
1052 }
1053 void sqlite3Fts5StructureRelease(void *p){
1054 if( p ){
1055 fts5StructureRelease((Fts5Structure*)p);
1056 }
1057 }
1058 int sqlite3Fts5StructureTest(Fts5Index *p, void *pStruct){
1059 if( p->pStruct!=(Fts5Structure*)pStruct ){
1060 return SQLITE_ABORT;
1061 }
1062 return SQLITE_OK;
1063 }
1064
1065 /*
1066 ** Ensure that structure object (*pp) is writable.
1067 **
1068 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If
1069 ** an error occurs, (*pRc) is set to an SQLite error code before returning.
1070 */
1071 static void fts5StructureMakeWritable(int *pRc, Fts5Structure **pp){
1072 Fts5Structure *p = *pp;
1073 if( *pRc==SQLITE_OK && p->nRef>1 ){
1074 i64 nByte = SZ_FTS5STRUCTURE(p->nLevel);
1075 Fts5Structure *pNew;
1076 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc, nByte);
1077 if( pNew ){
1078 int i;
1079 memcpy(pNew, p, nByte);
1080 for(i=0; i<p->nLevel; i++) pNew->aLevel[i].aSeg = 0;
1081 for(i=0; i<p->nLevel; i++){
1082 Fts5StructureLevel *pLvl = &pNew->aLevel[i];
1083 nByte = sizeof(Fts5StructureSegment) * pNew->aLevel[i].nSeg;
1084 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc, nByte);
1085 if( pLvl->aSeg==0 ){
1086 for(i=0; i<p->nLevel; i++){
1087 sqlite3_free(pNew->aLevel[i].aSeg);
1088 }
1089 sqlite3_free(pNew);
1090 return;
1091 }
1092 memcpy(pLvl->aSeg, p->aLevel[i].aSeg, nByte);
1093 }
1094 p->nRef--;
1095 pNew->nRef = 1;
1096 }
1097 *pp = pNew;
1098 }
1099 }
1100
1101 /*
1102 ** Deserialize and return the structure record currently stored in serialized
1103 ** form within buffer pData/nData.
1104 **
1105 ** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
1106 ** are over-allocated by one slot. This allows the structure contents
1107 ** to be more easily edited.
1108 **
1109 ** If an error occurs, *ppOut is set to NULL and an SQLite error code
1110 ** returned. Otherwise, *ppOut is set to point to the new object and
1111 ** SQLITE_OK returned.
1112 */
1113 static int fts5StructureDecode(
1114 const u8 *pData, /* Buffer containing serialized structure */
1115 int nData, /* Size of buffer pData in bytes */
1116 int *piCookie, /* Configuration cookie value */
1117 Fts5Structure **ppOut /* OUT: Deserialized object */
1118 ){
1119 int rc = SQLITE_OK;
1120 int i = 0;
1121 int iLvl;
1122 int nLevel = 0;
1123 int nSegment = 0;
1124 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
1125 Fts5Structure *pRet = 0; /* Structure object to return */
1126 int bStructureV2 = 0; /* True for FTS5_STRUCTURE_V2 */
1127 u64 nOriginCntr = 0; /* Largest origin value seen so far */
1128
1129 /* Grab the cookie value */
1130 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
1131 i = 4;
1132
1133 /* Check if this is a V2 structure record. Set bStructureV2 if it is. */
1134 if( 0==memcmp(&pData[i], FTS5_STRUCTURE_V2, 4) ){
1135 i += 4;
1136 bStructureV2 = 1;
1137 }
1138
1139 /* Read the total number of levels and segments from the start of the
1140 ** structure record. */
1141 i += fts5GetVarint32(&pData[i], nLevel);
1142 i += fts5GetVarint32(&pData[i], nSegment);
1143 if( nLevel>FTS5_MAX_SEGMENT || nLevel<0
1144 || nSegment>FTS5_MAX_SEGMENT || nSegment<0
1145 ){
1146 return FTS5_CORRUPT;
1147 }
1148 nByte = SZ_FTS5STRUCTURE(nLevel);
1149 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
1150
1151 if( pRet ){
1152 pRet->nRef = 1;
1153 pRet->nLevel = nLevel;
1154 pRet->nSegment = nSegment;
1155 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
1156
1157 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
1158 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
1159 int nTotal = 0;
1160 int iSeg;
1161
1162 if( i>=nData ){
1163 rc = FTS5_CORRUPT;
1164 }else{
1165 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
1166 i += fts5GetVarint32(&pData[i], nTotal);
1167 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT;
1168 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
1169 nTotal * sizeof(Fts5StructureSegment)
1170 );
1171 nSegment -= nTotal;
1172 }
1173
1174 if( rc==SQLITE_OK ){
1175 pLvl->nSeg = nTotal;
1176 for(iSeg=0; iSeg<nTotal; iSeg++){
1177 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
1178 if( i>=nData ){
1179 rc = FTS5_CORRUPT;
1180 break;
1181 }
1182 assert( pSeg!=0 );
1183 i += fts5GetVarint32(&pData[i], pSeg->iSegid);
1184 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst);
1185 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast);
1186 if( bStructureV2 ){
1187 i += fts5GetVarint(&pData[i], &pSeg->iOrigin1);
1188 i += fts5GetVarint(&pData[i], &pSeg->iOrigin2);
1189 i += fts5GetVarint32(&pData[i], pSeg->nPgTombstone);
1190 i += fts5GetVarint(&pData[i], &pSeg->nEntryTombstone);
1191 i += fts5GetVarint(&pData[i], &pSeg->nEntry);
1192 nOriginCntr = MAX(nOriginCntr, pSeg->iOrigin2);
1193 }
1194 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
1195 rc = FTS5_CORRUPT;
1196 break;
1197 }
1198 }
1199 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT;
1200 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT;
1201 }
1202 }
1203 if( nSegment!=0 && rc==SQLITE_OK ) rc = FTS5_CORRUPT;
1204 if( bStructureV2 ){
1205 pRet->nOriginCntr = nOriginCntr+1;
1206 }
1207
1208 if( rc!=SQLITE_OK ){
1209 fts5StructureRelease(pRet);
1210 pRet = 0;
1211 }
1212 }
1213
1214 *ppOut = pRet;
1215 return rc;
1216 }
1217
1218 /*
1219 ** Add a level to the Fts5Structure.aLevel[] array of structure object
1220 ** (*ppStruct).
1221 */
1222 static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
1223 fts5StructureMakeWritable(pRc, ppStruct);
1224 assert( (ppStruct!=0 && (*ppStruct)!=0) || (*pRc)!=SQLITE_OK );
1225 if( *pRc==SQLITE_OK ){
1226 Fts5Structure *pStruct = *ppStruct;
1227 int nLevel = pStruct->nLevel;
1228 sqlite3_int64 nByte = SZ_FTS5STRUCTURE(nLevel+2);
1229
1230 pStruct = sqlite3_realloc64(pStruct, nByte);
1231 if( pStruct ){
1232 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
1233 pStruct->nLevel++;
1234 *ppStruct = pStruct;
1235 }else{
1236 *pRc = SQLITE_NOMEM;
1237 }
1238 }
1239 }
1240
1241 /*
1242 ** Extend level iLvl so that there is room for at least nExtra more
1243 ** segments.
1244 */
1245 static void fts5StructureExtendLevel(
1246 int *pRc,
1247 Fts5Structure *pStruct,
1248 int iLvl,
1249 int nExtra,
1250 int bInsert
1251 ){
1252 if( *pRc==SQLITE_OK ){
1253 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
1254 Fts5StructureSegment *aNew;
1255 sqlite3_int64 nByte;
1256
1257 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
1258 aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
1259 if( aNew ){
1260 if( bInsert==0 ){
1261 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
1262 }else{
1263 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
1264 memmove(&aNew[nExtra], aNew, nMove);
1265 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
1266 }
1267 pLvl->aSeg = aNew;
1268 }else{
1269 *pRc = SQLITE_NOMEM;
1270 }
1271 }
1272 }
1273
1274 static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
1275 Fts5Structure *pRet = 0;
1276 Fts5Config *pConfig = p->pConfig;
1277 int iCookie; /* Configuration cookie */
1278 Fts5Data *pData;
1279
1280 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
1281 if( p->rc==SQLITE_OK ){
1282 /* TODO: Do we need this if the leaf-index is appended? Probably... */
1283 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING);
1284 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
1285 if( p->rc==SQLITE_OK ){
1286 if( (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
1287 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
1288 }
1289 }else if( p->rc==SQLITE_CORRUPT_VTAB ){
1290 sqlite3Fts5ConfigErrmsg(p->pConfig,
1291 "fts5: corrupt structure record for table \"%s\"", p->pConfig->zName
1292 );
1293 }
1294 fts5DataRelease(pData);
1295 if( p->rc!=SQLITE_OK ){
1296 fts5StructureRelease(pRet);
1297 pRet = 0;
1298 }
1299 }
1300
1301 return pRet;
1302 }
1303
1304 static i64 fts5IndexDataVersion(Fts5Index *p){
1305 i64 iVersion = 0;
1306
1307 if( p->rc==SQLITE_OK ){
1308 if( p->pDataVersion==0 ){
1309 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
1310 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
1311 );
1312 if( p->rc ) return 0;
1313 }
1314
1315 if( SQLITE_ROW==sqlite3_step(p->pDataVersion) ){
1316 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
1317 }
1318 p->rc = sqlite3_reset(p->pDataVersion);
1319 }
1320
1321 return iVersion;
1322 }
1323
1324 /*
1325 ** Read, deserialize and return the structure record.
1326 **
1327 ** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
1328 ** are over-allocated as described for function fts5StructureDecode()
1329 ** above.
1330 **
1331 ** If an error occurs, NULL is returned and an error code left in the
1332 ** Fts5Index handle. If an error has already occurred when this function
1333 ** is called, it is a no-op.
1334 */
1335 static Fts5Structure *fts5StructureRead(Fts5Index *p){
1336
1337 if( p->pStruct==0 ){
1338 p->iStructVersion = fts5IndexDataVersion(p);
1339 if( p->rc==SQLITE_OK ){
1340 p->pStruct = fts5StructureReadUncached(p);
1341 }
1342 }
1343
1344 #if 0
1345 else{
1346 Fts5Structure *pTest = fts5StructureReadUncached(p);
1347 if( pTest ){
1348 int i, j;
1349 assert_nc( p->pStruct->nSegment==pTest->nSegment );
1350 assert_nc( p->pStruct->nLevel==pTest->nLevel );
1351 for(i=0; i<pTest->nLevel; i++){
1352 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
1353 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
1354 for(j=0; j<pTest->aLevel[i].nSeg; j++){
1355 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
1356 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
1357 assert_nc( p1->iSegid==p2->iSegid );
1358 assert_nc( p1->pgnoFirst==p2->pgnoFirst );
1359 assert_nc( p1->pgnoLast==p2->pgnoLast );
1360 }
1361 }
1362 fts5StructureRelease(pTest);
1363 }
1364 }
1365 #endif
1366
1367 if( p->rc!=SQLITE_OK ) return 0;
1368 assert( p->iStructVersion!=0 );
1369 assert( p->pStruct!=0 );
1370 fts5StructureRef(p->pStruct);
1371 return p->pStruct;
1372 }
1373
1374 static void fts5StructureInvalidate(Fts5Index *p){
1375 if( p->pStruct ){
1376 fts5StructureRelease(p->pStruct);
1377 p->pStruct = 0;
1378 }
1379 }
1380
1381 /*
1382 ** Return the total number of segments in index structure pStruct. This
1383 ** function is only ever used as part of assert() conditions.
1384 */
1385 #ifdef SQLITE_DEBUG
1386 static int fts5StructureCountSegments(Fts5Structure *pStruct){
1387 int nSegment = 0; /* Total number of segments */
1388 if( pStruct ){
1389 int iLvl; /* Used to iterate through levels */
1390 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
1391 nSegment += pStruct->aLevel[iLvl].nSeg;
1392 }
1393 }
1394
1395 return nSegment;
1396 }
1397 #endif
1398
1399 #define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) { \
1400 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) ); \
1401 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
1402 (pBuf)->n += nBlob; \
1403 }
1404
1405 #define fts5BufferSafeAppendVarint(pBuf, iVal) { \
1406 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
1407 assert( (pBuf)->nSpace>=(pBuf)->n ); \
1408 }
1409
1410
1411 /*
1412 ** Serialize and store the "structure" record.
1413 **
1414 ** If an error occurs, leave an error code in the Fts5Index object. If an
1415 ** error has already occurred, this function is a no-op.
1416 */
1417 static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
1418 if( p->rc==SQLITE_OK ){
1419 Fts5Buffer buf; /* Buffer to serialize record into */
1420 int iLvl; /* Used to iterate through levels */
1421 int iCookie; /* Cookie value to store */
1422 int nHdr = (pStruct->nOriginCntr>0 ? (4+4+9+9+9) : (4+9+9));
1423
1424 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
1425 memset(&buf, 0, sizeof(Fts5Buffer));
1426
1427 /* Append the current configuration cookie */
1428 iCookie = p->pConfig->iCookie;
1429 if( iCookie<0 ) iCookie = 0;
1430
1431 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, nHdr) ){
1432 sqlite3Fts5Put32(buf.p, iCookie);
1433 buf.n = 4;
1434 if( pStruct->nOriginCntr>0 ){
1435 fts5BufferSafeAppendBlob(&buf, FTS5_STRUCTURE_V2, 4);
1436 }
1437 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
1438 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
1439 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
1440 }
1441
1442 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
1443 int iSeg; /* Used to iterate through segments */
1444 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
1445 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
1446 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
1447 assert( pLvl->nMerge<=pLvl->nSeg );
1448
1449 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
1450 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
1451 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iSegid);
1452 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoFirst);
1453 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoLast);
1454 if( pStruct->nOriginCntr>0 ){
1455 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin1);
1456 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin2);
1457 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nPgTombstone);
1458 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntryTombstone);
1459 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntry);
1460 }
1461 }
1462 }
1463
1464 fts5DataWrite(p, FTS5_STRUCTURE_ROWID, buf.p, buf.n);
1465 fts5BufferFree(&buf);
1466 }
1467 }
1468
1469 #if 0
1470 static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
1471 static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
1472 int rc = SQLITE_OK;
1473 Fts5Buffer buf;
1474 memset(&buf, 0, sizeof(buf));
1475 fts5DebugStructure(&rc, &buf, pStruct);
1476 fprintf(stdout, "%s: %s\n", zCaption, buf.p);
1477 fflush(stdout);
1478 fts5BufferFree(&buf);
1479 }
1480 #else
1481 # define fts5PrintStructure(x,y)
1482 #endif
1483
1484 static int fts5SegmentSize(Fts5StructureSegment *pSeg){
1485 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
1486 }
1487
1488 /*
1489 ** Return a copy of index structure pStruct. Except, promote as many
1490 ** segments as possible to level iPromote. If an OOM occurs, NULL is
1491 ** returned.
1492 */
1493 static void fts5StructurePromoteTo(
1494 Fts5Index *p,
1495 int iPromote,
1496 int szPromote,
1497 Fts5Structure *pStruct
1498 ){
1499 int il, is;
1500 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
1501
1502 if( pOut->nMerge==0 ){
1503 for(il=iPromote+1; il<pStruct->nLevel; il++){
1504 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
1505 if( pLvl->nMerge ) return;
1506 for(is=pLvl->nSeg-1; is>=0; is--){
1507 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
1508 if( sz>szPromote ) return;
1509 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
1510 if( p->rc ) return;
1511 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
1512 pOut->nSeg++;
1513 pLvl->nSeg--;
1514 }
1515 }
1516 }
1517 }
1518
1519 /*
1520 ** A new segment has just been written to level iLvl of index structure
1521 ** pStruct. This function determines if any segments should be promoted
1522 ** as a result. Segments are promoted in two scenarios:
1523 **
1524 ** a) If the segment just written is smaller than one or more segments
1525 ** within the previous populated level, it is promoted to the previous
1526 ** populated level.
1527 **
1528 ** b) If the segment just written is larger than the newest segment on
1529 ** the next populated level, then that segment, and any other adjacent
1530 ** segments that are also smaller than the one just written, are
1531 ** promoted.
1532 **
1533 ** If one or more segments are promoted, the structure object is updated
1534 ** to reflect this.
1535 */
1536 static void fts5StructurePromote(
1537 Fts5Index *p, /* FTS5 backend object */
1538 int iLvl, /* Index level just updated */
1539 Fts5Structure *pStruct /* Index structure */
1540 ){
1541 if( p->rc==SQLITE_OK ){
1542 int iTst;
1543 int iPromote = -1;
1544 int szPromote = 0; /* Promote anything this size or smaller */
1545 Fts5StructureSegment *pSeg; /* Segment just written */
1546 int szSeg; /* Size of segment just written */
1547 int nSeg = pStruct->aLevel[iLvl].nSeg;
1548
1549 if( nSeg==0 ) return;
1550 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
1551 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
1552
1553 /* Check for condition (a) */
1554 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
1555 if( iTst>=0 ){
1556 int i;
1557 int szMax = 0;
1558 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
1559 assert( pTst->nMerge==0 );
1560 for(i=0; i<pTst->nSeg; i++){
1561 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
1562 if( sz>szMax ) szMax = sz;
1563 }
1564 if( szMax>=szSeg ){
1565 /* Condition (a) is true. Promote the newest segment on level
1566 ** iLvl to level iTst. */
1567 iPromote = iTst;
1568 szPromote = szMax;
1569 }
1570 }
1571
1572 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
1573 ** is a no-op if it is not. */
1574 if( iPromote<0 ){
1575 iPromote = iLvl;
1576 szPromote = szSeg;
1577 }
1578 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
1579 }
1580 }
1581
1582
1583 /*
1584 ** Advance the iterator passed as the only argument. If the end of the
1585 ** doclist-index page is reached, return non-zero.
1586 */
1587 static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
1588 Fts5Data *pData = pLvl->pData;
1589
1590 if( pLvl->iOff==0 ){
1591 assert( pLvl->bEof==0 );
1592 pLvl->iOff = 1;
1593 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
1594 pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
1595 pLvl->iFirstOff = pLvl->iOff;
1596 }else{
1597 int iOff;
1598 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
1599 if( pData->p[iOff] ) break;
1600 }
1601
1602 if( iOff<pData->nn ){
1603 u64 iVal;
1604 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
1605 iOff += fts5GetVarint(&pData->p[iOff], &iVal);
1606 pLvl->iRowid += iVal;
1607 pLvl->iOff = iOff;
1608 }else{
1609 pLvl->bEof = 1;
1610 }
1611 }
1612
1613 return pLvl->bEof;
1614 }
1615
1616 /*
1617 ** Advance the iterator passed as the only argument.
1618 */
1619 static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
1620 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
1621
1622 assert( iLvl<pIter->nLvl );
1623 if( fts5DlidxLvlNext(pLvl) ){
1624 if( (iLvl+1) < pIter->nLvl ){
1625 fts5DlidxIterNextR(p, pIter, iLvl+1);
1626 if( pLvl[1].bEof==0 ){
1627 fts5DataRelease(pLvl->pData);
1628 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
1629 pLvl->pData = fts5DataRead(p,
1630 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
1631 );
1632 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
1633 }
1634 }
1635 }
1636
1637 return pIter->aLvl[0].bEof;
1638 }
1639 static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
1640 return fts5DlidxIterNextR(p, pIter, 0);
1641 }
1642
1643 /*
1644 ** The iterator passed as the first argument has the following fields set
1645 ** as follows. This function sets up the rest of the iterator so that it
1646 ** points to the first rowid in the doclist-index.
1647 **
1648 ** pData:
1649 ** pointer to doclist-index record,
1650 **
1651 ** When this function is called pIter->iLeafPgno is the page number the
1652 ** doclist is associated with (the one featuring the term).
1653 */
1654 static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
1655 int i;
1656 for(i=0; i<pIter->nLvl; i++){
1657 fts5DlidxLvlNext(&pIter->aLvl[i]);
1658 }
1659 return pIter->aLvl[0].bEof;
1660 }
1661
1662
1663 static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
1664 return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
1665 }
1666
1667 static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
1668 int i;
1669
1670 /* Advance each level to the last entry on the last page */
1671 for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
1672 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
1673 while( fts5DlidxLvlNext(pLvl)==0 );
1674 pLvl->bEof = 0;
1675
1676 if( i>0 ){
1677 Fts5DlidxLvl *pChild = &pLvl[-1];
1678 fts5DataRelease(pChild->pData);
1679 memset(pChild, 0, sizeof(Fts5DlidxLvl));
1680 pChild->pData = fts5DataRead(p,
1681 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
1682 );
1683 }
1684 }
1685 }
1686
1687 /*
1688 ** Move the iterator passed as the only argument to the previous entry.
1689 */
1690 static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
1691 int iOff = pLvl->iOff;
1692
1693 assert( pLvl->bEof==0 );
1694 if( iOff<=pLvl->iFirstOff ){
1695 pLvl->bEof = 1;
1696 }else{
1697 u8 *a = pLvl->pData->p;
1698
1699 pLvl->iOff = 0;
1700 fts5DlidxLvlNext(pLvl);
1701 while( 1 ){
1702 int nZero = 0;
1703 int ii = pLvl->iOff;
1704 u64 delta = 0;
1705
1706 while( a[ii]==0 ){
1707 nZero++;
1708 ii++;
1709 }
1710 ii += sqlite3Fts5GetVarint(&a[ii], &delta);
1711
1712 if( ii>=iOff ) break;
1713 pLvl->iLeafPgno += nZero+1;
1714 pLvl->iRowid += delta;
1715 pLvl->iOff = ii;
1716 }
1717 }
1718
1719 return pLvl->bEof;
1720 }
1721
1722 static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
1723 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
1724
1725 assert( iLvl<pIter->nLvl );
1726 if( fts5DlidxLvlPrev(pLvl) ){
1727 if( (iLvl+1) < pIter->nLvl ){
1728 fts5DlidxIterPrevR(p, pIter, iLvl+1);
1729 if( pLvl[1].bEof==0 ){
1730 fts5DataRelease(pLvl->pData);
1731 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
1732 pLvl->pData = fts5DataRead(p,
1733 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
1734 );
1735 if( pLvl->pData ){
1736 while( fts5DlidxLvlNext(pLvl)==0 );
1737 pLvl->bEof = 0;
1738 }
1739 }
1740 }
1741 }
1742
1743 return pIter->aLvl[0].bEof;
1744 }
1745 static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
1746 return fts5DlidxIterPrevR(p, pIter, 0);
1747 }
1748
1749 /*
1750 ** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
1751 */
1752 static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
1753 if( pIter ){
1754 int i;
1755 for(i=0; i<pIter->nLvl; i++){
1756 fts5DataRelease(pIter->aLvl[i].pData);
1757 }
1758 sqlite3_free(pIter);
1759 }
1760 }
1761
1762 static Fts5DlidxIter *fts5DlidxIterInit(
1763 Fts5Index *p, /* Fts5 Backend to iterate within */
1764 int bRev, /* True for ORDER BY ASC */
1765 int iSegid, /* Segment id */
1766 int iLeafPg /* Leaf page number to load dlidx for */
1767 ){
1768 Fts5DlidxIter *pIter = 0;
1769 int i;
1770 int bDone = 0;
1771
1772 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
1773 sqlite3_int64 nByte = SZ_FTS5DLIDXITER(i+1);
1774 Fts5DlidxIter *pNew;
1775
1776 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
1777 if( pNew==0 ){
1778 p->rc = SQLITE_NOMEM;
1779 }else{
1780 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
1781 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
1782 pIter = pNew;
1783 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
1784 pLvl->pData = fts5DataRead(p, iRowid);
1785 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
1786 bDone = 1;
1787 }
1788 pIter->nLvl = i+1;
1789 }
1790 }
1791
1792 if( p->rc==SQLITE_OK ){
1793 pIter->iSegid = iSegid;
1794 if( bRev==0 ){
1795 fts5DlidxIterFirst(pIter);
1796 }else{
1797 fts5DlidxIterLast(p, pIter);
1798 }
1799 }
1800
1801 if( p->rc!=SQLITE_OK ){
1802 fts5DlidxIterFree(pIter);
1803 pIter = 0;
1804 }
1805
1806 return pIter;
1807 }
1808
1809 static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
1810 return pIter->aLvl[0].iRowid;
1811 }
1812 static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
1813 return pIter->aLvl[0].iLeafPgno;
1814 }
1815
1816 /*
1817 ** Load the next leaf page into the segment iterator.
1818 */
1819 static void fts5SegIterNextPage(
1820 Fts5Index *p, /* FTS5 backend object */
1821 Fts5SegIter *pIter /* Iterator to advance to next page */
1822 ){
1823 Fts5Data *pLeaf;
1824 Fts5StructureSegment *pSeg = pIter->pSeg;
1825 fts5DataRelease(pIter->pLeaf);
1826 pIter->iLeafPgno++;
1827 if( pIter->pNextLeaf ){
1828 pIter->pLeaf = pIter->pNextLeaf;
1829 pIter->pNextLeaf = 0;
1830 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
1831 pIter->pLeaf = fts5LeafRead(p,
1832 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
1833 );
1834 }else{
1835 pIter->pLeaf = 0;
1836 }
1837 pLeaf = pIter->pLeaf;
1838
1839 if( pLeaf ){
1840 pIter->iPgidxOff = pLeaf->szLeaf;
1841 if( fts5LeafIsTermless(pLeaf) ){
1842 pIter->iEndofDoclist = pLeaf->nn+1;
1843 }else{
1844 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
1845 pIter->iEndofDoclist
1846 );
1847 }
1848 }
1849 }
1850
1851 /*
1852 ** Argument p points to a buffer containing a varint to be interpreted as a
1853 ** position list size field. Read the varint and return the number of bytes
1854 ** read. Before returning, set *pnSz to the number of bytes in the position
1855 ** list, and *pbDel to true if the delete flag is set, or false otherwise.
1856 */
1857 static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
1858 int nSz;
1859 int n = 0;
1860 fts5FastGetVarint32(p, n, nSz);
1861 assert_nc( nSz>=0 );
1862 *pnSz = nSz/2;
1863 *pbDel = nSz & 0x0001;
1864 return n;
1865 }
1866
1867 /*
1868 ** Fts5SegIter.iLeafOffset currently points to the first byte of a
1869 ** position-list size field. Read the value of the field and store it
1870 ** in the following variables:
1871 **
1872 ** Fts5SegIter.nPos
1873 ** Fts5SegIter.bDel
1874 **
1875 ** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
1876 ** position list content (if any).
1877 */
1878 static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
1879 if( p->rc==SQLITE_OK ){
1880 int iOff = pIter->iLeafOffset; /* Offset to read at */
1881 ASSERT_SZLEAF_OK(pIter->pLeaf);
1882 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
1883 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
1884 pIter->bDel = 0;
1885 pIter->nPos = 1;
1886 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
1887 pIter->bDel = 1;
1888 iOff++;
1889 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
1890 pIter->nPos = 1;
1891 iOff++;
1892 }else{
1893 pIter->nPos = 0;
1894 }
1895 }
1896 }else{
1897 int nSz;
1898 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
1899 pIter->bDel = (nSz & 0x0001);
1900 pIter->nPos = nSz>>1;
1901 assert_nc( pIter->nPos>=0 );
1902 }
1903 pIter->iLeafOffset = iOff;
1904 }
1905 }
1906
1907 static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
1908 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
1909 i64 iOff = pIter->iLeafOffset;
1910
1911 ASSERT_SZLEAF_OK(pIter->pLeaf);
1912 while( iOff>=pIter->pLeaf->szLeaf ){
1913 fts5SegIterNextPage(p, pIter);
1914 if( pIter->pLeaf==0 ){
1915 if( p->rc==SQLITE_OK ) FTS5_CORRUPT_ITER(p, pIter);
1916 return;
1917 }
1918 iOff = 4;
1919 a = pIter->pLeaf->p;
1920 }
1921 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
1922 pIter->iLeafOffset = iOff;
1923 }
1924
1925 /*
1926 ** Fts5SegIter.iLeafOffset currently points to the first byte of the
1927 ** "nSuffix" field of a term. Function parameter nKeep contains the value
1928 ** of the "nPrefix" field (if there was one - it is passed 0 if this is
1929 ** the first term in the segment).
1930 **
1931 ** This function populates:
1932 **
1933 ** Fts5SegIter.term
1934 ** Fts5SegIter.rowid
1935 **
1936 ** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
1937 ** the first position list. The position list belonging to document
1938 ** (Fts5SegIter.iRowid).
1939 */
1940 static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
1941 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
1942 i64 iOff = pIter->iLeafOffset; /* Offset to read at */
1943 int nNew; /* Bytes of new data */
1944
1945 iOff += fts5GetVarint32(&a[iOff], nNew);
1946 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
1947 FTS5_CORRUPT_ITER(p, pIter);
1948 return;
1949 }
1950 pIter->term.n = nKeep;
1951 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
1952 assert( pIter->term.n<=pIter->term.nSpace );
1953 iOff += nNew;
1954 pIter->iTermLeafOffset = iOff;
1955 pIter->iTermLeafPgno = pIter->iLeafPgno;
1956 pIter->iLeafOffset = iOff;
1957
1958 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
1959 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
1960 }else{
1961 int nExtra;
1962 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
1963 pIter->iEndofDoclist += nExtra;
1964 }
1965
1966 fts5SegIterLoadRowid(p, pIter);
1967 }
1968
1969 static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
1970 static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
1971 static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
1972
1973 static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
1974 if( pIter->flags & FTS5_SEGITER_REVERSE ){
1975 pIter->xNext = fts5SegIterNext_Reverse;
1976 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
1977 pIter->xNext = fts5SegIterNext_None;
1978 }else{
1979 pIter->xNext = fts5SegIterNext;
1980 }
1981 }
1982
1983 /*
1984 ** Allocate a tombstone hash page array object (pIter->pTombArray) for
1985 ** the iterator passed as the second argument. If an OOM error occurs,
1986 ** leave an error in the Fts5Index object.
1987 */
1988 static void fts5SegIterAllocTombstone(Fts5Index *p, Fts5SegIter *pIter){
1989 const i64 nTomb = (i64)pIter->pSeg->nPgTombstone;
1990 if( nTomb>0 ){
1991 i64 nByte = SZ_FTS5TOMBSTONEARRAY(nTomb+1);
1992 Fts5TombstoneArray *pNew;
1993 pNew = (Fts5TombstoneArray*)sqlite3Fts5MallocZero(&p->rc, nByte);
1994 if( pNew ){
1995 pNew->nTombstone = nTomb;
1996 pNew->nRef = 1;
1997 pIter->pTombArray = pNew;
1998 }
1999 }
2000 }
2001
2002 /*
2003 ** Initialize the iterator object pIter to iterate through the entries in
2004 ** segment pSeg. The iterator is left pointing to the first entry when
2005 ** this function returns.
2006 **
2007 ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
2008 ** an error has already occurred when this function is called, it is a no-op.
2009 */
2010 static void fts5SegIterInit(
2011 Fts5Index *p, /* FTS index object */
2012 Fts5StructureSegment *pSeg, /* Description of segment */
2013 Fts5SegIter *pIter /* Object to populate */
2014 ){
2015 if( pSeg->pgnoFirst==0 ){
2016 /* This happens if the segment is being used as an input to an incremental
2017 ** merge and all data has already been "trimmed". See function
2018 ** fts5TrimSegments() for details. In this case leave the iterator empty.
2019 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
2020 ** at EOF already. */
2021 assert( pIter->pLeaf==0 );
2022 return;
2023 }
2024
2025 if( p->rc==SQLITE_OK ){
2026 memset(pIter, 0, sizeof(*pIter));
2027 fts5SegIterSetNext(p, pIter);
2028 pIter->pSeg = pSeg;
2029 pIter->iLeafPgno = pSeg->pgnoFirst-1;
2030 do {
2031 fts5SegIterNextPage(p, pIter);
2032 }while( p->rc==SQLITE_OK && pIter->pLeaf && pIter->pLeaf->nn==4 );
2033 }
2034
2035 if( p->rc==SQLITE_OK && pIter->pLeaf ){
2036 pIter->iLeafOffset = 4;
2037 assert( pIter->pLeaf!=0 );
2038 assert_nc( pIter->pLeaf->nn>4 );
2039 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
2040 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
2041 fts5SegIterLoadTerm(p, pIter, 0);
2042 fts5SegIterLoadNPos(p, pIter);
2043 fts5SegIterAllocTombstone(p, pIter);
2044 }
2045 }
2046
2047 /*
2048 ** This function is only ever called on iterators created by calls to
2049 ** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
2050 **
2051 ** The iterator is in an unusual state when this function is called: the
2052 ** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
2053 ** the position-list size field for the first relevant rowid on the page.
2054 ** Fts5SegIter.rowid is set, but nPos and bDel are not.
2055 **
2056 ** This function advances the iterator so that it points to the last
2057 ** relevant rowid on the page and, if necessary, initializes the
2058 ** aRowidOffset[] and iRowidOffset variables. At this point the iterator
2059 ** is in its regular state - Fts5SegIter.iLeafOffset points to the first
2060 ** byte of the position list content associated with said rowid.
2061 */
2062 static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
2063 int eDetail = p->pConfig->eDetail;
2064 int n = pIter->pLeaf->szLeaf;
2065 int i = pIter->iLeafOffset;
2066 u8 *a = pIter->pLeaf->p;
2067 int iRowidOffset = 0;
2068
2069 if( n>pIter->iEndofDoclist ){
2070 n = pIter->iEndofDoclist;
2071 }
2072
2073 ASSERT_SZLEAF_OK(pIter->pLeaf);
2074 while( 1 ){
2075 u64 iDelta = 0;
2076
2077 if( i>=n ) break;
2078 if( eDetail==FTS5_DETAIL_NONE ){
2079 /* todo */
2080 if( i<n && a[i]==0 ){
2081 i++;
2082 if( i<n && a[i]==0 ) i++;
2083 }
2084 }else{
2085 int nPos;
2086 int bDummy;
2087 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
2088 i += nPos;
2089 }
2090 if( i>=n ) break;
2091 i += fts5GetVarint(&a[i], &iDelta);
2092 pIter->iRowid += iDelta;
2093
2094 /* If necessary, grow the pIter->aRowidOffset[] array. */
2095 if( iRowidOffset>=pIter->nRowidOffset ){
2096 i64 nNew = pIter->nRowidOffset + 8;
2097 int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
2098 if( aNew==0 ){
2099 p->rc = SQLITE_NOMEM;
2100 break;
2101 }
2102 pIter->aRowidOffset = aNew;
2103 pIter->nRowidOffset = nNew;
2104 }
2105
2106 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
2107 pIter->iLeafOffset = i;
2108 }
2109 pIter->iRowidOffset = iRowidOffset;
2110 fts5SegIterLoadNPos(p, pIter);
2111 }
2112
2113 /*
2114 **
2115 */
2116 static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
2117 assert( pIter->flags & FTS5_SEGITER_REVERSE );
2118 assert( pIter->flags & FTS5_SEGITER_ONETERM );
2119
2120 fts5DataRelease(pIter->pLeaf);
2121 pIter->pLeaf = 0;
2122 while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
2123 Fts5Data *pNew;
2124 pIter->iLeafPgno--;
2125 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
2126 pIter->pSeg->iSegid, pIter->iLeafPgno
2127 ));
2128 if( pNew ){
2129 /* iTermLeafOffset may be equal to szLeaf if the term is the last
2130 ** thing on the page - i.e. the first rowid is on the following page.
2131 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
2132 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
2133 assert( pIter->pLeaf==0 );
2134 if( pIter->iTermLeafOffset<pNew->szLeaf ){
2135 pIter->pLeaf = pNew;
2136 pIter->iLeafOffset = pIter->iTermLeafOffset;
2137 }
2138 }else{
2139 int iRowidOff;
2140 iRowidOff = fts5LeafFirstRowidOff(pNew);
2141 if( iRowidOff ){
2142 if( iRowidOff>=pNew->szLeaf ){
2143 FTS5_CORRUPT_ITER(p, pIter);
2144 }else{
2145 pIter->pLeaf = pNew;
2146 pIter->iLeafOffset = iRowidOff;
2147 }
2148 }
2149 }
2150
2151 if( pIter->pLeaf ){
2152 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
2153 pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
2154 break;
2155 }else{
2156 fts5DataRelease(pNew);
2157 }
2158 }
2159 }
2160
2161 if( pIter->pLeaf ){
2162 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
2163 fts5SegIterReverseInitPage(p, pIter);
2164 }
2165 }
2166
2167 /*
2168 ** Return true if the iterator passed as the second argument currently
2169 ** points to a delete marker. A delete marker is an entry with a 0 byte
2170 ** position-list.
2171 */
2172 static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
2173 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
2174 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
2175 }
2176
2177 /*
2178 ** Advance iterator pIter to the next entry.
2179 **
2180 ** This version of fts5SegIterNext() is only used by reverse iterators.
2181 */
2182 static void fts5SegIterNext_Reverse(
2183 Fts5Index *p, /* FTS5 backend object */
2184 Fts5SegIter *pIter, /* Iterator to advance */
2185 int *pbUnused /* Unused */
2186 ){
2187 assert( pIter->flags & FTS5_SEGITER_REVERSE );
2188 assert( pIter->pNextLeaf==0 );
2189 UNUSED_PARAM(pbUnused);
2190
2191 if( pIter->iRowidOffset>0 ){
2192 u8 *a = pIter->pLeaf->p;
2193 int iOff;
2194 u64 iDelta;
2195
2196 pIter->iRowidOffset--;
2197 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
2198 fts5SegIterLoadNPos(p, pIter);
2199 iOff = pIter->iLeafOffset;
2200 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
2201 iOff += pIter->nPos;
2202 }
2203 fts5GetVarint(&a[iOff], &iDelta);
2204 pIter->iRowid -= iDelta;
2205 }else{
2206 fts5SegIterReverseNewPage(p, pIter);
2207 }
2208 }
2209
2210 /*
2211 ** Advance iterator pIter to the next entry.
2212 **
2213 ** This version of fts5SegIterNext() is only used if detail=none and the
2214 ** iterator is not a reverse direction iterator.
2215 */
2216 static void fts5SegIterNext_None(
2217 Fts5Index *p, /* FTS5 backend object */
2218 Fts5SegIter *pIter, /* Iterator to advance */
2219 int *pbNewTerm /* OUT: Set for new term */
2220 ){
2221 int iOff;
2222
2223 assert( p->rc==SQLITE_OK );
2224 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
2225 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
2226
2227 ASSERT_SZLEAF_OK(pIter->pLeaf);
2228 iOff = pIter->iLeafOffset;
2229
2230 /* Next entry is on the next page */
2231 while( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
2232 fts5SegIterNextPage(p, pIter);
2233 if( p->rc || pIter->pLeaf==0 ) return;
2234 pIter->iRowid = 0;
2235 iOff = 4;
2236 }
2237
2238 if( iOff<pIter->iEndofDoclist ){
2239 /* Next entry is on the current page */
2240 u64 iDelta;
2241 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
2242 pIter->iLeafOffset = iOff;
2243 pIter->iRowid += iDelta;
2244 }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
2245 if( pIter->pSeg ){
2246 int nKeep = 0;
2247 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
2248 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
2249 }
2250 pIter->iLeafOffset = iOff;
2251 fts5SegIterLoadTerm(p, pIter, nKeep);
2252 }else{
2253 const u8 *pList = 0;
2254 const char *zTerm = 0;
2255 int nTerm = 0;
2256 int nList;
2257 sqlite3Fts5HashScanNext(p->pHash);
2258 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList);
2259 if( pList==0 ) goto next_none_eof;
2260 pIter->pLeaf->p = (u8*)pList;
2261 pIter->pLeaf->nn = nList;
2262 pIter->pLeaf->szLeaf = nList;
2263 pIter->iEndofDoclist = nList;
2264 sqlite3Fts5BufferSet(&p->rc,&pIter->term, nTerm, (u8*)zTerm);
2265 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
2266 }
2267
2268 if( pbNewTerm ) *pbNewTerm = 1;
2269 }else{
2270 goto next_none_eof;
2271 }
2272
2273 fts5SegIterLoadNPos(p, pIter);
2274
2275 return;
2276 next_none_eof:
2277 fts5DataRelease(pIter->pLeaf);
2278 pIter->pLeaf = 0;
2279 }
2280
2281
2282 /*
2283 ** Advance iterator pIter to the next entry.
2284 **
2285 ** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
2286 ** is not considered an error if the iterator reaches EOF. If an error has
2287 ** already occurred when this function is called, it is a no-op.
2288 */
2289 static void fts5SegIterNext(
2290 Fts5Index *p, /* FTS5 backend object */
2291 Fts5SegIter *pIter, /* Iterator to advance */
2292 int *pbNewTerm /* OUT: Set for new term */
2293 ){
2294 Fts5Data *pLeaf = pIter->pLeaf;
2295 int iOff;
2296 int bNewTerm = 0;
2297 int nKeep = 0;
2298 u8 *a;
2299 int n;
2300
2301 assert( pbNewTerm==0 || *pbNewTerm==0 );
2302 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
2303
2304 /* Search for the end of the position list within the current page. */
2305 a = pLeaf->p;
2306 n = pLeaf->szLeaf;
2307
2308 ASSERT_SZLEAF_OK(pLeaf);
2309 iOff = pIter->iLeafOffset + pIter->nPos;
2310
2311 if( iOff<n ){
2312 /* The next entry is on the current page. */
2313 assert_nc( iOff<=pIter->iEndofDoclist );
2314 if( iOff>=pIter->iEndofDoclist ){
2315 bNewTerm = 1;
2316 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
2317 iOff += fts5GetVarint32(&a[iOff], nKeep);
2318 }
2319 }else{
2320 u64 iDelta;
2321 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
2322 pIter->iRowid += iDelta;
2323 assert_nc( iDelta>0 );
2324 }
2325 pIter->iLeafOffset = iOff;
2326
2327 }else if( pIter->pSeg==0 ){
2328 const u8 *pList = 0;
2329 const char *zTerm = 0;
2330 int nTerm = 0;
2331 int nList = 0;
2332 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
2333 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
2334 sqlite3Fts5HashScanNext(p->pHash);
2335 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList);
2336 }
2337 if( pList==0 ){
2338 fts5DataRelease(pIter->pLeaf);
2339 pIter->pLeaf = 0;
2340 }else{
2341 pIter->pLeaf->p = (u8*)pList;
2342 pIter->pLeaf->nn = nList;
2343 pIter->pLeaf->szLeaf = nList;
2344 pIter->iEndofDoclist = nList+1;
2345 sqlite3Fts5BufferSet(&p->rc, &pIter->term, nTerm, (u8*)zTerm);
2346 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
2347 *pbNewTerm = 1;
2348 }
2349 }else{
2350 iOff = 0;
2351 /* Next entry is not on the current page */
2352 while( iOff==0 ){
2353 fts5SegIterNextPage(p, pIter);
2354 pLeaf = pIter->pLeaf;
2355 if( pLeaf==0 ) break;
2356 ASSERT_SZLEAF_OK(pLeaf);
2357 if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
2358 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
2359 pIter->iLeafOffset = iOff;
2360
2361 if( pLeaf->nn>pLeaf->szLeaf ){
2362 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
2363 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
2364 );
2365 }
2366 }
2367 else if( pLeaf->nn>pLeaf->szLeaf ){
2368 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
2369 &pLeaf->p[pLeaf->szLeaf], iOff
2370 );
2371 pIter->iLeafOffset = iOff;
2372 pIter->iEndofDoclist = iOff;
2373 bNewTerm = 1;
2374 }
2375 assert_nc( iOff<pLeaf->szLeaf );
2376 if( iOff>pLeaf->szLeaf ){
2377 FTS5_CORRUPT_ITER(p, pIter);
2378 return;
2379 }
2380 }
2381 }
2382
2383 /* Check if the iterator is now at EOF. If so, return early. */
2384 if( pIter->pLeaf ){
2385 if( bNewTerm ){
2386 if( pIter->flags & FTS5_SEGITER_ONETERM ){
2387 fts5DataRelease(pIter->pLeaf);
2388 pIter->pLeaf = 0;
2389 }else{
2390 fts5SegIterLoadTerm(p, pIter, nKeep);
2391 fts5SegIterLoadNPos(p, pIter);
2392 if( pbNewTerm ) *pbNewTerm = 1;
2393 }
2394 }else{
2395 /* The following could be done by calling fts5SegIterLoadNPos(). But
2396 ** this block is particularly performance critical, so equivalent
2397 ** code is inlined. */
2398 int nSz;
2399 assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn );
2400 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
2401 pIter->bDel = (nSz & 0x0001);
2402 pIter->nPos = nSz>>1;
2403 assert_nc( pIter->nPos>=0 );
2404 }
2405 }
2406 }
2407
2408 #define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
2409
2410 #define fts5IndexSkipVarint(a, iOff) { \
2411 int iEnd = iOff+9; \
2412 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
2413 }
2414
2415 /*
2416 ** Iterator pIter currently points to the first rowid in a doclist. This
2417 ** function sets the iterator up so that iterates in reverse order through
2418 ** the doclist.
2419 */
2420 static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
2421 Fts5DlidxIter *pDlidx = pIter->pDlidx;
2422 Fts5Data *pLast = 0;
2423 int pgnoLast = 0;
2424
2425 if( pDlidx && p->pConfig->iVersion==FTS5_CURRENT_VERSION ){
2426 int iSegid = pIter->pSeg->iSegid;
2427 pgnoLast = fts5DlidxIterPgno(pDlidx);
2428 pLast = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
2429 }else{
2430 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
2431
2432 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
2433 ** position-list content for the current rowid. Back it up so that it
2434 ** points to the start of the position-list size field. */
2435 int iPoslist;
2436 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
2437 iPoslist = pIter->iTermLeafOffset;
2438 }else{
2439 iPoslist = 4;
2440 }
2441 fts5IndexSkipVarint(pLeaf->p, iPoslist);
2442 pIter->iLeafOffset = iPoslist;
2443
2444 /* If this condition is true then the largest rowid for the current
2445 ** term may not be stored on the current page. So search forward to
2446 ** see where said rowid really is. */
2447 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
2448 int pgno;
2449 Fts5StructureSegment *pSeg = pIter->pSeg;
2450
2451 /* The last rowid in the doclist may not be on the current page. Search
2452 ** forward to find the page containing the last rowid. */
2453 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
2454 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
2455 Fts5Data *pNew = fts5LeafRead(p, iAbs);
2456 if( pNew ){
2457 int iRowid, bTermless;
2458 iRowid = fts5LeafFirstRowidOff(pNew);
2459 bTermless = fts5LeafIsTermless(pNew);
2460 if( iRowid ){
2461 SWAPVAL(Fts5Data*, pNew, pLast);
2462 pgnoLast = pgno;
2463 }
2464 fts5DataRelease(pNew);
2465 if( bTermless==0 ) break;
2466 }
2467 }
2468 }
2469 }
2470
2471 /* If pLast is NULL at this point, then the last rowid for this doclist
2472 ** lies on the page currently indicated by the iterator. In this case
2473 ** pIter->iLeafOffset is already set to point to the position-list size
2474 ** field associated with the first relevant rowid on the page.
2475 **
2476 ** Or, if pLast is non-NULL, then it is the page that contains the last
2477 ** rowid. In this case configure the iterator so that it points to the
2478 ** first rowid on this page.
2479 */
2480 if( pLast ){
2481 int iOff;
2482 fts5DataRelease(pIter->pLeaf);
2483 pIter->pLeaf = pLast;
2484 pIter->iLeafPgno = pgnoLast;
2485 if( p->rc==SQLITE_OK ){
2486 iOff = fts5LeafFirstRowidOff(pLast);
2487 if( iOff>pLast->szLeaf ){
2488 FTS5_CORRUPT_ITER(p, pIter);
2489 return;
2490 }
2491 iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
2492 pIter->iLeafOffset = iOff;
2493
2494 if( fts5LeafIsTermless(pLast) ){
2495 pIter->iEndofDoclist = pLast->nn+1;
2496 }else{
2497 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
2498 }
2499 }
2500 }
2501
2502 fts5SegIterReverseInitPage(p, pIter);
2503 }
2504
2505 /*
2506 ** Iterator pIter currently points to the first rowid of a doclist.
2507 ** There is a doclist-index associated with the final term on the current
2508 ** page. If the current term is the last term on the page, load the
2509 ** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
2510 */
2511 static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
2512 int iSeg = pIter->pSeg->iSegid;
2513 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
2514 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
2515
2516 assert( pIter->flags & FTS5_SEGITER_ONETERM );
2517 assert( pIter->pDlidx==0 );
2518
2519 /* Check if the current doclist ends on this page. If it does, return
2520 ** early without loading the doclist-index (as it belongs to a different
2521 ** term. */
2522 if( pIter->iTermLeafPgno==pIter->iLeafPgno
2523 && pIter->iEndofDoclist<pLeaf->szLeaf
2524 ){
2525 return;
2526 }
2527
2528 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
2529 }
2530
2531 /*
2532 ** The iterator object passed as the second argument currently contains
2533 ** no valid values except for the Fts5SegIter.pLeaf member variable. This
2534 ** function searches the leaf page for a term matching (pTerm/nTerm).
2535 **
2536 ** If the specified term is found on the page, then the iterator is left
2537 ** pointing to it. If argument bGe is zero and the term is not found,
2538 ** the iterator is left pointing at EOF.
2539 **
2540 ** If bGe is non-zero and the specified term is not found, then the
2541 ** iterator is left pointing to the smallest term in the segment that
2542 ** is larger than the specified term, even if this term is not on the
2543 ** current page.
2544 */
2545 static void fts5LeafSeek(
2546 Fts5Index *p, /* Leave any error code here */
2547 int bGe, /* True for a >= search */
2548 Fts5SegIter *pIter, /* Iterator to seek */
2549 const u8 *pTerm, int nTerm /* Term to search for */
2550 ){
2551 u32 iOff;
2552 const u8 *a = pIter->pLeaf->p;
2553 u32 n = (u32)pIter->pLeaf->nn;
2554
2555 u32 nMatch = 0;
2556 u32 nKeep = 0;
2557 u32 nNew = 0;
2558 u32 iTermOff;
2559 u32 iPgidx; /* Current offset in pgidx */
2560 int bEndOfPage = 0;
2561
2562 assert( p->rc==SQLITE_OK );
2563
2564 iPgidx = (u32)pIter->pLeaf->szLeaf;
2565 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
2566 iOff = iTermOff;
2567 if( iOff>n ){
2568 FTS5_CORRUPT_ITER(p, pIter);
2569 return;
2570 }
2571
2572 while( 1 ){
2573
2574 /* Figure out how many new bytes are in this term */
2575 fts5FastGetVarint32(a, iOff, nNew);
2576 if( nKeep<nMatch ){
2577 goto search_failed;
2578 }
2579 if( (iOff+nNew)>n ){
2580 FTS5_CORRUPT_ITER(p, pIter);
2581 return;
2582 }
2583
2584 assert( nKeep>=nMatch );
2585 if( nKeep==nMatch ){
2586 u32 nCmp;
2587 u32 i;
2588 nCmp = (u32)MIN(nNew, nTerm-nMatch);
2589 for(i=0; i<nCmp; i++){
2590 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
2591 }
2592 nMatch += i;
2593
2594 if( (u32)nTerm==nMatch ){
2595 if( i==nNew ){
2596 goto search_success;
2597 }else{
2598 goto search_failed;
2599 }
2600 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
2601 goto search_failed;
2602 }
2603 }
2604
2605 if( iPgidx>=n ){
2606 bEndOfPage = 1;
2607 break;
2608 }
2609
2610 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
2611 iTermOff += nKeep;
2612 iOff = iTermOff;
2613
2614 if( iOff>=n ){
2615 FTS5_CORRUPT_ITER(p, pIter);
2616 return;
2617 }
2618
2619 /* Read the nKeep field of the next term. */
2620 fts5FastGetVarint32(a, iOff, nKeep);
2621 }
2622
2623 search_failed:
2624 if( bGe==0 ){
2625 fts5DataRelease(pIter->pLeaf);
2626 pIter->pLeaf = 0;
2627 return;
2628 }else if( bEndOfPage ){
2629 do {
2630 fts5SegIterNextPage(p, pIter);
2631 if( pIter->pLeaf==0 ) return;
2632 a = pIter->pLeaf->p;
2633 if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
2634 iPgidx = (u32)pIter->pLeaf->szLeaf;
2635 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
2636 if( iOff<4 || (i64)iOff>=pIter->pLeaf->szLeaf ){
2637 FTS5_CORRUPT_ITER(p, pIter);
2638 return;
2639 }else{
2640 nKeep = 0;
2641 iTermOff = iOff;
2642 n = (u32)pIter->pLeaf->nn;
2643 iOff += fts5GetVarint32(&a[iOff], nNew);
2644 break;
2645 }
2646 }
2647 }while( 1 );
2648 }
2649
2650 search_success:
2651 if( (i64)iOff+nNew>n || nNew<1 ){
2652 FTS5_CORRUPT_ITER(p, pIter);
2653 return;
2654 }
2655 pIter->iLeafOffset = iOff + nNew;
2656 pIter->iTermLeafOffset = pIter->iLeafOffset;
2657 pIter->iTermLeafPgno = pIter->iLeafPgno;
2658
2659 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
2660 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
2661
2662 if( iPgidx>=n ){
2663 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
2664 }else{
2665 int nExtra;
2666 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
2667 pIter->iEndofDoclist = iTermOff + nExtra;
2668 }
2669 pIter->iPgidxOff = iPgidx;
2670
2671 fts5SegIterLoadRowid(p, pIter);
2672 fts5SegIterLoadNPos(p, pIter);
2673 }
2674
2675 static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
2676 if( p->pIdxSelect==0 ){
2677 Fts5Config *pConfig = p->pConfig;
2678 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
2679 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
2680 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
2681 pConfig->zDb, pConfig->zName
2682 ));
2683 }
2684 return p->pIdxSelect;
2685 }
2686
2687 /*
2688 ** Initialize the object pIter to point to term pTerm/nTerm within segment
2689 ** pSeg. If there is no such term in the index, the iterator is set to EOF.
2690 **
2691 ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
2692 ** an error has already occurred when this function is called, it is a no-op.
2693 */
2694 static void fts5SegIterSeekInit(
2695 Fts5Index *p, /* FTS5 backend */
2696 const u8 *pTerm, int nTerm, /* Term to seek to */
2697 int flags, /* Mask of FTS5INDEX_XXX flags */
2698 Fts5StructureSegment *pSeg, /* Description of segment */
2699 Fts5SegIter *pIter /* Object to populate */
2700 ){
2701 int iPg = 1;
2702 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
2703 int bDlidx = 0; /* True if there is a doclist-index */
2704 sqlite3_stmt *pIdxSelect = 0;
2705
2706 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
2707 assert( pTerm && nTerm );
2708 memset(pIter, 0, sizeof(*pIter));
2709 pIter->pSeg = pSeg;
2710
2711 /* This block sets stack variable iPg to the leaf page number that may
2712 ** contain term (pTerm/nTerm), if it is present in the segment. */
2713 pIdxSelect = fts5IdxSelectStmt(p);
2714 if( p->rc ) return;
2715 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
2716 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
2717 if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
2718 i64 val = sqlite3_column_int(pIdxSelect, 0);
2719 iPg = (int)(val>>1);
2720 bDlidx = (val & 0x0001);
2721 }
2722 p->rc = sqlite3_reset(pIdxSelect);
2723 sqlite3_bind_null(pIdxSelect, 2);
2724
2725 if( iPg<pSeg->pgnoFirst ){
2726 iPg = pSeg->pgnoFirst;
2727 bDlidx = 0;
2728 }
2729
2730 pIter->iLeafPgno = iPg - 1;
2731 fts5SegIterNextPage(p, pIter);
2732
2733 if( pIter->pLeaf ){
2734 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
2735 }
2736
2737 if( p->rc==SQLITE_OK && (bGe==0 || (flags & FTS5INDEX_QUERY_SCANONETERM)) ){
2738 pIter->flags |= FTS5_SEGITER_ONETERM;
2739 if( pIter->pLeaf ){
2740 if( flags & FTS5INDEX_QUERY_DESC ){
2741 pIter->flags |= FTS5_SEGITER_REVERSE;
2742 }
2743 if( bDlidx ){
2744 fts5SegIterLoadDlidx(p, pIter);
2745 }
2746 if( flags & FTS5INDEX_QUERY_DESC ){
2747 fts5SegIterReverse(p, pIter);
2748 }
2749 }
2750 }
2751
2752 fts5SegIterSetNext(p, pIter);
2753 if( 0==(flags & FTS5INDEX_QUERY_SCANONETERM) ){
2754 fts5SegIterAllocTombstone(p, pIter);
2755 }
2756
2757 /* Either:
2758 **
2759 ** 1) an error has occurred, or
2760 ** 2) the iterator points to EOF, or
2761 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
2762 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
2763 ** to an entry with a term greater than or equal to (pTerm/nTerm).
2764 */
2765 assert_nc( p->rc!=SQLITE_OK /* 1 */
2766 || pIter->pLeaf==0 /* 2 */
2767 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */
2768 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */
2769 );
2770 }
2771
2772
2773 /*
2774 ** SQL used by fts5SegIterNextInit() to find the page to open.
2775 */
2776 static sqlite3_stmt *fts5IdxNextStmt(Fts5Index *p){
2777 if( p->pIdxNextSelect==0 ){
2778 Fts5Config *pConfig = p->pConfig;
2779 fts5IndexPrepareStmt(p, &p->pIdxNextSelect, sqlite3_mprintf(
2780 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
2781 "segid=? AND term>? ORDER BY term ASC LIMIT 1",
2782 pConfig->zDb, pConfig->zName
2783 ));
2784
2785 }
2786 return p->pIdxNextSelect;
2787 }
2788
2789 /*
2790 ** This is similar to fts5SegIterSeekInit(), except that it initializes
2791 ** the segment iterator to point to the first term following the page
2792 ** with pToken/nToken on it.
2793 */
2794 static void fts5SegIterNextInit(
2795 Fts5Index *p,
2796 const char *pTerm, int nTerm,
2797 Fts5StructureSegment *pSeg, /* Description of segment */
2798 Fts5SegIter *pIter /* Object to populate */
2799 ){
2800 int iPg = -1; /* Page of segment to open */
2801 int bDlidx = 0;
2802 sqlite3_stmt *pSel = 0; /* SELECT to find iPg */
2803
2804 pSel = fts5IdxNextStmt(p);
2805 if( pSel ){
2806 assert( p->rc==SQLITE_OK );
2807 sqlite3_bind_int(pSel, 1, pSeg->iSegid);
2808 sqlite3_bind_blob(pSel, 2, pTerm, nTerm, SQLITE_STATIC);
2809
2810 if( sqlite3_step(pSel)==SQLITE_ROW ){
2811 i64 val = sqlite3_column_int64(pSel, 0);
2812 iPg = (int)(val>>1);
2813 bDlidx = (val & 0x0001);
2814 }
2815 p->rc = sqlite3_reset(pSel);
2816 sqlite3_bind_null(pSel, 2);
2817 if( p->rc ) return;
2818 }
2819
2820 memset(pIter, 0, sizeof(*pIter));
2821 pIter->pSeg = pSeg;
2822 pIter->flags |= FTS5_SEGITER_ONETERM;
2823 if( iPg>=0 ){
2824 pIter->iLeafPgno = iPg - 1;
2825 fts5SegIterNextPage(p, pIter);
2826 fts5SegIterSetNext(p, pIter);
2827 }
2828 if( pIter->pLeaf ){
2829 const u8 *a = pIter->pLeaf->p;
2830 int iTermOff = 0;
2831
2832 pIter->iPgidxOff = pIter->pLeaf->szLeaf;
2833 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], iTermOff);
2834 pIter->iLeafOffset = iTermOff;
2835 fts5SegIterLoadTerm(p, pIter, 0);
2836 fts5SegIterLoadNPos(p, pIter);
2837 if( bDlidx ) fts5SegIterLoadDlidx(p, pIter);
2838
2839 assert( p->rc!=SQLITE_OK ||
2840 fts5BufferCompareBlob(&pIter->term, (const u8*)pTerm, nTerm)>0
2841 );
2842 }
2843 }
2844
2845 /*
2846 ** Initialize the object pIter to point to term pTerm/nTerm within the
2847 ** in-memory hash table. If there is no such term in the hash-table, the
2848 ** iterator is set to EOF.
2849 **
2850 ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
2851 ** an error has already occurred when this function is called, it is a no-op.
2852 */
2853 static void fts5SegIterHashInit(
2854 Fts5Index *p, /* FTS5 backend */
2855 const u8 *pTerm, int nTerm, /* Term to seek to */
2856 int flags, /* Mask of FTS5INDEX_XXX flags */
2857 Fts5SegIter *pIter /* Object to populate */
2858 ){
2859 int nList = 0;
2860 const u8 *z = 0;
2861 int n = 0;
2862 Fts5Data *pLeaf = 0;
2863
2864 assert( p->pHash );
2865 assert( p->rc==SQLITE_OK );
2866
2867 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
2868 const u8 *pList = 0;
2869
2870 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
2871 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &n, &pList, &nList);
2872 if( pList ){
2873 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
2874 if( pLeaf ){
2875 pLeaf->p = (u8*)pList;
2876 }
2877 }
2878
2879 /* The call to sqlite3Fts5HashScanInit() causes the hash table to
2880 ** fill the size field of all existing position lists. This means they
2881 ** can no longer be appended to. Since the only scenario in which they
2882 ** can be appended to is if the previous operation on this table was
2883 ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
2884 ** possibility altogether. */
2885 p->bDelete = 0;
2886 }else{
2887 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
2888 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
2889 );
2890 if( pLeaf ){
2891 pLeaf->p = (u8*)&pLeaf[1];
2892 }
2893 z = pTerm;
2894 n = nTerm;
2895 pIter->flags |= FTS5_SEGITER_ONETERM;
2896 }
2897
2898 if( pLeaf ){
2899 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
2900 pLeaf->nn = pLeaf->szLeaf = nList;
2901 pIter->pLeaf = pLeaf;
2902 pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
2903 pIter->iEndofDoclist = pLeaf->nn;
2904
2905 if( flags & FTS5INDEX_QUERY_DESC ){
2906 pIter->flags |= FTS5_SEGITER_REVERSE;
2907 fts5SegIterReverseInitPage(p, pIter);
2908 }else{
2909 fts5SegIterLoadNPos(p, pIter);
2910 }
2911 }
2912
2913 fts5SegIterSetNext(p, pIter);
2914 }
2915
2916 /*
2917 ** Array ap[] contains n elements. Release each of these elements using
2918 ** fts5DataRelease(). Then free the array itself using sqlite3_free().
2919 */
2920 static void fts5IndexFreeArray(Fts5Data **ap, int n){
2921 if( ap ){
2922 int ii;
2923 for(ii=0; ii<n; ii++){
2924 fts5DataRelease(ap[ii]);
2925 }
2926 sqlite3_free(ap);
2927 }
2928 }
2929
2930 /*
2931 ** Decrement the ref-count of the object passed as the only argument. If it
2932 ** reaches 0, free it and its contents.
2933 */
2934 static void fts5TombstoneArrayDelete(Fts5TombstoneArray *p){
2935 if( p ){
2936 p->nRef--;
2937 if( p->nRef<=0 ){
2938 int ii;
2939 for(ii=0; ii<p->nTombstone; ii++){
2940 fts5DataRelease(p->apTombstone[ii]);
2941 }
2942 sqlite3_free(p);
2943 }
2944 }
2945 }
2946
2947 /*
2948 ** Zero the iterator passed as the only argument.
2949 */
2950 static void fts5SegIterClear(Fts5SegIter *pIter){
2951 fts5BufferFree(&pIter->term);
2952 fts5DataRelease(pIter->pLeaf);
2953 fts5DataRelease(pIter->pNextLeaf);
2954 fts5TombstoneArrayDelete(pIter->pTombArray);
2955 fts5DlidxIterFree(pIter->pDlidx);
2956 sqlite3_free(pIter->aRowidOffset);
2957 memset(pIter, 0, sizeof(Fts5SegIter));
2958 }
2959
2960 #ifdef SQLITE_DEBUG
2961
2962 /*
2963 ** This function is used as part of the big assert() procedure implemented by
2964 ** fts5AssertMultiIterSetup(). It ensures that the result currently stored
2965 ** in *pRes is the correct result of comparing the current positions of the
2966 ** two iterators.
2967 */
2968 static void fts5AssertComparisonResult(
2969 Fts5Iter *pIter,
2970 Fts5SegIter *p1,
2971 Fts5SegIter *p2,
2972 Fts5CResult *pRes
2973 ){
2974 int i1 = p1 - pIter->aSeg;
2975 int i2 = p2 - pIter->aSeg;
2976
2977 if( p1->pLeaf || p2->pLeaf ){
2978 if( p1->pLeaf==0 ){
2979 assert( pRes->iFirst==i2 );
2980 }else if( p2->pLeaf==0 ){
2981 assert( pRes->iFirst==i1 );
2982 }else{
2983 int nMin = MIN(p1->term.n, p2->term.n);
2984 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin);
2985 if( res==0 ) res = p1->term.n - p2->term.n;
2986
2987 if( res==0 ){
2988 assert( pRes->bTermEq==1 );
2989 assert( p1->iRowid!=p2->iRowid );
2990 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
2991 }else{
2992 assert( pRes->bTermEq==0 );
2993 }
2994
2995 if( res<0 ){
2996 assert( pRes->iFirst==i1 );
2997 }else{
2998 assert( pRes->iFirst==i2 );
2999 }
3000 }
3001 }
3002 }
3003
3004 /*
3005 ** This function is a no-op unless SQLITE_DEBUG is defined when this module
3006 ** is compiled. In that case, this function is essentially an assert()
3007 ** statement used to verify that the contents of the pIter->aFirst[] array
3008 ** are correct.
3009 */
3010 static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
3011 if( p->rc==SQLITE_OK ){
3012 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
3013 int i;
3014
3015 assert( (pFirst->pLeaf==0)==pIter->base.bEof );
3016
3017 /* Check that pIter->iSwitchRowid is set correctly. */
3018 for(i=0; i<pIter->nSeg; i++){
3019 Fts5SegIter *p1 = &pIter->aSeg[i];
3020 assert( p1==pFirst
3021 || p1->pLeaf==0
3022 || fts5BufferCompare(&pFirst->term, &p1->term)
3023 || p1->iRowid==pIter->iSwitchRowid
3024 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
3025 );
3026 }
3027
3028 for(i=0; i<pIter->nSeg; i+=2){
3029 Fts5SegIter *p1 = &pIter->aSeg[i];
3030 Fts5SegIter *p2 = &pIter->aSeg[i+1];
3031 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
3032 fts5AssertComparisonResult(pIter, p1, p2, pRes);
3033 }
3034
3035 for(i=1; i<(pIter->nSeg / 2); i+=2){
3036 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
3037 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
3038 Fts5CResult *pRes = &pIter->aFirst[i];
3039 fts5AssertComparisonResult(pIter, p1, p2, pRes);
3040 }
3041 }
3042 }
3043 #else
3044 # define fts5AssertMultiIterSetup(x,y)
3045 #endif
3046
3047 /*
3048 ** Do the comparison necessary to populate pIter->aFirst[iOut].
3049 **
3050 ** If the returned value is non-zero, then it is the index of an entry
3051 ** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
3052 ** to a key that is a duplicate of another, higher priority,
3053 ** segment-iterator in the pSeg->aSeg[] array.
3054 */
3055 static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
3056 int i1; /* Index of left-hand Fts5SegIter */
3057 int i2; /* Index of right-hand Fts5SegIter */
3058 int iRes;
3059 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
3060 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
3061 Fts5CResult *pRes = &pIter->aFirst[iOut];
3062
3063 assert( iOut<pIter->nSeg && iOut>0 );
3064 assert( pIter->bRev==0 || pIter->bRev==1 );
3065
3066 if( iOut>=(pIter->nSeg/2) ){
3067 i1 = (iOut - pIter->nSeg/2) * 2;
3068 i2 = i1 + 1;
3069 }else{
3070 i1 = pIter->aFirst[iOut*2].iFirst;
3071 i2 = pIter->aFirst[iOut*2+1].iFirst;
3072 }
3073 p1 = &pIter->aSeg[i1];
3074 p2 = &pIter->aSeg[i2];
3075
3076 pRes->bTermEq = 0;
3077 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
3078 iRes = i2;
3079 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
3080 iRes = i1;
3081 }else{
3082 int res = fts5BufferCompare(&p1->term, &p2->term);
3083 if( res==0 ){
3084 assert_nc( i2>i1 );
3085 assert_nc( i2!=0 );
3086 pRes->bTermEq = 1;
3087 if( p1->iRowid==p2->iRowid ){
3088 return i2;
3089 }
3090 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
3091 }
3092 assert( res!=0 );
3093 if( res<0 ){
3094 iRes = i1;
3095 }else{
3096 iRes = i2;
3097 }
3098 }
3099
3100 pRes->iFirst = (u16)iRes;
3101 return 0;
3102 }
3103
3104 /*
3105 ** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
3106 ** It is an error if leaf iLeafPgno does not exist. Unless the db is
3107 ** a 'secure-delete' db, if it contains no rowids then this is also an error.
3108 */
3109 static void fts5SegIterGotoPage(
3110 Fts5Index *p, /* FTS5 backend object */
3111 Fts5SegIter *pIter, /* Iterator to advance */
3112 int iLeafPgno
3113 ){
3114 assert( iLeafPgno>pIter->iLeafPgno );
3115
3116 if( iLeafPgno>pIter->pSeg->pgnoLast ){
3117 FTS5_CORRUPT_IDX(p);
3118 }else{
3119 fts5DataRelease(pIter->pNextLeaf);
3120 pIter->pNextLeaf = 0;
3121 pIter->iLeafPgno = iLeafPgno-1;
3122
3123 while( p->rc==SQLITE_OK ){
3124 int iOff;
3125 fts5SegIterNextPage(p, pIter);
3126 if( pIter->pLeaf==0 ) break;
3127 iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
3128 if( iOff>0 ){
3129 u8 *a = pIter->pLeaf->p;
3130 int n = pIter->pLeaf->szLeaf;
3131 if( iOff<4 || iOff>=n ){
3132 FTS5_CORRUPT_IDX(p);
3133 }else{
3134 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
3135 pIter->iLeafOffset = iOff;
3136 fts5SegIterLoadNPos(p, pIter);
3137 }
3138 break;
3139 }
3140 }
3141 }
3142 }
3143
3144 /*
3145 ** Advance the iterator passed as the second argument until it is at or
3146 ** past rowid iFrom. Regardless of the value of iFrom, the iterator is
3147 ** always advanced at least once.
3148 */
3149 static void fts5SegIterNextFrom(
3150 Fts5Index *p, /* FTS5 backend object */
3151 Fts5SegIter *pIter, /* Iterator to advance */
3152 i64 iMatch /* Advance iterator at least this far */
3153 ){
3154 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
3155 Fts5DlidxIter *pDlidx = pIter->pDlidx;
3156 int iLeafPgno = pIter->iLeafPgno;
3157 int bMove = 1;
3158
3159 assert( pIter->flags & FTS5_SEGITER_ONETERM );
3160 assert( pIter->pDlidx );
3161 assert( pIter->pLeaf );
3162
3163 if( bRev==0 ){
3164 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
3165 iLeafPgno = fts5DlidxIterPgno(pDlidx);
3166 fts5DlidxIterNext(p, pDlidx);
3167 }
3168 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
3169 if( iLeafPgno>pIter->iLeafPgno ){
3170 fts5SegIterGotoPage(p, pIter, iLeafPgno);
3171 bMove = 0;
3172 }
3173 }else{
3174 assert( pIter->pNextLeaf==0 );
3175 assert( iMatch<pIter->iRowid );
3176 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
3177 fts5DlidxIterPrev(p, pDlidx);
3178 }
3179 iLeafPgno = fts5DlidxIterPgno(pDlidx);
3180
3181 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
3182
3183 if( iLeafPgno<pIter->iLeafPgno ){
3184 pIter->iLeafPgno = iLeafPgno+1;
3185 fts5SegIterReverseNewPage(p, pIter);
3186 bMove = 0;
3187 }
3188 }
3189
3190 do{
3191 if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
3192 if( pIter->pLeaf==0 ) break;
3193 if( bRev==0 && pIter->iRowid>=iMatch ) break;
3194 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
3195 bMove = 1;
3196 }while( p->rc==SQLITE_OK );
3197 }
3198
3199 /*
3200 ** Free the iterator object passed as the second argument.
3201 */
3202 static void fts5MultiIterFree(Fts5Iter *pIter){
3203 if( pIter ){
3204 int i;
3205 for(i=0; i<pIter->nSeg; i++){
3206 fts5SegIterClear(&pIter->aSeg[i]);
3207 }
3208 fts5BufferFree(&pIter->poslist);
3209 sqlite3_free(pIter);
3210 }
3211 }
3212
3213 static void fts5MultiIterAdvanced(
3214 Fts5Index *p, /* FTS5 backend to iterate within */
3215 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
3216 int iChanged, /* Index of sub-iterator just advanced */
3217 int iMinset /* Minimum entry in aFirst[] to set */
3218 ){
3219 int i;
3220 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
3221 int iEq;
3222 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
3223 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
3224 assert( p->rc==SQLITE_OK );
3225 pSeg->xNext(p, pSeg, 0);
3226 i = pIter->nSeg + iEq;
3227 }
3228 }
3229 }
3230
3231 /*
3232 ** Sub-iterator iChanged of iterator pIter has just been advanced. It still
3233 ** points to the same term though - just a different rowid. This function
3234 ** attempts to update the contents of the pIter->aFirst[] accordingly.
3235 ** If it does so successfully, 0 is returned. Otherwise 1.
3236 **
3237 ** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
3238 ** on the iterator instead. That function does the same as this one, except
3239 ** that it deals with more complicated cases as well.
3240 */
3241 static int fts5MultiIterAdvanceRowid(
3242 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
3243 int iChanged, /* Index of sub-iterator just advanced */
3244 Fts5SegIter **ppFirst
3245 ){
3246 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
3247
3248 if( pNew->iRowid==pIter->iSwitchRowid
3249 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
3250 ){
3251 int i;
3252 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
3253 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
3254 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
3255 Fts5CResult *pRes = &pIter->aFirst[i];
3256
3257 assert( pNew->pLeaf );
3258 assert( pRes->bTermEq==0 || pOther->pLeaf );
3259
3260 if( pRes->bTermEq ){
3261 if( pNew->iRowid==pOther->iRowid ){
3262 return 1;
3263 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
3264 pIter->iSwitchRowid = pOther->iRowid;
3265 pNew = pOther;
3266 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
3267 pIter->iSwitchRowid = pOther->iRowid;
3268 }
3269 }
3270 pRes->iFirst = (u16)(pNew - pIter->aSeg);
3271 if( i==1 ) break;
3272
3273 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
3274 }
3275 }
3276
3277 *ppFirst = pNew;
3278 return 0;
3279 }
3280
3281 /*
3282 ** Set the pIter->bEof variable based on the state of the sub-iterators.
3283 */
3284 static void fts5MultiIterSetEof(Fts5Iter *pIter){
3285 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
3286 pIter->base.bEof = pSeg->pLeaf==0;
3287 pIter->iSwitchRowid = pSeg->iRowid;
3288 }
3289
3290 /*
3291 ** The argument to this macro must be an Fts5Data structure containing a
3292 ** tombstone hash page. This macro returns the key-size of the hash-page.
3293 */
3294 #define TOMBSTONE_KEYSIZE(pPg) (pPg->p[0]==4 ? 4 : 8)
3295
3296 #define TOMBSTONE_NSLOT(pPg) \
3297 ((pPg->nn > 16) ? ((pPg->nn-8) / TOMBSTONE_KEYSIZE(pPg)) : 1)
3298
3299 /*
3300 ** Query a single tombstone hash table for rowid iRowid. Return true if
3301 ** it is found or false otherwise. The tombstone hash table is one of
3302 ** nHashTable tables.
3303 */
3304 static int fts5IndexTombstoneQuery(
3305 Fts5Data *pHash, /* Hash table page to query */
3306 int nHashTable, /* Number of pages attached to segment */
3307 u64 iRowid /* Rowid to query hash for */
3308 ){
3309 const int szKey = TOMBSTONE_KEYSIZE(pHash);
3310 const int nSlot = TOMBSTONE_NSLOT(pHash);
3311 int iSlot = (iRowid / nHashTable) % nSlot;
3312 int nCollide = nSlot;
3313
3314 if( iRowid==0 ){
3315 return pHash->p[1];
3316 }else if( szKey==4 ){
3317 u32 *aSlot = (u32*)&pHash->p[8];
3318 while( aSlot[iSlot] ){
3319 if( fts5GetU32((u8*)&aSlot[iSlot])==iRowid ) return 1;
3320 if( nCollide--==0 ) break;
3321 iSlot = (iSlot+1)%nSlot;
3322 }
3323 }else{
3324 u64 *aSlot = (u64*)&pHash->p[8];
3325 while( aSlot[iSlot] ){
3326 if( fts5GetU64((u8*)&aSlot[iSlot])==iRowid ) return 1;
3327 if( nCollide--==0 ) break;
3328 iSlot = (iSlot+1)%nSlot;
3329 }
3330 }
3331
3332 return 0;
3333 }
3334
3335 /*
3336 ** Return true if the iterator passed as the only argument points
3337 ** to an segment entry for which there is a tombstone. Return false
3338 ** if there is no tombstone or if the iterator is already at EOF.
3339 */
3340 static int fts5MultiIterIsDeleted(Fts5Iter *pIter){
3341 int iFirst = pIter->aFirst[1].iFirst;
3342 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
3343 Fts5TombstoneArray *pArray = pSeg->pTombArray;
3344
3345 if( pSeg->pLeaf && pArray ){
3346 /* Figure out which page the rowid might be present on. */
3347 int iPg = ((u64)pSeg->iRowid) % pArray->nTombstone;
3348 assert( iPg>=0 );
3349
3350 /* If tombstone hash page iPg has not yet been loaded from the
3351 ** database, load it now. */
3352 if( pArray->apTombstone[iPg]==0 ){
3353 pArray->apTombstone[iPg] = fts5DataRead(pIter->pIndex,
3354 FTS5_TOMBSTONE_ROWID(pSeg->pSeg->iSegid, iPg)
3355 );
3356 if( pArray->apTombstone[iPg]==0 ) return 0;
3357 }
3358
3359 return fts5IndexTombstoneQuery(
3360 pArray->apTombstone[iPg],
3361 pArray->nTombstone,
3362 pSeg->iRowid
3363 );
3364 }
3365
3366 return 0;
3367 }
3368
3369 /*
3370 ** Move the iterator to the next entry.
3371 **
3372 ** If an error occurs, an error code is left in Fts5Index.rc. It is not
3373 ** considered an error if the iterator reaches EOF, or if it is already at
3374 ** EOF when this function is called.
3375 */
3376 static void fts5MultiIterNext(
3377 Fts5Index *p,
3378 Fts5Iter *pIter,
3379 int bFrom, /* True if argument iFrom is valid */
3380 i64 iFrom /* Advance at least as far as this */
3381 ){
3382 int bUseFrom = bFrom;
3383 assert( pIter->base.bEof==0 );
3384 while( p->rc==SQLITE_OK ){
3385 int iFirst = pIter->aFirst[1].iFirst;
3386 int bNewTerm = 0;
3387 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
3388 assert( p->rc==SQLITE_OK );
3389 if( bUseFrom && pSeg->pDlidx ){
3390 fts5SegIterNextFrom(p, pSeg, iFrom);
3391 }else{
3392 pSeg->xNext(p, pSeg, &bNewTerm);
3393 }
3394
3395 if( pSeg->pLeaf==0 || bNewTerm
3396 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
3397 ){
3398 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
3399 fts5MultiIterSetEof(pIter);
3400 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
3401 if( pSeg->pLeaf==0 ) return;
3402 }
3403
3404 fts5AssertMultiIterSetup(p, pIter);
3405 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
3406 if( (pIter->bSkipEmpty==0 || pSeg->nPos)
3407 && 0==fts5MultiIterIsDeleted(pIter)
3408 ){
3409 pIter->xSetOutputs(pIter, pSeg);
3410 return;
3411 }
3412 bUseFrom = 0;
3413 }
3414 }
3415
3416 static void fts5MultiIterNext2(
3417 Fts5Index *p,
3418 Fts5Iter *pIter,
3419 int *pbNewTerm /* OUT: True if *might* be new term */
3420 ){
3421 assert( pIter->bSkipEmpty );
3422 if( p->rc==SQLITE_OK ){
3423 *pbNewTerm = 0;
3424 do{
3425 int iFirst = pIter->aFirst[1].iFirst;
3426 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
3427 int bNewTerm = 0;
3428
3429 assert( p->rc==SQLITE_OK );
3430 pSeg->xNext(p, pSeg, &bNewTerm);
3431 if( pSeg->pLeaf==0 || bNewTerm
3432 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
3433 ){
3434 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
3435 fts5MultiIterSetEof(pIter);
3436 *pbNewTerm = 1;
3437 }
3438 fts5AssertMultiIterSetup(p, pIter);
3439
3440 }while( (fts5MultiIterIsEmpty(p, pIter) || fts5MultiIterIsDeleted(pIter))
3441 && (p->rc==SQLITE_OK)
3442 );
3443 }
3444 }
3445
3446 static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
3447 UNUSED_PARAM2(pUnused1, pUnused2);
3448 }
3449
3450 static Fts5Iter *fts5MultiIterAlloc(
3451 Fts5Index *p, /* FTS5 backend to iterate within */
3452 int nSeg
3453 ){
3454 Fts5Iter *pNew;
3455 i64 nSlot; /* Power of two >= nSeg */
3456
3457 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
3458 pNew = fts5IdxMalloc(p,
3459 SZ_FTS5ITER(nSlot) + /* pNew + pNew->aSeg[] */
3460 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
3461 );
3462 if( pNew ){
3463 pNew->nSeg = nSlot;
3464 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
3465 pNew->pIndex = p;
3466 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
3467 }
3468 return pNew;
3469 }
3470
3471 static void fts5PoslistCallback(
3472 Fts5Index *pUnused,
3473 void *pContext,
3474 const u8 *pChunk, int nChunk
3475 ){
3476 UNUSED_PARAM(pUnused);
3477 assert_nc( nChunk>=0 );
3478 if( nChunk>0 ){
3479 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
3480 }
3481 }
3482
3483 typedef struct PoslistCallbackCtx PoslistCallbackCtx;
3484 struct PoslistCallbackCtx {
3485 Fts5Buffer *pBuf; /* Append to this buffer */
3486 Fts5Colset *pColset; /* Restrict matches to this column */
3487 int eState; /* See above */
3488 };
3489
3490 typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
3491 struct PoslistOffsetsCtx {
3492 Fts5Buffer *pBuf; /* Append to this buffer */
3493 Fts5Colset *pColset; /* Restrict matches to this column */
3494 int iRead;
3495 int iWrite;
3496 };
3497
3498 /*
3499 ** TODO: Make this more efficient!
3500 */
3501 static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
3502 int i;
3503 for(i=0; i<pColset->nCol; i++){
3504 if( pColset->aiCol[i]==iCol ) return 1;
3505 }
3506 return 0;
3507 }
3508
3509 static void fts5PoslistOffsetsCallback(
3510 Fts5Index *pUnused,
3511 void *pContext,
3512 const u8 *pChunk, int nChunk
3513 ){
3514 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
3515 UNUSED_PARAM(pUnused);
3516 assert_nc( nChunk>=0 );
3517 if( nChunk>0 ){
3518 int i = 0;
3519 while( i<nChunk ){
3520 int iVal;
3521 i += fts5GetVarint32(&pChunk[i], iVal);
3522 iVal += pCtx->iRead - 2;
3523 pCtx->iRead = iVal;
3524 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
3525 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
3526 pCtx->iWrite = iVal;
3527 }
3528 }
3529 }
3530 }
3531
3532 static void fts5PoslistFilterCallback(
3533 Fts5Index *pUnused,
3534 void *pContext,
3535 const u8 *pChunk, int nChunk
3536 ){
3537 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
3538 UNUSED_PARAM(pUnused);
3539 assert_nc( nChunk>=0 );
3540 if( nChunk>0 ){
3541 /* Search through to find the first varint with value 1. This is the
3542 ** start of the next columns hits. */
3543 int i = 0;
3544 int iStart = 0;
3545
3546 if( pCtx->eState==2 ){
3547 int iCol;
3548 fts5FastGetVarint32(pChunk, i, iCol);
3549 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
3550 pCtx->eState = 1;
3551 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
3552 }else{
3553 pCtx->eState = 0;
3554 }
3555 }
3556
3557 do {
3558 while( i<nChunk && pChunk[i]!=0x01 ){
3559 fts5IndexSkipVarint(pChunk, i);
3560 }
3561 if( pCtx->eState ){
3562 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
3563 }
3564 if( i<nChunk ){
3565 int iCol;
3566 iStart = i;
3567 i++;
3568 if( i>=nChunk ){
3569 pCtx->eState = 2;
3570 }else{
3571 fts5FastGetVarint32(pChunk, i, iCol);
3572 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
3573 if( pCtx->eState ){
3574 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
3575 iStart = i;
3576 }
3577 }
3578 }
3579 }while( i<nChunk );
3580 }
3581 }
3582
3583 static void fts5ChunkIterate(
3584 Fts5Index *p, /* Index object */
3585 Fts5SegIter *pSeg, /* Poslist of this iterator */
3586 void *pCtx, /* Context pointer for xChunk callback */
3587 void (*xChunk)(Fts5Index*, void*, const u8*, int)
3588 ){
3589 int nRem = pSeg->nPos; /* Number of bytes still to come */
3590 Fts5Data *pData = 0;
3591 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
3592 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
3593 int pgno = pSeg->iLeafPgno;
3594 int pgnoSave = 0;
3595
3596 /* This function does not work with detail=none databases. */
3597 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
3598
3599 if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
3600 pgnoSave = pgno+1;
3601 }
3602
3603 while( 1 ){
3604 xChunk(p, pCtx, pChunk, nChunk);
3605 nRem -= nChunk;
3606 fts5DataRelease(pData);
3607 if( nRem<=0 ){
3608 break;
3609 }else if( pSeg->pSeg==0 ){
3610 FTS5_CORRUPT_IDX(p);
3611 return;
3612 }else{
3613 pgno++;
3614 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
3615 if( pData==0 ) break;
3616 pChunk = &pData->p[4];
3617 nChunk = MIN(nRem, pData->szLeaf - 4);
3618 if( pgno==pgnoSave ){
3619 assert( pSeg->pNextLeaf==0 );
3620 pSeg->pNextLeaf = pData;
3621 pData = 0;
3622 }
3623 }
3624 }
3625 }
3626
3627 /*
3628 ** Iterator pIter currently points to a valid entry (not EOF). This
3629 ** function appends the position list data for the current entry to
3630 ** buffer pBuf. It does not make a copy of the position-list size
3631 ** field.
3632 */
3633 static void fts5SegiterPoslist(
3634 Fts5Index *p,
3635 Fts5SegIter *pSeg,
3636 Fts5Colset *pColset,
3637 Fts5Buffer *pBuf
3638 ){
3639 assert( pBuf!=0 );
3640 assert( pSeg!=0 );
3641 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){
3642 assert( pBuf->p!=0 );
3643 assert( pBuf->nSpace >= pBuf->n+pSeg->nPos+FTS5_DATA_ZERO_PADDING );
3644 memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING);
3645 if( pColset==0 ){
3646 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
3647 }else{
3648 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
3649 PoslistCallbackCtx sCtx;
3650 sCtx.pBuf = pBuf;
3651 sCtx.pColset = pColset;
3652 sCtx.eState = fts5IndexColsetTest(pColset, 0);
3653 assert( sCtx.eState==0 || sCtx.eState==1 );
3654 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
3655 }else{
3656 PoslistOffsetsCtx sCtx;
3657 memset(&sCtx, 0, sizeof(sCtx));
3658 sCtx.pBuf = pBuf;
3659 sCtx.pColset = pColset;
3660 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
3661 }
3662 }
3663 }
3664 }
3665
3666 /*
3667 ** Parameter pPos points to a buffer containing a position list, size nPos.
3668 ** This function filters it according to pColset (which must be non-NULL)
3669 ** and sets pIter->base.pData/nData to point to the new position list.
3670 ** If memory is required for the new position list, use buffer pIter->poslist.
3671 ** Or, if the new position list is a contiguous subset of the input, set
3672 ** pIter->base.pData/nData to point directly to it.
3673 **
3674 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
3675 ** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM
3676 ** before returning.
3677 */
3678 static void fts5IndexExtractColset(
3679 int *pRc,
3680 Fts5Colset *pColset, /* Colset to filter on */
3681 const u8 *pPos, int nPos, /* Position list */
3682 Fts5Iter *pIter
3683 ){
3684 if( *pRc==SQLITE_OK ){
3685 const u8 *p = pPos;
3686 const u8 *aCopy = p;
3687 const u8 *pEnd = &p[nPos]; /* One byte past end of position list */
3688 int i = 0;
3689 int iCurrent = 0;
3690
3691 if( pColset->nCol>1 && sqlite3Fts5BufferSize(pRc, &pIter->poslist, nPos) ){
3692 return;
3693 }
3694
3695 while( 1 ){
3696 while( pColset->aiCol[i]<iCurrent ){
3697 i++;
3698 if( i==pColset->nCol ){
3699 pIter->base.pData = pIter->poslist.p;
3700 pIter->base.nData = pIter->poslist.n;
3701 return;
3702 }
3703 }
3704
3705 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
3706 ** not part of a varint */
3707 while( p<pEnd && *p!=0x01 ){
3708 while( p<pEnd && (*p++ & 0x80) );
3709 }
3710
3711 if( pColset->aiCol[i]==iCurrent ){
3712 if( pColset->nCol==1 ){
3713 pIter->base.pData = aCopy;
3714 pIter->base.nData = p-aCopy;
3715 return;
3716 }
3717 fts5BufferSafeAppendBlob(&pIter->poslist, aCopy, p-aCopy);
3718 }
3719 if( p>=pEnd ){
3720 pIter->base.pData = pIter->poslist.p;
3721 pIter->base.nData = pIter->poslist.n;
3722 return;
3723 }
3724 aCopy = p++;
3725 iCurrent = *p++;
3726 if( iCurrent & 0x80 ){
3727 p--;
3728 p += fts5GetVarint32(p, iCurrent);
3729 }
3730 }
3731 }
3732
3733 }
3734
3735 /*
3736 ** xSetOutputs callback used by detail=none tables.
3737 */
3738 static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
3739 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
3740 pIter->base.iRowid = pSeg->iRowid;
3741 pIter->base.nData = pSeg->nPos;
3742 }
3743
3744 /*
3745 ** xSetOutputs callback used by detail=full and detail=col tables when no
3746 ** column filters are specified.
3747 */
3748 static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
3749 pIter->base.iRowid = pSeg->iRowid;
3750 pIter->base.nData = pSeg->nPos;
3751
3752 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
3753 assert( pIter->pColset==0 );
3754
3755 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
3756 /* All data is stored on the current page. Populate the output
3757 ** variables to point into the body of the page object. */
3758 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
3759 }else{
3760 /* The data is distributed over two or more pages. Copy it into the
3761 ** Fts5Iter.poslist buffer and then set the output pointer to point
3762 ** to this buffer. */
3763 fts5BufferZero(&pIter->poslist);
3764 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
3765 pIter->base.pData = pIter->poslist.p;
3766 }
3767 }
3768
3769 /*
3770 ** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
3771 ** against no columns at all).
3772 */
3773 static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
3774 UNUSED_PARAM(pSeg);
3775 pIter->base.nData = 0;
3776 }
3777
3778 /*
3779 ** xSetOutputs callback used by detail=col when there is a column filter
3780 ** and there are 100 or more columns. Also called as a fallback from
3781 ** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
3782 */
3783 static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
3784 fts5BufferZero(&pIter->poslist);
3785 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
3786 pIter->base.iRowid = pSeg->iRowid;
3787 pIter->base.pData = pIter->poslist.p;
3788 pIter->base.nData = pIter->poslist.n;
3789 }
3790
3791 /*
3792 ** xSetOutputs callback used when:
3793 **
3794 ** * detail=col,
3795 ** * there is a column filter, and
3796 ** * the table contains 100 or fewer columns.
3797 **
3798 ** The last point is to ensure all column numbers are stored as
3799 ** single-byte varints.
3800 */
3801 static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
3802
3803 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
3804 assert( pIter->pColset );
3805 assert( pIter->poslist.nSpace>=pIter->pIndex->pConfig->nCol );
3806
3807 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf
3808 || pSeg->nPos>pIter->pIndex->pConfig->nCol
3809 ){
3810 fts5IterSetOutputs_Col(pIter, pSeg);
3811 }else{
3812 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
3813 u8 *pEnd = (u8*)&a[pSeg->nPos];
3814 int iPrev = 0;
3815 int *aiCol = pIter->pColset->aiCol;
3816 int *aiColEnd = &aiCol[pIter->pColset->nCol];
3817
3818 u8 *aOut = pIter->poslist.p;
3819 int iPrevOut = 0;
3820
3821 pIter->base.iRowid = pSeg->iRowid;
3822
3823 while( a<pEnd ){
3824 iPrev += (int)a++[0] - 2;
3825 while( *aiCol<iPrev ){
3826 aiCol++;
3827 if( aiCol==aiColEnd ) goto setoutputs_col_out;
3828 }
3829 if( *aiCol==iPrev ){
3830 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
3831 iPrevOut = iPrev;
3832 }
3833 }
3834
3835 setoutputs_col_out:
3836 pIter->base.pData = pIter->poslist.p;
3837 pIter->base.nData = aOut - pIter->poslist.p;
3838 }
3839 }
3840
3841 /*
3842 ** xSetOutputs callback used by detail=full when there is a column filter.
3843 */
3844 static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
3845 Fts5Colset *pColset = pIter->pColset;
3846 pIter->base.iRowid = pSeg->iRowid;
3847
3848 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
3849 assert( pColset );
3850
3851 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
3852 /* All data is stored on the current page. Populate the output
3853 ** variables to point into the body of the page object. */
3854 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
3855 int *pRc = &pIter->pIndex->rc;
3856 fts5BufferZero(&pIter->poslist);
3857 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, pIter);
3858 }else{
3859 /* The data is distributed over two or more pages. Copy it into the
3860 ** Fts5Iter.poslist buffer and then set the output pointer to point
3861 ** to this buffer. */
3862 fts5BufferZero(&pIter->poslist);
3863 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
3864 pIter->base.pData = pIter->poslist.p;
3865 pIter->base.nData = pIter->poslist.n;
3866 }
3867 }
3868
3869 static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
3870 assert( pIter!=0 || (*pRc)!=SQLITE_OK );
3871 if( *pRc==SQLITE_OK ){
3872 Fts5Config *pConfig = pIter->pIndex->pConfig;
3873 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
3874 pIter->xSetOutputs = fts5IterSetOutputs_None;
3875 }
3876
3877 else if( pIter->pColset==0 ){
3878 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
3879 }
3880
3881 else if( pIter->pColset->nCol==0 ){
3882 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
3883 }
3884
3885 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
3886 pIter->xSetOutputs = fts5IterSetOutputs_Full;
3887 }
3888
3889 else{
3890 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
3891 if( pConfig->nCol<=100 ){
3892 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
3893 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
3894 }else{
3895 pIter->xSetOutputs = fts5IterSetOutputs_Col;
3896 }
3897 }
3898 }
3899 }
3900
3901 /*
3902 ** All the component segment-iterators of pIter have been set up. This
3903 ** functions finishes setup for iterator pIter itself.
3904 */
3905 static void fts5MultiIterFinishSetup(Fts5Index *p, Fts5Iter *pIter){
3906 int iIter;
3907 for(iIter=pIter->nSeg-1; iIter>0; iIter--){
3908 int iEq;
3909 if( (iEq = fts5MultiIterDoCompare(pIter, iIter)) ){
3910 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
3911 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
3912 fts5MultiIterAdvanced(p, pIter, iEq, iIter);
3913 }
3914 }
3915 fts5MultiIterSetEof(pIter);
3916 fts5AssertMultiIterSetup(p, pIter);
3917
3918 if( (pIter->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter))
3919 || fts5MultiIterIsDeleted(pIter)
3920 ){
3921 fts5MultiIterNext(p, pIter, 0, 0);
3922 }else if( pIter->base.bEof==0 ){
3923 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
3924 pIter->xSetOutputs(pIter, pSeg);
3925 }
3926 }
3927
3928 /*
3929 ** Allocate a new Fts5Iter object.
3930 **
3931 ** The new object will be used to iterate through data in structure pStruct.
3932 ** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
3933 ** is zero or greater, data from the first nSegment segments on level iLevel
3934 ** is merged.
3935 **
3936 ** The iterator initially points to the first term/rowid entry in the
3937 ** iterated data.
3938 */
3939 static void fts5MultiIterNew(
3940 Fts5Index *p, /* FTS5 backend to iterate within */
3941 Fts5Structure *pStruct, /* Structure of specific index */
3942 int flags, /* FTS5INDEX_QUERY_XXX flags */
3943 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
3944 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
3945 int iLevel, /* Level to iterate (-1 for all) */
3946 int nSegment, /* Number of segments to merge (iLevel>=0) */
3947 Fts5Iter **ppOut /* New object */
3948 ){
3949 int nSeg = 0; /* Number of segment-iters in use */
3950 int iIter = 0; /* */
3951 int iSeg; /* Used to iterate through segments */
3952 Fts5StructureLevel *pLvl;
3953 Fts5Iter *pNew;
3954
3955 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
3956
3957 /* Allocate space for the new multi-seg-iterator. */
3958 if( p->rc==SQLITE_OK ){
3959 if( iLevel<0 ){
3960 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
3961 nSeg = pStruct->nSegment;
3962 nSeg += (p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH));
3963 }else{
3964 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
3965 }
3966 }
3967 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
3968 if( pNew==0 ){
3969 assert( p->rc!=SQLITE_OK );
3970 goto fts5MultiIterNew_post_check;
3971 }
3972 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
3973 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
3974 pNew->pColset = pColset;
3975 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
3976 fts5IterSetOutputCb(&p->rc, pNew);
3977 }
3978
3979 /* Initialize each of the component segment iterators. */
3980 if( p->rc==SQLITE_OK ){
3981 if( iLevel<0 ){
3982 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
3983 if( p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH) ){
3984 /* Add a segment iterator for the current contents of the hash table. */
3985 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
3986 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
3987 }
3988 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
3989 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
3990 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
3991 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
3992 if( pTerm==0 ){
3993 fts5SegIterInit(p, pSeg, pIter);
3994 }else{
3995 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
3996 }
3997 }
3998 }
3999 }else{
4000 pLvl = &pStruct->aLevel[iLevel];
4001 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
4002 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
4003 }
4004 }
4005 assert( iIter==nSeg );
4006 }
4007
4008 /* If the above was successful, each component iterator now points
4009 ** to the first entry in its segment. In this case initialize the
4010 ** aFirst[] array. Or, if an error has occurred, free the iterator
4011 ** object and set the output variable to NULL. */
4012 if( p->rc==SQLITE_OK ){
4013 fts5MultiIterFinishSetup(p, pNew);
4014 }else{
4015 fts5MultiIterFree(pNew);
4016 *ppOut = 0;
4017 }
4018
4019 fts5MultiIterNew_post_check:
4020 assert( (*ppOut)!=0 || p->rc!=SQLITE_OK );
4021 return;
4022 }
4023
4024 /*
4025 ** Create an Fts5Iter that iterates through the doclist provided
4026 ** as the second argument.
4027 */
4028 static void fts5MultiIterNew2(
4029 Fts5Index *p, /* FTS5 backend to iterate within */
4030 Fts5Data *pData, /* Doclist to iterate through */
4031 int bDesc, /* True for descending rowid order */
4032 Fts5Iter **ppOut /* New object */
4033 ){
4034 Fts5Iter *pNew;
4035 pNew = fts5MultiIterAlloc(p, 2);
4036 if( pNew ){
4037 Fts5SegIter *pIter = &pNew->aSeg[1];
4038 pIter->flags = FTS5_SEGITER_ONETERM;
4039 if( pData->szLeaf>0 ){
4040 pIter->pLeaf = pData;
4041 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
4042 pIter->iEndofDoclist = pData->nn;
4043 pNew->aFirst[1].iFirst = 1;
4044 if( bDesc ){
4045 pNew->bRev = 1;
4046 pIter->flags |= FTS5_SEGITER_REVERSE;
4047 fts5SegIterReverseInitPage(p, pIter);
4048 }else{
4049 fts5SegIterLoadNPos(p, pIter);
4050 }
4051 pData = 0;
4052 }else{
4053 pNew->base.bEof = 1;
4054 }
4055 fts5SegIterSetNext(p, pIter);
4056
4057 *ppOut = pNew;
4058 }
4059
4060 fts5DataRelease(pData);
4061 }
4062
4063 /*
4064 ** Return true if the iterator is at EOF or if an error has occurred.
4065 ** False otherwise.
4066 */
4067 static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
4068 assert( pIter!=0 || p->rc!=SQLITE_OK );
4069 assert( p->rc!=SQLITE_OK
4070 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
4071 );
4072 return (p->rc || pIter->base.bEof);
4073 }
4074
4075 /*
4076 ** Return the rowid of the entry that the iterator currently points
4077 ** to. If the iterator points to EOF when this function is called the
4078 ** results are undefined.
4079 */
4080 static i64 fts5MultiIterRowid(Fts5Iter *pIter){
4081 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
4082 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
4083 }
4084
4085 /*
4086 ** Move the iterator to the next entry at or following iMatch.
4087 */
4088 static void fts5MultiIterNextFrom(
4089 Fts5Index *p,
4090 Fts5Iter *pIter,
4091 i64 iMatch
4092 ){
4093 while( 1 ){
4094 i64 iRowid;
4095 fts5MultiIterNext(p, pIter, 1, iMatch);
4096 if( fts5MultiIterEof(p, pIter) ) break;
4097 iRowid = fts5MultiIterRowid(pIter);
4098 if( pIter->bRev==0 && iRowid>=iMatch ) break;
4099 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
4100 }
4101 }
4102
4103 /*
4104 ** Return a pointer to a buffer containing the term associated with the
4105 ** entry that the iterator currently points to.
4106 */
4107 static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
4108 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
4109 *pn = p->term.n;
4110 return p->term.p;
4111 }
4112
4113 /*
4114 ** Allocate a new segment-id for the structure pStruct. The new segment
4115 ** id must be between 1 and 65335 inclusive, and must not be used by
4116 ** any currently existing segment. If a free segment id cannot be found,
4117 ** SQLITE_FULL is returned.
4118 **
4119 ** If an error has already occurred, this function is a no-op. 0 is
4120 ** returned in this case.
4121 */
4122 static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
4123 int iSegid = 0;
4124
4125 if( p->rc==SQLITE_OK ){
4126 if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
4127 p->rc = SQLITE_FULL;
4128 }else{
4129 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
4130 ** array is 63 elements, or 252 bytes, in size. */
4131 u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
4132 int iLvl, iSeg;
4133 int i;
4134 u32 mask;
4135 memset(aUsed, 0, sizeof(aUsed));
4136 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
4137 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
4138 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
4139 if( iId<=FTS5_MAX_SEGMENT && iId>0 ){
4140 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
4141 }
4142 }
4143 }
4144
4145 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
4146 mask = aUsed[i];
4147 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
4148 iSegid += 1 + i*32;
4149
4150 #ifdef SQLITE_DEBUG
4151 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
4152 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
4153 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
4154 }
4155 }
4156 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
4157
4158 {
4159 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
4160 if( p->rc==SQLITE_OK ){
4161 u8 aBlob[2] = {0xff, 0xff};
4162 sqlite3_bind_int(pIdxSelect, 1, iSegid);
4163 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
4164 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
4165 p->rc = sqlite3_reset(pIdxSelect);
4166 sqlite3_bind_null(pIdxSelect, 2);
4167 }
4168 }
4169 #endif
4170 }
4171 }
4172
4173 return iSegid;
4174 }
4175
4176 /*
4177 ** Discard all data currently cached in the hash-tables.
4178 */
4179 static void fts5IndexDiscardData(Fts5Index *p){
4180 assert( p->pHash || p->nPendingData==0 );
4181 if( p->pHash ){
4182 sqlite3Fts5HashClear(p->pHash);
4183 p->nPendingData = 0;
4184 p->nPendingRow = 0;
4185 p->flushRc = SQLITE_OK;
4186 }
4187 p->nContentlessDelete = 0;
4188 }
4189
4190 /*
4191 ** Return the size of the prefix, in bytes, that buffer
4192 ** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
4193 **
4194 ** Buffer (pNew/<length-unknown>) is guaranteed to be greater
4195 ** than buffer (pOld/nOld).
4196 */
4197 static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
4198 int i;
4199 for(i=0; i<nOld; i++){
4200 if( pOld[i]!=pNew[i] ) break;
4201 }
4202 return i;
4203 }
4204
4205 static void fts5WriteDlidxClear(
4206 Fts5Index *p,
4207 Fts5SegWriter *pWriter,
4208 int bFlush /* If true, write dlidx to disk */
4209 ){
4210 int i;
4211 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
4212 for(i=0; i<pWriter->nDlidx; i++){
4213 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
4214 if( pDlidx->buf.n==0 ) break;
4215 if( bFlush ){
4216 assert( pDlidx->pgno!=0 );
4217 fts5DataWrite(p,
4218 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
4219 pDlidx->buf.p, pDlidx->buf.n
4220 );
4221 }
4222 sqlite3Fts5BufferZero(&pDlidx->buf);
4223 pDlidx->bPrevValid = 0;
4224 }
4225 }
4226
4227 /*
4228 ** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
4229 ** Any new array elements are zeroed before returning.
4230 */
4231 static int fts5WriteDlidxGrow(
4232 Fts5Index *p,
4233 Fts5SegWriter *pWriter,
4234 int nLvl
4235 ){
4236 if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
4237 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
4238 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
4239 );
4240 if( aDlidx==0 ){
4241 p->rc = SQLITE_NOMEM;
4242 }else{
4243 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
4244 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
4245 pWriter->aDlidx = aDlidx;
4246 pWriter->nDlidx = nLvl;
4247 }
4248 }
4249 return p->rc;
4250 }
4251
4252 /*
4253 ** If the current doclist-index accumulating in pWriter->aDlidx[] is large
4254 ** enough, flush it to disk and return 1. Otherwise discard it and return
4255 ** zero.
4256 */
4257 static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
4258 int bFlag = 0;
4259
4260 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
4261 ** to the database, also write the doclist-index to disk. */
4262 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
4263 bFlag = 1;
4264 }
4265 fts5WriteDlidxClear(p, pWriter, bFlag);
4266 pWriter->nEmpty = 0;
4267 return bFlag;
4268 }
4269
4270 /*
4271 ** This function is called whenever processing of the doclist for the
4272 ** last term on leaf page (pWriter->iBtPage) is completed.
4273 **
4274 ** The doclist-index for that term is currently stored in-memory within the
4275 ** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
4276 ** writes it out to disk. Or, if it is too small to bother with, discards
4277 ** it.
4278 **
4279 ** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
4280 */
4281 static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
4282 int bFlag;
4283
4284 assert( pWriter->iBtPage || pWriter->nEmpty==0 );
4285 if( pWriter->iBtPage==0 ) return;
4286 bFlag = fts5WriteFlushDlidx(p, pWriter);
4287
4288 if( p->rc==SQLITE_OK ){
4289 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
4290 /* The following was already done in fts5WriteInit(): */
4291 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
4292 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
4293 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
4294 sqlite3_step(p->pIdxWriter);
4295 p->rc = sqlite3_reset(p->pIdxWriter);
4296 sqlite3_bind_null(p->pIdxWriter, 2);
4297 }
4298 pWriter->iBtPage = 0;
4299 }
4300
4301 /*
4302 ** This is called once for each leaf page except the first that contains
4303 ** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
4304 ** is larger than all terms written to earlier leaves, and equal to or
4305 ** smaller than the first term on the new leaf.
4306 **
4307 ** If an error occurs, an error code is left in Fts5Index.rc. If an error
4308 ** has already occurred when this function is called, it is a no-op.
4309 */
4310 static void fts5WriteBtreeTerm(
4311 Fts5Index *p, /* FTS5 backend object */
4312 Fts5SegWriter *pWriter, /* Writer object */
4313 int nTerm, const u8 *pTerm /* First term on new page */
4314 ){
4315 fts5WriteFlushBtree(p, pWriter);
4316 if( p->rc==SQLITE_OK ){
4317 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
4318 pWriter->iBtPage = pWriter->writer.pgno;
4319 }
4320 }
4321
4322 /*
4323 ** This function is called when flushing a leaf page that contains no
4324 ** terms at all to disk.
4325 */
4326 static void fts5WriteBtreeNoTerm(
4327 Fts5Index *p, /* FTS5 backend object */
4328 Fts5SegWriter *pWriter /* Writer object */
4329 ){
4330 /* If there were no rowids on the leaf page either and the doclist-index
4331 ** has already been started, append an 0x00 byte to it. */
4332 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
4333 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
4334 assert( pDlidx->bPrevValid );
4335 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
4336 }
4337
4338 /* Increment the "number of sequential leaves without a term" counter. */
4339 pWriter->nEmpty++;
4340 }
4341
4342 static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
4343 i64 iRowid;
4344 int iOff;
4345
4346 iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
4347 fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
4348 return iRowid;
4349 }
4350
4351 /*
4352 ** Rowid iRowid has just been appended to the current leaf page. It is the
4353 ** first on the page. This function appends an appropriate entry to the current
4354 ** doclist-index.
4355 */
4356 static void fts5WriteDlidxAppend(
4357 Fts5Index *p,
4358 Fts5SegWriter *pWriter,
4359 i64 iRowid
4360 ){
4361 int i;
4362 int bDone = 0;
4363
4364 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
4365 i64 iVal;
4366 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
4367
4368 if( pDlidx->buf.n>=p->pConfig->pgsz ){
4369 /* The current doclist-index page is full. Write it to disk and push
4370 ** a copy of iRowid (which will become the first rowid on the next
4371 ** doclist-index leaf page) up into the next level of the b-tree
4372 ** hierarchy. If the node being flushed is currently the root node,
4373 ** also push its first rowid upwards. */
4374 pDlidx->buf.p[0] = 0x01; /* Not the root node */
4375 fts5DataWrite(p,
4376 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
4377 pDlidx->buf.p, pDlidx->buf.n
4378 );
4379 fts5WriteDlidxGrow(p, pWriter, i+2);
4380 pDlidx = &pWriter->aDlidx[i];
4381 if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
4382 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
4383
4384 /* This was the root node. Push its first rowid up to the new root. */
4385 pDlidx[1].pgno = pDlidx->pgno;
4386 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
4387 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
4388 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
4389 pDlidx[1].bPrevValid = 1;
4390 pDlidx[1].iPrev = iFirst;
4391 }
4392
4393 sqlite3Fts5BufferZero(&pDlidx->buf);
4394 pDlidx->bPrevValid = 0;
4395 pDlidx->pgno++;
4396 }else{
4397 bDone = 1;
4398 }
4399
4400 if( pDlidx->bPrevValid ){
4401 iVal = (u64)iRowid - (u64)pDlidx->iPrev;
4402 }else{
4403 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
4404 assert( pDlidx->buf.n==0 );
4405 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
4406 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
4407 iVal = iRowid;
4408 }
4409
4410 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
4411 pDlidx->bPrevValid = 1;
4412 pDlidx->iPrev = iRowid;
4413 }
4414 }
4415
4416 static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
4417 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
4418 Fts5PageWriter *pPage = &pWriter->writer;
4419 i64 iRowid;
4420
4421 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
4422
4423 /* Set the szLeaf header field. */
4424 assert( 0==fts5GetU16(&pPage->buf.p[2]) );
4425 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
4426
4427 if( pWriter->bFirstTermInPage ){
4428 /* No term was written to this page. */
4429 assert( pPage->pgidx.n==0 );
4430 fts5WriteBtreeNoTerm(p, pWriter);
4431 }else{
4432 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
4433 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
4434 }
4435
4436 /* Write the page out to disk */
4437 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
4438 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
4439
4440 /* Initialize the next page. */
4441 fts5BufferZero(&pPage->buf);
4442 fts5BufferZero(&pPage->pgidx);
4443 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
4444 pPage->iPrevPgidx = 0;
4445 pPage->pgno++;
4446
4447 /* Increase the leaves written counter */
4448 pWriter->nLeafWritten++;
4449
4450 /* The new leaf holds no terms or rowids */
4451 pWriter->bFirstTermInPage = 1;
4452 pWriter->bFirstRowidInPage = 1;
4453 }
4454
4455 /*
4456 ** Append term pTerm/nTerm to the segment being written by the writer passed
4457 ** as the second argument.
4458 **
4459 ** If an error occurs, set the Fts5Index.rc error code. If an error has
4460 ** already occurred, this function is a no-op.
4461 */
4462 static void fts5WriteAppendTerm(
4463 Fts5Index *p,
4464 Fts5SegWriter *pWriter,
4465 int nTerm, const u8 *pTerm
4466 ){
4467 int nPrefix; /* Bytes of prefix compression for term */
4468 Fts5PageWriter *pPage = &pWriter->writer;
4469 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
4470 int nMin = MIN(pPage->term.n, nTerm);
4471
4472 assert( p->rc==SQLITE_OK );
4473 assert( pPage->buf.n>=4 );
4474 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
4475
4476 /* If the current leaf page is full, flush it to disk. */
4477 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
4478 if( pPage->buf.n>4 ){
4479 fts5WriteFlushLeaf(p, pWriter);
4480 if( p->rc!=SQLITE_OK ) return;
4481 }
4482 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
4483 }
4484
4485 /* TODO1: Updating pgidx here. */
4486 pPgidx->n += sqlite3Fts5PutVarint(
4487 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
4488 );
4489 pPage->iPrevPgidx = pPage->buf.n;
4490 #if 0
4491 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
4492 pPgidx->n += 2;
4493 #endif
4494
4495 if( pWriter->bFirstTermInPage ){
4496 nPrefix = 0;
4497 if( pPage->pgno!=1 ){
4498 /* This is the first term on a leaf that is not the leftmost leaf in
4499 ** the segment b-tree. In this case it is necessary to add a term to
4500 ** the b-tree hierarchy that is (a) larger than the largest term
4501 ** already written to the segment and (b) smaller than or equal to
4502 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
4503 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
4504 ** previous term.
4505 **
4506 ** Usually, the previous term is available in pPage->term. The exception
4507 ** is if this is the first term written in an incremental-merge step.
4508 ** In this case the previous term is not available, so just write a
4509 ** copy of (pTerm/nTerm) into the parent node. This is slightly
4510 ** inefficient, but still correct. */
4511 int n = nTerm;
4512 if( pPage->term.n ){
4513 n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
4514 }
4515 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
4516 if( p->rc!=SQLITE_OK ) return;
4517 pPage = &pWriter->writer;
4518 }
4519 }else{
4520 nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
4521 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
4522 }
4523
4524 /* Append the number of bytes of new data, then the term data itself
4525 ** to the page. */
4526 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
4527 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
4528
4529 /* Update the Fts5PageWriter.term field. */
4530 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
4531 pWriter->bFirstTermInPage = 0;
4532
4533 pWriter->bFirstRowidInPage = 0;
4534 pWriter->bFirstRowidInDoclist = 1;
4535
4536 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
4537 pWriter->aDlidx[0].pgno = pPage->pgno;
4538 }
4539
4540 /*
4541 ** Append a rowid and position-list size field to the writers output.
4542 */
4543 static void fts5WriteAppendRowid(
4544 Fts5Index *p,
4545 Fts5SegWriter *pWriter,
4546 i64 iRowid
4547 ){
4548 if( p->rc==SQLITE_OK ){
4549 Fts5PageWriter *pPage = &pWriter->writer;
4550
4551 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
4552 fts5WriteFlushLeaf(p, pWriter);
4553 }
4554
4555 /* If this is to be the first rowid written to the page, set the
4556 ** rowid-pointer in the page-header. Also append a value to the dlidx
4557 ** buffer, in case a doclist-index is required. */
4558 if( pWriter->bFirstRowidInPage ){
4559 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
4560 fts5WriteDlidxAppend(p, pWriter, iRowid);
4561 }
4562
4563 /* Write the rowid. */
4564 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
4565 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
4566 }else{
4567 assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
4568 fts5BufferAppendVarint(&p->rc, &pPage->buf,
4569 (u64)iRowid - (u64)pWriter->iPrevRowid
4570 );
4571 }
4572 pWriter->iPrevRowid = iRowid;
4573 pWriter->bFirstRowidInDoclist = 0;
4574 pWriter->bFirstRowidInPage = 0;
4575 }
4576 }
4577
4578 static void fts5WriteAppendPoslistData(
4579 Fts5Index *p,
4580 Fts5SegWriter *pWriter,
4581 const u8 *aData,
4582 int nData
4583 ){
4584 Fts5PageWriter *pPage = &pWriter->writer;
4585 const u8 *a = aData;
4586 int n = nData;
4587
4588 assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK );
4589 while( p->rc==SQLITE_OK
4590 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
4591 ){
4592 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
4593 int nCopy = 0;
4594 while( nCopy<nReq ){
4595 i64 dummy;
4596 nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
4597 }
4598 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
4599 a += nCopy;
4600 n -= nCopy;
4601 fts5WriteFlushLeaf(p, pWriter);
4602 }
4603 if( n>0 ){
4604 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
4605 }
4606 }
4607
4608 /*
4609 ** Flush any data cached by the writer object to the database. Free any
4610 ** allocations associated with the writer.
4611 */
4612 static void fts5WriteFinish(
4613 Fts5Index *p,
4614 Fts5SegWriter *pWriter, /* Writer object */
4615 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
4616 ){
4617 int i;
4618 Fts5PageWriter *pLeaf = &pWriter->writer;
4619 if( p->rc==SQLITE_OK ){
4620 assert( pLeaf->pgno>=1 );
4621 if( pLeaf->buf.n>4 ){
4622 fts5WriteFlushLeaf(p, pWriter);
4623 }
4624 *pnLeaf = pLeaf->pgno-1;
4625 if( pLeaf->pgno>1 ){
4626 fts5WriteFlushBtree(p, pWriter);
4627 }
4628 }
4629 fts5BufferFree(&pLeaf->term);
4630 fts5BufferFree(&pLeaf->buf);
4631 fts5BufferFree(&pLeaf->pgidx);
4632 fts5BufferFree(&pWriter->btterm);
4633
4634 for(i=0; i<pWriter->nDlidx; i++){
4635 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
4636 }
4637 sqlite3_free(pWriter->aDlidx);
4638 }
4639
4640 static void fts5WriteInit(
4641 Fts5Index *p,
4642 Fts5SegWriter *pWriter,
4643 int iSegid
4644 ){
4645 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
4646
4647 memset(pWriter, 0, sizeof(Fts5SegWriter));
4648 pWriter->iSegid = iSegid;
4649
4650 fts5WriteDlidxGrow(p, pWriter, 1);
4651 pWriter->writer.pgno = 1;
4652 pWriter->bFirstTermInPage = 1;
4653 pWriter->iBtPage = 1;
4654
4655 assert( pWriter->writer.buf.n==0 );
4656 assert( pWriter->writer.pgidx.n==0 );
4657
4658 /* Grow the two buffers to pgsz + padding bytes in size. */
4659 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
4660 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
4661
4662 if( p->pIdxWriter==0 ){
4663 Fts5Config *pConfig = p->pConfig;
4664 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
4665 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
4666 pConfig->zDb, pConfig->zName
4667 ));
4668 }
4669
4670 if( p->rc==SQLITE_OK ){
4671 /* Initialize the 4-byte leaf-page header to 0x00. */
4672 memset(pWriter->writer.buf.p, 0, 4);
4673 pWriter->writer.buf.n = 4;
4674
4675 /* Bind the current output segment id to the index-writer. This is an
4676 ** optimization over binding the same value over and over as rows are
4677 ** inserted into %_idx by the current writer. */
4678 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
4679 }
4680 }
4681
4682 /*
4683 ** Iterator pIter was used to iterate through the input segments of on an
4684 ** incremental merge operation. This function is called if the incremental
4685 ** merge step has finished but the input has not been completely exhausted.
4686 */
4687 static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
4688 int i;
4689 Fts5Buffer buf;
4690 memset(&buf, 0, sizeof(Fts5Buffer));
4691 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK; i++){
4692 Fts5SegIter *pSeg = &pIter->aSeg[i];
4693 if( pSeg->pSeg==0 ){
4694 /* no-op */
4695 }else if( pSeg->pLeaf==0 ){
4696 /* All keys from this input segment have been transfered to the output.
4697 ** Set both the first and last page-numbers to 0 to indicate that the
4698 ** segment is now empty. */
4699 pSeg->pSeg->pgnoLast = 0;
4700 pSeg->pSeg->pgnoFirst = 0;
4701 }else{
4702 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
4703 i64 iLeafRowid;
4704 Fts5Data *pData;
4705 int iId = pSeg->pSeg->iSegid;
4706 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
4707
4708 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
4709 pData = fts5LeafRead(p, iLeafRowid);
4710 if( pData ){
4711 if( iOff>pData->szLeaf ){
4712 /* This can occur if the pages that the segments occupy overlap - if
4713 ** a single page has been assigned to more than one segment. In
4714 ** this case a prior iteration of this loop may have corrupted the
4715 ** segment currently being trimmed. */
4716 FTS5_CORRUPT_ROWID(p, iLeafRowid);
4717 }else{
4718 fts5BufferZero(&buf);
4719 fts5BufferGrow(&p->rc, &buf, pData->nn);
4720 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
4721 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
4722 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
4723 fts5BufferAppendBlob(&p->rc, &buf,pData->szLeaf-iOff,&pData->p[iOff]);
4724 if( p->rc==SQLITE_OK ){
4725 /* Set the szLeaf field */
4726 fts5PutU16(&buf.p[2], (u16)buf.n);
4727 }
4728
4729 /* Set up the new page-index array */
4730 fts5BufferAppendVarint(&p->rc, &buf, 4);
4731 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
4732 && pSeg->iEndofDoclist<pData->szLeaf
4733 && pSeg->iPgidxOff<=pData->nn
4734 ){
4735 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
4736 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
4737 fts5BufferAppendBlob(&p->rc, &buf,
4738 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
4739 );
4740 }
4741
4742 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
4743 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid);
4744 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
4745 }
4746 fts5DataRelease(pData);
4747 }
4748 }
4749 }
4750 fts5BufferFree(&buf);
4751 }
4752
4753 static void fts5MergeChunkCallback(
4754 Fts5Index *p,
4755 void *pCtx,
4756 const u8 *pChunk, int nChunk
4757 ){
4758 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
4759 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
4760 }
4761
4762 /*
4763 **
4764 */
4765 static void fts5IndexMergeLevel(
4766 Fts5Index *p, /* FTS5 backend object */
4767 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
4768 int iLvl, /* Level to read input from */
4769 int *pnRem /* Write up to this many output leaves */
4770 ){
4771 Fts5Structure *pStruct = *ppStruct;
4772 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
4773 Fts5StructureLevel *pLvlOut;
4774 Fts5Iter *pIter = 0; /* Iterator to read input data */
4775 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
4776 int nInput; /* Number of input segments */
4777 Fts5SegWriter writer; /* Writer object */
4778 Fts5StructureSegment *pSeg; /* Output segment */
4779 Fts5Buffer term;
4780 int bOldest; /* True if the output segment is the oldest */
4781 int eDetail = p->pConfig->eDetail;
4782 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
4783 int bTermWritten = 0; /* True if current term already output */
4784
4785 assert( iLvl<pStruct->nLevel );
4786 assert( pLvl->nMerge<=pLvl->nSeg );
4787
4788 memset(&writer, 0, sizeof(Fts5SegWriter));
4789 memset(&term, 0, sizeof(Fts5Buffer));
4790 if( pLvl->nMerge ){
4791 pLvlOut = &pStruct->aLevel[iLvl+1];
4792 assert( pLvlOut->nSeg>0 );
4793 nInput = pLvl->nMerge;
4794 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
4795
4796 fts5WriteInit(p, &writer, pSeg->iSegid);
4797 writer.writer.pgno = pSeg->pgnoLast+1;
4798 writer.iBtPage = 0;
4799 }else{
4800 int iSegid = fts5AllocateSegid(p, pStruct);
4801
4802 /* Extend the Fts5Structure object as required to ensure the output
4803 ** segment exists. */
4804 if( iLvl==pStruct->nLevel-1 ){
4805 fts5StructureAddLevel(&p->rc, ppStruct);
4806 pStruct = *ppStruct;
4807 }
4808 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
4809 if( p->rc ) return;
4810 pLvl = &pStruct->aLevel[iLvl];
4811 pLvlOut = &pStruct->aLevel[iLvl+1];
4812
4813 fts5WriteInit(p, &writer, iSegid);
4814
4815 /* Add the new segment to the output level */
4816 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
4817 pLvlOut->nSeg++;
4818 pSeg->pgnoFirst = 1;
4819 pSeg->iSegid = iSegid;
4820 pStruct->nSegment++;
4821
4822 /* Read input from all segments in the input level */
4823 nInput = pLvl->nSeg;
4824
4825 /* Set the range of origins that will go into the output segment. */
4826 if( pStruct->nOriginCntr>0 ){
4827 pSeg->iOrigin1 = pLvl->aSeg[0].iOrigin1;
4828 pSeg->iOrigin2 = pLvl->aSeg[pLvl->nSeg-1].iOrigin2;
4829 }
4830 }
4831 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
4832
4833 assert( iLvl>=0 );
4834 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
4835 fts5MultiIterEof(p, pIter)==0;
4836 fts5MultiIterNext(p, pIter, 0, 0)
4837 ){
4838 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
4839 int nPos; /* position-list size field value */
4840 int nTerm;
4841 const u8 *pTerm;
4842
4843 pTerm = fts5MultiIterTerm(pIter, &nTerm);
4844 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){
4845 if( pnRem && writer.nLeafWritten>nRem ){
4846 break;
4847 }
4848 fts5BufferSet(&p->rc, &term, nTerm, pTerm);
4849 bTermWritten =0;
4850 }
4851
4852 /* Check for key annihilation. */
4853 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
4854
4855 if( p->rc==SQLITE_OK && bTermWritten==0 ){
4856 /* This is a new term. Append a term to the output segment. */
4857 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
4858 bTermWritten = 1;
4859 }
4860
4861 /* Append the rowid to the output */
4862 /* WRITEPOSLISTSIZE */
4863 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
4864
4865 if( eDetail==FTS5_DETAIL_NONE ){
4866 if( pSegIter->bDel ){
4867 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
4868 if( pSegIter->nPos>0 ){
4869 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
4870 }
4871 }
4872 }else{
4873 /* Append the position-list data to the output */
4874 nPos = pSegIter->nPos*2 + pSegIter->bDel;
4875 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
4876 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
4877 }
4878 }
4879
4880 /* Flush the last leaf page to disk. Set the output segment b-tree height
4881 ** and last leaf page number at the same time. */
4882 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
4883
4884 assert( pIter!=0 || p->rc!=SQLITE_OK );
4885 if( fts5MultiIterEof(p, pIter) ){
4886 int i;
4887
4888 /* Remove the redundant segments from the %_data table */
4889 assert( pSeg->nEntry==0 );
4890 for(i=0; i<nInput; i++){
4891 Fts5StructureSegment *pOld = &pLvl->aSeg[i];
4892 pSeg->nEntry += (pOld->nEntry - pOld->nEntryTombstone);
4893 fts5DataRemoveSegment(p, pOld);
4894 }
4895
4896 /* Remove the redundant segments from the input level */
4897 if( pLvl->nSeg!=nInput ){
4898 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
4899 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
4900 }
4901 pStruct->nSegment -= nInput;
4902 pLvl->nSeg -= nInput;
4903 pLvl->nMerge = 0;
4904 if( pSeg->pgnoLast==0 ){
4905 pLvlOut->nSeg--;
4906 pStruct->nSegment--;
4907 }
4908 }else{
4909 assert( pSeg->pgnoLast>0 );
4910 fts5TrimSegments(p, pIter);
4911 pLvl->nMerge = nInput;
4912 }
4913
4914 fts5MultiIterFree(pIter);
4915 fts5BufferFree(&term);
4916 if( pnRem ) *pnRem -= writer.nLeafWritten;
4917 }
4918
4919 /*
4920 ** If this is not a contentless_delete=1 table, or if the 'deletemerge'
4921 ** configuration option is set to 0, then this function always returns -1.
4922 ** Otherwise, it searches the structure object passed as the second argument
4923 ** for a level suitable for merging due to having a large number of
4924 ** tombstones in the tombstone hash. If one is found, its index is returned.
4925 ** Otherwise, if there is no suitable level, -1.
4926 */
4927 static int fts5IndexFindDeleteMerge(Fts5Index *p, Fts5Structure *pStruct){
4928 Fts5Config *pConfig = p->pConfig;
4929 int iRet = -1;
4930 if( pConfig->bContentlessDelete && pConfig->nDeleteMerge>0 ){
4931 int ii;
4932 int nBest = 0;
4933
4934 for(ii=0; ii<pStruct->nLevel; ii++){
4935 Fts5StructureLevel *pLvl = &pStruct->aLevel[ii];
4936 i64 nEntry = 0;
4937 i64 nTomb = 0;
4938 int iSeg;
4939 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
4940 nEntry += pLvl->aSeg[iSeg].nEntry;
4941 nTomb += pLvl->aSeg[iSeg].nEntryTombstone;
4942 }
4943 assert_nc( nEntry>0 || pLvl->nSeg==0 );
4944 if( nEntry>0 ){
4945 int nPercent = (nTomb * 100) / nEntry;
4946 if( nPercent>=pConfig->nDeleteMerge && nPercent>nBest ){
4947 iRet = ii;
4948 nBest = nPercent;
4949 }
4950 }
4951
4952 /* If pLvl is already the input level to an ongoing merge, look no
4953 ** further for a merge candidate. The caller should be allowed to
4954 ** continue merging from pLvl first. */
4955 if( pLvl->nMerge ) break;
4956 }
4957 }
4958 return iRet;
4959 }
4960
4961 /*
4962 ** Do up to nPg pages of automerge work on the index.
4963 **
4964 ** Return true if any changes were actually made, or false otherwise.
4965 */
4966 static int fts5IndexMerge(
4967 Fts5Index *p, /* FTS5 backend object */
4968 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
4969 int nPg, /* Pages of work to do */
4970 int nMin /* Minimum number of segments to merge */
4971 ){
4972 int nRem = nPg;
4973 int bRet = 0;
4974 Fts5Structure *pStruct = *ppStruct;
4975 while( nRem>0 && p->rc==SQLITE_OK ){
4976 int iLvl; /* To iterate through levels */
4977 int iBestLvl = 0; /* Level offering the most input segments */
4978 int nBest = 0; /* Number of input segments on best level */
4979
4980 /* Set iBestLvl to the level to read input segments from. Or to -1 if
4981 ** there is no level suitable to merge segments from. */
4982 assert( pStruct->nLevel>0 );
4983 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
4984 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
4985 if( pLvl->nMerge ){
4986 if( pLvl->nMerge>nBest ){
4987 iBestLvl = iLvl;
4988 nBest = nMin;
4989 }
4990 break;
4991 }
4992 if( pLvl->nSeg>nBest ){
4993 nBest = pLvl->nSeg;
4994 iBestLvl = iLvl;
4995 }
4996 }
4997 if( nBest<nMin ){
4998 iBestLvl = fts5IndexFindDeleteMerge(p, pStruct);
4999 }
5000
5001 if( iBestLvl<0 ) break;
5002 bRet = 1;
5003 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
5004 if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
5005 fts5StructurePromote(p, iBestLvl+1, pStruct);
5006 }
5007
5008 if( nMin==1 ) nMin = 2;
5009 }
5010 *ppStruct = pStruct;
5011 return bRet;
5012 }
5013
5014 /*
5015 ** A total of nLeaf leaf pages of data has just been flushed to a level-0
5016 ** segment. This function updates the write-counter accordingly and, if
5017 ** necessary, performs incremental merge work.
5018 **
5019 ** If an error occurs, set the Fts5Index.rc error code. If an error has
5020 ** already occurred, this function is a no-op.
5021 */
5022 static void fts5IndexAutomerge(
5023 Fts5Index *p, /* FTS5 backend object */
5024 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
5025 int nLeaf /* Number of output leaves just written */
5026 ){
5027 if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 && ALWAYS((*ppStruct)!=0) ){
5028 Fts5Structure *pStruct = *ppStruct;
5029 u64 nWrite; /* Initial value of write-counter */
5030 int nWork; /* Number of work-quanta to perform */
5031 int nRem; /* Number of leaf pages left to write */
5032
5033 /* Update the write-counter. While doing so, set nWork. */
5034 nWrite = pStruct->nWriteCounter;
5035 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
5036 pStruct->nWriteCounter += nLeaf;
5037 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
5038
5039 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
5040 }
5041 }
5042
5043 static void fts5IndexCrisismerge(
5044 Fts5Index *p, /* FTS5 backend object */
5045 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
5046 ){
5047 const int nCrisis = p->pConfig->nCrisisMerge;
5048 Fts5Structure *pStruct = *ppStruct;
5049 if( pStruct && pStruct->nLevel>0 ){
5050 int iLvl = 0;
5051 while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
5052 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
5053 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
5054 fts5StructurePromote(p, iLvl+1, pStruct);
5055 iLvl++;
5056 }
5057 *ppStruct = pStruct;
5058 }
5059 }
5060
5061 static int fts5IndexReturn(Fts5Index *p){
5062 int rc = p->rc;
5063 p->rc = SQLITE_OK;
5064 return rc;
5065 }
5066
5067 /*
5068 ** Close the read-only blob handle, if it is open.
5069 */
5070 void sqlite3Fts5IndexCloseReader(Fts5Index *p){
5071 fts5IndexCloseReader(p);
5072 fts5IndexReturn(p);
5073 }
5074
5075 typedef struct Fts5FlushCtx Fts5FlushCtx;
5076 struct Fts5FlushCtx {
5077 Fts5Index *pIdx;
5078 Fts5SegWriter writer;
5079 };
5080
5081 /*
5082 ** Buffer aBuf[] contains a list of varints, all small enough to fit
5083 ** in a 32-bit integer. Return the size of the largest prefix of this
5084 ** list nMax bytes or less in size.
5085 */
5086 static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
5087 int ret;
5088 u32 dummy;
5089 ret = fts5GetVarint32(aBuf, dummy);
5090 if( ret<nMax ){
5091 while( 1 ){
5092 int i = fts5GetVarint32(&aBuf[ret], dummy);
5093 if( (ret + i) > nMax ) break;
5094 ret += i;
5095 }
5096 }
5097 return ret;
5098 }
5099
5100 /*
5101 ** Execute the SQL statement:
5102 **
5103 ** DELETE FROM %_idx WHERE (segid, (pgno/2)) = ($iSegid, $iPgno);
5104 **
5105 ** This is used when a secure-delete operation removes the last term
5106 ** from a segment leaf page. In that case the %_idx entry is removed
5107 ** too. This is done to ensure that if all instances of a token are
5108 ** removed from an fts5 database in secure-delete mode, no trace of
5109 ** the token itself remains in the database.
5110 */
5111 static void fts5SecureDeleteIdxEntry(
5112 Fts5Index *p, /* FTS5 backend object */
5113 int iSegid, /* Id of segment to delete entry for */
5114 int iPgno /* Page number within segment */
5115 ){
5116 if( iPgno!=1 ){
5117 assert( p->pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE );
5118 if( p->pDeleteFromIdx==0 ){
5119 fts5IndexPrepareStmt(p, &p->pDeleteFromIdx, sqlite3_mprintf(
5120 "DELETE FROM '%q'.'%q_idx' WHERE (segid, (pgno/2)) = (?1, ?2)",
5121 p->pConfig->zDb, p->pConfig->zName
5122 ));
5123 }
5124 if( p->rc==SQLITE_OK ){
5125 sqlite3_bind_int(p->pDeleteFromIdx, 1, iSegid);
5126 sqlite3_bind_int(p->pDeleteFromIdx, 2, iPgno);
5127 sqlite3_step(p->pDeleteFromIdx);
5128 p->rc = sqlite3_reset(p->pDeleteFromIdx);
5129 }
5130 }
5131 }
5132
5133 /*
5134 ** This is called when a secure-delete operation removes a position-list
5135 ** that overflows onto segment page iPgno of segment pSeg. This function
5136 ** rewrites node iPgno, and possibly one or more of its right-hand peers,
5137 ** to remove this portion of the position list.
5138 **
5139 ** Output variable (*pbLastInDoclist) is set to true if the position-list
5140 ** removed is followed by a new term or the end-of-segment, or false if
5141 ** it is followed by another rowid/position list.
5142 */
5143 static void fts5SecureDeleteOverflow(
5144 Fts5Index *p,
5145 Fts5StructureSegment *pSeg,
5146 int iPgno,
5147 int *pbLastInDoclist
5148 ){
5149 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE);
5150 int pgno;
5151 Fts5Data *pLeaf = 0;
5152 assert( iPgno!=1 );
5153
5154 *pbLastInDoclist = 1;
5155 for(pgno=iPgno; p->rc==SQLITE_OK && pgno<=pSeg->pgnoLast; pgno++){
5156 i64 iRowid = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
5157 int iNext = 0;
5158 u8 *aPg = 0;
5159
5160 pLeaf = fts5DataRead(p, iRowid);
5161 if( pLeaf==0 ) break;
5162 aPg = pLeaf->p;
5163
5164 iNext = fts5GetU16(&aPg[0]);
5165 if( iNext!=0 ){
5166 *pbLastInDoclist = 0;
5167 }
5168 if( iNext==0 && pLeaf->szLeaf!=pLeaf->nn ){
5169 fts5GetVarint32(&aPg[pLeaf->szLeaf], iNext);
5170 }
5171
5172 if( iNext==0 ){
5173 /* The page contains no terms or rowids. Replace it with an empty
5174 ** page and move on to the right-hand peer. */
5175 const u8 aEmpty[] = {0x00, 0x00, 0x00, 0x04};
5176 assert_nc( bDetailNone==0 || pLeaf->nn==4 );
5177 if( bDetailNone==0 ) fts5DataWrite(p, iRowid, aEmpty, sizeof(aEmpty));
5178 fts5DataRelease(pLeaf);
5179 pLeaf = 0;
5180 }else if( bDetailNone ){
5181 break;
5182 }else if( iNext>=pLeaf->szLeaf || pLeaf->nn<pLeaf->szLeaf || iNext<4 ){
5183 FTS5_CORRUPT_ROWID(p, iRowid);
5184 break;
5185 }else{
5186 int nShift = iNext - 4;
5187 int nPg;
5188
5189 int nIdx = 0;
5190 u8 *aIdx = 0;
5191
5192 /* Unless the current page footer is 0 bytes in size (in which case
5193 ** the new page footer will be as well), allocate and populate a
5194 ** buffer containing the new page footer. Set stack variables aIdx
5195 ** and nIdx accordingly. */
5196 if( pLeaf->nn>pLeaf->szLeaf ){
5197 int iFirst = 0;
5198 int i1 = pLeaf->szLeaf;
5199 int i2 = 0;
5200
5201 i1 += fts5GetVarint32(&aPg[i1], iFirst);
5202 if( iFirst<iNext ){
5203 FTS5_CORRUPT_ROWID(p, iRowid);
5204 break;
5205 }
5206 aIdx = sqlite3Fts5MallocZero(&p->rc, (pLeaf->nn-pLeaf->szLeaf)+2);
5207 if( aIdx==0 ) break;
5208 i2 = sqlite3Fts5PutVarint(aIdx, iFirst-nShift);
5209 if( i1<pLeaf->nn ){
5210 memcpy(&aIdx[i2], &aPg[i1], pLeaf->nn-i1);
5211 i2 += (pLeaf->nn-i1);
5212 }
5213 nIdx = i2;
5214 }
5215
5216 /* Modify the contents of buffer aPg[]. Set nPg to the new size
5217 ** in bytes. The new page is always smaller than the old. */
5218 nPg = pLeaf->szLeaf - nShift;
5219 memmove(&aPg[4], &aPg[4+nShift], nPg-4);
5220 fts5PutU16(&aPg[2], nPg);
5221 if( fts5GetU16(&aPg[0]) ) fts5PutU16(&aPg[0], 4);
5222 if( nIdx>0 ){
5223 memcpy(&aPg[nPg], aIdx, nIdx);
5224 nPg += nIdx;
5225 }
5226 sqlite3_free(aIdx);
5227
5228 /* Write the new page to disk and exit the loop */
5229 assert( nPg>4 || fts5GetU16(aPg)==0 );
5230 fts5DataWrite(p, iRowid, aPg, nPg);
5231 break;
5232 }
5233 }
5234 fts5DataRelease(pLeaf);
5235 }
5236
5237 /*
5238 ** Completely remove the entry that pSeg currently points to from
5239 ** the database.
5240 */
5241 static void fts5DoSecureDelete(
5242 Fts5Index *p,
5243 Fts5SegIter *pSeg
5244 ){
5245 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE);
5246 int iSegid = pSeg->pSeg->iSegid;
5247 u8 *aPg = pSeg->pLeaf->p;
5248 int nPg = pSeg->pLeaf->nn;
5249 int iPgIdx = pSeg->pLeaf->szLeaf; /* Offset of page footer */
5250
5251 u64 iDelta = 0;
5252 int iNextOff = 0;
5253 int iOff = 0;
5254 int nIdx = 0;
5255 u8 *aIdx = 0;
5256 int bLastInDoclist = 0;
5257 int iIdx = 0;
5258 int iStart = 0;
5259 int iDelKeyOff = 0; /* Offset of deleted key, if any */
5260
5261 nIdx = nPg-iPgIdx;
5262 aIdx = sqlite3Fts5MallocZero(&p->rc, ((i64)nIdx)+16);
5263 if( p->rc ) return;
5264 memcpy(aIdx, &aPg[iPgIdx], nIdx);
5265
5266 /* At this point segment iterator pSeg points to the entry
5267 ** this function should remove from the b-tree segment.
5268 **
5269 ** In detail=full or detail=column mode, pSeg->iLeafOffset is the
5270 ** offset of the first byte in the position-list for the entry to
5271 ** remove. Immediately before this comes two varints that will also
5272 ** need to be removed:
5273 **
5274 ** + the rowid or delta rowid value for the entry, and
5275 ** + the size of the position list in bytes.
5276 **
5277 ** Or, in detail=none mode, there is a single varint prior to
5278 ** pSeg->iLeafOffset - the rowid or delta rowid value.
5279 **
5280 ** This block sets the following variables:
5281 **
5282 ** iStart:
5283 ** The offset of the first byte of the rowid or delta-rowid
5284 ** value for the doclist entry being removed.
5285 **
5286 ** iDelta:
5287 ** The value of the rowid or delta-rowid value for the doclist
5288 ** entry being removed.
5289 **
5290 ** iNextOff:
5291 ** The offset of the next entry following the position list
5292 ** for the one being removed. If the position list for this
5293 ** entry overflows onto the next leaf page, this value will be
5294 ** greater than pLeaf->szLeaf.
5295 */
5296 {
5297 int iSOP; /* Start-Of-Position-list */
5298 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno ){
5299 iStart = pSeg->iTermLeafOffset;
5300 }else{
5301 iStart = fts5GetU16(&aPg[0]);
5302 }
5303 if( iStart>nPg ){
5304 FTS5_CORRUPT_IDX(p);
5305 sqlite3_free(aIdx);
5306 return;
5307 }
5308
5309 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
5310 assert_nc( iSOP<=pSeg->iLeafOffset );
5311
5312 if( bDetailNone ){
5313 while( iSOP<pSeg->iLeafOffset ){
5314 if( aPg[iSOP]==0x00 ) iSOP++;
5315 if( aPg[iSOP]==0x00 ) iSOP++;
5316 iStart = iSOP;
5317 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
5318 }
5319
5320 iNextOff = iSOP;
5321 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
5322 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
5323
5324 }else{
5325 int nPos = 0;
5326 iSOP += fts5GetVarint32(&aPg[iSOP], nPos);
5327 while( iSOP<pSeg->iLeafOffset ){
5328 iStart = iSOP + (nPos/2);
5329 iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta);
5330 iSOP += fts5GetVarint32(&aPg[iSOP], nPos);
5331 }
5332 assert_nc( iSOP==pSeg->iLeafOffset );
5333 iNextOff = iSOP + pSeg->nPos;
5334 }
5335 }
5336
5337 iOff = iStart;
5338
5339 /* If the position-list for the entry being removed flows over past
5340 ** the end of this page, delete the portion of the position-list on the
5341 ** next page and beyond.
5342 **
5343 ** Set variable bLastInDoclist to true if this entry happens
5344 ** to be the last rowid in the doclist for its term. */
5345 if( iNextOff>=iPgIdx ){
5346 int pgno = pSeg->iLeafPgno+1;
5347 fts5SecureDeleteOverflow(p, pSeg->pSeg, pgno, &bLastInDoclist);
5348 iNextOff = iPgIdx;
5349 }
5350
5351 if( pSeg->bDel==0 ){
5352 if( iNextOff!=iPgIdx ){
5353 /* Loop through the page-footer. If iNextOff (offset of the
5354 ** entry following the one we are removing) is equal to the
5355 ** offset of a key on this page, then the entry is the last
5356 ** in its doclist. */
5357 int iKeyOff = 0;
5358 for(iIdx=0; iIdx<nIdx; /* no-op */){
5359 u32 iVal = 0;
5360 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal);
5361 iKeyOff += iVal;
5362 if( iKeyOff==iNextOff ){
5363 bLastInDoclist = 1;
5364 }
5365 }
5366 }
5367
5368 /* If this is (a) the first rowid on a page and (b) is not followed by
5369 ** another position list on the same page, set the "first-rowid" field
5370 ** of the header to 0. */
5371 if( fts5GetU16(&aPg[0])==iStart && (bLastInDoclist || iNextOff==iPgIdx) ){
5372 fts5PutU16(&aPg[0], 0);
5373 }
5374 }
5375
5376 if( pSeg->bDel ){
5377 iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta);
5378 aPg[iOff++] = 0x01;
5379 }else if( bLastInDoclist==0 ){
5380 if( iNextOff!=iPgIdx ){
5381 u64 iNextDelta = 0;
5382 iNextOff += fts5GetVarint(&aPg[iNextOff], &iNextDelta);
5383 iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta + iNextDelta);
5384 }
5385 }else if(
5386 pSeg->iLeafPgno==pSeg->iTermLeafPgno
5387 && iStart==pSeg->iTermLeafOffset
5388 ){
5389 /* The entry being removed was the only position list in its
5390 ** doclist. Therefore the term needs to be removed as well. */
5391 int iKey = 0;
5392 int iKeyOff = 0;
5393
5394 /* Set iKeyOff to the offset of the term that will be removed - the
5395 ** last offset in the footer that is not greater than iStart. */
5396 for(iIdx=0; iIdx<nIdx; iKey++){
5397 u32 iVal = 0;
5398 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal);
5399 if( (iKeyOff+iVal)>(u32)iStart ) break;
5400 iKeyOff += iVal;
5401 }
5402 assert_nc( iKey>=1 );
5403
5404 /* Set iDelKeyOff to the value of the footer entry to remove from
5405 ** the page. */
5406 iDelKeyOff = iOff = iKeyOff;
5407
5408 if( iNextOff!=iPgIdx ){
5409 /* This is the only position-list associated with the term, and there
5410 ** is another term following it on this page. So the subsequent term
5411 ** needs to be moved to replace the term associated with the entry
5412 ** being removed. */
5413 u64 nPrefix = 0;
5414 u64 nSuffix = 0;
5415 u64 nPrefix2 = 0;
5416 u64 nSuffix2 = 0;
5417
5418 iDelKeyOff = iNextOff;
5419 iNextOff += fts5GetVarint(&aPg[iNextOff], &nPrefix2);
5420 iNextOff += fts5GetVarint(&aPg[iNextOff], &nSuffix2);
5421
5422 if( iKey!=1 ){
5423 iKeyOff += fts5GetVarint(&aPg[iKeyOff], &nPrefix);
5424 }
5425 iKeyOff += fts5GetVarint(&aPg[iKeyOff], &nSuffix);
5426
5427 nPrefix = MIN(nPrefix, nPrefix2);
5428 nSuffix = (nPrefix2 + nSuffix2) - nPrefix;
5429
5430 if( (iKeyOff+nSuffix)>(u64)iPgIdx || (iNextOff+nSuffix2)>(u64)iPgIdx ){
5431 FTS5_CORRUPT_IDX(p);
5432 }else{
5433 if( iKey!=1 ){
5434 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nPrefix);
5435 }
5436 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nSuffix);
5437 if( nPrefix2>(u64)pSeg->term.n ){
5438 FTS5_CORRUPT_IDX(p);
5439 }else if( nPrefix2>nPrefix ){
5440 memcpy(&aPg[iOff], &pSeg->term.p[nPrefix], nPrefix2-nPrefix);
5441 iOff += (nPrefix2-nPrefix);
5442 }
5443 memmove(&aPg[iOff], &aPg[iNextOff], nSuffix2);
5444 iOff += nSuffix2;
5445 iNextOff += nSuffix2;
5446 }
5447 }
5448 }else if( iStart==4 ){
5449 int iPgno;
5450
5451 assert_nc( pSeg->iLeafPgno>pSeg->iTermLeafPgno );
5452 /* The entry being removed may be the only position list in
5453 ** its doclist. */
5454 for(iPgno=pSeg->iLeafPgno-1; iPgno>pSeg->iTermLeafPgno; iPgno-- ){
5455 Fts5Data *pPg = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, iPgno));
5456 int bEmpty = (pPg && pPg->nn==4);
5457 fts5DataRelease(pPg);
5458 if( bEmpty==0 ) break;
5459 }
5460
5461 if( iPgno==pSeg->iTermLeafPgno ){
5462 i64 iId = FTS5_SEGMENT_ROWID(iSegid, pSeg->iTermLeafPgno);
5463 Fts5Data *pTerm = fts5DataRead(p, iId);
5464 if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){
5465 u8 *aTermIdx = &pTerm->p[pTerm->szLeaf];
5466 int nTermIdx = pTerm->nn - pTerm->szLeaf;
5467 int iTermIdx = 0;
5468 i64 iTermOff = 0;
5469
5470 while( 1 ){
5471 u32 iVal = 0;
5472 int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal);
5473 iTermOff += iVal;
5474 if( (iTermIdx+nByte)>=nTermIdx ) break;
5475 iTermIdx += nByte;
5476 }
5477 nTermIdx = iTermIdx;
5478
5479 if( iTermOff>pTerm->szLeaf ){
5480 FTS5_CORRUPT_IDX(p);
5481 }else{
5482 memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx);
5483 fts5PutU16(&pTerm->p[2], iTermOff);
5484 fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx);
5485 if( nTermIdx==0 ){
5486 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno);
5487 }
5488 }
5489 }
5490 fts5DataRelease(pTerm);
5491 }
5492 }
5493
5494 /* Assuming no error has occurred, this block does final edits to the
5495 ** leaf page before writing it back to disk. Input variables are:
5496 **
5497 ** nPg: Total initial size of leaf page.
5498 ** iPgIdx: Initial offset of page footer.
5499 **
5500 ** iOff: Offset to move data to
5501 ** iNextOff: Offset to move data from
5502 */
5503 if( p->rc==SQLITE_OK ){
5504 const int nMove = nPg - iNextOff; /* Number of bytes to move */
5505 int nShift = iNextOff - iOff; /* Distance to move them */
5506
5507 int iPrevKeyOut = 0;
5508 int iKeyIn = 0;
5509
5510 if( nMove>0 ){
5511 memmove(&aPg[iOff], &aPg[iNextOff], nMove);
5512 }
5513 iPgIdx -= nShift;
5514 nPg = iPgIdx;
5515 fts5PutU16(&aPg[2], iPgIdx);
5516
5517 for(iIdx=0; iIdx<nIdx; /* no-op */){
5518 u32 iVal = 0;
5519 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal);
5520 iKeyIn += iVal;
5521 if( iKeyIn!=iDelKeyOff ){
5522 int iKeyOut = (iKeyIn - (iKeyIn>iOff ? nShift : 0));
5523 nPg += sqlite3Fts5PutVarint(&aPg[nPg], iKeyOut - iPrevKeyOut);
5524 iPrevKeyOut = iKeyOut;
5525 }
5526 }
5527
5528 if( iPgIdx==nPg && nIdx>0 && pSeg->iLeafPgno!=1 ){
5529 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iLeafPgno);
5530 }
5531
5532 assert_nc( nPg>4 || fts5GetU16(aPg)==0 );
5533 fts5DataWrite(p, FTS5_SEGMENT_ROWID(iSegid,pSeg->iLeafPgno), aPg, nPg);
5534 }
5535 sqlite3_free(aIdx);
5536 }
5537
5538 /*
5539 ** This is called as part of flushing a delete to disk in 'secure-delete'
5540 ** mode. It edits the segments within the database described by argument
5541 ** pStruct to remove the entries for term zTerm, rowid iRowid.
5542 **
5543 ** Return SQLITE_OK if successful, or an SQLite error code if an error
5544 ** has occurred. Any error code is also stored in the Fts5Index handle.
5545 */
5546 static int fts5FlushSecureDelete(
5547 Fts5Index *p,
5548 Fts5Structure *pStruct,
5549 const char *zTerm,
5550 int nTerm,
5551 i64 iRowid
5552 ){
5553 const int f = FTS5INDEX_QUERY_SKIPHASH;
5554 Fts5Iter *pIter = 0; /* Used to find term instance */
5555
5556 /* If the version number has not been set to SECUREDELETE, do so now. */
5557 if( p->pConfig->iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE ){
5558 Fts5Config *pConfig = p->pConfig;
5559 sqlite3_stmt *pStmt = 0;
5560 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
5561 "REPLACE INTO %Q.'%q_config' VALUES ('version', %d)",
5562 pConfig->zDb, pConfig->zName, FTS5_CURRENT_VERSION_SECUREDELETE
5563 ));
5564 if( p->rc==SQLITE_OK ){
5565 int rc;
5566 sqlite3_step(pStmt);
5567 rc = sqlite3_finalize(pStmt);
5568 if( p->rc==SQLITE_OK ) p->rc = rc;
5569 pConfig->iCookie++;
5570 pConfig->iVersion = FTS5_CURRENT_VERSION_SECUREDELETE;
5571 }
5572 }
5573
5574 fts5MultiIterNew(p, pStruct, f, 0, (const u8*)zTerm, nTerm, -1, 0, &pIter);
5575 if( fts5MultiIterEof(p, pIter)==0 ){
5576 i64 iThis = fts5MultiIterRowid(pIter);
5577 if( iThis<iRowid ){
5578 fts5MultiIterNextFrom(p, pIter, iRowid);
5579 }
5580
5581 if( p->rc==SQLITE_OK
5582 && fts5MultiIterEof(p, pIter)==0
5583 && iRowid==fts5MultiIterRowid(pIter)
5584 ){
5585 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
5586 fts5DoSecureDelete(p, pSeg);
5587 }
5588 }
5589
5590 fts5MultiIterFree(pIter);
5591 return p->rc;
5592 }
5593
5594
5595 /*
5596 ** Flush the contents of in-memory hash table iHash to a new level-0
5597 ** segment on disk. Also update the corresponding structure record.
5598 **
5599 ** If an error occurs, set the Fts5Index.rc error code. If an error has
5600 ** already occurred, this function is a no-op.
5601 */
5602 static void fts5FlushOneHash(Fts5Index *p){
5603 Fts5Hash *pHash = p->pHash;
5604 Fts5Structure *pStruct;
5605 int iSegid;
5606 int pgnoLast = 0; /* Last leaf page number in segment */
5607
5608 /* Obtain a reference to the index structure and allocate a new segment-id
5609 ** for the new level-0 segment. */
5610 pStruct = fts5StructureRead(p);
5611 fts5StructureInvalidate(p);
5612
5613 if( sqlite3Fts5HashIsEmpty(pHash)==0 ){
5614 iSegid = fts5AllocateSegid(p, pStruct);
5615 if( iSegid ){
5616 const int pgsz = p->pConfig->pgsz;
5617 int eDetail = p->pConfig->eDetail;
5618 int bSecureDelete = p->pConfig->bSecureDelete;
5619 Fts5StructureSegment *pSeg; /* New segment within pStruct */
5620 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
5621 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
5622
5623 Fts5SegWriter writer;
5624 fts5WriteInit(p, &writer, iSegid);
5625
5626 pBuf = &writer.writer.buf;
5627 pPgidx = &writer.writer.pgidx;
5628
5629 /* fts5WriteInit() should have initialized the buffers to (most likely)
5630 ** the maximum space required. */
5631 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
5632 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
5633
5634 /* Begin scanning through hash table entries. This loop runs once for each
5635 ** term/doclist currently stored within the hash table. */
5636 if( p->rc==SQLITE_OK ){
5637 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
5638 }
5639 while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){
5640 const char *zTerm; /* Buffer containing term */
5641 int nTerm; /* Size of zTerm in bytes */
5642 const u8 *pDoclist; /* Pointer to doclist for this term */
5643 int nDoclist; /* Size of doclist in bytes */
5644
5645 /* Get the term and doclist for this entry. */
5646 sqlite3Fts5HashScanEntry(pHash, &zTerm, &nTerm, &pDoclist, &nDoclist);
5647 if( bSecureDelete==0 ){
5648 fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm);
5649 if( p->rc!=SQLITE_OK ) break;
5650 assert( writer.bFirstRowidInPage==0 );
5651 }
5652
5653 if( !bSecureDelete && pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
5654 /* The entire doclist will fit on the current leaf. */
5655 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
5656 }else{
5657 int bTermWritten = !bSecureDelete;
5658 i64 iRowid = 0;
5659 i64 iPrev = 0;
5660 int iOff = 0;
5661
5662 /* The entire doclist will not fit on this leaf. The following
5663 ** loop iterates through the poslists that make up the current
5664 ** doclist. */
5665 while( p->rc==SQLITE_OK && iOff<nDoclist ){
5666 u64 iDelta = 0;
5667 iOff += fts5GetVarint(&pDoclist[iOff], &iDelta);
5668 iRowid += iDelta;
5669
5670 /* If in secure delete mode, and if this entry in the poslist is
5671 ** in fact a delete, then edit the existing segments directly
5672 ** using fts5FlushSecureDelete(). */
5673 if( bSecureDelete ){
5674 if( eDetail==FTS5_DETAIL_NONE ){
5675 if( iOff<nDoclist && pDoclist[iOff]==0x00
5676 && !fts5FlushSecureDelete(p, pStruct, zTerm, nTerm, iRowid)
5677 ){
5678 iOff++;
5679 if( iOff<nDoclist && pDoclist[iOff]==0x00 ){
5680 iOff++;
5681 nDoclist = 0;
5682 }else{
5683 continue;
5684 }
5685 }
5686 }else if( (pDoclist[iOff] & 0x01)
5687 && !fts5FlushSecureDelete(p, pStruct, zTerm, nTerm, iRowid)
5688 ){
5689 if( p->rc!=SQLITE_OK || pDoclist[iOff]==0x01 ){
5690 iOff++;
5691 continue;
5692 }
5693 }
5694 }
5695
5696 if( p->rc==SQLITE_OK && bTermWritten==0 ){
5697 fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm);
5698 bTermWritten = 1;
5699 assert( p->rc!=SQLITE_OK || writer.bFirstRowidInPage==0 );
5700 }
5701
5702 if( writer.bFirstRowidInPage ){
5703 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
5704 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
5705 writer.bFirstRowidInPage = 0;
5706 fts5WriteDlidxAppend(p, &writer, iRowid);
5707 }else{
5708 u64 iRowidDelta = (u64)iRowid - (u64)iPrev;
5709 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowidDelta);
5710 }
5711 if( p->rc!=SQLITE_OK ) break;
5712 assert( pBuf->n<=pBuf->nSpace );
5713 iPrev = iRowid;
5714
5715 if( eDetail==FTS5_DETAIL_NONE ){
5716 if( iOff<nDoclist && pDoclist[iOff]==0 ){
5717 pBuf->p[pBuf->n++] = 0;
5718 iOff++;
5719 if( iOff<nDoclist && pDoclist[iOff]==0 ){
5720 pBuf->p[pBuf->n++] = 0;
5721 iOff++;
5722 }
5723 }
5724 if( (pBuf->n + pPgidx->n)>=pgsz ){
5725 fts5WriteFlushLeaf(p, &writer);
5726 }
5727 }else{
5728 int bDel = 0;
5729 int nPos = 0;
5730 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDel);
5731 if( bDel && bSecureDelete ){
5732 fts5BufferAppendVarint(&p->rc, pBuf, nPos*2);
5733 iOff += nCopy;
5734 nCopy = nPos;
5735 }else{
5736 nCopy += nPos;
5737 }
5738 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
5739 /* The entire poslist will fit on the current leaf. So copy
5740 ** it in one go. */
5741 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
5742 }else{
5743 /* The entire poslist will not fit on this leaf. So it needs
5744 ** to be broken into sections. The only qualification being
5745 ** that each varint must be stored contiguously. */
5746 const u8 *pPoslist = &pDoclist[iOff];
5747 int iPos = 0;
5748 while( p->rc==SQLITE_OK ){
5749 int nSpace = pgsz - pBuf->n - pPgidx->n;
5750 int n = 0;
5751 if( (nCopy - iPos)<=nSpace ){
5752 n = nCopy - iPos;
5753 }else{
5754 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
5755 }
5756 assert( n>0 );
5757 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
5758 iPos += n;
5759 if( (pBuf->n + pPgidx->n)>=pgsz ){
5760 fts5WriteFlushLeaf(p, &writer);
5761 }
5762 if( iPos>=nCopy ) break;
5763 }
5764 }
5765 iOff += nCopy;
5766 }
5767 }
5768 }
5769
5770 /* TODO2: Doclist terminator written here. */
5771 /* pBuf->p[pBuf->n++] = '\0'; */
5772 assert( pBuf->n<=pBuf->nSpace );
5773 if( p->rc==SQLITE_OK ) sqlite3Fts5HashScanNext(pHash);
5774 }
5775 fts5WriteFinish(p, &writer, &pgnoLast);
5776
5777 assert( p->rc!=SQLITE_OK || bSecureDelete || pgnoLast>0 );
5778 if( pgnoLast>0 ){
5779 /* Update the Fts5Structure. It is written back to the database by the
5780 ** fts5StructureRelease() call below. */
5781 if( pStruct->nLevel==0 ){
5782 fts5StructureAddLevel(&p->rc, &pStruct);
5783 }
5784 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
5785 if( p->rc==SQLITE_OK ){
5786 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
5787 pSeg->iSegid = iSegid;
5788 pSeg->pgnoFirst = 1;
5789 pSeg->pgnoLast = pgnoLast;
5790 if( pStruct->nOriginCntr>0 ){
5791 pSeg->iOrigin1 = pStruct->nOriginCntr;
5792 pSeg->iOrigin2 = pStruct->nOriginCntr;
5793 pSeg->nEntry = p->nPendingRow;
5794 pStruct->nOriginCntr++;
5795 }
5796 pStruct->nSegment++;
5797 }
5798 fts5StructurePromote(p, 0, pStruct);
5799 }
5800 }
5801 }
5802
5803 fts5IndexAutomerge(p, &pStruct, pgnoLast + p->nContentlessDelete);
5804 fts5IndexCrisismerge(p, &pStruct);
5805 fts5StructureWrite(p, pStruct);
5806 fts5StructureRelease(pStruct);
5807 }
5808
5809 /*
5810 ** Flush any data stored in the in-memory hash tables to the database.
5811 */
5812 static void fts5IndexFlush(Fts5Index *p){
5813 /* Unless it is empty, flush the hash table to disk */
5814 if( p->flushRc ){
5815 p->rc = p->flushRc;
5816 return;
5817 }
5818 if( p->nPendingData || p->nContentlessDelete ){
5819 assert( p->pHash );
5820 fts5FlushOneHash(p);
5821 if( p->rc==SQLITE_OK ){
5822 sqlite3Fts5HashClear(p->pHash);
5823 p->nPendingData = 0;
5824 p->nPendingRow = 0;
5825 p->nContentlessDelete = 0;
5826 }else if( p->nPendingData || p->nContentlessDelete ){
5827 p->flushRc = p->rc;
5828 }
5829 }
5830 }
5831
5832 static Fts5Structure *fts5IndexOptimizeStruct(
5833 Fts5Index *p,
5834 Fts5Structure *pStruct
5835 ){
5836 Fts5Structure *pNew = 0;
5837 sqlite3_int64 nByte = SZ_FTS5STRUCTURE(1);
5838 int nSeg = pStruct->nSegment;
5839 int i;
5840
5841 /* Figure out if this structure requires optimization. A structure does
5842 ** not require optimization if either:
5843 **
5844 ** 1. it consists of fewer than two segments, or
5845 ** 2. all segments are on the same level, or
5846 ** 3. all segments except one are currently inputs to a merge operation.
5847 **
5848 ** In the first case, if there are no tombstone hash pages, return NULL. In
5849 ** the second, increment the ref-count on *pStruct and return a copy of the
5850 ** pointer to it.
5851 */
5852 if( nSeg==0 ) return 0;
5853 for(i=0; i<pStruct->nLevel; i++){
5854 int nThis = pStruct->aLevel[i].nSeg;
5855 int nMerge = pStruct->aLevel[i].nMerge;
5856 if( nThis>0 && (nThis==nSeg || (nThis==nSeg-1 && nMerge==nThis)) ){
5857 if( nSeg==1 && nThis==1 && pStruct->aLevel[i].aSeg[0].nPgTombstone==0 ){
5858 return 0;
5859 }
5860 fts5StructureRef(pStruct);
5861 return pStruct;
5862 }
5863 assert( pStruct->aLevel[i].nMerge<=nThis );
5864 }
5865
5866 nByte += (((i64)pStruct->nLevel)+1) * sizeof(Fts5StructureLevel);
5867 assert( nByte==(i64)SZ_FTS5STRUCTURE(pStruct->nLevel+2) );
5868 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
5869
5870 if( pNew ){
5871 Fts5StructureLevel *pLvl;
5872 nByte = nSeg * sizeof(Fts5StructureSegment);
5873 pNew->nLevel = MIN(pStruct->nLevel+1, FTS5_MAX_LEVEL);
5874 pNew->nRef = 1;
5875 pNew->nWriteCounter = pStruct->nWriteCounter;
5876 pNew->nOriginCntr = pStruct->nOriginCntr;
5877 pLvl = &pNew->aLevel[pNew->nLevel-1];
5878 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
5879 if( pLvl->aSeg ){
5880 int iLvl, iSeg;
5881 int iSegOut = 0;
5882 /* Iterate through all segments, from oldest to newest. Add them to
5883 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
5884 ** segment in the data structure. */
5885 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
5886 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
5887 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
5888 iSegOut++;
5889 }
5890 }
5891 pNew->nSegment = pLvl->nSeg = nSeg;
5892 }else{
5893 sqlite3_free(pNew);
5894 pNew = 0;
5895 }
5896 }
5897
5898 return pNew;
5899 }
5900
5901 int sqlite3Fts5IndexOptimize(Fts5Index *p){
5902 Fts5Structure *pStruct;
5903 Fts5Structure *pNew = 0;
5904
5905 assert( p->rc==SQLITE_OK );
5906 fts5IndexFlush(p);
5907 assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 );
5908 pStruct = fts5StructureRead(p);
5909 assert( p->rc!=SQLITE_OK || pStruct!=0 );
5910 fts5StructureInvalidate(p);
5911
5912 if( pStruct ){
5913 pNew = fts5IndexOptimizeStruct(p, pStruct);
5914 }
5915 fts5StructureRelease(pStruct);
5916
5917 assert( pNew==0 || pNew->nSegment>0 );
5918 if( pNew ){
5919 int iLvl;
5920 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
5921 while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
5922 int nRem = FTS5_OPT_WORK_UNIT;
5923 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
5924 }
5925
5926 fts5StructureWrite(p, pNew);
5927 fts5StructureRelease(pNew);
5928 }
5929
5930 return fts5IndexReturn(p);
5931 }
5932
5933 /*
5934 ** This is called to implement the special "VALUES('merge', $nMerge)"
5935 ** INSERT command.
5936 */
5937 int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
5938 Fts5Structure *pStruct = 0;
5939
5940 fts5IndexFlush(p);
5941 pStruct = fts5StructureRead(p);
5942 if( pStruct ){
5943 int nMin = p->pConfig->nUsermerge;
5944 fts5StructureInvalidate(p);
5945 if( nMerge<0 ){
5946 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
5947 fts5StructureRelease(pStruct);
5948 pStruct = pNew;
5949 nMin = 1;
5950 nMerge = (nMerge==SMALLEST_INT32 ? LARGEST_INT32 : (nMerge*-1));
5951 }
5952 if( pStruct && pStruct->nLevel ){
5953 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
5954 fts5StructureWrite(p, pStruct);
5955 }
5956 }
5957 fts5StructureRelease(pStruct);
5958 }
5959 return fts5IndexReturn(p);
5960 }
5961
5962 static void fts5AppendRowid(
5963 Fts5Index *p,
5964 u64 iDelta,
5965 Fts5Iter *pUnused,
5966 Fts5Buffer *pBuf
5967 ){
5968 UNUSED_PARAM(pUnused);
5969 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
5970 }
5971
5972 static void fts5AppendPoslist(
5973 Fts5Index *p,
5974 u64 iDelta,
5975 Fts5Iter *pMulti,
5976 Fts5Buffer *pBuf
5977 ){
5978 int nData = pMulti->base.nData;
5979 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
5980 assert( nData>0 );
5981 if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){
5982 fts5BufferSafeAppendVarint(pBuf, iDelta);
5983 fts5BufferSafeAppendVarint(pBuf, nData*2);
5984 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
5985 memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING);
5986 }
5987 }
5988
5989
5990 static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
5991 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
5992
5993 assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) );
5994 if( p>=pIter->aEof ){
5995 pIter->aPoslist = 0;
5996 }else{
5997 i64 iDelta;
5998
5999 p += fts5GetVarint(p, (u64*)&iDelta);
6000 pIter->iRowid += iDelta;
6001
6002 /* Read position list size */
6003 if( p[0] & 0x80 ){
6004 int nPos;
6005 pIter->nSize = fts5GetVarint32(p, nPos);
6006 pIter->nPoslist = (nPos>>1);
6007 }else{
6008 pIter->nPoslist = ((int)(p[0])) >> 1;
6009 pIter->nSize = 1;
6010 }
6011
6012 pIter->aPoslist = p;
6013 if( &pIter->aPoslist[pIter->nPoslist]>pIter->aEof ){
6014 pIter->aPoslist = 0;
6015 }
6016 }
6017 }
6018
6019 static void fts5DoclistIterInit(
6020 Fts5Buffer *pBuf,
6021 Fts5DoclistIter *pIter
6022 ){
6023 memset(pIter, 0, sizeof(*pIter));
6024 if( pBuf->n>0 ){
6025 pIter->aPoslist = pBuf->p;
6026 pIter->aEof = &pBuf->p[pBuf->n];
6027 fts5DoclistIterNext(pIter);
6028 }
6029 }
6030
6031 #if 0
6032 /*
6033 ** Append a doclist to buffer pBuf.
6034 **
6035 ** This function assumes that space within the buffer has already been
6036 ** allocated.
6037 */
6038 static void fts5MergeAppendDocid(
6039 Fts5Buffer *pBuf, /* Buffer to write to */
6040 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */
6041 i64 iRowid /* Rowid to append */
6042 ){
6043 assert( pBuf->n!=0 || (*piLastRowid)==0 );
6044 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
6045 *piLastRowid = iRowid;
6046 }
6047 #endif
6048
6049 #define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
6050 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
6051 fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)); \
6052 (iLastRowid) = (iRowid); \
6053 }
6054
6055 /*
6056 ** Swap the contents of buffer *p1 with that of *p2.
6057 */
6058 static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
6059 Fts5Buffer tmp = *p1;
6060 *p1 = *p2;
6061 *p2 = tmp;
6062 }
6063
6064 static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
6065 int i = *piOff;
6066 if( i>=pBuf->n ){
6067 *piOff = -1;
6068 }else{
6069 u64 iVal;
6070 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
6071 *piRowid += iVal;
6072 }
6073 }
6074
6075 /*
6076 ** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
6077 ** In this case the buffers consist of a delta-encoded list of rowids only.
6078 */
6079 static void fts5MergeRowidLists(
6080 Fts5Index *p, /* FTS5 backend object */
6081 Fts5Buffer *p1, /* First list to merge */
6082 int nBuf, /* Number of entries in apBuf[] */
6083 Fts5Buffer *aBuf /* Array of other lists to merge into p1 */
6084 ){
6085 int i1 = 0;
6086 int i2 = 0;
6087 i64 iRowid1 = 0;
6088 i64 iRowid2 = 0;
6089 i64 iOut = 0;
6090 Fts5Buffer *p2 = &aBuf[0];
6091 Fts5Buffer out;
6092
6093 (void)nBuf;
6094 memset(&out, 0, sizeof(out));
6095 assert( nBuf==1 );
6096 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
6097 if( p->rc ) return;
6098
6099 fts5NextRowid(p1, &i1, &iRowid1);
6100 fts5NextRowid(p2, &i2, &iRowid2);
6101 while( i1>=0 || i2>=0 ){
6102 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
6103 assert( iOut==0 || iRowid1>iOut );
6104 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
6105 iOut = iRowid1;
6106 fts5NextRowid(p1, &i1, &iRowid1);
6107 }else{
6108 assert( iOut==0 || iRowid2>iOut );
6109 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
6110 iOut = iRowid2;
6111 if( i1>=0 && iRowid1==iRowid2 ){
6112 fts5NextRowid(p1, &i1, &iRowid1);
6113 }
6114 fts5NextRowid(p2, &i2, &iRowid2);
6115 }
6116 }
6117
6118 fts5BufferSwap(&out, p1);
6119 fts5BufferFree(&out);
6120 }
6121
6122 typedef struct PrefixMerger PrefixMerger;
6123 struct PrefixMerger {
6124 Fts5DoclistIter iter; /* Doclist iterator */
6125 i64 iPos; /* For iterating through a position list */
6126 int iOff;
6127 u8 *aPos;
6128 PrefixMerger *pNext; /* Next in docid/poslist order */
6129 };
6130
6131 static void fts5PrefixMergerInsertByRowid(
6132 PrefixMerger **ppHead,
6133 PrefixMerger *p
6134 ){
6135 if( p->iter.aPoslist ){
6136 PrefixMerger **pp = ppHead;
6137 while( *pp && p->iter.iRowid>(*pp)->iter.iRowid ){
6138 pp = &(*pp)->pNext;
6139 }
6140 p->pNext = *pp;
6141 *pp = p;
6142 }
6143 }
6144
6145 static void fts5PrefixMergerInsertByPosition(
6146 PrefixMerger **ppHead,
6147 PrefixMerger *p
6148 ){
6149 if( p->iPos>=0 ){
6150 PrefixMerger **pp = ppHead;
6151 while( *pp && p->iPos>(*pp)->iPos ){
6152 pp = &(*pp)->pNext;
6153 }
6154 p->pNext = *pp;
6155 *pp = p;
6156 }
6157 }
6158
6159
6160 /*
6161 ** Array aBuf[] contains nBuf doclists. These are all merged in with the
6162 ** doclist in buffer p1.
6163 */
6164 static void fts5MergePrefixLists(
6165 Fts5Index *p, /* FTS5 backend object */
6166 Fts5Buffer *p1, /* First list to merge */
6167 int nBuf, /* Number of buffers in array aBuf[] */
6168 Fts5Buffer *aBuf /* Other lists to merge in */
6169 ){
6170 #define fts5PrefixMergerNextPosition(p) \
6171 sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&(p)->iOff,&(p)->iPos)
6172 #define FTS5_MERGE_NLIST 16
6173 PrefixMerger aMerger[FTS5_MERGE_NLIST];
6174 PrefixMerger *pHead = 0;
6175 int i;
6176 int nOut = 0;
6177 Fts5Buffer out = {0, 0, 0};
6178 Fts5Buffer tmp = {0, 0, 0};
6179 i64 iLastRowid = 0;
6180
6181 /* Initialize a doclist-iterator for each input buffer. Arrange them in
6182 ** a linked-list starting at pHead in ascending order of rowid. Avoid
6183 ** linking any iterators already at EOF into the linked list at all. */
6184 assert( nBuf+1<=(int)(sizeof(aMerger)/sizeof(aMerger[0])) );
6185 memset(aMerger, 0, sizeof(PrefixMerger)*(nBuf+1));
6186 pHead = &aMerger[nBuf];
6187 fts5DoclistIterInit(p1, &pHead->iter);
6188 for(i=0; i<nBuf; i++){
6189 fts5DoclistIterInit(&aBuf[i], &aMerger[i].iter);
6190 fts5PrefixMergerInsertByRowid(&pHead, &aMerger[i]);
6191 nOut += aBuf[i].n;
6192 }
6193 if( nOut==0 ) return;
6194 nOut += p1->n + 9 + 10*nBuf;
6195
6196 /* The maximum size of the output is equal to the sum of the
6197 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
6198 ** first rowid in one input is a large negative number, and the first in
6199 ** the other a non-negative number, the delta for the non-negative
6200 ** number will be larger on disk than the literal integer value
6201 ** was.
6202 **
6203 ** Or, if the input position-lists are corrupt, then the output might
6204 ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1
6205 ** (the value PoslistNext64() uses for EOF) as a position and appending
6206 ** it to the output. This can happen at most once for each input
6207 ** position-list, hence (nBuf+1) 10 byte paddings. */
6208 if( sqlite3Fts5BufferSize(&p->rc, &out, nOut) ) return;
6209
6210 while( pHead ){
6211 fts5MergeAppendDocid(&out, iLastRowid, pHead->iter.iRowid);
6212
6213 if( pHead->pNext && iLastRowid==pHead->pNext->iter.iRowid ){
6214 /* Merge data from two or more poslists */
6215 i64 iPrev = 0;
6216 int nTmp = FTS5_DATA_ZERO_PADDING;
6217 int nMerge = 0;
6218 PrefixMerger *pSave = pHead;
6219 PrefixMerger *pThis = 0;
6220 int nTail = 0;
6221
6222 pHead = 0;
6223 while( pSave && pSave->iter.iRowid==iLastRowid ){
6224 PrefixMerger *pNext = pSave->pNext;
6225 pSave->iOff = 0;
6226 pSave->iPos = 0;
6227 pSave->aPos = &pSave->iter.aPoslist[pSave->iter.nSize];
6228 fts5PrefixMergerNextPosition(pSave);
6229 nTmp += pSave->iter.nPoslist + 10;
6230 nMerge++;
6231 fts5PrefixMergerInsertByPosition(&pHead, pSave);
6232 pSave = pNext;
6233 }
6234
6235 if( pHead==0 || pHead->pNext==0 ){
6236 FTS5_CORRUPT_IDX(p);
6237 break;
6238 }
6239
6240 /* See the earlier comment in this function for an explanation of why
6241 ** corrupt input position lists might cause the output to consume
6242 ** at most nMerge*10 bytes of unexpected space. */
6243 if( sqlite3Fts5BufferSize(&p->rc, &tmp, nTmp+nMerge*10) ){
6244 break;
6245 }
6246 fts5BufferZero(&tmp);
6247
6248 pThis = pHead;
6249 pHead = pThis->pNext;
6250 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
6251 fts5PrefixMergerNextPosition(pThis);
6252 fts5PrefixMergerInsertByPosition(&pHead, pThis);
6253
6254 while( pHead->pNext ){
6255 pThis = pHead;
6256 if( pThis->iPos!=iPrev ){
6257 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
6258 }
6259 fts5PrefixMergerNextPosition(pThis);
6260 pHead = pThis->pNext;
6261 fts5PrefixMergerInsertByPosition(&pHead, pThis);
6262 }
6263
6264 if( pHead->iPos!=iPrev ){
6265 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pHead->iPos);
6266 }
6267 nTail = pHead->iter.nPoslist - pHead->iOff;
6268
6269 /* WRITEPOSLISTSIZE */
6270 assert_nc( tmp.n+nTail<=nTmp );
6271 assert( tmp.n+nTail<=nTmp+nMerge*10 );
6272 if( tmp.n+nTail>nTmp-FTS5_DATA_ZERO_PADDING ){
6273 if( p->rc==SQLITE_OK ) FTS5_CORRUPT_IDX(p);
6274 break;
6275 }
6276 fts5BufferSafeAppendVarint(&out, (tmp.n+nTail) * 2);
6277 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
6278 if( nTail>0 ){
6279 fts5BufferSafeAppendBlob(&out, &pHead->aPos[pHead->iOff], nTail);
6280 }
6281
6282 pHead = pSave;
6283 for(i=0; i<nBuf+1; i++){
6284 PrefixMerger *pX = &aMerger[i];
6285 if( pX->iter.aPoslist && pX->iter.iRowid==iLastRowid ){
6286 fts5DoclistIterNext(&pX->iter);
6287 fts5PrefixMergerInsertByRowid(&pHead, pX);
6288 }
6289 }
6290
6291 }else{
6292 /* Copy poslist from pHead to output */
6293 PrefixMerger *pThis = pHead;
6294 Fts5DoclistIter *pI = &pThis->iter;
6295 fts5BufferSafeAppendBlob(&out, pI->aPoslist, pI->nPoslist+pI->nSize);
6296 fts5DoclistIterNext(pI);
6297 pHead = pThis->pNext;
6298 fts5PrefixMergerInsertByRowid(&pHead, pThis);
6299 }
6300 }
6301
6302 fts5BufferFree(p1);
6303 fts5BufferFree(&tmp);
6304 memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING);
6305 *p1 = out;
6306 }
6307
6308
6309 /*
6310 ** Iterate through a range of entries in the FTS index, invoking the xVisit
6311 ** callback for each of them.
6312 **
6313 ** Parameter pToken points to an nToken buffer containing an FTS index term
6314 ** (i.e. a document term with the preceding 1 byte index identifier -
6315 ** FTS5_MAIN_PREFIX or similar). If bPrefix is true, then the call visits
6316 ** all entries for terms that have pToken/nToken as a prefix. If bPrefix
6317 ** is false, then only entries with pToken/nToken as the entire key are
6318 ** visited.
6319 **
6320 ** If the current table is a tokendata=1 table, then if bPrefix is true then
6321 ** each index term is treated separately. However, if bPrefix is false, then
6322 ** all index terms corresponding to pToken/nToken are collapsed into a single
6323 ** term before the callback is invoked.
6324 **
6325 ** The callback invoked for each entry visited is specified by paramter xVisit.
6326 ** Each time it is invoked, it is passed a pointer to the Fts5Index object,
6327 ** a copy of the 7th paramter to this function (pCtx) and a pointer to the
6328 ** iterator that indicates the current entry. If the current entry is the
6329 ** first with a new term (i.e. different from that of the previous entry,
6330 ** including the very first term), then the final two parameters are passed
6331 ** a pointer to the term and its size in bytes, respectively. If the current
6332 ** entry is not the first associated with its term, these two parameters
6333 ** are passed 0.
6334 **
6335 ** If parameter pColset is not NULL, then it is used to filter entries before
6336 ** the callback is invoked.
6337 */
6338 static int fts5VisitEntries(
6339 Fts5Index *p, /* Fts5 index object */
6340 Fts5Colset *pColset, /* Columns filter to apply, or NULL */
6341 u8 *pToken, /* Buffer containing token */
6342 int nToken, /* Size of buffer pToken in bytes */
6343 int bPrefix, /* True for a prefix scan */
6344 void (*xVisit)(Fts5Index*, void *pCtx, Fts5Iter *pIter, const u8*, int),
6345 void *pCtx /* Passed as second argument to xVisit() */
6346 ){
6347 const int flags = (bPrefix ? FTS5INDEX_QUERY_SCAN : 0)
6348 | FTS5INDEX_QUERY_SKIPEMPTY
6349 | FTS5INDEX_QUERY_NOOUTPUT;
6350 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
6351 int bNewTerm = 1;
6352 Fts5Structure *pStruct = fts5StructureRead(p);
6353
6354 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
6355 fts5IterSetOutputCb(&p->rc, p1);
6356 for( /* no-op */ ;
6357 fts5MultiIterEof(p, p1)==0;
6358 fts5MultiIterNext2(p, p1, &bNewTerm)
6359 ){
6360 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
6361 int nNew = 0;
6362 const u8 *pNew = 0;
6363
6364 p1->xSetOutputs(p1, pSeg);
6365 if( p->rc ) break;
6366
6367 if( bNewTerm ){
6368 nNew = pSeg->term.n;
6369 pNew = pSeg->term.p;
6370 if( nNew<nToken || memcmp(pToken, pNew, nToken) ) break;
6371 }
6372
6373 xVisit(p, pCtx, p1, pNew, nNew);
6374 }
6375 fts5MultiIterFree(p1);
6376
6377 fts5StructureRelease(pStruct);
6378 return p->rc;
6379 }
6380
6381
6382 /*
6383 ** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an
6384 ** array of these for each row it visits (so all iRowid fields are the same).
6385 ** Or, for an iterator used by an "ORDER BY rank" query, it accumulates an
6386 ** array of these for the entire query (in which case iRowid fields may take
6387 ** a variety of values).
6388 **
6389 ** Each instance in the array indicates the iterator (and therefore term)
6390 ** associated with position iPos of rowid iRowid. This is used by the
6391 ** xInstToken() API.
6392 **
6393 ** iRowid:
6394 ** Rowid for the current entry.
6395 **
6396 ** iPos:
6397 ** Position of current entry within row. In the usual ((iCol<<32)+iOff)
6398 ** format (e.g. see macros FTS5_POS2COLUMN() and FTS5_POS2OFFSET()).
6399 **
6400 ** iIter:
6401 ** If the Fts5TokenDataIter iterator that the entry is part of is
6402 ** actually an iterator (i.e. with nIter>0, not just a container for
6403 ** Fts5TokenDataMap structures), then this variable is an index into
6404 ** the apIter[] array. The corresponding term is that which the iterator
6405 ** at apIter[iIter] currently points to.
6406 **
6407 ** Or, if the Fts5TokenDataIter iterator is just a container object
6408 ** (nIter==0), then iIter is an index into the term.p[] buffer where
6409 ** the term is stored.
6410 **
6411 ** nByte:
6412 ** In the case where iIter is an index into term.p[], this variable
6413 ** is the size of the term in bytes. If iIter is an index into apIter[],
6414 ** this variable is unused.
6415 */
6416 struct Fts5TokenDataMap {
6417 i64 iRowid; /* Row this token is located in */
6418 i64 iPos; /* Position of token */
6419 int iIter; /* Iterator token was read from */
6420 int nByte; /* Length of token in bytes (or 0) */
6421 };
6422
6423 /*
6424 ** An object used to supplement Fts5Iter for tokendata=1 iterators.
6425 **
6426 ** This object serves two purposes. The first is as a container for an array
6427 ** of Fts5TokenDataMap structures, which are used to find the token required
6428 ** when the xInstToken() API is used. This is done by the nMapAlloc, nMap and
6429 ** aMap[] variables.
6430 */
6431 struct Fts5TokenDataIter {
6432 i64 nMapAlloc; /* Allocated size of aMap[] in entries */
6433 i64 nMap; /* Number of valid entries in aMap[] */
6434 Fts5TokenDataMap *aMap; /* Array of (rowid+pos -> token) mappings */
6435
6436 /* The following are used for prefix-queries only. */
6437 Fts5Buffer terms;
6438
6439 /* The following are used for other full-token tokendata queries only. */
6440 i64 nIter;
6441 i64 nIterAlloc;
6442 Fts5PoslistReader *aPoslistReader;
6443 int *aPoslistToIter;
6444 Fts5Iter *apIter[FLEXARRAY];
6445 };
6446
6447 /* Size in bytes of an Fts5TokenDataIter object holding up to N iterators */
6448 #define SZ_FTS5TOKENDATAITER(N) \
6449 (offsetof(Fts5TokenDataIter,apIter) + (N)*sizeof(Fts5Iter))
6450
6451 /*
6452 ** The two input arrays - a1[] and a2[] - are in sorted order. This function
6453 ** merges the two arrays together and writes the result to output array
6454 ** aOut[]. aOut[] is guaranteed to be large enough to hold the result.
6455 **
6456 ** Duplicate entries are copied into the output. So the size of the output
6457 ** array is always (n1+n2) entries.
6458 */
6459 static void fts5TokendataMerge(
6460 Fts5TokenDataMap *a1, int n1, /* Input array 1 */
6461 Fts5TokenDataMap *a2, int n2, /* Input array 2 */
6462 Fts5TokenDataMap *aOut /* Output array */
6463 ){
6464 int i1 = 0;
6465 int i2 = 0;
6466
6467 assert( n1>=0 && n2>=0 );
6468 while( i1<n1 || i2<n2 ){
6469 Fts5TokenDataMap *pOut = &aOut[i1+i2];
6470 if( i2>=n2 || (i1<n1 && (
6471 a1[i1].iRowid<a2[i2].iRowid
6472 || (a1[i1].iRowid==a2[i2].iRowid && a1[i1].iPos<=a2[i2].iPos)
6473 ))){
6474 memcpy(pOut, &a1[i1], sizeof(Fts5TokenDataMap));
6475 i1++;
6476 }else{
6477 memcpy(pOut, &a2[i2], sizeof(Fts5TokenDataMap));
6478 i2++;
6479 }
6480 }
6481 }
6482
6483
6484 /*
6485 ** Append a mapping to the token-map belonging to object pT.
6486 */
6487 static void fts5TokendataIterAppendMap(
6488 Fts5Index *p,
6489 Fts5TokenDataIter *pT,
6490 int iIter,
6491 int nByte,
6492 i64 iRowid,
6493 i64 iPos
6494 ){
6495 if( p->rc==SQLITE_OK ){
6496 if( pT->nMap==pT->nMapAlloc ){
6497 i64 nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64;
6498 i64 nAlloc = nNew * sizeof(Fts5TokenDataMap);
6499 Fts5TokenDataMap *aNew;
6500
6501 aNew = (Fts5TokenDataMap*)sqlite3_realloc64(pT->aMap, nAlloc);
6502 if( aNew==0 ){
6503 p->rc = SQLITE_NOMEM;
6504 return;
6505 }
6506
6507 pT->aMap = aNew;
6508 pT->nMapAlloc = nNew;
6509 }
6510
6511 pT->aMap[pT->nMap].iRowid = iRowid;
6512 pT->aMap[pT->nMap].iPos = iPos;
6513 pT->aMap[pT->nMap].iIter = iIter;
6514 pT->aMap[pT->nMap].nByte = nByte;
6515 pT->nMap++;
6516 }
6517 }
6518
6519 /*
6520 ** Sort the contents of the pT->aMap[] array.
6521 **
6522 ** The sorting algorithm requires a malloc(). If this fails, an error code
6523 ** is left in Fts5Index.rc before returning.
6524 */
6525 static void fts5TokendataIterSortMap(Fts5Index *p, Fts5TokenDataIter *pT){
6526 Fts5TokenDataMap *aTmp = 0;
6527 i64 nByte = pT->nMap * sizeof(Fts5TokenDataMap);
6528
6529 aTmp = (Fts5TokenDataMap*)sqlite3Fts5MallocZero(&p->rc, nByte);
6530 if( aTmp ){
6531 Fts5TokenDataMap *a1 = pT->aMap;
6532 Fts5TokenDataMap *a2 = aTmp;
6533 i64 nHalf;
6534
6535 for(nHalf=1; nHalf<pT->nMap; nHalf=nHalf*2){
6536 int i1;
6537 for(i1=0; i1<pT->nMap; i1+=(nHalf*2)){
6538 int n1 = MIN(nHalf, pT->nMap-i1);
6539 int n2 = MIN(nHalf, pT->nMap-i1-n1);
6540 fts5TokendataMerge(&a1[i1], n1, &a1[i1+n1], n2, &a2[i1]);
6541 }
6542 SWAPVAL(Fts5TokenDataMap*, a1, a2);
6543 }
6544
6545 if( a1!=pT->aMap ){
6546 memcpy(pT->aMap, a1, pT->nMap*sizeof(Fts5TokenDataMap));
6547 }
6548 sqlite3_free(aTmp);
6549
6550 #ifdef SQLITE_DEBUG
6551 {
6552 int ii;
6553 for(ii=1; ii<pT->nMap; ii++){
6554 Fts5TokenDataMap *p1 = &pT->aMap[ii-1];
6555 Fts5TokenDataMap *p2 = &pT->aMap[ii];
6556 assert( p1->iRowid<p2->iRowid
6557 || (p1->iRowid==p2->iRowid && p1->iPos<=p2->iPos)
6558 );
6559 }
6560 }
6561 #endif
6562 }
6563 }
6564
6565 /*
6566 ** Delete an Fts5TokenDataIter structure and its contents.
6567 */
6568 static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){
6569 if( pSet ){
6570 int ii;
6571 for(ii=0; ii<pSet->nIter; ii++){
6572 fts5MultiIterFree(pSet->apIter[ii]);
6573 }
6574 fts5BufferFree(&pSet->terms);
6575 sqlite3_free(pSet->aPoslistReader);
6576 sqlite3_free(pSet->aMap);
6577 sqlite3_free(pSet);
6578 }
6579 }
6580
6581
6582 /*
6583 ** fts5VisitEntries() context object used by fts5SetupPrefixIterTokendata()
6584 ** to pass data to prefixIterSetupTokendataCb().
6585 */
6586 typedef struct TokendataSetupCtx TokendataSetupCtx;
6587 struct TokendataSetupCtx {
6588 Fts5TokenDataIter *pT; /* Object being populated with mappings */
6589 int iTermOff; /* Offset of current term in terms.p[] */
6590 int nTermByte; /* Size of current term in bytes */
6591 };
6592
6593 /*
6594 ** fts5VisitEntries() callback used by fts5SetupPrefixIterTokendata(). This
6595 ** callback adds an entry to the Fts5TokenDataIter.aMap[] array for each
6596 ** position in the current position-list. It doesn't matter that some of
6597 ** these may be out of order - they will be sorted later.
6598 */
6599 static void prefixIterSetupTokendataCb(
6600 Fts5Index *p,
6601 void *pCtx,
6602 Fts5Iter *p1,
6603 const u8 *pNew,
6604 int nNew
6605 ){
6606 TokendataSetupCtx *pSetup = (TokendataSetupCtx*)pCtx;
6607 int iPosOff = 0;
6608 i64 iPos = 0;
6609
6610 if( pNew ){
6611 pSetup->nTermByte = nNew-1;
6612 pSetup->iTermOff = pSetup->pT->terms.n;
6613 fts5BufferAppendBlob(&p->rc, &pSetup->pT->terms, nNew-1, pNew+1);
6614 }
6615
6616 while( 0==sqlite3Fts5PoslistNext64(
6617 p1->base.pData, p1->base.nData, &iPosOff, &iPos
6618 ) ){
6619 fts5TokendataIterAppendMap(p,
6620 pSetup->pT, pSetup->iTermOff, pSetup->nTermByte, p1->base.iRowid, iPos
6621 );
6622 }
6623 }
6624
6625
6626 /*
6627 ** Context object passed by fts5SetupPrefixIter() to fts5VisitEntries().
6628 */
6629 typedef struct PrefixSetupCtx PrefixSetupCtx;
6630 struct PrefixSetupCtx {
6631 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
6632 void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
6633 i64 iLastRowid;
6634 int nMerge;
6635 Fts5Buffer *aBuf;
6636 int nBuf;
6637 Fts5Buffer doclist;
6638 TokendataSetupCtx *pTokendata;
6639 };
6640
6641 /*
6642 ** fts5VisitEntries() callback used by fts5SetupPrefixIter()
6643 */
6644 static void prefixIterSetupCb(
6645 Fts5Index *p,
6646 void *pCtx,
6647 Fts5Iter *p1,
6648 const u8 *pNew,
6649 int nNew
6650 ){
6651 PrefixSetupCtx *pSetup = (PrefixSetupCtx*)pCtx;
6652 const int nMerge = pSetup->nMerge;
6653
6654 if( p1->base.nData>0 ){
6655 if( p1->base.iRowid<=pSetup->iLastRowid && pSetup->doclist.n>0 ){
6656 int i;
6657 for(i=0; p->rc==SQLITE_OK && pSetup->doclist.n; i++){
6658 int i1 = i*nMerge;
6659 int iStore;
6660 assert( i1+nMerge<=pSetup->nBuf );
6661 for(iStore=i1; iStore<i1+nMerge; iStore++){
6662 if( pSetup->aBuf[iStore].n==0 ){
6663 fts5BufferSwap(&pSetup->doclist, &pSetup->aBuf[iStore]);
6664 fts5BufferZero(&pSetup->doclist);
6665 break;
6666 }
6667 }
6668 if( iStore==i1+nMerge ){
6669 pSetup->xMerge(p, &pSetup->doclist, nMerge, &pSetup->aBuf[i1]);
6670 for(iStore=i1; iStore<i1+nMerge; iStore++){
6671 fts5BufferZero(&pSetup->aBuf[iStore]);
6672 }
6673 }
6674 }
6675 pSetup->iLastRowid = 0;
6676 }
6677
6678 pSetup->xAppend(
6679 p, (u64)p1->base.iRowid-(u64)pSetup->iLastRowid, p1, &pSetup->doclist
6680 );
6681 pSetup->iLastRowid = p1->base.iRowid;
6682 }
6683
6684 if( pSetup->pTokendata ){
6685 prefixIterSetupTokendataCb(p, (void*)pSetup->pTokendata, p1, pNew, nNew);
6686 }
6687 }
6688
6689 static void fts5SetupPrefixIter(
6690 Fts5Index *p, /* Index to read from */
6691 int bDesc, /* True for "ORDER BY rowid DESC" */
6692 int iIdx, /* Index to scan for data */
6693 u8 *pToken, /* Buffer containing prefix to match */
6694 int nToken, /* Size of buffer pToken in bytes */
6695 Fts5Colset *pColset, /* Restrict matches to these columns */
6696 Fts5Iter **ppIter /* OUT: New iterator */
6697 ){
6698 Fts5Structure *pStruct;
6699 PrefixSetupCtx s;
6700 TokendataSetupCtx s2;
6701
6702 memset(&s, 0, sizeof(s));
6703 memset(&s2, 0, sizeof(s2));
6704
6705 s.nMerge = 1;
6706 s.iLastRowid = 0;
6707 s.nBuf = 32;
6708 if( iIdx==0
6709 && p->pConfig->eDetail==FTS5_DETAIL_FULL
6710 && p->pConfig->bPrefixInsttoken
6711 ){
6712 s.pTokendata = &s2;
6713 s2.pT = (Fts5TokenDataIter*)fts5IdxMalloc(p, SZ_FTS5TOKENDATAITER(1));
6714 }
6715
6716 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
6717 s.xMerge = fts5MergeRowidLists;
6718 s.xAppend = fts5AppendRowid;
6719 }else{
6720 s.nMerge = FTS5_MERGE_NLIST-1;
6721 s.nBuf = s.nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
6722 s.xMerge = fts5MergePrefixLists;
6723 s.xAppend = fts5AppendPoslist;
6724 }
6725
6726 s.aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*s.nBuf);
6727 pStruct = fts5StructureRead(p);
6728 assert( p->rc!=SQLITE_OK || (s.aBuf && pStruct) );
6729
6730 if( p->rc==SQLITE_OK ){
6731 void *pCtx = (void*)&s;
6732 int i;
6733 Fts5Data *pData;
6734
6735 /* If iIdx is non-zero, then it is the number of a prefix-index for
6736 ** prefixes 1 character longer than the prefix being queried for. That
6737 ** index contains all the doclists required, except for the one
6738 ** corresponding to the prefix itself. That one is extracted from the
6739 ** main term index here. */
6740 if( iIdx!=0 ){
6741 pToken[0] = FTS5_MAIN_PREFIX;
6742 fts5VisitEntries(p, pColset, pToken, nToken, 0, prefixIterSetupCb, pCtx);
6743 }
6744
6745 pToken[0] = FTS5_MAIN_PREFIX + iIdx;
6746 fts5VisitEntries(p, pColset, pToken, nToken, 1, prefixIterSetupCb, pCtx);
6747
6748 assert( (s.nBuf%s.nMerge)==0 );
6749 for(i=0; i<s.nBuf; i+=s.nMerge){
6750 int iFree;
6751 if( p->rc==SQLITE_OK ){
6752 s.xMerge(p, &s.doclist, s.nMerge, &s.aBuf[i]);
6753 }
6754 for(iFree=i; iFree<i+s.nMerge; iFree++){
6755 fts5BufferFree(&s.aBuf[iFree]);
6756 }
6757 }
6758
6759 pData = fts5IdxMalloc(p, sizeof(*pData)
6760 + ((i64)s.doclist.n)+FTS5_DATA_ZERO_PADDING);
6761 assert( pData!=0 || p->rc!=SQLITE_OK );
6762 if( pData ){
6763 pData->p = (u8*)&pData[1];
6764 pData->nn = pData->szLeaf = s.doclist.n;
6765 if( s.doclist.n ) memcpy(pData->p, s.doclist.p, s.doclist.n);
6766 fts5MultiIterNew2(p, pData, bDesc, ppIter);
6767 }
6768
6769 assert( (*ppIter)!=0 || p->rc!=SQLITE_OK );
6770 if( p->rc==SQLITE_OK && s.pTokendata ){
6771 fts5TokendataIterSortMap(p, s2.pT);
6772 (*ppIter)->pTokenDataIter = s2.pT;
6773 s2.pT = 0;
6774 }
6775 }
6776
6777 fts5TokendataIterDelete(s2.pT);
6778 fts5BufferFree(&s.doclist);
6779 fts5StructureRelease(pStruct);
6780 sqlite3_free(s.aBuf);
6781 }
6782
6783
6784 /*
6785 ** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
6786 ** to the document with rowid iRowid.
6787 */
6788 int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
6789 assert( p->rc==SQLITE_OK );
6790
6791 /* Allocate the hash table if it has not already been allocated */
6792 if( p->pHash==0 ){
6793 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
6794 }
6795
6796 /* Flush the hash table to disk if required */
6797 if( iRowid<p->iWriteRowid
6798 || (iRowid==p->iWriteRowid && p->bDelete==0)
6799 || (p->nPendingData > p->pConfig->nHashSize)
6800 ){
6801 fts5IndexFlush(p);
6802 }
6803
6804 p->iWriteRowid = iRowid;
6805 p->bDelete = bDelete;
6806 if( bDelete==0 ){
6807 p->nPendingRow++;
6808 }
6809 return fts5IndexReturn(p);
6810 }
6811
6812 /*
6813 ** Commit data to disk.
6814 */
6815 int sqlite3Fts5IndexSync(Fts5Index *p){
6816 assert( p->rc==SQLITE_OK );
6817 fts5IndexFlush(p);
6818 fts5IndexCloseReader(p);
6819 return fts5IndexReturn(p);
6820 }
6821
6822 /*
6823 ** Discard any data stored in the in-memory hash tables. Do not write it
6824 ** to the database. Additionally, assume that the contents of the %_data
6825 ** table may have changed on disk. So any in-memory caches of %_data
6826 ** records must be invalidated.
6827 */
6828 int sqlite3Fts5IndexRollback(Fts5Index *p){
6829 fts5IndexCloseReader(p);
6830 fts5IndexDiscardData(p);
6831 fts5StructureInvalidate(p);
6832 return fts5IndexReturn(p);
6833 }
6834
6835 /*
6836 ** The %_data table is completely empty when this function is called. This
6837 ** function populates it with the initial structure objects for each index,
6838 ** and the initial version of the "averages" record (a zero-byte blob).
6839 */
6840 int sqlite3Fts5IndexReinit(Fts5Index *p){
6841 Fts5Structure *pTmp;
6842 union {
6843 Fts5Structure sFts;
6844 u8 tmpSpace[SZ_FTS5STRUCTURE(1)];
6845 } uFts;
6846 fts5StructureInvalidate(p);
6847 fts5IndexDiscardData(p);
6848 pTmp = &uFts.sFts;
6849 memset(uFts.tmpSpace, 0, sizeof(uFts.tmpSpace));
6850 if( p->pConfig->bContentlessDelete ){
6851 pTmp->nOriginCntr = 1;
6852 }
6853 fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0);
6854 fts5StructureWrite(p, pTmp);
6855 return fts5IndexReturn(p);
6856 }
6857
6858 /*
6859 ** Open a new Fts5Index handle. If the bCreate argument is true, create
6860 ** and initialize the underlying %_data table.
6861 **
6862 ** If successful, set *pp to point to the new object and return SQLITE_OK.
6863 ** Otherwise, set *pp to NULL and return an SQLite error code.
6864 */
6865 int sqlite3Fts5IndexOpen(
6866 Fts5Config *pConfig,
6867 int bCreate,
6868 Fts5Index **pp,
6869 char **pzErr
6870 ){
6871 int rc = SQLITE_OK;
6872 Fts5Index *p; /* New object */
6873
6874 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
6875 if( rc==SQLITE_OK ){
6876 p->pConfig = pConfig;
6877 p->nWorkUnit = FTS5_WORK_UNIT;
6878 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
6879 if( p->zDataTbl && bCreate ){
6880 rc = sqlite3Fts5CreateTable(
6881 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
6882 );
6883 if( rc==SQLITE_OK ){
6884 rc = sqlite3Fts5CreateTable(pConfig, "idx",
6885 "segid, term, pgno, PRIMARY KEY(segid, term)",
6886 1, pzErr
6887 );
6888 }
6889 if( rc==SQLITE_OK ){
6890 rc = sqlite3Fts5IndexReinit(p);
6891 }
6892 }
6893 }
6894
6895 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
6896 if( rc ){
6897 sqlite3Fts5IndexClose(p);
6898 *pp = 0;
6899 }
6900 return rc;
6901 }
6902
6903 /*
6904 ** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
6905 */
6906 int sqlite3Fts5IndexClose(Fts5Index *p){
6907 int rc = SQLITE_OK;
6908 if( p ){
6909 assert( p->pReader==0 );
6910 fts5StructureInvalidate(p);
6911 sqlite3_finalize(p->pWriter);
6912 sqlite3_finalize(p->pDeleter);
6913 sqlite3_finalize(p->pIdxWriter);
6914 sqlite3_finalize(p->pIdxDeleter);
6915 sqlite3_finalize(p->pIdxSelect);
6916 sqlite3_finalize(p->pIdxNextSelect);
6917 sqlite3_finalize(p->pDataVersion);
6918 sqlite3_finalize(p->pDeleteFromIdx);
6919 sqlite3Fts5HashFree(p->pHash);
6920 sqlite3_free(p->zDataTbl);
6921 sqlite3_free(p);
6922 }
6923 return rc;
6924 }
6925
6926 /*
6927 ** Argument p points to a buffer containing utf-8 text that is n bytes in
6928 ** size. Return the number of bytes in the nChar character prefix of the
6929 ** buffer, or 0 if there are less than nChar characters in total.
6930 */
6931 int sqlite3Fts5IndexCharlenToBytelen(
6932 const char *p,
6933 int nByte,
6934 int nChar
6935 ){
6936 int n = 0;
6937 int i;
6938 for(i=0; i<nChar; i++){
6939 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
6940 if( (unsigned char)p[n++]>=0xc0 ){
6941 if( n>=nByte ) return 0;
6942 while( (p[n] & 0xc0)==0x80 ){
6943 n++;
6944 if( n>=nByte ){
6945 if( i+1==nChar ) break;
6946 return 0;
6947 }
6948 }
6949 }
6950 }
6951 return n;
6952 }
6953
6954 /*
6955 ** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
6956 ** unicode characters in the string.
6957 */
6958 static int fts5IndexCharlen(const char *pIn, int nIn){
6959 int nChar = 0;
6960 int i = 0;
6961 while( i<nIn ){
6962 if( (unsigned char)pIn[i++]>=0xc0 ){
6963 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
6964 }
6965 nChar++;
6966 }
6967 return nChar;
6968 }
6969
6970 /*
6971 ** Insert or remove data to or from the index. Each time a document is
6972 ** added to or removed from the index, this function is called one or more
6973 ** times.
6974 **
6975 ** For an insert, it must be called once for each token in the new document.
6976 ** If the operation is a delete, it must be called (at least) once for each
6977 ** unique token in the document with an iCol value less than zero. The iPos
6978 ** argument is ignored for a delete.
6979 */
6980 int sqlite3Fts5IndexWrite(
6981 Fts5Index *p, /* Index to write to */
6982 int iCol, /* Column token appears in (-ve -> delete) */
6983 int iPos, /* Position of token within column */
6984 const char *pToken, int nToken /* Token to add or remove to or from index */
6985 ){
6986 int i; /* Used to iterate through indexes */
6987 int rc = SQLITE_OK; /* Return code */
6988 Fts5Config *pConfig = p->pConfig;
6989
6990 assert( p->rc==SQLITE_OK );
6991 assert( (iCol<0)==p->bDelete );
6992
6993 /* Add the entry to the main terms index. */
6994 rc = sqlite3Fts5HashWrite(
6995 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
6996 );
6997
6998 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
6999 const int nChar = pConfig->aPrefix[i];
7000 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
7001 if( nByte ){
7002 rc = sqlite3Fts5HashWrite(p->pHash,
7003 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX+i+1), pToken,
7004 nByte
7005 );
7006 }
7007 }
7008
7009 return rc;
7010 }
7011
7012 /*
7013 ** pToken points to a buffer of size nToken bytes containing a search
7014 ** term, including the index number at the start, used on a tokendata=1
7015 ** table. This function returns true if the term in buffer pBuf matches
7016 ** token pToken/nToken.
7017 */
7018 static int fts5IsTokendataPrefix(
7019 Fts5Buffer *pBuf,
7020 const u8 *pToken,
7021 int nToken
7022 ){
7023 return (
7024 pBuf->n>=nToken
7025 && 0==memcmp(pBuf->p, pToken, nToken)
7026 && (pBuf->n==nToken || pBuf->p[nToken]==0x00)
7027 );
7028 }
7029
7030 /*
7031 ** Ensure the segment-iterator passed as the only argument points to EOF.
7032 */
7033 static void fts5SegIterSetEOF(Fts5SegIter *pSeg){
7034 fts5DataRelease(pSeg->pLeaf);
7035 pSeg->pLeaf = 0;
7036 }
7037
7038 static void fts5IterClose(Fts5IndexIter *pIndexIter){
7039 if( pIndexIter ){
7040 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
7041 Fts5Index *pIndex = pIter->pIndex;
7042 fts5TokendataIterDelete(pIter->pTokenDataIter);
7043 fts5MultiIterFree(pIter);
7044 fts5IndexCloseReader(pIndex);
7045 }
7046 }
7047
7048 /*
7049 ** This function appends iterator pAppend to Fts5TokenDataIter pIn and
7050 ** returns the result.
7051 */
7052 static Fts5TokenDataIter *fts5AppendTokendataIter(
7053 Fts5Index *p, /* Index object (for error code) */
7054 Fts5TokenDataIter *pIn, /* Current Fts5TokenDataIter struct */
7055 Fts5Iter *pAppend /* Append this iterator */
7056 ){
7057 Fts5TokenDataIter *pRet = pIn;
7058
7059 if( p->rc==SQLITE_OK ){
7060 if( pIn==0 || pIn->nIter==pIn->nIterAlloc ){
7061 i64 nAlloc = pIn ? pIn->nIterAlloc*2 : 16;
7062 i64 nByte = SZ_FTS5TOKENDATAITER(nAlloc+1);
7063 Fts5TokenDataIter *pNew;
7064 pNew = (Fts5TokenDataIter*)sqlite3_realloc64(pIn, nByte);
7065
7066 if( pNew==0 ){
7067 p->rc = SQLITE_NOMEM;
7068 }else{
7069 if( pIn==0 ) memset(pNew, 0, nByte);
7070 pRet = pNew;
7071 pNew->nIterAlloc = nAlloc;
7072 }
7073 }
7074 }
7075 if( p->rc ){
7076 fts5IterClose((Fts5IndexIter*)pAppend);
7077 }else{
7078 pRet->apIter[pRet->nIter++] = pAppend;
7079 }
7080 assert( pRet==0 || pRet->nIter<=pRet->nIterAlloc );
7081
7082 return pRet;
7083 }
7084
7085 /*
7086 ** The iterator passed as the only argument must be a tokendata=1 iterator
7087 ** (pIter->pTokenDataIter!=0). This function sets the iterator output
7088 ** variables (pIter->base.*) according to the contents of the current
7089 ** row.
7090 */
7091 static void fts5IterSetOutputsTokendata(Fts5Iter *pIter){
7092 int ii;
7093 int nHit = 0;
7094 i64 iRowid = SMALLEST_INT64;
7095 int iMin = 0;
7096
7097 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
7098
7099 pIter->base.nData = 0;
7100 pIter->base.pData = 0;
7101
7102 for(ii=0; ii<pT->nIter; ii++){
7103 Fts5Iter *p = pT->apIter[ii];
7104 if( p->base.bEof==0 ){
7105 if( nHit==0 || p->base.iRowid<iRowid ){
7106 iRowid = p->base.iRowid;
7107 nHit = 1;
7108 pIter->base.pData = p->base.pData;
7109 pIter->base.nData = p->base.nData;
7110 iMin = ii;
7111 }else if( p->base.iRowid==iRowid ){
7112 nHit++;
7113 }
7114 }
7115 }
7116
7117 if( nHit==0 ){
7118 pIter->base.bEof = 1;
7119 }else{
7120 int eDetail = pIter->pIndex->pConfig->eDetail;
7121 pIter->base.bEof = 0;
7122 pIter->base.iRowid = iRowid;
7123
7124 if( nHit==1 && eDetail==FTS5_DETAIL_FULL ){
7125 fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, 0, iRowid, -1);
7126 }else
7127 if( nHit>1 && eDetail!=FTS5_DETAIL_NONE ){
7128 int nReader = 0;
7129 int nByte = 0;
7130 i64 iPrev = 0;
7131
7132 /* Allocate array of iterators if they are not already allocated. */
7133 if( pT->aPoslistReader==0 ){
7134 pT->aPoslistReader = (Fts5PoslistReader*)sqlite3Fts5MallocZero(
7135 &pIter->pIndex->rc,
7136 pT->nIter * (sizeof(Fts5PoslistReader) + sizeof(int))
7137 );
7138 if( pT->aPoslistReader==0 ) return;
7139 pT->aPoslistToIter = (int*)&pT->aPoslistReader[pT->nIter];
7140 }
7141
7142 /* Populate an iterator for each poslist that will be merged */
7143 for(ii=0; ii<pT->nIter; ii++){
7144 Fts5Iter *p = pT->apIter[ii];
7145 if( iRowid==p->base.iRowid ){
7146 pT->aPoslistToIter[nReader] = ii;
7147 sqlite3Fts5PoslistReaderInit(
7148 p->base.pData, p->base.nData, &pT->aPoslistReader[nReader++]
7149 );
7150 nByte += p->base.nData;
7151 }
7152 }
7153
7154 /* Ensure the output buffer is large enough */
7155 if( fts5BufferGrow(&pIter->pIndex->rc, &pIter->poslist, nByte+nHit*10) ){
7156 return;
7157 }
7158
7159 /* Ensure the token-mapping is large enough */
7160 if( eDetail==FTS5_DETAIL_FULL && pT->nMapAlloc<(pT->nMap + nByte) ){
7161 i64 nNew = (pT->nMapAlloc + nByte) * 2;
7162 Fts5TokenDataMap *aNew = (Fts5TokenDataMap*)sqlite3_realloc64(
7163 pT->aMap, nNew*sizeof(Fts5TokenDataMap)
7164 );
7165 if( aNew==0 ){
7166 pIter->pIndex->rc = SQLITE_NOMEM;
7167 return;
7168 }
7169 pT->aMap = aNew;
7170 pT->nMapAlloc = nNew;
7171 }
7172
7173 pIter->poslist.n = 0;
7174
7175 while( 1 ){
7176 i64 iMinPos = LARGEST_INT64;
7177
7178 /* Find smallest position */
7179 iMin = 0;
7180 for(ii=0; ii<nReader; ii++){
7181 Fts5PoslistReader *pReader = &pT->aPoslistReader[ii];
7182 if( pReader->bEof==0 ){
7183 if( pReader->iPos<iMinPos ){
7184 iMinPos = pReader->iPos;
7185 iMin = ii;
7186 }
7187 }
7188 }
7189
7190 /* If all readers were at EOF, break out of the loop. */
7191 if( iMinPos==LARGEST_INT64 ) break;
7192
7193 sqlite3Fts5PoslistSafeAppend(&pIter->poslist, &iPrev, iMinPos);
7194 sqlite3Fts5PoslistReaderNext(&pT->aPoslistReader[iMin]);
7195
7196 if( eDetail==FTS5_DETAIL_FULL ){
7197 pT->aMap[pT->nMap].iPos = iMinPos;
7198 pT->aMap[pT->nMap].iIter = pT->aPoslistToIter[iMin];
7199 pT->aMap[pT->nMap].iRowid = iRowid;
7200 pT->nMap++;
7201 }
7202 }
7203
7204 pIter->base.pData = pIter->poslist.p;
7205 pIter->base.nData = pIter->poslist.n;
7206 }
7207 }
7208 }
7209
7210 /*
7211 ** The iterator passed as the only argument must be a tokendata=1 iterator
7212 ** (pIter->pTokenDataIter!=0). This function advances the iterator. If
7213 ** argument bFrom is false, then the iterator is advanced to the next
7214 ** entry. Or, if bFrom is true, it is advanced to the first entry with
7215 ** a rowid of iFrom or greater.
7216 */
7217 static void fts5TokendataIterNext(Fts5Iter *pIter, int bFrom, i64 iFrom){
7218 int ii;
7219 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
7220 Fts5Index *pIndex = pIter->pIndex;
7221
7222 for(ii=0; ii<pT->nIter; ii++){
7223 Fts5Iter *p = pT->apIter[ii];
7224 if( p->base.bEof==0
7225 && (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowid<iFrom))
7226 ){
7227 fts5MultiIterNext(pIndex, p, bFrom, iFrom);
7228 while( bFrom && p->base.bEof==0
7229 && p->base.iRowid<iFrom
7230 && pIndex->rc==SQLITE_OK
7231 ){
7232 fts5MultiIterNext(pIndex, p, 0, 0);
7233 }
7234 }
7235 }
7236
7237 if( pIndex->rc==SQLITE_OK ){
7238 fts5IterSetOutputsTokendata(pIter);
7239 }
7240 }
7241
7242 /*
7243 ** If the segment-iterator passed as the first argument is at EOF, then
7244 ** set pIter->term to a copy of buffer pTerm.
7245 */
7246 static void fts5TokendataSetTermIfEof(Fts5Iter *pIter, Fts5Buffer *pTerm){
7247 if( pIter && pIter->aSeg[0].pLeaf==0 ){
7248 fts5BufferSet(&pIter->pIndex->rc, &pIter->aSeg[0].term, pTerm->n, pTerm->p);
7249 }
7250 }
7251
7252 /*
7253 ** This function sets up an iterator to use for a non-prefix query on a
7254 ** tokendata=1 table.
7255 */
7256 static Fts5Iter *fts5SetupTokendataIter(
7257 Fts5Index *p, /* FTS index to query */
7258 const u8 *pToken, /* Buffer containing query term */
7259 int nToken, /* Size of buffer pToken in bytes */
7260 Fts5Colset *pColset /* Colset to filter on */
7261 ){
7262 Fts5Iter *pRet = 0;
7263 Fts5TokenDataIter *pSet = 0;
7264 Fts5Structure *pStruct = 0;
7265 const int flags = FTS5INDEX_QUERY_SCANONETERM | FTS5INDEX_QUERY_SCAN;
7266
7267 Fts5Buffer bSeek = {0, 0, 0};
7268 Fts5Buffer *pSmall = 0;
7269
7270 fts5IndexFlush(p);
7271 pStruct = fts5StructureRead(p);
7272
7273 while( p->rc==SQLITE_OK ){
7274 Fts5Iter *pPrev = pSet ? pSet->apIter[pSet->nIter-1] : 0;
7275 Fts5Iter *pNew = 0;
7276 Fts5SegIter *pNewIter = 0;
7277 Fts5SegIter *pPrevIter = 0;
7278
7279 int iLvl, iSeg, ii;
7280
7281 pNew = fts5MultiIterAlloc(p, pStruct->nSegment);
7282 if( pSmall ){
7283 fts5BufferSet(&p->rc, &bSeek, pSmall->n, pSmall->p);
7284 fts5BufferAppendBlob(&p->rc, &bSeek, 1, (const u8*)"\0");
7285 }else{
7286 fts5BufferSet(&p->rc, &bSeek, nToken, pToken);
7287 }
7288 if( p->rc ){
7289 fts5IterClose((Fts5IndexIter*)pNew);
7290 break;
7291 }
7292
7293 pNewIter = &pNew->aSeg[0];
7294 pPrevIter = (pPrev ? &pPrev->aSeg[0] : 0);
7295 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
7296 for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){
7297 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
7298 int bDone = 0;
7299
7300 if( pPrevIter ){
7301 if( fts5BufferCompare(pSmall, &pPrevIter->term) ){
7302 memcpy(pNewIter, pPrevIter, sizeof(Fts5SegIter));
7303 memset(pPrevIter, 0, sizeof(Fts5SegIter));
7304 bDone = 1;
7305 }else if( pPrevIter->iEndofDoclist>pPrevIter->pLeaf->szLeaf ){
7306 fts5SegIterNextInit(p,(const char*)bSeek.p,bSeek.n-1,pSeg,pNewIter);
7307 bDone = 1;
7308 }
7309 }
7310
7311 if( bDone==0 ){
7312 fts5SegIterSeekInit(p, bSeek.p, bSeek.n, flags, pSeg, pNewIter);
7313 }
7314
7315 if( pPrevIter ){
7316 if( pPrevIter->pTombArray ){
7317 pNewIter->pTombArray = pPrevIter->pTombArray;
7318 pNewIter->pTombArray->nRef++;
7319 }
7320 }else{
7321 fts5SegIterAllocTombstone(p, pNewIter);
7322 }
7323
7324 pNewIter++;
7325 if( pPrevIter ) pPrevIter++;
7326 if( p->rc ) break;
7327 }
7328 }
7329 fts5TokendataSetTermIfEof(pPrev, pSmall);
7330
7331 pNew->bSkipEmpty = 1;
7332 pNew->pColset = pColset;
7333 fts5IterSetOutputCb(&p->rc, pNew);
7334
7335 /* Loop through all segments in the new iterator. Find the smallest
7336 ** term that any segment-iterator points to. Iterator pNew will be
7337 ** used for this term. Also, set any iterator that points to a term that
7338 ** does not match pToken/nToken to point to EOF */
7339 pSmall = 0;
7340 for(ii=0; ii<pNew->nSeg; ii++){
7341 Fts5SegIter *pII = &pNew->aSeg[ii];
7342 if( 0==fts5IsTokendataPrefix(&pII->term, pToken, nToken) ){
7343 fts5SegIterSetEOF(pII);
7344 }
7345 if( pII->pLeaf && (!pSmall || fts5BufferCompare(pSmall, &pII->term)>0) ){
7346 pSmall = &pII->term;
7347 }
7348 }
7349
7350 /* If pSmall is still NULL at this point, then the new iterator does
7351 ** not point to any terms that match the query. So delete it and break
7352 ** out of the loop - all required iterators have been collected. */
7353 if( pSmall==0 ){
7354 fts5IterClose((Fts5IndexIter*)pNew);
7355 break;
7356 }
7357
7358 /* Append this iterator to the set and continue. */
7359 pSet = fts5AppendTokendataIter(p, pSet, pNew);
7360 }
7361
7362 if( p->rc==SQLITE_OK && pSet ){
7363 int ii;
7364 for(ii=0; ii<pSet->nIter; ii++){
7365 Fts5Iter *pIter = pSet->apIter[ii];
7366 int iSeg;
7367 for(iSeg=0; iSeg<pIter->nSeg; iSeg++){
7368 pIter->aSeg[iSeg].flags |= FTS5_SEGITER_ONETERM;
7369 }
7370 fts5MultiIterFinishSetup(p, pIter);
7371 }
7372 }
7373
7374 if( p->rc==SQLITE_OK ){
7375 pRet = fts5MultiIterAlloc(p, 0);
7376 }
7377 if( pRet ){
7378 pRet->nSeg = 0;
7379 pRet->pTokenDataIter = pSet;
7380 if( pSet ){
7381 fts5IterSetOutputsTokendata(pRet);
7382 }else{
7383 pRet->base.bEof = 1;
7384 }
7385 }else{
7386 fts5TokendataIterDelete(pSet);
7387 }
7388
7389 fts5StructureRelease(pStruct);
7390 fts5BufferFree(&bSeek);
7391 return pRet;
7392 }
7393
7394 /*
7395 ** Open a new iterator to iterate though all rowid that match the
7396 ** specified token or token prefix.
7397 */
7398 int sqlite3Fts5IndexQuery(
7399 Fts5Index *p, /* FTS index to query */
7400 const char *pToken, int nToken, /* Token (or prefix) to query for */
7401 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
7402 Fts5Colset *pColset, /* Match these columns only */
7403 Fts5IndexIter **ppIter /* OUT: New iterator object */
7404 ){
7405 Fts5Config *pConfig = p->pConfig;
7406 Fts5Iter *pRet = 0;
7407 Fts5Buffer buf = {0, 0, 0};
7408
7409 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
7410 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
7411
7412 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
7413 int iIdx = 0; /* Index to search */
7414 int iPrefixIdx = 0; /* +1 prefix index */
7415 int bTokendata = pConfig->bTokendata;
7416 assert( buf.p!=0 );
7417 if( nToken>0 ) memcpy(&buf.p[1], pToken, nToken);
7418
7419 /* The NOTOKENDATA flag is set when each token in a tokendata=1 table
7420 ** should be treated individually, instead of merging all those with
7421 ** a common prefix into a single entry. This is used, for example, by
7422 ** queries performed as part of an integrity-check, or by the fts5vocab
7423 ** module. */
7424 if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){
7425 bTokendata = 0;
7426 }
7427
7428 /* Figure out which index to search and set iIdx accordingly. If this
7429 ** is a prefix query for which there is no prefix index, set iIdx to
7430 ** greater than pConfig->nPrefix to indicate that the query will be
7431 ** satisfied by scanning multiple terms in the main index.
7432 **
7433 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
7434 ** prefix-query. Instead of using a prefix-index (if one exists),
7435 ** evaluate the prefix query using the main FTS index. This is used
7436 ** for internal sanity checking by the integrity-check in debug
7437 ** mode only. */
7438 #ifdef SQLITE_DEBUG
7439 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
7440 assert( flags & FTS5INDEX_QUERY_PREFIX );
7441 iIdx = 1+pConfig->nPrefix;
7442 }else
7443 #endif
7444 if( flags & FTS5INDEX_QUERY_PREFIX ){
7445 int nChar = fts5IndexCharlen(pToken, nToken);
7446 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
7447 int nIdxChar = pConfig->aPrefix[iIdx-1];
7448 if( nIdxChar==nChar ) break;
7449 if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx;
7450 }
7451 }
7452
7453 if( bTokendata && iIdx==0 ){
7454 buf.p[0] = FTS5_MAIN_PREFIX;
7455 pRet = fts5SetupTokendataIter(p, buf.p, nToken+1, pColset);
7456 }else if( iIdx<=pConfig->nPrefix ){
7457 /* Straight index lookup */
7458 Fts5Structure *pStruct = fts5StructureRead(p);
7459 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
7460 if( pStruct ){
7461 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
7462 pColset, buf.p, nToken+1, -1, 0, &pRet
7463 );
7464 fts5StructureRelease(pStruct);
7465 }
7466 }else{
7467 /* Scan multiple terms in the main index for a prefix query. */
7468 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
7469 fts5SetupPrefixIter(p, bDesc, iPrefixIdx, buf.p, nToken+1, pColset,&pRet);
7470 if( pRet==0 ){
7471 assert( p->rc!=SQLITE_OK );
7472 }else{
7473 assert( pRet->pColset==0 );
7474 fts5IterSetOutputCb(&p->rc, pRet);
7475 if( p->rc==SQLITE_OK ){
7476 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
7477 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
7478 }
7479 }
7480 }
7481
7482 if( p->rc ){
7483 fts5IterClose((Fts5IndexIter*)pRet);
7484 pRet = 0;
7485 fts5IndexCloseReader(p);
7486 }
7487
7488 *ppIter = (Fts5IndexIter*)pRet;
7489 sqlite3Fts5BufferFree(&buf);
7490 }
7491 return fts5IndexReturn(p);
7492 }
7493
7494 /*
7495 ** Return true if the iterator passed as the only argument is at EOF.
7496 */
7497 /*
7498 ** Move to the next matching rowid.
7499 */
7500 int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
7501 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
7502 assert( pIter->pIndex->rc==SQLITE_OK );
7503 if( pIter->nSeg==0 ){
7504 assert( pIter->pTokenDataIter );
7505 fts5TokendataIterNext(pIter, 0, 0);
7506 }else{
7507 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
7508 }
7509 return fts5IndexReturn(pIter->pIndex);
7510 }
7511
7512 /*
7513 ** Move to the next matching term/rowid. Used by the fts5vocab module.
7514 */
7515 int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
7516 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
7517 Fts5Index *p = pIter->pIndex;
7518
7519 assert( pIter->pIndex->rc==SQLITE_OK );
7520
7521 fts5MultiIterNext(p, pIter, 0, 0);
7522 if( p->rc==SQLITE_OK ){
7523 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
7524 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
7525 fts5DataRelease(pSeg->pLeaf);
7526 pSeg->pLeaf = 0;
7527 pIter->base.bEof = 1;
7528 }
7529 }
7530
7531 return fts5IndexReturn(pIter->pIndex);
7532 }
7533
7534 /*
7535 ** Move to the next matching rowid that occurs at or after iMatch. The
7536 ** definition of "at or after" depends on whether this iterator iterates
7537 ** in ascending or descending rowid order.
7538 */
7539 int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
7540 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
7541 if( pIter->nSeg==0 ){
7542 assert( pIter->pTokenDataIter );
7543 fts5TokendataIterNext(pIter, 1, iMatch);
7544 }else{
7545 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
7546 }
7547 return fts5IndexReturn(pIter->pIndex);
7548 }
7549
7550 /*
7551 ** Return the current term.
7552 */
7553 const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
7554 int n;
7555 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
7556 assert_nc( z || n<=1 );
7557 *pn = n-1;
7558 return (z ? &z[1] : 0);
7559 }
7560
7561 /*
7562 ** pIter is a prefix query. This function populates pIter->pTokenDataIter
7563 ** with an Fts5TokenDataIter object containing mappings for all rows
7564 ** matched by the query.
7565 */
7566 static int fts5SetupPrefixIterTokendata(
7567 Fts5Iter *pIter,
7568 const char *pToken, /* Token prefix to search for */
7569 int nToken /* Size of pToken in bytes */
7570 ){
7571 Fts5Index *p = pIter->pIndex;
7572 Fts5Buffer token = {0, 0, 0};
7573 TokendataSetupCtx ctx;
7574
7575 memset(&ctx, 0, sizeof(ctx));
7576
7577 fts5BufferGrow(&p->rc, &token, nToken+1);
7578 assert( token.p!=0 || p->rc!=SQLITE_OK );
7579 ctx.pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc,
7580 SZ_FTS5TOKENDATAITER(1));
7581
7582 if( p->rc==SQLITE_OK ){
7583
7584 /* Fill in the token prefix to search for */
7585 token.p[0] = FTS5_MAIN_PREFIX;
7586 memcpy(&token.p[1], pToken, nToken);
7587 token.n = nToken+1;
7588
7589 fts5VisitEntries(
7590 p, 0, token.p, token.n, 1, prefixIterSetupTokendataCb, (void*)&ctx
7591 );
7592
7593 fts5TokendataIterSortMap(p, ctx.pT);
7594 }
7595
7596 if( p->rc==SQLITE_OK ){
7597 pIter->pTokenDataIter = ctx.pT;
7598 }else{
7599 fts5TokendataIterDelete(ctx.pT);
7600 }
7601 fts5BufferFree(&token);
7602
7603 return fts5IndexReturn(p);
7604 }
7605
7606 /*
7607 ** This is used by xInstToken() to access the token at offset iOff, column
7608 ** iCol of row iRowid. The token is returned via output variables *ppOut
7609 ** and *pnOut. The iterator passed as the first argument must be a tokendata=1
7610 ** iterator (pIter->pTokenDataIter!=0).
7611 **
7612 ** pToken/nToken:
7613 */
7614 int sqlite3Fts5IterToken(
7615 Fts5IndexIter *pIndexIter,
7616 const char *pToken, int nToken,
7617 i64 iRowid,
7618 int iCol,
7619 int iOff,
7620 const char **ppOut, int *pnOut
7621 ){
7622 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
7623 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
7624 i64 iPos = (((i64)iCol)<<32) + iOff;
7625 Fts5TokenDataMap *aMap = 0;
7626 int i1 = 0;
7627 int i2 = 0;
7628 int iTest = 0;
7629
7630 assert( pT || (pToken && pIter->nSeg>0) );
7631 if( pT==0 ){
7632 int rc = fts5SetupPrefixIterTokendata(pIter, pToken, nToken);
7633 if( rc!=SQLITE_OK ) return rc;
7634 pT = pIter->pTokenDataIter;
7635 }
7636
7637 i2 = pT->nMap;
7638 aMap = pT->aMap;
7639
7640 while( i2>i1 ){
7641 iTest = (i1 + i2) / 2;
7642
7643 if( aMap[iTest].iRowid<iRowid ){
7644 i1 = iTest+1;
7645 }else if( aMap[iTest].iRowid>iRowid ){
7646 i2 = iTest;
7647 }else{
7648 if( aMap[iTest].iPos<iPos ){
7649 if( aMap[iTest].iPos<0 ){
7650 break;
7651 }
7652 i1 = iTest+1;
7653 }else if( aMap[iTest].iPos>iPos ){
7654 i2 = iTest;
7655 }else{
7656 break;
7657 }
7658 }
7659 }
7660
7661 if( i2>i1 ){
7662 if( pIter->nSeg==0 ){
7663 Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter];
7664 *ppOut = (const char*)pMap->aSeg[0].term.p+1;
7665 *pnOut = pMap->aSeg[0].term.n-1;
7666 }else{
7667 Fts5TokenDataMap *p = &aMap[iTest];
7668 *ppOut = (const char*)&pT->terms.p[p->iIter];
7669 *pnOut = aMap[iTest].nByte;
7670 }
7671 }
7672
7673 return SQLITE_OK;
7674 }
7675
7676 /*
7677 ** Clear any existing entries from the token-map associated with the
7678 ** iterator passed as the only argument.
7679 */
7680 void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter *pIndexIter){
7681 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
7682 if( pIter && pIter->pTokenDataIter
7683 && (pIter->nSeg==0 || pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_FULL)
7684 ){
7685 pIter->pTokenDataIter->nMap = 0;
7686 }
7687 }
7688
7689 /*
7690 ** Set a token-mapping for the iterator passed as the first argument. This
7691 ** is used in detail=column or detail=none mode when a token is requested
7692 ** using the xInstToken() API. In this case the caller tokenizers the
7693 ** current row and configures the token-mapping via multiple calls to this
7694 ** function.
7695 */
7696 int sqlite3Fts5IndexIterWriteTokendata(
7697 Fts5IndexIter *pIndexIter,
7698 const char *pToken, int nToken,
7699 i64 iRowid, int iCol, int iOff
7700 ){
7701 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
7702 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
7703 Fts5Index *p = pIter->pIndex;
7704 i64 iPos = (((i64)iCol)<<32) + iOff;
7705
7706 assert( p->pConfig->eDetail!=FTS5_DETAIL_FULL );
7707 assert( pIter->pTokenDataIter || pIter->nSeg>0 );
7708 if( pIter->nSeg>0 ){
7709 /* This is a prefix term iterator. */
7710 if( pT==0 ){
7711 pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc,
7712 SZ_FTS5TOKENDATAITER(1));
7713 pIter->pTokenDataIter = pT;
7714 }
7715 if( pT ){
7716 fts5TokendataIterAppendMap(p, pT, pT->terms.n, nToken, iRowid, iPos);
7717 fts5BufferAppendBlob(&p->rc, &pT->terms, nToken, (const u8*)pToken);
7718 }
7719 }else{
7720 int ii;
7721 for(ii=0; ii<pT->nIter; ii++){
7722 Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term;
7723 if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break;
7724 }
7725 if( ii<pT->nIter ){
7726 fts5TokendataIterAppendMap(p, pT, ii, 0, iRowid, iPos);
7727 }
7728 }
7729 return fts5IndexReturn(p);
7730 }
7731
7732 /*
7733 ** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
7734 */
7735 void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
7736 if( pIndexIter ){
7737 Fts5Index *pIndex = ((Fts5Iter*)pIndexIter)->pIndex;
7738 fts5IterClose(pIndexIter);
7739 fts5IndexReturn(pIndex);
7740 }
7741 }
7742
7743 /*
7744 ** Read and decode the "averages" record from the database.
7745 **
7746 ** Parameter anSize must point to an array of size nCol, where nCol is
7747 ** the number of user defined columns in the FTS table.
7748 */
7749 int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
7750 int nCol = p->pConfig->nCol;
7751 Fts5Data *pData;
7752
7753 *pnRow = 0;
7754 memset(anSize, 0, sizeof(i64) * nCol);
7755 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
7756 if( p->rc==SQLITE_OK && pData->nn ){
7757 int i = 0;
7758 int iCol;
7759 i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
7760 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
7761 i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
7762 }
7763 }
7764
7765 fts5DataRelease(pData);
7766 return fts5IndexReturn(p);
7767 }
7768
7769 /*
7770 ** Replace the current "averages" record with the contents of the buffer
7771 ** supplied as the second argument.
7772 */
7773 int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
7774 assert( p->rc==SQLITE_OK );
7775 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
7776 return fts5IndexReturn(p);
7777 }
7778
7779 /*
7780 ** Return the total number of blocks this module has read from the %_data
7781 ** table since it was created.
7782 */
7783 int sqlite3Fts5IndexReads(Fts5Index *p){
7784 return p->nRead;
7785 }
7786
7787 /*
7788 ** Set the 32-bit cookie value stored at the start of all structure
7789 ** records to the value passed as the second argument.
7790 **
7791 ** Return SQLITE_OK if successful, or an SQLite error code if an error
7792 ** occurs.
7793 */
7794 int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
7795 int rc; /* Return code */
7796 Fts5Config *pConfig = p->pConfig; /* Configuration object */
7797 u8 aCookie[4]; /* Binary representation of iNew */
7798 sqlite3_blob *pBlob = 0;
7799
7800 assert( p->rc==SQLITE_OK );
7801 sqlite3Fts5Put32(aCookie, iNew);
7802
7803 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
7804 "block", FTS5_STRUCTURE_ROWID, 1, &pBlob
7805 );
7806 if( rc==SQLITE_OK ){
7807 sqlite3_blob_write(pBlob, aCookie, 4, 0);
7808 rc = sqlite3_blob_close(pBlob);
7809 }
7810
7811 return rc;
7812 }
7813
7814 int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
7815 Fts5Structure *pStruct;
7816 pStruct = fts5StructureRead(p);
7817 fts5StructureRelease(pStruct);
7818 return fts5IndexReturn(p);
7819 }
7820
7821 /*
7822 ** Retrieve the origin value that will be used for the segment currently
7823 ** being accumulated in the in-memory hash table when it is flushed to
7824 ** disk. If successful, SQLITE_OK is returned and (*piOrigin) set to
7825 ** the queried value. Or, if an error occurs, an error code is returned
7826 ** and the final value of (*piOrigin) is undefined.
7827 */
7828 int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin){
7829 Fts5Structure *pStruct;
7830 pStruct = fts5StructureRead(p);
7831 if( pStruct ){
7832 *piOrigin = pStruct->nOriginCntr;
7833 fts5StructureRelease(pStruct);
7834 }
7835 return fts5IndexReturn(p);
7836 }
7837
7838 /*
7839 ** Buffer pPg contains a page of a tombstone hash table - one of nPg pages
7840 ** associated with the same segment. This function adds rowid iRowid to
7841 ** the hash table. The caller is required to guarantee that there is at
7842 ** least one free slot on the page.
7843 **
7844 ** If parameter bForce is false and the hash table is deemed to be full
7845 ** (more than half of the slots are occupied), then non-zero is returned
7846 ** and iRowid not inserted. Or, if bForce is true or if the hash table page
7847 ** is not full, iRowid is inserted and zero returned.
7848 */
7849 static int fts5IndexTombstoneAddToPage(
7850 Fts5Data *pPg,
7851 int bForce,
7852 int nPg,
7853 u64 iRowid
7854 ){
7855 const int szKey = TOMBSTONE_KEYSIZE(pPg);
7856 const int nSlot = TOMBSTONE_NSLOT(pPg);
7857 const int nElem = fts5GetU32(&pPg->p[4]);
7858 int iSlot = (iRowid / nPg) % nSlot;
7859 int nCollide = nSlot;
7860
7861 if( szKey==4 && iRowid>0xFFFFFFFF ) return 2;
7862 if( iRowid==0 ){
7863 pPg->p[1] = 0x01;
7864 return 0;
7865 }
7866
7867 if( bForce==0 && nElem>=(nSlot/2) ){
7868 return 1;
7869 }
7870
7871 fts5PutU32(&pPg->p[4], nElem+1);
7872 if( szKey==4 ){
7873 u32 *aSlot = (u32*)&pPg->p[8];
7874 while( aSlot[iSlot] ){
7875 iSlot = (iSlot + 1) % nSlot;
7876 if( nCollide--==0 ) return 0;
7877 }
7878 fts5PutU32((u8*)&aSlot[iSlot], (u32)iRowid);
7879 }else{
7880 u64 *aSlot = (u64*)&pPg->p[8];
7881 while( aSlot[iSlot] ){
7882 iSlot = (iSlot + 1) % nSlot;
7883 if( nCollide--==0 ) return 0;
7884 }
7885 fts5PutU64((u8*)&aSlot[iSlot], iRowid);
7886 }
7887
7888 return 0;
7889 }
7890
7891 /*
7892 ** This function attempts to build a new hash containing all the keys
7893 ** currently in the tombstone hash table for segment pSeg. The new
7894 ** hash will be stored in the nOut buffers passed in array apOut[].
7895 ** All pages of the new hash use key-size szKey (4 or 8).
7896 **
7897 ** Return 0 if the hash is successfully rebuilt into the nOut pages.
7898 ** Or non-zero if it is not (because one page became overfull). In this
7899 ** case the caller should retry with a larger nOut parameter.
7900 **
7901 ** Parameter pData1 is page iPg1 of the hash table being rebuilt.
7902 */
7903 static int fts5IndexTombstoneRehash(
7904 Fts5Index *p,
7905 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
7906 Fts5Data *pData1, /* One page of current hash - or NULL */
7907 int iPg1, /* Which page of the current hash is pData1 */
7908 int szKey, /* 4 or 8, the keysize */
7909 int nOut, /* Number of output pages */
7910 Fts5Data **apOut /* Array of output hash pages */
7911 ){
7912 int ii;
7913 int res = 0;
7914
7915 /* Initialize the headers of all the output pages */
7916 for(ii=0; ii<nOut; ii++){
7917 apOut[ii]->p[0] = szKey;
7918 fts5PutU32(&apOut[ii]->p[4], 0);
7919 }
7920
7921 /* Loop through the current pages of the hash table. */
7922 for(ii=0; res==0 && ii<pSeg->nPgTombstone; ii++){
7923 Fts5Data *pData = 0; /* Page ii of the current hash table */
7924 Fts5Data *pFree = 0; /* Free this at the end of the loop */
7925
7926 if( iPg1==ii ){
7927 pData = pData1;
7928 }else{
7929 pFree = pData = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid, ii));
7930 }
7931
7932 if( pData ){
7933 int szKeyIn = TOMBSTONE_KEYSIZE(pData);
7934 int nSlotIn = (pData->nn - 8) / szKeyIn;
7935 int iIn;
7936 for(iIn=0; iIn<nSlotIn; iIn++){
7937 u64 iVal = 0;
7938
7939 /* Read the value from slot iIn of the input page into iVal. */
7940 if( szKeyIn==4 ){
7941 u32 *aSlot = (u32*)&pData->p[8];
7942 if( aSlot[iIn] ) iVal = fts5GetU32((u8*)&aSlot[iIn]);
7943 }else{
7944 u64 *aSlot = (u64*)&pData->p[8];
7945 if( aSlot[iIn] ) iVal = fts5GetU64((u8*)&aSlot[iIn]);
7946 }
7947
7948 /* If iVal is not 0 at this point, insert it into the new hash table */
7949 if( iVal ){
7950 Fts5Data *pPg = apOut[(iVal % nOut)];
7951 res = fts5IndexTombstoneAddToPage(pPg, 0, nOut, iVal);
7952 if( res ) break;
7953 }
7954 }
7955
7956 /* If this is page 0 of the old hash, copy the rowid-0-flag from the
7957 ** old hash to the new. */
7958 if( ii==0 ){
7959 apOut[0]->p[1] = pData->p[1];
7960 }
7961 }
7962 fts5DataRelease(pFree);
7963 }
7964
7965 return res;
7966 }
7967
7968 /*
7969 ** This is called to rebuild the hash table belonging to segment pSeg.
7970 ** If parameter pData1 is not NULL, then one page of the existing hash table
7971 ** has already been loaded - pData1, which is page iPg1. The key-size for
7972 ** the new hash table is szKey (4 or 8).
7973 **
7974 ** If successful, the new hash table is not written to disk. Instead,
7975 ** output parameter (*pnOut) is set to the number of pages in the new
7976 ** hash table, and (*papOut) to point to an array of buffers containing
7977 ** the new page data.
7978 **
7979 ** If an error occurs, an error code is left in the Fts5Index object and
7980 ** both output parameters set to 0 before returning.
7981 */
7982 static void fts5IndexTombstoneRebuild(
7983 Fts5Index *p,
7984 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
7985 Fts5Data *pData1, /* One page of current hash - or NULL */
7986 int iPg1, /* Which page of the current hash is pData1 */
7987 int szKey, /* 4 or 8, the keysize */
7988 int *pnOut, /* OUT: Number of output pages */
7989 Fts5Data ***papOut /* OUT: Output hash pages */
7990 ){
7991 const int MINSLOT = 32;
7992 int nSlotPerPage = MAX(MINSLOT, (p->pConfig->pgsz - 8) / szKey);
7993 int nSlot = 0; /* Number of slots in each output page */
7994 int nOut = 0;
7995
7996 /* Figure out how many output pages (nOut) and how many slots per
7997 ** page (nSlot). There are three possibilities:
7998 **
7999 ** 1. The hash table does not yet exist. In this case the new hash
8000 ** table will consist of a single page with MINSLOT slots.
8001 **
8002 ** 2. The hash table exists but is currently a single page. In this
8003 ** case an attempt is made to grow the page to accommodate the new
8004 ** entry. The page is allowed to grow up to nSlotPerPage (see above)
8005 ** slots.
8006 **
8007 ** 3. The hash table already consists of more than one page, or of
8008 ** a single page already so large that it cannot be grown. In this
8009 ** case the new hash consists of (nPg*2+1) pages of nSlotPerPage
8010 ** slots each, where nPg is the current number of pages in the
8011 ** hash table.
8012 */
8013 if( pSeg->nPgTombstone==0 ){
8014 /* Case 1. */
8015 nOut = 1;
8016 nSlot = MINSLOT;
8017 }else if( pSeg->nPgTombstone==1 ){
8018 /* Case 2. */
8019 int nElem = (int)fts5GetU32(&pData1->p[4]);
8020 assert( pData1 && iPg1==0 );
8021 nOut = 1;
8022 nSlot = MAX(nElem*4, MINSLOT);
8023 if( nSlot>nSlotPerPage ) nOut = 0;
8024 }
8025 if( nOut==0 ){
8026 /* Case 3. */
8027 nOut = (pSeg->nPgTombstone * 2 + 1);
8028 nSlot = nSlotPerPage;
8029 }
8030
8031 /* Allocate the required array and output pages */
8032 while( 1 ){
8033 int res = 0;
8034 int ii = 0;
8035 int szPage = 0;
8036 Fts5Data **apOut = 0;
8037
8038 /* Allocate space for the new hash table */
8039 assert( nSlot>=MINSLOT );
8040 apOut = (Fts5Data**)sqlite3Fts5MallocZero(&p->rc, sizeof(Fts5Data*) * nOut);
8041 szPage = 8 + nSlot*szKey;
8042 for(ii=0; ii<nOut; ii++){
8043 Fts5Data *pNew = (Fts5Data*)sqlite3Fts5MallocZero(&p->rc,
8044 sizeof(Fts5Data)+szPage
8045 );
8046 if( pNew ){
8047 pNew->nn = szPage;
8048 pNew->p = (u8*)&pNew[1];
8049 apOut[ii] = pNew;
8050 }
8051 }
8052
8053 /* Rebuild the hash table. */
8054 if( p->rc==SQLITE_OK ){
8055 res = fts5IndexTombstoneRehash(p, pSeg, pData1, iPg1, szKey, nOut, apOut);
8056 }
8057 if( res==0 ){
8058 if( p->rc ){
8059 fts5IndexFreeArray(apOut, nOut);
8060 apOut = 0;
8061 nOut = 0;
8062 }
8063 *pnOut = nOut;
8064 *papOut = apOut;
8065 break;
8066 }
8067
8068 /* If control flows to here, it was not possible to rebuild the hash
8069 ** table. Free all buffers and then try again with more pages. */
8070 assert( p->rc==SQLITE_OK );
8071 fts5IndexFreeArray(apOut, nOut);
8072 nSlot = nSlotPerPage;
8073 nOut = nOut*2 + 1;
8074 }
8075 }
8076
8077
8078 /*
8079 ** Add a tombstone for rowid iRowid to segment pSeg.
8080 */
8081 static void fts5IndexTombstoneAdd(
8082 Fts5Index *p,
8083 Fts5StructureSegment *pSeg,
8084 u64 iRowid
8085 ){
8086 Fts5Data *pPg = 0;
8087 int iPg = -1;
8088 int szKey = 0;
8089 int nHash = 0;
8090 Fts5Data **apHash = 0;
8091
8092 p->nContentlessDelete++;
8093
8094 if( pSeg->nPgTombstone>0 ){
8095 iPg = iRowid % pSeg->nPgTombstone;
8096 pPg = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg));
8097 if( pPg==0 ){
8098 assert( p->rc!=SQLITE_OK );
8099 return;
8100 }
8101
8102 if( 0==fts5IndexTombstoneAddToPage(pPg, 0, pSeg->nPgTombstone, iRowid) ){
8103 fts5DataWrite(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg), pPg->p, pPg->nn);
8104 fts5DataRelease(pPg);
8105 return;
8106 }
8107 }
8108
8109 /* Have to rebuild the hash table. First figure out the key-size (4 or 8). */
8110 szKey = pPg ? TOMBSTONE_KEYSIZE(pPg) : 4;
8111 if( iRowid>0xFFFFFFFF ) szKey = 8;
8112
8113 /* Rebuild the hash table */
8114 fts5IndexTombstoneRebuild(p, pSeg, pPg, iPg, szKey, &nHash, &apHash);
8115 assert( p->rc==SQLITE_OK || (nHash==0 && apHash==0) );
8116
8117 /* If all has succeeded, write the new rowid into one of the new hash
8118 ** table pages, then write them all out to disk. */
8119 if( nHash ){
8120 int ii = 0;
8121 fts5IndexTombstoneAddToPage(apHash[iRowid % nHash], 1, nHash, iRowid);
8122 for(ii=0; ii<nHash; ii++){
8123 i64 iTombstoneRowid = FTS5_TOMBSTONE_ROWID(pSeg->iSegid, ii);
8124 fts5DataWrite(p, iTombstoneRowid, apHash[ii]->p, apHash[ii]->nn);
8125 }
8126 pSeg->nPgTombstone = nHash;
8127 fts5StructureWrite(p, p->pStruct);
8128 }
8129
8130 fts5DataRelease(pPg);
8131 fts5IndexFreeArray(apHash, nHash);
8132 }
8133
8134 /*
8135 ** Add iRowid to the tombstone list of the segment or segments that contain
8136 ** rows from origin iOrigin. Return SQLITE_OK if successful, or an SQLite
8137 ** error code otherwise.
8138 */
8139 int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid){
8140 Fts5Structure *pStruct;
8141 pStruct = fts5StructureRead(p);
8142 if( pStruct ){
8143 int bFound = 0; /* True after pSeg->nEntryTombstone incr. */
8144 int iLvl;
8145 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
8146 int iSeg;
8147 for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){
8148 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
8149 if( pSeg->iOrigin1<=(u64)iOrigin && pSeg->iOrigin2>=(u64)iOrigin ){
8150 if( bFound==0 ){
8151 pSeg->nEntryTombstone++;
8152 bFound = 1;
8153 }
8154 fts5IndexTombstoneAdd(p, pSeg, iRowid);
8155 }
8156 }
8157 }
8158 fts5StructureRelease(pStruct);
8159 }
8160 return fts5IndexReturn(p);
8161 }
8162
8163 /*************************************************************************
8164 **************************************************************************
8165 ** Below this point is the implementation of the integrity-check
8166 ** functionality.
8167 */
8168
8169 /*
8170 ** Return a simple checksum value based on the arguments.
8171 */
8172 u64 sqlite3Fts5IndexEntryCksum(
8173 i64 iRowid,
8174 int iCol,
8175 int iPos,
8176 int iIdx,
8177 const char *pTerm,
8178 int nTerm
8179 ){
8180 int i;
8181 u64 ret = iRowid;
8182 ret += (ret<<3) + iCol;
8183 ret += (ret<<3) + iPos;
8184 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
8185 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
8186 return ret;
8187 }
8188
8189 #ifdef SQLITE_DEBUG
8190 /*
8191 ** This function is purely an internal test. It does not contribute to
8192 ** FTS functionality, or even the integrity-check, in any way.
8193 **
8194 ** Instead, it tests that the same set of pgno/rowid combinations are
8195 ** visited regardless of whether the doclist-index identified by parameters
8196 ** iSegid/iLeaf is iterated in forwards or reverse order.
8197 */
8198 static void fts5TestDlidxReverse(
8199 Fts5Index *p,
8200 int iSegid, /* Segment id to load from */
8201 int iLeaf /* Load doclist-index for this leaf */
8202 ){
8203 Fts5DlidxIter *pDlidx = 0;
8204 u64 cksum1 = 13;
8205 u64 cksum2 = 13;
8206
8207 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
8208 fts5DlidxIterEof(p, pDlidx)==0;
8209 fts5DlidxIterNext(p, pDlidx)
8210 ){
8211 i64 iRowid = fts5DlidxIterRowid(pDlidx);
8212 int pgno = fts5DlidxIterPgno(pDlidx);
8213 assert( pgno>iLeaf );
8214 cksum1 += iRowid + ((i64)pgno<<32);
8215 }
8216 fts5DlidxIterFree(pDlidx);
8217 pDlidx = 0;
8218
8219 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
8220 fts5DlidxIterEof(p, pDlidx)==0;
8221 fts5DlidxIterPrev(p, pDlidx)
8222 ){
8223 i64 iRowid = fts5DlidxIterRowid(pDlidx);
8224 int pgno = fts5DlidxIterPgno(pDlidx);
8225 assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
8226 cksum2 += iRowid + ((i64)pgno<<32);
8227 }
8228 fts5DlidxIterFree(pDlidx);
8229 pDlidx = 0;
8230
8231 if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
8232 }
8233
8234 static int fts5QueryCksum(
8235 Fts5Index *p, /* Fts5 index object */
8236 int iIdx,
8237 const char *z, /* Index key to query for */
8238 int n, /* Size of index key in bytes */
8239 int flags, /* Flags for Fts5IndexQuery */
8240 u64 *pCksum /* IN/OUT: Checksum value */
8241 ){
8242 int eDetail = p->pConfig->eDetail;
8243 u64 cksum = *pCksum;
8244 Fts5IndexIter *pIter = 0;
8245 int rc = sqlite3Fts5IndexQuery(
8246 p, z, n, (flags | FTS5INDEX_QUERY_NOTOKENDATA), 0, &pIter
8247 );
8248
8249 while( rc==SQLITE_OK && ALWAYS(pIter!=0) && 0==sqlite3Fts5IterEof(pIter) ){
8250 i64 rowid = pIter->iRowid;
8251
8252 if( eDetail==FTS5_DETAIL_NONE ){
8253 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
8254 }else{
8255 Fts5PoslistReader sReader;
8256 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
8257 sReader.bEof==0;
8258 sqlite3Fts5PoslistReaderNext(&sReader)
8259 ){
8260 int iCol = FTS5_POS2COLUMN(sReader.iPos);
8261 int iOff = FTS5_POS2OFFSET(sReader.iPos);
8262 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
8263 }
8264 }
8265 if( rc==SQLITE_OK ){
8266 rc = sqlite3Fts5IterNext(pIter);
8267 }
8268 }
8269 fts5IterClose(pIter);
8270
8271 *pCksum = cksum;
8272 return rc;
8273 }
8274
8275 /*
8276 ** Check if buffer z[], size n bytes, contains as series of valid utf-8
8277 ** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
8278 ** contain valid utf-8, return non-zero.
8279 */
8280 static int fts5TestUtf8(const char *z, int n){
8281 int i = 0;
8282 assert_nc( n>0 );
8283 while( i<n ){
8284 if( (z[i] & 0x80)==0x00 ){
8285 i++;
8286 }else
8287 if( (z[i] & 0xE0)==0xC0 ){
8288 if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
8289 i += 2;
8290 }else
8291 if( (z[i] & 0xF0)==0xE0 ){
8292 if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
8293 i += 3;
8294 }else
8295 if( (z[i] & 0xF8)==0xF0 ){
8296 if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
8297 if( (z[i+2] & 0xC0)!=0x80 ) return 1;
8298 i += 3;
8299 }else{
8300 return 1;
8301 }
8302 }
8303
8304 return 0;
8305 }
8306
8307 /*
8308 ** This function is also purely an internal test. It does not contribute to
8309 ** FTS functionality, or even the integrity-check, in any way.
8310 **
8311 ** This function sets output variable (*pbFail) to true if the test fails. Or
8312 ** leaves it unchanged if the test succeeds.
8313 */
8314 static void fts5TestTerm(
8315 Fts5Index *p,
8316 Fts5Buffer *pPrev, /* Previous term */
8317 const char *z, int n, /* Possibly new term to test */
8318 u64 expected,
8319 u64 *pCksum,
8320 int *pbFail
8321 ){
8322 int rc = p->rc;
8323 if( pPrev->n==0 ){
8324 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
8325 }else
8326 if( *pbFail==0
8327 && rc==SQLITE_OK
8328 && (pPrev->n!=n || memcmp(pPrev->p, z, n))
8329 && (p->pHash==0 || p->pHash->nEntry==0)
8330 ){
8331 u64 cksum3 = *pCksum;
8332 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
8333 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
8334 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
8335 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
8336 u64 ck1 = 0;
8337 u64 ck2 = 0;
8338
8339 /* Check that the results returned for ASC and DESC queries are
8340 ** the same. If not, call this corruption. */
8341 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
8342 if( rc==SQLITE_OK ){
8343 int f = flags|FTS5INDEX_QUERY_DESC;
8344 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
8345 }
8346 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
8347
8348 /* If this is a prefix query, check that the results returned if the
8349 ** the index is disabled are the same. In both ASC and DESC order.
8350 **
8351 ** This check may only be performed if the hash table is empty. This
8352 ** is because the hash table only supports a single scan query at
8353 ** a time, and the multi-iter loop from which this function is called
8354 ** is already performing such a scan.
8355 **
8356 ** Also only do this if buffer zTerm contains nTerm bytes of valid
8357 ** utf-8. Otherwise, the last part of the buffer contents might contain
8358 ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
8359 ** character stored in the main fts index, which will cause the
8360 ** test to fail. */
8361 if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
8362 if( iIdx>0 && rc==SQLITE_OK ){
8363 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
8364 ck2 = 0;
8365 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
8366 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
8367 }
8368 if( iIdx>0 && rc==SQLITE_OK ){
8369 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
8370 ck2 = 0;
8371 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
8372 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
8373 }
8374 }
8375
8376 cksum3 ^= ck1;
8377 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
8378
8379 if( rc==SQLITE_OK && cksum3!=expected ){
8380 *pbFail = 1;
8381 }
8382 *pCksum = cksum3;
8383 }
8384 p->rc = rc;
8385 }
8386
8387 #else
8388 # define fts5TestDlidxReverse(x,y,z)
8389 # define fts5TestTerm(t,u,v,w,x,y,z)
8390 #endif
8391
8392 /*
8393 ** Check that:
8394 **
8395 ** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
8396 ** contain zero terms.
8397 ** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
8398 ** contain zero rowids.
8399 */
8400 static void fts5IndexIntegrityCheckEmpty(
8401 Fts5Index *p,
8402 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
8403 int iFirst,
8404 int iNoRowid,
8405 int iLast
8406 ){
8407 int i;
8408
8409 /* Now check that the iter.nEmpty leaves following the current leaf
8410 ** (a) exist and (b) contain no terms. */
8411 for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
8412 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
8413 if( pLeaf ){
8414 if( !fts5LeafIsTermless(pLeaf)
8415 || (i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf))
8416 ){
8417 FTS5_CORRUPT_ROWID(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
8418 }
8419 }
8420 fts5DataRelease(pLeaf);
8421 }
8422 }
8423
8424 static void fts5IntegrityCheckPgidx(Fts5Index *p, i64 iRowid, Fts5Data *pLeaf){
8425 i64 iTermOff = 0;
8426 int ii;
8427
8428 Fts5Buffer buf1 = {0,0,0};
8429 Fts5Buffer buf2 = {0,0,0};
8430
8431 ii = pLeaf->szLeaf;
8432 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
8433 int res;
8434 i64 iOff;
8435 int nIncr;
8436
8437 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
8438 iTermOff += nIncr;
8439 iOff = iTermOff;
8440
8441 if( iOff>=pLeaf->szLeaf ){
8442 FTS5_CORRUPT_ROWID(p, iRowid);
8443 }else if( iTermOff==nIncr ){
8444 int nByte;
8445 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
8446 if( (iOff+nByte)>pLeaf->szLeaf ){
8447 FTS5_CORRUPT_ROWID(p, iRowid);
8448 }else{
8449 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
8450 }
8451 }else{
8452 int nKeep, nByte;
8453 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
8454 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
8455 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
8456 FTS5_CORRUPT_ROWID(p, iRowid);
8457 }else{
8458 buf1.n = nKeep;
8459 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
8460 }
8461
8462 if( p->rc==SQLITE_OK ){
8463 res = fts5BufferCompare(&buf1, &buf2);
8464 if( res<=0 ) FTS5_CORRUPT_ROWID(p, iRowid);
8465 }
8466 }
8467 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
8468 }
8469
8470 fts5BufferFree(&buf1);
8471 fts5BufferFree(&buf2);
8472 }
8473
8474 static void fts5IndexIntegrityCheckSegment(
8475 Fts5Index *p, /* FTS5 backend object */
8476 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
8477 ){
8478 Fts5Config *pConfig = p->pConfig;
8479 int bSecureDelete = (pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE);
8480 sqlite3_stmt *pStmt = 0;
8481 int rc2;
8482 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
8483 int iDlidxPrevLeaf = pSeg->pgnoLast;
8484
8485 if( pSeg->pgnoFirst==0 ) return;
8486
8487 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
8488 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
8489 "ORDER BY 1, 2",
8490 pConfig->zDb, pConfig->zName, pSeg->iSegid
8491 ));
8492
8493 /* Iterate through the b-tree hierarchy. */
8494 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
8495 i64 iRow; /* Rowid for this leaf */
8496 Fts5Data *pLeaf; /* Data for this leaf */
8497
8498 const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, 1);
8499 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
8500 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
8501 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
8502
8503 /* If the leaf in question has already been trimmed from the segment,
8504 ** ignore this b-tree entry. Otherwise, load it into memory. */
8505 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
8506 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
8507 pLeaf = fts5LeafRead(p, iRow);
8508 if( pLeaf==0 ) break;
8509
8510 /* Check that the leaf contains at least one term, and that it is equal
8511 ** to or larger than the split-key in zIdxTerm. Also check that if there
8512 ** is also a rowid pointer within the leaf page header, it points to a
8513 ** location before the term. */
8514 if( pLeaf->nn<=pLeaf->szLeaf ){
8515
8516 if( nIdxTerm==0
8517 && pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE
8518 && pLeaf->nn==pLeaf->szLeaf
8519 && pLeaf->nn==4
8520 ){
8521 /* special case - the very first page in a segment keeps its %_idx
8522 ** entry even if all the terms are removed from it by secure-delete
8523 ** operations. */
8524 }else{
8525 FTS5_CORRUPT_ROWID(p, iRow);
8526 }
8527
8528 }else{
8529 int iOff; /* Offset of first term on leaf */
8530 int iRowidOff; /* Offset of first rowid on leaf */
8531 int nTerm; /* Size of term on leaf in bytes */
8532 int res; /* Comparison of term and split-key */
8533
8534 iOff = fts5LeafFirstTermOff(pLeaf);
8535 iRowidOff = fts5LeafFirstRowidOff(pLeaf);
8536 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
8537 FTS5_CORRUPT_ROWID(p, iRow);
8538 }else{
8539 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
8540 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
8541 if( res==0 ) res = nTerm - nIdxTerm;
8542 if( res<0 ) FTS5_CORRUPT_ROWID(p, iRow);
8543 }
8544
8545 fts5IntegrityCheckPgidx(p, iRow, pLeaf);
8546 }
8547 fts5DataRelease(pLeaf);
8548 if( p->rc ) break;
8549
8550 /* Now check that the iter.nEmpty leaves following the current leaf
8551 ** (a) exist and (b) contain no terms. */
8552 fts5IndexIntegrityCheckEmpty(
8553 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
8554 );
8555 if( p->rc ) break;
8556
8557 /* If there is a doclist-index, check that it looks right. */
8558 if( bIdxDlidx ){
8559 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
8560 int iPrevLeaf = iIdxLeaf;
8561 int iSegid = pSeg->iSegid;
8562 int iPg = 0;
8563 i64 iKey;
8564
8565 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
8566 fts5DlidxIterEof(p, pDlidx)==0;
8567 fts5DlidxIterNext(p, pDlidx)
8568 ){
8569
8570 /* Check any rowid-less pages that occur before the current leaf. */
8571 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
8572 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
8573 pLeaf = fts5DataRead(p, iKey);
8574 if( pLeaf ){
8575 if( fts5LeafFirstRowidOff(pLeaf)!=0 ) FTS5_CORRUPT_ROWID(p, iKey);
8576 fts5DataRelease(pLeaf);
8577 }
8578 }
8579 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
8580
8581 /* Check that the leaf page indicated by the iterator really does
8582 ** contain the rowid suggested by the same. */
8583 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
8584 pLeaf = fts5DataRead(p, iKey);
8585 if( pLeaf ){
8586 i64 iRowid;
8587 int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
8588 ASSERT_SZLEAF_OK(pLeaf);
8589 if( iRowidOff>=pLeaf->szLeaf ){
8590 FTS5_CORRUPT_ROWID(p, iKey);
8591 }else if( bSecureDelete==0 || iRowidOff>0 ){
8592 i64 iDlRowid = fts5DlidxIterRowid(pDlidx);
8593 fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
8594 if( iRowid<iDlRowid || (bSecureDelete==0 && iRowid!=iDlRowid) ){
8595 FTS5_CORRUPT_ROWID(p, iKey);
8596 }
8597 }
8598 fts5DataRelease(pLeaf);
8599 }
8600 }
8601
8602 iDlidxPrevLeaf = iPg;
8603 fts5DlidxIterFree(pDlidx);
8604 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
8605 }else{
8606 iDlidxPrevLeaf = pSeg->pgnoLast;
8607 /* TODO: Check there is no doclist index */
8608 }
8609
8610 iIdxPrevLeaf = iIdxLeaf;
8611 }
8612
8613 rc2 = sqlite3_finalize(pStmt);
8614 if( p->rc==SQLITE_OK ) p->rc = rc2;
8615
8616 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
8617 #if 0
8618 if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
8619 p->rc = FTS5_CORRUPT;
8620 }
8621 #endif
8622 }
8623
8624
8625 /*
8626 ** Run internal checks to ensure that the FTS index (a) is internally
8627 ** consistent and (b) contains entries for which the XOR of the checksums
8628 ** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
8629 **
8630 ** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
8631 ** checksum does not match. Return SQLITE_OK if all checks pass without
8632 ** error, or some other SQLite error code if another error (e.g. OOM)
8633 ** occurs.
8634 */
8635 int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum, int bUseCksum){
8636 int eDetail = p->pConfig->eDetail;
8637 u64 cksum2 = 0; /* Checksum based on contents of indexes */
8638 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
8639 Fts5Iter *pIter; /* Used to iterate through entire index */
8640 Fts5Structure *pStruct; /* Index structure */
8641 int iLvl, iSeg;
8642
8643 #ifdef SQLITE_DEBUG
8644 /* Used by extra internal tests only run if NDEBUG is not defined */
8645 u64 cksum3 = 0; /* Checksum based on contents of indexes */
8646 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
8647 int bTestFail = 0;
8648 #endif
8649 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
8650
8651 /* Load the FTS index structure */
8652 pStruct = fts5StructureRead(p);
8653 if( pStruct==0 ){
8654 assert( p->rc!=SQLITE_OK );
8655 return fts5IndexReturn(p);
8656 }
8657
8658 /* Check that the internal nodes of each segment match the leaves */
8659 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
8660 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
8661 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
8662 fts5IndexIntegrityCheckSegment(p, pSeg);
8663 }
8664 }
8665
8666 /* The cksum argument passed to this function is a checksum calculated
8667 ** based on all expected entries in the FTS index (including prefix index
8668 ** entries). This block checks that a checksum calculated based on the
8669 ** actual contents of FTS index is identical.
8670 **
8671 ** Two versions of the same checksum are calculated. The first (stack
8672 ** variable cksum2) based on entries extracted from the full-text index
8673 ** while doing a linear scan of each individual index in turn.
8674 **
8675 ** As each term visited by the linear scans, a separate query for the
8676 ** same term is performed. cksum3 is calculated based on the entries
8677 ** extracted by these queries.
8678 */
8679 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
8680 fts5MultiIterEof(p, pIter)==0;
8681 fts5MultiIterNext(p, pIter, 0, 0)
8682 ){
8683 int n; /* Size of term in bytes */
8684 i64 iPos = 0; /* Position read from poslist */
8685 int iOff = 0; /* Offset within poslist */
8686 i64 iRowid = fts5MultiIterRowid(pIter);
8687 char *z = (char*)fts5MultiIterTerm(pIter, &n);
8688
8689 /* If this is a new term, query for it. Update cksum3 with the results. */
8690 fts5TestTerm(p, &term, z, n, cksum2, &cksum3, &bTestFail);
8691 if( p->rc ) break;
8692
8693 if( eDetail==FTS5_DETAIL_NONE ){
8694 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
8695 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
8696 }
8697 }else{
8698 poslist.n = 0;
8699 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
8700 fts5BufferAppendBlob(&p->rc, &poslist, 4, (const u8*)"\0\0\0\0");
8701 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
8702 int iCol = FTS5_POS2COLUMN(iPos);
8703 int iTokOff = FTS5_POS2OFFSET(iPos);
8704 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
8705 }
8706 }
8707 }
8708 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3, &bTestFail);
8709
8710 fts5MultiIterFree(pIter);
8711 if( p->rc==SQLITE_OK && bUseCksum && cksum!=cksum2 ){
8712 p->rc = FTS5_CORRUPT;
8713 sqlite3Fts5ConfigErrmsg(p->pConfig,
8714 "fts5: checksum mismatch for table \"%s\"", p->pConfig->zName
8715 );
8716 }
8717 #ifdef SQLITE_DEBUG
8718 /* In SQLITE_DEBUG builds, expensive extra checks were run as part of
8719 ** the integrity-check above. If no other errors were detected, but one
8720 ** of these tests failed, set the result to SQLITE_CORRUPT_VTAB here. */
8721 if( p->rc==SQLITE_OK && bTestFail ){
8722 p->rc = FTS5_CORRUPT;
8723 }
8724 fts5BufferFree(&term);
8725 #endif
8726
8727 fts5StructureRelease(pStruct);
8728 fts5BufferFree(&poslist);
8729 return fts5IndexReturn(p);
8730 }
8731
8732 /*************************************************************************
8733 **************************************************************************
8734 ** Below this point is the implementation of the fts5_decode() scalar
8735 ** function only.
8736 */
8737
8738 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8739 /*
8740 ** Decode a segment-data rowid from the %_data table. This function is
8741 ** the opposite of macro FTS5_SEGMENT_ROWID().
8742 */
8743 static void fts5DecodeRowid(
8744 i64 iRowid, /* Rowid from %_data table */
8745 int *pbTombstone, /* OUT: Tombstone hash flag */
8746 int *piSegid, /* OUT: Segment id */
8747 int *pbDlidx, /* OUT: Dlidx flag */
8748 int *piHeight, /* OUT: Height */
8749 int *piPgno /* OUT: Page number */
8750 ){
8751 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
8752 iRowid >>= FTS5_DATA_PAGE_B;
8753
8754 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
8755 iRowid >>= FTS5_DATA_HEIGHT_B;
8756
8757 *pbDlidx = (int)(iRowid & 0x0001);
8758 iRowid >>= FTS5_DATA_DLI_B;
8759
8760 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
8761 iRowid >>= FTS5_DATA_ID_B;
8762
8763 *pbTombstone = (int)(iRowid & 0x0001);
8764 }
8765 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8766
8767 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8768 static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
8769 int iSegid, iHeight, iPgno, bDlidx, bTomb; /* Rowid components */
8770 fts5DecodeRowid(iKey, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
8771
8772 if( iSegid==0 ){
8773 if( iKey==FTS5_AVERAGES_ROWID ){
8774 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
8775 }else{
8776 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
8777 }
8778 }
8779 else{
8780 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%s%ssegid=%d h=%d pgno=%d}",
8781 bDlidx ? "dlidx " : "",
8782 bTomb ? "tombstone " : "",
8783 iSegid, iHeight, iPgno
8784 );
8785 }
8786 }
8787 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8788
8789 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8790 static void fts5DebugStructure(
8791 int *pRc, /* IN/OUT: error code */
8792 Fts5Buffer *pBuf,
8793 Fts5Structure *p
8794 ){
8795 int iLvl, iSeg; /* Iterate through levels, segments */
8796
8797 for(iLvl=0; iLvl<p->nLevel; iLvl++){
8798 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
8799 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
8800 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
8801 );
8802 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
8803 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
8804 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d",
8805 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
8806 );
8807 if( pSeg->iOrigin1>0 ){
8808 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " origin=%lld..%lld",
8809 pSeg->iOrigin1, pSeg->iOrigin2
8810 );
8811 }
8812 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
8813 }
8814 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
8815 }
8816 }
8817 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8818
8819 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8820 /*
8821 ** This is part of the fts5_decode() debugging aid.
8822 **
8823 ** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
8824 ** function appends a human-readable representation of the same object
8825 ** to the buffer passed as the second argument.
8826 */
8827 static void fts5DecodeStructure(
8828 int *pRc, /* IN/OUT: error code */
8829 Fts5Buffer *pBuf,
8830 const u8 *pBlob, int nBlob
8831 ){
8832 int rc; /* Return code */
8833 Fts5Structure *p = 0; /* Decoded structure object */
8834
8835 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
8836 if( rc!=SQLITE_OK ){
8837 *pRc = rc;
8838 return;
8839 }
8840
8841 fts5DebugStructure(pRc, pBuf, p);
8842 fts5StructureRelease(p);
8843 }
8844 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8845
8846 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8847 /*
8848 ** This is part of the fts5_decode() debugging aid.
8849 **
8850 ** Arguments pBlob/nBlob contain an "averages" record. This function
8851 ** appends a human-readable representation of record to the buffer passed
8852 ** as the second argument.
8853 */
8854 static void fts5DecodeAverages(
8855 int *pRc, /* IN/OUT: error code */
8856 Fts5Buffer *pBuf,
8857 const u8 *pBlob, int nBlob
8858 ){
8859 int i = 0;
8860 const char *zSpace = "";
8861
8862 while( i<nBlob ){
8863 u64 iVal;
8864 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
8865 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
8866 zSpace = " ";
8867 }
8868 }
8869 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8870
8871 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8872 /*
8873 ** Buffer (a/n) is assumed to contain a list of serialized varints. Read
8874 ** each varint and append its string representation to buffer pBuf. Return
8875 ** after either the input buffer is exhausted or a 0 value is read.
8876 **
8877 ** The return value is the number of bytes read from the input buffer.
8878 */
8879 static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
8880 int iOff = 0;
8881 while( iOff<n ){
8882 int iVal;
8883 iOff += fts5GetVarint32(&a[iOff], iVal);
8884 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
8885 }
8886 return iOff;
8887 }
8888 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8889
8890 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8891 /*
8892 ** The start of buffer (a/n) contains the start of a doclist. The doclist
8893 ** may or may not finish within the buffer. This function appends a text
8894 ** representation of the part of the doclist that is present to buffer
8895 ** pBuf.
8896 **
8897 ** The return value is the number of bytes read from the input buffer.
8898 */
8899 static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
8900 i64 iDocid = 0;
8901 int iOff = 0;
8902
8903 if( n>0 ){
8904 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
8905 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
8906 }
8907 while( iOff<n ){
8908 int nPos;
8909 int bDel;
8910 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
8911 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
8912 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
8913 if( iOff<n ){
8914 i64 iDelta;
8915 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
8916 iDocid += iDelta;
8917 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
8918 }
8919 }
8920
8921 return iOff;
8922 }
8923 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8924
8925 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8926 /*
8927 ** This function is part of the fts5_decode() debugging function. It is
8928 ** only ever used with detail=none tables.
8929 **
8930 ** Buffer (pData/nData) contains a doclist in the format used by detail=none
8931 ** tables. This function appends a human-readable version of that list to
8932 ** buffer pBuf.
8933 **
8934 ** If *pRc is other than SQLITE_OK when this function is called, it is a
8935 ** no-op. If an OOM or other error occurs within this function, *pRc is
8936 ** set to an SQLite error code before returning. The final state of buffer
8937 ** pBuf is undefined in this case.
8938 */
8939 static void fts5DecodeRowidList(
8940 int *pRc, /* IN/OUT: Error code */
8941 Fts5Buffer *pBuf, /* Buffer to append text to */
8942 const u8 *pData, int nData /* Data to decode list-of-rowids from */
8943 ){
8944 int i = 0;
8945 i64 iRowid = 0;
8946
8947 while( i<nData ){
8948 const char *zApp = "";
8949 u64 iVal;
8950 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
8951 iRowid += iVal;
8952
8953 if( i<nData && pData[i]==0x00 ){
8954 i++;
8955 if( i<nData && pData[i]==0x00 ){
8956 i++;
8957 zApp = "+";
8958 }else{
8959 zApp = "*";
8960 }
8961 }
8962
8963 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
8964 }
8965 }
8966 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8967
8968 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8969 static void fts5BufferAppendTerm(int *pRc, Fts5Buffer *pBuf, Fts5Buffer *pTerm){
8970 int ii;
8971 fts5BufferGrow(pRc, pBuf, pTerm->n*2 + 1);
8972 if( *pRc==SQLITE_OK ){
8973 for(ii=0; ii<pTerm->n; ii++){
8974 if( pTerm->p[ii]==0x00 ){
8975 pBuf->p[pBuf->n++] = '\\';
8976 pBuf->p[pBuf->n++] = '0';
8977 }else{
8978 pBuf->p[pBuf->n++] = pTerm->p[ii];
8979 }
8980 }
8981 pBuf->p[pBuf->n] = 0x00;
8982 }
8983 }
8984 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
8985
8986 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
8987 /*
8988 ** The implementation of user-defined scalar function fts5_decode().
8989 */
8990 static void fts5DecodeFunction(
8991 sqlite3_context *pCtx, /* Function call context */
8992 int nArg, /* Number of args (always 2) */
8993 sqlite3_value **apVal /* Function arguments */
8994 ){
8995 i64 iRowid; /* Rowid for record being decoded */
8996 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
8997 int bTomb;
8998 const u8 *aBlob; int n; /* Record to decode */
8999 u8 *a = 0;
9000 Fts5Buffer s; /* Build up text to return here */
9001 int rc = SQLITE_OK; /* Return code */
9002 sqlite3_int64 nSpace = 0;
9003 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
9004
9005 assert( nArg==2 );
9006 UNUSED_PARAM(nArg);
9007 memset(&s, 0, sizeof(Fts5Buffer));
9008 iRowid = sqlite3_value_int64(apVal[0]);
9009
9010 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
9011 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
9012 ** buffer overreads even if the record is corrupt. */
9013 n = sqlite3_value_bytes(apVal[1]);
9014 aBlob = sqlite3_value_blob(apVal[1]);
9015 nSpace = ((i64)n) + FTS5_DATA_ZERO_PADDING;
9016 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
9017 if( a==0 ) goto decode_out;
9018 if( n>0 ) memcpy(a, aBlob, n);
9019
9020 fts5DecodeRowid(iRowid, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
9021
9022 fts5DebugRowid(&rc, &s, iRowid);
9023 if( bDlidx ){
9024 Fts5Data dlidx;
9025 Fts5DlidxLvl lvl;
9026
9027 dlidx.p = a;
9028 dlidx.nn = n;
9029
9030 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
9031 lvl.pData = &dlidx;
9032 lvl.iLeafPgno = iPgno;
9033
9034 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
9035 sqlite3Fts5BufferAppendPrintf(&rc, &s,
9036 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
9037 );
9038 }
9039 }else if( bTomb ){
9040 u32 nElem = fts5GetU32(&a[4]);
9041 int szKey = (aBlob[0]==4 || aBlob[0]==8) ? aBlob[0] : 8;
9042 int nSlot = (n - 8) / szKey;
9043 int ii;
9044 sqlite3Fts5BufferAppendPrintf(&rc, &s, " nElem=%d", (int)nElem);
9045 if( aBlob[1] ){
9046 sqlite3Fts5BufferAppendPrintf(&rc, &s, " 0");
9047 }
9048 for(ii=0; ii<nSlot; ii++){
9049 u64 iVal = 0;
9050 if( szKey==4 ){
9051 u32 *aSlot = (u32*)&aBlob[8];
9052 if( aSlot[ii] ) iVal = fts5GetU32((u8*)&aSlot[ii]);
9053 }else{
9054 u64 *aSlot = (u64*)&aBlob[8];
9055 if( aSlot[ii] ) iVal = fts5GetU64((u8*)&aSlot[ii]);
9056 }
9057 if( iVal!=0 ){
9058 sqlite3Fts5BufferAppendPrintf(&rc, &s, " %lld", (i64)iVal);
9059 }
9060 }
9061 }else if( iSegid==0 ){
9062 if( iRowid==FTS5_AVERAGES_ROWID ){
9063 fts5DecodeAverages(&rc, &s, a, n);
9064 }else{
9065 fts5DecodeStructure(&rc, &s, a, n);
9066 }
9067 }else if( eDetailNone ){
9068 Fts5Buffer term; /* Current term read from page */
9069 int szLeaf;
9070 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
9071 int iTermOff;
9072 int nKeep = 0;
9073 int iOff;
9074
9075 memset(&term, 0, sizeof(Fts5Buffer));
9076
9077 /* Decode any entries that occur before the first term. */
9078 if( szLeaf<n ){
9079 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
9080 }else{
9081 iTermOff = szLeaf;
9082 }
9083 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
9084
9085 iOff = iTermOff;
9086 while( iOff<szLeaf && rc==SQLITE_OK ){
9087 int nAppend;
9088
9089 /* Read the term data for the next term*/
9090 iOff += fts5GetVarint32(&a[iOff], nAppend);
9091 term.n = nKeep;
9092 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
9093 sqlite3Fts5BufferAppendPrintf(&rc, &s, " term=");
9094 fts5BufferAppendTerm(&rc, &s, &term);
9095 iOff += nAppend;
9096
9097 /* Figure out where the doclist for this term ends */
9098 if( iPgidxOff<n ){
9099 int nIncr;
9100 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
9101 iTermOff += nIncr;
9102 }else{
9103 iTermOff = szLeaf;
9104 }
9105 if( iTermOff>szLeaf ){
9106 rc = FTS5_CORRUPT;
9107 }else{
9108 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
9109 }
9110 iOff = iTermOff;
9111 if( iOff<szLeaf ){
9112 iOff += fts5GetVarint32(&a[iOff], nKeep);
9113 }
9114 }
9115
9116 fts5BufferFree(&term);
9117 }else{
9118 Fts5Buffer term; /* Current term read from page */
9119 int szLeaf; /* Offset of pgidx in a[] */
9120 int iPgidxOff;
9121 int iPgidxPrev = 0; /* Previous value read from pgidx */
9122 int iTermOff = 0;
9123 int iRowidOff = 0;
9124 int iOff;
9125 int nDoclist;
9126
9127 memset(&term, 0, sizeof(Fts5Buffer));
9128
9129 if( n<4 ){
9130 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
9131 goto decode_out;
9132 }else{
9133 iRowidOff = fts5GetU16(&a[0]);
9134 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
9135 if( iPgidxOff<n ){
9136 fts5GetVarint32(&a[iPgidxOff], iTermOff);
9137 }else if( iPgidxOff>n ){
9138 rc = FTS5_CORRUPT;
9139 goto decode_out;
9140 }
9141 }
9142
9143 /* Decode the position list tail at the start of the page */
9144 if( iRowidOff!=0 ){
9145 iOff = iRowidOff;
9146 }else if( iTermOff!=0 ){
9147 iOff = iTermOff;
9148 }else{
9149 iOff = szLeaf;
9150 }
9151 if( iOff>n ){
9152 rc = FTS5_CORRUPT;
9153 goto decode_out;
9154 }
9155 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
9156
9157 /* Decode any more doclist data that appears on the page before the
9158 ** first term. */
9159 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
9160 if( nDoclist+iOff>n ){
9161 rc = FTS5_CORRUPT;
9162 goto decode_out;
9163 }
9164 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
9165
9166 while( iPgidxOff<n && rc==SQLITE_OK ){
9167 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
9168 int nByte; /* Bytes of data */
9169 int iEnd;
9170
9171 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
9172 iPgidxPrev += nByte;
9173 iOff = iPgidxPrev;
9174
9175 if( iPgidxOff<n ){
9176 fts5GetVarint32(&a[iPgidxOff], nByte);
9177 iEnd = iPgidxPrev + nByte;
9178 }else{
9179 iEnd = szLeaf;
9180 }
9181 if( iEnd>szLeaf ){
9182 rc = FTS5_CORRUPT;
9183 break;
9184 }
9185
9186 if( bFirst==0 ){
9187 iOff += fts5GetVarint32(&a[iOff], nByte);
9188 if( nByte>term.n ){
9189 rc = FTS5_CORRUPT;
9190 break;
9191 }
9192 term.n = nByte;
9193 }
9194 iOff += fts5GetVarint32(&a[iOff], nByte);
9195 if( iOff+nByte>n ){
9196 rc = FTS5_CORRUPT;
9197 break;
9198 }
9199 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
9200 iOff += nByte;
9201
9202 sqlite3Fts5BufferAppendPrintf(&rc, &s, " term=");
9203 fts5BufferAppendTerm(&rc, &s, &term);
9204 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
9205 }
9206
9207 fts5BufferFree(&term);
9208 }
9209
9210 decode_out:
9211 sqlite3_free(a);
9212 if( rc==SQLITE_OK ){
9213 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
9214 }else{
9215 sqlite3_result_error_code(pCtx, rc);
9216 }
9217 fts5BufferFree(&s);
9218 }
9219 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
9220
9221 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
9222 /*
9223 ** The implementation of user-defined scalar function fts5_rowid().
9224 */
9225 static void fts5RowidFunction(
9226 sqlite3_context *pCtx, /* Function call context */
9227 int nArg, /* Number of args (always 2) */
9228 sqlite3_value **apVal /* Function arguments */
9229 ){
9230 const char *zArg;
9231 if( nArg==0 ){
9232 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
9233 }else{
9234 zArg = (const char*)sqlite3_value_text(apVal[0]);
9235 if( 0==sqlite3_stricmp(zArg, "segment") ){
9236 i64 iRowid;
9237 int segid, pgno;
9238 if( nArg!=3 ){
9239 sqlite3_result_error(pCtx,
9240 "should be: fts5_rowid('segment', segid, pgno))", -1
9241 );
9242 }else{
9243 segid = sqlite3_value_int(apVal[1]);
9244 pgno = sqlite3_value_int(apVal[2]);
9245 iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
9246 sqlite3_result_int64(pCtx, iRowid);
9247 }
9248 }else{
9249 sqlite3_result_error(pCtx,
9250 "first arg to fts5_rowid() must be 'segment'" , -1
9251 );
9252 }
9253 }
9254 }
9255 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
9256
9257 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
9258
9259 typedef struct Fts5StructVtab Fts5StructVtab;
9260 struct Fts5StructVtab {
9261 sqlite3_vtab base;
9262 };
9263
9264 typedef struct Fts5StructVcsr Fts5StructVcsr;
9265 struct Fts5StructVcsr {
9266 sqlite3_vtab_cursor base;
9267 Fts5Structure *pStruct;
9268 int iLevel;
9269 int iSeg;
9270 int iRowid;
9271 };
9272
9273 /*
9274 ** Create a new fts5_structure() table-valued function.
9275 */
9276 static int fts5structConnectMethod(
9277 sqlite3 *db,
9278 void *pAux,
9279 int argc, const char *const*argv,
9280 sqlite3_vtab **ppVtab,
9281 char **pzErr
9282 ){
9283 Fts5StructVtab *pNew = 0;
9284 int rc = SQLITE_OK;
9285
9286 rc = sqlite3_declare_vtab(db,
9287 "CREATE TABLE xyz("
9288 "level, segment, merge, segid, leaf1, leaf2, loc1, loc2, "
9289 "npgtombstone, nentrytombstone, nentry, struct HIDDEN);"
9290 );
9291 if( rc==SQLITE_OK ){
9292 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
9293 }
9294
9295 *ppVtab = (sqlite3_vtab*)pNew;
9296 return rc;
9297 }
9298
9299 /*
9300 ** We must have a single struct=? constraint that will be passed through
9301 ** into the xFilter method. If there is no valid struct=? constraint,
9302 ** then return an SQLITE_CONSTRAINT error.
9303 */
9304 static int fts5structBestIndexMethod(
9305 sqlite3_vtab *tab,
9306 sqlite3_index_info *pIdxInfo
9307 ){
9308 int i;
9309 int rc = SQLITE_CONSTRAINT;
9310 struct sqlite3_index_constraint *p;
9311 pIdxInfo->estimatedCost = (double)100;
9312 pIdxInfo->estimatedRows = 100;
9313 pIdxInfo->idxNum = 0;
9314 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
9315 if( p->usable==0 ) continue;
9316 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==11 ){
9317 rc = SQLITE_OK;
9318 pIdxInfo->aConstraintUsage[i].omit = 1;
9319 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
9320 break;
9321 }
9322 }
9323 return rc;
9324 }
9325
9326 /*
9327 ** This method is the destructor for bytecodevtab objects.
9328 */
9329 static int fts5structDisconnectMethod(sqlite3_vtab *pVtab){
9330 Fts5StructVtab *p = (Fts5StructVtab*)pVtab;
9331 sqlite3_free(p);
9332 return SQLITE_OK;
9333 }
9334
9335 /*
9336 ** Constructor for a new bytecodevtab_cursor object.
9337 */
9338 static int fts5structOpenMethod(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
9339 int rc = SQLITE_OK;
9340 Fts5StructVcsr *pNew = 0;
9341
9342 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
9343 *ppCsr = (sqlite3_vtab_cursor*)pNew;
9344
9345 return SQLITE_OK;
9346 }
9347
9348 /*
9349 ** Destructor for a bytecodevtab_cursor.
9350 */
9351 static int fts5structCloseMethod(sqlite3_vtab_cursor *cur){
9352 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
9353 fts5StructureRelease(pCsr->pStruct);
9354 sqlite3_free(pCsr);
9355 return SQLITE_OK;
9356 }
9357
9358
9359 /*
9360 ** Advance a bytecodevtab_cursor to its next row of output.
9361 */
9362 static int fts5structNextMethod(sqlite3_vtab_cursor *cur){
9363 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
9364 Fts5Structure *p = pCsr->pStruct;
9365
9366 assert( pCsr->pStruct );
9367 pCsr->iSeg++;
9368 pCsr->iRowid++;
9369 while( pCsr->iLevel<p->nLevel && pCsr->iSeg>=p->aLevel[pCsr->iLevel].nSeg ){
9370 pCsr->iLevel++;
9371 pCsr->iSeg = 0;
9372 }
9373 if( pCsr->iLevel>=p->nLevel ){
9374 fts5StructureRelease(pCsr->pStruct);
9375 pCsr->pStruct = 0;
9376 }
9377 return SQLITE_OK;
9378 }
9379
9380 /*
9381 ** Return TRUE if the cursor has been moved off of the last
9382 ** row of output.
9383 */
9384 static int fts5structEofMethod(sqlite3_vtab_cursor *cur){
9385 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
9386 return pCsr->pStruct==0;
9387 }
9388
9389 static int fts5structRowidMethod(
9390 sqlite3_vtab_cursor *cur,
9391 sqlite_int64 *piRowid
9392 ){
9393 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
9394 *piRowid = pCsr->iRowid;
9395 return SQLITE_OK;
9396 }
9397
9398 /*
9399 ** Return values of columns for the row at which the bytecodevtab_cursor
9400 ** is currently pointing.
9401 */
9402 static int fts5structColumnMethod(
9403 sqlite3_vtab_cursor *cur, /* The cursor */
9404 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
9405 int i /* Which column to return */
9406 ){
9407 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
9408 Fts5Structure *p = pCsr->pStruct;
9409 Fts5StructureSegment *pSeg = &p->aLevel[pCsr->iLevel].aSeg[pCsr->iSeg];
9410
9411 switch( i ){
9412 case 0: /* level */
9413 sqlite3_result_int(ctx, pCsr->iLevel);
9414 break;
9415 case 1: /* segment */
9416 sqlite3_result_int(ctx, pCsr->iSeg);
9417 break;
9418 case 2: /* merge */
9419 sqlite3_result_int(ctx, pCsr->iSeg < p->aLevel[pCsr->iLevel].nMerge);
9420 break;
9421 case 3: /* segid */
9422 sqlite3_result_int(ctx, pSeg->iSegid);
9423 break;
9424 case 4: /* leaf1 */
9425 sqlite3_result_int(ctx, pSeg->pgnoFirst);
9426 break;
9427 case 5: /* leaf2 */
9428 sqlite3_result_int(ctx, pSeg->pgnoLast);
9429 break;
9430 case 6: /* origin1 */
9431 sqlite3_result_int64(ctx, pSeg->iOrigin1);
9432 break;
9433 case 7: /* origin2 */
9434 sqlite3_result_int64(ctx, pSeg->iOrigin2);
9435 break;
9436 case 8: /* npgtombstone */
9437 sqlite3_result_int(ctx, pSeg->nPgTombstone);
9438 break;
9439 case 9: /* nentrytombstone */
9440 sqlite3_result_int64(ctx, pSeg->nEntryTombstone);
9441 break;
9442 case 10: /* nentry */
9443 sqlite3_result_int64(ctx, pSeg->nEntry);
9444 break;
9445 }
9446 return SQLITE_OK;
9447 }
9448
9449 /*
9450 ** Initialize a cursor.
9451 **
9452 ** idxNum==0 means show all subprograms
9453 ** idxNum==1 means show only the main bytecode and omit subprograms.
9454 */
9455 static int fts5structFilterMethod(
9456 sqlite3_vtab_cursor *pVtabCursor,
9457 int idxNum, const char *idxStr,
9458 int argc, sqlite3_value **argv
9459 ){
9460 Fts5StructVcsr *pCsr = (Fts5StructVcsr *)pVtabCursor;
9461 int rc = SQLITE_OK;
9462
9463 const u8 *aBlob = 0;
9464 int nBlob = 0;
9465
9466 assert( argc==1 );
9467 fts5StructureRelease(pCsr->pStruct);
9468 pCsr->pStruct = 0;
9469
9470 nBlob = sqlite3_value_bytes(argv[0]);
9471 aBlob = (const u8*)sqlite3_value_blob(argv[0]);
9472 rc = fts5StructureDecode(aBlob, nBlob, 0, &pCsr->pStruct);
9473 if( rc==SQLITE_OK ){
9474 pCsr->iLevel = 0;
9475 pCsr->iRowid = 0;
9476 pCsr->iSeg = -1;
9477 rc = fts5structNextMethod(pVtabCursor);
9478 }
9479
9480 return rc;
9481 }
9482
9483 #endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
9484
9485 /*
9486 ** This is called as part of registering the FTS5 module with database
9487 ** connection db. It registers several user-defined scalar functions useful
9488 ** with FTS5.
9489 **
9490 ** If successful, SQLITE_OK is returned. If an error occurs, some other
9491 ** SQLite error code is returned instead.
9492 */
9493 int sqlite3Fts5IndexInit(sqlite3 *db){
9494 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
9495 int rc = sqlite3_create_function(
9496 db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
9497 );
9498
9499 if( rc==SQLITE_OK ){
9500 rc = sqlite3_create_function(
9501 db, "fts5_decode_none", 2,
9502 SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
9503 );
9504 }
9505
9506 if( rc==SQLITE_OK ){
9507 rc = sqlite3_create_function(
9508 db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
9509 );
9510 }
9511
9512 if( rc==SQLITE_OK ){
9513 static const sqlite3_module fts5structure_module = {
9514 0, /* iVersion */
9515 0, /* xCreate */
9516 fts5structConnectMethod, /* xConnect */
9517 fts5structBestIndexMethod, /* xBestIndex */
9518 fts5structDisconnectMethod, /* xDisconnect */
9519 0, /* xDestroy */
9520 fts5structOpenMethod, /* xOpen */
9521 fts5structCloseMethod, /* xClose */
9522 fts5structFilterMethod, /* xFilter */
9523 fts5structNextMethod, /* xNext */
9524 fts5structEofMethod, /* xEof */
9525 fts5structColumnMethod, /* xColumn */
9526 fts5structRowidMethod, /* xRowid */
9527 0, /* xUpdate */
9528 0, /* xBegin */
9529 0, /* xSync */
9530 0, /* xCommit */
9531 0, /* xRollback */
9532 0, /* xFindFunction */
9533 0, /* xRename */
9534 0, /* xSavepoint */
9535 0, /* xRelease */
9536 0, /* xRollbackTo */
9537 0, /* xShadowName */
9538 0 /* xIntegrity */
9539 };
9540 rc = sqlite3_create_module(db, "fts5_structure", &fts5structure_module, 0);
9541 }
9542 return rc;
9543 #else
9544 return SQLITE_OK;
9545 UNUSED_PARAM(db);
9546 #endif
9547 }
9548
9549
9550 int sqlite3Fts5IndexReset(Fts5Index *p){
9551 assert( p->pStruct==0 || p->iStructVersion!=0 );
9552 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
9553 fts5StructureInvalidate(p);
9554 }
9555 return fts5IndexReturn(p);
9556 }