Skip to content

Commit c3d7e5f

Browse files
committed
HHH-18377 - Support for uuid v6 and v7 generated ids
1 parent 2f335cd commit c3d7e5f

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

hibernate-core/src/main/java/org/hibernate/id/uuid/UuidVersion6Strategy.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.concurrent.locks.Lock;
1515
import java.util.concurrent.locks.ReentrantLock;
1616

17+
import org.hibernate.Internal;
1718
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1819
import org.hibernate.id.UUIDGenerationStrategy;
1920

@@ -30,64 +31,51 @@
3031
* <li>48 bits - pseudorandom data to provide uniqueness.</li>
3132
* </ul>
3233
*
34+
* @apiNote This strategy is field-compatible with Version 1, with the time bits reordered for improved DB locality.
35+
*
3336
* @author Cedomir Igaly
3437
*/
3538
public class UuidVersion6Strategy implements UUIDGenerationStrategy, UuidValueGenerator {
3639

37-
private static final Instant EPOCH_1582;
40+
public static final UuidVersion6Strategy INSTANCE = new UuidVersion6Strategy();
3841

39-
static {
40-
EPOCH_1582 = LocalDate.of( 1582, 10, 15 )
41-
.atStartOfDay( ZoneId.of( "UTC" ) )
42-
.toInstant();
43-
}
42+
private static final Instant EPOCH_1582 = LocalDate.of( 1582, 10, 15 )
43+
.atStartOfDay( ZoneId.of( "UTC" ) )
44+
.toInstant();
4445

4546
private static class Holder {
46-
4747
static final SecureRandom numberGenerator = new SecureRandom();
4848
}
4949

50-
public static final UuidVersion6Strategy INSTANCE = new UuidVersion6Strategy();
51-
5250
private final Lock lock = new ReentrantLock( true );
53-
51+
private final AtomicLong clockSequence = new AtomicLong( 0 );
5452
private long currentTimestamp;
5553

56-
private final AtomicLong clockSequence = new AtomicLong( 0 );
5754

55+
@Internal
5856
public UuidVersion6Strategy() {
5957
this( getCurrentTimestamp(), 0 );
6058
}
6159

60+
@Internal
6261
public UuidVersion6Strategy(final long currentTimestamp, final long clockSequence) {
6362
this.currentTimestamp = currentTimestamp;
6463
this.clockSequence.set( clockSequence );
6564
}
6665

6766
/**
68-
* A variant 6
67+
* Version 6
6968
*/
7069
@Override
7170
public int getGeneratedVersion() {
72-
// UUIDv6 is a field-compatible version of UUIDv1, reordered for improved DB locality
7371
return 6;
7472
}
7573

76-
/**
77-
* Delegates to {@link #generateUuid}
78-
*/
7974
@Override
8075
public UUID generateUUID(SharedSessionContractImplementor session) {
8176
return generateUuid( session );
8277
}
8378

84-
85-
/**
86-
* @param session session
87-
*
88-
* @return UUID version 6
89-
* @see UuidValueGenerator#generateUuid(SharedSessionContractImplementor)
90-
*/
9179
@Override
9280
public UUID generateUuid(SharedSessionContractImplementor session) {
9381
final long currentTimestamp = getCurrentTimestamp();

hibernate-core/src/main/java/org/hibernate/id/uuid/UuidVersion7Strategy.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.concurrent.locks.Lock;
1313
import java.util.concurrent.locks.ReentrantLock;
1414

15+
import org.hibernate.Internal;
1516
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1617
import org.hibernate.id.UUIDGenerationStrategy;
1718

@@ -33,59 +34,48 @@
3334
* <li>48 bits - pseudorandom data to provide uniqueness.</li>
3435
* </ul>
3536
*
37+
* @apiNote Version 7 features a time-ordered value field derived from the widely implemented and
38+
* well-known Unix Epoch timestamp source, the number of milliseconds since midnight 1 Jan 1970 UTC,
39+
* leap seconds excluded.
40+
*
3641
* @author Cedomir Igaly
3742
*/
3843
public class UuidVersion7Strategy implements UUIDGenerationStrategy, UuidValueGenerator {
3944

4045
public static final UuidVersion7Strategy INSTANCE = new UuidVersion7Strategy();
4146

4247
private static class Holder {
43-
4448
static final SecureRandom numberGenerator = new SecureRandom();
4549
}
4650

4751
private final Lock lock = new ReentrantLock( true );
48-
49-
private Duration currentTimestamp;
50-
5152
private final AtomicLong clockSequence;
53+
private Duration currentTimestamp;
5254

55+
@Internal
5356
public UuidVersion7Strategy() {
5457
this( getCurrentTimestamp(), 0 );
5558
}
5659

60+
@Internal
5761
public UuidVersion7Strategy(final Duration currentTimestamp, final long clockSequence) {
5862
this.currentTimestamp = currentTimestamp;
5963
this.clockSequence = new AtomicLong( clockSequence );
6064
}
6165

6266
/**
63-
* A variant 7
67+
* Version 7
6468
*/
6569
@Override
6670
public int getGeneratedVersion() {
67-
/*
68-
* UUIDv7 features a time-ordered value field derived from the widely implemented and well-
69-
* known Unix Epoch timestamp source, the number of milliseconds since midnight 1 Jan 1970 UTC,
70-
* leap seconds excluded.
71-
*/
7271
return 7;
7372
}
7473

75-
/**
76-
* Delegates to {@link #generateUuid}
77-
*/
7874
@Override
7975
public UUID generateUUID(SharedSessionContractImplementor session) {
8076
return generateUuid( session );
8177
}
8278

83-
/**
84-
* @param session session
85-
*
86-
* @return UUID version 7
87-
* @see UuidValueGenerator#generateUuid(SharedSessionContractImplementor)
88-
*/
8979
@Override
9080
public UUID generateUuid(SharedSessionContractImplementor session) {
9181
final Duration currentTimestamp = getCurrentTimestamp();

0 commit comments

Comments
 (0)