From 68b3ca61f4638602b846bd336a5be5a10bd0d3ae Mon Sep 17 00:00:00 2001 From: James Lamb Date: Fri, 31 Jan 2025 22:11:17 -0600 Subject: [PATCH] drop {uuid} dependency (fixes #238) (#240) --- .ci/setup.sh | 2 +- r-pkg/DESCRIPTION | 3 +-- r-pkg/NAMESPACE | 1 - r-pkg/R/es_search.R | 23 ++++++++++++++++++----- r-pkg/R/helperfuns.R | 14 ++++++++++++++ r-pkg/R/unpack_nested_data.R | 7 ++++++- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/.ci/setup.sh b/.ci/setup.sh index 11eb42b..79c8305 100755 --- a/.ci/setup.sh +++ b/.ci/setup.sh @@ -19,5 +19,5 @@ sudo apt-get install \ tidy \ qpdf -Rscript -e "install.packages(c('assertthat', 'covr', 'data.table', 'futile.logger', 'httr', 'jsonlite', 'knitr', 'lintr', 'markdown', 'purrr', 'stringr', 'testthat', 'uuid'), repos = 'https://cran.r-project.org', Ncpus = parallel::detectCores())" +Rscript -e "install.packages(c('assertthat', 'covr', 'data.table', 'futile.logger', 'httr', 'jsonlite', 'knitr', 'lintr', 'markdown', 'purrr', 'stringr', 'testthat'), repos = 'https://cran.r-project.org', Ncpus = parallel::detectCores())" cp test-data/* r-pkg/inst/testdata/ diff --git a/r-pkg/DESCRIPTION b/r-pkg/DESCRIPTION index a959520..2d6e00c 100644 --- a/r-pkg/DESCRIPTION +++ b/r-pkg/DESCRIPTION @@ -29,8 +29,7 @@ Imports: jsonlite, purrr, stringr, - utils, - uuid + utils Suggests: knitr, markdown, diff --git a/r-pkg/NAMESPACE b/r-pkg/NAMESPACE index ab2db40..55403a9 100644 --- a/r-pkg/NAMESPACE +++ b/r-pkg/NAMESPACE @@ -45,4 +45,3 @@ importFrom(stringr,str_replace_all) importFrom(stringr,str_split) importFrom(stringr,str_split_fixed) importFrom(utils,read.table) -importFrom(uuid,UUIDgenerate) diff --git a/r-pkg/R/es_search.R b/r-pkg/R/es_search.R index 6113962..c441fdc 100644 --- a/r-pkg/R/es_search.R +++ b/r-pkg/R/es_search.R @@ -224,7 +224,6 @@ es_search <- function(es_host #' @importFrom data.table rbindlist setkeyv #' @importFrom jsonlite fromJSON #' @importFrom parallel clusterMap makeForkCluster makePSOCKcluster stopCluster -#' @importFrom uuid UUIDgenerate # nolint end .fetch_all <- function(es_host , es_index @@ -277,7 +276,8 @@ es_search <- function(es_host # Find a safe path to write to and create it repeat { - out_path <- file.path(intermediates_dir, uuid::UUIDgenerate()) + random_dirname <- sprintf("tmp-%s", .random_string(36L)) + out_path <- file.path(intermediates_dir, random_dirname) if (!dir.exists(out_path)) { break } @@ -329,7 +329,14 @@ es_search <- function(es_host scroll_id <- enc2utf8(firstResult[["_scroll_id"]]) # Write to disk - write(x = firstResultJSON, file = file.path(out_path, paste0(uuid::UUIDgenerate(), ".json"))) + write( + x = firstResultJSON + , file = tempfile( + pattern = "es-" + , tmpdir = out_path + , fileext = ".json" + ) + ) # Clean up memory rm("firstResult", "firstResultJSON") @@ -464,7 +471,6 @@ es_search <- function(es_host # max_hits. #' @importFrom httr add_headers #' @importFrom jsonlite fromJSON -#' @importFrom uuid UUIDgenerate .keep_on_pullin <- function(scroll_id , out_path , max_hits @@ -511,7 +517,14 @@ es_search <- function(es_host scroll_id <- resultList[["_scroll_id"]] # Write out JSON to a temporary file - write(x = resultJSON, file = file.path(out_path, paste0(uuid::UUIDgenerate(), ".json"))) + write( + x = resultJSON + , file = tempfile( + pattern = "es-" + , tmpdir = out_path + , fileext = ".json" + ) + ) # Increment the count hits_pulled <- hits_pulled + hitsInThisPage diff --git a/r-pkg/R/helperfuns.R b/r-pkg/R/helperfuns.R index c0af015..1a04317 100644 --- a/r-pkg/R/helperfuns.R +++ b/r-pkg/R/helperfuns.R @@ -7,6 +7,20 @@ return(httr::content(response, as = as)) } +# [title] Get a random length-n string +# [name] .random_string +# [description] Get a random length-n string of lowercase letters. +# Note that this uses sample() and so might produce deterministic +# results in programs where set.seed() is used to control randomness. +.random_string <- function(num_characters) { + return( + paste0( + sample(letters, replace = TRUE, size = num_characters) + , collapse = "" + ) + ) +} + # [title] Execute an HTTP request and return the result # [name] .request # [description] Mainly here to making mocking easier in testing, but this diff --git a/r-pkg/R/unpack_nested_data.R b/r-pkg/R/unpack_nested_data.R index 5abc109..52d02a0 100644 --- a/r-pkg/R/unpack_nested_data.R +++ b/r-pkg/R/unpack_nested_data.R @@ -56,7 +56,12 @@ unpack_nested_data <- function(chomped_df, col_to_unpack) { inDT <- data.table::copy(chomped_df) # Define a column name to store original row ID - joinCol <- uuid::UUIDgenerate() + repeat { + joinCol <- .random_string(36L) + if (!(joinCol %in% names(inDT))) { + break + } + } inDT[, (joinCol) := .I] # Take out the packed column