The design of Redis database's underlying data structure __c language

Source: Internet
Author: User
Tags data structures key string redis redis server

Recently read Huangjianhong's "Redis Design and implementation", read the first part, it is the introduction of the bottom-up approach. Starting from the lowest data structure, the data structure used is introduced, and then the database is formed. I'm going to make a Redis study note, and here I'll sort it out from the top down. databases in the server

The Redis server saves all databases in a DB array of server state redis.h/redisserver structures, each of which is a REDIS.H/REDISDB structure, and each REDISDB structure represents a database.

struct redisserver{...
        An array of Redisdb, in which each item in the DB is a database
        redisdb *db;
        ...}

Redis is a key-value pair database, each database represented by a REDISDB structure, where the dictionary dict in the REDISDB structure holds all the key-value pairs in the database.
Definition of Redisdb structural body

typedef struct redisdb{...
        Save all the key values in the database to
        dict *dict;
        ...}

When we enter at the client

Redis>set msg "Hello"
OK

(typically by default in database 0, you can switch databases through select), you create two objects, one is a key string object (because the key is always a string object), and the other is a value string object (here is a string object). objects in the Redis

In Redis, the object is used to represent the keys and values in the database, and the key is always a string object, and the value object can be a string object, a list object, a hash object, a collection object, and an ordered set object of 5 kinds. Each object is represented by a redisobject structure in which the three properties associated with saving data are type,encoding,ptr.

typedef struct redisobject{
        unsigned type;
        unsigned encoding;
        void *ptr;
        ...}

The type form represents the object in the above 5, again emphasizing that the key can only be a string object (redis_string). The PTR pointer points to the underlying data structure of the object implementation, encoding represents the encoding, and the corresponding object of the different encoding is implemented with different data structures. These low-level data structures include int, embstr, simple dynamic string (SDS), dictionary, two-terminal linked list, compression lists, integer sets, jump tables, dictionaries, etc. How database is stored

As mentioned earlier, each database in Redis is represented by a REDISDB structure, and the dict in the REDISDB structure preserves all the key-value pairs in the database, each of which is an object, and the value is an object. This dictionary is generally called the key space.
Suppose you create a string:

Redis>set message "Hello World"

A list:

Redis>rpush alphabet "A" "B" "C"

And a hash table:

Redis>hmset Book name "Redis in ACtion" author "Josiah L. Carlson" publisher "Manning"

Then the redisdb of the database is stored as shown in the following illustration:

Note that each key in the dict is a stringobject, and the object corresponding to the value of the key varies depending on the stored data.
Book this hash table's ' name ' Redis in ACtion ' author ' Josiah L. Carlson ' publisher ' Manning ' is a value in Redisdb and a hash object of its own. It can be implemented by ziplist (compressed list), and can be realized by dictionary. Equivalent to "dictionary" in the "Big Dictionary" (Key Space) (a dictionary implementation of a Hash object)

For the structure of the dictionary, the structure of the SDS, the structure of the compressed list, or the structure of the set or ordered set will be sorted back.

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.