Configuration.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Snowflake;
  12. class Configuration implements ConfigurationInterface
  13. {
  14. protected int $millisecondBits = 41;
  15. protected int $dataCenterIdBits = 5;
  16. protected int $workerIdBits = 5;
  17. protected int $sequenceBits = 12;
  18. public function maxWorkerId(): int
  19. {
  20. return -1 ^ (-1 << $this->workerIdBits);
  21. }
  22. public function maxDataCenterId(): int
  23. {
  24. return -1 ^ (-1 << $this->dataCenterIdBits);
  25. }
  26. public function maxSequence(): int
  27. {
  28. return -1 ^ (-1 << $this->sequenceBits);
  29. }
  30. public function getTimestampLeftShift(): int
  31. {
  32. return $this->sequenceBits + $this->workerIdBits + $this->dataCenterIdBits;
  33. }
  34. public function getDataCenterIdShift(): int
  35. {
  36. return $this->sequenceBits + $this->workerIdBits;
  37. }
  38. public function getWorkerIdShift(): int
  39. {
  40. return $this->sequenceBits;
  41. }
  42. public function getTimestampBits(): int
  43. {
  44. return $this->millisecondBits;
  45. }
  46. public function getDataCenterIdBits(): int
  47. {
  48. return $this->dataCenterIdBits;
  49. }
  50. public function getWorkerIdBits(): int
  51. {
  52. return $this->workerIdBits;
  53. }
  54. public function getSequenceBits(): int
  55. {
  56. return $this->sequenceBits;
  57. }
  58. }