Jump table data structures -skiplist Redis

In Redisin zseta composite structure:

  • Use hashto store valueand scoremappings

  • Use according jump table to provide scorethe function of sorting, and can specify the scorerange to get valuea list of

structure

zsetThe interior is a hashdictionary 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, Redisa total of 64layers that can accommodate up 2^64elements.

Each kvblock of code that is zslnode, headerin valueis NULLthe value scoreof Double.MIN_VALUE. kvBe used between the link pointer doubly linked list, in accordance with these keys to scorebe ordered, different kvfloors may be different, the higher the number of layers kv, the less the same layer kvusing pointers between series, for each layer traverse the elements are from kv headerstarting.

Common Operations

Seek

As shown in FIG, locate purple kv, first from headerthe top to start traversing traverse to the first than the ksmaller value of the node, and then continue to look down a level of the layer than the last kelement 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

Invoked zaddwhen the method, if the corresponding valueabsent, directly inserted; and only if there is already updated score, it needs to be updated.

If the new value scorewill not bring change ranking positions, there is no need to adjust the position, directly modify the elements of scorevalue to, or need to adjust the position of the node.

RedisWhen 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 scorevalue is the same as

In extreme cases, zsetall elements of scorethe same, this time to find the performance is not degraded O(n)because zsetof the sort not only consider score, if scorethe same thing will then compare the valuevalues.

Computing elements rankings

zsetCan rankobtain ranking elements, primarily because Redis, for the skiplistnode forwardpointer optimized to each forwardpointer adding spanattribute indicates the previous node along the current layer, forwardthe pointer jumps to the current node when the intermediate node number will be skipped.

With spanproperty, in calculating the ranking of an element, just to pass on all nodes of the search path spanattributes overlay can calculate the final rankvalue.

Guess you like

Origin www.cnblogs.com/jeemzz/p/11485707.html