Redis SortedSet Implementation principle

Source: Internet
Author: User
Tags data structures redis sort

The data structures supported in Redis are much more than memcached, such as basic strings, hash tables, lists, collections, sortable sets, as well as the various operations on the data structure, which is an important reason for Redis to be popular. Of course, the reason Redis can be popular, far more than this one, such as support high concurrency of Read and write, data persistence, efficient memory management and elimination mechanism ...

From the Git commit history of Redis, it can be found that in 2009/10/24 in version 1.050, Redis began to support sortable sets, in which only one command Zadd was provided, and the macro definition was as follows:

1     {"Zadd", zaddcommand,4,redis_cmd_bulk| Redis_cmd_denyoom},

So what is a sortable set? From the beginning of Redis 1.0, we have provided the collection (set) of this data structure, the set is a mathematical concept of the set is a "disorder, certainty, cross-specific", the elements in the collection can not guarantee the order of elements, and business needs, may be more than a set, It also requires the ability to quickly sort the elements of a collection, so that a data structure such as a sortable set is provided in Redis, and it seems reasonable to add a sort function on the basis of the collection, and perhaps someone will ask that there is no sort command in Redis. The following action is not the same can be achieved on the unordered set of the sorting function well, yes, it is possible, but here we have been emphasizing the fast two words, and the time complexity of the sort command is O (N+m*log (M)), the sortable set to get a certain range of elements in the time Complexity O (Log (N) + M )

root@bjpengpeng-virtualbox:/home/bjpengpeng/redis-3.0.1/src#./REDIS-CLI 
127.0.0.1:6379> Sort set
1) "1 "
2)" 2 "
3)" 3 "
4)" 5 "
127.0.0.1:6379> sort set desc
1)" 5 "
2)" 3 "
3)" 2 "
4)" 1 "
  127.0.0.1:6379>

Before you understand how sortable sets are implemented, you need to understand a data structure jump table (skip list), jump table with AVL, red black tree ... The data structure is simple, the algorithm is easy to understand, but the time complexity of the query is equal to the balance binary tree/red black tree, and the basic structure of the jumping table is shown in the figure below.

In the image above, the entire jumping table structure holds 4 elements 5->10->20->30, the red line in the figure represents the Find element 30 o'clock, the path to find the route, from the head pointer array in the top-level pointer refers to the 20 start comparison, compared with the normal list lookup, the query can jump the table of elements , the above figure in Query 30, found 30:20 large, then the lookup is 20 start, and ordinary linked list of the query must be an element of a comparison, time complexity of O (n)

With the basic structure of the jump table shown in the figure above, and then see how to insert elements into the jump table, insert elements into the jump table, because of the randomness of the level of the element, the average is O (logn), in plain view, is to find where the element should be inserted, and then the normal move pointer problem, Think about the insertion of the ordered single-linked list, the time complexity is also O (n), the following figure is the process of inserting element 28 into the jump table, the red line in the figure represents the process of finding the insertion position, the green Line represents the movement of the pointer, the element is inserted into

With the jump table to find and insert then look at how to delete the element in the jump table, skip the table to delete the elements of the process, find the element to be deleted, find, move the pointer, as shown in the following figure, delete element 30

With the above jump table basic structure diagram and principle, self design and implementation of the Jump table bar, so that when you see the structure of the jump table in Redis we will be more familiar with, easier to understand, "below is the data structure of the jump table in Redis and related code to form the code of the run-down", The basic data structure that defines the jump table is as follows

1 2 3 4 5 6 7 8 9 All in one, #include <stdio.h >
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.