Redis中的hashtable底层数据结构

制图工具:GitHub网站      (如果觉得我的图还行,直接用我的也可以)

Redis整体结构(Redis server v=4.0.12 ,Redis服务器版本为4.0.12)

 源码: server.h

typedef struct redisDb {
    dict *dict;                 /* The keyspace for this DB */
    dict *expires;              /* Timeout of keys with a timeout set */
    dict *blocking_keys;        /* Keys with clients waiting for data (BLPOP)*/
    dict *ready_keys;           /* Blocked keys that received a PUSH */
    dict *watched_keys;         /* WATCHED keys for MULTI/EXEC CAS */
    int id;                     /* Database ID */
    long long avg_ttl;          /* Average TTL, just for stats */
} redisDb;

  *dict:字典结构 

 *expire: 过期时间设置

 *blocking_keys:阻塞命令

  *ready_keys:

  *watched_keys

 id: 数据库编号

  avg_ttl:平均ttl

 

  字典结构:dict.h 

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

  *type : 

  * privdata:私有数据

  dictht  ht[2]  : hashtable,  为什么数组长度为2: 另一个使用来进行hashtable扩容的

  rehashidx:重哈希

   iterators: 

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;
typedef struct dictht {
    dictEntry **table;
    unsigned long size;
    unsigned long sizemask;
    unsigned long used;
} dictht;
typedef struct dictEntry {
    void *key;
    union {
        void *val;
        uint64_t u64;
        int64_t s64;
        double d;
    } v;
    struct dictEntry *next;
} dictEntry;

key : redisObject 

typedef struct redisObject {
    unsigned type:4;
    unsigned encoding:4;
    unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
                            * LFU data (least significant 8 bits frequency
                            * and most significant 16 bits access time). */
    int refcount;
    void *ptr;
} robj;

type : 区分类型,string,list,hash,set, sorted  set

encoding:数据编码,包括embstr, int, raw

Lru: 内存淘汰

refcount: 内存管理的

*ptr :真实数据存储

总体示意图:

 

 

Ziplist(压缩列表)

 

示意图:

  • 属性介绍:

  zlbytes:  32bit,表示ziplist占用字节的总数
  zltail:   32bit,表示ziplist表中最后一项(entry)在ziplist中的偏移字节数,通过zltail可以很方便地找到最后一项,从而可以在   ziplist尾端快速地执行push或pop操作
  zlen:   16bit,表示ziplist中数据项(entry)的个数
 prerawlen:前一个entry的数据长度
 len:   entry中的数据长度
 data:  真实数据的存储

特点:

  • ziplist是为节省内存空间而生的【紧凑型】。
  • ziplist是一个为Redis专门提供的底层数据结构之一,本身可以有序也可以无序。当作为listhash的底层实现时,节点之间没有顺序;当作为zset的底层实现时,节点之间会按照大小顺序排列。

 quicklist(快速列表)

源码:  quicklist.h

typedef struct quicklist {
    quicklistNode *head;
    quicklistNode *tail;
    unsigned long count;        /* total count of all entries in all ziplists */
    unsigned long len;          /* number of quicklistNodes */
    int fill : 16;              /* fill factor for individual nodes */
    unsigned int compress : 16; /* depth of end nodes not to compress;0=off */
} quicklist;
typedef struct quicklistNode {
    struct quicklistNode *prev;
    struct quicklistNode *next;
    unsigned char *zl;
    unsigned int sz;             /* ziplist size in bytes */
    unsigned int count : 16;     /* count of items in ziplist */
    unsigned int encoding : 2;   /* RAW==1 or LZF==2 */
    unsigned int container : 2;  /* NONE==1 or ZIPLIST==2 */
    unsigned int recompress : 1; /* was this node previous compressed? */
    unsigned int attempted_compress : 1; /* node can't compress; too small */
    unsigned int extra : 10; /* more bits to steal for future usage */
} quicklistNode;

示意图:

 属性介绍:

   head: 头结点

  tail:尾结点

 count:ziplist中的entry节点的个数

 len: quicknode的个数

 prev:前一个节点的指针

 next:下一个节点的指针

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值