OLD | NEW |
1 /* | 1 /* |
2 ** 2009 Oct 23 | 2 ** 2009 Oct 23 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ****************************************************************************** | 11 ****************************************************************************** |
12 */ | 12 */ |
13 | 13 |
14 #include "fts3Int.h" | 14 #include "fts3Int.h" |
15 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) | 15 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
16 | 16 |
17 #include <string.h> | 17 #include <string.h> |
18 #include <assert.h> | 18 #include <assert.h> |
19 | 19 |
20 /* | 20 /* |
21 ** Characters that may appear in the second argument to matchinfo(). | 21 ** Characters that may appear in the second argument to matchinfo(). |
22 */ | 22 */ |
23 #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */ | 23 #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */ |
24 #define FTS3_MATCHINFO_NCOL 'c' /* 1 value */ | 24 #define FTS3_MATCHINFO_NCOL 'c' /* 1 value */ |
25 #define FTS3_MATCHINFO_NDOC 'n' /* 1 value */ | 25 #define FTS3_MATCHINFO_NDOC 'n' /* 1 value */ |
26 #define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */ | 26 #define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */ |
27 #define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */ | 27 #define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */ |
28 #define FTS3_MATCHINFO_LCS 's' /* nCol values */ | 28 #define FTS3_MATCHINFO_LCS 's' /* nCol values */ |
29 #define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */ | 29 #define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */ |
| 30 #define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */ |
| 31 #define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */ |
30 | 32 |
31 /* | 33 /* |
32 ** The default value for the second argument to matchinfo(). | 34 ** The default value for the second argument to matchinfo(). |
33 */ | 35 */ |
34 #define FTS3_MATCHINFO_DEFAULT "pcx" | 36 #define FTS3_MATCHINFO_DEFAULT "pcx" |
35 | 37 |
36 | 38 |
37 /* | 39 /* |
38 ** Used as an fts3ExprIterate() context when loading phrase doclists to | 40 ** Used as an fts3ExprIterate() context when loading phrase doclists to |
39 ** Fts3Expr.aDoclist[]/nDoclist. | 41 ** Fts3Expr.aDoclist[]/nDoclist. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 /* | 83 /* |
82 ** This type is used as an fts3ExprIterate() context object while | 84 ** This type is used as an fts3ExprIterate() context object while |
83 ** accumulating the data returned by the matchinfo() function. | 85 ** accumulating the data returned by the matchinfo() function. |
84 */ | 86 */ |
85 typedef struct MatchInfo MatchInfo; | 87 typedef struct MatchInfo MatchInfo; |
86 struct MatchInfo { | 88 struct MatchInfo { |
87 Fts3Cursor *pCursor; /* FTS3 Cursor */ | 89 Fts3Cursor *pCursor; /* FTS3 Cursor */ |
88 int nCol; /* Number of columns in table */ | 90 int nCol; /* Number of columns in table */ |
89 int nPhrase; /* Number of matchable phrases in query */ | 91 int nPhrase; /* Number of matchable phrases in query */ |
90 sqlite3_int64 nDoc; /* Number of docs in database */ | 92 sqlite3_int64 nDoc; /* Number of docs in database */ |
| 93 char flag; |
91 u32 *aMatchinfo; /* Pre-allocated buffer */ | 94 u32 *aMatchinfo; /* Pre-allocated buffer */ |
92 }; | 95 }; |
93 | 96 |
| 97 /* |
| 98 ** An instance of this structure is used to manage a pair of buffers, each |
| 99 ** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below |
| 100 ** for details. |
| 101 */ |
| 102 struct MatchinfoBuffer { |
| 103 u8 aRef[3]; |
| 104 int nElem; |
| 105 int bGlobal; /* Set if global data is loaded */ |
| 106 char *zMatchinfo; |
| 107 u32 aMatchinfo[1]; |
| 108 }; |
94 | 109 |
95 | 110 |
96 /* | 111 /* |
97 ** The snippet() and offsets() functions both return text values. An instance | 112 ** The snippet() and offsets() functions both return text values. An instance |
98 ** of the following structure is used to accumulate those values while the | 113 ** of the following structure is used to accumulate those values while the |
99 ** functions are running. See fts3StringAppend() for details. | 114 ** functions are running. See fts3StringAppend() for details. |
100 */ | 115 */ |
101 typedef struct StrBuffer StrBuffer; | 116 typedef struct StrBuffer StrBuffer; |
102 struct StrBuffer { | 117 struct StrBuffer { |
103 char *z; /* Pointer to buffer containing string */ | 118 char *z; /* Pointer to buffer containing string */ |
104 int n; /* Length of z in bytes (excl. nul-term) */ | 119 int n; /* Length of z in bytes (excl. nul-term) */ |
105 int nAlloc; /* Allocated size of buffer z in bytes */ | 120 int nAlloc; /* Allocated size of buffer z in bytes */ |
106 }; | 121 }; |
107 | 122 |
108 | 123 |
| 124 /************************************************************************* |
| 125 ** Start of MatchinfoBuffer code. |
| 126 */ |
| 127 |
| 128 /* |
| 129 ** Allocate a two-slot MatchinfoBuffer object. |
| 130 */ |
| 131 static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){ |
| 132 MatchinfoBuffer *pRet; |
| 133 int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer); |
| 134 int nStr = (int)strlen(zMatchinfo); |
| 135 |
| 136 pRet = sqlite3_malloc(nByte + nStr+1); |
| 137 if( pRet ){ |
| 138 memset(pRet, 0, nByte); |
| 139 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet; |
| 140 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1); |
| 141 pRet->nElem = nElem; |
| 142 pRet->zMatchinfo = ((char*)pRet) + nByte; |
| 143 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1); |
| 144 pRet->aRef[0] = 1; |
| 145 } |
| 146 |
| 147 return pRet; |
| 148 } |
| 149 |
| 150 static void fts3MIBufferFree(void *p){ |
| 151 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]); |
| 152 |
| 153 assert( (u32*)p==&pBuf->aMatchinfo[1] |
| 154 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2] |
| 155 ); |
| 156 if( (u32*)p==&pBuf->aMatchinfo[1] ){ |
| 157 pBuf->aRef[1] = 0; |
| 158 }else{ |
| 159 pBuf->aRef[2] = 0; |
| 160 } |
| 161 |
| 162 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){ |
| 163 sqlite3_free(pBuf); |
| 164 } |
| 165 } |
| 166 |
| 167 static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){ |
| 168 void (*xRet)(void*) = 0; |
| 169 u32 *aOut = 0; |
| 170 |
| 171 if( p->aRef[1]==0 ){ |
| 172 p->aRef[1] = 1; |
| 173 aOut = &p->aMatchinfo[1]; |
| 174 xRet = fts3MIBufferFree; |
| 175 } |
| 176 else if( p->aRef[2]==0 ){ |
| 177 p->aRef[2] = 1; |
| 178 aOut = &p->aMatchinfo[p->nElem+2]; |
| 179 xRet = fts3MIBufferFree; |
| 180 }else{ |
| 181 aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32)); |
| 182 if( aOut ){ |
| 183 xRet = sqlite3_free; |
| 184 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32)); |
| 185 } |
| 186 } |
| 187 |
| 188 *paOut = aOut; |
| 189 return xRet; |
| 190 } |
| 191 |
| 192 static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){ |
| 193 p->bGlobal = 1; |
| 194 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32)); |
| 195 } |
| 196 |
| 197 /* |
| 198 ** Free a MatchinfoBuffer object allocated using fts3MIBufferNew() |
| 199 */ |
| 200 void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){ |
| 201 if( p ){ |
| 202 assert( p->aRef[0]==1 ); |
| 203 p->aRef[0] = 0; |
| 204 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){ |
| 205 sqlite3_free(p); |
| 206 } |
| 207 } |
| 208 } |
| 209 |
| 210 /* |
| 211 ** End of MatchinfoBuffer code. |
| 212 *************************************************************************/ |
| 213 |
| 214 |
109 /* | 215 /* |
110 ** This function is used to help iterate through a position-list. A position | 216 ** This function is used to help iterate through a position-list. A position |
111 ** list is a list of unique integers, sorted from smallest to largest. Each | 217 ** list is a list of unique integers, sorted from smallest to largest. Each |
112 ** element of the list is represented by an FTS3 varint that takes the value | 218 ** element of the list is represented by an FTS3 varint that takes the value |
113 ** of the difference between the current element and the previous one plus | 219 ** of the difference between the current element and the previous one plus |
114 ** two. For example, to store the position-list: | 220 ** two. For example, to store the position-list: |
115 ** | 221 ** |
116 ** 4 9 113 | 222 ** 4 9 113 |
117 ** | 223 ** |
118 ** the three varints: | 224 ** the three varints: |
(...skipping 16 matching lines...) Expand all Loading... |
135 /* | 241 /* |
136 ** Helper function for fts3ExprIterate() (see below). | 242 ** Helper function for fts3ExprIterate() (see below). |
137 */ | 243 */ |
138 static int fts3ExprIterate2( | 244 static int fts3ExprIterate2( |
139 Fts3Expr *pExpr, /* Expression to iterate phrases of */ | 245 Fts3Expr *pExpr, /* Expression to iterate phrases of */ |
140 int *piPhrase, /* Pointer to phrase counter */ | 246 int *piPhrase, /* Pointer to phrase counter */ |
141 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ | 247 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ |
142 void *pCtx /* Second argument to pass to callback */ | 248 void *pCtx /* Second argument to pass to callback */ |
143 ){ | 249 ){ |
144 int rc; /* Return code */ | 250 int rc; /* Return code */ |
145 int eType = pExpr->eType; /* Type of expression node pExpr */ | 251 int eType = pExpr->eType; /* Type of expression node pExpr */ |
146 | 252 |
147 if( eType!=FTSQUERY_PHRASE ){ | 253 if( eType!=FTSQUERY_PHRASE ){ |
148 assert( pExpr->pLeft && pExpr->pRight ); | 254 assert( pExpr->pLeft && pExpr->pRight ); |
149 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx); | 255 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx); |
150 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){ | 256 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){ |
151 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx); | 257 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx); |
152 } | 258 } |
153 }else{ | 259 }else{ |
154 rc = x(pExpr, *piPhrase, pCtx); | 260 rc = x(pExpr, *piPhrase, pCtx); |
155 (*piPhrase)++; | 261 (*piPhrase)++; |
(...skipping 13 matching lines...) Expand all Loading... |
169 */ | 275 */ |
170 static int fts3ExprIterate( | 276 static int fts3ExprIterate( |
171 Fts3Expr *pExpr, /* Expression to iterate phrases of */ | 277 Fts3Expr *pExpr, /* Expression to iterate phrases of */ |
172 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ | 278 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ |
173 void *pCtx /* Second argument to pass to callback */ | 279 void *pCtx /* Second argument to pass to callback */ |
174 ){ | 280 ){ |
175 int iPhrase = 0; /* Variable used as the phrase counter */ | 281 int iPhrase = 0; /* Variable used as the phrase counter */ |
176 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx); | 282 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx); |
177 } | 283 } |
178 | 284 |
| 285 |
179 /* | 286 /* |
180 ** This is an fts3ExprIterate() callback used while loading the doclists | 287 ** This is an fts3ExprIterate() callback used while loading the doclists |
181 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also | 288 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also |
182 ** fts3ExprLoadDoclists(). | 289 ** fts3ExprLoadDoclists(). |
183 */ | 290 */ |
184 static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ | 291 static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ |
185 int rc = SQLITE_OK; | 292 int rc = SQLITE_OK; |
186 Fts3Phrase *pPhrase = pExpr->pPhrase; | 293 Fts3Phrase *pPhrase = pExpr->pPhrase; |
187 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx; | 294 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx; |
188 | 295 |
(...skipping 24 matching lines...) Expand all Loading... |
213 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */ | 320 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */ |
214 sCtx.pCsr = pCsr; | 321 sCtx.pCsr = pCsr; |
215 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx); | 322 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx); |
216 if( pnPhrase ) *pnPhrase = sCtx.nPhrase; | 323 if( pnPhrase ) *pnPhrase = sCtx.nPhrase; |
217 if( pnToken ) *pnToken = sCtx.nToken; | 324 if( pnToken ) *pnToken = sCtx.nToken; |
218 return rc; | 325 return rc; |
219 } | 326 } |
220 | 327 |
221 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ | 328 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ |
222 (*(int *)ctx)++; | 329 (*(int *)ctx)++; |
223 UNUSED_PARAMETER(pExpr); | 330 pExpr->iPhrase = iPhrase; |
224 UNUSED_PARAMETER(iPhrase); | |
225 return SQLITE_OK; | 331 return SQLITE_OK; |
226 } | 332 } |
227 static int fts3ExprPhraseCount(Fts3Expr *pExpr){ | 333 static int fts3ExprPhraseCount(Fts3Expr *pExpr){ |
228 int nPhrase = 0; | 334 int nPhrase = 0; |
229 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); | 335 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); |
230 return nPhrase; | 336 return nPhrase; |
231 } | 337 } |
232 | 338 |
233 /* | 339 /* |
234 ** Advance the position list iterator specified by the first two | 340 ** Advance the position list iterator specified by the first two |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 memset(sIter.aPhrase, 0, nByte); | 541 memset(sIter.aPhrase, 0, nByte); |
436 | 542 |
437 /* Initialize the contents of the SnippetIter object. Then iterate through | 543 /* Initialize the contents of the SnippetIter object. Then iterate through |
438 ** the set of phrases in the expression to populate the aPhrase[] array. | 544 ** the set of phrases in the expression to populate the aPhrase[] array. |
439 */ | 545 */ |
440 sIter.pCsr = pCsr; | 546 sIter.pCsr = pCsr; |
441 sIter.iCol = iCol; | 547 sIter.iCol = iCol; |
442 sIter.nSnippet = nSnippet; | 548 sIter.nSnippet = nSnippet; |
443 sIter.nPhrase = nList; | 549 sIter.nPhrase = nList; |
444 sIter.iCurrent = -1; | 550 sIter.iCurrent = -1; |
445 (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter); | 551 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter); |
| 552 if( rc==SQLITE_OK ){ |
446 | 553 |
447 /* Set the *pmSeen output variable. */ | 554 /* Set the *pmSeen output variable. */ |
448 for(i=0; i<nList; i++){ | 555 for(i=0; i<nList; i++){ |
449 if( sIter.aPhrase[i].pHead ){ | 556 if( sIter.aPhrase[i].pHead ){ |
450 *pmSeen |= (u64)1 << i; | 557 *pmSeen |= (u64)1 << i; |
| 558 } |
451 } | 559 } |
| 560 |
| 561 /* Loop through all candidate snippets. Store the best snippet in |
| 562 ** *pFragment. Store its associated 'score' in iBestScore. |
| 563 */ |
| 564 pFragment->iCol = iCol; |
| 565 while( !fts3SnippetNextCandidate(&sIter) ){ |
| 566 int iPos; |
| 567 int iScore; |
| 568 u64 mCover; |
| 569 u64 mHighlite; |
| 570 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite); |
| 571 assert( iScore>=0 ); |
| 572 if( iScore>iBestScore ){ |
| 573 pFragment->iPos = iPos; |
| 574 pFragment->hlmask = mHighlite; |
| 575 pFragment->covered = mCover; |
| 576 iBestScore = iScore; |
| 577 } |
| 578 } |
| 579 |
| 580 *piScore = iBestScore; |
452 } | 581 } |
453 | |
454 /* Loop through all candidate snippets. Store the best snippet in | |
455 ** *pFragment. Store its associated 'score' in iBestScore. | |
456 */ | |
457 pFragment->iCol = iCol; | |
458 while( !fts3SnippetNextCandidate(&sIter) ){ | |
459 int iPos; | |
460 int iScore; | |
461 u64 mCover; | |
462 u64 mHighlight; | |
463 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight); | |
464 assert( iScore>=0 ); | |
465 if( iScore>iBestScore ){ | |
466 pFragment->iPos = iPos; | |
467 pFragment->hlmask = mHighlight; | |
468 pFragment->covered = mCover; | |
469 iBestScore = iScore; | |
470 } | |
471 } | |
472 | |
473 sqlite3_free(sIter.aPhrase); | 582 sqlite3_free(sIter.aPhrase); |
474 *piScore = iBestScore; | 583 return rc; |
475 return SQLITE_OK; | |
476 } | 584 } |
477 | 585 |
478 | 586 |
479 /* | 587 /* |
480 ** Append a string to the string-buffer passed as the first argument. | 588 ** Append a string to the string-buffer passed as the first argument. |
481 ** | 589 ** |
482 ** If nAppend is negative, then the length of the string zAppend is | 590 ** If nAppend is negative, then the length of the string zAppend is |
483 ** determined using strlen(). | 591 ** determined using strlen(). |
484 */ | 592 */ |
485 static int fts3StringAppend( | 593 static int fts3StringAppend( |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 int n = nDoc - iBegin; | 781 int n = nDoc - iBegin; |
674 rc = fts3SnippetShift( | 782 rc = fts3SnippetShift( |
675 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask | 783 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask |
676 ); | 784 ); |
677 isShiftDone = 1; | 785 isShiftDone = 1; |
678 | 786 |
679 /* Now that the shift has been done, check if the initial "..." are | 787 /* Now that the shift has been done, check if the initial "..." are |
680 ** required. They are required if (a) this is not the first fragment, | 788 ** required. They are required if (a) this is not the first fragment, |
681 ** or (b) this fragment does not begin at position 0 of its column. | 789 ** or (b) this fragment does not begin at position 0 of its column. |
682 */ | 790 */ |
683 if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){ | 791 if( rc==SQLITE_OK ){ |
684 rc = fts3StringAppend(pOut, zEllipsis, -1); | 792 if( iPos>0 || iFragment>0 ){ |
| 793 rc = fts3StringAppend(pOut, zEllipsis, -1); |
| 794 }else if( iBegin ){ |
| 795 rc = fts3StringAppend(pOut, zDoc, iBegin); |
| 796 } |
685 } | 797 } |
686 if( rc!=SQLITE_OK || iCurrent<iPos ) continue; | 798 if( rc!=SQLITE_OK || iCurrent<iPos ) continue; |
687 } | 799 } |
688 | 800 |
689 if( iCurrent>=(iPos+nSnippet) ){ | 801 if( iCurrent>=(iPos+nSnippet) ){ |
690 if( isLast ){ | 802 if( isLast ){ |
691 rc = fts3StringAppend(pOut, zEllipsis, -1); | 803 rc = fts3StringAppend(pOut, zEllipsis, -1); |
692 } | 804 } |
693 break; | 805 break; |
694 } | 806 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 while( 0xFE & (*pEnd | c) ){ | 843 while( 0xFE & (*pEnd | c) ){ |
732 c = *pEnd++ & 0x80; | 844 c = *pEnd++ & 0x80; |
733 if( !c ) nEntry++; | 845 if( !c ) nEntry++; |
734 } | 846 } |
735 | 847 |
736 *ppCollist = pEnd; | 848 *ppCollist = pEnd; |
737 return nEntry; | 849 return nEntry; |
738 } | 850 } |
739 | 851 |
740 /* | 852 /* |
| 853 ** This function gathers 'y' or 'b' data for a single phrase. |
| 854 */ |
| 855 static void fts3ExprLHits( |
| 856 Fts3Expr *pExpr, /* Phrase expression node */ |
| 857 MatchInfo *p /* Matchinfo context */ |
| 858 ){ |
| 859 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab; |
| 860 int iStart; |
| 861 Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 862 char *pIter = pPhrase->doclist.pList; |
| 863 int iCol = 0; |
| 864 |
| 865 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS ); |
| 866 if( p->flag==FTS3_MATCHINFO_LHITS ){ |
| 867 iStart = pExpr->iPhrase * p->nCol; |
| 868 }else{ |
| 869 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32); |
| 870 } |
| 871 |
| 872 while( 1 ){ |
| 873 int nHit = fts3ColumnlistCount(&pIter); |
| 874 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){ |
| 875 if( p->flag==FTS3_MATCHINFO_LHITS ){ |
| 876 p->aMatchinfo[iStart + iCol] = (u32)nHit; |
| 877 }else if( nHit ){ |
| 878 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F)); |
| 879 } |
| 880 } |
| 881 assert( *pIter==0x00 || *pIter==0x01 ); |
| 882 if( *pIter!=0x01 ) break; |
| 883 pIter++; |
| 884 pIter += fts3GetVarint32(pIter, &iCol); |
| 885 } |
| 886 } |
| 887 |
| 888 /* |
| 889 ** Gather the results for matchinfo directives 'y' and 'b'. |
| 890 */ |
| 891 static void fts3ExprLHitGather( |
| 892 Fts3Expr *pExpr, |
| 893 MatchInfo *p |
| 894 ){ |
| 895 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) ); |
| 896 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){ |
| 897 if( pExpr->pLeft ){ |
| 898 fts3ExprLHitGather(pExpr->pLeft, p); |
| 899 fts3ExprLHitGather(pExpr->pRight, p); |
| 900 }else{ |
| 901 fts3ExprLHits(pExpr, p); |
| 902 } |
| 903 } |
| 904 } |
| 905 |
| 906 /* |
741 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats | 907 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats |
742 ** for a single query. | 908 ** for a single query. |
743 ** | 909 ** |
744 ** fts3ExprIterate() callback to load the 'global' elements of a | 910 ** fts3ExprIterate() callback to load the 'global' elements of a |
745 ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements | 911 ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements |
746 ** of the matchinfo array that are constant for all rows returned by the | 912 ** of the matchinfo array that are constant for all rows returned by the |
747 ** current query. | 913 ** current query. |
748 ** | 914 ** |
749 ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This | 915 ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This |
750 ** function populates Matchinfo.aMatchinfo[] as follows: | 916 ** function populates Matchinfo.aMatchinfo[] as follows: |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 char cArg, | 974 char cArg, |
809 char **pzErr | 975 char **pzErr |
810 ){ | 976 ){ |
811 if( (cArg==FTS3_MATCHINFO_NPHRASE) | 977 if( (cArg==FTS3_MATCHINFO_NPHRASE) |
812 || (cArg==FTS3_MATCHINFO_NCOL) | 978 || (cArg==FTS3_MATCHINFO_NCOL) |
813 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4) | 979 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4) |
814 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4) | 980 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4) |
815 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize) | 981 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize) |
816 || (cArg==FTS3_MATCHINFO_LCS) | 982 || (cArg==FTS3_MATCHINFO_LCS) |
817 || (cArg==FTS3_MATCHINFO_HITS) | 983 || (cArg==FTS3_MATCHINFO_HITS) |
| 984 || (cArg==FTS3_MATCHINFO_LHITS) |
| 985 || (cArg==FTS3_MATCHINFO_LHITS_BM) |
818 ){ | 986 ){ |
819 return SQLITE_OK; | 987 return SQLITE_OK; |
820 } | 988 } |
821 *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg); | 989 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg); |
822 return SQLITE_ERROR; | 990 return SQLITE_ERROR; |
823 } | 991 } |
824 | 992 |
825 static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ | 993 static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ |
826 int nVal; /* Number of integers output by cArg */ | 994 int nVal; /* Number of integers output by cArg */ |
827 | 995 |
828 switch( cArg ){ | 996 switch( cArg ){ |
829 case FTS3_MATCHINFO_NDOC: | 997 case FTS3_MATCHINFO_NDOC: |
830 case FTS3_MATCHINFO_NPHRASE: | 998 case FTS3_MATCHINFO_NPHRASE: |
831 case FTS3_MATCHINFO_NCOL: | 999 case FTS3_MATCHINFO_NCOL: |
832 nVal = 1; | 1000 nVal = 1; |
833 break; | 1001 break; |
834 | 1002 |
835 case FTS3_MATCHINFO_AVGLENGTH: | 1003 case FTS3_MATCHINFO_AVGLENGTH: |
836 case FTS3_MATCHINFO_LENGTH: | 1004 case FTS3_MATCHINFO_LENGTH: |
837 case FTS3_MATCHINFO_LCS: | 1005 case FTS3_MATCHINFO_LCS: |
838 nVal = pInfo->nCol; | 1006 nVal = pInfo->nCol; |
839 break; | 1007 break; |
840 | 1008 |
| 1009 case FTS3_MATCHINFO_LHITS: |
| 1010 nVal = pInfo->nCol * pInfo->nPhrase; |
| 1011 break; |
| 1012 |
| 1013 case FTS3_MATCHINFO_LHITS_BM: |
| 1014 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32); |
| 1015 break; |
| 1016 |
841 default: | 1017 default: |
842 assert( cArg==FTS3_MATCHINFO_HITS ); | 1018 assert( cArg==FTS3_MATCHINFO_HITS ); |
843 nVal = pInfo->nCol * pInfo->nPhrase * 3; | 1019 nVal = pInfo->nCol * pInfo->nPhrase * 3; |
844 break; | 1020 break; |
845 } | 1021 } |
846 | 1022 |
847 return nVal; | 1023 return nVal; |
848 } | 1024 } |
849 | 1025 |
850 static int fts3MatchinfoSelectDoctotal( | 1026 static int fts3MatchinfoSelectDoctotal( |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 int bGlobal, /* True to grab the global stats */ | 1201 int bGlobal, /* True to grab the global stats */ |
1026 MatchInfo *pInfo, /* Matchinfo context object */ | 1202 MatchInfo *pInfo, /* Matchinfo context object */ |
1027 const char *zArg /* Matchinfo format string */ | 1203 const char *zArg /* Matchinfo format string */ |
1028 ){ | 1204 ){ |
1029 int rc = SQLITE_OK; | 1205 int rc = SQLITE_OK; |
1030 int i; | 1206 int i; |
1031 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; | 1207 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
1032 sqlite3_stmt *pSelect = 0; | 1208 sqlite3_stmt *pSelect = 0; |
1033 | 1209 |
1034 for(i=0; rc==SQLITE_OK && zArg[i]; i++){ | 1210 for(i=0; rc==SQLITE_OK && zArg[i]; i++){ |
1035 | 1211 pInfo->flag = zArg[i]; |
1036 switch( zArg[i] ){ | 1212 switch( zArg[i] ){ |
1037 case FTS3_MATCHINFO_NPHRASE: | 1213 case FTS3_MATCHINFO_NPHRASE: |
1038 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase; | 1214 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase; |
1039 break; | 1215 break; |
1040 | 1216 |
1041 case FTS3_MATCHINFO_NCOL: | 1217 case FTS3_MATCHINFO_NCOL: |
1042 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol; | 1218 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol; |
1043 break; | 1219 break; |
1044 | 1220 |
1045 case FTS3_MATCHINFO_NDOC: | 1221 case FTS3_MATCHINFO_NDOC: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1085 break; | 1261 break; |
1086 } | 1262 } |
1087 | 1263 |
1088 case FTS3_MATCHINFO_LCS: | 1264 case FTS3_MATCHINFO_LCS: |
1089 rc = fts3ExprLoadDoclists(pCsr, 0, 0); | 1265 rc = fts3ExprLoadDoclists(pCsr, 0, 0); |
1090 if( rc==SQLITE_OK ){ | 1266 if( rc==SQLITE_OK ){ |
1091 rc = fts3MatchinfoLcs(pCsr, pInfo); | 1267 rc = fts3MatchinfoLcs(pCsr, pInfo); |
1092 } | 1268 } |
1093 break; | 1269 break; |
1094 | 1270 |
| 1271 case FTS3_MATCHINFO_LHITS_BM: |
| 1272 case FTS3_MATCHINFO_LHITS: { |
| 1273 int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32); |
| 1274 memset(pInfo->aMatchinfo, 0, nZero); |
| 1275 fts3ExprLHitGather(pCsr->pExpr, pInfo); |
| 1276 break; |
| 1277 } |
| 1278 |
1095 default: { | 1279 default: { |
1096 Fts3Expr *pExpr; | 1280 Fts3Expr *pExpr; |
1097 assert( zArg[i]==FTS3_MATCHINFO_HITS ); | 1281 assert( zArg[i]==FTS3_MATCHINFO_HITS ); |
1098 pExpr = pCsr->pExpr; | 1282 pExpr = pCsr->pExpr; |
1099 rc = fts3ExprLoadDoclists(pCsr, 0, 0); | 1283 rc = fts3ExprLoadDoclists(pCsr, 0, 0); |
1100 if( rc!=SQLITE_OK ) break; | 1284 if( rc!=SQLITE_OK ) break; |
1101 if( bGlobal ){ | 1285 if( bGlobal ){ |
1102 if( pCsr->pDeferred ){ | 1286 if( pCsr->pDeferred ){ |
1103 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0); | 1287 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0); |
1104 if( rc!=SQLITE_OK ) break; | 1288 if( rc!=SQLITE_OK ) break; |
1105 } | 1289 } |
1106 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); | 1290 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); |
| 1291 sqlite3Fts3EvalTestDeferred(pCsr, &rc); |
1107 if( rc!=SQLITE_OK ) break; | 1292 if( rc!=SQLITE_OK ) break; |
1108 } | 1293 } |
1109 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); | 1294 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); |
1110 break; | 1295 break; |
1111 } | 1296 } |
1112 } | 1297 } |
1113 | 1298 |
1114 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]); | 1299 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]); |
1115 } | 1300 } |
1116 | 1301 |
1117 sqlite3_reset(pSelect); | 1302 sqlite3_reset(pSelect); |
1118 return rc; | 1303 return rc; |
1119 } | 1304 } |
1120 | 1305 |
1121 | 1306 |
1122 /* | 1307 /* |
1123 ** Populate pCsr->aMatchinfo[] with data for the current row. The | 1308 ** Populate pCsr->aMatchinfo[] with data for the current row. The |
1124 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32). | 1309 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32). |
1125 */ | 1310 */ |
1126 static int fts3GetMatchinfo( | 1311 static void fts3GetMatchinfo( |
| 1312 sqlite3_context *pCtx, /* Return results here */ |
1127 Fts3Cursor *pCsr, /* FTS3 Cursor object */ | 1313 Fts3Cursor *pCsr, /* FTS3 Cursor object */ |
1128 const char *zArg /* Second argument to matchinfo() function */ | 1314 const char *zArg /* Second argument to matchinfo() function */ |
1129 ){ | 1315 ){ |
1130 MatchInfo sInfo; | 1316 MatchInfo sInfo; |
1131 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; | 1317 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
1132 int rc = SQLITE_OK; | 1318 int rc = SQLITE_OK; |
1133 int bGlobal = 0; /* Collect 'global' stats as well as local */ | 1319 int bGlobal = 0; /* Collect 'global' stats as well as local */ |
1134 | 1320 |
| 1321 u32 *aOut = 0; |
| 1322 void (*xDestroyOut)(void*) = 0; |
| 1323 |
1135 memset(&sInfo, 0, sizeof(MatchInfo)); | 1324 memset(&sInfo, 0, sizeof(MatchInfo)); |
1136 sInfo.pCursor = pCsr; | 1325 sInfo.pCursor = pCsr; |
1137 sInfo.nCol = pTab->nColumn; | 1326 sInfo.nCol = pTab->nColumn; |
1138 | 1327 |
1139 /* If there is cached matchinfo() data, but the format string for the | 1328 /* If there is cached matchinfo() data, but the format string for the |
1140 ** cache does not match the format string for this request, discard | 1329 ** cache does not match the format string for this request, discard |
1141 ** the cached data. */ | 1330 ** the cached data. */ |
1142 if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){ | 1331 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){ |
1143 assert( pCsr->aMatchinfo ); | 1332 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); |
1144 sqlite3_free(pCsr->aMatchinfo); | 1333 pCsr->pMIBuffer = 0; |
1145 pCsr->zMatchinfo = 0; | |
1146 pCsr->aMatchinfo = 0; | |
1147 } | 1334 } |
1148 | 1335 |
1149 /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the | 1336 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the |
1150 ** matchinfo function has been called for this query. In this case | 1337 ** matchinfo function has been called for this query. In this case |
1151 ** allocate the array used to accumulate the matchinfo data and | 1338 ** allocate the array used to accumulate the matchinfo data and |
1152 ** initialize those elements that are constant for every row. | 1339 ** initialize those elements that are constant for every row. |
1153 */ | 1340 */ |
1154 if( pCsr->aMatchinfo==0 ){ | 1341 if( pCsr->pMIBuffer==0 ){ |
1155 int nMatchinfo = 0; /* Number of u32 elements in match-info */ | 1342 int nMatchinfo = 0; /* Number of u32 elements in match-info */ |
1156 int nArg; /* Bytes in zArg */ | |
1157 int i; /* Used to iterate through zArg */ | 1343 int i; /* Used to iterate through zArg */ |
1158 | 1344 |
1159 /* Determine the number of phrases in the query */ | 1345 /* Determine the number of phrases in the query */ |
1160 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr); | 1346 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr); |
1161 sInfo.nPhrase = pCsr->nPhrase; | 1347 sInfo.nPhrase = pCsr->nPhrase; |
1162 | 1348 |
1163 /* Determine the number of integers in the buffer returned by this call. */ | 1349 /* Determine the number of integers in the buffer returned by this call. */ |
1164 for(i=0; zArg[i]; i++){ | 1350 for(i=0; zArg[i]; i++){ |
| 1351 char *zErr = 0; |
| 1352 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){ |
| 1353 sqlite3_result_error(pCtx, zErr, -1); |
| 1354 sqlite3_free(zErr); |
| 1355 return; |
| 1356 } |
1165 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]); | 1357 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]); |
1166 } | 1358 } |
1167 | 1359 |
1168 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */ | 1360 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */ |
1169 nArg = (int)strlen(zArg); | 1361 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg); |
1170 pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1); | 1362 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM; |
1171 if( !pCsr->aMatchinfo ) return SQLITE_NOMEM; | |
1172 | 1363 |
1173 pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo]; | |
1174 pCsr->nMatchinfo = nMatchinfo; | |
1175 memcpy(pCsr->zMatchinfo, zArg, nArg+1); | |
1176 memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo); | |
1177 pCsr->isMatchinfoNeeded = 1; | 1364 pCsr->isMatchinfoNeeded = 1; |
1178 bGlobal = 1; | 1365 bGlobal = 1; |
1179 } | 1366 } |
1180 | 1367 |
1181 sInfo.aMatchinfo = pCsr->aMatchinfo; | 1368 if( rc==SQLITE_OK ){ |
1182 sInfo.nPhrase = pCsr->nPhrase; | 1369 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut); |
1183 if( pCsr->isMatchinfoNeeded ){ | 1370 if( xDestroyOut==0 ){ |
1184 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg); | 1371 rc = SQLITE_NOMEM; |
1185 pCsr->isMatchinfoNeeded = 0; | 1372 } |
1186 } | 1373 } |
1187 | 1374 |
1188 return rc; | 1375 if( rc==SQLITE_OK ){ |
| 1376 sInfo.aMatchinfo = aOut; |
| 1377 sInfo.nPhrase = pCsr->nPhrase; |
| 1378 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg); |
| 1379 if( bGlobal ){ |
| 1380 fts3MIBufferSetGlobal(pCsr->pMIBuffer); |
| 1381 } |
| 1382 } |
| 1383 |
| 1384 if( rc!=SQLITE_OK ){ |
| 1385 sqlite3_result_error_code(pCtx, rc); |
| 1386 if( xDestroyOut ) xDestroyOut(aOut); |
| 1387 }else{ |
| 1388 int n = pCsr->pMIBuffer->nElem * sizeof(u32); |
| 1389 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut); |
| 1390 } |
1189 } | 1391 } |
1190 | 1392 |
1191 /* | 1393 /* |
1192 ** Implementation of snippet() function. | 1394 ** Implementation of snippet() function. |
1193 */ | 1395 */ |
1194 void sqlite3Fts3Snippet( | 1396 void sqlite3Fts3Snippet( |
1195 sqlite3_context *pCtx, /* SQLite function call context */ | 1397 sqlite3_context *pCtx, /* SQLite function call context */ |
1196 Fts3Cursor *pCsr, /* Cursor object */ | 1398 Fts3Cursor *pCsr, /* Cursor object */ |
1197 const char *zStart, /* Snippet start text - "<b>" */ | 1399 const char *zStart, /* Snippet start text - "<b>" */ |
1198 const char *zEnd, /* Snippet end text - "</b>" */ | 1400 const char *zEnd, /* Snippet end text - "</b>" */ |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1240 SnippetFragment *pFragment = &aSnippet[iSnip]; | 1442 SnippetFragment *pFragment = &aSnippet[iSnip]; |
1241 | 1443 |
1242 memset(pFragment, 0, sizeof(*pFragment)); | 1444 memset(pFragment, 0, sizeof(*pFragment)); |
1243 | 1445 |
1244 /* Loop through all columns of the table being considered for snippets. | 1446 /* Loop through all columns of the table being considered for snippets. |
1245 ** If the iCol argument to this function was negative, this means all | 1447 ** If the iCol argument to this function was negative, this means all |
1246 ** columns of the FTS3 table. Otherwise, only column iCol is considered. | 1448 ** columns of the FTS3 table. Otherwise, only column iCol is considered. |
1247 */ | 1449 */ |
1248 for(iRead=0; iRead<pTab->nColumn; iRead++){ | 1450 for(iRead=0; iRead<pTab->nColumn; iRead++){ |
1249 SnippetFragment sF = {0, 0, 0, 0}; | 1451 SnippetFragment sF = {0, 0, 0, 0}; |
1250 int iS; | 1452 int iS = 0; |
1251 if( iCol>=0 && iRead!=iCol ) continue; | 1453 if( iCol>=0 && iRead!=iCol ) continue; |
1252 | 1454 |
1253 /* Find the best snippet of nFToken tokens in column iRead. */ | 1455 /* Find the best snippet of nFToken tokens in column iRead. */ |
1254 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS); | 1456 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS); |
1255 if( rc!=SQLITE_OK ){ | 1457 if( rc!=SQLITE_OK ){ |
1256 goto snippet_out; | 1458 goto snippet_out; |
1257 } | 1459 } |
1258 if( iS>iBestScore ){ | 1460 if( iS>iBestScore ){ |
1259 *pFragment = sF; | 1461 *pFragment = sF; |
1260 iBestScore = iS; | 1462 iBestScore = iS; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1384 int iCurrent = 0; | 1586 int iCurrent = 0; |
1385 const char *zDoc; | 1587 const char *zDoc; |
1386 int nDoc; | 1588 int nDoc; |
1387 | 1589 |
1388 /* Initialize the contents of sCtx.aTerm[] for column iCol. There is | 1590 /* Initialize the contents of sCtx.aTerm[] for column iCol. There is |
1389 ** no way that this operation can fail, so the return code from | 1591 ** no way that this operation can fail, so the return code from |
1390 ** fts3ExprIterate() can be discarded. | 1592 ** fts3ExprIterate() can be discarded. |
1391 */ | 1593 */ |
1392 sCtx.iCol = iCol; | 1594 sCtx.iCol = iCol; |
1393 sCtx.iTerm = 0; | 1595 sCtx.iTerm = 0; |
1394 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx); | 1596 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx); |
1395 | 1597 |
1396 /* Retreive the text stored in column iCol. If an SQL NULL is stored | 1598 /* Retreive the text stored in column iCol. If an SQL NULL is stored |
1397 ** in column iCol, jump immediately to the next iteration of the loop. | 1599 ** in column iCol, jump immediately to the next iteration of the loop. |
1398 ** If an OOM occurs while retrieving the data (this can happen if SQLite | 1600 ** If an OOM occurs while retrieving the data (this can happen if SQLite |
1399 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM | 1601 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM |
1400 ** to the caller. | 1602 ** to the caller. |
1401 */ | 1603 */ |
1402 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1); | 1604 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1); |
1403 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1); | 1605 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1); |
1404 if( zDoc==0 ){ | 1606 if( zDoc==0 ){ |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1476 | 1678 |
1477 /* | 1679 /* |
1478 ** Implementation of matchinfo() function. | 1680 ** Implementation of matchinfo() function. |
1479 */ | 1681 */ |
1480 void sqlite3Fts3Matchinfo( | 1682 void sqlite3Fts3Matchinfo( |
1481 sqlite3_context *pContext, /* Function call context */ | 1683 sqlite3_context *pContext, /* Function call context */ |
1482 Fts3Cursor *pCsr, /* FTS3 table cursor */ | 1684 Fts3Cursor *pCsr, /* FTS3 table cursor */ |
1483 const char *zArg /* Second arg to matchinfo() function */ | 1685 const char *zArg /* Second arg to matchinfo() function */ |
1484 ){ | 1686 ){ |
1485 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; | 1687 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
1486 int rc; | |
1487 int i; | |
1488 const char *zFormat; | 1688 const char *zFormat; |
1489 | 1689 |
1490 if( zArg ){ | 1690 if( zArg ){ |
1491 for(i=0; zArg[i]; i++){ | |
1492 char *zErr = 0; | |
1493 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){ | |
1494 sqlite3_result_error(pContext, zErr, -1); | |
1495 sqlite3_free(zErr); | |
1496 return; | |
1497 } | |
1498 } | |
1499 zFormat = zArg; | 1691 zFormat = zArg; |
1500 }else{ | 1692 }else{ |
1501 zFormat = FTS3_MATCHINFO_DEFAULT; | 1693 zFormat = FTS3_MATCHINFO_DEFAULT; |
1502 } | 1694 } |
1503 | 1695 |
1504 if( !pCsr->pExpr ){ | 1696 if( !pCsr->pExpr ){ |
1505 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC); | 1697 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC); |
1506 return; | 1698 return; |
1507 } | |
1508 | |
1509 /* Retrieve matchinfo() data. */ | |
1510 rc = fts3GetMatchinfo(pCsr, zFormat); | |
1511 sqlite3Fts3SegmentsClose(pTab); | |
1512 | |
1513 if( rc!=SQLITE_OK ){ | |
1514 sqlite3_result_error_code(pContext, rc); | |
1515 }else{ | 1699 }else{ |
1516 int n = pCsr->nMatchinfo * sizeof(u32); | 1700 /* Retrieve matchinfo() data. */ |
1517 sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT); | 1701 fts3GetMatchinfo(pContext, pCsr, zFormat); |
| 1702 sqlite3Fts3SegmentsClose(pTab); |
1518 } | 1703 } |
1519 } | 1704 } |
1520 | 1705 |
1521 #endif | 1706 #endif |
OLD | NEW |