Non-word characters don't terminate tag names.
[mediawiki.git] / includes / HashRing.php
blobcd39ad818532a8c9325d7ec6fdceec650052afb4
1 <?php
2 /**
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
20 * @file
21 * @author Aaron Schulz
24 /**
25 * Convenience class for weighted consistent hash rings
27 * @since 1.22
29 class HashRing {
30 /** @var Array (location => (start, end)) */
31 protected $ring = array();
33 const RING_SIZE = 268435456; // 2^28
35 /**
36 * @param array $map (location => weight)
38 public function __construct( array $map ) {
39 $map = array_filter( $map, function( $w ) { return $w > 0; } );
40 if ( !count( $map ) ) {
41 throw new MWException( "Ring is empty or all weights are zero." );
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 );
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 $sum = array_sum( $map );
53 $standardMap = array();
54 foreach ( $map as $location => $weight ) {
55 $standardMap[$location] = (int)floor( $weight / $sum * self::RING_SIZE );
57 // Build a ring of RING_SIZE spots, with each location at a spot in location hash order
58 $index = 0;
59 foreach ( $standardMap as $location => $weight ) {
60 // Location covers half-closed interval [$index,$index + $weight)
61 $this->ring[$location] = array( $index, $index + $weight );
62 $index += $weight;
64 // Make sure the last location covers what is left
65 end( $this->ring );
66 $this->ring[key( $this->ring )][1] = self::RING_SIZE;
69 /**
70 * Get the location of an item on the ring
72 * @param string $item
73 * @return string Location
75 public function getLocation( $item ) {
76 $locations = $this->getLocations( $item, 1 );
77 return $locations[0];
80 /**
81 * Get the location of an item on the ring, as well as the next clockwise locations
83 * @param string $item
84 * @param integer $limit Maximum number of locations to return
85 * @return array List of locations
87 public function getLocations( $item, $limit ) {
88 $locations = array();
89 $primaryLocation = null;
90 $spot = hexdec( substr( sha1( $item ), 0, 7 ) ); // first 28 bits
91 foreach ( $this->ring as $location => $range ) {
92 if ( count( $locations ) >= $limit ) {
93 break;
95 // The $primaryLocation is the location the item spot is in.
96 // After that is reached, keep appending the next locations.
97 if ( ( $range[0] <= $spot && $spot < $range[1] ) || $primaryLocation !== null ) {
98 if ( $primaryLocation === null ) {
99 $primaryLocation = $location;
101 $locations[] = $location;
104 // If more locations are requested, wrap-around and keep adding them
105 reset( $this->ring );
106 while ( count( $locations ) < $limit ) {
107 list( $location, ) = each( $this->ring );
108 if ( $location === $primaryLocation ) {
109 break; // don't go in circles
111 $locations[] = $location;
113 return $locations;