redis源码分析(3)sds
sds是redis中用来处理字符串的数据结构。sds的定义在sds.h中:
typedef char *sds;
简洁明了!简明扼要!(X,玩我呢是吧!这特么不就是c中的字符串么?!)。像redis这种高端大气上档次的服务器显然不会这么的幼稚。在sds的定义之后,还有一个结构体:
struct sdshdr {
int len;
int free;
char buf[];
}
有len,有free,这就有点意思了。很明显,根据这个结构体的定义,这是sds的header,用来存储sds的信息。注意最后的buf定义,这个buf数组没有设置长度。这是为神马呢?在gcc中,这种方式可以使得buf成为一个可变的数组,也就是说,可以扩展buf同时又保证在使用的时候,感觉buf始终在struct sdshdr中。有点啰嗦,其实可以用下图展示:
sdshdr sds
| |
V V
----------------------------
|len | free | buf … |
----------------------------
这个就是sds的内存分布图。struct sdshdr这个结构体放在了真正的数据之前,且是紧挨着的。这样,通过buf引用的数组其实就是后面的数据。这个是利用了c中数组访问的特点。
下面我们来看看如何创建一个sds:
/* Create a new sds string with the content specified by the 'init' pointer
* and 'initlen'.
* If NULL is used for 'init' the string is initialized with zero bytes.
*
* The string is always null-termined (all the sds strings are, always) so
* even if you create an sds string with:
*
* mystring = sdsnewlen("abc",3");
*
* You can print the string with printf() as there is an implicit \0 at the
* end of the string. However the string is binary safe and can contain
* \0 characters in the middle, as the length is stored in the sds header. */
sds sdsnewlen(const void *init, size_t initlen) {
struct sdshdr *sh; if (init) {
sh = zmalloc(sizeof(struct sdshdr)+initlen+);
} else {
sh = zcalloc(sizeof(struct sdshdr)+initlen+);
}
if (sh == NULL) return NULL;
sh->len = initlen;
sh->free = ;
if (initlen && init)
memcpy(sh->buf, init, initlen);
sh->buf[initlen] = '\0';
return (char*)sh->buf;
}
重点是这句(zcalloc也一样,只是分配内存的时候顺带初始化为0):
sh = zmalloc(sizeof(struct sdshdr)+initlen+)
创建一个sds的时候,实际申请的内存大小为sdshdr的大小,加上调用者希望的sds的大小,再加一。另外,zmalloc的返回值直接赋值给了sh,sh是struct sdshdr。那么,在创建一个sds的时候,将sds的struct sdshdr放到了真正的数据的前面,这样可以通过buf引用到后面的数据。多加一个一是为了保证有地方放'\0'。根据注释,sds默认以'\0'结尾,且可以存放二进制的数据,因为struct sdshdr中存放了数据的长度。在sdsnewlen的最后,返回的是(char\*)sh->buf,也就是说sds实际指向的就是一个char\*数组。**所有可以对char\*的操作也同时可以操作sds**。
那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的实际长度和可用空间。核心代码就是这句:
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
将sds的地址减去struct sdshdr的长度然后赋值给sh,这就得到了sds对应的struct sdshdr。根据前面的内存分布图,struct sdshdr始终是在数据的前面,一次很容易得到struct sdshdr的地址。得到了struct sdshdr的地址之后,其他的就很简单了。
sds支持动态的扩展空间,sdsMakeRoomFor这个函数用来扩展sds的空间:
/* Enlarge the free space at the end of the sds string so that the caller
* is sure that after calling this function can overwrite up to addlen
* bytes after the end of the string, plus one more byte for nul term.
*
* Note: this does not change the *length* of the sds string as returned
* by sdslen(), but only the free buffer space we have. */
sds sdsMakeRoomFor(sds s, size_t addlen) {
struct sdshdr *sh, *newsh;
size_t free = sdsavail(s);
size_t len, newlen; if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= ;
else
newlen += SDS_MAX_PREALLOC;
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+);
if (newsh == NULL) return NULL; newsh->free = newlen - len;
return newsh->buf;
}
这个函数保证sds至少有addlen长度的空间可用。这个函数体现了sds的空间扩展策略。如果有足够的空间,则直接返回。如果空间不够,当len+addlen小于SDS_MAX_PREALLOC时,将空间扩展到(len+addlen)\*2。当len+addlen大于SDS_MAX_PREALLOC,将空间扩展到len+addlen+SDS_MAX_PREALLOC。sds的扩展考虑了实际需要的空间大小,扩展的效率要高一些。如果每次扩大原来的二倍,当需要的空间大于初始空间二倍时,需要多次的扩展操作,也就意味着多次的zrealloc操作。sds的扩展可以在任何情况下一次扩展到位。
sds最大的特点就是所有可以对char\*的操作都可以操作sds,这在实际使用sds的的时候可以带来很多方便。比如,从socket中读取数据存储到sds中,可以如下操作:
/* sds s */
int oldlen = sdslen(s);
s = sdsMakeRoomFor(s, BUFFER_SIZE);
nread = read(fd, s+oldlen, BUFFER_SIZE);
sdsIncrLen(s, nread);
在调用read的时候,可以把sds看做是char\*来处理(实际上sds就是char\*)。当然,最后一定要调用sdsIncrLen来修正sds的长度。
redis源码分析(3)sds的更多相关文章
- redis源码分析(一)-sds实现
redis支持多种数据类型,sds(simple dynamic string)是最基本的一种,redis中的字符串类型大多使用sds保存,它支持动态的扩展与压缩,并提供许多工具函数.这篇文章将分析s ...
- Redis源码分析(sds)
源码版本:redis-4.0.1 源码位置:https://github.com/antirez/sds 一.SDS简介 sds (Simple Dynamic String),Simple的意思是简 ...
- Redis源码分析:serverCron - redis源码笔记
[redis源码分析]http://blog.csdn.net/column/details/redis-source.html Redis源代码重要目录 dict.c:也是很重要的两个文件,主要 ...
- redis源码分析之事务Transaction(下)
接着上一篇,这篇文章分析一下redis事务操作中multi,exec,discard三个核心命令. 原文地址:http://www.jianshu.com/p/e22615586595 看本篇文章前需 ...
- redis源码分析之有序集SortedSet
有序集SortedSet算是redis中一个很有特色的数据结构,通过这篇文章来总结一下这块知识点. 原文地址:http://www.jianshu.com/p/75ca5a359f9f 一.有序集So ...
- Redis源码分析(intset)
源码版本:4.0.1 源码位置: intset.h:数据结构的定义 intset.c:创建.增删等操作实现 1. 整数集合简介 intset是Redis内存数据结构之一,和之前的 sds. skipl ...
- Redis源码分析(dict)
源码版本:redis-4.0.1 源码位置: dict.h:dictEntry.dictht.dict等数据结构定义. dict.c:创建.插入.查找等功能实现. 一.dict 简介 dict (di ...
- redis源码分析之发布订阅(pub/sub)
redis算是缓存界的老大哥了,最近做的事情对redis依赖较多,使用了里面的发布订阅功能,事务功能以及SortedSet等数据结构,后面准备好好学习总结一下redis的一些知识点. 原文地址:htt ...
- [Redis源码阅读]sds字符串实现
初衷 从开始工作就开始使用Redis,也有一段时间了,但都只是停留在使用阶段,没有往更深的角度探索,每次想读源码都止步在阅读书籍上,因为看完书很快又忘了,这次逼自己先读代码.因为个人觉得写作需要阅读文 ...
- redis源码分析之事务Transaction(上)
这周学习了一下redis事务功能的实现原理,本来是想用一篇文章进行总结的,写完以后发现这块内容比较多,而且多个命令之间又互相依赖,放在一篇文章里一方面篇幅会比较大,另一方面文章组织结构会比较乱,不容易 ...
随机推荐
- Python函数的初识
一 什么是函数 定义: 定义一个事情或者功能,等到需要用的时候直接用就可以了,那么这个定义的就是一个函数 函数 : 对代码块和功能的封装和定义 函数定义的格式: def 函数名() ...
- 委托小结及Func用法
首先,委托是一种类型,由关键字delegate声明.确切的说,委托是一种可用于封装命名或者匿名方法的引用类型. 它类似于 C++ 中的函数指针,而且是类型安全和可靠的. 委托类型的声明与 ...
- Java微信公众平台开发(三)--接收消息的分类及实体的创建
转自:http://www.cuiyongzhi.com/post/41.html 前面一篇有说道应用服务器和腾讯服务器是通过消息进行通讯的,并简单介绍了微信端post的消息类型,这里我们将建立消息实 ...
- 解决The JSP specification requires that an attribute name is preceded by whitespace问题
笔者今天调试界面出现下边这个问题: The JSP specification requires that an attribute name is preceded by whitespace 经查 ...
- Make Cents
Every year, an elephant qualifies to the Arab Collegiate Programming Competition. He graduated this ...
- HDR
[HDR] 什么是 HDR? 高动态范围拍摄(HDR)现在已经得到广泛使用,被用来补偿大多数数码成像传感器有限的动态范围.照片的动态范围是指最暗的色彩与最亮的色彩之间的亮度范围——也可以一并表示色调范 ...
- Spring总结十:事务案例
数据库表Account: 导包: <dependencies> <!--测试--> <dependency> <groupId>junit</gr ...
- css二阴影+第三维度z-index
<span style="text-shadow: 3px 3px 1px #888;">shadowed</span><span style=&qu ...
- - description 方法作用
自定义一个Person类 @interface Person : NSObject { int _age; double _height; double _weight; NSString *_nam ...
- python中Dict与OrderedDict
使用dict时,Key是无序的.在对dict做迭代时,我们无法确定Key的顺序. 如果要保持Key的顺序,可以用OrderedDict: from collections import Ordered ...