Redis Source Learning (core data structure anatomy)

Source: Internet
Author: User

Redis is a key, a value database, and a memory database. The architecture of an internet company is now standard.

The supported data objects are string, list, set, zest, and hash object.


Data:

The core structure of the database is dict (implementation is using HASHMAP):

Key:string

Value:string or list or set or zest or hash object.

DICT data structure Definition:

typedef struct DICTHT {    //hash table array    dictentry **table;    Hash table size    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;} dictht;
Table is an array of hash tables, each element is a dictentry (hash node) pointer, and the dictentry is defined as follows:

typedef struct DICTENTRY {    //key    void *key;    Value    Union {        void *val;        uint64_t U64;        int64_t s64;    } V;    Point to the next hash table node, forming a list of    struct dictentry *next;} dictentry;
Where key is the key to the hash table, and value is the object that Val points to ( string, list, set, zest, and hash object), either a 64-bit unsigned shape, or a 64-bit signed shape, next is a pointer to the next hash node.

The data structure diagram is represented as follows:



Database: The core data structure of the database is described above, and now it is analyzed from the perspective of the database service initiation. Start the portal main function:
int main (int argc, char **argv) {//...//Initialize service initserver ();//...}
Initserver function:
void Initserver () {    //...    /* Create the Redis databases, and initialize other internal state. *    ///Create and initialize the database structure for    (j = 0; J < Server.dbnum; J + +) {        server.db[j].dict = dictcreate (&dbdicttype,null );        Server.db[j].expires = Dictcreate (&keyptrdicttype,null);        Server.db[j].blocking_keys = Dictcreate (&keylistdicttype,null);        Server.db[j].ready_keys = Dictcreate (&setdicttype,null);        Server.db[j].watched_keys = Dictcreate (&keylistdicttype,null);        Server.db[j].eviction_pool = Evictionpoolalloc ();        Server.db[j].id = j;        Server.db[j].avg_ttl = 0;    }    //...}
Where: struct reidsserver the definition of the Server;//server object.

Redisserver structure:

struct Redisserver {//...//Redis db array redisdb *db;<span style= "White-space:pre" ></span>//number of databases <span Style= "White-space:pre" ></span>int dbnum;//...}
A redisserver can be defined Dbnum database, in the process of use can switch, using the Select command, such as: Select 0, select 1 ...

REDISDB's data structure:

/* Redis database representation. There is multiple databases identified * by integers from 0 (the default database) up to the max configured * database. The database number is the ' ID ' field in the structure.                 */typedef struct REDISDB {//Database key space, save all the key values in the database Dict *dict;              /* The keyspace for this DB *///key expires, the dictionary key is the key, the dictionary value is the expired event UNIX timestamp dict *expires;        /* Timeout of keys with a timeout set *///is in the blocking state of the key dict *blocking_keys;           /* keys with clients waiting for data (Blpop) *///can unblock the key dict *ready_keys;         /* Blocked keys that received a PUSH *///is being monitored by the Watch command key dict *watched_keys;    /* Watched keys for multi/exec CAS */struct evictionpoolentry *eviction_pool;                     /* Eviction Pool of keys *///database number int id;          /* Database ID *///The average TTL of the keys for the databases, statistics long long avg_ttl; /* Average TTL, just for stats */} REDISDB;

You can see that the core is a hash table dict.


Redis Source Learning (core data structure anatomy)

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.