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.
This commit is contained in:
Daniele Alessandri
2011-08-20 18:07:42 +02:00
parent c6fa5232bc
commit b30f495345
2 changed files with 6 additions and 2 deletions
+5 -1
View File
@@ -80,7 +80,7 @@ class HashRing implements IDistributionStrategy {
protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
$nodeObject = $node['object'];
$nodeHash = (string) $nodeObject;
$nodeHash = $this->getNodeHash($nodeObject);
$replicas = (int) round($weightRatio * $totalNodes * $replicas);
for ($i = 0; $i < $replicas; $i++) {
$key = crc32("$nodeHash:$i");
@@ -88,6 +88,10 @@ class HashRing implements IDistributionStrategy {
}
}
protected function getNodeHash($nodeObject) {
return (string) $nodeObject;
}
public function generateKey($value) {
return crc32($value);
}
+1 -1
View File
@@ -11,7 +11,7 @@ class KetamaPureRing extends HashRing {
protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
$nodeObject = $node['object'];
$nodeHash = (string) $nodeObject;
$nodeHash = $this->getNodeHash($nodeObject);
$replicas = (int) floor($weightRatio * $totalNodes * ($replicas / 4));
for ($i = 0; $i < $replicas; $i++) {
$unpackedDigest = unpack('V4', md5("$nodeHash-$i", true));