| 1 |
/* |
| 2 |
** 2011 Jan 27 |
| 3 |
** |
| 4 |
** The author disclaims copyright to this source code. In place of |
| 5 |
** a legal notice, here is a blessing: |
| 6 |
** |
| 7 |
** May you do good and not evil. |
| 8 |
** May you find forgiveness for yourself and forgive others. |
| 9 |
** May you share freely, never taking more than you give. |
| 10 |
** |
| 11 |
****************************************************************************** |
| 12 |
** |
| 13 |
** This file is not part of the production FTS code. It is only used for |
| 14 |
** testing. It contains a virtual table implementation that provides direct |
| 15 |
** access to the full-text index of an FTS table. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "fts3Int.h" |
| 19 |
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 20 |
#ifdef SQLITE_TEST |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
#include <assert.h> |
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
typedef struct Fts3termTable Fts3termTable; |
| 27 |
typedef struct Fts3termCursor Fts3termCursor; |
| 28 |
|
| 29 |
struct Fts3termTable { |
| 30 |
sqlite3_vtab base; /* Base class used by SQLite core */ |
| 31 |
int iIndex; /* Index for Fts3Table.aIndex[] */ |
| 32 |
Fts3Table *pFts3Tab; |
| 33 |
}; |
| 34 |
|
| 35 |
struct Fts3termCursor { |
| 36 |
sqlite3_vtab_cursor base; /* Base class used by SQLite core */ |
| 37 |
Fts3MultiSegReader csr; /* Must be right after "base" */ |
| 38 |
Fts3SegFilter filter; |
| 39 |
|
| 40 |
int isEof; /* True if cursor is at EOF */ |
| 41 |
char *pNext; |
| 42 |
|
| 43 |
sqlite3_int64 iRowid; /* Current 'rowid' value */ |
| 44 |
sqlite3_int64 iDocid; /* Current 'docid' value */ |
| 45 |
int iCol; /* Current 'col' value */ |
| 46 |
int iPos; /* Current 'pos' value */ |
| 47 |
}; |
| 48 |
|
| 49 |
/* |
| 50 |
** Schema of the terms table. |
| 51 |
*/ |
| 52 |
#define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, docid, col, pos)" |
| 53 |
|
| 54 |
/* |
| 55 |
** This function does all the work for both the xConnect and xCreate methods. |
| 56 |
** These tables have no persistent representation of their own, so xConnect |
| 57 |
** and xCreate are identical operations. |
| 58 |
*/ |
| 59 |
static int fts3termConnectMethod( |
| 60 |
sqlite3 *db, /* Database connection */ |
| 61 |
void *pCtx, /* Non-zero for an fts4prefix table */ |
| 62 |
int argc, /* Number of elements in argv array */ |
| 63 |
const char * const *argv, /* xCreate/xConnect argument array */ |
| 64 |
sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ |
| 65 |
char **pzErr /* OUT: sqlite3_malloc'd error message */ |
| 66 |
){ |
| 67 |
char const *zDb; /* Name of database (e.g. "main") */ |
| 68 |
char const *zFts3; /* Name of fts3 table */ |
| 69 |
int nDb; /* Result of strlen(zDb) */ |
| 70 |
int nFts3; /* Result of strlen(zFts3) */ |
| 71 |
sqlite3_int64 nByte; /* Bytes of space to allocate here */ |
| 72 |
int rc; /* value returned by declare_vtab() */ |
| 73 |
Fts3termTable *p; /* Virtual table object to return */ |
| 74 |
int iIndex = 0; |
| 75 |
|
| 76 |
UNUSED_PARAMETER(pCtx); |
| 77 |
if( argc==5 ){ |
| 78 |
iIndex = atoi(argv[4]); |
| 79 |
argc--; |
| 80 |
} |
| 81 |
|
| 82 |
*ppVtab = 0; |
| 83 |
|
| 84 |
/* The user should specify a single argument - the name of an fts3 table. */ |
| 85 |
if( argc!=4 ){ |
| 86 |
sqlite3Fts3ErrMsg(pzErr, |
| 87 |
"wrong number of arguments to fts4term constructor" |
| 88 |
); |
| 89 |
return SQLITE_ERROR; |
| 90 |
} |
| 91 |
|
| 92 |
zDb = argv[1]; |
| 93 |
nDb = (int)strlen(zDb); |
| 94 |
zFts3 = argv[3]; |
| 95 |
nFts3 = (int)strlen(zFts3); |
| 96 |
|
| 97 |
rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA); |
| 98 |
if( rc!=SQLITE_OK ) return rc; |
| 99 |
|
| 100 |
nByte = sizeof(Fts3termTable); |
| 101 |
p = (Fts3termTable *)sqlite3Fts3MallocZero(nByte); |
| 102 |
if( !p ) return SQLITE_NOMEM; |
| 103 |
|
| 104 |
p->pFts3Tab = (Fts3Table*)sqlite3Fts3MallocZero( |
| 105 |
sizeof(Fts3Table) + nDb + nFts3 + 2 |
| 106 |
); |
| 107 |
if( p->pFts3Tab==0 ){ |
| 108 |
sqlite3_free(p); |
| 109 |
return SQLITE_NOMEM; |
| 110 |
} |
| 111 |
p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1]; |
| 112 |
p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1]; |
| 113 |
p->pFts3Tab->db = db; |
| 114 |
p->pFts3Tab->nIndex = iIndex+1; |
| 115 |
p->iIndex = iIndex; |
| 116 |
|
| 117 |
memcpy((char *)p->pFts3Tab->zDb, zDb, nDb); |
| 118 |
memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3); |
| 119 |
sqlite3Fts3Dequote((char *)p->pFts3Tab->zName); |
| 120 |
|
| 121 |
*ppVtab = (sqlite3_vtab *)p; |
| 122 |
return SQLITE_OK; |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
** This function does the work for both the xDisconnect and xDestroy methods. |
| 127 |
** These tables have no persistent representation of their own, so xDisconnect |
| 128 |
** and xDestroy are identical operations. |
| 129 |
*/ |
| 130 |
static int fts3termDisconnectMethod(sqlite3_vtab *pVtab){ |
| 131 |
Fts3termTable *p = (Fts3termTable *)pVtab; |
| 132 |
Fts3Table *pFts3 = p->pFts3Tab; |
| 133 |
int i; |
| 134 |
|
| 135 |
/* Free any prepared statements held */ |
| 136 |
for(i=0; i<SizeofArray(pFts3->aStmt); i++){ |
| 137 |
sqlite3_finalize(pFts3->aStmt[i]); |
| 138 |
} |
| 139 |
sqlite3_free(pFts3->zSegmentsTbl); |
| 140 |
sqlite3_free(pFts3); |
| 141 |
sqlite3_free(p); |
| 142 |
return SQLITE_OK; |
| 143 |
} |
| 144 |
|
| 145 |
#define FTS4AUX_EQ_CONSTRAINT 1 |
| 146 |
#define FTS4AUX_GE_CONSTRAINT 2 |
| 147 |
#define FTS4AUX_LE_CONSTRAINT 4 |
| 148 |
|
| 149 |
/* |
| 150 |
** xBestIndex - Analyze a WHERE and ORDER BY clause. |
| 151 |
*/ |
| 152 |
static int fts3termBestIndexMethod( |
| 153 |
sqlite3_vtab *pVTab, |
| 154 |
sqlite3_index_info *pInfo |
| 155 |
){ |
| 156 |
UNUSED_PARAMETER(pVTab); |
| 157 |
|
| 158 |
/* This vtab naturally does "ORDER BY term, docid, col, pos". */ |
| 159 |
if( pInfo->nOrderBy ){ |
| 160 |
int i; |
| 161 |
for(i=0; i<pInfo->nOrderBy; i++){ |
| 162 |
if( pInfo->aOrderBy[i].iColumn!=i || pInfo->aOrderBy[i].desc ) break; |
| 163 |
} |
| 164 |
if( i==pInfo->nOrderBy ){ |
| 165 |
pInfo->orderByConsumed = 1; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return SQLITE_OK; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
** xOpen - Open a cursor. |
| 174 |
*/ |
| 175 |
static int fts3termOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ |
| 176 |
Fts3termCursor *pCsr; /* Pointer to cursor object to return */ |
| 177 |
|
| 178 |
UNUSED_PARAMETER(pVTab); |
| 179 |
|
| 180 |
pCsr = (Fts3termCursor *)sqlite3_malloc(sizeof(Fts3termCursor)); |
| 181 |
if( !pCsr ) return SQLITE_NOMEM; |
| 182 |
memset(pCsr, 0, sizeof(Fts3termCursor)); |
| 183 |
|
| 184 |
*ppCsr = (sqlite3_vtab_cursor *)pCsr; |
| 185 |
return SQLITE_OK; |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
** xClose - Close a cursor. |
| 190 |
*/ |
| 191 |
static int fts3termCloseMethod(sqlite3_vtab_cursor *pCursor){ |
| 192 |
Fts3Table *pFts3 = ((Fts3termTable *)pCursor->pVtab)->pFts3Tab; |
| 193 |
Fts3termCursor *pCsr = (Fts3termCursor *)pCursor; |
| 194 |
|
| 195 |
sqlite3Fts3SegmentsClose(pFts3); |
| 196 |
sqlite3Fts3SegReaderFinish(&pCsr->csr); |
| 197 |
sqlite3_free(pCsr); |
| 198 |
return SQLITE_OK; |
| 199 |
} |
| 200 |
|
| 201 |
/* |
| 202 |
** xNext - Advance the cursor to the next row, if any. |
| 203 |
*/ |
| 204 |
static int fts3termNextMethod(sqlite3_vtab_cursor *pCursor){ |
| 205 |
Fts3termCursor *pCsr = (Fts3termCursor *)pCursor; |
| 206 |
Fts3Table *pFts3 = ((Fts3termTable *)pCursor->pVtab)->pFts3Tab; |
| 207 |
int rc; |
| 208 |
sqlite3_int64 v; |
| 209 |
|
| 210 |
/* Increment our pretend rowid value. */ |
| 211 |
pCsr->iRowid++; |
| 212 |
|
| 213 |
/* Advance to the next term in the full-text index. */ |
| 214 |
if( pCsr->csr.aDoclist==0 |
| 215 |
|| pCsr->pNext>=&pCsr->csr.aDoclist[pCsr->csr.nDoclist-1] |
| 216 |
){ |
| 217 |
rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr); |
| 218 |
if( rc!=SQLITE_ROW ){ |
| 219 |
pCsr->isEof = 1; |
| 220 |
return rc; |
| 221 |
} |
| 222 |
|
| 223 |
pCsr->iCol = 0; |
| 224 |
pCsr->iPos = 0; |
| 225 |
pCsr->iDocid = 0; |
| 226 |
pCsr->pNext = pCsr->csr.aDoclist; |
| 227 |
|
| 228 |
/* Read docid */ |
| 229 |
pCsr->pNext += sqlite3Fts3GetVarint(pCsr->pNext, &pCsr->iDocid); |
| 230 |
} |
| 231 |
|
| 232 |
pCsr->pNext += sqlite3Fts3GetVarint(pCsr->pNext, &v); |
| 233 |
if( v==0 ){ |
| 234 |
pCsr->pNext += sqlite3Fts3GetVarint(pCsr->pNext, &v); |
| 235 |
pCsr->iDocid += v; |
| 236 |
pCsr->pNext += sqlite3Fts3GetVarint(pCsr->pNext, &v); |
| 237 |
pCsr->iCol = 0; |
| 238 |
pCsr->iPos = 0; |
| 239 |
} |
| 240 |
|
| 241 |
if( v==1 ){ |
| 242 |
pCsr->pNext += sqlite3Fts3GetVarint(pCsr->pNext, &v); |
| 243 |
pCsr->iCol += (int)v; |
| 244 |
pCsr->iPos = 0; |
| 245 |
pCsr->pNext += sqlite3Fts3GetVarint(pCsr->pNext, &v); |
| 246 |
} |
| 247 |
|
| 248 |
pCsr->iPos += (int)(v - 2); |
| 249 |
|
| 250 |
return SQLITE_OK; |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
** xFilter - Initialize a cursor to point at the start of its data. |
| 255 |
*/ |
| 256 |
static int fts3termFilterMethod( |
| 257 |
sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ |
| 258 |
int idxNum, /* Strategy index */ |
| 259 |
const char *idxStr, /* Unused */ |
| 260 |
int nVal, /* Number of elements in apVal */ |
| 261 |
sqlite3_value **apVal /* Arguments for the indexing scheme */ |
| 262 |
){ |
| 263 |
Fts3termCursor *pCsr = (Fts3termCursor *)pCursor; |
| 264 |
Fts3termTable *p = (Fts3termTable *)pCursor->pVtab; |
| 265 |
Fts3Table *pFts3 = p->pFts3Tab; |
| 266 |
int rc; |
| 267 |
|
| 268 |
UNUSED_PARAMETER(nVal); |
| 269 |
UNUSED_PARAMETER(idxNum); |
| 270 |
UNUSED_PARAMETER(idxStr); |
| 271 |
UNUSED_PARAMETER(apVal); |
| 272 |
|
| 273 |
assert( idxStr==0 && idxNum==0 ); |
| 274 |
|
| 275 |
/* In case this cursor is being reused, close and zero it. */ |
| 276 |
testcase(pCsr->filter.zTerm); |
| 277 |
sqlite3Fts3SegReaderFinish(&pCsr->csr); |
| 278 |
memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr); |
| 279 |
|
| 280 |
pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY; |
| 281 |
pCsr->filter.flags |= FTS3_SEGMENT_SCAN; |
| 282 |
|
| 283 |
rc = sqlite3Fts3SegReaderCursor(pFts3, 0, p->iIndex, FTS3_SEGCURSOR_ALL, |
| 284 |
pCsr->filter.zTerm, pCsr->filter.nTerm, 0, 1, &pCsr->csr |
| 285 |
); |
| 286 |
if( rc==SQLITE_OK ){ |
| 287 |
rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter); |
| 288 |
} |
| 289 |
if( rc==SQLITE_OK ){ |
| 290 |
rc = fts3termNextMethod(pCursor); |
| 291 |
} |
| 292 |
return rc; |
| 293 |
} |
| 294 |
|
| 295 |
/* |
| 296 |
** xEof - Return true if the cursor is at EOF, or false otherwise. |
| 297 |
*/ |
| 298 |
static int fts3termEofMethod(sqlite3_vtab_cursor *pCursor){ |
| 299 |
Fts3termCursor *pCsr = (Fts3termCursor *)pCursor; |
| 300 |
return pCsr->isEof; |
| 301 |
} |
| 302 |
|
| 303 |
/* |
| 304 |
** xColumn - Return a column value. |
| 305 |
*/ |
| 306 |
static int fts3termColumnMethod( |
| 307 |
sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ |
| 308 |
sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ |
| 309 |
int iCol /* Index of column to read value from */ |
| 310 |
){ |
| 311 |
Fts3termCursor *p = (Fts3termCursor *)pCursor; |
| 312 |
|
| 313 |
assert( iCol>=0 && iCol<=3 ); |
| 314 |
switch( iCol ){ |
| 315 |
case 0: |
| 316 |
sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT); |
| 317 |
break; |
| 318 |
case 1: |
| 319 |
sqlite3_result_int64(pCtx, p->iDocid); |
| 320 |
break; |
| 321 |
case 2: |
| 322 |
sqlite3_result_int64(pCtx, p->iCol); |
| 323 |
break; |
| 324 |
default: |
| 325 |
sqlite3_result_int64(pCtx, p->iPos); |
| 326 |
break; |
| 327 |
} |
| 328 |
|
| 329 |
return SQLITE_OK; |
| 330 |
} |
| 331 |
|
| 332 |
/* |
| 333 |
** xRowid - Return the current rowid for the cursor. |
| 334 |
*/ |
| 335 |
static int fts3termRowidMethod( |
| 336 |
sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ |
| 337 |
sqlite_int64 *pRowid /* OUT: Rowid value */ |
| 338 |
){ |
| 339 |
Fts3termCursor *pCsr = (Fts3termCursor *)pCursor; |
| 340 |
*pRowid = pCsr->iRowid; |
| 341 |
return SQLITE_OK; |
| 342 |
} |
| 343 |
|
| 344 |
/* |
| 345 |
** Register the fts3term module with database connection db. Return SQLITE_OK |
| 346 |
** if successful or an error code if sqlite3_create_module() fails. |
| 347 |
*/ |
| 348 |
int sqlite3Fts3InitTerm(sqlite3 *db){ |
| 349 |
static const sqlite3_module fts3term_module = { |
| 350 |
0, /* iVersion */ |
| 351 |
fts3termConnectMethod, /* xCreate */ |
| 352 |
fts3termConnectMethod, /* xConnect */ |
| 353 |
fts3termBestIndexMethod, /* xBestIndex */ |
| 354 |
fts3termDisconnectMethod, /* xDisconnect */ |
| 355 |
fts3termDisconnectMethod, /* xDestroy */ |
| 356 |
fts3termOpenMethod, /* xOpen */ |
| 357 |
fts3termCloseMethod, /* xClose */ |
| 358 |
fts3termFilterMethod, /* xFilter */ |
| 359 |
fts3termNextMethod, /* xNext */ |
| 360 |
fts3termEofMethod, /* xEof */ |
| 361 |
fts3termColumnMethod, /* xColumn */ |
| 362 |
fts3termRowidMethod, /* xRowid */ |
| 363 |
0, /* xUpdate */ |
| 364 |
0, /* xBegin */ |
| 365 |
0, /* xSync */ |
| 366 |
0, /* xCommit */ |
| 367 |
0, /* xRollback */ |
| 368 |
0, /* xFindFunction */ |
| 369 |
0, /* xRename */ |
| 370 |
0, /* xSavepoint */ |
| 371 |
0, /* xRelease */ |
| 372 |
0, /* xRollbackTo */ |
| 373 |
0, /* xShadowName */ |
| 374 |
0 /* xIntegrity */ |
| 375 |
}; |
| 376 |
int rc; /* Return code */ |
| 377 |
|
| 378 |
rc = sqlite3_create_module(db, "fts4term", &fts3term_module, 0); |
| 379 |
return rc; |
| 380 |
} |
| 381 |
|
| 382 |
#endif |
| 383 |
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |