-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create UUIDs stdlib package (#25819)
closes #25590
- Loading branch information
1 parent
54adf21
commit 0f95988
Showing
8 changed files
with
106 additions
and
100 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
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
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,7 @@ | ||
# UUIDs | ||
|
||
```@docs | ||
UUIDs.uuid1 | ||
UUIDs.uuid4 | ||
UUIDs.uuid_version | ||
``` |
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,82 @@ | ||
module UUIDs | ||
|
||
using Random | ||
|
||
export UUID, uuid1, uuid4, uuid_version | ||
|
||
import Base: UUID | ||
|
||
""" | ||
uuid_version(u::UUID) -> Int | ||
Inspects the given UUID and returns its version | ||
(see [RFC 4122](https://www.ietf.org/rfc/rfc4122)). | ||
# Examples | ||
```jldoctest | ||
julia> uuid_version(uuid4()) | ||
4 | ||
``` | ||
""" | ||
uuid_version(u::UUID) = Int((u.value >> 76) & 0xf) | ||
|
||
""" | ||
uuid1([rng::AbstractRNG=GLOBAL_RNG]) -> UUID | ||
Generates a version 1 (time-based) universally unique identifier (UUID), as specified | ||
by RFC 4122. Note that the Node ID is randomly generated (does not identify the host) | ||
according to section 4.5 of the RFC. | ||
# Examples | ||
```jldoctest | ||
julia> rng = MersenneTwister(1234); | ||
julia> uuid1(rng) | ||
2cc938da-5937-11e7-196e-0f4ef71aa64b | ||
``` | ||
""" | ||
function uuid1(rng::AbstractRNG=Random.GLOBAL_RNG) | ||
u = rand(rng, UInt128) | ||
|
||
# mask off clock sequence and node | ||
u &= 0x00000000000000003fffffffffffffff | ||
|
||
# set the unicast/multicast bit and version | ||
u |= 0x00000000000010000000010000000000 | ||
|
||
# 0x01b21dd213814000 is the number of 100 nanosecond intervals | ||
# between the UUID epoch and Unix epoch | ||
timestamp = round(UInt64, time() * 1e7) + 0x01b21dd213814000 | ||
ts_low = timestamp & typemax(UInt32) | ||
ts_mid = (timestamp >> 32) & typemax(UInt16) | ||
ts_hi = (timestamp >> 48) & 0x0fff | ||
|
||
u |= UInt128(ts_low) << 96 | ||
u |= UInt128(ts_mid) << 80 | ||
u |= UInt128(ts_hi) << 64 | ||
|
||
UUID(u) | ||
end | ||
|
||
""" | ||
uuid4([rng::AbstractRNG=GLOBAL_RNG]) -> UUID | ||
Generates a version 4 (random or pseudo-random) universally unique identifier (UUID), | ||
as specified by RFC 4122. | ||
# Examples | ||
```jldoctest | ||
julia> rng = MersenneTwister(1234); | ||
julia> uuid4(rng) | ||
82015f10-44cc-4827-996e-0f4ef71aa64b | ||
``` | ||
""" | ||
function uuid4(rng::AbstractRNG=Random.GLOBAL_RNG) | ||
u = rand(rng, UInt128) | ||
u &= 0xffffffffffff0fff3fffffffffffffff | ||
u |= 0x00000000000040008000000000000000 | ||
UUID(u) | ||
end | ||
|
||
end |
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,14 @@ | ||
using UUIDs, Random | ||
|
||
u1 = uuid1() | ||
u4 = uuid4() | ||
@test uuid_version(u1) == 1 | ||
@test uuid_version(u4) == 4 | ||
@test u1 == UUID(string(u1)) == UUID(GenericString(string(u1))) | ||
@test u4 == UUID(string(u4)) == UUID(GenericString(string(u4))) | ||
@test u1 == UUID(UInt128(u1)) | ||
@test u4 == UUID(UInt128(u4)) | ||
@test uuid4(MersenneTwister(0)) == uuid4(MersenneTwister(0)) | ||
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-446655440000") | ||
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-44665544000098") | ||
@test_throws ArgumentError UUID("z50e8400-e29b-41d4-a716-446655440000") |
0f95988
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.
Executing the daily benchmark build, I will reply here when finished:
@nanosoldier
runbenchmarks(ALL, isdaily = true)
0f95988
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.
Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan