Преглед изворни кода

Introduced the new KetamaPureRing class with the same key distribution algorithm of libketama.

Daniele Alessandri пре 15 година
родитељ
комит
6d5060b226
1 измењених фајлова са 25 додато и 0 уклоњено
  1. 25 0
      lib/Predis.php

+ 25 - 0
lib/Predis.php

@@ -1488,6 +1488,31 @@ class HashRing implements IRing {
     }
 }
 
+class KetamaPureRing extends HashRing {
+    const DEFAULT_REPLICAS = 160;
+
+    public function __construct() {
+        parent::__construct($this::DEFAULT_REPLICAS);
+    }
+
+    protected function addNodeToRing(&$ring, $node, $totalNodes, $weightRatio, $replicas) {
+        $nodeObject = $node['object'];
+        $nodeHash = (string) $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];
+    }
+}
+
 abstract class MultiBulkResponseIteratorBase implements \Iterator, \Countable {
     protected $_position, $_current, $_replySize;