redis源碼學習-dict
阿新 • • 發佈:2018-07-26
const 通過 mixin because read int32 table ase 表頭
- 1.字典相關的幾個結構體
dict由hash table存儲key-value, hash table數組每一個元素存放dictEntry鏈接的鏈表頭結點,dictEntry節點存放key-value
typedef struct dictEntry { void *key; union { void *val; uint64_t u64; int64_t s64; double d; } v; struct dictEntry *next; } dictEntry; typedef structdictht { dictEntry **table; // 指向dictEntry數組的指針 unsigned long size; //哈希表table的大小,初始化大小為4 unsigned long sizemask; // size - 1 ,用來對hash值求與計算獲得index unsigned long used; // 已經賦值了的數量 } dictht; typedef struct dict { dictType *type; // 方法 void *privdata; // 保存key和value dictht ht[2]; //hash table long rehashidx; // 如果rehashidx=-1表示沒有進行rehash,如果如果rehashidx>-1,則表示正在進行rehash,搬運的位置是rehashidx int iterators; /* number of iterators currently running */ } dict;
- 2.動態擴容方法 int dictRehash(dict *d, int n)
為了對dictht進行動態擴容,rehash方法將ht[0]中的值搬n個到ht[1]中, 分批次進行搬運,直到ht[0]中的值都搬到ht[1]上,再將ht[1]指針交給ht[0],rehashidx=-1,完成此次rehash過程
int dictRehash(dict *d, int n) { int empty_visits = n * 10; /* Max number of empty buckets to visit. */ if (!dictIsRehashing(d)) return 0; // 從ht[0]中搬n個鏈表到ht[1]中 while (n-- && d->ht[0].used != 0) { dictEntry *de, *nextde; /* Note that rehashidx can‘t overflow as we are sure there are more * elements because ht[0].used != 0 */ assert(d->ht[0].size > (unsigned long) d->rehashidx); // 通過rehashidx可以接著從上一次搬完的位置開始搬 while (d->ht[0].table[d->rehashidx] == NULL) { d->rehashidx++; if (--empty_visits == 0) return 1; } de = d->ht[0].table[d->rehashidx]; /* Move all the keys in this bucket from the old to the new hash HT */ // 把ht[0]上的一個鏈表搬到ht[1]上 while (de) { unsigned int h; nextde = de->next; /* Get the index in the new hash table */ h = dictHashKey(d, de->key) & d->ht[1].sizemask; de->next = d->ht[1].table[h]; d->ht[1].table[h] = de; d->ht[0].used--; d->ht[1].used++; de = nextde; } d->ht[0].table[d->rehashidx] = NULL; d->rehashidx++; } /* Check if we already rehashed the whole table... */ if (d->ht[0].used == 0) { zfree(d->ht[0].table); d->ht[0] = d->ht[1]; _dictReset(&d->ht[1]); d->rehashidx = -1; return 0; } /* More to rehash... */ return 1; }
-
3.使用到的幾個hash算法
-
針對int的hash函數
unsigned int dictIntHashFunction(unsigned int key) { key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; }
-
MurmurHash2算法
unsigned int dictGenHashFunction(const void *key, int len) { /* ‘m‘ and ‘r‘ are mixing constants generated offline. They‘re not really ‘magic‘, they just happen to work well. */ uint32_t seed = dict_hash_function_seed; const uint32_t m = 0x5bd1e995; const int r = 24; /* Initialize the hash to a ‘random‘ value */ uint32_t h = seed ^len; /* Mix 4 bytes at a time into the hash */ const unsigned char *data = (const unsigned char *) key; // 長度大於等於4的情況 while (len >= 4) { uint32_t k = *(uint32_t *) data; // 4*8=32, 取4個字節當作uint32 k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; data += 4; len -= 4; } /* Handle the last few bytes of the input array */ // 剩下的長度小於4 switch (len) { case 3: h ^= data[2] << 16; case 2: h ^= data[1] << 8; case 1: h ^= data[0]; h *= m; }; /* Do a few final mixes of the hash to ensure the last few * bytes are well-incorporated. */ h ^= h >> 13; h *= m; h ^= h >> 15; return (unsigned int) h; }
- djb hash算法
-
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) { unsigned int hash = (unsigned int) dict_hash_function_seed; while (len--) hash = ((hash << 5) + hash) + (tolower(*buf++)); /* hash * 33 + c */ return hash; }
細節前往
(https://github.com/fangwendong/redis-learning/tree/master/struct/dict
)
redis源碼學習-dict