当前位置:网站首页>Redis advanced - correspondence between object and code base

Redis advanced - correspondence between object and code base

2022-06-12 00:37:00 Noblegasesgoo

1. String object

String type yes redis Basic data types , Not only all key All are String type , Other data types , The elements of other data types are also strings , Note, however, that the length of the string cannot exceed 512M.

1.1 code

  • int code : What's kept is that you can use long Type means An integer value .
  • embstr code : preservation length < 44 byte String (redis3.2 Before the version was 39 byte , And then 44 byte ).
  • raw code : preservation length > 44 byte String (redis3.2 Before the version was 39 byte , And then 44 byte ).

As can be seen from the above ,int Encoding is used to hold integer values , then embstr Encoding is used to save short strings ,raw Encoding is used to hold long strings .

1.2 Memory layout

 Insert picture description here

1.3 raw and embstr The difference between

raw and embstr Is to use redisObject and sds Save data , Their differences are as follows :

  • embstr Use of allocates memory space only once ( This is the result of redisObject And sds Memory is contiguous and contiguous Why ),raw It needs to be allocated twice ( To assign separately to redisObject and sds).

So with raw comparison ,embstr The advantage of the is that less space is allocated when creating , Free space less once when deleting , And all the data of the object is continuous space , Convenient query . and embstr There are also disadvantages , If it exceeds the size , To reallocate memory space .

So to avoid reallocating memory ,redis Medium embstr Implemented as a read-only .

notes :Redis Floating point type is also saved as string in , Switch back when necessary .

1.4 Code conversion

When int code Saved values No longer an integer , or The size is over long The scope of the when , Automatically convert to raw.

about embstr code , Because in redis It is read-only , In the face of embstr object When making modifications , Will be converted into raw Modify again , therefore , As long as it's a modification embstr object , The modified object must be raw Of , Whether it's reached or not 44 Bytes .

2. List objects

list list , It's a simple list of strings , Sort by insertion order , Because the bottom layer is actually a linked list , So you can plug it in .

2.1 code

The encoding of the list object is quicklist( Quick list ).

In previous versions linked and ziplist These two codes . further , at present Redis Defined 10 Object encoding method macro naming , Two of them are completely idle OBJ_ENCODING_ZIPMAP And OBJ_ENCODING_LINKEDLIST. from Redis In the evolution history of , The former is the encoding value that may be supported later ( The code is still ), The latter should be completely eliminated .

2.2 Memory layout

 Insert picture description here

3. Hash object

Hash object Key is a string type , A value is a collection of key value pairs .

3.1 code

The encoding of hash objects can be implemented ziplist perhaps hashtable.

There are two corresponding underlying implementations , One is ziplist One is dict.

3.2 Memory layout

 Insert picture description here

Pay attention to the imprecision in the above figure :

  1. ziplist Each of them entry, In addition to the binary data of the key and the value itself , Other fields are also included , But it is not drawn in the picture
  2. dict The bottom layer may hold two dictht example
  3. There is no drawing of dict Hash conflict for

It should be noted that : When HT code , namely Use dict As the underlying data structure of the hash object , Both keys and values are in sds Is stored in the form of .

3.3 Illustrate with examples

The copyright belongs to https://pdai.tech all . link :https://www.pdai.tech/md/db/nosql-redis/db-redis-data-type-enc.html

When using ziplist, That is, when the compressed list is used as the underlying implementation , The new key value pairs are saved to the end of the compressed list , For example, execute the following command :

hset profile name "Tom"
hset profile age 25
hset profile career "Programmer"

If you use ziplist,profile Store as follows :

 Insert picture description here

When using hashtable When coding , The above commands are stored as follows :

 Insert picture description here

hashtable Encoded hash table objects use dictionary data structure at the bottom , Each key value pair in the hash object uses a dictionary key value pair .

When we talked about compressed lists earlier , The compressed list we introduced is Redis Developed to save memory , It is a sequential data structure composed of a series of special coded continuous memory blocks , Relative to dictionary data structure , Compressed list for fewer elements 、 Scenes with small element length . The advantage is centralized storage , Save a space .

3.4 Encoding conversion

And the above list object ziplist The coding is the same , When both of the following conditions are satisfied , Use ziplist code :

  1. The total number of elements saved in the list < 512 individual , This condition can be set through the... In the configuration file set-max-intset-entries Make changes
  2. Length of each element < 64 byte

If these two conditions are not met, use hashtable code .

4. Set object

A collection of objects set yes string type ( The whole number will also be changed into string Type to store ) The unordered set of .

Notice the difference between sets and lists :

  • Elements in a collection are unordered , Therefore, an element cannot be manipulated through an index
  • Elements in a collection cannot be repeated

4.1 code

The code of the collection object can be intset perhaps hashtable;

There are two underlying implementations :

  • intset: Centralized storage can only be Numerical data , also It has to be an integer .
  • dict: take == All data is stored in dict In the key of , The value field is idle ==.
     Insert picture description here

4.2 Illustrate with examples

SADD numbers 1 3 5

 Insert picture description here

SADD Dfruits "apple" "banana" "cherry"

 Insert picture description here

4.3 Encoding conversion

When the following two conditions are satisfied in the case of collective contract , Use intset code :

  1. All elements in a collection object are integers
  2. All elements of the collection object cannot exceed 512 individual ( You can use the of the configuration file set-max-intset-entries To configure )

If you can't meet these two conditions, use hashtable code .

5. Zset object

And the above set Object comparison ,zset Objects are ordered , Different from using index subscript as sorting basis in list ,zset Set a for each element score Sort by .

5.1 code

zset There are still two underlying implementations of objects :

  • The first one is :ziplist As an underlying implementation , The corresponding code value macro is ZIPLIST
    • Use ziplist To achieve an ordered set , Only need ziplist On the basis of this data structure, just sort and de duplicate .
  • The second kind :dict And skiplist Together as the underlying implementation , The corresponding code value macro is SKIPLIST
    • Use skiplist To achieve an ordered set , Let's talk about it in detail .

First encode as ZIPLIST When ,zset The memory layout of is as follows :

 Insert picture description here

Encoded as skiplist When ,zset The memory layout of is as follows :

 Insert picture description here

In fact, ordered set can be realized by using dictionary or jump table alone , But the two data structures are combined , The effect can be superimposed .

Because if we use it alone dict , Although it can O(1) Time complexity to find the score of members , But because dictionaries store elements in an unordered way , So every time you do a range lookup , All have to be sorted ;

If we use it alone skiplist To achieve , You can perform a range lookup operation , But the lookup operation starts from O(1) -> O(logN).

therefore Redis Use them together to implement zset.

5.2 Illustrate with examples

The copyright belongs to https://pdai.tech all . link :https://www.pdai.tech/md/db/nosql-redis/db-redis-data-type-enc.html

ZADD price 8.5 apple 5.0 banana 6.0 cherry

 Insert picture description here

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mOZ1Tnnf-1646041617394)(2021-12-15-redis The corresponding relationship between the object and the code base is explained in detail .assets/db-redis-x-object-14.png)]

5.3 Encoding conversion

When the ordered set contract satisfies the following two conditions , Object use ziplist code :

  1. Save the number of elements < 128
  2. The length of all saved elements < 64 byte

If the above two conditions cannot be met, use skiplist and dict Combined data structures .

Again , The above two conditions can also be modified in the configuration file , The corresponding fields are zset-max-ziplist-entries and zset-max-ziplist-value.

Code cloud warehouse synchronization notes , You can take it yourself. Welcome star correct :https://gitee.com/noblegasesgoo/notes

 If something goes wrong, I hope the leaders in the comment area can discuss and correct each other , Maintain the health of the community. Let's work together , There is no tolerance for wrong knowledge .
										——————————————————————  Love you  noblegasesgoo
原网站

版权声明
本文为[Noblegasesgoo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011445575767.html