/** * @param string $key * @param long $seed * @param array $partitions * @return null|string */ public static function getTreatment($key, $seed, $partitions) { $logMsg = "Splitter evaluating partitions ... \n\n Bucketing Key: {$key} \n\n Seed: {$seed} \n\n Partitions: " . print_r($partitions, true); SplitApp::logger()->debug($logMsg); $bucket = abs(\SplitIO\hash($key, $seed) % 100) + 1; SplitApp::logger()->info("Butcket: " . $bucket); $accumulatedSize = 0; foreach ($partitions as $partition) { if ($partition instanceof Partition) { $accumulatedSize += $partition->getSize(); if ($bucket <= $accumulatedSize) { return $partition->getTreatment(); } } } return null; }
public function testHashFunction() { $handle = fopen(__DIR__ . "/../../files/sample-data.csv", "r"); if ($handle) { while (($line = fgets($handle)) !== false) { $_line = explode(',', $line); if ($_line[0] == '#seed') { continue; } $hash = \SplitIO\splitHash($_line[1], $_line[0]); $bucket = abs(\SplitIO\hash($_line[1], $_line[0]) % 100) + 1; $this->assertEquals((int) $_line[2], (int) $hash, "Hash, Expected: " . $_line[2] . " Calculated: " . $hash); $this->assertEquals((int) $_line[3], (int) $bucket, "Bucket, Expected: " . $_line[3] . " Calculated: " . $bucket); } fclose($handle); } else { $this->assertTrue(false, "Sample Data not found"); } }