interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / fts5 / fts5_vocab.c

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