redis的字典定义和实现在dict.h和dict.c文件中。
1.字典结构
其中涉及到数据结构,如下所示:
1.1 字典类型,包含了一系列字典所需要用到的函数
1.2 哈希表结构,每个字典有两个哈希表。当哈希表扩容时实现散列。
1.3 dictEntry为字典的条目,其定义如下:
2. 字典的遍历--字典遍历器
注意:当safe=1时,该遍历器是安全的,即字典可以在遍历的同时执行dictAdd,dictFind,和别的函数。否则遍历器是不安全的,遍历时只能执行dictNext()。
迭代器提供了遍历字典中所有元素的方法,通过dicGetIterator()获得迭代器后,使用dictNext(dictIterator *)获得下一个元素。遍历的过程,先从ht[0]开始,依次从第一个桶table[0]开始遍历桶中的元素,然后遍历table[1],'***,table[size],若正在扩容,则会继续遍历ht[1]中的桶。遍历桶中元素时,依次访问链表中的每一个元素。
3.宏定义函数
<span style="color: #0000ff;">if ((d)->type-><span style="color: #000000;">valDup) \
entry->v.val = (d)->type->valDup((d)-><span style="color: #000000;">privdata,val); \
<span style="color: #0000ff;">else<span style="color: #000000;"> \
entry->v.val =<span style="color: #000000;"> (val); \
} <span style="color: #0000ff;">while(<span style="color: #800080;">0<span style="color: #000000;">)
<span style="color: #0000ff;">#define dictSetSignedIntegerVal(entry,val) \
<span style="color: #0000ff;">do { entry->v.s64 = val; } <span style="color: #0000ff;">while(<span style="color: #800080;">0<span style="color: #000000;">)
<span style="color: #0000ff;">#define dictSetUnsignedIntegerVal(entry,val) \
<span style="color: #0000ff;">do { entry->v.u64 = val; } <span style="color: #0000ff;">while(<span style="color: #800080;">0<span style="color: #000000;">)
<span style="color: #0000ff;">#define dictFreeKey(d,entry) \
<span style="color: #0000ff;">if ((d)->type-><span style="color: #000000;">keyDestructor) \
(d)->type->keyDestructor((d)->privdata,(entry)-><span style="color: #000000;">key)
<span style="color: #0000ff;">#define dictSetKey(d,key) do { \
<span style="color: #0000ff;">if ((d)->type-><span style="color: #000000;">keyDup) \
entry->key = (d)->type->keyDup((d)-><span style="color: #000000;">privdata,key); \
<span style="color: #0000ff;">else<span style="color: #000000;"> \
entry->key =<span style="color: #000000;"> (key); \
} <span style="color: #0000ff;">while(<span style="color: #800080;">0<span style="color: #000000;">)
<span style="color: #0000ff;">#define dictCompareKeys(d,key1,key2) \<span style="color: #000000;">
(((d)->type->keyCompare) ?<span style="color: #000000;"> \
(d)->type->keyCompare((d)-><span style="color: #000000;">privdata,key2) : \
(key1) ==<span style="color: #000000;"> (key2))
<span style="color: #0000ff;">#define dictHashKey(d,key) (d)->type->hashFunction(key)
<span style="color: #0000ff;">#define dictGetKey(he) ((he)->key)
<span style="color: #0000ff;">#define dictGetVal(he) ((he)->v.val)
<span style="color: #0000ff;">#define dictGetSignedIntegerVal(he) ((he)->v.s64)
<span style="color: #0000ff;">#define dictGetUnsignedIntegerVal(he) ((he)->v.u64)
<span style="color: #0000ff;">#define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size)
<span style="color: #0000ff;">#define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used)
<span style="color: #0000ff;">#define dictIsRehashing(ht) ((ht)->rehashidx != -1)
4. 字典提供的api,有字典的创建,增加、删除、修改记录,还有迭代器(前面已经介绍)和自动扩容(下面介绍)。
5.外部定义变量
<span style="color: #0000ff;">extern
<span style="color: #000000;"> dictType dictTypeHeapStringCopyKey;<span style="color: #0000ff;">extern<span style="color: #000000;"> dictType dictTypeHeapStrings;
<span style="color: #0000ff;">extern dictType dictTypeHeapStringCopyKeyValue;
6. 自动扩容
Redis使用标识dict_can_resize来记录字典是否可以扩容,可以使用dictEnableResize()方法和dictDisableResize()来改变此标识。使用dictResize()来扩容,但需要首先判断是否允许扩容及是否正在扩容。若可以扩容,则调用dictExpand()扩容,然后调用dictRehashMilliseconds()启动扩容,并指定扩容过程中记录的copy速度。请看程序:
6.1 dictResize()
</span><span style="color: #0000ff;">if</span> (!dict_can_resize || dictIsRehashing(d)) <span style="color: #0000ff;">return</span><span style="color: #000000;"> DICT_ERR;
minimal </span>= d->ht[<span style="color: #800080;">0</span><span style="color: #000000;">].used;
</span><span style="color: #0000ff;">if</span> (minimal <<span style="color: #000000;"> DICT_HT_INITIAL_SIZE)
minimal </span>=<span style="color: #000000;"> DICT_HT_INITIAL_SIZE;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> dictExpand(d,minimal);
}
6.2 dictExpand()
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> the size is invalid if it is smaller than the number of
* elements already inside the hash table </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">if</span> (<span style="color: #000000;">dictIsRehashing</span>(d) || d->ht[<span style="color: #800080;">0</span>].used ><span style="color: #000000;"> size)
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> DICT_ERR;
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> Allocate the new hash table and initialize all pointers to NULL </span><span style="color: #008000;">*/</span><span style="color: #000000;">
n.size </span>=<span style="color: #000000;"> realsize;
n.sizemask </span>= realsize-<span style="color: #800080;">1</span><span style="color: #000000;">;
n.table </span>= zcalloc(realsize*<span style="color: #0000ff;">sizeof</span>(dictEntry*<span style="color: #000000;">));
n.used </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> Is this the first initialization? If so it's not really a rehashing
* we just set the first hash table so that it can accept keys. </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">if</span> (d->ht[<span style="color: #800080;">0</span>].table ==<span style="color: #000000;"> NULL) {
d</span>->ht[<span style="color: #800080;">0</span>] =<span style="color: #000000;"> n;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> DICT_OK;
}
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> Prepare a second hash table for incremental rehashing </span><span style="color: #008000;">*/</span><span style="color: #000000;">
d</span>->ht[<span style="color: #800080;">1</span>] =<span style="color: #000000;"> n;
d</span>->rehashidx = <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> DICT_OK;
}
6.3
</span><span style="color: #0000ff;">while</span>(dictRehash(d,<span style="color: #800080;">100</span><span style="color: #000000;">)) {
rehashes </span>+= <span style="color: #800080;">100</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (timeInMilliseconds()-start > ms) <span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> rehashes;
}