redis系列笔记之数据结构SDS
2016-06-27 18:17
513 查看
redis的功能就不多做介绍了,本系列文章重点是从源码角度揣度其设计思想,另外所有的源码都是基于redis3.0.7版的开源包
今天就说一说其底层用于存储字符串的数据结构SDS,话不多少先把关键源码粘出来
typedef char *sds;
struct sdshdr {
unsigned int len;
unsigned int free;
char buf[];
};
先简单说下各个成员代表的意思,len表示存储的字符串长度,free表示buf中剩余的存储空间,buf用于存储字符串。
看了上面的代码不知道你是否会对typedef char *sds;这句话产生疑惑,为什么不是typedef struct sdshdr *sds?
再看下两个基本的内联方法:
static inline size_t sdslen(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->len;
}
static inline size_t sdsavail(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->free;
}
内联方法中获取sds存储的字符串长度时为什么 是通过(void*)(s-(sizeof(struct sdshdr)))获取struct sdshdr *对象?
带着这两疑惑看下它的初始化方法:
sds sdsnewlen(const void *init, size_t initlen) {
struct sdshdr *sh;
if (init) {
sh = zmalloc(sizeof(struct sdshdr)+initlen+1); //+1是为了预留一个位置保存'\0'
} else {
sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
}
if (sh == NULL) return NULL;
sh->len = initlen;
sh->free = 0;
if (initlen && init)
memcpy(sh->buf, init, initlen);
sh->buf[initlen] = '\0';
return (char*)sh->buf;
}
是不是有点小明白了,其实sds指针并不是指向sdshdr的首地址,而是buf的地址,这也就是为什么要通过(void*)(s-(sizeof(struct sdshdr)))获取struct sdshdr *对象
从初始化方法可以看出,sds其实是对char数组的一种扩展,在数组前添加了两个int空间存储字符串长度和剩余空间,添加这两个属性的好处是:
①降低了获取字符串长度的时间复杂度,变成了O(1)
②通过校验剩余空间大小,可以避免字符串操作中常会发生的数组越界问题
今天就说一说其底层用于存储字符串的数据结构SDS,话不多少先把关键源码粘出来
typedef char *sds;
struct sdshdr {
unsigned int len;
unsigned int free;
char buf[];
};
先简单说下各个成员代表的意思,len表示存储的字符串长度,free表示buf中剩余的存储空间,buf用于存储字符串。
看了上面的代码不知道你是否会对typedef char *sds;这句话产生疑惑,为什么不是typedef struct sdshdr *sds?
再看下两个基本的内联方法:
static inline size_t sdslen(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->len;
}
static inline size_t sdsavail(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->free;
}
内联方法中获取sds存储的字符串长度时为什么 是通过(void*)(s-(sizeof(struct sdshdr)))获取struct sdshdr *对象?
带着这两疑惑看下它的初始化方法:
sds sdsnewlen(const void *init, size_t initlen) {
struct sdshdr *sh;
if (init) {
sh = zmalloc(sizeof(struct sdshdr)+initlen+1); //+1是为了预留一个位置保存'\0'
} else {
sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
}
if (sh == NULL) return NULL;
sh->len = initlen;
sh->free = 0;
if (initlen && init)
memcpy(sh->buf, init, initlen);
sh->buf[initlen] = '\0';
return (char*)sh->buf;
}
是不是有点小明白了,其实sds指针并不是指向sdshdr的首地址,而是buf的地址,这也就是为什么要通过(void*)(s-(sizeof(struct sdshdr)))获取struct sdshdr *对象
从初始化方法可以看出,sds其实是对char数组的一种扩展,在数组前添加了两个int空间存储字符串长度和剩余空间,添加这两个属性的好处是:
①降低了获取字符串长度的时间复杂度,变成了O(1)
②通过校验剩余空间大小,可以避免字符串操作中常会发生的数组越界问题
相关文章推荐
- redis安装问题小结
- 使用 Redis 和 Python 构建一个共享单车的应用程序
- Redis偶发连接失败案例实战记录
- C#数据结构之顺序表(SeqList)实例详解
- Redis中实现查找某个值的范围
- win 7 安装redis服务【笔记】
- redis的hGetAll函数的性能问题(记Redis那坑人的HGETALL)
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- Redis实现信息已读未读状态提示
- Lua教程(七):数据结构详解
- redis中修改配置文件中的端口号 密码方法
- 在Ruby on Rails上使用Redis Store的方法
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- Redis和Memcache的区别总结
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一