MediaWiki  1.23.1
UIDGenerator Class Reference

Class for getting statistically unique IDs. More...

Public Member Functions

 __destruct ()
 

Static Public Member Functions

static newRawUUIDv4 ( $flags=0)
 Return an RFC4122 compliant v4 UUID. More...
 
static newSequentialPerNodeID ( $bucket, $bits=48, $flags=0)
 Return an ID that is sequential only for this node and bucket. More...
 
static newSequentialPerNodeIDs ( $bucket, $bits, $count, $flags=0)
 Return IDs that are sequential only for this node and bucket. More...
 
static newTimestampedUID128 ( $base=10)
 Get a statistically unique 128-bit unsigned integer ID string. More...
 
static newTimestampedUID88 ( $base=10)
 Get a statistically unique 88-bit unsigned integer ID string. More...
 
static newUUIDv4 ( $flags=0)
 Return an RFC4122 compliant v4 UUID. More...
 
static unitTestTearDown ()
 Cleanup resources when tearing down after a unit test. More...
 

Public Attributes

const QUICK_RAND = 1
 
const QUICK_VOLATILE = 2
 

Protected Member Functions

 __construct ()
 
 deleteCacheFiles ()
 Delete all cache files that have been created. More...
 
 getSequentialPerNodeIDs ( $bucket, $bits, $count, $flags)
 Return IDs that are sequential only for this node and bucket. More...
 
 getTimestampAndDelay ( $lockFile, $clockSeqSize, $counterSize)
 Get a (time,counter,clock sequence) where (time,counter) is higher than any previous (time,counter) value for the given clock sequence. More...
 
 getTimestampedID128 (array $info)
 
 getTimestampedID88 (array $info)
 
 millisecondsSinceEpochBinary (array $time)
 
 timeWaitUntil (array $time)
 Wait till the current timestamp reaches $time and return the current timestamp. More...
 

Static Protected Member Functions

static millitime ()
 
static singleton ()
 

Protected Attributes

Array $fileHandles = array()
 
 $lockFile128
 
 $lockFile88
 
 $nodeId32
 
 $nodeId48
 
 $nodeIdFile
 

Static Protected Attributes

static $instance = null
 

Detailed Description

Class for getting statistically unique IDs.

Since
1.21

Definition at line 29 of file UIDGenerator.php.

Constructor & Destructor Documentation

◆ __construct()

UIDGenerator::__construct ( )
protected

◆ __destruct()

UIDGenerator::__destruct ( )

Definition at line 501 of file UIDGenerator.php.

Member Function Documentation

◆ deleteCacheFiles()

UIDGenerator::deleteCacheFiles ( )
protected

Delete all cache files that have been created.

This is a cleanup method primarily meant to be used from unit tests to avoid poluting the local filesystem. If used outside of a unit test environment it should be used with caution as it may destroy state saved in the files.

See also
unitTestTearDown
Since
1.23

Definition at line 468 of file UIDGenerator.php.

References $path, and as.

◆ getSequentialPerNodeIDs()

UIDGenerator::getSequentialPerNodeIDs (   $bucket,
  $bits,
  $count,
  $flags 
)
protected

Return IDs that are sequential only for this node and bucket.

See also
UIDGenerator::newSequentialPerNodeID()
Parameters
string$bucketArbitrary bucket name (should be ASCII)
integer$bitsBit size (16 to 48) of resulting numbers before wrap-around
integer$countNumber of IDs to return (1 to 10000)
integer$flags(supports UIDGenerator::QUICK_VOLATILE)
Returns
array Ordered list of float integer values

Definition at line 263 of file UIDGenerator.php.

References $cache, $count, $e, $flags, $path, array(), ObjectCache\newAccelerator(), and wfTempDir().

◆ getTimestampAndDelay()

UIDGenerator::getTimestampAndDelay (   $lockFile,
  $clockSeqSize,
  $counterSize 
)
protected

Get a (time,counter,clock sequence) where (time,counter) is higher than any previous (time,counter) value for the given clock sequence.

This is useful for making UIDs sequential on a per-node bases.

Parameters
string$lockFileName of a local lock file
$clockSeqSizeinteger The number of possible clock sequence values
$counterSizeinteger The number of possible counter values
Returns
Array (result of UIDGenerator::millitime(), counter, clock sequence)
Exceptions
MWException

Definition at line 342 of file UIDGenerator.php.

References $path, $time, array(), millitime(), and timeWaitUntil().

◆ getTimestampedID128()

UIDGenerator::getTimestampedID128 ( array  $info)
protected
Parameters
array$info(UIDGenerator::millitime(), counter, clock sequence)
Returns
string 128 bits

Definition at line 166 of file UIDGenerator.php.

References $nodeId48, $time, list, and millisecondsSinceEpochBinary().

◆ getTimestampedID88()

UIDGenerator::getTimestampedID88 ( array  $info)
protected
Parameters
array$time(UIDGenerator::millitime(), clock sequence)
Returns
string 88 bits

Definition at line 122 of file UIDGenerator.php.

References $nodeId32, $time, list, and millisecondsSinceEpochBinary().

◆ millisecondsSinceEpochBinary()

UIDGenerator::millisecondsSinceEpochBinary ( array  $time)
protected
Parameters
array$timeResult of UIDGenerator::millitime()
Returns
string 46 MSBs of "milliseconds since epoch" in binary (rolls over in 4201)

Definition at line 437 of file UIDGenerator.php.

References $time, list, and wfBaseConvert().

Referenced by getTimestampedID128(), and getTimestampedID88().

◆ millitime()

static UIDGenerator::millitime ( )
staticprotected
Returns
Array (current time in seconds, milliseconds since then)

Definition at line 451 of file UIDGenerator.php.

References array(), and list.

Referenced by getTimestampAndDelay(), and timeWaitUntil().

◆ newRawUUIDv4()

static UIDGenerator::newRawUUIDv4 (   $flags = 0)
static

Return an RFC4122 compliant v4 UUID.

Parameters
$flagsinteger Bitfield (supports UIDGenerator::QUICK_RAND)
Returns
string 32 hex characters with no hyphens
Exceptions
MWException

Definition at line 217 of file UIDGenerator.php.

References $flags.

Referenced by JobQueueRedis\getNewJobFields(), UIDGeneratorTest\testRawUUIDv4(), and UIDGeneratorTest\testRawUUIDv4QuickRand().

◆ newSequentialPerNodeID()

static UIDGenerator::newSequentialPerNodeID (   $bucket,
  $bits = 48,
  $flags = 0 
)
static

Return an ID that is sequential only for this node and bucket.

These IDs are suitable for per-host sequence numbers, e.g. for some packet protocols. If UIDGenerator::QUICK_VOLATILE is used the counter might reset on server restart.

Parameters
string$bucketArbitrary bucket name (should be ASCII)
integer$bitsBit size (<=48) of resulting numbers before wrap-around
integer$flags(supports UIDGenerator::QUICK_VOLATILE)
Returns
float Integer value as float
Since
1.23

Definition at line 233 of file UIDGenerator.php.

References $flags.

Referenced by UIDGeneratorTest\testNewSequentialID().

◆ newSequentialPerNodeIDs()

static UIDGenerator::newSequentialPerNodeIDs (   $bucket,
  $bits,
  $count,
  $flags = 0 
)
static

Return IDs that are sequential only for this node and bucket.

See also
UIDGenerator::newSequentialPerNodeID()
Parameters
string$bucketArbitrary bucket name (should be ASCII)
integer$bitsBit size (16 to 48) of resulting numbers before wrap-around
integer$countNumber of IDs to return (1 to 10000)
integer$flags(supports UIDGenerator::QUICK_VOLATILE)
Returns
array Ordered list of float integer values
Since
1.23

Definition at line 248 of file UIDGenerator.php.

References $count, $flags, and singleton().

Referenced by SquidUpdate\HTCPPurge(), and UIDGeneratorTest\testNewSequentialIDs().

◆ newTimestampedUID128()

static UIDGenerator::newTimestampedUID128 (   $base = 10)
static

Get a statistically unique 128-bit unsigned integer ID string.

The bits of the UID are prefixed with the time (down to the millisecond).

These IDs are suitable as globally unique IDs, without any enforced uniqueness. New rows almost always have higher UIDs, which makes B-TREE updates on INSERT fast. They can also be stored as "DECIMAL(39) UNSIGNED" or BINARY(16) in MySQL.

UID generation is serialized on each server (as the node ID is for the whole machine).

Parameters
$baseinteger Specifies a base other than 10
Returns
string Number
Exceptions
MWException

Definition at line 152 of file UIDGenerator.php.

References $time, singleton(), and wfBaseConvert().

Referenced by ExternalStoreMwstore\store().

◆ newTimestampedUID88()

static UIDGenerator::newTimestampedUID88 (   $base = 10)
static

Get a statistically unique 88-bit unsigned integer ID string.

The bits of the UID are prefixed with the time (down to the millisecond).

These IDs are suitable as values for the shard key of distributed data. If a column uses these as values, it should be declared UNIQUE to handle collisions. New rows almost always have higher UIDs, which makes B-TREE updates on INSERT fast. They can also be stored "DECIMAL(27) UNSIGNED" or BINARY(11) in MySQL.

UID generation is serialized on each server (as the node ID is for the whole machine).

Parameters
$baseinteger Specifies a base other than 10
Returns
string Number
Exceptions
MWException

Definition at line 108 of file UIDGenerator.php.

References $time, singleton(), and wfBaseConvert().

◆ newUUIDv4()

static UIDGenerator::newUUIDv4 (   $flags = 0)
static

Return an RFC4122 compliant v4 UUID.

Parameters
$flagsinteger Bitfield (supports UIDGenerator::QUICK_RAND)
Returns
string
Exceptions
MWException

Definition at line 191 of file UIDGenerator.php.

References $flags, MWCryptRand\generateHex(), QUICK_RAND, and wfRandomString().

Referenced by UIDGeneratorTest\testUUIDv4().

◆ singleton()

static UIDGenerator::singleton ( )
staticprotected

◆ timeWaitUntil()

UIDGenerator::timeWaitUntil ( array  $time)
protected

Wait till the current timestamp reaches $time and return the current timestamp.

This returns false if it would have to wait more than 10ms.

Parameters
array$timeResult of UIDGenerator::millitime()
Returns
Array|bool UIDGenerator::millitime() result or false

Definition at line 422 of file UIDGenerator.php.

References $time, and millitime().

Referenced by getTimestampAndDelay().

◆ unitTestTearDown()

static UIDGenerator::unitTestTearDown ( )
static

Cleanup resources when tearing down after a unit test.

This is a cleanup method primarily meant to be used from unit tests to avoid poluting the local filesystem. If used outside of a unit test environment it should be used with caution as it may destroy state saved in the files.

See also
deleteCacheFiles
Since
1.23

Definition at line 495 of file UIDGenerator.php.

References singleton().

Referenced by UIDGeneratorTest\tearDown().

Member Data Documentation

◆ $fileHandles

Array UIDGenerator::$fileHandles = array()
protected

Definition at line 40 of file UIDGenerator.php.

◆ $instance

UIDGenerator::$instance = null
staticprotected

Definition at line 31 of file UIDGenerator.php.

Referenced by singleton().

◆ $lockFile128

UIDGenerator::$lockFile128
protected

Definition at line 38 of file UIDGenerator.php.

◆ $lockFile88

UIDGenerator::$lockFile88
protected

Definition at line 37 of file UIDGenerator.php.

◆ $nodeId32

UIDGenerator::$nodeId32
protected

Definition at line 34 of file UIDGenerator.php.

Referenced by getTimestampedID88().

◆ $nodeId48

UIDGenerator::$nodeId48
protected

Definition at line 35 of file UIDGenerator.php.

Referenced by getTimestampedID128().

◆ $nodeIdFile

UIDGenerator::$nodeIdFile
protected

Definition at line 33 of file UIDGenerator.php.

◆ QUICK_RAND

const UIDGenerator::QUICK_RAND = 1

◆ QUICK_VOLATILE

const UIDGenerator::QUICK_VOLATILE = 2

Definition at line 43 of file UIDGenerator.php.

Referenced by SquidUpdate\HTCPPurge().


The documentation for this class was generated from the following file: