Skip to content

Commit

Permalink
HHH-18377 Renamed State record properties with addres 'last' prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
cigaly authored and beikov committed Oct 16, 2024
1 parent 6daec2e commit 3419f8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ private static class Holder {

}

private record State(long timestamp, int sequence) {
private record State(long lastTimestamp, int lastSequence) {
public State getNextState() {
final long now = instantToTimestamp();
if ( this.timestamp < now ) {
if ( this.lastTimestamp < now ) {
return new State(
now,
randomSequence()
);
}
else if ( sequence == 0x3FFF ) {
else if ( lastSequence == 0x3FFF ) {
return new State(
this.timestamp + 1,
this.lastTimestamp + 1,
randomSequence()
);
}
else {
return new State( timestamp, sequence + 1 );
return new State( lastTimestamp, lastSequence + 1 );
}
}

Expand Down Expand Up @@ -104,15 +104,15 @@ public UUID generateUuid(final SharedSessionContractImplementor session) {

return new UUID(
// MSB bits 0-47 - most significant 32 bits of the 60-bit starting timestamp
state.timestamp << 4 & 0xFFFF_FFFF_FFFF_0000L
state.lastTimestamp << 4 & 0xFFFF_FFFF_FFFF_0000L
// MSB bits 48-51 - version = 6
| 0x6000L
// MSB bits 52-63 - least significant 12 bits from the 60-bit starting timestamp
| state.timestamp & 0x0FFFL,
| state.lastTimestamp & 0x0FFFL,
// LSB bits 0-1 - variant = 4
0x8000_0000_0000_0000L
// LSB bits 2-15 - clock sequence
| ( state.sequence & 0x3FFFL ) << 48
| ( state.lastSequence & 0x3FFFL ) << 48
// LSB bits 16-63 - pseudorandom data, least significant bit of the first octet is set to 1
| randomNode()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ private static class Holder {

}

public record State(Instant timestamp, int sequence) {
public record State(Instant lastTimestamp, int lastSequence) {
public long millis() {
return timestamp.toEpochMilli();
return lastTimestamp.toEpochMilli();
}

public long nanos() {
return (long) ( ( timestamp.getNano() % 1_000_000L ) * 0.004096 );
return (long) ( ( lastTimestamp.getNano() % 1_000_000L ) * 0.004096 );
}

public State getNextState() {
final Instant now = Instant.now();
if ( timestamp.toEpochMilli() < now.toEpochMilli() ) {
if ( lastTimestamp.toEpochMilli() < now.toEpochMilli() ) {
return new State(
now.truncatedTo( MILLIS ),
randomSequence()
);
}
else if ( sequence == 0x3FFF ) {
else if ( lastSequence == 0x3FFF ) {
return new State(
timestamp.plusMillis( 1 ),
lastTimestamp.plusMillis( 1 ),
Holder.numberGenerator.nextInt( 1 << 14 )
);
}
else {
return new State( timestamp, sequence + 1 );
return new State( lastTimestamp, lastSequence + 1 );
}
}

Expand Down Expand Up @@ -116,7 +116,7 @@ public UUID generateUuid(final SharedSessionContractImplementor session) {
// LSB bits 0-1 - variant = 4
0x8000_0000_0000_0000L
// LSB bits 2-15 - counter
| ( state.sequence & 0x3FFFL ) << 48
| ( state.lastSequence & 0x3FFFL ) << 48
// LSB bits 16-63 - pseudorandom data
| randomNode()
);
Expand Down

0 comments on commit 3419f8f

Please sign in to comment.