Skip to content
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

IH-577 Implement v7 UUID generation #5866

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rdn32
Copy link

@rdn32 rdn32 commented Jul 19, 2024

  • New function Uuidx.make_v7_uuid, with the idea being that ordering v7 UUIDs alphabetically will also order them by creation time
  • The values produced by Uuidx.make_uuid_urnd hadn't necessarily been valid UUIDs, since the variant and version fields were being filled in randomly - this is now fixed so that it returns v4 UUIDs.
  • There are a couple of functions for generating v4 and v7 from known inputs, for the purpose of unit testing. (The v4 function is mainly there so I could check the setting of variant and version fields by comparing the output with that which Python's UUID module produces.)

@rdn32 rdn32 force-pushed the private/rnewton/IH-577-uuidv7 branch from 7b9e9f6 to c5085e6 Compare July 19, 2024 13:08
val make_v7_uuid_from_time_and_bytes : int * int64 -> int64 -> 'a t
(** For testing only: create a v7 UUID, as defined in RFC 9562 5.7 *)

val make_v7_uuid : unit -> 'a t
Copy link
Member

Choose a reason for hiding this comment

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

Could this be changed to accept a function for the timestamp? That way we could use a monotonic clock source. The signature could be (unit -> uint64) -> 'a t
I know it would not be standard-compliant, and we would need to be careful about not persisting the produced timestamps, but I believe losing monotonicity here is something very undesired.

Copy link
Author

Choose a reason for hiding this comment

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

I've gone for a hybrid approach whereby I use ptime to get the system time initially, then use mtime to get monotonic adjustments to that - so that we keep monotonicity but still have timestamps that are at least approximately correct.

@rdn32 rdn32 force-pushed the private/rnewton/IH-577-uuidv7 branch 2 times, most recently from f416ed4 to 037eef9 Compare August 9, 2024 12:31
@lindig
Copy link
Contributor

lindig commented Aug 12, 2024

Is there a chance to upstream this code? We are using uuidm and it would be nice to expose this to more scrutiny by upstreaming it.

@psafont
Copy link
Member

psafont commented Aug 22, 2024

Is there a chance to upstream this code? We are using uuidm and it would be nice to expose this to more scrutiny by upstreaming it.

Because it's a new standard, it has leeway on how it can be implemented, and it needs to be as an additional package, I think there's a low chance to get into upstream. Having the suggestion upstream would maybe make its author to implement it

@rdn32
Copy link
Author

rdn32 commented Aug 30, 2024

It looks like generating a timestamp can't reasonably be done by uuidm (without introducing a dependency) but constructing v7 UUID given a timestamp could be. I can put together a PR. and see how well it goes down.

@rdn32
Copy link
Author

rdn32 commented Sep 13, 2024

It looks like generating a timestamp can't reasonably be done by uuidm (without introducing a dependency) but constructing v7 UUID given a timestamp could be. I can put together a PR. and see how well it goes down.

The PR is dbuenzli/uuidm#14. Daniel Bünzli has added a '👍' reaction, which I take to be a good sign.

@psafont
Copy link
Member

psafont commented Sep 13, 2024

He usually commits his own version of the change while referencing the original author

@rdn32 rdn32 force-pushed the private/rnewton/IH-577-uuidv7 branch from 037eef9 to 24a4f75 Compare September 13, 2024 14:39
@dbuenzli
Copy link

Please chime in on the PR to discuss this. I think it's a good idea to add basic support for v7 in uuidm.

* New function Uuidx.make_v7_uuid, with the idea being that ordering v7
  UUIDs alphabetically will also order them by creation time. The code
  that constructs a v7 UUIDs from a time and some random bytes has been
  submitted to uuidm and will be present in v0.9.9.
* The values produced by Uuidx.make_uuid_urnd hadn't necessarily been
  valid UUIDs, since the variant and version fields were being filled in
  randomly - this is now fixed so that it returns v4 UUIDs as
  constructed by the uuidm module.
* There is a function for generating v7 from known inputs, for the
  purpose of unit testing.

Signed-off-by: Robin Newton <robin.newton@cloud.com>
@rdn32 rdn32 force-pushed the private/rnewton/IH-577-uuidv7 branch from 24a4f75 to bbb2135 Compare September 27, 2024 17:25
@lindig
Copy link
Contributor

lindig commented Sep 30, 2024

I understand that v7 UUID will be implemented upstream as I suggested - which is great. Could you summarize what this PR achieves?

@rdn32
Copy link
Author

rdn32 commented Oct 4, 2024

I understand that v7 UUID will be implemented upstream as I suggested - which is great. Could you summarize what this PR achieves?

The main thing it does is to give uuidx a wrapper for uuidm's new v7 UUID generation - including producing monotonic-yet-broadly-accurate timestamps.

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

Successfully merging this pull request may close these issues.

4 participants