Merge "Fixed Bug 40464 Placeholder attribute of searchInput element"
[lhc/web/wiklou.git] / includes / HashRing.php
1 <?php
2 /**
3 * Convenience class for weighted consistent hash rings.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Convenience class for weighted consistent hash rings
26 *
27 * @since 1.22
28 */
29 class HashRing {
30 /** @var Array (location => (start, end)) */
31 protected $ring = array();
32
33 const RING_SIZE = 16777216; // 2^24
34
35 /**
36 * @param array $map (location => weight)
37 */
38 public function __construct( array $map ) {
39 $sum = array_sum( $map );
40 if ( !count( $map ) || $sum <= 0 ) {
41 throw new MWException( "Ring is empty or all weights are zero." );
42 }
43 // Sort the locations based on the hash of their names
44 $hashes = array();
45 foreach ( $map as $location => $weight ) {
46 $hashes[$location] = sha1( $location );
47 }
48 uksort( $map, function ( $a, $b ) use ( $hashes ) {
49 return strcmp( $hashes[$a], $hashes[$b] );
50 } );
51 // Fit the map to weight-proportionate one with a space of size RING_SIZE
52 $standardMap = array();
53 foreach ( $map as $location => $weight ) {
54 $standardMap[$location] = (int)floor( $weight / $sum * self::RING_SIZE );
55 }
56 // Build a ring of RING_SIZE spots, with each location at a spot in location hash order
57 $index = 0;
58 foreach ( $standardMap as $location => $weight ) {
59 // Location covers half-closed interval [$index,$index + $weight)
60 $this->ring[$location] = array( $index, $index + $weight );
61 $index += $weight;
62 }
63 // Make sure the last location covers what is left
64 end( $this->ring );
65 $this->ring[key( $this->ring )][1] = self::RING_SIZE;
66 }
67
68 /**
69 * Get the location of an item on the ring
70 *
71 * @param string $item
72 * @return string Location
73 */
74 public function getLocation( $item ) {
75 $locations = $this->getLocations( $item, 1 );
76 return $locations[0];
77 }
78
79 /**
80 * Get the location of an item on the ring, as well as the next clockwise locations
81 *
82 * @param string $item
83 * @param integer $limit Maximum number of locations to return
84 * @return array List of locations
85 */
86 public function getLocations( $item, $limit ) {
87 $locations = array();
88 $primaryLocation = null;
89 $spot = hexdec( substr( sha1( $item ), 0, 6 ) ); // first 24 bits
90 foreach ( $this->ring as $location => $range ) {
91 if ( count( $locations ) >= $limit ) {
92 break;
93 }
94 // The $primaryLocation is the location the item spot is in.
95 // After that is reached, keep appending the next locations.
96 if ( ( $range[0] <= $spot && $spot < $range[1] ) || $primaryLocation !== null ) {
97 if ( $primaryLocation === null ) {
98 $primaryLocation = $location;
99 }
100 $locations[] = $location;
101 }
102 }
103 // If more locations are requested, wrap-around and keep adding them
104 reset( $this->ring );
105 while ( count( $locations ) < $limit ) {
106 list( $location, ) = each( $this->ring );
107 if ( $location === $primaryLocation ) {
108 break; // don't go in circles
109 }
110 $locations[] = $location;
111 }
112 return $locations;
113 }
114 }