This section describes the Redis hash dictionary used to find the corresponding value through the key value. Note how the Redis dictionary performs rehash. The data structure of source code dict. h dict. c is shown in. The hash dictionary is represented by a dict structure, which contains two hash tables for rehash operations. Meanwhile, hash tables use tables to resolve conflicts. The specific data structure is as follows: [cpp]/** hash table node */typedef struct dictEntry {// key void * key; // value union {void * val; uint64_t u64; int64_t s64;} v; // link back to node struct dictEntry * next;} dictEntry; /** type-specific processing functions of a cluster */typedef struct dictType {// calculate the key's hash function unsigned int (* hashFunction) (const void * key ); // function void * (* keyDup) (void * privdata, const void * key) of the replication key; // function void * (* valDup) (void * privdata, const void * obj); // Compare the two key functions int (* keyCompare) (void * privdata, const void * key1, const void * key2 ); // key interpretation function void (* keyDestructor) (void * privdata, void * key); // value interpretation function void (* valDestructor) (void * privdata, void * obj);} dictType;/** hash table */typedef struct dictht {// array of hash table node pointers (commonly known as buckets) dictEntry ** table; // The size of the pointer array unsigned long size; // The length mask of the pointer array, used to calculate the index value unsigned long sizemask; // The hash shows the number of nodes unsigned long Used;} dictht;/** dictionary ** each dictionary uses two hash tables for progressive rehash */typedef struct dict {// type-specific processing function dictType * type; // type processing function's private data void * privdata; // hash table (2) dictht ht [2]; // mark the rehash progress, -1 indicates that int rehashidx is not performed on rehash; // number of currently running security iterators int iterators;} dict; /** dictionary iterator ** if the safe attribute value is 1, it indicates that the iterator is a secure iterator. * When the security iterator is iterating a dictionary, it can still call dictAdd, dictFind, and other functions. ** If the value of the safe attribute is 0, it indicates that this is not a security iterator. * If the running iterator is an insecure iterator, it can only call the dictNext function on the dictionary. */Typedef struct dictIterator {// dictionary in iteration dict * d; int table, // Number of the hash table being iterated (0 or 1) index, // The index of the hash table array being iterated is safe; // is it safe? DictEntry * entry, // current hash node * nextEntry; // next node of the current hash node} dictIterator; analysis rehash uses two hash tables in the dictionary to facilitate rehash, there are two main rehash Methods: one is to call cron in the background to perform hash Based on step 1, and the other is to perform Step 1 hash when adding and deleting the elements in the dictionary. Here, each hash step is measured in the linked list corresponding to a real hash key, that is to say, the step-by-step rehash is to rehash a key linked list in the original hash table to the position in the new hash table. Here we can see that Redis sacrifices the memory but achieves a performance response. Scatter the rehash operation to avoid sharp performance degradation caused by blocking. If the attributes of the www.2cto.com iterator are secure, it indicates that other operations such as dictAdd can be performed during the iteration, if the iterator is insecure, you can only perform next operations. Redis also restricts rehash when an iterator accesses the dictionary, because this will cause multiple accesses to an element. However, the dictionary can be traversed at any time during the rehash process. Once the iterator starts to traverse the dictionary, the rehash will pause until no iterator is available to traverse the dictionary.