4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This is an SQLite module implementing full-text search.
19 #define FTS5_DEFAULT_PAGE_SIZE 4050
20 #define FTS5_DEFAULT_AUTOMERGE 4
21 #define FTS5_DEFAULT_USERMERGE 4
22 #define FTS5_DEFAULT_CRISISMERGE 16
23 #define FTS5_DEFAULT_HASHSIZE (1024*1024)
25 /* Maximum allowed page size */
26 #define FTS5_MAX_PAGE_SIZE (64*1024)
28 static int fts5_iswhitespace(char x
){
32 static int fts5_isopenquote(char x
){
33 return (x
=='"' || x
=='\'' || x
=='[' || x
=='`');
37 ** Argument pIn points to a character that is part of a nul-terminated
38 ** string. Return a pointer to the first character following *pIn in
39 ** the string that is not a white-space character.
41 static const char *fts5ConfigSkipWhitespace(const char *pIn
){
44 while( fts5_iswhitespace(*p
) ){ p
++; }
50 ** Argument pIn points to a character that is part of a nul-terminated
51 ** string. Return a pointer to the first character following *pIn in
52 ** the string that is not a "bareword" character.
54 static const char *fts5ConfigSkipBareword(const char *pIn
){
56 while ( sqlite3Fts5IsBareword(*p
) ) p
++;
61 static int fts5_isdigit(char a
){
62 return (a
>='0' && a
<='9');
67 static const char *fts5ConfigSkipLiteral(const char *pIn
){
71 if( sqlite3_strnicmp("null", p
, 4)==0 ){
82 while( (*p
>='a' && *p
<='f')
83 || (*p
>='A' && *p
<='F')
84 || (*p
>='0' && *p
<='9')
88 if( *p
=='\'' && 0==((p
-pIn
)%2) ){
103 if( *p
!='\'' ) break;
112 if( *p
=='+' || *p
=='-' ) p
++;
113 while( fts5_isdigit(*p
) ) p
++;
115 /* At this point, if the literal was an integer, the parse is
116 ** finished. Or, if it is a floating point value, it may continue
117 ** with either a decimal point or an 'E' character. */
118 if( *p
=='.' && fts5_isdigit(p
[1]) ){
120 while( fts5_isdigit(*p
) ) p
++;
131 ** The first character of the string pointed to by argument z is guaranteed
132 ** to be an open-quote character (see function fts5_isopenquote()).
134 ** This function searches for the corresponding close-quote character within
135 ** the string and, if found, dequotes the string in place and adds a new
136 ** nul-terminator byte.
138 ** If the close-quote is found, the value returned is the byte offset of
139 ** the character immediately following it. Or, if the close-quote is not
140 ** found, -1 is returned. If -1 is returned, the buffer is left in an
143 static int fts5Dequote(char *z
){
149 /* Set stack variable q to the close-quote character */
150 assert( q
=='[' || q
=='\'' || q
=='"' || q
=='`' );
151 if( q
=='[' ) q
= ']';
156 /* Character iIn was the close quote. */
160 /* Character iIn and iIn+1 form an escaped quote character. Skip
161 ** the input cursor past both and copy a single quote character
162 ** to the output buffer. */
167 z
[iOut
++] = z
[iIn
++];
176 ** Convert an SQL-style quoted string into a normal string by removing
177 ** the quote characters. The conversion is done in-place. If the
178 ** input does not begin with a quote character, then this routine
188 void sqlite3Fts5Dequote(char *z
){
189 char quote
; /* Quote character (if any ) */
191 assert( 0==fts5_iswhitespace(z
[0]) );
193 if( quote
=='[' || quote
=='\'' || quote
=='"' || quote
=='`' ){
203 typedef struct Fts5Enum Fts5Enum
;
205 static int fts5ConfigSetEnum(
206 const Fts5Enum
*aEnum
,
210 int nEnum
= (int)strlen(zEnum
);
214 for(i
=0; aEnum
[i
].zName
; i
++){
215 if( sqlite3_strnicmp(aEnum
[i
].zName
, zEnum
, nEnum
)==0 ){
216 if( iVal
>=0 ) return SQLITE_ERROR
;
217 iVal
= aEnum
[i
].eVal
;
222 return iVal
<0 ? SQLITE_ERROR
: SQLITE_OK
;
226 ** Parse a "special" CREATE VIRTUAL TABLE directive and update
227 ** configuration object pConfig as appropriate.
229 ** If successful, object pConfig is updated and SQLITE_OK returned. If
230 ** an error occurs, an SQLite error code is returned and an error message
231 ** may be left in *pzErr. It is the responsibility of the caller to
232 ** eventually free any such error message using sqlite3_free().
234 static int fts5ConfigParseSpecial(
236 Fts5Config
*pConfig
, /* Configuration object to update */
237 const char *zCmd
, /* Special command to parse */
238 const char *zArg
, /* Argument to parse */
239 char **pzErr
/* OUT: Error message */
242 int nCmd
= (int)strlen(zCmd
);
243 if( sqlite3_strnicmp("prefix", zCmd
, nCmd
)==0 ){
244 const int nByte
= sizeof(int) * FTS5_MAX_PREFIX_INDEXES
;
247 if( pConfig
->aPrefix
==0 ){
248 pConfig
->aPrefix
= sqlite3Fts5MallocZero(&rc
, nByte
);
256 while( p
[0]==' ' ) p
++;
257 if( bFirst
==0 && p
[0]==',' ){
259 while( p
[0]==' ' ) p
++;
260 }else if( p
[0]=='\0' ){
263 if( p
[0]<'0' || p
[0]>'9' ){
264 *pzErr
= sqlite3_mprintf("malformed prefix=... directive");
269 if( pConfig
->nPrefix
==FTS5_MAX_PREFIX_INDEXES
){
270 *pzErr
= sqlite3_mprintf(
271 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
277 while( p
[0]>='0' && p
[0]<='9' && nPre
<1000 ){
278 nPre
= nPre
*10 + (p
[0] - '0');
282 if( nPre
<=0 || nPre
>=1000 ){
283 *pzErr
= sqlite3_mprintf("prefix length out of range (max 999)");
288 pConfig
->aPrefix
[pConfig
->nPrefix
] = nPre
;
292 assert( pConfig
->nPrefix
<=FTS5_MAX_PREFIX_INDEXES
);
296 if( sqlite3_strnicmp("tokenize", zCmd
, nCmd
)==0 ){
297 const char *p
= (const char*)zArg
;
298 sqlite3_int64 nArg
= strlen(zArg
) + 1;
299 char **azArg
= sqlite3Fts5MallocZero(&rc
, sizeof(char*) * nArg
);
300 char *pDel
= sqlite3Fts5MallocZero(&rc
, nArg
* 2);
303 if( azArg
&& pSpace
){
305 *pzErr
= sqlite3_mprintf("multiple tokenize=... directives");
308 for(nArg
=0; p
&& *p
; nArg
++){
309 const char *p2
= fts5ConfigSkipWhitespace(p
);
311 p
= fts5ConfigSkipLiteral(p2
);
313 p
= fts5ConfigSkipBareword(p2
);
316 memcpy(pSpace
, p2
, p
-p2
);
317 azArg
[nArg
] = pSpace
;
318 sqlite3Fts5Dequote(pSpace
);
319 pSpace
+= (p
- p2
) + 1;
320 p
= fts5ConfigSkipWhitespace(p
);
324 *pzErr
= sqlite3_mprintf("parse error in tokenize directive");
327 rc
= sqlite3Fts5GetTokenizer(pGlobal
,
328 (const char**)azArg
, (int)nArg
, pConfig
,
340 if( sqlite3_strnicmp("content", zCmd
, nCmd
)==0 ){
341 if( pConfig
->eContent
!=FTS5_CONTENT_NORMAL
){
342 *pzErr
= sqlite3_mprintf("multiple content=... directives");
346 pConfig
->eContent
= FTS5_CONTENT_EXTERNAL
;
347 pConfig
->zContent
= sqlite3Fts5Mprintf(&rc
, "%Q.%Q", pConfig
->zDb
,zArg
);
349 pConfig
->eContent
= FTS5_CONTENT_NONE
;
355 if( sqlite3_strnicmp("content_rowid", zCmd
, nCmd
)==0 ){
356 if( pConfig
->zContentRowid
){
357 *pzErr
= sqlite3_mprintf("multiple content_rowid=... directives");
360 pConfig
->zContentRowid
= sqlite3Fts5Strndup(&rc
, zArg
, -1);
365 if( sqlite3_strnicmp("columnsize", zCmd
, nCmd
)==0 ){
366 if( (zArg
[0]!='0' && zArg
[0]!='1') || zArg
[1]!='\0' ){
367 *pzErr
= sqlite3_mprintf("malformed columnsize=... directive");
370 pConfig
->bColumnsize
= (zArg
[0]=='1');
375 if( sqlite3_strnicmp("detail", zCmd
, nCmd
)==0 ){
376 const Fts5Enum aDetail
[] = {
377 { "none", FTS5_DETAIL_NONE
},
378 { "full", FTS5_DETAIL_FULL
},
379 { "columns", FTS5_DETAIL_COLUMNS
},
383 if( (rc
= fts5ConfigSetEnum(aDetail
, zArg
, &pConfig
->eDetail
)) ){
384 *pzErr
= sqlite3_mprintf("malformed detail=... directive");
389 *pzErr
= sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd
, zCmd
);
394 ** Allocate an instance of the default tokenizer ("simple") at
395 ** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
396 ** code if an error occurs.
398 static int fts5ConfigDefaultTokenizer(Fts5Global
*pGlobal
, Fts5Config
*pConfig
){
399 assert( pConfig
->pTok
==0 && pConfig
->pTokApi
==0 );
400 return sqlite3Fts5GetTokenizer(pGlobal
, 0, 0, pConfig
, 0);
404 ** Gobble up the first bareword or quoted word from the input buffer zIn.
405 ** Return a pointer to the character immediately following the last in
406 ** the gobbled word if successful, or a NULL pointer otherwise (failed
407 ** to find close-quote character).
409 ** Before returning, set pzOut to point to a new buffer containing a
410 ** nul-terminated, dequoted copy of the gobbled word. If the word was
411 ** quoted, *pbQuoted is also set to 1 before returning.
413 ** If *pRc is other than SQLITE_OK when this function is called, it is
414 ** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
415 ** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
416 ** set if a parse error (failed to find close quote) occurs.
418 static const char *fts5ConfigGobbleWord(
419 int *pRc
, /* IN/OUT: Error code */
420 const char *zIn
, /* Buffer to gobble string/bareword from */
421 char **pzOut
, /* OUT: malloc'd buffer containing str/bw */
422 int *pbQuoted
/* OUT: Set to true if dequoting required */
424 const char *zRet
= 0;
426 sqlite3_int64 nIn
= strlen(zIn
);
427 char *zOut
= sqlite3_malloc64(nIn
+1);
429 assert( *pRc
==SQLITE_OK
);
436 memcpy(zOut
, zIn
, (size_t)(nIn
+1));
437 if( fts5_isopenquote(zOut
[0]) ){
438 int ii
= fts5Dequote(zOut
);
442 zRet
= fts5ConfigSkipBareword(zIn
);
444 zOut
[zRet
-zIn
] = '\0';
458 static int fts5ConfigParseColumn(
465 if( 0==sqlite3_stricmp(zCol
, FTS5_RANK_NAME
)
466 || 0==sqlite3_stricmp(zCol
, FTS5_ROWID_NAME
)
468 *pzErr
= sqlite3_mprintf("reserved fts5 column name: %s", zCol
);
471 if( 0==sqlite3_stricmp(zArg
, "unindexed") ){
472 p
->abUnindexed
[p
->nCol
] = 1;
474 *pzErr
= sqlite3_mprintf("unrecognized column option: %s", zArg
);
479 p
->azCol
[p
->nCol
++] = zCol
;
484 ** Populate the Fts5Config.zContentExprlist string.
486 static int fts5ConfigMakeExprlist(Fts5Config
*p
){
489 Fts5Buffer buf
= {0, 0, 0};
491 sqlite3Fts5BufferAppendPrintf(&rc
, &buf
, "T.%Q", p
->zContentRowid
);
492 if( p
->eContent
!=FTS5_CONTENT_NONE
){
493 for(i
=0; i
<p
->nCol
; i
++){
494 if( p
->eContent
==FTS5_CONTENT_EXTERNAL
){
495 sqlite3Fts5BufferAppendPrintf(&rc
, &buf
, ", T.%Q", p
->azCol
[i
]);
497 sqlite3Fts5BufferAppendPrintf(&rc
, &buf
, ", T.c%d", i
);
502 assert( p
->zContentExprlist
==0 );
503 p
->zContentExprlist
= (char*)buf
.p
;
508 ** Arguments nArg/azArg contain the string arguments passed to the xCreate
509 ** or xConnect method of the virtual table. This function attempts to
510 ** allocate an instance of Fts5Config containing the results of parsing
513 ** If successful, SQLITE_OK is returned and *ppOut is set to point to the
514 ** new Fts5Config object. If an error occurs, an SQLite error code is
515 ** returned, *ppOut is set to NULL and an error message may be left in
516 ** *pzErr. It is the responsibility of the caller to eventually free any
517 ** such error message using sqlite3_free().
519 int sqlite3Fts5ConfigParse(
522 int nArg
, /* Number of arguments */
523 const char **azArg
, /* Array of nArg CREATE VIRTUAL TABLE args */
524 Fts5Config
**ppOut
, /* OUT: Results of parse */
525 char **pzErr
/* OUT: Error message */
527 int rc
= SQLITE_OK
; /* Return code */
528 Fts5Config
*pRet
; /* New object to return */
532 *ppOut
= pRet
= (Fts5Config
*)sqlite3_malloc(sizeof(Fts5Config
));
533 if( pRet
==0 ) return SQLITE_NOMEM
;
534 memset(pRet
, 0, sizeof(Fts5Config
));
538 nByte
= nArg
* (sizeof(char*) + sizeof(u8
));
539 pRet
->azCol
= (char**)sqlite3Fts5MallocZero(&rc
, nByte
);
540 pRet
->abUnindexed
= pRet
->azCol
? (u8
*)&pRet
->azCol
[nArg
] : 0;
541 pRet
->zDb
= sqlite3Fts5Strndup(&rc
, azArg
[1], -1);
542 pRet
->zName
= sqlite3Fts5Strndup(&rc
, azArg
[2], -1);
543 pRet
->bColumnsize
= 1;
544 pRet
->eDetail
= FTS5_DETAIL_FULL
;
546 pRet
->bPrefixIndex
= 1;
548 if( rc
==SQLITE_OK
&& sqlite3_stricmp(pRet
->zName
, FTS5_RANK_NAME
)==0 ){
549 *pzErr
= sqlite3_mprintf("reserved fts5 table name: %s", pRet
->zName
);
553 assert( (pRet
->abUnindexed
&& pRet
->azCol
) || rc
!=SQLITE_OK
);
554 for(i
=3; rc
==SQLITE_OK
&& i
<nArg
; i
++){
555 const char *zOrig
= azArg
[i
];
562 z
= fts5ConfigGobbleWord(&rc
, zOrig
, &zOne
, &bMustBeCol
);
563 z
= fts5ConfigSkipWhitespace(z
);
568 if( bMustBeCol
) z
= 0;
570 z
= fts5ConfigSkipWhitespace(z
);
573 z
= fts5ConfigGobbleWord(&rc
, z
, &zTwo
, &bDummy
);
574 if( z
&& z
[0] ) z
= 0;
579 *pzErr
= sqlite3_mprintf("parse error in \"%s\"", zOrig
);
583 rc
= fts5ConfigParseSpecial(pGlobal
, pRet
,
584 ALWAYS(zOne
)?zOne
:"",
589 rc
= fts5ConfigParseColumn(pRet
, zOne
, zTwo
, pzErr
);
599 /* If a tokenizer= option was successfully parsed, the tokenizer has
600 ** already been allocated. Otherwise, allocate an instance of the default
601 ** tokenizer (unicode61) now. */
602 if( rc
==SQLITE_OK
&& pRet
->pTok
==0 ){
603 rc
= fts5ConfigDefaultTokenizer(pGlobal
, pRet
);
606 /* If no zContent option was specified, fill in the default values. */
607 if( rc
==SQLITE_OK
&& pRet
->zContent
==0 ){
608 const char *zTail
= 0;
609 assert( pRet
->eContent
==FTS5_CONTENT_NORMAL
610 || pRet
->eContent
==FTS5_CONTENT_NONE
612 if( pRet
->eContent
==FTS5_CONTENT_NORMAL
){
614 }else if( pRet
->bColumnsize
){
619 pRet
->zContent
= sqlite3Fts5Mprintf(
620 &rc
, "%Q.'%q_%s'", pRet
->zDb
, pRet
->zName
, zTail
625 if( rc
==SQLITE_OK
&& pRet
->zContentRowid
==0 ){
626 pRet
->zContentRowid
= sqlite3Fts5Strndup(&rc
, "rowid", -1);
629 /* Formulate the zContentExprlist text */
631 rc
= fts5ConfigMakeExprlist(pRet
);
635 sqlite3Fts5ConfigFree(pRet
);
642 ** Free the configuration object passed as the only argument.
644 void sqlite3Fts5ConfigFree(Fts5Config
*pConfig
){
648 pConfig
->pTokApi
->xDelete(pConfig
->pTok
);
650 sqlite3_free(pConfig
->zDb
);
651 sqlite3_free(pConfig
->zName
);
652 for(i
=0; i
<pConfig
->nCol
; i
++){
653 sqlite3_free(pConfig
->azCol
[i
]);
655 sqlite3_free(pConfig
->azCol
);
656 sqlite3_free(pConfig
->aPrefix
);
657 sqlite3_free(pConfig
->zRank
);
658 sqlite3_free(pConfig
->zRankArgs
);
659 sqlite3_free(pConfig
->zContent
);
660 sqlite3_free(pConfig
->zContentRowid
);
661 sqlite3_free(pConfig
->zContentExprlist
);
662 sqlite3_free(pConfig
);
667 ** Call sqlite3_declare_vtab() based on the contents of the configuration
668 ** object passed as the only argument. Return SQLITE_OK if successful, or
669 ** an SQLite error code if an error occurs.
671 int sqlite3Fts5ConfigDeclareVtab(Fts5Config
*pConfig
){
676 zSql
= sqlite3Fts5Mprintf(&rc
, "CREATE TABLE x(");
677 for(i
=0; zSql
&& i
<pConfig
->nCol
; i
++){
678 const char *zSep
= (i
==0?"":", ");
679 zSql
= sqlite3Fts5Mprintf(&rc
, "%z%s%Q", zSql
, zSep
, pConfig
->azCol
[i
]);
681 zSql
= sqlite3Fts5Mprintf(&rc
, "%z, %Q HIDDEN, %s HIDDEN)",
682 zSql
, pConfig
->zName
, FTS5_RANK_NAME
685 assert( zSql
|| rc
==SQLITE_NOMEM
);
687 rc
= sqlite3_declare_vtab(pConfig
->db
, zSql
);
695 ** Tokenize the text passed via the second and third arguments.
697 ** The callback is invoked once for each token in the input text. The
698 ** arguments passed to it are, in order:
700 ** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
701 ** const char *pToken // Pointer to buffer containing token
702 ** int nToken // Size of token in bytes
703 ** int iStart // Byte offset of start of token within input text
704 ** int iEnd // Byte offset of end of token within input text
705 ** int iPos // Position of token in input (first token is 0)
707 ** If the callback returns a non-zero value the tokenization is abandoned
708 ** and no further callbacks are issued.
710 ** This function returns SQLITE_OK if successful or an SQLite error code
711 ** if an error occurs. If the tokenization was abandoned early because
712 ** the callback returned SQLITE_DONE, this is not an error and this function
713 ** still returns SQLITE_OK. Or, if the tokenization was abandoned early
714 ** because the callback returned another non-zero value, it is assumed
715 ** to be an SQLite error code and returned to the caller.
717 int sqlite3Fts5Tokenize(
718 Fts5Config
*pConfig
, /* FTS5 Configuration object */
719 int flags
, /* FTS5_TOKENIZE_* flags */
720 const char *pText
, int nText
, /* Text to tokenize */
721 void *pCtx
, /* Context passed to xToken() */
722 int (*xToken
)(void*, int, const char*, int, int, int) /* Callback */
724 if( pText
==0 ) return SQLITE_OK
;
725 return pConfig
->pTokApi
->xTokenize(
726 pConfig
->pTok
, pCtx
, flags
, pText
, nText
, xToken
731 ** Argument pIn points to the first character in what is expected to be
732 ** a comma-separated list of SQL literals followed by a ')' character.
733 ** If it actually is this, return a pointer to the ')'. Otherwise, return
734 ** NULL to indicate a parse error.
736 static const char *fts5ConfigSkipArgs(const char *pIn
){
740 p
= fts5ConfigSkipWhitespace(p
);
741 p
= fts5ConfigSkipLiteral(p
);
742 p
= fts5ConfigSkipWhitespace(p
);
743 if( p
==0 || *p
==')' ) break;
755 ** Parameter zIn contains a rank() function specification. The format of
758 ** + Bareword (function name)
759 ** + Open parenthesis - "("
760 ** + Zero or more SQL literals in a comma separated list
761 ** + Close parenthesis - ")"
763 int sqlite3Fts5ConfigParseRank(
764 const char *zIn
, /* Input string */
765 char **pzRank
, /* OUT: Rank function name */
766 char **pzRankArgs
/* OUT: Rank function arguments */
780 p
= fts5ConfigSkipWhitespace(p
);
782 p
= fts5ConfigSkipBareword(p
);
785 zRank
= sqlite3Fts5MallocZero(&rc
, 1 + p
- pRank
);
786 if( zRank
) memcpy(zRank
, pRank
, p
-pRank
);
792 p
= fts5ConfigSkipWhitespace(p
);
793 if( *p
!='(' ) rc
= SQLITE_ERROR
;
798 p
= fts5ConfigSkipWhitespace(p
);
801 p
= fts5ConfigSkipArgs(p
);
805 zRankArgs
= sqlite3Fts5MallocZero(&rc
, 1 + p
- pArgs
);
806 if( zRankArgs
) memcpy(zRankArgs
, pArgs
, p
-pArgs
);
814 assert( zRankArgs
==0 );
817 *pzRankArgs
= zRankArgs
;
822 int sqlite3Fts5ConfigSetValue(
830 if( 0==sqlite3_stricmp(zKey
, "pgsz") ){
832 if( SQLITE_INTEGER
==sqlite3_value_numeric_type(pVal
) ){
833 pgsz
= sqlite3_value_int(pVal
);
835 if( pgsz
<32 || pgsz
>FTS5_MAX_PAGE_SIZE
){
838 pConfig
->pgsz
= pgsz
;
842 else if( 0==sqlite3_stricmp(zKey
, "hashsize") ){
844 if( SQLITE_INTEGER
==sqlite3_value_numeric_type(pVal
) ){
845 nHashSize
= sqlite3_value_int(pVal
);
850 pConfig
->nHashSize
= nHashSize
;
854 else if( 0==sqlite3_stricmp(zKey
, "automerge") ){
856 if( SQLITE_INTEGER
==sqlite3_value_numeric_type(pVal
) ){
857 nAutomerge
= sqlite3_value_int(pVal
);
859 if( nAutomerge
<0 || nAutomerge
>64 ){
862 if( nAutomerge
==1 ) nAutomerge
= FTS5_DEFAULT_AUTOMERGE
;
863 pConfig
->nAutomerge
= nAutomerge
;
867 else if( 0==sqlite3_stricmp(zKey
, "usermerge") ){
869 if( SQLITE_INTEGER
==sqlite3_value_numeric_type(pVal
) ){
870 nUsermerge
= sqlite3_value_int(pVal
);
872 if( nUsermerge
<2 || nUsermerge
>16 ){
875 pConfig
->nUsermerge
= nUsermerge
;
879 else if( 0==sqlite3_stricmp(zKey
, "crisismerge") ){
880 int nCrisisMerge
= -1;
881 if( SQLITE_INTEGER
==sqlite3_value_numeric_type(pVal
) ){
882 nCrisisMerge
= sqlite3_value_int(pVal
);
884 if( nCrisisMerge
<0 ){
887 if( nCrisisMerge
<=1 ) nCrisisMerge
= FTS5_DEFAULT_CRISISMERGE
;
888 if( nCrisisMerge
>=FTS5_MAX_SEGMENT
) nCrisisMerge
= FTS5_MAX_SEGMENT
-1;
889 pConfig
->nCrisisMerge
= nCrisisMerge
;
893 else if( 0==sqlite3_stricmp(zKey
, "rank") ){
894 const char *zIn
= (const char*)sqlite3_value_text(pVal
);
897 rc
= sqlite3Fts5ConfigParseRank(zIn
, &zRank
, &zRankArgs
);
899 sqlite3_free(pConfig
->zRank
);
900 sqlite3_free(pConfig
->zRankArgs
);
901 pConfig
->zRank
= zRank
;
902 pConfig
->zRankArgs
= zRankArgs
;
903 }else if( rc
==SQLITE_ERROR
){
909 else if( 0==sqlite3_stricmp(zKey
, "secure-delete") ){
911 if( SQLITE_INTEGER
==sqlite3_value_numeric_type(pVal
) ){
912 bVal
= sqlite3_value_int(pVal
);
917 pConfig
->bSecureDelete
= (bVal
? 1 : 0);
926 ** Load the contents of the %_config table into memory.
928 int sqlite3Fts5ConfigLoad(Fts5Config
*pConfig
, int iCookie
){
929 const char *zSelect
= "SELECT k, v FROM %Q.'%q_config'";
935 /* Set default values */
936 pConfig
->pgsz
= FTS5_DEFAULT_PAGE_SIZE
;
937 pConfig
->nAutomerge
= FTS5_DEFAULT_AUTOMERGE
;
938 pConfig
->nUsermerge
= FTS5_DEFAULT_USERMERGE
;
939 pConfig
->nCrisisMerge
= FTS5_DEFAULT_CRISISMERGE
;
940 pConfig
->nHashSize
= FTS5_DEFAULT_HASHSIZE
;
942 zSql
= sqlite3Fts5Mprintf(&rc
, zSelect
, pConfig
->zDb
, pConfig
->zName
);
944 rc
= sqlite3_prepare_v2(pConfig
->db
, zSql
, -1, &p
, 0);
948 assert( rc
==SQLITE_OK
|| p
==0 );
950 while( SQLITE_ROW
==sqlite3_step(p
) ){
951 const char *zK
= (const char*)sqlite3_column_text(p
, 0);
952 sqlite3_value
*pVal
= sqlite3_column_value(p
, 1);
953 if( 0==sqlite3_stricmp(zK
, "version") ){
954 iVersion
= sqlite3_value_int(pVal
);
957 sqlite3Fts5ConfigSetValue(pConfig
, zK
, pVal
, &bDummy
);
960 rc
= sqlite3_finalize(p
);
964 && iVersion
!=FTS5_CURRENT_VERSION
965 && iVersion
!=FTS5_CURRENT_VERSION_SECUREDELETE
968 if( pConfig
->pzErrmsg
){
969 assert( 0==*pConfig
->pzErrmsg
);
970 *pConfig
->pzErrmsg
= sqlite3_mprintf("invalid fts5 file format "
971 "(found %d, expected %d or %d) - run 'rebuild'",
972 iVersion
, FTS5_CURRENT_VERSION
, FTS5_CURRENT_VERSION_SECUREDELETE
976 pConfig
->iVersion
= iVersion
;
980 pConfig
->iCookie
= iCookie
;