]> git.saurik.com Git - redis.git/blame - src/dict.c
Merge remote branch 'pietern/ziplist-eff'
[redis.git] / src / dict.c
CommitLineData
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
3856f147 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. */
884d4b39 57static int dict_can_resize = 1;
3856f147 58static unsigned int dict_force_resize_ratio = 5;
884d4b39 59
ed9b544e 60/* -------------------------- private prototypes ---------------------------- */
61
62static int _dictExpandIfNeeded(dict *ht);
f2923bec 63static unsigned long _dictNextPower(unsigned long size);
ed9b544e 64static int _dictKeyIndex(dict *ht, const void *key);
65static int _dictInit(dict *ht, dictType *type, void *privDataPtr);
66
67/* -------------------------- hash functions -------------------------------- */
68
69/* Thomas Wang's 32 bit Mix Function */
70unsigned 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 */
82unsigned 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. */
89unsigned 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(). */
5413c40d 101static void _dictReset(dictht *ht)
ed9b544e 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 */
110dict *dictCreate(dictType *type,
111 void *privDataPtr)
112{
d9dd352b 113 dict *d = zmalloc(sizeof(*d));
ed9b544e 114
5413c40d 115 _dictInit(d,type,privDataPtr);
116 return d;
ed9b544e 117}
118
119/* Initialize the hash table */
5413c40d 120int _dictInit(dict *d, dictType *type,
ed9b544e 121 void *privDataPtr)
122{
5413c40d 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;
ed9b544e 129 return DICT_OK;
130}
131
132/* Resize the table to the minimal size that contains all the elements,
3856f147 133 * but with the invariant of a USER/BUCKETS ratio near to <= 1 */
5413c40d 134int dictResize(dict *d)
ed9b544e 135{
5413c40d 136 int minimal;
ed9b544e 137
5413c40d 138 if (!dict_can_resize || dictIsRehashing(d)) return DICT_ERR;
139 minimal = d->ht[0].used;
ed9b544e 140 if (minimal < DICT_HT_INITIAL_SIZE)
141 minimal = DICT_HT_INITIAL_SIZE;
5413c40d 142 return dictExpand(d, minimal);
ed9b544e 143}
144
145/* Expand or create the hashtable */
5413c40d 146int dictExpand(dict *d, unsigned long size)
ed9b544e 147{
5413c40d 148 dictht n; /* the new hashtable */
149 unsigned long realsize = _dictNextPower(size);
ed9b544e 150
151 /* the size is invalid if it is smaller than the number of
152 * elements already inside the hashtable */
5413c40d 153 if (dictIsRehashing(d) || d->ht[0].used > size)
ed9b544e 154 return DICT_ERR;
155
399f2f40 156 /* Allocate the new hashtable and initialize all pointers to NULL */
ed9b544e 157 n.size = realsize;
158 n.sizemask = realsize-1;
399f2f40 159 n.table = zcalloc(realsize*sizeof(dictEntry*));
5413c40d 160 n.used = 0;
ed9b544e 161
5413c40d 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 }
ed9b544e 168
5413c40d 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. */
179int 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) {
d9dd352b 187 zfree(d->ht[0].table);
5413c40d 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) {
ed9b544e 200 unsigned int h;
201
5413c40d 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;
ed9b544e 210 }
5413c40d 211 d->ht[0].table[d->rehashidx] = NULL;
212 d->rehashidx++;
ed9b544e 213 }
5413c40d 214 return 1;
215}
ed9b544e 216
8ca3e9d1 217long 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 */
225int 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
5413c40d 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. */
244static void _dictRehashStep(dict *d) {
245 if (d->iterators == 0) dictRehash(d,1);
ed9b544e 246}
247
248/* Add an element to the target hash table */
5413c40d 249int dictAdd(dict *d, void *key, void *val)
ed9b544e 250{
251 int index;
252 dictEntry *entry;
5413c40d 253 dictht *ht;
254
255 if (dictIsRehashing(d)) _dictRehashStep(d);
ed9b544e 256
257 /* Get the index of the new element, or -1 if
258 * the element already exists. */
5413c40d 259 if ((index = _dictKeyIndex(d, key)) == -1)
ed9b544e 260 return DICT_ERR;
261
262 /* Allocates the memory and stores key */
5413c40d 263 ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
d9dd352b 264 entry = zmalloc(sizeof(*entry));
ed9b544e 265 entry->next = ht->table[index];
266 ht->table[index] = entry;
5413c40d 267 ht->used++;
ed9b544e 268
269 /* Set the hash entry fields. */
5413c40d 270 dictSetHashKey(d, entry, key);
271 dictSetHashVal(d, entry, val);
ed9b544e 272 return DICT_OK;
273}
274
121796f7 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. */
5413c40d 279int dictReplace(dict *d, void *key, void *val)
ed9b544e 280{
2069d06a 281 dictEntry *entry, auxentry;
ed9b544e 282
283 /* Try to add the element. If the key
284 * does not exists dictAdd will suceed. */
5413c40d 285 if (dictAdd(d, key, val) == DICT_OK)
121796f7 286 return 1;
ed9b544e 287 /* It already exists, get the entry */
5413c40d 288 entry = dictFind(d, key);
ed9b544e 289 /* Free the old value and set the new one */
2069d06a 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;
5413c40d 296 dictSetHashVal(d, entry, val);
297 dictFreeEntryVal(d, &auxentry);
121796f7 298 return 0;
ed9b544e 299}
300
301/* Search and remove an element */
5413c40d 302static int dictGenericDelete(dict *d, const void *key, int nofree)
ed9b544e 303{
5413c40d 304 unsigned int h, idx;
ed9b544e 305 dictEntry *he, *prevHe;
5413c40d 306 int table;
ed9b544e 307
5413c40d 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);
ed9b544e 311
5413c40d 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 }
d9dd352b 327 zfree(he);
5413c40d 328 d->ht[table].used--;
329 return DICT_OK;
ed9b544e 330 }
5413c40d 331 prevHe = he;
332 he = he->next;
ed9b544e 333 }
5413c40d 334 if (!dictIsRehashing(d)) break;
ed9b544e 335 }
336 return DICT_ERR; /* not found */
337}
338
339int dictDelete(dict *ht, const void *key) {
340 return dictGenericDelete(ht,key,0);
341}
342
343int dictDeleteNoFree(dict *ht, const void *key) {
344 return dictGenericDelete(ht,key,1);
345}
346
5413c40d 347/* Destroy an entire dictionary */
348int _dictClear(dict *d, dictht *ht)
ed9b544e 349{
f2923bec 350 unsigned long i;
ed9b544e 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;
5413c40d 359 dictFreeEntryKey(d, he);
360 dictFreeEntryVal(d, he);
d9dd352b 361 zfree(he);
ed9b544e 362 ht->used--;
363 he = nextHe;
364 }
365 }
366 /* Free the table and the allocated cache structure */
d9dd352b 367 zfree(ht->table);
ed9b544e 368 /* Re-initialize the table */
369 _dictReset(ht);
370 return DICT_OK; /* never fails */
371}
372
373/* Clear & Release the hash table */
5413c40d 374void dictRelease(dict *d)
ed9b544e 375{
5413c40d 376 _dictClear(d,&d->ht[0]);
377 _dictClear(d,&d->ht[1]);
d9dd352b 378 zfree(d);
ed9b544e 379}
380
5413c40d 381dictEntry *dictFind(dict *d, const void *key)
ed9b544e 382{
383 dictEntry *he;
5413c40d 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;
ed9b544e 398 }
399 return NULL;
400}
401
58e1c9c1 402void *dictFetchValue(dict *d, const void *key) {
403 dictEntry *he;
404
405 he = dictFind(d,key);
406 return he ? dictGetEntryVal(he) : NULL;
407}
408
5413c40d 409dictIterator *dictGetIterator(dict *d)
ed9b544e 410{
d9dd352b 411 dictIterator *iter = zmalloc(sizeof(*iter));
ed9b544e 412
5413c40d 413 iter->d = d;
414 iter->table = 0;
ed9b544e 415 iter->index = -1;
416 iter->entry = NULL;
417 iter->nextEntry = NULL;
418 return iter;
419}
420
421dictEntry *dictNext(dictIterator *iter)
422{
423 while (1) {
424 if (iter->entry == NULL) {
5413c40d 425 dictht *ht = &iter->d->ht[iter->table];
426 if (iter->index == -1 && iter->table == 0) iter->d->iterators++;
ed9b544e 427 iter->index++;
5413c40d 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];
ed9b544e 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
451void dictReleaseIterator(dictIterator *iter)
452{
5413c40d 453 if (!(iter->index == -1 && iter->table == 0)) iter->d->iterators--;
d9dd352b 454 zfree(iter);
ed9b544e 455}
456
457/* Return a random entry from the hash table. Useful to
458 * implement randomized algorithms */
5413c40d 459dictEntry *dictGetRandomKey(dict *d)
ed9b544e 460{
5413c40d 461 dictEntry *he, *orighe;
ed9b544e 462 unsigned int h;
463 int listlen, listele;
464
5413c40d 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 }
ed9b544e 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.
5413c40d 482 * The only sane way to do so is counting the elements and
ed9b544e 483 * select a random index. */
484 listlen = 0;
5413c40d 485 orighe = he;
ed9b544e 486 while(he) {
487 he = he->next;
488 listlen++;
489 }
490 listele = random() % listlen;
5413c40d 491 he = orighe;
ed9b544e 492 while(listele--) he = he->next;
493 return he;
494}
495
496/* ------------------------- private functions ------------------------------ */
497
498/* Expand the hash table if needed */
5413c40d 499static int _dictExpandIfNeeded(dict *d)
ed9b544e 500{
3856f147 501 /* Incremental rehashing already in progress. Return. */
5413c40d 502 if (dictIsRehashing(d)) return DICT_OK;
3856f147 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 {
5413c40d 515 return dictExpand(d, ((d->ht[0].size > d->ht[0].used) ?
516 d->ht[0].size : d->ht[0].used)*2);
3856f147 517 }
ed9b544e 518 return DICT_OK;
519}
520
521/* Our hash table capability is a power of two */
f2923bec 522static unsigned long _dictNextPower(unsigned long size)
ed9b544e 523{
f2923bec 524 unsigned long i = DICT_HT_INITIAL_SIZE;
ed9b544e 525
f2923bec 526 if (size >= LONG_MAX) return LONG_MAX;
ed9b544e 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'.
5413c40d 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. */
540static int _dictKeyIndex(dict *d, const void *key)
ed9b544e 541{
8ca3e9d1 542 unsigned int h, idx, table;
ed9b544e 543 dictEntry *he;
544
545 /* Expand the hashtable if needed */
5413c40d 546 if (_dictExpandIfNeeded(d) == DICT_ERR)
ed9b544e 547 return -1;
548 /* Compute the key hash value */
5413c40d 549 h = dictHashKey(d, key);
8ca3e9d1 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;
ed9b544e 560 }
8ca3e9d1 561 return idx;
ed9b544e 562}
563
5413c40d 564void dictEmpty(dict *d) {
565 _dictClear(d,&d->ht[0]);
566 _dictClear(d,&d->ht[1]);
567 d->rehashidx = -1;
568 d->iterators = 0;
ed9b544e 569}
570
571#define DICT_STATS_VECTLEN 50
5413c40d 572static void _dictPrintStatsHt(dictht *ht) {
f2923bec 573 unsigned long i, slots = 0, chainlen, maxchainlen = 0;
574 unsigned long totchainlen = 0;
575 unsigned long clvector[DICT_STATS_VECTLEN];
ed9b544e 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");
f2923bec 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);
ed9b544e 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;
f2923bec 612 printf(" %s%ld: %ld (%.02f%%)\n",(i == DICT_STATS_VECTLEN-1)?">= ":"", i, clvector[i], ((float)clvector[i]/ht->size)*100);
ed9b544e 613 }
614}
615
5413c40d 616void 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
884d4b39 624void dictEnableResize(void) {
625 dict_can_resize = 1;
626}
627
628void dictDisableResize(void) {
dae121d9 629 dict_can_resize = 0;
884d4b39 630}
631
e0be2289 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
ed9b544e 638/* ----------------------- StringCopy Hash Table Type ------------------------*/
639
640static unsigned int _dictStringCopyHTHashFunction(const void *key)
641{
642 return dictGenHashFunction(key, strlen(key));
643}
644
b1e0bd4b 645static void *_dictStringDup(void *privdata, const void *key)
ed9b544e 646{
647 int len = strlen(key);
d9dd352b 648 char *copy = zmalloc(len+1);
ed9b544e 649 DICT_NOTUSED(privdata);
650
651 memcpy(copy, key, len);
652 copy[len] = '\0';
653 return copy;
654}
655
ed9b544e 656static 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
b1e0bd4b 664static void _dictStringDestructor(void *privdata, void *key)
ed9b544e 665{
666 DICT_NOTUSED(privdata);
667
d9dd352b 668 zfree(key);
ed9b544e 669}
670
671dictType dictTypeHeapStringCopyKey = {
b1e0bd4b
BK
672 _dictStringCopyHTHashFunction, /* hash function */
673 _dictStringDup, /* key dup */
674 NULL, /* val dup */
675 _dictStringCopyHTKeyCompare, /* key compare */
676 _dictStringDestructor, /* key destructor */
677 NULL /* val destructor */
ed9b544e 678};
679
680/* This is like StringCopy but does not auto-duplicate the key.
681 * It's used for intepreter's shared strings. */
682dictType dictTypeHeapStrings = {
b1e0bd4b
BK
683 _dictStringCopyHTHashFunction, /* hash function */
684 NULL, /* key dup */
685 NULL, /* val dup */
686 _dictStringCopyHTKeyCompare, /* key compare */
687 _dictStringDestructor, /* key destructor */
688 NULL /* val destructor */
ed9b544e 689};
690
691/* This is like StringCopy but also automatically handle dynamic
692 * allocated C strings as values. */
693dictType dictTypeHeapStringCopyKeyValue = {
b1e0bd4b
BK
694 _dictStringCopyHTHashFunction, /* hash function */
695 _dictStringDup, /* key dup */
696 _dictStringDup, /* val dup */
697 _dictStringCopyHTKeyCompare, /* key compare */
698 _dictStringDestructor, /* key destructor */
699 _dictStringDestructor, /* val destructor */
ed9b544e 700};
e0be2289 701#endif