redis 2.6.0 数据结构

在这个版本中,已经完全去除了虚拟内存的设计,将关注点放到优化数据结构来提高性能。

  • dict
typedef struct dictEntry {

    void *key; 

    union { // 使用union节省内存

        void *val; // 值,当然有各种类型。string,hash,set,zset,list

        uint64_t u64;

        int64_t s64;

    } v;

    struct dictEntry *next;

} dictEntry;

  

typedef struct dictType {
// 提供各种方法
    unsigned int (*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;

  

/* This is our hash table structure. Every dictionary has two of this as we

 * implement incremental rehashing, for the old to the new table. */

typedef struct dictht {

    dictEntry **table; // 拉链式

    unsigned long size;

    unsigned long sizemask;

    unsigned long used;

} dictht;

  

typedef struct dict {

    dictType *type; // 字典的类型,对于 key 而言,提供各种方法。

    void *privdata;

    dictht ht[2]; // 使用两张表,是为了上当扩容时不要求一次扩容完成,而是渐进的,以防长时间堵塞线程

    int rehashidx; /* rehashing not in progress if rehashidx == -1 */

    int iterators; /* number of iterators currently running */

} dict;

正如上文所言我们需要不同的数据类型

/* Object types */

#define REDIS_STRING 0

#define REDIS_LIST 1

#define REDIS_SET 2

#define REDIS_ZSET 3

#define REDIS_HASH 4


typedef struct redisObject {

    unsigned type:4;

    unsigned notused:2;     /* Not used */

    unsigned encoding:4;

    unsigned lru:22;        /* lru time (relative to server.lruclock) */

    int refcount;

    void *ptr;

} robj;
向外界提供固定的数据类型,但是底层交由不同的数据结构进行实现。
#define REDIS_ENCODING_RAW 0     /* Raw representation */

#define REDIS_ENCODING_INT 1     /* Encoded as integer */

#define REDIS_ENCODING_HT 2      /* Encoded as hash table */

#define REDIS_ENCODING_ZIPMAP 3  /* Encoded as zipmap */

#define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */

#define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */

#define REDIS_ENCODING_INTSET 6  /* Encoded as intset */

#define REDIS_ENCODING_SKIPLIST 7  /* Encoded as skiplist */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值