Redis底层数据结构之quicklist-三、quicklistNode结构

时间:2024-04-23 08:55:52

基于listpack(V6.2)

/* quicklistNode is a 32 byte struct describing a listpack for a quicklist.
* We use bit fields keep the quicklistNode at 32 bytes.
* count: 16 bits, max 65536 (max lp bytes is 65k, so max count actually < 32k).
* encoding: 2 bits, RAW=1, LZF=2.
* container: 2 bits, PLAIN=1 (a single item as char array), PACKED=2 (listpack with multiple items).
* recompress: 1 bit, bool, true if node is temporary decompressed for usage.
* attempted_compress: 1 bit, boolean, used for verifying during testing.
* dont_compress: 1 bit, boolean, used for preventing compression of entry.
* extra: 9 bits, free for future use; pads out the remainder of 32 bits */
typedef struct quicklistNode {
   struct quicklistNode *prev;
   struct quicklistNode *next;
   unsigned char *entry;
   size_t sz;             /* entry size in bytes */
   unsigned int count : 16;     /* count of items in listpack */
   unsigned int encoding : 2;   /* RAW==1 or LZF==2 */
   unsigned int container : 2;  /* PLAIN==1 or PACKED==2 */
   unsigned int recompress : 1; /* was this node previous compressed? */
   unsigned int attempted_compress : 1; /* node can't compress; too small */
   unsigned int dont_compress : 1; /* prevent compression of entry that will be used later */
   unsigned int extra : 9; /* more bits to steal for future usage */
} quicklistNode;

基于ziplist

typedef struct quicklistNode {
   // 前一个节点(ziplist)指针
   struct quicklistNode *prev;
   // 后一个节点(ziplist)指针
   struct quicklistNode *next;
   // 当前节点ziplist指针
   unsigned char *zl;
   // 当前节点ziplist的字节大小,即zlbytes
   unsigned int sz;             /* ziplist size in bytes */
   // 当前节点ziplist中entry的数量
   unsigned int count : 16;     /* count of items in ziplist */
   // 编码方式:1-ziplist; 2-lzf压缩模式
   unsigned int encoding : 2;   /* RAW==1 or LZF==2 */
   // 数据容器类型:1-其他(预留扩展类型);2-ziplist
   unsigned int container : 2;  /* NONE==1 or ZIPLIST==2 */
   // 是否被压缩:1-说明被解压,将来要重新压缩。
   unsigned int recompress : 1; /* was this node previous compressed? */
   // 测试字段
   unsigned int attempted_compress : 1; /* node can't compress; too small */
   // 预留字段
   unsigned int extra : 10; /* more bits to steal for future usage */
} quicklistNode;