[Redis study notes (3)] detailed explanation of the jump table and integer collection structure

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. Skip list

(I. Overview

        The hop list is an ordered data structure, which achieves the purpose of quickly accessing nodes by maintaining multiple pointers to other nodes in the node. It supports average O(logN) and worst-case O(N) node search. The efficiency is similar to that of a balanced tree. Because it is simpler in advance, it is a benign substitute for a balanced tree. Redis only uses the jump table in two places, one is to implement an ordered set of keys, and the other is to be used as an internal data structure in the cluster nodes.

(Two) the realization of the jump table

        The Redis skip list is defined by the zskiplistNode structure and the zskiplist structure. The zskiplistNode structure represents the skip list node, and the zskiplist structure is used to store the relevant information of the skip list node.


1. zskiplist

The structure contains the following attributes:

        header: the header node of the jump table;

        tail: the tail node of the skip list;

        level: Record the level of the node with the largest level in the current hop table, excluding the head node;

        length: the length of the skip list

2. zskiplistNode

The structure contains the following attributes:

        level array: each layer of the jump table node, each layer has two attributes, one is the forward pointer, and the other is the span. The program can use these layers to speed up access to other nodes. The more the number of layers, the speed Faster. Every time a new jump table node is created, a value between 1-32 is randomly generated as the size of the level array, that is, the height of the level.

        Forward pointer: used to access other nodes located at the end of the table.
Span: record the distance between the node pointed to by the forward pointer and the current node. The span is used to calculate the ranking. In the process of finding a node, it will be visited along the way. The result of adding up the spans of all layers is the ranking of the target node in the jump table.

        Back pointer: point to the previous node located at the current node;

        Score: the branches of each node are sorted from small to large;
        member objects: each node saves member objects, the member object is a pointer to a string object (SDS), in the same jump table, each node saves The member objects of must be unique, but the scores saved by multiple nodes can be the same, and nodes with the same score will be sorted according to the size of the member objects in the lexicographical order.


two. Integer set

(I. Overview

        The integer set is one of the underlying implementations of the set key. When a set contains only integer value elements and the number of elements in this set is small, Redis will use the integer set as the underlying implementation of the set key.

(2) Realization

        Integer set is an abstract data structure used by Redis to store integer values. It can store integer values ​​of type int16_t, int32_t, int64_t, and has deduplication operations.

The structure is as follows:

typedef struct intset{
    
    
	uint32_t encoding;
	uint32_t length;
	int8_t contents[];
}intset;

        Each element in the contents array is each integer in the set, arranged in ascending order of value, and there are no duplicates;

        The length attribute records the number of elements contained in the integer set;

        The encoding attribute determines the type of items stored in the contents array;


(3) Upgrade

        Whenever we want to add a new element to the integer collection, and the type of the new element is longer than the type of the existing elements in the integer collection, the integer collection needs to be upgraded before the new element can be added to the integer collection. Proceed as follows:

        1. According to the type of the new element, expand the space size of the underlying array of the integer set, and allocate space for the new element;

        2. Convert all the existing elements of the underlying array to the same type as the new element, and place the converted element in the correct position. Note that because the size of the element is expanded, the position of the new element is determined to be the last one, then Reallocate positions from the back to the front to keep the orderliness unchanged;

        3. Add new elements to the underlying array;

        Due to the upgrade operation, the time complexity of adding a new element to the integer set is O(N). Note that the set of integers does not support downgrade operations. Once the array is upgraded, it will remain in the upgraded state.


(4) Advantages of upgrading

1. Increase flexibility

        Since the C language is a statically typed language, in order to avoid type errors, we will not put different types of values ​​in the same data structure. The upgrade using integer collections can automatically adapt to new elements without worrying about type errors.

2. Save memory

        The upgrade will only be when needed, instead of directly using the largest type in a large package, saving memory as much as possible.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113874690