Redis從入門到放棄系列(五) ZSet

Redis從入門到放棄系列(五) ZSet

本文例子基於:5.0.4 ZSet是Redis中一種比較複雜的數據結構,當存儲大小在128以內且member得長度在64如下,其實現爲zipList,超過爲SkipListjava

突然發現,到如今第五篇文章,尚未講到zipList,然而前面例如Hash,List的篇章都涉及到了zipList的,後面會單獨寫一篇zipList的實現的~立Flag 請期待 【Redis從入門到放棄系列(外傳) ZipList】node

言歸正傳,首先讓咱們來看一下該如何在redis裏面使用ZSet類型redis

//將一個或多個元素及其分數加入到有序集合裏面
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
複製代碼

代碼示例:

//添加元素
>zadd store 1000 xiaoming 2000 xiaoqiang 3000 xiaoyue
(integer) 3
//返回指定區間內的有序集合列表 
> zrange store 0 -1 withscores    
1) "xiaoming"                                   
2) "1000"                                       
3) "xiaoqiang"                                  
4) "2000"                                       
5) "xiaoyue"                                    
6) "3000"                  
//返回有序集合的數量
>zcard store (integer) 3 //查看處於1000到2000的存款的人數 >zcount store 1000 2000 (integer) 2 //查詢處於1000到2000的存款的人羣 > ZRANGEBYSCORE store 1000 2000 1) "xiaoming" 2) "xiaoqiang" //根據member查看當前排名 >zrank store xiaoming (integer) 0 複製代碼

至此,redis zset的用法先告一段落.算法


源碼解析

按照慣例,先來一波zset的數據結構數據結構

/* ZSETs use a specialized version of Skiplists */
typedef struct zskiplistNode {
    sds ele;
    double score;
    struct zskiplistNode *backward;
    struct zskiplistLevel {
        struct zskiplistNode *forward;
        unsigned long span;
    } level[];
} zskiplistNode;

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

typedef struct zset {
    dict *dict;
    zskiplist *zsl;
} zset;
複製代碼

SkipList編碼的有序集合底層是使用一個命爲zset的結構體構成的,該結構體擁有兩種數據類型,dict跟zskiplist。zskiplist按照score從小到大保存全部集合元素,dict則保存着member到score的映射關係,兩個數據結構共用着相同元素的ele和score的內存。 zskiplist是一個雙向鏈表,這是爲了方便倒序方式獲取一個範圍內的元素。 關於跳躍鏈表的講解請參考漫畫算法:什麼是跳躍表?app

當咱們在使用zadd key member的時候,redis是如何實現的呢?讓咱們來看一下源碼:dom

/* Insert a new node in the skiplist. Assumes the element does not already * exist (up to the caller to enforce that). The skiplist takes ownership * of the passed SDS string 'ele'. */
zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele) {
    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
    unsigned int rank[ZSKIPLIST_MAXLEVEL];
    int i, level;

    serverAssert(!isnan(score));
    x = zsl->header;
    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 &&
                    sdscmp(x->level[i].forward->ele,ele) < 0)))
        {
            rank[i] += x->level[i].span;
            x = x->level[i].forward;
        }
        update[i] = x;
    }
    /* we assume the element is not already inside, since we allow duplicated * scores, reinserting the same element 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,ele);
    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 */
        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;
}
複製代碼

上面的流程咱們用一張圖來表示,以下所示:ide

當咱們在使用 zrank key member的時候,zset是怎麼實現的呢?讓咱們一塊兒來看一下源碼

long zsetRank(robj *zobj, sds ele, int reverse) {
    unsigned long llen;
    unsigned long rank;

    llen = zsetLength(zobj);

    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
        //忽略掉 zipList查找過程
    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
        zset *zs = zobj->ptr;
        zskiplist *zsl = zs->zsl;
        dictEntry *de;
        double score;

        de = dictFind(zs->dict,ele);
        if (de != NULL) {
            score = *(double*)dictGetVal(de);
            rank = zslGetRank(zsl,score,ele);
            /* Existing elements always have a rank. */
            serverAssert(rank != 0);
            if (reverse)
                return llen-rank;
            else
                return rank-1;
        } else {
            return -1;
        }
    } else {
        serverPanic("Unknown sorted set encoding");
    }
}
/* Find the rank for an element by both score and key. * Returns 0 when the element cannot be found, rank otherwise. * Note that the rank is 1-based due to the span of zsl->header to the * first element. */
unsigned long zslGetRank(zskiplist *zsl, double score, sds ele) {
    zskiplistNode *x;
    unsigned long rank = 0;
    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 &&
                sdscmp(x->level[i].forward->ele,ele) <= 0))) {
            rank += x->level[i].span;
            x = x->level[i].forward;
        }

        /* x might be equal to zsl->header, so test if obj is non-NULL */
        if (x->ele && sdscmp(x->ele,ele) == 0) {
            return rank;
        }
    }
    return 0;
}
複製代碼

其實查找的時候跟上面插入流程是有不少地方享受的,獲取用戶的排名是經過累加的span。編碼

應用場景

1.排行榜spa

2.存儲社交關係

3.滑動窗口應用

相關文章
相關標籤/搜索