Skip to content

Commit

Permalink
more robust cache of R5 jar
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Jan 13, 2024
1 parent 132a50b commit a21ca99
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 36 deletions.
4 changes: 2 additions & 2 deletions r-package/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: r5r
Title: Rapid Realistic Routing with 'R5'
Version: 1.1.0999
Version: 1.1.0.9000
Authors@R: c(
person("Marcus", "Saraiva", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0001-6218-2338")),
Expand Down Expand Up @@ -34,7 +34,7 @@ Description: Rapid realistic routing on multimodal transport networks
<https://docs.conveyal.com/changelog>) corresponds with the R5 version
that r5r depends on.
License: MIT + file LICENSE
URL: https://github.com/ipeaGIT/r5r, https://ipeagit.github.io/r5r/
URL: https://github.com/ipeaGIT/r5r
BugReports: https://github.com/ipeaGIT/r5r/issues
Depends:
R (>= 3.6)
Expand Down
1 change: 1 addition & 0 deletions r-package/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(expanded_travel_time_matrix)
export(find_snap)
export(isochrone)
export(pareto_frontier)
export(r5r_cache)
export(r5r_sitrep)
export(read_fare_structure)
export(setup_fare_structure)
Expand Down
2 changes: 1 addition & 1 deletion r-package/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

**Major changes**

- r5r now stores R5 Jar file at `tools::R_user_dir("r5r", which = "cache")`
- r5r now stores R5 Jar file at user dir using `tools::R_user_dir()`
- By using the JDK 21, this version of r5r also fixed an incompatibility with MAC ARM processors. Closed [#315](https://github.com/ipeaGIT/r5r/issues/315)

**Minor changes**
Expand Down
6 changes: 4 additions & 2 deletions r-package/R/download_r5.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ download_r5 <- function(version = "7.0.0",
options(timeout = max(600, getOption("timeout")))


# download R5's jar -----------------------------------------------------
# download R5 jar -----------------------------------------------------

if (!dir.exists(r5r_env$cache_dir)) dir.create(r5r_env$cache_dir, recursive = TRUE)

file_url <- fileurl_from_metadata(version)
filename <- basename(file_url)

jar_file <- data.table::fifelse(
temp_dir,
file.path(tempdir(), filename),
file.path(tools::R_user_dir("r5r", which = "cache"), filename)
file.path( r5r_env$cache_dir , filename)
)


Expand Down
32 changes: 32 additions & 0 deletions r-package/R/onLoad.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# initial message about ram memory
.onAttach <- function(lib, pkg) {
packageStartupMessage(
"Please make sure you have already allocated ",
"some memory to Java by running:\n",
" options(java.parameters = '-Xmx2G').\n",
"You should replace '2G' by the amount of memory you'll require. ",
"Currently, Java memory is set to ", getOption("java.parameters")
)
}

# package global variables
r5r_env <- new.env(parent = emptyenv()) # nocov start

.onLoad <- function(lib, pkg) {

# JAR version
r5r_env$r5_jar_version <- "7.0.0"

# create dir to store R5 Jar
cache_d <- paste0('r5r/r5_jar_v', r5r_env$r5_jar_version)
r5r_env$cache_dir <- tools::R_user_dir(cache_d, which = 'cache')
if (!dir.exists(r5r_env$cache_dir)) dir.create(r5r_env$cache_dir, recursive = TRUE)
# gsub("\\\\", "/", r5r_env$cache_dir)

## delete any JAR files from old releases
dir_above <- dirname(r5r_env$cache_dir)
all_cache <- list.files(dir_above, pattern = 'r5',full.names = TRUE)
old_cache <- all_cache[!grepl(r5r_env$r5_jar_version, all_cache)]
if(length(old_cache)>0){ unlink(old_cache, recursive = TRUE) }

}
15 changes: 0 additions & 15 deletions r-package/R/r5r.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,3 @@ if (getRversion() >= "2.15.1") {
)
)
}

.onAttach <- function(lib, pkg) {
packageStartupMessage(
"Please make sure you have already allocated ",
"some memory to Java by running:\n",
" options(java.parameters = '-Xmx2G').\n",
"You should replace '2G' by the amount of memory you'll require. ",
"Currently, Java memory is set to ", getOption("java.parameters")
)

# create dir to store R5 Jar
jar_dir <- tools::R_user_dir("r5r", which = "cache")
if (!dir.exists(jar_dir)) dir.create(jar_dir, recursive = TRUE)

}
70 changes: 70 additions & 0 deletions r-package/R/r5r_cache.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' Manage cached files from the r5r package
#'
#' @param list_files Logical. Whether to print a message with the address of r5r
#' JAR files cached locally. Defaults to `TRUE`.
#' @param delete_file String. The file name (basename) of a JAR file cached
#' locally that should be deleted. Defaults to `NULL`, so that no
#' file is deleted. If `delete_file = "all"`, then all cached files are
#' deleted.
#'
#' @return A message indicating which file exist and/or which ones have been
#' deleted from local cache directory.
#' @export
#' @family Cache data
#' @examplesIf identical(tolower(Sys.getenv("NOT_CRAN")), "true")
#' # list all files cached
#' r5r_cache(list_files = TRUE)
#'
#' # delete particular file
#' r5r_cache(delete_file = '2010_deaths')
#'
r5r_cache <- function(list_files = TRUE,
delete_file = NULL){

# check inputs
checkmate::assert_logical(list_files)
checkmate::assert_character(delete_file, null.ok = TRUE)

# find / create local dir
if (!dir.exists(r5r_env$cache_dir)) { dir.create(r5r_env$cache_dir, recursive=TRUE) }

# list cached files
files <- list.files(dirname(r5r_env$cache_dir), full.names = TRUE)

# if wants to delete file
# delete_file = "2_families.parquet"
if (!is.null(delete_file)) {

# IF file does not exist, print message
if (!any(grepl(delete_file, files)) & delete_file != "all") {
message(paste0("The file '", delete_file, "' is not cached."))
}

# IF file exists, delete file
if (any(grepl(delete_file, files))) {
f <- files[grepl(delete_file, files)]
unlink(f, recursive = TRUE)
message(paste0("The file '", delete_file, "' has been removed."))
}

# Delete ALL file
if (delete_file=='all') {

# delete any files from censobr, current and old data releases
dir_above <- dirname(r5r_env$cache_dir)
unlink(dir_above, recursive = TRUE)
message(paste0("All files have been removed."))

}
}

# list cached files
files <- list.files(r5r_env$cache_dir, full.names = TRUE)

# print file names
if(isTRUE(list_files)){
message('Files currently chached:')
message(paste0(files, collapse = '\n'))
}
}

6 changes: 3 additions & 3 deletions r-package/R/r5r_sitrep.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
r5r_sitrep <- function() {
r5r_package_version <- utils::packageVersion("r5r")

jar_dir <- tools::R_user_dir("r5r", which = "cache")
jar_dir <- r5r_env$cache_dir
jar_dir_files <- list.files(jar_dir)
jar_dir_files_full_names <- list.files(jar_dir, full.names = TRUE)

r5r_jar <- jar_dir_files[grepl("r5r_\\d_\\d_\\d.*\\.jar", jar_dir_files)]
r5r_jar_version <- sub("\\.jar", "", sub("r5r_", "", r5r_jar))
r5r_jar_version <- gsub("_", "\\.", r5r_jar_version)

r5r_jar_version <- r5r_env$r5_jar_version
r5r_jar_path <- jar_dir_files_full_names[
grepl("\\/r5r_\\d_\\d_\\d*\\.jar", jar_dir_files_full_names)
]
Expand Down
2 changes: 1 addition & 1 deletion r-package/R/setup_r5.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ setup_r5 <- function(data_path,
jar_file <- data.table::fifelse(
temp_dir,
file.path(tempdir(), filename),
file.path(tools::R_user_dir("r5r", which = "cache"), filename)
file.path( r5r_env$cache_dir, filename)
)

# If there isn't a JAR already, download it
Expand Down
1 change: 0 additions & 1 deletion r-package/man/r5r.Rd

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

34 changes: 34 additions & 0 deletions r-package/man/r5r_cache.Rd

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

12 changes: 12 additions & 0 deletions r-package/tests/tests_rafa/test_rafa.R
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,9 @@ lapply(X=docs, FUN = tools::showNonASCIIfile)
library(tictoc)
library(beepr)

# run only the tests
testthat::test_local()


# LOCAL
utils::remove.packages('r5r')
Expand All @@ -780,6 +783,7 @@ tictoc::tic()
Sys.setenv(NOT_CRAN = "false")
devtools::check(pkg = ".", cran = TRUE, env_vars = c(NOT_CRAN = "false"))
tictoc::toc()
beepr::beep()


devtools::check_win_release(pkg = ".")
Expand All @@ -797,6 +801,14 @@ devtools::check(pkg = ".", cran = TRUE, env_vars = c(NOT_CRAN = "false"))
tictoc::toc()


# extrachecks -----------------
#' https://github.com/JosiahParry/extrachecks
#' remotes::install_github("JosiahParry/extrachecks")

library(extrachecks)
extrachecks::extrachecks()



# submit to CRAN -----------------
usethis::use_cran_comments('teste 2222, , asdadsad')
Expand Down
4 changes: 1 addition & 3 deletions r-package/tests/testthat/test-write_fare_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ test_that("written structure is identical to original", {
})

# clean cache
jar_dir <- tools::R_user_dir("r5r", which = "cache")
f <- list.files(jar_dir, full.names = TRUE)
unlink(f, recursive = TRUE)
r5r::r5r_cache(delete_file = 'all')
5 changes: 2 additions & 3 deletions r-package/vignettes/r5r.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,8 @@ rJava::.jgc(R.gc = TRUE)

```{r, eval = TRUE, include = FALSE, message = FALSE}
# clean cache (CRAN policy)
jar_dir <- tools::R_user_dir("r5r", which = "cache")
f <- list.files(jar_dir, full.names = TRUE)
unlink(f, recursive = TRUE)
r5r::r5r_cache(delete_file = 'all')

```

If you have any suggestions or want to report an error, please visit [the package GitHub page](https://github.com/ipeaGIT/r5r).
Expand Down
2 changes: 0 additions & 2 deletions r-package/vignettes/time_window.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ The `detailed_itineraries()`, on the other hand, does not return travel times or





### Cleaning up after usage

`r5r` objects are still allocated to any amount of memory previously set after they are done with their calculations. In order to remove an existing `r5r` object and reallocate the memory it had been using, we use the `stop_r5` function followed by a call to Java's garbage collector, as follows:
Expand Down
4 changes: 1 addition & 3 deletions r-package/vignettes/travel_time_matrix.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ rJava::.jgc(R.gc = TRUE)

```{r, eval = TRUE, include = FALSE, message = FALSE}
# clean cache (CRAN policy)
jar_dir <- tools::R_user_dir("r5r", which = "cache")
f <- list.files(jar_dir, full.names = TRUE)
unlink(f, recursive = TRUE)
r5r::r5r_cache(delete_file = 'all')

```

Expand Down

0 comments on commit a21ca99

Please sign in to comment.