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

相关文章

Redis Cluster模式配置

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

Springboot整合Redis主从实践

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

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Redis指南及6.2.x版本安装过程

《Redis指南及6.2.x版本安装过程》Redis是完全开源免费的,遵守BSD协议,是一个高性能(NOSQL)的key-value数据库,Redis是一个开源的使用ANSIC语言编写、支持网络、... 目录概述Redis特点Redis应用场景缓存缓存分布式会话分布式锁社交网络最新列表Redis各版本介绍旧

Java如何从Redis中批量读取数据

《Java如何从Redis中批量读取数据》:本文主要介绍Java如何从Redis中批量读取数据的情况,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一.背景概述二.分析与实现三.发现问题与屡次改进3.1.QPS过高而且波动很大3.2.程序中断,抛异常3.3.内存消

Redis中的Lettuce使用详解

《Redis中的Lettuce使用详解》Lettuce是一个高级的、线程安全的Redis客户端,用于与Redis数据库交互,Lettuce是一个功能强大、使用方便的Redis客户端,适用于各种规模的J... 目录简介特点连接池连接池特点连接池管理连接池优势连接池配置参数监控常用监控工具通过JMX监控通过Pr

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢

Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)

《Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)》本文主要介绍了Golang分布式锁实现,采用Redis+Lua脚本确保原子性,持可重入和自动续期,用于防止超卖及重复下单,具有一定... 目录1 概念应用场景分布式锁必备特性2 思路分析宕机与过期防止误删keyLua保证原子性可重入锁自动

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3