|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ennexa\Snowflake; |
| 4 | + |
| 5 | +use Exception\InvalidArgumentException; |
| 6 | +use Exception\InvalidSystemClockException; |
| 7 | + |
| 8 | +class Generator { |
| 9 | + const NODE_LEN = 8; |
| 10 | + const WORKER_LEN = 8; |
| 11 | + const SEQUENCE_LEN = 8; |
| 12 | + |
| 13 | + private $instanceId = 0; |
| 14 | + private $startEpoch = 1546300800000; |
| 15 | + private $sequenceMax; |
| 16 | + private $store; |
| 17 | + |
| 18 | + private static function getMaxValue(int $len) |
| 19 | + { |
| 20 | + return -1 ^ (-1 << $len); |
| 21 | + } |
| 22 | + |
| 23 | + public static function generateInstanceId($nodeId = 0, $workerId = 0) |
| 24 | + { |
| 25 | + $nodeIdMax = $this->getMaxValue(self::NODE_LEN); |
| 26 | + if ($nodeId < 0 || $nodeId > $nodeIdMax) { |
| 27 | + throw InvalidArgumentException("Node ID should be between 0 and $nodeIdMax"); |
| 28 | + } |
| 29 | + |
| 30 | + $workerIdMax = $this->getMaxValue(self::WORKER_LEN); |
| 31 | + if ($workerId < 0 || $workerId > $workerIdMax) { |
| 32 | + throw InvalidArgumentException("Worker ID should be between 0 and $workerIdMax"); |
| 33 | + } |
| 34 | + |
| 35 | + return $nodeId << self::WORKER_LEN | $workerId; |
| 36 | + } |
| 37 | + |
| 38 | + public function __construct(StoreInterface $store, int $instanceId = 0, ?int $startEpoch = null) |
| 39 | + { |
| 40 | + $this->setInstanceId($instanceId); |
| 41 | + $this->setStore($store); |
| 42 | + |
| 43 | + if (!is_null($startEpoch)) { |
| 44 | + $this->startEpoch = $startEpoch; |
| 45 | + } |
| 46 | + |
| 47 | + $this->sequenceMask = -1 ^ (-1 << self::SEQUENCE_LEN); |
| 48 | + $this->sequenceMax = -1 & $this->sequenceMask; |
| 49 | + $this->tickShift = self::NODE_LEN + self::WORKER_LEN + self::SEQUENCE_LEN; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Set the sequence store |
| 54 | + * |
| 55 | + * @param int Instance Id |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + public function setStore(StoreInterface $store) |
| 59 | + { |
| 60 | + $this->store = $store; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the current generator instance id |
| 65 | + * |
| 66 | + * @param int Instance Id |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public function getInstanceId() |
| 70 | + { |
| 71 | + return $this->instanceId >> self::SEQUENCE_LEN; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Set the instance id for the generator instance |
| 76 | + * |
| 77 | + * @param int Instance Id |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function setInstanceId(int $instanceId) |
| 81 | + { |
| 82 | + $this->instanceId = $instanceId << self::SEQUENCE_LEN; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the next sequence |
| 87 | + * |
| 88 | + * @return array timestamp and sequence number |
| 89 | + */ |
| 90 | + public function nextSequence() |
| 91 | + { |
| 92 | + return $this->store->next($this->instanceId); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Generate a unique id based on the epoch and instance id |
| 97 | + * |
| 98 | + * @return int unique 64-bit id |
| 99 | + * @throws InvalidSystemClockException |
| 100 | + */ |
| 101 | + public function nextId() |
| 102 | + { |
| 103 | + list($timestamp, $sequence) = $this->nextSequence(); |
| 104 | + |
| 105 | + if ($sequence < 0) { |
| 106 | + $ticks = $timestamp - $this->startEpoch; |
| 107 | + throw new InvalidSystemClockException("Clock moved backwards or wrapped around. Refusing to generate id for $ticks ticks"); |
| 108 | + } |
| 109 | + |
| 110 | + $ticks = ($timestamp - $this->startEpoch) << $this->tickShift; |
| 111 | + |
| 112 | + return (string)($ticks | $this->instanceId | $sequence); |
| 113 | + } |
| 114 | +} |
0 commit comments