OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** 2009 Oct 23 |
| 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 |
| 14 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 15 |
| 16 #include "fts3Int.h" |
| 17 #include <string.h> |
| 18 #include <assert.h> |
| 19 |
| 20 |
| 21 /* |
| 22 ** Used as an fts3ExprIterate() context when loading phrase doclists to |
| 23 ** Fts3Expr.aDoclist[]/nDoclist. |
| 24 */ |
| 25 typedef struct LoadDoclistCtx LoadDoclistCtx; |
| 26 struct LoadDoclistCtx { |
| 27 Fts3Table *pTab; /* FTS3 Table */ |
| 28 int nPhrase; /* Number of phrases seen so far */ |
| 29 int nToken; /* Number of tokens seen so far */ |
| 30 }; |
| 31 |
| 32 /* |
| 33 ** The following types are used as part of the implementation of the |
| 34 ** fts3BestSnippet() routine. |
| 35 */ |
| 36 typedef struct SnippetIter SnippetIter; |
| 37 typedef struct SnippetPhrase SnippetPhrase; |
| 38 typedef struct SnippetFragment SnippetFragment; |
| 39 |
| 40 struct SnippetIter { |
| 41 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */ |
| 42 int iCol; /* Extract snippet from this column */ |
| 43 int nSnippet; /* Requested snippet length (in tokens) */ |
| 44 int nPhrase; /* Number of phrases in query */ |
| 45 SnippetPhrase *aPhrase; /* Array of size nPhrase */ |
| 46 int iCurrent; /* First token of current snippet */ |
| 47 }; |
| 48 |
| 49 struct SnippetPhrase { |
| 50 int nToken; /* Number of tokens in phrase */ |
| 51 char *pList; /* Pointer to start of phrase position list */ |
| 52 int iHead; /* Next value in position list */ |
| 53 char *pHead; /* Position list data following iHead */ |
| 54 int iTail; /* Next value in trailing position list */ |
| 55 char *pTail; /* Position list data following iTail */ |
| 56 }; |
| 57 |
| 58 struct SnippetFragment { |
| 59 int iCol; /* Column snippet is extracted from */ |
| 60 int iPos; /* Index of first token in snippet */ |
| 61 u64 covered; /* Mask of query phrases covered */ |
| 62 u64 hlmask; /* Mask of snippet terms to highlight */ |
| 63 }; |
| 64 |
| 65 /* |
| 66 ** This type is used as an fts3ExprIterate() context object while |
| 67 ** accumulating the data returned by the matchinfo() function. |
| 68 */ |
| 69 typedef struct MatchInfo MatchInfo; |
| 70 struct MatchInfo { |
| 71 Fts3Cursor *pCursor; /* FTS3 Cursor */ |
| 72 int nCol; /* Number of columns in table */ |
| 73 u32 *aMatchinfo; /* Pre-allocated buffer */ |
| 74 }; |
| 75 |
| 76 |
| 77 |
| 78 /* |
| 79 ** The snippet() and offsets() functions both return text values. An instance |
| 80 ** of the following structure is used to accumulate those values while the |
| 81 ** functions are running. See fts3StringAppend() for details. |
| 82 */ |
| 83 typedef struct StrBuffer StrBuffer; |
| 84 struct StrBuffer { |
| 85 char *z; /* Pointer to buffer containing string */ |
| 86 int n; /* Length of z in bytes (excl. nul-term) */ |
| 87 int nAlloc; /* Allocated size of buffer z in bytes */ |
| 88 }; |
| 89 |
| 90 |
| 91 /* |
| 92 ** This function is used to help iterate through a position-list. A position |
| 93 ** list is a list of unique integers, sorted from smallest to largest. Each |
| 94 ** element of the list is represented by an FTS3 varint that takes the value |
| 95 ** of the difference between the current element and the previous one plus |
| 96 ** two. For example, to store the position-list: |
| 97 ** |
| 98 ** 4 9 113 |
| 99 ** |
| 100 ** the three varints: |
| 101 ** |
| 102 ** 6 7 106 |
| 103 ** |
| 104 ** are encoded. |
| 105 ** |
| 106 ** When this function is called, *pp points to the start of an element of |
| 107 ** the list. *piPos contains the value of the previous entry in the list. |
| 108 ** After it returns, *piPos contains the value of the next element of the |
| 109 ** list and *pp is advanced to the following varint. |
| 110 */ |
| 111 static void fts3GetDeltaPosition(char **pp, int *piPos){ |
| 112 int iVal; |
| 113 *pp += sqlite3Fts3GetVarint32(*pp, &iVal); |
| 114 *piPos += (iVal-2); |
| 115 } |
| 116 |
| 117 /* |
| 118 ** Helper function for fts3ExprIterate() (see below). |
| 119 */ |
| 120 static int fts3ExprIterate2( |
| 121 Fts3Expr *pExpr, /* Expression to iterate phrases of */ |
| 122 int *piPhrase, /* Pointer to phrase counter */ |
| 123 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ |
| 124 void *pCtx /* Second argument to pass to callback */ |
| 125 ){ |
| 126 int rc; /* Return code */ |
| 127 int eType = pExpr->eType; /* Type of expression node pExpr */ |
| 128 |
| 129 if( eType!=FTSQUERY_PHRASE ){ |
| 130 assert( pExpr->pLeft && pExpr->pRight ); |
| 131 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx); |
| 132 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){ |
| 133 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx); |
| 134 } |
| 135 }else{ |
| 136 rc = x(pExpr, *piPhrase, pCtx); |
| 137 (*piPhrase)++; |
| 138 } |
| 139 return rc; |
| 140 } |
| 141 |
| 142 /* |
| 143 ** Iterate through all phrase nodes in an FTS3 query, except those that |
| 144 ** are part of a sub-tree that is the right-hand-side of a NOT operator. |
| 145 ** For each phrase node found, the supplied callback function is invoked. |
| 146 ** |
| 147 ** If the callback function returns anything other than SQLITE_OK, |
| 148 ** the iteration is abandoned and the error code returned immediately. |
| 149 ** Otherwise, SQLITE_OK is returned after a callback has been made for |
| 150 ** all eligible phrase nodes. |
| 151 */ |
| 152 static int fts3ExprIterate( |
| 153 Fts3Expr *pExpr, /* Expression to iterate phrases of */ |
| 154 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ |
| 155 void *pCtx /* Second argument to pass to callback */ |
| 156 ){ |
| 157 int iPhrase = 0; /* Variable used as the phrase counter */ |
| 158 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx); |
| 159 } |
| 160 |
| 161 /* |
| 162 ** The argument to this function is always a phrase node. Its doclist |
| 163 ** (Fts3Expr.aDoclist[]) and the doclists associated with all phrase nodes |
| 164 ** to the left of this one in the query tree have already been loaded. |
| 165 ** |
| 166 ** If this phrase node is part of a series of phrase nodes joined by |
| 167 ** NEAR operators (and is not the left-most of said series), then elements are |
| 168 ** removed from the phrases doclist consistent with the NEAR restriction. If |
| 169 ** required, elements may be removed from the doclists of phrases to the |
| 170 ** left of this one that are part of the same series of NEAR operator |
| 171 ** connected phrases. |
| 172 ** |
| 173 ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK. |
| 174 */ |
| 175 static int fts3ExprNearTrim(Fts3Expr *pExpr){ |
| 176 int rc = SQLITE_OK; |
| 177 Fts3Expr *pParent = pExpr->pParent; |
| 178 |
| 179 assert( pExpr->eType==FTSQUERY_PHRASE ); |
| 180 while( rc==SQLITE_OK |
| 181 && pParent |
| 182 && pParent->eType==FTSQUERY_NEAR |
| 183 && pParent->pRight==pExpr |
| 184 ){ |
| 185 /* This expression (pExpr) is the right-hand-side of a NEAR operator. |
| 186 ** Find the expression to the left of the same operator. |
| 187 */ |
| 188 int nNear = pParent->nNear; |
| 189 Fts3Expr *pLeft = pParent->pLeft; |
| 190 |
| 191 if( pLeft->eType!=FTSQUERY_PHRASE ){ |
| 192 assert( pLeft->eType==FTSQUERY_NEAR ); |
| 193 assert( pLeft->pRight->eType==FTSQUERY_PHRASE ); |
| 194 pLeft = pLeft->pRight; |
| 195 } |
| 196 |
| 197 rc = sqlite3Fts3ExprNearTrim(pLeft, pExpr, nNear); |
| 198 |
| 199 pExpr = pLeft; |
| 200 pParent = pExpr->pParent; |
| 201 } |
| 202 |
| 203 return rc; |
| 204 } |
| 205 |
| 206 /* |
| 207 ** This is an fts3ExprIterate() callback used while loading the doclists |
| 208 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also |
| 209 ** fts3ExprLoadDoclists(). |
| 210 */ |
| 211 static int fts3ExprLoadDoclistsCb1(Fts3Expr *pExpr, int iPhrase, void *ctx){ |
| 212 int rc = SQLITE_OK; |
| 213 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx; |
| 214 |
| 215 UNUSED_PARAMETER(iPhrase); |
| 216 |
| 217 p->nPhrase++; |
| 218 p->nToken += pExpr->pPhrase->nToken; |
| 219 |
| 220 if( pExpr->isLoaded==0 ){ |
| 221 rc = sqlite3Fts3ExprLoadDoclist(p->pTab, pExpr); |
| 222 pExpr->isLoaded = 1; |
| 223 if( rc==SQLITE_OK ){ |
| 224 rc = fts3ExprNearTrim(pExpr); |
| 225 } |
| 226 } |
| 227 |
| 228 return rc; |
| 229 } |
| 230 |
| 231 /* |
| 232 ** This is an fts3ExprIterate() callback used while loading the doclists |
| 233 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also |
| 234 ** fts3ExprLoadDoclists(). |
| 235 */ |
| 236 static int fts3ExprLoadDoclistsCb2(Fts3Expr *pExpr, int iPhrase, void *ctx){ |
| 237 UNUSED_PARAMETER(iPhrase); |
| 238 UNUSED_PARAMETER(ctx); |
| 239 if( pExpr->aDoclist ){ |
| 240 pExpr->pCurrent = pExpr->aDoclist; |
| 241 pExpr->iCurrent = 0; |
| 242 pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent, &pExpr->iCurrent); |
| 243 } |
| 244 return SQLITE_OK; |
| 245 } |
| 246 |
| 247 /* |
| 248 ** Load the doclists for each phrase in the query associated with FTS3 cursor |
| 249 ** pCsr. |
| 250 ** |
| 251 ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable |
| 252 ** phrases in the expression (all phrases except those directly or |
| 253 ** indirectly descended from the right-hand-side of a NOT operator). If |
| 254 ** pnToken is not NULL, then it is set to the number of tokens in all |
| 255 ** matchable phrases of the expression. |
| 256 */ |
| 257 static int fts3ExprLoadDoclists( |
| 258 Fts3Cursor *pCsr, /* Fts3 cursor for current query */ |
| 259 int *pnPhrase, /* OUT: Number of phrases in query */ |
| 260 int *pnToken /* OUT: Number of tokens in query */ |
| 261 ){ |
| 262 int rc; /* Return Code */ |
| 263 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */ |
| 264 sCtx.pTab = (Fts3Table *)pCsr->base.pVtab; |
| 265 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb1, (void *)&sCtx); |
| 266 if( rc==SQLITE_OK ){ |
| 267 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb2, 0); |
| 268 } |
| 269 if( pnPhrase ) *pnPhrase = sCtx.nPhrase; |
| 270 if( pnToken ) *pnToken = sCtx.nToken; |
| 271 return rc; |
| 272 } |
| 273 |
| 274 /* |
| 275 ** Advance the position list iterator specified by the first two |
| 276 ** arguments so that it points to the first element with a value greater |
| 277 ** than or equal to parameter iNext. |
| 278 */ |
| 279 static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){ |
| 280 char *pIter = *ppIter; |
| 281 if( pIter ){ |
| 282 int iIter = *piIter; |
| 283 |
| 284 while( iIter<iNext ){ |
| 285 if( 0==(*pIter & 0xFE) ){ |
| 286 iIter = -1; |
| 287 pIter = 0; |
| 288 break; |
| 289 } |
| 290 fts3GetDeltaPosition(&pIter, &iIter); |
| 291 } |
| 292 |
| 293 *piIter = iIter; |
| 294 *ppIter = pIter; |
| 295 } |
| 296 } |
| 297 |
| 298 /* |
| 299 ** Advance the snippet iterator to the next candidate snippet. |
| 300 */ |
| 301 static int fts3SnippetNextCandidate(SnippetIter *pIter){ |
| 302 int i; /* Loop counter */ |
| 303 |
| 304 if( pIter->iCurrent<0 ){ |
| 305 /* The SnippetIter object has just been initialized. The first snippet |
| 306 ** candidate always starts at offset 0 (even if this candidate has a |
| 307 ** score of 0.0). |
| 308 */ |
| 309 pIter->iCurrent = 0; |
| 310 |
| 311 /* Advance the 'head' iterator of each phrase to the first offset that |
| 312 ** is greater than or equal to (iNext+nSnippet). |
| 313 */ |
| 314 for(i=0; i<pIter->nPhrase; i++){ |
| 315 SnippetPhrase *pPhrase = &pIter->aPhrase[i]; |
| 316 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet); |
| 317 } |
| 318 }else{ |
| 319 int iStart; |
| 320 int iEnd = 0x7FFFFFFF; |
| 321 |
| 322 for(i=0; i<pIter->nPhrase; i++){ |
| 323 SnippetPhrase *pPhrase = &pIter->aPhrase[i]; |
| 324 if( pPhrase->pHead && pPhrase->iHead<iEnd ){ |
| 325 iEnd = pPhrase->iHead; |
| 326 } |
| 327 } |
| 328 if( iEnd==0x7FFFFFFF ){ |
| 329 return 1; |
| 330 } |
| 331 |
| 332 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1; |
| 333 for(i=0; i<pIter->nPhrase; i++){ |
| 334 SnippetPhrase *pPhrase = &pIter->aPhrase[i]; |
| 335 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1); |
| 336 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart); |
| 337 } |
| 338 } |
| 339 |
| 340 return 0; |
| 341 } |
| 342 |
| 343 /* |
| 344 ** Retrieve information about the current candidate snippet of snippet |
| 345 ** iterator pIter. |
| 346 */ |
| 347 static void fts3SnippetDetails( |
| 348 SnippetIter *pIter, /* Snippet iterator */ |
| 349 u64 mCovered, /* Bitmask of phrases already covered */ |
| 350 int *piToken, /* OUT: First token of proposed snippet */ |
| 351 int *piScore, /* OUT: "Score" for this snippet */ |
| 352 u64 *pmCover, /* OUT: Bitmask of phrases covered */ |
| 353 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */ |
| 354 ){ |
| 355 int iStart = pIter->iCurrent; /* First token of snippet */ |
| 356 int iScore = 0; /* Score of this snippet */ |
| 357 int i; /* Loop counter */ |
| 358 u64 mCover = 0; /* Mask of phrases covered by this snippet */ |
| 359 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */ |
| 360 |
| 361 for(i=0; i<pIter->nPhrase; i++){ |
| 362 SnippetPhrase *pPhrase = &pIter->aPhrase[i]; |
| 363 if( pPhrase->pTail ){ |
| 364 char *pCsr = pPhrase->pTail; |
| 365 int iCsr = pPhrase->iTail; |
| 366 |
| 367 while( iCsr<(iStart+pIter->nSnippet) ){ |
| 368 int j; |
| 369 u64 mPhrase = (u64)1 << i; |
| 370 u64 mPos = (u64)1 << (iCsr - iStart); |
| 371 assert( iCsr>=iStart ); |
| 372 if( (mCover|mCovered)&mPhrase ){ |
| 373 iScore++; |
| 374 }else{ |
| 375 iScore += 1000; |
| 376 } |
| 377 mCover |= mPhrase; |
| 378 |
| 379 for(j=0; j<pPhrase->nToken; j++){ |
| 380 mHighlight |= (mPos>>j); |
| 381 } |
| 382 |
| 383 if( 0==(*pCsr & 0x0FE) ) break; |
| 384 fts3GetDeltaPosition(&pCsr, &iCsr); |
| 385 } |
| 386 } |
| 387 } |
| 388 |
| 389 /* Set the output variables before returning. */ |
| 390 *piToken = iStart; |
| 391 *piScore = iScore; |
| 392 *pmCover = mCover; |
| 393 *pmHighlight = mHighlight; |
| 394 } |
| 395 |
| 396 /* |
| 397 ** This function is an fts3ExprIterate() callback used by fts3BestSnippet(). |
| 398 ** Each invocation populates an element of the SnippetIter.aPhrase[] array. |
| 399 */ |
| 400 static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){ |
| 401 SnippetIter *p = (SnippetIter *)ctx; |
| 402 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase]; |
| 403 char *pCsr; |
| 404 |
| 405 pPhrase->nToken = pExpr->pPhrase->nToken; |
| 406 |
| 407 pCsr = sqlite3Fts3FindPositions(pExpr, p->pCsr->iPrevId, p->iCol); |
| 408 if( pCsr ){ |
| 409 int iFirst = 0; |
| 410 pPhrase->pList = pCsr; |
| 411 fts3GetDeltaPosition(&pCsr, &iFirst); |
| 412 pPhrase->pHead = pCsr; |
| 413 pPhrase->pTail = pCsr; |
| 414 pPhrase->iHead = iFirst; |
| 415 pPhrase->iTail = iFirst; |
| 416 }else{ |
| 417 assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 ); |
| 418 } |
| 419 |
| 420 return SQLITE_OK; |
| 421 } |
| 422 |
| 423 /* |
| 424 ** Select the fragment of text consisting of nFragment contiguous tokens |
| 425 ** from column iCol that represent the "best" snippet. The best snippet |
| 426 ** is the snippet with the highest score, where scores are calculated |
| 427 ** by adding: |
| 428 ** |
| 429 ** (a) +1 point for each occurence of a matchable phrase in the snippet. |
| 430 ** |
| 431 ** (b) +1000 points for the first occurence of each matchable phrase in |
| 432 ** the snippet for which the corresponding mCovered bit is not set. |
| 433 ** |
| 434 ** The selected snippet parameters are stored in structure *pFragment before |
| 435 ** returning. The score of the selected snippet is stored in *piScore |
| 436 ** before returning. |
| 437 */ |
| 438 static int fts3BestSnippet( |
| 439 int nSnippet, /* Desired snippet length */ |
| 440 Fts3Cursor *pCsr, /* Cursor to create snippet for */ |
| 441 int iCol, /* Index of column to create snippet from */ |
| 442 u64 mCovered, /* Mask of phrases already covered */ |
| 443 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */ |
| 444 SnippetFragment *pFragment, /* OUT: Best snippet found */ |
| 445 int *piScore /* OUT: Score of snippet pFragment */ |
| 446 ){ |
| 447 int rc; /* Return Code */ |
| 448 int nList; /* Number of phrases in expression */ |
| 449 SnippetIter sIter; /* Iterates through snippet candidates */ |
| 450 int nByte; /* Number of bytes of space to allocate */ |
| 451 int iBestScore = -1; /* Best snippet score found so far */ |
| 452 int i; /* Loop counter */ |
| 453 |
| 454 memset(&sIter, 0, sizeof(sIter)); |
| 455 |
| 456 /* Iterate through the phrases in the expression to count them. The same |
| 457 ** callback makes sure the doclists are loaded for each phrase. |
| 458 */ |
| 459 rc = fts3ExprLoadDoclists(pCsr, &nList, 0); |
| 460 if( rc!=SQLITE_OK ){ |
| 461 return rc; |
| 462 } |
| 463 |
| 464 /* Now that it is known how many phrases there are, allocate and zero |
| 465 ** the required space using malloc(). |
| 466 */ |
| 467 nByte = sizeof(SnippetPhrase) * nList; |
| 468 sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte); |
| 469 if( !sIter.aPhrase ){ |
| 470 return SQLITE_NOMEM; |
| 471 } |
| 472 memset(sIter.aPhrase, 0, nByte); |
| 473 |
| 474 /* Initialize the contents of the SnippetIter object. Then iterate through |
| 475 ** the set of phrases in the expression to populate the aPhrase[] array. |
| 476 */ |
| 477 sIter.pCsr = pCsr; |
| 478 sIter.iCol = iCol; |
| 479 sIter.nSnippet = nSnippet; |
| 480 sIter.nPhrase = nList; |
| 481 sIter.iCurrent = -1; |
| 482 (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter); |
| 483 |
| 484 /* Set the *pmSeen output variable. */ |
| 485 for(i=0; i<nList; i++){ |
| 486 if( sIter.aPhrase[i].pHead ){ |
| 487 *pmSeen |= (u64)1 << i; |
| 488 } |
| 489 } |
| 490 |
| 491 /* Loop through all candidate snippets. Store the best snippet in |
| 492 ** *pFragment. Store its associated 'score' in iBestScore. |
| 493 */ |
| 494 pFragment->iCol = iCol; |
| 495 while( !fts3SnippetNextCandidate(&sIter) ){ |
| 496 int iPos; |
| 497 int iScore; |
| 498 u64 mCover; |
| 499 u64 mHighlight; |
| 500 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight); |
| 501 assert( iScore>=0 ); |
| 502 if( iScore>iBestScore ){ |
| 503 pFragment->iPos = iPos; |
| 504 pFragment->hlmask = mHighlight; |
| 505 pFragment->covered = mCover; |
| 506 iBestScore = iScore; |
| 507 } |
| 508 } |
| 509 |
| 510 sqlite3_free(sIter.aPhrase); |
| 511 *piScore = iBestScore; |
| 512 return SQLITE_OK; |
| 513 } |
| 514 |
| 515 |
| 516 /* |
| 517 ** Append a string to the string-buffer passed as the first argument. |
| 518 ** |
| 519 ** If nAppend is negative, then the length of the string zAppend is |
| 520 ** determined using strlen(). |
| 521 */ |
| 522 static int fts3StringAppend( |
| 523 StrBuffer *pStr, /* Buffer to append to */ |
| 524 const char *zAppend, /* Pointer to data to append to buffer */ |
| 525 int nAppend /* Size of zAppend in bytes (or -1) */ |
| 526 ){ |
| 527 if( nAppend<0 ){ |
| 528 nAppend = (int)strlen(zAppend); |
| 529 } |
| 530 |
| 531 /* If there is insufficient space allocated at StrBuffer.z, use realloc() |
| 532 ** to grow the buffer until so that it is big enough to accomadate the |
| 533 ** appended data. |
| 534 */ |
| 535 if( pStr->n+nAppend+1>=pStr->nAlloc ){ |
| 536 int nAlloc = pStr->nAlloc+nAppend+100; |
| 537 char *zNew = sqlite3_realloc(pStr->z, nAlloc); |
| 538 if( !zNew ){ |
| 539 return SQLITE_NOMEM; |
| 540 } |
| 541 pStr->z = zNew; |
| 542 pStr->nAlloc = nAlloc; |
| 543 } |
| 544 |
| 545 /* Append the data to the string buffer. */ |
| 546 memcpy(&pStr->z[pStr->n], zAppend, nAppend); |
| 547 pStr->n += nAppend; |
| 548 pStr->z[pStr->n] = '\0'; |
| 549 |
| 550 return SQLITE_OK; |
| 551 } |
| 552 |
| 553 /* |
| 554 ** The fts3BestSnippet() function often selects snippets that end with a |
| 555 ** query term. That is, the final term of the snippet is always a term |
| 556 ** that requires highlighting. For example, if 'X' is a highlighted term |
| 557 ** and '.' is a non-highlighted term, BestSnippet() may select: |
| 558 ** |
| 559 ** ........X.....X |
| 560 ** |
| 561 ** This function "shifts" the beginning of the snippet forward in the |
| 562 ** document so that there are approximately the same number of |
| 563 ** non-highlighted terms to the right of the final highlighted term as there |
| 564 ** are to the left of the first highlighted term. For example, to this: |
| 565 ** |
| 566 ** ....X.....X.... |
| 567 ** |
| 568 ** This is done as part of extracting the snippet text, not when selecting |
| 569 ** the snippet. Snippet selection is done based on doclists only, so there |
| 570 ** is no way for fts3BestSnippet() to know whether or not the document |
| 571 ** actually contains terms that follow the final highlighted term. |
| 572 */ |
| 573 static int fts3SnippetShift( |
| 574 Fts3Table *pTab, /* FTS3 table snippet comes from */ |
| 575 int nSnippet, /* Number of tokens desired for snippet */ |
| 576 const char *zDoc, /* Document text to extract snippet from */ |
| 577 int nDoc, /* Size of buffer zDoc in bytes */ |
| 578 int *piPos, /* IN/OUT: First token of snippet */ |
| 579 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */ |
| 580 ){ |
| 581 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */ |
| 582 |
| 583 if( hlmask ){ |
| 584 int nLeft; /* Tokens to the left of first highlight */ |
| 585 int nRight; /* Tokens to the right of last highlight */ |
| 586 int nDesired; /* Ideal number of tokens to shift forward */ |
| 587 |
| 588 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++); |
| 589 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++); |
| 590 nDesired = (nLeft-nRight)/2; |
| 591 |
| 592 /* Ideally, the start of the snippet should be pushed forward in the |
| 593 ** document nDesired tokens. This block checks if there are actually |
| 594 ** nDesired tokens to the right of the snippet. If so, *piPos and |
| 595 ** *pHlMask are updated to shift the snippet nDesired tokens to the |
| 596 ** right. Otherwise, the snippet is shifted by the number of tokens |
| 597 ** available. |
| 598 */ |
| 599 if( nDesired>0 ){ |
| 600 int nShift; /* Number of tokens to shift snippet by */ |
| 601 int iCurrent = 0; /* Token counter */ |
| 602 int rc; /* Return Code */ |
| 603 sqlite3_tokenizer_module *pMod; |
| 604 sqlite3_tokenizer_cursor *pC; |
| 605 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule; |
| 606 |
| 607 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired) |
| 608 ** or more tokens in zDoc/nDoc. |
| 609 */ |
| 610 rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC); |
| 611 if( rc!=SQLITE_OK ){ |
| 612 return rc; |
| 613 } |
| 614 pC->pTokenizer = pTab->pTokenizer; |
| 615 while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){ |
| 616 const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3; |
| 617 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent); |
| 618 } |
| 619 pMod->xClose(pC); |
| 620 if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; } |
| 621 |
| 622 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet; |
| 623 assert( nShift<=nDesired ); |
| 624 if( nShift>0 ){ |
| 625 *piPos += nShift; |
| 626 *pHlmask = hlmask >> nShift; |
| 627 } |
| 628 } |
| 629 } |
| 630 return SQLITE_OK; |
| 631 } |
| 632 |
| 633 /* |
| 634 ** Extract the snippet text for fragment pFragment from cursor pCsr and |
| 635 ** append it to string buffer pOut. |
| 636 */ |
| 637 static int fts3SnippetText( |
| 638 Fts3Cursor *pCsr, /* FTS3 Cursor */ |
| 639 SnippetFragment *pFragment, /* Snippet to extract */ |
| 640 int iFragment, /* Fragment number */ |
| 641 int isLast, /* True for final fragment in snippet */ |
| 642 int nSnippet, /* Number of tokens in extracted snippet */ |
| 643 const char *zOpen, /* String inserted before highlighted term */ |
| 644 const char *zClose, /* String inserted after highlighted term */ |
| 645 const char *zEllipsis, /* String inserted between snippets */ |
| 646 StrBuffer *pOut /* Write output here */ |
| 647 ){ |
| 648 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 649 int rc; /* Return code */ |
| 650 const char *zDoc; /* Document text to extract snippet from */ |
| 651 int nDoc; /* Size of zDoc in bytes */ |
| 652 int iCurrent = 0; /* Current token number of document */ |
| 653 int iEnd = 0; /* Byte offset of end of current token */ |
| 654 int isShiftDone = 0; /* True after snippet is shifted */ |
| 655 int iPos = pFragment->iPos; /* First token of snippet */ |
| 656 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */ |
| 657 int iCol = pFragment->iCol+1; /* Query column to extract text from */ |
| 658 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */ |
| 659 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */ |
| 660 const char *ZDUMMY; /* Dummy argument used with tokenizer */ |
| 661 int DUMMY1; /* Dummy argument used with tokenizer */ |
| 662 |
| 663 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol); |
| 664 if( zDoc==0 ){ |
| 665 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){ |
| 666 return SQLITE_NOMEM; |
| 667 } |
| 668 return SQLITE_OK; |
| 669 } |
| 670 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol); |
| 671 |
| 672 /* Open a token cursor on the document. */ |
| 673 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule; |
| 674 rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC); |
| 675 if( rc!=SQLITE_OK ){ |
| 676 return rc; |
| 677 } |
| 678 pC->pTokenizer = pTab->pTokenizer; |
| 679 |
| 680 while( rc==SQLITE_OK ){ |
| 681 int iBegin; /* Offset in zDoc of start of token */ |
| 682 int iFin; /* Offset in zDoc of end of token */ |
| 683 int isHighlight; /* True for highlighted terms */ |
| 684 |
| 685 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent); |
| 686 if( rc!=SQLITE_OK ){ |
| 687 if( rc==SQLITE_DONE ){ |
| 688 /* Special case - the last token of the snippet is also the last token |
| 689 ** of the column. Append any punctuation that occurred between the end |
| 690 ** of the previous token and the end of the document to the output. |
| 691 ** Then break out of the loop. */ |
| 692 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1); |
| 693 } |
| 694 break; |
| 695 } |
| 696 if( iCurrent<iPos ){ continue; } |
| 697 |
| 698 if( !isShiftDone ){ |
| 699 int n = nDoc - iBegin; |
| 700 rc = fts3SnippetShift(pTab, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask); |
| 701 isShiftDone = 1; |
| 702 |
| 703 /* Now that the shift has been done, check if the initial "..." are |
| 704 ** required. They are required if (a) this is not the first fragment, |
| 705 ** or (b) this fragment does not begin at position 0 of its column. |
| 706 */ |
| 707 if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){ |
| 708 rc = fts3StringAppend(pOut, zEllipsis, -1); |
| 709 } |
| 710 if( rc!=SQLITE_OK || iCurrent<iPos ) continue; |
| 711 } |
| 712 |
| 713 if( iCurrent>=(iPos+nSnippet) ){ |
| 714 if( isLast ){ |
| 715 rc = fts3StringAppend(pOut, zEllipsis, -1); |
| 716 } |
| 717 break; |
| 718 } |
| 719 |
| 720 /* Set isHighlight to true if this term should be highlighted. */ |
| 721 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0; |
| 722 |
| 723 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd); |
| 724 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1); |
| 725 if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin); |
| 726 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1); |
| 727 |
| 728 iEnd = iFin; |
| 729 } |
| 730 |
| 731 pMod->xClose(pC); |
| 732 return rc; |
| 733 } |
| 734 |
| 735 |
| 736 /* |
| 737 ** This function is used to count the entries in a column-list (a |
| 738 ** delta-encoded list of term offsets within a single column of a single |
| 739 ** row). When this function is called, *ppCollist should point to the |
| 740 ** beginning of the first varint in the column-list (the varint that |
| 741 ** contains the position of the first matching term in the column data). |
| 742 ** Before returning, *ppCollist is set to point to the first byte after |
| 743 ** the last varint in the column-list (either the 0x00 signifying the end |
| 744 ** of the position-list, or the 0x01 that precedes the column number of |
| 745 ** the next column in the position-list). |
| 746 ** |
| 747 ** The number of elements in the column-list is returned. |
| 748 */ |
| 749 static int fts3ColumnlistCount(char **ppCollist){ |
| 750 char *pEnd = *ppCollist; |
| 751 char c = 0; |
| 752 int nEntry = 0; |
| 753 |
| 754 /* A column-list is terminated by either a 0x01 or 0x00. */ |
| 755 while( 0xFE & (*pEnd | c) ){ |
| 756 c = *pEnd++ & 0x80; |
| 757 if( !c ) nEntry++; |
| 758 } |
| 759 |
| 760 *ppCollist = pEnd; |
| 761 return nEntry; |
| 762 } |
| 763 |
| 764 static void fts3LoadColumnlistCounts(char **pp, u32 *aOut, int isGlobal){ |
| 765 char *pCsr = *pp; |
| 766 while( *pCsr ){ |
| 767 int nHit; |
| 768 sqlite3_int64 iCol = 0; |
| 769 if( *pCsr==0x01 ){ |
| 770 pCsr++; |
| 771 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol); |
| 772 } |
| 773 nHit = fts3ColumnlistCount(&pCsr); |
| 774 assert( nHit>0 ); |
| 775 if( isGlobal ){ |
| 776 aOut[iCol*3+1]++; |
| 777 } |
| 778 aOut[iCol*3] += nHit; |
| 779 } |
| 780 pCsr++; |
| 781 *pp = pCsr; |
| 782 } |
| 783 |
| 784 /* |
| 785 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats |
| 786 ** for a single query. The "global" stats are those elements of the matchinfo |
| 787 ** array that are constant for all rows returned by the current query. |
| 788 */ |
| 789 static int fts3ExprGlobalMatchinfoCb( |
| 790 Fts3Expr *pExpr, /* Phrase expression node */ |
| 791 int iPhrase, /* Phrase number (numbered from zero) */ |
| 792 void *pCtx /* Pointer to MatchInfo structure */ |
| 793 ){ |
| 794 MatchInfo *p = (MatchInfo *)pCtx; |
| 795 char *pCsr; |
| 796 char *pEnd; |
| 797 const int iStart = 2 + (iPhrase * p->nCol * 3) + 1; |
| 798 |
| 799 assert( pExpr->isLoaded ); |
| 800 |
| 801 /* Fill in the global hit count matrix row for this phrase. */ |
| 802 pCsr = pExpr->aDoclist; |
| 803 pEnd = &pExpr->aDoclist[pExpr->nDoclist]; |
| 804 while( pCsr<pEnd ){ |
| 805 while( *pCsr++ & 0x80 ); /* Skip past docid. */ |
| 806 fts3LoadColumnlistCounts(&pCsr, &p->aMatchinfo[iStart], 1); |
| 807 } |
| 808 |
| 809 return SQLITE_OK; |
| 810 } |
| 811 |
| 812 /* |
| 813 ** fts3ExprIterate() callback used to collect the "local" matchinfo stats |
| 814 ** for a single query. The "local" stats are those elements of the matchinfo |
| 815 ** array that are different for each row returned by the query. |
| 816 */ |
| 817 static int fts3ExprLocalMatchinfoCb( |
| 818 Fts3Expr *pExpr, /* Phrase expression node */ |
| 819 int iPhrase, /* Phrase number */ |
| 820 void *pCtx /* Pointer to MatchInfo structure */ |
| 821 ){ |
| 822 MatchInfo *p = (MatchInfo *)pCtx; |
| 823 |
| 824 if( pExpr->aDoclist ){ |
| 825 char *pCsr; |
| 826 int iStart = 2 + (iPhrase * p->nCol * 3); |
| 827 int i; |
| 828 |
| 829 for(i=0; i<p->nCol; i++) p->aMatchinfo[iStart+i*3] = 0; |
| 830 |
| 831 pCsr = sqlite3Fts3FindPositions(pExpr, p->pCursor->iPrevId, -1); |
| 832 if( pCsr ){ |
| 833 fts3LoadColumnlistCounts(&pCsr, &p->aMatchinfo[iStart], 0); |
| 834 } |
| 835 } |
| 836 |
| 837 return SQLITE_OK; |
| 838 } |
| 839 |
| 840 /* |
| 841 ** Populate pCsr->aMatchinfo[] with data for the current row. The |
| 842 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32). |
| 843 */ |
| 844 static int fts3GetMatchinfo(Fts3Cursor *pCsr){ |
| 845 MatchInfo sInfo; |
| 846 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 847 int rc = SQLITE_OK; |
| 848 |
| 849 sInfo.pCursor = pCsr; |
| 850 sInfo.nCol = pTab->nColumn; |
| 851 |
| 852 if( pCsr->aMatchinfo==0 ){ |
| 853 /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the |
| 854 ** matchinfo function has been called for this query. In this case |
| 855 ** allocate the array used to accumulate the matchinfo data and |
| 856 ** initialize those elements that are constant for every row. |
| 857 */ |
| 858 int nPhrase; /* Number of phrases */ |
| 859 int nMatchinfo; /* Number of u32 elements in match-info */ |
| 860 |
| 861 /* Load doclists for each phrase in the query. */ |
| 862 rc = fts3ExprLoadDoclists(pCsr, &nPhrase, 0); |
| 863 if( rc!=SQLITE_OK ){ |
| 864 return rc; |
| 865 } |
| 866 nMatchinfo = 2 + 3*sInfo.nCol*nPhrase; |
| 867 if( pTab->bHasDocsize ){ |
| 868 nMatchinfo += 1 + 2*pTab->nColumn; |
| 869 } |
| 870 |
| 871 sInfo.aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo); |
| 872 if( !sInfo.aMatchinfo ){ |
| 873 return SQLITE_NOMEM; |
| 874 } |
| 875 memset(sInfo.aMatchinfo, 0, sizeof(u32)*nMatchinfo); |
| 876 |
| 877 |
| 878 /* First element of match-info is the number of phrases in the query */ |
| 879 sInfo.aMatchinfo[0] = nPhrase; |
| 880 sInfo.aMatchinfo[1] = sInfo.nCol; |
| 881 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprGlobalMatchinfoCb,(void*)&sInfo); |
| 882 if( pTab->bHasDocsize ){ |
| 883 int ofst = 2 + 3*sInfo.aMatchinfo[0]*sInfo.aMatchinfo[1]; |
| 884 rc = sqlite3Fts3MatchinfoDocsizeGlobal(pCsr, &sInfo.aMatchinfo[ofst]); |
| 885 } |
| 886 pCsr->aMatchinfo = sInfo.aMatchinfo; |
| 887 pCsr->isMatchinfoNeeded = 1; |
| 888 } |
| 889 |
| 890 sInfo.aMatchinfo = pCsr->aMatchinfo; |
| 891 if( rc==SQLITE_OK && pCsr->isMatchinfoNeeded ){ |
| 892 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLocalMatchinfoCb, (void*)&sInfo); |
| 893 if( pTab->bHasDocsize ){ |
| 894 int ofst = 2 + 3*sInfo.aMatchinfo[0]*sInfo.aMatchinfo[1]; |
| 895 rc = sqlite3Fts3MatchinfoDocsizeLocal(pCsr, &sInfo.aMatchinfo[ofst]); |
| 896 } |
| 897 pCsr->isMatchinfoNeeded = 0; |
| 898 } |
| 899 |
| 900 return SQLITE_OK; |
| 901 } |
| 902 |
| 903 /* |
| 904 ** Implementation of snippet() function. |
| 905 */ |
| 906 void sqlite3Fts3Snippet( |
| 907 sqlite3_context *pCtx, /* SQLite function call context */ |
| 908 Fts3Cursor *pCsr, /* Cursor object */ |
| 909 const char *zStart, /* Snippet start text - "<b>" */ |
| 910 const char *zEnd, /* Snippet end text - "</b>" */ |
| 911 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */ |
| 912 int iCol, /* Extract snippet from this column */ |
| 913 int nToken /* Approximate number of tokens in snippet */ |
| 914 ){ |
| 915 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 916 int rc = SQLITE_OK; |
| 917 int i; |
| 918 StrBuffer res = {0, 0, 0}; |
| 919 |
| 920 /* The returned text includes up to four fragments of text extracted from |
| 921 ** the data in the current row. The first iteration of the for(...) loop |
| 922 ** below attempts to locate a single fragment of text nToken tokens in |
| 923 ** size that contains at least one instance of all phrases in the query |
| 924 ** expression that appear in the current row. If such a fragment of text |
| 925 ** cannot be found, the second iteration of the loop attempts to locate |
| 926 ** a pair of fragments, and so on. |
| 927 */ |
| 928 int nSnippet = 0; /* Number of fragments in this snippet */ |
| 929 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */ |
| 930 int nFToken = -1; /* Number of tokens in each fragment */ |
| 931 |
| 932 if( !pCsr->pExpr ){ |
| 933 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); |
| 934 return; |
| 935 } |
| 936 |
| 937 for(nSnippet=1; 1; nSnippet++){ |
| 938 |
| 939 int iSnip; /* Loop counter 0..nSnippet-1 */ |
| 940 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */ |
| 941 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */ |
| 942 |
| 943 if( nToken>=0 ){ |
| 944 nFToken = (nToken+nSnippet-1) / nSnippet; |
| 945 }else{ |
| 946 nFToken = -1 * nToken; |
| 947 } |
| 948 |
| 949 for(iSnip=0; iSnip<nSnippet; iSnip++){ |
| 950 int iBestScore = -1; /* Best score of columns checked so far */ |
| 951 int iRead; /* Used to iterate through columns */ |
| 952 SnippetFragment *pFragment = &aSnippet[iSnip]; |
| 953 |
| 954 memset(pFragment, 0, sizeof(*pFragment)); |
| 955 |
| 956 /* Loop through all columns of the table being considered for snippets. |
| 957 ** If the iCol argument to this function was negative, this means all |
| 958 ** columns of the FTS3 table. Otherwise, only column iCol is considered. |
| 959 */ |
| 960 for(iRead=0; iRead<pTab->nColumn; iRead++){ |
| 961 SnippetFragment sF; |
| 962 int iS; |
| 963 if( iCol>=0 && iRead!=iCol ) continue; |
| 964 |
| 965 /* Find the best snippet of nFToken tokens in column iRead. */ |
| 966 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS); |
| 967 if( rc!=SQLITE_OK ){ |
| 968 goto snippet_out; |
| 969 } |
| 970 if( iS>iBestScore ){ |
| 971 *pFragment = sF; |
| 972 iBestScore = iS; |
| 973 } |
| 974 } |
| 975 |
| 976 mCovered |= pFragment->covered; |
| 977 } |
| 978 |
| 979 /* If all query phrases seen by fts3BestSnippet() are present in at least |
| 980 ** one of the nSnippet snippet fragments, break out of the loop. |
| 981 */ |
| 982 assert( (mCovered&mSeen)==mCovered ); |
| 983 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break; |
| 984 } |
| 985 |
| 986 assert( nFToken>0 ); |
| 987 |
| 988 for(i=0; i<nSnippet && rc==SQLITE_OK; i++){ |
| 989 rc = fts3SnippetText(pCsr, &aSnippet[i], |
| 990 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res |
| 991 ); |
| 992 } |
| 993 |
| 994 snippet_out: |
| 995 if( rc!=SQLITE_OK ){ |
| 996 sqlite3_result_error_code(pCtx, rc); |
| 997 sqlite3_free(res.z); |
| 998 }else{ |
| 999 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free); |
| 1000 } |
| 1001 } |
| 1002 |
| 1003 |
| 1004 typedef struct TermOffset TermOffset; |
| 1005 typedef struct TermOffsetCtx TermOffsetCtx; |
| 1006 |
| 1007 struct TermOffset { |
| 1008 char *pList; /* Position-list */ |
| 1009 int iPos; /* Position just read from pList */ |
| 1010 int iOff; /* Offset of this term from read positions */ |
| 1011 }; |
| 1012 |
| 1013 struct TermOffsetCtx { |
| 1014 int iCol; /* Column of table to populate aTerm for */ |
| 1015 int iTerm; |
| 1016 sqlite3_int64 iDocid; |
| 1017 TermOffset *aTerm; |
| 1018 }; |
| 1019 |
| 1020 /* |
| 1021 ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets(). |
| 1022 */ |
| 1023 static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){ |
| 1024 TermOffsetCtx *p = (TermOffsetCtx *)ctx; |
| 1025 int nTerm; /* Number of tokens in phrase */ |
| 1026 int iTerm; /* For looping through nTerm phrase terms */ |
| 1027 char *pList; /* Pointer to position list for phrase */ |
| 1028 int iPos = 0; /* First position in position-list */ |
| 1029 |
| 1030 UNUSED_PARAMETER(iPhrase); |
| 1031 pList = sqlite3Fts3FindPositions(pExpr, p->iDocid, p->iCol); |
| 1032 nTerm = pExpr->pPhrase->nToken; |
| 1033 if( pList ){ |
| 1034 fts3GetDeltaPosition(&pList, &iPos); |
| 1035 assert( iPos>=0 ); |
| 1036 } |
| 1037 |
| 1038 for(iTerm=0; iTerm<nTerm; iTerm++){ |
| 1039 TermOffset *pT = &p->aTerm[p->iTerm++]; |
| 1040 pT->iOff = nTerm-iTerm-1; |
| 1041 pT->pList = pList; |
| 1042 pT->iPos = iPos; |
| 1043 } |
| 1044 |
| 1045 return SQLITE_OK; |
| 1046 } |
| 1047 |
| 1048 /* |
| 1049 ** Implementation of offsets() function. |
| 1050 */ |
| 1051 void sqlite3Fts3Offsets( |
| 1052 sqlite3_context *pCtx, /* SQLite function call context */ |
| 1053 Fts3Cursor *pCsr /* Cursor object */ |
| 1054 ){ |
| 1055 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 1056 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule; |
| 1057 const char *ZDUMMY; /* Dummy argument used with xNext() */ |
| 1058 int NDUMMY; /* Dummy argument used with xNext() */ |
| 1059 int rc; /* Return Code */ |
| 1060 int nToken; /* Number of tokens in query */ |
| 1061 int iCol; /* Column currently being processed */ |
| 1062 StrBuffer res = {0, 0, 0}; /* Result string */ |
| 1063 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */ |
| 1064 |
| 1065 if( !pCsr->pExpr ){ |
| 1066 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); |
| 1067 return; |
| 1068 } |
| 1069 |
| 1070 memset(&sCtx, 0, sizeof(sCtx)); |
| 1071 assert( pCsr->isRequireSeek==0 ); |
| 1072 |
| 1073 /* Count the number of terms in the query */ |
| 1074 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken); |
| 1075 if( rc!=SQLITE_OK ) goto offsets_out; |
| 1076 |
| 1077 /* Allocate the array of TermOffset iterators. */ |
| 1078 sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken); |
| 1079 if( 0==sCtx.aTerm ){ |
| 1080 rc = SQLITE_NOMEM; |
| 1081 goto offsets_out; |
| 1082 } |
| 1083 sCtx.iDocid = pCsr->iPrevId; |
| 1084 |
| 1085 /* Loop through the table columns, appending offset information to |
| 1086 ** string-buffer res for each column. |
| 1087 */ |
| 1088 for(iCol=0; iCol<pTab->nColumn; iCol++){ |
| 1089 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */ |
| 1090 int iStart; |
| 1091 int iEnd; |
| 1092 int iCurrent; |
| 1093 const char *zDoc; |
| 1094 int nDoc; |
| 1095 |
| 1096 /* Initialize the contents of sCtx.aTerm[] for column iCol. There is |
| 1097 ** no way that this operation can fail, so the return code from |
| 1098 ** fts3ExprIterate() can be discarded. |
| 1099 */ |
| 1100 sCtx.iCol = iCol; |
| 1101 sCtx.iTerm = 0; |
| 1102 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx); |
| 1103 |
| 1104 /* Retreive the text stored in column iCol. If an SQL NULL is stored |
| 1105 ** in column iCol, jump immediately to the next iteration of the loop. |
| 1106 ** If an OOM occurs while retrieving the data (this can happen if SQLite |
| 1107 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM |
| 1108 ** to the caller. |
| 1109 */ |
| 1110 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1); |
| 1111 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1); |
| 1112 if( zDoc==0 ){ |
| 1113 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){ |
| 1114 continue; |
| 1115 } |
| 1116 rc = SQLITE_NOMEM; |
| 1117 goto offsets_out; |
| 1118 } |
| 1119 |
| 1120 /* Initialize a tokenizer iterator to iterate through column iCol. */ |
| 1121 rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC); |
| 1122 if( rc!=SQLITE_OK ) goto offsets_out; |
| 1123 pC->pTokenizer = pTab->pTokenizer; |
| 1124 |
| 1125 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent); |
| 1126 while( rc==SQLITE_OK ){ |
| 1127 int i; /* Used to loop through terms */ |
| 1128 int iMinPos = 0x7FFFFFFF; /* Position of next token */ |
| 1129 TermOffset *pTerm = 0; /* TermOffset associated with next token */ |
| 1130 |
| 1131 for(i=0; i<nToken; i++){ |
| 1132 TermOffset *pT = &sCtx.aTerm[i]; |
| 1133 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){ |
| 1134 iMinPos = pT->iPos-pT->iOff; |
| 1135 pTerm = pT; |
| 1136 } |
| 1137 } |
| 1138 |
| 1139 if( !pTerm ){ |
| 1140 /* All offsets for this column have been gathered. */ |
| 1141 break; |
| 1142 }else{ |
| 1143 assert( iCurrent<=iMinPos ); |
| 1144 if( 0==(0xFE&*pTerm->pList) ){ |
| 1145 pTerm->pList = 0; |
| 1146 }else{ |
| 1147 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos); |
| 1148 } |
| 1149 while( rc==SQLITE_OK && iCurrent<iMinPos ){ |
| 1150 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent); |
| 1151 } |
| 1152 if( rc==SQLITE_OK ){ |
| 1153 char aBuffer[64]; |
| 1154 sqlite3_snprintf(sizeof(aBuffer), aBuffer, |
| 1155 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart |
| 1156 ); |
| 1157 rc = fts3StringAppend(&res, aBuffer, -1); |
| 1158 }else if( rc==SQLITE_DONE ){ |
| 1159 rc = SQLITE_CORRUPT; |
| 1160 } |
| 1161 } |
| 1162 } |
| 1163 if( rc==SQLITE_DONE ){ |
| 1164 rc = SQLITE_OK; |
| 1165 } |
| 1166 |
| 1167 pMod->xClose(pC); |
| 1168 if( rc!=SQLITE_OK ) goto offsets_out; |
| 1169 } |
| 1170 |
| 1171 offsets_out: |
| 1172 sqlite3_free(sCtx.aTerm); |
| 1173 assert( rc!=SQLITE_DONE ); |
| 1174 if( rc!=SQLITE_OK ){ |
| 1175 sqlite3_result_error_code(pCtx, rc); |
| 1176 sqlite3_free(res.z); |
| 1177 }else{ |
| 1178 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free); |
| 1179 } |
| 1180 return; |
| 1181 } |
| 1182 |
| 1183 /* |
| 1184 ** Implementation of matchinfo() function. |
| 1185 */ |
| 1186 void sqlite3Fts3Matchinfo(sqlite3_context *pContext, Fts3Cursor *pCsr){ |
| 1187 int rc; |
| 1188 if( !pCsr->pExpr ){ |
| 1189 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC); |
| 1190 return; |
| 1191 } |
| 1192 rc = fts3GetMatchinfo(pCsr); |
| 1193 if( rc!=SQLITE_OK ){ |
| 1194 sqlite3_result_error_code(pContext, rc); |
| 1195 }else{ |
| 1196 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab; |
| 1197 int n = sizeof(u32)*(2+pCsr->aMatchinfo[0]*pCsr->aMatchinfo[1]*3); |
| 1198 if( pTab->bHasDocsize ){ |
| 1199 n += sizeof(u32)*(1 + 2*pTab->nColumn); |
| 1200 } |
| 1201 sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT); |
| 1202 } |
| 1203 } |
| 1204 |
| 1205 #endif |
OLD | NEW |