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利用scapy模块写一个TCP路由追踪和扫描存活IP的脚本
前言: 没有前言 0x01 from scapy.all import * import sys from socket import * import os from threading impor ...
- Flask之邮件扩展
4.4 Flask—Mail 在开发过程中,很多应用程序都需要通过邮件提醒用户,Flask的扩展包Flask-Mail通过包装了Python内置的smtplib包,可以用在Flask程序中发送邮件. ...
- flask ---映射到数据库
在当前项目文件下:运行cmd指令(terminal中) (1)python manage.py db init ----初始化文件 (2)python manage.py db migrate-- ...
- ResponseUtil
package util; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.http.Htt ...
- Oracle11gR2_ADG管理之resinstate实战
主库上打开闪回 SQL> select flashback_on from v$database; FLASHBACK_ON ------------------ YES 模拟断电 SQL> ...
- copyWithZone详解
[copyWithZone详解] NSObject实现了-copy.+copy.+copyWithZone方法.代码如下: + (id)copy { return (id)self; } + (id) ...
- Spring总结十:事务案例
数据库表Account: 导包: <dependencies> <!--测试--> <dependency> <groupId>junit</gr ...
- Quick Find
--------------------siwuxie095 Quick Find 这里介绍并查集的一种实现思路:Qui ...
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- 面试题:JVM类加载机制详解(一)JVM类加载过程 背1
首先Throws(抛出)几个自己学习过程中一直疑惑的问题: 1.什么是类加载?什么时候进行类加载? 2.什么是类初始化?什么时候进行类初始化? 3.什么时候会为变量分配内存? 4.什么时候会为变量赋默 ...