这是我参与「第五届青训营 」伴学笔记创作活动的第 8 天
整数集合(intset)是 Redis 集合键的底层实现之一,当一个集合只包含整数值元素时,并且这个集合的元素个数不多时,Redis 会使用 intset 作为集合键的底层实现。本文从源码角度理解 intset 的底层实现原理和查找、插入、升级的原理。
数据结构
intset 是 Redis 用于保存整数值的集合的抽象数据结构,可以保存类型为 int_16、int_32 或者 int_64 类型的整数值,并且保证集合中不会出现重复元素。
intset 定义在 intset.h 文件中。
typedef struct intset {
// 编码方式
uint32_t encoding;
// 集合包含的元素数量
uint32_t length;
// 保存元素的数组
int8_t contents[];
}
encoding:表示带符号的元素类型,包括INTSET_ENC_INT16、INTSET_ENC_INT32和INTSET_ENC_INT64length:表示元素的个数,即 contents 数组的长度content:是一个动态数组,按需分配空间。集合的每个元素都保存在数组中,且在数组中按从小到大的顺序有序排列,并且数组中不包含任何重复项
需要注意的是,contents 数组虽然被声明为 int8_t 类型,但实际上数组并不存储任何 int8_t 类型的值,真实的类型取决于 encoding 的值。
类型的选择参照以下原则
#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;
}
intset 的查找
intset 的查找方式采用的是折半查找,时间复杂度为 O(logN)。
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;
}
}
intset 的升级
当将一个元素添加到 intset 中,且元素的类型比当前 intset 现有的所有元素类型都要长时,则 intset 需要进行升级,然后才能将新元素插入其中。
升级总计需要分三步进行。
- 根据新元素的类型,扩展 intset 的 contents 数组的空间大小,并为新元素分配空间
- 将 contents 数组的所有现有元素都转换为与新元素类型相同的类型,并将类型转换后的元素放到正确的位置上,而且需要维持有序的性质不变
- 将新元素添加到 contents 数组中
/* Set the value at pos, using the configured encoding. */
static void _intsetSet(intset *is, int pos, int64_t value) {
uint32_t encoding = intrev32ifbe(is->encoding);
if (encoding == INTSET_ENC_INT64) {
((int64_t*)is->contents)[pos] = value;
memrev64ifbe(((int64_t*)is->contents)+pos);
} else if (encoding == INTSET_ENC_INT32) {
((int32_t*)is->contents)[pos] = value;
memrev32ifbe(((int32_t*)is->contents)+pos);
} else {
((int16_t*)is->contents)[pos] = value;
memrev16ifbe(((int16_t*)is->contents)+pos);
}
}
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 */
// 设置新的 encoding 并 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)
// 如果值在 0 位置则直接设置新元素
_intsetSet(is,0,value);
else
// 否则根据值设置元素
_intsetSet(is,intrev32ifbe(is->length),value);
is->length = intrev32ifbe(intrev32ifbe(is->length)+1);
return is;
}
总结
- intset 是集合键的底层实现之一
- intset 底层实现为一个数组,这个数组是有序的、无重复的方式保存集合元素
- 当新插入元素的类型变长时,intset 可以进行类型的升级操作
- 升级操作给 intset 带来了更多操作的灵活性,并尽可能地节约了内存
- intset 只支持升级操作,并不支持降级操作