redis中並沒有專門給跳躍表兩個文件。在5.0.7的版本中,結構體的聲明與定義、介面的聲明在server.h中,介面的定義在t_zset.c中,所有開頭為zsl的函數。 一、數據結構 單個節點: typedef struct zskiplistNode { //key,唯一 sds ele; // ...
redis中並沒有專門給跳躍表兩個文件。在5.0.7的版本中,結構體的聲明與定義、介面的聲明在server.h中,介面的定義在t_zset.c中,所有開頭為zsl的函數。
一、數據結構
單個節點:
typedef struct zskiplistNode { //key,唯一 sds ele; //分值,可重覆 double score; //後退指針 struct zskiplistNode *backward; //層 struct zskiplistLevel { //前進指針 struct zskiplistNode *forward; //到本層下一節點的跨度,用於計算rank unsigned long span; } level[]; } zskiplistNode;
zskiplistNode定義了跳躍表中每個節點的數據結構,它是一個變長結構體。
1 /* 2 +------------------------+ 3 |sds ele | /+-----------------------------+ 4 +------------------------+ / |struct zskiplistNode *forward| 5 |double score | / +-----------------------------+ 6 +------------------------+ / |unsigned long span | 7 |zskiplistNode * backward| / +-----------------------------+ 8 +------------------------+/ . . 9 |zskiplistLevel level[] | . . 10 +------------------------+\ . . 11 \ +-----------------------------+ 12 \ |struct zskiplistNode *forward| 13 \ +-----------------------------+ 14 \ |unsigned long span | 15 \+-----------------------------+ 16 */
將用以下結構表示:
1 /* 2 +--------+ 3 |level[1]| 4 |1(span) | 5 +--------+ 6 |level[0]| 7 |1(span) | 8 +--------+ 9 |backward| 10 +--------+ 11 |score | 12 +--------+ 13 |ele | 14 +--------+ 15 */
如:
1 /* 2 +--------+ +--------+ +--------+ 3 |level[1]|--------------->|level[1]|--------------->|level[1]| 4 |2 | |2 | |0 | 5 +--------+ +--------+ +--------+ +--------+ +--------+ 6 |level[0]|-->|level[0]|-->|level[0]|-->|level[0]|-->|level[0]| 7 |1 | |1 | |1 | |1 | |0 | 8 +--------+ +--------+ +--------+ +--------+ +--------+ 9 |backward|<--|backward|<--|backward|<--|backward|<--|backward| 10 +--------+ +--------+ +--------+ +--------+ +--------+ 11 |1 | |2 | |3 | |4 | |5 | 12 +--------+ +--------+ +--------+ +--------+ +--------+ 13 |a | |b | |c | |d | |e | 14 +--------+ +--------+ +--------+ +--------+ +--------+ 15 */
跳錶:
1 typedef struct zskiplist { 2 //頭/尾節點 3 struct zskiplistNode *header, *tail; 4 //總長度 5 unsigned long length; 6 //總層數 7 int level; 8 } zskiplist;
因其頭節點固定為空節點,固整體結構:
1 /* 2 +--------+ +--------+ +--------+ 3 |level[1]|--------------->|level[1]|--------------->|level[1]| 4 |2 | |2 | |0 | 5 +--------+ +--------+ +--------+ +--------+ +--------+ 6 |level[0]|-->|level[0]|-->|level[0]|-->|level[0]|-->|level[0]| 7 |1 | |1 | |1 | |1 | |0 | 8 +--------+ +--------+ +--------+ +--------+ +--------+ 9 |backward|<--|backward|<--|backward|<--|backward|<--|backward| 10 +--------+ +--------+ +--------+ +--------+ +--------+ 11 |0 | |2 | |3 | |4 | |5 | 12 +--------+ +--------+ +--------+ +--------+ +--------+ 13 |NULL | |b | |c | |d | |e | 14 +-->+--------+ +--------+ +--------+ +--------+ +--------+<--+ 15 | | 16 | +--------+ | 17 +---|header | | 18 +--------+ | 19 |tail |-------------------------------------------------------+ 20 +--------+ 21 |length=4| 22 +--------+ 23 |level=2 | 24 +--------+ 25 */
每個level層都是一條單身鏈表,其中level[0]中包含所有元素。
二、創建
根據指定的level,創建一個跳錶節點:
1 zskiplistNode *zslCreateNode(int level, double score, sds ele) { 2 zskiplistNode *zn = 3 zmalloc(sizeof(*zn)+level*sizeof(struct zskiplistLevel)); 4 zn->score = score; 5 zn->ele = ele; 6 return zn; 7 }
創建一個跳錶:
1 #define ZSKIPLIST_MAXLEVEL 64 /* Should be enough for 2^64 elements */ 2 3 zskiplist *zslCreate(void) { 4 int j; 5 zskiplist *zsl; 6 7 zsl = zmalloc(sizeof(*zsl)); 8 zsl->level = 1; 9 zsl->length = 0; 10 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL); 11 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) { 12 zsl->header->level[j].forward = NULL; 13 zsl->header->level[j].span = 0; 14 } 15 zsl->header->backward = NULL; 16 zsl->tail = NULL; 17 return zsl; 18 }
redis中定義的最大層數為64層。且在剛創建時,會生成一個空的頭節點,這樣就可以不用再考慮節點數從0至1或者從1至0時要處理的各種特殊情況。
剛創完的跳錶結構(結構中以4做為最大層數,後同):
1 /* 2 +--------+ 3 |level[3]|-->NULL 4 |0 | 5 +--------+ 6 |level[2]|-->NULL 7 |0 | 8 +--------+ 9 |level[1]|-->NULL 10 |0 | 11 +--------+ 12 |level[0]|-->NULL 13 |0 | 14 +--------+ 15 NULL<-|backward| 16 +--------+ 17 |0 | 18 +--------+ 19 |NULL | 20 +-->+--------+ 21 | 22 | +--------+ 23 +---|header | 24 +--------+ 25 |tail |-->NULL 26 +--------+ 27 |length=0| 28 +--------+ 29 |level=1 | 30 +--------+ 31 */
三、插入節點
1 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */ 2 3 int zslRandomLevel(void) { 4 int level = 1; 5 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF)) 6 level += 1; 7 return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL; 8 }
redis中使用的決定新插入節點層數據的方法是拋硬幣法,且“硬幣”只有25%的幾率是正面。
插入方法:
1 zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele) { 2 //update數組,用於存儲查找路徑 3 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x; 4 5 //rank數組,用於存儲每層路徑節點的排名 6 unsigned int rank[ZSKIPLIST_MAXLEVEL]; 7 int i, level; 8 9 serverAssert(!isnan(score)); 10 x = zsl->header; 11 12 //先查找插入位置 13 for (i = zsl->level-1; i >= 0; i--) { 14 /* store rank that is crossed to reach the insert position */ 15 rank[i] = i == (zsl->level-1) ? 0 : rank[i+1]; 16 while (x->level[i].forward && 17 (x->level[i].forward->score < score || 18 (x->level[i].forward->score == score && 19 sdscmp(x->level[i].forward->ele,ele) < 0))) 20 { 21 rank[i] += x->level[i].span; 22 x = x->level[i].forward; 23 } 24 update[i] = x; 25 } 26 27 //隨機一個level 28 level = zslRandomLevel(); 29 30 //若當前最大level不夠,則補齊update與rank數組 31 if (level > zsl->level) { 32 for (i = zsl->level; i < level; i++) { 33 rank[i] = 0; 34 update[i] = zsl->header; 35 update[i]->level[i].span = zsl->length; 36 } 37 zsl->level = level; 38 } 39 40 //創建一個節點,並插入 41 x = zslCreateNode(level,score,ele); 42 for (i = 0; i < level; i++) { 43 x->level[i].forward = update[i]->level[i].forward; 44 update[i]->level[i].forward = x; 45 46 x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]); 47 update[i]->level[i].span = (rank[0] - rank[i]) + 1; 48 } 49 50 //update數組中,比插入節點level更高的各成員的跨度增加 51 for (i = level; i < zsl->level; i++) { 52 update[i]->level[i].span++; 53 } 54 55 x->backward = (update[0] == zsl->header) ? NULL : update[0]; 56 if (x->level[0].forward) 57 x->level[0].forward->backward = x; 58 else 59 zsl->tail = x; 60 zsl->length++; 61 return x; 62 }
從註釋可知,redis的跳錶允許同score的情況發生,但是不允許同ele,且是由調用者在外部保證。若插入順序為e,b,c,d,則插入e時:
step1、定義update數組與rank數組。
1 /* 2 update rank 3 +--------+ +--------+ 4 |NULL | |0 | 5 +--------+ +--------+ 6 |NULL | |0 | 7 +--------+ +--------+ 8 |NULL | |0 | 9 +--------+ +--------+ 10 |NULL | |0 | 11 +--------+ +--------+ 12 */
實際在linux環境運行時,不會預設初始化,應該是一堆臟數據,此處是為了方便處理結構
step2、查找位置後
1 /* 2 update rank 3 +--------+ +--------+ 4 |NULL | |0 | 5 +--------+ +--------+ 6 |NULL | |0 | 7 +--------+ +--------+ 8 |NULL | |0 | 9 +--------+ +--------+ 10 |header | |0 | 11 +--------+ +--------+ 12 */
step3、e的level為2,比跳錶的大,故要補齊update與rank數組
1 /* 2 update rank 3 +--------+ +--------+ 4 |NULL | |0 | 5 +--------+ +--------+ 6 |NULL | |0 | 7 +--------+ +--------+ 8 |header | |0 | 9 +--------+ +--------+ 10 |header | |0 | 11 +--------+ +--------+ 12 */
step4、插入節點,與單身鏈表插入相同,將新節點e各層,插入到update數組中記錄的各層節點之後,並使用rank數組,計算跨度
1 /* 2 +--------+ 3 |level[3]|-->NULL 4 |0 | 5 +--------+ 6 |level[2]|-->NULL 7 |0 | 8 +--------+ +--------+ 9 |level[1]|-->|level[1]|-->NULL 10 |1 | |0 | 11 +--------+ +--------+ 12 |level[0]|-->|level[0]|-->NULL 13 |1 | |0 | 14 +--------+ +--------+ 15 NULL<-|backward| |backward| 16 +--------+ +--------+ 17 |0 | |5 | 18 +--------+ +--------+ 19 |NULL | |e | 20 +-->+--------+ +--------+ 21 | 22 | +--------+ 23 +---|header | 24 +--------+ 25 |tail | 26 +--------+ 27 |length=0| 28 +--------+ 29 |level=1 | 30 +--------+ 31 */
step5、處理新插入節點的backward指針,與跳錶的tail指針:
1 /* 2 +--------+ 3 |level[3]|-->NULL 4 |0 | 5 +--------+ 6 |level[2]|-->NULL 7 |0 | 8 +--------+ +--------+ 9 |level[1]|-->|level[1]|-->NULL 10 |1 | |0 | 11 +--------+ +--------+ 12 |level[0]|-->|level[0]|-->NULL 13 |1 | |0 | 14 +--------+ +--------+ 15 NULL<-|backward| |backward| 16 +--------+ +--------+ 17 |0 | |5 | 18 +--------+ +--------+ 19 |NULL | |e | 20 +-->+--------+ +--------+<--+ 21 | | 22 | +--------+ | 23 +---|header | | 24 +--------+ | 25 |tail |----------------+ 26 +--------+ 27 |length=1| 28 +--------+ 29 |level=2 | 30 +--------+ 31 32 */
此時插入b:
找到位置後的update與rank數組:
1 /* 2 update rank 3 +--------+ +--------+ 4 |NULL | |0 | 5 +--------+ +--------+ 6 |NULL | |0 | 7 +--------+ +--------+ 8 |header | |0 | 9 +--------+ +--------+ 10 |header | |0 | 11 +--------+ +--------+ 12 */
插入b節點後:
1 /* 2 +--------+ 3 |level[3]|-->NULL 4 |0 | 5 +--------+ 6 |level[2]|-->NULL 7 |0 | 8 +--------+ +--------+ 9 |level[1]|--------------->|level[1]|-->NULL 10 |2 | |0 | 11 +--------+ +--------+ +--------+ 12 |level[0]|-->|level[0]|-->|level[0]|-->NULL 13 |1 | |1 | |0 | 14 +--------+ +--------+ +--------+ 15 NULL<-|backward| |backward|<--|backward| 16 +--------+ +--------+ +--------+ 17 |0 | |2 | |5 | 18 +--------+ +--------+ +--------+ 19 |NULL | |b | |e | 20 +-->+--------+ +--------+ +--------+<--+ 21 | | 22 | +--------+ | 23 +---|header | | 24 +--------+ | 25 |tail |-----------------------------+ 26 +--------+ 27 |length=2| 28 +--------+ 29 |level=2 | 30 +--------+ 31 */
需要註意的是,update數組idx = 1的節點並沒有新的插入操作,span要自增,表示本層跨度增加了1。
插入c時的update與rank數組:
1 /* 2 update rank 3 +--------+ +--------+ 4 |NULL | |0 | 5 +--------+ +--------+ 6 |NULL | |0 | 7 +--------+ +--------+ 8 |header | |0 | 9 +--------+ +--------+ 10 |b | |1 | 11 +--------+ +--------+ 12 */
插入c後:
1 /* 2 +--------+ 3 |level[3]|-->NULL 4 |0 | 5 +--------+ 6 |level[2]|-->NULL 7 |0 | 8 +--------+ +--------+ +--------+ 9 |level[1]|--------------->|level[1]|-->|level[1]|-->NULL 10 |2 | |1 | |0 | 11 +--------+ +--------+ +--------+ +--------+ 12 |level[0]|-->|level[0]|-->|level[0]|-->|level[0]|-->NULL 13 |1 | |1 | |1 | |0 | 14 +--------+ +--------+ +--------+ +--------+ 15 NULL<-|backward| |backward|<--|backward|<--|backward| 16 +--------+ +--------+ +--------+ +--------+ 17 |0 | |2 | |3 | |5 | 18 +--------+ +--------+ +--------+ +--------+ 19 |NULL | |b | |c | |e | 20 +-->+--------+ +--------+ +--------+ +--------+<--+ 21 | | 22 | +--------+ | 23 +---|header | | 24 +--------+ | 25 |tail |------------------------------------------+ 26 +--------+ 27 |length=3| 28 +--------+ 29 |level=2 | 30 +--------+ 31 /*
最後插入d:
update與rank數組:
1 /* 2 update rank 3 +--------+ +--------+ 4 |NULL | |0 | 5 +--------+ +--------+ 6 |NULL | |0 | 7 +--------+ +--------+ 8 |c | |2 | 9 +--------+ +--------+ 10 |c | |2 | 11 +--------+ +--------+ 12 */
插入d:
1 /* 2 +--------+ 3 |level[3]|-->NULL 4 |0 | 5 +--------+ 6 |level[2]|-->NULL 7 |0 | 8 +--------+ +--------+ +--------+ 9 |level[1]|--------------->|level[1]|--------------->|level[1]|-->NULL 10 |2 | |2 | |0 | 11 +--------+ +--------+ +--------+ +--------+ +--------+ 12 |level[0]|-->|level[0]|-->|level[0]|-->|level[0]|-->|level[0]|-->NULL 13 |1 | |1 | |1 | |1 | |0 | 14 +--------+ +--------+ +--------+ +--------+ +--------+ 15 NULL<-|backward| |backward|<--|backward|<--|backward|<--|backward| 16 +--------+ +--------+ +--------+ +--------+ +--------+ 17 |0 | |2 | |3 | |4 | |5 | 18 +--------+ +--------+ +--------+ +--------+ +--------+ 19 |NULL | |b | |c | |d | |e | 20 +-->+--------+ +--------+ +--------+ +--------+ +--------+<--+ 21 | | 22 | +--------+ | 23 +---|header | | 24 +--------+ | 25 |tail |-------------------------------------------------------+ 26 +--------+ 27 |length=4| 28 +--------+ 29 |level=2 | 30 +--------+ 31 /*
如果此時要新插入節點a,score為4.5,則update與rank數組分別為:
1 /* 2 update rank 3 +--------+ +--------+ 4 |NULL | |0 | 5 +--------+ +--------+ 6 |NULL | |0 | 7 +--------+ +--------+ 8 |c | |2 | 9 +--------+ +--------+ 10 |d | |3 | 11 +--------+ +--------+ 12 */
四、刪除節點
在已經查找到位置,與已知update數組時的刪除方法:
1 void 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 }
刪除本節點之後,對應路徑相應得做處理。
從跳錶中刪除指定節點的操作:
1 int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node) { 2 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x; 3 int i; 4 5 //先用score與ele查找,生成update數組 6 x = zsl->header; 7 for (i = zsl->level-1; i >= 0; i--) { 8 while (x->level[i].forward && 9 (x->level[i].forward->score < score || 10 (x->level[i].forward->score == score && 11 sdscmp(x->level[i].forward->ele,ele) < 0))) 12 { 13 x = x->level[i].forward; 14 } 15 update[i] = x; 16 } 17 18 //跳錶允許同score,防止誤刪,做一下ele校驗 19 if (x && score == x->score && sdscmp(x->ele,ele) == 0) { 20 zslDeleteNode(zsl, x, update); 21 if (!node) 22 zslFreeNode(x); 23 else 24 *node = x; 25 return 1; 26 } 27 return 0; 28 }
如以下跳錶:
1 /* 2 +--------+ 3 |level[3]|-->NULL 4 |0 | 5 +--------+ 6 |level[2]|-->NULL 7 |0 | 8 +--------+ +--------+ +--------+ 9 |level[1]|--------------->|level[1]|--------------->|level[1]|-->NULL 10 |2 | |2 | |0 | 11 +--------+ +--------+ +--------+ +--------+ +--------+ 12 |level[0]|-->|level[0]|-->|level[0]|-->|level[0]|-->|level[0]|-->NULL 13 |1 | |1 | |1 | |1 | |0 | 14 +--------+ +--------+ +--------+ +--------+ +--------+ 15 NULL<-|backward| |backward|<--|backward|<--|backward|<--|backward| 16 +--------+ +--------+ +--------+ +--------+ +--------+ 17 |0 | |2 | |3 | |4 | |5 | 18 +--------+ +--------+ +--------+ +--------+ +--------+ 19 |NULL | |b | |c | |d | |e | 20 +-->+--------+ +--------+ +--------+ +--------+ +--------+<--+ 21 | | 22 | +--------+ | 23 +---|header | | 24 +--------+ | 25 |tail |-------------------------------------------------------+ 26 +--------+ 27 |length=4| 28 +--------+ 29 |level=2 | 30 +--------+ 31 /*
要刪除節點d,生成的update數組為:
1 /*