S3 class "UUID" represents vector of UUIDs in native form (128-bit). They are typically obtained by calling UUIDgenerate, UUIDparse or as.UUID.

Methods exist for common operations such as as.character, print, c, subsetting and comparison operators. Note that arithmetic and other operations are not allowed.

UUIDs have three possible representations: as character vectors (in the hyphenated 8-4-4-4-12 hexadecimal form), the UUID class described here and raw vectors. In the latter case the raw vector must be of length 16 or it must be a matrix with 16 rows. Since matrices in R are stored in colum-major format, UUID must be contiguous and thus form the columns of the raw matrix, which may be slightly counter-intuitive, but is far moreefficient.

as.character method exist for UUID objects and converts it to a character vector of lower-case UUID string representation.

as.raw method converts UUIDs to raw vectors or matrices as describe above. Similarly, a as.UUID method for raw vectors performs the inverse transformation.

as.UUID(x, ...)
is.UUID(x)

Arguments

x

object to coerce / check

...

unused

Value

as.UUID returns an object of the class "UUID"

representing a vector of UUIDs. Any elements that are not valid UUIDs will yield NA values.

is.UUID returns TRUE if the object is of the class

"UUID" and FALSE otherwise.

Details

Internally, the underlying object uses complex numbers to store 128-bit values with each UUID represented as one complex number. There may be cases where some operations strip the class attribute which will lead to complex values being visible, but their behavior should be regarded as undefined.

NA values in the UUID class are internally stored as a special value a2070000-0000-f07f-a207-00000000f07f which is not a valid UUID (since the version of that UUID is 15 which does not exist). This is an R extension and will be automatically converted to NA where possible, but the raw format does not support NAs so it will be visible there. Coercions to/from string and UUIDs handle NAs correctly and thus this internal representation should not be relied upon by any code and may change in the future.

Note

Comparisons are much faster between UUID vectors than between UUID vectors and other types, because in the latter case all values are coerced to strings before comparison which is very expensive.

However, x == y does not necessarily yield the same result as as.UUID(x) == as.UUID(y). For example, for x a valid UUID object of length one and y = "foo" the former will be FALSE while the latter will be NA due to coercion not yielding a valid UUID value represented by NA.

Author

Simon Urbanek

Examples

(u <- as.UUID("837bc850-07d9-42f9-9afb-716409bf87b7"))
#> UUID: 837bc850-07d9-42f9-9afb-716409bf87b7
(uv <- c(u, NA, UUIDgenerate(n=3, output="uuid")))
#> UUID vector:
#> [1] "837bc850-07d9-42f9-9afb-716409bf87b7"
#> [2] NA                                    
#> [3] "67f0c77d-5f58-45c7-96b0-a9657849bc50"
#> [4] "f3ff966b-7a30-4f4d-9b75-ec8ae4d95e34"
#> [5] "aad98bb6-89af-4be2-bbea-7a671c7a0359"
as.character(u)
#> [1] "837bc850-07d9-42f9-9afb-716409bf87b7"
uv == u
#> [1]  TRUE    NA FALSE FALSE FALSE
is.na(uv)
#> [1] FALSE  TRUE FALSE FALSE FALSE
identical(as.UUID(as.character(uv)), uv)
#> [1] TRUE
as.raw(u)
#>  [1] 83 7b c8 50 07 d9 42 f9 9a fb 71 64 09 bf 87 b7

## all forms are can be coerced losslessly
identical(as.UUID(as.raw(uv)), uv)
#> [1] TRUE
identical(as.UUID(as.character(as.UUID(as.raw(uv)))), uv)
#> [1] TRUE