]> git.saurik.com Git - redis.git/blame_incremental - src/dict.c
use the function deprecated attribute if compiling with GCC to get warnings for mallo...
[redis.git] / src / dict.c
... / ...
CommitLineData
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. */
53static int dict_can_resize = 1;
54
55/* -------------------------- private prototypes ---------------------------- */
56
57static int _dictExpandIfNeeded(dict *ht);
58static unsigned long _dictNextPower(unsigned long size);
59static int _dictKeyIndex(dict *ht, const void *key);
60static int _dictInit(dict *ht, dictType *type, void *privDataPtr);
61
62/* -------------------------- hash functions -------------------------------- */
63
64/* Thomas Wang's 32 bit Mix Function */
65unsigned 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 */
77unsigned 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. */
84unsigned 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(). */
96static 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 */
105dict *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 */
115int _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 */
129int 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 */
141int 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 /* Allocate the new hashtable and initialize all pointers to NULL */
152 n.size = realsize;
153 n.sizemask = realsize-1;
154 n.table = zcalloc(realsize*sizeof(dictEntry*));
155 n.used = 0;
156
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 }
163
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. */
174int 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) {
182 zfree(d->ht[0].table);
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) {
195 unsigned int h;
196
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;
205 }
206 d->ht[0].table[d->rehashidx] = NULL;
207 d->rehashidx++;
208 }
209 return 1;
210}
211
212long 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 */
220int 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
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. */
239static void _dictRehashStep(dict *d) {
240 if (d->iterators == 0) dictRehash(d,1);
241}
242
243/* Add an element to the target hash table */
244int dictAdd(dict *d, void *key, void *val)
245{
246 int index;
247 dictEntry *entry;
248 dictht *ht;
249
250 if (dictIsRehashing(d)) _dictRehashStep(d);
251
252 /* Get the index of the new element, or -1 if
253 * the element already exists. */
254 if ((index = _dictKeyIndex(d, key)) == -1)
255 return DICT_ERR;
256
257 /* Allocates the memory and stores key */
258 ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
259 entry = zmalloc(sizeof(*entry));
260 entry->next = ht->table[index];
261 ht->table[index] = entry;
262 ht->used++;
263
264 /* Set the hash entry fields. */
265 dictSetHashKey(d, entry, key);
266 dictSetHashVal(d, entry, val);
267 return DICT_OK;
268}
269
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. */
274int dictReplace(dict *d, void *key, void *val)
275{
276 dictEntry *entry, auxentry;
277
278 /* Try to add the element. If the key
279 * does not exists dictAdd will suceed. */
280 if (dictAdd(d, key, val) == DICT_OK)
281 return 1;
282 /* It already exists, get the entry */
283 entry = dictFind(d, key);
284 /* Free the old value and set the new one */
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;
291 dictSetHashVal(d, entry, val);
292 dictFreeEntryVal(d, &auxentry);
293 return 0;
294}
295
296/* Search and remove an element */
297static int dictGenericDelete(dict *d, const void *key, int nofree)
298{
299 unsigned int h, idx;
300 dictEntry *he, *prevHe;
301 int table;
302
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);
306
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 }
322 zfree(he);
323 d->ht[table].used--;
324 return DICT_OK;
325 }
326 prevHe = he;
327 he = he->next;
328 }
329 if (!dictIsRehashing(d)) break;
330 }
331 return DICT_ERR; /* not found */
332}
333
334int dictDelete(dict *ht, const void *key) {
335 return dictGenericDelete(ht,key,0);
336}
337
338int dictDeleteNoFree(dict *ht, const void *key) {
339 return dictGenericDelete(ht,key,1);
340}
341
342/* Destroy an entire dictionary */
343int _dictClear(dict *d, dictht *ht)
344{
345 unsigned long i;
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;
354 dictFreeEntryKey(d, he);
355 dictFreeEntryVal(d, he);
356 zfree(he);
357 ht->used--;
358 he = nextHe;
359 }
360 }
361 /* Free the table and the allocated cache structure */
362 zfree(ht->table);
363 /* Re-initialize the table */
364 _dictReset(ht);
365 return DICT_OK; /* never fails */
366}
367
368/* Clear & Release the hash table */
369void dictRelease(dict *d)
370{
371 _dictClear(d,&d->ht[0]);
372 _dictClear(d,&d->ht[1]);
373 zfree(d);
374}
375
376dictEntry *dictFind(dict *d, const void *key)
377{
378 dictEntry *he;
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;
393 }
394 return NULL;
395}
396
397void *dictFetchValue(dict *d, const void *key) {
398 dictEntry *he;
399
400 he = dictFind(d,key);
401 return he ? dictGetEntryVal(he) : NULL;
402}
403
404dictIterator *dictGetIterator(dict *d)
405{
406 dictIterator *iter = zmalloc(sizeof(*iter));
407
408 iter->d = d;
409 iter->table = 0;
410 iter->index = -1;
411 iter->entry = NULL;
412 iter->nextEntry = NULL;
413 return iter;
414}
415
416dictEntry *dictNext(dictIterator *iter)
417{
418 while (1) {
419 if (iter->entry == NULL) {
420 dictht *ht = &iter->d->ht[iter->table];
421 if (iter->index == -1 && iter->table == 0) iter->d->iterators++;
422 iter->index++;
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];
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
446void dictReleaseIterator(dictIterator *iter)
447{
448 if (!(iter->index == -1 && iter->table == 0)) iter->d->iterators--;
449 zfree(iter);
450}
451
452/* Return a random entry from the hash table. Useful to
453 * implement randomized algorithms */
454dictEntry *dictGetRandomKey(dict *d)
455{
456 dictEntry *he, *orighe;
457 unsigned int h;
458 int listlen, listele;
459
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 }
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.
477 * The only sane way to do so is counting the elements and
478 * select a random index. */
479 listlen = 0;
480 orighe = he;
481 while(he) {
482 he = he->next;
483 listlen++;
484 }
485 listele = random() % listlen;
486 he = orighe;
487 while(listele--) he = he->next;
488 return he;
489}
490
491/* ------------------------- private functions ------------------------------ */
492
493/* Expand the hash table if needed */
494static int _dictExpandIfNeeded(dict *d)
495{
496 /* If the hash table is empty expand it to the intial size,
497 * if the table is "full" dobule its size. */
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);
504 return DICT_OK;
505}
506
507/* Our hash table capability is a power of two */
508static unsigned long _dictNextPower(unsigned long size)
509{
510 unsigned long i = DICT_HT_INITIAL_SIZE;
511
512 if (size >= LONG_MAX) return LONG_MAX;
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'.
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. */
526static int _dictKeyIndex(dict *d, const void *key)
527{
528 unsigned int h, idx, table;
529 dictEntry *he;
530
531 /* Expand the hashtable if needed */
532 if (_dictExpandIfNeeded(d) == DICT_ERR)
533 return -1;
534 /* Compute the key hash value */
535 h = dictHashKey(d, key);
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;
546 }
547 return idx;
548}
549
550void dictEmpty(dict *d) {
551 _dictClear(d,&d->ht[0]);
552 _dictClear(d,&d->ht[1]);
553 d->rehashidx = -1;
554 d->iterators = 0;
555}
556
557#define DICT_STATS_VECTLEN 50
558static void _dictPrintStatsHt(dictht *ht) {
559 unsigned long i, slots = 0, chainlen, maxchainlen = 0;
560 unsigned long totchainlen = 0;
561 unsigned long clvector[DICT_STATS_VECTLEN];
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");
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);
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;
598 printf(" %s%ld: %ld (%.02f%%)\n",(i == DICT_STATS_VECTLEN-1)?">= ":"", i, clvector[i], ((float)clvector[i]/ht->size)*100);
599 }
600}
601
602void 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
610void dictEnableResize(void) {
611 dict_can_resize = 1;
612}
613
614void dictDisableResize(void) {
615 dict_can_resize = 0;
616}
617
618/* ----------------------- StringCopy Hash Table Type ------------------------*/
619
620static unsigned int _dictStringCopyHTHashFunction(const void *key)
621{
622 return dictGenHashFunction(key, strlen(key));
623}
624
625static void *_dictStringDup(void *privdata, const void *key)
626{
627 int len = strlen(key);
628 char *copy = zmalloc(len+1);
629 DICT_NOTUSED(privdata);
630
631 memcpy(copy, key, len);
632 copy[len] = '\0';
633 return copy;
634}
635
636static int _dictStringCopyHTKeyCompare(void *privdata, const void *key1,
637 const void *key2)
638{
639 DICT_NOTUSED(privdata);
640
641 return strcmp(key1, key2) == 0;
642}
643
644static void _dictStringDestructor(void *privdata, void *key)
645{
646 DICT_NOTUSED(privdata);
647
648 zfree(key);
649}
650
651dictType dictTypeHeapStringCopyKey = {
652 _dictStringCopyHTHashFunction, /* hash function */
653 _dictStringDup, /* key dup */
654 NULL, /* val dup */
655 _dictStringCopyHTKeyCompare, /* key compare */
656 _dictStringDestructor, /* key destructor */
657 NULL /* val destructor */
658};
659
660/* This is like StringCopy but does not auto-duplicate the key.
661 * It's used for intepreter's shared strings. */
662dictType dictTypeHeapStrings = {
663 _dictStringCopyHTHashFunction, /* hash function */
664 NULL, /* key dup */
665 NULL, /* val dup */
666 _dictStringCopyHTKeyCompare, /* key compare */
667 _dictStringDestructor, /* key destructor */
668 NULL /* val destructor */
669};
670
671/* This is like StringCopy but also automatically handle dynamic
672 * allocated C strings as values. */
673dictType dictTypeHeapStringCopyKeyValue = {
674 _dictStringCopyHTHashFunction, /* hash function */
675 _dictStringDup, /* key dup */
676 _dictStringDup, /* val dup */
677 _dictStringCopyHTKeyCompare, /* key compare */
678 _dictStringDestructor, /* key destructor */
679 _dictStringDestructor, /* val destructor */
680};