MediaWiki  1.23.1
HashRing.php
Go to the documentation of this file.
1 <?php
29 class HashRing {
31  protected $sourceMap = array();
33  protected $ring = array();
34 
35  const RING_SIZE = 268435456; // 2^28
36 
40  public function __construct( array $map ) {
41  $map = array_filter( $map, function ( $w ) {
42  return $w > 0;
43  } );
44  if ( !count( $map ) ) {
45  throw new UnexpectedValueException( "Ring is empty or all weights are zero." );
46  }
47  $this->sourceMap = $map;
48  // Sort the locations based on the hash of their names
49  $hashes = array();
50  foreach ( $map as $location => $weight ) {
51  $hashes[$location] = sha1( $location );
52  }
53  uksort( $map, function ( $a, $b ) use ( $hashes ) {
54  return strcmp( $hashes[$a], $hashes[$b] );
55  } );
56  // Fit the map to weight-proportionate one with a space of size RING_SIZE
57  $sum = array_sum( $map );
58  $standardMap = array();
59  foreach ( $map as $location => $weight ) {
60  $standardMap[$location] = (int)floor( $weight / $sum * self::RING_SIZE );
61  }
62  // Build a ring of RING_SIZE spots, with each location at a spot in location hash order
63  $index = 0;
64  foreach ( $standardMap as $location => $weight ) {
65  // Location covers half-closed interval [$index,$index + $weight)
66  $this->ring[$location] = array( $index, $index + $weight );
67  $index += $weight;
68  }
69  // Make sure the last location covers what is left
70  end( $this->ring );
71  $this->ring[key( $this->ring )][1] = self::RING_SIZE;
72  }
73 
80  public function getLocation( $item ) {
81  $locations = $this->getLocations( $item, 1 );
82 
83  return $locations[0];
84  }
85 
93  public function getLocations( $item, $limit ) {
94  $locations = array();
95  $primaryLocation = null;
96  $spot = hexdec( substr( sha1( $item ), 0, 7 ) ); // first 28 bits
97  foreach ( $this->ring as $location => $range ) {
98  if ( count( $locations ) >= $limit ) {
99  break;
100  }
101  // The $primaryLocation is the location the item spot is in.
102  // After that is reached, keep appending the next locations.
103  if ( ( $range[0] <= $spot && $spot < $range[1] ) || $primaryLocation !== null ) {
104  if ( $primaryLocation === null ) {
105  $primaryLocation = $location;
106  }
107  $locations[] = $location;
108  }
109  }
110  // If more locations are requested, wrap-around and keep adding them
111  reset( $this->ring );
112  while ( count( $locations ) < $limit ) {
113  list( $location, ) = each( $this->ring );
114  if ( $location === $primaryLocation ) {
115  break; // don't go in circles
116  }
117  $locations[] = $location;
118  }
119 
120  return $locations;
121  }
122 
128  public function getLocationWeights() {
129  return $this->sourceMap;
130  }
131 
138  public function newWithoutLocation( $location ) {
139  $map = $this->sourceMap;
140  unset( $map[$location] );
141  if ( count( $map ) ) {
142  return new self( $map );
143  }
144 
145  return false;
146  }
147 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
HashRing\getLocations
getLocations( $item, $limit)
Get the location of an item on the ring, as well as the next clockwise locations.
Definition: HashRing.php:91
HashRing\__construct
__construct(array $map)
Definition: HashRing.php:38
$limit
if( $sleep) $limit
Definition: importImages.php:99
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
HashRing\getLocation
getLocation( $item)
Get the location of an item on the ring.
Definition: HashRing.php:78
HashRing\getLocationWeights
getLocationWeights()
Get the map of locations to weight (ignores 0-weight items)
Definition: HashRing.php:126
HashRing\$sourceMap
Array $sourceMap
(location => weight) *
Definition: HashRing.php:30
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
HashRing\$ring
Array $ring
(location => (start, end)) *
Definition: HashRing.php:31
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$hashes
$hashes
Definition: testCompression.php:62
HashRing\RING_SIZE
const RING_SIZE
Definition: HashRing.php:33
HashRing
Convenience class for weighted consistent hash rings.
Definition: HashRing.php:29
HashRing\newWithoutLocation
newWithoutLocation( $location)
Get a new hash ring with a location removed from the ring.
Definition: HashRing.php:136