亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

redis源碼筆記-dict.h

系統 2551 0

這篇介紹redis最后一個基礎數據結構——hash表。可以毫不夸張的說,hash表是redis一切存儲的基礎,也是redis得以快如飛的基礎。

注:其實還有個intset,不過intset是在持久化dump到硬盤時為節省空間設計的,和我們這里談的不一樣。

dict的設計呢,簡單的說是一個雙表,“一主一從”,不定時rehash,建議大家在讀代碼前能夠對這個設計有所了解。Anyway,隨便搜一搜,很多文章的。

dict.h

      
          1
      
      
        #ifndef __DICT_H

      
      
          2
      
      
        #define
      
       __DICT_H

      
          3
      
      
          4
      
      
        #define
      
       DICT_OK 0

      
          5
      
      
        #define
      
       DICT_ERR 1

      
          6
      
      
          7
      
      
        /*
      
      
         Unused arguments generate annoying warnings... 
      
      
        */
      
      
          8
      
      
        #define
      
       DICT_NOTUSED(V) ((void) V)          //redis實現里很多這種NOTUSED宏,算是作者偷懶吧

      
          9
      
      
         10
      
       typedef 
      
        struct
      
      
         dictEntry {

      
      
         11
      
      
        void
      
       *
      
        key;

      
      
         12
      
      
        void
      
       *
      
        val;

      
      
         13
      
      
        struct
      
       dictEntry *
      
        next;

      
      
         14
      
      
        } dictEntry;                                 //redis的一個dict表項,存的是key , val, 以及因為redis的hash是用的拉鏈法,所以有一個指向下一個dictEntry的指針;
        
//一個dictEntry的內存占用為12個字節,記住這個數字。
15 16 typedef struct dictType { 17 unsigned int (*hashFunction)( const void * key); //hash函數 18 void *(*keyDup)( void *privdata, const void * key); //復制key 19 void *(*valDup)( void *privdata, const void * obj); //復制value 20 int (*keyCompare)( void *privdata, const void *key1, const void * key2); //key的compare函數 21 void (*keyDestructor)( void *privdata, void * key); 22 void (*valDestructor)( void *privdata, void * obj); 23 } dictType; //dict的類型 24 25 /* This is our hash table structure. Every dictionary has two of this as we 26 * implement incremental rehashing, for the old to the new table. */ 27 typedef struct dictht { 28 dictEntry ** table; 29 unsigned long size; //size 30 unsigned long sizemask; 31 unsigned long used; //used 32 } dictht; //這個是hash table結構,每個dictionary有兩個hash 表,實現的是遞增的rehash 33 34 typedef struct dict { 35 dictType * type; 36 void * privdata; 37 dictht ht[ 2 ]; 38 int rehashidx; /* rehashing not in progress if rehashidx == -1 */ //記錄是否在rehash的一個flag,rehash進行過程中,有些操作不能進行 39 int iterators; /* number of iterators currently running */ //記錄在dict上正在執行的iter數 40 } dict; //dict的數據結構 41 42 /* If safe is set to 1 this is a safe iteartor, that means, you can call 43 * dictAdd, dictFind, and other functions against the dictionary even while 44 * iterating. Otherwise it is a non safe iterator, and only dictNext() 45 * should be called while iterating. */
//迭代器按照是否可以執行改變hash表的函數區別為safe iterator和non safe iterator, non safe iterator只能執行dictNext函數
46 typedef struct dictIterator { 47 dict * d; 48 int table, index, safe; 49 dictEntry *entry, * nextEntry; 50 } dictIterator; 51 52 /* This is the initial size of every hash table */ 53 #define DICT_HT_INITIAL_SIZE 4 54 55 /* ------------------------------- Macros ------------------------------------ */ 56 #define dictFreeEntryVal(d, entry) \ 57 if ((d)->type-> valDestructor) \ 58 (d)->type->valDestructor((d)->privdata, (entry)-> val) 59 60 #define dictSetHashVal(d, entry, _val_) do { \ 61 if ((d)->type-> valDup) \ 62 entry->val = (d)->type->valDup((d)-> privdata, _val_); \ 63 else \ 64 entry->val = (_val_); \ 65 } while ( 0 ) 66 67 #define dictFreeEntryKey(d, entry) \ 68 if ((d)->type-> keyDestructor) \ 69 (d)->type->keyDestructor((d)->privdata, (entry)-> key) 70 71 #define dictSetHashKey(d, entry, _key_) do { \ 72 if ((d)->type-> keyDup) \ 73 entry->key = (d)->type->keyDup((d)-> privdata, _key_); \ 74 else \ 75 entry->key = (_key_); \ 76 } while ( 0 ) 77 78 #define dictCompareHashKeys(d, key1, key2) \ 79 (((d)->type->keyCompare) ? \ 80 (d)->type->keyCompare((d)-> privdata, key1, key2) : \ 81 (key1) == (key2)) 82 83 #define dictHashKey(d, key) (d)->type->hashFunction(key) 84 85 #define dictGetEntryKey(he) ((he)->key) 86 #define dictGetEntryVal(he) ((he)->val) 87 #define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size) 88 #define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used) 89 #define dictIsRehashing(ht) ((ht)->rehashidx != -1) 90 91 /* API */ 92 dict *dictCreate(dictType *type, void * privDataPtr); 93 int dictExpand(dict *d, unsigned long size); 94 int dictAdd(dict *d, void *key, void * val); 95 int dictReplace(dict *d, void *key, void * val); 96 int dictDelete(dict *d, const void * key); 97 int dictDeleteNoFree(dict *d, const void * key); 98 void dictRelease(dict * d); 99 dictEntry * dictFind(dict *d, const void * key); 100 void *dictFetchValue(dict *d, const void * key); 101 int dictResize(dict * d); 102 dictIterator *dictGetIterator(dict * d); 103 dictIterator *dictGetSafeIterator(dict * d); 104 dictEntry *dictNext(dictIterator * iter); 105 void dictReleaseIterator(dictIterator * iter); 106 dictEntry *dictGetRandomKey(dict * d); 107 void dictPrintStats(dict * d); 108 unsigned int dictGenHashFunction( const unsigned char *buf, int len); 109 unsigned int dictGenCaseHashFunction( const unsigned char *buf, int len); 110 void dictEmpty(dict * d); 111 void dictEnableResize( void ); 112 void dictDisableResize( void ); 113 int dictRehash(dict *d, int n); //進行多少個dictEntry的rehash 114 int dictRehashMilliseconds(dict *d, int ms); //限制rehash執行的時間,這兩個函數都在redis的主流程中被調用,避免因為執行rehash,無法響應client請求。 115 116 /* Hash table types */ 117 extern dictType dictTypeHeapStringCopyKey; 118 extern dictType dictTypeHeapStrings; 119 extern dictType dictTypeHeapStringCopyKeyValue; //三種預先定義好的dictType,具體定義參考其他文件。 120 121 #endif /* __DICT_H */

redis源碼筆記-dict.h


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲精品一 | 久久视频这里只有精品35 | 日本成人不卡 | 日本精高清区一 | 国内精品久久久久久西瓜色吧 | 国产伦精品一区二区三区网站 | 欧美性猛交xxx嘿人猛交 | 国产一级毛片网站 | 久久国产精品老女人 | 可以免费观看欧美一级毛片 | 色偷偷亚洲第一成人综合网址 | 成人短视频视频在线观看网站 | 成年女人毛片免费播放人 | 国产99小视频 | 99热人人| 久久久噜噜噜久久老司机 | 成人亚洲精品7777 | 特级无码a级毛片特黄 | 狠狠狠色丁香婷婷综合久久五月 | 99久久www免费 | 久久资源总站 | 日本级毛片免费观看 | 美女a毛片 | 狠狠色丁香婷婷综合视频 | 狠狠色噜噜狠狠狠狠2018 | 不卡伦理 | 激情久久婷婷 | 国内精品视频一区二区八戒 | 国产欧美久久久精品影院 | 91在线网址 | 欧美日韩高清一区 | 久久久免费观成人影院 | 日本在线观看不卡免费视频 | 国产亚洲欧美另类一区二区三区 | 亚洲欧美日韩国产综合 | 久久午夜影院 | 日本一级一片免在线观看 | 91探花视频在线观看 | 国产亚洲精品激情一区二区三区 | 伊人久久青草青青综合 | 毛茸茸的浓密在线视频 |