標籤:
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字典資料結構