blob: 9d9be94e748bb17c08478a849c62635f6eb021f6 [file] [log] [blame]
drhbeae3192001-09-22 18:12:08 +00001/*
2** 2001 September 22
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** This is the implementation of generic hash-tables
13** used in SQLite.
14**
drh8a1e5942009-04-28 15:43:45 +000015** $Id: hash.c,v 1.35 2009/04/28 15:43:45 drh Exp $
drhbeae3192001-09-22 18:12:08 +000016*/
17#include "sqliteInt.h"
18#include <assert.h>
19
20/* Turn bulk memory into a hash table object by initializing the
21** fields of the Hash structure.
drhaacc5432002-01-06 17:07:40 +000022**
drh76d7f8b2004-06-30 22:43:21 +000023** "pNew" is a pointer to the hash table that is to be initialized.
drh1b67f3c2008-10-10 17:41:28 +000024** "copyKey" is true if the hash table should make its own private
25** copy of keys and false if it should just use the supplied pointer.
drhbeae3192001-09-22 18:12:08 +000026*/
drh1b67f3c2008-10-10 17:41:28 +000027void sqlite3HashInit(Hash *pNew, int copyKey){
drh76d7f8b2004-06-30 22:43:21 +000028 assert( pNew!=0 );
drh1b67f3c2008-10-10 17:41:28 +000029 pNew->copyKey = copyKey!=0;
drh76d7f8b2004-06-30 22:43:21 +000030 pNew->first = 0;
31 pNew->count = 0;
32 pNew->htsize = 0;
33 pNew->ht = 0;
drhbeae3192001-09-22 18:12:08 +000034}
35
36/* Remove all entries from a hash table. Reclaim all memory.
drhaacc5432002-01-06 17:07:40 +000037** Call this routine to delete a hash table or to reset a hash table
38** to the empty state.
drhbeae3192001-09-22 18:12:08 +000039*/
danielk19774adee202004-05-08 08:23:19 +000040void sqlite3HashClear(Hash *pH){
drhbeae3192001-09-22 18:12:08 +000041 HashElem *elem; /* For looping over all elements of the table */
42
43 assert( pH!=0 );
44 elem = pH->first;
45 pH->first = 0;
drh41eb9e92008-04-02 18:33:07 +000046 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +000047 pH->ht = 0;
48 pH->htsize = 0;
49 while( elem ){
50 HashElem *next_elem = elem->next;
drhe2f02ba2009-01-09 01:12:27 +000051 if( pH->copyKey ){
drh17435752007-08-16 04:30:38 +000052 sqlite3_free(elem->pKey);
drhbeae3192001-09-22 18:12:08 +000053 }
drh17435752007-08-16 04:30:38 +000054 sqlite3_free(elem);
drhbeae3192001-09-22 18:12:08 +000055 elem = next_elem;
56 }
57 pH->count = 0;
58}
59
drhbeae3192001-09-22 18:12:08 +000060/*
61** Hash and comparison functions when the mode is SQLITE_HASH_STRING
62*/
drh8a1e5942009-04-28 15:43:45 +000063static unsigned int strHash(const void *pKey, int nKey){
danielk197752a83fb2005-01-31 12:56:44 +000064 const char *z = (const char *)pKey;
65 int h = 0;
drha83ccca2009-04-28 13:01:09 +000066 assert( nKey>0 );
danielk197752a83fb2005-01-31 12:56:44 +000067 while( nKey > 0 ){
68 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
69 nKey--;
70 }
drh8a1e5942009-04-28 15:43:45 +000071 return h;
drhbeae3192001-09-22 18:12:08 +000072}
73static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
drhe8cf2ca2004-08-20 14:08:50 +000074 if( n1!=n2 ) return 1;
danielk19774adee202004-05-08 08:23:19 +000075 return sqlite3StrNICmp((const char*)pKey1,(const char*)pKey2,n1);
drhbeae3192001-09-22 18:12:08 +000076}
77
drhe8cf2ca2004-08-20 14:08:50 +000078
drh8a1e5942009-04-28 15:43:45 +000079/* Link pNew element into the hash table pH. If pEntry!=0 then also
80** insert pNew into the pEntry hash bucket.
drhe8cf2ca2004-08-20 14:08:50 +000081*/
82static void insertElement(
83 Hash *pH, /* The complete hash table */
84 struct _ht *pEntry, /* The entry into which pNew is inserted */
85 HashElem *pNew /* The element to be inserted */
86){
87 HashElem *pHead; /* First element already in pEntry */
drh8a1e5942009-04-28 15:43:45 +000088 if( pEntry ){
89 pHead = pEntry->count ? pEntry->chain : 0;
90 pEntry->count++;
91 pEntry->chain = pNew;
92 }else{
93 pHead = 0;
94 }
drhe8cf2ca2004-08-20 14:08:50 +000095 if( pHead ){
96 pNew->next = pHead;
97 pNew->prev = pHead->prev;
98 if( pHead->prev ){ pHead->prev->next = pNew; }
99 else { pH->first = pNew; }
100 pHead->prev = pNew;
101 }else{
102 pNew->next = pH->first;
103 if( pH->first ){ pH->first->prev = pNew; }
104 pNew->prev = 0;
105 pH->first = pNew;
106 }
drhbeae3192001-09-22 18:12:08 +0000107}
108
109
drhaacc5432002-01-06 17:07:40 +0000110/* Resize the hash table so that it cantains "new_size" buckets.
drh8a1e5942009-04-28 15:43:45 +0000111**
112** The hash table might fail to resize if sqlite3_malloc() fails or
113** if the new size is the same as the prior size.
114** Return TRUE if the resize occurs and false if not.
drhbeae3192001-09-22 18:12:08 +0000115*/
drh8a1e5942009-04-28 15:43:45 +0000116static int rehash(Hash *pH, unsigned int new_size){
drhbeae3192001-09-22 18:12:08 +0000117 struct _ht *new_ht; /* The new hash table */
118 HashElem *elem, *next_elem; /* For looping over existing elements */
drhbeae3192001-09-22 18:12:08 +0000119
drh8a1e5942009-04-28 15:43:45 +0000120#if SQLITE_MALLOC_SOFT_LIMIT>0
drheee4c8c2008-02-18 22:24:57 +0000121 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
122 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
123 }
drh8a1e5942009-04-28 15:43:45 +0000124 if( new_size==pH->htsize ) return 0;
drheee4c8c2008-02-18 22:24:57 +0000125#endif
danielk1977a1644fd2007-08-29 12:31:25 +0000126
drh8a1e5942009-04-28 15:43:45 +0000127 /* The inability to allocates space for a larger hash table is
128 ** a performance hit but it is not a fatal error. So mark the
129 ** allocation as a benign.
danielk1977a1644fd2007-08-29 12:31:25 +0000130 */
drh8a1e5942009-04-28 15:43:45 +0000131 sqlite3BeginBenignMalloc();
132 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
133 sqlite3EndBenignMalloc();
drh643167f2008-01-22 21:30:53 +0000134
drh8a1e5942009-04-28 15:43:45 +0000135 if( new_ht==0 ) return 0;
drh41eb9e92008-04-02 18:33:07 +0000136 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +0000137 pH->ht = new_ht;
drh8a1e5942009-04-28 15:43:45 +0000138 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
139 memset(new_ht, 0, new_size*sizeof(struct _ht));
drhbeae3192001-09-22 18:12:08 +0000140 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
drh8a1e5942009-04-28 15:43:45 +0000141 unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
drhbeae3192001-09-22 18:12:08 +0000142 next_elem = elem->next;
drhe8cf2ca2004-08-20 14:08:50 +0000143 insertElement(pH, &new_ht[h], elem);
drhbeae3192001-09-22 18:12:08 +0000144 }
drh8a1e5942009-04-28 15:43:45 +0000145 return 1;
drhbeae3192001-09-22 18:12:08 +0000146}
147
148/* This function (for internal use only) locates an element in an
drhaacc5432002-01-06 17:07:40 +0000149** hash table that matches the given key. The hash for this key has
150** already been computed and is passed as the 4th parameter.
drhbeae3192001-09-22 18:12:08 +0000151*/
152static HashElem *findElementGivenHash(
153 const Hash *pH, /* The pH to be searched */
154 const void *pKey, /* The key we are searching for */
drh8a1e5942009-04-28 15:43:45 +0000155 int nKey, /* Bytes in key (not counting zero terminator) */
156 unsigned int h /* The hash for this key. */
drhbeae3192001-09-22 18:12:08 +0000157){
158 HashElem *elem; /* Used to loop thru the element list */
159 int count; /* Number of elements left to test */
drhbeae3192001-09-22 18:12:08 +0000160
161 if( pH->ht ){
drhe8cf2ca2004-08-20 14:08:50 +0000162 struct _ht *pEntry = &pH->ht[h];
163 elem = pEntry->chain;
164 count = pEntry->count;
drh8a1e5942009-04-28 15:43:45 +0000165 }else{
166 elem = pH->first;
167 count = pH->count;
168 }
169 while( count-- && ALWAYS(elem) ){
170 if( strCompare(elem->pKey,elem->nKey,pKey,nKey)==0 ){
171 return elem;
drhbeae3192001-09-22 18:12:08 +0000172 }
drh8a1e5942009-04-28 15:43:45 +0000173 elem = elem->next;
drhbeae3192001-09-22 18:12:08 +0000174 }
175 return 0;
176}
177
drh81a20f22001-10-12 17:30:04 +0000178/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000179** element and a hash on the element's key.
180*/
181static void removeElementGivenHash(
182 Hash *pH, /* The pH containing "elem" */
183 HashElem* elem, /* The element to be removed from the pH */
drh8a1e5942009-04-28 15:43:45 +0000184 unsigned int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000185){
drhe8cf2ca2004-08-20 14:08:50 +0000186 struct _ht *pEntry;
drhbeae3192001-09-22 18:12:08 +0000187 if( elem->prev ){
188 elem->prev->next = elem->next;
189 }else{
190 pH->first = elem->next;
191 }
192 if( elem->next ){
193 elem->next->prev = elem->prev;
194 }
drh8a1e5942009-04-28 15:43:45 +0000195 if( pH->ht ){
196 pEntry = &pH->ht[h];
197 if( pEntry->chain==elem ){
198 pEntry->chain = elem->next;
199 }
200 pEntry->count--;
201 assert( pEntry->count>=0 );
drhbeae3192001-09-22 18:12:08 +0000202 }
drh93553ad2007-03-31 03:59:23 +0000203 if( pH->copyKey ){
drh17435752007-08-16 04:30:38 +0000204 sqlite3_free(elem->pKey);
drhbeae3192001-09-22 18:12:08 +0000205 }
drh17435752007-08-16 04:30:38 +0000206 sqlite3_free( elem );
drhbeae3192001-09-22 18:12:08 +0000207 pH->count--;
drh762e5842005-10-03 15:11:08 +0000208 if( pH->count<=0 ){
209 assert( pH->first==0 );
210 assert( pH->count==0 );
211 sqlite3HashClear(pH);
212 }
drhbeae3192001-09-22 18:12:08 +0000213}
214
drhaacc5432002-01-06 17:07:40 +0000215/* Attempt to locate an element of the hash table pH with a key
danielk19777c836f02007-09-04 14:31:47 +0000216** that matches pKey,nKey. Return the data for this element if it is
217** found, or NULL if there is no match.
218*/
219void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){
220 HashElem *elem; /* The element that matches key */
drh8a1e5942009-04-28 15:43:45 +0000221 unsigned int h; /* A hash on key */
222
223 assert( pH!=0 );
224 assert( pKey!=0 );
225 assert( nKey>0 );
226 if( pH->ht ){
227 h = strHash(pKey, nKey) % pH->htsize;
228 }else{
229 h = 0;
230 }
231 elem = findElementGivenHash(pH, pKey, nKey, h);
drhbeae3192001-09-22 18:12:08 +0000232 return elem ? elem->data : 0;
233}
234
drh81a20f22001-10-12 17:30:04 +0000235/* Insert an element into the hash table pH. The key is pKey,nKey
236** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000237**
drh81a20f22001-10-12 17:30:04 +0000238** If no element exists with a matching key, then a new
239** element is created. A copy of the key is made if the copyKey
240** flag is set. NULL is returned.
drhbeae3192001-09-22 18:12:08 +0000241**
242** If another element already exists with the same key, then the
243** new data replaces the old data and the old data is returned.
drh6d4abfb2001-10-22 02:58:08 +0000244** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000245** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000246**
247** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000248** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000249*/
danielk19774adee202004-05-08 08:23:19 +0000250void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){
drh8a1e5942009-04-28 15:43:45 +0000251 unsigned int h; /* the hash of the key modulo hash table size */
drhbeae3192001-09-22 18:12:08 +0000252 HashElem *elem; /* Used to loop thru the element list */
253 HashElem *new_elem; /* New element added to the pH */
drhbeae3192001-09-22 18:12:08 +0000254
255 assert( pH!=0 );
drh8a1e5942009-04-28 15:43:45 +0000256 assert( pKey!=0 );
257 assert( nKey>0 );
drheee4c8c2008-02-18 22:24:57 +0000258 if( pH->htsize ){
drh8a1e5942009-04-28 15:43:45 +0000259 h = strHash(pKey, nKey) % pH->htsize;
260 }else{
261 h = 0;
262 }
263 elem = findElementGivenHash(pH,pKey,nKey,h);
264 if( elem ){
265 void *old_data = elem->data;
266 if( data==0 ){
267 removeElementGivenHash(pH,elem,h);
268 }else{
269 elem->data = data;
270 if( !pH->copyKey ){
271 elem->pKey = (void *)pKey;
danielk1977cd2543b2007-09-03 15:03:20 +0000272 }
drh8a1e5942009-04-28 15:43:45 +0000273 assert(nKey==elem->nKey);
drhbeae3192001-09-22 18:12:08 +0000274 }
drh8a1e5942009-04-28 15:43:45 +0000275 return old_data;
drhbeae3192001-09-22 18:12:08 +0000276 }
277 if( data==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000278 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
drh6d4abfb2001-10-22 02:58:08 +0000279 if( new_elem==0 ) return data;
drh8a1e5942009-04-28 15:43:45 +0000280 if( pH->copyKey ){
drhe5ae5732008-06-15 02:51:47 +0000281 new_elem->pKey = sqlite3Malloc( nKey );
drhbeae3192001-09-22 18:12:08 +0000282 if( new_elem->pKey==0 ){
drh17435752007-08-16 04:30:38 +0000283 sqlite3_free(new_elem);
drh6d4abfb2001-10-22 02:58:08 +0000284 return data;
drhbeae3192001-09-22 18:12:08 +0000285 }
286 memcpy((void*)new_elem->pKey, pKey, nKey);
287 }else{
drh2ce1a6e2002-05-21 23:44:30 +0000288 new_elem->pKey = (void*)pKey;
drhbeae3192001-09-22 18:12:08 +0000289 }
290 new_elem->nKey = nKey;
drh8a1e5942009-04-28 15:43:45 +0000291 new_elem->data = data;
drhbeae3192001-09-22 18:12:08 +0000292 pH->count++;
drh8a1e5942009-04-28 15:43:45 +0000293 if( pH->count>=10 && pH->count > 2*pH->htsize ){
294 if( rehash(pH, pH->count*2) && pH->htsize ){
295 h = strHash(pKey, nKey) % pH->htsize;
drhe8cf2ca2004-08-20 14:08:50 +0000296 }
drhbeae3192001-09-22 18:12:08 +0000297 }
drh8a1e5942009-04-28 15:43:45 +0000298 if( pH->ht ){
299 insertElement(pH, &pH->ht[h], new_elem);
300 }else{
301 insertElement(pH, 0, new_elem);
drhbeae3192001-09-22 18:12:08 +0000302 }
drhbeae3192001-09-22 18:12:08 +0000303 return 0;
304}