OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** 2014 May 31 |
| 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 |
| 15 |
| 16 #include "fts5Int.h" |
| 17 |
| 18 int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){ |
| 19 if( (u32)pBuf->nSpace<nByte ){ |
| 20 u32 nNew = pBuf->nSpace ? pBuf->nSpace : 64; |
| 21 u8 *pNew; |
| 22 while( nNew<nByte ){ |
| 23 nNew = nNew * 2; |
| 24 } |
| 25 pNew = sqlite3_realloc(pBuf->p, nNew); |
| 26 if( pNew==0 ){ |
| 27 *pRc = SQLITE_NOMEM; |
| 28 return 1; |
| 29 }else{ |
| 30 pBuf->nSpace = nNew; |
| 31 pBuf->p = pNew; |
| 32 } |
| 33 } |
| 34 return 0; |
| 35 } |
| 36 |
| 37 |
| 38 /* |
| 39 ** Encode value iVal as an SQLite varint and append it to the buffer object |
| 40 ** pBuf. If an OOM error occurs, set the error code in p. |
| 41 */ |
| 42 void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){ |
| 43 if( fts5BufferGrow(pRc, pBuf, 9) ) return; |
| 44 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal); |
| 45 } |
| 46 |
| 47 void sqlite3Fts5Put32(u8 *aBuf, int iVal){ |
| 48 aBuf[0] = (iVal>>24) & 0x00FF; |
| 49 aBuf[1] = (iVal>>16) & 0x00FF; |
| 50 aBuf[2] = (iVal>> 8) & 0x00FF; |
| 51 aBuf[3] = (iVal>> 0) & 0x00FF; |
| 52 } |
| 53 |
| 54 int sqlite3Fts5Get32(const u8 *aBuf){ |
| 55 return (aBuf[0] << 24) + (aBuf[1] << 16) + (aBuf[2] << 8) + aBuf[3]; |
| 56 } |
| 57 |
| 58 /* |
| 59 ** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set |
| 60 ** the error code in p. If an error has already occurred when this function |
| 61 ** is called, it is a no-op. |
| 62 */ |
| 63 void sqlite3Fts5BufferAppendBlob( |
| 64 int *pRc, |
| 65 Fts5Buffer *pBuf, |
| 66 u32 nData, |
| 67 const u8 *pData |
| 68 ){ |
| 69 assert_nc( *pRc || nData>=0 ); |
| 70 if( fts5BufferGrow(pRc, pBuf, nData) ) return; |
| 71 memcpy(&pBuf->p[pBuf->n], pData, nData); |
| 72 pBuf->n += nData; |
| 73 } |
| 74 |
| 75 /* |
| 76 ** Append the nul-terminated string zStr to the buffer pBuf. This function |
| 77 ** ensures that the byte following the buffer data is set to 0x00, even |
| 78 ** though this byte is not included in the pBuf->n count. |
| 79 */ |
| 80 void sqlite3Fts5BufferAppendString( |
| 81 int *pRc, |
| 82 Fts5Buffer *pBuf, |
| 83 const char *zStr |
| 84 ){ |
| 85 int nStr = (int)strlen(zStr); |
| 86 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr); |
| 87 pBuf->n--; |
| 88 } |
| 89 |
| 90 /* |
| 91 ** Argument zFmt is a printf() style format string. This function performs |
| 92 ** the printf() style processing, then appends the results to buffer pBuf. |
| 93 ** |
| 94 ** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte |
| 95 ** following the buffer data is set to 0x00, even though this byte is not |
| 96 ** included in the pBuf->n count. |
| 97 */ |
| 98 void sqlite3Fts5BufferAppendPrintf( |
| 99 int *pRc, |
| 100 Fts5Buffer *pBuf, |
| 101 char *zFmt, ... |
| 102 ){ |
| 103 if( *pRc==SQLITE_OK ){ |
| 104 char *zTmp; |
| 105 va_list ap; |
| 106 va_start(ap, zFmt); |
| 107 zTmp = sqlite3_vmprintf(zFmt, ap); |
| 108 va_end(ap); |
| 109 |
| 110 if( zTmp==0 ){ |
| 111 *pRc = SQLITE_NOMEM; |
| 112 }else{ |
| 113 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp); |
| 114 sqlite3_free(zTmp); |
| 115 } |
| 116 } |
| 117 } |
| 118 |
| 119 char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){ |
| 120 char *zRet = 0; |
| 121 if( *pRc==SQLITE_OK ){ |
| 122 va_list ap; |
| 123 va_start(ap, zFmt); |
| 124 zRet = sqlite3_vmprintf(zFmt, ap); |
| 125 va_end(ap); |
| 126 if( zRet==0 ){ |
| 127 *pRc = SQLITE_NOMEM; |
| 128 } |
| 129 } |
| 130 return zRet; |
| 131 } |
| 132 |
| 133 |
| 134 /* |
| 135 ** Free any buffer allocated by pBuf. Zero the structure before returning. |
| 136 */ |
| 137 void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){ |
| 138 sqlite3_free(pBuf->p); |
| 139 memset(pBuf, 0, sizeof(Fts5Buffer)); |
| 140 } |
| 141 |
| 142 /* |
| 143 ** Zero the contents of the buffer object. But do not free the associated |
| 144 ** memory allocation. |
| 145 */ |
| 146 void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){ |
| 147 pBuf->n = 0; |
| 148 } |
| 149 |
| 150 /* |
| 151 ** Set the buffer to contain nData/pData. If an OOM error occurs, leave an |
| 152 ** the error code in p. If an error has already occurred when this function |
| 153 ** is called, it is a no-op. |
| 154 */ |
| 155 void sqlite3Fts5BufferSet( |
| 156 int *pRc, |
| 157 Fts5Buffer *pBuf, |
| 158 int nData, |
| 159 const u8 *pData |
| 160 ){ |
| 161 pBuf->n = 0; |
| 162 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData); |
| 163 } |
| 164 |
| 165 int sqlite3Fts5PoslistNext64( |
| 166 const u8 *a, int n, /* Buffer containing poslist */ |
| 167 int *pi, /* IN/OUT: Offset within a[] */ |
| 168 i64 *piOff /* IN/OUT: Current offset */ |
| 169 ){ |
| 170 int i = *pi; |
| 171 if( i>=n ){ |
| 172 /* EOF */ |
| 173 *piOff = -1; |
| 174 return 1; |
| 175 }else{ |
| 176 i64 iOff = *piOff; |
| 177 int iVal; |
| 178 fts5FastGetVarint32(a, i, iVal); |
| 179 if( iVal==1 ){ |
| 180 fts5FastGetVarint32(a, i, iVal); |
| 181 iOff = ((i64)iVal) << 32; |
| 182 fts5FastGetVarint32(a, i, iVal); |
| 183 } |
| 184 *piOff = iOff + (iVal-2); |
| 185 *pi = i; |
| 186 return 0; |
| 187 } |
| 188 } |
| 189 |
| 190 |
| 191 /* |
| 192 ** Advance the iterator object passed as the only argument. Return true |
| 193 ** if the iterator reaches EOF, or false otherwise. |
| 194 */ |
| 195 int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){ |
| 196 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){ |
| 197 pIter->bEof = 1; |
| 198 } |
| 199 return pIter->bEof; |
| 200 } |
| 201 |
| 202 int sqlite3Fts5PoslistReaderInit( |
| 203 const u8 *a, int n, /* Poslist buffer to iterate through */ |
| 204 Fts5PoslistReader *pIter /* Iterator object to initialize */ |
| 205 ){ |
| 206 memset(pIter, 0, sizeof(*pIter)); |
| 207 pIter->a = a; |
| 208 pIter->n = n; |
| 209 sqlite3Fts5PoslistReaderNext(pIter); |
| 210 return pIter->bEof; |
| 211 } |
| 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 |
| 234 int sqlite3Fts5PoslistWriterAppend( |
| 235 Fts5Buffer *pBuf, |
| 236 Fts5PoslistWriter *pWriter, |
| 237 i64 iPos |
| 238 ){ |
| 239 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */ |
| 240 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc; |
| 241 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos); |
| 242 return SQLITE_OK; |
| 243 } |
| 244 |
| 245 void *sqlite3Fts5MallocZero(int *pRc, int nByte){ |
| 246 void *pRet = 0; |
| 247 if( *pRc==SQLITE_OK ){ |
| 248 pRet = sqlite3_malloc(nByte); |
| 249 if( pRet==0 && nByte>0 ){ |
| 250 *pRc = SQLITE_NOMEM; |
| 251 }else{ |
| 252 memset(pRet, 0, nByte); |
| 253 } |
| 254 } |
| 255 return pRet; |
| 256 } |
| 257 |
| 258 /* |
| 259 ** Return a nul-terminated copy of the string indicated by pIn. If nIn |
| 260 ** is non-negative, then it is the length of the string in bytes. Otherwise, |
| 261 ** the length of the string is determined using strlen(). |
| 262 ** |
| 263 ** It is the responsibility of the caller to eventually free the returned |
| 264 ** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned. |
| 265 */ |
| 266 char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){ |
| 267 char *zRet = 0; |
| 268 if( *pRc==SQLITE_OK ){ |
| 269 if( nIn<0 ){ |
| 270 nIn = (int)strlen(pIn); |
| 271 } |
| 272 zRet = (char*)sqlite3_malloc(nIn+1); |
| 273 if( zRet ){ |
| 274 memcpy(zRet, pIn, nIn); |
| 275 zRet[nIn] = '\0'; |
| 276 }else{ |
| 277 *pRc = SQLITE_NOMEM; |
| 278 } |
| 279 } |
| 280 return zRet; |
| 281 } |
| 282 |
| 283 |
| 284 /* |
| 285 ** Return true if character 't' may be part of an FTS5 bareword, or false |
| 286 ** otherwise. Characters that may be part of barewords: |
| 287 ** |
| 288 ** * All non-ASCII characters, |
| 289 ** * The 52 upper and lower case ASCII characters, and |
| 290 ** * The 10 integer ASCII characters. |
| 291 ** * The underscore character "_" (0x5F). |
| 292 ** * The unicode "subsitute" character (0x1A). |
| 293 */ |
| 294 int sqlite3Fts5IsBareword(char t){ |
| 295 u8 aBareword[128] = { |
| 296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */ |
| 297 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */ |
| 298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */ |
| 299 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */ |
| 300 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */ |
| 301 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */ |
| 302 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */ |
| 303 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */ |
| 304 }; |
| 305 |
| 306 return (t & 0x80) || aBareword[(int)t]; |
| 307 } |
| 308 |
| 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 |