字典(dictionary),又名映射(map)或关联数组(associative array), [http://en.wikipedia.org/wiki/Associative_array]是一种抽象数据结构,由一集键值对(key-value pairs)组成,各个键值对的键各不相同,程序可以添加新的键值对到字典中,或者基于键进行查找、更新或删除等操作。
本章先对字典在 Redis 中的应用进行介绍,接着讲解字典的具体实现方式,以及这个字典实现要解决的问题,最后,以对字典迭代器的介绍作为本章的结束。
字典在 Redis 中的应用广泛,使用频率可以说和 SDS 以及双端链表不相上下,基本上各个功能模块都有用到字典的地方。
其中,字典的主要用途有以下两个:
以下两个小节分别介绍这两种用途。
Redis 是一个键值对数据库,数据库中的键值对由字典保存:每个数据库都有一个对应的字典,这个字典被称之为键空间(key space)。
当用户添加一个键值对到数据库时(不论键值对是什么类型),程序就将该键值对添加到键空间;当用户从数据库中删除键值对时,程序就会将这个键值对从键空间中删除;等等。
举个例子,执行 FLUSHDB [http://redis.readthedocs.org/en/latest/server/flushdb.html#flushdb] 可以清空键空间里的所有键值对数据:
redis> FLUSHDB OK
执行 DBSIZE [http://redis.readthedocs.org/en/latest/server/dbsize.html#dbsize] 则返回键空间里现有的键值对:
redis> DBSIZE (integer) 0
还可以用 SET [http://redis.readthedocs.org/en/latest/string/set.html#set] 设置一个字符串键到键空间,并用 GET [http://redis.readthedocs.org/en/latest/string/get.html#get] 从键空间中取出该字符串键的值:
redis> SET number 10086 OK redis> GET number "10086" redis> DBSIZE (integer) 1
后面的《数据库》一章会对键空间以及数据库的实现作详细的介绍,届时将看到,大部分针对数据库的命令,比如 DBSIZE [http://redis.readthedocs.org/en/latest/server/dbsize.html#dbsize] 、 FLUSHDB [http://redis.readthedocs.org/en/latest/server/flushdb.html#flushdb] 、 RANDOMKEY [http://redis.readthedocs.org/en/latest/key/randomkey.html#randomkey] ,等等,都是构建于对字典的操作之上的;而那些创建、更新、删除和查找键值对的命令,也无一例外地需要在键空间上进行操作。
Redis 的 Hash 类型键使用以下两种数据结构作为底层实现:
因为压缩列表比字典更节省内存,所以程序在创建新 Hash 键时,默认使用压缩列表作为底层实现,当有需要时,程序才会将底层实现从压缩列表转换到字典。
当用户操作一个 Hash 键时,键值在底层就可能是一个哈希表:
redis> HSET book name "The design and implementation of Redis" (integer) 1 redis> HSET book type "source code analysis" (integer) 1 redis> HSET book release-date "2013.3.8" (integer) 1 redis> HGETALL book 1) "name" 2) "The design and implementation of Redis" 3) "type" 4) "source code analysis" 5) "release-date" 6) "2013.3.8"
《哈希表》章节给出了关于哈希类型键的更多信息,并介绍了压缩列表和字典之间的转换条件。
介绍完了字典的用途,现在让我们来看看字典数据结构的定义。
实现字典的方法有很多种:
在众多可能的实现中,Redis 选择了高效、实现简单的哈希表,作为字典的底层实现。
dict.h/dict 给出了这个字典的定义:
/*
* 字典
*
* 每个字典使用两个哈希表,用于实现渐进式 rehash
*/
typedef struct dict {
// 特定于类型的处理函数
dictType *type;
// 类型处理函数的私有数据
void *privdata;
// 哈希表(2 个)
dictht ht[2];
// 记录 rehash 进度的标志,值为 -1 表示 rehash 未进行
int rehashidx;
// 当前正在运作的安全迭代器数量
int iterators;
} dict;以下是用于处理 dict 类型的 API ,它们的作用及相应的算法复杂度:
| 操作 | 函数 | 算法复杂度 |
|---|---|---|
| 创建一个新字典 | dictCreate | (O(1)) |
| 添加新键值对到字典 | dictAdd | (O(1)) |
| 添加或更新给定键的值 | dictReplace | (O(1)) |
| 在字典中查找给定键所在的节点 | dictFind | (O(1)) |
| 在字典中查找给定键的值 | dictFetchValue | (O(1)) |
| 从字典中随机返回一个节点 | dictGetRandomKey | (O(1)) |
| 根据给定键,删除字典中的键值对 | dictDelete | (O(1)) |
| 清空并释放字典 | dictRelease | (O(N)) |
| 清空并重置(但不释放)字典 | dictEmpty | (O(N)) |
| 缩小字典 | dictResize | (O(N)) |
| 扩大字典 | dictExpand | (O(N)) |
| 对字典进行给定步数的 rehash | dictRehash | (O(N)) |
| 在给定毫秒内,对字典进行rehash | dictRehashMilliseconds | (O(N)) |
注意 dict 类型使用了两个指针,分别指向两个哈希表。
其中,0 号哈希表(ht[0])是字典主要使用的哈希表,而 1 号哈希表(ht[1])则只有在程序对 0 号哈希表进行 rehash 时才使用。
接下来两个小节将对哈希表的实现,以及哈希表所使用的哈希算法进行介绍。
字典所使用的哈希表实现由 dict.h/dictht 类型定义:
/*
* 哈希表
*/
typedef struct dictht {
// 哈希表节点指针数组(俗称桶,bucket)
dictEntry **table;
// 指针数组的大小
unsigned long size;
// 指针数组的长度掩码,用于计算索引值
unsigned long sizemask;
// 哈希表现有的节点数量
unsigned long used;
} dictht;table 属性是个数组,数组的每个元素都是个指向 dictEntry 结构的指针。
每个 dictEntry 都保存着一个键值对,以及一个指向另一个 dictEntry 结构的指针:
/*
* 哈希表节点
*/
typedef struct dictEntry {
// 键
void *key;
// 值
union {
void *val;
uint64_t u64;
int64_t s64;
} v;
// 链往后继节点
struct dictEntry *next;
} dictEntry;next 属性指向另一个 dictEntry 结构,多个 dictEntry 可以通过 next 指针串连成链表,从这里可以看出,dictht使用链地址法来处理键碰撞 [http://en.wikipedia.org/wiki/Hash_table#Separate_chaining]:当多个不同的键拥有相同的哈希值时,哈希表用一个链表将这些键连接起来。
下图展示了一个由 dictht 和数个 dictEntry 组成的哈希表例子:
dictht |
如果再加上之前列出的 dict 类型,那么整个字典结构可以表示如下:
ht[2] | rehashidx: -1 | iterators: 0", fillcolor = "#A8E270"]; ht0 [label="dictht |
在上图的字典示例中,字典虽然创建了两个哈希表,但正在使用的只有 0 号哈希表,这说明字典未进行 rehash 状态。
Redis 目前使用两种不同的哈希算法:
使用哪种算法取决于具体应用所处理的数据:
dictCreate 函数创建并返回一个新字典:
dict *d = dictCreate(&hash_type, NULL);
d 的值可以用图片表示如下:
ht[2] | rehashidx | iterators", fillcolor = "#A8E270"]; ht0 [label="dictht |
新创建的两个哈希表都没有为 table 属性分配任何空间:
ht[0]->table 的空间分配将在第一次往字典添加键值对时进行;ht[1]->table 的空间分配将在 rehash 开始时进行;根据字典所处的状态,将给定的键值对添加到字典可能会引起一系列复杂的操作:
table 属性为空),则程序需要对 0 号哈希表进行初始化;当程序处理完以上三种情况之后,新的键值对才会被真正地添加到字典上。
整个添加流程可以用下图表示:
key_exists_or_not; return_null_if_key_exists [label="返回 NULL ,\n表示添加失败"]; key_exists_or_not -> return_null_if_key_exists [label="是"]; dict_empty_or_not [label="ht[0]\n 未分配任何空间?", shape=diamond, fillcolor = "#95BBE3"]; key_exists_or_not -> dict_empty_or_not [label="否"]; init_hash_table_one [label="初始化 ht[0]"]; dict_empty_or_not -> init_hash_table_one [label="是"]; init_hash_table_one -> need_rehash_or_not; need_rehash_or_not [label="需要 rehash ?", shape=diamond, fillcolor = "#95BBE3"]; dict_empty_or_not -> need_rehash_or_not [label="否"]; begin_incremental_rehash [label="开始渐进式 rehash "]; need_rehash_or_not -> begin_incremental_rehash [label="需要,\n并且 rehash 未进行"]; begin_incremental_rehash -> rehashing_or_not; rehashing_or_not [label="rehash\n 正在进行中?", shape=diamond, fillcolor = "#95BBE3"]; need_rehash_or_not -> rehashing_or_not [label="不需要,\n或者 rehash 正在进行"]; is_rehashing [label="选择 ht[1] 作为新键值对的添加目标"]; not_rehashing [label="选择 ht[0] 作为新键值对的添加目标"]; rehashing_or_not -> is_rehashing [label="是"]; rehashing_or_not -> not_rehashing [label="否"]; calc_hash_code_and_index_by_key [label="根据给定键,计算出哈希值,以及索引值"]; is_rehashing -> calc_hash_code_and_index_by_key; not_rehashing -> calc_hash_code_and_index_by_key; create_entry_and_assoc_key_and_value [label="创建新 dictEntry ,并保存给定键值对"]; calc_hash_code_and_index_by_key -> create_entry_and_assoc_key_and_value; add_entry_to_hashtable [label="根据索引值,将新节点添加到目标哈希表"]; create_entry_and_assoc_key_and_value -> add_entry_to_hashtable;}" />
在接下来的三节中,我们将分别看到,添加操作如何在以下三种情况中执行:
当第一次往空字典里添加键值对时,程序会根据 dict.h/DICT_HT_INITIAL_SIZE 里指定的大小为d->ht[0]->table 分配空间(在目前的版本中, DICT_HT_INITIAL_SIZE 的值为 4 )。
以下是字典空白时的样子:
ht[2] | rehashidx | iterators", fillcolor = "#A8E270"]; ht0 [label="dictht |
以下是往空白字典添加了第一个键值对之后的样子:
ht[2] | rehashidx | iterators", fillcolor = "#A8E270"]; ht0 [label="dictht |
在哈希表实现中,当两个不同的键拥有相同的哈希值时,称这两个键发生碰撞(collision),而哈希表实现必须想办法对碰撞进行处理。
字典哈希表所使用的碰撞解决方法被称之为链地址法 [http://en.wikipedia.org/wiki/Hash_table#Separate_chaining]:这种方法使用链表将多个哈希值相同的节点串连在一起,从而解决冲突问题。
假设现在有一个带有三个节点的哈希表,如下图:
0 | 1 | 2 | 3 ", fillcolor = "#F2F2F2"]; pair_1 [label="dictEntry |{key1 | value1 |next}", fillcolor = "#FADCAD"]; pair_2 [label="dictEntry |{key2 | value2 |next}", fillcolor = "#FADCAD"]; pair_3 [label="dictEntry |{key3 | value3 |next}", fillcolor = "#FADCAD"]; null0 [label="NULL", shape=plaintext]; null1 [label="NULL", shape=plaintext]; null2 [label="NULL", shape=plaintext]; null3 [label="NULL", shape=plaintext]; // lines bucket:table0 -> pair_1:head; pair_1:next -> null0; bucket:table1 -> null1; bucket:table2 -> pair_2:head; pair_2:next -> null2; bucket:table3 -> pair_3:head; pair_3:next -> null3; // label label = "添加碰撞节点之前";}" />
对于一个新的键值对 key4 和 value4 ,如果 key4 的哈希值和 key1 的哈希值相同,那么它们将在哈希表的 0 号索引上发生碰撞。
通过将 key4-value4 和 key1-value1 两个键值对用链表连接起来,就可以解决碰撞的问题:
0 | 1 | 2 | 3 ", fillcolor = "#F2F2F2"]; pair_1 [label="dictEntry |{key1 | value1 |next}", fillcolor = "#FADCAD"]; pair_2 [label="dictEntry |{key2 | value2 |next}", fillcolor = "#FADCAD"]; pair_3 [label="dictEntry |{key3 | value3 |next}", fillcolor = "#FADCAD"]; pair_4 [label="dictEntry |{key4 | value4 |next}", fillcolor = "#FFC1C1"]; null0 [label="NULL", shape=plaintext]; null1 [label="NULL", shape=plaintext]; null2 [label="NULL", shape=plaintext]; null3 [label="NULL", shape=plaintext]; // lines bucket:table0 -> pair_4:head; pair_4:next -> pair_1:head; pair_1:next -> null0; bucket:table1 -> null1; bucket:table2 -> pair_2:head; pair_2:next -> null2; bucket:table3 -> pair_3:head; pair_3:next -> null3; // label label = "添加碰撞节点之后";}" />
对于使用链地址法来解决碰撞问题的哈希表 dictht 来说,哈希表的性能取决于大小(size属性)与保存节点数量(used属性)之间的比率:
举个例子,下面这个哈希表,平均每次失败查找只需要访问 1 个节点(非空节点访问 2 次,空节点访问 1 次):
0 |<1> 1 |<2> 2 |<3> 3 |<4> 4 |<5> 5 |<6> 6 |<7> 7", fillcolor = "#F2F2F2"]; // nodes node [height=.1]; node0 [label="Entry", fillcolor = "#FADCAD"]; node0_null [label="NULL", shape=plaintext]; node1 [label="Entry", fillcolor = "#FADCAD"]; node1_null [label="NULL", shape=plaintext]; node2 [label="NULL", shape=plaintext]; node3 [label="Entry", fillcolor = "#FADCAD"]; node3_null [label="NULL", shape=plaintext]; node4 [label="NULL", shape=plaintext]; node5 [label="Entry", fillcolor = "#FADCAD"]; node5_null [label="NULL", shape=plaintext]; node6 [label="NULL", shape=plaintext]; node7 [label="NULL", shape=plaintext]; bucket:0 -> node0; node0 -> node0_null; bucket:1 -> node1; node1 -> node1_null; bucket:2 -> node2; bucket:3 -> node3; node3 -> node3_null; bucket:4 -> node4; bucket:5 -> node5; node5 -> node5_null; bucket:6 -> node6; bucket:7 -> node7;}" />
而下面这个哈希表,平均每次失败查找需要访问 5 个节点:
0 |<1> 1 |<2> 2 |<3> 3 |<4> 4 |<5> 5 |<6> 6 |<7> 7", fillcolor = "#F2F2F2"]; // nodes node [height=.1]; // node 0 node0 [label="Entry", fillcolor = "#FADCAD"]; node01 [label="Entry", fillcolor = "#FADCAD"]; node02 [label="Entry", fillcolor = "#FADCAD"]; node03 [label="Entry", fillcolor = "#FADCAD"]; node04 [label="Entry", fillcolor = "#FADCAD"]; node05 [label="NULL", shape=plaintext]; bucket:0 -> node0; node0 -> node01; node01 -> node02; node02 -> node03; node03 -> node04; node04 -> node05; // node 1 node1 [label="Entry", fillcolor = "#FADCAD"]; node11 [label="Entry", fillcolor = "#FADCAD"]; node12 [label="Entry", fillcolor = "#FADCAD"]; node13 [label="NULL", shape=plaintext]; bucket:1 -> node1; node1 -> node11; node11 -> node12; node12 -> node13; // node 2 node2 [label="Entry", fillcolor = "#FADCAD"]; node21 [label="Entry", fillcolor = "#FADCAD"]; node22 [label="Entry", fillcolor = "#FADCAD"]; node23 [label="Entry", fillcolor = "#FADCAD"]; node24 [label="Entry", fillcolor = "#FADCAD"]; node25 [label="NULL", shape=plaintext]; bucket:2 -> node2; node2 -> node21; node21 -> node22; node22 -> node23; node23 -> node24; node24 -> node25; // node 3 node3 [label="Entry", fillcolor = "#FADCAD"]; node31 [label="Entry", fillcolor = "#FADCAD"]; node32 [label="Entry", fillcolor = "#FADCAD"]; node33 [label="Entry", fillcolor = "#FADCAD"]; node34 [label="Entry", fillcolor = "#FADCAD"]; node35 [label="NULL", shape=plaintext]; bucket:3 -> node3; node3 -> node31; node31 -> node32; node32 -> node33; node33 -> node34; node34 -> node35; // node 4 node4 [label="Entry", fillcolor = "#FADCAD"]; node41 [label="Entry", fillcolor = "#FADCAD"]; node42 [label="NULL", shape=plaintext]; bucket:4 -> node4; node4 -> node41; node41 -> node42; // node 5 node5 [label="Entry", fillcolor = "#FADCAD"]; node51 [label="Entry", fillcolor = "#FADCAD"]; node52 [label="Entry", fillcolor = "#FADCAD"]; node53 [label="Entry", fillcolor = "#FADCAD"]; node54 [label="Entry", fillcolor = "#FADCAD"]; node55 [label="NULL", shape=plaintext]; bucket:5 -> node5; node5 -> node51; node51 -> node52; node52 -> node53; node53 -> node54; node54 -> node55; // node 6 node6 [label="Entry", fillcolor = "#FADCAD"]; node61 [label="Entry", fillcolor = "#FADCAD"]; node62 [label="Entry", fillcolor = "#FADCAD"]; node63 [label="Entry", fillcolor = "#FADCAD"]; node64 [label="NULL", shape=plaintext]; bucket:6 -> node6; node6 -> node61; node61 -> node62; node62 -> node63; node63 -> node64; // node 7 node7 [label="Entry", fillcolor = "#FADCAD"]; node71 [label="Entry", fillcolor = "#FADCAD"]; node72 [label="Entry", fillcolor = "#FADCAD"]; node73 [label="Entry", fillcolor = "#FADCAD"]; node74 [label="Entry", fillcolor = "#FADCAD"]; node75 [label="NULL", shape=plaintext]; bucket:7 -> node7; node7 -> node71; node71 -> node72; node72 -> node73; node73 -> node74; node74 -> node75;}" />
为了在字典的键值对不断增多的情况下保持良好的性能,字典需要对所使用的哈希表(ht[0])进行 rehash 操作:在不修改任何键值对的情况下,对哈希表进行扩容,尽量将比率维持在 1:1 左右。
dictAdd 在每次向字典添加新键值对之前, 都会对哈希表 ht[0] 进行检查,对于 ht[0] 的 size 和 used 属性,如果它们之间的比率 ratio = used / size 满足以下任何一个条件的话,rehash 过程就会被激活:
ratio >= 1 ,且变量 dict_can_resize 为真。ratio 大于变量 dict_force_resize_ratio (目前版本中, dict_force_resize_ratio 的值为 5 )。Note
什么时候 dict_can_resize 会为假?
在前面介绍字典的应用时也说到过,数据库就是字典,数据库里的哈希类型键也是字典,当 Redis 使用子进程对数据库执行后台持久化任务时(比如执行 BGSAVE 或 BGREWRITEAOF 时),为了最大化地利用系统的 copy on write [http://en.wikipedia.org/wiki/Copy-on-write] 机制,程序会暂时将 dict_can_resize 设为假,避免执行自然 rehash ,从而减少程序对内存的触碰(touch)。
当持久化任务完成之后,dict_can_resize 会重新被设为真。
另一方面,当字典满足了强制 rehash 的条件时,即使 dict_can_resize 不为真(有 BGSAVE 或 BGREWRITEAOF 正在执行),这个字典一样会被 rehash 。
字典的 rehash 操作实际上就是执行以下任务:
ht[0]->table 更大的 ht[1]->table ;ht[0]->table 中的所有键值对迁移到 ht[1]->table ;ht[0] 的数据清空,并将 ht[1] 替换为新的 ht[0] ;经过以上步骤之后,程序就在不改变原有键值对数据的基础上,增大了哈希表的大小。
作为例子,以下四个小节展示了一次对哈希表进行 rehash 的完整过程。
这个阶段有两个事情要做:
rehashidx 为 0 ,标识着 rehash 的开始;ht[1]->table 分配空间,大小至少为 ht[0]->used 的两倍;这时的字典是这个样子:
ht[2] | rehashidx: 0 | iterators", fillcolor = "#A8E270"]; ht0 [label="dictht |
在这个阶段, ht[0]->table 的节点会被逐渐迁移到 ht[1]->table ,因为 rehash 是分多次进行的(细节在下一节解释),字典的 rehashidx 变量会记录 rehash 进行到 ht[0] 的哪个索引位置上。
以下是 rehashidx 值为 2 时,字典的样子:
ht[2] | rehashidx: 2 | iterators", fillcolor = "#A8E270"]; ht0 [label="dictht |
注意除了节点的移动外,字典的 rehashidx 、 ht[0]->used 和 ht[1]->used 三个属性也产生了变化。
到了这个阶段,所有的节点都已经从 ht[0] 迁移到 ht[1] 了:
ht[2] | rehashidx: 3 | iterators", fillcolor = "#A8E270"]; ht0 [label="dictht |
在 rehash 的最后阶段,程序会执行以下工作:
ht[0] 的空间;ht[1] 来代替 ht[0] ,使原来的 ht[1] 成为新的 ht[0] ;ht[1] ;rehashidx 属性设置为 -1 ,标识 rehash 已停止;以下是字典 rehash 完毕之后的样子:
ht[2] | rehashidx: -1 | iterators", fillcolor = "#A8E270"]; ht0 [label="dictht |
对比字典 rehash 前后,新的 ht[0] 空间更大,并且字典原有的键值对也没有被修改或者删除。
在上一节,我们了解了字典的 rehash 过程,需要特别指出的是, rehash 程序并不是在激活之后,就马上执行直到完成的,而是分多次、渐进式地完成的。
假设这样一个场景:在一个有很多键值对的字典里,某个用户在添加新键值对时触发了 rehash 过程,如果这个 rehash 过程必须将所有键值对迁移完毕之后才将结果返回给用户,这样的处理方式将是非常不友好的。
另一方面,要求服务器必须阻塞直到 rehash 完成,这对于 Redis 服务器本身也是不能接受的。
为了解决这个问题,Redis 使用了渐进式(incremental)的 rehash 方式:通过将 rehash 分散到多个步骤中进行,从而避免了集中式的计算。
渐进式 rehash 主要由 _dictRehashStep 和 dictRehashMilliseconds 两个函数进行:
_dictRehashStep 用于对数据库字典、以及哈希键的字典进行被动 rehash ;dictRehashMilliseconds 则由 Redis 服务器常规任务程序(server cron job)执行,用于对数据库字典进行主动 rehash ;每次执行 _dictRehashStep ,ht[0]->table 哈希表第一个不为空的索引上的所有节点就会全部迁移到 ht[1]->table 。
在 rehash 开始进行之后(d->rehashidx 不为 -1),每次执行一次添加、查找、删除操作,_dictRehashStep 都会被执行一次:
rehashing_or_not; dictFind -> rehashing_or_not; dictDelete -> rehashing_or_not; dictGetRandomKey -> rehashing_or_not; rehashing_or_not -> _dictRehashStep [label="是"]; _dictRehashStep -> one_index;}" />
因为字典会保持哈希表大小和节点数的比率在一个很小的范围内,所以每个索引上的节点数量不会很多(从目前版本的 rehash 条件来看,平均只有一个,最多通常也不会超过五个),所以在执行操作的同时,对单个索引上的节点进行迁移,几乎不会对响应时间造成影响。
dictRehashMilliseconds 可以在指定的毫秒数内,对字典进行 rehash 。
当 Redis 的服务器常规任务执行时,dictRehashMilliseconds 会被执行,在规定的时间内,尽可能地对数据库字典中那些需要 rehash 的字典进行 rehash ,从而加速数据库字典的 rehash 进程(progress)。
在哈希表进行 rehash 时,字典还会采取一些特别的措施,确保 rehash 顺利、正确地进行:
ht[0] 上进行,还需要在 ht[1] 上进行。ht[1] 而不是 ht[0] ,这样保证 ht[0] 的节点数量在整个 rehash 过程中都只减不增。上面关于 rehash 的章节描述了通过 rehash 对字典进行扩展(expand)的情况,如果哈希表的可用节点数比已用节点数大很多的话,那么也可以通过对哈希表进行 rehash 来收缩(shrink)字典。
收缩 rehash 和上面展示的扩展 rehash 的操作几乎一样,执行以下步骤:
ht[0]->table 小的 ht[1]->table ;ht[0]->table 中的所有键值对迁移到 ht[1]->table ;ht[0] 的数据清空,并将 ht[1] 替换为新的 ht[0] ;扩展 rehash 和收缩 rehash 执行完全相同的过程,一个 rehash 是扩展还是收缩字典,关键在于新分配的 ht[1]->table 的大小:
ht[1]->table 比 ht[0]->table 要大;ht[1]->table 比 ht[0]->table 要小;字典的收缩规则由 redis.c/htNeedsResize 函数定义:
/*
* 检查字典的使用率是否低于系统允许的最小比率
*
* 是的话返回 1 ,否则返回 0 。
*/
int htNeedsResize(dict *dict) {
long long size, used;
// 哈希表大小
size = dictSlots(dict);
// 哈希表已用节点数量
used = dictSize(dict);
// 当哈希表的大小大于 DICT_HT_INITIAL_SIZE
// 并且字典的填充率低于 REDIS_HT_MINFILL 时
// 返回 1
return (size && used && size > DICT_HT_INITIAL_SIZE &&
(used*100/size < REDIS_HT_MINFILL));
}在默认情况下,REDIS_HT_MINFILL 的值为 10 ,也即是说,当字典的填充率低于 10% 时,程序就可以对这个字典进行收缩操作了。
字典收缩和字典扩展的一个区别是:
因此,使用字典的程序可以决定何时对字典进行收缩:
htNeedsResize 函数,如果字典达到了收缩的标准,程序将立即对字典进行收缩;redis.c/tryResizeHashTables 函数决定,具体信息请参考《数据库》一章的《数据库空间的收缩和扩展》小节;除了添加操作和伸展/收缩操作之外,字典还定义了一些其他操作,比如常见的查找、删除和更新。
因为链地址法哈希表实现的相关信息可以从任何一本数据结构或算法书上找到,这里不再对字典的其他操作进行介绍,不过前面对创建字典、添加键值对、收缩和扩展 rehash 的讨论已经涵盖了字典模块的核心内容。
字典带有自己的迭代器 [http://en.wikipedia.org/wiki/Iterator]实现 ——对字典进行迭代实际上就是对字典所使用的哈希表进行迭代:
整个迭代过程可以用伪代码表示如下:
def iter_dict(dict): # 迭代 0 号哈希表 iter_table(ht[0]->table) # 如果正在执行 rehash ,那么也迭代 1 号哈希表 if dict.is_rehashing(): iter_table(ht[1]->table) def iter_table(table): # 遍历哈希表上的所有索引 for index in table: # 跳过空索引 if table[index].empty(): continue # 遍历索引上的所有节点 for node in table[index]: # 处理节点 do_something_with(node)
字典的迭代器有两种:
以下是迭代器的数据结构定义:
/*
* 字典迭代器
*/
typedef struct dictIterator {
dict *d; // 正在迭代的字典
int table, // 正在迭代的哈希表的号码(0 或者 1)
index, // 正在迭代的哈希表数组的索引
safe; // 是否安全?
dictEntry *entry, // 当前哈希节点
*nextEntry; // 当前哈希节点的后继节点
} dictIterator;以下函数是这个迭代器的 API ,API 的作用及相关算法复杂度:
| 函数 | 作用 | 算法复杂度 |
|---|---|---|
dictGetIterator | 创建一个不安全迭代器。 | (O(1)) |
dictGetSafeIterator | 创建一个安全迭代器。 | (O(1)) |
dictNext | 返回迭代器指向的当前节点,如果迭代完毕,返回 NULL 。 | (O(1)) |
dictReleaseIterator | 释放迭代器。 | (O(1)) |