timestamp-first-comb-codec.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. .. _customize.timestamp-first-comb-codec:
  2. ==========================
  3. Timestamp-first COMB Codec
  4. ==========================
  5. .. attention::
  6. :ref:`Version 7, Unix Epoch time UUIDs <rfc4122.version7>` are a new version
  7. of UUID that eliminate the need for the timestamp-first COMB codec. If you
  8. aren't currently using the timestamp-first COMB codec, and you need
  9. time-based, sortable UUIDs, consider using version 7 UUIDs.
  10. :ref:`Version 4, random UUIDs <rfc4122.version4>` are doubly problematic when it
  11. comes to sorting and storing to databases (see :ref:`database.order`), since
  12. their values are random, and there is no timestamp associated with them that may
  13. be rearranged, like with the :ref:`ordered-time codec
  14. <customize.ordered-time-codec>`. In 2002, Jimmy Nilsson recognized this problem
  15. with random UUIDs and proposed a solution he called "COMBs" (see "`The Cost of
  16. GUIDs as Primary Keys`_").
  17. So-called because they *combine* random bytes with a timestamp, the
  18. timestamp-first COMB codec replaces the first 48 bits of a version 4, random
  19. UUID with a Unix timestamp and microseconds, creating an identifier that can be
  20. sorted by creation time. These UUIDs are *monotonically increasing*, each one
  21. coming after the previously-created one, in a proper sort order.
  22. .. code-block:: php
  23. :caption: Use the timestamp-first COMB codec to generate a version 4 UUID
  24. :name: customize.timestamp-first-comb-codec-example
  25. use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
  26. use Ramsey\Uuid\Generator\CombGenerator;
  27. use Ramsey\Uuid\UuidFactory;
  28. $factory = new UuidFactory();
  29. $codec = new TimestampFirstCombCodec($factory->getUuidBuilder());
  30. $factory->setCodec($codec);
  31. $factory->setRandomGenerator(new CombGenerator(
  32. $factory->getRandomGenerator(),
  33. $factory->getNumberConverter()
  34. ));
  35. $timestampFirstComb = $factory->uuid4();
  36. printf(
  37. "UUID: %s\nVersion: %d\nBytes: %s\n",
  38. $timestampFirstComb->toString(),
  39. $timestampFirstComb->getFields()->getVersion(),
  40. bin2hex($timestampFirstComb->getBytes())
  41. );
  42. This will use the timestamp-first COMB codec to generate a version 4 UUID with
  43. the timestamp replacing the first 48 bits and will print out details about the
  44. UUID similar to these:
  45. .. code-block:: text
  46. UUID: 9009ebcc-cd99-4b5f-90cf-9155607d2de9
  47. Version: 4
  48. Bytes: 9009ebcccd994b5f90cf9155607d2de9
  49. Note that the bytes are in the same order as the string representation. Unlike
  50. the :ref:`ordered-time codec <customize.ordered-time-codec>`, the
  51. timestamp-first COMB codec affects both the string representation and the byte
  52. representation. This means either the string UUID or the bytes may be stored to
  53. a datastore and sorted. To learn more, see :ref:`database`.
  54. .. _The Cost of GUIDs as Primary Keys: https://www.informit.com/articles/printerfriendly/25862