@@ -68,6 +68,17 @@ class GlobalIdGenerator {
6868 */
6969 private const FILE_PREFIX = 'mw-GlobalIdGenerator ' ;
7070
71+ /** Key used in the serialized clock state map that is stored on disk */
72+ private const CLOCK_TIME = 'time ' ;
73+ /** Key used in the serialized clock state map that is stored on disk */
74+ private const CLOCK_COUNTER = 'counter ' ;
75+ /** Key used in the serialized clock state map that is stored on disk */
76+ private const CLOCK_SEQUENCE = 'clkSeq ' ;
77+ /** Key used in the serialized clock state map that is stored on disk */
78+ private const CLOCK_OFFSET = 'offset ' ;
79+ /** Key used in the serialized clock state map that is stored on disk */
80+ private const CLOCK_OFFSET_COUNTER = 'offsetCounter ' ;
81+
7182 /**
7283 * @param string $tempDirectory A writable temporary directory
7384 * @param BagOStuff $srvCache A server-local APC-like cache instance
@@ -110,24 +121,19 @@ public function newTimestampedUID88( $base = 10 ) {
110121 Assert::parameter ( $ base >= 2 , '$base ' , 'must be >= 2 ' );
111122
112123 $ info = $ this ->getTimeAndDelay ( 'lockFile88 ' , 1 , 1024 , 1024 );
113- $ info [' offsetCounter ' ] %= 1024 ;
124+ $ info [self :: CLOCK_OFFSET_COUNTER ] %= 1024 ;
114125
115126 return \Wikimedia \base_convert ( $ this ->getTimestampedID88 ( $ info ), 2 , $ base );
116127 }
117128
118129 /**
119- * @param array $info result of GlobalIdGenerator::getTimeAndDelay(), or
120- * for sub classes, a sequential array like (time, offsetCounter).
130+ * @param array $info result of GlobalIdGenerator::getTimeAndDelay()
121131 * @return string 88 bits
122132 * @throws RuntimeException
123133 */
124134 protected function getTimestampedID88 ( array $ info ) {
125- if ( isset ( $ info ['time ' ] ) ) {
126- $ time = $ info ['time ' ];
127- $ counter = $ info ['offsetCounter ' ];
128- } else {
129- list ( $ time , $ counter ) = $ info ;
130- }
135+ $ time = $ info [self ::CLOCK_TIME ];
136+ $ counter = $ info [self ::CLOCK_OFFSET_COUNTER ];
131137 // Take the 46 LSBs of "milliseconds since epoch"
132138 $ id_bin = $ this ->millisecondsSinceEpochBinary ( $ time );
133139 // Add a 10 bit counter resulting in 56 bits total
@@ -162,25 +168,20 @@ public function newTimestampedUID128( $base = 10 ) {
162168 Assert::parameter ( $ base >= 2 , '$base ' , 'must be >= 2 ' );
163169
164170 $ info = $ this ->getTimeAndDelay ( 'lockFile128 ' , 16384 , 1048576 , 1048576 );
165- $ info [' offsetCounter ' ] %= 1048576 ;
171+ $ info [self :: CLOCK_OFFSET_COUNTER ] %= 1048576 ;
166172
167173 return \Wikimedia \base_convert ( $ this ->getTimestampedID128 ( $ info ), 2 , $ base );
168174 }
169175
170176 /**
171- * @param array $info The result of GlobalIdGenerator::getTimeAndDelay(),
172- * for sub classes, a seqencial array like (time, offsetCounter, clkSeq).
177+ * @param array $info The result of GlobalIdGenerator::getTimeAndDelay()
173178 * @return string 128 bits
174179 * @throws RuntimeException
175180 */
176181 protected function getTimestampedID128 ( array $ info ) {
177- if ( isset ( $ info ['time ' ] ) ) {
178- $ time = $ info ['time ' ];
179- $ counter = $ info ['offsetCounter ' ];
180- $ clkSeq = $ info ['clkSeq ' ];
181- } else {
182- list ( $ time , $ counter , $ clkSeq ) = $ info ;
183- }
182+ $ time = $ info [self ::CLOCK_TIME ];
183+ $ counter = $ info [self ::CLOCK_OFFSET_COUNTER ];
184+ $ clkSeq = $ info [self ::CLOCK_SEQUENCE ];
184185 // Take the 46 LSBs of "milliseconds since epoch"
185186 $ id_bin = $ this ->millisecondsSinceEpochBinary ( $ time );
186187 // Add a 20 bit counter resulting in 66 bits total
@@ -215,8 +216,11 @@ public function newUUIDv1() {
215216 * @return string 128 bits
216217 */
217218 protected function getUUIDv1 ( array $ info ) {
218- $ clkSeq_bin = \Wikimedia \base_convert ( $ info ['clkSeq ' ], 10 , 2 , 14 );
219- $ time_bin = $ this ->intervalsSinceGregorianBinary ( $ info ['time ' ], $ info ['offsetCounter ' ] );
219+ $ clkSeq_bin = \Wikimedia \base_convert ( $ info [self ::CLOCK_SEQUENCE ], 10 , 2 , 14 );
220+ $ time_bin = $ this ->intervalsSinceGregorianBinary (
221+ $ info [self ::CLOCK_TIME ],
222+ $ info [self ::CLOCK_OFFSET_COUNTER ]
223+ );
220224 // Take the 32 bits of "time low"
221225 $ id_bin = substr ( $ time_bin , 28 , 32 );
222226 // Add 16 bits of "time mid" resulting in 48 bits total
@@ -434,11 +438,11 @@ protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) {
434438 * @param int $counterSize The number of possible counter values
435439 * @param int $offsetSize The number of possible offset values
436440 * @return array Array with the following keys:
437- * - array 'time': array of seconds int and milliseconds int.
438- * - int ' counter'.
439- * - int 'clkSeq'.
440- * - int ' offset': .
441- * - int 'offsetCounter'.
441+ * - GlobalIdGenerator::CLOCK_TIME: (integer seconds, integer milliseconds) array
442+ * - GlobalIdGenerator::CLOCK_COUNTER: integer millisecond tie-breaking counter
443+ * - GlobalIdGenerator::CLOCK_SEQUENCE: integer clock identifier that is local to the node
444+ * - GlobalIdGenerator::CLOCK_OFFSET: integer offset for millisecond tie-breaking counter
445+ * - GlobalIdGenerator::CLOCK_OFFSET_COUNTER: integer; CLOCK_COUNTER with CLOCK_OFFSET applied
442446 * @throws RuntimeException
443447 */
444448 protected function getTimeAndDelay ( $ lockFile , $ clockSeqSize , $ counterSize , $ offsetSize ) {
@@ -559,11 +563,11 @@ protected function getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $off
559563 $ counter = $ msecCounter % 1000 ;
560564
561565 return [
562- ' time ' => [ $ sec , $ msec ],
563- ' counter ' => $ counter ,
564- ' clkSeq ' => $ clkSeq ,
565- ' offset ' => $ randOffset ,
566- ' offsetCounter ' => $ counter + $ randOffset ,
566+ self :: CLOCK_TIME => [ $ sec , $ msec ],
567+ self :: CLOCK_COUNTER => $ counter ,
568+ self :: CLOCK_SEQUENCE => $ clkSeq ,
569+ self :: CLOCK_OFFSET => $ randOffset ,
570+ self :: CLOCK_OFFSET_COUNTER => $ counter + $ randOffset ,
567571 ];
568572 }
569573
0 commit comments