In Redis
in zset
a composite structure:
-
Use
hash
to storevalue
andscore
mappings -
Use according jump table to provide
score
the function of sorting, and can specify thescore
range to getvalue
a list of
structure
zset
The interior is a hash
dictionary plus a jump tableskiplist
struct zslnode { String value; Double Score; zslnode * [] Forwards; // multilayer connection pointer zslnode Backward * // back pointer } zslnode; struct ZSL { zslnode * header; // jump table pointer int maxLevel; // current top jump table Map < String , zslnode *> HT; // all keys hash to the structure } zsl;
The picture shows a jump of intentions, in fact, Redis
a total of 64
layers that can accommodate up 2^64
elements.
Each kv
block of code that is zslnode
, header
in value
is NULL
the value score
of Double.MIN_VALUE
. kv
Be used between the link pointer doubly linked list, in accordance with these keys to score
be ordered, different kv
floors may be different, the higher the number of layers kv
, the less the same layer kv
using pointers between series, for each layer traverse the elements are from kv header
starting.
Common Operations
Seek
As shown in FIG, locate purple kv
, first from header
the top to start traversing traverse to the first than the k
smaller value of the node, and then continue to look down a level of the layer than the last k
element smaller, and so on, until Find up to the elements.
In the middle of a series of nodes called the search path when searching, it is from the top until the last one is smaller than the target node node list of elements in each layer of the bottom.
insert
After the new node is inserted, you first need to search for a suitable insertion point, similar to the search process to find the right node can start to create a new node. It is randomly assigned a number of layers required when creating the node, the new node and the node on the search path and then concatenated by the front and rear pointers.
If the new node allocation table than the current high jump maximum height, then, need to be updated about the maximum height of the jump table.
delete
Deletion and insertion process similar to the process, you need to first find out the search path, then the relevant node for each layer, we need to look at before and after the rearrangement pointer, taking care to update it the highest rise maxLevel
.
Update
If the new value score
will not bring change ranking positions, there is no need to adjust the position, directly modify the elements of score
value to, or need to adjust the position of the node.
Redis
When updating the node position using the delete element, and then insert this element method, so that no need to adjust the position is determined, only requires two route search.
If the score
value is the same as
In extreme cases, zset
all elements of score
the same, this time to find the performance is not degraded O(n)
because zset
of the sort not only consider score
, if score
the same thing will then compare the value
values.
Computing elements rankings
zset
Can rank
obtain ranking elements, primarily because Redis
, for the skiplist
node forward
pointer optimized to each forward
pointer adding span
attribute indicates the previous node along the current layer, forward
the pointer jumps to the current node when the intermediate node number will be skipped.
With span
property, in calculating the ranking of an element, just to pass on all nodes of the search path span
attributes overlay can calculate the final rank