漫话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

相关文章

Knife4j+Axios+Redis前后端分离架构下的 API 管理与会话方案(最新推荐)

《Knife4j+Axios+Redis前后端分离架构下的API管理与会话方案(最新推荐)》本文主要介绍了Swagger与Knife4j的配置要点、前后端对接方法以及分布式Session实现原理,... 目录一、Swagger 与 Knife4j 的深度理解及配置要点Knife4j 配置关键要点1.Spri

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

Redis的持久化之RDB和AOF机制详解

《Redis的持久化之RDB和AOF机制详解》:本文主要介绍Redis的持久化之RDB和AOF机制,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述RDB(Redis Database)核心原理触发方式手动触发自动触发AOF(Append-Only File)核

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模

SpringBoot连接Redis集群教程

《SpringBoot连接Redis集群教程》:本文主要介绍SpringBoot连接Redis集群教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 依赖2. 修改配置文件3. 创建RedisClusterConfig4. 测试总结1. 依赖 <de

SpringBoot+Redis防止接口重复提交问题

《SpringBoot+Redis防止接口重复提交问题》:本文主要介绍SpringBoot+Redis防止接口重复提交问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录前言实现思路代码示例测试总结前言在项目的使用使用过程中,经常会出现某些操作在短时间内频繁提交。例

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

浅析如何保证MySQL与Redis数据一致性

《浅析如何保证MySQL与Redis数据一致性》在互联网应用中,MySQL作为持久化存储引擎,Redis作为高性能缓存层,两者的组合能有效提升系统性能,下面我们来看看如何保证两者的数据一致性吧... 目录一、数据不一致性的根源1.1 典型不一致场景1.2 关键矛盾点二、一致性保障策略2.1 基础策略:更新数

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect