fts3_tokenize_vtab.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. ** 2013 Apr 22
  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 contains code for the "fts3tokenize" virtual table module.
  14. ** An fts3tokenize virtual table is created as follows:
  15. **
  16. ** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
  17. ** <tokenizer-name>, <arg-1>, ...
  18. ** );
  19. **
  20. ** The table created has the following schema:
  21. **
  22. ** CREATE TABLE <tbl>(input, token, start, end, position)
  23. **
  24. ** When queried, the query must include a WHERE clause of type:
  25. **
  26. ** input = <string>
  27. **
  28. ** The virtual table module tokenizes this <string>, using the FTS3
  29. ** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
  30. ** statement and returns one row for each token in the result. With
  31. ** fields set as follows:
  32. **
  33. ** input: Always set to a copy of <string>
  34. ** token: A token from the input.
  35. ** start: Byte offset of the token within the input <string>.
  36. ** end: Byte offset of the byte immediately following the end of the
  37. ** token within the input string.
  38. ** pos: Token offset of token within input.
  39. **
  40. */
  41. #include "fts3Int.h"
  42. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  43. #include <string.h>
  44. #include <assert.h>
  45. typedef struct Fts3tokTable Fts3tokTable;
  46. typedef struct Fts3tokCursor Fts3tokCursor;
  47. /*
  48. ** Virtual table structure.
  49. */
  50. struct Fts3tokTable {
  51. sqlite3_vtab base; /* Base class used by SQLite core */
  52. const sqlite3_tokenizer_module *pMod;
  53. sqlite3_tokenizer *pTok;
  54. };
  55. /*
  56. ** Virtual table cursor structure.
  57. */
  58. struct Fts3tokCursor {
  59. sqlite3_vtab_cursor base; /* Base class used by SQLite core */
  60. char *zInput; /* Input string */
  61. sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
  62. int iRowid; /* Current 'rowid' value */
  63. const char *zToken; /* Current 'token' value */
  64. int nToken; /* Size of zToken in bytes */
  65. int iStart; /* Current 'start' value */
  66. int iEnd; /* Current 'end' value */
  67. int iPos; /* Current 'pos' value */
  68. };
  69. /*
  70. ** Query FTS for the tokenizer implementation named zName.
  71. */
  72. static int fts3tokQueryTokenizer(
  73. Fts3Hash *pHash,
  74. const char *zName,
  75. const sqlite3_tokenizer_module **pp,
  76. char **pzErr
  77. ){
  78. sqlite3_tokenizer_module *p;
  79. int nName = (int)strlen(zName);
  80. p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
  81. if( !p ){
  82. sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
  83. return SQLITE_ERROR;
  84. }
  85. *pp = p;
  86. return SQLITE_OK;
  87. }
  88. /*
  89. ** The second argument, argv[], is an array of pointers to nul-terminated
  90. ** strings. This function makes a copy of the array and strings into a
  91. ** single block of memory. It then dequotes any of the strings that appear
  92. ** to be quoted.
  93. **
  94. ** If successful, output parameter *pazDequote is set to point at the
  95. ** array of dequoted strings and SQLITE_OK is returned. The caller is
  96. ** responsible for eventually calling sqlite3_free() to free the array
  97. ** in this case. Or, if an error occurs, an SQLite error code is returned.
  98. ** The final value of *pazDequote is undefined in this case.
  99. */
  100. static int fts3tokDequoteArray(
  101. int argc, /* Number of elements in argv[] */
  102. const char * const *argv, /* Input array */
  103. char ***pazDequote /* Output array */
  104. ){
  105. int rc = SQLITE_OK; /* Return code */
  106. if( argc==0 ){
  107. *pazDequote = 0;
  108. }else{
  109. int i;
  110. int nByte = 0;
  111. char **azDequote;
  112. for(i=0; i<argc; i++){
  113. nByte += (int)(strlen(argv[i]) + 1);
  114. }
  115. *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
  116. if( azDequote==0 ){
  117. rc = SQLITE_NOMEM;
  118. }else{
  119. char *pSpace = (char *)&azDequote[argc];
  120. for(i=0; i<argc; i++){
  121. int n = (int)strlen(argv[i]);
  122. azDequote[i] = pSpace;
  123. memcpy(pSpace, argv[i], n+1);
  124. sqlite3Fts3Dequote(pSpace);
  125. pSpace += (n+1);
  126. }
  127. }
  128. }
  129. return rc;
  130. }
  131. /*
  132. ** Schema of the tokenizer table.
  133. */
  134. #define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
  135. /*
  136. ** This function does all the work for both the xConnect and xCreate methods.
  137. ** These tables have no persistent representation of their own, so xConnect
  138. ** and xCreate are identical operations.
  139. **
  140. ** argv[0]: module name
  141. ** argv[1]: database name
  142. ** argv[2]: table name
  143. ** argv[3]: first argument (tokenizer name)
  144. */
  145. static int fts3tokConnectMethod(
  146. sqlite3 *db, /* Database connection */
  147. void *pHash, /* Hash table of tokenizers */
  148. int argc, /* Number of elements in argv array */
  149. const char * const *argv, /* xCreate/xConnect argument array */
  150. sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
  151. char **pzErr /* OUT: sqlite3_malloc'd error message */
  152. ){
  153. Fts3tokTable *pTab = 0;
  154. const sqlite3_tokenizer_module *pMod = 0;
  155. sqlite3_tokenizer *pTok = 0;
  156. int rc;
  157. char **azDequote = 0;
  158. int nDequote;
  159. rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
  160. if( rc!=SQLITE_OK ) return rc;
  161. nDequote = argc-3;
  162. rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
  163. if( rc==SQLITE_OK ){
  164. const char *zModule;
  165. if( nDequote<1 ){
  166. zModule = "simple";
  167. }else{
  168. zModule = azDequote[0];
  169. }
  170. rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
  171. }
  172. assert( (rc==SQLITE_OK)==(pMod!=0) );
  173. if( rc==SQLITE_OK ){
  174. const char * const *azArg = 0;
  175. if( nDequote>1 ) azArg = (const char * const *)&azDequote[1];
  176. rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
  177. }
  178. if( rc==SQLITE_OK ){
  179. pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
  180. if( pTab==0 ){
  181. rc = SQLITE_NOMEM;
  182. }
  183. }
  184. if( rc==SQLITE_OK ){
  185. memset(pTab, 0, sizeof(Fts3tokTable));
  186. pTab->pMod = pMod;
  187. pTab->pTok = pTok;
  188. *ppVtab = &pTab->base;
  189. }else{
  190. if( pTok ){
  191. pMod->xDestroy(pTok);
  192. }
  193. }
  194. sqlite3_free(azDequote);
  195. return rc;
  196. }
  197. /*
  198. ** This function does the work for both the xDisconnect and xDestroy methods.
  199. ** These tables have no persistent representation of their own, so xDisconnect
  200. ** and xDestroy are identical operations.
  201. */
  202. static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
  203. Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
  204. pTab->pMod->xDestroy(pTab->pTok);
  205. sqlite3_free(pTab);
  206. return SQLITE_OK;
  207. }
  208. /*
  209. ** xBestIndex - Analyze a WHERE and ORDER BY clause.
  210. */
  211. static int fts3tokBestIndexMethod(
  212. sqlite3_vtab *pVTab,
  213. sqlite3_index_info *pInfo
  214. ){
  215. int i;
  216. UNUSED_PARAMETER(pVTab);
  217. for(i=0; i<pInfo->nConstraint; i++){
  218. if( pInfo->aConstraint[i].usable
  219. && pInfo->aConstraint[i].iColumn==0
  220. && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
  221. ){
  222. pInfo->idxNum = 1;
  223. pInfo->aConstraintUsage[i].argvIndex = 1;
  224. pInfo->aConstraintUsage[i].omit = 1;
  225. pInfo->estimatedCost = 1;
  226. return SQLITE_OK;
  227. }
  228. }
  229. pInfo->idxNum = 0;
  230. assert( pInfo->estimatedCost>1000000.0 );
  231. return SQLITE_OK;
  232. }
  233. /*
  234. ** xOpen - Open a cursor.
  235. */
  236. static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
  237. Fts3tokCursor *pCsr;
  238. UNUSED_PARAMETER(pVTab);
  239. pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
  240. if( pCsr==0 ){
  241. return SQLITE_NOMEM;
  242. }
  243. memset(pCsr, 0, sizeof(Fts3tokCursor));
  244. *ppCsr = (sqlite3_vtab_cursor *)pCsr;
  245. return SQLITE_OK;
  246. }
  247. /*
  248. ** Reset the tokenizer cursor passed as the only argument. As if it had
  249. ** just been returned by fts3tokOpenMethod().
  250. */
  251. static void fts3tokResetCursor(Fts3tokCursor *pCsr){
  252. if( pCsr->pCsr ){
  253. Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
  254. pTab->pMod->xClose(pCsr->pCsr);
  255. pCsr->pCsr = 0;
  256. }
  257. sqlite3_free(pCsr->zInput);
  258. pCsr->zInput = 0;
  259. pCsr->zToken = 0;
  260. pCsr->nToken = 0;
  261. pCsr->iStart = 0;
  262. pCsr->iEnd = 0;
  263. pCsr->iPos = 0;
  264. pCsr->iRowid = 0;
  265. }
  266. /*
  267. ** xClose - Close a cursor.
  268. */
  269. static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
  270. Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
  271. fts3tokResetCursor(pCsr);
  272. sqlite3_free(pCsr);
  273. return SQLITE_OK;
  274. }
  275. /*
  276. ** xNext - Advance the cursor to the next row, if any.
  277. */
  278. static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
  279. Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
  280. Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
  281. int rc; /* Return code */
  282. pCsr->iRowid++;
  283. rc = pTab->pMod->xNext(pCsr->pCsr,
  284. &pCsr->zToken, &pCsr->nToken,
  285. &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
  286. );
  287. if( rc!=SQLITE_OK ){
  288. fts3tokResetCursor(pCsr);
  289. if( rc==SQLITE_DONE ) rc = SQLITE_OK;
  290. }
  291. return rc;
  292. }
  293. /*
  294. ** xFilter - Initialize a cursor to point at the start of its data.
  295. */
  296. static int fts3tokFilterMethod(
  297. sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
  298. int idxNum, /* Strategy index */
  299. const char *idxStr, /* Unused */
  300. int nVal, /* Number of elements in apVal */
  301. sqlite3_value **apVal /* Arguments for the indexing scheme */
  302. ){
  303. int rc = SQLITE_ERROR;
  304. Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
  305. Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
  306. UNUSED_PARAMETER(idxStr);
  307. UNUSED_PARAMETER(nVal);
  308. fts3tokResetCursor(pCsr);
  309. if( idxNum==1 ){
  310. const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
  311. int nByte = sqlite3_value_bytes(apVal[0]);
  312. pCsr->zInput = sqlite3_malloc64(nByte+1);
  313. if( pCsr->zInput==0 ){
  314. rc = SQLITE_NOMEM;
  315. }else{
  316. if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte);
  317. pCsr->zInput[nByte] = 0;
  318. rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
  319. if( rc==SQLITE_OK ){
  320. pCsr->pCsr->pTokenizer = pTab->pTok;
  321. }
  322. }
  323. }
  324. if( rc!=SQLITE_OK ) return rc;
  325. return fts3tokNextMethod(pCursor);
  326. }
  327. /*
  328. ** xEof - Return true if the cursor is at EOF, or false otherwise.
  329. */
  330. static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
  331. Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
  332. return (pCsr->zToken==0);
  333. }
  334. /*
  335. ** xColumn - Return a column value.
  336. */
  337. static int fts3tokColumnMethod(
  338. sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
  339. sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
  340. int iCol /* Index of column to read value from */
  341. ){
  342. Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
  343. /* CREATE TABLE x(input, token, start, end, position) */
  344. switch( iCol ){
  345. case 0:
  346. sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
  347. break;
  348. case 1:
  349. sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
  350. break;
  351. case 2:
  352. sqlite3_result_int(pCtx, pCsr->iStart);
  353. break;
  354. case 3:
  355. sqlite3_result_int(pCtx, pCsr->iEnd);
  356. break;
  357. default:
  358. assert( iCol==4 );
  359. sqlite3_result_int(pCtx, pCsr->iPos);
  360. break;
  361. }
  362. return SQLITE_OK;
  363. }
  364. /*
  365. ** xRowid - Return the current rowid for the cursor.
  366. */
  367. static int fts3tokRowidMethod(
  368. sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
  369. sqlite_int64 *pRowid /* OUT: Rowid value */
  370. ){
  371. Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
  372. *pRowid = (sqlite3_int64)pCsr->iRowid;
  373. return SQLITE_OK;
  374. }
  375. /*
  376. ** Register the fts3tok module with database connection db. Return SQLITE_OK
  377. ** if successful or an error code if sqlite3_create_module() fails.
  378. */
  379. int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){
  380. static const sqlite3_module fts3tok_module = {
  381. 0, /* iVersion */
  382. fts3tokConnectMethod, /* xCreate */
  383. fts3tokConnectMethod, /* xConnect */
  384. fts3tokBestIndexMethod, /* xBestIndex */
  385. fts3tokDisconnectMethod, /* xDisconnect */
  386. fts3tokDisconnectMethod, /* xDestroy */
  387. fts3tokOpenMethod, /* xOpen */
  388. fts3tokCloseMethod, /* xClose */
  389. fts3tokFilterMethod, /* xFilter */
  390. fts3tokNextMethod, /* xNext */
  391. fts3tokEofMethod, /* xEof */
  392. fts3tokColumnMethod, /* xColumn */
  393. fts3tokRowidMethod, /* xRowid */
  394. 0, /* xUpdate */
  395. 0, /* xBegin */
  396. 0, /* xSync */
  397. 0, /* xCommit */
  398. 0, /* xRollback */
  399. 0, /* xFindFunction */
  400. 0, /* xRename */
  401. 0, /* xSavepoint */
  402. 0, /* xRelease */
  403. 0, /* xRollbackTo */
  404. 0, /* xShadowName */
  405. 0 /* xIntegrity */
  406. };
  407. int rc; /* Return code */
  408. rc = sqlite3_create_module_v2(
  409. db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy
  410. );
  411. return rc;
  412. }
  413. #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */