/* The ziplist is a specially encoded dually linked list that is designed
* to be very memory efficient. It stores both strings
and
integer values,
* where integers are encoded
as
actual integers instead of a series of
* characters. It allows push
and
pop operations on either side of the list
* in O(1) time. However, because every operation requires a reallocation of
* the memory used by the ziplist, the actual complexity is related to the
* amount of memory used by the ziplist.
*
* ziplist是一个编码后的列表,特殊的设计使得内存操作非常有效率,此列表可以同时存放
* 字符串和整数类型,列表可以在头尾各边支持推加和弹出操作在O(1)常量时间,但是,因为每次
* 操作设计到内存的重新分配释放,所以加大了操作的复杂性
* ----------------------------------------------------------------------------
*
* ziplist的结构组成:
* ZIPLIST OVERALL LAYOUT:
* The general layout of the ziplist is
as
follows:
* <zlbytes><zltail><zllen><entry><entry><zlend>
*
* <zlbytes> is an unsigned integer to hold the number of bytes that the
* ziplist occupies. This value needs to be stored to be able to resize the
* entire structure without the need to traverse it first.
* <zipbytes>代表着ziplist占有的字节数,这方便当重新调整大小的时候不需要重新从头遍历
*
* <zltail> is the offset to the last entry in the list. This allows a pop
* operation on the far side of the list without the need
for
full traversal.
* <zltail>记录了最后一个entry的位置在列表中,可以方便快速在列表末尾弹出操作
*
* <zllen> is the number of entries.When this value is larger than 2**16-2,
* we need to traverse the entire list to know how many items it holds.
* <zllen>记录的是ziplist里面entry数据结点的总数
*
* <zlend> is a single byte special value, equal to 255, which indicates the
*
end
of the list.
* <zlend>代表的是结束标识别,用单字节表示,值是255,就是11111111
*
* ZIPLIST ENTRIES:
* Every entry in the ziplist is prefixed by a header that contains two pieces
* of information. First, the length of the previous entry is stored to be
* able to traverse the list from back to front. Second, the encoding with an
* optional string length of the entry itself is stored.
* 每个entry数据结点主要包含2部分信息,第一个,上一个结点的长度,主要就可以可以从任意结点从后往前遍历整个列表
* 第二个,编码字符串的方式的类型保存
*
* The length of the previous entry is encoded in the following way:
* If this length is smaller than 254 bytes, it will only consume a single
* byte that takes the length
as
value. When the length is greater than
or
* equal to 254, it will consume 5 bytes. The first byte is set to 254 to
* indicate a larger value is following. The remaining 4 bytes take the
* length of the previous entry
as
value.
* 之前的数据结点的字符串长度的长度少于254个字节,他将消耗单个字节,一个字节8位,最大可表示长度为2的8次方
* 当字符串的长度大于254个字节,则用5个字节表示,第一个字节被设置成254,其余的4个字节占据的长度为之前的数据结点的长度
*
* The other header field of the entry itself depends on the contents of the
* entry. When the entry is a string, the first 2 bits of this header will hold
* the type of encoding used to store the length of the string, followed by the
* actual length of the string. When the entry is an integer the first 2 bits
* are both set to 1. The following 2 bits are used to specify what kind of
* integer will be stored after this header. An overview of the different
* types
and
encodings is
as
follows:
* 头部信息中的另一个值记录着编码的方式,当编码的是字符串,头部的前2位为00,01,10共3种
* 如果编码的是整型数字的时候,则头部的前2位为11,代表的是整数编码,后面2位代表什么类型整型值将会在头部后面被编码
* 00-int16_t, 01-int32_t, 10-int64_t, 11-24 bit signed,还有比较特殊的2个,11111110-8 bit signed,
* 1111 0000 - 1111 1101,代表的是整型值0-12,头尾都已经存在,都不能使用,与传统的通过固定的指针表示长度,这么做的好处实现
* 可以更合理的分配内存
*
* String字符串编码的3种形式
* |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.
*
* All the integers are represented in little endian byte order.
*
* ----------------------------------------------------------------------------