思想
在redis所中通常的压缩方法是通过位之间的紧密排列来实现的,
对数据的掌控精确到bit
ziplist结构说明,按顺序排列
| 名称 | 类型 | 说明 |
---|
zlbytes | uint32_t | 存储整//个ziplist占用的字节数 |
zltail | uint32_t | 表结尾偏移量 |
zllen | uint16_t | 节点的数目 |
entry | —— | 内容(节点) |
…. | —— | 内容(节点) |
entry | —— | 内容(节点) |
zlend | uint8_t | 表结构结束标志,为255常量 |
entry结构
| prev_entry_bytes_length | encoding | content |
---|
prev_entry_bytes_length
若前一个节点长度小于254字节,prev_entry_bytes_length只占一个字节,否则占5个字节,第一个字节被设为254,其余4个字节存储节点的长度
encoding
encoding 保存当前content的数据类型和长度
整数类型编码
开头为11表示存储的是整数,接下来的位表明数字类型
| 编码 | 长度 | content类型 |
---|
11000000 | 1 byte | int16_t (2 bytes) |
11010000 | 1 byte | int32_t (4 bytes) |
11100000 | 1 byte | int64_t (8 bytes) |
11110000 | 1 byte | Integer 24 bit signed (3 bytes) |
11111110 | 1 byte | Integer 8 bit signed (1 byte) |
1111xxxx(0000~1101) | 4 bits | 4 bit integer |
字符串类型编码
除去开头两位其他表示字符串长度
| 编码 | 长度 | 字符串长度 |
---|
00pppppp | 1 byte | 小于等于63 bytes (6 bits) |
01ppppppqqqqqqqq | 2 bytes | 小于等于16383 bytes (14 bits) |
10______qqqqqqqqrrrrrrrrsssssssstttttttt | 5 bytes | 大于等于 16384 bytes |