summaryrefslogtreecommitdiff
path: root/dict.h
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2010-04-15 11:59:13 +0200
committerantirez <antirez@gmail.com>2010-04-15 11:59:13 +0200
commit5413c40da79d06b53b276b5358e04706a2738e9b (patch)
treee5ea4ada15f95e133c43dbd8b618b956c484e7a6 /dict.h
parente6cca5dba6ad046625c195c9c4f39c7ce06ad080 (diff)
downloadredis-5413c40da79d06b53b276b5358e04706a2738e9b.tar.gz
Incrementally rehahsing hash table! Thanks to Derek Collison and Pieter Noordhuis for feedbacks/help
Diffstat (limited to 'dict.h')
-rw-r--r--dict.h53
1 files changed, 32 insertions, 21 deletions
diff --git a/dict.h b/dict.h
index 3a8e9b0b8..15dbb37f3 100644
--- a/dict.h
+++ b/dict.h
@@ -57,17 +57,26 @@ typedef struct dictType {
void (*valDestructor)(void *privdata, void *obj);
} dictType;
-typedef struct dict {
+/* This is our hash table structure. Every dictionary has two of this as we
+ * implement incremental rehashing, for the old to the new table. */
+typedef struct dictht {
dictEntry **table;
- dictType *type;
unsigned long size;
unsigned long sizemask;
unsigned long used;
+} dictht;
+
+typedef struct dict {
+ dictType *type;
void *privdata;
+ dictht ht[2];
+ int rehashidx; /* rehashing not in progress if rehashidx == -1 */
+ int iterators; /* number of iterators currently running */
} dict;
typedef struct dictIterator {
- dict *ht;
+ dict *d;
+ int table;
int index;
dictEntry *entry, *nextEntry;
} dictIterator;
@@ -76,39 +85,40 @@ typedef struct dictIterator {
#define DICT_HT_INITIAL_SIZE 4
/* ------------------------------- Macros ------------------------------------*/
-#define dictFreeEntryVal(ht, entry) \
- if ((ht)->type->valDestructor) \
- (ht)->type->valDestructor((ht)->privdata, (entry)->val)
+#define dictFreeEntryVal(d, entry) \
+ if ((d)->type->valDestructor) \
+ (d)->type->valDestructor((d)->privdata, (entry)->val)
-#define dictSetHashVal(ht, entry, _val_) do { \
- if ((ht)->type->valDup) \
- entry->val = (ht)->type->valDup((ht)->privdata, _val_); \
+#define dictSetHashVal(d, entry, _val_) do { \
+ if ((d)->type->valDup) \
+ entry->val = (d)->type->valDup((d)->privdata, _val_); \
else \
entry->val = (_val_); \
} while(0)
-#define dictFreeEntryKey(ht, entry) \
- if ((ht)->type->keyDestructor) \
- (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
+#define dictFreeEntryKey(d, entry) \
+ if ((d)->type->keyDestructor) \
+ (d)->type->keyDestructor((d)->privdata, (entry)->key)
-#define dictSetHashKey(ht, entry, _key_) do { \
- if ((ht)->type->keyDup) \
- entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
+#define dictSetHashKey(d, entry, _key_) do { \
+ if ((d)->type->keyDup) \
+ entry->key = (d)->type->keyDup((d)->privdata, _key_); \
else \
entry->key = (_key_); \
} while(0)
-#define dictCompareHashKeys(ht, key1, key2) \
- (((ht)->type->keyCompare) ? \
- (ht)->type->keyCompare((ht)->privdata, key1, key2) : \
+#define dictCompareHashKeys(d, key1, key2) \
+ (((d)->type->keyCompare) ? \
+ (d)->type->keyCompare((d)->privdata, key1, key2) : \
(key1) == (key2))
-#define dictHashKey(ht, key) (ht)->type->hashFunction(key)
+#define dictHashKey(d, key) (d)->type->hashFunction(key)
#define dictGetEntryKey(he) ((he)->key)
#define dictGetEntryVal(he) ((he)->val)
-#define dictSlots(ht) ((ht)->size)
-#define dictSize(ht) ((ht)->used)
+#define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size)
+#define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used)
+#define dictIsRehashing(ht) ((ht)->rehashidx != -1)
/* API */
dict *dictCreate(dictType *type, void *privDataPtr);
@@ -129,6 +139,7 @@ unsigned int dictGenHashFunction(const unsigned char *buf, int len);
void dictEmpty(dict *ht);
void dictEnableResize(void);
void dictDisableResize(void);
+int dictRehash(dict *d, int n);
/* Hash table types */
extern dictType dictTypeHeapStringCopyKey;