redis內部數據結構的數據結構

redis內部數據結構的數據結構

redis對外的公衆的數據結構有五種string,list, sethashzsetredis

編碼常量 編碼所對應的底層數據結構
REDIS_ENCODING_INT long 類型的整數
REDIS_ENCODING_EMBSTR embstr 編碼的簡單動態字符串
REDIS_ENCODING_RAW 簡單動態字符串
REDIS_ENCODING_HT 字典
REDIS_ENCODING_LINKEDLIST 雙端鏈表
REDIS_ENCODING_ZIPLIST 壓縮列表
REDIS_ENCODING_INTSET 整數集合
REDIS_ENCODING_SKIPLIST 跳躍表和字典
  • string 底層數據結構
    string字符串對象的編碼能夠是int、raw或者embstr數組

  • list
    list 的數據結構是一個雙向鏈表的+ziplist 組成的quicklist數據結構

  • set
    set的底層就是dict和intset函數

  • hash
    hash 的底層就是 dict和ziplistui

  • sortedset
    sortedset的底層就是skiplist和ziplist編碼

dic 底層spa

/*Hash表一個節點包含Key,Value數據對 */
typedef struct dictEntry {
    void *key;
    union {
        void *val;
        uint64_t u64;
        int64_t s64;
        double d;
    } v;
    struct dictEntry *next; /* 指向下一個節點, 連接表的方式解決Hash衝突 */
} 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;

typedef struct dictht {
    dictEntry **table; /* dictEntry*數組,Hash表 */
    unsigned long size; /* Hash表總大小 */
    unsigned long sizemask; /* 計算在table中索引的掩碼, 值是size-1 */
    unsigned long used; /* Hash表已使用的大小 */
} dictht;

typedef struct dict {
    dictType *type;
    void *privdata;
    dictht ht[2]; /* 兩個hash表,rehash時使用*/
    long rehashidx; /* rehash的索引, -1表示沒有進行rehash */
    int iterators; /*  */
} dict;

ziplist指針

typedef struct ziplist{
     /*ziplist分配的內存大小*/
     uint32_t bytes;
     /*達到尾部的偏移量*/
     uint32_t tail_offset;
     /*存儲元素實體個數*/
     uint16_t length;
     /*存儲內容實體元素*/
     unsigned char* content[];
     /*尾部標識*/
     unsigned char end;
}ziplist;

/*元素實體全部信息, 僅僅是描述使用, 內存中並不是如此存儲*/
typedef struct zlentry {
     /*前一個元素長度須要空間和前一個元素長度*/
    unsigned int prevrawlensize, prevrawlen;
     /*元素長度須要空間和元素長度*/
    unsigned int lensize, len;
     /*頭部長度即prevrawlensize + lensize*/
    unsigned int headersize;
     /*元素內容編碼*/
    unsigned char encoding;
     /*元素實際內容*/
    unsigned char *p;
}zlentry;

skiplistcode

typedef struct zskiplistNode {
     /*成員object對象*/
    robj *obj;
     /*分數字段依賴此值對skiplist進行排序*/
    double score;
     /*插入層中指向上一個元素level數組*/
    struct zskiplistNode *backward;
    struct zskiplistLevel {
          /*每層中指向下一個元素指針*/
        struct zskiplistNode *forward;
          /*距離下一個元素之間元素數量, 即forward指向的元素*/
        unsigned int span;
    } level[];
} zskiplistNode;

typedef struct zskiplist {
     /*跳躍表頭節點和尾節點*/
    struct zskiplistNode *header, *tail;
     /*跳躍表中元素個數*/
    unsigned long length;
     /*跳躍表當前最大層數*/
    int level;
} zskiplist;

intset對象

typedef struct intset {
     /*編碼*/
    uint32_t encoding;
     /*長度*/
    uint32_t length;
     /*集合內容,按升序排列數組*/
    int8_t contents[];
} intset;
 

淘汰策略從大方向分的話就有LRU(最久未使用),LFU(最少使用), TTL(即將過時刪除)從這幾個劃分的小領域淘汰策略具體稍後介紹

相關文章
相關標籤/搜索