| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2001 September 22 | 2 ** 2001 September 22 |
| 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 ** |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 /* Turn bulk memory into a hash table object by initializing the | 49 /* Turn bulk memory into a hash table object by initializing the |
| 50 ** fields of the Hash structure. | 50 ** fields of the Hash structure. |
| 51 ** | 51 ** |
| 52 ** "pNew" is a pointer to the hash table that is to be initialized. | 52 ** "pNew" is a pointer to the hash table that is to be initialized. |
| 53 ** keyClass is one of the constants | 53 ** keyClass is one of the constants |
| 54 ** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass | 54 ** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass |
| 55 ** determines what kind of key the hash table will use. "copyKey" is | 55 ** determines what kind of key the hash table will use. "copyKey" is |
| 56 ** true if the hash table should make its own private copy of keys and | 56 ** true if the hash table should make its own private copy of keys and |
| 57 ** false if it should just use the supplied pointer. | 57 ** false if it should just use the supplied pointer. |
| 58 */ | 58 */ |
| 59 void sqlite3Fts3HashInit(fts3Hash *pNew, int keyClass, int copyKey){ | 59 void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){ |
| 60 assert( pNew!=0 ); | 60 assert( pNew!=0 ); |
| 61 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY ); | 61 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY ); |
| 62 pNew->keyClass = keyClass; | 62 pNew->keyClass = keyClass; |
| 63 pNew->copyKey = copyKey; | 63 pNew->copyKey = copyKey; |
| 64 pNew->first = 0; | 64 pNew->first = 0; |
| 65 pNew->count = 0; | 65 pNew->count = 0; |
| 66 pNew->htsize = 0; | 66 pNew->htsize = 0; |
| 67 pNew->ht = 0; | 67 pNew->ht = 0; |
| 68 } | 68 } |
| 69 | 69 |
| 70 /* Remove all entries from a hash table. Reclaim all memory. | 70 /* Remove all entries from a hash table. Reclaim all memory. |
| 71 ** Call this routine to delete a hash table or to reset a hash table | 71 ** Call this routine to delete a hash table or to reset a hash table |
| 72 ** to the empty state. | 72 ** to the empty state. |
| 73 */ | 73 */ |
| 74 void sqlite3Fts3HashClear(fts3Hash *pH){ | 74 void sqlite3Fts3HashClear(Fts3Hash *pH){ |
| 75 fts3HashElem *elem; /* For looping over all elements of the table */ | 75 Fts3HashElem *elem; /* For looping over all elements of the table */ |
| 76 | 76 |
| 77 assert( pH!=0 ); | 77 assert( pH!=0 ); |
| 78 elem = pH->first; | 78 elem = pH->first; |
| 79 pH->first = 0; | 79 pH->first = 0; |
| 80 fts3HashFree(pH->ht); | 80 fts3HashFree(pH->ht); |
| 81 pH->ht = 0; | 81 pH->ht = 0; |
| 82 pH->htsize = 0; | 82 pH->htsize = 0; |
| 83 while( elem ){ | 83 while( elem ){ |
| 84 fts3HashElem *next_elem = elem->next; | 84 Fts3HashElem *next_elem = elem->next; |
| 85 if( pH->copyKey && elem->pKey ){ | 85 if( pH->copyKey && elem->pKey ){ |
| 86 fts3HashFree(elem->pKey); | 86 fts3HashFree(elem->pKey); |
| 87 } | 87 } |
| 88 fts3HashFree(elem); | 88 fts3HashFree(elem); |
| 89 elem = next_elem; | 89 elem = next_elem; |
| 90 } | 90 } |
| 91 pH->count = 0; | 91 pH->count = 0; |
| 92 } | 92 } |
| 93 | 93 |
| 94 /* | 94 /* |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return &fts3StrCompare; | 157 return &fts3StrCompare; |
| 158 }else{ | 158 }else{ |
| 159 assert( keyClass==FTS3_HASH_BINARY ); | 159 assert( keyClass==FTS3_HASH_BINARY ); |
| 160 return &fts3BinCompare; | 160 return &fts3BinCompare; |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 /* Link an element into the hash table | 164 /* Link an element into the hash table |
| 165 */ | 165 */ |
| 166 static void fts3HashInsertElement( | 166 static void fts3HashInsertElement( |
| 167 fts3Hash *pH, /* The complete hash table */ | 167 Fts3Hash *pH, /* The complete hash table */ |
| 168 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */ | 168 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */ |
| 169 fts3HashElem *pNew /* The element to be inserted */ | 169 Fts3HashElem *pNew /* The element to be inserted */ |
| 170 ){ | 170 ){ |
| 171 fts3HashElem *pHead; /* First element already in pEntry */ | 171 Fts3HashElem *pHead; /* First element already in pEntry */ |
| 172 pHead = pEntry->chain; | 172 pHead = pEntry->chain; |
| 173 if( pHead ){ | 173 if( pHead ){ |
| 174 pNew->next = pHead; | 174 pNew->next = pHead; |
| 175 pNew->prev = pHead->prev; | 175 pNew->prev = pHead->prev; |
| 176 if( pHead->prev ){ pHead->prev->next = pNew; } | 176 if( pHead->prev ){ pHead->prev->next = pNew; } |
| 177 else { pH->first = pNew; } | 177 else { pH->first = pNew; } |
| 178 pHead->prev = pNew; | 178 pHead->prev = pNew; |
| 179 }else{ | 179 }else{ |
| 180 pNew->next = pH->first; | 180 pNew->next = pH->first; |
| 181 if( pH->first ){ pH->first->prev = pNew; } | 181 if( pH->first ){ pH->first->prev = pNew; } |
| 182 pNew->prev = 0; | 182 pNew->prev = 0; |
| 183 pH->first = pNew; | 183 pH->first = pNew; |
| 184 } | 184 } |
| 185 pEntry->count++; | 185 pEntry->count++; |
| 186 pEntry->chain = pNew; | 186 pEntry->chain = pNew; |
| 187 } | 187 } |
| 188 | 188 |
| 189 | 189 |
| 190 /* Resize the hash table so that it cantains "new_size" buckets. | 190 /* Resize the hash table so that it cantains "new_size" buckets. |
| 191 ** "new_size" must be a power of 2. The hash table might fail | 191 ** "new_size" must be a power of 2. The hash table might fail |
| 192 ** to resize if sqliteMalloc() fails. | 192 ** to resize if sqliteMalloc() fails. |
| 193 ** |
| 194 ** Return non-zero if a memory allocation error occurs. |
| 193 */ | 195 */ |
| 194 static void fts3Rehash(fts3Hash *pH, int new_size){ | 196 static int fts3Rehash(Fts3Hash *pH, int new_size){ |
| 195 struct _fts3ht *new_ht; /* The new hash table */ | 197 struct _fts3ht *new_ht; /* The new hash table */ |
| 196 fts3HashElem *elem, *next_elem; /* For looping over existing elements */ | 198 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */ |
| 197 int (*xHash)(const void*,int); /* The hash function */ | 199 int (*xHash)(const void*,int); /* The hash function */ |
| 198 | 200 |
| 199 assert( (new_size & (new_size-1))==0 ); | 201 assert( (new_size & (new_size-1))==0 ); |
| 200 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) ); | 202 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) ); |
| 201 if( new_ht==0 ) return; | 203 if( new_ht==0 ) return 1; |
| 202 fts3HashFree(pH->ht); | 204 fts3HashFree(pH->ht); |
| 203 pH->ht = new_ht; | 205 pH->ht = new_ht; |
| 204 pH->htsize = new_size; | 206 pH->htsize = new_size; |
| 205 xHash = ftsHashFunction(pH->keyClass); | 207 xHash = ftsHashFunction(pH->keyClass); |
| 206 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ | 208 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ |
| 207 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); | 209 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); |
| 208 next_elem = elem->next; | 210 next_elem = elem->next; |
| 209 fts3HashInsertElement(pH, &new_ht[h], elem); | 211 fts3HashInsertElement(pH, &new_ht[h], elem); |
| 210 } | 212 } |
| 213 return 0; |
| 211 } | 214 } |
| 212 | 215 |
| 213 /* This function (for internal use only) locates an element in an | 216 /* This function (for internal use only) locates an element in an |
| 214 ** hash table that matches the given key. The hash for this key has | 217 ** hash table that matches the given key. The hash for this key has |
| 215 ** already been computed and is passed as the 4th parameter. | 218 ** already been computed and is passed as the 4th parameter. |
| 216 */ | 219 */ |
| 217 static fts3HashElem *fts3FindElementByHash( | 220 static Fts3HashElem *fts3FindElementByHash( |
| 218 const fts3Hash *pH, /* The pH to be searched */ | 221 const Fts3Hash *pH, /* The pH to be searched */ |
| 219 const void *pKey, /* The key we are searching for */ | 222 const void *pKey, /* The key we are searching for */ |
| 220 int nKey, | 223 int nKey, |
| 221 int h /* The hash for this key. */ | 224 int h /* The hash for this key. */ |
| 222 ){ | 225 ){ |
| 223 fts3HashElem *elem; /* Used to loop thru the element list */ | 226 Fts3HashElem *elem; /* Used to loop thru the element list */ |
| 224 int count; /* Number of elements left to test */ | 227 int count; /* Number of elements left to test */ |
| 225 int (*xCompare)(const void*,int,const void*,int); /* comparison function */ | 228 int (*xCompare)(const void*,int,const void*,int); /* comparison function */ |
| 226 | 229 |
| 227 if( pH->ht ){ | 230 if( pH->ht ){ |
| 228 struct _fts3ht *pEntry = &pH->ht[h]; | 231 struct _fts3ht *pEntry = &pH->ht[h]; |
| 229 elem = pEntry->chain; | 232 elem = pEntry->chain; |
| 230 count = pEntry->count; | 233 count = pEntry->count; |
| 231 xCompare = ftsCompareFunction(pH->keyClass); | 234 xCompare = ftsCompareFunction(pH->keyClass); |
| 232 while( count-- && elem ){ | 235 while( count-- && elem ){ |
| 233 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ | 236 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ |
| 234 return elem; | 237 return elem; |
| 235 } | 238 } |
| 236 elem = elem->next; | 239 elem = elem->next; |
| 237 } | 240 } |
| 238 } | 241 } |
| 239 return 0; | 242 return 0; |
| 240 } | 243 } |
| 241 | 244 |
| 242 /* Remove a single entry from the hash table given a pointer to that | 245 /* Remove a single entry from the hash table given a pointer to that |
| 243 ** element and a hash on the element's key. | 246 ** element and a hash on the element's key. |
| 244 */ | 247 */ |
| 245 static void fts3RemoveElementByHash( | 248 static void fts3RemoveElementByHash( |
| 246 fts3Hash *pH, /* The pH containing "elem" */ | 249 Fts3Hash *pH, /* The pH containing "elem" */ |
| 247 fts3HashElem* elem, /* The element to be removed from the pH */ | 250 Fts3HashElem* elem, /* The element to be removed from the pH */ |
| 248 int h /* Hash value for the element */ | 251 int h /* Hash value for the element */ |
| 249 ){ | 252 ){ |
| 250 struct _fts3ht *pEntry; | 253 struct _fts3ht *pEntry; |
| 251 if( elem->prev ){ | 254 if( elem->prev ){ |
| 252 elem->prev->next = elem->next; | 255 elem->prev->next = elem->next; |
| 253 }else{ | 256 }else{ |
| 254 pH->first = elem->next; | 257 pH->first = elem->next; |
| 255 } | 258 } |
| 256 if( elem->next ){ | 259 if( elem->next ){ |
| 257 elem->next->prev = elem->prev; | 260 elem->next->prev = elem->prev; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 269 } | 272 } |
| 270 fts3HashFree( elem ); | 273 fts3HashFree( elem ); |
| 271 pH->count--; | 274 pH->count--; |
| 272 if( pH->count<=0 ){ | 275 if( pH->count<=0 ){ |
| 273 assert( pH->first==0 ); | 276 assert( pH->first==0 ); |
| 274 assert( pH->count==0 ); | 277 assert( pH->count==0 ); |
| 275 fts3HashClear(pH); | 278 fts3HashClear(pH); |
| 276 } | 279 } |
| 277 } | 280 } |
| 278 | 281 |
| 279 /* Attempt to locate an element of the hash table pH with a key | 282 Fts3HashElem *sqlite3Fts3HashFindElem( |
| 280 ** that matches pKey,nKey. Return the data for this element if it is | 283 const Fts3Hash *pH, |
| 281 ** found, or NULL if there is no match. | 284 const void *pKey, |
| 282 */ | 285 int nKey |
| 283 void *sqlite3Fts3HashFind(const fts3Hash *pH, const void *pKey, int nKey){ | 286 ){ |
| 284 int h; /* A hash on key */ | 287 int h; /* A hash on key */ |
| 285 fts3HashElem *elem; /* The element that matches key */ | |
| 286 int (*xHash)(const void*,int); /* The hash function */ | 288 int (*xHash)(const void*,int); /* The hash function */ |
| 287 | 289 |
| 288 if( pH==0 || pH->ht==0 ) return 0; | 290 if( pH==0 || pH->ht==0 ) return 0; |
| 289 xHash = ftsHashFunction(pH->keyClass); | 291 xHash = ftsHashFunction(pH->keyClass); |
| 290 assert( xHash!=0 ); | 292 assert( xHash!=0 ); |
| 291 h = (*xHash)(pKey,nKey); | 293 h = (*xHash)(pKey,nKey); |
| 292 assert( (pH->htsize & (pH->htsize-1))==0 ); | 294 assert( (pH->htsize & (pH->htsize-1))==0 ); |
| 293 elem = fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1)); | 295 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1)); |
| 294 return elem ? elem->data : 0; | 296 } |
| 297 |
| 298 /* |
| 299 ** Attempt to locate an element of the hash table pH with a key |
| 300 ** that matches pKey,nKey. Return the data for this element if it is |
| 301 ** found, or NULL if there is no match. |
| 302 */ |
| 303 void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){ |
| 304 Fts3HashElem *pElem; /* The element that matches key (if any) */ |
| 305 |
| 306 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey); |
| 307 return pElem ? pElem->data : 0; |
| 295 } | 308 } |
| 296 | 309 |
| 297 /* Insert an element into the hash table pH. The key is pKey,nKey | 310 /* Insert an element into the hash table pH. The key is pKey,nKey |
| 298 ** and the data is "data". | 311 ** and the data is "data". |
| 299 ** | 312 ** |
| 300 ** If no element exists with a matching key, then a new | 313 ** If no element exists with a matching key, then a new |
| 301 ** element is created. A copy of the key is made if the copyKey | 314 ** element is created. A copy of the key is made if the copyKey |
| 302 ** flag is set. NULL is returned. | 315 ** flag is set. NULL is returned. |
| 303 ** | 316 ** |
| 304 ** If another element already exists with the same key, then the | 317 ** If another element already exists with the same key, then the |
| 305 ** new data replaces the old data and the old data is returned. | 318 ** new data replaces the old data and the old data is returned. |
| 306 ** The key is not copied in this instance. If a malloc fails, then | 319 ** The key is not copied in this instance. If a malloc fails, then |
| 307 ** the new data is returned and the hash table is unchanged. | 320 ** the new data is returned and the hash table is unchanged. |
| 308 ** | 321 ** |
| 309 ** If the "data" parameter to this function is NULL, then the | 322 ** If the "data" parameter to this function is NULL, then the |
| 310 ** element corresponding to "key" is removed from the hash table. | 323 ** element corresponding to "key" is removed from the hash table. |
| 311 */ | 324 */ |
| 312 void *sqlite3Fts3HashInsert( | 325 void *sqlite3Fts3HashInsert( |
| 313 fts3Hash *pH, /* The hash table to insert into */ | 326 Fts3Hash *pH, /* The hash table to insert into */ |
| 314 const void *pKey, /* The key */ | 327 const void *pKey, /* The key */ |
| 315 int nKey, /* Number of bytes in the key */ | 328 int nKey, /* Number of bytes in the key */ |
| 316 void *data /* The data */ | 329 void *data /* The data */ |
| 317 ){ | 330 ){ |
| 318 int hraw; /* Raw hash value of the key */ | 331 int hraw; /* Raw hash value of the key */ |
| 319 int h; /* the hash of the key modulo hash table size */ | 332 int h; /* the hash of the key modulo hash table size */ |
| 320 fts3HashElem *elem; /* Used to loop thru the element list */ | 333 Fts3HashElem *elem; /* Used to loop thru the element list */ |
| 321 fts3HashElem *new_elem; /* New element added to the pH */ | 334 Fts3HashElem *new_elem; /* New element added to the pH */ |
| 322 int (*xHash)(const void*,int); /* The hash function */ | 335 int (*xHash)(const void*,int); /* The hash function */ |
| 323 | 336 |
| 324 assert( pH!=0 ); | 337 assert( pH!=0 ); |
| 325 xHash = ftsHashFunction(pH->keyClass); | 338 xHash = ftsHashFunction(pH->keyClass); |
| 326 assert( xHash!=0 ); | 339 assert( xHash!=0 ); |
| 327 hraw = (*xHash)(pKey, nKey); | 340 hraw = (*xHash)(pKey, nKey); |
| 328 assert( (pH->htsize & (pH->htsize-1))==0 ); | 341 assert( (pH->htsize & (pH->htsize-1))==0 ); |
| 329 h = hraw & (pH->htsize-1); | 342 h = hraw & (pH->htsize-1); |
| 330 elem = fts3FindElementByHash(pH,pKey,nKey,h); | 343 elem = fts3FindElementByHash(pH,pKey,nKey,h); |
| 331 if( elem ){ | 344 if( elem ){ |
| 332 void *old_data = elem->data; | 345 void *old_data = elem->data; |
| 333 if( data==0 ){ | 346 if( data==0 ){ |
| 334 fts3RemoveElementByHash(pH,elem,h); | 347 fts3RemoveElementByHash(pH,elem,h); |
| 335 }else{ | 348 }else{ |
| 336 elem->data = data; | 349 elem->data = data; |
| 337 } | 350 } |
| 338 return old_data; | 351 return old_data; |
| 339 } | 352 } |
| 340 if( data==0 ) return 0; | 353 if( data==0 ) return 0; |
| 341 if( pH->htsize==0 ){ | 354 if( (pH->htsize==0 && fts3Rehash(pH,8)) |
| 342 fts3Rehash(pH,8); | 355 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2)) |
| 343 if( pH->htsize==0 ){ | 356 ){ |
| 344 pH->count = 0; | 357 pH->count = 0; |
| 345 return data; | 358 return data; |
| 346 } | |
| 347 } | 359 } |
| 348 new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) ); | 360 assert( pH->htsize>0 ); |
| 361 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) ); |
| 349 if( new_elem==0 ) return data; | 362 if( new_elem==0 ) return data; |
| 350 if( pH->copyKey && pKey!=0 ){ | 363 if( pH->copyKey && pKey!=0 ){ |
| 351 new_elem->pKey = fts3HashMalloc( nKey ); | 364 new_elem->pKey = fts3HashMalloc( nKey ); |
| 352 if( new_elem->pKey==0 ){ | 365 if( new_elem->pKey==0 ){ |
| 353 fts3HashFree(new_elem); | 366 fts3HashFree(new_elem); |
| 354 return data; | 367 return data; |
| 355 } | 368 } |
| 356 memcpy((void*)new_elem->pKey, pKey, nKey); | 369 memcpy((void*)new_elem->pKey, pKey, nKey); |
| 357 }else{ | 370 }else{ |
| 358 new_elem->pKey = (void*)pKey; | 371 new_elem->pKey = (void*)pKey; |
| 359 } | 372 } |
| 360 new_elem->nKey = nKey; | 373 new_elem->nKey = nKey; |
| 361 pH->count++; | 374 pH->count++; |
| 362 if( pH->count > pH->htsize ){ | |
| 363 fts3Rehash(pH,pH->htsize*2); | |
| 364 } | |
| 365 assert( pH->htsize>0 ); | 375 assert( pH->htsize>0 ); |
| 366 assert( (pH->htsize & (pH->htsize-1))==0 ); | 376 assert( (pH->htsize & (pH->htsize-1))==0 ); |
| 367 h = hraw & (pH->htsize-1); | 377 h = hraw & (pH->htsize-1); |
| 368 fts3HashInsertElement(pH, &pH->ht[h], new_elem); | 378 fts3HashInsertElement(pH, &pH->ht[h], new_elem); |
| 369 new_elem->data = data; | 379 new_elem->data = data; |
| 370 return 0; | 380 return 0; |
| 371 } | 381 } |
| 372 | 382 |
| 373 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ | 383 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |
| OLD | NEW |