1.字典作用
- 实现数据库键空间(key space);
- 用作 Hash 类型键的底层实现之一;
2.字典实现的数据结构
typedef struct dict {
// 特定于类型的处理函数
dictType *type;
// 类型处理函数的私有数据
void *privdata;
// 哈希表(2 个)
dictht ht[2];
// 记录 rehash 进度的标志,值为 -1 表示 rehash 未进行
int rehashidx;
// 当前正在运作的安全迭代器数量
int iterators;
} dict;
/*
* 哈希表
*/
typedef struct dictht {
// 哈希表节点指针数组(俗称桶,bucket)
dictEntry **table;
// 指针数组的大小
unsigned long size;
// 指针数组的长度掩码,用于计算索引值
unsigned long sizemask;
// 哈希表现有的节点数量
unsigned long used;
} dictht;
/*
* 哈希表节点
*/
typedef struct dictEntry {
// 键
void *key;
// 值
union {
void *val;
uint64_t u64;
int64_t s64;
} v;
// 链往后继节点
struct dictEntry *next;
} dictEntry;
![digraph hash_table_example {
// setting
rankdir = LR;
node[shape=record, style = filled];
edge [style = bold];
// nodes
dict [label="dict | type | privdata |<ht> ht[2] | rehashidx: -1 | iterators: 0", fillcolor = "#A8E270"];
ht0 [label="<dictht>dictht |<table> table | size: 4 | sizemask: 3 | used: 3", fillcolor = "#95BBE3"];
ht1 [label="<dictht>dictht |<table> table | size: 0 | sizemask: 0 | used: 0", fillcolor = "#95BBE3"];
bucket [label="<head>dictEntry**
(bucket) |<table0> 0 |<table1> 1 |<table2> 2 |<table3> 3 ", fillcolor = "#F2F2F2"];
pair_1 [label="<head>dictEntry |{key1 | value1 |<next>next}", fillcolor = "#FADCAD"];
pair_2 [label="<head>dictEntry |{key2 | value2 |<next>next}", fillcolor = "#FADCAD"];
pair_3 [label="<head>dictEntry |{key3 | value3 |<next>next}", fillcolor = "#FADCAD"];
null0 [label="NULL", shape=plaintext];
null1 [label="NULL", shape=plaintext];
null2 [label="NULL", shape=plaintext];
null3 [label="NULL", shape=plaintext];
tnull1 [label="NULL", shape=plaintext];
// lines
dict:ht -> ht0:dictht [label="ht[0]"];
dict:ht -> ht1:dictht [label="ht[1]"];
ht0:table -> bucket:head;
ht1:table -> tnull1;
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;
}](http://redisbook.readthedocs.org/en/latest/_images/graphviz-6989792733a041b23cdc0b8f126434590c50a4e4.svg)
3.Redis 目前使用两种不同的哈希算法:
- MurmurHash2 32 bit 算法:这种算法的分布率和速度都非常好, 具体信息请参考 MurmurHash 的主页:http://code.google.com/p/smhasher/ 。
- 基于 djb 算法实现的一个大小写无关散列算法:具体信息请参考 http://www.cse.yorku.ca/~oz/hash.html 。
4.发生rehash的条件
ratio = used / size
- 自然 rehash : ratio >= 1 ,且变量 dict_can_resize 为真。
- 强制 rehash : ratio 大于变量 dict_force_resize_ratio (目前版本中, dict_force_resize_ratio 的值为 5 )
5.rehash 执行过程
- 创建一个比 ht[0]->table 更大的 ht[1]->table ;
- 将 ht[0]->table 中的所有键值对迁移到 ht[1]->table ;
- 将原有 ht[0] 的数据清空,并将 ht[1] 替换为新的 ht[0] ;