在了解这个之前,首先要了解一下redis中定义的几种数据结构。
sds(简单动态字符串)
SDS以Redis中定义的一种用于存储字符串的数据结构。redis中保存的key都是以sds的形式存储的。
sds在redis源码中的定义如下:
typedef char *sds;
//sdshdr5 is never used
struct __attribute__ ((__packed__)) sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {
uint8_t len; // 当前字符数组长度
uint8_t alloc; //当前字符数组总共分配的内存大小
unsigned char flags; //当前字符数组的属性。用来标识到底是sdshdr8 还是sdshdr16 等
char buf[]; //字符串真正的值
};
struct __attribute__ ((__packed__)) sdshdr16 {
uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr32 {
uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr64 {