-
-
Notifications
You must be signed in to change notification settings - Fork 112
Add alternate version to UUIDv7 generator that uses random values on every call (not just for different timestamp) #94
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ebf9312
Add alternate version to UUIDv7 generator. TimeBaseEpochGenerator cre…
magdel 9ba8ce8
Merge branch 'master' into feature/epochRandom
cowtowncoder fa08798
Update release notes, javadocs, version references
cowtowncoder 639b012
Improve code sharing a bit by demoting byte access methods to base class
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/main/java/com/fasterxml/uuid/impl/TimeBasedEpochRandomGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.fasterxml.uuid.impl; | ||
|
||
import com.fasterxml.uuid.NoArgGenerator; | ||
import com.fasterxml.uuid.UUIDClock; | ||
import com.fasterxml.uuid.UUIDType; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* Implementation of UUID generator that uses time/location based generation | ||
* method field from the Unix Epoch timestamp source - the number of | ||
* milliseconds seconds since midnight 1 Jan 1970 UTC, leap seconds excluded. | ||
* This is usually referred to as "Version 7". | ||
* In addition to that random part is regenerated for every new UUID. | ||
* This removes possibilities to have almost similar UUID, when calls | ||
* to generate are made within same millisecond. | ||
* <p> | ||
* As all JUG provided implementations, this generator is fully thread-safe. | ||
* Additionally it can also be made externally synchronized with other instances | ||
* (even ones running on other JVMs); to do this, use | ||
* {@link com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer} (or | ||
* equivalent). | ||
* | ||
* @since 4.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5.0 |
||
*/ | ||
public class TimeBasedEpochRandomGenerator extends NoArgGenerator | ||
{ | ||
private static final int ENTROPY_BYTE_LENGTH = 10; | ||
|
||
/* | ||
/********************************************************************** | ||
/* Configuration | ||
/********************************************************************** | ||
*/ | ||
|
||
/** | ||
* Random number generator that this generator uses. | ||
*/ | ||
protected final Random _random; | ||
|
||
/** | ||
* Underlying {@link UUIDClock} used for accessing current time, to use for | ||
* generation. | ||
* | ||
* @since 4.3 | ||
*/ | ||
protected final UUIDClock _clock; | ||
|
||
private final byte[] _lastEntropy = new byte[ENTROPY_BYTE_LENGTH]; | ||
private final Lock lock = new ReentrantLock(); | ||
|
||
/* | ||
/********************************************************************** | ||
/* Construction | ||
/********************************************************************** | ||
*/ | ||
|
||
/** | ||
* @param rnd Random number generator to use for generating UUIDs; if null, | ||
* shared default generator is used. Note that it is strongly recommend to | ||
* use a <b>good</b> (pseudo) random number generator; for example, JDK's | ||
* {@link SecureRandom}. | ||
*/ | ||
public TimeBasedEpochRandomGenerator(Random rnd) { | ||
this(rnd, UUIDClock.systemTimeClock()); | ||
} | ||
|
||
/** | ||
* @param rnd Random number generator to use for generating UUIDs; if null, | ||
* shared default generator is used. Note that it is strongly recommend to | ||
* use a <b>good</b> (pseudo) random number generator; for example, JDK's | ||
* {@link SecureRandom}. | ||
* @param clock clock Object used for accessing current time to use for generation | ||
*/ | ||
public TimeBasedEpochRandomGenerator(Random rnd, UUIDClock clock) | ||
{ | ||
if (rnd == null) { | ||
rnd = LazyRandom.sharedSecureRandom(); | ||
} | ||
_random = rnd; | ||
_clock = clock; | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Access to config | ||
/********************************************************************** | ||
*/ | ||
|
||
@Override | ||
public UUIDType getType() { return UUIDType.TIME_BASED_EPOCH; } | ||
|
||
/* | ||
/********************************************************************** | ||
/* UUID generation | ||
/********************************************************************** | ||
*/ | ||
|
||
@Override | ||
public UUID generate() | ||
{ | ||
return construct(_clock.currentTimeMillis()); | ||
} | ||
|
||
/** | ||
* Method that will construct actual {@link UUID} instance for given | ||
* unix epoch timestamp: called by {@link #generate()} but may alternatively be | ||
* called directly to construct an instance with known timestamp. | ||
* NOTE: calling this method directly produces somewhat distinct UUIDs as | ||
* "entropy" value is still generated as necessary to avoid producing same | ||
* {@link UUID} even if same timestamp is being passed. | ||
* | ||
* @param rawTimestamp unix epoch millis | ||
* | ||
* @return unix epoch time based UUID | ||
* | ||
* @since 4.3 | ||
*/ | ||
public UUID construct(long rawTimestamp) | ||
{ | ||
lock.lock(); | ||
try { | ||
_random.nextBytes(_lastEntropy); | ||
return UUIDUtil.constructUUID(UUIDType.TIME_BASED_EPOCH, (rawTimestamp << 16) | _toShort(_lastEntropy, 0), _toLong(_lastEntropy, 2)); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Internal methods | ||
/********************************************************************** | ||
*/ | ||
|
||
protected final static long _toLong(byte[] buffer, int offset) | ||
{ | ||
long l1 = _toInt(buffer, offset); | ||
long l2 = _toInt(buffer, offset+4); | ||
long l = (l1 << 32) + ((l2 << 32) >>> 32); | ||
return l; | ||
} | ||
|
||
private final static long _toInt(byte[] buffer, int offset) | ||
{ | ||
return (buffer[offset] << 24) | ||
+ ((buffer[++offset] & 0xFF) << 16) | ||
+ ((buffer[++offset] & 0xFF) << 8) | ||
+ (buffer[++offset] & 0xFF); | ||
} | ||
|
||
private final static long _toShort(byte[] buffer, int offset) | ||
{ | ||
return ((buffer[offset] & 0xFF) << 8) | ||
+ (buffer[++offset] & 0xFF); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.