Skip to content

Commit b366701

Browse files
committed
Update documentation with references to RFC 9562
1 parent 6700833 commit b366701

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1263
-1305
lines changed

docs/LICENSE

Lines changed: 395 additions & 0 deletions
Large diffs are not rendered by default.

docs/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# ramsey/uuid Documentation
22

3-
Changes to the documentation are automatically built by [Read the Docs][] and
4-
viewable from <https://uuid.ramsey.dev>.
3+
Changes to the documentation are automatically built by [Read the Docs][] and viewable from <https://uuid.ramsey.dev>.
54

65
## Getting Started
76

@@ -17,8 +16,8 @@ pip install -r requirements.txt
1716

1817
## Building the Docs
1918

20-
To build the docs, change to the `docs/` directory, and make sure you're working
21-
on the virtualenv environment created in the last step.
19+
To build the docs, change to the `docs/` directory, and make sure you're working on the virtualenv environment created
20+
in the last step.
2221

2322
``` bash
2423
cd docs/
@@ -32,5 +31,4 @@ Then, to view the docs after building them:
3231
open _build/html/index.html
3332
```
3433

35-
3634
[read the docs]: https://readthedocs.org

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_version():
2323
if os.environ.get('READTHEDOCS') == 'True':
2424
return os.environ.get('READTHEDOCS_VERSION')
2525

26-
pipe = Popen('git branch | grep \*', stdout=PIPE, shell=True, universal_newlines=True)
26+
pipe = Popen('git branch | grep \\*', stdout=PIPE, shell=True, universal_newlines=True)
2727
version = pipe.stdout.read()
2828

2929
if version:

docs/copyright.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44
Copyright
55
=========
66

7-
Copyright © 2012-|current_year| Ben Ramsey <[email protected]>
7+
Copyright © 2012-|current_year| `Ben Ramsey <https://benramsey.com>`_ and
8+
`contributors <https://github.com/ramsey/uuid/contributors>`_.
89

9-
This work is licensed under the Creative Commons Attribution 4.0 International
10-
License. To view a copy of this license, visit
11-
http://creativecommons.org/licenses/by/4.0/ or send a letter to Creative
12-
Commons, PO Box 1866, Mountain View, CA 94042, USA.
10+
Documentation for ramsey/uuid is licensed under the Creative Commons Attribution 4.0 International License. To view a
11+
copy of this license, visit http://creativecommons.org/licenses/by/4.0/ or send a letter to Creative Commons, PO Box
12+
1866, Mountain View, CA 94042, USA.
13+
14+
ramsey/uuid is open source software: you can distribute it and/or modify it under the terms of the MIT License (the
15+
"License"). You may not use ramsey/uuid except in compliance with the License.
16+
17+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
18+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
19+
language governing permissions and limitations under the License.
20+
21+
You should have received a copy of the MIT License along with this program. If not, see
22+
https://opensource.org/license/mit/.

docs/customize.rst

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,30 @@ Customization
1414
customize/validators
1515
customize/factory
1616

17-
ramsey/uuid offers a variety of ways to modify the standard behavior of the
18-
library through dependency injection. Using `FeatureSet`_, `UuidFactory`_, and
19-
:php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory()>`, you are able
20-
to replace just about any `builder`_, `codec`_, `converter`_, `generator`_,
21-
`provider`_, and more.
22-
23-
Ordered-time Codec
24-
The ordered-time codec exists to rearrange the bytes of a version 1,
25-
Gregorian time UUID so that the timestamp portion of the UUID is
26-
monotonically increasing. To learn more, see :ref:`customize.ordered-time-codec`.
27-
28-
Timestamp-first COMB Codec
29-
The timestamp-first COMB codec replaces part of a version 4, random UUID
30-
with a timestamp, so that the UUID becomes monotonically increasing. To
31-
learn more, see :ref:`customize.timestamp-first-comb-codec`.
17+
ramsey/uuid offers a variety of ways to modify the standard behavior of the library through dependency injection. Using
18+
`FeatureSet`_, `UuidFactory`_, and :php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory()>`, you are able to
19+
replace just about any `builder`_, `codec`_, `converter`_, `generator`_, `provider`_, and more.
20+
21+
Ordered-time Codec *(deprecated)*
22+
The ordered-time codec exists to rearrange the bytes of a version 1, Gregorian time UUID so that the timestamp
23+
portion of the UUID is monotonically increasing. To learn more, see :ref:`customize.ordered-time-codec`.
24+
25+
Timestamp-first COMB Codec *(deprecated)*
26+
The timestamp-first COMB codec replaces part of a version 4, random UUID with a timestamp, so that the UUID becomes
27+
monotonically increasing. To learn more, see :ref:`customize.timestamp-first-comb-codec`.
3228

3329
Using a Custom Calculator
34-
It's possible to replace the default calculator ramsey/uuid uses. If your
35-
requirements require a different solution for making calculations, see
36-
:ref:`customize.calculators`.
30+
It's possible to replace the default calculator ramsey/uuid uses. If your requirements require a different solution
31+
for making calculations, see :ref:`customize.calculators`.
3732

3833
Using a Custom Validator
39-
If your requirements require a different level of validation or a different
40-
UUID format, you may replace the default validator. See
41-
:ref:`customize.validators`, to learn more.
34+
If your requirements require a different level of validation or a different UUID format, you may replace the default
35+
validator. See :ref:`customize.validators`, to learn more.
4236

4337
Replace the Default Factory
44-
Not only are you able to inject alternate builders, codecs, etc. into the
45-
factory and use the factory to generate UUIDs, you may also replace the
46-
global, static factory used by the static methods on the Uuid class. To find
47-
out how, see :ref:`customize.factory`.
48-
38+
Not only are you able to inject alternate builders, codecs, etc. into the factory and use the factory to generate
39+
UUIDs, you may also replace the global, static factory used by the static methods on the Uuid class. To find out
40+
how, see :ref:`customize.factory`.
4941

5042
.. _UuidFactory: https://github.com/ramsey/uuid/blob/4.x/src/UuidFactory.php
5143
.. _FeatureSet: https://github.com/ramsey/uuid/blob/4.x/src/FeatureSet.php

docs/customize/calculators.rst

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
Using a Custom Calculator
55
=========================
66

7-
By default, ramsey/uuid uses `brick/math`_ as its internal calculator. However,
8-
you may change the calculator, if your needs require something else.
7+
By default, ramsey/uuid uses `brick/math`_ as its internal calculator. However, you may change the calculator, if your
8+
needs require something else.
99

10-
To swap the default calculator with your custom one, first make an adapter that
11-
wraps your custom calculator and implements
12-
:php:interface:`Ramsey\\Uuid\\Math\\CalculatorInterface`. This might look
13-
something like this:
10+
To swap the default calculator with your custom one, first make an adapter that wraps your custom calculator and
11+
implements :php:interface:`Ramsey\\Uuid\\Math\\CalculatorInterface`. This might look something like this:
1412

1513
.. code-block:: php
1614
:caption: Create a custom calculator wrapper that implements CalculatorInterface
@@ -47,10 +45,9 @@ something like this:
4745
4846
}
4947
50-
The easiest way to use your custom calculator wrapper is to instantiate a new
51-
FeatureSet, set the calculator on it, and pass the FeatureSet into a new
52-
UuidFactory. Using the factory, you may then generate and work with UUIDs, using
53-
your custom calculator.
48+
The easiest way to use your custom calculator wrapper is to instantiate a new FeatureSet, set the calculator on it, and
49+
pass the FeatureSet into a new UuidFactory. Using the factory, you may then generate and work with UUIDs, using your
50+
custom calculator.
5451

5552
.. code-block:: php
5653
:caption: Use your custom calculator wrapper when working with UUIDs
@@ -71,5 +68,4 @@ your custom calculator.
7168
7269
$uuid = $factory->uuid1();
7370
74-
7571
.. _brick/math: https://github.com/brick/math

docs/customize/factory.rst

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Replace the Default Factory
55
===========================
66

7-
In many of the examples throughout this documentation, we've seen how to
8-
configure the factory and then use that factory to generate and work with UUIDs.
7+
In many of the examples throughout this documentation, we've seen how to configure the factory and then use that factory
8+
to generate and work with UUIDs.
99

1010
For example:
1111

@@ -23,9 +23,8 @@ For example:
2323
2424
$orderedTimeUuid = $factory->uuid1();
2525
26-
When doing this, the default behavior of ramsey/uuid is left intact. If we call
27-
``Uuid::uuid1()`` to generate a version 1 UUID after configuring the factory as
28-
shown above, it won't use :ref:`OrderedTimeCodec <customize.ordered-time-codec>`
26+
When doing this, the default behavior of ramsey/uuid is left intact. If we call ``Uuid::uuid1()`` to generate a version
27+
1 UUID after configuring the factory as shown above, it won't use :ref:`OrderedTimeCodec <customize.ordered-time-codec>`
2928
to generate the UUID.
3029

3130
.. code-block:: php
@@ -48,10 +47,9 @@ to generate the UUID.
4847
bin2hex($uuid->getBytes())
4948
);
5049
51-
In this example, we print out details for two different UUIDs. The first was
52-
generated with the :ref:`OrderedTimeCodec <customize.ordered-time-codec>` using
53-
``$factory->uuid1()``. The second was generated using ``Uuid::uuid1()``. It
54-
looks something like this:
50+
In this example, we print out details for two different UUIDs. The first was generated with the :ref:`OrderedTimeCodec
51+
<customize.ordered-time-codec>` using ``$factory->uuid1()``. The second was generated using ``Uuid::uuid1()``. It looks
52+
something like this:
5553

5654
.. code-block:: text
5755
@@ -61,15 +59,13 @@ looks something like this:
6159
UUID: 2ff09730-6251-11ea-ba64-0242ac130003
6260
Bytes: 2ff09730625111eaba640242ac130003
6361
64-
Notice the arrangement of the bytes. The first set of bytes has been rearranged,
65-
according to the ordered-time codec rules, but the second set of bytes remains
66-
in the same order as the UUID string.
62+
Notice the arrangement of the bytes. The first set of bytes has been rearranged, according to the ordered-time codec
63+
rules, but the second set of bytes remains in the same order as the UUID string.
6764

6865
*Configuring the factory does not change the default behavior.*
6966

70-
If we want to change the default behavior, we must *replace* the factory used
71-
by the Uuid static methods, and we can do this using the
72-
:php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory>` static method.
67+
If we want to change the default behavior, we must *replace* the factory used by the Uuid static methods, and we can do
68+
this using the :php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory>` static method.
7369

7470
.. code-block:: php
7571
:caption: Replace the factory to globally affect Uuid behavior
@@ -79,15 +75,12 @@ by the Uuid static methods, and we can do this using the
7975
8076
$uuid = Uuid::uuid1();
8177
82-
Now, every time we call :php:meth:`Uuid::uuid() <Ramsey\\Uuid\\Uuid::uuid1>`,
83-
ramsey/uuid will use the factory configured with the :ref:`OrderedTimeCodec
84-
<customize.ordered-time-codec>` to generate version 1 UUIDs.
78+
Now, every time we call :php:meth:`Uuid::uuid() <Ramsey\\Uuid\\Uuid::uuid1>`, ramsey/uuid will use the factory configured
79+
with the :ref:`OrderedTimeCodec <customize.ordered-time-codec>` to generate version 1 UUIDs.
8580

8681
.. warning::
8782

88-
Calling :php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory>` to
89-
replace the factory will change the behavior of Uuid no matter where it is
90-
used, so keep this in mind when replacing the factory. If you replace the
91-
factory deep inside a method somewhere, any later code that calls a static
92-
method on :php:class:`Ramsey\\Uuid\\Uuid` will use the new factory to
93-
generate UUIDs.
83+
Calling :php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory>` to replace the factory will change the
84+
behavior of Uuid no matter where it is used, so keep this in mind when replacing the factory. If you replace the
85+
factory deep inside a method somewhere, any later code that calls a static method on :php:class:`Ramsey\\Uuid\\Uuid`
86+
will use the new factory to generate UUIDs.

docs/customize/ordered-time-codec.rst

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ Ordered-time Codec
66

77
.. attention::
88

9-
:ref:`Version 6, reordered time UUIDs <rfc4122.version6>` are a new version
10-
of UUID that eliminate the need for the ordered-time codec. If you aren't
11-
currently using the ordered-time codec, and you need time-based, sortable
12-
UUIDs, consider using version 6 UUIDs.
13-
14-
UUIDs arrange their bytes according to the standard recommended by `RFC 4122`_.
15-
Unfortunately, this means the bytes aren't in an arrangement that supports
16-
sorting by creation time or an otherwise incrementing value. The Percona
17-
article, "`Storing UUID Values in MySQL`_," explains at length the problems this
18-
can cause. It also recommends a solution: the *ordered-time UUID*.
19-
20-
RFC 4122 version 1, Gregorian time UUIDs rearrange the bytes of the time fields
21-
so that the lowest bytes appear first, the middle bytes are next, and the
22-
highest bytes come last. Logical sorting is not possible with this arrangement.
23-
24-
An ordered-time UUID is a version 1 UUID with the time fields arranged in
25-
logical order so that the UUIDs can be sorted by creation time. These UUIDs are
26-
*monotonically increasing*, each one coming after the previously-created one, in
27-
a proper sort order.
9+
The :php:class:`Ramsey\\Uuid\\Codec\\OrderedTimeCodec` class is deprecated. Please migrate to
10+
:ref:`version 6, reordered Gregorian time UUIDs <rfc4122.version6>`.
11+
12+
UUIDs arrange their bytes according to the standard recommended by `RFC 9562`_ (formerly `RFC 4122`_). Unfortunately,
13+
this means the bytes aren't in an arrangement that supports sorting by creation time or an otherwise incrementing value.
14+
The Percona article, "`Storing UUID Values in MySQL`_," explains at length the problems this can cause. It also
15+
recommends a solution: the *ordered-time UUID*.
16+
17+
`RFC 9562 version 1, Gregorian time UUIDs <https://www.rfc-editor.org/rfc/rfc9562#section-5.1>`_ rearrange the bytes of
18+
the time fields so that the lowest bytes appear first, the middle bytes are next, and the highest bytes come last.
19+
Logical sorting is not possible with this arrangement.
20+
21+
An ordered-time UUID is a version 1 UUID with the time fields arranged in logical order so that the UUIDs can be sorted
22+
by creation time. These UUIDs are *monotonically increasing*, each one coming after the previously-created one, in a
23+
proper sort order.
2824

2925
.. code-block:: php
3026
:caption: Use the ordered-time codec to generate a version 1 UUID
@@ -49,8 +45,7 @@ a proper sort order.
4945
bin2hex($orderedTimeUuid->getBytes())
5046
);
5147
52-
This will use the ordered-time codec to generate a version 1 UUID and will print
53-
out details about the UUID similar to these:
48+
This will use the ordered-time codec to generate a version 1 UUID and will print out details about the UUID similar to these:
5449

5550
.. code-block:: text
5651
@@ -62,25 +57,22 @@ out details about the UUID similar to these:
6257
6358
.. attention::
6459

65-
Only the byte representation is rearranged. The string representation
66-
follows the format of a standard version 1 UUID. This means only the byte
67-
representation of an ordered-time codec encoded UUID may be used for
68-
sorting, such as with database results.
60+
Only the byte representation is rearranged. The string representation follows the format of a standard version 1
61+
UUID. This means only the byte representation of an ordered-time codec encoded UUID may be used for sorting, such as
62+
with database results.
6963

70-
To store the byte representation to a database field, see
71-
:ref:`database.bytes`.
64+
To store the byte representation to a database field, see :ref:`database.bytes`.
7265

7366
.. hint::
7467

75-
If you use this codec and store the bytes of the UUID to the database, as
76-
recommended above, you will need to use this codec to decode the bytes, as
77-
well. Otherwise, the UUID string value will be incorrect.
68+
If you use this codec and store the bytes of the UUID to the database, as recommended above, you will need to use
69+
this codec to decode the bytes, as well. Otherwise, the UUID string value will be incorrect.
7870

7971
.. code-block:: php
8072
8173
// Using a factory configured with the OrderedTimeCodec, as shown above.
8274
$orderedTimeUuid = $factory->fromBytes($bytes);
8375
84-
85-
.. _RFC 4122: https://tools.ietf.org/html/rfc4122
86-
.. _Storing UUID Values in MySQL: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/
76+
.. _RFC 4122: https://www.rfc-editor.org/rfc/rfc4122
77+
.. _RFC 9562: https://www.rfc-editor.org/rfc/rfc9562
78+
.. _Storing UUID Values in MySQL: https://www.percona.com/blog/store-uuid-optimized-way/

docs/customize/timestamp-first-comb-codec.rst

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,19 @@ Timestamp-first COMB Codec
66

77
.. attention::
88

9-
:ref:`Version 7, Unix Epoch time UUIDs <rfc4122.version7>` are a new version
10-
of UUID that eliminate the need for the timestamp-first COMB codec. If you
11-
aren't currently using the timestamp-first COMB codec, and you need
12-
time-based, sortable UUIDs, consider using version 7 UUIDs.
13-
14-
:ref:`Version 4, random UUIDs <rfc4122.version4>` are doubly problematic when it
15-
comes to sorting and storing to databases (see :ref:`database.order`), since
16-
their values are random, and there is no timestamp associated with them that may
17-
be rearranged, like with the :ref:`ordered-time codec
18-
<customize.ordered-time-codec>`. In 2002, Jimmy Nilsson recognized this problem
19-
with random UUIDs and proposed a solution he called "COMBs" (see "`The Cost of
20-
GUIDs as Primary Keys`_").
21-
22-
So-called because they *combine* random bytes with a timestamp, the
23-
timestamp-first COMB codec replaces the first 48 bits of a version 4, random
24-
UUID with a Unix timestamp and microseconds, creating an identifier that can be
25-
sorted by creation time. These UUIDs are *monotonically increasing*, each one
26-
coming after the previously-created one, in a proper sort order.
9+
The :php:class:`Ramsey\\Uuid\\Codec\\TimestampFirstCombCodec` class is deprecated. Please migrate to
10+
:ref:`version 7, Unix Epoch time UUIDs <rfc4122.version7>`.
11+
12+
:ref:`Version 4, random UUIDs <rfc4122.version4>` are doubly problematic when it comes to sorting and storing to
13+
databases (see :ref:`database.order`), since their values are random, and there is no timestamp associated with them
14+
that may be rearranged, like with the :ref:`ordered-time codec <customize.ordered-time-codec>`. In 2002, Jimmy Nilsson
15+
recognized this problem with random UUIDs and proposed a solution he called "COMBs" (see "`The Cost of GUIDs as Primary
16+
Keys`_").
17+
18+
So-called because they *combine* random bytes with a timestamp, the timestamp-first COMB codec replaces the first 48
19+
bits of a version 4, random UUID with a Unix timestamp and microseconds, creating an identifier that can be sorted by
20+
creation time. These UUIDs are *monotonically increasing*, each one coming after the previously-created one, in a proper
21+
sort order.
2722

2823
.. code-block:: php
2924
:caption: Use the timestamp-first COMB codec to generate a version 4 UUID
@@ -52,21 +47,18 @@ coming after the previously-created one, in a proper sort order.
5247
bin2hex($timestampFirstComb->getBytes())
5348
);
5449
55-
This will use the timestamp-first COMB codec to generate a version 4 UUID with
56-
the timestamp replacing the first 48 bits and will print out details about the
57-
UUID similar to these:
50+
This will use the timestamp-first COMB codec to generate a version 4 UUID with the timestamp replacing the first 48 bits
51+
and will print out details about the UUID similar to these:
5852

5953
.. code-block:: text
6054
6155
UUID: 9009ebcc-cd99-4b5f-90cf-9155607d2de9
6256
Version: 4
6357
Bytes: 9009ebcccd994b5f90cf9155607d2de9
6458
65-
Note that the bytes are in the same order as the string representation. Unlike
66-
the :ref:`ordered-time codec <customize.ordered-time-codec>`, the
67-
timestamp-first COMB codec affects both the string representation and the byte
68-
representation. This means either the string UUID or the bytes may be stored to
69-
a datastore and sorted. To learn more, see :ref:`database`.
70-
59+
Note that the bytes are in the same order as the string representation. Unlike the :ref:`ordered-time codec
60+
<customize.ordered-time-codec>`, the timestamp-first COMB codec affects both the string representation and the byte
61+
representation. This means either the string UUID or the bytes may be stored to a datastore and sorted. To learn more,
62+
see :ref:`database`.
7163

72-
.. _The Cost of GUIDs as Primary Keys: https://www.informit.com/articles/printerfriendly/25862
64+
.. _The Cost of GUIDs as Primary Keys: https://web.archive.org/web/20240118030355/https://www.informit.com/articles/printerfriendly/25862

0 commit comments

Comments
 (0)