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