blob: e81dcf95e43848fa7086b7f781c090adc7e6ca8e [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.
drhbeae3192001-09-22 18:12:08 +000014*/
15#include "sqliteInt.h"
16#include <assert.h>
17
18/* Turn bulk memory into a hash table object by initializing the
19** fields of the Hash structure.
drhaacc5432002-01-06 17:07:40 +000020**
drh76d7f8b2004-06-30 22:43:21 +000021** "pNew" is a pointer to the hash table that is to be initialized.
drhbeae3192001-09-22 18:12:08 +000022*/
drhe61922a2009-05-02 13:29:37 +000023void sqlite3HashInit(Hash *pNew){
drh76d7f8b2004-06-30 22:43:21 +000024 assert( pNew!=0 );
drh76d7f8b2004-06-30 22:43:21 +000025 pNew->first = 0;
26 pNew->count = 0;
27 pNew->htsize = 0;
28 pNew->ht = 0;
drhbeae3192001-09-22 18:12:08 +000029}
30
31/* Remove all entries from a hash table. Reclaim all memory.
drhaacc5432002-01-06 17:07:40 +000032** Call this routine to delete a hash table or to reset a hash table
33** to the empty state.
drhbeae3192001-09-22 18:12:08 +000034*/
danielk19774adee202004-05-08 08:23:19 +000035void sqlite3HashClear(Hash *pH){
drhbeae3192001-09-22 18:12:08 +000036 HashElem *elem; /* For looping over all elements of the table */
37
38 assert( pH!=0 );
39 elem = pH->first;
40 pH->first = 0;
drh41eb9e92008-04-02 18:33:07 +000041 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +000042 pH->ht = 0;
43 pH->htsize = 0;
44 while( elem ){
45 HashElem *next_elem = elem->next;
drh17435752007-08-16 04:30:38 +000046 sqlite3_free(elem);
drhbeae3192001-09-22 18:12:08 +000047 elem = next_elem;
48 }
49 pH->count = 0;
50}
51
drhbeae3192001-09-22 18:12:08 +000052/*
drhe61922a2009-05-02 13:29:37 +000053** The hashing function.
drhbeae3192001-09-22 18:12:08 +000054*/
drhe61922a2009-05-02 13:29:37 +000055static unsigned int strHash(const char *z, int nKey){
danielk197752a83fb2005-01-31 12:56:44 +000056 int h = 0;
drh0d59d172009-04-28 17:33:16 +000057 assert( nKey>=0 );
danielk197752a83fb2005-01-31 12:56:44 +000058 while( nKey > 0 ){
59 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
60 nKey--;
61 }
drh8a1e5942009-04-28 15:43:45 +000062 return h;
drhbeae3192001-09-22 18:12:08 +000063}
drhbeae3192001-09-22 18:12:08 +000064
drhe8cf2ca2004-08-20 14:08:50 +000065
drh8a1e5942009-04-28 15:43:45 +000066/* Link pNew element into the hash table pH. If pEntry!=0 then also
67** insert pNew into the pEntry hash bucket.
drhe8cf2ca2004-08-20 14:08:50 +000068*/
69static void insertElement(
70 Hash *pH, /* The complete hash table */
71 struct _ht *pEntry, /* The entry into which pNew is inserted */
72 HashElem *pNew /* The element to be inserted */
73){
74 HashElem *pHead; /* First element already in pEntry */
drh8a1e5942009-04-28 15:43:45 +000075 if( pEntry ){
76 pHead = pEntry->count ? pEntry->chain : 0;
77 pEntry->count++;
78 pEntry->chain = pNew;
79 }else{
80 pHead = 0;
81 }
drhe8cf2ca2004-08-20 14:08:50 +000082 if( pHead ){
83 pNew->next = pHead;
84 pNew->prev = pHead->prev;
85 if( pHead->prev ){ pHead->prev->next = pNew; }
86 else { pH->first = pNew; }
87 pHead->prev = pNew;
88 }else{
89 pNew->next = pH->first;
90 if( pH->first ){ pH->first->prev = pNew; }
91 pNew->prev = 0;
92 pH->first = pNew;
93 }
drhbeae3192001-09-22 18:12:08 +000094}
95
96
drhaacc5432002-01-06 17:07:40 +000097/* Resize the hash table so that it cantains "new_size" buckets.
drh8a1e5942009-04-28 15:43:45 +000098**
99** The hash table might fail to resize if sqlite3_malloc() fails or
100** if the new size is the same as the prior size.
101** Return TRUE if the resize occurs and false if not.
drhbeae3192001-09-22 18:12:08 +0000102*/
drh8a1e5942009-04-28 15:43:45 +0000103static int rehash(Hash *pH, unsigned int new_size){
drhbeae3192001-09-22 18:12:08 +0000104 struct _ht *new_ht; /* The new hash table */
105 HashElem *elem, *next_elem; /* For looping over existing elements */
drhbeae3192001-09-22 18:12:08 +0000106
drh8a1e5942009-04-28 15:43:45 +0000107#if SQLITE_MALLOC_SOFT_LIMIT>0
drheee4c8c2008-02-18 22:24:57 +0000108 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
109 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
110 }
drh8a1e5942009-04-28 15:43:45 +0000111 if( new_size==pH->htsize ) return 0;
drheee4c8c2008-02-18 22:24:57 +0000112#endif
danielk1977a1644fd2007-08-29 12:31:25 +0000113
drh8a1e5942009-04-28 15:43:45 +0000114 /* The inability to allocates space for a larger hash table is
115 ** a performance hit but it is not a fatal error. So mark the
dan38d07302012-08-07 15:19:27 +0000116 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
117 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
118 ** only zeroes the requested number of bytes whereas this module will
119 ** use the actual amount of space allocated for the hash table (which
120 ** may be larger than the requested amount).
danielk1977a1644fd2007-08-29 12:31:25 +0000121 */
drh8a1e5942009-04-28 15:43:45 +0000122 sqlite3BeginBenignMalloc();
dan38d07302012-08-07 15:19:27 +0000123 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
drh8a1e5942009-04-28 15:43:45 +0000124 sqlite3EndBenignMalloc();
drh643167f2008-01-22 21:30:53 +0000125
drh8a1e5942009-04-28 15:43:45 +0000126 if( new_ht==0 ) return 0;
drh41eb9e92008-04-02 18:33:07 +0000127 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +0000128 pH->ht = new_ht;
drh8a1e5942009-04-28 15:43:45 +0000129 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
dan38d07302012-08-07 15:19:27 +0000130 memset(new_ht, 0, new_size*sizeof(struct _ht));
drhbeae3192001-09-22 18:12:08 +0000131 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
drh8a1e5942009-04-28 15:43:45 +0000132 unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
drhbeae3192001-09-22 18:12:08 +0000133 next_elem = elem->next;
drhe8cf2ca2004-08-20 14:08:50 +0000134 insertElement(pH, &new_ht[h], elem);
drhbeae3192001-09-22 18:12:08 +0000135 }
drh8a1e5942009-04-28 15:43:45 +0000136 return 1;
drhbeae3192001-09-22 18:12:08 +0000137}
138
139/* This function (for internal use only) locates an element in an
drhaacc5432002-01-06 17:07:40 +0000140** hash table that matches the given key. The hash for this key has
141** already been computed and is passed as the 4th parameter.
drhbeae3192001-09-22 18:12:08 +0000142*/
143static HashElem *findElementGivenHash(
144 const Hash *pH, /* The pH to be searched */
drhe61922a2009-05-02 13:29:37 +0000145 const char *pKey, /* The key we are searching for */
drh8a1e5942009-04-28 15:43:45 +0000146 int nKey, /* Bytes in key (not counting zero terminator) */
147 unsigned int h /* The hash for this key. */
drhbeae3192001-09-22 18:12:08 +0000148){
149 HashElem *elem; /* Used to loop thru the element list */
150 int count; /* Number of elements left to test */
drhbeae3192001-09-22 18:12:08 +0000151
152 if( pH->ht ){
drhe8cf2ca2004-08-20 14:08:50 +0000153 struct _ht *pEntry = &pH->ht[h];
154 elem = pEntry->chain;
155 count = pEntry->count;
drh8a1e5942009-04-28 15:43:45 +0000156 }else{
157 elem = pH->first;
158 count = pH->count;
159 }
160 while( count-- && ALWAYS(elem) ){
drhe61922a2009-05-02 13:29:37 +0000161 if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
drh8a1e5942009-04-28 15:43:45 +0000162 return elem;
drhbeae3192001-09-22 18:12:08 +0000163 }
drh8a1e5942009-04-28 15:43:45 +0000164 elem = elem->next;
drhbeae3192001-09-22 18:12:08 +0000165 }
166 return 0;
167}
168
drh81a20f22001-10-12 17:30:04 +0000169/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000170** element and a hash on the element's key.
171*/
172static void removeElementGivenHash(
173 Hash *pH, /* The pH containing "elem" */
174 HashElem* elem, /* The element to be removed from the pH */
drh8a1e5942009-04-28 15:43:45 +0000175 unsigned int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000176){
drhe8cf2ca2004-08-20 14:08:50 +0000177 struct _ht *pEntry;
drhbeae3192001-09-22 18:12:08 +0000178 if( elem->prev ){
179 elem->prev->next = elem->next;
180 }else{
181 pH->first = elem->next;
182 }
183 if( elem->next ){
184 elem->next->prev = elem->prev;
185 }
drh8a1e5942009-04-28 15:43:45 +0000186 if( pH->ht ){
187 pEntry = &pH->ht[h];
188 if( pEntry->chain==elem ){
189 pEntry->chain = elem->next;
190 }
191 pEntry->count--;
192 assert( pEntry->count>=0 );
drhbeae3192001-09-22 18:12:08 +0000193 }
drh17435752007-08-16 04:30:38 +0000194 sqlite3_free( elem );
drhbeae3192001-09-22 18:12:08 +0000195 pH->count--;
mistachkin5f070c72012-10-18 10:35:19 +0000196 if( pH->count==0 ){
drh762e5842005-10-03 15:11:08 +0000197 assert( pH->first==0 );
198 assert( pH->count==0 );
199 sqlite3HashClear(pH);
200 }
drhbeae3192001-09-22 18:12:08 +0000201}
202
drhaacc5432002-01-06 17:07:40 +0000203/* Attempt to locate an element of the hash table pH with a key
danielk19777c836f02007-09-04 14:31:47 +0000204** that matches pKey,nKey. Return the data for this element if it is
205** found, or NULL if there is no match.
206*/
drhe61922a2009-05-02 13:29:37 +0000207void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
danielk19777c836f02007-09-04 14:31:47 +0000208 HashElem *elem; /* The element that matches key */
drh8a1e5942009-04-28 15:43:45 +0000209 unsigned int h; /* A hash on key */
210
211 assert( pH!=0 );
212 assert( pKey!=0 );
drh0d59d172009-04-28 17:33:16 +0000213 assert( nKey>=0 );
drh8a1e5942009-04-28 15:43:45 +0000214 if( pH->ht ){
215 h = strHash(pKey, nKey) % pH->htsize;
216 }else{
217 h = 0;
218 }
219 elem = findElementGivenHash(pH, pKey, nKey, h);
drhbeae3192001-09-22 18:12:08 +0000220 return elem ? elem->data : 0;
221}
222
drh81a20f22001-10-12 17:30:04 +0000223/* Insert an element into the hash table pH. The key is pKey,nKey
224** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000225**
drh81a20f22001-10-12 17:30:04 +0000226** If no element exists with a matching key, then a new
drhe61922a2009-05-02 13:29:37 +0000227** element is created and NULL is returned.
drhbeae3192001-09-22 18:12:08 +0000228**
229** If another element already exists with the same key, then the
230** new data replaces the old data and the old data is returned.
drh6d4abfb2001-10-22 02:58:08 +0000231** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000232** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000233**
234** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000235** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000236*/
drhe61922a2009-05-02 13:29:37 +0000237void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
drh8a1e5942009-04-28 15:43:45 +0000238 unsigned int h; /* the hash of the key modulo hash table size */
drhbeae3192001-09-22 18:12:08 +0000239 HashElem *elem; /* Used to loop thru the element list */
240 HashElem *new_elem; /* New element added to the pH */
drhbeae3192001-09-22 18:12:08 +0000241
242 assert( pH!=0 );
drh8a1e5942009-04-28 15:43:45 +0000243 assert( pKey!=0 );
drh0d59d172009-04-28 17:33:16 +0000244 assert( nKey>=0 );
drheee4c8c2008-02-18 22:24:57 +0000245 if( pH->htsize ){
drh8a1e5942009-04-28 15:43:45 +0000246 h = strHash(pKey, nKey) % pH->htsize;
247 }else{
248 h = 0;
249 }
250 elem = findElementGivenHash(pH,pKey,nKey,h);
251 if( elem ){
252 void *old_data = elem->data;
253 if( data==0 ){
254 removeElementGivenHash(pH,elem,h);
255 }else{
256 elem->data = data;
drhe61922a2009-05-02 13:29:37 +0000257 elem->pKey = pKey;
drh8a1e5942009-04-28 15:43:45 +0000258 assert(nKey==elem->nKey);
drhbeae3192001-09-22 18:12:08 +0000259 }
drh8a1e5942009-04-28 15:43:45 +0000260 return old_data;
drhbeae3192001-09-22 18:12:08 +0000261 }
262 if( data==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000263 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
drh6d4abfb2001-10-22 02:58:08 +0000264 if( new_elem==0 ) return data;
drhe61922a2009-05-02 13:29:37 +0000265 new_elem->pKey = pKey;
drhbeae3192001-09-22 18:12:08 +0000266 new_elem->nKey = nKey;
drh8a1e5942009-04-28 15:43:45 +0000267 new_elem->data = data;
drhbeae3192001-09-22 18:12:08 +0000268 pH->count++;
drh8a1e5942009-04-28 15:43:45 +0000269 if( pH->count>=10 && pH->count > 2*pH->htsize ){
drh782b8732009-05-09 23:29:12 +0000270 if( rehash(pH, pH->count*2) ){
271 assert( pH->htsize>0 );
drh8a1e5942009-04-28 15:43:45 +0000272 h = strHash(pKey, nKey) % pH->htsize;
drhe8cf2ca2004-08-20 14:08:50 +0000273 }
drhbeae3192001-09-22 18:12:08 +0000274 }
drh8a1e5942009-04-28 15:43:45 +0000275 if( pH->ht ){
276 insertElement(pH, &pH->ht[h], new_elem);
277 }else{
278 insertElement(pH, 0, new_elem);
drhbeae3192001-09-22 18:12:08 +0000279 }
drhbeae3192001-09-22 18:12:08 +0000280 return 0;
281}