Snowflake.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * This file is part of the godruoyi/php-snowflake.
  4. *
  5. * (c) Godruoyi <g@godruoyi.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled.
  8. */
  9. namespace Godruoyi\Snowflake;
  10. class Snowflake
  11. {
  12. const MAX_TIMESTAMP_LENGTH = 41;
  13. const MAX_DATACENTER_LENGTH = 5;
  14. const MAX_WORKID_LENGTH = 5;
  15. const MAX_SEQUENCE_LENGTH = 12;
  16. const MAX_FIRST_LENGTH = 1;
  17. /**
  18. * The data center id.
  19. *
  20. * @var int
  21. */
  22. protected $datacenter;
  23. /**
  24. * The worker id.
  25. *
  26. * @var int
  27. */
  28. protected $workerid;
  29. /**
  30. * The Sequence Resolver instance.
  31. *
  32. * @var \Godruoyi\Snowflake\SequenceResolver|null
  33. */
  34. protected $sequence;
  35. /**
  36. * The start timestamp.
  37. *
  38. * @var int
  39. */
  40. protected $startTime;
  41. /**
  42. * Default sequence resolver.
  43. *
  44. * @var \Godruoyi\Snowflake\SequenceResolver|null
  45. */
  46. protected $defaultSequenceResolver;
  47. /**
  48. * Build Snowflake Instance.
  49. *
  50. * @param int $datacenter
  51. * @param int $workerid
  52. */
  53. public function __construct(int $datacenter = null, int $workerid = null)
  54. {
  55. $maxDataCenter = -1 ^ (-1 << self::MAX_DATACENTER_LENGTH);
  56. $maxWorkId = -1 ^ (-1 << self::MAX_WORKID_LENGTH);
  57. // If not set datacenter or workid, we will set a default value to use.
  58. $this->datacenter = $datacenter > $maxDataCenter || $datacenter < 0 ? mt_rand(0, 31) : $datacenter;
  59. $this->workerid = $workerid > $maxWorkId || $workerid < 0 ? mt_rand(0, 31) : $workerid;
  60. }
  61. /**
  62. * Get snowflake id.
  63. *
  64. * @return int
  65. */
  66. public function id()
  67. {
  68. $currentTime = $this->getCurrentMicrotime();
  69. while (($sequence = $this->callResolver($currentTime)) > (-1 ^ (-1 << self::MAX_SEQUENCE_LENGTH))) {
  70. usleep(1);
  71. $currentTime = $this->getCurrentMicrotime();
  72. }
  73. $workerLeftMoveLength = self::MAX_SEQUENCE_LENGTH;
  74. $datacenterLeftMoveLength = self::MAX_WORKID_LENGTH + $workerLeftMoveLength;
  75. $timestampLeftMoveLength = self::MAX_DATACENTER_LENGTH + $datacenterLeftMoveLength;
  76. return (string) ((($currentTime - $this->getStartTimeStamp()) << $timestampLeftMoveLength)
  77. | ($this->datacenter << $datacenterLeftMoveLength)
  78. | ($this->workerid << $workerLeftMoveLength)
  79. | ($sequence));
  80. }
  81. /**
  82. * Parse snowflake id.
  83. *
  84. * @param string $id
  85. *
  86. * @return array
  87. */
  88. public function parseId(string $id, $transform = false): array
  89. {
  90. $id = decbin($id);
  91. $data = [
  92. 'timestamp' => substr($id, 0, -22),
  93. 'sequence' => substr($id, -12),
  94. 'workerid' => substr($id, -17, 5),
  95. 'datacenter' => substr($id, -22, 5),
  96. ];
  97. return $transform ? array_map(function ($value) {
  98. return bindec($value);
  99. }, $data) : $data;
  100. }
  101. /**
  102. * Get current microtime timestamp.
  103. *
  104. * @return int
  105. */
  106. public function getCurrentMicrotime()
  107. {
  108. return floor(microtime(true) * 1000) | 0;
  109. }
  110. /**
  111. * Set start time (millisecond).
  112. *
  113. * @param int $startTime
  114. */
  115. public function setStartTimeStamp(int $startTime)
  116. {
  117. $missTime = $this->getCurrentMicrotime() - $startTime;
  118. if ($missTime < 0 || $missTime > ($maxTimeDiff = ((1 << self::MAX_TIMESTAMP_LENGTH) - 1))) {
  119. throw new \Exception('The starttime cannot be greater than current time and the maximum time difference is '.$maxTimeDiff);
  120. }
  121. $this->startTime = $startTime;
  122. return $this;
  123. }
  124. /**
  125. * Get start timestamp (millisecond), If not set default to 2019-08-08 08:08:08.
  126. *
  127. * @return int
  128. */
  129. public function getStartTimeStamp()
  130. {
  131. if ($this->startTime > 0) {
  132. return $this->startTime;
  133. }
  134. // We set a default start time if you not set.
  135. $defaultTime = '2019-08-08 08:08:08';
  136. return strtotime($defaultTime) * 1000;
  137. }
  138. /**
  139. * Set Sequence Resolver.
  140. *
  141. * @param SequenceResolver|callable $sequence
  142. */
  143. public function setSequenceResolver($sequence)
  144. {
  145. $this->sequence = $sequence;
  146. return $this;
  147. }
  148. /**
  149. * Get Sequence Resolver.
  150. *
  151. * @return \Godruoyi\Snowflake\SequenceResolver|callable|null
  152. */
  153. public function getSequenceResolver()
  154. {
  155. return $this->sequence;
  156. }
  157. /**
  158. * Get Default Sequence Resolver.
  159. *
  160. * @return \Godruoyi\Snowflake\SequenceResolver
  161. */
  162. public function getDefaultSequenceResolver(): SequenceResolver
  163. {
  164. return $this->defaultSequenceResolver ?: $this->defaultSequenceResolver = new RandomSequenceResolver();
  165. }
  166. /**
  167. * Call resolver.
  168. *
  169. * @param callable|\Godruoyi\Snowflake\SequenceResolver $resolver
  170. * @param int $maxSequence
  171. *
  172. * @return int
  173. */
  174. protected function callResolver($currentTime)
  175. {
  176. $resolver = $this->getSequenceResolver();
  177. if (is_callable($resolver)) {
  178. return $resolver($currentTime);
  179. }
  180. return is_null($resolver) || !($resolver instanceof SequenceResolver)
  181. ? $this->getDefaultSequenceResolver()->sequence($currentTime)
  182. : $resolver->sequence($currentTime);
  183. }
  184. }