15 days play redis--fourth hash object type

Source: Internet
Author: User

The hash in Redis is also the high-frequency data structure that we use, its structure is basically similar to the hashtable,dictionary in the programming language, if we have any logic to use in the future

Dictionary storage, can be based on the scene first consider Redis oh, at least can be installed force, now I default you already have the urge to pretend, open the Redis Handbook, see what we use to get

Method of loading and forcing.

One: Common methods

As long as a data structure, the most basic is always curd,redis in the insert and update, always only need to replace the set, such as the following hset, such as:

I did not choose a way to carefully explain the previous articles, in fact, there is nothing to explain, like a C # Class of a method just, know to pass some of the parameters are OK, such as to

Say the Hset, it's formatted as follows:

Next, I'm working on CentOS,

[Email protected] redis-3.0.5]$ src/redis-CLI127.0.0.1:6379>Clear127.0.0.1:6379> hset person name Jack (integer)1127.0.0.1:6379>HsetPerson Age -(integer)1127.0.0.1:6379> hset person sex famale (integer)1127.0.0.1:6379> hgetall  person1)"name"2)"Jack"3)" Age"4)" -"5)"Sex"6)"Famale"127.0.0.1:6379> hkeys  person1)"name"2)" Age"3)"Sex"127.0.0.1:6379> hvals  person1)"Jack"2)" -"3)"Famale"127.0.0.1:6379>

Perhaps someone looked at the above console a little doubt, that is, there are several parameters, such as Person,name, and then the value, if you read the first article, you probably understand,

In fact, at this level of redis, it always has a key, a value, the key is always a string object, that is, the SDS object, and the value of the kind of more, there are string objects, there are queue objects,

There is this hash object, the future of the orderly set of objects and so on, if you do not understand the words, the conversion into C # language is.

1 var person=New dictionary<string,string>(); 2 person. ADD ("name","Jack"); 3 .....

Call method is so simple, the key is that you need to look at the manual from time to time, in fact, the most important thing is to understand the principle of its in the Redis source code is good.

Second: the principle of exploration

The source code of the hash is in the dict.h, the enumeration is as follows:

typedefstructDictentry {void*key; Union {void*Val;        uint64_t U64;        int64_t S64; DoubleD;    } V; structDictentry *Next;} Dictentry;typedefstructDicttype {unsignedint(*hashfunction) (Const void*key); void* (*keydup) (void*privdata,Const void*key); void* (*valdup) (void*privdata,Const void*obj); int(*keycompare) (void*privdata,Const void*key1,Const void*Key2); void(*keydestructor) (void*privdata,void*key); void(*valdestructor) (void*privdata,void*obj);} Dicttype;/*This is our hash table structure. Every dictionary has both of this as we * implement incremental rehashing, for the new 0. */typedefstructdictht {dictentry**table; unsignedLongsize; unsignedLongSizemask; unsignedLongused;} Dictht;typedefstructdict {dicttype*type; void*Privdata; DICTHT ht[2]; LongRehashidx;/*rehashing not in progress if rehashidx = =-1*/    intiterators;/*Number of iterators currently running*/} dict;/*If Safe is set to 1 this is a safe iterator, so means, you can call * Dictadd, Dictfind, and other functions agains T the dictionary even while * iterating. Otherwise It is a non safe iterator, and only dictnext () * Should being called while iterating. */typedefstructDictiterator {dict*D; Longindex; inttable, safe; Dictentry*entry, *NextEntry; /*Unsafe iterator fingerprint for misuse detection.*/    Long Longfingerprint;} Dictiterator;

The above is the source code data structure that we use hash, and then I will take a piece of the logical relationship.

1. Dict structure

1typedefstructDict {2Dicttype *type;3     void*Privdata;4DICTHT ht[2];5     LongRehashidx;/*rehashing not in progress if rehashidx = =-1*/6     intiterators;/*Number of iterators currently running*/7} dict;

This structure is the real underlying data structure of the hash, and you can see that there are 5 properties.

<1> Dicttype *type

You can see that its type is dicttype, as you can see from the above, it is defined with an enumeration structure, as follows:

1typedefstructDicttype {2Unsignedint(*hashfunction) (Const void*key);3     void* (*keydup) (void*privdata,Const void*key);4     void* (*valdup) (void*privdata,Const void*obj);5     int(*keycompare) (void*privdata,Const void*key1,Const void*key2);6     void(*keydestructor) (void*privdata,void*key);7     void(*valdestructor) (void*privdata,void*obj);8} Dicttype;

From the above data structure you can see there are some methods, but there is a very important method, that is the first hashfunction, you can see that it is to calculate the hash value,

Same as the hash value in C # dictionary.

<2> dictht Ht[2]

You may wonder why this property is an array of 2 sizes, actually using ht[0], and ht[1] is a temporary array for expanding the hash table, which is also very wonderful,

It is also very subtle, why does Redis do this??? Think about it. You might understand that there are two ways to scale up, either one-time expansion, or incremental expansion, what is the scale behind this expansion?

What do you mean? Is that I do not affect the expansion of the front end of the curd, I slowly transfer data from ht[0] to ht[1], while rehashindex to record the situation of transfer, when all the transfer

When you're done, change ht[1] to ht[0], it's that simple.

2. DICTH structure

1 struct dictht {2     dictentry **table; 3     Long size; 4     Long Sizemask; 5     Long used; 6 } dictht;

<1> Dictentry **table;

From the structure above, you can see a very important property: Dictentry **table, where table is an array, the array type is dictentry, since it is an array,

The three attributes that follow are good to understand, size is an array of sizes, sizemask and arrays, used the size of the array used, and now we're focusing on dictentry.

The array entity type above.

3. Dictentry structure

1typedefstructDictentry {2     void*key;3 Union {4         void*Val;5 uint64_t U64;6 int64_t S64;7         DoubleD;8 } V;9     structDictentry *Next;Ten} dictentry;

From this data structure you can see that there are three large attributes.

The first one is: *key: It is the key in the hash table.

The second one is: the *val of Union is the value of hash.

The third one is: *next is to prevent hash conflict using the chain means.

This principle is the same as the dictionary in C #.

I do not know if you understand it, if you summarize the above description, I can draw the following hash structure diagram.

Well, stop there and go to the company.

15 days play redis--fourth hash object 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.