|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.id.uuid; |
| 8 | + |
| 9 | +import java.security.SecureRandom; |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.Instant; |
| 12 | +import java.time.LocalDate; |
| 13 | +import java.time.ZoneId; |
| 14 | +import java.util.UUID; |
| 15 | +import java.util.concurrent.atomic.AtomicLong; |
| 16 | +import java.util.concurrent.locks.Lock; |
| 17 | +import java.util.concurrent.locks.ReentrantLock; |
| 18 | + |
| 19 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 20 | +import org.hibernate.id.UUIDGenerationStrategy; |
| 21 | + |
| 22 | +/** |
| 23 | + * Implements a <a href="https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-6">UUID Version 6</a> generation strategy as defined by the {@link UUID#randomUUID()} method. |
| 24 | + * |
| 25 | + * @author Cedomir Igaly |
| 26 | + */ |
| 27 | +public class UuidV6ValueGenerator implements UUIDGenerationStrategy, UuidValueGenerator { |
| 28 | + |
| 29 | + private static final Instant EPOCH_1582; |
| 30 | + |
| 31 | + static { |
| 32 | + EPOCH_1582 = LocalDate.of( 1582, 10, 15 ) |
| 33 | + .atStartOfDay( ZoneId.of( "UTC" ) ) |
| 34 | + .toInstant(); |
| 35 | + } |
| 36 | + |
| 37 | + private static class Holder { |
| 38 | + |
| 39 | + static final SecureRandom numberGenerator = new SecureRandom(); |
| 40 | + } |
| 41 | + |
| 42 | + public static final UuidV6ValueGenerator INSTANCE = new UuidV6ValueGenerator(); |
| 43 | + |
| 44 | + private final Lock lock = new ReentrantLock( true ); |
| 45 | + |
| 46 | + private long currentTimestamp; |
| 47 | + |
| 48 | + private final AtomicLong clockSequence = new AtomicLong( 0 ); |
| 49 | + |
| 50 | + public UuidV6ValueGenerator() { |
| 51 | + this( getCurrentTimestamp(), 0 ); |
| 52 | + } |
| 53 | + |
| 54 | + public UuidV6ValueGenerator(final long currentTimestamp, final long clockSequence) { |
| 55 | + this.currentTimestamp = currentTimestamp; |
| 56 | + this.clockSequence.set( clockSequence ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * A variant 6 |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public int getGeneratedVersion() { |
| 64 | + // UUIDv6 is a field-compatible version of UUIDv1, reordered for improved DB locality |
| 65 | + return 6; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Delegates to {@link UUID#randomUUID()} |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public UUID generateUUID(SharedSessionContractImplementor session) { |
| 73 | + return generateUuid( session ); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * <ul> |
| 79 | + * <li>32 bits - the most significant 32 bits of the 60-bit starting timestamp.</li> |
| 80 | + * <li>16 bits - the middle 16 bits of the 60-bit starting timestamp.</li> |
| 81 | + * <li>4 bits - version field, set to 0b0110 (6).</li> |
| 82 | + * <li>12 bits - the least significant 12 bits from the 60-bit starting timestamp.</li> |
| 83 | + * <li>2 bits - variant field, set to 0b10.</li> |
| 84 | + * <li>14 bits - the clock sequence, resets to 0 when timestamp changes. </li> |
| 85 | + * <li>48 bits - pseudorandom data to provide uniqueness.</li> |
| 86 | + * </ul> |
| 87 | + * |
| 88 | + * @param session session |
| 89 | + * |
| 90 | + * @return UUID version 6 |
| 91 | + * @see UuidValueGenerator#generateUuid(SharedSessionContractImplementor) |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public UUID generateUuid(SharedSessionContractImplementor session) { |
| 95 | + final long currentTimestamp = getCurrentTimestamp(); |
| 96 | + |
| 97 | + return new UUID( |
| 98 | + currentTimestamp << 4 & 0xFFFF_FFFF_FFFF_0000L |
| 99 | + | 0x6000L |
| 100 | + | currentTimestamp & 0x0FFFL, |
| 101 | + 0x8000_0000_0000_0000L |
| 102 | + | ( getSequence( currentTimestamp ) & 0x3FFFL ) << 48 |
| 103 | + | Holder.numberGenerator.nextLong() & 0xFFFF_FFFF_FFFFL |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + private long getSequence(final long currentTimestamp) { |
| 109 | + lock.lock(); |
| 110 | + try { |
| 111 | + if ( this.currentTimestamp > currentTimestamp ) { |
| 112 | + this.currentTimestamp = currentTimestamp; |
| 113 | + clockSequence.set( 0 ); |
| 114 | + } |
| 115 | + } |
| 116 | + finally { |
| 117 | + lock.unlock(); |
| 118 | + } |
| 119 | + return clockSequence.getAndIncrement(); |
| 120 | + } |
| 121 | + |
| 122 | + private static long getCurrentTimestamp() { |
| 123 | + final Duration duration = Duration.between( EPOCH_1582, Instant.now() ); |
| 124 | + return duration.toSeconds() * 10_000_000 + duration.toNanosPart() / 100; |
| 125 | + } |
| 126 | +} |
0 commit comments