Skip to content

Commit

Permalink
Abstract metadata caching to helper functions and store cached metata…
Browse files Browse the repository at this point in the history
…data as list

Also cache media metadata. Relates to #57 and #58
  • Loading branch information
matildastevenson committed Jul 13, 2021
1 parent 2a96f60 commit bf449d1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
4 changes: 3 additions & 1 deletion R/ala_counts.R
Expand Up @@ -122,9 +122,11 @@ ala_counts <- function(taxa = NULL, filters = NULL, locations = NULL, group_by,
)
}
names(counts) <- c(group_by, "count")
attr(counts, "data_type") <- "counts"

if (caching) {
saveRDS(counts, cache_file)
write_cache_file(object = counts, data_type = "occurrences", query = list(),
cache_file = cache_file)
}

return(counts)
Expand Down
16 changes: 16 additions & 0 deletions R/ala_media.R
Expand Up @@ -55,6 +55,7 @@ ala_media <- function(taxa = NULL, filters = NULL, locations = NULL,
image_url <- server_config("images_base_url")

verbose <- getOption("galah_config")$verbose
caching <- getOption("galah_config")$caching
assert_that(!missing(download_dir),
msg = "A path to an existing directory to download images to is required")
assert_that(file.exists(download_dir))
Expand All @@ -64,6 +65,16 @@ ala_media <- function(taxa = NULL, filters = NULL, locations = NULL,
warning("No filters have been provided. All images and sounds will be downloaded.")
}

if (caching) {
cache_file <- cache_filename(
"media",
unlist(build_query(taxa, filters, locations, columns))
)
if (file.exists(cache_file)) {
return(read_cache_file(cache_file))
}
}

# Check whether any of the filters are media-specific filters and
# filter to records with image/sound/video
media_filters <- filters[filters$name %in% search_fields(type = "media")$id,]
Expand Down Expand Up @@ -133,6 +144,11 @@ ala_media <- function(taxa = NULL, filters = NULL, locations = NULL,
if (verbose) {
message("\n",nrow(all_data), " files were downloaded to ", download_dir)
}
attr(all_data, "data_type") <- "media"
if (caching) {
write_cache_file(object = all_data, data_type = "media", query = list(),
cache_file = cache_file)
}
return(all_data)
}

Expand Down
6 changes: 4 additions & 2 deletions R/ala_occurrences.R
Expand Up @@ -114,9 +114,11 @@ no valid column names have been provided. To check whether column names are vali
# add DOI as attribute
attr(df, "doi") <- get_doi(mint_doi, data_path)
attr(df, "search_url") <- download_resp$search_url

attr(df, "data_type") <- "occurrences"

if (caching) {
saveRDS(df, file = cache_file)
write_cache_file(object = df, data_type = "occurrences", query = list(),
cache_file = cache_file)
}
return(df)
}
Expand Down
6 changes: 4 additions & 2 deletions R/ala_species.R
Expand Up @@ -72,9 +72,11 @@ ala_species <- function(taxa = NULL, filters = NULL, locations = NULL) {
cache_file = tmp)
# overwrite file with fixed names
names(data) <- rename_columns(names(data), type = "checklist")

attr(data, "data_type") <- "species"

if (caching) {
saveRDS(data, cache_file)
write_cache_file(object = data, data_type = "species", query = list(),
cache_file = cache_file)
}
return(data)
}
12 changes: 10 additions & 2 deletions R/utilities_internal.R
Expand Up @@ -251,10 +251,17 @@ check_taxa_arg <- function(taxa) {

# Read cached file
read_cache_file <- function(filename) {
if (getOption("galah_config")$caching) { message("Using cached file") }
if (getOption("galah_config")$verbose) { message("Using cached file") }
readRDS(filename)
}

# Write file to cache and metadata to metadata cache
write_cache_file <- function(object, query, data_type, cache_file) {
if (getOption("galah_config")$verbose) { message("Writing to cache file") }
saveRDS(object, cache_file)
write_metadata(query, data_type, cache_file)
}

# Hash cache filename from argument list
cache_filename <- function(...) {
args <- c(...)
Expand All @@ -264,14 +271,15 @@ cache_filename <- function(...) {

# Write function call metadata to RDS file to enable metadata viewing with
# `find_cached_files()`
write_metadata <- function(cache_file, data_type, query) {
write_metadata <- function(query, data_type, cache_file) {
metadata_file <- file.path(getOption("galah_config")$cache_directory,
"metadata.rds")
if (file.exists(metadata_file)) {
metadata <- readRDS(metadata_file)
} else {
metadata <- list()
}
file_id <- str_split(cache_file, "\\.")[[1]][1]
metadata$`file_id` <- list(data_type = data_type, query = query)
saveRDS(metadata, metadata_file)
}
Expand Down

0 comments on commit bf449d1

Please sign in to comment.