Example of consistent HASH algorithm implemented by PHP and consistent hash Algorithm

Source: Internet
Author: User

Example of consistent HASH algorithm implemented by PHP and consistent hash Algorithm

This article describes the consistent HASH algorithm implemented by PHP. We will share this with you for your reference. The details are as follows:

<? Php // + example // | Perfect Is Shit // + -------------------------------------------------------------------- // | PHP implementation: consistent HASH algorithm // + example // | hoautr: alexander <gt199899@gmail.com> // + -------------------------------------------------------------------------- // | Datetime: 2017 -01-11 16:01:36 // + -------------------------------------------------------------------------- // | Copyright: perfect Is Shit // + -------------------------------------------------------------------------- class ConsistentHashing {// ring // hash-> node private $ _ ring = array (); // All nodes // node-> hash public $ nodes = array (); // virtual node public $ virtual = 64 for each node; /*** construct * @ param array $ node list initialized by nodes */public fun Ction _ construct ($ nodes = array () {if (! Empty ($ nodes) {foreach ($ nodes as $ value) {$ this-> addNode ($ value );}}} /*** get the ring content * @ return array $ this-> _ ring */public function getRing () {return $ this-> _ ring ;} /*** time33 function * @ param string $ str * @ return 32-bit positive integer * @ author */public function time33 ($ str) {// hash (I) = hash (I-1) * 33 + str [I] // $ hash = 5381; # Set hash to 0, it is better than set to 5381 distribution !!! $ Hash = 0; $ s = md5 ($ str); // compared with other versions, md5 encryption is performed $ seed = 5; $ len = 32; // The encrypted length is 32 for ($ I = 0; $ I <$ len; $ I ++) {// (hash <5) + hash is equivalent to hash * 33 // $ hash = sprintf ("% u", $ hash * 33) + ord ($ s {$ I }); // $ hash = ($ hash * 33 + ord ($ s {$ I}) & 0x7FFFFFFF; $ hash = ($ hash <$ seed) + $ hash + ord ($ s {$ I});} return $ hash & 0x7FFFFFFF ;} /*** Add a node * @ param string $ node name * @ return object $ this */public func Tion addNode ($ node) {if (in_array ($ node, array_keys ($ this-> nodes) {return ;}for ($ I = 1; $ I <= $ this-> virtual; $ I ++) {$ key = $ this-> time33 ($ node. '-'. $ I); $ this-> _ ring [$ key] = $ node; $ this-> nodes [$ node] [] = $ key ;} ksort ($ this-> _ ring, SORT_NUMERIC); return $ this ;} /*** obtain the node mapped to the string HASH on the ring * @ param string $ key * @ return string $ node */public function getNode ($ key) {$ node = current ($ t His-> _ ring); $ hash = $ this-> time33 ($ key); foreach ($ this-> _ ring as $ key => $ value) {if ($ hash <= $ key) {$ node = $ value; break ;}} return $ node ;} /*** obtain the KEY mapped to a specific node * This method must be manually called, this method is not recommended in special cases * @ param string $ node * @ param string $ keyPre * @ return mixed */public function getKey ($ node, $ keyPre = "") {if (! In_array ($ node, array_keys ($ this-> nodes) {return false;} $ result = false; for ($ I = 1; $ I <= 10000; $ I ++) {$ key = $ keyPre. md5 (rand (1000,999 9); if ($ this-> getNode ($ key) ==$ node) {$ result = true; break ;}} return $ result? $ Key: false ;}$ ch_obj = new ConsistentHashing (); $ ch_obj-> addNode ('node _ 1 '); $ ch_obj-> addNode ('node _ 2'); $ ch_obj-> addNode ('node _ 3'); $ ch_obj-> addNode ('node _ 4 '); $ ch_obj-> addNode ('node _ 5'); $ ch_obj-> addNode ('node _ 6 '); // + keys // | view the node mapped to the key // + ------------------------------------------------------------------------ $ key1 = "asofiwjamfdalks Jfkasasdflasfja "; $ key2 =" encrypt "; $ key3 =" encrypt "; $ key4 =" iowanfasijfmasdnfoas "; $ key5 =" encrypt "; $ key6 =" qjklasjdifoajfalsjflsa "; echo sprintf ("%-50 s ing to node % s \ n", $ key1, $ ch_obj-> getNode ($ key1 )); echo sprintf ("%-50 s ing to node % s \ n", $ key2, $ ch_obj-> getNode ($ key2 )); echo sprintf ("%-50 s ing to node % s \ n", $ key3, $ ch_obj-> getNode ($ key3 )); Echo sprintf ("%-50 s ing to node % s \ n", $ key4, $ ch_obj-> getNode ($ key4 )); echo sprintf ("%-50 s ing to node % s \ n", $ key5, $ ch_obj-> getNode ($ key5 )); echo sprintf ("%-50 s ing to node % s \ n", $ key6, $ ch_obj-> getNode ($ key6 )); /// + metrics // | view the ring and node information // + metrics // var_dump ($ ch_obj-> getRing (); // var_dum P ($ ch_obj-> nodes ); // + keys // | get the KEY of a specific node // + keys // $ key1 = $ ch_obj-> getKey ('node _ 1', 'Pre _'); // var_dump ($ key1); // + tests // | test distribution // + -------------------------------------------------------------------- -- // $ Keys = array (); // $ rings = array (); // for ($ I = 1; $ I <= 60000; $ I ++) {// $ key = sha1 (rand (); // $ node = $ ch_obj-> getNode ($ key ); // $ rings [$ node] = isset ($ rings [$ node])? + $ Rings [$ node]: 1; //} // var_dump ($ rings );

Running result:

Asofiwjamfdalksjfkasasdflasfja ing region to node region ing to node node_1iowanfasijfmasdnfoas ing to node node_2pqkisndfhoalnfiewlkl ing to node node_3qjklasjdifoajfalsjflsa to node node_5

PS: Here are two hash-related online tools for your reference:

Online hash/hash algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tools:
Http://tools.jb51.net/password/hash_md5_sha

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.