麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > PHP > 正文

PHP實現(xiàn)的一致性哈希算法完整實例

2024-05-04 23:40:25
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP實現(xiàn)的一致性哈希算法,以完整實例形式分析了PHP哈希算法的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP實現(xiàn)的一致性哈希算法。分享給大家供大家參考,具體如下:

 

 
  1. <?php 
  2. /** 
  3. * Flexihash - A simple consistent hashing implementation for PHP. 
  4.  
  5. * The MIT License 
  6.  
  7. * Copyright (c) 2008 Paul Annesley 
  8.  
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy 
  10. * of this software and associated documentation files (the "Software"), to deal 
  11. * in the Software without restriction, including without limitation the rights 
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  13. * copies of the Software, and to permit persons to whom the Software is 
  14. * furnished to do so, subject to the following conditions: 
  15.  
  16. * The above copyright notice and this permission notice shall be included in 
  17. * all copies or substantial portions of the Software. 
  18.  
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
  25. * THE SOFTWARE. 
  26.  
  27. * @author Paul Annesley 
  28. * @link http://paul.annesley.cc/ 
  29. * @copyright Paul Annesley, 2008 
  30. * @comment by MyZ (http://blog.csdn.net/mayongzhan) 
  31. */ 
  32. /** 
  33. * A simple consistent hashing implementation with pluggable hash algorithms. 
  34. * 
  35. * @author Paul Annesley 
  36. * @package Flexihash 
  37. * @licence http://www.opensource.org/licenses/mit-license.php 
  38. */ 
  39. class Flexihash 
  40. /** 
  41. * The number of positions to hash each target to. 
  42. * 
  43. * @var int 
  44. * @comment 虛擬節(jié)點數(shù),解決節(jié)點分布不均的問題 
  45. */ 
  46. private $_replicas = 64; 
  47. /** 
  48. * The hash algorithm, encapsulated in a Flexihash_Hasher implementation. 
  49. * @var object Flexihash_Hasher 
  50. * @comment 使用的hash方法 : md5,crc32 
  51. */ 
  52. private $_hasher; 
  53. /** 
  54. * Internal counter for current number of targets. 
  55. * @var int 
  56. * @comment 節(jié)點記數(shù)器 
  57. */ 
  58. private $_targetCount = 0; 
  59. /** 
  60. * Internal map of positions (hash outputs) to targets 
  61. * @var array { position => target, ... } 
  62. * @comment 位置對應節(jié)點,用于lookup中根據(jù)位置確定要訪問的節(jié)點 
  63. */ 
  64. private $_positionToTarget = array(); 
  65. /** 
  66. * Internal map of targets to lists of positions that target is hashed to. 
  67. * @var array { target => [ position, position, ... ], ... } 
  68. * @comment 節(jié)點對應位置,用于刪除節(jié)點 
  69. */ 
  70. private $_targetToPositions = array(); 
  71. /** 
  72. * Whether the internal map of positions to targets is already sorted. 
  73. * @var boolean 
  74. * @comment 是否已排序 
  75. */ 
  76. private $_positionToTargetSorted = false
  77. /** 
  78. * Constructor 
  79. * @param object $hasher Flexihash_Hasher 
  80. * @param int $replicas Amount of positions to hash each target to. 
  81. * @comment 構造函數(shù),確定要使用的hash方法和需擬節(jié)點數(shù),虛擬節(jié)點數(shù)越多,分布越均勻,但程序的分布式運算越慢 
  82. */ 
  83. public function __construct(Flexihash_Hasher $hasher = null, $replicas = null
  84. $this->_hasher = $hasher ? $hasher : new Flexihash_Crc32Hasher(); 
  85. if (!empty($replicas)) $this->_replicas = $replicas; 
  86. /** 
  87. * Add a target. 
  88. * @param string $target 
  89. * @chainable 
  90. * @comment 添加節(jié)點,根據(jù)虛擬節(jié)點數(shù),將節(jié)點分布到多個虛擬位置上 
  91. */ 
  92. public function addTarget($target) 
  93. if (isset($this->_targetToPositions[$target])) 
  94. throw new Flexihash_Exception("Target '$target' already exists."); 
  95. $this->_targetToPositions[$target] = array(); 
  96. // hash the target into multiple positions 
  97. for ($i = 0; $i < $this->_replicas; $i++) 
  98. $position = $this->_hasher->hash($target . $i); 
  99. $this->_positionToTarget[$position] = $target; // lookup 
  100. $this->_targetToPositions[$target] []= $position; // target removal 
  101. $this->_positionToTargetSorted = false
  102. $this->_targetCount++; 
  103. return $this
  104. /** 
  105. * Add a list of targets. 
  106. * @param array $targets 
  107. * @chainable 
  108. */ 
  109. public function addTargets($targets) 
  110. foreach ($targets as $target) 
  111. $this->addTarget($target); 
  112. return $this
  113. /** 
  114. * Remove a target. 
  115. * @param string $target 
  116. * @chainable 
  117. */ 
  118. public function removeTarget($target) 
  119. if (!isset($this->_targetToPositions[$target])) 
  120. throw new Flexihash_Exception("Target '$target' does not exist."); 
  121. foreach ($this->_targetToPositions[$target] as $position) 
  122. unset($this->_positionToTarget[$position]); 
  123. unset($this->_targetToPositions[$target]); 
  124. $this->_targetCount--; 
  125. return $this
  126. /** 
  127. * A list of all potential targets 
  128. * @return array 
  129. */ 
  130. public function getAllTargets() 
  131. return array_keys($this->_targetToPositions); 
  132. /** 
  133. * Looks up the target for the given resource. 
  134. * @param string $resource 
  135. * @return string 
  136. */ 
  137. public function lookup($resource) 
  138. $targets = $this->lookupList($resource, 1); 
  139. if (empty($targets)) throw new Flexihash_Exception('No targets exist'); 
  140. return $targets[0]; 
  141. /** 
  142. * Get a list of targets for the resource, in order of precedence. 
  143. * Up to $requestedCount targets are returned, less if there are fewer in total. 
  144. * 
  145. * @param string $resource 
  146. * @param int $requestedCount The length of the list to return 
  147. * @return array List of targets 
  148. * @comment 查找當前的資源對應的節(jié)點, 
  149. * 節(jié)點為空則返回空,節(jié)點只有一個則返回該節(jié)點, 
  150. * 對當前資源進行hash,對所有的位置進行排序,在有序的位置列上尋找當前資源的位置 
  151. * 當全部沒有找到的時候,將資源的位置確定為有序位置的第一個(形成一個環(huán)) 
  152. * 返回所找到的節(jié)點 
  153. */ 
  154. public function lookupList($resource, $requestedCount) 
  155. if (!$requestedCount) 
  156. throw new Flexihash_Exception('Invalid count requested'); 
  157. // handle no targets 
  158. if (empty($this->_positionToTarget)) 
  159. return array(); 
  160. // optimize single target 
  161. if ($this->_targetCount == 1) 
  162. return array_unique(array_values($this->_positionToTarget)); 
  163. // hash resource to a position 
  164. $resourcePosition = $this->_hasher->hash($resource); 
  165. $results = array(); 
  166. $collect = false
  167. $this->_sortPositionTargets(); 
  168. // search values above the resourcePosition 
  169. foreach ($this->_positionToTarget as $key => $value) 
  170. // start collecting targets after passing resource position 
  171. if (!$collect && $key > $resourcePosition) 
  172. $collect = true
  173. // only collect the first instance of any target 
  174. if ($collect && !in_array($value, $results)) 
  175. $results []= $value; 
  176. // return when enough results, or list exhausted 
  177. if (count($results) == $requestedCount || count($results) == $this->_targetCount) 
  178. return $results; 
  179. // loop to start - search values below the resourcePosition 
  180. foreach ($this->_positionToTarget as $key => $value) 
  181. if (!in_array($value, $results)) 
  182. $results []= $value; 
  183. // return when enough results, or list exhausted 
  184. if (count($results) == $requestedCount || count($results) == $this->_targetCount) 
  185. return $results; 
  186. // return results after iterating through both "parts" 
  187. return $results; 
  188. public function __toString() 
  189. return sprintf( 
  190. '%s{targets:[%s]}'
  191. get_class($this), 
  192. implode(',', $this->getAllTargets()) 
  193. ); 
  194. // ---------------------------------------- 
  195. // private methods 
  196. /** 
  197. * Sorts the internal mapping (positions to targets) by position 
  198. */ 
  199. private function _sortPositionTargets() 
  200. // sort by key (position) if not already 
  201. if (!$this->_positionToTargetSorted) 
  202. ksort($this->_positionToTarget, SORT_REGULAR); 
  203. $this->_positionToTargetSorted = true
  204. /** 
  205. * Hashes given values into a sortable fixed size address space. 
  206. * 
  207. * @author Paul Annesley 
  208. * @package Flexihash 
  209. * @licence http://www.opensource.org/licenses/mit-license.php 
  210. */ 
  211. interface Flexihash_Hasher 
  212. /** 
  213. * Hashes the given string into a 32bit address space. 
  214. * 
  215. * Note that the output may be more than 32bits of raw data, for example 
  216. * hexidecimal characters representing a 32bit value. 
  217. * 
  218. * The data must have 0xFFFFFFFF possible values, and be sortable by 
  219. * PHP sort functions using SORT_REGULAR. 
  220. * 
  221. * @param string 
  222. * @return mixed A sortable format with 0xFFFFFFFF possible values 
  223. */ 
  224. public function hash($string); 
  225. /** 
  226. * Uses CRC32 to hash a value into a signed 32bit int address space. 
  227. * Under 32bit PHP this (safely) overflows into negatives ints. 
  228. * 
  229. * @author Paul Annesley 
  230. * @package Flexihash 
  231. * @licence http://www.opensource.org/licenses/mit-license.php 
  232. */ 
  233. class Flexihash_Crc32Hasher 
  234. implements Flexihash_Hasher 
  235. /* (non-phpdoc) 
  236. * @see Flexihash_Hasher::hash() 
  237. */ 
  238. public function hash($string) 
  239. return crc32($string); 
  240. /** 
  241. * Uses CRC32 to hash a value into a 32bit binary string data address space. 
  242. * 
  243. * @author Paul Annesley 
  244. * @package Flexihash 
  245. * @licence http://www.opensource.org/licenses/mit-license.php 
  246. */ 
  247. class Flexihash_Md5Hasher 
  248. implements Flexihash_Hasher 
  249. /* (non-phpdoc) 
  250. * @see Flexihash_Hasher::hash() 
  251. */ 
  252. public function hash($string) 
  253. return substr(md5($string), 0, 8); // 8 hexits = 32bit 
  254. // 4 bytes of binary md5 data could also be used, but 
  255. // performance seems to be the same. 
  256. /** 
  257. * An exception thrown by Flexihash. 
  258. * 
  259. * @author Paul Annesley 
  260. * @package Flexihash 
  261. * @licence http://www.opensource.org/licenses/mit-license.php 
  262. */ 
  263. class Flexihash_Exception extends Exception 

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 88xx成人永久免费观看 | 欧日韩| 视屏一区| 福利在线国产 | 性生活视频一级 | 国产精品无码久久久久 | 久久综合久久综合久久综合 | 欧美成人影院 | 成人男女啪啪免费观看网站四虎 | 久久久久久久久久久久久久av | 黄色午夜剧场 | 一区二区三级视频 | 高清av在线 | 天天色综合2| 国产精品69久久 | 狠狠操操 | 西川av在线一区二区三区 | 毛片在线免费播放 | 国av在线 | 亚洲天堂在线电影 | 福利在线免费视频 | 福利一区二区三区视频在线观看 | 毛片在线视频免费观看 | 亚洲最新黄色网址 | 成人性生活视频在线播放 | 久久国产精品无码网站 | 在线看一区二区三区 | 国产一区网址 | 国产精选91 | 免费看成人av | 国产精品99久久久久久大便 | 日韩精品久久久久久久电影99爱 | 久色免费视频 | 暴力肉体进入hdxxxx0 | 国产精品v片在线观看不卡 成人一区二区三区在线 | 免费欧美一级视频 | 四虎久草 | 国产精品av久久久久久久久久 | 久久成人免费网站 | 久久艹精品 | 亚洲欧美国产精品va在线观看 |