一、Redis Hash
1, Redis dictionary implemented as the underlying hash table, a hash table which nodes can have multiple hash tables, each hash table of the node is saved in a dictionary key pair.
2, Redis hash table used by the dictionary dict.h/dictht
definition structure:
typedef struct dictht { // hash table array dictEntry ** Table; // hash table size unsigned Long size; // hash table size mask for calculating the index value // always equal size -. 1 unsigned Long sizemask ; // number of the hash table of an existing node unsigned Long Used; } dictht;
1.table property is a genus group, each element of the array pointing to adict.h/dictEntry结构的指针,每个
dictEntry结构保存着一个键值对;
2.size attribute records the size of the hash table, that is, the size of the array table, and used property records the hash table there are nodes (key-value pairs) number;
3.sizemask value of the attribute is always equal to size-1, and the property values determined with a hash key which index table should be placed above the array;
3. hash table node uses dictEntry
the structure of said each dictEntry
structure are preserved in a key-value pair:
typedef struct dictEntry { // key void * Key; // value Union { void * Val; uint64_t U64; an int64_t S64; } V; // point to the next node hash table, a linked list is formed struct dictEntry * Next; } dictEntry;
1.key attribute holds the key to the key, and v is the attribute value of the stored key-value pair, where the value of v may be a pointer, or a uint64_t integer, or an integer int64_t;
2.next property is a pointer to another node in the hash table, the hash value of the pointer may be a plurality of the same key problems connected together, in order to resolve the conflict bond;
Illustrate how, connected together by the next pointer to the same key two index values k1 and k0:
4, Redis in the dictionary dict.h/dict
shows the structure of:
typedef struct dict { // Type-specific function dictType * type; // private data void * privdata; // hash table dictht HT [ 2 ]; // rehash index // When rehash not performed, a value of -1 int rehashidx ; / * rehashing Not in Progress IF rehashidx == -1 * / } dict;
1. type
properties and privdata
properties for different types of key-value pairs to create a set of polymorphic dictionaries;
2. The ht
property is an array that contains two entries, each entry in the array is a dictht
hash table, under normal circumstances, only use the dictionary ht[0]
hash table, ht[1]
hash table will only to ht[0]
be used when the hash table rehash ;
3. In addition to ht[1]
outside, and another rehash related properties that rehashidx
: rehash it records the current pace, if not currently performing rehash, then its value -1
.
4 illustrates a normal state of the dictionary (not for the rehash) of: