Then the previous article said, here we continue to analyze the Rdb file storage structure, first of all know that the Rdb file is in the "snapshot" mode of Redis will be generated, then if
We understand the structure of the Rdb file, is not let us to "snapshot" mode can do a very aware of it???
One: Analysis of RDB structure
First of all, we have a conceptual understanding of the Rdb file, such as the following picture:
, we probably saw a brief storage mode for the Rdb file, but for a better comparison, I'm going to save an empty database and compare it to see the effect:
Then we open the Dump.rdb file with Winhex and look at its 16 binary.
OK, the open I have opened, below we hit comparison.
1. Redis parameters: You can see in the first 5 bytes of the 16 binary, is the "REDIS" five characters, the role of this is obvious, it must be judged whether the current file is "RDB
File ", so that it is convenient to use constant time to distinguish ...
2. Db_version: After the Redis character, we see 4 bytes of 0006, which is db_version in the RDB file structure diagram. Yes, it's also very simple,
is to determine the current Redis version number, yes No???
3. Database: Because I am demonstrating an empty database, naturally there is no corresponding structure, wait for us to insert the record, and then compare.
4. EOF: From the winhex above whether you see, it occupies a byte of space, is a "Y" added two points, because the Unicode can not be represented, so there is a
Garbled, of course, 16 binary can be identified, is called "FF", see no??? Then its role is to identify the end of the database.
5. Checksum: From the name you can see, it is a checksum, the principle of course is to see if the file is corrupted, or whether it is modified, so it is a bit like the current OAuth authentication,
Yes, it takes up 8 bytes, which is the last: DC B3 F0 5A DC F2 56 ...
Two: The RDB file structure with data demonstration
Well, above I have shown all the parameters except database, let's set one of the simplest string types to see if the database structure ...
Open the Dump.rdb file with Winhex as follows:
In order to facilitate the comparison, I marked in the diagram the database start position, that is, the hexadecimal FE ...
1. Database [SELECTDB]: As you can see, Selectdb is actually a byte that cannot be marked with Unicode, hexadecimal is Fe, when Redis touches this character
When you know what to do .... To be ready to execute the SELECT command ...
2. Database[db_number]: After Fe, we see the 16 binary "03", that is, to switch to a third database, remember? When I was in the set data,
Once executed the Select 3, that is, the data set to the 3rd database, if you forget, it's OK, I use the Redis client open to you see ~ ~
3. Database[pairs][type]: When you know the Select database, the next operation is how to analyze the Key,value data, in Key/value data, the first
is type, in fact this type is your value of the encoding type, you can see in the Winhex 0, that is, the following source code:
4. Database[pairs][key][len]: After the type is called the key, and the combination of key mode is "Len,value", where Len is the length of the key, you can also see that
The Winhex represents "04", meaning the length of name is 4. Right...
5. Database[pairs][key][value] Similarly, the pattern here is also "Len,value", preceded by the value of length, followed by the value of the specific values ...
Well, probably said so much, hope to help you ...
Merry Christmas ~~~~~ ~~~~~
15-Day redis--11th chapter gives you a thorough understanding of the RDB storage structure