漫话Redis源码之三十三

2024-02-06 09:48
文章标签 源码 redis 三十三 漫话

本文主要是介绍漫话Redis源码之三十三,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一个函数是分配新的rax,  返回的是指针。这个函数对异常的考虑也还很全,如果获取不到内存,就返回NULL

/* Allocate a new rax and return its pointer. On out of memory the function* returns NULL. */
rax *raxNew(void) {rax *rax = rax_malloc(sizeof(*rax));if (rax == NULL) return NULL;rax->numele = 0;rax->numnodes = 1;rax->head = raxNewNode(0,0);if (rax->head == NULL) {rax_free(rax);return NULL;} else {return rax;}
}/* realloc the node to make room for auxiliary data in order* to store an item in that node. On out of memory NULL is returned. */
raxNode *raxReallocForData(raxNode *n, void *data) {if (data == NULL) return n; /* No reallocation needed, setting isnull=1 */size_t curlen = raxNodeCurrentLength(n);return rax_realloc(n,curlen+sizeof(void*));
}/* Set the node auxiliary data to the specified pointer. */
void raxSetData(raxNode *n, void *data) {n->iskey = 1;if (data != NULL) {n->isnull = 0;void **ndata = (void**)((char*)n+raxNodeCurrentLength(n)-sizeof(void*));memcpy(ndata,&data,sizeof(data));} else {n->isnull = 1;}
}/* Get the node auxiliary data. */
void *raxGetData(raxNode *n) {if (n->isnull) return NULL;void **ndata =(void**)((char*)n+raxNodeCurrentLength(n)-sizeof(void*));void *data;memcpy(&data,ndata,sizeof(data));return data;
}/* Add a new child to the node 'n' representing the character 'c' and return* its new pointer, as well as the child pointer by reference. Additionally* '***parentlink' is populated with the raxNode pointer-to-pointer of where* the new child was stored, which is useful for the caller to replace the* child pointer if it gets reallocated.** On success the new parent node pointer is returned (it may change because* of the realloc, so the caller should discard 'n' and use the new value).* On out of memory NULL is returned, and the old node is still valid. */
raxNode *raxAddChild(raxNode *n, unsigned char c, raxNode **childptr, raxNode ***parentlink) {assert(n->iscompr == 0);size_t curlen = raxNodeCurrentLength(n);n->size++;size_t newlen = raxNodeCurrentLength(n);n->size--; /* For now restore the orignal size. We'll update it only onsuccess at the end. *//* Alloc the new child we will link to 'n'. */raxNode *child = raxNewNode(0,0);if (child == NULL) return NULL;/* Make space in the original node. */raxNode *newn = rax_realloc(n,newlen);if (newn == NULL) {rax_free(child);return NULL;}n = newn;/* After the reallocation, we have up to 8/16 (depending on the system* pointer size, and the required node padding) bytes at the end, that is,* the additional char in the 'data' section, plus one pointer to the new* child, plus the padding needed in order to store addresses into aligned* locations.** So if we start with the following node, having "abde" edges.** Note:* - We assume 4 bytes pointer for simplicity.* - Each space below corresponds to one byte** [HDR*][abde][Aptr][Bptr][Dptr][Eptr]|AUXP|** After the reallocation we need: 1 byte for the new edge character* plus 4 bytes for a new child pointer (assuming 32 bit machine).* However after adding 1 byte to the edge char, the header + the edge* characters are no longer aligned, so we also need 3 bytes of padding.* In total the reallocation will add 1+4+3 bytes = 8 bytes:** (Blank bytes are represented by ".")** [HDR*][abde][Aptr][Bptr][Dptr][Eptr]|AUXP|[....][....]** Let's find where to insert the new child in order to make sure* it is inserted in-place lexicographically. Assuming we are adding* a child "c" in our case pos will be = 2 after the end of the following* loop. */int pos;for (pos = 0; pos < n->size; pos++) {if (n->data[pos] > c) break;}/* Now, if present, move auxiliary data pointer at the end* so that we can mess with the other data without overwriting it.* We will obtain something like that:** [HDR*][abde][Aptr][Bptr][Dptr][Eptr][....][....]|AUXP|*/unsigned char *src, *dst;if (n->iskey && !n->isnull) {src = ((unsigned char*)n+curlen-sizeof(void*));dst = ((unsigned char*)n+newlen-sizeof(void*));memmove(dst,src,sizeof(void*));}/* Compute the "shift", that is, how many bytes we need to move the* pointers section forward because of the addition of the new child* byte in the string section. Note that if we had no padding, that* would be always "1", since we are adding a single byte in the string* section of the node (where now there is "abde" basically).** However we have padding, so it could be zero, or up to 8.** Another way to think at the shift is, how many bytes we need to* move child pointers forward *other than* the obvious sizeof(void*)* needed for the additional pointer itself. */size_t shift = newlen - curlen - sizeof(void*);/* We said we are adding a node with edge 'c'. The insertion* point is between 'b' and 'd', so the 'pos' variable value is* the index of the first child pointer that we need to move forward* to make space for our new pointer.** To start, move all the child pointers after the insertion point* of shift+sizeof(pointer) bytes on the right, to obtain:** [HDR*][abde][Aptr][Bptr][....][....][Dptr][Eptr]|AUXP|*/src = n->data+n->size+raxPadding(n->size)+sizeof(raxNode*)*pos;memmove(src+shift+sizeof(raxNode*),src,sizeof(raxNode*)*(n->size-pos));/* Move the pointers to the left of the insertion position as well. Often* we don't need to do anything if there was already some padding to use. In* that case the final destination of the pointers will be the same, however* in our example there was no pre-existing padding, so we added one byte* plus thre bytes of padding. After the next memmove() things will look* like thata:** [HDR*][abde][....][Aptr][Bptr][....][Dptr][Eptr]|AUXP|*/if (shift) {src = (unsigned char*) raxNodeFirstChildPtr(n);memmove(src+shift,src,sizeof(raxNode*)*pos);}/* Now make the space for the additional char in the data section,* but also move the pointers before the insertion point to the right* by shift bytes, in order to obtain the following:** [HDR*][ab.d][e...][Aptr][Bptr][....][Dptr][Eptr]|AUXP|*/src = n->data+pos;memmove(src+1,src,n->size-pos);/* We can now set the character and its child node pointer to get:** [HDR*][abcd][e...][Aptr][Bptr][....][Dptr][Eptr]|AUXP|* [HDR*][abcd][e...][Aptr][Bptr][Cptr][Dptr][Eptr]|AUXP|*/n->data[pos] = c;n->size++;src = (unsigned char*) raxNodeFirstChildPtr(n);raxNode **childfield = (raxNode**)(src+sizeof(raxNode*)*pos);memcpy(childfield,&child,sizeof(child));*childptr = child;*parentlink = childfield;return n;
}

这篇关于漫话Redis源码之三十三的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683910

相关文章

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

redis+lua实现分布式限流的示例

《redis+lua实现分布式限流的示例》本文主要介绍了redis+lua实现分布式限流的示例,可以实现复杂的限流逻辑,如滑动窗口限流,并且避免了多步操作导致的并发问题,具有一定的参考价值,感兴趣的可... 目录为什么使用Redis+Lua实现分布式限流使用ZSET也可以实现限流,为什么选择lua的方式实现

Redis中管道操作pipeline的实现

《Redis中管道操作pipeline的实现》RedisPipeline是一种优化客户端与服务器通信的技术,通过批量发送和接收命令减少网络往返次数,提高命令执行效率,本文就来介绍一下Redis中管道操... 目录什么是pipeline场景一:我要向Redis新增大批量的数据分批处理事务( MULTI/EXE

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、