在前面介绍压缩列表ziplist的时候我们提到过,zset内部有两种存储结构,一种是ziplist,另一种是跳跃列表skiplist。为了彻底理解zset的内部结构,我们就再来介绍一下skiplist。
skiplist介绍
顾名思义,skiplist本质上是一个有序的多维的list。我们先回顾一下一维列表是如何进行查找的。
如上图,我们要查找一个元素,就需要从头节点开始遍历,直到找到对应的节点或者是第一个大于要查找的元素的节点(没找到)。时间复杂度为O(N)。
这个查找效率是比较低的,如果我们把列表的某些节点拔高一层,例如把每两个节点中有一个节点变成两层。那么第二层的节点只有第一层的一半,查找效率也就会提高。
查找的步骤是从头节点的顶层开始,查到第一个大于指定元素的节点时,退回上一节点,在下一层继续查找。
例如我们要在上面的列表中查询16。
-
从头节点的最顶层开始,先到节点7。
-
7的下一个节点是39,大于16,因此我们退回到7
-
从7开始,在下一层继续查找,就可以找到16。
这个例子中遍历的节点不比一维列表少,但是当节点更多,查找的数字更大时,这种做法的优势就体现出来了。还是上面的例子,如果我们要查找的是39,那么只需要访问两个节点(7、39)就可以找到了。这比一维列表要减少一半的数量。
为了避免插入操作的时间复杂度是O(N),skiplist每层的数量不会严格按照2:1的比例,而是对每个要插入的元素随机一个层数。
随机层数的计算过程如下:
-
每个节点都有第一层
-
那么它有第二层的概率是p,有第三层的概率是p*p
-
不能超过最大层数
Redis中的实现是
1/* Returns a random level for the new skiplist node we are going to create. 2 * The return value of this function is between 1 and ZSKIPLIST_MAXLEVEL 3 * (both inclusive), with a powerlaw-alike distribution where higher 4 * levels are less likely to be returned. */ 5int zslRandomLevel(void) { 6 int level = 1; 7 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF)) 8 level += 1; 9 return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;10}
其中ZSKIPLIST_P的值是0.25,存在上一层的概率是1/4,也就是说相对于我们上面的例子更加扁平化一些。ZSKIPLIST_MAXLEVEL的值是64,即最高允许64层。
Redis中的skiplist
Redis中的skiplist是作为zset的一种内部存储结构
1/* ZSETs use a specialized version of Skiplists */ 2typedef struct zskiplistNode { 3 sds ele; 4 double score; 5 struct zskiplistNode *backward; 6 struct zskiplistLevel { 7 struct zskiplistNode *forward; 8 unsigned long span; 9 } level[];10} zskiplistNode;1112typedef struct zskiplist {13 struct zskiplistNode *header, *tail;14 unsigned long length;15 int level;16} zskiplist;1718typedef struct zset {19 dict *dict;20 zskiplist *zsl;21} zset;
可以看到zset是由一个hash和一个skiplist组成。
skiplist的结构包括头尾指针,长度和当前跳跃列表的层数。
而在zskiplistNode,也就是跳跃列表的节点中包括
-
ele,即节点存储的数据
-
节点的分数score
-
回溯指针是在第一层指向前一个节点的指针,也就是说Redis的skiplist第一层是一个双向列表
-
节点各层级的指针level[],每层对应一个指针forward,以及这个指针跨越了多少个节点span。span用于计算元素的排名
了解了zset和skiplist的结构之后,我们就来看一下zset的基本操作的实现。
插入过程
前面我们介绍压缩列表的插入过程的时候就有提到过skiplist的插入,在zsetAdd函数中,Redis对zset的编码方式进行了判断,分别处理skiplist和ziplist。ziplist的部分前文已经介绍过了,今天就来看一下skiplist的部分。
1if (zobj->encoding == OBJ_ENCODING_SKIPLIST) { 2 zset *zs = zobj->ptr; 3 zskiplistNode *znode; 4 dictEntry *de; 5 6 de = dictFind(zs->dict,ele); 7 if (de != NULL) { 8 /* NX? Return, same element already exists. */ 9 if (nx) {10 *flags |= ZADD_NOP;11 return 1;12 }13 curscore = *(double*)dictGetVal(de);1415 /* Prepare the score for the increment if needed. */16 if (incr) {17 score += curscore;18 if (isnan(score)) {19 *flags |= ZADD_NAN;20 return 0;21 }22 if (newscore) *newscore = score;23 }2425 /* Remove and re-insert when score changes. */26 if (score != curscore) {27 znode = zslUpdateScore(zs->zsl,curscore,ele,score);28 /* Note that we did not removed the original element from29 * the hash table representing the sorted set, so we just30 * update the score. */31 dictGetVal(de) = &znode->score; /* Update score ptr. */32 *flags |= ZADD_UPDATED;33 }34 return 1;35 } else if (!xx) {36 ele = sdsdup(ele);37 znode = zslInsert(zs->zsl,score,ele);38 serverAssert(dictAdd(zs->dict,ele,&znode->score) == DICT_OK);39 *flags |= ZADD_ADDED;40 if (newscore) *newscore = score;41 return 1;42 } else {43 *flags |= ZADD_NOP;44 return 1;45 }46}
首先是查找对应元素是否存在,如果存在并且没有参数NX,就记录下这个元素当前的分数。这里可以看出zset中的hash字典是用来根据元素获取分数的。
接着判断是不是要执行increment命令,如果是的话,就用当前分数加上指定分数,得到新的分数newscore。如果分数发生了变化,就调用zslUpdateScore函数,来更新skiplist中的节点,另外还要多一步操作来更新hash字典中的分数。
如果要插入的元素不存在,那么就直接调用zslInsert函数。
1zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele) { 2 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x; 3 unsigned int rank[ZSKIPLIST_MAXLEVEL]; 4 int i, level; 5 6 serverAssert(!isnan(score)); 7 x = zsl->header; 8 for (i = zsl->level-1; i >= 0; i--) { 9 /* store rank that is crossed to reach the insert position */10 rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];11 while (x->level[i].forward &&12 (x->level[i].forward->score < score ||13 (x->level[i].forward->score == score &&14 sdscmp(x->level[i].forward->ele,ele) < 0)))15 {16 rank[i] += x->level[i].span;17 x = x->level[i].forward;18 }19 update[i] = x;20 }21 /* we assume the element is not already inside, since we allow duplicated22 * scores, reinserting the same element should never happen since the23 * caller of zslInsert() should test in the hash table if the element is24 * already inside or not. */25 level = zslRandomLevel();26 if (level > zsl->level) {27 for (i = zsl->level; i < level; i++) {28 rank[i] = 0;29 update[i] = zsl->header;30 update[i]->level[i].span = zsl->length;31 }32 zsl->level = level;33 }34 x = zslCreateNode(level,score,ele);35 for (i = 0; i < level; i++) {36 x->level[i].forward = update[i]->level[i].forward;37 update[i]->level[i].forward = x;3839 /* update span covered by update[i] as x is inserted here */40 x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]);41 update[i]->level[i].span = (rank[0] - rank[i]) + 1;42 }4344 /* increment span for untouched levels */45 for (i = level; i < zsl->level; i++) {46 update[i]->level[i].span++;47 }4849 x->backward = (update[0] == zsl->header) ? NULL : update[0];50 if (x->level[0].forward)51 x->level[0].forward->backward = x;52 else53 zsl->tail = x;54 zsl->length++;55 return x;56}
函数一开始定义了两个数组,update数组用来存储搜索路径,rank数组用来存储节点跨度。
第一步操作是找出要插入节点的搜索路径,并且记录节点跨度数。
接着开始插入,先随机一个层数。如果随机出的层数大于当前的层数,就需要继续填充update和rank数组,并更新skiplist的最大层数。
然后调用zslCreateNode函数创建新的节点。
创建好节点后,就根据搜索路径数据提供的位置,从第一层开始,逐层插入节点(更新指针),并其他节点的span值。
最后还要更新回溯节点,以及将skiplist的长度加一。
这就是插入新元素的整个过程。
更新过程
了解了插入过程以后我们再回过头来看更新过程
1zskiplistNode *zslUpdateScore(zskiplist *zsl, double curscore, sds ele, double newscore) { 2 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x; 3 int i; 4 5 /* We need to seek to element to update to start: this is useful anyway, 6 * we'll have to update or remove it. */ 7 x = zsl->header; 8 for (i = zsl->level-1; i >= 0; i--) { 9 while (x->level[i].forward &&10 (x->level[i].forward->score < curscore ||11 (x->level[i].forward->score == curscore &&12 sdscmp(x->level[i].forward->ele,ele) < 0)))13 {14 x = x->level[i].forward;15 }16 update[i] = x;17 }1819 /* Jump to our element: note that this function assumes that the20 * element with the matching score exists. */21 x = x->level[0].forward;22 serverAssert(x && curscore == x->score && sdscmp(x->ele,ele) == 0);2324 /* If the node, after the score update, would be still exactly25 * at the same position, we can just update the score without26 * actually removing and re-inserting the element in the skiplist. */27 if ((x->backward == NULL || x->backward->score < newscore) &&28 (x->level[0].forward == NULL || x->level[0].forward->score > newscore))29 {30 x->score = newscore;31 return x;32 }3334 /* No way to reuse the old node: we need to remove and insert a new35 * one at a different place. */36 zslDeleteNode(zsl, x, update);37 zskiplistNode *newnode = zslInsert(zsl,newscore,x->ele);38 /* We reused the old node x->ele SDS string, free the node now39 * since zslInsert created a new one. */40 x->ele = NULL;41 zslFreeNode(x);42 return newnode;43}
和插入过程一样,先保存了搜索路径。并且定位到要更新的节点,如果更新后节点位置不变,则直接返回。否则,就要先调用zslDeleteNode函数删除该节点,再插入新的节点。
删除过程
Redis中skiplist的更新过程还是比较容易理解的,就是先删除再插入,那么我们接下来就看看它是如何删除节点的。
1void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) { 2 int i; 3 for (i = 0; i < zsl->level; i++) { 4 if (update[i]->level[i].forward == x) { 5 update[i]->level[i].span += x->level[i].span - 1; 6 update[i]->level[i].forward = x->level[i].forward; 7 } else { 8 update[i]->level[i].span -= 1; 9 }10 }11 if (x->level[0].forward) {12 x->level[0].forward->backward = x->backward;13 } else {14 zsl->tail = x->backward;15 }16 while(zsl->level > 1 && zsl->header->level[zsl->level-1].forward == NULL)17 zsl->level--;18 zsl->length--;19}
删除过程的代码也比较容易理解,首先按照搜索路径,从下到上,逐层更新前向指针。然后更新回溯指针。如果删除节点的层数是最大的层数,那么还需要更新skiplist的level字段。最后长度减一。
总结
skiplist是节点有层级的list,节点的查找过程可以跨越多个节点,从而节省查找时间。
Redis的zset由hash字典和skiplist组成,hash字典负责数据到分数的对应,skiplist负责根据分数查找数据。
Redis中skiplist插入和删除操作都依赖于搜索路径,更新操作是先删除再插入。
推荐阅读
Skip Lists: A Probabilistic Alternative to Balanced Trees(后台回复skiplist获取pdf)
《Redis 深度历险:核心原理与应用实践》