-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #36077 [Uid] Add support for UUIDv6 (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Uid] Add support for UUIDv6 | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - See https://github.com/uuid6/uuid6-ietf-draft/blob/master/draft-peabody-dispatch-new-uuid-format-00.txt A v6 UUID is a lexicographically-sortable-v1. This makes it db-index friendly (same as ULIDs). For reference: - v1 has no benefits over v6 except being in the current official RFC - v6 is order-friendly and leaks time data + stable entropy (potentially bound to a MAC address or equivalent) - ULID is also order-friendly and leaks time data, with high entropy (crypto random source) - v4 is pure crypto random source, aka no order and no leak of anything. Commits ------- b705ee1 [Uid] Add support for UUIDv6
- Loading branch information
Showing
5 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid; | ||
|
||
/** | ||
* A v6 UUID is lexicographically sortable and contains a 60-bit timestamp and 63 extra unique bits. | ||
* | ||
* @experimental in 5.1 | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class UuidV6 extends Uuid | ||
{ | ||
protected const TYPE = 6; | ||
|
||
// https://tools.ietf.org/html/rfc4122#section-4.1.4 | ||
// 0x01b21dd213814000 is the number of 100-ns intervals between the | ||
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. | ||
private const TIME_OFFSET_INT = 0x01b21dd213814000; | ||
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00"; | ||
|
||
public function __construct(string $uuid = null) | ||
{ | ||
if (null === $uuid) { | ||
$uuid = uuid_create(UUID_TYPE_TIME); | ||
$this->uuid = substr($uuid, 15, 3).substr($uuid, 9, 4).$uuid[0].'-'.substr($uuid, 1, 4).'-6'.substr($uuid, 5, 3).substr($uuid, 18); | ||
} else { | ||
parent::__construct($uuid); | ||
} | ||
} | ||
|
||
public function getTime(): float | ||
{ | ||
$time = '0'.substr($this->uuid, 0, 8).substr($this->uuid, 9, 4).substr($this->uuid, 15, 3); | ||
|
||
if (\PHP_INT_SIZE >= 8) { | ||
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000; | ||
} | ||
|
||
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT); | ||
$time = BinaryUtil::add($time, self::TIME_OFFSET_COM); | ||
$time[0] = $time[0] & "\x7F"; | ||
|
||
return BinaryUtil::toBase($time, BinaryUtil::BASE10) / 10000000; | ||
} | ||
|
||
public function getNode(): string | ||
{ | ||
return substr($this->uuid, 24); | ||
} | ||
} |