Skip to content

Commit ccd4170

Browse files
committed
GlobalIdGenerator: Code cleanup
Mostly comment cleanups Change-Id: I2f04dff266bcf1ee5f8745b24d7c7eb2c853fd28
1 parent 6811612 commit ccd4170

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

includes/libs/uuid/GlobalIdGenerator.php

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class GlobalIdGenerator {
5454
protected $lockFileUUID;
5555

5656
/** @var array Cached file handles */
57-
protected $fileHandles = []; // cached file handles
57+
protected $fileHandles = [];
5858

5959
/** @var int B/C constant (deprecated since 1.36) */
6060
public const QUICK_VOLATILE = 1;
@@ -77,7 +77,7 @@ class GlobalIdGenerator {
7777
private const CLOCK_OFFSET_COUNTER = 'offsetCounter';
7878

7979
/**
80-
* @param string $tempDirectory A writable temporary directory
80+
* @param string|bool $tempDirectory A writable temporary directory
8181
* @param callback $shellCallback A callback that takes a shell command and returns the output
8282
*/
8383
public function __construct( $tempDirectory, $shellCallback ) {
@@ -367,7 +367,7 @@ public function getTimestampFromUUIDv1( string $uuid, int $format = TS_MW ) {
367367
*/
368368
protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) {
369369
if ( $count <= 0 ) {
370-
return []; // nothing to do
370+
return [];
371371
}
372372
if ( $bits < 16 || $bits > 48 ) {
373373
throw new RuntimeException( "Requested bit size ($bits) is out of range." );
@@ -379,7 +379,7 @@ protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) {
379379
$handle = $this->fileHandles[$path];
380380
} else {
381381
$handle = fopen( $path, 'cb+' );
382-
$this->fileHandles[$path] = $handle ?: null; // cache
382+
$this->fileHandles[$path] = $handle ?: null;
383383
}
384384
// Acquire the UID lock file
385385
if ( $handle === false ) {
@@ -391,19 +391,27 @@ protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) {
391391
}
392392
// Fetch the counter value and increment it...
393393
rewind( $handle );
394-
$counter = floor( (float)trim( fgets( $handle ) ) ) + $count; // fetch as float
394+
395+
// fetch as float
396+
$counter = floor( (float)trim( fgets( $handle ) ) ) + $count;
397+
395398
// Write back the new counter value
396399
ftruncate( $handle, 0 );
397400
rewind( $handle );
401+
398402
// Use fmod() to avoid "division by zero" on 32 bit machines
399-
fwrite( $handle, (string)fmod( $counter, 2 ** 48 ) ); // warp-around as needed
403+
// warp-around as needed
404+
fwrite( $handle, (string)fmod( $counter, 2 ** 48 ) );
400405
fflush( $handle );
406+
401407
// Release the UID lock file
402408
flock( $handle, LOCK_UN );
403409

404410
$ids = [];
405411
$divisor = 2 ** $bits;
406-
$currentId = floor( $counter - $count ); // pre-increment counter value
412+
413+
// pre-increment counter value
414+
$currentId = floor( $counter - $count );
407415
for ( $i = 0; $i < $count; ++$i ) {
408416
// Use fmod() to avoid "division by zero" on 32 bit machines
409417
$ids[] = fmod( ++$currentId, $divisor );
@@ -435,7 +443,7 @@ protected function getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $off
435443
$handle = $this->fileHandles[$this->$lockFile];
436444
} else {
437445
$handle = fopen( $this->$lockFile, 'cb+' );
438-
$this->fileHandles[$this->$lockFile] = $handle ?: null; // cache
446+
$this->fileHandles[$this->$lockFile] = $handle ?: null;
439447
}
440448
// Acquire the UID lock file
441449
if ( $handle === false ) {
@@ -571,7 +579,8 @@ protected function timeWaitUntil( $time ) {
571579
// current time is higher than or equal to than $time
572580
return $ct;
573581
}
574-
} while ( ( microtime( true ) - $start ) <= 0.010 ); // up to 10ms
582+
// up to 10ms
583+
} while ( ( microtime( true ) - $start ) <= 0.010 );
575584

576585
return false;
577586
}
@@ -601,20 +610,28 @@ protected function millisecondsSinceEpochBinary( array $time ) {
601610
protected function intervalsSinceGregorianBinary( array $time, $delta = 0 ) {
602611
list( $sec, $msec ) = $time;
603612
$offset = '122192928000000000';
604-
if ( PHP_INT_SIZE >= 8 ) { // 64 bit integers
613+
614+
// 64 bit integers
615+
if ( PHP_INT_SIZE >= 8 ) {
605616
$ts = ( 1000 * $sec + $msec ) * 10000 + (int)$offset + $delta;
606617
$id_bin = str_pad( decbin( $ts % ( 2 ** 60 ) ), 60, '0', STR_PAD_LEFT );
607618
} elseif ( extension_loaded( 'gmp' ) ) {
608-
$ts = gmp_add( gmp_mul( (string)$sec, '1000' ), (string)$msec ); // ms
609-
$ts = gmp_add( gmp_mul( $ts, '10000' ), $offset ); // 100ns intervals
619+
// ms
620+
$ts = gmp_add( gmp_mul( (string)$sec, '1000' ), (string)$msec );
621+
// 100ns intervals
622+
$ts = gmp_add( gmp_mul( $ts, '10000' ), $offset );
610623
$ts = gmp_add( $ts, (string)$delta );
611-
$ts = gmp_mod( $ts, gmp_pow( '2', '60' ) ); // wrap around
624+
// wrap around
625+
$ts = gmp_mod( $ts, gmp_pow( '2', '60' ) );
612626
$id_bin = str_pad( gmp_strval( $ts, 2 ), 60, '0', STR_PAD_LEFT );
613627
} elseif ( extension_loaded( 'bcmath' ) ) {
614-
$ts = bcadd( bcmul( $sec, 1000 ), $msec ); // ms
615-
$ts = bcadd( bcmul( $ts, 10000 ), $offset ); // 100ns intervals
628+
// ms
629+
$ts = bcadd( bcmul( $sec, 1000 ), $msec );
630+
// 100ns intervals
631+
$ts = bcadd( bcmul( $ts, 10000 ), $offset );
616632
$ts = bcadd( $ts, (string)$delta );
617-
$ts = bcmod( $ts, bcpow( 2, 60 ) ); // wrap around
633+
// wrap around
634+
$ts = bcmod( $ts, bcpow( 2, 60 ) );
618635
$id_bin = \Wikimedia\base_convert( $ts, 10, 2, 60 );
619636
} else {
620637
throw new RuntimeException( 'bcmath or gmp extension required for 32 bit machines.' );
@@ -627,7 +644,7 @@ protected function intervalsSinceGregorianBinary( array $time, $delta = 0 ) {
627644
*/
628645
private function load() {
629646
if ( $this->loaded ) {
630-
return; // already called
647+
return;
631648
}
632649

633650
$this->loaded = true;
@@ -645,7 +662,8 @@ private function load() {
645662
$line = substr( $csv, 0, strcspn( $csv, "\n" ) );
646663
$info = str_getcsv( $line );
647664
$nodeId = isset( $info[0] ) ? str_replace( '-', '', $info[0] ) : '';
648-
} elseif ( is_executable( '/sbin/ifconfig' ) ) { // Linux/BSD/Solaris/OS X
665+
} elseif ( is_executable( '/sbin/ifconfig' ) ) {
666+
// Linux/BSD/Solaris/OS X
649667
// See https://linux.die.net/man/8/ifconfig
650668
$m = [];
651669
preg_match( '/\s([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s/',
@@ -655,9 +673,10 @@ private function load() {
655673
AtEase::restoreWarnings();
656674
if ( !preg_match( '/^[0-9a-f]{12}$/i', $nodeId ) ) {
657675
$nodeId = bin2hex( random_bytes( 12 / 2 ) );
658-
$nodeId[1] = dechex( hexdec( $nodeId[1] ) | 0x1 ); // set multicast bit
676+
// set multicast bit
677+
$nodeId[1] = dechex( hexdec( $nodeId[1] ) | 0x1 );
659678
}
660-
file_put_contents( $this->nodeIdFile, $nodeId ); // cache
679+
file_put_contents( $this->nodeIdFile, $nodeId );
661680
}
662681
$this->nodeId32 = \Wikimedia\base_convert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 );
663682
$this->nodeId48 = \Wikimedia\base_convert( $nodeId, 16, 2, 48 );

0 commit comments

Comments
 (0)