标签:gobject may 另一个 level margin 包含 use 来源 store
跳跃表是一种有序数据结构,他是通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的。在的插入和删除都可以在O(lgN)时间复杂度内搞定
Redis在两个地方用到跳跃表,一个是实现有序集合键,另一个是在集群节点中用作内部数据结构。
1.跳跃表的实现
/* ZSETs use a specialized version of Skiplists */ typedef struct zskiplistNode { robj *obj; //分值 double score; //后退指针 struct zskiplistNode *backward; //层 struct zskiplistLevel { //前进指针 struct zskiplistNode *forward; //跨度 unsigned int span; } level[]; } zskiplistNode; typedef struct zskiplist { struct zskiplistNode *header, *tail; unsigned long length; //记录跳跃表内,层数最大的节点的层数 int level; //记录跳跃表的长度 也就是目前包含节点的数量 } zskiplist;
跳跃表的结构如图:图片来源
跳跃表节点每个元素都包含一个指向其他节点的指针,程序可以通过这些层来加快访问其他节点的速度,并且层数越多,访问节点的速度就越快。
每次创建一个新跳跃表节点的时候,程序都会根据幂次定律(越大的数出现的概率越小)随机生成一个1~32之间的值作为level。
2.跳跃表的实现
1.跳跃表的结构
/* ZSETs use a specialized version of Skiplists */ typedef struct zskiplistNode { robj *obj; //分值 double score; //后退指针 struct zskiplistNode *backward; //层 struct zskiplistLevel { //前进指针 struct zskiplistNode *forward; //跨度 unsigned int span; } level[]; } zskiplistNode; typedef struct zskiplist { struct zskiplistNode *header, *tail; unsigned long length; //记录跳跃表内,层数最大的节点的层数 int level; //记录跳跃表的长度 也就是目前包含节点的数量 } zskiplist;
2.跳跃表的创建
//创建一个跳表的节点 zskiplistNode *zslCreateNode(int level, double score, robj *obj) { zskiplistNode *zn = zmalloc(sizeof(*zn)+level*sizeof(struct zskiplistLevel)); zn->score = score; zn->obj = obj; return zn; } zskiplist *zslCreate(void) { int j; //表头 zskiplist *zsl; zsl = zmalloc(sizeof(*zsl)); zsl->level = 1; //初始化节点层数为1 zsl->length = 0; //初始化节点数为0 //ZSKIPLIST_MAXLEVEL = 32 头节点默认32层 所有节点层数不能超过32 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL); //初始化 表头的32个level层 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) { zsl->header->level[j].forward = NULL; //初始化头节点层结构数组 zsl->header->level[j].span = 0; } zsl->header->backward = NULL; zsl->tail = NULL; return zsl; }
3.跳跃表的插入
//插入元素 zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj) { zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x; //update节点数组 保存插入节点每一层的前驱节点 unsigned int rank[ZSKIPLIST_MAXLEVEL]; //rank 用于保存 插入位置在每一层中跨过的节点数 int i, level; redisAssert(!isnan(score)); x = zsl->header; //设置头节点 for (i = zsl->level-1; i >= 0; i--) { /* store rank that is crossed to reach the insert position */ //如果是最高层 则表示扩过0个节点 否则 记录跨过的节点数 与上一层相同 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; x = x->level[i].forward; } update[i] = x; //记录第i层的前驱节点 } /* 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(); //随机生成层数 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; } zsl->level = level; } x = zslCreateNode(level,score,obj); //生成x node for (i = 0; i < level; i++) { //插入node节点 并更新前驱节点和跨度 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 */ x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]); update[i]->level[i].span = (rank[0] - rank[i]) + 1; } /* 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; }
4.跳跃表的删除
//逐层遍历 删除x节点 update 记录x节点每一层的前驱节点 void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) { int i; for (i = 0; i < zsl->level; i++) { if (update[i]->level[i].forward == x) { update[i]->level[i].span += x->level[i].span - 1; update[i]->level[i].forward = x->level[i].forward; } else { update[i]->level[i].span -= 1; } } if (x->level[0].forward) { x->level[0].forward->backward = x->backward; } else { zsl->tail = x->backward; } while(zsl->level > 1 && zsl->header->level[zsl->level-1].forward == NULL) zsl->level--; zsl->length--; } /* Delete an element with matching score/object from the skiplist. */ int zslDelete(zskiplist *zsl, double score, robj *obj) { zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x; int i; x = zsl->header; for (i = zsl->level-1; i >= 0; i--) { 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))) x = x->level[i].forward; update[i] = x; //记录节点的前驱节点 } /* We may have multiple elements with the same score, what we need * is to find the element with both the right score and object. */ x = x->level[0].forward; if (x && score == x->score && equalStringObjects(x->obj,obj)) { zslDeleteNode(zsl, x, update); zslFreeNode(x); return 1; } return 0; /* not found */ }
5.跳跃表查找:会给定一个range,然后在跳跃表中查找相应的值,可用于查找删除操作。
range的数据结构
/* Struct to hold a inclusive/exclusive range spec by score comparison. */ typedef struct { double min, max; //最大值 最小值 int minex, maxex; /* are min or max exclusive? */ //是否为开闭空间 1为开 0为闭 } zrangespec; /* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */ //一个字典顺序的范围 typedef struct { robj *min, *max; /* May be set to shared.(minstring|maxstring) */ int minex, maxex; /* are min or max exclusive? */ } zlexrangespec;
例如返回区间的最小值
static int zslValueGteMin(double value, zrangespec *spec) { return spec->minex ? (value > spec->min) : (value >= spec->min); } static int zslValueLteMax(double value, zrangespec *spec) { return spec->maxex ? (value < spec->max) : (value <= spec->max); } /* Returns if there is a part of the zset is in range. */ //判断是否有(range->min,range->max)在哈希表中 int zslIsInRange(zskiplist *zsl, zrangespec *range) { zskiplistNode *x; /* Test for ranges that will always be empty. */ if (range->min > range->max || (range->min == range->max && (range->minex || range->maxex))) return 0; x = zsl->tail; if (x == NULL || !zslValueGteMin(x->score,range)) return 0; x = zsl->header->level[0].forward; if (x == NULL || !zslValueLteMax(x->score,range)) return 0; return 1; } /* Find the first node that is contained in the specified range. * Returns NULL when no element is contained in the range. */ //返回满足区间最小值 zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range) { zskiplistNode *x; int i; /* If everything is out of range, return early. */ if (!zslIsInRange(zsl,range)) return NULL; x = zsl->header; for (i = zsl->level-1; i >= 0; i--) { /* Go forward while *OUT* of range. */ while (x->level[i].forward && !zslValueGteMin(x->level[i].forward->score,range)) x = x->level[i].forward; } /* This is an inner range, so the next node cannot be NULL. */ x = x->level[0].forward; redisAssert(x != NULL); /* Check if score <= max. */ if (!zslValueLteMax(x->score,range)) return NULL; return x; }
标签:gobject may 另一个 level margin 包含 use 来源 store
原文地址:https://www.cnblogs.com/chenyang920/p/13166822.html