The dictionary and hash structure in Redis

Source: Internet
Author: User
Tags hash redis rehash

A dictionary is a data structure implementation in Redis that is used to hold key-value pairs.

Each key in the dictionary is unique, and the program can find the value associated with it based on the key.


The underlying implementation of the Redis database is a dictionary, and the operation of the database for additions and deletions is accomplished by manipulating the dictionary.

The dictionary is also one of the underlying implementations of the hash key, and Redis uses the dictionary as the underlying implementation of the hash key if a hash key contains more key-value pairs or longer string lengths.

---------------------------------------------------


The Redis Dictionary is implemented as a hash table,



The hash table is defined by the DICT.H/DICTHT structure:

typedef struts dictht{
  //hash table array
  dictentry **table;
  Hash table array
  unsigned long size;
  Hash table size mask, used to calculate index value
  //always equals size-1
  unsigned long sizemask;
  The hash table already has the number of nodes
  unsigned long used;
} DICTTH;
The table property is an array, with each element pointing to a pointer to a hash table node.


The hash table node is defined by the Dict.h/dictentry structure

<pre name= "code" class= "CPP" >typedef struts dictentry{
  //key
  void *key;
  Value  
union{
     void *val;
     Uint64_tu64;
     Int64_ts64;
  }
  Point to the next hash table node, form the list
  strut dictentry *next;
} Dictentry;

The value of a key-value pair can be a pointer, an uint64_t integer, or a int64_t integer .

the next property works by: If multiple key-value pairs have the same hash value, they become a one-way list to resolve the conflict.

dictionaries in Redis are defined by the DICT.H/DICT structure

typedef struts dict{
<pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" >  //Type-specific functions
  Dicttype *type;
  Private data
  void *private;
//hash table
  dictth ht[2]
  //rehash index
  //When rehash is no longer in progress, the value is-1
int Trehashidx;
} Dict

The Type property and private property are used to create a polymorphic dictionary for different types of key-value pairs.

The DICTTYPE structure preserves some functions for manipulating key-value pairs, and Redis sets different functions for dictionaries of different purposes.

Private saves the arguments passed to the function.

The HT attribute holds two hash tables, usually using only one hash table and another hash table as a copy.


This diagram shows a dictionary in a normal state:




--------------------------------------------------------------------------------------


When a key-value pair needs to be stored in a dictionary,

1. The program uses the hash algorithm to calculate the hashcode of the key based on the key value.

2. The index of the key-value pair is calculated from the hashcode and Sizemask attributes calculated by key.

3. Place the key-value pair on the specified index of the hash table array according to the index.

3.1 If a key-value pair already exists at the index, a unidirectional list is formed and the newly added key-value pair is placed in the table header of the linked list.

3.2 If not, join directly.


REHASHC operation

In the process of adding new data continuously, in order to reduce the load factor of the hash table, the program expands or scales the size of the hash table by rehash Operation .

This operation uses the H "0" and H "1" in the dictionary.

Load factor =ht "0". Used/ht "0". Size

The program performs a shrink operation when the load factor is less than 0.1.

Extension: ht[1] >= ht[0].used*2 2^n
Shrinkage: ht[1] = ht[0].used of 2^n

Rehash operation, rehash action is divided into several times, progressive completion. Avoids the large amount of computation that the centralized rehash brings.
During a progressive rehash operation, when a new value is added to the dictionary, it is saved in h[1], and the key-value pairs in h[0] are only reduced and not incremented.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.