redis_字典_哈希hash

字典、哈希表基本数据结构

redis字典使用哈希表作为底层实现,基本结构就是数组+散列

typedef struct dictht {
    // 哈希表数组
    dictEntry **table;
    // 哈希表数组的大小
    unsigned long size;
    // 掩码,用于计算新加入的元素的index(sizemask值等于size-1)
    unsigned ling sizemask;
    // 哈希表中已有节点的数量
    unsigned long used;
} dictht;

 其中,哈希表数组中的每一个节点,与1.8之前的jdk中map很相似,也是链表结构

typedef struct dictEntry {
    //
    void *key
    //
    union {
        void *val;
        uint64_t u64;
        int64_t s64;
    } v;
    // 指向下个哈希表节点,形成链表
    struct dictEntry *next;
} dictEntry;

最终的redis字典内部,由2个哈希表构成。平时只使用其中的一个,当需要扩展或收缩哈希表时,启用第二个,后续详细介绍

redis字典结构如下:

typedef struct dict {
    dictType *type
    void *privdata;
    // 哈希表(实际存储数据),平时使用ht[0],当需要扩展或收缩哈希表时,将ht[0]的数据rehash到ht[1],然后释放ht[0],将ht[1]设置为ht[0],并在ht[1]新建一个空白哈希表,done
    dictht ht[2];
    // 指向下个哈希表节点,形成链表
    struct dictEntry *next;
} dictEntry;

猜你喜欢

转载自www.cnblogs.com/loveCheery/p/9133674.html