1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Cleanup the hash functions in FTS3. (CVS 4440)

FossilOrigin-Name: ac645c8f30aac0d98fc481260084c9bd3975a845
This commit is contained in:
drh
2007-09-20 12:53:27 +00:00
parent 38514a9fb2
commit 8a07c7a414
4 changed files with 47 additions and 45 deletions

View File

@ -31,13 +31,19 @@
#include "fts3_hash.h"
static void *malloc_and_zero(int n){
void *p = malloc(n);
/*
** Malloc and Free functions
*/
static void *fts3HashMalloc(int n){
void *p = sqlite3_malloc(n);
if( p ){
memset(p, 0, n);
}
return p;
}
static void fts3HashFree(void *p){
sqlite3_free(p);
}
/* Turn bulk memory into a hash table object by initializing the
** fields of the Hash structure.
@ -58,8 +64,6 @@ void sqlite3Fts3HashInit(fts3Hash *pNew, int keyClass, int copyKey){
pNew->count = 0;
pNew->htsize = 0;
pNew->ht = 0;
pNew->xMalloc = malloc_and_zero;
pNew->xFree = free;
}
/* Remove all entries from a hash table. Reclaim all memory.
@ -72,15 +76,15 @@ void sqlite3Fts3HashClear(fts3Hash *pH){
assert( pH!=0 );
elem = pH->first;
pH->first = 0;
if( pH->ht ) pH->xFree(pH->ht);
fts3HashFree(pH->ht);
pH->ht = 0;
pH->htsize = 0;
while( elem ){
fts3HashElem *next_elem = elem->next;
if( pH->copyKey && elem->pKey ){
pH->xFree(elem->pKey);
fts3HashFree(elem->pKey);
}
pH->xFree(elem);
fts3HashFree(elem);
elem = next_elem;
}
pH->count = 0;
@ -89,7 +93,7 @@ void sqlite3Fts3HashClear(fts3Hash *pH){
/*
** Hash and comparison functions when the mode is FTS3_HASH_STRING
*/
static int strHash(const void *pKey, int nKey){
static int fts3StrHash(const void *pKey, int nKey){
const char *z = (const char *)pKey;
int h = 0;
if( nKey<=0 ) nKey = (int) strlen(z);
@ -99,7 +103,7 @@ static int strHash(const void *pKey, int nKey){
}
return h & 0x7fffffff;
}
static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
if( n1!=n2 ) return 1;
return strncmp((const char*)pKey1,(const char*)pKey2,n1);
}
@ -107,7 +111,7 @@ static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
/*
** Hash and comparison functions when the mode is FTS3_HASH_BINARY
*/
static int binHash(const void *pKey, int nKey){
static int fts3BinHash(const void *pKey, int nKey){
int h = 0;
const char *z = (const char *)pKey;
while( nKey-- > 0 ){
@ -115,7 +119,7 @@ static int binHash(const void *pKey, int nKey){
}
return h & 0x7fffffff;
}
static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
if( n1!=n2 ) return 1;
return memcmp(pKey1,pKey2,n1);
}
@ -134,10 +138,10 @@ static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
*/
static int (*hashFunction(int keyClass))(const void*,int){
if( keyClass==FTS3_HASH_STRING ){
return &strHash;
return &fts3StrHash;
}else{
assert( keyClass==FTS3_HASH_BINARY );
return &binHash;
return &fts3BinHash;
}
}
@ -149,16 +153,16 @@ static int (*hashFunction(int keyClass))(const void*,int){
*/
static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
if( keyClass==FTS3_HASH_STRING ){
return &strCompare;
return &fts3StrCompare;
}else{
assert( keyClass==FTS3_HASH_BINARY );
return &binCompare;
return &fts3BinCompare;
}
}
/* Link an element into the hash table
*/
static void insertElement(
static void fts3HashInsertElement(
fts3Hash *pH, /* The complete hash table */
struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
fts3HashElem *pNew /* The element to be inserted */
@ -186,22 +190,22 @@ static void insertElement(
** "new_size" must be a power of 2. The hash table might fail
** to resize if sqliteMalloc() fails.
*/
static void rehash(fts3Hash *pH, int new_size){
static void fts3Rehash(fts3Hash *pH, int new_size){
struct _fts3ht *new_ht; /* The new hash table */
fts3HashElem *elem, *next_elem; /* For looping over existing elements */
int (*xHash)(const void*,int); /* The hash function */
assert( (new_size & (new_size-1))==0 );
new_ht = (struct _fts3ht *)pH->xMalloc( new_size*sizeof(struct _fts3ht) );
new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
if( new_ht==0 ) return;
if( pH->ht ) pH->xFree(pH->ht);
fts3HashFree(pH->ht);
pH->ht = new_ht;
pH->htsize = new_size;
xHash = hashFunction(pH->keyClass);
for(elem=pH->first, pH->first=0; elem; elem = next_elem){
int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
next_elem = elem->next;
insertElement(pH, &new_ht[h], elem);
fts3HashInsertElement(pH, &new_ht[h], elem);
}
}
@ -209,7 +213,7 @@ static void rehash(fts3Hash *pH, int new_size){
** hash table that matches the given key. The hash for this key has
** already been computed and is passed as the 4th parameter.
*/
static fts3HashElem *findElementGivenHash(
static fts3HashElem *fts3FindElementByHash(
const fts3Hash *pH, /* The pH to be searched */
const void *pKey, /* The key we are searching for */
int nKey,
@ -237,7 +241,7 @@ static fts3HashElem *findElementGivenHash(
/* Remove a single entry from the hash table given a pointer to that
** element and a hash on the element's key.
*/
static void removeElementGivenHash(
static void fts3RemoveElementByHash(
fts3Hash *pH, /* The pH containing "elem" */
fts3HashElem* elem, /* The element to be removed from the pH */
int h /* Hash value for the element */
@ -260,9 +264,9 @@ static void removeElementGivenHash(
pEntry->chain = 0;
}
if( pH->copyKey && elem->pKey ){
pH->xFree(elem->pKey);
fts3HashFree(elem->pKey);
}
pH->xFree( elem );
fts3HashFree( elem );
pH->count--;
if( pH->count<=0 ){
assert( pH->first==0 );
@ -285,7 +289,7 @@ void *sqlite3Fts3HashFind(const fts3Hash *pH, const void *pKey, int nKey){
assert( xHash!=0 );
h = (*xHash)(pKey,nKey);
assert( (pH->htsize & (pH->htsize-1))==0 );
elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
elem = fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
return elem ? elem->data : 0;
}
@ -322,23 +326,23 @@ void *sqlite3Fts3HashInsert(
hraw = (*xHash)(pKey, nKey);
assert( (pH->htsize & (pH->htsize-1))==0 );
h = hraw & (pH->htsize-1);
elem = findElementGivenHash(pH,pKey,nKey,h);
elem = fts3FindElementByHash(pH,pKey,nKey,h);
if( elem ){
void *old_data = elem->data;
if( data==0 ){
removeElementGivenHash(pH,elem,h);
fts3RemoveElementByHash(pH,elem,h);
}else{
elem->data = data;
}
return old_data;
}
if( data==0 ) return 0;
new_elem = (fts3HashElem*)pH->xMalloc( sizeof(fts3HashElem) );
new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) );
if( new_elem==0 ) return data;
if( pH->copyKey && pKey!=0 ){
new_elem->pKey = pH->xMalloc( nKey );
new_elem->pKey = fts3HashMalloc( nKey );
if( new_elem->pKey==0 ){
pH->xFree(new_elem);
fts3HashFree(new_elem);
return data;
}
memcpy((void*)new_elem->pKey, pKey, nKey);
@ -348,20 +352,20 @@ void *sqlite3Fts3HashInsert(
new_elem->nKey = nKey;
pH->count++;
if( pH->htsize==0 ){
rehash(pH,8);
fts3Rehash(pH,8);
if( pH->htsize==0 ){
pH->count = 0;
pH->xFree(new_elem);
fts3HashFree(new_elem);
return data;
}
}
if( pH->count > pH->htsize ){
rehash(pH,pH->htsize*2);
fts3Rehash(pH,pH->htsize*2);
}
assert( pH->htsize>0 );
assert( (pH->htsize & (pH->htsize-1))==0 );
h = hraw & (pH->htsize-1);
insertElement(pH, &pH->ht[h], new_elem);
fts3HashInsertElement(pH, &pH->ht[h], new_elem);
new_elem->data = data;
return 0;
}

View File

@ -34,8 +34,6 @@ struct fts3Hash {
char copyKey; /* True if copy of key made on insert */
int count; /* Number of entries in this table */
fts3HashElem *first; /* The first element of the array */
void *(*xMalloc)(int); /* malloc() function to use */
void (*xFree)(void *); /* free() function to use */
int htsize; /* Number of buckets in the hash table */
struct _fts3ht { /* the hash table */
int count; /* Number of entries with this hash */