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