Redis字典資料結構

來源:互聯網
上載者:User

標籤:

  • 字典節點。key/value結構。 

typedef struct dictEntry {    void *key;    union {        void *val;        uint64_t u64;        int64_t s64;        double d;    } v;    struct dictEntry *next;} dictEntry;


  • 字典表

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]; //rehash, from old to new    long rehashidx; /* rehashing not in progress if rehashidx == -1 */    int iterators; /* number of iterators currently running */} dict;



  • 迭代器

將safe置1可將迭代器定義為安全的,也就是說添加、尋找或者其他動作是與其他的迭代器排他的。

如果是非安全的,則只能進行遍曆操作。

/* If safe is set to 1 this is a safe iterator, that means, you can call * dictAdd, dictFind, and other functions against the dictionary even while * iterating. Otherwise it is a non safe iterator, and only dictNext() * should be called while iterating. */typedef struct dictIterator {    dict *d;    long index;    int table, safe;    dictEntry *entry, *nextEntry;    /* unsafe iterator fingerprint for misuse detection. */    long long fingerprint;} dictIterator;


迭代器設計的時候採用指紋的方法(long long dictFingerprint(dict *d) )

申請迭代器的時候,會根據當前字典的地址、大小和使用量計算一個HASH值。每次使用迭代器的時候,比較這個迭代器的指紋

與當前字典的指紋是否一樣,如果不同則此迭代器不能使用。

關於字典的REHASH

字典本質上講就是一個雜湊表。Redis的rehash策略是,記錄每次進行rehash的位置,在每一次

對字典的操作時判斷是否在進行rehash操作。如果是則rehash,為了不會出現某個操作耗時太久

定義了單次操作的最大bucket個數


Redis字典資料結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.