-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* uuid: added uuid v7, can be used as db friendly primary key
Signed-off-by: neo <1100909+neowu@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,41 @@ | ||
package core.framework.util; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.security.SecureRandom; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
public class UUIDv7 { | ||
// refer to https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#name-uuid-version-7 | ||
private static final SecureRandom RANDOM = new SecureRandom(); | ||
|
||
public static UUID randomUUID() { | ||
byte[] value = randomBytes(); | ||
ByteBuffer buf = ByteBuffer.wrap(value); | ||
long high = buf.getLong(); | ||
long low = buf.getLong(); | ||
return new UUID(high, low); | ||
} | ||
|
||
private static byte[] randomBytes() { | ||
byte[] value = new byte[16]; | ||
RANDOM.nextBytes(value); | ||
|
||
long timestamp = System.currentTimeMillis(); | ||
// timestamp | ||
value[0] = (byte) ((timestamp >> 40) & 0xFF); | ||
value[1] = (byte) ((timestamp >> 32) & 0xFF); | ||
value[2] = (byte) ((timestamp >> 24) & 0xFF); | ||
value[3] = (byte) ((timestamp >> 16) & 0xFF); | ||
value[4] = (byte) ((timestamp >> 8) & 0xFF); | ||
value[5] = (byte) (timestamp & 0xFF); | ||
|
||
// version and variant | ||
value[6] = (byte) ((value[6] & 0x0F) | 0x70); | ||
value[8] = (byte) ((value[8] & 0x3F) | 0x80); | ||
|
||
return value; | ||
} | ||
} |
This file contains 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,15 @@ | ||
package core.framework.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class UUIDv7Test { | ||
@Test | ||
void randomUUID() { | ||
UUID uuid = UUIDv7.randomUUID(); | ||
assertThat(uuid.version()).isEqualTo(7); | ||
} | ||
} |