1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GpsLab component. |
5
|
|
|
* |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Base64UID\Generator\Binary; |
12
|
|
|
|
13
|
|
|
use GpsLab\Component\Base64UID\Exception\ArgumentRangeException; |
14
|
|
|
use GpsLab\Component\Base64UID\Exception\ArgumentTypeException; |
15
|
|
|
use GpsLab\Component\Base64UID\Exception\BitmapOverflowException; |
16
|
|
|
use GpsLab\Component\Base64UID\Exception\ProcessorArchitectureException; |
17
|
|
|
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException; |
18
|
|
|
|
19
|
|
|
class SnowflakeGenerator implements BinaryGenerator |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $generator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $generator_length; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $sequence_length; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $time_length; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $time_offset; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $last_time = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
private $sequence = 0; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Snowflake. |
58
|
|
|
* |
59
|
|
|
* The time length defines the limit of the stored date: |
60
|
|
|
* 40-bits = 1111111111111111111111111111111111111111 = 1099511627775 = 2004-11-03 19:53:48 (UTC) |
61
|
|
|
* 41-bits = 11111111111111111111111111111111111111111 = 2199023255551 = 2039-09-07 15:47:36 (UTC) |
62
|
|
|
* 42-bits = 111111111111111111111111111111111111111111 = 4398046511103 = 2109-05-15 07:35:11 (UTC) |
63
|
|
|
* 43-bits = 1111111111111111111111111111111111111111111 = 8796093022207 = 2248-09-26 15:10:22 (UTC) |
64
|
|
|
* 44-bits = 11111111111111111111111111111111111111111111 = 17592186044415 = 2527-06-23 06:20:44 (UTC) |
65
|
|
|
* 45-bits = 111111111111111111111111111111111111111111111 = 35184372088831 = 3084-12-12 12:41:29 (UTC) |
66
|
|
|
* |
67
|
|
|
* The time offset allows to move the starting point of time in microseconds, |
68
|
|
|
* which reduces the size of the stored time: |
69
|
|
|
* 0 = 1970-01-01 00:00:00 (UTC) |
70
|
|
|
* 1577836800000 = 2020-01-01 00:00:00 (UTC) |
71
|
|
|
* |
72
|
|
|
* @param int $generator |
73
|
|
|
* @param int $generator_length |
74
|
|
|
* @param int $sequence_length |
75
|
|
|
* @param int $time_length |
76
|
|
|
* @param int $time_offset |
77
|
|
|
*/ |
78
|
28 |
|
public function __construct( |
79
|
|
|
$generator, |
80
|
|
|
$generator_length = 10, |
81
|
|
|
$sequence_length = 12, |
82
|
|
|
$time_length = 41, |
83
|
|
|
$time_offset = 1577836800000 |
84
|
|
|
) { |
85
|
|
|
// @codeCoverageIgnoreStart |
86
|
|
|
// can't reproduce this condition in tests |
87
|
|
View Code Duplication |
if (PHP_INT_SIZE * 8 < 64) { |
|
|
|
|
88
|
|
|
throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8)); |
89
|
|
|
} |
90
|
|
|
// @codeCoverageIgnoreEnd |
91
|
|
|
|
92
|
28 |
|
if (!is_int($generator)) { |
93
|
1 |
|
throw new ArgumentTypeException(sprintf('Generator should be integer, got "%s" instead.', gettype($generator))); |
94
|
|
|
} |
95
|
|
|
|
96
|
27 |
|
if (!is_int($generator_length)) { |
97
|
1 |
|
throw new ArgumentTypeException(sprintf('Generator length should be integer, got "%s" instead.', gettype($generator_length))); |
98
|
|
|
} |
99
|
|
|
|
100
|
26 |
|
if (!is_int($sequence_length)) { |
101
|
1 |
|
throw new ArgumentTypeException(sprintf('Sequence length should be integer, got "%s" instead.', gettype($sequence_length))); |
102
|
|
|
} |
103
|
|
|
|
104
|
25 |
|
if (!is_int($time_length)) { |
105
|
1 |
|
throw new ArgumentTypeException(sprintf('Time length should be integer, got "%s" instead.', gettype($time_length))); |
106
|
|
|
} |
107
|
|
|
|
108
|
24 |
|
if (!is_int($time_offset)) { |
109
|
1 |
|
throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($time_offset))); |
110
|
|
|
} |
111
|
|
|
|
112
|
23 |
|
if ($generator < 0) { |
113
|
1 |
|
throw new ZeroArgumentException(sprintf('Generator should be grate then "0", got "%d" instead.', $generator)); |
114
|
|
|
} |
115
|
|
|
|
116
|
22 |
|
if ($generator_length < 0) { |
117
|
1 |
|
throw new ZeroArgumentException(sprintf('Generator length should be grate then "0", got "%d" instead.', $generator_length)); |
118
|
|
|
} |
119
|
|
|
|
120
|
21 |
|
if ($sequence_length < 0) { |
121
|
1 |
|
throw new ZeroArgumentException(sprintf('Sequence length should be grate then "0", got "%d" instead.', $sequence_length)); |
122
|
|
|
} |
123
|
|
|
|
124
|
20 |
|
if ($time_length < 0) { |
125
|
1 |
|
throw new ZeroArgumentException(sprintf('Time length should be grate then "0", got "%d" instead.', $time_length)); |
126
|
|
|
} |
127
|
|
|
|
128
|
19 |
|
if ($time_offset < 0) { |
129
|
1 |
|
throw new ZeroArgumentException(sprintf('Time offset should be grate then "0", got "%d" instead.', $time_offset)); |
130
|
|
|
} |
131
|
|
|
|
132
|
18 |
|
if ($generator_length + $sequence_length + $time_length > 64 - 1) { |
133
|
3 |
|
throw new ArgumentRangeException(sprintf('Length of generator, sequence and time for UID should be less than or equal to "%d", got "%d" instead.', 64 - 1, $generator_length + $sequence_length + $time_length)); |
134
|
|
|
} |
135
|
|
|
|
136
|
15 |
|
$max_generator_id = (int) bindec(str_repeat('1', $generator_length)); |
137
|
|
|
|
138
|
15 |
|
if ($generator > $max_generator_id) { |
139
|
10 |
|
throw new ArgumentRangeException(sprintf('Generator should be grate then or equal to "%d", got "%d" instead.', $max_generator_id, $generator)); |
140
|
|
|
} |
141
|
|
|
|
142
|
5 |
|
$now = (int) floor(microtime(true) * 1000); |
143
|
|
|
|
144
|
5 |
|
if ($time_offset > $now) { |
145
|
1 |
|
throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset)); |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
$min_time_length = strlen(decbin($now - $time_offset)); |
149
|
|
|
|
150
|
4 |
|
if ($time_length < $min_time_length) { |
151
|
|
|
throw new ArgumentRangeException(sprintf('Length of time for UID should be grate then or equal to "%d", got "%d" instead.', $min_time_length, $time_length)); |
152
|
|
|
} |
153
|
|
|
|
154
|
4 |
|
$this->generator = $generator; |
155
|
4 |
|
$this->generator_length = $generator_length; |
156
|
4 |
|
$this->sequence_length = $sequence_length; |
157
|
4 |
|
$this->time_length = $time_length; |
158
|
4 |
|
$this->time_offset = $time_offset; |
159
|
4 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
4 |
|
public function generate() |
165
|
|
|
{ |
166
|
4 |
|
$time = ((int) floor(microtime(true) * 1000) - $this->time_offset); |
167
|
|
|
|
168
|
|
|
// @codeCoverageIgnoreStart |
169
|
|
|
// overflow validation is in the constructor, |
170
|
|
|
// but there is a chance that overflow will occur in the process of using this service |
171
|
|
|
if ($time >= 1 << $this->time_length) { |
172
|
|
|
throw new BitmapOverflowException(sprintf('Bitmap for time is overflow of %d bits.', $this->time_length)); |
173
|
|
|
} |
174
|
|
|
// @codeCoverageIgnoreEnd |
175
|
|
|
|
176
|
4 |
|
if ($this->last_time === $time) { |
177
|
1 |
|
++$this->sequence; |
178
|
|
|
} else { |
179
|
4 |
|
$this->last_time = $time; |
180
|
4 |
|
$this->sequence = 0; |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
$uid = 1 << 64 - 1; |
184
|
4 |
|
$uid |= $time << $this->generator_length + $this->sequence_length; |
185
|
4 |
|
$uid |= $this->generator << $this->sequence_length; |
186
|
4 |
|
$uid |= $this->sequence; |
187
|
|
|
|
188
|
4 |
|
return $uid; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.