Redis源码分析之工具类util

在redis源码中的辅助工具类中,主要包括大小端转换、SHA算法以及util.h中对应的算法。

大小端转换:

LittleEndian:低位字节数据存放于低地址,高位字节数据存放于高地址。

BigEndian:低位字节数据存放于高地址,高位字节数据存放于低地址。

Linux系统中有自带的大小端转换方法,16位、32位的转换,但是没有针对64位的转换,但是redis中加入了对64位数据的大小端转换方法,api接口如下:

void memrev16(void *p);
void memrev32(void *p);
void memrev64(void *p);
uint16_t intrev16(uint16_t v);
uint32_t intrev32(uint32_t v);
uint64_t intrev64(uint64_t v);

其中,以64位为例:

void memrev64(void *p) {unsigned char *x = p, t;t = x[0];x[0] = x[7];x[7] = t;t = x[1];x[1] = x[6];x[6] = t;t = x[2];x[2] = x[5];x[5] = t;t = x[3];x[3] = x[4];x[4] = t;
}

x[7]与x[0]、x[6]与x[1]、x[5]与x[2]、x[4]与x[3]进行互换,是不是很简单,一目了然。

在Redis中,加密算法的实现使用的是SHA算法。SHA是安全hash算法,与md5一样,都是属于消息摘要算法,底层实现的机制是hash,不可逆的,在加密长度上,做了很大的扩展,安全性也更高长度不超过2^64位的字符串或二进制流,最终生成一个20Byte的摘要。

typedef struct {uint32_t state[5];uint32_t count[2];unsigned char buffer[64];
} SHA1_CTX;void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]);
void SHA1Init(SHA1_CTX* context);
void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len);
void SHA1Final(unsigned char digest[20], SHA1_CTX* context);

测试方法或者调用方法如下:

int sha1Test(int argc, char **argv)
{SHA1_CTX ctx;unsigned char hash[20], buf[BUFSIZE];int i;UNUSED(argc);UNUSED(argv);for(i=0;i<BUFSIZE;i++)buf[i] = i;SHA1Init(&ctx);for(i=0;i<1000;i++)SHA1Update(&ctx, buf, BUFSIZE);SHA1Final(hash, &ctx);printf("SHA1=");for(i=0;i<20;i++)printf("%02x", hash[i]);printf("\n");return 0;
}

util.c通用工具类的算法实现,具体的API,主要涉及的是数字和字符串之间的转换,如下:

#define MAX_LONG_DOUBLE_CHARS 5*1024int stringmatchlen(const char *p, int plen, const char *s, int slen, int nocase);
int stringmatch(const char *p, const char *s, int nocase);
int stringmatchlen_fuzz_test(void);
long long memtoll(const char *p, int *err);
uint32_t digits10(uint64_t v);
uint32_t sdigits10(int64_t v);
int ll2string(char *s, size_t len, long long value);
int string2ll(const char *s, size_t slen, long long *value);
int string2l(const char *s, size_t slen, long *value);
int string2ld(const char *s, size_t slen, long double *dp);
int d2string(char *buf, size_t len, double value);
int ld2string(char *buf, size_t len, long double value, int humanfriendly);
sds getAbsolutePath(char *filename);
unsigned long getTimeZone(void);

根据函数的名字就可以知晓该函数的作用,其中有一个方法是ll2string(),将long long型数字转字符的方法,正常的做法,就是除10取余,加上对应的数字字符,但是要转换的可是long long型数字,长度非常长,效率会导致比较低,所以在Redis中,直接按除100算,2位,2位的赋值,而且用数字字符数字,做处理,直接按下标来赋值,避免了对余数的多次判断,代码如下:

/* Convert a long long into a string. Returns the number of* characters needed to represent the number.* If the buffer is not big enough to store the string, 0 is returned.** Based on the following article (that apparently does not provide a* novel approach but only publicizes an already used technique):** https://www.facebook.com/notes/facebook-engineering/three-optimization-tips-for-c/10151361643253920** Modified in order to handle signed integers since the original code was* designed for unsigned integers. */
int ll2string(char *dst, size_t dstlen, long long svalue) {static const char digits[201] ="0001020304050607080910111213141516171819""2021222324252627282930313233343536373839""4041424344454647484950515253545556575859""6061626364656667686970717273747576777879""8081828384858687888990919293949596979899";int negative;unsigned long long value;/* The main loop works with 64bit unsigned integers for simplicity, so* we convert the number here and remember if it is negative. */if (svalue < 0) {if (svalue != LLONG_MIN) {value = -svalue;} else {value = ((unsigned long long) LLONG_MAX)+1;}negative = 1;} else {value = svalue;negative = 0;}/* Check length. */uint32_t const length = digits10(value)+negative;if (length >= dstlen) return 0;/* Null term. */uint32_t next = length;dst[next] = '\0';next--;while (value >= 100) {int const i = (value % 100) * 2;value /= 100;dst[next] = digits[i + 1];dst[next - 1] = digits[i];next -= 2;}/* Handle last 1-2 digits. */if (value < 10) {dst[next] = '0' + (uint32_t) value;} else {int i = (uint32_t) value * 2;dst[next] = digits[i + 1];dst[next - 1] = digits[i];}/* Add sign. */if (negative) dst[0] = '-';return length;
}

digit[201]就是从00-99的数字字符,余数的赋值就通过这个数组,高效,方便,是提高了很多的速度。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463438.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux下如何安装软件

一、解析Linux应用软件安装包通常Linux应用软件的安装包有三种&#xff1a;1&#xff09; tar包&#xff0c;如software-1.2.3-1.tar.gz。它是使用UNIX系统的打包工具tar打包的。2&#xff09; rpm包&#xff0c;如software-1.2.3-1.i386.rpm。它是RedHat Linux提供的一种包封装…

Python深浅拷贝辨析

1 import copy2 3 list1 [11, 22, [33, 44]]4 list2 list15 list3 list1[:]6 list4 copy.copy(list1)7 list5 copy.deepcopy(list1)8 9 list1[0] 0 # 对列表的首层做增删改查操作 10 print("list1:",id(list1),list1) # list1: 1455502266696 [0, 22, […

生活规则

1.朋友请你吃饭&#xff0c;不要觉得理所当然&#xff0c;请礼尚往来&#xff0c;否则你的名声会越来越臭。 2.给自己定目标&#xff0c;一年&#xff0c;两年&#xff0c;五年&#xff0c;也许你出生不如别人好&#xff0c;通过努力&#xff0c;往往可以改变70%的命运。破罐子…

[AX]AX2012 SSRS报表使用Report Data Method

在AX2012的SSRS报表中可以使用c#或者Visual basic .net编写report data method来获取和操作数据&#xff0c;由report data method返回的数据可以用在报表的表达式中&#xff0c;也可以用作dataset的数据源。 使用Report data method首先需要创建AX model工程&#xff0c;在工程…

HIVE和HBASE区别

转载&#xff1a;https://www.cnblogs.com/justinzhang/p/4273470.html 1. 两者分别是什么&#xff1f; Apache Hive是一个构建在Hadoop基础设施之上的数据仓库。通过Hive可以使用HQL语言查询存放在HDFS上的数据。HQL是一种类SQL语言&#xff0c;这种语言最终被转化为Map/Re…

php调试

今天在使用php 的session 的时候&#xff0c;出现了以前就遇见但是又解决不了的问题&#xff0c;在页面上出现如下提示&#xff1a; Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at E:\php…

C 结构体

C 结构体C 数组允许定义可存储相同类型数据项的变量&#xff0c;结构是 C 编程中另一种用户自定义的可用的数据类型&#xff0c;它允许您存储不同类型的数据项。结构用于表示一条记录&#xff0c;假设您想要跟踪图书馆中书本的动态&#xff0c;您可能需要跟踪每本书的下列属性&…

lightoj1259 线性筛的另一种写法 v变成bool标记数组

也是用线性筛&#xff0c;但是v用int会爆&#xff0c;所以这个线性筛用的是另外一种写法 #include<cstdio> #include<cmath> #include<queue> #include<vector> #include<cstring> #include<iostream> #include<algorithm> using na…

Redis基数统计之HyperLogLog小内存大用处

转载&#xff1a;https://blog.csdn.net/azhegps/article/details/71158952 我们一直都知道&#xff0c;redis几大常用数据结构&#xff0c;字符串、散列、列表、集合、有序集合。其实后来Redis做了很多补充&#xff0c;其中之一就是HyperLogLog&#xff0c;另外的还有GEO&…

matlab中的qr函数

转自&#xff1a;https://blog.csdn.net/qq278672818/article/details/62038630 实数矩阵A的QR分解是把A分解为A QR这里的Q是正交矩阵&#xff08;意味着QTQ I&#xff09;而R是上三角矩阵。类似的&#xff0c;我们可以定义A的QL, RQ和LQ分解。更一般的说&#xff0c;我们可以…

(String) 和 String.valueOf() 两种字符串转换的区别

使用 String.valueOf() 进行数据转换&#xff0c;如果被转换的数据为 null, 则这种方法将返回一个 "null" 字符串 &#xff08;String&#xff09; 方法进行转换时&#xff0c;如果被转换的数据为 null, 则返回 null 对象而不是一个 "null" 字符串。转载于…

利用有名管道实现进程间的通信

1 /*****************************************************************2 * Copyright (C) 2018 FBI WARNING. All rights reserved.3 * 4 * 文件名称&#xff1a;fifo_write.c5 * 创 建 者&#xff1a;constantine6 * 创建日期&#xff1a;2018年02月26日7 * 描 …

为什么分布式一定要有redis,redis的一些优缺点

1、为什么使用redis 分析:博主觉得在项目中使用redis&#xff0c;主要是从两个角度去考虑:性能和并发。当然&#xff0c;redis还具备可以做分布式锁等其他功能&#xff0c;但是如果只是为了分布式锁这些其他功能&#xff0c;完全还有其他中间件(如zookpeer等)代替&#xff0c;…

Google protobuf使用技巧和经验

Google protobuf是非常出色的开源工具&#xff0c;在项目中可以用它来作为服务间数据交互的接口&#xff0c;例如rpc服务、数据文件传输等。protobuf为proto文件中定义的对象提供了标准的序列化和反序列化方法&#xff0c;可以很方便的对pb对象进行各种解析和转换。以下是我总结…

show部分书...

继续购入中 转载于:https://www.cnblogs.com/Clingingboy/archive/2009/06/09/1499816.html

linux 中用PPA安装软件

一般来说 PPA 提供了三条命令&#xff0c;如下面的命令&#xff1a; sudo apt-get sudo apt-get update sudo apt-get install 其中&#xff0c;第一行的代码后面加上 获得安装软件的地址 第二行为更新系统源地址 第三行为安装软件

HTTP_POST———使用mysql_udf与curl库完成http_post通信模块(mysql_udf,multi_curl,http,post)...

HTTP_POST———使用mysql_udf与curl库完成http_post通信模块&#xff08;mysql_udf,multi_curl,http,post&#xff09; 这个模块其目前主要用于xoyo江湖的sns与kingsoft_xoyo自主研发的TCSQL数据库做数据同步&#xff0c;当有feed插入sns数据库&#xff0c;使用触 发器调用该模…

LSM树存储模型

LSM&#xff08;log-structed-merge-tree&#xff09; leveldb和rocksdb底层使用LSM树做存储引擎&#xff0c;LSM树使用skiplist做索引&#xff0c;他们先将数据写入内存中&#xff0c;按照key进行划分&#xff0c;定期的merge写入到磁盘中&#xff0c;合并后数据写入下一层le…

js-图片预加载

//图片预加载 //闭包模拟局部作用于(function($){function Preload(imgs,options){this.imgs (typeof imgs string) ? [imgs]:imgs;this.opts $.extend({},Preload.DEFAULTS,options);if(this.opts.order ordered){//有序加载this._ordered();}else{//无序加载this._unord…

LevelDb实现原理

原文地址&#xff1a;http://www.samecity.com/blog/Index.asp?SortID12&#xff0c; 最近由于工作上的需求&#xff0c;需要用到leveldb&#xff0c;因此转载此文章用于以后的查询使用。 LevelDb日知录之一&#xff1a;LevelDb 101 说起LevelDb也许您不清楚&#xff0c;但是…