quicklist是一种双链表,每个节点是一个ziplist,quicklist.h中提供的结构定义如下:
typedef struct quicklistNode {
struct quicklistNode *prev;
struct quicklistNode *next;
unsigned char *zl;
unsigned int sz; /* ziplist size in bytes */
unsigned int count : 16; /* count of items in ziplist */
unsigned int encoding : 2; /* RAW==1 or LZF==2 */
unsigned int container : 2; /* NONE==1 or ZIPLIST==2 */
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;
typedef struct quicklistLZF {
unsigned int sz; /* LZF size in bytes*/
char compressed[];
} quicklistLZF;
typedef struct quicklist {
quicklistNode *head;
quicklistNode *tail;
unsigned long count; /* total count of all entries in all ziplists */
unsigned long len; /* number of quicklistNodes */
int fill : 16; /* fill factor for individual nodes */
unsigned int compress : 16; /* depth of end nodes not to compress;0=off */
} quicklist;
typedef struct quicklistIter {
const quicklist *quicklist;
quicklistNode *current;
unsigned char *zi;
long offset; /* offset in current ziplist */
int direction;
} quicklistIter;
typedef struct quicklistEntry {
const quicklist *quicklist;
quicklistNode *node;
unsigned char *zi;
unsigned char *value;
long long longval;
unsigned int sz;
int offset;
} quicklistEntry;
常用的有3个,quicklistNode是双链表的节点,描述一个ziplist,quicklist是双链表的头,包含节点数量和所有ziplist中的entry的数量,quicklistIter是迭代器
quicklist.c中提供的api:
quicklistCreate:创建一个quicklist,初始装填因子是-2
quicklistSetCompressDepth:设置一个quicklist的压缩的节点深度阈值
quicklistSetFill:设置一个quicklist的装填因子
quicklistSetOptions:是上面两个函数的封装,设置这两个值
quicklistNew:创建一个quicklist并设置depth和fill两个值
quicklistCreateNode:创建一个node
quicklistCount:返回一个quicklist中保存的所有entry数量
quicklistRelease:释放整个的quicklist
__quicklistCompressNode:对一个node执行压缩,zl指向压缩后的内存
quicklistCompressNode:若node未被压缩过,执行压缩
__quicklistDecompressNode:对一个node执行解压缩,zl指向解压缩的内存
quicklistDecompressNode:若node被压缩过,执行解压缩
quicklistDecompressNodeForUse:若一个节点被压缩过,执行解压缩,并设置不能被立刻压缩
quicklistGetLzf:获取一个压缩节点的内容
quicklistAllowsCompression:检查一个node是否可以执行压缩操作
__quicklistCompress:从quicklist的头和尾向中间压缩
quicklistCompress:压缩quicklist中的一个node
quicklistRecompressOnly:调用过quicklistDecompressNodeForUse之后,用此函数压缩节点
__quicklistInsertNode:插入一个节点
_quicklistInsertNodeBefore:在某个节点之后插入一个节点
_quicklistInsertNodeAfter:在某个节点之后插入一个节点
_quicklistNodeSizeMeetsOptimizationRequirement:根据优化等级判断是否需要优化
_quicklistNodeAllowInsert:判断一个quicklist是否允许插入
_quicklistNodeAllowMerge:判断两个quicklist是否允许合并
quicklistNodeUpdateSz:设置一个node的sz字段
quicklistPushHead:向quicklist的头节点的ziplist中插入一个entry
quicklistPushTail:向quicklist的尾节点的ziplist中插入一个entry
quicklistAppendZiplist:向quicklist尾插入一个node,挂接ziplist
quicklistAppendValuesFromZiplist:将一个ziplist的所有entry插入到quicklist的尾节点