This source file includes following definitions.
- sqlite3Fts3PutVarint
- sqlite3Fts3GetVarint
- sqlite3Fts3GetVarint32
- sqlite3Fts3VarintLen
- sqlite3Fts3Dequote
- fts3GetDeltaVarint
- fts3GetDeltaVarint2
- fts3DisconnectMethod
- fts3DbExec
- fts3DestroyMethod
- fts3DeclareVtab
- fts3CreateTables
- fts3DatabasePageSize
- fts3IsSpecialColumn
- fts3Appendf
- fts3QuoteId
- fts3ReadExprList
- fts3WriteExprList
- fts3InitVtab
- fts3ConnectMethod
- fts3CreateMethod
- fts3BestIndexMethod
- fts3OpenMethod
- fts3CloseMethod
- fts3CursorSeek
- fts3ScanInteriorNode
- fts3SelectLeaf
- fts3PutDeltaVarint
- fts3PoslistCopy
- fts3ColumnlistCopy
- fts3ReadNextPos
- fts3PutColNumber
- fts3PoslistMerge
- fts3PoslistPhraseMerge
- fts3PoslistNearMerge
- fts3DoclistMerge
- fts3TermSelectMerge
- fts3TermSelectCb
- fts3DeferredTermSelect
- sqlite3Fts3SegReaderCursor
- fts3TermSegReaderCursor
- fts3SegReaderCursorFree
- fts3TermSelect
- fts3DoclistCountDocids
- fts3DeferExpression
- fts3DoclistStripPositions
- fts3PhraseSelect
- fts3NearMerge
- sqlite3Fts3ExprNearTrim
- fts3ExprAllocateSegReaders
- fts3ExprFreeSegReaders
- fts3ExprCost
- fts3ExprAssignCosts
- fts3EvalExpr
- fts3EvalDeferred
- fts3NextMethod
- fts3FilterMethod
- fts3EofMethod
- fts3RowidMethod
- fts3ColumnMethod
- fts3UpdateMethod
- fts3SyncMethod
- fts3BeginMethod
- fts3CommitMethod
- fts3RollbackMethod
- sqlite3Fts3ExprLoadDoclist
- sqlite3Fts3ExprLoadFtDoclist
- sqlite3Fts3FindPositions
- fts3FunctionArg
- fts3SnippetFunc
- fts3OffsetsFunc
- fts3OptimizeFunc
- fts3MatchinfoFunc
- fts3FindFunctionMethod
- fts3RenameMethod
- hashDestroy
- sqlite3Fts3Init
- sqlite3_extension_init
#define CHROMIUM_FTS3_CHANGES 1
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
# define SQLITE_CORE 1
#endif
#include "fts3Int.h"
#include <assert.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "fts3.h"
#ifndef SQLITE_CORE
# include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#endif
int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
unsigned char *q = (unsigned char *) p;
sqlite_uint64 vu = v;
do{
*q++ = (unsigned char) ((vu & 0x7f) | 0x80);
vu >>= 7;
}while( vu!=0 );
q[-1] &= 0x7f;
assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
return (int) (q - (unsigned char *)p);
}
int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
const unsigned char *q = (const unsigned char *) p;
sqlite_uint64 x = 0, y = 1;
while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
x += y * (*q++ & 0x7f);
y <<= 7;
}
x += y * (*q++);
*v = (sqlite_int64) x;
return (int) (q - (unsigned char *)p);
}
int sqlite3Fts3GetVarint32(const char *p, int *pi){
sqlite_int64 i;
int ret = sqlite3Fts3GetVarint(p, &i);
*pi = (int) i;
return ret;
}
int sqlite3Fts3VarintLen(sqlite3_uint64 v){
int i = 0;
do{
i++;
v >>= 7;
}while( v!=0 );
return i;
}
void sqlite3Fts3Dequote(char *z){
char quote;
quote = z[0];
if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
int iIn = 1;
int iOut = 0;
if( quote=='[' ) quote = ']';
while( ALWAYS(z[iIn]) ){
if( z[iIn]==quote ){
if( z[iIn+1]!=quote ) break;
z[iOut++] = quote;
iIn += 2;
}else{
z[iOut++] = z[iIn++];
}
}
z[iOut] = '\0';
}
}
static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
sqlite3_int64 iVal;
*pp += sqlite3Fts3GetVarint(*pp, &iVal);
*pVal += iVal;
}
static void fts3GetDeltaVarint2(char **pp, char *pEnd, sqlite3_int64 *pVal){
if( *pp>=pEnd ){
*pp = 0;
}else{
fts3GetDeltaVarint(pp, pVal);
}
}
static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
Fts3Table *p = (Fts3Table *)pVtab;
int i;
assert( p->nPendingData==0 );
assert( p->pSegments==0 );
for(i=0; i<SizeofArray(p->aStmt); i++){
sqlite3_finalize(p->aStmt[i]);
}
sqlite3_free(p->zSegmentsTbl);
sqlite3_free(p->zReadExprlist);
sqlite3_free(p->zWriteExprlist);
p->pTokenizer->pModule->xDestroy(p->pTokenizer);
sqlite3_free(p);
return SQLITE_OK;
}
static void fts3DbExec(
int *pRc,
sqlite3 *db,
const char *zFormat,
...
){
va_list ap;
char *zSql;
if( *pRc ) return;
va_start(ap, zFormat);
zSql = sqlite3_vmprintf(zFormat, ap);
va_end(ap);
if( zSql==0 ){
*pRc = SQLITE_NOMEM;
}else{
*pRc = sqlite3_exec(db, zSql, 0, 0, 0);
sqlite3_free(zSql);
}
}
static int fts3DestroyMethod(sqlite3_vtab *pVtab){
int rc = SQLITE_OK;
Fts3Table *p = (Fts3Table *)pVtab;
sqlite3 *db = p->db;
fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName);
fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName);
fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName);
fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName);
fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName);
return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
}
static void fts3DeclareVtab(int *pRc, Fts3Table *p){
if( *pRc==SQLITE_OK ){
int i;
int rc;
char *zSql;
char *zCols;
zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
for(i=1; zCols && i<p->nColumn; i++){
zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
}
zSql = sqlite3_mprintf(
"CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName
);
if( !zCols || !zSql ){
rc = SQLITE_NOMEM;
}else{
rc = sqlite3_declare_vtab(p->db, zSql);
}
sqlite3_free(zSql);
sqlite3_free(zCols);
*pRc = rc;
}
}
static int fts3CreateTables(Fts3Table *p){
int rc = SQLITE_OK;
int i;
char *zContentCols;
sqlite3 *db = p->db;
zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
for(i=0; zContentCols && i<p->nColumn; i++){
char *z = p->azColumn[i];
zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
}
if( zContentCols==0 ) rc = SQLITE_NOMEM;
fts3DbExec(&rc, db,
"CREATE TABLE %Q.'%q_content'(%s)",
p->zDb, p->zName, zContentCols
);
sqlite3_free(zContentCols);
fts3DbExec(&rc, db,
"CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
p->zDb, p->zName
);
fts3DbExec(&rc, db,
"CREATE TABLE %Q.'%q_segdir'("
"level INTEGER,"
"idx INTEGER,"
"start_block INTEGER,"
"leaves_end_block INTEGER,"
"end_block INTEGER,"
"root BLOB,"
"PRIMARY KEY(level, idx)"
");",
p->zDb, p->zName
);
if( p->bHasDocsize ){
fts3DbExec(&rc, db,
"CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
p->zDb, p->zName
);
}
if( p->bHasStat ){
fts3DbExec(&rc, db,
"CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
p->zDb, p->zName
);
}
return rc;
}
static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
if( *pRc==SQLITE_OK ){
int rc;
char *zSql;
sqlite3_stmt *pStmt;
zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
if( !zSql ){
rc = SQLITE_NOMEM;
}else{
rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
if( rc==SQLITE_OK ){
sqlite3_step(pStmt);
p->nPgsz = sqlite3_column_int(pStmt, 0);
rc = sqlite3_finalize(pStmt);
}else if( rc==SQLITE_AUTH ){
p->nPgsz = 1024;
rc = SQLITE_OK;
}
}
assert( p->nPgsz>0 || rc!=SQLITE_OK );
sqlite3_free(zSql);
*pRc = rc;
}
}
static int fts3IsSpecialColumn(
const char *z,
int *pnKey,
char **pzValue
){
char *zValue;
const char *zCsr = z;
while( *zCsr!='=' ){
if( *zCsr=='\0' ) return 0;
zCsr++;
}
*pnKey = (int)(zCsr-z);
zValue = sqlite3_mprintf("%s", &zCsr[1]);
if( zValue ){
sqlite3Fts3Dequote(zValue);
}
*pzValue = zValue;
return 1;
}
static void fts3Appendf(
int *pRc,
char **pz,
const char *zFormat,
...
){
if( *pRc==SQLITE_OK ){
va_list ap;
char *z;
va_start(ap, zFormat);
z = sqlite3_vmprintf(zFormat, ap);
if( z && *pz ){
char *z2 = sqlite3_mprintf("%s%s", *pz, z);
sqlite3_free(z);
z = z2;
}
if( z==0 ) *pRc = SQLITE_NOMEM;
sqlite3_free(*pz);
*pz = z;
}
}
static char *fts3QuoteId(char const *zInput){
int nRet;
char *zRet;
nRet = 2 + strlen(zInput)*2 + 1;
zRet = sqlite3_malloc(nRet);
if( zRet ){
int i;
char *z = zRet;
*(z++) = '"';
for(i=0; zInput[i]; i++){
if( zInput[i]=='"' ) *(z++) = '"';
*(z++) = zInput[i];
}
*(z++) = '"';
*(z++) = '\0';
}
return zRet;
}
static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
char *zRet = 0;
char *zFree = 0;
char *zFunction;
int i;
if( !zFunc ){
zFunction = "";
}else{
zFree = zFunction = fts3QuoteId(zFunc);
}
fts3Appendf(pRc, &zRet, "docid");
for(i=0; i<p->nColumn; i++){
fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
}
sqlite3_free(zFree);
return zRet;
}
static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
char *zRet = 0;
char *zFree = 0;
char *zFunction;
int i;
if( !zFunc ){
zFunction = "";
}else{
zFree = zFunction = fts3QuoteId(zFunc);
}
fts3Appendf(pRc, &zRet, "?");
for(i=0; i<p->nColumn; i++){
fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
}
sqlite3_free(zFree);
return zRet;
}
static int fts3InitVtab(
int isCreate,
sqlite3 *db,
void *pAux,
int argc,
const char * const *argv,
sqlite3_vtab **ppVTab,
char **pzErr
){
Fts3Hash *pHash = (Fts3Hash *)pAux;
Fts3Table *p = 0;
int rc = SQLITE_OK;
int i;
int nByte;
int iCol;
int nString = 0;
int nCol = 0;
char *zCsr;
int nDb;
int nName;
int isFts4 = (argv[0][3]=='4');
int bNoDocsize = 0;
const char **aCol;
sqlite3_tokenizer *pTokenizer = 0;
char *zCompress = 0;
char *zUncompress = 0;
assert( strlen(argv[0])==4 );
assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
|| (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
);
nDb = (int)strlen(argv[1]) + 1;
nName = (int)strlen(argv[2]) + 1;
aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
if( !aCol ) return SQLITE_NOMEM;
memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
for(i=3; rc==SQLITE_OK && i<argc; i++){
char const *z = argv[i];
int nKey;
char *zVal;
if( !pTokenizer
&& strlen(z)>8
&& 0==sqlite3_strnicmp(z, "tokenize", 8)
&& 0==sqlite3Fts3IsIdChar(z[8])
){
rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
}
else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
if( !zVal ){
rc = SQLITE_NOMEM;
goto fts3_init_out;
}
if( nKey==9 && 0==sqlite3_strnicmp(z, "matchinfo", 9) ){
if( strlen(zVal)==4 && 0==sqlite3_strnicmp(zVal, "fts3", 4) ){
bNoDocsize = 1;
}else{
*pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
rc = SQLITE_ERROR;
}
}else if( nKey==8 && 0==sqlite3_strnicmp(z, "compress", 8) ){
zCompress = zVal;
zVal = 0;
}else if( nKey==10 && 0==sqlite3_strnicmp(z, "uncompress", 10) ){
zUncompress = zVal;
zVal = 0;
}else{
*pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
rc = SQLITE_ERROR;
}
sqlite3_free(zVal);
}
else {
nString += (int)(strlen(z) + 1);
aCol[nCol++] = z;
}
}
if( rc!=SQLITE_OK ) goto fts3_init_out;
if( nCol==0 ){
assert( nString==0 );
aCol[0] = "content";
nString = 8;
nCol = 1;
}
if( pTokenizer==0 ){
rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
if( rc!=SQLITE_OK ) goto fts3_init_out;
}
assert( pTokenizer );
nByte = sizeof(Fts3Table) +
nCol * sizeof(char *) +
nName +
nDb +
nString;
p = (Fts3Table*)sqlite3_malloc(nByte);
if( p==0 ){
rc = SQLITE_NOMEM;
goto fts3_init_out;
}
memset(p, 0, nByte);
p->db = db;
p->nColumn = nCol;
p->nPendingData = 0;
p->azColumn = (char **)&p[1];
p->pTokenizer = pTokenizer;
p->nNodeSize = 1000;
p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
p->bHasDocsize = (isFts4 && bNoDocsize==0);
p->bHasStat = isFts4;
fts3HashInit(&p->pendingTerms, FTS3_HASH_STRING, 1);
zCsr = (char *)&p->azColumn[nCol];
p->zName = zCsr;
memcpy(zCsr, argv[2], nName);
zCsr += nName;
p->zDb = zCsr;
memcpy(zCsr, argv[1], nDb);
zCsr += nDb;
for(iCol=0; iCol<nCol; iCol++){
char *z;
int n;
z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
memcpy(zCsr, z, n);
zCsr[n] = '\0';
sqlite3Fts3Dequote(zCsr);
p->azColumn[iCol] = zCsr;
zCsr += n+1;
assert( zCsr <= &((char *)p)[nByte] );
}
if( (zCompress==0)!=(zUncompress==0) ){
char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
rc = SQLITE_ERROR;
*pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
}
p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
if( rc!=SQLITE_OK ) goto fts3_init_out;
if( isCreate ){
rc = fts3CreateTables(p);
}
fts3DatabasePageSize(&rc, p);
fts3DeclareVtab(&rc, p);
fts3_init_out:
sqlite3_free(zCompress);
sqlite3_free(zUncompress);
sqlite3_free((void *)aCol);
if( rc!=SQLITE_OK ){
if( p ){
fts3DisconnectMethod((sqlite3_vtab *)p);
}else if( pTokenizer ){
pTokenizer->pModule->xDestroy(pTokenizer);
}
}else{
*ppVTab = &p->base;
}
return rc;
}
static int fts3ConnectMethod(
sqlite3 *db,
void *pAux,
int argc,
const char * const *argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
}
static int fts3CreateMethod(
sqlite3 *db,
void *pAux,
int argc,
const char * const *argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
}
static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
Fts3Table *p = (Fts3Table *)pVTab;
int i;
int iCons = -1;
pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
pInfo->estimatedCost = 500000;
for(i=0; i<pInfo->nConstraint; i++){
struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
if( pCons->usable==0 ) continue;
if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
&& (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
){
pInfo->idxNum = FTS3_DOCID_SEARCH;
pInfo->estimatedCost = 1.0;
iCons = i;
}
if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
&& pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
){
pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
pInfo->estimatedCost = 2.0;
iCons = i;
break;
}
}
if( iCons>=0 ){
pInfo->aConstraintUsage[iCons].argvIndex = 1;
pInfo->aConstraintUsage[iCons].omit = 1;
}
return SQLITE_OK;
}
static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
sqlite3_vtab_cursor *pCsr;
UNUSED_PARAMETER(pVTab);
*ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
if( !pCsr ){
return SQLITE_NOMEM;
}
memset(pCsr, 0, sizeof(Fts3Cursor));
return SQLITE_OK;
}
static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
sqlite3_finalize(pCsr->pStmt);
sqlite3Fts3ExprFree(pCsr->pExpr);
sqlite3Fts3FreeDeferredTokens(pCsr);
sqlite3_free(pCsr->aDoclist);
sqlite3_free(pCsr->aMatchinfo);
sqlite3_free(pCsr);
return SQLITE_OK;
}
static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
if( pCsr->isRequireSeek ){
pCsr->isRequireSeek = 0;
sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
return SQLITE_OK;
}else{
int rc = sqlite3_reset(pCsr->pStmt);
if( rc==SQLITE_OK ){
rc = SQLITE_CORRUPT;
}
pCsr->isEof = 1;
if( pContext ){
sqlite3_result_error_code(pContext, rc);
}
return rc;
}
}else{
return SQLITE_OK;
}
}
static int fts3ScanInteriorNode(
const char *zTerm,
int nTerm,
const char *zNode,
int nNode,
sqlite3_int64 *piFirst,
sqlite3_int64 *piLast
){
int rc = SQLITE_OK;
const char *zCsr = zNode;
const char *zEnd = &zCsr[nNode];
char *zBuffer = 0;
int nAlloc = 0;
int isFirstTerm = 1;
sqlite3_int64 iChild;
zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
if( zCsr>zEnd ){
return SQLITE_CORRUPT;
}
while( zCsr<zEnd && (piFirst || piLast) ){
int cmp;
int nSuffix;
int nPrefix = 0;
int nBuffer;
if( !isFirstTerm ){
zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
}
isFirstTerm = 0;
zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
if( nPrefix<0 || nSuffix<0
|| &zCsr[nSuffix]<zCsr || &zCsr[nSuffix]>zEnd ){
rc = SQLITE_CORRUPT;
goto finish_scan;
}
if( nPrefix+nSuffix>nAlloc ){
char *zNew;
nAlloc = (nPrefix+nSuffix) * 2;
zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
if( !zNew ){
rc = SQLITE_NOMEM;
goto finish_scan;
}
zBuffer = zNew;
}
memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
nBuffer = nPrefix + nSuffix;
zCsr += nSuffix;
cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
*piFirst = iChild;
piFirst = 0;
}
if( piLast && cmp<0 ){
*piLast = iChild;
piLast = 0;
}
iChild++;
};
if( piFirst ) *piFirst = iChild;
if( piLast ) *piLast = iChild;
finish_scan:
sqlite3_free(zBuffer);
return rc;
}
static int fts3SelectLeaf(
Fts3Table *p,
const char *zTerm,
int nTerm,
const char *zNode,
int nNode,
sqlite3_int64 *piLeaf,
sqlite3_int64 *piLeaf2
){
int rc;
int iHeight;
assert( piLeaf || piLeaf2 );
sqlite3Fts3GetVarint32(zNode, &iHeight);
rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
if( rc==SQLITE_OK && iHeight>1 ){
char *zBlob = 0;
int nBlob;
if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob);
if( rc==SQLITE_OK ){
rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
}
sqlite3_free(zBlob);
piLeaf = 0;
zBlob = 0;
}
if( rc==SQLITE_OK ){
rc = sqlite3Fts3ReadBlock(p, piLeaf ? *piLeaf : *piLeaf2, &zBlob, &nBlob);
}
if( rc==SQLITE_OK ){
rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
}
sqlite3_free(zBlob);
}
return rc;
}
static void fts3PutDeltaVarint(
char **pp,
sqlite3_int64 *piPrev,
sqlite3_int64 iVal
){
assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
*pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
*piPrev = iVal;
}
static void fts3PoslistCopy(char **pp, char **ppPoslist){
char *pEnd = *ppPoslist;
char c = 0;
while( *pEnd | c ){
c = *pEnd++ & 0x80;
testcase( c!=0 && (*pEnd)==0 );
}
pEnd++;
if( pp ){
int n = (int)(pEnd - *ppPoslist);
char *p = *pp;
memcpy(p, *ppPoslist, n);
p += n;
*pp = p;
}
*ppPoslist = pEnd;
}
static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
char *pEnd = *ppPoslist;
char c = 0;
while( 0xFE & (*pEnd | c) ){
c = *pEnd++ & 0x80;
testcase( c!=0 && ((*pEnd)&0xfe)==0 );
}
if( pp ){
int n = (int)(pEnd - *ppPoslist);
char *p = *pp;
memcpy(p, *ppPoslist, n);
p += n;
*pp = p;
}
*ppPoslist = pEnd;
}
#define POSITION_LIST_END 0x7fffffff
static void fts3ReadNextPos(
char **pp,
sqlite3_int64 *pi
){
if( (**pp)&0xFE ){
fts3GetDeltaVarint(pp, pi);
*pi -= 2;
}else{
*pi = POSITION_LIST_END;
}
}
static int fts3PutColNumber(char **pp, int iCol){
int n = 0;
if( iCol ){
char *p = *pp;
n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
*p = 0x01;
*pp = &p[n];
}
return n;
}
static void fts3PoslistMerge(
char **pp,
char **pp1,
char **pp2
){
char *p = *pp;
char *p1 = *pp1;
char *p2 = *pp2;
while( *p1 || *p2 ){
int iCol1;
int iCol2;
if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
else iCol1 = 0;
if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
else iCol2 = 0;
if( iCol1==iCol2 ){
sqlite3_int64 i1 = 0;
sqlite3_int64 i2 = 0;
sqlite3_int64 iPrev = 0;
int n = fts3PutColNumber(&p, iCol1);
p1 += n;
p2 += n;
fts3GetDeltaVarint(&p1, &i1);
fts3GetDeltaVarint(&p2, &i2);
do {
fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
iPrev -= 2;
if( i1==i2 ){
fts3ReadNextPos(&p1, &i1);
fts3ReadNextPos(&p2, &i2);
}else if( i1<i2 ){
fts3ReadNextPos(&p1, &i1);
}else{
fts3ReadNextPos(&p2, &i2);
}
}while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
}else if( iCol1<iCol2 ){
p1 += fts3PutColNumber(&p, iCol1);
fts3ColumnlistCopy(&p, &p1);
}else{
p2 += fts3PutColNumber(&p, iCol2);
fts3ColumnlistCopy(&p, &p2);
}
}
*p++ = POS_END;
*pp = p;
*pp1 = p1 + 1;
*pp2 = p2 + 1;
}
static int fts3PoslistPhraseMerge(
char **pp,
int nToken,
int isSaveLeft,
int isExact,
char **pp1,
char **pp2
){
char *p = (pp ? *pp : 0);
char *p1 = *pp1;
char *p2 = *pp2;
int iCol1 = 0;
int iCol2 = 0;
assert( isSaveLeft==0 || isExact==0 );
assert( *p1!=0 && *p2!=0 );
if( *p1==POS_COLUMN ){
p1++;
p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
}
if( *p2==POS_COLUMN ){
p2++;
p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
}
while( 1 ){
if( iCol1==iCol2 ){
char *pSave = p;
sqlite3_int64 iPrev = 0;
sqlite3_int64 iPos1 = 0;
sqlite3_int64 iPos2 = 0;
if( pp && iCol1 ){
*p++ = POS_COLUMN;
p += sqlite3Fts3PutVarint(p, iCol1);
}
assert( *p1!=POS_END && *p1!=POS_COLUMN );
assert( *p2!=POS_END && *p2!=POS_COLUMN );
fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
while( 1 ){
if( iPos2==iPos1+nToken
|| (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
){
sqlite3_int64 iSave;
if( !pp ){
fts3PoslistCopy(0, &p2);
fts3PoslistCopy(0, &p1);
*pp1 = p1;
*pp2 = p2;
return 1;
}
iSave = isSaveLeft ? iPos1 : iPos2;
fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
pSave = 0;
}
if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
if( (*p2&0xFE)==0 ) break;
fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
}else{
if( (*p1&0xFE)==0 ) break;
fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
}
}
if( pSave ){
assert( pp && p );
p = pSave;
}
fts3ColumnlistCopy(0, &p1);
fts3ColumnlistCopy(0, &p2);
assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
if( 0==*p1 || 0==*p2 ) break;
p1++;
p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
p2++;
p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
}
else if( iCol1<iCol2 ){
fts3ColumnlistCopy(0, &p1);
if( 0==*p1 ) break;
p1++;
p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
}else{
fts3ColumnlistCopy(0, &p2);
if( 0==*p2 ) break;
p2++;
p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
}
}
fts3PoslistCopy(0, &p2);
fts3PoslistCopy(0, &p1);
*pp1 = p1;
*pp2 = p2;
if( !pp || *pp==p ){
return 0;
}
*p++ = 0x00;
*pp = p;
return 1;
}
static int fts3PoslistNearMerge(
char **pp,
char *aTmp,
int nRight,
int nLeft,
char **pp1,
char **pp2
){
char *p1 = *pp1;
char *p2 = *pp2;
if( !pp ){
if( fts3PoslistPhraseMerge(0, nRight, 0, 0, pp1, pp2) ) return 1;
*pp1 = p1;
*pp2 = p2;
return fts3PoslistPhraseMerge(0, nLeft, 0, 0, pp2, pp1);
}else{
char *pTmp1 = aTmp;
char *pTmp2;
char *aTmp2;
int res = 1;
fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
aTmp2 = pTmp2 = pTmp1;
*pp1 = p1;
*pp2 = p2;
fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
fts3PoslistMerge(pp, &aTmp, &aTmp2);
}else if( pTmp1!=aTmp ){
fts3PoslistCopy(pp, &aTmp);
}else if( pTmp2!=aTmp2 ){
fts3PoslistCopy(pp, &aTmp2);
}else{
res = 0;
}
return res;
}
}
#define MERGE_NOT 2
#define MERGE_AND 3
#define MERGE_OR 4
#define MERGE_POS_OR 5
#define MERGE_PHRASE 6
#define MERGE_POS_PHRASE 7
#define MERGE_NEAR 8
#define MERGE_POS_NEAR 9
static int fts3DoclistMerge(
int mergetype,
int nParam1,
int nParam2,
char *aBuffer,
int *pnBuffer,
char *a1,
int n1,
char *a2,
int n2,
int *pnDoc
){
sqlite3_int64 i1 = 0;
sqlite3_int64 i2 = 0;
sqlite3_int64 iPrev = 0;
char *p = aBuffer;
char *p1 = a1;
char *p2 = a2;
char *pEnd1 = &a1[n1];
char *pEnd2 = &a2[n2];
int nDoc = 0;
assert( mergetype==MERGE_OR || mergetype==MERGE_POS_OR
|| mergetype==MERGE_AND || mergetype==MERGE_NOT
|| mergetype==MERGE_PHRASE || mergetype==MERGE_POS_PHRASE
|| mergetype==MERGE_NEAR || mergetype==MERGE_POS_NEAR
);
if( !aBuffer ){
*pnBuffer = 0;
return SQLITE_NOMEM;
}
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
switch( mergetype ){
case MERGE_OR:
case MERGE_POS_OR:
while( p1 || p2 ){
if( p2 && p1 && i1==i2 ){
fts3PutDeltaVarint(&p, &iPrev, i1);
if( mergetype==MERGE_POS_OR ) fts3PoslistMerge(&p, &p1, &p2);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( !p2 || (p1 && i1<i2) ){
fts3PutDeltaVarint(&p, &iPrev, i1);
if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3PutDeltaVarint(&p, &iPrev, i2);
if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p2);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
case MERGE_AND:
while( p1 && p2 ){
if( i1==i2 ){
fts3PutDeltaVarint(&p, &iPrev, i1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
nDoc++;
}else if( i1<i2 ){
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
case MERGE_NOT:
while( p1 ){
if( p2 && i1==i2 ){
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( !p2 || i1<i2 ){
fts3PutDeltaVarint(&p, &iPrev, i1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
case MERGE_POS_PHRASE:
case MERGE_PHRASE: {
char **ppPos = (mergetype==MERGE_PHRASE ? 0 : &p);
while( p1 && p2 ){
if( i1==i2 ){
char *pSave = p;
sqlite3_int64 iPrevSave = iPrev;
fts3PutDeltaVarint(&p, &iPrev, i1);
if( 0==fts3PoslistPhraseMerge(ppPos, nParam1, 0, 1, &p1, &p2) ){
p = pSave;
iPrev = iPrevSave;
}else{
nDoc++;
}
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( i1<i2 ){
fts3PoslistCopy(0, &p1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3PoslistCopy(0, &p2);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
}
default: assert( mergetype==MERGE_POS_NEAR || mergetype==MERGE_NEAR ); {
char *aTmp = 0;
char **ppPos = 0;
if( mergetype==MERGE_POS_NEAR ){
ppPos = &p;
aTmp = sqlite3_malloc(2*(n1+n2+1));
if( !aTmp ){
return SQLITE_NOMEM;
}
}
while( p1 && p2 ){
if( i1==i2 ){
char *pSave = p;
sqlite3_int64 iPrevSave = iPrev;
fts3PutDeltaVarint(&p, &iPrev, i1);
if( !fts3PoslistNearMerge(ppPos, aTmp, nParam1, nParam2, &p1, &p2) ){
iPrev = iPrevSave;
p = pSave;
}
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( i1<i2 ){
fts3PoslistCopy(0, &p1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3PoslistCopy(0, &p2);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
sqlite3_free(aTmp);
break;
}
}
if( pnDoc ) *pnDoc = nDoc;
*pnBuffer = (int)(p-aBuffer);
return SQLITE_OK;
}
typedef struct TermSelect TermSelect;
struct TermSelect {
int isReqPos;
char *aaOutput[16];
int anOutput[16];
};
static int fts3TermSelectMerge(TermSelect *pTS){
int mergetype = (pTS->isReqPos ? MERGE_POS_OR : MERGE_OR);
char *aOut = 0;
int nOut = 0;
int i;
for(i=0; i<SizeofArray(pTS->aaOutput); i++){
if( pTS->aaOutput[i] ){
if( !aOut ){
aOut = pTS->aaOutput[i];
nOut = pTS->anOutput[i];
pTS->aaOutput[i] = 0;
}else{
int nNew = nOut + pTS->anOutput[i];
char *aNew = sqlite3_malloc(nNew);
if( !aNew ){
sqlite3_free(aOut);
return SQLITE_NOMEM;
}
fts3DoclistMerge(mergetype, 0, 0,
aNew, &nNew, pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, 0
);
sqlite3_free(pTS->aaOutput[i]);
sqlite3_free(aOut);
pTS->aaOutput[i] = 0;
aOut = aNew;
nOut = nNew;
}
}
}
pTS->aaOutput[0] = aOut;
pTS->anOutput[0] = nOut;
return SQLITE_OK;
}
static int fts3TermSelectCb(
Fts3Table *p,
void *pContext,
char *zTerm,
int nTerm,
char *aDoclist,
int nDoclist
){
TermSelect *pTS = (TermSelect *)pContext;
UNUSED_PARAMETER(p);
UNUSED_PARAMETER(zTerm);
UNUSED_PARAMETER(nTerm);
if( pTS->aaOutput[0]==0 ){
pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
pTS->anOutput[0] = nDoclist;
if( pTS->aaOutput[0] ){
memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
}else{
return SQLITE_NOMEM;
}
}else{
int mergetype = (pTS->isReqPos ? MERGE_POS_OR : MERGE_OR);
char *aMerge = aDoclist;
int nMerge = nDoclist;
int iOut;
for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
char *aNew;
int nNew;
if( pTS->aaOutput[iOut]==0 ){
assert( iOut>0 );
pTS->aaOutput[iOut] = aMerge;
pTS->anOutput[iOut] = nMerge;
break;
}
nNew = nMerge + pTS->anOutput[iOut];
aNew = sqlite3_malloc(nNew);
if( !aNew ){
if( aMerge!=aDoclist ){
sqlite3_free(aMerge);
}
return SQLITE_NOMEM;
}
fts3DoclistMerge(mergetype, 0, 0, aNew, &nNew,
pTS->aaOutput[iOut], pTS->anOutput[iOut], aMerge, nMerge, 0
);
if( iOut>0 ) sqlite3_free(aMerge);
sqlite3_free(pTS->aaOutput[iOut]);
pTS->aaOutput[iOut] = 0;
aMerge = aNew;
nMerge = nNew;
if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
pTS->aaOutput[iOut] = aMerge;
pTS->anOutput[iOut] = nMerge;
}
}
}
return SQLITE_OK;
}
static int fts3DeferredTermSelect(
Fts3DeferredToken *pToken,
int isTermPos,
int *pnOut,
char **ppOut
){
char *aSource;
int nSource;
aSource = sqlite3Fts3DeferredDoclist(pToken, &nSource);
if( !aSource ){
*pnOut = 0;
*ppOut = 0;
}else if( isTermPos ){
*ppOut = sqlite3_malloc(nSource);
if( !*ppOut ) return SQLITE_NOMEM;
memcpy(*ppOut, aSource, nSource);
*pnOut = nSource;
}else{
sqlite3_int64 docid;
*pnOut = sqlite3Fts3GetVarint(aSource, &docid);
*ppOut = sqlite3_malloc(*pnOut);
if( !*ppOut ) return SQLITE_NOMEM;
sqlite3Fts3PutVarint(*ppOut, docid);
}
return SQLITE_OK;
}
int sqlite3Fts3SegReaderCursor(
Fts3Table *p,
int iLevel,
const char *zTerm,
int nTerm,
int isPrefix,
int isScan,
Fts3SegReaderCursor *pCsr
){
int rc = SQLITE_OK;
int rc2;
int iAge = 0;
sqlite3_stmt *pStmt = 0;
Fts3SegReader *pPending = 0;
assert( iLevel==FTS3_SEGCURSOR_ALL
|| iLevel==FTS3_SEGCURSOR_PENDING
|| iLevel>=0
);
assert( FTS3_SEGCURSOR_PENDING<0 );
assert( FTS3_SEGCURSOR_ALL<0 );
assert( iLevel==FTS3_SEGCURSOR_ALL || (zTerm==0 && isPrefix==1) );
assert( isPrefix==0 || isScan==0 );
memset(pCsr, 0, sizeof(Fts3SegReaderCursor));
assert( isScan==0 || fts3HashCount(&p->pendingTerms)==0 );
if( iLevel<0 && isScan==0 ){
rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pPending);
if( rc==SQLITE_OK && pPending ){
int nByte = (sizeof(Fts3SegReader *) * 16);
pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
if( pCsr->apSegment==0 ){
rc = SQLITE_NOMEM;
}else{
pCsr->apSegment[0] = pPending;
pCsr->nSegment = 1;
pPending = 0;
}
}
}
if( iLevel!=FTS3_SEGCURSOR_PENDING ){
if( rc==SQLITE_OK ){
rc = sqlite3Fts3AllSegdirs(p, iLevel, &pStmt);
}
while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
int nRoot = sqlite3_column_bytes(pStmt, 4);
char const *zRoot = sqlite3_column_blob(pStmt, 4);
if( (pCsr->nSegment%16)==0 ){
Fts3SegReader **apNew;
int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
if( !apNew ){
rc = SQLITE_NOMEM;
goto finished;
}
pCsr->apSegment = apNew;
}
if( iStartBlock && zTerm ){
sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
if( rc!=SQLITE_OK ) goto finished;
if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
}
rc = sqlite3Fts3SegReaderNew(iAge, iStartBlock, iLeavesEndBlock,
iEndBlock, zRoot, nRoot, &pCsr->apSegment[pCsr->nSegment]
);
if( rc!=SQLITE_OK ) goto finished;
pCsr->nSegment++;
iAge++;
}
}
finished:
rc2 = sqlite3_reset(pStmt);
if( rc==SQLITE_DONE ) rc = rc2;
sqlite3Fts3SegReaderFree(pPending);
return rc;
}
static int fts3TermSegReaderCursor(
Fts3Cursor *pCsr,
const char *zTerm,
int nTerm,
int isPrefix,
Fts3SegReaderCursor **ppSegcsr
){
Fts3SegReaderCursor *pSegcsr;
int rc = SQLITE_NOMEM;
pSegcsr = sqlite3_malloc(sizeof(Fts3SegReaderCursor));
if( pSegcsr ){
Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
int i;
int nCost = 0;
rc = sqlite3Fts3SegReaderCursor(
p, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr);
for(i=0; rc==SQLITE_OK && i<pSegcsr->nSegment; i++){
rc = sqlite3Fts3SegReaderCost(pCsr, pSegcsr->apSegment[i], &nCost);
}
pSegcsr->nCost = nCost;
}
*ppSegcsr = pSegcsr;
return rc;
}
static void fts3SegReaderCursorFree(Fts3SegReaderCursor *pSegcsr){
sqlite3Fts3SegReaderFinish(pSegcsr);
sqlite3_free(pSegcsr);
}
static int fts3TermSelect(
Fts3Table *p,
Fts3PhraseToken *pTok,
int iColumn,
int isReqPos,
int *pnOut,
char **ppOut
){
int rc;
Fts3SegReaderCursor *pSegcsr;
TermSelect tsc;
Fts3SegFilter filter;
pSegcsr = pTok->pSegcsr;
memset(&tsc, 0, sizeof(TermSelect));
tsc.isReqPos = isReqPos;
filter.flags = FTS3_SEGMENT_IGNORE_EMPTY
| (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
| (isReqPos ? FTS3_SEGMENT_REQUIRE_POS : 0)
| (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
filter.iCol = iColumn;
filter.zTerm = pTok->z;
filter.nTerm = pTok->n;
rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
while( SQLITE_OK==rc
&& SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
){
rc = fts3TermSelectCb(p, (void *)&tsc,
pSegcsr->zTerm, pSegcsr->nTerm, pSegcsr->aDoclist, pSegcsr->nDoclist
);
}
if( rc==SQLITE_OK ){
rc = fts3TermSelectMerge(&tsc);
}
if( rc==SQLITE_OK ){
*ppOut = tsc.aaOutput[0];
*pnOut = tsc.anOutput[0];
}else{
int i;
for(i=0; i<SizeofArray(tsc.aaOutput); i++){
sqlite3_free(tsc.aaOutput[i]);
}
}
fts3SegReaderCursorFree(pSegcsr);
pTok->pSegcsr = 0;
return rc;
}
static int fts3DoclistCountDocids(int isPoslist, char *aList, int nList){
int nDoc = 0;
if( aList ){
char *aEnd = &aList[nList];
char *p = aList;
if( !isPoslist ){
while( p<aEnd ) nDoc += (((*p++)&0x80)==0);
}else{
while( p<aEnd ){
nDoc++;
while( (*p++)&0x80 );
fts3PoslistCopy(0, &p);
}
}
}
return nDoc;
}
static int fts3DeferExpression(Fts3Cursor *pCsr, Fts3Expr *pExpr){
int rc = SQLITE_OK;
if( pExpr ){
rc = fts3DeferExpression(pCsr, pExpr->pLeft);
if( rc==SQLITE_OK ){
rc = fts3DeferExpression(pCsr, pExpr->pRight);
}
if( pExpr->eType==FTSQUERY_PHRASE ){
int iCol = pExpr->pPhrase->iColumn;
int i;
for(i=0; rc==SQLITE_OK && i<pExpr->pPhrase->nToken; i++){
Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
if( pToken->pDeferred==0 ){
rc = sqlite3Fts3DeferToken(pCsr, pToken, iCol);
}
}
}
}
return rc;
}
static void fts3DoclistStripPositions(
char *aList,
int *pnList
){
if( aList ){
char *aEnd = &aList[*pnList];
char *p = aList;
char *pOut = aList;
while( p<aEnd ){
sqlite3_int64 delta;
p += sqlite3Fts3GetVarint(p, &delta);
fts3PoslistCopy(0, &p);
pOut += sqlite3Fts3PutVarint(pOut, delta);
}
*pnList = (int)(pOut - aList);
}
}
static int fts3PhraseSelect(
Fts3Cursor *pCsr,
Fts3Phrase *pPhrase,
int isReqPos,
char **paOut,
int *pnOut
){
char *pOut = 0;
int nOut = 0;
int rc = SQLITE_OK;
int ii;
int iCol = pPhrase->iColumn;
int isTermPos = (pPhrase->nToken>1 || isReqPos);
Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
int isFirst = 1;
int iPrevTok = 0;
int nDoc = 0;
for(ii=0; ii<pPhrase->nToken; ii++){
Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
if( pTok->pSegcsr==0 ){
if( (pCsr->eEvalmode==FTS3_EVAL_FILTER)
|| (pCsr->eEvalmode==FTS3_EVAL_NEXT && pCsr->pDeferred==0)
|| (pCsr->eEvalmode==FTS3_EVAL_MATCHINFO && pTok->bFulltext)
){
rc = fts3TermSegReaderCursor(
pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
);
if( rc!=SQLITE_OK ) return rc;
}
}
}
for(ii=0; ii<pPhrase->nToken; ii++){
Fts3PhraseToken *pTok;
int iTok = 0;
char *pList = 0;
int nList = 0;
if( pCsr->eEvalmode==FTS3_EVAL_MATCHINFO ){
assert( isReqPos );
iTok = ii;
pTok = &pPhrase->aToken[iTok];
if( pTok->bFulltext==0 ) continue;
}else if( pCsr->eEvalmode==FTS3_EVAL_NEXT || isReqPos ){
iTok = ii;
pTok = &pPhrase->aToken[iTok];
}else{
int nMinCost = 0x7FFFFFFF;
int jj;
for(jj=0; jj<pPhrase->nToken; jj++){
Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[jj].pSegcsr;
if( pSegcsr && pSegcsr->nCost<nMinCost ){
iTok = jj;
nMinCost = pSegcsr->nCost;
}
}
pTok = &pPhrase->aToken[iTok];
if( nMinCost>nDoc && ii>0 ){
rc = fts3DeferExpression(pCsr, pCsr->pExpr);
break;
}
}
if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){
rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList);
}else{
if( pTok->pSegcsr ){
rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList);
}
pTok->bFulltext = 1;
}
assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pSegcsr==0 );
if( rc!=SQLITE_OK ) break;
if( isFirst ){
pOut = pList;
nOut = nList;
if( pCsr->eEvalmode==FTS3_EVAL_FILTER && pPhrase->nToken>1 ){
nDoc = fts3DoclistCountDocids(1, pOut, nOut);
}
isFirst = 0;
iPrevTok = iTok;
}else{
char *aLeft, *aRight;
int nLeft, nRight;
int nDist;
int mt;
mt = MERGE_POS_PHRASE;
if( ii==pPhrase->nToken-1 && !isReqPos ) mt = MERGE_PHRASE;
assert( iPrevTok!=iTok );
if( iPrevTok<iTok ){
aLeft = pOut;
nLeft = nOut;
aRight = pList;
nRight = nList;
nDist = iTok-iPrevTok;
iPrevTok = iTok;
}else{
aRight = pOut;
nRight = nOut;
aLeft = pList;
nLeft = nList;
nDist = iPrevTok-iTok;
}
pOut = aRight;
fts3DoclistMerge(
mt, nDist, 0, pOut, &nOut, aLeft, nLeft, aRight, nRight, &nDoc
);
sqlite3_free(aLeft);
}
assert( nOut==0 || pOut!=0 );
}
if( rc==SQLITE_OK ){
if( ii!=pPhrase->nToken ){
assert( pCsr->eEvalmode==FTS3_EVAL_FILTER && isReqPos==0 );
fts3DoclistStripPositions(pOut, &nOut);
}
*paOut = pOut;
*pnOut = nOut;
}else{
sqlite3_free(pOut);
}
return rc;
}
static int fts3NearMerge(
int mergetype,
int nNear,
int nTokenLeft,
char *aLeft,
int nLeft,
int nTokenRight,
char *aRight,
int nRight,
char **paOut,
int *pnOut
){
char *aOut;
int rc;
assert( mergetype==MERGE_POS_NEAR || MERGE_NEAR );
aOut = sqlite3_malloc(nLeft+nRight+1);
if( aOut==0 ){
rc = SQLITE_NOMEM;
}else{
rc = fts3DoclistMerge(mergetype, nNear+nTokenRight, nNear+nTokenLeft,
aOut, pnOut, aLeft, nLeft, aRight, nRight, 0
);
if( rc!=SQLITE_OK ){
sqlite3_free(aOut);
aOut = 0;
}
}
*paOut = aOut;
return rc;
}
int sqlite3Fts3ExprNearTrim(Fts3Expr *pLeft, Fts3Expr *pRight, int nNear){
int rc;
assert( pLeft->eType==FTSQUERY_PHRASE );
assert( pRight->eType==FTSQUERY_PHRASE );
assert( pLeft->isLoaded && pRight->isLoaded );
if( pLeft->aDoclist==0 || pRight->aDoclist==0 ){
sqlite3_free(pLeft->aDoclist);
sqlite3_free(pRight->aDoclist);
pRight->aDoclist = 0;
pLeft->aDoclist = 0;
rc = SQLITE_OK;
}else{
char *aOut;
int nOut;
rc = fts3NearMerge(MERGE_POS_NEAR, nNear,
pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist,
pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist,
&aOut, &nOut
);
if( rc!=SQLITE_OK ) return rc;
sqlite3_free(pRight->aDoclist);
pRight->aDoclist = aOut;
pRight->nDoclist = nOut;
rc = fts3NearMerge(MERGE_POS_NEAR, nNear,
pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist,
pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist,
&aOut, &nOut
);
sqlite3_free(pLeft->aDoclist);
pLeft->aDoclist = aOut;
pLeft->nDoclist = nOut;
}
return rc;
}
static int fts3ExprAllocateSegReaders(
Fts3Cursor *pCsr,
Fts3Expr *pExpr,
int *pnExpr
){
int rc = SQLITE_OK;
assert( pCsr->eEvalmode==FTS3_EVAL_FILTER );
if( pnExpr && pExpr->eType!=FTSQUERY_AND ){
(*pnExpr)++;
pnExpr = 0;
}
if( pExpr->eType==FTSQUERY_PHRASE ){
Fts3Phrase *pPhrase = pExpr->pPhrase;
int ii;
for(ii=0; rc==SQLITE_OK && ii<pPhrase->nToken; ii++){
Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
if( pTok->pSegcsr==0 ){
rc = fts3TermSegReaderCursor(
pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
);
}
}
}else{
rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pLeft, pnExpr);
if( rc==SQLITE_OK ){
rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pRight, pnExpr);
}
}
return rc;
}
static void fts3ExprFreeSegReaders(Fts3Expr *pExpr){
if( pExpr ){
Fts3Phrase *pPhrase = pExpr->pPhrase;
if( pPhrase ){
int kk;
for(kk=0; kk<pPhrase->nToken; kk++){
fts3SegReaderCursorFree(pPhrase->aToken[kk].pSegcsr);
pPhrase->aToken[kk].pSegcsr = 0;
}
}
fts3ExprFreeSegReaders(pExpr->pLeft);
fts3ExprFreeSegReaders(pExpr->pRight);
}
}
static int fts3ExprCost(Fts3Expr *pExpr){
int nCost;
if( pExpr->eType==FTSQUERY_PHRASE ){
Fts3Phrase *pPhrase = pExpr->pPhrase;
int ii;
nCost = 0;
for(ii=0; ii<pPhrase->nToken; ii++){
Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[ii].pSegcsr;
if( pSegcsr ) nCost += pSegcsr->nCost;
}
}else{
nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight);
}
return nCost;
}
typedef struct ExprAndCost ExprAndCost;
struct ExprAndCost {
Fts3Expr *pExpr;
int nCost;
};
static void fts3ExprAssignCosts(
Fts3Expr *pExpr,
ExprAndCost **ppExprCost
){
if( pExpr->eType==FTSQUERY_AND ){
fts3ExprAssignCosts(pExpr->pLeft, ppExprCost);
fts3ExprAssignCosts(pExpr->pRight, ppExprCost);
}else{
(*ppExprCost)->pExpr = pExpr;
(*ppExprCost)->nCost = fts3ExprCost(pExpr);
(*ppExprCost)++;
}
}
static int fts3EvalExpr(
Fts3Cursor *p,
Fts3Expr *pExpr,
char **paOut,
int *pnOut,
int isReqPos
){
int rc = SQLITE_OK;
*paOut = 0;
*pnOut = 0;
if( pExpr ){
assert( pExpr->eType==FTSQUERY_NEAR || pExpr->eType==FTSQUERY_OR
|| pExpr->eType==FTSQUERY_AND || pExpr->eType==FTSQUERY_NOT
|| pExpr->eType==FTSQUERY_PHRASE
);
assert( pExpr->eType==FTSQUERY_PHRASE || isReqPos==0 );
if( pExpr->eType==FTSQUERY_PHRASE ){
rc = fts3PhraseSelect(p, pExpr->pPhrase,
isReqPos || (pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR),
paOut, pnOut
);
fts3ExprFreeSegReaders(pExpr);
}else if( p->eEvalmode==FTS3_EVAL_FILTER && pExpr->eType==FTSQUERY_AND ){
ExprAndCost *aExpr = 0;
int nExpr = 0;
char *aRet = 0;
int nRet = 0;
int nDoc = 0x7FFFFFFF;
assert( !isReqPos );
rc = fts3ExprAllocateSegReaders(p, pExpr, &nExpr);
if( rc==SQLITE_OK ){
assert( nExpr>1 );
aExpr = sqlite3_malloc(sizeof(ExprAndCost) * nExpr);
if( !aExpr ) rc = SQLITE_NOMEM;
}
if( rc==SQLITE_OK ){
int ii;
fts3ExprAssignCosts(pExpr, &aExpr);
aExpr -= nExpr;
for(ii=0; ii<nExpr; ii++){
char *aNew;
int nNew;
int jj;
ExprAndCost *pBest = 0;
for(jj=0; jj<nExpr; jj++){
ExprAndCost *pCand = &aExpr[jj];
if( pCand->pExpr && (pBest==0 || pCand->nCost<pBest->nCost) ){
pBest = pCand;
}
}
if( pBest->nCost>nDoc ){
rc = fts3DeferExpression(p, p->pExpr);
break;
}else{
rc = fts3EvalExpr(p, pBest->pExpr, &aNew, &nNew, 0);
if( rc!=SQLITE_OK ) break;
pBest->pExpr = 0;
if( ii==0 ){
aRet = aNew;
nRet = nNew;
nDoc = fts3DoclistCountDocids(0, aRet, nRet);
}else{
fts3DoclistMerge(
MERGE_AND, 0, 0, aRet, &nRet, aRet, nRet, aNew, nNew, &nDoc
);
sqlite3_free(aNew);
}
}
}
}
if( rc==SQLITE_OK ){
*paOut = aRet;
*pnOut = nRet;
}else{
assert( *paOut==0 );
sqlite3_free(aRet);
}
sqlite3_free(aExpr);
fts3ExprFreeSegReaders(pExpr);
}else{
char *aLeft;
char *aRight;
int nLeft;
int nRight;
assert( pExpr->eType==FTSQUERY_NEAR
|| pExpr->eType==FTSQUERY_OR
|| pExpr->eType==FTSQUERY_NOT
|| (pExpr->eType==FTSQUERY_AND && p->eEvalmode==FTS3_EVAL_NEXT)
);
if( 0==(rc = fts3EvalExpr(p, pExpr->pRight, &aRight, &nRight, isReqPos))
&& 0==(rc = fts3EvalExpr(p, pExpr->pLeft, &aLeft, &nLeft, isReqPos))
){
switch( pExpr->eType ){
case FTSQUERY_NEAR: {
Fts3Expr *pLeft;
Fts3Expr *pRight;
int mergetype = MERGE_NEAR;
if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){
mergetype = MERGE_POS_NEAR;
}
pLeft = pExpr->pLeft;
while( pLeft->eType==FTSQUERY_NEAR ){
pLeft=pLeft->pRight;
}
pRight = pExpr->pRight;
assert( pRight->eType==FTSQUERY_PHRASE );
assert( pLeft->eType==FTSQUERY_PHRASE );
rc = fts3NearMerge(mergetype, pExpr->nNear,
pLeft->pPhrase->nToken, aLeft, nLeft,
pRight->pPhrase->nToken, aRight, nRight,
paOut, pnOut
);
sqlite3_free(aLeft);
break;
}
case FTSQUERY_OR: {
char *aBuffer = sqlite3_malloc(nRight+nLeft+1);
rc = fts3DoclistMerge(MERGE_OR, 0, 0, aBuffer, pnOut,
aLeft, nLeft, aRight, nRight, 0
);
*paOut = aBuffer;
sqlite3_free(aLeft);
break;
}
default: {
assert( FTSQUERY_NOT==MERGE_NOT && FTSQUERY_AND==MERGE_AND );
fts3DoclistMerge(pExpr->eType, 0, 0, aLeft, pnOut,
aLeft, nLeft, aRight, nRight, 0
);
*paOut = aLeft;
break;
}
}
}
sqlite3_free(aRight);
}
}
assert( rc==SQLITE_OK || *paOut==0 );
return rc;
}
static int fts3EvalDeferred(
Fts3Cursor *pCsr,
int *pbRes
){
int rc = SQLITE_OK;
if( pCsr->pDeferred==0 ){
*pbRes = 1;
}else{
rc = fts3CursorSeek(0, pCsr);
if( rc==SQLITE_OK ){
sqlite3Fts3FreeDeferredDoclists(pCsr);
rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
}
if( rc==SQLITE_OK ){
char *a = 0;
int n = 0;
rc = fts3EvalExpr(pCsr, pCsr->pExpr, &a, &n, 0);
assert( n>=0 );
*pbRes = (n>0);
sqlite3_free(a);
}
}
return rc;
}
static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
int res;
int rc = SQLITE_OK;
Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
pCsr->eEvalmode = FTS3_EVAL_NEXT;
do {
if( pCsr->aDoclist==0 ){
if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
pCsr->isEof = 1;
rc = sqlite3_reset(pCsr->pStmt);
break;
}
pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
}else{
if( pCsr->pNextId>=&pCsr->aDoclist[pCsr->nDoclist] ){
pCsr->isEof = 1;
break;
}
sqlite3_reset(pCsr->pStmt);
fts3GetDeltaVarint(&pCsr->pNextId, &pCsr->iPrevId);
pCsr->isRequireSeek = 1;
pCsr->isMatchinfoNeeded = 1;
}
}while( SQLITE_OK==(rc = fts3EvalDeferred(pCsr, &res)) && res==0 );
return rc;
}
static int fts3FilterMethod(
sqlite3_vtab_cursor *pCursor,
int idxNum,
const char *idxStr,
int nVal,
sqlite3_value **apVal
){
const char *azSql[] = {
"SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?",
"SELECT %s FROM %Q.'%q_content' AS x ",
};
int rc;
char *zSql;
Fts3Table *p = (Fts3Table *)pCursor->pVtab;
Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
UNUSED_PARAMETER(idxStr);
UNUSED_PARAMETER(nVal);
assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
assert( nVal==0 || nVal==1 );
assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
assert( p->pSegments==0 );
sqlite3_finalize(pCsr->pStmt);
sqlite3_free(pCsr->aDoclist);
sqlite3Fts3ExprFree(pCsr->pExpr);
memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
return SQLITE_NOMEM;
}
rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn,
iCol, zQuery, -1, &pCsr->pExpr
);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_ERROR ){
p->base.zErrMsg = sqlite3_mprintf("malformed MATCH expression: [%s]",
zQuery);
}
return rc;
}
rc = sqlite3Fts3ReadLock(p);
if( rc!=SQLITE_OK ) return rc;
rc = fts3EvalExpr(pCsr, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0);
sqlite3Fts3SegmentsClose(p);
if( rc!=SQLITE_OK ) return rc;
pCsr->pNextId = pCsr->aDoclist;
pCsr->iPrevId = 0;
}
zSql = (char *)azSql[idxNum==FTS3_FULLSCAN_SEARCH];
zSql = sqlite3_mprintf(zSql, p->zReadExprlist, p->zDb, p->zName);
if( !zSql ){
rc = SQLITE_NOMEM;
}else{
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
sqlite3_free(zSql);
}
if( rc==SQLITE_OK && idxNum==FTS3_DOCID_SEARCH ){
rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
}
pCsr->eSearch = (i16)idxNum;
if( rc!=SQLITE_OK ) return rc;
return fts3NextMethod(pCursor);
}
static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
return ((Fts3Cursor *)pCursor)->isEof;
}
static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
if( pCsr->aDoclist ){
*pRowid = pCsr->iPrevId;
}else{
assert( pCsr->isRequireSeek==0 );
*pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
}
return SQLITE_OK;
}
static int fts3ColumnMethod(
sqlite3_vtab_cursor *pCursor,
sqlite3_context *pContext,
int iCol
){
int rc;
Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
Fts3Table *p = (Fts3Table *)pCursor->pVtab;
assert( iCol>=0 && iCol<=p->nColumn+1 );
if( iCol==p->nColumn+1 ){
sqlite3_int64 iRowid;
rc = fts3RowidMethod(pCursor, &iRowid);
sqlite3_result_int64(pContext, iRowid);
}else if( iCol==p->nColumn ){
sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
rc = SQLITE_OK;
}else{
rc = fts3CursorSeek(0, pCsr);
if( rc==SQLITE_OK ){
sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1));
}
}
return rc;
}
static int fts3UpdateMethod(
sqlite3_vtab *pVtab,
int nArg,
sqlite3_value **apVal,
sqlite_int64 *pRowid
){
return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
}
static int fts3SyncMethod(sqlite3_vtab *pVtab){
int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
return rc;
}
static int fts3BeginMethod(sqlite3_vtab *pVtab){
UNUSED_PARAMETER(pVtab);
assert( ((Fts3Table *)pVtab)->nPendingData==0 );
return SQLITE_OK;
}
static int fts3CommitMethod(sqlite3_vtab *pVtab){
UNUSED_PARAMETER(pVtab);
assert( ((Fts3Table *)pVtab)->nPendingData==0 );
return SQLITE_OK;
}
static int fts3RollbackMethod(sqlite3_vtab *pVtab){
sqlite3Fts3PendingTermsClear((Fts3Table *)pVtab);
return SQLITE_OK;
}
int sqlite3Fts3ExprLoadDoclist(Fts3Cursor *pCsr, Fts3Expr *pExpr){
int rc;
assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase );
assert( pCsr->eEvalmode==FTS3_EVAL_NEXT );
rc = fts3EvalExpr(pCsr, pExpr, &pExpr->aDoclist, &pExpr->nDoclist, 1);
return rc;
}
int sqlite3Fts3ExprLoadFtDoclist(
Fts3Cursor *pCsr,
Fts3Expr *pExpr,
char **paDoclist,
int *pnDoclist
){
int rc;
assert( pCsr->eEvalmode==FTS3_EVAL_NEXT );
assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase );
pCsr->eEvalmode = FTS3_EVAL_MATCHINFO;
rc = fts3EvalExpr(pCsr, pExpr, paDoclist, pnDoclist, 1);
pCsr->eEvalmode = FTS3_EVAL_NEXT;
return rc;
}
char *sqlite3Fts3FindPositions(
Fts3Expr *pExpr,
sqlite3_int64 iDocid,
int iCol
){
assert( pExpr->isLoaded );
if( pExpr->aDoclist ){
char *pEnd = &pExpr->aDoclist[pExpr->nDoclist];
char *pCsr;
if( pExpr->pCurrent==0 ){
pExpr->pCurrent = pExpr->aDoclist;
pExpr->iCurrent = 0;
pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent,&pExpr->iCurrent);
}
pCsr = pExpr->pCurrent;
assert( pCsr );
while( pCsr<pEnd ){
if( pExpr->iCurrent<iDocid ){
fts3PoslistCopy(0, &pCsr);
if( pCsr<pEnd ){
fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent);
}
pExpr->pCurrent = pCsr;
}else{
if( pExpr->iCurrent==iDocid ){
int iThis = 0;
if( iCol<0 ){
return pCsr;
}
while( iThis<iCol ){
fts3ColumnlistCopy(0, &pCsr);
if( *pCsr==0x00 ) return 0;
pCsr++;
pCsr += sqlite3Fts3GetVarint32(pCsr, &iThis);
}
if( iCol==iThis && (*pCsr&0xFE) ) return pCsr;
}
return 0;
}
}
}
return 0;
}
static int fts3FunctionArg(
sqlite3_context *pContext,
const char *zFunc,
sqlite3_value *pVal,
Fts3Cursor **ppCsr
){
Fts3Cursor *pRet;
if( sqlite3_value_type(pVal)!=SQLITE_BLOB
|| sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
){
char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
sqlite3_result_error(pContext, zErr, -1);
sqlite3_free(zErr);
return SQLITE_ERROR;
}
memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
*ppCsr = pRet;
return SQLITE_OK;
}
static void fts3SnippetFunc(
sqlite3_context *pContext,
int nVal,
sqlite3_value **apVal
){
Fts3Cursor *pCsr;
const char *zStart = "<b>";
const char *zEnd = "</b>";
const char *zEllipsis = "<b>...</b>";
int iCol = -1;
int nToken = 15;
assert( nVal>=1 );
if( nVal>6 ){
sqlite3_result_error(pContext,
"wrong number of arguments to function snippet()", -1);
return;
}
if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
switch( nVal ){
case 6: nToken = sqlite3_value_int(apVal[5]);
case 5: iCol = sqlite3_value_int(apVal[4]);
case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
}
if( !zEllipsis || !zEnd || !zStart ){
sqlite3_result_error_nomem(pContext);
}else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
}
}
static void fts3OffsetsFunc(
sqlite3_context *pContext,
int nVal,
sqlite3_value **apVal
){
Fts3Cursor *pCsr;
UNUSED_PARAMETER(nVal);
assert( nVal==1 );
if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
assert( pCsr );
if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
sqlite3Fts3Offsets(pContext, pCsr);
}
}
static void fts3OptimizeFunc(
sqlite3_context *pContext,
int nVal,
sqlite3_value **apVal
){
int rc;
Fts3Table *p;
Fts3Cursor *pCursor;
UNUSED_PARAMETER(nVal);
assert( nVal==1 );
if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
p = (Fts3Table *)pCursor->base.pVtab;
assert( p );
rc = sqlite3Fts3Optimize(p);
switch( rc ){
case SQLITE_OK:
sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
break;
case SQLITE_DONE:
sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
break;
default:
sqlite3_result_error_code(pContext, rc);
break;
}
}
static void fts3MatchinfoFunc(
sqlite3_context *pContext,
int nVal,
sqlite3_value **apVal
){
Fts3Cursor *pCsr;
assert( nVal==1 || nVal==2 );
if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
const char *zArg = 0;
if( nVal>1 ){
zArg = (const char *)sqlite3_value_text(apVal[1]);
}
sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
}
}
static int fts3FindFunctionMethod(
sqlite3_vtab *pVtab,
int nArg,
const char *zName,
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
void **ppArg
){
struct Overloaded {
const char *zName;
void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
} aOverload[] = {
{ "snippet", fts3SnippetFunc },
{ "offsets", fts3OffsetsFunc },
{ "optimize", fts3OptimizeFunc },
{ "matchinfo", fts3MatchinfoFunc },
};
int i;
UNUSED_PARAMETER(pVtab);
UNUSED_PARAMETER(nArg);
UNUSED_PARAMETER(ppArg);
for(i=0; i<SizeofArray(aOverload); i++){
if( strcmp(zName, aOverload[i].zName)==0 ){
*pxFunc = aOverload[i].xFunc;
return 1;
}
}
return 0;
}
static int fts3RenameMethod(
sqlite3_vtab *pVtab,
const char *zName
){
Fts3Table *p = (Fts3Table *)pVtab;
sqlite3 *db = p->db;
int rc;
rc = sqlite3Fts3PendingTermsFlush(p);
if( rc!=SQLITE_OK ){
return rc;
}
fts3DbExec(&rc, db,
"ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
p->zDb, p->zName, zName
);
if( p->bHasDocsize ){
fts3DbExec(&rc, db,
"ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
p->zDb, p->zName, zName
);
}
if( p->bHasStat ){
fts3DbExec(&rc, db,
"ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
p->zDb, p->zName, zName
);
}
fts3DbExec(&rc, db,
"ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
p->zDb, p->zName, zName
);
fts3DbExec(&rc, db,
"ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
p->zDb, p->zName, zName
);
return rc;
}
static const sqlite3_module fts3Module = {
0,
fts3CreateMethod,
fts3ConnectMethod,
fts3BestIndexMethod,
fts3DisconnectMethod,
fts3DestroyMethod,
fts3OpenMethod,
fts3CloseMethod,
fts3FilterMethod,
fts3NextMethod,
fts3EofMethod,
fts3ColumnMethod,
fts3RowidMethod,
fts3UpdateMethod,
fts3BeginMethod,
fts3SyncMethod,
fts3CommitMethod,
fts3RollbackMethod,
fts3FindFunctionMethod,
fts3RenameMethod,
};
static void hashDestroy(void *p){
Fts3Hash *pHash = (Fts3Hash *)p;
sqlite3Fts3HashClear(pHash);
sqlite3_free(pHash);
}
void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
#ifdef SQLITE_ENABLE_ICU
void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
#endif
int sqlite3Fts3Init(sqlite3 *db){
int rc = SQLITE_OK;
Fts3Hash *pHash = 0;
const sqlite3_tokenizer_module *pSimple = 0;
const sqlite3_tokenizer_module *pPorter = 0;
#ifdef SQLITE_ENABLE_ICU
const sqlite3_tokenizer_module *pIcu = 0;
sqlite3Fts3IcuTokenizerModule(&pIcu);
#endif
rc = sqlite3Fts3InitAux(db);
if( rc!=SQLITE_OK ) return rc;
sqlite3Fts3SimpleTokenizerModule(&pSimple);
sqlite3Fts3PorterTokenizerModule(&pPorter);
pHash = sqlite3_malloc(sizeof(Fts3Hash));
if( !pHash ){
rc = SQLITE_NOMEM;
}else{
sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
}
if( rc==SQLITE_OK ){
if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
|| sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
#ifdef SQLITE_ENABLE_ICU
|| (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
#endif
){
rc = SQLITE_NOMEM;
}
}
#ifdef SQLITE_TEST
if( rc==SQLITE_OK ){
rc = sqlite3Fts3ExprInitTestInterface(db);
}
#endif
if( SQLITE_OK==rc
#if CHROMIUM_FTS3_CHANGES && !SQLITE_TEST
#else
&& SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
#endif
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
){
rc = sqlite3_create_module_v2(
db, "fts3", &fts3Module, (void *)pHash, hashDestroy
);
#if CHROMIUM_FTS3_CHANGES && !SQLITE_TEST
#else
if( rc==SQLITE_OK ){
rc = sqlite3_create_module_v2(
db, "fts4", &fts3Module, (void *)pHash, 0
);
}
#endif
return rc;
}
assert( rc!=SQLITE_OK );
if( pHash ){
sqlite3Fts3HashClear(pHash);
sqlite3_free(pHash);
}
return rc;
}
#if !SQLITE_CORE
int sqlite3_extension_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
SQLITE_EXTENSION_INIT2(pApi)
return sqlite3Fts3Init(db);
}
#endif
#endif