You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here are the different sections of the [RDB file](https://rdb.fnordig.de/file_format.html), in order:
38
+
+ Header section
39
+
+ Metadata section
40
+
+ Database section
41
+
+ End of file section
42
+
#### Header section
43
+
start with some magic number
44
+
```sh
45
+
52 45 44 49 53 30 30 31 31 // Magic string + version number (ASCII): "REDIS0011".
46
+
```
47
+
#### Metadata section
48
+
contains zero or more "metadata subsections", which each specify a single metadata attribute
49
+
e.g.
50
+
```sh
51
+
FA // Indicates the start of a metadata subsection.
52
+
09 72 65 64 69 73 2D 76 65 72 // The name of the metadata attribute (string encoded): "redis-ver".
53
+
06 36 2E 30 2E 31 36 // The value of the metadata attribute (string encoded): "6.0.16".
54
+
```
55
+
#### Database section
56
+
contains zero or more "database subsections," which each describe a single database.
57
+
e.g.
58
+
```sh
59
+
FE // Indicates the start of a database subsection.
60
+
00 /* The index of the database (size encoded). Here, the index is 0. */
61
+
62
+
FB // Indicates that hash table size information follows.
63
+
03 /* The size of the hash table that stores the keys and values (size encoded). Here, the total key-value hash table size is 3. */
64
+
02 /* The size of the hash table that stores the expires of the keys (size encoded). Here, the number of keys with an expiry is 2. */
25
65
```
26
66
27
67
```sh
28
-
redis-cli SET foo bar
68
+
00 /* The 1-byte flag that specifies the value’s type and encoding. Here, the flag is 0, which means "string."*/
69
+
06 66 6F 6F 62 61 72 // The name of the key (string encoded). Here, it's "foobar".
70
+
06 62 61 7A 71 75 78 // The value (string encoded). Here, it's "bazqux".
29
71
```
30
72
31
73
```sh
32
-
redis-cli SET foo bar px 100
74
+
FC /* Indicates that this key ("foo") has an expire, and that the expire timestamp is expressed in milliseconds. */
75
+
15 72 E7 07 8F 01 00 00 /* The expire timestamp, expressed in Unix time, stored as an 8-byte unsigned long, in little-endian (read right-to-left). Here, the expire timestamp is 1713824559637. */
76
+
00 // Value type is string.
77
+
03 66 6F 6F // Key name is "foo".
78
+
03 62 61 72 // Value is "bar".
33
79
```
34
80
35
81
```sh
36
-
redis-cli GET foo
82
+
FD /* Indicates that this key ("baz") has an expire, and that the expire timestamp is expressed in seconds. */
83
+
52 ED 2A 66 /* The expire timestamp, expressed in Unix time, stored as an 4-byte unsigned integer, in little-endian (read right-to-left). Here, the expire timestamp is 1714089298. */
84
+
00 // Value type is string.
85
+
03 62 61 7A // Key name is "baz".
86
+
03 71 75 78 // Value is "qux".
37
87
```
38
88
89
+
In summary,
90
+
- Optional expire information (one of the following):
91
+
- Timestamp in seconds:
92
+
- FD
93
+
- Expire timestamp in seconds (4-byte unsigned integer)
94
+
- Timestamp in milliseconds:
95
+
- FC
96
+
- Expire timestamp in milliseconds (8-byte unsigned long)
97
+
- Value type (1-byte flag)
98
+
- Key (string encoded)
99
+
- Value (encoding depends on value type)
39
100
101
+
#### End of file section
102
+
```sh
103
+
FF /* Indicates that the file is ending, and that the checksum follows. */
104
+
89 3b b7 4e f8 0f 77 19 // An 8-byte CRC64 checksum of the entire file.
105
+
```
40
106
41
-
## RDB Persistence
42
-
Get Redis-rs server config
107
+
#### Size encoding
43
108
```sh
44
-
redis-cli CONFIG GET dbfilename
109
+
/* If the first two bits are 0b00:
110
+
The size is the remaining 6 bits of the byte.
111
+
In this example, the size is 10: */
112
+
0A
113
+
00001010
114
+
115
+
/* If the first two bits are 0b01:
116
+
The size is the next 14 bits
117
+
(remaining 6 bits in the first byte, combined with the next byte),
118
+
in big-endian (read left-to-right).
119
+
In this example, the size is 700: */
120
+
42 BC
121
+
01000010 10111100
122
+
123
+
/* If the first two bits are 0b10:
124
+
Ignore the remaining 6 bits of the first byte.
125
+
The size is the next 4 bytes, in big-endian (read left-to-right).
126
+
In this example, the size is 17000: */
127
+
80 00 00 42 68
128
+
10000000 00000000 00000000 01000010 01101000
129
+
130
+
/* If the first two bits are 0b11:
131
+
The remaining 6 bits specify a type of string encoding.
132
+
See string encoding section. */
133
+
```
134
+
135
+
#### String encoding
136
+
+ The size of the string (size encoded).
137
+
+ The string.
138
+
```sh
139
+
/* The 0x0D size specifies that the string is 13 characters long. The remaining characters spell out "Hello, World!". */
140
+
0D 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
141
+
```
142
+
For sizes that begin with 0b11, the remaining 6 bits indicate a type of string format:
143
+
```sh
144
+
/* The 0xC0 size indicates the string is an 8-bit integer. In this example, the string is "123". */
145
+
C0 7B
146
+
147
+
/* The 0xC1 size indicates the string is a 16-bit integer. The remaining bytes are in little-endian (read right-to-left). In this example, the string is "12345". */
148
+
C1 39 30
149
+
150
+
/* The 0xC2 size indicates the string is a 32-bit integer. The remaining bytes are in little-endian (read right-to-left), In this example, the string is "1234567". */
151
+
C2 87 D6 12 00
152
+
153
+
/* The 0xC3 size indicates that the string is compressed with the LZF algorithm. You will not encounter LZF-compressed strings in this challenge. */
0 commit comments