1 #ifndef __DICT_H
2 #define __DICT_H
3
4 #define DICT_OK 0
5 #define DICT_ERR 1
6
7 /* Unused arguments generate annoying warnings... */
8 #define DICT_NOTUSED(V) ((void) V) //redis实现里很多这种NOTUSED宏,算是作者偷懒吧
9
10 typedef struct dictEntry {
11 void *key;
12 void *val;
13 struct dictEntry *next;
14 } dictEntry; //redis的一个dict表项,存的是key , val, 以及因为redis的hash是用的拉链法,所以有一个指向下一个dictEntry的指针;
//一个dictEntry的内存占用为12个字节,记住这个数字。
15
16 typedef struct dictType {
17 unsigned int (*hashFunction)(const void *key); //hash函数
18 void *(*keyDup)(void *privdata, const void *key); //复制key
19 void *(*valDup)(void *privdata, const void *obj); //复制value
20 int (*keyCompare)(void *privdata, const void *key1, const void *key2); //key的compare函数
21 void (*keyDestructor)(void *privdata, void *key);
22 void (*valDestructor)(void *privdata, void *obj);
23 } dictType; //dict的类型
24
25 /* This is our hash table structure. Every dictionary has two of this as we
26 * implement incremental rehashing, for the old to the new table. */
27 typedef struct dictht {
28 dictEntry **table;
29 unsigned long size; //size
30 unsigned long sizemask;
31 unsigned long used; //used
32 } dictht; //这个是hash table结构,每个dictionary有两个hash 表,实现的是递增的rehash
33
34 typedef struct dict {
35 dictType *type;
36 void *privdata;
37 dictht ht[2];
38 int rehashidx; /* rehashing not in progress if rehashidx == -1 */ //记录是否在rehash的一个flag,rehash进行过程中,有些操作不能进行
39 int iterators; /* number of iterators currently running */ //记录在dict上正在执行的iter数
40 } dict; //dict的数据结构
41
42 /* If safe is set to 1 this is a safe iteartor, that means, you can call
43 * dictAdd, dictFind, and other functions against the dictionary even while
44 * iterating. Otherwise it is a non safe iterator, and only dictNext()
45 * should be called while iterating. */
//迭代器按照是否可以执行改变hash表的函数区别为safe iterator和non safe iterator, non safe iterator只能执行dictNext函数