莱芜网站开发代理,惠州建设工程交易网站,推荐几个高端大气上档次网站,大理州建设局官方网站#xff08;笔记总结自b站黑马程序员课程#xff09;
一、结构
IntSet是Redis中set集合的一种实现方式#xff0c;基于整数数组来实现#xff0c;并且具备长度可变、有序等特征。 结构如下#xff1a;
typedef struct intset {uint32_t encoding; //编码方式uint32_t l…笔记总结自b站黑马程序员课程
一、结构
IntSet是Redis中set集合的一种实现方式基于整数数组来实现并且具备长度可变、有序等特征。 结构如下
typedef struct intset {uint32_t encoding; //编码方式uint32_t length; //元素个数int8_t contents[]; //整数数组
} intset;
注意整数数组记录的是每一个数的起始地址决定数字长度是编码方式。
其中的encoding包含三种模式表示存储的整数大小不同
#define INTSET_ENC_INT16 (sizeof(int16_t))
#define INTSET_ENC_INT32 (sizeof(int32_t))
#define INTSET_ENC_INT64 (sizeof(int64_t))为了方便查找Redis会将intset中所有的整数按照升序依次保存在contents数组中结构如图 现在数组中每个数字都在int16_t的范围内因此采用的编码方式是INTSET_ENC_INT16每部分占用的字节大小为
①encoding4字节固定
②length4字节固定
③contents2字节 * 3 6字节
二、扩展
我们向该其中添加一个数字50000这个数字超出了int16_t的范围intset会自动升级编码方式到合适的大小。 以当前案例来说流程如下 升级编码为INTSET_ENC_INT32, 每个整数占4字节并按照新的编码方式及元素个数扩容数组 倒序依次将数组中的元素拷贝到扩容后的正确位置倒序可以规避数据覆盖问题 将待添加的元素放入数组末尾 最后将inset的encoding属性改为INTSET_ENC_INT32将length属性改为4 三、源码
源码理解就行注意插入数据底层用到了二分查找的算法。
插入新数据
/* 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. *///判断编码是否超过当前intset的编码if (valenc intrev32ifbe(is-encoding)) {//超出编码需要升级/* This always succeeds, so we dont need to curry *success. */return intsetUpgradeAndAdd(is,value);} else {//在当前intset中查找值与value一样元素的角标pos/* 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);//移动数组中pos之后的元素到pos1给新元素腾出空间if (pos intrev32ifbe(is-length)) intsetMoveTail(is,pos,pos1);}//插入新元素_intsetSet(is,pos,value);//重置元素长度is-length intrev32ifbe(intrev32ifbe(is-length)1);return is;
}升级编码方式
/* Upgrades the intset to a larger encoding and inserts the given integer. */
static intset *intsetUpgradeAndAdd(intset *is, int64_t value) {//获取当前inset编码uint8_t curenc intrev32ifbe(is-encoding);//获取新编码uint8_t newenc _intsetValueEncoding(value);int length intrev32ifbe(is-length); //获取元素个数//判断新元素是大于0还是小于0小于0插入队首大于0插入队尾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 dont 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,lengthprepend,_intsetGetEncoded(is,length,curenc));/* Set the value at the beginning or the end. *///插入新元素prepend决定是队首还是队尾if (prepend)_intsetSet(is,0,value);else_intsetSet(is,intrev32ifbe(is-length),value);//修改数组长度is-length intrev32ifbe(intrev32ifbe(is-length)1);return is;
}四、总结
Intset可以看做是特殊的整数数组具备一些特点 Redis会确保Intset中的元素唯一、有序 具备类型升级机制可以节省内存空间 底层采用二分查找方式来查询 注意一般适合在数据量不是很多的情况下使用