Phan\Library\Hasher\Consistent::getGroup PHP Method

getGroup() public method

Do a binary search in the consistent hashing ring to find the group.
public getGroup ( string $key ) : integer
$key string
return integer - an integer between 0 and $this->_groupCount - 1, inclusive
    public function getGroup(string $key) : int
    {
        $searchHash = self::generate_key_hash($key);
        $begin = 0;
        $end = count($this->_hashRingIds) - 1;
        while ($begin <= $end) {
            $pos = $begin + ($end - $begin >> 1);
            $curVal = $this->_hashRingIds[$pos];
            if ($searchHash > $curVal) {
                $begin = $pos + 1;
            } else {
                $end = $pos - 1;
            }
        }
        // Postcondition: $this->_hashRingIds[$begin] >= $searchHash, and $this->_hashRingIds[$begin - 1] does not exist or is less than $searchHash.
        // Fetch the group corresponding to that hash in the hash ring.
        return $this->_hashRingGroups[$begin];
    }