| 1 |
/* |
| 2 |
** 2015 May 08 |
| 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 |
** This is an SQLite virtual table module implementing direct access to an |
| 14 |
** existing FTS5 index. The module may create several different types of |
| 15 |
** tables: |
| 16 |
** |
| 17 |
** col: |
| 18 |
** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col)); |
| 19 |
** |
| 20 |
** One row for each term/column combination. The value of $doc is set to |
| 21 |
** the number of fts5 rows that contain at least one instance of term |
| 22 |
** $term within column $col. Field $cnt is set to the total number of |
| 23 |
** instances of term $term in column $col (in any row of the fts5 table). |
| 24 |
** |
| 25 |
** row: |
| 26 |
** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term)); |
| 27 |
** |
| 28 |
** One row for each term in the database. The value of $doc is set to |
| 29 |
** the number of fts5 rows that contain at least one instance of term |
| 30 |
** $term. Field $cnt is set to the total number of instances of term |
| 31 |
** $term in the database. |
| 32 |
** |
| 33 |
** instance: |
| 34 |
** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>)); |
| 35 |
** |
| 36 |
** One row for each term instance in the database. |
| 37 |
*/ |
| 38 |
|
| 39 |
|
| 40 |
#include "fts5Int.h" |
| 41 |
|
| 42 |
|
| 43 |
typedef struct Fts5VocabTable Fts5VocabTable; |
| 44 |
typedef struct Fts5VocabCursor Fts5VocabCursor; |
| 45 |
|
| 46 |
struct Fts5VocabTable { |
| 47 |
sqlite3_vtab base; |
| 48 |
char *zFts5Tbl; /* Name of fts5 table */ |
| 49 |
char *zFts5Db; /* Db containing fts5 table */ |
| 50 |
sqlite3 *db; /* Database handle */ |
| 51 |
Fts5Global *pGlobal; /* FTS5 global object for this database */ |
| 52 |
int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */ |
| 53 |
unsigned bBusy; /* True if busy */ |
| 54 |
}; |
| 55 |
|
| 56 |
struct Fts5VocabCursor { |
| 57 |
sqlite3_vtab_cursor base; |
| 58 |
sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */ |
| 59 |
Fts5Table *pFts5; /* Associated FTS5 table */ |
| 60 |
|
| 61 |
int bEof; /* True if this cursor is at EOF */ |
| 62 |
Fts5IndexIter *pIter; /* Term/rowid iterator object */ |
| 63 |
void *pStruct; /* From sqlite3Fts5StructureRef() */ |
| 64 |
|
| 65 |
int nLeTerm; /* Size of zLeTerm in bytes */ |
| 66 |
char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */ |
| 67 |
int colUsed; /* Copy of sqlite3_index_info.colUsed */ |
| 68 |
|
| 69 |
/* These are used by 'col' tables only */ |
| 70 |
int iCol; |
| 71 |
i64 *aCnt; |
| 72 |
i64 *aDoc; |
| 73 |
|
| 74 |
/* Output values used by all tables. */ |
| 75 |
i64 rowid; /* This table's current rowid value */ |
| 76 |
Fts5Buffer term; /* Current value of 'term' column */ |
| 77 |
|
| 78 |
/* Output values Used by 'instance' tables only */ |
| 79 |
i64 iInstPos; |
| 80 |
int iInstOff; |
| 81 |
}; |
| 82 |
|
| 83 |
#define FTS5_VOCAB_COL 0 |
| 84 |
#define FTS5_VOCAB_ROW 1 |
| 85 |
#define FTS5_VOCAB_INSTANCE 2 |
| 86 |
|
| 87 |
#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt" |
| 88 |
#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt" |
| 89 |
#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset" |
| 90 |
|
| 91 |
/* |
| 92 |
** Bits for the mask used as the idxNum value by xBestIndex/xFilter. |
| 93 |
*/ |
| 94 |
#define FTS5_VOCAB_TERM_EQ 0x0100 |
| 95 |
#define FTS5_VOCAB_TERM_GE 0x0200 |
| 96 |
#define FTS5_VOCAB_TERM_LE 0x0400 |
| 97 |
|
| 98 |
#define FTS5_VOCAB_COLUSED_MASK 0xFF |
| 99 |
|
| 100 |
|
| 101 |
/* |
| 102 |
** Translate a string containing an fts5vocab table type to an |
| 103 |
** FTS5_VOCAB_XXX constant. If successful, set *peType to the output |
| 104 |
** value and return SQLITE_OK. Otherwise, set *pzErr to an error message |
| 105 |
** and return SQLITE_ERROR. |
| 106 |
*/ |
| 107 |
static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){ |
| 108 |
int rc = SQLITE_OK; |
| 109 |
char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1); |
| 110 |
if( rc==SQLITE_OK ){ |
| 111 |
sqlite3Fts5Dequote(zCopy); |
| 112 |
if( sqlite3_stricmp(zCopy, "col")==0 ){ |
| 113 |
*peType = FTS5_VOCAB_COL; |
| 114 |
}else |
| 115 |
|
| 116 |
if( sqlite3_stricmp(zCopy, "row")==0 ){ |
| 117 |
*peType = FTS5_VOCAB_ROW; |
| 118 |
}else |
| 119 |
if( sqlite3_stricmp(zCopy, "instance")==0 ){ |
| 120 |
*peType = FTS5_VOCAB_INSTANCE; |
| 121 |
}else |
| 122 |
{ |
| 123 |
*pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy); |
| 124 |
rc = SQLITE_ERROR; |
| 125 |
} |
| 126 |
sqlite3_free(zCopy); |
| 127 |
} |
| 128 |
|
| 129 |
return rc; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/* |
| 134 |
** The xDisconnect() virtual table method. |
| 135 |
*/ |
| 136 |
static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){ |
| 137 |
Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab; |
| 138 |
sqlite3_free(pTab); |
| 139 |
return SQLITE_OK; |
| 140 |
} |
| 141 |
|
| 142 |
/* |
| 143 |
** The xDestroy() virtual table method. |
| 144 |
*/ |
| 145 |
static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){ |
| 146 |
Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab; |
| 147 |
sqlite3_free(pTab); |
| 148 |
return SQLITE_OK; |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
** This function is the implementation of both the xConnect and xCreate |
| 153 |
** methods of the FTS3 virtual table. |
| 154 |
** |
| 155 |
** The argv[] array contains the following: |
| 156 |
** |
| 157 |
** argv[0] -> module name ("fts5vocab") |
| 158 |
** argv[1] -> database name |
| 159 |
** argv[2] -> table name |
| 160 |
** |
| 161 |
** then: |
| 162 |
** |
| 163 |
** argv[3] -> name of fts5 table |
| 164 |
** argv[4] -> type of fts5vocab table |
| 165 |
** |
| 166 |
** or, for tables in the TEMP schema only. |
| 167 |
** |
| 168 |
** argv[3] -> name of fts5 tables database |
| 169 |
** argv[4] -> name of fts5 table |
| 170 |
** argv[5] -> type of fts5vocab table |
| 171 |
*/ |
| 172 |
static int fts5VocabInitVtab( |
| 173 |
sqlite3 *db, /* The SQLite database connection */ |
| 174 |
void *pAux, /* Pointer to Fts5Global object */ |
| 175 |
int argc, /* Number of elements in argv array */ |
| 176 |
const char * const *argv, /* xCreate/xConnect argument array */ |
| 177 |
sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */ |
| 178 |
char **pzErr /* Write any error message here */ |
| 179 |
){ |
| 180 |
const char *azSchema[] = { |
| 181 |
"CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")", |
| 182 |
"CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")", |
| 183 |
"CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")" |
| 184 |
}; |
| 185 |
|
| 186 |
Fts5VocabTable *pRet = 0; |
| 187 |
int rc = SQLITE_OK; /* Return code */ |
| 188 |
int bDb; |
| 189 |
|
| 190 |
bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0); |
| 191 |
|
| 192 |
if( argc!=5 && bDb==0 ){ |
| 193 |
*pzErr = sqlite3_mprintf("wrong number of vtable arguments"); |
| 194 |
rc = SQLITE_ERROR; |
| 195 |
}else{ |
| 196 |
i64 nByte; /* Bytes of space to allocate */ |
| 197 |
const char *zDb = bDb ? argv[3] : argv[1]; |
| 198 |
const char *zTab = bDb ? argv[4] : argv[3]; |
| 199 |
const char *zType = bDb ? argv[5] : argv[4]; |
| 200 |
i64 nDb = strlen(zDb)+1; |
| 201 |
i64 nTab = strlen(zTab)+1; |
| 202 |
int eType = 0; |
| 203 |
|
| 204 |
rc = fts5VocabTableType(zType, pzErr, &eType); |
| 205 |
if( rc==SQLITE_OK ){ |
| 206 |
assert( eType>=0 && eType<ArraySize(azSchema) ); |
| 207 |
rc = sqlite3_declare_vtab(db, azSchema[eType]); |
| 208 |
} |
| 209 |
|
| 210 |
nByte = sizeof(Fts5VocabTable) + nDb + nTab; |
| 211 |
pRet = sqlite3Fts5MallocZero(&rc, nByte); |
| 212 |
if( pRet ){ |
| 213 |
pRet->pGlobal = (Fts5Global*)pAux; |
| 214 |
pRet->eType = eType; |
| 215 |
pRet->db = db; |
| 216 |
pRet->zFts5Tbl = (char*)&pRet[1]; |
| 217 |
pRet->zFts5Db = &pRet->zFts5Tbl[nTab]; |
| 218 |
memcpy(pRet->zFts5Tbl, zTab, nTab); |
| 219 |
memcpy(pRet->zFts5Db, zDb, nDb); |
| 220 |
sqlite3Fts5Dequote(pRet->zFts5Tbl); |
| 221 |
sqlite3Fts5Dequote(pRet->zFts5Db); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
*ppVTab = (sqlite3_vtab*)pRet; |
| 226 |
return rc; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/* |
| 231 |
** The xConnect() and xCreate() methods for the virtual table. All the |
| 232 |
** work is done in function fts5VocabInitVtab(). |
| 233 |
*/ |
| 234 |
static int fts5VocabConnectMethod( |
| 235 |
sqlite3 *db, /* Database connection */ |
| 236 |
void *pAux, /* Pointer to tokenizer hash table */ |
| 237 |
int argc, /* Number of elements in argv array */ |
| 238 |
const char * const *argv, /* xCreate/xConnect argument array */ |
| 239 |
sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ |
| 240 |
char **pzErr /* OUT: sqlite3_malloc'd error message */ |
| 241 |
){ |
| 242 |
return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr); |
| 243 |
} |
| 244 |
static int fts5VocabCreateMethod( |
| 245 |
sqlite3 *db, /* Database connection */ |
| 246 |
void *pAux, /* Pointer to tokenizer hash table */ |
| 247 |
int argc, /* Number of elements in argv array */ |
| 248 |
const char * const *argv, /* xCreate/xConnect argument array */ |
| 249 |
sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ |
| 250 |
char **pzErr /* OUT: sqlite3_malloc'd error message */ |
| 251 |
){ |
| 252 |
return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr); |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
** Implementation of the xBestIndex method. |
| 257 |
** |
| 258 |
** Only constraints of the form: |
| 259 |
** |
| 260 |
** term <= ? |
| 261 |
** term == ? |
| 262 |
** term >= ? |
| 263 |
** |
| 264 |
** are interpreted. Less-than and less-than-or-equal are treated |
| 265 |
** identically, as are greater-than and greater-than-or-equal. |
| 266 |
*/ |
| 267 |
static int fts5VocabBestIndexMethod( |
| 268 |
sqlite3_vtab *pUnused, |
| 269 |
sqlite3_index_info *pInfo |
| 270 |
){ |
| 271 |
int i; |
| 272 |
int iTermEq = -1; |
| 273 |
int iTermGe = -1; |
| 274 |
int iTermLe = -1; |
| 275 |
int idxNum = (int)pInfo->colUsed; |
| 276 |
int nArg = 0; |
| 277 |
|
| 278 |
UNUSED_PARAM(pUnused); |
| 279 |
|
| 280 |
assert( (pInfo->colUsed & FTS5_VOCAB_COLUSED_MASK)==pInfo->colUsed ); |
| 281 |
|
| 282 |
for(i=0; i<pInfo->nConstraint; i++){ |
| 283 |
struct sqlite3_index_constraint *p = &pInfo->aConstraint[i]; |
| 284 |
if( p->usable==0 ) continue; |
| 285 |
if( p->iColumn==0 ){ /* term column */ |
| 286 |
if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i; |
| 287 |
if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i; |
| 288 |
if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i; |
| 289 |
if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i; |
| 290 |
if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
if( iTermEq>=0 ){ |
| 295 |
idxNum |= FTS5_VOCAB_TERM_EQ; |
| 296 |
pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg; |
| 297 |
pInfo->estimatedCost = 100; |
| 298 |
}else{ |
| 299 |
pInfo->estimatedCost = 1000000; |
| 300 |
if( iTermGe>=0 ){ |
| 301 |
idxNum |= FTS5_VOCAB_TERM_GE; |
| 302 |
pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg; |
| 303 |
pInfo->estimatedCost = pInfo->estimatedCost / 2; |
| 304 |
} |
| 305 |
if( iTermLe>=0 ){ |
| 306 |
idxNum |= FTS5_VOCAB_TERM_LE; |
| 307 |
pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg; |
| 308 |
pInfo->estimatedCost = pInfo->estimatedCost / 2; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/* This virtual table always delivers results in ascending order of |
| 313 |
** the "term" column (column 0). So if the user has requested this |
| 314 |
** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the |
| 315 |
** sqlite3_index_info.orderByConsumed flag to tell the core the results |
| 316 |
** are already in sorted order. */ |
| 317 |
if( pInfo->nOrderBy==1 |
| 318 |
&& pInfo->aOrderBy[0].iColumn==0 |
| 319 |
&& pInfo->aOrderBy[0].desc==0 |
| 320 |
){ |
| 321 |
pInfo->orderByConsumed = 1; |
| 322 |
} |
| 323 |
|
| 324 |
pInfo->idxNum = idxNum; |
| 325 |
return SQLITE_OK; |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
** Implementation of xOpen method. |
| 330 |
*/ |
| 331 |
static int fts5VocabOpenMethod( |
| 332 |
sqlite3_vtab *pVTab, |
| 333 |
sqlite3_vtab_cursor **ppCsr |
| 334 |
){ |
| 335 |
Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab; |
| 336 |
Fts5Table *pFts5 = 0; |
| 337 |
Fts5VocabCursor *pCsr = 0; |
| 338 |
int rc = SQLITE_OK; |
| 339 |
sqlite3_stmt *pStmt = 0; |
| 340 |
char *zSql = 0; |
| 341 |
|
| 342 |
if( pTab->bBusy ){ |
| 343 |
pVTab->zErrMsg = sqlite3_mprintf( |
| 344 |
"recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl |
| 345 |
); |
| 346 |
return SQLITE_ERROR; |
| 347 |
} |
| 348 |
zSql = sqlite3Fts5Mprintf(&rc, |
| 349 |
"SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'", |
| 350 |
pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl |
| 351 |
); |
| 352 |
if( zSql ){ |
| 353 |
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0); |
| 354 |
} |
| 355 |
sqlite3_free(zSql); |
| 356 |
assert( rc==SQLITE_OK || pStmt==0 ); |
| 357 |
if( rc==SQLITE_ERROR ) rc = SQLITE_OK; |
| 358 |
|
| 359 |
pTab->bBusy = 1; |
| 360 |
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 361 |
i64 iId = sqlite3_column_int64(pStmt, 0); |
| 362 |
pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId); |
| 363 |
} |
| 364 |
pTab->bBusy = 0; |
| 365 |
|
| 366 |
if( rc==SQLITE_OK ){ |
| 367 |
if( pFts5==0 ){ |
| 368 |
rc = sqlite3_finalize(pStmt); |
| 369 |
pStmt = 0; |
| 370 |
if( rc==SQLITE_OK ){ |
| 371 |
pVTab->zErrMsg = sqlite3_mprintf( |
| 372 |
"no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl |
| 373 |
); |
| 374 |
rc = SQLITE_ERROR; |
| 375 |
} |
| 376 |
}else{ |
| 377 |
rc = sqlite3Fts5FlushToDisk(pFts5); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
if( rc==SQLITE_OK ){ |
| 382 |
i64 nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor); |
| 383 |
pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte); |
| 384 |
} |
| 385 |
|
| 386 |
if( pCsr ){ |
| 387 |
pCsr->pFts5 = pFts5; |
| 388 |
pCsr->pStmt = pStmt; |
| 389 |
pCsr->aCnt = (i64*)&pCsr[1]; |
| 390 |
pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol]; |
| 391 |
}else{ |
| 392 |
sqlite3_finalize(pStmt); |
| 393 |
} |
| 394 |
|
| 395 |
*ppCsr = (sqlite3_vtab_cursor*)pCsr; |
| 396 |
return rc; |
| 397 |
} |
| 398 |
|
| 399 |
/* |
| 400 |
** Restore cursor pCsr to the state it was in immediately after being |
| 401 |
** created by the xOpen() method. |
| 402 |
*/ |
| 403 |
static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){ |
| 404 |
int nCol = pCsr->pFts5->pConfig->nCol; |
| 405 |
pCsr->rowid = 0; |
| 406 |
sqlite3Fts5IterClose(pCsr->pIter); |
| 407 |
sqlite3Fts5StructureRelease(pCsr->pStruct); |
| 408 |
pCsr->pStruct = 0; |
| 409 |
pCsr->pIter = 0; |
| 410 |
sqlite3_free(pCsr->zLeTerm); |
| 411 |
pCsr->nLeTerm = -1; |
| 412 |
pCsr->zLeTerm = 0; |
| 413 |
pCsr->bEof = 0; |
| 414 |
pCsr->iCol = 0; |
| 415 |
pCsr->iInstPos = 0; |
| 416 |
pCsr->iInstOff = 0; |
| 417 |
pCsr->colUsed = 0; |
| 418 |
memset(pCsr->aCnt, 0, sizeof(i64)*nCol); |
| 419 |
memset(pCsr->aDoc, 0, sizeof(i64)*nCol); |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
** Close the cursor. For additional information see the documentation |
| 424 |
** on the xClose method of the virtual table interface. |
| 425 |
*/ |
| 426 |
static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){ |
| 427 |
Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; |
| 428 |
fts5VocabResetCursor(pCsr); |
| 429 |
sqlite3Fts5BufferFree(&pCsr->term); |
| 430 |
sqlite3_finalize(pCsr->pStmt); |
| 431 |
sqlite3_free(pCsr); |
| 432 |
return SQLITE_OK; |
| 433 |
} |
| 434 |
|
| 435 |
static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){ |
| 436 |
int rc = SQLITE_OK; |
| 437 |
|
| 438 |
if( sqlite3Fts5IterEof(pCsr->pIter) ){ |
| 439 |
pCsr->bEof = 1; |
| 440 |
}else{ |
| 441 |
const char *zTerm; |
| 442 |
int nTerm; |
| 443 |
zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm); |
| 444 |
if( pCsr->nLeTerm>=0 ){ |
| 445 |
int nCmp = MIN(nTerm, pCsr->nLeTerm); |
| 446 |
int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp); |
| 447 |
if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){ |
| 448 |
pCsr->bEof = 1; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm); |
| 453 |
} |
| 454 |
return rc; |
| 455 |
} |
| 456 |
|
| 457 |
static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){ |
| 458 |
int eDetail = pCsr->pFts5->pConfig->eDetail; |
| 459 |
int rc = SQLITE_OK; |
| 460 |
Fts5IndexIter *pIter = pCsr->pIter; |
| 461 |
i64 *pp = &pCsr->iInstPos; |
| 462 |
int *po = &pCsr->iInstOff; |
| 463 |
|
| 464 |
assert( sqlite3Fts5IterEof(pIter)==0 ); |
| 465 |
assert( pCsr->bEof==0 ); |
| 466 |
while( eDetail==FTS5_DETAIL_NONE |
| 467 |
|| sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp) |
| 468 |
){ |
| 469 |
pCsr->iInstPos = 0; |
| 470 |
pCsr->iInstOff = 0; |
| 471 |
|
| 472 |
rc = sqlite3Fts5IterNextScan(pCsr->pIter); |
| 473 |
if( rc==SQLITE_OK ){ |
| 474 |
rc = fts5VocabInstanceNewTerm(pCsr); |
| 475 |
if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break; |
| 476 |
} |
| 477 |
if( rc ){ |
| 478 |
pCsr->bEof = 1; |
| 479 |
break; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
return rc; |
| 484 |
} |
| 485 |
|
| 486 |
/* |
| 487 |
** Advance the cursor to the next row in the table. |
| 488 |
*/ |
| 489 |
static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){ |
| 490 |
Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; |
| 491 |
Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab; |
| 492 |
int nCol = pCsr->pFts5->pConfig->nCol; |
| 493 |
int rc; |
| 494 |
|
| 495 |
rc = sqlite3Fts5StructureTest(pCsr->pFts5->pIndex, pCsr->pStruct); |
| 496 |
if( rc!=SQLITE_OK ) return rc; |
| 497 |
pCsr->rowid++; |
| 498 |
|
| 499 |
if( pTab->eType==FTS5_VOCAB_INSTANCE ){ |
| 500 |
return fts5VocabInstanceNext(pCsr); |
| 501 |
} |
| 502 |
|
| 503 |
if( pTab->eType==FTS5_VOCAB_COL ){ |
| 504 |
for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){ |
| 505 |
if( pCsr->aDoc[pCsr->iCol] ) break; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){ |
| 510 |
if( sqlite3Fts5IterEof(pCsr->pIter) ){ |
| 511 |
pCsr->bEof = 1; |
| 512 |
}else{ |
| 513 |
const char *zTerm; |
| 514 |
int nTerm; |
| 515 |
|
| 516 |
zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm); |
| 517 |
assert( nTerm>=0 ); |
| 518 |
if( pCsr->nLeTerm>=0 ){ |
| 519 |
int nCmp = MIN(nTerm, pCsr->nLeTerm); |
| 520 |
int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp); |
| 521 |
if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){ |
| 522 |
pCsr->bEof = 1; |
| 523 |
return SQLITE_OK; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm); |
| 528 |
memset(pCsr->aCnt, 0, nCol * sizeof(i64)); |
| 529 |
memset(pCsr->aDoc, 0, nCol * sizeof(i64)); |
| 530 |
pCsr->iCol = 0; |
| 531 |
|
| 532 |
assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW ); |
| 533 |
while( rc==SQLITE_OK ){ |
| 534 |
int eDetail = pCsr->pFts5->pConfig->eDetail; |
| 535 |
const u8 *pPos; int nPos; /* Position list */ |
| 536 |
i64 iPos = 0; /* 64-bit position read from poslist */ |
| 537 |
int iOff = 0; /* Current offset within position list */ |
| 538 |
|
| 539 |
pPos = pCsr->pIter->pData; |
| 540 |
nPos = pCsr->pIter->nData; |
| 541 |
|
| 542 |
switch( pTab->eType ){ |
| 543 |
case FTS5_VOCAB_ROW: |
| 544 |
/* Do not bother counting the number of instances if the "cnt" |
| 545 |
** column is not being read (according to colUsed). */ |
| 546 |
if( eDetail==FTS5_DETAIL_FULL && (pCsr->colUsed & 0x04) ){ |
| 547 |
while( iPos<nPos ){ |
| 548 |
u32 ii; |
| 549 |
fts5FastGetVarint32(pPos, iPos, ii); |
| 550 |
if( ii==1 ){ |
| 551 |
/* New column in the position list */ |
| 552 |
fts5FastGetVarint32(pPos, iPos, ii); |
| 553 |
}else{ |
| 554 |
/* An instance - increment pCsr->aCnt[] */ |
| 555 |
pCsr->aCnt[0]++; |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
pCsr->aDoc[0]++; |
| 560 |
break; |
| 561 |
|
| 562 |
case FTS5_VOCAB_COL: |
| 563 |
if( eDetail==FTS5_DETAIL_FULL ){ |
| 564 |
int iCol = -1; |
| 565 |
while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){ |
| 566 |
int ii = FTS5_POS2COLUMN(iPos); |
| 567 |
if( iCol!=ii ){ |
| 568 |
if( ii>=nCol ){ |
| 569 |
rc = FTS5_CORRUPT; |
| 570 |
break; |
| 571 |
} |
| 572 |
pCsr->aDoc[ii]++; |
| 573 |
iCol = ii; |
| 574 |
} |
| 575 |
pCsr->aCnt[ii]++; |
| 576 |
} |
| 577 |
}else if( eDetail==FTS5_DETAIL_COLUMNS ){ |
| 578 |
while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){ |
| 579 |
assert_nc( iPos>=0 && iPos<nCol ); |
| 580 |
if( iPos>=nCol ){ |
| 581 |
rc = FTS5_CORRUPT; |
| 582 |
break; |
| 583 |
} |
| 584 |
pCsr->aDoc[iPos]++; |
| 585 |
} |
| 586 |
}else{ |
| 587 |
assert( eDetail==FTS5_DETAIL_NONE ); |
| 588 |
pCsr->aDoc[0]++; |
| 589 |
} |
| 590 |
break; |
| 591 |
|
| 592 |
default: |
| 593 |
assert( pTab->eType==FTS5_VOCAB_INSTANCE ); |
| 594 |
break; |
| 595 |
} |
| 596 |
|
| 597 |
if( rc==SQLITE_OK ){ |
| 598 |
rc = sqlite3Fts5IterNextScan(pCsr->pIter); |
| 599 |
} |
| 600 |
if( pTab->eType==FTS5_VOCAB_INSTANCE ) break; |
| 601 |
|
| 602 |
if( rc==SQLITE_OK ){ |
| 603 |
zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm); |
| 604 |
if( nTerm!=pCsr->term.n |
| 605 |
|| (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm)) |
| 606 |
){ |
| 607 |
break; |
| 608 |
} |
| 609 |
if( sqlite3Fts5IterEof(pCsr->pIter) ) break; |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){ |
| 616 |
for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++); |
| 617 |
if( pCsr->iCol==nCol ){ |
| 618 |
rc = FTS5_CORRUPT; |
| 619 |
} |
| 620 |
} |
| 621 |
return rc; |
| 622 |
} |
| 623 |
|
| 624 |
/* |
| 625 |
** This is the xFilter implementation for the virtual table. |
| 626 |
*/ |
| 627 |
static int fts5VocabFilterMethod( |
| 628 |
sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ |
| 629 |
int idxNum, /* Strategy index */ |
| 630 |
const char *zUnused, /* Unused */ |
| 631 |
int nUnused, /* Number of elements in apVal */ |
| 632 |
sqlite3_value **apVal /* Arguments for the indexing scheme */ |
| 633 |
){ |
| 634 |
Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab; |
| 635 |
Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; |
| 636 |
int eType = pTab->eType; |
| 637 |
int rc = SQLITE_OK; |
| 638 |
|
| 639 |
int iVal = 0; |
| 640 |
int f = FTS5INDEX_QUERY_SCAN; |
| 641 |
const char *zTerm = 0; |
| 642 |
int nTerm = 0; |
| 643 |
|
| 644 |
sqlite3_value *pEq = 0; |
| 645 |
sqlite3_value *pGe = 0; |
| 646 |
sqlite3_value *pLe = 0; |
| 647 |
|
| 648 |
UNUSED_PARAM2(zUnused, nUnused); |
| 649 |
|
| 650 |
fts5VocabResetCursor(pCsr); |
| 651 |
if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++]; |
| 652 |
if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++]; |
| 653 |
if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++]; |
| 654 |
pCsr->colUsed = (idxNum & FTS5_VOCAB_COLUSED_MASK); |
| 655 |
|
| 656 |
if( pEq ){ |
| 657 |
zTerm = (const char *)sqlite3_value_text(pEq); |
| 658 |
nTerm = sqlite3_value_bytes(pEq); |
| 659 |
f = FTS5INDEX_QUERY_NOTOKENDATA; |
| 660 |
}else{ |
| 661 |
if( pGe ){ |
| 662 |
zTerm = (const char *)sqlite3_value_text(pGe); |
| 663 |
nTerm = sqlite3_value_bytes(pGe); |
| 664 |
} |
| 665 |
if( pLe ){ |
| 666 |
const char *zCopy = (const char *)sqlite3_value_text(pLe); |
| 667 |
if( zCopy==0 ) zCopy = ""; |
| 668 |
pCsr->nLeTerm = sqlite3_value_bytes(pLe); |
| 669 |
pCsr->zLeTerm = sqlite3_malloc64((i64)pCsr->nLeTerm+1); |
| 670 |
if( pCsr->zLeTerm==0 ){ |
| 671 |
rc = SQLITE_NOMEM; |
| 672 |
}else{ |
| 673 |
memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1); |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
if( rc==SQLITE_OK ){ |
| 679 |
Fts5Index *pIndex = pCsr->pFts5->pIndex; |
| 680 |
rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter); |
| 681 |
if( rc==SQLITE_OK ){ |
| 682 |
pCsr->pStruct = sqlite3Fts5StructureRef(pIndex); |
| 683 |
} |
| 684 |
} |
| 685 |
if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){ |
| 686 |
rc = fts5VocabInstanceNewTerm(pCsr); |
| 687 |
} |
| 688 |
if( rc==SQLITE_OK && !pCsr->bEof |
| 689 |
&& (eType!=FTS5_VOCAB_INSTANCE |
| 690 |
|| pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE) |
| 691 |
){ |
| 692 |
rc = fts5VocabNextMethod(pCursor); |
| 693 |
} |
| 694 |
|
| 695 |
return rc; |
| 696 |
} |
| 697 |
|
| 698 |
/* |
| 699 |
** This is the xEof method of the virtual table. SQLite calls this |
| 700 |
** routine to find out if it has reached the end of a result set. |
| 701 |
*/ |
| 702 |
static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){ |
| 703 |
Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; |
| 704 |
return pCsr->bEof; |
| 705 |
} |
| 706 |
|
| 707 |
static int fts5VocabColumnMethod( |
| 708 |
sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ |
| 709 |
sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ |
| 710 |
int iCol /* Index of column to read value from */ |
| 711 |
){ |
| 712 |
Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; |
| 713 |
int eDetail = pCsr->pFts5->pConfig->eDetail; |
| 714 |
int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType; |
| 715 |
i64 iVal = 0; |
| 716 |
|
| 717 |
if( iCol==0 ){ |
| 718 |
sqlite3_result_text( |
| 719 |
pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT |
| 720 |
); |
| 721 |
}else if( eType==FTS5_VOCAB_COL ){ |
| 722 |
assert( iCol==1 || iCol==2 || iCol==3 ); |
| 723 |
if( iCol==1 ){ |
| 724 |
if( eDetail!=FTS5_DETAIL_NONE ){ |
| 725 |
const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol]; |
| 726 |
sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC); |
| 727 |
} |
| 728 |
}else if( iCol==2 ){ |
| 729 |
iVal = pCsr->aDoc[pCsr->iCol]; |
| 730 |
}else{ |
| 731 |
iVal = pCsr->aCnt[pCsr->iCol]; |
| 732 |
} |
| 733 |
}else if( eType==FTS5_VOCAB_ROW ){ |
| 734 |
assert( iCol==1 || iCol==2 ); |
| 735 |
if( iCol==1 ){ |
| 736 |
iVal = pCsr->aDoc[0]; |
| 737 |
}else{ |
| 738 |
iVal = pCsr->aCnt[0]; |
| 739 |
} |
| 740 |
}else{ |
| 741 |
assert( eType==FTS5_VOCAB_INSTANCE ); |
| 742 |
switch( iCol ){ |
| 743 |
case 1: |
| 744 |
sqlite3_result_int64(pCtx, pCsr->pIter->iRowid); |
| 745 |
break; |
| 746 |
case 2: { |
| 747 |
int ii = -1; |
| 748 |
if( eDetail==FTS5_DETAIL_FULL ){ |
| 749 |
ii = FTS5_POS2COLUMN(pCsr->iInstPos); |
| 750 |
}else if( eDetail==FTS5_DETAIL_COLUMNS ){ |
| 751 |
ii = (int)pCsr->iInstPos; |
| 752 |
} |
| 753 |
if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){ |
| 754 |
const char *z = pCsr->pFts5->pConfig->azCol[ii]; |
| 755 |
sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC); |
| 756 |
} |
| 757 |
break; |
| 758 |
} |
| 759 |
default: { |
| 760 |
assert( iCol==3 ); |
| 761 |
if( eDetail==FTS5_DETAIL_FULL ){ |
| 762 |
int ii = FTS5_POS2OFFSET(pCsr->iInstPos); |
| 763 |
sqlite3_result_int(pCtx, ii); |
| 764 |
} |
| 765 |
break; |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
if( iVal>0 ) sqlite3_result_int64(pCtx, iVal); |
| 771 |
return SQLITE_OK; |
| 772 |
} |
| 773 |
|
| 774 |
/* |
| 775 |
** This is the xRowid method. The SQLite core calls this routine to |
| 776 |
** retrieve the rowid for the current row of the result set. The |
| 777 |
** rowid should be written to *pRowid. |
| 778 |
*/ |
| 779 |
static int fts5VocabRowidMethod( |
| 780 |
sqlite3_vtab_cursor *pCursor, |
| 781 |
sqlite_int64 *pRowid |
| 782 |
){ |
| 783 |
Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; |
| 784 |
*pRowid = pCsr->rowid; |
| 785 |
return SQLITE_OK; |
| 786 |
} |
| 787 |
|
| 788 |
int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){ |
| 789 |
static const sqlite3_module fts5Vocab = { |
| 790 |
/* iVersion */ 2, |
| 791 |
/* xCreate */ fts5VocabCreateMethod, |
| 792 |
/* xConnect */ fts5VocabConnectMethod, |
| 793 |
/* xBestIndex */ fts5VocabBestIndexMethod, |
| 794 |
/* xDisconnect */ fts5VocabDisconnectMethod, |
| 795 |
/* xDestroy */ fts5VocabDestroyMethod, |
| 796 |
/* xOpen */ fts5VocabOpenMethod, |
| 797 |
/* xClose */ fts5VocabCloseMethod, |
| 798 |
/* xFilter */ fts5VocabFilterMethod, |
| 799 |
/* xNext */ fts5VocabNextMethod, |
| 800 |
/* xEof */ fts5VocabEofMethod, |
| 801 |
/* xColumn */ fts5VocabColumnMethod, |
| 802 |
/* xRowid */ fts5VocabRowidMethod, |
| 803 |
/* xUpdate */ 0, |
| 804 |
/* xBegin */ 0, |
| 805 |
/* xSync */ 0, |
| 806 |
/* xCommit */ 0, |
| 807 |
/* xRollback */ 0, |
| 808 |
/* xFindFunction */ 0, |
| 809 |
/* xRename */ 0, |
| 810 |
/* xSavepoint */ 0, |
| 811 |
/* xRelease */ 0, |
| 812 |
/* xRollbackTo */ 0, |
| 813 |
/* xShadowName */ 0, |
| 814 |
/* xIntegrity */ 0 |
| 815 |
}; |
| 816 |
void *p = (void*)pGlobal; |
| 817 |
|
| 818 |
return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0); |
| 819 |
} |