Quickly understand what a dictionary in Redis is

Introduction to Dictionaries

A dictionary is a data structure used to store key-value pairs, which can quickly find the corresponding value through the key in the key-value pair. In the C language used by Redis, there is no built-in dictionary, so Redis implements the dictionary itself.

All the keys and values ​​of the entire Redis database form a global dictionary, and the operations of adding, deleting, modifying and querying the database are built on the operations of the dictionary.

The dictionary is also one of the underlying implementations of Redis' basic data type hash. When the length of the key and value of the hash data type is large or the number of key-value pairs is large, Redis will use the dictionary as hash data. The underlying implementation of the type.

The dictionary is also one of the underlying implementations of the basic data type ordered set (zset) of Redis. When the member length of all elements in the ordered set is long or the number of elements is large, Redis will use the jump table and hash. Tables are implemented internally as sorted sets.

Implementation of the dictionary

A dictionary in Redis is represented by a dictstructure, and its underlying implementation uses a hash table. A hash table contains multiple hash table nodes, and each hash table node stores a key-value pair in the dictionary. . Let's take a look at each one in detail.

dictionary structure

A dictionary is represented by a dictstructure, which contains the following properties:

  • typeProperty: A pointer to a dictTypestructure, each dictTypeof which holds a set of functions that operate on key-value pairs of its specific type.
  • privdataProperties: Holds optional parameters that need to be passed to a function of its specific type.
  • htProperties: An array of two items, each of which is a hash table. In general, the dictionary only uses the first hash table, and only uses the second hash table when rehashing.
  • rehashidxAttribute: used to record the progress of the rehash. When there is no rehash, its value is -1.

Among them, after typeattributes and privdataattributes are combined, different processing operations can be performed for different types of key-value pairs, such as: calculating hash values, copying keys, copying values, comparing keys, destroying keys, destroying values, and so on.

hash table

A hash table dicthtis represented by a structure, which contains the following properties:

  • tableAttribute: A hash table array, each item in the array is a pointer to a hash table node, and each hash table node stores a key-value pair in the dictionary.
  • sizeAttribute: The size of the hash table, which is also the size of the hash table array ( table).
  • sizemaskAttribute: Hash table size mask, used to calculate the index value, always equal to the hash table size ( size)-1.
  • usedAttribute: The number of existing nodes in the hash table.

hash table node

Hash table nodes are dictEntryrepresented by structures, which contain the following properties:

  • keyAttributes: Used to hold keys in key-value pairs.
  • vAttribute: used to hold the value in a key-value pair. It can be a pointer, an uint64_tinteger, or an int64_tinteger.
  • nextAttribute: A pointer to the next hash table node. When a key conflict occurs, it can connect multiple hash table nodes to form a singly linked list.

Dictionary example

Having said so much, it is relatively abstract and difficult to understand. Let's take an example:

Manneko Gakusha

This is the internal structure of a dictionary, in which there are 3 key-value pairs, the keys are: Wan, Cat, and Society.

Finally, thank you for being so handsome, and for giving me likes and attention .

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324031669&siteId=291194637