Redis源码初探之数据结构

Redis可以存储键和5种不同类型的数据结构,它们是STRING(字符串),lIST(列表),SET(集合),HASH(散列表),ZSET(有序集合)。
1.字符串(String)可以是字符串、整数或者浮点数。Redis对整个字符串或者其中部分执行操作。对整数和浮点数执行自增或者自减操作。
2.链表(List)链表的每个节点都包含一个字符串.Redis可以从链表两端插入或者弹出元素。

/*链表节点*/
typedef struct listNode {
    struct listNode *prev;
    struct listNode *next;
    void *value;
} listNode;
/*节点迭代器*/
typedef struct listIter {
    listNode *next;
    int direction;
} listIter;
/*链表*/
typedef struct list {
    listNode *head;/*头节点*/
    listNode *tail;/*尾节点*/
    void *(*dup)(void *ptr);
    void (*free)(void *ptr);
    int (*match)(void *ptr, void *key);
    unsigned long len;/*链表长度*/
} list;

3.集合(SET)包含字符串的无序收集器,集合中的字符串唯一。
4.散列表(HASH)包含键值对的无序列表
5.有序集合(ZSET)元素按照分值的大小顺序排列。

 

typedef struct zset {
    dict *dict;/*字典*/
    zskiplist *zsl;
} zset;
5.1字典项定义
/*dictEntry是字典的一条记录*/
typedef struct dictEntry {
    void *key;
    union {
        void *val;
        uint64_t u64;
        int64_t s64;
        double d;
    } v;
    struct dictEntry *next;/*下一条记录*/
} dictEntry;

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;

/* 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;
    void *privdata;
    dictht ht[2];
    long rehashidx; /* rehashing not in progress if rehashidx == -1 */
    unsigned long iterators; /* number of iterators currently running */
} dict;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值