redis源码学习redisObject
Posted 看,未来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis源码学习redisObject相关的知识,希望对你有一定的参考价值。
使用的是redis6.0.6版本,因为我第一次接触 redis 时它就是这个最新稳定版。
robj
redis中的数据对象 server.h/redisObject 是redis内部存储的数据定义的抽象类型。
//英文是自带的,中文是我写的
typedef struct redisObject
unsigned type:4; //数据类型
unsigned encoding:4;//编码格式
unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
* LFU data (least significant 8 bits frequency
* and most significant 16 bits access time). */
//LRU时间戳 or LRU计数
int refcount; //引用计数,为了节省内存,redis会在多处引用同一个redisObject
void *ptr; //指向实际的数据结构
robj;
前面仨儿,嗯,挤一个 unsigned 的不同地址位。嗯,大师果然是艰苦朴素,勤俭持“内存”啊。
数据类型
/* A redis object, that is a type able to hold a string / list / set */
/* The actual Redis Object */
#define OBJ_STRING 0 /* String object. */
#define OBJ_LIST 1 /* List object. */
#define OBJ_SET 2 /* Set object. */
#define OBJ_ZSET 3 /* Sorted set object. */
#define OBJ_HASH 4 /* Hash object. */
/* The "module" object type is a special one that signals that the object
* is one directly managed by a Redis module. In this case the value points
* to a moduleValue struct, which contains the object value (which is only
* handled by the module itself) and the RedisModuleType struct which lists
* function pointers in order to serialize, deserialize, AOF-rewrite and
* free the object.
*
* Inside the RDB file, module types are encoded as OBJ_MODULE followed
* by a 64 bit module type ID, which has a 54 bits module-specific signature
* in order to dispatch the loading to the right module, plus a 10 bits
* encoding version. */
#define OBJ_MODULE 5 /* Module object. 自定义消息类型*/
#define OBJ_STREAM 6 /* Stream object. 消息流*/
编码类型
/* Objects encoding. Some kind of objects like Strings and Hashes can be
* internally represented in multiple ways. The 'encoding' field of the object
* is set to one of this fields for this object. */
#define OBJ_ENCODING_RAW 0 /* Raw representation 简单动态字符串*/
#define OBJ_ENCODING_INT 1 /* Encoded as integer 整数*/
#define OBJ_ENCODING_HT 2 /* Encoded as hash table 字典*/
#define OBJ_ENCODING_ZIPMAP 3 /* Encoded as zipmap 未使用*/
#define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. 不再使用*/
#define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist 压缩列表*/
#define OBJ_ENCODING_INTSET 6 /* Encoded as intset 整数集合*/
#define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist 跳表*/
#define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding 简单动态字符串*/
#define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists 快速链表*/
#define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks 流*/
随机应变的对象编码
对象的整个周期中,编码不是一成不变的。也是为了节约嘛。比如上面可以看到有个整数集合,当集合中所有元素都可以用整数表示时,底层数据结构采用整数集合。看:
int setTypeAdd(robj *subject, sds value)
long long llval;
if (subject->encoding == OBJ_ENCODING_HT)
dict *ht = subject->ptr;
dictEntry *de = dictAddRaw(ht,value,NULL);
if (de)
dictSetKey(ht,de,sdsdup(value));
dictSetVal(ht,de,NULL);
return 1;
else if (subject->encoding == OBJ_ENCODING_INTSET)
if (isSdsRepresentableAsLongLong(value,&llval) == C_OK)
uint8_t success = 0;
subject->ptr = intsetAdd(subject->ptr,llval,&success);
if (success)
/* Convert to regular set when the intset contains
* too many entries. */
if (intsetLen(subject->ptr) > server.set_max_intset_entries)
setTypeConvert(subject,OBJ_ENCODING_HT);
return 1;
else
/* Failed to get integer from object, convert to regular set. */
setTypeConvert(subject,OBJ_ENCODING_HT);
/* The set *was* an intset and this value is not integer
* encodable, so dictAdd should always work. */
serverAssert(dictAdd(subject->ptr,sdsdup(value),NULL) == DICT_OK);
return 1;
else
serverPanic("Unknown set encoding");
return 0;
当执行sadd命令向集合中添加元素时,redis会校验待添加的元素是否可以解析为整数。如果解析失败,则会将集合存储结构转换为字典。
对象在不同情况下会采用不同的方式存储,那同时采用多种数据结构存储呢?也是会的。我们来看一下例子:
有序字典zset
typedef struct zset
dict *dict;
zskiplist *zsl;
zset;
目前看来是:字典单个检索快,跳表批量检索稳。
那有个疑问了:这里就不艰苦朴素,勤俭持家了?
这里面存的是指针副本,不是数据副本哈。该花的地方还是得花啊,勤俭持家不等于抠抠搜搜嘛。
回到robj
我们再看robj。
1)当robj存储的数据可以用long类型表示时,数据直接存储在ptr字段。
2)refcount用于实现对象的共享,实现思想比较经典了,具体可以看一下智能指针。
void incrRefCount(robj *o)
if (o->refcount < OBJ_FIRST_SPECIAL_REFCOUNT)
o->refcount++;
else
if (o->refcount == OBJ_SHARED_REFCOUNT)
/* Nothing to do: this refcount is immutable. */
else if (o->refcount == OBJ_STATIC_REFCOUNT)
serverPanic("You tried to retain an object allocated in the stack");
void decrRefCount(robj *o)
if (o->refcount == 1)
switch(o->type)
case OBJ_STRING: freeStringObject(o); break;
case OBJ_LIST: freeListObject(o); break;
case OBJ_SET: freeSetObject(o); break;
case OBJ_ZSET: freeZsetObject(o); break;
case OBJ_HASH: freeHashObject(o); break;
case OBJ_MODULE: freeModuleObject(o); break;
case OBJ_STREAM: freeStreamObject(o); break;
default: serverPanic("Unknown object type"); break;
zfree(o);
else
if (o->refcount <= 0) serverPanic("decrRefCount against refcount <= 0");
if (o->refcount != OBJ_SHARED_REFCOUNT) o->refcount--;
3)lru,缓存淘汰策略(不一定就是LRU哈,不要被事物的表面现象所迷惑,也有可能是LFU,在配置文件中设定)
redis获取时间是以一秒为周期执行系统调用获取精确时间,存储在server.lfu_decay_time中,不是实时获取的。
4)LFUDecrAndReturn,这个函数虽然我看不太懂,但是思想还是很不错的。
其返回计数值,实现了计数值随时间衰减的过程。不然越老的数据一般情况下访问次数越大,即使该对象可能很长时间没有访问了、
/* If the object decrement time is reached decrement the LFU counter but
* do not update LFU fields of the object, we update the access time
* and counter in an explicit way when the object is really accessed.
* And we will times halve the counter according to the times of
* elapsed time than server.lfu_decay_time.
* Return the object frequency counter.
*
* This function is used in order to scan the dataset for the best object
* to fit: as we check for the candidate, we incrementally decrement the
* counter of the scanned objects if needed. */
unsigned long LFUDecrAndReturn(robj *o)
unsigned long ldt = o->lru >> 8;
unsigned long counter = o->lru & 255;
unsigned long num_periods = server.lfu_decay_time ? LFUTimeElapsed(ldt) / server.lfu_decay_time : 0;
if (num_periods)
counter = (num_periods > counter) ? 0 : counter - num_periods;
return counter;
以上是关于redis源码学习redisObject的主要内容,如果未能解决你的问题,请参考以下文章
Redis源码剖析和注释--- 对象系统(redisObject)