55package org .hibernate .id .uuid ;
66
77import java .security .SecureRandom ;
8- import java .time .Duration ;
98import java .time .Instant ;
109import java .util .UUID ;
1110import java .util .concurrent .atomic .AtomicLong ;
1615import org .hibernate .engine .spi .SharedSessionContractImplementor ;
1716import org .hibernate .id .UUIDGenerationStrategy ;
1817
19- import static java .time .Instant .EPOCH ;
2018import static java .time .temporal .ChronoUnit .MILLIS ;
2119
2220/**
@@ -50,15 +48,15 @@ private static class Holder {
5048
5149 private final Lock lock = new ReentrantLock ( true );
5250 private final AtomicLong clockSequence ;
53- private Duration currentTimestamp ;
51+ private Instant currentTimestamp ;
5452
5553 @ Internal
5654 public UuidVersion7Strategy () {
57- this ( getCurrentTimestamp (), 0 );
55+ this ( Instant . now (), 0 );
5856 }
5957
6058 @ Internal
61- public UuidVersion7Strategy (final Duration currentTimestamp , final long clockSequence ) {
59+ public UuidVersion7Strategy (final Instant currentTimestamp , final long clockSequence ) {
6260 this .currentTimestamp = currentTimestamp ;
6361 this .clockSequence = new AtomicLong ( clockSequence );
6462 }
@@ -78,12 +76,12 @@ public UUID generateUUID(SharedSessionContractImplementor session) {
7876
7977 @ Override
8078 public UUID generateUuid (SharedSessionContractImplementor session ) {
81- final Duration currentTimestamp = getCurrentTimestamp ();
79+ final Instant currentTimestamp = Instant . now ();
8280
8381 final long seq = getSequence ( currentTimestamp );
8482
85- final long millis = currentTimestamp .getSeconds () * 1000 + currentTimestamp . getNano () / 1_000_000 ;
86- final long nanosPart = Math . round ( ( currentTimestamp .getNano () % 1_000_000L ) * 0.004096 );
83+ final long millis = currentTimestamp .toEpochMilli () ;
84+ final long nanosPart = ( long ) ( ( currentTimestamp .getNano () % 1_000_000L ) * 0.004096 );
8785
8886 return new UUID (
8987 // MSB bits 0-47 - 48-bit big-endian unsigned number of the Unix Epoch timestamp in milliseconds
@@ -101,21 +99,17 @@ public UUID generateUuid(SharedSessionContractImplementor session) {
10199 );
102100 }
103101
104- private long getSequence (final Duration currentTimestamp ) {
102+ private long getSequence (final Instant currentTimestamp ) {
105103 lock .lock ();
106104 try {
107- if ( ! this .currentTimestamp .equals ( currentTimestamp ) ) {
108- this .currentTimestamp = currentTimestamp ;
109- clockSequence .set ( 0 );
105+ if ( this .currentTimestamp .toEpochMilli () < currentTimestamp . toEpochMilli ( ) ) {
106+ this .currentTimestamp = currentTimestamp . truncatedTo ( MILLIS ) ;
107+ clockSequence .updateAndGet ( l -> l & 0x1FFFL );
110108 }
111109 }
112110 finally {
113111 lock .unlock ();
114112 }
115113 return clockSequence .getAndIncrement ();
116114 }
117-
118- private static Duration getCurrentTimestamp () {
119- return Duration .between ( EPOCH , Instant .now () ).truncatedTo ( MILLIS );
120- }
121115}
0 commit comments