From b30f495345fa14f9fdea24cd44e374a2144cfa5c Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 20 Aug 2011 18:07:42 +0200 Subject: [PATCH] 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. --- lib/Predis/Distribution/HashRing.php | 6 +++++- lib/Predis/Distribution/KetamaPureRing.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Predis/Distribution/HashRing.php b/lib/Predis/Distribution/HashRing.php index ddd11c14..53e0099e 100644 --- a/lib/Predis/Distribution/HashRing.php +++ b/lib/Predis/Distribution/HashRing.php @@ -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); } diff --git a/lib/Predis/Distribution/KetamaPureRing.php b/lib/Predis/Distribution/KetamaPureRing.php index 48b975a1..5439531d 100644 --- a/lib/Predis/Distribution/KetamaPureRing.php +++ b/lib/Predis/Distribution/KetamaPureRing.php @@ -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));