Skip to content

Commit

Permalink
drop {uuid} dependency (fixes #238) (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Feb 1, 2025
1 parent e4eb4ac commit 68b3ca6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .ci/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/
3 changes: 1 addition & 2 deletions r-pkg/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ Imports:
jsonlite,
purrr,
stringr,
utils,
uuid
utils
Suggests:
knitr,
markdown,
Expand Down
1 change: 0 additions & 1 deletion r-pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 18 additions & 5 deletions r-pkg/R/es_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions r-pkg/R/helperfuns.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion r-pkg/R/unpack_nested_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 68b3ca6

Please sign in to comment.