redis ziplist

ziplist中的代码多一些,其实不用倒不用仔细看代码,因为这是一种紧密编码格式的双向链表。

我们了解下它的编码方式和接口就可以了。关于它的编码方式,ziplist.c开头的注释很详细。

ziplist是一种内存高效的特殊编码的双向链表,它可以保存字符串和整数。在两边做push和pop操作都只要O(1)时间(注:这种说法把realloc和memcpy都当作O(1)了)。但是每个操作都要relalloc,所以真实的复杂度取决于ziplist占用的内存大小。当中的整数全部采用little endian编码。

ziplist基本结构为<zlbytes><zltail><zllen><entry><entry><zlend>

zlbytes为占用的总字节数,便于resize;

zltail为尾部的entry,便于从后往前遍历;

zlend为0xff。

每个entry包含上个entry的字节数,便于从后往前遍历。另外包含了存储单位(字符串或整数)的长度和数据,具体是这样的,哈哈:

 * |00pppppp| - 1 byte
 *      String value with length less than or equal to 63 bytes (6 bits).
 * |01pppppp|qqqqqqqq| - 2 bytes
 *      String value with length less than or equal to 16383 bytes (14 bits).
 * |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
 *      String value with length greater than or equal to 16384 bytes.
 * |11000000| - 1 byte
 *      Integer encoded as int16_t (2 bytes).
 * |11010000| - 1 byte
 *      Integer encoded as int32_t (4 bytes).
 * |11100000| - 1 byte
 *      Integer encoded as int64_t (8 bytes).
 * |11110000| - 1 byte
 *      Integer encoded as 24 bit signed (3 bytes).
 * |11111110| - 1 byte
 *      Integer encoded as 8 bit signed (1 byte).
 * |1111xxxx| - (with xxxx between 0000 and 1101) immediate 4 bit integer.
 *      Unsigned integer from 0 to 12. The encoded value is actually from
 *      1 to 13 because 0000 and 1111 can not be used, so 1 should be
 *      subtracted from the encoded 4 bit value to obtain the right value.
 * |11111111| - End of ziplist.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值