15 Days play redis--sixth article ordered collection type

Source: Internet
Author: User

Today, let's talk about the last data type in Redis, "ordered collection type", looking back at several data structures that I've learned before, and wondering if you will be heartily lamenting that the open source world is really good, write this

Some code of good-hearted people really want to live a life, no matter what we want to not think of things, in this world has existed, once, we want to put all the data in accordance with the data structure model

into memory, however, in order to achieve a memory-sharing approach, the memory has to be packaged as a separate deployment of WCF, but also to consider how to serialize, when the sequence of problems, too much trouble

Too many ... Later only to know that there is a redis such hanging hair, can be advanced, low-level data structure packaged separately into a shared memory (Redis), advanced data structure, is the

The "ordered set", which corresponds to the sortdictionary in C #, I'll talk about it in detail.

One: Ordered set (SortedSet)

Perhaps some of the first contact with the SortedSet collection may say, what are the usage scenarios for this collection??? I can tell you clearly that the predators of "range finder" are "ordered collections",

any In large data volumes, the time complexity of finding a range is always o[(Logn) +m], where M: the number of elements returned.

To get from easy to difficult, let's take a look at the Redis handbook and select a few of our common methods to observe and observe the effects ...

From the above 17 commands, there is no doubt that the commonly used commands are zadd,zrem,zrangebyscore,zrange.

1. Zadd

Zadd key score member [[Score member] [SCORE member] ...] Adds one or more member elements and their score values to the ordered set key.

This is the official explanation, the assignment is similar to Hashtable, but the key here is orderly. Let me give you an example: I have a fruits collection, which records every water

The price of the fruit, and then I obtain the corresponding fruit information according to the various actions of price.

With the basic information above, I'll send them to the sortedset in one step, such as:

From the above picture, do not know you have found anything unusual??? There are at least two kinds.

<1> floating-point approximation problems, such as grape, when I add, I specify 2.8, in the Redis show me the approximate value of 2.79999 .... That's okay, that's what it is.

<2> By default, SortedSet is stored in ascending order of key.

2. Zrange,zrevrange

Zrange key start stop [Withscores] returns the member of the ordered set key, within the specified interval. The positions of the members are sorted by increasing the score value (from small to large).

Above is Zrange format template, the front I said Zadd when in fact I have said, but this is not the focus, in saying Zadd left a problem is Zrange

The default is to sort by key in ascending order, right, then if you want to display in reverse, how to do??? In fact, you can use the Zrange image method Zrevrange, such as:

3. Zrangebyscore

Zrangebyscore key min Max [withscores] [LIMIT offset count] Returns the ordered set key, where all score values are between Min and Max (including equals min or Max) members. Ordered set members are arranged in ascending order of score values (from small to large).

This is the most important method for the SortedSet, the beginning of the article I also said that the order of the most favorable range search, since it is to find, you have to have the conditions, right, I give an example:

<1> I'm going to find a 1-4-dollar fruit category, and I'll certainly find "grapes, apples," such as:

1 127.0.0.1:6379> Zrangebyscore Fruits1 4Withscores2 1)"Grape"3 2)"2.7999999999999998"4 3)"Apple"5 4)"3.5"6 127.0.0.1:6379>

<2> I'm going to find out which of the 1-4 in the range is the closest 4 pieces of fruit??? The problem is to find the apple option, what if we find it??? Think about it, I can do this,

Reverse all the numbers in the 1-4 interval and take the first one, right, like this:

127.0.0.1:6379> Zrevrangebyscore Fruits4 1Withscores1)"Apple"2)"3.5"3)"Grape"4)"2.7999999999999998"127.0.0.1:6379> Zrevrangebyscore Fruits4 1Withscores limit0 11)"Apple"2)"3.5"127.0.0.1:6379>

4. Zrem

Zrem key member [member ...] Removes one or more members from an ordered set key, and non-existent members are ignored. An error is returned when key exists but is not an ordered set type.

As with other methods, the purpose of Zrem is to delete the specified value member, for example, here I want to delete scores=3.5 's apple record.

127.0.0.1:6379>zrem Fruits Apple (integer)1127.0.0.1:6379> Zrange Fruits0-1Withscores1)"Grape"2)"2.7999999999999998"3)"Pear"4)"4.0999999999999996"5)"Banana"6)"5"7)"Nut"8)"9.1999999999999993"127.0.0.1:6379>

You will find that there is no Apple related record, because it has been deleted by me ...

Second: the principle of exploration

Simple operation has been demonstrated, and then explore the next sortedset in the end is what data structure support, we should have heard, sortedset in the curd of the amortization also analysis

Is the complexity of log (N), can be comparable with the balanced binary tree, it is 1987 years to come out of the new high-efficiency data structure "jumping table (skiplist)", Skiplist is the place to jump out of the tree mold

Type of thinking, the time complexity of log (N) is constructed with the mode of multi-level linked list, the height of the layer is increased or not, the pattern of random number is used, and this is the same as the idea of "treap tree" to keep the "tree"

Or the "linked list" balance.

Detailed I will not say Ah, otherwise it is an article, if not to understand, we can see Baidu Encyclopedia: Http://baike.baidu.com/link?url=I8F7T

w933zjiebea_-dw9kensfkxmni0idwnb10n1qnvfroh_ubzcupgwnvgrpfw3ickhewgayjm_o51xchs8a

I've probably seen this picture in the encyclopedia, just like this:

There are three strands in this picture, right, in skiplist it is necessary to ensure that the data in each chain must be in order.

1. If you want to find node 6 in the Level1 layer, then you need to traverse each step, need 6 times to find the data correctly.

2. If you find node 6 in the LEVEL2 layer, then you need 4 times to find it.

3. If you find node 6 in the LEVEL3 layer, then you need 3 times to find ....

Now macro understanding, is not there is a feeling, if level of the higher layer, relative to find the data need to traverse the less number, right, this is the idea of jumping table, otherwise how to jump ha,

Next we look at how Redis defines this skiplist, its source code in REDIS.H:

1 /*Zsets Use a specialized version of Skiplists*/2typedefstructZskiplistnode {3RobJ *obj;4     Doublescore;5     structZskiplistnode *backward;6     structZskiplistlevel {7         structZskiplistnode *forward;8Unsignedintspan;9 } level[];Ten } Zskiplistnode; One  AtypedefstructZskiplist { -     structZskiplistnode *header, *tail; -UnsignedLonglength; the     intLevel ; -} zskiplist;

The following points can be seen from the source code:

<1> Zskiplistnode is the nodes node in Skiplist, there is a level[] array in the node, and if you're smart enough, you should know that this level[] is stored in

The level1,level2,level3 of these three chains.

<2> level[] Inside is the zskiplistlevel entity, which has a *forward pointer that points to subsequent nodes in the same layer.

<3> in Zskiplistlevel There is also a robj type of *obj pointer, this is the Redisobject object ha, which is stored in our value, and then there is a

Score property, this is the key value ... Skiplist is based on it to sort of ha.

<4> Next is the second enumeration zskiplist, this is not interesting, pure packaging layer, such as the length inside the record skiplist in the number of nodes, Level records skiplist

The current number of layers, using *header,*tail to record the first and tail nodes in the skiplist ... That's all ...

Well, whether you understand it or not, it's basically the case ... Go to work! ~ ~

15 Days play redis--sixth article ordered collection type

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.