You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: generator.go
+46-14Lines changed: 46 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -81,9 +81,9 @@ func NewV6() (UUID, error) {
81
81
}
82
82
83
83
// NewV7 returns a k-sortable UUID based on the current millisecond precision
84
-
// UNIX epoch and 74 bits of pseudorandom data.
84
+
// UNIX epoch and 74 bits of pseudorandom data. It supports single-node batch generation (multiple UUIDs in the same timestamp) with a Monotonic Random counter.
85
85
//
86
-
// This is implemented based on revision 03 of the Peabody UUID draft, and may
86
+
// This is implemented based on revision 04 of the Peabody UUID draft, and may
87
87
// be subject to change pending further revisions. Until the final specification
88
88
// revision is finished, changes required to implement updates to the spec will
89
89
// not be considered a breaking change. They will happen as a minor version
//UUIDv7 features a 48 bit timestamp. First 32bit (4bytes) represents seconds since 1970, followed by 2 bytes for the ms granularity.
377
+
u[0] =byte(ms>>40) //1-6 bytes: big-endian unsigned number of Unix epoch timestamp
358
378
u[1] =byte(ms>>32)
359
379
u[2] =byte(ms>>24)
360
380
u[3] =byte(ms>>16)
361
381
u[4] =byte(ms>>8)
362
382
u[5] =byte(ms)
363
383
384
+
//support batching by using a monotonic pseudo-random sequence
385
+
//The 6th byte contains the version and partially rand_a data.
386
+
//We will lose the most significant bites from the clockSeq (with SetVersion), but it is ok, we need the least significant that contains the counter to ensure the monotonic property
387
+
binary.BigEndian.PutUint16(u[6:8], clockSeq) // set rand_a with clock seq which is random and monotonic
388
+
389
+
//override first 4bits of u[6].
364
390
u.SetVersion(V7)
391
+
392
+
//set rand_b 64bits of pseudo-random bits (first 2 will be overridden)
393
+
if_, err=io.ReadFull(g.rand, u[8:16]); err!=nil {
394
+
returnNil, err
395
+
}
396
+
//override first 2 bits of byte[8] for the variant
0 commit comments