当前位置:网站首页>Redis jump table
Redis jump table
2022-06-26 00:06:00 【Just put a flower in heaven and earth】
brief introduction
By layering some nodes of an ordered set , Start with the top layer and search backwards , If the next The node is larger than the value to find or next The node is NULL, Start from this node , Lower one level and continue to look backward , By analogy , If found, the node is returned ; Otherwise return to NULL. Use this principle to find nodes , When the number of nodes is large , Some nodes can be skipped , Query efficiency is greatly improved , This is the basic idea of jump table .
Implementation process of jump table

Jump tables have the following properties :
- The jump table consists of many layers
- The jump table has a header node , There is one in the header node 64 Layer structure , The structure of each layer contains a pointer to the next node of this layer , The number of nodes spanning the next node in this layer is span
- Except for the head node , The layer height of the node with the most layers is The height of the jump table (level)
- Each layer is an ordered list , Data increment
- except header Outside the node , An element appears in the upper ordered linked list , Then it must appear in the lower ordered linked list
- The last node of each layer of the jump table points to NULL, Indicates the end of the ordered linked list of this layer
- The jump table has a tail The pointer , Point to the last node of the jump table .
- The bottom level ordered linked list contains all nodes , The number of nodes at the bottom layer is The length of the jump table (length)( Excluding header nodes )
- Each node contains a backward pointer , The head node and the first node point to NULL; Other nodes point to the previous node at the bottom
Each node of the jump table maintains multiple pointers to other nodes , So look it up in the jump table 、 Insert 、 Some nodes can be skipped when deleting , Quickly find the nodes needed for the operation . in the final analysis , Jump table is to achieve the purpose of fast search by sacrificing space . The jump table is compared with the balance tree , The implementation is simpler , Just be familiar with the ordered linked list , You can easily master the jump table .
Structure of jump table and jump table nodes
Jump table node structure
Jump table node zskiplistNode The structure is as follows :
typedef struct zskiplistNode {
sds ele;
double score;
struct zskiplistNode *backward;
struct zskiplistLevel {
struct zskiplistNode *forward;
unsigned int span;
} level[];
} zskiplistNode;
This structure contains the following attributes :
- ele: Used to store string type data .
- score: Used to store sorting scores .
- backward: Back pointer , It can only point to the previous node at the bottom of the current node , The head node and the first node ----backward Point to NULL, Use... When traversing the jump table from back to front .
- level: Is a flexible array . The length of each node array is different , When generating jump table nodes , Randomly generate one 1~64 Value , The higher the value, the lower the probability of occurrence .
level Each item of the array contains the following two elements :- forward : Point to the next node in this layer , Tail node forward Point to NULL.
- span:forward The number of elements between the pointed node and this node .span The bigger the value is. , The more nodes skipped .
Jump table structure
In addition to the jump table node , You also need a jump table structure to manage nodes ,Redis Use zskiplist Structure , as follows :
typedef struct zskiplist {
struct zskiplistNode *header, *tail;
unsigned long length;
int level;
} zskiplist;
This structure contains the following attributes :
- header: Point to the jump table node . The head node is a special node of the jump table , its level The number of array elements is 64. The head node does not store any information in an ordered set member and score value ,ele The value is NULL,score The value is 0; It is not included in the total length of the jump table . When the header node is initialized ,64 An element of forward All point to NULL,span Values are 0.
- tail: Point to the jump footer node .
- length: Jump table length , Represents the total number of nodes except the head node .
- level: The height of the jump table .
You can know from the properties of the jump table structure , The program can be O(1) Under the time complexity of , Get the head node of the jump table quickly 、 Tail node 、 Length and height .
Basic operation of jump table
Create a jump table
Redis5 The minimum height of middle node floor is 1, The maximum value is 64.
Redis Randomly generate a 1~64 Value , As the height of the new node , The higher the value, the lower the probability of occurrence .** After the node layer height is determined, it will not be modified .
Each node of the jump table is an element of an ordered set , When creating jump table nodes , The floor height of the node to be created 、 The score is 、member And so on . For each node of the jump table , We need to apply for memory to store .
zskiplistNode The last element of the structure is the flexible array , When applying for memory, you need to specify the size of the flexible array , The memory occupied by a node is zskiplistNode The memory size is the same as level individual zskiplistLevel The sum of the memory sizes of .
After allocating space , Initialize node variables .
The head node is a special node , Do not store ordered sets of member Information . The head node is the first inserted node in the jump table , Its level Each item of the array forward All for NULL,span Values are 0.
After creating the header node , You can create a jump table :
- Create a jump table structure object zsl.
- take zsl The header node pointer of points to the newly created header node .
- The jump surface height is initialized to 1, The length is initialized to 0, The tail node points to NULL.
Insert node
Steps to insert a node :
- Find the location to insert
- Adjust the height of the skip meter
- Insert node
- adjustment backward
Delete node
To delete a node :
- Find the node that needs to be updated
- Set up span and forward
Delete jump table
After getting the jump table object , The... Of the ab initio node 0 Layer start , adopt forward The pointer traverses backwards step by step , Each time a node is encountered, it frees its memory . When the memory of all nodes is released , Release jump table object , That is, the deletion of the jump table is completed .
Learning links
边栏推荐
- Given the parameter n, there will be n integers 1, 2, 3,... From 1 to n, n. These n arrays have n! An arrangement that lists all columns in ascending order of size and marks them one by one. Given n a
- STEP7 master station and remote i/o networking_ Old bear passing by_ Sina blog
- Studio5k V28 installation and cracking_ Old bear passing by_ Sina blog
- Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
- Keil compilation run error, missing error: # 5: # includecore_ cm3.h_ Old bear passing by_ Sina blog
- Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification
- About Simple Data Visualization
- 关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案
- 【微信公众号H5】 生成带参数进入公众号关注页的二维码 监听用户关注公众号事件 自定义菜单栏 (服务端)
- Final and static
猜你喜欢

我的博客今天2岁167天了,我领取了先锋博主徽章_过路老熊_新浪博客

Connecting MySQL database with VBScript_ Old bear passing by_ Sina blog

10.4.1 data console

(转载)进程和线程的形象解释

数组常用的一些操作方法

文献调研(四):基于case-based reasoning、ANN、PCA的建筑小时用电量预测

keil编译运行错误,缺少error:#5:#includecore_cm3.h_过路老熊_新浪博客

用frp搭建云电脑

迅为RK3568开发板使用RKNN-Toolkit-lite2运行测试程序

Hand made pl-2303hx USB to TTL level serial port circuit_ Old bear passing by_ Sina blog
随机推荐
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
About the solution to prompt modulenotfounderror: no module named'pymongo 'when running the scratch project
Several common rich text editors
Unsigned and signed vernacular
P3052 [USACO12MAR]Cows in a Skyscraper G
10.2.3、Kylin_ The dimension is required for kylin
Smt贴片加工出现元件立碑的解决方法
Talk about the copy on write mechanism of PHP variables or parameters
文献调研(一):基于集成学习和能耗模式分类的办公楼小时能耗预测
P3052 [USACO12MAR]Cows in a Skyscraper G
Network protocol: detailed explanation of redis protocol
Shredding Company poj 1416
ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided
About Simple Data Visualization
10.4.1 data console
huibian
Keil compilation run error, missing error: # 5: # includecore_ cm3.h_ Old bear passing by_ Sina blog
Let's talk about string today
猕猴桃酵素的功效_过路老熊_新浪博客
huibian