Skip to content

Commit

Permalink
shorten tar_repository_cas_local() metadata string
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Aug 26, 2024
1 parent 0a6fa33 commit bdc8332
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ export(tar_callr_args_default)
export(tar_callr_inner_try)
export(tar_cancel)
export(tar_canceled)
export(tar_cas_d)
export(tar_cas_e)
export(tar_cas_u)
export(tar_completed)
export(tar_condition_traced)
export(tar_config_get)
Expand Down Expand Up @@ -488,9 +491,6 @@ export(tar_read_raw)
export(tar_renv)
export(tar_repository_cas)
export(tar_repository_cas_local)
export(tar_repository_cas_local_download)
export(tar_repository_cas_local_exists)
export(tar_repository_cas_local_upload)
export(tar_reprex)
export(tar_resources)
export(tar_resources_aws)
Expand Down
22 changes: 13 additions & 9 deletions R/tar_repository_cas_local.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ tar_repository_cas_local <- function(
exists <- function(key) {
}
body(upload) <- substitute(
targets::tar_repository_cas_local_upload(cas, key, path),
targets::tar_cas_u(cas, key, path),
env = data
)
body(download) <- substitute(
targets::tar_repository_cas_local_download(cas, key, path),
targets::tar_cas_d(cas, key, path),
env = data
)
body(exists) <- substitute(
targets::tar_repository_cas_local_exists(cas, key),
targets::tar_cas_e(cas, key),
env = data
)
tar_repository_cas(
Expand All @@ -80,11 +80,13 @@ tar_repository_cas_local <- function(
#' @export
#' @keywords internal
#' @description For internal use only.
#' @details The short function name helps reduce the size of the
#' [tar_repository_cas()] format string and save space in the metadata.
#' @return Called for its side effects.
#' @param cas File path to the CAS repository.
#' @param key Key of the object in the CAS system.
#' @param path Staging path of the file.
tar_repository_cas_local_upload <- function(cas, key, path) {
tar_cas_u <- function(cas, key, path) {
to <- file.path(cas, key)
if (!file.exists(to)) {
# Defined in R/utils_files.R. Works on both files and directories.
Expand All @@ -97,8 +99,8 @@ tar_repository_cas_local_upload <- function(cas, key, path) {
#' @keywords internal
#' @description For internal use only.
#' @return Called for its side effects.
#' @inheritParams tar_repository_cas_local_upload
tar_repository_cas_local_download <- function(cas, key, path) {
#' @inheritParams tar_cas_u
tar_cas_d <- function(cas, key, path) {
# Defined in R/utils_files.R. Works on both directories.
file_copy(file.path(cas, key), path)
}
Expand All @@ -107,13 +109,15 @@ tar_repository_cas_local_download <- function(cas, key, path) {
#' @export
#' @keywords internal
#' @description For internal use only.
#' @details [tar_repository_cas_local_exists()] uses an in-memory cache
#' @details The short function name helps reduce the size of the
#' [tar_repository_cas()] format string and save space in the metadata.
#' @details [tar_cas_e()] uses an in-memory cache
#' in a package internal environment to maintain a list of keys that
#' exists. This avoids expensive one-time lookups to the file system
#' during [tar_make()].
#' @return `TRUE` if the key exists in the CAS system, `FALSE` otherwise.
#' @inheritParams tar_repository_cas_local_upload
tar_repository_cas_local_exists <- function(cas, key) {
#' @inheritParams tar_cas_u
tar_cas_e <- function(cas, key) {
if (is.null(tar_repository_cas_local_cache[[cas]])) {
keys <- list.files(cas)
data <- rep(TRUE, length(keys))
Expand Down
6 changes: 3 additions & 3 deletions man/tar_repository_cas_local_download.Rd → man/tar_cas_d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions man/tar_repository_cas_local_exists.Rd → man/tar_cas_e.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions man/tar_repository_cas_local_upload.Rd → man/tar_cas_u.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions tests/testthat/test-tar_repository_cas_local.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
tar_test("tar_repository_cas_local_upload()", {
tar_test("tar_cas_u() (upload)", {
for (index in seq_len(2L)) {
file.create("x")
tar_repository_cas_local_upload("cas", "key", "x")
tar_cas_u("cas", "key", "x")
expect_true(file.exists(file.path("cas", "key")))
}
})

tar_test("tar_repository_cas_local_download()", {
tar_test("tar_cas_d() (download)", {
file.create("x")
tar_repository_cas_local_upload("cas", "key", "x")
tar_repository_cas_local_download("cas", "key", "file")
tar_cas_u("cas", "key", "x")
tar_cas_d("cas", "key", "file")
expect_true(file.exists("file"))
})

tar_test("tar_repository_cas_local_exists()", {
tar_test("tar_cas_e() (exists)", {
tar_repository_cas_local_cache[["cas"]] <- NULL
on.exit(tar_repository_cas_local_cache[["cas"]] <- NULL)
expect_false(tar_repository_cas_local_exists("cas", "key"))
expect_false(tar_cas_e("cas", "key"))
file.create("x")
tar_repository_cas_local_upload("cas", "key", "x")
expect_true(tar_repository_cas_local_exists("cas", "key"))
expect_true(tar_repository_cas_local_exists("cas", "key"))
expect_false(tar_repository_cas_local_exists("cas", "key2"))
tar_cas_u("cas", "key", "x")
expect_true(tar_cas_e("cas", "key"))
expect_true(tar_cas_e("cas", "key"))
expect_false(tar_cas_e("cas", "key2"))
file.create("x")
tar_repository_cas_local_upload("cas", "key2", "x")
expect_true(tar_repository_cas_local_exists("cas", "key2"))
tar_cas_u("cas", "key2", "x")
expect_true(tar_cas_e("cas", "key2"))
})

tar_test("local CAS repository works", {
Expand Down

0 comments on commit bdc8332

Please sign in to comment.