Files
predis/lib/Predis/Distribution/KetamaPureRing.php
T
Daniele Alessandri b30f495345 Change how the hash of a node is retrieved in the hashring-based distributor.
Now classes that extend Predis\Distribution\HashRing can override how the hash
of a node is retrieved in order to implement different kind of distribution
strategies while reusing the base algorithm.
2011-08-20 18:07:42 +02:00

35 lines
1.1 KiB
PHP

<?php
namespace Predis\Distribution;
class KetamaPureRing extends HashRing {
const DEFAULT_REPLICAS = 160;
public function __construct() {
parent::__construct($this::DEFAULT_REPLICAS);
}
protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
$nodeObject = $node['object'];
$nodeHash = $this->getNodeHash($nodeObject);
$replicas = (int) floor($weightRatio * $totalNodes * ($replicas / 4));
for ($i = 0; $i < $replicas; $i++) {
$unpackedDigest = unpack('V4', md5("$nodeHash-$i", true));
foreach ($unpackedDigest as $key) {
$ring[$key] = $nodeObject;
}
}
}
public function generateKey($value) {
$hash = unpack('V', md5($value, true));
return $hash[1];
}
protected function wrapAroundStrategy($upper, $lower, $ringKeysCount) {
// Binary search for the first item in _ringkeys with a value greater
// or equal to the key. If no such item exists, return the first item.
return $lower < $ringKeysCount ? $lower : 0;
}
}