2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** 2014 May 31
|
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2015-03-04 11:29:24 +03:00
|
|
|
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
#include "fts5Int.h"
|
|
|
|
|
2024-08-01 20:15:17 +03:00
|
|
|
/*
|
|
|
|
** pSavedRow:
|
|
|
|
** SQL statement FTS5_STMT_LOOKUP2 is a copy of FTS5_STMT_LOOKUP, it
|
|
|
|
** does a by-rowid lookup to retrieve a single row from the %_content
|
|
|
|
** table or equivalent external-content table/view.
|
|
|
|
**
|
|
|
|
** However, FTS5_STMT_LOOKUP2 is only used when retrieving the original
|
|
|
|
** values for a row being UPDATEd. In that case, the SQL statement is
|
|
|
|
** not reset and pSavedRow is set to point at it. This is so that the
|
|
|
|
** insert operation that follows the delete may access the original
|
|
|
|
** row values for any new values for which sqlite3_value_nochange() returns
|
|
|
|
** true. i.e. if the user executes:
|
|
|
|
**
|
|
|
|
** CREATE VIRTUAL TABLE ft USING fts5(a, b, c, locale=1);
|
|
|
|
** ...
|
|
|
|
** UPDATE fts SET a=?, b=? WHERE rowid=?;
|
|
|
|
**
|
|
|
|
** then the value passed to the xUpdate() method of this table as the
|
|
|
|
** new.c value is an sqlite3_value_nochange() value. So in this case it
|
|
|
|
** must be read from the saved row stored in Fts5Storage.pSavedRow.
|
|
|
|
**
|
|
|
|
** This is necessary - using sqlite3_value_nochange() instead of just having
|
|
|
|
** SQLite pass the original value back via xUpdate() - so as not to discard
|
|
|
|
** any locale information associated with such values.
|
|
|
|
**
|
|
|
|
*/
|
2014-06-23 15:33:22 +04:00
|
|
|
struct Fts5Storage {
|
|
|
|
Fts5Config *pConfig;
|
|
|
|
Fts5Index *pIndex;
|
2014-08-12 20:07:35 +04:00
|
|
|
int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
|
2014-07-21 15:44:47 +04:00
|
|
|
i64 nTotalRow; /* Total number of rows in FTS table */
|
|
|
|
i64 *aTotalSize; /* Total sizes of each column */
|
2024-07-27 22:46:10 +03:00
|
|
|
sqlite3_stmt *pSavedRow;
|
|
|
|
sqlite3_stmt *aStmt[12];
|
2014-06-23 15:33:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-06-24 20:59:06 +04:00
|
|
|
#if FTS5_STMT_SCAN_ASC!=0
|
|
|
|
# error "FTS5_STMT_SCAN_ASC mismatch"
|
|
|
|
#endif
|
|
|
|
#if FTS5_STMT_SCAN_DESC!=1
|
|
|
|
# error "FTS5_STMT_SCAN_DESC mismatch"
|
|
|
|
#endif
|
|
|
|
#if FTS5_STMT_LOOKUP!=2
|
|
|
|
# error "FTS5_STMT_LOOKUP mismatch"
|
|
|
|
#endif
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2024-08-01 20:15:17 +03:00
|
|
|
#define FTS5_STMT_LOOKUP2 3
|
2024-07-27 22:46:10 +03:00
|
|
|
#define FTS5_STMT_INSERT_CONTENT 4
|
|
|
|
#define FTS5_STMT_REPLACE_CONTENT 5
|
|
|
|
#define FTS5_STMT_DELETE_CONTENT 6
|
2024-08-01 20:15:17 +03:00
|
|
|
#define FTS5_STMT_REPLACE_DOCSIZE 7
|
2024-07-27 22:46:10 +03:00
|
|
|
#define FTS5_STMT_DELETE_DOCSIZE 8
|
|
|
|
#define FTS5_STMT_LOOKUP_DOCSIZE 9
|
|
|
|
#define FTS5_STMT_REPLACE_CONFIG 10
|
2024-08-01 20:15:17 +03:00
|
|
|
#define FTS5_STMT_SCAN 11
|
2015-06-06 19:28:29 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** Prepare the two insert statements - Fts5Storage.pInsertContent and
|
|
|
|
** Fts5Storage.pInsertDocsize - if they have not already been prepared.
|
|
|
|
** Return SQLITE_OK if successful, or an SQLite error code if an error
|
|
|
|
** occurs.
|
|
|
|
*/
|
|
|
|
static int fts5StorageGetStmt(
|
|
|
|
Fts5Storage *p, /* Storage handle */
|
|
|
|
int eStmt, /* FTS5_STMT_XXX constant */
|
2015-01-05 23:41:39 +03:00
|
|
|
sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
|
|
|
|
char **pzErrMsg /* OUT: Error message (if any) */
|
2014-06-23 15:33:22 +04:00
|
|
|
){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
2015-06-09 23:58:39 +03:00
|
|
|
/* If there is no %_docsize table, there should be no requests for
|
|
|
|
** statements to operate on it. */
|
|
|
|
assert( p->pConfig->bColumnsize || (
|
|
|
|
eStmt!=FTS5_STMT_REPLACE_DOCSIZE
|
|
|
|
&& eStmt!=FTS5_STMT_DELETE_DOCSIZE
|
|
|
|
&& eStmt!=FTS5_STMT_LOOKUP_DOCSIZE
|
|
|
|
));
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
|
|
|
|
if( p->aStmt[eStmt]==0 ){
|
|
|
|
const char *azStmt[] = {
|
2015-06-06 19:28:29 +03:00
|
|
|
"SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
|
|
|
|
"SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
|
|
|
|
"SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
|
2024-07-27 22:46:10 +03:00
|
|
|
"SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP2 */
|
2014-06-24 20:59:06 +04:00
|
|
|
|
|
|
|
"INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
|
|
|
|
"REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
|
|
|
|
"DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
|
2023-07-10 23:44:09 +03:00
|
|
|
"REPLACE INTO %Q.'%q_docsize' VALUES(?,?%s)", /* REPLACE_DOCSIZE */
|
2014-06-24 20:59:06 +04:00
|
|
|
"DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
|
2014-07-19 19:35:09 +04:00
|
|
|
|
2023-07-10 23:44:09 +03:00
|
|
|
"SELECT sz%s FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
|
2014-11-27 23:03:45 +03:00
|
|
|
|
|
|
|
"REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
|
2015-06-06 19:28:29 +03:00
|
|
|
"SELECT %s FROM %s AS T", /* SCAN */
|
2014-06-23 15:33:22 +04:00
|
|
|
};
|
2015-01-03 23:44:58 +03:00
|
|
|
Fts5Config *pC = p->pConfig;
|
2014-06-23 15:33:22 +04:00
|
|
|
char *zSql = 0;
|
|
|
|
|
2024-07-27 22:46:10 +03:00
|
|
|
assert( ArraySize(azStmt)==ArraySize(p->aStmt) );
|
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
switch( eStmt ){
|
2015-06-06 19:28:29 +03:00
|
|
|
case FTS5_STMT_SCAN:
|
|
|
|
zSql = sqlite3_mprintf(azStmt[eStmt],
|
|
|
|
pC->zContentExprlist, pC->zContent
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
case FTS5_STMT_SCAN_ASC:
|
|
|
|
case FTS5_STMT_SCAN_DESC:
|
2015-06-06 19:28:29 +03:00
|
|
|
zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
|
|
|
|
pC->zContent, pC->zContentRowid, pC->zContentRowid,
|
|
|
|
pC->zContentRowid
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
case FTS5_STMT_LOOKUP:
|
2024-07-27 22:46:10 +03:00
|
|
|
case FTS5_STMT_LOOKUP2:
|
2015-04-27 19:21:49 +03:00
|
|
|
zSql = sqlite3_mprintf(azStmt[eStmt],
|
|
|
|
pC->zContentExprlist, pC->zContent, pC->zContentRowid
|
|
|
|
);
|
2015-01-03 23:44:58 +03:00
|
|
|
break;
|
|
|
|
|
2024-09-27 13:57:41 +03:00
|
|
|
case FTS5_STMT_INSERT_CONTENT:
|
|
|
|
case FTS5_STMT_REPLACE_CONTENT: {
|
2024-09-03 21:55:38 +03:00
|
|
|
char *zBind = 0;
|
2015-01-03 23:44:58 +03:00
|
|
|
int i;
|
|
|
|
|
2024-09-03 21:55:38 +03:00
|
|
|
assert( pC->eContent==FTS5_CONTENT_NORMAL
|
|
|
|
|| pC->eContent==FTS5_CONTENT_UNINDEXED
|
|
|
|
);
|
|
|
|
|
2024-09-13 18:37:31 +03:00
|
|
|
/* Add bindings for the "c*" columns - those that store the actual
|
|
|
|
** table content. If eContent==NORMAL, then there is one binding
|
|
|
|
** for each column. Or, if eContent==UNINDEXED, then there are only
|
|
|
|
** bindings for the UNINDEXED columns. */
|
|
|
|
for(i=0; rc==SQLITE_OK && i<(pC->nCol+1); i++){
|
2024-09-03 21:55:38 +03:00
|
|
|
if( !i || pC->eContent==FTS5_CONTENT_NORMAL || pC->abUnindexed[i-1] ){
|
|
|
|
zBind = sqlite3Fts5Mprintf(&rc, "%z%s?%d", zBind, zBind?",":"",i+1);
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2024-09-03 21:55:38 +03:00
|
|
|
|
2024-09-13 18:37:31 +03:00
|
|
|
/* Add bindings for any "l*" columns. Only non-UNINDEXED columns
|
|
|
|
** require these. */
|
|
|
|
if( pC->bLocale && pC->eContent==FTS5_CONTENT_NORMAL ){
|
|
|
|
for(i=0; rc==SQLITE_OK && i<pC->nCol; i++){
|
|
|
|
if( pC->abUnindexed[i]==0 ){
|
|
|
|
zBind = sqlite3Fts5Mprintf(&rc, "%z,?%d", zBind, pC->nCol+i+2);
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2024-09-13 18:37:31 +03:00
|
|
|
|
2024-09-03 21:55:38 +03:00
|
|
|
zSql = sqlite3Fts5Mprintf(&rc, azStmt[eStmt], pC->zDb, pC->zName,zBind);
|
|
|
|
sqlite3_free(zBind);
|
2015-01-03 23:44:58 +03:00
|
|
|
break;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
|
2023-07-10 23:44:09 +03:00
|
|
|
case FTS5_STMT_REPLACE_DOCSIZE:
|
|
|
|
zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName,
|
|
|
|
(pC->bContentlessDelete ? ",?" : "")
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FTS5_STMT_LOOKUP_DOCSIZE:
|
|
|
|
zSql = sqlite3_mprintf(azStmt[eStmt],
|
2023-07-17 20:59:58 +03:00
|
|
|
(pC->bContentlessDelete ? ",origin" : ""),
|
2023-07-10 23:44:09 +03:00
|
|
|
pC->zDb, pC->zName
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
default:
|
|
|
|
zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
|
|
|
|
break;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( zSql==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
2018-12-21 22:29:11 +03:00
|
|
|
int f = SQLITE_PREPARE_PERSISTENT;
|
2024-07-27 22:46:10 +03:00
|
|
|
if( eStmt>FTS5_STMT_LOOKUP2 ) f |= SQLITE_PREPARE_NO_VTAB;
|
2019-08-05 15:55:56 +03:00
|
|
|
p->pConfig->bLock++;
|
2018-12-21 22:29:11 +03:00
|
|
|
rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
|
2019-08-05 15:55:56 +03:00
|
|
|
p->pConfig->bLock--;
|
2014-06-23 15:33:22 +04:00
|
|
|
sqlite3_free(zSql);
|
2015-01-05 23:41:39 +03:00
|
|
|
if( rc!=SQLITE_OK && pzErrMsg ){
|
|
|
|
*pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ppStmt = p->aStmt[eStmt];
|
2016-03-18 03:39:40 +03:00
|
|
|
sqlite3_reset(*ppStmt);
|
2014-06-23 15:33:22 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-07 20:11:11 +03:00
|
|
|
|
|
|
|
static int fts5ExecPrintf(
|
|
|
|
sqlite3 *db,
|
|
|
|
char **pzErr,
|
|
|
|
const char *zFormat,
|
|
|
|
...
|
|
|
|
){
|
2014-06-23 15:33:22 +04:00
|
|
|
int rc;
|
2015-01-07 20:11:11 +03:00
|
|
|
va_list ap; /* ... printf arguments */
|
2015-06-26 07:34:36 +03:00
|
|
|
char *zSql;
|
|
|
|
|
2015-01-07 20:11:11 +03:00
|
|
|
va_start(ap, zFormat);
|
2015-06-26 07:34:36 +03:00
|
|
|
zSql = sqlite3_vmprintf(zFormat, ap);
|
2015-01-07 20:11:11 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
if( zSql==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
2015-01-07 20:11:11 +03:00
|
|
|
rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
|
2014-06-23 15:33:22 +04:00
|
|
|
sqlite3_free(zSql);
|
|
|
|
}
|
2015-01-07 20:11:11 +03:00
|
|
|
|
|
|
|
va_end(ap);
|
2014-06-23 15:33:22 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-07 20:11:11 +03:00
|
|
|
/*
|
2015-04-28 21:35:28 +03:00
|
|
|
** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
|
|
|
|
** code otherwise.
|
2015-01-07 20:11:11 +03:00
|
|
|
*/
|
2015-04-28 21:35:28 +03:00
|
|
|
int sqlite3Fts5DropAll(Fts5Config *pConfig){
|
|
|
|
int rc = fts5ExecPrintf(pConfig->db, 0,
|
|
|
|
"DROP TABLE IF EXISTS %Q.'%q_data';"
|
2015-07-15 22:46:02 +03:00
|
|
|
"DROP TABLE IF EXISTS %Q.'%q_idx';"
|
2015-04-28 21:35:28 +03:00
|
|
|
"DROP TABLE IF EXISTS %Q.'%q_config';",
|
|
|
|
pConfig->zDb, pConfig->zName,
|
2015-07-15 22:46:02 +03:00
|
|
|
pConfig->zDb, pConfig->zName,
|
2015-04-28 21:35:28 +03:00
|
|
|
pConfig->zDb, pConfig->zName
|
2015-01-07 20:11:11 +03:00
|
|
|
);
|
2015-06-09 23:58:39 +03:00
|
|
|
if( rc==SQLITE_OK && pConfig->bColumnsize ){
|
|
|
|
rc = fts5ExecPrintf(pConfig->db, 0,
|
|
|
|
"DROP TABLE IF EXISTS %Q.'%q_docsize';",
|
|
|
|
pConfig->zDb, pConfig->zName
|
|
|
|
);
|
|
|
|
}
|
2015-04-28 21:35:28 +03:00
|
|
|
if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
|
|
|
|
rc = fts5ExecPrintf(pConfig->db, 0,
|
|
|
|
"DROP TABLE IF EXISTS %Q.'%q_content';",
|
|
|
|
pConfig->zDb, pConfig->zName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return rc;
|
2015-01-07 20:11:11 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 13:45:34 +03:00
|
|
|
static void fts5StorageRenameOne(
|
|
|
|
Fts5Config *pConfig, /* Current FTS5 configuration */
|
|
|
|
int *pRc, /* IN/OUT: Error code */
|
|
|
|
const char *zTail, /* Tail of table name e.g. "data", "config" */
|
|
|
|
const char *zName /* New name of FTS5 table */
|
|
|
|
){
|
|
|
|
if( *pRc==SQLITE_OK ){
|
|
|
|
*pRc = fts5ExecPrintf(pConfig->db, 0,
|
|
|
|
"ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
|
|
|
|
pConfig->zDb, pConfig->zName, zTail, zName, zTail
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
|
|
|
|
Fts5Config *pConfig = pStorage->pConfig;
|
2017-04-08 12:12:20 +03:00
|
|
|
int rc = sqlite3Fts5StorageSync(pStorage);
|
2015-06-10 13:45:34 +03:00
|
|
|
|
|
|
|
fts5StorageRenameOne(pConfig, &rc, "data", zName);
|
2015-07-15 22:46:02 +03:00
|
|
|
fts5StorageRenameOne(pConfig, &rc, "idx", zName);
|
2015-06-10 13:45:34 +03:00
|
|
|
fts5StorageRenameOne(pConfig, &rc, "config", zName);
|
|
|
|
if( pConfig->bColumnsize ){
|
|
|
|
fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
|
|
|
|
}
|
|
|
|
if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
|
|
|
|
fts5StorageRenameOne(pConfig, &rc, "content", zName);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** Create the shadow table named zPost, with definition zDefn. Return
|
|
|
|
** SQLITE_OK if successful, or an SQLite error code otherwise.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5CreateTable(
|
|
|
|
Fts5Config *pConfig, /* FTS5 configuration */
|
|
|
|
const char *zPost, /* Shadow table to create (e.g. "content") */
|
|
|
|
const char *zDefn, /* Columns etc. for shadow table */
|
2014-11-27 23:03:45 +03:00
|
|
|
int bWithout, /* True for without rowid */
|
2014-06-23 15:33:22 +04:00
|
|
|
char **pzErr /* OUT: Error message */
|
|
|
|
){
|
|
|
|
int rc;
|
2015-01-07 20:11:11 +03:00
|
|
|
char *zErr = 0;
|
|
|
|
|
|
|
|
rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
|
2016-07-15 22:17:19 +03:00
|
|
|
pConfig->zDb, pConfig->zName, zPost, zDefn,
|
|
|
|
#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
|
|
|
|
bWithout?" WITHOUT ROWID":
|
|
|
|
#endif
|
|
|
|
""
|
2014-06-23 15:33:22 +04:00
|
|
|
);
|
2015-01-07 20:11:11 +03:00
|
|
|
if( zErr ){
|
|
|
|
*pzErr = sqlite3_mprintf(
|
|
|
|
"fts5: error creating shadow table %q_%s: %s",
|
|
|
|
pConfig->zName, zPost, zErr
|
|
|
|
);
|
|
|
|
sqlite3_free(zErr);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-01-07 20:11:11 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Open a new Fts5Index handle. If the bCreate argument is true, create
|
|
|
|
** and initialize the underlying tables
|
|
|
|
**
|
|
|
|
** If successful, set *pp to point to the new object and return SQLITE_OK.
|
|
|
|
** Otherwise, set *pp to NULL and return an SQLite error code.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5StorageOpen(
|
|
|
|
Fts5Config *pConfig,
|
|
|
|
Fts5Index *pIndex,
|
|
|
|
int bCreate,
|
|
|
|
Fts5Storage **pp,
|
|
|
|
char **pzErr /* OUT: Error message */
|
|
|
|
){
|
2014-08-09 22:22:59 +04:00
|
|
|
int rc = SQLITE_OK;
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5Storage *p; /* New object */
|
2019-01-08 23:02:48 +03:00
|
|
|
sqlite3_int64 nByte; /* Bytes of space to allocate */
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2014-07-21 15:44:47 +04:00
|
|
|
nByte = sizeof(Fts5Storage) /* Fts5Storage object */
|
|
|
|
+ pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
|
2019-01-08 23:02:48 +03:00
|
|
|
*pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
|
2014-06-23 15:33:22 +04:00
|
|
|
if( !p ) return SQLITE_NOMEM;
|
|
|
|
|
2019-03-20 08:45:03 +03:00
|
|
|
memset(p, 0, (size_t)nByte);
|
2014-07-21 15:44:47 +04:00
|
|
|
p->aTotalSize = (i64*)&p[1];
|
2014-06-23 15:33:22 +04:00
|
|
|
p->pConfig = pConfig;
|
|
|
|
p->pIndex = pIndex;
|
|
|
|
|
|
|
|
if( bCreate ){
|
2024-09-03 21:55:38 +03:00
|
|
|
if( pConfig->eContent==FTS5_CONTENT_NORMAL
|
|
|
|
|| pConfig->eContent==FTS5_CONTENT_UNINDEXED
|
|
|
|
){
|
2015-05-30 14:49:58 +03:00
|
|
|
int nDefn = 32 + pConfig->nCol*10;
|
2024-09-10 19:19:31 +03:00
|
|
|
char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 20);
|
2015-01-03 23:44:58 +03:00
|
|
|
if( zDefn==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
int i;
|
2015-05-30 14:49:58 +03:00
|
|
|
int iOff;
|
|
|
|
sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
|
2015-10-14 23:34:57 +03:00
|
|
|
iOff = (int)strlen(zDefn);
|
2015-01-03 23:44:58 +03:00
|
|
|
for(i=0; i<pConfig->nCol; i++){
|
2024-09-03 21:55:38 +03:00
|
|
|
if( pConfig->eContent==FTS5_CONTENT_NORMAL
|
|
|
|
|| pConfig->abUnindexed[i]
|
|
|
|
){
|
|
|
|
sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
|
|
|
|
iOff += (int)strlen(&zDefn[iOff]);
|
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
2024-09-10 19:19:31 +03:00
|
|
|
if( pConfig->bLocale ){
|
|
|
|
for(i=0; i<pConfig->nCol; i++){
|
|
|
|
if( pConfig->abUnindexed[i]==0 ){
|
|
|
|
sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", l%d", i);
|
|
|
|
iOff += (int)strlen(&zDefn[iOff]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
sqlite3_free(zDefn);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
|
2015-06-09 23:58:39 +03:00
|
|
|
if( rc==SQLITE_OK && pConfig->bColumnsize ){
|
2023-07-10 23:44:09 +03:00
|
|
|
const char *zCols = "id INTEGER PRIMARY KEY, sz BLOB";
|
|
|
|
if( pConfig->bContentlessDelete ){
|
2023-07-17 20:59:58 +03:00
|
|
|
zCols = "id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER";
|
2023-07-10 23:44:09 +03:00
|
|
|
}
|
|
|
|
rc = sqlite3Fts5CreateTable(pConfig, "docsize", zCols, 0, pzErr);
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts5CreateTable(
|
|
|
|
pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
|
2014-06-23 15:33:22 +04:00
|
|
|
);
|
|
|
|
}
|
2015-05-07 22:29:46 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( rc ){
|
2015-04-28 21:35:28 +03:00
|
|
|
sqlite3Fts5StorageClose(p);
|
2014-06-23 15:33:22 +04:00
|
|
|
*pp = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
|
|
|
|
*/
|
2015-04-28 21:35:28 +03:00
|
|
|
int sqlite3Fts5StorageClose(Fts5Storage *p){
|
2014-06-23 15:33:22 +04:00
|
|
|
int rc = SQLITE_OK;
|
2014-12-03 20:27:35 +03:00
|
|
|
if( p ){
|
|
|
|
int i;
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2014-12-03 20:27:35 +03:00
|
|
|
/* Finalize all SQL statements */
|
2016-01-23 21:51:59 +03:00
|
|
|
for(i=0; i<ArraySize(p->aStmt); i++){
|
2014-12-03 20:27:35 +03:00
|
|
|
sqlite3_finalize(p->aStmt[i]);
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2014-12-03 20:27:35 +03:00
|
|
|
sqlite3_free(p);
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct Fts5InsertCtx Fts5InsertCtx;
|
|
|
|
struct Fts5InsertCtx {
|
|
|
|
Fts5Storage *pStorage;
|
|
|
|
int iCol;
|
2014-07-19 19:35:09 +04:00
|
|
|
int szCol; /* Size of column value in tokens */
|
2014-06-23 15:33:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Tokenization callback used when inserting tokens into the FTS index.
|
|
|
|
*/
|
|
|
|
static int fts5StorageInsertCallback(
|
|
|
|
void *pContext, /* Pointer to Fts5InsertCtx object */
|
2015-08-29 18:44:27 +03:00
|
|
|
int tflags,
|
2014-06-23 15:33:22 +04:00
|
|
|
const char *pToken, /* Buffer containing token */
|
|
|
|
int nToken, /* Size of token in bytes */
|
2016-02-11 20:01:32 +03:00
|
|
|
int iUnused1, /* Start offset of token */
|
|
|
|
int iUnused2 /* End offset of token */
|
2014-06-23 15:33:22 +04:00
|
|
|
){
|
|
|
|
Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
|
|
|
|
Fts5Index *pIdx = pCtx->pStorage->pIndex;
|
2016-02-11 20:01:32 +03:00
|
|
|
UNUSED_PARAM2(iUnused1, iUnused2);
|
2016-03-23 18:04:00 +03:00
|
|
|
if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
|
2015-08-29 21:46:12 +03:00
|
|
|
if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
|
2015-08-29 18:44:27 +03:00
|
|
|
pCtx->szCol++;
|
|
|
|
}
|
|
|
|
return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2024-08-01 20:15:17 +03:00
|
|
|
/*
|
|
|
|
** This function is used as part of an UPDATE statement that modifies the
|
|
|
|
** rowid of a row. In that case, this function is called first to set
|
|
|
|
** Fts5Storage.pSavedRow to point to a statement that may be used to
|
|
|
|
** access the original values of the row being deleted - iDel.
|
|
|
|
**
|
|
|
|
** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
|
|
|
|
** It is not considered an error if row iDel does not exist. In this case
|
|
|
|
** pSavedRow is not set and SQLITE_OK returned.
|
|
|
|
*/
|
2024-07-27 22:46:10 +03:00
|
|
|
int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
sqlite3_stmt *pSeek = 0;
|
|
|
|
|
|
|
|
assert( p->pSavedRow==0 );
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP+1, &pSeek, 0);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_int64(pSeek, 1, iDel);
|
|
|
|
if( sqlite3_step(pSeek)!=SQLITE_ROW ){
|
|
|
|
rc = sqlite3_reset(pSeek);
|
|
|
|
}else{
|
|
|
|
p->pSavedRow = pSeek;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** If a row with rowid iDel is present in the %_content table, add the
|
|
|
|
** delete-markers to the FTS index necessary to delete it. Do not actually
|
|
|
|
** remove the %_content row at this time though.
|
2024-08-01 20:15:17 +03:00
|
|
|
**
|
|
|
|
** If parameter bSaveRow is true, then Fts5Storage.pSavedRow is left
|
|
|
|
** pointing to a statement (FTS5_STMT_LOOKUP2) that may be used to access
|
|
|
|
** the original values of the row being deleted. This is used by UPDATE
|
|
|
|
** statements.
|
2014-06-23 15:33:22 +04:00
|
|
|
*/
|
2016-01-15 22:54:47 +03:00
|
|
|
static int fts5StorageDeleteFromIndex(
|
|
|
|
Fts5Storage *p,
|
|
|
|
i64 iDel,
|
2024-07-27 22:46:10 +03:00
|
|
|
sqlite3_value **apVal,
|
2024-08-01 20:15:17 +03:00
|
|
|
int bSaveRow /* True to set pSavedRow */
|
2016-01-15 22:54:47 +03:00
|
|
|
){
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5Config *pConfig = p->pConfig;
|
2016-01-15 22:54:47 +03:00
|
|
|
sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
|
2023-07-17 21:40:39 +03:00
|
|
|
int rc = SQLITE_OK; /* Return code */
|
2016-01-15 22:54:47 +03:00
|
|
|
int rc2; /* sqlite3_reset() return code */
|
|
|
|
int iCol;
|
|
|
|
Fts5InsertCtx ctx;
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2024-07-27 22:46:10 +03:00
|
|
|
assert( bSaveRow==0 || apVal==0 );
|
|
|
|
assert( bSaveRow==0 || bSaveRow==1 );
|
|
|
|
assert( FTS5_STMT_LOOKUP2==FTS5_STMT_LOOKUP+1 );
|
|
|
|
|
2016-01-15 22:54:47 +03:00
|
|
|
if( apVal==0 ){
|
2024-07-27 22:46:10 +03:00
|
|
|
if( p->pSavedRow && bSaveRow ){
|
|
|
|
pSeek = p->pSavedRow;
|
|
|
|
p->pSavedRow = 0;
|
|
|
|
}else{
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP+bSaveRow, &pSeek, 0);
|
|
|
|
if( rc!=SQLITE_OK ) return rc;
|
|
|
|
sqlite3_bind_int64(pSeek, 1, iDel);
|
|
|
|
if( sqlite3_step(pSeek)!=SQLITE_ROW ){
|
|
|
|
return sqlite3_reset(pSeek);
|
|
|
|
}
|
2016-01-15 22:54:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.pStorage = p;
|
|
|
|
ctx.iCol = -1;
|
|
|
|
for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
|
|
|
|
if( pConfig->abUnindexed[iCol-1]==0 ){
|
2024-07-26 23:50:33 +03:00
|
|
|
sqlite3_value *pVal = 0;
|
|
|
|
const char *pText = 0;
|
|
|
|
int nText = 0;
|
2024-09-10 19:19:31 +03:00
|
|
|
const char *pLoc = 0;
|
|
|
|
int nLoc = 0;
|
2024-07-26 23:50:33 +03:00
|
|
|
|
2021-10-05 21:33:38 +03:00
|
|
|
assert( pSeek==0 || apVal==0 );
|
|
|
|
assert( pSeek!=0 || apVal!=0 );
|
2016-01-15 22:54:47 +03:00
|
|
|
if( pSeek ){
|
2024-07-26 23:50:33 +03:00
|
|
|
pVal = sqlite3_column_value(pSeek, iCol);
|
2021-10-05 21:59:05 +03:00
|
|
|
}else{
|
2024-07-26 23:50:33 +03:00
|
|
|
pVal = apVal[iCol-1];
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2024-07-26 23:50:33 +03:00
|
|
|
|
2024-09-10 19:19:31 +03:00
|
|
|
if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
|
|
|
|
rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
|
|
|
|
}else{
|
|
|
|
pText = (const char*)sqlite3_value_text(pVal);
|
|
|
|
nText = sqlite3_value_bytes(pVal);
|
|
|
|
if( pConfig->bLocale && pSeek ){
|
|
|
|
pLoc = (const char*)sqlite3_column_text(pSeek, iCol + pConfig->nCol);
|
|
|
|
nLoc = sqlite3_column_bytes(pSeek, iCol + pConfig->nCol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 23:50:33 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
|
2024-07-26 23:50:33 +03:00
|
|
|
ctx.szCol = 0;
|
|
|
|
rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
|
|
|
|
pText, nText, (void*)&ctx, fts5StorageInsertCallback
|
|
|
|
);
|
|
|
|
p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
|
2024-08-16 20:54:00 +03:00
|
|
|
if( rc==SQLITE_OK && p->aTotalSize[iCol-1]<0 ){
|
2024-07-26 23:50:33 +03:00
|
|
|
rc = FTS5_CORRUPT;
|
|
|
|
}
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5ClearLocale(pConfig);
|
2020-09-11 18:01:49 +03:00
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 18:01:49 +03:00
|
|
|
if( rc==SQLITE_OK && p->nTotalRow<1 ){
|
|
|
|
rc = FTS5_CORRUPT;
|
|
|
|
}else{
|
|
|
|
p->nTotalRow--;
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2024-07-27 22:46:10 +03:00
|
|
|
if( rc==SQLITE_OK && bSaveRow ){
|
|
|
|
assert( p->pSavedRow==0 );
|
|
|
|
p->pSavedRow = pSeek;
|
|
|
|
}else{
|
|
|
|
rc2 = sqlite3_reset(pSeek);
|
|
|
|
if( rc==SQLITE_OK ) rc = rc2;
|
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-08-01 20:15:17 +03:00
|
|
|
/*
|
|
|
|
** Reset any saved statement pSavedRow. Zero pSavedRow as well. This
|
|
|
|
** should be called by the xUpdate() method of the fts5 table before
|
|
|
|
** returning from any operation that may have set Fts5Storage.pSavedRow.
|
|
|
|
*/
|
2024-07-27 22:46:10 +03:00
|
|
|
void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage *pStorage){
|
2024-08-01 20:15:17 +03:00
|
|
|
assert( pStorage->pSavedRow==0
|
|
|
|
|| pStorage->pSavedRow==pStorage->aStmt[FTS5_STMT_LOOKUP2]
|
|
|
|
);
|
2024-07-27 22:46:10 +03:00
|
|
|
sqlite3_reset(pStorage->pSavedRow);
|
|
|
|
pStorage->pSavedRow = 0;
|
|
|
|
}
|
|
|
|
|
2023-07-19 21:47:02 +03:00
|
|
|
/*
|
|
|
|
** This function is called to process a DELETE on a contentless_delete=1
|
|
|
|
** table. It adds the tombstone required to delete the entry with rowid
|
|
|
|
** iDel. If successful, SQLITE_OK is returned. Or, if an error occurs,
|
|
|
|
** an SQLite error code.
|
|
|
|
*/
|
2023-07-10 23:44:09 +03:00
|
|
|
static int fts5StorageContentlessDelete(Fts5Storage *p, i64 iDel){
|
2023-07-19 21:47:02 +03:00
|
|
|
i64 iOrigin = 0;
|
2023-07-10 23:44:09 +03:00
|
|
|
sqlite3_stmt *pLookup = 0;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
|
|
|
assert( p->pConfig->bContentlessDelete );
|
2024-09-03 21:55:38 +03:00
|
|
|
assert( p->pConfig->eContent==FTS5_CONTENT_NONE
|
|
|
|
|| p->pConfig->eContent==FTS5_CONTENT_UNINDEXED
|
|
|
|
);
|
2023-07-10 23:44:09 +03:00
|
|
|
|
2023-07-17 20:59:58 +03:00
|
|
|
/* Look up the origin of the document in the %_docsize table. Store
|
2023-07-19 21:47:02 +03:00
|
|
|
** this in stack variable iOrigin. */
|
2023-07-10 23:44:09 +03:00
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_int64(pLookup, 1, iDel);
|
|
|
|
if( SQLITE_ROW==sqlite3_step(pLookup) ){
|
2023-07-19 21:47:02 +03:00
|
|
|
iOrigin = sqlite3_column_int64(pLookup, 1);
|
2023-07-10 23:44:09 +03:00
|
|
|
}
|
|
|
|
rc = sqlite3_reset(pLookup);
|
|
|
|
}
|
|
|
|
|
2023-07-19 21:47:02 +03:00
|
|
|
if( rc==SQLITE_OK && iOrigin!=0 ){
|
|
|
|
rc = sqlite3Fts5IndexContentlessDelete(p->pIndex, iOrigin, iDel);
|
2023-07-10 23:44:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2014-07-21 19:45:26 +04:00
|
|
|
|
2014-07-19 19:35:09 +04:00
|
|
|
/*
|
|
|
|
** Insert a record into the %_docsize table. Specifically, do:
|
|
|
|
**
|
|
|
|
** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
|
2015-06-09 23:58:39 +03:00
|
|
|
**
|
|
|
|
** If there is no %_docsize table (as happens if the columnsize=0 option
|
|
|
|
** is specified when the FTS5 table is created), this function is a no-op.
|
2014-07-19 19:35:09 +04:00
|
|
|
*/
|
|
|
|
static int fts5StorageInsertDocsize(
|
|
|
|
Fts5Storage *p, /* Storage module to write to */
|
|
|
|
i64 iRowid, /* id value */
|
|
|
|
Fts5Buffer *pBuf /* sz value */
|
|
|
|
){
|
2015-06-09 23:58:39 +03:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
if( p->pConfig->bColumnsize ){
|
|
|
|
sqlite3_stmt *pReplace = 0;
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_int64(pReplace, 1, iRowid);
|
2023-07-10 23:44:09 +03:00
|
|
|
if( p->pConfig->bContentlessDelete ){
|
2023-07-17 20:59:58 +03:00
|
|
|
i64 iOrigin = 0;
|
|
|
|
rc = sqlite3Fts5IndexGetOrigin(p->pIndex, &iOrigin);
|
|
|
|
sqlite3_bind_int64(pReplace, 3, iOrigin);
|
2023-07-10 23:44:09 +03:00
|
|
|
}
|
2024-08-16 20:54:00 +03:00
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
|
|
|
|
sqlite3_step(pReplace);
|
|
|
|
rc = sqlite3_reset(pReplace);
|
|
|
|
sqlite3_bind_null(pReplace, 2);
|
2015-06-09 23:58:39 +03:00
|
|
|
}
|
2014-07-19 19:35:09 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:44:47 +04:00
|
|
|
/*
|
2014-08-12 20:07:35 +04:00
|
|
|
** Load the contents of the "averages" record from disk into the
|
|
|
|
** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
|
|
|
|
** argument bCache is true, set the p->bTotalsValid flag to indicate
|
|
|
|
** that the contents of aTotalSize[] and nTotalRow are valid until
|
|
|
|
** further notice.
|
2014-07-21 15:44:47 +04:00
|
|
|
**
|
|
|
|
** Return SQLITE_OK if successful, or an SQLite error code if an error
|
|
|
|
** occurs.
|
|
|
|
*/
|
2014-08-12 20:07:35 +04:00
|
|
|
static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
if( p->bTotalsValid==0 ){
|
2015-09-03 17:22:27 +03:00
|
|
|
rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
|
2014-08-12 20:07:35 +04:00
|
|
|
p->bTotalsValid = bCache;
|
2014-07-21 15:44:47 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Store the current contents of the p->nTotalRow and p->aTotalSize[]
|
|
|
|
** variables in the "averages" record on disk.
|
|
|
|
**
|
|
|
|
** Return SQLITE_OK if successful, or an SQLite error code if an error
|
|
|
|
** occurs.
|
|
|
|
*/
|
|
|
|
static int fts5StorageSaveTotals(Fts5Storage *p){
|
|
|
|
int nCol = p->pConfig->nCol;
|
|
|
|
int i;
|
|
|
|
Fts5Buffer buf;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
memset(&buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
|
|
|
|
}
|
|
|
|
sqlite3_free(buf.p);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:45:26 +04:00
|
|
|
/*
|
|
|
|
** Remove a row from the FTS table.
|
|
|
|
*/
|
2024-07-27 22:46:10 +03:00
|
|
|
int sqlite3Fts5StorageDelete(
|
|
|
|
Fts5Storage *p, /* Storage object */
|
|
|
|
i64 iDel, /* Rowid to delete from table */
|
|
|
|
sqlite3_value **apVal, /* Optional - values to remove from index */
|
2024-08-01 20:15:17 +03:00
|
|
|
int bSaveRow /* If true, set pSavedRow for deleted row */
|
2024-07-27 22:46:10 +03:00
|
|
|
){
|
2015-06-09 23:58:39 +03:00
|
|
|
Fts5Config *pConfig = p->pConfig;
|
2014-07-21 19:45:26 +04:00
|
|
|
int rc;
|
2015-07-14 20:18:05 +03:00
|
|
|
sqlite3_stmt *pDel = 0;
|
2014-07-21 19:45:26 +04:00
|
|
|
|
2016-01-15 22:54:47 +03:00
|
|
|
assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
|
2014-08-12 20:07:35 +04:00
|
|
|
rc = fts5StorageLoadTotals(p, 1);
|
2014-07-21 19:45:26 +04:00
|
|
|
|
|
|
|
/* Delete the index records */
|
2023-07-17 21:40:39 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:45:26 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2023-07-10 23:44:09 +03:00
|
|
|
if( p->pConfig->bContentlessDelete ){
|
|
|
|
rc = fts5StorageContentlessDelete(p, iDel);
|
2024-09-27 21:32:52 +03:00
|
|
|
if( rc==SQLITE_OK
|
|
|
|
&& bSaveRow
|
|
|
|
&& p->pConfig->eContent==FTS5_CONTENT_UNINDEXED
|
|
|
|
){
|
|
|
|
rc = sqlite3Fts5StorageFindDeleteRow(p, iDel);
|
|
|
|
}
|
2023-07-10 23:44:09 +03:00
|
|
|
}else{
|
2024-07-27 22:46:10 +03:00
|
|
|
rc = fts5StorageDeleteFromIndex(p, iDel, apVal, bSaveRow);
|
2023-07-10 23:44:09 +03:00
|
|
|
}
|
2014-07-21 19:45:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the %_docsize record */
|
2015-06-09 23:58:39 +03:00
|
|
|
if( rc==SQLITE_OK && pConfig->bColumnsize ){
|
2015-01-05 23:41:39 +03:00
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
|
2015-06-23 18:06:13 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_int64(pDel, 1, iDel);
|
|
|
|
sqlite3_step(pDel);
|
|
|
|
rc = sqlite3_reset(pDel);
|
|
|
|
}
|
2014-07-21 19:45:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the %_content record */
|
2024-09-03 21:55:38 +03:00
|
|
|
if( pConfig->eContent==FTS5_CONTENT_NORMAL
|
|
|
|
|| pConfig->eContent==FTS5_CONTENT_UNINDEXED
|
|
|
|
){
|
2015-10-27 23:04:53 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_int64(pDel, 1, iDel);
|
|
|
|
sqlite3_step(pDel);
|
|
|
|
rc = sqlite3_reset(pDel);
|
|
|
|
}
|
2014-07-21 19:45:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2014-07-21 15:44:47 +04:00
|
|
|
|
2015-01-07 20:11:11 +03:00
|
|
|
/*
|
|
|
|
** Delete all entries in the FTS5 index.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
|
|
|
|
Fts5Config *pConfig = p->pConfig;
|
|
|
|
int rc;
|
|
|
|
|
2019-10-08 16:34:24 +03:00
|
|
|
p->bTotalsValid = 0;
|
|
|
|
|
2015-01-07 20:11:11 +03:00
|
|
|
/* Delete the contents of the %_data and %_docsize tables. */
|
|
|
|
rc = fts5ExecPrintf(pConfig->db, 0,
|
2015-07-27 14:43:36 +03:00
|
|
|
"DELETE FROM %Q.'%q_data';"
|
|
|
|
"DELETE FROM %Q.'%q_idx';",
|
|
|
|
pConfig->zDb, pConfig->zName,
|
2015-01-07 20:11:11 +03:00
|
|
|
pConfig->zDb, pConfig->zName
|
|
|
|
);
|
2015-06-09 23:58:39 +03:00
|
|
|
if( rc==SQLITE_OK && pConfig->bColumnsize ){
|
|
|
|
rc = fts5ExecPrintf(pConfig->db, 0,
|
2024-09-03 21:55:38 +03:00
|
|
|
"DELETE FROM %Q.'%q_docsize';", pConfig->zDb, pConfig->zName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_UNINDEXED ){
|
|
|
|
rc = fts5ExecPrintf(pConfig->db, 0,
|
|
|
|
"DELETE FROM %Q.'%q_content';", pConfig->zDb, pConfig->zName
|
2015-06-09 23:58:39 +03:00
|
|
|
);
|
|
|
|
}
|
2015-01-07 20:11:11 +03:00
|
|
|
|
|
|
|
/* Reinitialize the %_data table. This call creates the initial structure
|
|
|
|
** and averages records. */
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts5IndexReinit(p->pIndex);
|
|
|
|
}
|
2015-05-07 22:29:46 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
|
|
|
|
}
|
2015-01-07 20:11:11 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite3Fts5StorageRebuild(Fts5Storage *p){
|
|
|
|
Fts5Buffer buf = {0,0,0};
|
|
|
|
Fts5Config *pConfig = p->pConfig;
|
|
|
|
sqlite3_stmt *pScan = 0;
|
|
|
|
Fts5InsertCtx ctx;
|
2019-01-05 10:17:56 +03:00
|
|
|
int rc, rc2;
|
2015-01-07 20:11:11 +03:00
|
|
|
|
|
|
|
memset(&ctx, 0, sizeof(Fts5InsertCtx));
|
|
|
|
ctx.pStorage = p;
|
|
|
|
rc = sqlite3Fts5StorageDeleteAll(p);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5StorageLoadTotals(p, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
2023-12-14 18:31:35 +03:00
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, pConfig->pzErrmsg);
|
2015-01-07 20:11:11 +03:00
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
|
2015-01-07 20:11:11 +03:00
|
|
|
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){
|
|
|
|
i64 iRowid = sqlite3_column_int64(pScan, 0);
|
|
|
|
|
|
|
|
sqlite3Fts5BufferZero(&buf);
|
2015-10-02 23:04:30 +03:00
|
|
|
rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
|
2015-01-07 20:11:11 +03:00
|
|
|
for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
|
|
|
|
ctx.szCol = 0;
|
2015-04-24 22:41:43 +03:00
|
|
|
if( pConfig->abUnindexed[ctx.iCol]==0 ){
|
2024-08-01 20:15:17 +03:00
|
|
|
int nText = 0; /* Size of pText in bytes */
|
|
|
|
const char *pText = 0; /* Pointer to buffer containing text value */
|
2024-09-10 19:19:31 +03:00
|
|
|
int nLoc = 0; /* Size of pLoc in bytes */
|
|
|
|
const char *pLoc = 0; /* Pointer to buffer containing text value */
|
|
|
|
|
2024-08-01 20:15:17 +03:00
|
|
|
sqlite3_value *pVal = sqlite3_column_value(pScan, ctx.iCol+1);
|
2024-09-10 19:19:31 +03:00
|
|
|
if( pConfig->eContent==FTS5_CONTENT_EXTERNAL
|
|
|
|
&& sqlite3Fts5IsLocaleValue(pConfig, pVal)
|
|
|
|
){
|
|
|
|
rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
|
|
|
|
}else{
|
|
|
|
pText = (const char*)sqlite3_value_text(pVal);
|
|
|
|
nText = sqlite3_value_bytes(pVal);
|
|
|
|
if( pConfig->bLocale ){
|
|
|
|
int iCol = ctx.iCol + 1 + pConfig->nCol;
|
|
|
|
pLoc = (const char*)sqlite3_column_text(pScan, iCol);
|
|
|
|
nLoc = sqlite3_column_bytes(pScan, iCol);
|
|
|
|
}
|
|
|
|
}
|
2024-07-26 23:50:33 +03:00
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
|
2024-07-26 23:50:33 +03:00
|
|
|
rc = sqlite3Fts5Tokenize(pConfig,
|
|
|
|
FTS5_TOKENIZE_DOCUMENT,
|
|
|
|
pText, nText,
|
|
|
|
(void*)&ctx,
|
|
|
|
fts5StorageInsertCallback
|
|
|
|
);
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5ClearLocale(pConfig);
|
2024-07-26 23:50:33 +03:00
|
|
|
}
|
2015-04-24 22:41:43 +03:00
|
|
|
}
|
2015-01-07 20:11:11 +03:00
|
|
|
sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
|
|
|
|
p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
|
|
|
|
}
|
|
|
|
p->nTotalRow++;
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5StorageInsertDocsize(p, iRowid, &buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_free(buf.p);
|
2019-01-05 10:17:56 +03:00
|
|
|
rc2 = sqlite3_reset(pScan);
|
|
|
|
if( rc==SQLITE_OK ) rc = rc2;
|
2015-01-07 20:11:11 +03:00
|
|
|
|
|
|
|
/* Write the averages record */
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5StorageSaveTotals(p);
|
|
|
|
}
|
|
|
|
return rc;
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
|
|
|
|
2015-01-07 22:33:11 +03:00
|
|
|
int sqlite3Fts5StorageOptimize(Fts5Storage *p){
|
|
|
|
return sqlite3Fts5IndexOptimize(p->pIndex);
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:31:56 +03:00
|
|
|
int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
|
|
|
|
return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
|
|
|
|
}
|
|
|
|
|
2016-03-16 22:48:10 +03:00
|
|
|
int sqlite3Fts5StorageReset(Fts5Storage *p){
|
|
|
|
return sqlite3Fts5IndexReset(p->pIndex);
|
|
|
|
}
|
|
|
|
|
2015-01-03 23:44:58 +03:00
|
|
|
/*
|
|
|
|
** Allocate a new rowid. This is used for "external content" tables when
|
|
|
|
** a NULL value is inserted into the rowid column. The new rowid is allocated
|
|
|
|
** by inserting a dummy row into the %_docsize table. The dummy will be
|
|
|
|
** overwritten later.
|
2015-06-09 23:58:39 +03:00
|
|
|
**
|
|
|
|
** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
|
|
|
|
** this case the user is required to provide a rowid explicitly.
|
2015-01-03 23:44:58 +03:00
|
|
|
*/
|
|
|
|
static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
|
2015-06-09 23:58:39 +03:00
|
|
|
int rc = SQLITE_MISMATCH;
|
|
|
|
if( p->pConfig->bColumnsize ){
|
|
|
|
sqlite3_stmt *pReplace = 0;
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_null(pReplace, 1);
|
|
|
|
sqlite3_bind_null(pReplace, 2);
|
|
|
|
sqlite3_step(pReplace);
|
|
|
|
rc = sqlite3_reset(pReplace);
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
*piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
|
|
|
|
}
|
2015-01-03 23:44:58 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
2015-10-02 23:04:30 +03:00
|
|
|
** Insert a new row into the FTS content table.
|
2014-06-23 15:33:22 +04:00
|
|
|
*/
|
2015-10-02 23:04:30 +03:00
|
|
|
int sqlite3Fts5StorageContentInsert(
|
|
|
|
Fts5Storage *p,
|
2024-09-27 13:57:41 +03:00
|
|
|
int bReplace, /* True to use REPLACE instead of INSERT */
|
2015-10-02 23:04:30 +03:00
|
|
|
sqlite3_value **apVal,
|
|
|
|
i64 *piRowid
|
|
|
|
){
|
|
|
|
Fts5Config *pConfig = p->pConfig;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
|
|
|
/* Insert the new row into the %_content table. */
|
2024-09-03 21:55:38 +03:00
|
|
|
if( pConfig->eContent!=FTS5_CONTENT_NORMAL
|
|
|
|
&& pConfig->eContent!=FTS5_CONTENT_UNINDEXED
|
|
|
|
){
|
2015-10-02 23:04:30 +03:00
|
|
|
if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){
|
|
|
|
*piRowid = sqlite3_value_int64(apVal[1]);
|
|
|
|
}else{
|
|
|
|
rc = fts5StorageNewRowid(p, piRowid);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
|
|
|
|
int i; /* Counter variable */
|
2024-09-27 13:57:41 +03:00
|
|
|
|
|
|
|
assert( FTS5_STMT_INSERT_CONTENT+1==FTS5_STMT_REPLACE_CONTENT );
|
|
|
|
assert( bReplace==0 || bReplace==1 );
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT+bReplace, &pInsert, 0);
|
2024-09-10 19:19:31 +03:00
|
|
|
if( pInsert ) sqlite3_clear_bindings(pInsert);
|
|
|
|
|
|
|
|
/* Bind the rowid value */
|
|
|
|
sqlite3_bind_value(pInsert, 1, apVal[1]);
|
|
|
|
|
|
|
|
/* Loop through values for user-defined columns. i=2 is the leftmost
|
|
|
|
** user-defined column. As is column 1 of pSavedRow. */
|
|
|
|
for(i=2; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
|
|
|
|
int bUnindexed = pConfig->abUnindexed[i-2];
|
2024-09-13 18:37:31 +03:00
|
|
|
if( pConfig->eContent==FTS5_CONTENT_NORMAL || bUnindexed ){
|
2024-09-03 21:55:38 +03:00
|
|
|
sqlite3_value *pVal = apVal[i];
|
2024-09-13 18:37:31 +03:00
|
|
|
|
2024-09-03 21:55:38 +03:00
|
|
|
if( sqlite3_value_nochange(pVal) && p->pSavedRow ){
|
2024-09-13 18:37:31 +03:00
|
|
|
/* This is an UPDATE statement, and user-defined column (i-2) was not
|
|
|
|
** modified. Retrieve the value from Fts5Storage.pSavedRow. */
|
2024-09-03 21:55:38 +03:00
|
|
|
pVal = sqlite3_column_value(p->pSavedRow, i-1);
|
2024-09-13 18:37:31 +03:00
|
|
|
if( pConfig->bLocale && bUnindexed==0 ){
|
|
|
|
sqlite3_bind_value(pInsert, pConfig->nCol + i,
|
|
|
|
sqlite3_column_value(p->pSavedRow, pConfig->nCol + i - 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}else if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
|
|
|
|
const char *pText = 0;
|
|
|
|
const char *pLoc = 0;
|
|
|
|
int nText = 0;
|
|
|
|
int nLoc = 0;
|
2024-09-03 21:55:38 +03:00
|
|
|
assert( pConfig->bLocale );
|
2024-09-10 19:19:31 +03:00
|
|
|
|
2024-09-13 18:37:31 +03:00
|
|
|
rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
|
|
|
|
if( rc==SQLITE_OK ){
|
2024-09-03 21:55:38 +03:00
|
|
|
sqlite3_bind_text(pInsert, i, pText, nText, SQLITE_TRANSIENT);
|
2024-09-13 18:37:31 +03:00
|
|
|
if( bUnindexed==0 ){
|
|
|
|
int iLoc = pConfig->nCol + i;
|
|
|
|
sqlite3_bind_text(pInsert, iLoc, pLoc, nLoc, SQLITE_TRANSIENT);
|
|
|
|
}
|
2024-09-03 21:55:38 +03:00
|
|
|
}
|
2024-09-13 18:37:31 +03:00
|
|
|
|
2024-09-03 21:55:38 +03:00
|
|
|
continue;
|
2024-07-30 23:39:58 +03:00
|
|
|
}
|
2024-09-10 19:19:31 +03:00
|
|
|
|
2024-09-03 21:55:38 +03:00
|
|
|
rc = sqlite3_bind_value(pInsert, i, pVal);
|
2024-07-27 22:46:10 +03:00
|
|
|
}
|
2015-10-02 23:04:30 +03:00
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_step(pInsert);
|
|
|
|
rc = sqlite3_reset(pInsert);
|
|
|
|
}
|
|
|
|
*piRowid = sqlite3_last_insert_rowid(pConfig->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Insert new entries into the FTS index and %_docsize table.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5StorageIndexInsert(
|
|
|
|
Fts5Storage *p,
|
|
|
|
sqlite3_value **apVal,
|
|
|
|
i64 iRowid
|
2014-06-23 15:33:22 +04:00
|
|
|
){
|
|
|
|
Fts5Config *pConfig = p->pConfig;
|
|
|
|
int rc = SQLITE_OK; /* Return code */
|
|
|
|
Fts5InsertCtx ctx; /* Tokenization callback context object */
|
2014-07-19 19:35:09 +04:00
|
|
|
Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
|
|
|
|
|
|
|
|
memset(&buf, 0, sizeof(Fts5Buffer));
|
2015-10-02 23:04:30 +03:00
|
|
|
ctx.pStorage = p;
|
2014-08-12 20:07:35 +04:00
|
|
|
rc = fts5StorageLoadTotals(p, 1);
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2014-12-03 20:27:35 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
2015-10-02 23:04:30 +03:00
|
|
|
rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
|
2014-12-03 20:27:35 +03:00
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
|
2014-07-19 19:35:09 +04:00
|
|
|
ctx.szCol = 0;
|
2015-04-24 22:41:43 +03:00
|
|
|
if( pConfig->abUnindexed[ctx.iCol]==0 ){
|
2024-08-01 20:15:17 +03:00
|
|
|
int nText = 0; /* Size of pText in bytes */
|
|
|
|
const char *pText = 0; /* Pointer to buffer containing text value */
|
2024-09-10 19:19:31 +03:00
|
|
|
int nLoc = 0; /* Size of pText in bytes */
|
|
|
|
const char *pLoc = 0; /* Pointer to buffer containing text value */
|
|
|
|
|
2024-07-27 22:46:10 +03:00
|
|
|
sqlite3_value *pVal = apVal[ctx.iCol+2];
|
|
|
|
if( p->pSavedRow && sqlite3_value_nochange(pVal) ){
|
|
|
|
pVal = sqlite3_column_value(p->pSavedRow, ctx.iCol+1);
|
2024-09-10 19:19:31 +03:00
|
|
|
if( pConfig->eContent==FTS5_CONTENT_NORMAL && pConfig->bLocale ){
|
|
|
|
int iCol = ctx.iCol + 1 + pConfig->nCol;
|
|
|
|
pLoc = (const char*)sqlite3_column_text(p->pSavedRow, iCol);
|
|
|
|
nLoc = sqlite3_column_bytes(p->pSavedRow, iCol);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
pVal = apVal[ctx.iCol+2];
|
2024-07-27 22:46:10 +03:00
|
|
|
}
|
2024-09-10 19:19:31 +03:00
|
|
|
|
|
|
|
if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
|
|
|
|
rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
|
|
|
|
}else{
|
|
|
|
pText = (const char*)sqlite3_value_text(pVal);
|
|
|
|
nText = sqlite3_value_bytes(pVal);
|
2024-07-27 22:46:10 +03:00
|
|
|
}
|
2024-09-10 19:19:31 +03:00
|
|
|
|
2024-07-26 23:50:33 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
|
2024-08-01 20:15:17 +03:00
|
|
|
rc = sqlite3Fts5Tokenize(pConfig,
|
|
|
|
FTS5_TOKENIZE_DOCUMENT, pText, nText, (void*)&ctx,
|
|
|
|
fts5StorageInsertCallback
|
|
|
|
);
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5ClearLocale(pConfig);
|
2024-07-26 23:50:33 +03:00
|
|
|
}
|
2015-04-24 22:41:43 +03:00
|
|
|
}
|
2014-07-19 19:35:09 +04:00
|
|
|
sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
|
2014-07-21 15:44:47 +04:00
|
|
|
p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
|
2014-07-19 19:35:09 +04:00
|
|
|
}
|
2014-07-21 15:44:47 +04:00
|
|
|
p->nTotalRow++;
|
2014-07-19 19:35:09 +04:00
|
|
|
|
|
|
|
/* Write the %_docsize record */
|
|
|
|
if( rc==SQLITE_OK ){
|
2015-10-02 23:04:30 +03:00
|
|
|
rc = fts5StorageInsertDocsize(p, iRowid, &buf);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2014-07-19 19:35:09 +04:00
|
|
|
sqlite3_free(buf.p);
|
2014-06-23 15:33:22 +04:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-07-23 23:31:56 +04:00
|
|
|
static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
|
|
|
|
Fts5Config *pConfig = p->pConfig;
|
|
|
|
char *zSql;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
|
|
|
|
pConfig->zDb, pConfig->zName, zSuffix
|
|
|
|
);
|
|
|
|
if( zSql==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
sqlite3_stmt *pCnt = 0;
|
|
|
|
rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
|
2015-01-21 23:30:14 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
if( SQLITE_ROW==sqlite3_step(pCnt) ){
|
|
|
|
*pnRow = sqlite3_column_int64(pCnt, 0);
|
|
|
|
}
|
|
|
|
rc = sqlite3_finalize(pCnt);
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_free(zSql);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** Context object used by sqlite3Fts5StorageIntegrity().
|
|
|
|
*/
|
|
|
|
typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
|
|
|
|
struct Fts5IntegrityCtx {
|
|
|
|
i64 iRowid;
|
|
|
|
int iCol;
|
2014-07-23 23:31:56 +04:00
|
|
|
int szCol;
|
2014-06-23 15:33:22 +04:00
|
|
|
u64 cksum;
|
2015-12-17 23:36:13 +03:00
|
|
|
Fts5Termset *pTermset;
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5Config *pConfig;
|
|
|
|
};
|
|
|
|
|
2015-12-21 21:45:09 +03:00
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/*
|
|
|
|
** Tokenization callback used by integrity check.
|
|
|
|
*/
|
|
|
|
static int fts5StorageIntegrityCallback(
|
2015-12-17 23:36:13 +03:00
|
|
|
void *pContext, /* Pointer to Fts5IntegrityCtx object */
|
2015-08-29 18:44:27 +03:00
|
|
|
int tflags,
|
2014-06-23 15:33:22 +04:00
|
|
|
const char *pToken, /* Buffer containing token */
|
|
|
|
int nToken, /* Size of token in bytes */
|
2016-02-11 20:01:32 +03:00
|
|
|
int iUnused1, /* Start offset of token */
|
|
|
|
int iUnused2 /* End offset of token */
|
2014-06-23 15:33:22 +04:00
|
|
|
){
|
|
|
|
Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
|
2015-12-21 21:45:09 +03:00
|
|
|
Fts5Termset *pTermset = pCtx->pTermset;
|
|
|
|
int bPresent;
|
|
|
|
int ii;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
int iPos;
|
|
|
|
int iCol;
|
|
|
|
|
2016-02-11 20:01:32 +03:00
|
|
|
UNUSED_PARAM2(iUnused1, iUnused2);
|
2016-03-23 18:04:00 +03:00
|
|
|
if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
|
2016-02-11 20:01:32 +03:00
|
|
|
|
2015-08-29 21:46:12 +03:00
|
|
|
if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
|
2015-08-29 18:44:27 +03:00
|
|
|
pCtx->szCol++;
|
|
|
|
}
|
2015-12-17 23:36:13 +03:00
|
|
|
|
2015-12-31 21:39:14 +03:00
|
|
|
switch( pCtx->pConfig->eDetail ){
|
|
|
|
case FTS5_DETAIL_FULL:
|
|
|
|
iPos = pCtx->szCol-1;
|
|
|
|
iCol = pCtx->iCol;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FTS5_DETAIL_COLUMNS:
|
|
|
|
iPos = pCtx->iCol;
|
|
|
|
iCol = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
|
|
|
|
iPos = 0;
|
|
|
|
iCol = 0;
|
|
|
|
break;
|
|
|
|
}
|
2015-12-21 21:45:09 +03:00
|
|
|
|
|
|
|
rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
|
|
|
|
if( rc==SQLITE_OK && bPresent==0 ){
|
|
|
|
pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
|
|
|
|
pCtx->iRowid, iCol, iPos, 0, pToken, nToken
|
2015-12-17 23:36:13 +03:00
|
|
|
);
|
|
|
|
}
|
2015-12-21 21:45:09 +03:00
|
|
|
|
|
|
|
for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
|
|
|
|
const int nChar = pCtx->pConfig->aPrefix[ii];
|
|
|
|
int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
|
|
|
|
if( nByte ){
|
|
|
|
rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
|
|
|
|
if( bPresent==0 ){
|
|
|
|
pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
|
|
|
|
pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 23:36:13 +03:00
|
|
|
return rc;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Check that the contents of the FTS index match that of the %_content
|
|
|
|
** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
|
|
|
|
** some other SQLite error code if an error occurs while attempting to
|
|
|
|
** determine this.
|
|
|
|
*/
|
2020-09-21 17:53:21 +03:00
|
|
|
int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5Config *pConfig = p->pConfig;
|
2020-09-21 17:53:21 +03:00
|
|
|
int rc = SQLITE_OK; /* Return code */
|
2014-07-23 23:31:56 +04:00
|
|
|
int *aColSize; /* Array of size pConfig->nCol */
|
|
|
|
i64 *aTotalSize; /* Array of size pConfig->nCol */
|
2014-06-23 15:33:22 +04:00
|
|
|
Fts5IntegrityCtx ctx;
|
|
|
|
sqlite3_stmt *pScan;
|
2020-09-21 17:53:21 +03:00
|
|
|
int bUseCksum;
|
2014-06-23 15:33:22 +04:00
|
|
|
|
|
|
|
memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
|
|
|
|
ctx.pConfig = p->pConfig;
|
2019-01-08 23:02:48 +03:00
|
|
|
aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
|
2014-07-23 23:31:56 +04:00
|
|
|
if( !aTotalSize ) return SQLITE_NOMEM;
|
|
|
|
aColSize = (int*)&aTotalSize[pConfig->nCol];
|
|
|
|
memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2020-09-21 17:53:21 +03:00
|
|
|
bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL
|
|
|
|
|| (pConfig->eContent==FTS5_CONTENT_EXTERNAL && iArg)
|
|
|
|
);
|
|
|
|
if( bUseCksum ){
|
|
|
|
/* Generate the expected index checksum based on the contents of the
|
|
|
|
** %_content table. This block stores the checksum in ctx.cksum. */
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
int rc2;
|
|
|
|
while( SQLITE_ROW==sqlite3_step(pScan) ){
|
|
|
|
int i;
|
|
|
|
ctx.iRowid = sqlite3_column_int64(pScan, 0);
|
2015-01-06 22:08:26 +03:00
|
|
|
ctx.szCol = 0;
|
2020-09-21 17:53:21 +03:00
|
|
|
if( pConfig->bColumnsize ){
|
|
|
|
rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
|
2015-12-17 23:36:13 +03:00
|
|
|
}
|
2020-09-21 17:53:21 +03:00
|
|
|
if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
|
|
|
|
rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
|
2015-09-03 14:00:50 +03:00
|
|
|
}
|
2020-09-21 17:53:21 +03:00
|
|
|
for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
|
2024-09-10 19:19:31 +03:00
|
|
|
if( pConfig->abUnindexed[i]==0 ){
|
|
|
|
const char *pText = 0;
|
|
|
|
int nText = 0;
|
|
|
|
const char *pLoc = 0;
|
|
|
|
int nLoc = 0;
|
|
|
|
sqlite3_value *pVal = sqlite3_column_value(pScan, i+1);
|
|
|
|
|
|
|
|
if( pConfig->eContent==FTS5_CONTENT_EXTERNAL
|
|
|
|
&& sqlite3Fts5IsLocaleValue(pConfig, pVal)
|
|
|
|
){
|
|
|
|
rc = sqlite3Fts5DecodeLocaleValue(
|
|
|
|
pVal, &pText, &nText, &pLoc, &nLoc
|
|
|
|
);
|
|
|
|
}else{
|
|
|
|
if( pConfig->eContent==FTS5_CONTENT_NORMAL && pConfig->bLocale ){
|
|
|
|
int iCol = i + 1 + pConfig->nCol;
|
|
|
|
pLoc = (const char*)sqlite3_column_text(pScan, iCol);
|
|
|
|
nLoc = sqlite3_column_bytes(pScan, iCol);
|
|
|
|
}
|
|
|
|
pText = (const char*)sqlite3_value_text(pVal);
|
|
|
|
nText = sqlite3_value_bytes(pVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.iCol = i;
|
|
|
|
ctx.szCol = 0;
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
|
|
|
|
rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
|
|
|
|
}
|
2024-07-26 23:50:33 +03:00
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
|
2024-07-26 23:50:33 +03:00
|
|
|
rc = sqlite3Fts5Tokenize(pConfig,
|
|
|
|
FTS5_TOKENIZE_DOCUMENT,
|
|
|
|
pText, nText,
|
|
|
|
(void*)&ctx,
|
|
|
|
fts5StorageIntegrityCallback
|
|
|
|
);
|
2024-09-10 19:19:31 +03:00
|
|
|
sqlite3Fts5ClearLocale(pConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is not a columnsize=0 database, check that the number
|
|
|
|
** of tokens in the value matches the aColSize[] value read from
|
|
|
|
** the %_docsize table. */
|
|
|
|
if( rc==SQLITE_OK
|
|
|
|
&& pConfig->bColumnsize
|
|
|
|
&& ctx.szCol!=aColSize[i]
|
|
|
|
){
|
|
|
|
rc = FTS5_CORRUPT;
|
|
|
|
}
|
|
|
|
aTotalSize[i] += ctx.szCol;
|
|
|
|
if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
|
|
|
|
sqlite3Fts5TermsetFree(ctx.pTermset);
|
|
|
|
ctx.pTermset = 0;
|
2024-07-26 23:50:33 +03:00
|
|
|
}
|
2020-09-21 17:53:21 +03:00
|
|
|
}
|
2015-12-31 21:39:14 +03:00
|
|
|
}
|
2020-09-21 17:53:21 +03:00
|
|
|
sqlite3Fts5TermsetFree(ctx.pTermset);
|
|
|
|
ctx.pTermset = 0;
|
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ) break;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
2020-09-21 17:53:21 +03:00
|
|
|
rc2 = sqlite3_reset(pScan);
|
|
|
|
if( rc==SQLITE_OK ) rc = rc2;
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2020-09-21 17:53:21 +03:00
|
|
|
/* Test that the "totals" (sometimes called "averages") record looks Ok */
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
int i;
|
|
|
|
rc = fts5StorageLoadTotals(p, 0);
|
|
|
|
for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
|
|
|
|
if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
|
|
|
|
}
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
|
|
|
|
2020-09-21 17:53:21 +03:00
|
|
|
/* Check that the %_docsize and %_content tables contain the expected
|
|
|
|
** number of rows. */
|
|
|
|
if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
|
|
|
|
i64 nRow = 0;
|
|
|
|
rc = fts5StorageCount(p, "content", &nRow);
|
|
|
|
if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK && pConfig->bColumnsize ){
|
|
|
|
i64 nRow = 0;
|
|
|
|
rc = fts5StorageCount(p, "docsize", &nRow);
|
|
|
|
if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
|
|
|
|
}
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 15:33:22 +04:00
|
|
|
/* Pass the expected checksum down to the FTS index module. It will
|
|
|
|
** verify, amongst other things, that it matches the checksum generated by
|
|
|
|
** inspecting the index itself. */
|
|
|
|
if( rc==SQLITE_OK ){
|
2020-09-21 17:53:21 +03:00
|
|
|
rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum, bUseCksum);
|
2014-06-23 15:33:22 +04:00
|
|
|
}
|
|
|
|
|
2014-07-23 23:31:56 +04:00
|
|
|
sqlite3_free(aTotalSize);
|
2014-06-23 15:33:22 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:59:06 +04:00
|
|
|
/*
|
|
|
|
** Obtain an SQLite statement handle that may be used to read data from the
|
|
|
|
** %_content table.
|
|
|
|
*/
|
2015-01-05 23:41:39 +03:00
|
|
|
int sqlite3Fts5StorageStmt(
|
|
|
|
Fts5Storage *p,
|
|
|
|
int eStmt,
|
|
|
|
sqlite3_stmt **pp,
|
|
|
|
char **pzErrMsg
|
|
|
|
){
|
2014-06-24 20:59:06 +04:00
|
|
|
int rc;
|
|
|
|
assert( eStmt==FTS5_STMT_SCAN_ASC
|
|
|
|
|| eStmt==FTS5_STMT_SCAN_DESC
|
|
|
|
|| eStmt==FTS5_STMT_LOOKUP
|
|
|
|
);
|
2015-01-05 23:41:39 +03:00
|
|
|
rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
|
2014-06-24 20:59:06 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
assert( p->aStmt[eStmt]==*pp );
|
|
|
|
p->aStmt[eStmt] = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Release an SQLite statement handle obtained via an earlier call to
|
|
|
|
** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
|
|
|
|
** must match that passed to the sqlite3Fts5StorageStmt() call.
|
|
|
|
*/
|
|
|
|
void sqlite3Fts5StorageStmtRelease(
|
|
|
|
Fts5Storage *p,
|
|
|
|
int eStmt,
|
|
|
|
sqlite3_stmt *pStmt
|
|
|
|
){
|
|
|
|
assert( eStmt==FTS5_STMT_SCAN_ASC
|
|
|
|
|| eStmt==FTS5_STMT_SCAN_DESC
|
|
|
|
|| eStmt==FTS5_STMT_LOOKUP
|
|
|
|
);
|
|
|
|
if( p->aStmt[eStmt]==0 ){
|
|
|
|
sqlite3_reset(pStmt);
|
|
|
|
p->aStmt[eStmt] = pStmt;
|
|
|
|
}else{
|
|
|
|
sqlite3_finalize(pStmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 19:35:09 +04:00
|
|
|
static int fts5StorageDecodeSizeArray(
|
|
|
|
int *aCol, int nCol, /* Array to populate */
|
|
|
|
const u8 *aBlob, int nBlob /* Record to read varints from */
|
|
|
|
){
|
|
|
|
int i;
|
|
|
|
int iOff = 0;
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
if( iOff>=nBlob ) return 1;
|
2015-05-30 14:49:58 +03:00
|
|
|
iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
|
2014-07-19 19:35:09 +04:00
|
|
|
}
|
|
|
|
return (iOff!=nBlob);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Argument aCol points to an array of integers containing one entry for
|
|
|
|
** each table column. This function reads the %_docsize record for the
|
|
|
|
** specified rowid and populates aCol[] with the results.
|
|
|
|
**
|
|
|
|
** An SQLite error code is returned if an error occurs, or SQLITE_OK
|
|
|
|
** otherwise.
|
|
|
|
*/
|
|
|
|
int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
|
2015-09-03 14:00:50 +03:00
|
|
|
int nCol = p->pConfig->nCol; /* Number of user columns in table */
|
|
|
|
sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
|
|
|
|
int rc; /* Return Code */
|
|
|
|
|
|
|
|
assert( p->pConfig->bColumnsize );
|
|
|
|
rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
|
2021-10-05 20:41:12 +03:00
|
|
|
if( pLookup ){
|
2014-07-19 19:35:09 +04:00
|
|
|
int bCorrupt = 1;
|
2021-10-05 20:41:12 +03:00
|
|
|
assert( rc==SQLITE_OK );
|
2014-07-19 19:35:09 +04:00
|
|
|
sqlite3_bind_int64(pLookup, 1, iRowid);
|
|
|
|
if( SQLITE_ROW==sqlite3_step(pLookup) ){
|
|
|
|
const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
|
|
|
|
int nBlob = sqlite3_column_bytes(pLookup, 0);
|
|
|
|
if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
|
|
|
|
bCorrupt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc = sqlite3_reset(pLookup);
|
|
|
|
if( bCorrupt && rc==SQLITE_OK ){
|
2015-01-23 20:43:21 +03:00
|
|
|
rc = FTS5_CORRUPT;
|
2014-07-19 19:35:09 +04:00
|
|
|
}
|
2021-10-05 20:41:12 +03:00
|
|
|
}else{
|
|
|
|
assert( rc!=SQLITE_OK );
|
2014-07-19 19:35:09 +04:00
|
|
|
}
|
2015-06-09 23:58:39 +03:00
|
|
|
|
2014-07-19 19:35:09 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-07-26 00:30:47 +04:00
|
|
|
int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
|
2014-08-12 20:07:35 +04:00
|
|
|
int rc = fts5StorageLoadTotals(p, 0);
|
2014-07-21 15:44:47 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2014-12-23 22:18:34 +03:00
|
|
|
*pnToken = 0;
|
|
|
|
if( iCol<0 ){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<p->pConfig->nCol; i++){
|
|
|
|
*pnToken += p->aTotalSize[i];
|
|
|
|
}
|
|
|
|
}else if( iCol<p->pConfig->nCol ){
|
|
|
|
*pnToken = p->aTotalSize[iCol];
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_RANGE;
|
|
|
|
}
|
2014-07-26 00:30:47 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
|
2014-08-12 20:07:35 +04:00
|
|
|
int rc = fts5StorageLoadTotals(p, 0);
|
2014-07-26 00:30:47 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2019-01-19 18:55:27 +03:00
|
|
|
/* nTotalRow being zero does not necessarily indicate a corrupt
|
|
|
|
** database - it might be that the FTS5 table really does contain zero
|
|
|
|
** rows. However this function is only called from the xRowCount() API,
|
|
|
|
** and there is no way for that API to be invoked if the table contains
|
|
|
|
** no rows. Hence the FTS5_CORRUPT return. */
|
2014-07-26 00:30:47 +04:00
|
|
|
*pnRow = p->nTotalRow;
|
2019-01-19 18:55:27 +03:00
|
|
|
if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT;
|
2014-07-21 15:44:47 +04:00
|
|
|
}
|
|
|
|
return rc;
|
2014-07-19 19:35:09 +04:00
|
|
|
}
|
2014-06-23 15:33:22 +04:00
|
|
|
|
2014-08-12 20:07:35 +04:00
|
|
|
/*
|
|
|
|
** Flush any data currently held in-memory to disk.
|
|
|
|
*/
|
2017-04-08 12:12:20 +03:00
|
|
|
int sqlite3Fts5StorageSync(Fts5Storage *p){
|
2017-02-27 17:52:48 +03:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
|
2017-02-27 20:06:56 +03:00
|
|
|
if( p->bTotalsValid ){
|
2017-02-27 17:52:48 +03:00
|
|
|
rc = fts5StorageSaveTotals(p);
|
2023-10-20 22:59:12 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
p->bTotalsValid = 0;
|
|
|
|
}
|
2014-08-12 20:07:35 +04:00
|
|
|
}
|
2017-02-27 17:52:48 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
2017-04-08 12:12:20 +03:00
|
|
|
rc = sqlite3Fts5IndexSync(p->pIndex);
|
2017-02-27 17:52:48 +03:00
|
|
|
}
|
|
|
|
sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
|
|
|
|
return rc;
|
2014-08-12 20:07:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite3Fts5StorageRollback(Fts5Storage *p){
|
|
|
|
p->bTotalsValid = 0;
|
|
|
|
return sqlite3Fts5IndexRollback(p->pIndex);
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:03:45 +03:00
|
|
|
int sqlite3Fts5StorageConfigValue(
|
|
|
|
Fts5Storage *p,
|
2015-05-07 22:29:46 +03:00
|
|
|
const char *z,
|
|
|
|
sqlite3_value *pVal,
|
|
|
|
int iVal
|
2014-11-27 23:03:45 +03:00
|
|
|
){
|
|
|
|
sqlite3_stmt *pReplace = 0;
|
2015-01-05 23:41:39 +03:00
|
|
|
int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0);
|
2014-11-27 23:03:45 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
2015-01-21 23:30:14 +03:00
|
|
|
sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC);
|
2015-05-07 22:29:46 +03:00
|
|
|
if( pVal ){
|
|
|
|
sqlite3_bind_value(pReplace, 2, pVal);
|
|
|
|
}else{
|
|
|
|
sqlite3_bind_int(pReplace, 2, iVal);
|
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
sqlite3_step(pReplace);
|
|
|
|
rc = sqlite3_reset(pReplace);
|
2018-02-07 21:02:50 +03:00
|
|
|
sqlite3_bind_null(pReplace, 1);
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
2015-05-07 22:29:46 +03:00
|
|
|
if( rc==SQLITE_OK && pVal ){
|
2014-11-28 23:01:13 +03:00
|
|
|
int iNew = p->pConfig->iCookie + 1;
|
|
|
|
rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
p->pConfig->iCookie = iNew;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
return rc;
|
|
|
|
}
|