探究Redis字符串(Hacking Strings)
来源:互联网 发布:开普币网络传销骗局 编辑:程序博客网 时间:2024/06/05 22:44
探究Redis字符串(Hacking Strings)
原文:http://www.redis.io/topics/internals-sds
源码:https://github.com/antirez/redis/blob/unstable/src/sds.c
Redis字符串实现包含在sds.c中。(sds代表Simple Dynamic Strings.)
sds.h中结构体sdshdr表示字符串。
struct sdshdr { long len; long free; char buf[];};
buf字符数组存储字符串字符。
len存储buf长度。获取Redis字符串的复杂度是O(1)。
free存储余下可用字节数。
len和free可认为是buf字符数组的元数据。
创建Redis字符串
sds.h中定义了一种新的数据类型sds,它是字符数组的别称。
typedef char *sds;
sds.c
中的sdsnewlen
用于创建新的Redis字符串。
sds sdsnewlen(const void *init, size_t initlen) { struct sdshdr *sh; sh = zmalloc(sizeof(struct sdshdr)+initlen+1);#ifdef SDS_ABORT_ON_OOM if (sh == NULL) sdsOomAbort();#else if (sh == NULL) return NULL;#endif sh->len = initlen; sh->free = 0; if (initlen) { if (init) memcpy(sh->buf, init, initlen); else memset(sh->buf,0,initlen); } sh->buf[initlen] = '\0'; return (char*)sh->buf;}
值得注意的是,Redis字符串是sdshdr类型的变量,但sdsnewlen换回的是字符指针。这是一种策略,需要解释一下。
假如我传见一个字符串:
sdsnewlen("redis", 5);
This creates a new variable of type struct sdshdr
allocating memory forlen
and free
fields as well as for the buf
character array.
sh = zmalloc(sizeof(struct sdshdr)+initlen+1); // initlen is length of init argument.
sdsnewlen成功创建Redis字符串后,内存布局:-----------|5|0|redis|-----------^ ^sh sh->buf
sdsnewlen把sh->buf返回给调用者。
如果需要,怎么释放呢?
你实际需要要sh指针,但你只有sh->buf指针。
是否能从sh->buf获取sh?
回答是肯定的。通过指针运算可以实现。看上图,如果你从sh->buf减去两个长整型长度就可得sh。
看看sdslen函数,理解这个策略的机制:
size_t sdslen(const sds s) { struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); return sh->len;}
理解了sdslen函数后,其他的函数就很容易理解了。
Redis字符串的实现细节隐藏在接口内部,使用者不需要去了解它的实现机制,只需使用接口函数,给它传递字符指针即可。
- 探究Redis字符串(Hacking Strings)
- redis 学习手册之字符串类型strings操作命令
- 设备标识字符串(Device Identification Strings)
- Working with Strings(使用Oracle字符串)
- LeetCode | Multiply Strings(字符串相乘)
- UVa 455 - Periodic Strings(字符串处理)
- UVa 455 Periodic Strings(周期字符串)
- [LeetCode-205] Isomorphic Strings(同构字符串)
- Glib学习(5) 字符串 Strings
- LeetCode:Isomorphic Strings(同构字符串)
- poj 2406 Power Strings-字符串(kmp)
- [LeetCode]--205. Isomorphic Strings(同构字符串)
- 205. Isomorphic Strings (同构字符串判断)
- CF544A:Set of Strings(字符串处理)
- POJ: Surprising Strings(map、字符串处理)
- Redis Internals (strings)
- Redis 数据类型 Strings
- 【Redis】----Redis字符串(String)
- 多用户同时处理同一条数据解决办法
- hash表
- MyEclipse安装FatJar
- android 新浪微博
- 最无私的Android资料(书籍+代码)分享-不要积分
- 探究Redis字符串(Hacking Strings)
- Android系统Surface机制的SurfaceFlinger服务的线程模型分析
- 3D程序员面试题
- 20121012失眠的夜晚
- LeetCode (A)
- 快速找出一个数组中的最大数、第二大数
- 巨人网络面试题
- mime
- simhash算法的原理[转]