读REDIS数据结构

一.DICT

主要有两个问题:

1.散列冲突,解决办法是拉链法

typedef struct dictEntry {
    void *key;
    union {
        void *val;
        uint64_t u64;
        int64_t s64;
    } v;
    struct dictEntry *next;
} dictEntry;

next字段向后拉链

2.扩容时候的rehash,做类似于copy on write

typedef struct dict {
    dictType *type;
    void *privdata;
    dictht ht[2];
    int rehashidx; /* rehashing not in progress if rehashidx == -1 */
    int iterators; /* number of iterators currently running */
} dict;

ht[0] 是存储了没扩容时候的数据,ht[1]存储扩容以后的数据。如果在需要扩容的时候,任何对哈希表的操作都会从ht[0]中找到一个key rehash以后放到ht[1],逐渐把ht[0]中的数据放到ht[1],当ht[0]中全部rehash完以后,释放空间,ht[0]指向ht[1]。

原文地址:https://www.cnblogs.com/23lalala/p/3825775.html