Debian Codegraph @

R/filebacked.R

raw
1
2 # tools for caching values read from a file, and invalidating those values if
3 # the file mtime changes. use `renv_filebacked_set()` to associate some value
4 # with a file at a particular point in time; `renv_filebacked_get()` will return
5 # that value, or NULL of the file mtime has changed
6 the$filebacked_cache <- new.env(parent = emptyenv())
7
8 renv_filebacked_clear <- function(context, path = NULL) {
9
10 # get cache associated with this context
11 envir <- renv_filebacked_envir(context)
12
13 # list all available cached results
14 existing <- ls(envir = envir, all.names = TRUE)
15
16 # if path is set, use it; otherwise remove everything
17 path <- path %||% existing
18
19 # validate the requested paths exist in the environment
20 removable <- renv_vector_intersect(path, existing)
21
22 # remove them
23 rm(list = removable, envir = envir)
24 }
25
26 renv_filebacked_set <- function(context, path, value) {
27
28 # validate the path
29 stopifnot(renv_path_absolute(path))
30
31 # create our cache entry
32 info <- renv_file_info(path)
33 entry <- list(value = value, info = info)
34
35 # store it
36 envir <- renv_filebacked_envir(context)
37 assign(path, entry, envir = envir)
38 invisible(value)
39
40 }
41
42 renv_filebacked_get <- function(context, path) {
43
44 # validate the path
45 if (!renv_path_absolute(path))
46 stopf("internal error: '%s' is not an absolute path", path)
47
48 # get contextd sub-environment
49 envir <- renv_filebacked_envir(context)
50
51 # check for entry in the cache
52 entry <- envir[[path]]
53 if (is.null(entry))
54 return(NULL)
55
56 # extract pieces of interest
57 value <- entry$value
58 oldinfo <- entry$info
59 newinfo <- renv_file_info(path)
60
61 # if the file didn't exist when we set the entry,
62 # check and see if it's still not there
63 if (is.na(oldinfo$isdir) && is.na(newinfo$isdir))
64 return(value)
65
66 # compare on fields of interest
67 fields <- c("size", "isdir", "mtime")
68 if (!identical(oldinfo[fields], newinfo[fields]))
69 return(NULL)
70
71 # looks good
72 value
73
74 }
75
76 renv_filebacked_envir <- function(context) {
77 the$filebacked_cache[[context]] <-
78 the$filebacked_cache[[context]] %||%
79 new.env(parent = emptyenv())
80 }
81
82 filebacked <- function(context, path, callback, ...) {
83
84 # don't use filebacked cache when disabled
85 config <- config$filebacked.cache()
86 if (identical(config, FALSE))
87 return(callback(path, ...))
88
89 # check for cache entry -- if available, use it
90 cache <- renv_filebacked_get(context, path)
91 if (!is.null(cache))
92 return(cache)
93
94 # otherwise, generate our value and cache it
95 result <- callback(path, ...)
96 renv_filebacked_set(context, path, result)
97
98 result
99
100 }
101
102 renv_filebacked_invalidate <- function(path) {
103 renv_scope_options(warn = -1L)
104 eapply(the$filebacked_cache, function(context) {
105 if (exists(path, envir = context))
106 rm(list = path, envir = context)
107 })
108 }