ordered-time-codec.rst 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. .. _customize.ordered-time-codec:
  2. ==================
  3. Ordered-time Codec
  4. ==================
  5. .. attention::
  6. :ref:`Version 6, reordered time UUIDs <rfc4122.version6>` are a new version
  7. of UUID that eliminate the need for the ordered-time codec. If you aren't
  8. currently using the ordered-time codec, and you need time-based, sortable
  9. UUIDs, consider using version 6 UUIDs.
  10. UUIDs arrange their bytes according to the standard recommended by `RFC 4122`_.
  11. Unfortunately, this means the bytes aren't in an arrangement that supports
  12. sorting by creation time or an otherwise incrementing value. The Percona
  13. article, "`Storing UUID Values in MySQL`_," explains at length the problems this
  14. can cause. It also recommends a solution: the *ordered-time UUID*.
  15. RFC 4122 version 1, Gregorian time UUIDs rearrange the bytes of the time fields
  16. so that the lowest bytes appear first, the middle bytes are next, and the
  17. highest bytes come last. Logical sorting is not possible with this arrangement.
  18. An ordered-time UUID is a version 1 UUID with the time fields arranged in
  19. logical order so that the UUIDs can be sorted by creation time. These UUIDs are
  20. *monotonically increasing*, each one coming after the previously-created one, in
  21. a proper sort order.
  22. .. code-block:: php
  23. :caption: Use the ordered-time codec to generate a version 1 UUID
  24. :name: customize.ordered-time-codec-example
  25. use Ramsey\Uuid\Codec\OrderedTimeCodec;
  26. use Ramsey\Uuid\UuidFactory;
  27. $factory = new UuidFactory();
  28. $codec = new OrderedTimeCodec($factory->getUuidBuilder());
  29. $factory->setCodec($codec);
  30. $orderedTimeUuid = $factory->uuid1();
  31. printf(
  32. "UUID: %s\nVersion: %d\nDate: %s\nNode: %s\nBytes: %s\n",
  33. $orderedTimeUuid->toString(),
  34. $orderedTimeUuid->getFields()->getVersion(),
  35. $orderedTimeUuid->getDateTime()->format('r'),
  36. $orderedTimeUuid->getFields()->getNode()->toString(),
  37. bin2hex($orderedTimeUuid->getBytes())
  38. );
  39. This will use the ordered-time codec to generate a version 1 UUID and will print
  40. out details about the UUID similar to these:
  41. .. code-block:: text
  42. UUID: 593200aa-61ae-11ea-bbf2-0242ac130003
  43. Version: 1
  44. Date: Mon, 09 Mar 2020 02:33:23 +0000
  45. Node: 0242ac130003
  46. Bytes: 11ea61ae593200aabbf20242ac130003
  47. .. attention::
  48. Only the byte representation is rearranged. The string representation
  49. follows the format of a standard version 1 UUID. This means only the byte
  50. representation of an ordered-time codec encoded UUID may be used for
  51. sorting, such as with database results.
  52. To store the byte representation to a database field, see
  53. :ref:`database.bytes`.
  54. .. hint::
  55. If you use this codec and store the bytes of the UUID to the database, as
  56. recommended above, you will need to use this codec to decode the bytes, as
  57. well. Otherwise, the UUID string value will be incorrect.
  58. .. code-block:: php
  59. // Using a factory configured with the OrderedTimeCodec, as shown above.
  60. $orderedTimeUuid = $factory->fromBytes($bytes);
  61. .. _RFC 4122: https://tools.ietf.org/html/rfc4122
  62. .. _Storing UUID Values in MySQL: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/