Skip to content

Commit ec533a4

Browse files
committed
tar_repository_cas_local_gc()
1 parent bdc8332 commit ec533a4

24 files changed

+287
-58
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ export(tar_read_raw)
491491
export(tar_renv)
492492
export(tar_repository_cas)
493493
export(tar_repository_cas_local)
494+
export(tar_repository_cas_local_gc)
494495
export(tar_reprex)
495496
export(tar_resources)
496497
export(tar_resources_aws)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Avoid saving a file in `_targets/objects` for `error = "null"`. Instead, switch to a special `"null"` storage format class if `error` is `"null"` the target throws an error. This should allow users to more freely create new formats with `tar_format()` without worrying about how to handle `NULL` objects created by `error = "null"`.
1111
* Implement `format = "auto"` (#1311, @hadley).
1212
* Replace `pingr` dependency with `base::socketConnection()` for local URL utilities (#1317, #1318, @Adafede).
13-
* Implement `tar_repository_cas()` and `tar_repository_cas_local()` for content-addressable storage (#1232, #1314, @noamross).
13+
* Implement `tar_repository_cas()`, `tar_repository_cas_local()`, and `tar_repository_cas_local_gc()` for content-addressable storage (#1232, #1314, @noamross).
1414

1515
# targets 1.7.1
1616

R/class_options.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ options_class <- R6::R6Class(
303303
self$repository %|||% "local"
304304
},
305305
get_repository_meta = function() {
306-
(self$repository_meta %|||% self$repository) %|||% "local"
306+
(self$repository_meta %|||%
307+
if_any(is_repository_cas(self$repository), "local", self$repository)
308+
) %|||%
309+
"local"
307310
},
308311
get_iteration = function() {
309312
self$iteration %|||% "vector"
@@ -501,7 +504,11 @@ options_class <- R6::R6Class(
501504
tar_assert_repository(repository)
502505
},
503506
validate_repository_meta = function(repository_meta) {
504-
tar_assert_repository(repository_meta)
507+
tar_assert_in(
508+
repository_meta,
509+
choices = c("local", "aws", "gcp"),
510+
msg = "repository_meta must be one of \"local\", \"aws\", or \"gcp\"."
511+
)
505512
},
506513
validate_iteration = function(iteration) {
507514
tar_assert_flag(iteration, c("vector", "list", "group"))

R/tar_option_set.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
#' @inheritSection tar_target Storage formats
1313
#' @inheritParams tar_target
1414
#' @param repository_meta Character of length 1 with the same values as
15-
#' `repository` (`"aws"`, `"gcp"`, `"local"`). Cloud repository
15+
#' `repository` but excluding content-addressable storage
16+
#' (`"aws"`, `"gcp"`, `"local"`). Cloud repository
1617
#' for the metadata text files in `_targets/meta/`, including target
17-
#' metadata and progress data. Defaults to `tar_option_get("repository")`.
18+
#' metadata and progress data. Defaults to `tar_option_get("repository")`
19+
#' except in the case of content-addressable storage (CAS).
20+
#' When `tar_option_get("repository")` is a CAS repository,
21+
#' the default value of `repository_meta` is `"local"`.
1822
#' @param imports Character vector of package names.
1923
#' For every package listed, `targets` tracks every
2024
#' dataset and every object in the package namespace

R/tar_repository_cas.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#' @title Define a custom content-addressable storage
22
#' (CAS) repository (an experimental feature).
33
#' @export
4-
#' @family storage
4+
#' @family content-addressable storage
55
#' @description Define a custom storage repository that uses
66
#' content-addressable storage (CAS).
7-
#' @details Without content-addressable storage (CAS),
7+
#' @section Content-addressable storage:
8+
#' Without content-addressable storage (CAS),
89
#' the output data of a pipeline is organized based
910
#' on the names of the targets. For example,
1011
#' if your pipeline has a target `x`,
@@ -178,7 +179,7 @@
178179
#' tar_target(z, write_file(y), format = "file", repository = repository)
179180
#' )
180181
#' })
181-
#' tar_make(callr_function = NULL)
182+
#' tar_make()
182183
#' tar_read(y)
183184
#' tar_read(z)
184185
#' list.files("cas")

R/tar_repository_cas_local.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#' @title Local content-addressable storage (CAS) repository
22
#' (an experimental feature).
33
#' @export
4-
#' @family storage
4+
#' @family content-addressable storage
55
#' @description Local content-addressable storage (CAS) repository.
66
#' @details Pass to the `repository` argument of [tar_target()] or
77
#' [tar_option_set()] to use a local CAS system.
8+
#' @inheritSection tar_repository_cas Content-addressable storage
89
#' @return A character string from [tar_repository_cas()] which may be
910
#' passed to the `repository` argument of [tar_target()] or
1011
#' [tar_option_set()] to use a local CAS system.
@@ -32,7 +33,7 @@
3233
#' tar_target(z, write_file(y), format = "file", repository = repository)
3334
#' )
3435
#' })
35-
#' tar_make(callr_function = NULL)
36+
#' tar_make()
3637
#' tar_read(y)
3738
#' tar_read(z)
3839
#' list.files("cas")

R/tar_repository_cas_local_gc.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#' @title Local CAS garbage collection
2+
#' @export
3+
#' @family content-addressable storage
4+
#' @description Garbage collection for a local content-addressable
5+
#' storage system.
6+
#' @details Deletes all the files in the local CAS which are not in
7+
#' `tar_meta(targets_only = TRUE)$data`, including all locally saved
8+
#' historical data of the pipeline. This clears disk space, but
9+
#' at the expense of removing historical data and data from
10+
#' other colleagues who worked on the same project.
11+
#' @inheritSection tar_repository_cas Content-addressable storage
12+
#' @return `NULL` (invisibly). Called for its side effects.
13+
#' Removes files from the CAS repository at `path`.
14+
#' @inheritParams tar_meta
15+
#' @inheritParams tar_repository_cas_local
16+
#' @examples
17+
#' if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) { # for CRAN
18+
#' tar_dir({ # tar_dir() runs code from a temp dir for CRAN.
19+
#' tar_script({
20+
#' tar_option_set(seed = NA, repository = tar_repository_cas_local())
21+
#' list(tar_target(x, sample.int(n = 9e9, size = 1)))
22+
#' })
23+
#' for (index in seq_len(3)) tar_make(reporter = "silent")
24+
#' list.files("_targets/cas")
25+
#' tar_repository_cas_local_gc()
26+
#' list.files("_targets/cas")
27+
#' tar_meta(names = any_of("x"), fields = any_of("data"))
28+
#' })
29+
#' }
30+
tar_repository_cas_local_gc <- function(
31+
path = file.path(targets::tar_config_get("store"), "cas"),
32+
store = targets::tar_config_get("store")
33+
) {
34+
meta <- targets::tar_meta(
35+
fields = tidyselect::any_of("data"),
36+
targets_only = TRUE,
37+
store = store
38+
)
39+
keys <- list.files(path)
40+
remove <- setdiff(keys, meta$data)
41+
unlink(file.path(path, remove), force = TRUE, recursive = TRUE)
42+
invisible()
43+
}

R/tar_target.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
#' See the cloud storage section of
145145
#' <https://books.ropensci.org/targets/data.html>
146146
#' for details for instructions.
147+
#' * A character string from [tar_repository_cas()] for content-addressable
148+
#' storage.
147149
#'
148150
#' Note: if `repository` is not `"local"` and `format` is `"file"`
149151
#' then the target should create a single output file.

_pkgdown.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ reference:
6262
- 'tar_load_raw'
6363
- 'tar_load_everything'
6464
- 'tar_objects'
65-
- 'tar_repository_cas'
66-
- 'tar_repository_cas_local'
6765
- 'tar_read'
6866
- 'tar_read_raw'
67+
- title: Content-addressable storage
68+
contents:
69+
- 'tar_repository_cas'
70+
- 'tar_repository_cas_local'
71+
- 'tar_repository_cas_local_gc'
6972
- title: Metadata
7073
contents:
7174
- 'tar_crew'

man/tar_format.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_load.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_load_everything.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_load_raw.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_objects.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_option_set.Rd

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_read.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_read_raw.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_repository_cas.Rd

Lines changed: 8 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tar_repository_cas_local.Rd

Lines changed: 52 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)