1
0

fts3_tokenizer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. ** 2007 June 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 is part of an SQLite module implementing full-text search.
  14. ** This particular file implements the generic tokenizer interface.
  15. */
  16. /*
  17. ** The code in this file is only compiled if:
  18. **
  19. ** * The FTS3 module is being built as an extension
  20. ** (in which case SQLITE_CORE is not defined), or
  21. **
  22. ** * The FTS3 module is being built into the core of
  23. ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  24. */
  25. #include "fts3Int.h"
  26. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  27. #include <assert.h>
  28. #include <string.h>
  29. /*
  30. ** Implementation of the SQL scalar function for accessing the underlying
  31. ** hash table. This function may be called as follows:
  32. **
  33. ** SELECT <function-name>(<key-name>);
  34. ** SELECT <function-name>(<key-name>, <pointer>);
  35. **
  36. ** where <function-name> is the name passed as the second argument
  37. ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
  38. **
  39. ** If the <pointer> argument is specified, it must be a blob value
  40. ** containing a pointer to be stored as the hash data corresponding
  41. ** to the string <key-name>. If <pointer> is not specified, then
  42. ** the string <key-name> must already exist in the has table. Otherwise,
  43. ** an error is returned.
  44. **
  45. ** Whether or not the <pointer> argument is specified, the value returned
  46. ** is a blob containing the pointer stored as the hash data corresponding
  47. ** to string <key-name> (after the hash-table is updated, if applicable).
  48. */
  49. static void scalarFunc(
  50. sqlite3_context *context,
  51. int argc,
  52. sqlite3_value **argv
  53. ){
  54. Fts3Hash *pHash;
  55. void *pPtr = 0;
  56. const unsigned char *zName;
  57. int nName;
  58. assert( argc==1 || argc==2 );
  59. pHash = (Fts3Hash *)sqlite3_user_data(context);
  60. zName = sqlite3_value_text(argv[0]);
  61. nName = sqlite3_value_bytes(argv[0])+1;
  62. if( argc==2 ){
  63. void *pOld;
  64. int n = sqlite3_value_bytes(argv[1]);
  65. if( n!=sizeof(pPtr) ){
  66. sqlite3_result_error(context, "argument type mismatch", -1);
  67. return;
  68. }
  69. pPtr = *(void **)sqlite3_value_blob(argv[1]);
  70. pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
  71. if( pOld==pPtr ){
  72. sqlite3_result_error(context, "out of memory", -1);
  73. return;
  74. }
  75. }else{
  76. pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
  77. if( !pPtr ){
  78. char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
  79. sqlite3_result_error(context, zErr, -1);
  80. sqlite3_free(zErr);
  81. return;
  82. }
  83. }
  84. sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
  85. }
  86. int sqlite3Fts3IsIdChar(char c){
  87. static const char isFtsIdChar[] = {
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
  90. 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
  91. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
  92. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
  93. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
  94. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
  95. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
  96. };
  97. return (c&0x80 || isFtsIdChar[(int)(c)]);
  98. }
  99. const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
  100. const char *z1;
  101. const char *z2 = 0;
  102. /* Find the start of the next token. */
  103. z1 = zStr;
  104. while( z2==0 ){
  105. char c = *z1;
  106. switch( c ){
  107. case '\0': return 0; /* No more tokens here */
  108. case '\'':
  109. case '"':
  110. case '`': {
  111. z2 = z1;
  112. while( *++z2 && (*z2!=c || *++z2==c) );
  113. break;
  114. }
  115. case '[':
  116. z2 = &z1[1];
  117. while( *z2 && z2[0]!=']' ) z2++;
  118. if( *z2 ) z2++;
  119. break;
  120. default:
  121. if( sqlite3Fts3IsIdChar(*z1) ){
  122. z2 = &z1[1];
  123. while( sqlite3Fts3IsIdChar(*z2) ) z2++;
  124. }else{
  125. z1++;
  126. }
  127. }
  128. }
  129. *pn = (int)(z2-z1);
  130. return z1;
  131. }
  132. int sqlite3Fts3InitTokenizer(
  133. Fts3Hash *pHash, /* Tokenizer hash table */
  134. const char *zArg, /* Tokenizer name */
  135. sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
  136. char **pzErr /* OUT: Set to malloced error message */
  137. ){
  138. int rc;
  139. char *z = (char *)zArg;
  140. int n = 0;
  141. char *zCopy;
  142. char *zEnd; /* Pointer to nul-term of zCopy */
  143. sqlite3_tokenizer_module *m;
  144. zCopy = sqlite3_mprintf("%s", zArg);
  145. if( !zCopy ) return SQLITE_NOMEM;
  146. zEnd = &zCopy[strlen(zCopy)];
  147. z = (char *)sqlite3Fts3NextToken(zCopy, &n);
  148. z[n] = '\0';
  149. sqlite3Fts3Dequote(z);
  150. m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
  151. if( !m ){
  152. *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
  153. rc = SQLITE_ERROR;
  154. }else{
  155. char const **aArg = 0;
  156. int iArg = 0;
  157. z = &z[n+1];
  158. while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
  159. int nNew = sizeof(char *)*(iArg+1);
  160. char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
  161. if( !aNew ){
  162. sqlite3_free(zCopy);
  163. sqlite3_free((void *)aArg);
  164. return SQLITE_NOMEM;
  165. }
  166. aArg = aNew;
  167. aArg[iArg++] = z;
  168. z[n] = '\0';
  169. sqlite3Fts3Dequote(z);
  170. z = &z[n+1];
  171. }
  172. rc = m->xCreate(iArg, aArg, ppTok);
  173. assert( rc!=SQLITE_OK || *ppTok );
  174. if( rc!=SQLITE_OK ){
  175. *pzErr = sqlite3_mprintf("unknown tokenizer");
  176. }else{
  177. (*ppTok)->pModule = m;
  178. }
  179. sqlite3_free((void *)aArg);
  180. }
  181. sqlite3_free(zCopy);
  182. return rc;
  183. }
  184. #ifdef SQLITE_TEST
  185. #include <tcl.h>
  186. #include <string.h>
  187. /*
  188. ** Implementation of a special SQL scalar function for testing tokenizers
  189. ** designed to be used in concert with the Tcl testing framework. This
  190. ** function must be called with two or more arguments:
  191. **
  192. ** SELECT <function-name>(<key-name>, ..., <input-string>);
  193. **
  194. ** where <function-name> is the name passed as the second argument
  195. ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
  196. ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
  197. **
  198. ** The return value is a string that may be interpreted as a Tcl
  199. ** list. For each token in the <input-string>, three elements are
  200. ** added to the returned list. The first is the token position, the
  201. ** second is the token text (folded, stemmed, etc.) and the third is the
  202. ** substring of <input-string> associated with the token. For example,
  203. ** using the built-in "simple" tokenizer:
  204. **
  205. ** SELECT fts_tokenizer_test('simple', 'I don't see how');
  206. **
  207. ** will return the string:
  208. **
  209. ** "{0 i I 1 dont don't 2 see see 3 how how}"
  210. **
  211. */
  212. static void testFunc(
  213. sqlite3_context *context,
  214. int argc,
  215. sqlite3_value **argv
  216. ){
  217. Fts3Hash *pHash;
  218. sqlite3_tokenizer_module *p;
  219. sqlite3_tokenizer *pTokenizer = 0;
  220. sqlite3_tokenizer_cursor *pCsr = 0;
  221. const char *zErr = 0;
  222. const char *zName;
  223. int nName;
  224. const char *zInput;
  225. int nInput;
  226. const char *azArg[64];
  227. const char *zToken;
  228. int nToken = 0;
  229. int iStart = 0;
  230. int iEnd = 0;
  231. int iPos = 0;
  232. int i;
  233. Tcl_Obj *pRet;
  234. if( argc<2 ){
  235. sqlite3_result_error(context, "insufficient arguments", -1);
  236. return;
  237. }
  238. nName = sqlite3_value_bytes(argv[0]);
  239. zName = (const char *)sqlite3_value_text(argv[0]);
  240. nInput = sqlite3_value_bytes(argv[argc-1]);
  241. zInput = (const char *)sqlite3_value_text(argv[argc-1]);
  242. pHash = (Fts3Hash *)sqlite3_user_data(context);
  243. p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
  244. if( !p ){
  245. char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
  246. sqlite3_result_error(context, zErr, -1);
  247. sqlite3_free(zErr);
  248. return;
  249. }
  250. pRet = Tcl_NewObj();
  251. Tcl_IncrRefCount(pRet);
  252. for(i=1; i<argc-1; i++){
  253. azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
  254. }
  255. if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
  256. zErr = "error in xCreate()";
  257. goto finish;
  258. }
  259. pTokenizer->pModule = p;
  260. if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
  261. zErr = "error in xOpen()";
  262. goto finish;
  263. }
  264. while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
  265. Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
  266. Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
  267. zToken = &zInput[iStart];
  268. nToken = iEnd-iStart;
  269. Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
  270. }
  271. if( SQLITE_OK!=p->xClose(pCsr) ){
  272. zErr = "error in xClose()";
  273. goto finish;
  274. }
  275. if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
  276. zErr = "error in xDestroy()";
  277. goto finish;
  278. }
  279. finish:
  280. if( zErr ){
  281. sqlite3_result_error(context, zErr, -1);
  282. }else{
  283. sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
  284. }
  285. Tcl_DecrRefCount(pRet);
  286. }
  287. static
  288. int registerTokenizer(
  289. sqlite3 *db,
  290. char *zName,
  291. const sqlite3_tokenizer_module *p
  292. ){
  293. int rc;
  294. sqlite3_stmt *pStmt;
  295. const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
  296. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  297. if( rc!=SQLITE_OK ){
  298. return rc;
  299. }
  300. sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
  301. sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
  302. sqlite3_step(pStmt);
  303. return sqlite3_finalize(pStmt);
  304. }
  305. static
  306. int queryTokenizer(
  307. sqlite3 *db,
  308. char *zName,
  309. const sqlite3_tokenizer_module **pp
  310. ){
  311. int rc;
  312. sqlite3_stmt *pStmt;
  313. const char zSql[] = "SELECT fts3_tokenizer(?)";
  314. *pp = 0;
  315. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  316. if( rc!=SQLITE_OK ){
  317. return rc;
  318. }
  319. sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
  320. if( SQLITE_ROW==sqlite3_step(pStmt) ){
  321. if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
  322. memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
  323. }
  324. }
  325. return sqlite3_finalize(pStmt);
  326. }
  327. void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  328. /*
  329. ** Implementation of the scalar function fts3_tokenizer_internal_test().
  330. ** This function is used for testing only, it is not included in the
  331. ** build unless SQLITE_TEST is defined.
  332. **
  333. ** The purpose of this is to test that the fts3_tokenizer() function
  334. ** can be used as designed by the C-code in the queryTokenizer and
  335. ** registerTokenizer() functions above. These two functions are repeated
  336. ** in the README.tokenizer file as an example, so it is important to
  337. ** test them.
  338. **
  339. ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
  340. ** function with no arguments. An assert() will fail if a problem is
  341. ** detected. i.e.:
  342. **
  343. ** SELECT fts3_tokenizer_internal_test();
  344. **
  345. */
  346. static void intTestFunc(
  347. sqlite3_context *context,
  348. int argc,
  349. sqlite3_value **argv
  350. ){
  351. int rc;
  352. const sqlite3_tokenizer_module *p1;
  353. const sqlite3_tokenizer_module *p2;
  354. sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
  355. UNUSED_PARAMETER(argc);
  356. UNUSED_PARAMETER(argv);
  357. /* Test the query function */
  358. sqlite3Fts3SimpleTokenizerModule(&p1);
  359. rc = queryTokenizer(db, "simple", &p2);
  360. assert( rc==SQLITE_OK );
  361. assert( p1==p2 );
  362. rc = queryTokenizer(db, "nosuchtokenizer", &p2);
  363. assert( rc==SQLITE_ERROR );
  364. assert( p2==0 );
  365. assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
  366. /* Test the storage function */
  367. rc = registerTokenizer(db, "nosuchtokenizer", p1);
  368. assert( rc==SQLITE_OK );
  369. rc = queryTokenizer(db, "nosuchtokenizer", &p2);
  370. assert( rc==SQLITE_OK );
  371. assert( p2==p1 );
  372. sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
  373. }
  374. #endif
  375. /*
  376. ** Set up SQL objects in database db used to access the contents of
  377. ** the hash table pointed to by argument pHash. The hash table must
  378. ** been initialized to use string keys, and to take a private copy
  379. ** of the key when a value is inserted. i.e. by a call similar to:
  380. **
  381. ** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
  382. **
  383. ** This function adds a scalar function (see header comment above
  384. ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
  385. ** defined at compilation time, a temporary virtual table (see header
  386. ** comment above struct HashTableVtab) to the database schema. Both
  387. ** provide read/write access to the contents of *pHash.
  388. **
  389. ** The third argument to this function, zName, is used as the name
  390. ** of both the scalar and, if created, the virtual table.
  391. */
  392. int sqlite3Fts3InitHashTable(
  393. sqlite3 *db,
  394. Fts3Hash *pHash,
  395. const char *zName
  396. ){
  397. int rc = SQLITE_OK;
  398. void *p = (void *)pHash;
  399. const int any = SQLITE_ANY;
  400. #ifdef SQLITE_TEST
  401. char *zTest = 0;
  402. char *zTest2 = 0;
  403. void *pdb = (void *)db;
  404. zTest = sqlite3_mprintf("%s_test", zName);
  405. zTest2 = sqlite3_mprintf("%s_internal_test", zName);
  406. if( !zTest || !zTest2 ){
  407. rc = SQLITE_NOMEM;
  408. }
  409. #endif
  410. if( SQLITE_OK==rc ){
  411. rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
  412. }
  413. if( SQLITE_OK==rc ){
  414. rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
  415. }
  416. #ifdef SQLITE_TEST
  417. if( SQLITE_OK==rc ){
  418. rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
  419. }
  420. if( SQLITE_OK==rc ){
  421. rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
  422. }
  423. #endif
  424. #ifdef SQLITE_TEST
  425. sqlite3_free(zTest);
  426. sqlite3_free(zTest2);
  427. #endif
  428. return rc;
  429. }
  430. #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */