xie233 / redis_some

0 stars 0 forks source link

SkipList跳跃表阅读 #1

Open xie233 opened 5 years ago

xie233 commented 5 years ago

跳跃表主要用在有序集合中,和字典一起为zset提供常数查找,有序插入O(logN). 下面分别是有序集合,跳跃表和跳跃表节点的结构

typedef struct zset {
    dict *dict;
    zskiplist *zsl;
} zset;

typedef struct zskiplist {
    struct zskiplistNode *header, *tail;
    PORT_ULONG length;
    int level;
} zskiplist;

typedef struct zskiplistNode {
    robj *obj;
    double score;
    struct zskiplistNode *backward;
    struct zskiplistLevel {
        struct zskiplistNode *forward;
        unsigned int span;
    } level[];
} zskiplistNode;

在 redis3.0 中 c_zset.c 文件跳跃表插入节点的实现,ZSKIPLIST_MAXLEVEL层(32层)


zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj) {
    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
//记录每层小于score,或者等于score但相应的字符串小的节点,插入节点的更新路径(每一层的上一个节点),插入节点需要修改每一层这个节点的上一个节点的信息(跨度),
    unsigned int rank[ZSKIPLIST_MAXLEVEL];
//记录每一层插入节点的上一个节点在skiplist中的排名
    int i, level; 
 //索引,层数
    serverAssert(!isnan(score));
    x = zsl->header;
//x用于迭代zskiplistNode
    for (i = zsl->level-1; i >= 0; i--) {
        /* store rank that is crossed to reach the insert position */
        rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
        while (x->level[i].forward &&//短路判断
            (x->level[i].forward->score < score ||
                (x->level[i].forward->score == score &&
                compareStringObjects(x->level[i].forward->obj,obj) < 0))) {
            rank[i] += x->level[i].span;
//层越往下,越接近score,排名也在增大
            x = x->level[i].forward;
        }
        update[i] = x;
//当前层的最后一个小于score,或者等于score但相应的字符串小的节点
    }
    /* we assume the key is not already inside, since we allow duplicated
     * scores, and the re-insertion of score and redis object should never
     * happen since the caller of zslInsert() should test in the hash table
     * if the element is already inside or not. */
    level = zslRandomLevel();
//给插入节点一个随机层数(1/4)的上升几率
    if (level > zsl->level) {
//
        for (i = zsl->level; i < level; i++) {
            rank[i] = 0;
            update[i] = zsl->header;
            update[i]->level[i].span = zsl->length;
//高出部分,只有头结点有forward指针
        }
        zsl->level = level;
    }
    x = zslCreateNode(level,score,obj);
    for (i = 0; i < level; i++) {
        x->level[i].forward = update[i]->level[i].forward;
        update[i]->level[i].forward = x;

        /* update span covered by update[i] as x is inserted here */
//更新跨度,需要用的rank数组,x在i层上一个节点的排名

        x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]);
        update[i]->level[i].span = (rank[0] - rank[i]) + 1;
//很明显了,i层的上一个节点到x的跨度等于    i层的上一个节点到0层的上一个节点的跨度再加1(0层上一个节点到x的跨度)
    }

    /* increment span for untouched levels */
    for (i = level; i < zsl->level; i++) {
        update[i]->level[i].span++;
    }

    x->backward = (update[0] == zsl->header) ? NULL : update[0];
    if (x->level[0].forward)
        x->level[0].forward->backward = x;
    else
        zsl->tail = x;
    zsl->length++;
    return x;
}
xie233 commented 5 years ago

c1

对照源码,在跳跃表中插入8的过程,红色的是表示跨度,蓝色插入过程,rank数组保存第i层x的上一个节点的排名,update数组表示了更新路径,即每一层的x的上一个节点,header->p2->p2>p3>p3 update

插入8之后: c2