Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/fts5/fts5_storage.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / fts5 / fts5_storage.c` 1561 lines ยท 48021 bytes Raw Blame History 1 /* 2 ** 2014 May 31 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ****************************************************************************** 12 ** 13 */ 14 15 16 17 #include "fts5Int.h" 18 19 /* 20 ** pSavedRow: 21 ** SQL statement FTS5_STMT_LOOKUP2 is a copy of FTS5_STMT_LOOKUP, it 22 ** does a by-rowid lookup to retrieve a single row from the %_content 23 ** table or equivalent external-content table/view. 24 ** 25 ** However, FTS5_STMT_LOOKUP2 is only used when retrieving the original 26 ** values for a row being UPDATEd. In that case, the SQL statement is 27 ** not reset and pSavedRow is set to point at it. This is so that the 28 ** insert operation that follows the delete may access the original 29 ** row values for any new values for which sqlite3_value_nochange() returns 30 ** true. i.e. if the user executes: 31 ** 32 ** CREATE VIRTUAL TABLE ft USING fts5(a, b, c, locale=1); 33 ** ... 34 ** UPDATE fts SET a=?, b=? WHERE rowid=?; 35 ** 36 ** then the value passed to the xUpdate() method of this table as the 37 ** new.c value is an sqlite3_value_nochange() value. So in this case it 38 ** must be read from the saved row stored in Fts5Storage.pSavedRow. 39 ** 40 ** This is necessary - using sqlite3_value_nochange() instead of just having 41 ** SQLite pass the original value back via xUpdate() - so as not to discard 42 ** any locale information associated with such values. 43 ** 44 */ 45 struct Fts5Storage { 46 Fts5Config *pConfig; 47 Fts5Index *pIndex; 48 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */ 49 i64 nTotalRow; /* Total number of rows in FTS table */ 50 i64 *aTotalSize; /* Total sizes of each column */ 51 sqlite3_stmt *pSavedRow; 52 sqlite3_stmt *aStmt[12]; 53 }; 54 55 56 #if FTS5_STMT_SCAN_ASC!=0 57 # error "FTS5_STMT_SCAN_ASC mismatch" 58 #endif 59 #if FTS5_STMT_SCAN_DESC!=1 60 # error "FTS5_STMT_SCAN_DESC mismatch" 61 #endif 62 #if FTS5_STMT_LOOKUP!=2 63 # error "FTS5_STMT_LOOKUP mismatch" 64 #endif 65 66 #define FTS5_STMT_LOOKUP2 3 67 #define FTS5_STMT_INSERT_CONTENT 4 68 #define FTS5_STMT_REPLACE_CONTENT 5 69 #define FTS5_STMT_DELETE_CONTENT 6 70 #define FTS5_STMT_REPLACE_DOCSIZE 7 71 #define FTS5_STMT_DELETE_DOCSIZE 8 72 #define FTS5_STMT_LOOKUP_DOCSIZE 9 73 #define FTS5_STMT_REPLACE_CONFIG 10 74 #define FTS5_STMT_SCAN 11 75 76 /* 77 ** Prepare the two insert statements - Fts5Storage.pInsertContent and 78 ** Fts5Storage.pInsertDocsize - if they have not already been prepared. 79 ** Return SQLITE_OK if successful, or an SQLite error code if an error 80 ** occurs. 81 */ 82 static int fts5StorageGetStmt( 83 Fts5Storage *p, /* Storage handle */ 84 int eStmt, /* FTS5_STMT_XXX constant */ 85 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */ 86 char **pzErrMsg /* OUT: Error message (if any) (not used) */ 87 ){ 88 int rc = SQLITE_OK; 89 90 (void)pzErrMsg; 91 92 /* If there is no %_docsize table, there should be no requests for 93 ** statements to operate on it. */ 94 assert( p->pConfig->bColumnsize || ( 95 eStmt!=FTS5_STMT_REPLACE_DOCSIZE 96 && eStmt!=FTS5_STMT_DELETE_DOCSIZE 97 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE 98 )); 99 100 assert( eStmt>=0 && eStmtaStmt) ); 101 if( p->aStmt[eStmt]==0 ){ 102 const char *azStmt[] = { 103 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC", 104 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC", 105 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */ 106 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP2 */ 107 108 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */ 109 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */ 110 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */ 111 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?%s)", /* REPLACE_DOCSIZE */ 112 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */ 113 114 "SELECT sz%s FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */ 115 116 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */ 117 "SELECT %s FROM %s AS T", /* SCAN */ 118 }; 119 Fts5Config *pC = p->pConfig; 120 char *zSql = 0; 121 122 assert( ArraySize(azStmt)==ArraySize(p->aStmt) ); 123 124 switch( eStmt ){ 125 case FTS5_STMT_SCAN: 126 zSql = sqlite3_mprintf(azStmt[eStmt], 127 pC->zContentExprlist, pC->zContent 128 ); 129 break; 130 131 case FTS5_STMT_SCAN_ASC: 132 case FTS5_STMT_SCAN_DESC: 133 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist, 134 pC->zContent, pC->zContentRowid, pC->zContentRowid, 135 pC->zContentRowid 136 ); 137 break; 138 139 case FTS5_STMT_LOOKUP: 140 case FTS5_STMT_LOOKUP2: 141 zSql = sqlite3_mprintf(azStmt[eStmt], 142 pC->zContentExprlist, pC->zContent, pC->zContentRowid 143 ); 144 break; 145 146 case FTS5_STMT_INSERT_CONTENT: 147 case FTS5_STMT_REPLACE_CONTENT: { 148 char *zBind = 0; 149 int i; 150 151 assert( pC->eContent==FTS5_CONTENT_NORMAL 152 || pC->eContent==FTS5_CONTENT_UNINDEXED 153 ); 154 155 /* Add bindings for the "c*" columns - those that store the actual 156 ** table content. If eContent==NORMAL, then there is one binding 157 ** for each column. Or, if eContent==UNINDEXED, then there are only 158 ** bindings for the UNINDEXED columns. */ 159 for(i=0; rc==SQLITE_OK && i<(pC->nCol+1); i++){ 160 if( !i || pC->eContent==FTS5_CONTENT_NORMAL || pC->abUnindexed[i-1] ){ 161 zBind = sqlite3Fts5Mprintf(&rc, "%z%s?%d", zBind, zBind?",":"",i+1); 162 } 163 } 164 165 /* Add bindings for any "l*" columns. Only non-UNINDEXED columns 166 ** require these. */ 167 if( pC->bLocale && pC->eContent==FTS5_CONTENT_NORMAL ){ 168 for(i=0; rc==SQLITE_OK && inCol; i++){ 169 if( pC->abUnindexed[i]==0 ){ 170 zBind = sqlite3Fts5Mprintf(&rc, "%z,?%d", zBind, pC->nCol+i+2); 171 } 172 } 173 } 174 175 zSql = sqlite3Fts5Mprintf(&rc, azStmt[eStmt], pC->zDb, pC->zName,zBind); 176 sqlite3_free(zBind); 177 break; 178 } 179 180 case FTS5_STMT_REPLACE_DOCSIZE: 181 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, 182 (pC->bContentlessDelete ? ",?" : "") 183 ); 184 break; 185 186 case FTS5_STMT_LOOKUP_DOCSIZE: 187 zSql = sqlite3_mprintf(azStmt[eStmt], 188 (pC->bContentlessDelete ? ",origin" : ""), 189 pC->zDb, pC->zName 190 ); 191 break; 192 193 default: 194 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName); 195 break; 196 } 197 198 if( zSql==0 ){ 199 rc = SQLITE_NOMEM; 200 }else{ 201 int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_FROM_DDL; 202 if( eStmt>FTS5_STMT_LOOKUP2 ) f |= SQLITE_PREPARE_NO_VTAB; 203 p->pConfig->bLock++; 204 rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0); 205 p->pConfig->bLock--; 206 sqlite3_free(zSql); 207 if( rc==SQLITE_ERROR && eStmt>FTS5_STMT_LOOKUP2 && eStmtpConfig->pzErrmsg ){ 212 assert( *p->pConfig->pzErrmsg==0 ); 213 *p->pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db)); 214 } 215 } 216 } 217 218 *ppStmt = p->aStmt[eStmt]; 219 sqlite3_reset(*ppStmt); 220 return rc; 221 } 222 223 224 static int fts5ExecPrintf( 225 sqlite3 *db, 226 char **pzErr, 227 const char *zFormat, 228 ... 229 ){ 230 int rc; 231 va_list ap; /* ... printf arguments */ 232 char *zSql; 233 234 va_start(ap, zFormat); 235 zSql = sqlite3_vmprintf(zFormat, ap); 236 237 if( zSql==0 ){ 238 rc = SQLITE_NOMEM; 239 }else{ 240 rc = sqlite3_exec(db, zSql, 0, 0, pzErr); 241 sqlite3_free(zSql); 242 } 243 244 va_end(ap); 245 return rc; 246 } 247 248 /* 249 ** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error 250 ** code otherwise. 251 */ 252 int sqlite3Fts5DropAll(Fts5Config *pConfig){ 253 int rc = fts5ExecPrintf(pConfig->db, 0, 254 "DROP TABLE IF EXISTS %Q.'%q_data';" 255 "DROP TABLE IF EXISTS %Q.'%q_idx';" 256 "DROP TABLE IF EXISTS %Q.'%q_config';", 257 pConfig->zDb, pConfig->zName, 258 pConfig->zDb, pConfig->zName, 259 pConfig->zDb, pConfig->zName 260 ); 261 if( rc==SQLITE_OK && pConfig->bColumnsize ){ 262 rc = fts5ExecPrintf(pConfig->db, 0, 263 "DROP TABLE IF EXISTS %Q.'%q_docsize';", 264 pConfig->zDb, pConfig->zName 265 ); 266 } 267 if( rc==SQLITE_OK ){ 268 if( pConfig->eContent==FTS5_CONTENT_NORMAL 269 || pConfig->bContentlessUnindexed 270 ){ 271 rc = fts5ExecPrintf(pConfig->db, 0, 272 "DROP TABLE IF EXISTS %Q.'%q_content';", 273 pConfig->zDb, pConfig->zName 274 ); 275 } 276 } 277 return rc; 278 } 279 280 static void fts5StorageRenameOne( 281 Fts5Config *pConfig, /* Current FTS5 configuration */ 282 int *pRc, /* IN/OUT: Error code */ 283 const char *zTail, /* Tail of table name e.g. "data", "config" */ 284 const char *zName /* New name of FTS5 table */ 285 ){ 286 if( *pRc==SQLITE_OK ){ 287 *pRc = fts5ExecPrintf(pConfig->db, 0, 288 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';", 289 pConfig->zDb, pConfig->zName, zTail, zName, zTail 290 ); 291 } 292 } 293 294 int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){ 295 Fts5Config *pConfig = pStorage->pConfig; 296 int rc = sqlite3Fts5StorageSync(pStorage); 297 298 fts5StorageRenameOne(pConfig, &rc, "data", zName); 299 fts5StorageRenameOne(pConfig, &rc, "idx", zName); 300 fts5StorageRenameOne(pConfig, &rc, "config", zName); 301 if( pConfig->bColumnsize ){ 302 fts5StorageRenameOne(pConfig, &rc, "docsize", zName); 303 } 304 if( pConfig->eContent==FTS5_CONTENT_NORMAL 305 || pConfig->bContentlessUnindexed 306 ){ 307 fts5StorageRenameOne(pConfig, &rc, "content", zName); 308 } 309 return rc; 310 } 311 312 /* 313 ** Create the shadow table named zPost, with definition zDefn. Return 314 ** SQLITE_OK if successful, or an SQLite error code otherwise. 315 */ 316 int sqlite3Fts5CreateTable( 317 Fts5Config *pConfig, /* FTS5 configuration */ 318 const char *zPost, /* Shadow table to create (e.g. "content") */ 319 const char *zDefn, /* Columns etc. for shadow table */ 320 int bWithout, /* True for without rowid */ 321 char **pzErr /* OUT: Error message */ 322 ){ 323 int rc; 324 char *zErr = 0; 325 326 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s", 327 pConfig->zDb, pConfig->zName, zPost, zDefn, 328 #ifndef SQLITE_FTS5_NO_WITHOUT_ROWID 329 bWithout?" WITHOUT ROWID": 330 #endif 331 "" 332 ); 333 if( zErr ){ 334 *pzErr = sqlite3_mprintf( 335 "fts5: error creating shadow table %q_%s: %s", 336 pConfig->zName, zPost, zErr 337 ); 338 sqlite3_free(zErr); 339 } 340 341 return rc; 342 } 343 344 /* 345 ** Open a new Fts5Index handle. If the bCreate argument is true, create 346 ** and initialize the underlying tables 347 ** 348 ** If successful, set *pp to point to the new object and return SQLITE_OK. 349 ** Otherwise, set *pp to NULL and return an SQLite error code. 350 */ 351 int sqlite3Fts5StorageOpen( 352 Fts5Config *pConfig, 353 Fts5Index *pIndex, 354 int bCreate, 355 Fts5Storage **pp, 356 char **pzErr /* OUT: Error message */ 357 ){ 358 int rc = SQLITE_OK; 359 Fts5Storage *p; /* New object */ 360 sqlite3_int64 nByte; /* Bytes of space to allocate */ 361 362 nByte = sizeof(Fts5Storage) /* Fts5Storage object */ 363 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */ 364 *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte); 365 if( !p ) return SQLITE_NOMEM; 366 367 memset(p, 0, (size_t)nByte); 368 p->aTotalSize = (i64*)&p[1]; 369 p->pConfig = pConfig; 370 p->pIndex = pIndex; 371 372 if( bCreate ){ 373 if( pConfig->eContent==FTS5_CONTENT_NORMAL 374 || pConfig->eContent==FTS5_CONTENT_UNINDEXED 375 ){ 376 int i = 0; 377 char *zDefn = 0; 378 sqlite3_str *pDefn = sqlite3_str_new(pConfig->db); 379 380 sqlite3_str_appendf(pDefn, "id INTEGER PRIMARY KEY"); 381 for(i=0; inCol; i++){ 382 if( pConfig->eContent==FTS5_CONTENT_NORMAL || pConfig->abUnindexed[i] ){ 383 sqlite3_str_appendf(pDefn, ", c%d", i); 384 } 385 } 386 if( pConfig->bLocale && pConfig->eContent==FTS5_CONTENT_NORMAL ){ 387 for(i=0; inCol; i++){ 388 if( pConfig->abUnindexed[i]==0 ){ 389 sqlite3_str_appendf(pDefn, ", l%d", i); 390 } 391 } 392 } 393 zDefn = sqlite3_str_finish(pDefn); 394 395 if( zDefn ){ 396 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr); 397 sqlite3_free(zDefn); 398 }else{ 399 rc = SQLITE_NOMEM; 400 } 401 } 402 403 if( rc==SQLITE_OK && pConfig->bColumnsize ){ 404 const char *zCols = "id INTEGER PRIMARY KEY, sz BLOB"; 405 if( pConfig->bContentlessDelete ){ 406 zCols = "id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER"; 407 } 408 rc = sqlite3Fts5CreateTable(pConfig, "docsize", zCols, 0, pzErr); 409 } 410 if( rc==SQLITE_OK ){ 411 rc = sqlite3Fts5CreateTable( 412 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr 413 ); 414 } 415 if( rc==SQLITE_OK ){ 416 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION); 417 } 418 } 419 420 if( rc ){ 421 sqlite3Fts5StorageClose(p); 422 *pp = 0; 423 } 424 return rc; 425 } 426 427 /* 428 ** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen(). 429 */ 430 int sqlite3Fts5StorageClose(Fts5Storage *p){ 431 int rc = SQLITE_OK; 432 if( p ){ 433 int i; 434 435 /* Finalize all SQL statements */ 436 for(i=0; iaStmt); i++){ 437 sqlite3_finalize(p->aStmt[i]); 438 } 439 440 sqlite3_free(p); 441 } 442 return rc; 443 } 444 445 typedef struct Fts5InsertCtx Fts5InsertCtx; 446 struct Fts5InsertCtx { 447 Fts5Storage *pStorage; 448 int iCol; 449 int szCol; /* Size of column value in tokens */ 450 }; 451 452 /* 453 ** Tokenization callback used when inserting tokens into the FTS index. 454 */ 455 static int fts5StorageInsertCallback( 456 void *pContext, /* Pointer to Fts5InsertCtx object */ 457 int tflags, 458 const char *pToken, /* Buffer containing token */ 459 int nToken, /* Size of token in bytes */ 460 int iUnused1, /* Start offset of token */ 461 int iUnused2 /* End offset of token */ 462 ){ 463 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext; 464 Fts5Index *pIdx = pCtx->pStorage->pIndex; 465 UNUSED_PARAM2(iUnused1, iUnused2); 466 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE; 467 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){ 468 pCtx->szCol++; 469 } 470 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken); 471 } 472 473 /* 474 ** This function is used as part of an UPDATE statement that modifies the 475 ** rowid of a row. In that case, this function is called first to set 476 ** Fts5Storage.pSavedRow to point to a statement that may be used to 477 ** access the original values of the row being deleted - iDel. 478 ** 479 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. 480 ** It is not considered an error if row iDel does not exist. In this case 481 ** pSavedRow is not set and SQLITE_OK returned. 482 */ 483 int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel){ 484 int rc = SQLITE_OK; 485 sqlite3_stmt *pSeek = 0; 486 487 assert( p->pSavedRow==0 ); 488 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP+1, &pSeek, 0); 489 if( rc==SQLITE_OK ){ 490 sqlite3_bind_int64(pSeek, 1, iDel); 491 if( sqlite3_step(pSeek)!=SQLITE_ROW ){ 492 rc = sqlite3_reset(pSeek); 493 }else{ 494 p->pSavedRow = pSeek; 495 } 496 } 497 498 return rc; 499 } 500 501 /* 502 ** If a row with rowid iDel is present in the %_content table, add the 503 ** delete-markers to the FTS index necessary to delete it. Do not actually 504 ** remove the %_content row at this time though. 505 ** 506 ** If parameter bSaveRow is true, then Fts5Storage.pSavedRow is left 507 ** pointing to a statement (FTS5_STMT_LOOKUP2) that may be used to access 508 ** the original values of the row being deleted. This is used by UPDATE 509 ** statements. 510 */ 511 static int fts5StorageDeleteFromIndex( 512 Fts5Storage *p, 513 i64 iDel, 514 sqlite3_value **apVal, 515 int bSaveRow /* True to set pSavedRow */ 516 ){ 517 Fts5Config *pConfig = p->pConfig; 518 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */ 519 int rc = SQLITE_OK; /* Return code */ 520 int rc2; /* sqlite3_reset() return code */ 521 int iCol; 522 Fts5InsertCtx ctx; 523 524 assert( bSaveRow==0 || apVal==0 ); 525 assert( bSaveRow==0 || bSaveRow==1 ); 526 assert( FTS5_STMT_LOOKUP2==FTS5_STMT_LOOKUP+1 ); 527 528 if( apVal==0 ){ 529 if( p->pSavedRow && bSaveRow ){ 530 pSeek = p->pSavedRow; 531 p->pSavedRow = 0; 532 }else{ 533 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP+bSaveRow, &pSeek, 0); 534 if( rc!=SQLITE_OK ) return rc; 535 sqlite3_bind_int64(pSeek, 1, iDel); 536 if( sqlite3_step(pSeek)!=SQLITE_ROW ){ 537 return sqlite3_reset(pSeek); 538 } 539 } 540 } 541 542 ctx.pStorage = p; 543 ctx.iCol = -1; 544 for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){ 545 if( pConfig->abUnindexed[iCol-1]==0 ){ 546 sqlite3_value *pVal = 0; 547 sqlite3_value *pFree = 0; 548 const char *pText = 0; 549 int nText = 0; 550 const char *pLoc = 0; 551 int nLoc = 0; 552 553 assert( pSeek==0 || apVal==0 ); 554 assert( pSeek!=0 || apVal!=0 ); 555 if( pSeek ){ 556 pVal = sqlite3_column_value(pSeek, iCol); 557 }else{ 558 pVal = apVal[iCol-1]; 559 } 560 561 if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ 562 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); 563 }else{ 564 if( sqlite3_value_type(pVal)!=SQLITE_TEXT ){ 565 /* Make a copy of the value to work with. This is because the call 566 ** to sqlite3_value_text() below forces the type of the value to 567 ** SQLITE_TEXT, and we may need to use it again later. */ 568 pFree = pVal = sqlite3_value_dup(pVal); 569 if( pVal==0 ){ 570 rc = SQLITE_NOMEM; 571 } 572 } 573 if( rc==SQLITE_OK ){ 574 pText = (const char*)sqlite3_value_text(pVal); 575 nText = sqlite3_value_bytes(pVal); 576 if( pConfig->bLocale && pSeek ){ 577 pLoc = (const char*)sqlite3_column_text(pSeek, iCol+pConfig->nCol); 578 nLoc = sqlite3_column_bytes(pSeek, iCol + pConfig->nCol); 579 } 580 } 581 } 582 583 if( rc==SQLITE_OK ){ 584 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); 585 ctx.szCol = 0; 586 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT, 587 pText, nText, (void*)&ctx, fts5StorageInsertCallback 588 ); 589 p->aTotalSize[iCol-1] -= (i64)ctx.szCol; 590 if( rc==SQLITE_OK && p->aTotalSize[iCol-1]<0 ){ 591 rc = FTS5_CORRUPT; 592 } 593 sqlite3Fts5ClearLocale(pConfig); 594 } 595 sqlite3_value_free(pFree); 596 } 597 } 598 if( rc==SQLITE_OK && p->nTotalRow<1 ){ 599 rc = FTS5_CORRUPT; 600 }else{ 601 p->nTotalRow--; 602 } 603 604 if( rc==SQLITE_OK && bSaveRow ){ 605 assert( p->pSavedRow==0 ); 606 p->pSavedRow = pSeek; 607 }else{ 608 rc2 = sqlite3_reset(pSeek); 609 if( rc==SQLITE_OK ) rc = rc2; 610 } 611 return rc; 612 } 613 614 /* 615 ** Reset any saved statement pSavedRow. Zero pSavedRow as well. This 616 ** should be called by the xUpdate() method of the fts5 table before 617 ** returning from any operation that may have set Fts5Storage.pSavedRow. 618 */ 619 void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage *pStorage){ 620 assert( pStorage->pSavedRow==0 621 || pStorage->pSavedRow==pStorage->aStmt[FTS5_STMT_LOOKUP2] 622 ); 623 sqlite3_reset(pStorage->pSavedRow); 624 pStorage->pSavedRow = 0; 625 } 626 627 /* 628 ** Load the contents of the "averages" record from disk into the 629 ** p->nTotalRow and p->aTotalSize[] variables. If successful, and if 630 ** argument bCache is true, set the p->bTotalsValid flag to indicate 631 ** that the contents of aTotalSize[] and nTotalRow are valid until 632 ** further notice. 633 ** 634 ** Return SQLITE_OK if successful, or an SQLite error code if an error 635 ** occurs. 636 */ 637 static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){ 638 int rc = SQLITE_OK; 639 if( p->bTotalsValid==0 ){ 640 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize); 641 p->bTotalsValid = bCache; 642 } 643 return rc; 644 } 645 646 /* 647 ** Store the current contents of the p->nTotalRow and p->aTotalSize[] 648 ** variables in the "averages" record on disk. 649 ** 650 ** Return SQLITE_OK if successful, or an SQLite error code if an error 651 ** occurs. 652 */ 653 static int fts5StorageSaveTotals(Fts5Storage *p){ 654 int nCol = p->pConfig->nCol; 655 int i; 656 Fts5Buffer buf; 657 int rc = SQLITE_OK; 658 memset(&buf, 0, sizeof(buf)); 659 660 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow); 661 for(i=0; iaTotalSize[i]); 663 } 664 if( rc==SQLITE_OK ){ 665 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n); 666 } 667 sqlite3_free(buf.p); 668 669 return rc; 670 } 671 672 static int fts5StorageDecodeSizeArray( 673 int *aCol, int nCol, /* Array to populate */ 674 const u8 *aBlob, int nBlob /* Record to read varints from */ 675 ){ 676 int i; 677 int iOff = 0; 678 for(i=0; i=nBlob ) return 1; 680 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]); 681 } 682 return (iOff!=nBlob); 683 } 684 685 /* 686 ** This function is called to process a DELETE on a contentless_delete=1 687 ** table. It adds the tombstone required to delete the entry with rowid 688 ** iDel. If successful, SQLITE_OK is returned. Or, if an error occurs, 689 ** an SQLite error code. 690 */ 691 static int fts5StorageContentlessDelete(Fts5Storage *p, i64 iDel){ 692 i64 iOrigin = 0; 693 sqlite3_stmt *pLookup = 0; 694 int rc = SQLITE_OK; 695 696 assert( p->pConfig->bContentlessDelete ); 697 assert( p->pConfig->eContent==FTS5_CONTENT_NONE 698 || p->pConfig->eContent==FTS5_CONTENT_UNINDEXED 699 ); 700 701 rc = fts5StorageLoadTotals(p, 1); 702 if( rc!=SQLITE_OK ) return rc; 703 704 /* Look up the origin of the document in the %_docsize table. Store 705 ** this in stack variable iOrigin. */ 706 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0); 707 if( rc==SQLITE_OK ){ 708 int rc2; 709 sqlite3_bind_int64(pLookup, 1, iDel); 710 if( SQLITE_ROW==sqlite3_step(pLookup) ){ 711 int *aCol = (int*)sqlite3_malloc64(p->pConfig->nCol * sizeof(int)); 712 if( aCol==0 ){ 713 rc = SQLITE_NOMEM; 714 }else{ 715 const u8 *aBlob = sqlite3_column_blob(pLookup, 0); 716 int nBlob = sqlite3_column_bytes(pLookup, 0); 717 int ii; 718 if( fts5StorageDecodeSizeArray(aCol, p->pConfig->nCol, aBlob, nBlob) ){ 719 rc = SQLITE_CORRUPT_VTAB; 720 }else{ 721 for(ii=0; iipConfig->nCol; ii++){ 722 p->aTotalSize[ii] -= aCol[ii]; 723 } 724 } 725 sqlite3_free(aCol); 726 } 727 iOrigin = sqlite3_column_int64(pLookup, 1); 728 } 729 rc2 = sqlite3_reset(pLookup); 730 if( rc==SQLITE_OK ) rc = rc2; 731 } 732 733 if( rc==SQLITE_OK && iOrigin!=0 ){ 734 rc = sqlite3Fts5IndexContentlessDelete(p->pIndex, iOrigin, iDel); 735 p->nTotalRow--; 736 } 737 738 return rc; 739 } 740 741 /* 742 ** Insert a record into the %_docsize table. Specifically, do: 743 ** 744 ** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf); 745 ** 746 ** If there is no %_docsize table (as happens if the columnsize=0 option 747 ** is specified when the FTS5 table is created), this function is a no-op. 748 */ 749 static int fts5StorageInsertDocsize( 750 Fts5Storage *p, /* Storage module to write to */ 751 i64 iRowid, /* id value */ 752 Fts5Buffer *pBuf /* sz value */ 753 ){ 754 int rc = SQLITE_OK; 755 if( p->pConfig->bColumnsize ){ 756 sqlite3_stmt *pReplace = 0; 757 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0); 758 if( rc==SQLITE_OK ){ 759 sqlite3_bind_int64(pReplace, 1, iRowid); 760 if( p->pConfig->bContentlessDelete ){ 761 i64 iOrigin = 0; 762 rc = sqlite3Fts5IndexGetOrigin(p->pIndex, &iOrigin); 763 sqlite3_bind_int64(pReplace, 3, iOrigin); 764 } 765 } 766 if( rc==SQLITE_OK ){ 767 rc = sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC); 768 if( rc==SQLITE_OK ){ 769 sqlite3_step(pReplace); 770 rc = sqlite3_reset(pReplace); 771 sqlite3_bind_null(pReplace, 2); 772 } 773 } 774 } 775 return rc; 776 } 777 778 /* 779 ** Remove a row from the FTS table. 780 */ 781 int sqlite3Fts5StorageDelete( 782 Fts5Storage *p, /* Storage object */ 783 i64 iDel, /* Rowid to delete from table */ 784 sqlite3_value **apVal, /* Optional - values to remove from index */ 785 int bSaveRow /* If true, set pSavedRow for deleted row */ 786 ){ 787 Fts5Config *pConfig = p->pConfig; 788 int rc; 789 sqlite3_stmt *pDel = 0; 790 791 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 ); 792 rc = fts5StorageLoadTotals(p, 1); 793 794 /* Delete the index records */ 795 if( rc==SQLITE_OK ){ 796 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel); 797 } 798 799 if( rc==SQLITE_OK ){ 800 if( p->pConfig->bContentlessDelete ){ 801 rc = fts5StorageContentlessDelete(p, iDel); 802 if( rc==SQLITE_OK 803 && bSaveRow 804 && p->pConfig->eContent==FTS5_CONTENT_UNINDEXED 805 ){ 806 rc = sqlite3Fts5StorageFindDeleteRow(p, iDel); 807 } 808 }else{ 809 rc = fts5StorageDeleteFromIndex(p, iDel, apVal, bSaveRow); 810 } 811 } 812 813 /* Delete the %_docsize record */ 814 if( rc==SQLITE_OK && pConfig->bColumnsize ){ 815 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0); 816 if( rc==SQLITE_OK ){ 817 sqlite3_bind_int64(pDel, 1, iDel); 818 sqlite3_step(pDel); 819 rc = sqlite3_reset(pDel); 820 } 821 } 822 823 /* Delete the %_content record */ 824 if( pConfig->eContent==FTS5_CONTENT_NORMAL 825 || pConfig->eContent==FTS5_CONTENT_UNINDEXED 826 ){ 827 if( rc==SQLITE_OK ){ 828 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0); 829 } 830 if( rc==SQLITE_OK ){ 831 sqlite3_bind_int64(pDel, 1, iDel); 832 sqlite3_step(pDel); 833 rc = sqlite3_reset(pDel); 834 } 835 } 836 837 return rc; 838 } 839 840 /* 841 ** Delete all entries in the FTS5 index. 842 */ 843 int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){ 844 Fts5Config *pConfig = p->pConfig; 845 int rc; 846 847 p->bTotalsValid = 0; 848 849 /* Delete the contents of the %_data and %_docsize tables. */ 850 rc = fts5ExecPrintf(pConfig->db, 0, 851 "DELETE FROM %Q.'%q_data';" 852 "DELETE FROM %Q.'%q_idx';", 853 pConfig->zDb, pConfig->zName, 854 pConfig->zDb, pConfig->zName 855 ); 856 if( rc==SQLITE_OK && pConfig->bColumnsize ){ 857 rc = fts5ExecPrintf(pConfig->db, 0, 858 "DELETE FROM %Q.'%q_docsize';", pConfig->zDb, pConfig->zName 859 ); 860 } 861 862 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_UNINDEXED ){ 863 rc = fts5ExecPrintf(pConfig->db, 0, 864 "DELETE FROM %Q.'%q_content';", pConfig->zDb, pConfig->zName 865 ); 866 } 867 868 /* Reinitialize the %_data table. This call creates the initial structure 869 ** and averages records. */ 870 if( rc==SQLITE_OK ){ 871 rc = sqlite3Fts5IndexReinit(p->pIndex); 872 } 873 if( rc==SQLITE_OK ){ 874 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION); 875 } 876 return rc; 877 } 878 879 int sqlite3Fts5StorageRebuild(Fts5Storage *p){ 880 Fts5Buffer buf = {0,0,0}; 881 Fts5Config *pConfig = p->pConfig; 882 sqlite3_stmt *pScan = 0; 883 Fts5InsertCtx ctx; 884 int rc, rc2; 885 886 memset(&ctx, 0, sizeof(Fts5InsertCtx)); 887 ctx.pStorage = p; 888 rc = sqlite3Fts5StorageDeleteAll(p); 889 if( rc==SQLITE_OK ){ 890 rc = fts5StorageLoadTotals(p, 1); 891 } 892 893 if( rc==SQLITE_OK ){ 894 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, pConfig->pzErrmsg); 895 } 896 897 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){ 898 i64 iRowid = sqlite3_column_int64(pScan, 0); 899 900 sqlite3Fts5BufferZero(&buf); 901 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid); 902 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iColnCol; ctx.iCol++){ 903 ctx.szCol = 0; 904 if( pConfig->abUnindexed[ctx.iCol]==0 ){ 905 int nText = 0; /* Size of pText in bytes */ 906 const char *pText = 0; /* Pointer to buffer containing text value */ 907 int nLoc = 0; /* Size of pLoc in bytes */ 908 const char *pLoc = 0; /* Pointer to buffer containing text value */ 909 910 sqlite3_value *pVal = sqlite3_column_value(pScan, ctx.iCol+1); 911 if( pConfig->eContent==FTS5_CONTENT_EXTERNAL 912 && sqlite3Fts5IsLocaleValue(pConfig, pVal) 913 ){ 914 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); 915 }else{ 916 pText = (const char*)sqlite3_value_text(pVal); 917 nText = sqlite3_value_bytes(pVal); 918 if( pConfig->bLocale ){ 919 int iCol = ctx.iCol + 1 + pConfig->nCol; 920 pLoc = (const char*)sqlite3_column_text(pScan, iCol); 921 nLoc = sqlite3_column_bytes(pScan, iCol); 922 } 923 } 924 925 if( rc==SQLITE_OK ){ 926 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); 927 rc = sqlite3Fts5Tokenize(pConfig, 928 FTS5_TOKENIZE_DOCUMENT, 929 pText, nText, 930 (void*)&ctx, 931 fts5StorageInsertCallback 932 ); 933 sqlite3Fts5ClearLocale(pConfig); 934 } 935 } 936 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol); 937 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol; 938 } 939 p->nTotalRow++; 940 941 if( rc==SQLITE_OK ){ 942 rc = fts5StorageInsertDocsize(p, iRowid, &buf); 943 } 944 } 945 sqlite3_free(buf.p); 946 rc2 = sqlite3_reset(pScan); 947 if( rc==SQLITE_OK ) rc = rc2; 948 949 /* Write the averages record */ 950 if( rc==SQLITE_OK ){ 951 rc = fts5StorageSaveTotals(p); 952 } 953 return rc; 954 } 955 956 int sqlite3Fts5StorageOptimize(Fts5Storage *p){ 957 return sqlite3Fts5IndexOptimize(p->pIndex); 958 } 959 960 int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){ 961 return sqlite3Fts5IndexMerge(p->pIndex, nMerge); 962 } 963 964 int sqlite3Fts5StorageReset(Fts5Storage *p){ 965 return sqlite3Fts5IndexReset(p->pIndex); 966 } 967 968 /* 969 ** Allocate a new rowid. This is used for "external content" tables when 970 ** a NULL value is inserted into the rowid column. The new rowid is allocated 971 ** by inserting a dummy row into the %_docsize table. The dummy will be 972 ** overwritten later. 973 ** 974 ** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In 975 ** this case the user is required to provide a rowid explicitly. 976 */ 977 static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){ 978 int rc = SQLITE_MISMATCH; 979 if( p->pConfig->bColumnsize ){ 980 sqlite3_stmt *pReplace = 0; 981 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0); 982 if( rc==SQLITE_OK ){ 983 sqlite3_bind_null(pReplace, 1); 984 sqlite3_bind_null(pReplace, 2); 985 sqlite3_step(pReplace); 986 rc = sqlite3_reset(pReplace); 987 } 988 if( rc==SQLITE_OK ){ 989 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db); 990 } 991 } 992 return rc; 993 } 994 995 /* 996 ** Insert a new row into the FTS content table. 997 */ 998 int sqlite3Fts5StorageContentInsert( 999 Fts5Storage *p, 1000 int bReplace, /* True to use REPLACE instead of INSERT */ 1001 sqlite3_value **apVal, 1002 i64 *piRowid 1003 ){ 1004 Fts5Config *pConfig = p->pConfig; 1005 int rc = SQLITE_OK; 1006 1007 /* Insert the new row into the %_content table. */ 1008 if( pConfig->eContent!=FTS5_CONTENT_NORMAL 1009 && pConfig->eContent!=FTS5_CONTENT_UNINDEXED 1010 ){ 1011 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){ 1012 *piRowid = sqlite3_value_int64(apVal[1]); 1013 }else{ 1014 rc = fts5StorageNewRowid(p, piRowid); 1015 } 1016 }else{ 1017 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */ 1018 int i; /* Counter variable */ 1019 1020 assert( FTS5_STMT_INSERT_CONTENT+1==FTS5_STMT_REPLACE_CONTENT ); 1021 assert( bReplace==0 || bReplace==1 ); 1022 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT+bReplace, &pInsert, 0); 1023 if( pInsert ) sqlite3_clear_bindings(pInsert); 1024 1025 /* Bind the rowid value */ 1026 sqlite3_bind_value(pInsert, 1, apVal[1]); 1027 1028 /* Loop through values for user-defined columns. i=2 is the leftmost 1029 ** user-defined column. As is column 1 of pSavedRow. */ 1030 for(i=2; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){ 1031 int bUnindexed = pConfig->abUnindexed[i-2]; 1032 if( pConfig->eContent==FTS5_CONTENT_NORMAL || bUnindexed ){ 1033 sqlite3_value *pVal = apVal[i]; 1034 1035 if( sqlite3_value_nochange(pVal) && p->pSavedRow ){ 1036 /* This is an UPDATE statement, and user-defined column (i-2) was not 1037 ** modified. Retrieve the value from Fts5Storage.pSavedRow. */ 1038 pVal = sqlite3_column_value(p->pSavedRow, i-1); 1039 if( pConfig->bLocale && bUnindexed==0 ){ 1040 sqlite3_bind_value(pInsert, pConfig->nCol + i, 1041 sqlite3_column_value(p->pSavedRow, pConfig->nCol + i - 1) 1042 ); 1043 } 1044 }else if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ 1045 const char *pText = 0; 1046 const char *pLoc = 0; 1047 int nText = 0; 1048 int nLoc = 0; 1049 assert( pConfig->bLocale ); 1050 1051 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); 1052 if( rc==SQLITE_OK ){ 1053 sqlite3_bind_text(pInsert, i, pText, nText, SQLITE_TRANSIENT); 1054 if( bUnindexed==0 ){ 1055 int iLoc = pConfig->nCol + i; 1056 sqlite3_bind_text(pInsert, iLoc, pLoc, nLoc, SQLITE_TRANSIENT); 1057 } 1058 } 1059 1060 continue; 1061 } 1062 1063 rc = sqlite3_bind_value(pInsert, i, pVal); 1064 } 1065 } 1066 if( rc==SQLITE_OK ){ 1067 sqlite3_step(pInsert); 1068 rc = sqlite3_reset(pInsert); 1069 } 1070 *piRowid = sqlite3_last_insert_rowid(pConfig->db); 1071 } 1072 1073 return rc; 1074 } 1075 1076 /* 1077 ** Insert new entries into the FTS index and %_docsize table. 1078 */ 1079 int sqlite3Fts5StorageIndexInsert( 1080 Fts5Storage *p, 1081 sqlite3_value **apVal, 1082 i64 iRowid 1083 ){ 1084 Fts5Config *pConfig = p->pConfig; 1085 int rc = SQLITE_OK; /* Return code */ 1086 Fts5InsertCtx ctx; /* Tokenization callback context object */ 1087 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */ 1088 1089 memset(&buf, 0, sizeof(Fts5Buffer)); 1090 ctx.pStorage = p; 1091 rc = fts5StorageLoadTotals(p, 1); 1092 1093 if( rc==SQLITE_OK ){ 1094 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid); 1095 } 1096 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iColnCol; ctx.iCol++){ 1097 ctx.szCol = 0; 1098 if( pConfig->abUnindexed[ctx.iCol]==0 ){ 1099 int nText = 0; /* Size of pText in bytes */ 1100 const char *pText = 0; /* Pointer to buffer containing text value */ 1101 int nLoc = 0; /* Size of pText in bytes */ 1102 const char *pLoc = 0; /* Pointer to buffer containing text value */ 1103 1104 sqlite3_value *pVal = apVal[ctx.iCol+2]; 1105 if( p->pSavedRow && sqlite3_value_nochange(pVal) ){ 1106 pVal = sqlite3_column_value(p->pSavedRow, ctx.iCol+1); 1107 if( pConfig->eContent==FTS5_CONTENT_NORMAL && pConfig->bLocale ){ 1108 int iCol = ctx.iCol + 1 + pConfig->nCol; 1109 pLoc = (const char*)sqlite3_column_text(p->pSavedRow, iCol); 1110 nLoc = sqlite3_column_bytes(p->pSavedRow, iCol); 1111 } 1112 }else{ 1113 pVal = apVal[ctx.iCol+2]; 1114 } 1115 1116 if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ 1117 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); 1118 }else{ 1119 pText = (const char*)sqlite3_value_text(pVal); 1120 nText = sqlite3_value_bytes(pVal); 1121 } 1122 1123 if( rc==SQLITE_OK ){ 1124 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); 1125 rc = sqlite3Fts5Tokenize(pConfig, 1126 FTS5_TOKENIZE_DOCUMENT, pText, nText, (void*)&ctx, 1127 fts5StorageInsertCallback 1128 ); 1129 sqlite3Fts5ClearLocale(pConfig); 1130 } 1131 } 1132 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol); 1133 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol; 1134 } 1135 p->nTotalRow++; 1136 1137 /* Write the %_docsize record */ 1138 if( rc==SQLITE_OK ){ 1139 rc = fts5StorageInsertDocsize(p, iRowid, &buf); 1140 } 1141 sqlite3_free(buf.p); 1142 1143 return rc; 1144 } 1145 1146 static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){ 1147 Fts5Config *pConfig = p->pConfig; 1148 char *zSql; 1149 int rc; 1150 1151 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'", 1152 pConfig->zDb, pConfig->zName, zSuffix 1153 ); 1154 if( zSql==0 ){ 1155 rc = SQLITE_NOMEM; 1156 }else{ 1157 sqlite3_stmt *pCnt = 0; 1158 rc = sqlite3_prepare_v3( 1159 pConfig->db, zSql, -1, SQLITE_PREPARE_FROM_DDL, &pCnt, 0 1160 ); 1161 if( rc==SQLITE_OK ){ 1162 if( SQLITE_ROW==sqlite3_step(pCnt) ){ 1163 *pnRow = sqlite3_column_int64(pCnt, 0); 1164 } 1165 rc = sqlite3_finalize(pCnt); 1166 } 1167 } 1168 1169 sqlite3_free(zSql); 1170 return rc; 1171 } 1172 1173 /* 1174 ** Context object used by sqlite3Fts5StorageIntegrity(). 1175 */ 1176 typedef struct Fts5IntegrityCtx Fts5IntegrityCtx; 1177 struct Fts5IntegrityCtx { 1178 i64 iRowid; 1179 int iCol; 1180 int szCol; 1181 u64 cksum; 1182 Fts5Termset *pTermset; 1183 Fts5Config *pConfig; 1184 }; 1185 1186 1187 /* 1188 ** Tokenization callback used by integrity check. 1189 */ 1190 static int fts5StorageIntegrityCallback( 1191 void *pContext, /* Pointer to Fts5IntegrityCtx object */ 1192 int tflags, 1193 const char *pToken, /* Buffer containing token */ 1194 int nToken, /* Size of token in bytes */ 1195 int iUnused1, /* Start offset of token */ 1196 int iUnused2 /* End offset of token */ 1197 ){ 1198 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext; 1199 Fts5Termset *pTermset = pCtx->pTermset; 1200 int bPresent; 1201 int ii; 1202 int rc = SQLITE_OK; 1203 int iPos; 1204 int iCol; 1205 1206 UNUSED_PARAM2(iUnused1, iUnused2); 1207 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE; 1208 1209 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){ 1210 pCtx->szCol++; 1211 } 1212 1213 switch( pCtx->pConfig->eDetail ){ 1214 case FTS5_DETAIL_FULL: 1215 iPos = pCtx->szCol-1; 1216 iCol = pCtx->iCol; 1217 break; 1218 1219 case FTS5_DETAIL_COLUMNS: 1220 iPos = pCtx->iCol; 1221 iCol = 0; 1222 break; 1223 1224 default: 1225 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE ); 1226 iPos = 0; 1227 iCol = 0; 1228 break; 1229 } 1230 1231 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent); 1232 if( rc==SQLITE_OK && bPresent==0 ){ 1233 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum( 1234 pCtx->iRowid, iCol, iPos, 0, pToken, nToken 1235 ); 1236 } 1237 1238 for(ii=0; rc==SQLITE_OK && iipConfig->nPrefix; ii++){ 1239 const int nChar = pCtx->pConfig->aPrefix[ii]; 1240 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar); 1241 if( nByte ){ 1242 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent); 1243 if( bPresent==0 ){ 1244 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum( 1245 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte 1246 ); 1247 } 1248 } 1249 } 1250 1251 return rc; 1252 } 1253 1254 /* 1255 ** Check that the contents of the FTS index match that of the %_content 1256 ** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return 1257 ** some other SQLite error code if an error occurs while attempting to 1258 ** determine this. 1259 */ 1260 int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){ 1261 Fts5Config *pConfig = p->pConfig; 1262 int rc = SQLITE_OK; /* Return code */ 1263 int *aColSize; /* Array of size pConfig->nCol */ 1264 i64 *aTotalSize; /* Array of size pConfig->nCol */ 1265 Fts5IntegrityCtx ctx; 1266 sqlite3_stmt *pScan; 1267 int bUseCksum; 1268 1269 memset(&ctx, 0, sizeof(Fts5IntegrityCtx)); 1270 ctx.pConfig = p->pConfig; 1271 aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64))); 1272 if( !aTotalSize ) return SQLITE_NOMEM; 1273 aColSize = (int*)&aTotalSize[pConfig->nCol]; 1274 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol); 1275 1276 bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL 1277 || (pConfig->eContent==FTS5_CONTENT_EXTERNAL && iArg) 1278 ); 1279 if( bUseCksum ){ 1280 /* Generate the expected index checksum based on the contents of the 1281 ** %_content table. This block stores the checksum in ctx.cksum. */ 1282 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0); 1283 if( rc==SQLITE_OK ){ 1284 int rc2; 1285 while( SQLITE_ROW==sqlite3_step(pScan) ){ 1286 int i; 1287 ctx.iRowid = sqlite3_column_int64(pScan, 0); 1288 ctx.szCol = 0; 1289 if( pConfig->bColumnsize ){ 1290 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize); 1291 } 1292 if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){ 1293 rc = sqlite3Fts5TermsetNew(&ctx.pTermset); 1294 } 1295 for(i=0; rc==SQLITE_OK && inCol; i++){ 1296 if( pConfig->abUnindexed[i]==0 ){ 1297 const char *pText = 0; 1298 int nText = 0; 1299 const char *pLoc = 0; 1300 int nLoc = 0; 1301 sqlite3_value *pVal = sqlite3_column_value(pScan, i+1); 1302 1303 if( pConfig->eContent==FTS5_CONTENT_EXTERNAL 1304 && sqlite3Fts5IsLocaleValue(pConfig, pVal) 1305 ){ 1306 rc = sqlite3Fts5DecodeLocaleValue( 1307 pVal, &pText, &nText, &pLoc, &nLoc 1308 ); 1309 }else{ 1310 if( pConfig->eContent==FTS5_CONTENT_NORMAL && pConfig->bLocale ){ 1311 int iCol = i + 1 + pConfig->nCol; 1312 pLoc = (const char*)sqlite3_column_text(pScan, iCol); 1313 nLoc = sqlite3_column_bytes(pScan, iCol); 1314 } 1315 pText = (const char*)sqlite3_value_text(pVal); 1316 nText = sqlite3_value_bytes(pVal); 1317 } 1318 1319 ctx.iCol = i; 1320 ctx.szCol = 0; 1321 1322 if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_COLUMNS ){ 1323 rc = sqlite3Fts5TermsetNew(&ctx.pTermset); 1324 } 1325 1326 if( rc==SQLITE_OK ){ 1327 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); 1328 rc = sqlite3Fts5Tokenize(pConfig, 1329 FTS5_TOKENIZE_DOCUMENT, 1330 pText, nText, 1331 (void*)&ctx, 1332 fts5StorageIntegrityCallback 1333 ); 1334 sqlite3Fts5ClearLocale(pConfig); 1335 } 1336 1337 /* If this is not a columnsize=0 database, check that the number 1338 ** of tokens in the value matches the aColSize[] value read from 1339 ** the %_docsize table. */ 1340 if( rc==SQLITE_OK 1341 && pConfig->bColumnsize 1342 && ctx.szCol!=aColSize[i] 1343 ){ 1344 rc = FTS5_CORRUPT; 1345 } 1346 aTotalSize[i] += ctx.szCol; 1347 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){ 1348 sqlite3Fts5TermsetFree(ctx.pTermset); 1349 ctx.pTermset = 0; 1350 } 1351 } 1352 } 1353 sqlite3Fts5TermsetFree(ctx.pTermset); 1354 ctx.pTermset = 0; 1355 1356 if( rc!=SQLITE_OK ) break; 1357 } 1358 rc2 = sqlite3_reset(pScan); 1359 if( rc==SQLITE_OK ) rc = rc2; 1360 } 1361 1362 /* Test that the "totals" (sometimes called "averages") record looks Ok */ 1363 if( rc==SQLITE_OK ){ 1364 int i; 1365 rc = fts5StorageLoadTotals(p, 0); 1366 for(i=0; rc==SQLITE_OK && inCol; i++){ 1367 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT; 1368 } 1369 } 1370 1371 /* Check that the %_docsize and %_content tables contain the expected 1372 ** number of rows. */ 1373 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){ 1374 i64 nRow = 0; 1375 rc = fts5StorageCount(p, "content", &nRow); 1376 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT; 1377 } 1378 if( rc==SQLITE_OK && pConfig->bColumnsize ){ 1379 i64 nRow = 0; 1380 rc = fts5StorageCount(p, "docsize", &nRow); 1381 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT; 1382 } 1383 } 1384 1385 /* Pass the expected checksum down to the FTS index module. It will 1386 ** verify, amongst other things, that it matches the checksum generated by 1387 ** inspecting the index itself. */ 1388 if( rc==SQLITE_OK ){ 1389 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum, bUseCksum); 1390 } 1391 1392 sqlite3_free(aTotalSize); 1393 return rc; 1394 } 1395 1396 /* 1397 ** Obtain an SQLite statement handle that may be used to read data from the 1398 ** %_content table. 1399 */ 1400 int sqlite3Fts5StorageStmt( 1401 Fts5Storage *p, 1402 int eStmt, 1403 sqlite3_stmt **pp, 1404 char **pzErrMsg 1405 ){ 1406 int rc; 1407 assert( eStmt==FTS5_STMT_SCAN_ASC 1408 || eStmt==FTS5_STMT_SCAN_DESC 1409 || eStmt==FTS5_STMT_LOOKUP 1410 ); 1411 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg); 1412 if( rc==SQLITE_OK ){ 1413 assert( p->aStmt[eStmt]==*pp ); 1414 p->aStmt[eStmt] = 0; 1415 } 1416 return rc; 1417 } 1418 1419 /* 1420 ** Release an SQLite statement handle obtained via an earlier call to 1421 ** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function 1422 ** must match that passed to the sqlite3Fts5StorageStmt() call. 1423 */ 1424 void sqlite3Fts5StorageStmtRelease( 1425 Fts5Storage *p, 1426 int eStmt, 1427 sqlite3_stmt *pStmt 1428 ){ 1429 assert( eStmt==FTS5_STMT_SCAN_ASC 1430 || eStmt==FTS5_STMT_SCAN_DESC 1431 || eStmt==FTS5_STMT_LOOKUP 1432 ); 1433 if( p->aStmt[eStmt]==0 ){ 1434 sqlite3_reset(pStmt); 1435 p->aStmt[eStmt] = pStmt; 1436 }else{ 1437 sqlite3_finalize(pStmt); 1438 } 1439 } 1440 1441 /* 1442 ** Argument aCol points to an array of integers containing one entry for 1443 ** each table column. This function reads the %_docsize record for the 1444 ** specified rowid and populates aCol[] with the results. 1445 ** 1446 ** An SQLite error code is returned if an error occurs, or SQLITE_OK 1447 ** otherwise. 1448 */ 1449 int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){ 1450 int nCol = p->pConfig->nCol; /* Number of user columns in table */ 1451 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */ 1452 int rc; /* Return Code */ 1453 1454 assert( p->pConfig->bColumnsize ); 1455 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0); 1456 if( pLookup ){ 1457 int bCorrupt = 1; 1458 assert( rc==SQLITE_OK ); 1459 sqlite3_bind_int64(pLookup, 1, iRowid); 1460 if( SQLITE_ROW==sqlite3_step(pLookup) ){ 1461 const u8 *aBlob = sqlite3_column_blob(pLookup, 0); 1462 int nBlob = sqlite3_column_bytes(pLookup, 0); 1463 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){ 1464 bCorrupt = 0; 1465 } 1466 } 1467 rc = sqlite3_reset(pLookup); 1468 if( bCorrupt && rc==SQLITE_OK ){ 1469 rc = FTS5_CORRUPT; 1470 } 1471 }else{ 1472 assert( rc!=SQLITE_OK ); 1473 } 1474 1475 return rc; 1476 } 1477 1478 int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){ 1479 int rc = fts5StorageLoadTotals(p, 0); 1480 if( rc==SQLITE_OK ){ 1481 *pnToken = 0; 1482 if( iCol<0 ){ 1483 int i; 1484 for(i=0; ipConfig->nCol; i++){ 1485 *pnToken += p->aTotalSize[i]; 1486 } 1487 }else if( iColpConfig->nCol ){ 1488 *pnToken = p->aTotalSize[iCol]; 1489 }else{ 1490 rc = SQLITE_RANGE; 1491 } 1492 } 1493 return rc; 1494 } 1495 1496 int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){ 1497 int rc = fts5StorageLoadTotals(p, 0); 1498 if( rc==SQLITE_OK ){ 1499 /* nTotalRow being zero does not necessarily indicate a corrupt 1500 ** database - it might be that the FTS5 table really does contain zero 1501 ** rows. However this function is only called from the xRowCount() API, 1502 ** and there is no way for that API to be invoked if the table contains 1503 ** no rows. Hence the FTS5_CORRUPT return. */ 1504 *pnRow = p->nTotalRow; 1505 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT; 1506 } 1507 return rc; 1508 } 1509 1510 /* 1511 ** Flush any data currently held in-memory to disk. 1512 */ 1513 int sqlite3Fts5StorageSync(Fts5Storage *p){ 1514 int rc = SQLITE_OK; 1515 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db); 1516 if( p->bTotalsValid ){ 1517 rc = fts5StorageSaveTotals(p); 1518 if( rc==SQLITE_OK ){ 1519 p->bTotalsValid = 0; 1520 } 1521 } 1522 if( rc==SQLITE_OK ){ 1523 rc = sqlite3Fts5IndexSync(p->pIndex); 1524 } 1525 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid); 1526 return rc; 1527 } 1528 1529 int sqlite3Fts5StorageRollback(Fts5Storage *p){ 1530 p->bTotalsValid = 0; 1531 return sqlite3Fts5IndexRollback(p->pIndex); 1532 } 1533 1534 int sqlite3Fts5StorageConfigValue( 1535 Fts5Storage *p, 1536 const char *z, 1537 sqlite3_value *pVal, 1538 int iVal 1539 ){ 1540 sqlite3_stmt *pReplace = 0; 1541 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0); 1542 if( rc==SQLITE_OK ){ 1543 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC); 1544 if( pVal ){ 1545 sqlite3_bind_value(pReplace, 2, pVal); 1546 }else{ 1547 sqlite3_bind_int(pReplace, 2, iVal); 1548 } 1549 sqlite3_step(pReplace); 1550 rc = sqlite3_reset(pReplace); 1551 sqlite3_bind_null(pReplace, 1); 1552 } 1553 if( rc==SQLITE_OK && pVal ){ 1554 int iNew = p->pConfig->iCookie + 1; 1555 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew); 1556 if( rc==SQLITE_OK ){ 1557 p->pConfig->iCookie = iNew; 1558 } 1559 } 1560 return rc; 1561 }