4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This is the implementation of generic hash-tables used in SQLite.
13 ** We've modified it slightly to serve as a standalone hash table
14 ** implementation for the full-text indexing module.
18 ** The code in this file is only compiled if:
20 ** * The FTS2 module is being built as an extension
21 ** (in which case SQLITE_CORE is not defined), or
23 ** * The FTS2 module is being built into the core of
24 ** SQLite (in which case SQLITE_ENABLE_FTS2 is defined).
26 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2)
33 #include "sqlite3ext.h"
34 SQLITE_EXTENSION_INIT3
35 #include "fts2_hash.h"
38 ** Malloc and Free functions
40 static void *fts2HashMalloc(int n
){
41 void *p
= sqlite3_malloc(n
);
47 static void fts2HashFree(void *p
){
51 /* Turn bulk memory into a hash table object by initializing the
52 ** fields of the Hash structure.
54 ** "pNew" is a pointer to the hash table that is to be initialized.
55 ** keyClass is one of the constants
56 ** FTS2_HASH_BINARY or FTS2_HASH_STRING. The value of keyClass
57 ** determines what kind of key the hash table will use. "copyKey" is
58 ** true if the hash table should make its own private copy of keys and
59 ** false if it should just use the supplied pointer.
61 void sqlite3Fts2HashInit(fts2Hash
*pNew
, int keyClass
, int copyKey
){
63 assert( keyClass
>=FTS2_HASH_STRING
&& keyClass
<=FTS2_HASH_BINARY
);
64 pNew
->keyClass
= keyClass
;
65 pNew
->copyKey
= copyKey
;
72 /* Remove all entries from a hash table. Reclaim all memory.
73 ** Call this routine to delete a hash table or to reset a hash table
74 ** to the empty state.
76 void sqlite3Fts2HashClear(fts2Hash
*pH
){
77 fts2HashElem
*elem
; /* For looping over all elements of the table */
86 fts2HashElem
*next_elem
= elem
->next
;
87 if( pH
->copyKey
&& elem
->pKey
){
88 fts2HashFree(elem
->pKey
);
97 ** Hash and comparison functions when the mode is FTS2_HASH_STRING
99 static int strHash(const void *pKey
, int nKey
){
100 const char *z
= (const char *)pKey
;
102 if( nKey
<=0 ) nKey
= (int) strlen(z
);
104 h
= (h
<<3) ^ h
^ *z
++;
107 return h
& 0x7fffffff;
109 static int strCompare(const void *pKey1
, int n1
, const void *pKey2
, int n2
){
110 if( n1
!=n2
) return 1;
111 return strncmp((const char*)pKey1
,(const char*)pKey2
,n1
);
115 ** Hash and comparison functions when the mode is FTS2_HASH_BINARY
117 static int binHash(const void *pKey
, int nKey
){
119 const char *z
= (const char *)pKey
;
121 h
= (h
<<3) ^ h
^ *(z
++);
123 return h
& 0x7fffffff;
125 static int binCompare(const void *pKey1
, int n1
, const void *pKey2
, int n2
){
126 if( n1
!=n2
) return 1;
127 return memcmp(pKey1
,pKey2
,n1
);
131 ** Return a pointer to the appropriate hash function given the key class.
133 ** The C syntax in this function definition may be unfamilar to some
134 ** programmers, so we provide the following additional explanation:
136 ** The name of the function is "hashFunction". The function takes a
137 ** single parameter "keyClass". The return value of hashFunction()
138 ** is a pointer to another function. Specifically, the return value
139 ** of hashFunction() is a pointer to a function that takes two parameters
140 ** with types "const void*" and "int" and returns an "int".
142 static int (*hashFunction(int keyClass
))(const void*,int){
143 if( keyClass
==FTS2_HASH_STRING
){
146 assert( keyClass
==FTS2_HASH_BINARY
);
152 ** Return a pointer to the appropriate hash function given the key class.
154 ** For help in interpreted the obscure C code in the function definition,
155 ** see the header comment on the previous function.
157 static int (*compareFunction(int keyClass
))(const void*,int,const void*,int){
158 if( keyClass
==FTS2_HASH_STRING
){
161 assert( keyClass
==FTS2_HASH_BINARY
);
166 /* Link an element into the hash table
168 static void insertElement(
169 fts2Hash
*pH
, /* The complete hash table */
170 struct _fts2ht
*pEntry
, /* The entry into which pNew is inserted */
171 fts2HashElem
*pNew
/* The element to be inserted */
173 fts2HashElem
*pHead
; /* First element already in pEntry */
174 pHead
= pEntry
->chain
;
177 pNew
->prev
= pHead
->prev
;
178 if( pHead
->prev
){ pHead
->prev
->next
= pNew
; }
179 else { pH
->first
= pNew
; }
182 pNew
->next
= pH
->first
;
183 if( pH
->first
){ pH
->first
->prev
= pNew
; }
188 pEntry
->chain
= pNew
;
192 /* Resize the hash table so that it cantains "new_size" buckets.
193 ** "new_size" must be a power of 2. The hash table might fail
194 ** to resize if sqliteMalloc() fails.
196 static void rehash(fts2Hash
*pH
, int new_size
){
197 struct _fts2ht
*new_ht
; /* The new hash table */
198 fts2HashElem
*elem
, *next_elem
; /* For looping over existing elements */
199 int (*xHash
)(const void*,int); /* The hash function */
201 assert( (new_size
& (new_size
-1))==0 );
202 new_ht
= (struct _fts2ht
*)fts2HashMalloc( new_size
*sizeof(struct _fts2ht
) );
203 if( new_ht
==0 ) return;
204 fts2HashFree(pH
->ht
);
206 pH
->htsize
= new_size
;
207 xHash
= hashFunction(pH
->keyClass
);
208 for(elem
=pH
->first
, pH
->first
=0; elem
; elem
= next_elem
){
209 int h
= (*xHash
)(elem
->pKey
, elem
->nKey
) & (new_size
-1);
210 next_elem
= elem
->next
;
211 insertElement(pH
, &new_ht
[h
], elem
);
215 /* This function (for internal use only) locates an element in an
216 ** hash table that matches the given key. The hash for this key has
217 ** already been computed and is passed as the 4th parameter.
219 static fts2HashElem
*findElementGivenHash(
220 const fts2Hash
*pH
, /* The pH to be searched */
221 const void *pKey
, /* The key we are searching for */
223 int h
/* The hash for this key. */
225 fts2HashElem
*elem
; /* Used to loop thru the element list */
226 int count
; /* Number of elements left to test */
227 int (*xCompare
)(const void*,int,const void*,int); /* comparison function */
230 struct _fts2ht
*pEntry
= &pH
->ht
[h
];
231 elem
= pEntry
->chain
;
232 count
= pEntry
->count
;
233 xCompare
= compareFunction(pH
->keyClass
);
234 while( count
-- && elem
){
235 if( (*xCompare
)(elem
->pKey
,elem
->nKey
,pKey
,nKey
)==0 ){
244 /* Remove a single entry from the hash table given a pointer to that
245 ** element and a hash on the element's key.
247 static void removeElementGivenHash(
248 fts2Hash
*pH
, /* The pH containing "elem" */
249 fts2HashElem
* elem
, /* The element to be removed from the pH */
250 int h
/* Hash value for the element */
252 struct _fts2ht
*pEntry
;
254 elem
->prev
->next
= elem
->next
;
256 pH
->first
= elem
->next
;
259 elem
->next
->prev
= elem
->prev
;
262 if( pEntry
->chain
==elem
){
263 pEntry
->chain
= elem
->next
;
266 if( pEntry
->count
<=0 ){
269 if( pH
->copyKey
&& elem
->pKey
){
270 fts2HashFree(elem
->pKey
);
272 fts2HashFree( elem
);
275 assert( pH
->first
==0 );
276 assert( pH
->count
==0 );
281 /* Attempt to locate an element of the hash table pH with a key
282 ** that matches pKey,nKey. Return the data for this element if it is
283 ** found, or NULL if there is no match.
285 void *sqlite3Fts2HashFind(const fts2Hash
*pH
, const void *pKey
, int nKey
){
286 int h
; /* A hash on key */
287 fts2HashElem
*elem
; /* The element that matches key */
288 int (*xHash
)(const void*,int); /* The hash function */
290 if( pH
==0 || pH
->ht
==0 ) return 0;
291 xHash
= hashFunction(pH
->keyClass
);
293 h
= (*xHash
)(pKey
,nKey
);
294 assert( (pH
->htsize
& (pH
->htsize
-1))==0 );
295 elem
= findElementGivenHash(pH
,pKey
,nKey
, h
& (pH
->htsize
-1));
296 return elem
? elem
->data
: 0;
299 /* Insert an element into the hash table pH. The key is pKey,nKey
300 ** and the data is "data".
302 ** If no element exists with a matching key, then a new
303 ** element is created. A copy of the key is made if the copyKey
304 ** flag is set. NULL is returned.
306 ** If another element already exists with the same key, then the
307 ** new data replaces the old data and the old data is returned.
308 ** The key is not copied in this instance. If a malloc fails, then
309 ** the new data is returned and the hash table is unchanged.
311 ** If the "data" parameter to this function is NULL, then the
312 ** element corresponding to "key" is removed from the hash table.
314 void *sqlite3Fts2HashInsert(
315 fts2Hash
*pH
, /* The hash table to insert into */
316 const void *pKey
, /* The key */
317 int nKey
, /* Number of bytes in the key */
318 void *data
/* The data */
320 int hraw
; /* Raw hash value of the key */
321 int h
; /* the hash of the key modulo hash table size */
322 fts2HashElem
*elem
; /* Used to loop thru the element list */
323 fts2HashElem
*new_elem
; /* New element added to the pH */
324 int (*xHash
)(const void*,int); /* The hash function */
327 xHash
= hashFunction(pH
->keyClass
);
329 hraw
= (*xHash
)(pKey
, nKey
);
330 assert( (pH
->htsize
& (pH
->htsize
-1))==0 );
331 h
= hraw
& (pH
->htsize
-1);
332 elem
= findElementGivenHash(pH
,pKey
,nKey
,h
);
334 void *old_data
= elem
->data
;
336 removeElementGivenHash(pH
,elem
,h
);
342 if( data
==0 ) return 0;
343 new_elem
= (fts2HashElem
*)fts2HashMalloc( sizeof(fts2HashElem
) );
344 if( new_elem
==0 ) return data
;
345 if( pH
->copyKey
&& pKey
!=0 ){
346 new_elem
->pKey
= fts2HashMalloc( nKey
);
347 if( new_elem
->pKey
==0 ){
348 fts2HashFree(new_elem
);
351 memcpy((void*)new_elem
->pKey
, pKey
, nKey
);
353 new_elem
->pKey
= (void*)pKey
;
355 new_elem
->nKey
= nKey
;
361 fts2HashFree(new_elem
);
365 if( pH
->count
> pH
->htsize
){
366 rehash(pH
,pH
->htsize
*2);
368 assert( pH
->htsize
>0 );
369 assert( (pH
->htsize
& (pH
->htsize
-1))==0 );
370 h
= hraw
& (pH
->htsize
-1);
371 insertElement(pH
, &pH
->ht
[h
], new_elem
);
372 new_elem
->data
= data
;
376 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */