3 * Convenience class for weighted consistent hash rings.
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.
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.
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
21 * @author Aaron Schulz
25 * Convenience class for weighted consistent hash rings
30 /** @var Array (location => (start, end)) */
31 protected $ring = array();
33 const RING_SIZE
= 16777216; // 2^24
36 * @param array $map (location => weight)
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." );
43 // Sort the locations based on the hash of their names
45 foreach ( $map as $location => $weight ) {
46 $hashes[$location] = sha1( $location );
48 uksort( $map, function ( $a, $b ) use ( $hashes ) {
49 return strcmp( $hashes[$a], $hashes[$b] );
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
);
56 // Build a ring of RING_SIZE spots, with each location at a spot in location hash order
58 foreach ( $standardMap as $location => $weight ) {
59 // Location covers half-closed interval [$index,$index + $weight)
60 $this->ring
[$location] = array( $index, $index +
$weight );
63 // Make sure the last location covers what is left
65 $this->ring
[key( $this->ring
)][1] = self
::RING_SIZE
;
69 * Get the location of an item on the ring
72 * @return string Location
74 public function getLocation( $item ) {
75 $locations = $this->getLocations( $item, 1 );
80 * Get the location of an item on the ring, as well as the next clockwise locations
83 * @param integer $limit Maximum number of locations to return
84 * @return array List of locations
86 public function getLocations( $item, $limit ) {
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 ) {
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;
100 $locations[] = $location;
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
110 $locations[] = $location;