美文网首页
Redis 字典(dict.h/dict.c)(4)

Redis 字典(dict.h/dict.c)(4)

作者: lmem | 来源:发表于2017-05-21 10:43 被阅读99次

Redis字典的底层实现为哈希表, 每个字典使用两个哈希表, 一般情况下只使用 0 号哈希表, 只有在 rehash 进行时, 才会同时使用 0 号和 1 号哈希表。 哈希表使用链地址法来解决键冲突的问题。 自动 Rehash 扩展或收缩哈希表。 对哈希表的 rehash 是分多次、渐进式地进行的

Paste_Image.png

1.rehash介绍,(扩容)

字典的 rehash 操作实际上就是执行以下任务:

  • 创建一个比 ht[0]->table 更大的 ht[1]->table , size为大于used*2的2的指数, 开始值为4;
  • 将 ht[0]->table 中的所有键值对迁移到 ht[1]->table ;
  • 将原有 ht[0] 的数据清空,并将 ht[1] 替换为新的 ht[0] ;

进行rehash的条件:

  • 自然 rehash : ratio >= 1 ,且变量 dict_can_resize 为真。
  • 强制 rehash : ratio 大于变量 dict_force_resize_ratio (目前版本中, dict_force_resize_ratio 的值为 5 )。

阶进rehash:

  • 主动方式:databaseCron中调用dictRehashMilliseconds执行一毫秒。
  • 被动方式:调用dictAdd,dicFind,dictDelete,dictGetRandomKey时,调用_dictRehashStep,迁移一个非空桶。

2.数据结构

//dict 元素
typedef struct dictEntry {
    void *key;
    //共同体,只会存在一个值,按照最长的变量开辟一个空间
    //场景:各数据类型各变量占用空间差不多并且对各变量同时使用要求不高的场合
    union {
        void *val;
        uint64_t u64;//无符号
        int64_t s64;
        double d;
    } v;//值
    struct dictEntry *next;
} dictEntry;

//类型描述
typedef struct dictType {
    unsigned int (*hashFunction)(const void *key); //hash函数
    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. */
//hash 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];
    long rehashidx; /* rehashing not in progress if rehashidx == -1 */
    int iterators; /* number of iterators currently running */
} dict;

相关文章

  • Redis数据结构--字典

    字典是Redis的重要数据结构,Redis的数据库就是使用字典作为底层实现的。代码位于dict.h和dict.c中...

  • redis 内部数据结构(1.3)-字典

    dict.h dict.c罪过罪过,封闭开发,晚上回家也懒惰了没有学习,今天继续。 Redis 的字典使用哈希表作...

  • dict.c

    Redis的字典结构,说起来和Java的HashMap有点相似。主要是由dict.c来实现,在dict.h中进行了...

  • Redis 字典(dict.h/dict.c)(4)

    Redis字典的底层实现为哈希表, 每个字典使用两个哈希表, 一般情况下只使用 0 号哈希表, 只有在 rehas...

  • 工作中遇到的hashtable

    一.redis 中使用的字典 redis的字典是由hash表实现的,代码主要是在dict.cpp/dict.h中 ...

  • redis字典源码简单分析

    字典是redis底层数据结构之一,在dict.c中实现,下面分析下他的实现。 一.简介 redis的dict仍然是...

  • redis源码4--字典Dict:关键的结构定义

    源码文件在 dict.h 和 dict.c 中 哈希表节点定义 哈希表节点使用 dictEntry 结构表示,一个...

  • Redis字典底层实现

    字典的实现 hash表结构 table 属性是一个数组, 数组中的每个元素都是一个指向 dict.h/dictEn...

  • redis入门

    Redis入门 redis是什么? redis (Remote Dictionary Server)远程服务字典...

  • 《Redis设计与实现》

    《Redis设计与实现》 作者 黄健宏 1.1 Redis版本说明 第 2 章【简单动态字符串】、第4-5章【字典...

网友评论

      本文标题:Redis 字典(dict.h/dict.c)(4)

      本文链接:https://www.haomeiwen.com/subject/dbfpxxtx.html