1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
## Create a universally unique identifier (UUID) for an R object
#' @importFrom digest digest
uuid <- function(source, keep_source = FALSE) {
uuid <- digest(source)
uuid <- strsplit(uuid, split = "")[[1]]
uuid <- paste(c(uuid[1:8], "-", uuid[9:12], "-", uuid[13:16], "-", uuid[17:20], "-", uuid[21:32]), collapse = "")
if (keep_source) attr(uuid, "source") <- source
uuid
} ## uuid()
uuid_of_connection <- function(con, ..., must_work = TRUE) {
stop_if_not(inherits(con, "connection"))
if (must_work) {
info <- summary(con)
info$opened <- NULL
uuid <- uuid(info, ...)
} else {
uuid <- tryCatch({
info <- summary(con)
info$opened <- NULL
uuid(info, ...)
}, error = function(ex) {
attr(con, "uuid", exact = TRUE)
})
}
uuid
} ## uuid_of_connection()
## A universally unique identifier (UUID) for the current
## R process UUID. Generated only once per process ID 'pid'.
## The 'pid' may differ when using forked processes.
session_uuid <- local({
uuids <- list()
function(pid = Sys.getpid(), attributes = TRUE) {
pidstr <- as.character(pid)
uuid <- uuids[[pidstr]]
if (!is.null(uuid)) {
if (!attributes) attr(uuid, "source") <- NULL
return(uuid)
}
info <- Sys.info()
host <- Sys.getenv(c("HOST", "HOSTNAME", "COMPUTERNAME"))
host <- host[nzchar(host)]
host <- if (length(host) == 0L) info[["nodename"]] else host[1L]
info <- list(
host = host,
info = info,
pid = pid,
time = Sys.time(),
## NOTE: This will set/update .GlobalEnv$.Random.seed
## FIXME: Should/can this be using stealth_sample() instead?
random = sample.int(.Machine$integer.max, size = 1L)
)
uuid <- uuid(info, keep_source = TRUE)
uuids[[pidstr]] <<- uuid
if (!attributes) attr(uuid, "source") <- NULL
uuid
}
})
|