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

View File

@ -34,8 +34,6 @@ struct fts3Hash {
char copyKey; /* True if copy of key made on insert */ char copyKey; /* True if copy of key made on insert */
int count; /* Number of entries in this table */ int count; /* Number of entries in this table */
fts3HashElem *first; /* The first element of the array */ 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 */ int htsize; /* Number of buckets in the hash table */
struct _fts3ht { /* the hash table */ struct _fts3ht { /* the hash table */
int count; /* Number of entries with this hash */ int count; /* Number of entries with this hash */

View File

@ -1,5 +1,5 @@
C get\srid\sof\sremaining\sGCC\s4.3\s-Wall\scompiler\swarnings\sby\sinitializing\stwo\svariables\sand\sone\sstructure\sproperly\s(although\sthe\scode\spath\swas\salready\srather\ssafe)\s(CVS\s4439) C Cleanup\sthe\shash\sfunctions\sin\sFTS3.\s(CVS\s4440)
D 2007-09-20T11:34:18 D 2007-09-20T12:53:28
F Makefile.in cbfb898945536a8f9ea8b897e1586dd1fdbcc5db F Makefile.in cbfb898945536a8f9ea8b897e1586dd1fdbcc5db
F Makefile.linux-gcc 65241babba6faf1152bf86574477baab19190499 F Makefile.linux-gcc 65241babba6faf1152bf86574477baab19190499
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -51,8 +51,8 @@ F ext/fts3/README.tokenizers a97c9a55b3422f6cb04af9de9296fe2447ea4a78
F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
F ext/fts3/fts3.c 6b390b5054f9267ee5778dccffc856a3d70e7b70 F ext/fts3/fts3.c 6b390b5054f9267ee5778dccffc856a3d70e7b70
F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe
F ext/fts3/fts3_hash.c 84654768178452b00bbc986dd878a8299dc1e3dc F ext/fts3/fts3_hash.c 1c2dc969a5b485848fb804c0ac41a046f18a09c9
F ext/fts3/fts3_hash.h af585d6867d478fc0457f64cfaae60e09541e63a F ext/fts3/fts3_hash.h 004b759e1602ff16dfa02fea3ca1c77336ad6798
F ext/fts3/fts3_icu.c 35a5d08fea8f12edecb8a58fdb33452eb8f17948 F ext/fts3/fts3_icu.c 35a5d08fea8f12edecb8a58fdb33452eb8f17948
F ext/fts3/fts3_porter.c a3e823a0a8fbb038750c9aa55043e19bdb08eacb F ext/fts3/fts3_porter.c a3e823a0a8fbb038750c9aa55043e19bdb08eacb
F ext/fts3/fts3_tokenizer.c 81e7604555b24dce6bc487d8169ca4ef51c71e4c F ext/fts3/fts3_tokenizer.c 81e7604555b24dce6bc487d8169ca4ef51c71e4c
@ -580,7 +580,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5 F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5
P c2ac43a4ef674c0202d5bd1ec57fc25c89d0554e P d748694f8d9b14d98b6fe7ceb404754692761705
R b948484588679e72e10618f1789eb46d R 8fa4ab546f014755f6bea9393d1cafc9
U rse U drh
Z 4b5325865a92adc308765583c725dbd7 Z ed8c3b918a22b61a19882d5a8de5f03a

View File

@ -1 +1 @@
d748694f8d9b14d98b6fe7ceb404754692761705 ac645c8f30aac0d98fc481260084c9bd3975a845