| 1 |
|
| 2 |
the$index <- new.env(parent = emptyenv()) |
| 3 |
|
| 4 |
index <- function(scope, key = NULL, value = NULL, limit = 3600L) { |
| 5 |
|
| 6 |
enabled <- renv_index_enabled(scope, key) |
| 7 |
if (!enabled) |
| 8 |
return(value) |
| 9 |
|
| 10 |
# resolve the root directory |
| 11 |
root <- renv_paths_index(scope) |
| 12 |
|
| 13 |
# make sure the directory we're indexing exists |
| 14 |
memoize( |
| 15 |
key = root, |
| 16 |
value = ensure_directory(root, umask = "0") |
| 17 |
) |
| 18 |
|
| 19 |
# make sure the directory is readable / writable |
| 20 |
# otherwise, attempts to lock will fail |
| 21 |
# https://github.com/rstudio/renv/issues/1171 |
| 22 |
if (!renv_index_writable(root)) |
| 23 |
return(value) |
| 24 |
|
| 25 |
# resolve other variables |
| 26 |
key <- if (!is.null(key)) renv_index_encode(key) |
| 27 |
now <- as.integer(Sys.time()) |
| 28 |
|
| 29 |
# acquire index lock |
| 30 |
lockfile <- file.path(root, "index.lock") |
| 31 |
renv_scope_lock(lockfile) |
| 32 |
|
| 33 |
# load the index file |
| 34 |
index <- tryCatch(renv_index_load(root, scope), error = identity) |
| 35 |
if (inherits(index, "error")) |
| 36 |
return(value) |
| 37 |
|
| 38 |
# return index as-is when key is NULL |
| 39 |
if (is.null(key)) |
| 40 |
return(index) |
| 41 |
|
| 42 |
# check for an index entry, and return it if it exists |
| 43 |
item <- renv_index_get(root, scope, index, key, now, limit) |
| 44 |
if (!is.null(item)) |
| 45 |
return(item) |
| 46 |
|
| 47 |
# otherwise, update the index |
| 48 |
renv_index_set(root, scope, index, key, value, now, limit) |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
renv_index_load <- function(root, scope) { |
| 53 |
|
| 54 |
filebacked( |
| 55 |
context = "renv_index_load", |
| 56 |
path = file.path(root, "index.json"), |
| 57 |
callback = renv_index_load_impl |
| 58 |
) |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
renv_index_load_impl <- function(path) { |
| 63 |
|
| 64 |
json <- tryCatch( |
| 65 |
withCallingHandlers( |
| 66 |
renv_json_read(path), |
| 67 |
warning = function(w) invokeRestart("muffleWarning") |
| 68 |
), |
| 69 |
error = identity |
| 70 |
) |
| 71 |
|
| 72 |
if (inherits(json, "error")) { |
| 73 |
unlink(path) |
| 74 |
return(list()) |
| 75 |
} |
| 76 |
|
| 77 |
json |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
renv_index_get <- function(root, scope, index, key, now, limit) { |
| 82 |
|
| 83 |
# check for index entry |
| 84 |
entry <- index[[key]] |
| 85 |
if (is.null(entry)) |
| 86 |
return(NULL) |
| 87 |
|
| 88 |
# see if it's expired |
| 89 |
if (renv_index_expired(entry, now, limit)) |
| 90 |
return(NULL) |
| 91 |
|
| 92 |
# check for in-memory cached value |
| 93 |
value <- the$index[[scope]][[key]] |
| 94 |
if (!is.null(value)) |
| 95 |
return(value) |
| 96 |
|
| 97 |
# otherwise, try to read from disk |
| 98 |
data <- file.path(root, entry$data) |
| 99 |
if (!file.exists(data)) |
| 100 |
return(NULL) |
| 101 |
|
| 102 |
# read data from disk |
| 103 |
value <- readRDS(data) |
| 104 |
|
| 105 |
# add to in-memory cache |
| 106 |
the$index[[scope]] <- |
| 107 |
the$index[[scope]] %||% |
| 108 |
new.env(parent = emptyenv()) |
| 109 |
|
| 110 |
the$index[[scope]][[key]] <- value |
| 111 |
|
| 112 |
# return value |
| 113 |
value |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
renv_index_set <- function(root, scope, index, key, value, now, limit) { |
| 118 |
|
| 119 |
# force promises |
| 120 |
force(value) |
| 121 |
|
| 122 |
# files being written here should be shared |
| 123 |
renv_scope_umask("0") |
| 124 |
|
| 125 |
# write data into index |
| 126 |
data <- tempfile("data-", tmpdir = root, fileext = ".rds") |
| 127 |
ensure_parent_directory(data) |
| 128 |
saveRDS(value, file = data, version = 2L) |
| 129 |
|
| 130 |
# clean up stale entries |
| 131 |
index <- renv_index_clean(root, scope, index, now, limit) |
| 132 |
|
| 133 |
# add index entry |
| 134 |
index[[key]] <- list(time = now, data = basename(data)) |
| 135 |
|
| 136 |
# update index file |
| 137 |
path <- file.path(root, "index.json") |
| 138 |
ensure_parent_directory(path) |
| 139 |
|
| 140 |
# write to tempfile and then copy to minimize risk of collisions |
| 141 |
tempfile <- tempfile(".index-", tmpdir = dirname(path), fileext = ".json") |
| 142 |
renv_json_write(index, file = tempfile) |
| 143 |
file.rename(tempfile, path) |
| 144 |
|
| 145 |
# return value |
| 146 |
value |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
renv_index_encode <- function(key) { |
| 151 |
key <- stringify(key) |
| 152 |
memoize(key, renv_hash_text(key)) |
| 153 |
} |
| 154 |
|
| 155 |
renv_index_clean <- function(root, scope, index, now, limit) { |
| 156 |
|
| 157 |
# figure out what cache entries have expired |
| 158 |
ok <- enum_lgl( |
| 159 |
index, |
| 160 |
renv_index_clean_impl, |
| 161 |
root = root, |
| 162 |
scope = scope, |
| 163 |
index = index, |
| 164 |
now = now, |
| 165 |
limit = limit |
| 166 |
) |
| 167 |
|
| 168 |
# return the existing cache entries |
| 169 |
index[ok] |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
renv_index_clean_impl <- function(key, entry, root, scope, index, now, limit) { |
| 174 |
|
| 175 |
# check if cache entry has expired |
| 176 |
expired <- renv_index_expired(entry, now, limit) |
| 177 |
if (!expired) |
| 178 |
return(TRUE) |
| 179 |
|
| 180 |
# remove from in-memory cache |
| 181 |
cache <- the$index[[scope]] |
| 182 |
cache[[key]] <- NULL |
| 183 |
|
| 184 |
# remove from disk |
| 185 |
unlink(file.path(root, entry$data), force = TRUE) |
| 186 |
|
| 187 |
FALSE |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
renv_index_expired <- function(entry, now, limit) { |
| 192 |
now - entry$time >= limit |
| 193 |
} |
| 194 |
|
| 195 |
renv_index_enabled <- function(scope, key) { |
| 196 |
getOption("renv.index.enabled", default = TRUE) |
| 197 |
} |
| 198 |
|
| 199 |
renv_index_writable <- function(root) { |
| 200 |
memoize( |
| 201 |
key = root, |
| 202 |
value = unname(file.access(root, 7L) == 0L) |
| 203 |
) |
| 204 |
} |
| 205 |
|
| 206 |
# in case of emergency, break glass |
| 207 |
renv_index_reset <- function(root = NULL) { |
| 208 |
root <- root %||% renv_paths_index() |
| 209 |
lockfiles <- list.files(root, pattern = "^index\\.lock$", full.names = TRUE) |
| 210 |
unlink(lockfiles) |
| 211 |
} |
| 212 |
|