Flexihash\Flexihash::addTarget PHP Method

addTarget() public method

Add a target.
public addTarget ( string $target, float $weight = 1 )
$target string
$weight float
    public function addTarget($target, $weight = 1)
    {
        if (isset($this->targetToPositions[$target])) {
            throw new Exception("Target '{$target}' already exists.");
        }
        $this->targetToPositions[$target] = [];
        // hash the target into multiple positions
        for ($i = 0; $i < round($this->replicas * $weight); ++$i) {
            $position = $this->hasher->hash($target . $i);
            $this->positionToTarget[$position] = $target;
            // lookup
            $this->targetToPositions[$target][] = $position;
            // target removal
        }
        $this->positionToTargetSorted = false;
        ++$this->targetCount;
        return $this;
    }

Usage Example

Esempio n. 1
0
 public function testHashDistributionWithCrc32Hasher()
 {
     $hashSpace = new Flexihash(new Crc32Hasher());
     foreach (range(1, $this->_targets) as $i) {
         $hashSpace->addTarget("target{$i}");
     }
     $results = array();
     foreach (range(1, $this->_lookups) as $i) {
         $results[$i] = $hashSpace->lookup("t{$i}");
     }
     $distribution = array();
     foreach ($hashSpace->getAllTargets() as $target) {
         $distribution[$target] = count(array_keys($results, $target));
     }
     echo sprintf("\nDistribution of %d lookups per target (min/max/median/avg): %d/%d/%d/%d \n", $this->_lookups / $this->_targets, min($distribution), max($distribution), round($this->_median($distribution)), round(array_sum($distribution) / count($distribution)));
 }
All Usage Examples Of Flexihash\Flexihash::addTarget