]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* Hash Tables Implementation. |
2 | * | |
3 | * This file implements in memory hash tables with insert/del/replace/find/ | |
4 | * get-random-element operations. Hash tables will auto resize if needed | |
5 | * tables of power of two in size are used, collisions are handled by | |
6 | * chaining. See the source code for more information... :) | |
7 | * | |
12d090d2 | 8 | * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com> |
ed9b544e | 9 | * All rights reserved. |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or without | |
12 | * modification, are permitted provided that the following conditions are met: | |
13 | * | |
14 | * * Redistributions of source code must retain the above copyright notice, | |
15 | * this list of conditions and the following disclaimer. | |
16 | * * Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in the | |
18 | * documentation and/or other materials provided with the distribution. | |
19 | * * Neither the name of Redis nor the names of its contributors may be used | |
20 | * to endorse or promote products derived from this software without | |
21 | * specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
33 | * POSSIBILITY OF SUCH DAMAGE. | |
34 | */ | |
35 | ||
23d4709d | 36 | #include "fmacros.h" |
37 | ||
ed9b544e | 38 | #include <stdio.h> |
39 | #include <stdlib.h> | |
40 | #include <string.h> | |
41 | #include <stdarg.h> | |
42 | #include <assert.h> | |
f2923bec | 43 | #include <limits.h> |
8ca3e9d1 | 44 | #include <sys/time.h> |
ed9b544e | 45 | |
46 | #include "dict.h" | |
47 | #include "zmalloc.h" | |
48 | ||
884d4b39 | 49 | /* Using dictEnableResize() / dictDisableResize() we make possible to |
50 | * enable/disable resizing of the hash table as needed. This is very important | |
51 | * for Redis, as we use copy-on-write and don't want to move too much memory | |
52 | * around when there is a child performing saving operations. */ | |
53 | static int dict_can_resize = 1; | |
54 | ||
ed9b544e | 55 | /* -------------------------- private prototypes ---------------------------- */ |
56 | ||
57 | static int _dictExpandIfNeeded(dict *ht); | |
f2923bec | 58 | static unsigned long _dictNextPower(unsigned long size); |
ed9b544e | 59 | static int _dictKeyIndex(dict *ht, const void *key); |
60 | static int _dictInit(dict *ht, dictType *type, void *privDataPtr); | |
61 | ||
62 | /* -------------------------- hash functions -------------------------------- */ | |
63 | ||
64 | /* Thomas Wang's 32 bit Mix Function */ | |
65 | unsigned int dictIntHashFunction(unsigned int key) | |
66 | { | |
67 | key += ~(key << 15); | |
68 | key ^= (key >> 10); | |
69 | key += (key << 3); | |
70 | key ^= (key >> 6); | |
71 | key += ~(key << 11); | |
72 | key ^= (key >> 16); | |
73 | return key; | |
74 | } | |
75 | ||
76 | /* Identity hash function for integer keys */ | |
77 | unsigned int dictIdentityHashFunction(unsigned int key) | |
78 | { | |
79 | return key; | |
80 | } | |
81 | ||
82 | /* Generic hash function (a popular one from Bernstein). | |
83 | * I tested a few and this was the best. */ | |
84 | unsigned int dictGenHashFunction(const unsigned char *buf, int len) { | |
85 | unsigned int hash = 5381; | |
86 | ||
87 | while (len--) | |
88 | hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */ | |
89 | return hash; | |
90 | } | |
91 | ||
92 | /* ----------------------------- API implementation ------------------------- */ | |
93 | ||
94 | /* Reset an hashtable already initialized with ht_init(). | |
95 | * NOTE: This function should only called by ht_destroy(). */ | |
5413c40d | 96 | static void _dictReset(dictht *ht) |
ed9b544e | 97 | { |
98 | ht->table = NULL; | |
99 | ht->size = 0; | |
100 | ht->sizemask = 0; | |
101 | ht->used = 0; | |
102 | } | |
103 | ||
104 | /* Create a new hash table */ | |
105 | dict *dictCreate(dictType *type, | |
106 | void *privDataPtr) | |
107 | { | |
d9dd352b | 108 | dict *d = zmalloc(sizeof(*d)); |
ed9b544e | 109 | |
5413c40d | 110 | _dictInit(d,type,privDataPtr); |
111 | return d; | |
ed9b544e | 112 | } |
113 | ||
114 | /* Initialize the hash table */ | |
5413c40d | 115 | int _dictInit(dict *d, dictType *type, |
ed9b544e | 116 | void *privDataPtr) |
117 | { | |
5413c40d | 118 | _dictReset(&d->ht[0]); |
119 | _dictReset(&d->ht[1]); | |
120 | d->type = type; | |
121 | d->privdata = privDataPtr; | |
122 | d->rehashidx = -1; | |
123 | d->iterators = 0; | |
ed9b544e | 124 | return DICT_OK; |
125 | } | |
126 | ||
127 | /* Resize the table to the minimal size that contains all the elements, | |
128 | * but with the invariant of a USER/BUCKETS ration near to <= 1 */ | |
5413c40d | 129 | int dictResize(dict *d) |
ed9b544e | 130 | { |
5413c40d | 131 | int minimal; |
ed9b544e | 132 | |
5413c40d | 133 | if (!dict_can_resize || dictIsRehashing(d)) return DICT_ERR; |
134 | minimal = d->ht[0].used; | |
ed9b544e | 135 | if (minimal < DICT_HT_INITIAL_SIZE) |
136 | minimal = DICT_HT_INITIAL_SIZE; | |
5413c40d | 137 | return dictExpand(d, minimal); |
ed9b544e | 138 | } |
139 | ||
140 | /* Expand or create the hashtable */ | |
5413c40d | 141 | int dictExpand(dict *d, unsigned long size) |
ed9b544e | 142 | { |
5413c40d | 143 | dictht n; /* the new hashtable */ |
144 | unsigned long realsize = _dictNextPower(size); | |
ed9b544e | 145 | |
146 | /* the size is invalid if it is smaller than the number of | |
147 | * elements already inside the hashtable */ | |
5413c40d | 148 | if (dictIsRehashing(d) || d->ht[0].used > size) |
ed9b544e | 149 | return DICT_ERR; |
150 | ||
399f2f40 | 151 | /* Allocate the new hashtable and initialize all pointers to NULL */ |
ed9b544e | 152 | n.size = realsize; |
153 | n.sizemask = realsize-1; | |
399f2f40 | 154 | n.table = zcalloc(realsize*sizeof(dictEntry*)); |
5413c40d | 155 | n.used = 0; |
ed9b544e | 156 | |
5413c40d | 157 | /* Is this the first initialization? If so it's not really a rehashing |
158 | * we just set the first hash table so that it can accept keys. */ | |
159 | if (d->ht[0].table == NULL) { | |
160 | d->ht[0] = n; | |
161 | return DICT_OK; | |
162 | } | |
ed9b544e | 163 | |
5413c40d | 164 | /* Prepare a second hash table for incremental rehashing */ |
165 | d->ht[1] = n; | |
166 | d->rehashidx = 0; | |
167 | return DICT_OK; | |
168 | } | |
169 | ||
170 | /* Performs N steps of incremental rehashing. Returns 1 if there are still | |
171 | * keys to move from the old to the new hash table, otherwise 0 is returned. | |
172 | * Note that a rehashing step consists in moving a bucket (that may have more | |
173 | * thank one key as we use chaining) from the old to the new hash table. */ | |
174 | int dictRehash(dict *d, int n) { | |
175 | if (!dictIsRehashing(d)) return 0; | |
176 | ||
177 | while(n--) { | |
178 | dictEntry *de, *nextde; | |
179 | ||
180 | /* Check if we already rehashed the whole table... */ | |
181 | if (d->ht[0].used == 0) { | |
d9dd352b | 182 | zfree(d->ht[0].table); |
5413c40d | 183 | d->ht[0] = d->ht[1]; |
184 | _dictReset(&d->ht[1]); | |
185 | d->rehashidx = -1; | |
186 | return 0; | |
187 | } | |
188 | ||
189 | /* Note that rehashidx can't overflow as we are sure there are more | |
190 | * elements because ht[0].used != 0 */ | |
191 | while(d->ht[0].table[d->rehashidx] == NULL) d->rehashidx++; | |
192 | de = d->ht[0].table[d->rehashidx]; | |
193 | /* Move all the keys in this bucket from the old to the new hash HT */ | |
194 | while(de) { | |
ed9b544e | 195 | unsigned int h; |
196 | ||
5413c40d | 197 | nextde = de->next; |
198 | /* Get the index in the new hash table */ | |
199 | h = dictHashKey(d, de->key) & d->ht[1].sizemask; | |
200 | de->next = d->ht[1].table[h]; | |
201 | d->ht[1].table[h] = de; | |
202 | d->ht[0].used--; | |
203 | d->ht[1].used++; | |
204 | de = nextde; | |
ed9b544e | 205 | } |
5413c40d | 206 | d->ht[0].table[d->rehashidx] = NULL; |
207 | d->rehashidx++; | |
ed9b544e | 208 | } |
5413c40d | 209 | return 1; |
210 | } | |
ed9b544e | 211 | |
8ca3e9d1 | 212 | long long timeInMilliseconds(void) { |
213 | struct timeval tv; | |
214 | ||
215 | gettimeofday(&tv,NULL); | |
216 | return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000); | |
217 | } | |
218 | ||
219 | /* Rehash for an amount of time between ms milliseconds and ms+1 milliseconds */ | |
220 | int dictRehashMilliseconds(dict *d, int ms) { | |
221 | long long start = timeInMilliseconds(); | |
222 | int rehashes = 0; | |
223 | ||
224 | while(dictRehash(d,100)) { | |
225 | rehashes += 100; | |
226 | if (timeInMilliseconds()-start > ms) break; | |
227 | } | |
228 | return rehashes; | |
229 | } | |
230 | ||
5413c40d | 231 | /* This function performs just a step of rehashing, and only if there are |
232 | * not iterators bound to our hash table. When we have iterators in the middle | |
233 | * of a rehashing we can't mess with the two hash tables otherwise some element | |
234 | * can be missed or duplicated. | |
235 | * | |
236 | * This function is called by common lookup or update operations in the | |
237 | * dictionary so that the hash table automatically migrates from H1 to H2 | |
238 | * while it is actively used. */ | |
239 | static void _dictRehashStep(dict *d) { | |
240 | if (d->iterators == 0) dictRehash(d,1); | |
ed9b544e | 241 | } |
242 | ||
243 | /* Add an element to the target hash table */ | |
5413c40d | 244 | int dictAdd(dict *d, void *key, void *val) |
ed9b544e | 245 | { |
246 | int index; | |
247 | dictEntry *entry; | |
5413c40d | 248 | dictht *ht; |
249 | ||
250 | if (dictIsRehashing(d)) _dictRehashStep(d); | |
ed9b544e | 251 | |
252 | /* Get the index of the new element, or -1 if | |
253 | * the element already exists. */ | |
5413c40d | 254 | if ((index = _dictKeyIndex(d, key)) == -1) |
ed9b544e | 255 | return DICT_ERR; |
256 | ||
257 | /* Allocates the memory and stores key */ | |
5413c40d | 258 | ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0]; |
d9dd352b | 259 | entry = zmalloc(sizeof(*entry)); |
ed9b544e | 260 | entry->next = ht->table[index]; |
261 | ht->table[index] = entry; | |
5413c40d | 262 | ht->used++; |
ed9b544e | 263 | |
264 | /* Set the hash entry fields. */ | |
5413c40d | 265 | dictSetHashKey(d, entry, key); |
266 | dictSetHashVal(d, entry, val); | |
ed9b544e | 267 | return DICT_OK; |
268 | } | |
269 | ||
121796f7 | 270 | /* Add an element, discarding the old if the key already exists. |
271 | * Return 1 if the key was added from scratch, 0 if there was already an | |
272 | * element with such key and dictReplace() just performed a value update | |
273 | * operation. */ | |
5413c40d | 274 | int dictReplace(dict *d, void *key, void *val) |
ed9b544e | 275 | { |
2069d06a | 276 | dictEntry *entry, auxentry; |
ed9b544e | 277 | |
278 | /* Try to add the element. If the key | |
279 | * does not exists dictAdd will suceed. */ | |
5413c40d | 280 | if (dictAdd(d, key, val) == DICT_OK) |
121796f7 | 281 | return 1; |
ed9b544e | 282 | /* It already exists, get the entry */ |
5413c40d | 283 | entry = dictFind(d, key); |
ed9b544e | 284 | /* Free the old value and set the new one */ |
2069d06a | 285 | /* Set the new value and free the old one. Note that it is important |
286 | * to do that in this order, as the value may just be exactly the same | |
287 | * as the previous one. In this context, think to reference counting, | |
288 | * you want to increment (set), and then decrement (free), and not the | |
289 | * reverse. */ | |
290 | auxentry = *entry; | |
5413c40d | 291 | dictSetHashVal(d, entry, val); |
292 | dictFreeEntryVal(d, &auxentry); | |
121796f7 | 293 | return 0; |
ed9b544e | 294 | } |
295 | ||
296 | /* Search and remove an element */ | |
5413c40d | 297 | static int dictGenericDelete(dict *d, const void *key, int nofree) |
ed9b544e | 298 | { |
5413c40d | 299 | unsigned int h, idx; |
ed9b544e | 300 | dictEntry *he, *prevHe; |
5413c40d | 301 | int table; |
ed9b544e | 302 | |
5413c40d | 303 | if (d->ht[0].size == 0) return DICT_ERR; /* d->ht[0].table is NULL */ |
304 | if (dictIsRehashing(d)) _dictRehashStep(d); | |
305 | h = dictHashKey(d, key); | |
ed9b544e | 306 | |
5413c40d | 307 | for (table = 0; table <= 1; table++) { |
308 | idx = h & d->ht[table].sizemask; | |
309 | he = d->ht[table].table[idx]; | |
310 | prevHe = NULL; | |
311 | while(he) { | |
312 | if (dictCompareHashKeys(d, key, he->key)) { | |
313 | /* Unlink the element from the list */ | |
314 | if (prevHe) | |
315 | prevHe->next = he->next; | |
316 | else | |
317 | d->ht[table].table[idx] = he->next; | |
318 | if (!nofree) { | |
319 | dictFreeEntryKey(d, he); | |
320 | dictFreeEntryVal(d, he); | |
321 | } | |
d9dd352b | 322 | zfree(he); |
5413c40d | 323 | d->ht[table].used--; |
324 | return DICT_OK; | |
ed9b544e | 325 | } |
5413c40d | 326 | prevHe = he; |
327 | he = he->next; | |
ed9b544e | 328 | } |
5413c40d | 329 | if (!dictIsRehashing(d)) break; |
ed9b544e | 330 | } |
331 | return DICT_ERR; /* not found */ | |
332 | } | |
333 | ||
334 | int dictDelete(dict *ht, const void *key) { | |
335 | return dictGenericDelete(ht,key,0); | |
336 | } | |
337 | ||
338 | int dictDeleteNoFree(dict *ht, const void *key) { | |
339 | return dictGenericDelete(ht,key,1); | |
340 | } | |
341 | ||
5413c40d | 342 | /* Destroy an entire dictionary */ |
343 | int _dictClear(dict *d, dictht *ht) | |
ed9b544e | 344 | { |
f2923bec | 345 | unsigned long i; |
ed9b544e | 346 | |
347 | /* Free all the elements */ | |
348 | for (i = 0; i < ht->size && ht->used > 0; i++) { | |
349 | dictEntry *he, *nextHe; | |
350 | ||
351 | if ((he = ht->table[i]) == NULL) continue; | |
352 | while(he) { | |
353 | nextHe = he->next; | |
5413c40d | 354 | dictFreeEntryKey(d, he); |
355 | dictFreeEntryVal(d, he); | |
d9dd352b | 356 | zfree(he); |
ed9b544e | 357 | ht->used--; |
358 | he = nextHe; | |
359 | } | |
360 | } | |
361 | /* Free the table and the allocated cache structure */ | |
d9dd352b | 362 | zfree(ht->table); |
ed9b544e | 363 | /* Re-initialize the table */ |
364 | _dictReset(ht); | |
365 | return DICT_OK; /* never fails */ | |
366 | } | |
367 | ||
368 | /* Clear & Release the hash table */ | |
5413c40d | 369 | void dictRelease(dict *d) |
ed9b544e | 370 | { |
5413c40d | 371 | _dictClear(d,&d->ht[0]); |
372 | _dictClear(d,&d->ht[1]); | |
d9dd352b | 373 | zfree(d); |
ed9b544e | 374 | } |
375 | ||
5413c40d | 376 | dictEntry *dictFind(dict *d, const void *key) |
ed9b544e | 377 | { |
378 | dictEntry *he; | |
5413c40d | 379 | unsigned int h, idx, table; |
380 | ||
381 | if (d->ht[0].size == 0) return NULL; /* We don't have a table at all */ | |
382 | if (dictIsRehashing(d)) _dictRehashStep(d); | |
383 | h = dictHashKey(d, key); | |
384 | for (table = 0; table <= 1; table++) { | |
385 | idx = h & d->ht[table].sizemask; | |
386 | he = d->ht[table].table[idx]; | |
387 | while(he) { | |
388 | if (dictCompareHashKeys(d, key, he->key)) | |
389 | return he; | |
390 | he = he->next; | |
391 | } | |
392 | if (!dictIsRehashing(d)) return NULL; | |
ed9b544e | 393 | } |
394 | return NULL; | |
395 | } | |
396 | ||
58e1c9c1 | 397 | void *dictFetchValue(dict *d, const void *key) { |
398 | dictEntry *he; | |
399 | ||
400 | he = dictFind(d,key); | |
401 | return he ? dictGetEntryVal(he) : NULL; | |
402 | } | |
403 | ||
5413c40d | 404 | dictIterator *dictGetIterator(dict *d) |
ed9b544e | 405 | { |
d9dd352b | 406 | dictIterator *iter = zmalloc(sizeof(*iter)); |
ed9b544e | 407 | |
5413c40d | 408 | iter->d = d; |
409 | iter->table = 0; | |
ed9b544e | 410 | iter->index = -1; |
411 | iter->entry = NULL; | |
412 | iter->nextEntry = NULL; | |
413 | return iter; | |
414 | } | |
415 | ||
416 | dictEntry *dictNext(dictIterator *iter) | |
417 | { | |
418 | while (1) { | |
419 | if (iter->entry == NULL) { | |
5413c40d | 420 | dictht *ht = &iter->d->ht[iter->table]; |
421 | if (iter->index == -1 && iter->table == 0) iter->d->iterators++; | |
ed9b544e | 422 | iter->index++; |
5413c40d | 423 | if (iter->index >= (signed) ht->size) { |
424 | if (dictIsRehashing(iter->d) && iter->table == 0) { | |
425 | iter->table++; | |
426 | iter->index = 0; | |
427 | ht = &iter->d->ht[1]; | |
428 | } else { | |
429 | break; | |
430 | } | |
431 | } | |
432 | iter->entry = ht->table[iter->index]; | |
ed9b544e | 433 | } else { |
434 | iter->entry = iter->nextEntry; | |
435 | } | |
436 | if (iter->entry) { | |
437 | /* We need to save the 'next' here, the iterator user | |
438 | * may delete the entry we are returning. */ | |
439 | iter->nextEntry = iter->entry->next; | |
440 | return iter->entry; | |
441 | } | |
442 | } | |
443 | return NULL; | |
444 | } | |
445 | ||
446 | void dictReleaseIterator(dictIterator *iter) | |
447 | { | |
5413c40d | 448 | if (!(iter->index == -1 && iter->table == 0)) iter->d->iterators--; |
d9dd352b | 449 | zfree(iter); |
ed9b544e | 450 | } |
451 | ||
452 | /* Return a random entry from the hash table. Useful to | |
453 | * implement randomized algorithms */ | |
5413c40d | 454 | dictEntry *dictGetRandomKey(dict *d) |
ed9b544e | 455 | { |
5413c40d | 456 | dictEntry *he, *orighe; |
ed9b544e | 457 | unsigned int h; |
458 | int listlen, listele; | |
459 | ||
5413c40d | 460 | if (dictSize(d) == 0) return NULL; |
461 | if (dictIsRehashing(d)) _dictRehashStep(d); | |
462 | if (dictIsRehashing(d)) { | |
463 | do { | |
464 | h = random() % (d->ht[0].size+d->ht[1].size); | |
465 | he = (h >= d->ht[0].size) ? d->ht[1].table[h - d->ht[0].size] : | |
466 | d->ht[0].table[h]; | |
467 | } while(he == NULL); | |
468 | } else { | |
469 | do { | |
470 | h = random() & d->ht[0].sizemask; | |
471 | he = d->ht[0].table[h]; | |
472 | } while(he == NULL); | |
473 | } | |
ed9b544e | 474 | |
475 | /* Now we found a non empty bucket, but it is a linked | |
476 | * list and we need to get a random element from the list. | |
5413c40d | 477 | * The only sane way to do so is counting the elements and |
ed9b544e | 478 | * select a random index. */ |
479 | listlen = 0; | |
5413c40d | 480 | orighe = he; |
ed9b544e | 481 | while(he) { |
482 | he = he->next; | |
483 | listlen++; | |
484 | } | |
485 | listele = random() % listlen; | |
5413c40d | 486 | he = orighe; |
ed9b544e | 487 | while(listele--) he = he->next; |
488 | return he; | |
489 | } | |
490 | ||
491 | /* ------------------------- private functions ------------------------------ */ | |
492 | ||
493 | /* Expand the hash table if needed */ | |
5413c40d | 494 | static int _dictExpandIfNeeded(dict *d) |
ed9b544e | 495 | { |
496 | /* If the hash table is empty expand it to the intial size, | |
497 | * if the table is "full" dobule its size. */ | |
5413c40d | 498 | if (dictIsRehashing(d)) return DICT_OK; |
499 | if (d->ht[0].size == 0) | |
500 | return dictExpand(d, DICT_HT_INITIAL_SIZE); | |
501 | if (d->ht[0].used >= d->ht[0].size && dict_can_resize) | |
502 | return dictExpand(d, ((d->ht[0].size > d->ht[0].used) ? | |
503 | d->ht[0].size : d->ht[0].used)*2); | |
ed9b544e | 504 | return DICT_OK; |
505 | } | |
506 | ||
507 | /* Our hash table capability is a power of two */ | |
f2923bec | 508 | static unsigned long _dictNextPower(unsigned long size) |
ed9b544e | 509 | { |
f2923bec | 510 | unsigned long i = DICT_HT_INITIAL_SIZE; |
ed9b544e | 511 | |
f2923bec | 512 | if (size >= LONG_MAX) return LONG_MAX; |
ed9b544e | 513 | while(1) { |
514 | if (i >= size) | |
515 | return i; | |
516 | i *= 2; | |
517 | } | |
518 | } | |
519 | ||
520 | /* Returns the index of a free slot that can be populated with | |
521 | * an hash entry for the given 'key'. | |
5413c40d | 522 | * If the key already exists, -1 is returned. |
523 | * | |
524 | * Note that if we are in the process of rehashing the hash table, the | |
525 | * index is always returned in the context of the second (new) hash table. */ | |
526 | static int _dictKeyIndex(dict *d, const void *key) | |
ed9b544e | 527 | { |
8ca3e9d1 | 528 | unsigned int h, idx, table; |
ed9b544e | 529 | dictEntry *he; |
530 | ||
531 | /* Expand the hashtable if needed */ | |
5413c40d | 532 | if (_dictExpandIfNeeded(d) == DICT_ERR) |
ed9b544e | 533 | return -1; |
534 | /* Compute the key hash value */ | |
5413c40d | 535 | h = dictHashKey(d, key); |
8ca3e9d1 | 536 | for (table = 0; table <= 1; table++) { |
537 | idx = h & d->ht[table].sizemask; | |
538 | /* Search if this slot does not already contain the given key */ | |
539 | he = d->ht[table].table[idx]; | |
540 | while(he) { | |
541 | if (dictCompareHashKeys(d, key, he->key)) | |
542 | return -1; | |
543 | he = he->next; | |
544 | } | |
545 | if (!dictIsRehashing(d)) break; | |
ed9b544e | 546 | } |
8ca3e9d1 | 547 | return idx; |
ed9b544e | 548 | } |
549 | ||
5413c40d | 550 | void dictEmpty(dict *d) { |
551 | _dictClear(d,&d->ht[0]); | |
552 | _dictClear(d,&d->ht[1]); | |
553 | d->rehashidx = -1; | |
554 | d->iterators = 0; | |
ed9b544e | 555 | } |
556 | ||
557 | #define DICT_STATS_VECTLEN 50 | |
5413c40d | 558 | static void _dictPrintStatsHt(dictht *ht) { |
f2923bec | 559 | unsigned long i, slots = 0, chainlen, maxchainlen = 0; |
560 | unsigned long totchainlen = 0; | |
561 | unsigned long clvector[DICT_STATS_VECTLEN]; | |
ed9b544e | 562 | |
563 | if (ht->used == 0) { | |
564 | printf("No stats available for empty dictionaries\n"); | |
565 | return; | |
566 | } | |
567 | ||
568 | for (i = 0; i < DICT_STATS_VECTLEN; i++) clvector[i] = 0; | |
569 | for (i = 0; i < ht->size; i++) { | |
570 | dictEntry *he; | |
571 | ||
572 | if (ht->table[i] == NULL) { | |
573 | clvector[0]++; | |
574 | continue; | |
575 | } | |
576 | slots++; | |
577 | /* For each hash entry on this slot... */ | |
578 | chainlen = 0; | |
579 | he = ht->table[i]; | |
580 | while(he) { | |
581 | chainlen++; | |
582 | he = he->next; | |
583 | } | |
584 | clvector[(chainlen < DICT_STATS_VECTLEN) ? chainlen : (DICT_STATS_VECTLEN-1)]++; | |
585 | if (chainlen > maxchainlen) maxchainlen = chainlen; | |
586 | totchainlen += chainlen; | |
587 | } | |
588 | printf("Hash table stats:\n"); | |
f2923bec | 589 | printf(" table size: %ld\n", ht->size); |
590 | printf(" number of elements: %ld\n", ht->used); | |
591 | printf(" different slots: %ld\n", slots); | |
592 | printf(" max chain length: %ld\n", maxchainlen); | |
ed9b544e | 593 | printf(" avg chain length (counted): %.02f\n", (float)totchainlen/slots); |
594 | printf(" avg chain length (computed): %.02f\n", (float)ht->used/slots); | |
595 | printf(" Chain length distribution:\n"); | |
596 | for (i = 0; i < DICT_STATS_VECTLEN-1; i++) { | |
597 | if (clvector[i] == 0) continue; | |
f2923bec | 598 | printf(" %s%ld: %ld (%.02f%%)\n",(i == DICT_STATS_VECTLEN-1)?">= ":"", i, clvector[i], ((float)clvector[i]/ht->size)*100); |
ed9b544e | 599 | } |
600 | } | |
601 | ||
5413c40d | 602 | void dictPrintStats(dict *d) { |
603 | _dictPrintStatsHt(&d->ht[0]); | |
604 | if (dictIsRehashing(d)) { | |
605 | printf("-- Rehashing into ht[1]:\n"); | |
606 | _dictPrintStatsHt(&d->ht[1]); | |
607 | } | |
608 | } | |
609 | ||
884d4b39 | 610 | void dictEnableResize(void) { |
611 | dict_can_resize = 1; | |
612 | } | |
613 | ||
614 | void dictDisableResize(void) { | |
dae121d9 | 615 | dict_can_resize = 0; |
884d4b39 | 616 | } |
617 | ||
e0be2289 | 618 | #if 0 |
619 | ||
620 | /* The following are just example hash table types implementations. | |
621 | * Not useful for Redis so they are commented out. | |
622 | */ | |
623 | ||
ed9b544e | 624 | /* ----------------------- StringCopy Hash Table Type ------------------------*/ |
625 | ||
626 | static unsigned int _dictStringCopyHTHashFunction(const void *key) | |
627 | { | |
628 | return dictGenHashFunction(key, strlen(key)); | |
629 | } | |
630 | ||
b1e0bd4b | 631 | static void *_dictStringDup(void *privdata, const void *key) |
ed9b544e | 632 | { |
633 | int len = strlen(key); | |
d9dd352b | 634 | char *copy = zmalloc(len+1); |
ed9b544e | 635 | DICT_NOTUSED(privdata); |
636 | ||
637 | memcpy(copy, key, len); | |
638 | copy[len] = '\0'; | |
639 | return copy; | |
640 | } | |
641 | ||
ed9b544e | 642 | static int _dictStringCopyHTKeyCompare(void *privdata, const void *key1, |
643 | const void *key2) | |
644 | { | |
645 | DICT_NOTUSED(privdata); | |
646 | ||
647 | return strcmp(key1, key2) == 0; | |
648 | } | |
649 | ||
b1e0bd4b | 650 | static void _dictStringDestructor(void *privdata, void *key) |
ed9b544e | 651 | { |
652 | DICT_NOTUSED(privdata); | |
653 | ||
d9dd352b | 654 | zfree(key); |
ed9b544e | 655 | } |
656 | ||
657 | dictType dictTypeHeapStringCopyKey = { | |
b1e0bd4b BK |
658 | _dictStringCopyHTHashFunction, /* hash function */ |
659 | _dictStringDup, /* key dup */ | |
660 | NULL, /* val dup */ | |
661 | _dictStringCopyHTKeyCompare, /* key compare */ | |
662 | _dictStringDestructor, /* key destructor */ | |
663 | NULL /* val destructor */ | |
ed9b544e | 664 | }; |
665 | ||
666 | /* This is like StringCopy but does not auto-duplicate the key. | |
667 | * It's used for intepreter's shared strings. */ | |
668 | dictType dictTypeHeapStrings = { | |
b1e0bd4b BK |
669 | _dictStringCopyHTHashFunction, /* hash function */ |
670 | NULL, /* key dup */ | |
671 | NULL, /* val dup */ | |
672 | _dictStringCopyHTKeyCompare, /* key compare */ | |
673 | _dictStringDestructor, /* key destructor */ | |
674 | NULL /* val destructor */ | |
ed9b544e | 675 | }; |
676 | ||
677 | /* This is like StringCopy but also automatically handle dynamic | |
678 | * allocated C strings as values. */ | |
679 | dictType dictTypeHeapStringCopyKeyValue = { | |
b1e0bd4b BK |
680 | _dictStringCopyHTHashFunction, /* hash function */ |
681 | _dictStringDup, /* key dup */ | |
682 | _dictStringDup, /* val dup */ | |
683 | _dictStringCopyHTKeyCompare, /* key compare */ | |
684 | _dictStringDestructor, /* key destructor */ | |
685 | _dictStringDestructor, /* val destructor */ | |
ed9b544e | 686 | }; |
e0be2289 | 687 | #endif |