-
Notifications
You must be signed in to change notification settings - Fork 54
1.6. UUIDv6
It is one of the UUID formats proposed in an IETF draft.
The Time-ordered UUID has a timestamp and a node identifier. These two information represent WHEN and WHERE the UUID was created.
The timestamp is the count of 100 nanosecond intervals since 1582年10月15日, the beginning of Gregorian calendar.
The timestamp bytes are kept in the original order, unlike the time-based UUID (version 1). This is the only difference between version 1 and version 6.
The node identifier can be a MAC address, a hash of system information, a specific number or a random number (default).
💡 HINT
UUIDv6, UUIDv7, and UUIDv8; what are they?: https://playfulprogramming.com/posts/uuid-v6-8
See the section Choosing the node identifier.
// with a static random number as node identifier UUID uuid = UuidCreator.getTimeOrdered();
// with a MAC address as node identifier UUID uuid = UuidCreator.getTimeOrderedWithMac();
// with a hash of hostname, MAC and IP as node identifier UUID uuid = UuidCreator.getTimeOrderedWithHash();
// with a changing random number as node identifier UUID uuid = UuidCreator.getTimeOrderedWithRandom();
Sequence of time-ordered UUIDs:
1e98eff0-eddc-6470-a649-ad1cde652e10
1e98eff0-eddc-6471-a649-ad1cde652e10
1e98eff0-eddc-6472-a649-ad1cde652e10
1e98eff0-eddc-6473-a649-ad1cde652e10
1e98eff0-eddc-6474-a649-ad1cde652e10
1e98eff0-eddc-6475-a649-ad1cde652e10
1e98eff0-eddc-6476-a649-ad1cde652e10
1e98eff0-eddc-6477-a649-ad1cde652e10
1e98eff0-eddc-6478-a649-ad1cde652e10
1e98eff0-eddc-6479-a649-ad1cde652e10
1e98eff0-eddc-647a-a649-ad1cde652e10
1e98eff0-eddc-647b-a649-ad1cde652e10
1e98eff0-eddc-647c-a649-ad1cde652e10
1e98eff0-eddc-647d-a649-ad1cde652e10
1e98eff0-eddc-647e-a649-ad1cde652e10
1e98eff0-eddc-647f-a649-ad1cde652e10
^ look
|-----------------|----|-----------|
timestamp clkseq node id
Other usage examples:
// Time-ordered with a date and time chosen by you Instant myInstant = Instant.parse("1987-01-23T01:23:45.123456789Z"); UUID uuid = UuidCreator.getTimeOrdered(myInstant, null, null);
// Time-ordered with a clock sequence chosen by you Integer myClockSeq = 0xAAAA; // Override the random clock sequence UUID uuid = UuidCreator.getTimeOrdered(null, myClockSeq, null);
// Time-ordered with a node identifier chosen by you Long myNode = 0xAAAAAAAAAAAAL; // Override the random node identifier UUID uuid = UuidCreator.getTimeOrdered(null, null, myNode);
Time-ordered and Time-based UUID share the same base implementation and examples. This section will focus on the only difference between them.
The Time-ordered UUID inherits the same characteristics of the time-based UUID. The only difference is that the timestamp bits are not rearranged as the Time-based UUID does.
Time-ordered timestamp arrangement
00000000-0000-v000-m000-000000000000
|1-------|2---|3---|
1: timestamp high *****
2: timestamp mid ***
3: timestamp low *
The RFC-4122 splits the timestamp bits into three parts: high, middle, and low. Then it reverses the order of these three parts to create a time-based UUID. The reason for this layout is not documented in the RFC-4122.
In the time-ordered, the timestamp bits are kept in the original layout. See the comparison below.
Instant:
2019年09月22日T17:52:55.033Z
Instant's timestamp in hexadecimal:
01e9dd61ce117e90
Timestamp split into high, middle and low bits:
_1e9 dd61 ce117e90
_aaa bbbb cccccccc
a: time high
b: time middle
c: time low
_: version
Timestamp in time-based layout (version 1):
ce117e90-dd61-11e9-0000-000000000000
cccccccc-bbbb-_aaa-dddd-eeeeeeeeeeee
c: time low
b: time middle
a: time high
_: version
d: clock sequence
e: node id
Timestamp in time-ordered layout (version 6):
1e9dd61c-e117-6e90-0000-000000000000
aaabbbbc-cccc-_ccc-dddd-eeeeeeeeeeee
a: time high
b: time middle
c: time low
_: version
d: clock sequence
e: node id