String data storage structures Redis

Copyright: please indicate the source https://blog.csdn.net/shiyan719902675/article/details/90812442

Preface:

  Redis In use, the operator of our most commonly used set key value, or get key value. It contains the redis basic data types: String, redis string type is the most basic type, it can store any string of the form, including binary data (JSON, Image ...).

I have not thought about redis through which data structure to store the data it? Today to bring you a closer look.

Open Source:

  We downloaded open source package redis into the src directory, thinking, how do you know which one is the structure type String source of it? Then we slowly find, slowly find, which structure type String source like it? Oh, Big Boss tinkling, found this sds.c like String source, ha ha ha ha ha ha ha ha ha ha ha ha ha, really witty! Real-time proved that this is an extremely inefficient way.

  The right way open source is to find the official documentation. In Redis official page, there are Quick links, there are official Github. We enter GitHub. In GitHub, there is a brief description of the source code. We turned down, find the following description:

  

We can see very clearly the official introduction, sds.c is Redis string library. Next, we will be happy to see the source code.

Source analysis:

part1: sds.h

  In the source package, there sds.c and sds.h file. In the C language, .h file header file usually, .c source file. In the source file can be called variable defined in the header file, the structure, and some other data or data type. So let's look at the header file defines data types.

   

 

 

It defines five structures in the file header, namely: sdshdr5, sdshdr8, sdshdr16,32,64, the same type of data structure in each (of course, different from the defined length). Each parameter specific meaning (not to consider sdshdr5, above written very clearly, sdshdr5 is never used.):

  • len:表示当前sds的长度,
  • alloc:表示为sds分配的内存大小
  • flag:用来表示当前sds的类型。如上图所示 001,010,011,100分别为8,16,32,64
  • char buf[]:sds实际存放的数据

 

当然,头文件中还定义了许多方法,通过名称我们可以大概知其意。如:static inline size_t sdslen(const sds s)  获取sds的长度, static inline void sdsinclen(sds s, size_t inc) 长度+1,还有许多。

part2:sds.c

  在sds.c中,引用了sds.h中定义的数据结构,已申明的方法和已实现的方法等。此文件中主要定义了对sds数据结构的具体操作,如:初始化方式,设置sds的len,等一些列操作,感兴趣的可以具体研究下源码。此处不一一详解啦(水平有限,误导不好)。

 

 

结束语

  通过本文,了解了Redis中存储String类型采用的数据结构,以及数据结构中具体的数据,参数等,还有String 是如何操作的。希望对大家有帮助, 谢谢!

 

Guess you like

Origin blog.csdn.net/shiyan719902675/article/details/90812442