diff options
author | nick evans <[email protected]> | 2023-06-29 18:39:29 -0400 |
---|---|---|
committer | git <[email protected]> | 2023-09-19 01:55:27 +0000 |
commit | dfb2b4cbc9aa5edc315e210bf6bfd92fcf6e45de () | |
tree | 239d6eb81409e7d3e3fecfd979570ef2fa44fd05 | |
parent | e77c766b7ab17e801c5cfa881754c392f8c13f0b (diff) |
[ruby/securerandom] Add support for UUID version 7
Although the specification for UUIDv7 is still in draft, the UUIDv7 algorithm has been relatively stable as it progresses to completion. Version 7 UUIDs can be very useful, because they are lexographically sortable, which can improve e.g: database index locality. See section 6.10 of the draft specification for further explanation: https://datatracker.ietf.org/doc/draft-ietf-uuidrev-rfc4122bis/ The specification allows up to 12 bits of extra timestamp precision, to make UUID generation closer to monotonically increasing. This provides between 1ms and ~240ns of timestamp precision. At the cost of some code complexity and a small performance penalty, a kwarg may specify any arbitrary precision between 0 and 12 extra bits. Any stronger guarantees of monotonicity have considerably larger tradeoffs, so nothing more is implemented. This limitation is documented. Ruby issue: https://bugs.ruby-lang.org/issues/19735 https://.com/ruby/securerandom/commit/34ed1a2ec3
-rw-r--r-- | lib/random/formatter.rb | 119 | ||||
-rw-r--r-- | test/ruby/test_random_formatter.rb | 48 |
2 files changed, 167 insertions, 0 deletions
@@ -174,6 +174,125 @@ module Random::Formatter "%08x-%04x-%04x-%04x-%04x%08x" % ary end private def gen_random(n) self.bytes(n) end @@ -75,6 +75,54 @@ module Random::Formatter assert_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/, uuid) end def test_alphanumeric 65.times do |n| an = @it.alphanumeric(n) |