blob: 9f1587aa4b3673244c0ce673af668e9893e2adb7 [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**
drh782b8732009-05-09 23:29:12 +000015** $Id: hash.c,v 1.38 2009/05/09 23:29:12 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.
drhbeae3192001-09-22 18:12:08 +000024*/
drhe61922a2009-05-02 13:29:37 +000025void sqlite3HashInit(Hash *pNew){
drh76d7f8b2004-06-30 22:43:21 +000026 assert( pNew!=0 );
drh76d7f8b2004-06-30 22:43:21 +000027 pNew->first = 0;
28 pNew->count = 0;
29 pNew->htsize = 0;
30 pNew->ht = 0;
drhbeae3192001-09-22 18:12:08 +000031}
32
33/* Remove all entries from a hash table. Reclaim all memory.
drhaacc5432002-01-06 17:07:40 +000034** Call this routine to delete a hash table or to reset a hash table
35** to the empty state.
drhbeae3192001-09-22 18:12:08 +000036*/
danielk19774adee202004-05-08 08:23:19 +000037void sqlite3HashClear(Hash *pH){
drhbeae3192001-09-22 18:12:08 +000038 HashElem *elem; /* For looping over all elements of the table */
39
40 assert( pH!=0 );
41 elem = pH->first;
42 pH->first = 0;
drh41eb9e92008-04-02 18:33:07 +000043 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +000044 pH->ht = 0;
45 pH->htsize = 0;
46 while( elem ){
47 HashElem *next_elem = elem->next;
drh17435752007-08-16 04:30:38 +000048 sqlite3_free(elem);
drhbeae3192001-09-22 18:12:08 +000049 elem = next_elem;
50 }
51 pH->count = 0;
52}
53
drhbeae3192001-09-22 18:12:08 +000054/*
drhe61922a2009-05-02 13:29:37 +000055** The hashing function.
drhbeae3192001-09-22 18:12:08 +000056*/
drhe61922a2009-05-02 13:29:37 +000057static unsigned int strHash(const char *z, int nKey){
danielk197752a83fb2005-01-31 12:56:44 +000058 int h = 0;
drh0d59d172009-04-28 17:33:16 +000059 assert( nKey>=0 );
danielk197752a83fb2005-01-31 12:56:44 +000060 while( nKey > 0 ){
61 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
62 nKey--;
63 }
drh8a1e5942009-04-28 15:43:45 +000064 return h;
drhbeae3192001-09-22 18:12:08 +000065}
drhbeae3192001-09-22 18:12:08 +000066
drhe8cf2ca2004-08-20 14:08:50 +000067
drh8a1e5942009-04-28 15:43:45 +000068/* Link pNew element into the hash table pH. If pEntry!=0 then also
69** insert pNew into the pEntry hash bucket.
drhe8cf2ca2004-08-20 14:08:50 +000070*/
71static void insertElement(
72 Hash *pH, /* The complete hash table */
73 struct _ht *pEntry, /* The entry into which pNew is inserted */
74 HashElem *pNew /* The element to be inserted */
75){
76 HashElem *pHead; /* First element already in pEntry */
drh8a1e5942009-04-28 15:43:45 +000077 if( pEntry ){
78 pHead = pEntry->count ? pEntry->chain : 0;
79 pEntry->count++;
80 pEntry->chain = pNew;
81 }else{
82 pHead = 0;
83 }
drhe8cf2ca2004-08-20 14:08:50 +000084 if( pHead ){
85 pNew->next = pHead;
86 pNew->prev = pHead->prev;
87 if( pHead->prev ){ pHead->prev->next = pNew; }
88 else { pH->first = pNew; }
89 pHead->prev = pNew;
90 }else{
91 pNew->next = pH->first;
92 if( pH->first ){ pH->first->prev = pNew; }
93 pNew->prev = 0;
94 pH->first = pNew;
95 }
drhbeae3192001-09-22 18:12:08 +000096}
97
98
drhaacc5432002-01-06 17:07:40 +000099/* Resize the hash table so that it cantains "new_size" buckets.
drh8a1e5942009-04-28 15:43:45 +0000100**
101** The hash table might fail to resize if sqlite3_malloc() fails or
102** if the new size is the same as the prior size.
103** Return TRUE if the resize occurs and false if not.
drhbeae3192001-09-22 18:12:08 +0000104*/
drh8a1e5942009-04-28 15:43:45 +0000105static int rehash(Hash *pH, unsigned int new_size){
drhbeae3192001-09-22 18:12:08 +0000106 struct _ht *new_ht; /* The new hash table */
107 HashElem *elem, *next_elem; /* For looping over existing elements */
drhbeae3192001-09-22 18:12:08 +0000108
drh8a1e5942009-04-28 15:43:45 +0000109#if SQLITE_MALLOC_SOFT_LIMIT>0
drheee4c8c2008-02-18 22:24:57 +0000110 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
111 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
112 }
drh8a1e5942009-04-28 15:43:45 +0000113 if( new_size==pH->htsize ) return 0;
drheee4c8c2008-02-18 22:24:57 +0000114#endif
danielk1977a1644fd2007-08-29 12:31:25 +0000115
drh8a1e5942009-04-28 15:43:45 +0000116 /* The inability to allocates space for a larger hash table is
117 ** a performance hit but it is not a fatal error. So mark the
118 ** allocation as a benign.
danielk1977a1644fd2007-08-29 12:31:25 +0000119 */
drh8a1e5942009-04-28 15:43:45 +0000120 sqlite3BeginBenignMalloc();
121 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
122 sqlite3EndBenignMalloc();
drh643167f2008-01-22 21:30:53 +0000123
drh8a1e5942009-04-28 15:43:45 +0000124 if( new_ht==0 ) return 0;
drh41eb9e92008-04-02 18:33:07 +0000125 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +0000126 pH->ht = new_ht;
drh8a1e5942009-04-28 15:43:45 +0000127 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
128 memset(new_ht, 0, new_size*sizeof(struct _ht));
drhbeae3192001-09-22 18:12:08 +0000129 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
drh8a1e5942009-04-28 15:43:45 +0000130 unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
drhbeae3192001-09-22 18:12:08 +0000131 next_elem = elem->next;
drhe8cf2ca2004-08-20 14:08:50 +0000132 insertElement(pH, &new_ht[h], elem);
drhbeae3192001-09-22 18:12:08 +0000133 }
drh8a1e5942009-04-28 15:43:45 +0000134 return 1;
drhbeae3192001-09-22 18:12:08 +0000135}
136
137/* This function (for internal use only) locates an element in an
drhaacc5432002-01-06 17:07:40 +0000138** hash table that matches the given key. The hash for this key has
139** already been computed and is passed as the 4th parameter.
drhbeae3192001-09-22 18:12:08 +0000140*/
141static HashElem *findElementGivenHash(
142 const Hash *pH, /* The pH to be searched */
drhe61922a2009-05-02 13:29:37 +0000143 const char *pKey, /* The key we are searching for */
drh8a1e5942009-04-28 15:43:45 +0000144 int nKey, /* Bytes in key (not counting zero terminator) */
145 unsigned int h /* The hash for this key. */
drhbeae3192001-09-22 18:12:08 +0000146){
147 HashElem *elem; /* Used to loop thru the element list */
148 int count; /* Number of elements left to test */
drhbeae3192001-09-22 18:12:08 +0000149
150 if( pH->ht ){
drhe8cf2ca2004-08-20 14:08:50 +0000151 struct _ht *pEntry = &pH->ht[h];
152 elem = pEntry->chain;
153 count = pEntry->count;
drh8a1e5942009-04-28 15:43:45 +0000154 }else{
155 elem = pH->first;
156 count = pH->count;
157 }
158 while( count-- && ALWAYS(elem) ){
drhe61922a2009-05-02 13:29:37 +0000159 if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
drh8a1e5942009-04-28 15:43:45 +0000160 return elem;
drhbeae3192001-09-22 18:12:08 +0000161 }
drh8a1e5942009-04-28 15:43:45 +0000162 elem = elem->next;
drhbeae3192001-09-22 18:12:08 +0000163 }
164 return 0;
165}
166
drh81a20f22001-10-12 17:30:04 +0000167/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000168** element and a hash on the element's key.
169*/
170static void removeElementGivenHash(
171 Hash *pH, /* The pH containing "elem" */
172 HashElem* elem, /* The element to be removed from the pH */
drh8a1e5942009-04-28 15:43:45 +0000173 unsigned int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000174){
drhe8cf2ca2004-08-20 14:08:50 +0000175 struct _ht *pEntry;
drhbeae3192001-09-22 18:12:08 +0000176 if( elem->prev ){
177 elem->prev->next = elem->next;
178 }else{
179 pH->first = elem->next;
180 }
181 if( elem->next ){
182 elem->next->prev = elem->prev;
183 }
drh8a1e5942009-04-28 15:43:45 +0000184 if( pH->ht ){
185 pEntry = &pH->ht[h];
186 if( pEntry->chain==elem ){
187 pEntry->chain = elem->next;
188 }
189 pEntry->count--;
190 assert( pEntry->count>=0 );
drhbeae3192001-09-22 18:12:08 +0000191 }
drh17435752007-08-16 04:30:38 +0000192 sqlite3_free( elem );
drhbeae3192001-09-22 18:12:08 +0000193 pH->count--;
drh762e5842005-10-03 15:11:08 +0000194 if( pH->count<=0 ){
195 assert( pH->first==0 );
196 assert( pH->count==0 );
197 sqlite3HashClear(pH);
198 }
drhbeae3192001-09-22 18:12:08 +0000199}
200
drhaacc5432002-01-06 17:07:40 +0000201/* Attempt to locate an element of the hash table pH with a key
danielk19777c836f02007-09-04 14:31:47 +0000202** that matches pKey,nKey. Return the data for this element if it is
203** found, or NULL if there is no match.
204*/
drhe61922a2009-05-02 13:29:37 +0000205void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
danielk19777c836f02007-09-04 14:31:47 +0000206 HashElem *elem; /* The element that matches key */
drh8a1e5942009-04-28 15:43:45 +0000207 unsigned int h; /* A hash on key */
208
209 assert( pH!=0 );
210 assert( pKey!=0 );
drh0d59d172009-04-28 17:33:16 +0000211 assert( nKey>=0 );
drh8a1e5942009-04-28 15:43:45 +0000212 if( pH->ht ){
213 h = strHash(pKey, nKey) % pH->htsize;
214 }else{
215 h = 0;
216 }
217 elem = findElementGivenHash(pH, pKey, nKey, h);
drhbeae3192001-09-22 18:12:08 +0000218 return elem ? elem->data : 0;
219}
220
drh81a20f22001-10-12 17:30:04 +0000221/* Insert an element into the hash table pH. The key is pKey,nKey
222** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000223**
drh81a20f22001-10-12 17:30:04 +0000224** If no element exists with a matching key, then a new
drhe61922a2009-05-02 13:29:37 +0000225** element is created and NULL is returned.
drhbeae3192001-09-22 18:12:08 +0000226**
227** If another element already exists with the same key, then the
228** new data replaces the old data and the old data is returned.
drh6d4abfb2001-10-22 02:58:08 +0000229** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000230** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000231**
232** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000233** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000234*/
drhe61922a2009-05-02 13:29:37 +0000235void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
drh8a1e5942009-04-28 15:43:45 +0000236 unsigned int h; /* the hash of the key modulo hash table size */
drhbeae3192001-09-22 18:12:08 +0000237 HashElem *elem; /* Used to loop thru the element list */
238 HashElem *new_elem; /* New element added to the pH */
drhbeae3192001-09-22 18:12:08 +0000239
240 assert( pH!=0 );
drh8a1e5942009-04-28 15:43:45 +0000241 assert( pKey!=0 );
drh0d59d172009-04-28 17:33:16 +0000242 assert( nKey>=0 );
drheee4c8c2008-02-18 22:24:57 +0000243 if( pH->htsize ){
drh8a1e5942009-04-28 15:43:45 +0000244 h = strHash(pKey, nKey) % pH->htsize;
245 }else{
246 h = 0;
247 }
248 elem = findElementGivenHash(pH,pKey,nKey,h);
249 if( elem ){
250 void *old_data = elem->data;
251 if( data==0 ){
252 removeElementGivenHash(pH,elem,h);
253 }else{
254 elem->data = data;
drhe61922a2009-05-02 13:29:37 +0000255 elem->pKey = pKey;
drh8a1e5942009-04-28 15:43:45 +0000256 assert(nKey==elem->nKey);
drhbeae3192001-09-22 18:12:08 +0000257 }
drh8a1e5942009-04-28 15:43:45 +0000258 return old_data;
drhbeae3192001-09-22 18:12:08 +0000259 }
260 if( data==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000261 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
drh6d4abfb2001-10-22 02:58:08 +0000262 if( new_elem==0 ) return data;
drhe61922a2009-05-02 13:29:37 +0000263 new_elem->pKey = pKey;
drhbeae3192001-09-22 18:12:08 +0000264 new_elem->nKey = nKey;
drh8a1e5942009-04-28 15:43:45 +0000265 new_elem->data = data;
drhbeae3192001-09-22 18:12:08 +0000266 pH->count++;
drh8a1e5942009-04-28 15:43:45 +0000267 if( pH->count>=10 && pH->count > 2*pH->htsize ){
drh782b8732009-05-09 23:29:12 +0000268 if( rehash(pH, pH->count*2) ){
269 assert( pH->htsize>0 );
drh8a1e5942009-04-28 15:43:45 +0000270 h = strHash(pKey, nKey) % pH->htsize;
drhe8cf2ca2004-08-20 14:08:50 +0000271 }
drhbeae3192001-09-22 18:12:08 +0000272 }
drh8a1e5942009-04-28 15:43:45 +0000273 if( pH->ht ){
274 insertElement(pH, &pH->ht[h], new_elem);
275 }else{
276 insertElement(pH, 0, new_elem);
drhbeae3192001-09-22 18:12:08 +0000277 }
drhbeae3192001-09-22 18:12:08 +0000278 return 0;
279}