[Redis] hashmap data structure

A description

redis wherein a data type hashmap, i.e., the hash table

Normal achieve hashmap:

1. Allocate fixed size bucket size n

2. Compute hash key value, and the modulo n, to obtain the index position index tub

2 The calculated index, and then stored in the corresponding bucket

4. When confronted with a collision, the collision problem will be solved through the list


Two, redis defined data structure


struct dictht: for the actual implementation of the hash table structure

struct dict: an outer layer encapsulating the hash table, a function is mainly used when the current dictht rehash required, which creates a new dictht, and when the word will be requested to complete the transfer of a portion of the work


struct dictEntry: per barrel, which is the next conflict resolution list

Three, rehash:

Redis normal hash table and hash table to realize there is not much difference, the only difference is somewhat different in the case of the need to re-expansion of rehash-

As we know, redis is single-threaded single-process mode, for handling rehash If you have completed a one-time data, a large amount of data in time, will be

Very time-consuming operation, and therefore is not a one-time redis finished handling all the data, but each time the request will trigger handling work,

But the handling of data is a small part of it.

1.lazy rehash, when each operation dict, it will carry a slot to a new hashmap

2.active rehash, each over a period of time will be data handling


Fourth, the small point:

When using a hashmap, does not mean that each will directly redis way hash table to store data, and therefore when used alone key-value, value is used as hashmap,

It may just be used to store small amounts of data and simple, so in comprehensive consideration of performance and memory consumption so on, in the beginning redis will pass ziplist (doubly-linked list to store the compressed data) related links .

The control will be used when hashmap to store, can be controlled by parameters:

hash-max-ziplist-entries 512 (entry number of the maximum storage ziplist)

hash-max-ziplist-value 64 (the maximum number of bytes ziplist a stored entry)

Any more than these two limits, will speak ziplist converted into hashmap

Published 140 original articles · won praise 28 · views 180 000 +

Guess you like

Origin blog.csdn.net/qq_16097611/article/details/79872678