Redis source code analysis: dict. c and dict. h

Source: Internet
Author: User
Tags rehash

Introduction

Hash table is one of the core structures of redis. In redis source code, dict. c and dict. h defines the hash structure used by redis. In this article, we will. c and dict. h.

Because dict. the implementation of the separate chaining hash table used in c can be found in any algorithm book. Therefore, this article does not make too much ink on operations such as search and Addition and deletion, instead, it focuses on the operational process of the entire dictionary structure and the progressive rehash operation of hash tables.

Data Structure Overview

Dict. h defines several data structures used by dict. c programs, such as dict, dictht, and dictEntry. The relationships between them can be used to describe (click to enlarge ):

Data Structure Implementation Details

The big chart in the previous section shows the relationships between data structures. Now, let's focus on the three core data structures: dict, dictht, and dictEntry.

The dict structure is defined as follows:

  1. /* Dictionary structure */
  2. Typedef StructDict {
  3. DictType * type;// A family of functions used for values of different types in the hash table
  4. Void* Privdata;
  5. Dictht ht [2];// Each dictionary uses two hash tables.
  6. IntRehashidx;// Indicates whether rehash is in progress. If not, it is-1.
  7. IntIterators;// Number of iterator currently in use
  8. } Dict;

The comments of the Code basically describe the functions of the relevant attributes. Some of the notes that need to be supplemented are:

Each dictionary uses two hash tables. To implement incremental rehash, redis will move the elements of hash table No. 0 to hash table No. 1 one by one, after the hash table No. 0 is cleared, relevant details will be provided later in the article. Don't worry!

In addition, rehashidx records are actually indexed by rehash. For example, if rehash is applied to 10th elements, the value of rehashidx is 9, and so on. If rehash is not performed, the value of rehashidx is-1.

Next let's take a look at the hash table structure-dictht structure. This hash table is implemented by a separate chaining hash table, which solves the conflict problem by placing elements with the same hash value in a linked list:

  1. Typedef StructDictht {
  2. DictEntry ** table;// Node pointer Array
  3. UnsignedLongSize;// Number of buckets
  4. UnsignedLongSizemask;// Mask code for address Index Calculation
  5. UnsignedLongUsed;// Number of existing nodes
  6. } Dictht;

The table attribute is an array with a node pointer. It is used as a linked list.

The size, sizemask, and used attributes seem a little dizzy at the beginning. In fact, they represent:

Size: the number of buckets, that is, the size of the table array.

Sizemask: this value is calculated by size-1. After the hash value of the given key is calculated, it performs & operation with this value, determines where the element is placed in the table array.

Used: the number of elements in the hash table, that is, the total number of dictEntry structures saved in the hash table.

Okay. Finally, the node Structure of the linked list -- dictEntry:

  1. Typedef StructDictEntry {
  2. Void* Key;// Key
  3. Union{
  4. Void* Val;
  5. Uint64_t u64;
  6. Int64_t s64;
  7. } V;// Value (there can be several different types)
  8. StructDictEntry * next;// Point to the next hash node (Form A linked list)
  9. } DictEntry;

This structure... A, well, there is nothing to say about this structure. Please hurry to the next part.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
  • Next Page

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.