字典是一种key和value建立一一对应关系的一种结构,redis中的字典用hash实现,代码在dict.h和dict.c中。dict.h中提供的结构和宏定义:
typedef struct dictEntry {
void *key;
union {
void *val;
uint64_t u64;
int64_t s64;
double d;
} v;
struct dictEntry *next;
} dictEntry;
typedef struct dictType {
uint64_t (*hashFunction)(const void *key);
void *(*keyDup)(void *privdata, const void *key);
void *(*valDup)(void *privdata, const void *obj);
int (*keyCompare)(void *privdata, const void *key1, const void *key2);
void (*keyDestructor)(void *privdata, void *key);
void (*valDestructor)(void *privdata, void *obj);
} dictType;
/* 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;
unsigned long size;
unsigned long sizemask;
unsigned long used; //这个是所有元素数,一个桶可能拉出来很多元素
} dictht;
typedef struct dict {
dictType *type;
void *privdata;
dictht ht[2];
/* rehashidx是rehashing过程中当前访问到的ht[0]的第index个桶 */
long rehashidx; /* rehashing not in progress if rehashidx == -1 */
unsigned long iterators; /* number of iterators currently running */
} dict;
/* If safe is set to 1 this is a safe iterator, that means, you can call
* dictAdd, dictFind, and other functions against the dictionary even while
* iterating. Otherwise it is a non safe iterator, and only dictNext()
* should be called while iterating. */
typedef struct dictIterator {
dict *d;
long index;
int table, safe;
dictEntry *entry, *nextEntry;
/* unsafe iterator fingerprint for misuse detection. */
long long fingerprint;
} dictIterator;
typedef void (dictScanFunction)(void *privdata, const dictEntry *de);
typedef void (dictScanBucketFunction)(void *privdata, dictEntry **bucketref);
/* This is the initial size of every hash table */
#define DICT_HT_INITIAL_SIZE 4
/* ------------------------------- Macros ------------------------------------*/
#define dictFreeVal(d, entry) \
if ((d)->type->valDestructor) \
(d)->type->valDestructor((d)->privdata, (entry)->v.val)
#define dictSetVal(d, entry, _val_) do { \
if ((d)->type->valDup) \
(entry)->v.val = (d)->type->valDup((d)->privdata, _val_); \
else \
(entry)->v.val = (_val_); \
} while(0)
#define dictSetSignedIntegerVal(entry, _val_) \
do { (entry)->v.s64 = _val_; } while(0)
#define dictSetUnsignedIntegerVal(entry, _val_) \
do { (entry)->v.u64 = _val_; } while(0)
#define dictSetDoubleVal(entry, _val_) \
do { (entry)->v.d = _val_; } while(0)
#define dictFreeKey(d, entry) \
if ((d)->type->keyDestructor) \
(d)->type->keyDestructor((d)->privdata, (entry)->key)
#define dictSetKey(d, entry, _key_) do { \
if ((d)->type->keyDup) \
(entry)->key = (d)->type->keyDup((d)->privdata, _key_); \
else \
(entry)->key = (_key_); \
} while(0)
#define dictCompareKeys(d, key1, key2) \
(((d)->type->keyCompare) ? \
(d)->type->keyCompare((d)->privdata, key1, key2) : \
(key1) == (key2))
#define dictHashKey(d, key) (d)->type->hashFunction(key)
#define dictGetKey(he) ((he)->key)
#define dictGetVal(he) ((he)->v.val)
#define dictGetSignedIntegerVal(he) ((he)->v.s64)
#define dictGetUnsignedIntegerVal(he) ((he)->v.u64)
#define dictGetDoubleVal(he) ((he)->v.d)
#define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size)
#define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used)
#define dictIsRehashing(d) ((d)->rehashidx != -1)
dictEntry是字典节点,内部包含一个key,key对应的value,和指向下一个字典节点的指针,这个指针用于分离链接法解决冲突,在散列到同一个hash桶的时候使用next找到下一个节点
dictType提供了一组用于管理hash结构的方法,如复制、比较等
dictht是hash表的结构,二级指针table表示这是一个使用分离链接法的hash表,每个dict头中有两个这样的hash表,用于装填因子达到阈值时倍增hash空间,使用两个hash的原因是,对分布式系统,比如客户服务器体系,某个客户访问可能导致装填因子达到阈值,倍增hash表,这是一个很耗时和性能的工作,因此对于这个倒霉的客户很不友好,所以使用两个hash表用于解决这个问题,用空间换性能,但是只要调整hash大小就会有性能问题,所以redis提供开关控制是否允许调整hash表大小。其他字段,size是大小,是刚好比希望的size大的2的幂,sizemask就是size-1,由于size是2的幂,所以掩码是全f,used是字典节点的个数
dict是字典控制结构的头,内部包含方法管理容器,两个hash表,正在使用的迭代器数量
dictIterator是迭代器,内部有一个safe标记用来标识是否能在迭代期间进行find,add等操作
dict底层使用siphash,是一种非加密hash算法,dict.c中提供的api:
dictSetHashFunctionSeed:将提供的hash种子保存到全局变量
dictGetHashFunctionSeed:从全局变量中获取种子
dictGenHashFunction:使用key计算hash index,hash算法用siphash
dictGenCaseHashFunction:字符串类型的case hash计算index
_dictRest:将一个hash表结构清零
dictCreate:创建一个字典并初始化
_dictInit:字典初始化
dictRisize:将hash表尽可能减小,会导致装填因子接近1
dictExpend:重建一个hash表,注意此函数只是准备好了ht[1],并没有开始开始搬移
dictRehash:准备好ht[1]以后可以调用此函数,每次从ht[0]搬移n个有内容的桶到ht[1]中,注意最多只允许访问ht[0]中的10 * n个空桶,否则就算没访问到n个空桶,也终止
timeInMilliseconds:计算当前时间对应的毫秒
dictRehashMilliseconds:每次从ht[0]搬移100个节点到ht[1]中,到一定时间之后终止
_dictRehashStep:从ht[0]搬移一个节点到ht[1]中,是一个原子操作,只有没有迭代器的时候才能进行此操作
dictadd:向一个字典中插入一个节点并设置节点的value
dictAddRaw:向一个字典中插入一个节点
dictReplace:向字典中插入或者重写,若key对应的节点不存在时插入,若已存在,更新value
dictAddOrFind:添加或查找一个节点,并将这个节点返回,注意此函数中不涉及value
dictGenericDelete:从字典中删除一个节点,可由调用者决定是否释放节点内存
dictDelete:从字典中删除一个节点并删除节点内存
dictUnlink:从字典中删除一个节点但保留节点内存
dictFreeUnlinkedEntry:调用dictUnlink后,调用此函数将节点内存释放
_dictClear:删除一个字典中的一个ht
dictRelease:删除一个字典,对ht[0]和ht[1]都调用_dictClear,然后释放dict的内存
dictFind:在字典中用key查找一个节点并将其返回
dictFetchValue:在字典中用key查找一个节点并返回其value
dictFingerprint:产生一个dict的64bit指纹,使用64bit整数哈希,用于不安全迭代器
dictGetIterator:创建一个不安全的迭代器
dictGetSafeIterator:创建一个安全的迭代器
dictNext:取一个迭代器的下一个节点
dictReleaseIterator:销毁一个迭代器
dictGetRandomKey:返回字典中一个随机非空节点,是完全随机,有两次随机过程,第一次在ht[0]和ht[1]中找一个随机的非空桶,第二次在找到的非空桶中获取一个随机的元素
dictGetSomeKeys:返回一个字典中的若干随机节点,是不完全的随机,先用随机数找到一个桶,若这个桶是非空的,取这个桶中的所有节点,若是空的,继续下一个桶,连续遇到5个空桶的话重置随机数,找到给定数量的节点或者达到阈值,结束。此函数倾向于返回聚集的元素
rev:将给定值的二进制位反转,支持64位
dictScan:单步遍历一个字典,使用Pieter Noordhuis的反转二进制比特迭代算法,保证遍历完整的同时,将rehashing过程中的重复遍历降低到最少,从小表到大表,不会重复,从大表到小表,可能有重复。这个算法的本质是利用了掩码右边是连续的1左边是连续的0这个结构特点,每次将游标在高位加1,从左向右进位,从而保证右边若干个bit在rehash过程中是一样的
_dictExpandIfNeeded:根据条件决定是否要扩张一个字典中的hash表,条件是,若配置了允许rehash,装填因子到达1的时候就扩展,否则装填因子达到5的时候扩展,无论哪种,扩展后都保证装填因子小于0.5
_dictNextPower:返回比给定值大的第一个2的幂
_dictKeyIndex:根据key和key的hash值查找一个可以插入的位置的hash index
dictEmpty:清空一个字典,包含清空2个hash表与迭代器
dictEnableResize:设置允许扩张表
dictDisableResize:设置不允许扩张表
dictGetHash:使用key计算hash值
dictFindEntryRefByPtrAndHash:使用key和key的hash值在字典中查找一个节点