Redis (three) data structure Hash hash

Introduction to a hash object

  1. Almost all programming languages ​​provide hash types, which may be called hashes, dictionaries, and associative arrays;
  2. Hash

In Redis, the hash type refers to the key value itself is a key-value pair structure, such as value={ {field1, value1},...{fieldN, valueN}}, both of the Redis key-value pair and the hash type The relationship can be represented by the following figure
Insert picture description here

Two commonly used commands

Command the best person to knock it.

Three, internal coding

There are two types of internal coding for hash types:

  1. ziplist (compressed list): When the number of hash type elements is less than the hash-max-ziplist-entries configuration (default 512), and all values ​​are less than the hash-max-ziplist-value configuration (default 64 bytes), Redis Will use ziplist as the internal implementation of hash, ziplist uses a more compact structure to achieve continuous storage of multiple elements, so it is better than hashtable in terms of saving memory
  2. Hashtable (hash table): When the hash type cannot meet the conditions of the ziplist, Redis will use the hashtable as the internal implementation of the hash, because at this time the read and write efficiency of the ziplist will decrease, and the read and write time complexity of the hashtable is O (1)

Insert picture description here
Specific differences see two codes of understanding

Four data structure

dictht is a hash table structure that uses the zipper method to store the dictEntry of hash conflicts.

typedef struct dictht{
    
    
    //哈希表数组
    dictEntry **table;
    //哈希表大小
    unsigned long size;
    //哈希表大小掩码,用于计算索引值
    unsigned long sizemask;
    //该哈希表已有节点的数量
    unsigned long used;
}
 
typedef struct dictEntry{
    
    
    //键
    void *key;
    //值
    union{
    
    
        void *val;
        uint64_tu64;
        int64_ts64;
    }
    struct dictEntry *next;
}

The Redis dictionary dict contains two hash tables dictht, which is to facilitate the rehash operation. When expanding the capacity, rehash the key-value pairs on one of the dicthts to the other dictht, release the space after completion and exchange the roles of the two dicthts.

typedef struct dict {
    
    
    dictType *type;
    void *privdata;
    dictht ht[2];
    long rehashidx; /* rehashing not in progress if rehashidx == -1 */
    unsigned long iterators; /* number of iterators currently running */
} dict;

The rehash operation is not done all at once, but in a progressive way . The purpose is to avoid the burden of performing too many rehash operations on the server at one time.

Progressive rehash is completed by recording the rehashidx of the dict. It starts from 0 and then does not execute a rehash. For example, in a rehash, rehash dict[0] to dict[1], this time put dict[0] on table[ The key-value pair of rehashidx] is rehash to dict[1], the table[rehashidx] of dict[0] points to null, and rehashidx++ is set.

During the rehash, every time the dictionary is added, deleted, searched or updated, a progressive rehash will be performed. The use of progressive rehash will cause the data in the dictionary to be scattered in two dicthts, so the operation of the dictionary will also be performed on the two hash tables. For example, when searching, first search from ht[0], then search ht[1]. When adding, add it directly to ht[1].

Reference article

Redis

Guess you like

Origin blog.csdn.net/GreedySnaker/article/details/115164389