读者福利
更多笔记分享
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;
最后
我还通过一些渠道整理了一些大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。
还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。
Cloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。**
[外链图片转存中…(img-feTEDtHC-1715048728638)]