KetamaPureRing.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace Predis\Distribution;
  3. class KetamaPureRing extends HashRing {
  4. const DEFAULT_REPLICAS = 160;
  5. public function __construct() {
  6. parent::__construct($this::DEFAULT_REPLICAS);
  7. }
  8. protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
  9. $nodeObject = $node['object'];
  10. $nodeHash = $this->getNodeHash($nodeObject);
  11. $replicas = (int) floor($weightRatio * $totalNodes * ($replicas / 4));
  12. for ($i = 0; $i < $replicas; $i++) {
  13. $unpackedDigest = unpack('V4', md5("$nodeHash-$i", true));
  14. foreach ($unpackedDigest as $key) {
  15. $ring[$key] = $nodeObject;
  16. }
  17. }
  18. }
  19. public function generateKey($value) {
  20. $hash = unpack('V', md5($value, true));
  21. return $hash[1];
  22. }
  23. protected function wrapAroundStrategy($upper, $lower, $ringKeysCount) {
  24. // Binary search for the first item in _ringkeys with a value greater
  25. // or equal to the key. If no such item exists, return the first item.
  26. return $lower < $ringKeysCount ? $lower : 0;
  27. }
  28. }