Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Uid] Add UuidV1::toV6(), UuidV1::toV7() and UuidV6::toV7() #53060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 26, 2023

Conversation

fancyweb
Copy link
Contributor

@fancyweb fancyweb commented Dec 13, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? false
Issues -
License MIT

The goal is to help working with new UUIDv6 & v7 versions when you receive legacy UUIDv1.

UUIDv6 and v1 are 100% compatible since v6 is just a "reordering" of v1.

UUIDv7 and v1 are not strictly compatible for several reasons: the timestamp precision is not the same. Also, there is no clock seq or variant or node in v7 but full randomness instead. And maybe others?
However, I believe we can still technically do the conversion (timestamp -> timestamp, clock seq + variant + node -> randomness) with at least one concession: the sub-milliseconds v1 timestamp precision is lost since v7 doesn't support it.

My proposed v1 to v7 implementation has 2 issues:

  • sub-microsecond entropy is entirely lost
  • monotonicity is lost?

I'm opening this PR so that @nicolas-grekas gets triggered by the inefficiency of my code and finds a better solution 😁
I'm kidding, I actually know he already has an idea because he explained it to me during the Brussels HackDay but I couldn't understand it well enough to be able to implement it 😅

@nicolas-grekas nicolas-grekas changed the title [Uid] Add UuidV6::fromV1() and UuidV7::fromV1() methods [Uid] Add UuidV1::toV6(), UuidV1::toV7() and UuidV6::toV7() Dec 19, 2023
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pushing this :)

I added a second commit where I renamed from*() methods to to*()
and I made the resulting UUIDv7 monotonic within the same 100-ns (modulo the clock-seq)

Copy link
Contributor Author

@fancyweb fancyweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for looking at this Nicolas 🐐

It looks like we are close to the perfect solution thanks to you. However, after reviewing it and testing it I've found that the xor doesn't always produce valid UUIDs (see failing test).

Also, what do you think of going directly from v1 to v7 to avoid the unneeded v6 step? I'm actually not even sure v6 -> v7 is needed at all since they are part of the same RFC iteration: I don't see any case where a system would be able to generate v6 but not v7 directly? 🤔

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Dec 21, 2023

Also, what do you think of going directly from v1 to v7 to avoid the unneeded v6 step? I'm actually not even sure v6 -> v7 is needed at all since they are part of the same RFC iteration: I don't see any case where a system would be able to generate v6 but not v7 directly? 🤔

It doesn't cost much to provide v6 to v7 so why skip it?

@fancyweb
Copy link
Contributor Author

I don't cost much to provide v6 to v7 so why skip it?

Because nobody will use it 😅

I reviewed it again and it's OK for me. I can't approve it since it's my own PR. I force-pushed again to add more assertions.

@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants