version6.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. .. _rfc4122.version6:
  2. =========================
  3. Version 6: Reordered Time
  4. =========================
  5. .. note::
  6. Version 6, reordered time UUIDs are a new format of UUID, proposed in an
  7. `Internet-Draft under review`_ at the IETF. While the draft is still going
  8. through the IETF process, the version 6 format is not expected to change
  9. in any way that breaks compatibility.
  10. .. attention::
  11. If you need a time-based UUID, and you don't need the other features
  12. included in version 6 UUIDs, we recommend using
  13. :ref:`version 7 UUIDs <rfc4122.version7>`.
  14. Version 6 UUIDs solve `two problems that have long existed`_ with the use of
  15. :ref:`version 1 <rfc4122.version1>` UUIDs:
  16. 1. Scattered database records
  17. 2. Inability to sort by an identifier in a meaningful way (i.e., insert order)
  18. To overcome these issues, we need the ability to generate UUIDs that are
  19. *monotonically increasing* while still providing all the benefits of version
  20. 1 UUIDs.
  21. Version 6 UUIDs do this by storing the time in standard byte order, instead of
  22. breaking it up and rearranging the time bytes, according to the `RFC 4122`_
  23. definition. All other fields remain the same, and the version maintains its
  24. position, according to RFC 4122.
  25. In all other ways, version 6 UUIDs function like version 1 UUIDs.
  26. .. tip::
  27. Prior to version 4.0.0, ramsey/uuid provided a solution for this with the
  28. :ref:`ordered-time codec <customize.ordered-time-codec>`. Use of the
  29. ordered-time codec is still valid and acceptable. However, you may replace
  30. UUIDs generated using the ordered-time codec with version 6 UUIDs. Keep
  31. reading to find out how.
  32. .. code-block:: php
  33. :caption: Generate a version 6, reordered time UUID
  34. :name: rfc4122.version6.example
  35. use Ramsey\Uuid\Uuid;
  36. $uuid = Uuid::uuid6();
  37. printf(
  38. "UUID: %s\nVersion: %d\nDate: %s\nNode: %s\n",
  39. $uuid->toString(),
  40. $uuid->getFields()->getVersion(),
  41. $uuid->getDateTime()->format('r'),
  42. $uuid->getFields()->getNode()->toString()
  43. );
  44. This will generate a version 6 UUID and print out its string representation, the
  45. time the UUID was created, and the node used to create the UUID.
  46. It will look something like this:
  47. .. code-block:: text
  48. UUID: 1ea60f56-b67b-61fc-829a-0242ac130003
  49. Version: 6
  50. Date: Sun, 08 Mar 2020 04:29:37 +0000
  51. Node: 0242ac130003
  52. You may provide custom values for version 6 UUIDs, including node and clock
  53. sequence.
  54. .. code-block:: php
  55. :caption: Provide custom node and clock sequence to create a version 6,
  56. reordered time UUID
  57. :name: rfc4122.version6.custom-example
  58. use Ramsey\Uuid\Provider\Node\StaticNodeProvider;
  59. use Ramsey\Uuid\Type\Hexadecimal;
  60. use Ramsey\Uuid\Uuid;
  61. $nodeProvider = new StaticNodeProvider(new Hexadecimal('121212121212'));
  62. $clockSequence = 16383;
  63. $uuid = Uuid::uuid6($nodeProvider->getNode(), $clockSequence);
  64. .. tip::
  65. Version 6 UUIDs generated in ramsey/uuid are instances of UuidV6. Check out
  66. the :php:class:`Ramsey\\Uuid\\Rfc4122\\UuidV6` API documentation to
  67. learn more about what you can do with a UuidV6 instance.
  68. .. _rfc4122.version6.nodes:
  69. Custom and Random Nodes
  70. #######################
  71. In the :ref:`example above <rfc4122.version6.custom-example>`, we provided a
  72. custom node when generating a version 6 UUID. You may also generate random
  73. node values.
  74. To learn more, see the :ref:`rfc4122.version1.custom` and
  75. :ref:`rfc4122.version1.random` sections under :ref:`rfc4122.version1`.
  76. .. _rfc4122.version6.clock:
  77. Clock Sequence
  78. ##############
  79. In a version 6 UUID, the clock sequence serves the same purpose as in a version
  80. 1 UUID. See :ref:`rfc4122.version1.clock` to learn more.
  81. .. _rfc4122.version6.version1-conversion:
  82. Version 1-to-6 Conversion
  83. #########################
  84. It is possible to convert back-and-forth between version 6 and version 1 UUIDs.
  85. .. code-block:: php
  86. :caption: Convert a version 1 UUID to a version 6 UUID
  87. :name: rfc4122.version6.convert-version1-example
  88. use Ramsey\Uuid\Rfc4122\UuidV1;
  89. use Ramsey\Uuid\Rfc4122\UuidV6;
  90. use Ramsey\Uuid\Uuid;
  91. $uuid1 = Uuid::fromString('3960c5d8-60f8-11ea-bc55-0242ac130003');
  92. if ($uuid1 instanceof UuidV1) {
  93. $uuid6 = UuidV6::fromUuidV1($uuid1);
  94. }
  95. .. code-block:: php
  96. :caption: Convert a version 6 UUID to a version 1 UUID
  97. :name: rfc4122.version6.convert-version6-example
  98. use Ramsey\Uuid\Rfc4122\UuidV6;
  99. use Ramsey\Uuid\Uuid;
  100. $uuid6 = Uuid::fromString('1ea60f83-960c-65d8-bc55-0242ac130003');
  101. if ($uuid6 instanceof UuidV6) {
  102. $uuid1 = $uuid6->toUuidV1();
  103. }
  104. .. _rfc4122.version6.ordered-time-conversion:
  105. Ordered-time to Version 6 Conversion
  106. ####################################
  107. You may convert UUIDs previously generated and stored using the
  108. :ref:`ordered-time codec <customize.ordered-time-codec>` into version 6 UUIDs.
  109. .. caution::
  110. If you perform this conversion, the bytes and string representation of your
  111. UUIDs will change. This will break any software that expects your
  112. identifiers to be fixed.
  113. .. code-block:: php
  114. :caption: Convert an ordered-time codec encoded UUID to a version 6 UUID
  115. :name: rfc4122.version6.convert-ordered-time-example
  116. use Ramsey\Uuid\Codec\OrderedTimeCodec;
  117. use Ramsey\Uuid\Rfc4122\UuidV1;
  118. use Ramsey\Uuid\Rfc4122\UuidV6;
  119. use Ramsey\Uuid\UuidFactory;
  120. // The bytes of a version 1 UUID previously stored in some datastore
  121. // after encoding to bytes with the OrderedTimeCodec.
  122. $bytes = hex2bin('11ea60faf17c8af6ad23acde48001122');
  123. $factory = new UuidFactory();
  124. $codec = new OrderedTimeCodec($factory->getUuidBuilder());
  125. $factory->setCodec($codec);
  126. $orderedTimeUuid = $factory->fromBytes($bytes);
  127. if ($orderedTimeUuid instanceof UuidV1) {
  128. $uuid6 = UuidV6::fromUuidV1($orderedTimeUuid);
  129. }
  130. .. _rfc4122.version6.privacy:
  131. Privacy Concerns
  132. ################
  133. Like :ref:`version 1 UUIDs <rfc4122.version1>`, version 6 UUIDs use a MAC
  134. address from a local hardware network interface. This means it is possible to
  135. uniquely identify the machine on which a version 6 UUID was created.
  136. If the value provided by the timestamp of a version 6 UUID is important to you,
  137. but you do not wish to expose the interface address of any of your local
  138. machines, see :ref:`rfc4122.version6.nodes`.
  139. If you do not need an identifier with a node value embedded in it, but you still
  140. need the benefit of a monotonically increasing unique identifier, see
  141. :ref:`rfc4122.version7`.
  142. .. _Internet-Draft under review: https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-00#section-5.6
  143. .. _two problems that have long existed: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/
  144. .. _RFC 4122: https://tools.ietf.org/html/rfc4122