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.