D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Random
/
Formatter
/
Filename :
uuid_v7-i.ri
back
Copy
U:RDoc::AnyMethod[iI"uuid_v7:ETI"Random::Formatter#uuid_v7;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"?Generate a random v7 UUID (Universally Unique IDentifier).;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I" require 'random/formatter' ;TI" ;TI"@Random.uuid_v7 # => "0188d4c3-1311-7f96-85c7-242a7aa58f1e" ;TI"@Random.uuid_v7 # => "0188d4c3-16fe-744f-86af-38fa04c62bb5" ;TI"@Random.uuid_v7 # => "0188d4c3-1af8-764f-b049-c204ce0afa23" ;TI"@Random.uuid_v7 # => "0188d4c3-1e74-7085-b14f-ef6415dc6f31" ;TI"?# |<--sorted-->| |<----- random ---->| ;TI" ;TI" # or ;TI"prng = Random.new ;TI">prng.uuid_v7 # => "0188ca51-5e72-7950-a11d-def7ff977c98" ;T:@format0o; ; [I"NThe version 7 UUID starts with the least significant 48 bits of a 64 bit ;TI"PUnix timestamp (milliseconds since the epoch) and fills the remaining bits ;TI">with random data, excluding the version and variant bits.;T@o; ; [ I"NThis allows version 7 UUIDs to be sorted by creation time. Time ordered ;TI"LUUIDs can be used for better database index locality of newly inserted ;TI"Rrecords, which may have a significant performance benefit compared to random ;TI"data inserts.;T@o; ; [I"<The result contains 74 random bits (9.25 random bytes).;T@o; ; [I"JNote that this method cannot be made reproducible because its output ;TI"6includes not only random bits but also timestamp.;T@o; ; [I"OSee RFC9562[https://www.rfc-editor.org/rfc/rfc9562] for details of UUIDv7.;T@S:RDoc::Markup::Heading: leveli : textI"Monotonicity;T@o; ; [I"LUUIDv7 has millisecond precision by default, so multiple UUIDs created ;TI"Lwithin the same millisecond are not issued in monotonically increasing ;TI"Horder. To create UUIDs that are time-ordered with sub-millisecond ;TI"Eprecision, up to 12 bits of additional timestamp may added with ;TI"Q+extra_timestamp_bits+. The extra timestamp precision comes at the expense ;TI"Pof random bits. Setting <tt>extra_timestamp_bits: 12</tt> provides ~244ns ;TI"?of precision, but only 62 random bits (7.75 random bytes).;T@o;; [I"prng = Random.new ;TI"=Array.new(4) { prng.uuid_v7(extra_timestamp_bits: 12) } ;TI" # => ;TI".["0188d4c7-13da-74f9-8b53-22a786ffdd5a", ;TI". "0188d4c7-13da-753b-83a5-7fb9b2afaeea", ;TI". "0188d4c7-13da-754a-88ea-ac0baeedd8db", ;TI". "0188d4c7-13da-7557-83e1-7cad9cda0d8d"] ;TI",# |<--- sorted --->| |<-- random --->| ;TI" ;TI"<Array.new(4) { prng.uuid_v7(extra_timestamp_bits: 8) } ;TI" # => ;TI".["0188d4c7-3333-7a95-850a-de6edb858f7e", ;TI"A "0188d4c7-3333-7ae8-842e-bc3a8b7d0cf9", # <- out of order ;TI"A "0188d4c7-3333-7ae2-995a-9f135dc44ead", # <- out of order ;TI". "0188d4c7-3333-7af9-87c3-8f612edac82e"] ;TI",# |<--- sorted -->||<---- random --->| ;T; 0o; ; [ I"QAny rollbacks of the system clock will break monotonicity. UUIDv7 is based ;TI"Oon UTC, which excludes leap seconds and can rollback the clock. To avoid ;TI"Qthis, the system clock can synchronize with an NTP server configured to use ;TI"Ma "leap smear" approach. NTP or PTP will also be needed to synchronize ;TI"across distributed nodes.;T@o; ; [ I"OCounters and other mechanisms for stronger guarantees of monotonicity are ;TI"Mnot implemented. Applications with stricter requirements should follow ;TI"_{Section 6.2}[https://www.rfc-editor.org/rfc/rfc9562.html#name-monotonicity-and-counters] ;TI"of the specification.;T: @fileI"lib/random/formatter.rb;T:0@omit_headings_from_table_of_contents_below000[ I"(extra_timestamp_bits: 0);T@[FI"Formatter;TcRDoc::NormalModule00