OLD | NEW |
1 /* | 1 /* |
2 ** 2014 May 31 | 2 ** 2014 May 31 |
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 | 14 |
15 | 15 |
16 #include "fts5Int.h" | 16 #include "fts5Int.h" |
17 | 17 |
18 int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, int nByte){ | 18 int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){ |
19 int nNew = pBuf->nSpace ? pBuf->nSpace*2 : 64; | 19 if( (u32)pBuf->nSpace<nByte ){ |
20 u8 *pNew; | 20 u32 nNew = pBuf->nSpace ? pBuf->nSpace : 64; |
21 while( nNew<nByte ){ | 21 u8 *pNew; |
22 nNew = nNew * 2; | 22 while( nNew<nByte ){ |
23 } | 23 nNew = nNew * 2; |
24 pNew = sqlite3_realloc(pBuf->p, nNew); | 24 } |
25 if( pNew==0 ){ | 25 pNew = sqlite3_realloc(pBuf->p, nNew); |
26 *pRc = SQLITE_NOMEM; | 26 if( pNew==0 ){ |
27 return 1; | 27 *pRc = SQLITE_NOMEM; |
28 }else{ | 28 return 1; |
29 pBuf->nSpace = nNew; | 29 }else{ |
30 pBuf->p = pNew; | 30 pBuf->nSpace = nNew; |
| 31 pBuf->p = pNew; |
| 32 } |
31 } | 33 } |
32 return 0; | 34 return 0; |
33 } | 35 } |
34 | 36 |
35 | 37 |
36 /* | 38 /* |
37 ** Encode value iVal as an SQLite varint and append it to the buffer object | 39 ** Encode value iVal as an SQLite varint and append it to the buffer object |
38 ** pBuf. If an OOM error occurs, set the error code in p. | 40 ** pBuf. If an OOM error occurs, set the error code in p. |
39 */ | 41 */ |
40 void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){ | 42 void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){ |
(...skipping 13 matching lines...) Expand all Loading... |
54 } | 56 } |
55 | 57 |
56 /* | 58 /* |
57 ** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set | 59 ** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set |
58 ** the error code in p. If an error has already occurred when this function | 60 ** the error code in p. If an error has already occurred when this function |
59 ** is called, it is a no-op. | 61 ** is called, it is a no-op. |
60 */ | 62 */ |
61 void sqlite3Fts5BufferAppendBlob( | 63 void sqlite3Fts5BufferAppendBlob( |
62 int *pRc, | 64 int *pRc, |
63 Fts5Buffer *pBuf, | 65 Fts5Buffer *pBuf, |
64 int nData, | 66 u32 nData, |
65 const u8 *pData | 67 const u8 *pData |
66 ){ | 68 ){ |
67 assert( *pRc || nData>=0 ); | 69 assert_nc( *pRc || nData>=0 ); |
68 if( fts5BufferGrow(pRc, pBuf, nData) ) return; | 70 if( fts5BufferGrow(pRc, pBuf, nData) ) return; |
69 memcpy(&pBuf->p[pBuf->n], pData, nData); | 71 memcpy(&pBuf->p[pBuf->n], pData, nData); |
70 pBuf->n += nData; | 72 pBuf->n += nData; |
71 } | 73 } |
72 | 74 |
73 /* | 75 /* |
74 ** Append the nul-terminated string zStr to the buffer pBuf. This function | 76 ** Append the nul-terminated string zStr to the buffer pBuf. This function |
75 ** ensures that the byte following the buffer data is set to 0x00, even | 77 ** ensures that the byte following the buffer data is set to 0x00, even |
76 ** though this byte is not included in the pBuf->n count. | 78 ** though this byte is not included in the pBuf->n count. |
77 */ | 79 */ |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 const u8 *a, int n, /* Poslist buffer to iterate through */ | 203 const u8 *a, int n, /* Poslist buffer to iterate through */ |
202 Fts5PoslistReader *pIter /* Iterator object to initialize */ | 204 Fts5PoslistReader *pIter /* Iterator object to initialize */ |
203 ){ | 205 ){ |
204 memset(pIter, 0, sizeof(*pIter)); | 206 memset(pIter, 0, sizeof(*pIter)); |
205 pIter->a = a; | 207 pIter->a = a; |
206 pIter->n = n; | 208 pIter->n = n; |
207 sqlite3Fts5PoslistReaderNext(pIter); | 209 sqlite3Fts5PoslistReaderNext(pIter); |
208 return pIter->bEof; | 210 return pIter->bEof; |
209 } | 211 } |
210 | 212 |
| 213 /* |
| 214 ** Append position iPos to the position list being accumulated in buffer |
| 215 ** pBuf, which must be already be large enough to hold the new data. |
| 216 ** The previous position written to this list is *piPrev. *piPrev is set |
| 217 ** to iPos before returning. |
| 218 */ |
| 219 void sqlite3Fts5PoslistSafeAppend( |
| 220 Fts5Buffer *pBuf, |
| 221 i64 *piPrev, |
| 222 i64 iPos |
| 223 ){ |
| 224 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32; |
| 225 if( (iPos & colmask) != (*piPrev & colmask) ){ |
| 226 pBuf->p[pBuf->n++] = 1; |
| 227 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32)); |
| 228 *piPrev = (iPos & colmask); |
| 229 } |
| 230 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2); |
| 231 *piPrev = iPos; |
| 232 } |
| 233 |
211 int sqlite3Fts5PoslistWriterAppend( | 234 int sqlite3Fts5PoslistWriterAppend( |
212 Fts5Buffer *pBuf, | 235 Fts5Buffer *pBuf, |
213 Fts5PoslistWriter *pWriter, | 236 Fts5PoslistWriter *pWriter, |
214 i64 iPos | 237 i64 iPos |
215 ){ | 238 ){ |
216 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32; | 239 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */ |
217 int rc = SQLITE_OK; | 240 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc; |
218 if( 0==fts5BufferGrow(&rc, pBuf, 5+5+5) ){ | 241 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos); |
219 if( (iPos & colmask) != (pWriter->iPrev & colmask) ){ | 242 return SQLITE_OK; |
220 pBuf->p[pBuf->n++] = 1; | |
221 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32)); | |
222 pWriter->iPrev = (iPos & colmask); | |
223 } | |
224 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-pWriter->iPrev)+2); | |
225 pWriter->iPrev = iPos; | |
226 } | |
227 return rc; | |
228 } | 243 } |
229 | 244 |
230 void *sqlite3Fts5MallocZero(int *pRc, int nByte){ | 245 void *sqlite3Fts5MallocZero(int *pRc, int nByte){ |
231 void *pRet = 0; | 246 void *pRet = 0; |
232 if( *pRc==SQLITE_OK ){ | 247 if( *pRc==SQLITE_OK ){ |
233 pRet = sqlite3_malloc(nByte); | 248 pRet = sqlite3_malloc(nByte); |
234 if( pRet==0 && nByte>0 ){ | 249 if( pRet==0 && nByte>0 ){ |
235 *pRc = SQLITE_NOMEM; | 250 *pRc = SQLITE_NOMEM; |
236 }else{ | 251 }else{ |
237 memset(pRet, 0, nByte); | 252 memset(pRet, 0, nByte); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */ | 300 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */ |
286 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */ | 301 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */ |
287 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */ | 302 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */ |
288 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */ | 303 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */ |
289 }; | 304 }; |
290 | 305 |
291 return (t & 0x80) || aBareword[(int)t]; | 306 return (t & 0x80) || aBareword[(int)t]; |
292 } | 307 } |
293 | 308 |
294 | 309 |
| 310 /************************************************************************* |
| 311 */ |
| 312 typedef struct Fts5TermsetEntry Fts5TermsetEntry; |
| 313 struct Fts5TermsetEntry { |
| 314 char *pTerm; |
| 315 int nTerm; |
| 316 int iIdx; /* Index (main or aPrefix[] entry) */ |
| 317 Fts5TermsetEntry *pNext; |
| 318 }; |
| 319 |
| 320 struct Fts5Termset { |
| 321 Fts5TermsetEntry *apHash[512]; |
| 322 }; |
| 323 |
| 324 int sqlite3Fts5TermsetNew(Fts5Termset **pp){ |
| 325 int rc = SQLITE_OK; |
| 326 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset)); |
| 327 return rc; |
| 328 } |
| 329 |
| 330 int sqlite3Fts5TermsetAdd( |
| 331 Fts5Termset *p, |
| 332 int iIdx, |
| 333 const char *pTerm, int nTerm, |
| 334 int *pbPresent |
| 335 ){ |
| 336 int rc = SQLITE_OK; |
| 337 *pbPresent = 0; |
| 338 if( p ){ |
| 339 int i; |
| 340 u32 hash = 13; |
| 341 Fts5TermsetEntry *pEntry; |
| 342 |
| 343 /* Calculate a hash value for this term. This is the same hash checksum |
| 344 ** used by the fts5_hash.c module. This is not important for correct |
| 345 ** operation of the module, but is necessary to ensure that some tests |
| 346 ** designed to produce hash table collisions really do work. */ |
| 347 for(i=nTerm-1; i>=0; i--){ |
| 348 hash = (hash << 3) ^ hash ^ pTerm[i]; |
| 349 } |
| 350 hash = (hash << 3) ^ hash ^ iIdx; |
| 351 hash = hash % ArraySize(p->apHash); |
| 352 |
| 353 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){ |
| 354 if( pEntry->iIdx==iIdx |
| 355 && pEntry->nTerm==nTerm |
| 356 && memcmp(pEntry->pTerm, pTerm, nTerm)==0 |
| 357 ){ |
| 358 *pbPresent = 1; |
| 359 break; |
| 360 } |
| 361 } |
| 362 |
| 363 if( pEntry==0 ){ |
| 364 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm); |
| 365 if( pEntry ){ |
| 366 pEntry->pTerm = (char*)&pEntry[1]; |
| 367 pEntry->nTerm = nTerm; |
| 368 pEntry->iIdx = iIdx; |
| 369 memcpy(pEntry->pTerm, pTerm, nTerm); |
| 370 pEntry->pNext = p->apHash[hash]; |
| 371 p->apHash[hash] = pEntry; |
| 372 } |
| 373 } |
| 374 } |
| 375 |
| 376 return rc; |
| 377 } |
| 378 |
| 379 void sqlite3Fts5TermsetFree(Fts5Termset *p){ |
| 380 if( p ){ |
| 381 u32 i; |
| 382 for(i=0; i<ArraySize(p->apHash); i++){ |
| 383 Fts5TermsetEntry *pEntry = p->apHash[i]; |
| 384 while( pEntry ){ |
| 385 Fts5TermsetEntry *pDel = pEntry; |
| 386 pEntry = pEntry->pNext; |
| 387 sqlite3_free(pDel); |
| 388 } |
| 389 } |
| 390 sqlite3_free(p); |
| 391 } |
| 392 } |
OLD | NEW |