-
-
Notifications
You must be signed in to change notification settings - Fork 110
Variant V7 #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variant V7 #48
Conversation
public synchronized long getTimestamp() | ||
public long getTimestamp() | ||
{ | ||
return getTimestamp(kClockOffset); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
took the synchronized off and just punt to the protected version with the Gregorian offset
* timestamp source - the number of milliseconds seconds since midnight 1 Jan 1970 UTC, | ||
* leap seconds excluded | ||
*/ | ||
public long getTimestampV7() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new timestamp for variant 7
return getTimestamp(0); | ||
} | ||
|
||
protected synchronized long getTimestamp(long offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original timestamp method, with the offset passed in
int midhi = (clockHi << 16) | (clockHi >>> 16); | ||
final byte[] b = new byte[2]; | ||
_random.nextBytes(b); | ||
midhi = midhi | (((b[0] & 0xFF) << 8) + (b[1] & 0xFF)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My brain is turned to mush from advanced age, so hopefully this is correct.
// and reconstruct | ||
long l1 = (((long) clockLo) << 32) | midhiL; | ||
// last detail: must force 2 MSB to be '10' | ||
long _uuidL2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec talks about the "variant" here in the 2 msb of this long, but I couldn't find what to use.
not sure how you managed to keep your mind when originally implementing this stuff.
// and reconstruct | ||
long l1 = (((long) clockLo) << 32) | midhiL; | ||
// last detail: must force 2 MSB to be '10' | ||
long _uuidL2 = _toLong(buffer, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
include variant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That 2-bit var
is a mystery to me... I don't get what that is, after reading and re-reading the spec. There's separate 4-bit version already so... what is this 2-bit thing. :-/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to future self: "variant" is 0x10 as per:
https://www.rfc-editor.org/rfc/rfc4122#section-4.1.1
and remains the same from the earlier spec. So that's all good. Although new code does the right thing and calls UUIDUtil.constructUUID
with type
so it all gets set properly.
Still fails because I am an idiot when it comes to bit twiddling
@Override | ||
public UUID generate() | ||
{ | ||
final long rawTimestamp = _timer.getTimestampV7(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cowtowncoder I am still a moron when it comes to bit twiddling. Have no idea what I'm doing wrong here ;)
I've fallen and I can't get up
// first we'll collect the UUID time stamp which is | ||
// the number of 100-nanosecond intervals since | ||
// 00:00:00.00 15 October 1582 | ||
long uuid_time = 0L; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this is correct, but...
checkUUIDArrayForUniqueness(uuid_array); | ||
|
||
// check that all uuids have timestamps between the start and end time | ||
// checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uncomment when my sanity is restored
Ok thanks; hope to read this later today... sounds like there may be quite a bit to digest again :) |
No worries. I’m likely just doing something obviously silly. |
{ | ||
// we need to convert from 100-nanosecond units (as used in UUIDs) | ||
// to millisecond units as used in UTC based time | ||
final long MILLI_CONVERSION_FACTOR = 10000L; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, no, Variant 7 uses time value in milliseconds, not 100 nsec (unlike variants 1 and 6)
Ok, so, my head is hurting wrt bit/byte shuffling code so I'll check that another day. But I did notice one problem: code assumes that Variant 7 uses 100nsec granularity, like existing time-based variant 1 (and new 6). But as far as I can read, spec says that V7 uses millisecond granularity for simplicity. This is the reason why those 12 bits of randomness are needed to pad up things. So, this complicates changes to Other than that looks good; I hope to look more into this within next couple days; at latest on Juneteenth when I might have a Free OSS Coding Day for a change. :) |
Think this should be correct now. |
@Override | ||
public UUID generate() | ||
{ | ||
ByteBuffer buff = ByteBuffer.allocate(2 * 8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
taking the cowards way out with a byte buffer
@Hellblazer I do believe this is correct! Good job & apologies once again for unreliable follow-up. |
buff.position(6); | ||
buff.put(buffer); | ||
buff.position(0); | ||
buff.putLong(rawTimestamp << 16); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implements #46.