fts3Int.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. ** 2009 Nov 12
  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. */
  14. #ifndef _FTSINT_H
  15. #define _FTSINT_H
  16. #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  17. # define NDEBUG 1
  18. #endif
  19. /* FTS3/FTS4 require virtual tables */
  20. #ifdef SQLITE_OMIT_VIRTUALTABLE
  21. # undef SQLITE_ENABLE_FTS3
  22. # undef SQLITE_ENABLE_FTS4
  23. #endif
  24. /*
  25. ** FTS4 is really an extension for FTS3. It is enabled using the
  26. ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
  27. ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
  28. */
  29. #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
  30. # define SQLITE_ENABLE_FTS3
  31. #endif
  32. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  33. /* If not building as part of the core, include sqlite3ext.h. */
  34. #ifndef SQLITE_CORE
  35. # include "sqlite3ext.h"
  36. SQLITE_EXTENSION_INIT3
  37. #endif
  38. #include "sqlite3.h"
  39. #include "fts3_tokenizer.h"
  40. #include "fts3_hash.h"
  41. /*
  42. ** This constant determines the maximum depth of an FTS expression tree
  43. ** that the library will create and use. FTS uses recursion to perform
  44. ** various operations on the query tree, so the disadvantage of a large
  45. ** limit is that it may allow very large queries to use large amounts
  46. ** of stack space (perhaps causing a stack overflow).
  47. */
  48. #ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
  49. # define SQLITE_FTS3_MAX_EXPR_DEPTH 12
  50. #endif
  51. /*
  52. ** This constant controls how often segments are merged. Once there are
  53. ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
  54. ** segment of level N+1.
  55. */
  56. #define FTS3_MERGE_COUNT 16
  57. /*
  58. ** This is the maximum amount of data (in bytes) to store in the
  59. ** Fts3Table.pendingTerms hash table. Normally, the hash table is
  60. ** populated as documents are inserted/updated/deleted in a transaction
  61. ** and used to create a new segment when the transaction is committed.
  62. ** However if this limit is reached midway through a transaction, a new
  63. ** segment is created and the hash table cleared immediately.
  64. */
  65. #define FTS3_MAX_PENDING_DATA (1*1024*1024)
  66. /*
  67. ** Macro to return the number of elements in an array. SQLite has a
  68. ** similar macro called ArraySize(). Use a different name to avoid
  69. ** a collision when building an amalgamation with built-in FTS3.
  70. */
  71. #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
  72. #ifndef MIN
  73. # define MIN(x,y) ((x)<(y)?(x):(y))
  74. #endif
  75. #ifndef MAX
  76. # define MAX(x,y) ((x)>(y)?(x):(y))
  77. #endif
  78. /*
  79. ** Maximum length of a varint encoded integer. The varint format is different
  80. ** from that used by SQLite, so the maximum length is 10, not 9.
  81. */
  82. #define FTS3_VARINT_MAX 10
  83. #define FTS3_BUFFER_PADDING 8
  84. /*
  85. ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
  86. ** in the document set and zero or more prefix indexes. All indexes are stored
  87. ** as one or more b+-trees in the %_segments and %_segdir tables.
  88. **
  89. ** It is possible to determine which index a b+-tree belongs to based on the
  90. ** value stored in the "%_segdir.level" column. Given this value L, the index
  91. ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
  92. ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
  93. ** between 1024 and 2047 to index 1, and so on.
  94. **
  95. ** It is considered impossible for an index to use more than 1024 levels. In
  96. ** theory though this may happen, but only after at least
  97. ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
  98. */
  99. #define FTS3_SEGDIR_MAXLEVEL 1024
  100. #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
  101. /*
  102. ** The testcase() macro is only used by the amalgamation. If undefined,
  103. ** make it a no-op.
  104. */
  105. #ifndef testcase
  106. # define testcase(X)
  107. #endif
  108. /*
  109. ** Terminator values for position-lists and column-lists.
  110. */
  111. #define POS_COLUMN (1) /* Column-list terminator */
  112. #define POS_END (0) /* Position-list terminator */
  113. /*
  114. ** The assert_fts3_nc() macro is similar to the assert() macro, except that it
  115. ** is used for assert() conditions that are true only if it can be
  116. ** guranteed that the database is not corrupt.
  117. */
  118. #ifdef SQLITE_DEBUG
  119. extern int sqlite3_fts3_may_be_corrupt;
  120. # define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x))
  121. #else
  122. # define assert_fts3_nc(x) assert(x)
  123. #endif
  124. /*
  125. ** This section provides definitions to allow the
  126. ** FTS3 extension to be compiled outside of the
  127. ** amalgamation.
  128. */
  129. #ifndef SQLITE_AMALGAMATION
  130. /*
  131. ** Macros indicating that conditional expressions are always true or
  132. ** false.
  133. */
  134. #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
  135. # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
  136. #endif
  137. #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
  138. # define ALWAYS(X) (1)
  139. # define NEVER(X) (0)
  140. #elif !defined(NDEBUG)
  141. # define ALWAYS(X) ((X)?1:(assert(0),0))
  142. # define NEVER(X) ((X)?(assert(0),1):0)
  143. #else
  144. # define ALWAYS(X) (X)
  145. # define NEVER(X) (X)
  146. #endif
  147. /*
  148. ** Internal types used by SQLite.
  149. */
  150. typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
  151. typedef short int i16; /* 2-byte (or larger) signed integer */
  152. typedef unsigned int u32; /* 4-byte unsigned integer */
  153. typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
  154. typedef sqlite3_int64 i64; /* 8-byte signed integer */
  155. /*
  156. ** Macro used to suppress compiler warnings for unused parameters.
  157. */
  158. #define UNUSED_PARAMETER(x) (void)(x)
  159. /*
  160. ** Activate assert() only if SQLITE_TEST is enabled.
  161. */
  162. #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  163. # define NDEBUG 1
  164. #endif
  165. /*
  166. ** The TESTONLY macro is used to enclose variable declarations or
  167. ** other bits of code that are needed to support the arguments
  168. ** within testcase() and assert() macros.
  169. */
  170. #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  171. # define TESTONLY(X) X
  172. #else
  173. # define TESTONLY(X)
  174. #endif
  175. #define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
  176. #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  177. #define deliberate_fall_through
  178. #endif /* SQLITE_AMALGAMATION */
  179. #ifdef SQLITE_DEBUG
  180. int sqlite3Fts3Corrupt(void);
  181. # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
  182. #else
  183. # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
  184. #endif
  185. typedef struct Fts3Table Fts3Table;
  186. typedef struct Fts3Cursor Fts3Cursor;
  187. typedef struct Fts3Expr Fts3Expr;
  188. typedef struct Fts3Phrase Fts3Phrase;
  189. typedef struct Fts3PhraseToken Fts3PhraseToken;
  190. typedef struct Fts3Doclist Fts3Doclist;
  191. typedef struct Fts3SegFilter Fts3SegFilter;
  192. typedef struct Fts3DeferredToken Fts3DeferredToken;
  193. typedef struct Fts3SegReader Fts3SegReader;
  194. typedef struct Fts3MultiSegReader Fts3MultiSegReader;
  195. typedef struct MatchinfoBuffer MatchinfoBuffer;
  196. /*
  197. ** A connection to a fulltext index is an instance of the following
  198. ** structure. The xCreate and xConnect methods create an instance
  199. ** of this structure and xDestroy and xDisconnect free that instance.
  200. ** All other methods receive a pointer to the structure as one of their
  201. ** arguments.
  202. */
  203. struct Fts3Table {
  204. sqlite3_vtab base; /* Base class used by SQLite core */
  205. sqlite3 *db; /* The database connection */
  206. const char *zDb; /* logical database name */
  207. const char *zName; /* virtual table name */
  208. int nColumn; /* number of named columns in virtual table */
  209. char **azColumn; /* column names. malloced */
  210. u8 *abNotindexed; /* True for 'notindexed' columns */
  211. sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
  212. char *zContentTbl; /* content=xxx option, or NULL */
  213. char *zLanguageid; /* languageid=xxx option, or NULL */
  214. int nAutoincrmerge; /* Value configured by 'automerge' */
  215. u32 nLeafAdd; /* Number of leaf blocks added this trans */
  216. int bLock; /* Used to prevent recursive content= tbls */
  217. /* Precompiled statements used by the implementation. Each of these
  218. ** statements is run and reset within a single virtual table API call.
  219. */
  220. sqlite3_stmt *aStmt[40];
  221. sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
  222. char *zReadExprlist;
  223. char *zWriteExprlist;
  224. int nNodeSize; /* Soft limit for node size */
  225. u8 bFts4; /* True for FTS4, false for FTS3 */
  226. u8 bHasStat; /* True if %_stat table exists (2==unknown) */
  227. u8 bHasDocsize; /* True if %_docsize table exists */
  228. u8 bDescIdx; /* True if doclists are in reverse order */
  229. u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
  230. int nPgsz; /* Page size for host database */
  231. char *zSegmentsTbl; /* Name of %_segments table */
  232. sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
  233. int iSavepoint;
  234. /*
  235. ** The following array of hash tables is used to buffer pending index
  236. ** updates during transactions. All pending updates buffered at any one
  237. ** time must share a common language-id (see the FTS4 langid= feature).
  238. ** The current language id is stored in variable iPrevLangid.
  239. **
  240. ** A single FTS4 table may have multiple full-text indexes. For each index
  241. ** there is an entry in the aIndex[] array. Index 0 is an index of all the
  242. ** terms that appear in the document set. Each subsequent index in aIndex[]
  243. ** is an index of prefixes of a specific length.
  244. **
  245. ** Variable nPendingData contains an estimate the memory consumed by the
  246. ** pending data structures, including hash table overhead, but not including
  247. ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
  248. ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
  249. ** recently inserted record.
  250. */
  251. int nIndex; /* Size of aIndex[] */
  252. struct Fts3Index {
  253. int nPrefix; /* Prefix length (0 for main terms index) */
  254. Fts3Hash hPending; /* Pending terms table for this index */
  255. } *aIndex;
  256. int nMaxPendingData; /* Max pending data before flush to disk */
  257. int nPendingData; /* Current bytes of pending data */
  258. sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
  259. int iPrevLangid; /* Langid of recently inserted document */
  260. int bPrevDelete; /* True if last operation was a delete */
  261. #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  262. /* State variables used for validating that the transaction control
  263. ** methods of the virtual table are called at appropriate times. These
  264. ** values do not contribute to FTS functionality; they are used for
  265. ** verifying the operation of the SQLite core.
  266. */
  267. int inTransaction; /* True after xBegin but before xCommit/xRollback */
  268. int mxSavepoint; /* Largest valid xSavepoint integer */
  269. #endif
  270. #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  271. /* True to disable the incremental doclist optimization. This is controled
  272. ** by special insert command 'test-no-incr-doclist'. */
  273. int bNoIncrDoclist;
  274. /* Number of segments in a level */
  275. int nMergeCount;
  276. #endif
  277. };
  278. /* Macro to find the number of segments to merge */
  279. #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  280. # define MergeCount(P) ((P)->nMergeCount)
  281. #else
  282. # define MergeCount(P) FTS3_MERGE_COUNT
  283. #endif
  284. /*
  285. ** When the core wants to read from the virtual table, it creates a
  286. ** virtual table cursor (an instance of the following structure) using
  287. ** the xOpen method. Cursors are destroyed using the xClose method.
  288. */
  289. struct Fts3Cursor {
  290. sqlite3_vtab_cursor base; /* Base class used by SQLite core */
  291. i16 eSearch; /* Search strategy (see below) */
  292. u8 isEof; /* True if at End Of Results */
  293. u8 isRequireSeek; /* True if must seek pStmt to %_content row */
  294. u8 bSeekStmt; /* True if pStmt is a seek */
  295. sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
  296. Fts3Expr *pExpr; /* Parsed MATCH query string */
  297. int iLangid; /* Language being queried for */
  298. int nPhrase; /* Number of matchable phrases in query */
  299. Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
  300. sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
  301. char *pNextId; /* Pointer into the body of aDoclist */
  302. char *aDoclist; /* List of docids for full-text queries */
  303. int nDoclist; /* Size of buffer at aDoclist */
  304. u8 bDesc; /* True to sort in descending order */
  305. int eEvalmode; /* An FTS3_EVAL_XX constant */
  306. int nRowAvg; /* Average size of database rows, in pages */
  307. sqlite3_int64 nDoc; /* Documents in table */
  308. i64 iMinDocid; /* Minimum docid to return */
  309. i64 iMaxDocid; /* Maximum docid to return */
  310. int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
  311. MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
  312. };
  313. #define FTS3_EVAL_FILTER 0
  314. #define FTS3_EVAL_NEXT 1
  315. #define FTS3_EVAL_MATCHINFO 2
  316. /*
  317. ** The Fts3Cursor.eSearch member is always set to one of the following.
  318. ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
  319. ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
  320. ** of the column to be searched. For example, in
  321. **
  322. ** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
  323. ** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
  324. **
  325. ** Because the LHS of the MATCH operator is 2nd column "b",
  326. ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
  327. ** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
  328. ** indicating that all columns should be searched,
  329. ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
  330. */
  331. #define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
  332. #define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
  333. #define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
  334. /*
  335. ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
  336. ** the xBestIndex() method contains the Fts3Cursor.eSearch value described
  337. ** above. The upper 16-bits contain a combination of the following
  338. ** bits, used to describe extra constraints on full-text searches.
  339. */
  340. #define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */
  341. #define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */
  342. #define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */
  343. struct Fts3Doclist {
  344. char *aAll; /* Array containing doclist (or NULL) */
  345. int nAll; /* Size of a[] in bytes */
  346. char *pNextDocid; /* Pointer to next docid */
  347. sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
  348. int bFreeList; /* True if pList should be sqlite3_free()d */
  349. char *pList; /* Pointer to position list following iDocid */
  350. int nList; /* Length of position list */
  351. };
  352. /*
  353. ** A "phrase" is a sequence of one or more tokens that must match in
  354. ** sequence. A single token is the base case and the most common case.
  355. ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
  356. ** nToken will be the number of tokens in the string.
  357. */
  358. struct Fts3PhraseToken {
  359. char *z; /* Text of the token */
  360. int n; /* Number of bytes in buffer z */
  361. int isPrefix; /* True if token ends with a "*" character */
  362. int bFirst; /* True if token must appear at position 0 */
  363. /* Variables above this point are populated when the expression is
  364. ** parsed (by code in fts3_expr.c). Below this point the variables are
  365. ** used when evaluating the expression. */
  366. Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
  367. Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
  368. };
  369. struct Fts3Phrase {
  370. /* Cache of doclist for this phrase. */
  371. Fts3Doclist doclist;
  372. int bIncr; /* True if doclist is loaded incrementally */
  373. int iDoclistToken;
  374. /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
  375. ** OR condition. */
  376. char *pOrPoslist;
  377. i64 iOrDocid;
  378. /* Variables below this point are populated by fts3_expr.c when parsing
  379. ** a MATCH expression. Everything above is part of the evaluation phase.
  380. */
  381. int nToken; /* Number of tokens in the phrase */
  382. int iColumn; /* Index of column this phrase must match */
  383. Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
  384. };
  385. /*
  386. ** A tree of these objects forms the RHS of a MATCH operator.
  387. **
  388. ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
  389. ** points to a malloced buffer, size nDoclist bytes, containing the results
  390. ** of this phrase query in FTS3 doclist format. As usual, the initial
  391. ** "Length" field found in doclists stored on disk is omitted from this
  392. ** buffer.
  393. **
  394. ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
  395. ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
  396. ** where nCol is the number of columns in the queried FTS table. The array
  397. ** is populated as follows:
  398. **
  399. ** aMI[iCol*3 + 0] = Undefined
  400. ** aMI[iCol*3 + 1] = Number of occurrences
  401. ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
  402. **
  403. ** The aMI array is allocated using sqlite3_malloc(). It should be freed
  404. ** when the expression node is.
  405. */
  406. struct Fts3Expr {
  407. int eType; /* One of the FTSQUERY_XXX values defined below */
  408. int nNear; /* Valid if eType==FTSQUERY_NEAR */
  409. Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
  410. Fts3Expr *pLeft; /* Left operand */
  411. Fts3Expr *pRight; /* Right operand */
  412. Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
  413. /* The following are used by the fts3_eval.c module. */
  414. sqlite3_int64 iDocid; /* Current docid */
  415. u8 bEof; /* True this expression is at EOF already */
  416. u8 bStart; /* True if iDocid is valid */
  417. u8 bDeferred; /* True if this expression is entirely deferred */
  418. /* The following are used by the fts3_snippet.c module. */
  419. int iPhrase; /* Index of this phrase in matchinfo() results */
  420. u32 *aMI; /* See above */
  421. };
  422. /*
  423. ** Candidate values for Fts3Query.eType. Note that the order of the first
  424. ** four values is in order of precedence when parsing expressions. For
  425. ** example, the following:
  426. **
  427. ** "a OR b AND c NOT d NEAR e"
  428. **
  429. ** is equivalent to:
  430. **
  431. ** "a OR (b AND (c NOT (d NEAR e)))"
  432. */
  433. #define FTSQUERY_NEAR 1
  434. #define FTSQUERY_NOT 2
  435. #define FTSQUERY_AND 3
  436. #define FTSQUERY_OR 4
  437. #define FTSQUERY_PHRASE 5
  438. /* fts3_write.c */
  439. int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
  440. int sqlite3Fts3PendingTermsFlush(Fts3Table *);
  441. void sqlite3Fts3PendingTermsClear(Fts3Table *);
  442. int sqlite3Fts3Optimize(Fts3Table *);
  443. int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
  444. sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
  445. int sqlite3Fts3SegReaderPending(
  446. Fts3Table*,int,const char*,int,int,Fts3SegReader**);
  447. void sqlite3Fts3SegReaderFree(Fts3SegReader *);
  448. int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
  449. int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
  450. int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
  451. int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
  452. #ifndef SQLITE_DISABLE_FTS4_DEFERRED
  453. void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
  454. int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
  455. int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
  456. void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
  457. int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
  458. #else
  459. # define sqlite3Fts3FreeDeferredTokens(x)
  460. # define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
  461. # define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
  462. # define sqlite3Fts3FreeDeferredDoclists(x)
  463. # define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
  464. #endif
  465. void sqlite3Fts3SegmentsClose(Fts3Table *);
  466. int sqlite3Fts3MaxLevel(Fts3Table *, int *);
  467. /* Special values interpreted by sqlite3SegReaderCursor() */
  468. #define FTS3_SEGCURSOR_PENDING -1
  469. #define FTS3_SEGCURSOR_ALL -2
  470. int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
  471. int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
  472. void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
  473. int sqlite3Fts3SegReaderCursor(Fts3Table *,
  474. int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
  475. /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
  476. #define FTS3_SEGMENT_REQUIRE_POS 0x00000001
  477. #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
  478. #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
  479. #define FTS3_SEGMENT_PREFIX 0x00000008
  480. #define FTS3_SEGMENT_SCAN 0x00000010
  481. #define FTS3_SEGMENT_FIRST 0x00000020
  482. /* Type passed as 4th argument to SegmentReaderIterate() */
  483. struct Fts3SegFilter {
  484. const char *zTerm;
  485. int nTerm;
  486. int iCol;
  487. int flags;
  488. };
  489. struct Fts3MultiSegReader {
  490. /* Used internally by sqlite3Fts3SegReaderXXX() calls */
  491. Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
  492. int nSegment; /* Size of apSegment array */
  493. int nAdvance; /* How many seg-readers to advance */
  494. Fts3SegFilter *pFilter; /* Pointer to filter object */
  495. char *aBuffer; /* Buffer to merge doclists in */
  496. i64 nBuffer; /* Allocated size of aBuffer[] in bytes */
  497. int iColFilter; /* If >=0, filter for this column */
  498. int bRestart;
  499. /* Used by fts3.c only. */
  500. int nCost; /* Cost of running iterator */
  501. int bLookup; /* True if a lookup of a single entry. */
  502. /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
  503. char *zTerm; /* Pointer to term buffer */
  504. int nTerm; /* Size of zTerm in bytes */
  505. char *aDoclist; /* Pointer to doclist buffer */
  506. int nDoclist; /* Size of aDoclist[] in bytes */
  507. };
  508. int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
  509. #define fts3GetVarint32(p, piVal) ( \
  510. (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
  511. )
  512. /* fts3.c */
  513. void sqlite3Fts3ErrMsg(char**,const char*,...);
  514. int sqlite3Fts3PutVarint(char *, sqlite3_int64);
  515. int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
  516. int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
  517. int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
  518. int sqlite3Fts3GetVarint32(const char *, int *);
  519. int sqlite3Fts3VarintLen(sqlite3_uint64);
  520. void sqlite3Fts3Dequote(char *);
  521. void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
  522. int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
  523. int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
  524. void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
  525. int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
  526. int sqlite3Fts3ReadInt(const char *z, int *pnOut);
  527. /* fts3_tokenizer.c */
  528. const char *sqlite3Fts3NextToken(const char *, int *);
  529. int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
  530. int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
  531. sqlite3_tokenizer **, char **
  532. );
  533. int sqlite3Fts3IsIdChar(char);
  534. /* fts3_snippet.c */
  535. void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
  536. void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
  537. const char *, const char *, int, int
  538. );
  539. void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
  540. void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
  541. /* fts3_expr.c */
  542. int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
  543. char **, int, int, int, const char *, int, Fts3Expr **, char **
  544. );
  545. void sqlite3Fts3ExprFree(Fts3Expr *);
  546. #ifdef SQLITE_TEST
  547. int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
  548. int sqlite3Fts3InitTerm(sqlite3 *db);
  549. #endif
  550. void *sqlite3Fts3MallocZero(i64 nByte);
  551. int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
  552. sqlite3_tokenizer_cursor **
  553. );
  554. /* fts3_aux.c */
  555. int sqlite3Fts3InitAux(sqlite3 *db);
  556. void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
  557. int sqlite3Fts3MsrIncrStart(
  558. Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
  559. int sqlite3Fts3MsrIncrNext(
  560. Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
  561. int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
  562. int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
  563. int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
  564. int sqlite3Fts3MsrCancel(Fts3Cursor*, Fts3Expr*);
  565. /* fts3_tokenize_vtab.c */
  566. int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*));
  567. /* fts3_unicode2.c (functions generated by parsing unicode text files) */
  568. #ifndef SQLITE_DISABLE_FTS3_UNICODE
  569. int sqlite3FtsUnicodeFold(int, int);
  570. int sqlite3FtsUnicodeIsalnum(int);
  571. int sqlite3FtsUnicodeIsdiacritic(int);
  572. #endif
  573. int sqlite3Fts3ExprIterate(Fts3Expr*, int (*x)(Fts3Expr*,int,void*), void*);
  574. int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk);
  575. #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
  576. #endif /* _FTSINT_H */