Some of the technical details of Redis source are worth learning 02

Source: Internet
Author: User

1. Implementation of the hash function in Redis:

Redis uses a different hash function for integer key and string key

For integer key,redis, the Dict.c/dictinthashfunction function is implemented with the use of Thomas Wang's all-bit Mix functions:

1 /*Thomas Wang's + bit Mix Function*/2UnsignedintDictinthashfunction (unsignedintkey)3 {4Key + = ~ (Key << the);5Key ^= (Key >>Ten);6Key + = (Key <<3);7Key ^= (Key >>6);8Key + = ~ (Key << One);9Key ^= (Key >> -);Ten     returnkey; One}

The beauty of this code I did not have time to study carefully, and so the study will be here to make up, but found a two at the beginning of a good link:

The first is the link to the Thomas Wang God Himself:

Http://web.archive.org/web/20071223173210/http://www.concentric.net/~Ttwang/tech/inthash.htm

The other is the summary written by others based on the links above and other information

http://blog.csdn.net/jasper_xulei/article/details/18364313

For Key,redis in string form, the MURMURHASH2 algorithm and the DJB algorithm are used:

The MURMURHASH2 algorithm is case sensitive to key and produces inconsistent results on big-endian machines and small-end machines.

The dict.c/dictgenhashfunction of Redis is the C language implementation of the MURMURHASH2 algorithm:

1UnsignedintDictgenhashfunction (Const void*key,intLen) {2     /*' m ' and ' R ' are mixing constants generated offline.3 they ' re not really ' magic ', they just happen to work well. */4uint32_t seed =Dict_hash_function_seed;5     Constuint32_t m =0x5bd1e995;6     Const intR = -;7 8     /*Initialize the hash to a ' random ' value*/9uint32_t h = seed ^Len;Ten  One     /*Mix 4 bytes at a time into the hash*/ A     ConstUnsignedChar*data = (ConstUnsignedChar*) key; -  -      while(Len >=4) { theuint32_t k = * (uint32_t*) data; -  -K *=m; -K-^= K >>R; +K *=m; -  +H *=m; AH ^=K; at  -Data + =4; -Len-=4; -     } -  -     /*Handle The last few bytes of the input array*/ in     Switch(len) { -      Case 3: H ^= data[2] << -; to      Case 2: H ^= data[1] <<8; +      Case 1: H ^= data[0]; H *=m; -     }; the  *     /*Do a few final mixes of the "hash to ensure" the last few $ * Bytes is well-incorporated.*/Panax NotoginsengH ^= H >> -; -H *=m; theH ^= H >> the; +  A     return(unsignedint) H; the}

Redis, by virtue of the DJB function, implements a case-insensitive hash function dict.c/dictgencasehashfunction:

1UnsignedintDictgencasehashfunction (ConstUnsignedChar*buf,intLen) {2Unsignedinthash = (unsignedint) Dict_hash_function_seed;3 4      while(len--)5hash = (Hash <<5+ hash) + (ToLower (*buf++));/*Hash * + C*/6     returnHash;7}

The above three hash functions (Dictinthashfunction, Dictinthashfunction, dictgencasehashfunction) are used in different areas of Redis to achieve the hash requirements in different situations, This will be explained in more detail next.

The use of several different hashing functions in different situations in 2.Redis

Some of the technical details of Redis source are worth learning 02

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.