Redis data structure (2) dictionary, redis Data Structure dictionary

Source: Internet
Author: User
Tags rehash

Redis data structure (2) dictionary, redis Data Structure dictionary

The Redis dictionary is actually a Hash table. Its implementation is similar to the hashmap structure in JAVA. Key-Value pairs are stored in Key-Value mode, but there are some differences.
The hashmap structure in java contains the hash table and implements rehash self-expansion;
The redis dictionary implements the hash table through the dictht structure and the rehash through the dictionary (dict) (the dictionary contains a dictht array dictht ht [2]).

Implementation of Redis dictionary
Redis dictionaryHash table usedDefined by the dict. h/dictht structure:

typedef struct dictht{dictEntry **table;unsigned long size;unsigned long sizemask;unsigned long used;}dictht;

Table is an array of dictEntry structures. Each dictEntry structure stores a key-value pair. Size is the size of the table Array (note that it is not the number of key-value pairs); used is the number of key-value pairs; sizemask is size-1, the purpose will be mentioned later;
The dictEntry structure is defined as follows:

typedef struct dictEntry{void *key;union{void *val;uint64_t u64;int64_t s64;} v;struct dictEntry *next;}

The key is the key, and the v is the value. It can be defined as a pointer, A uint64_t integer, or an int64_t integer.
The next attribute points to the next dictEntry to form a linked list structure. In the dictionary structure, the hash value of each key-value is mapped to the subscript of the table. If the hash value of multiple keys is mapped to the same subscript of the table, then, these key-value pairs form a linked list through the next pointer and store it in the current subscript of the table.

In RedisDictionaryRepresented by the dict. h/dict structure:

typedef struct dict{dictType *type;void *privdata;dictht ht[2];int rehashidx;} dict;

Note:

  • Dictht is the hash table structure described above;
  • The type pointer is a dictType structure that stores some functions used to operate on key-value pairs of specific types. Redis sets different types of specific functions for different dictionaries;
  • Privdata stores the optional parameters that need to be passed to the specific function of type;
    The dictType structure is as follows:
Typedef struct dictType {// calculate the hash value unsigned int (* hashFunction) (const void * key); // copy key void * (* keyDup) (void * privdata, const void * key); // copy the value void * (* valDup) (void * privdata, const void * obj); // compare the key int * (* keyCOmpare) (void * privdata, const void * key1, const void * key2); // destroy the key void (* keyDestructor) (void * privdata, void * key ); // destroy value void (* valDestructor) (void * privdata, void * obj);} dictType;
  • The ht array contains two dictht structures. at ordinary times, only ht [0] is used, and ht [1] is used for rehash.
  • Rehashidx is a flag. If rehash is not currently performed, the value is-1. redis performs rehash progressively. During rehash, each operation is performed, add 1 to the rehashidx value;

Redis Hash Algorithm

(Not yet to be continued ...)

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.