Redis从入门到放弃系列(四) Set

Redis从入门到放弃系列(三) List

首先让我们来看一下该如何在redis里面使用Set类型

//设置key的集合中的值为member
sadd key member [member ...]

代码示例:

> sadd books java python c
(integer) 3
//当我们重复添加相同的数据的时候,redis返回为0
> sadd books java python c
(integer) 0
----------------------------------
//返回books集合的所有元素
>smembers books
1) "c"
2) "python"
3) "java"
----------------------------------
//判断某个元素是否在集合里面
>sismember books c
(integer) 1
>sismember books 99
(integer) 0
----------------------------------
//两个集合的交集
> sadd new_books java c++ R
(integer) 3
> SINTER books new_books
1) "java"
----------------------------------
//两个集合的并集
> SUNION books new_books
1) "java"
2) "python"
3) "c"
4) "c++"
5) "R"
//两个集合的差集
> SMEMBERS books
1) "c"
2) "python"
3) "java"
> SMEMBERS new_books
1) "R"
2) "c++"
3) "java"
>  SDIFF books new_books
1) "python"
2) "c"
>  SDIFF new_books books
1) "R"
2) "c++"

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


源码解析

本文开头的时候讲set实现分为intset跟hashtable,hashtable这块讲解的话可以去回头看一下Redis从入门到放弃系列(二) Hash 本节重点来讲一下intset~当存储元素为整数的时候,redis为了节省空间,采用了intset这种数据结构来做存储,我们知道set结构存储字符串的时候都是无序的,可当采用intset来存储的整数的时候, set是有序的,内部采用了二分法方便快速查询 让我们先来看一下intset内部结构

typedef struct intset {
    uint32_t encoding;
    uint32_t length;
    int8_t contents[];
} intset;

我们发现intset里面其实是由一个变量类型,一个长度表示的,也就是说要计算当前intset占据的字节:encoding * length; redis在使用intset的时候,首先会判断当前插入的value的大小,然后返回不同字节的类型

/* Note that these encodings are ordered, so:
 * INTSET_ENC_INT16 < INTSET_ENC_INT32 < INTSET_ENC_INT64. */
#define INTSET_ENC_INT16 (sizeof(int16_t))
#define INTSET_ENC_INT32 (sizeof(int32_t))
#define INTSET_ENC_INT64 (sizeof(int64_t))

/* Return the required encoding for the provided value. */
static uint8_t _intsetValueEncoding(int64_t v) {
    if (v < INT32_MIN || v > INT32_MAX)
        return INTSET_ENC_INT64;
    else if (v < INT16_MIN || v > INT16_MAX)
        return INTSET_ENC_INT32;
    else
        return INTSET_ENC_INT16;
}

当每次插入的value的值大于当前类型的话,redis会将intset升级为更大的编码

/* Upgrades the intset to a larger encoding and inserts the given integer. */
static intset *intsetUpgradeAndAdd(intset *is, int64_t value) {
    uint8_t curenc = intrev32ifbe(is->encoding);
    uint8_t newenc = _intsetValueEncoding(value);
    int length = intrev32ifbe(is->length);
    int prepend = value < 0 ? 1 : 0;

    /* First set new encoding and resize */
    is->encoding = intrev32ifbe(newenc);
    is = intsetResize(is,intrev32ifbe(is->length)+1);

    /* Upgrade back-to-front so we don't overwrite values.
     * Note that the "prepend" variable is used to make sure we have an empty
     * space at either the beginning or the end of the intset. */
    while(length--)
        _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc));

    /* Set the value at the beginning or the end. */
    if (prepend)
        _intsetSet(is,0,value);
    else
        _intsetSet(is,intrev32ifbe(is->length),value);
    is->length = intrev32ifbe(intrev32ifbe(is->length)+1);
    return is;
}

前面我们说过,intset是一个有序的,然后查找的时候采用了二分法来查找元素,那么其内部是如何实现的呢?

/* Search for the position of "value". Return 1 when the value was found and
 * sets "pos" to the position of the value within the intset. Return 0 when
 * the value is not present in the intset and sets "pos" to the position
 * where "value" can be inserted. */
static uint8_t intsetSearch(intset *is, int64_t value, uint32_t *pos) {
    int min = 0, max = intrev32ifbe(is->length)-1, mid = -1;
    int64_t cur = -1;

    /* The value can never be found when the set is empty */
    if (intrev32ifbe(is->length) == 0) {
        if (pos) *pos = 0;
        return 0;
    } else {
        /* Check for the case where we know we cannot find the value,
         * but do know the insert position. */
        if (value > _intsetGet(is,max)) {
            if (pos) *pos = intrev32ifbe(is->length);
            return 0;
        } else if (value < _intsetGet(is,0)) {
            if (pos) *pos = 0;
            return 0;
        }
    }

    while(max >= min) {
        mid = ((unsigned int)min + (unsigned int)max) >> 1;
        cur = _intsetGet(is,mid);
        if (value > cur) {
            min = mid+1;
        } else if (value < cur) {
            max = mid-1;
        } else {
            break;
        }
    }

    if (value == cur) {
        if (pos) *pos = mid;
        return 1;
    } else {
        if (pos) *pos = min;
        return 0;
    }
}
/* Insert an integer in the intset */
intset *intsetAdd(intset *is, int64_t value, uint8_t *success) {
    uint8_t valenc = _intsetValueEncoding(value);
    uint32_t pos;
    if (success) *success = 1;

    /* Upgrade encoding if necessary. If we need to upgrade, we know that
     * this value should be either appended (if > 0) or prepended (if < 0),
     * because it lies outside the range of existing values. */
    if (valenc > intrev32ifbe(is->encoding)) {
        /* This always succeeds, so we don't need to curry *success. */
        return intsetUpgradeAndAdd(is,value);
    } else {
        /* Abort if the value is already present in the set.
         * This call will populate "pos" with the right position to insert
         * the value when it cannot be found. */
        if (intsetSearch(is,value,&pos)) {
            if (success) *success = 0;
            return is;
        }

        is = intsetResize(is,intrev32ifbe(is->length)+1);
        if (pos < intrev32ifbe(is->length)) intsetMoveTail(is,pos,pos+1);
    }

    _intsetSet(is,pos,value);
    is->length = intrev32ifbe(intrev32ifbe(is->length)+1);
    return is;
}

重点在while操作那一段~我们可以看到,其查找采用了二分法,那么如何让其有序呢?

if (intsetSearch(is,value,&pos)) {
    if (success) *success = 0;
    return is;
}

看到这一段了吧?判断查找的时候,将pos的位置查找了出来,给下面_intsetSet操作做前奏~

应用场景

1.去重~ 2.查看两个人的共同爱好

写在最后

祝大家520快乐~

Redis从入门到放弃系列(四) Set-LMLPHP

04-24 19:53