Standardise wording of verbs relating to revision deletion
[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 = 268435456; // 2^28
34
35 /**
36 * @param array $map (location => weight)
37 */
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." );
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 $sum = array_sum( $map );
53 $standardMap = array();
54 foreach ( $map as $location => $weight ) {
55 $standardMap[$location] = (int)floor( $weight / $sum * self::RING_SIZE );
56 }
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;
63 }
64 // Make sure the last location covers what is left
65 end( $this->ring );
66 $this->ring[key( $this->ring )][1] = self::RING_SIZE;
67 }
68
69 /**
70 * Get the location of an item on the ring
71 *
72 * @param string $item
73 * @return string Location
74 */
75 public function getLocation( $item ) {
76 $locations = $this->getLocations( $item, 1 );
77 return $locations[0];
78 }
79
80 /**
81 * Get the location of an item on the ring, as well as the next clockwise locations
82 *
83 * @param string $item
84 * @param integer $limit Maximum number of locations to return
85 * @return array List of locations
86 */
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;
94 }
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;
100 }
101 $locations[] = $location;
102 }
103 }
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
110 }
111 $locations[] = $location;
112 }
113 return $locations;
114 }
115 }