From dffedf637b0c627e7397cfab3abff564ce79622b Mon Sep 17 00:00:00 2001 From: IlyaZar Date: Tue, 28 Mar 2023 13:51:20 +0200 Subject: [PATCH] Add baseline storage class - as a port of odds - with baseline functions from qs To-Do: - possibly refactor baseline class that currently implements disk storage (qs) - add memory storage (cachem) - add remote (memoise + bank) --- DESCRIPTION | 5 ++- R/storage.R | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 R/storage.R diff --git a/DESCRIPTION b/DESCRIPTION index 5726ac9..18cb2c9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,10 @@ LazyData: true RoxygenNote: 7.2.3 Imports: shiny, - attempt + attempt, + R6, + qs, + fs Suggests: knitr, rmarkdown, diff --git a/R/storage.R b/R/storage.R new file mode 100644 index 0000000..cb1af7c --- /dev/null +++ b/R/storage.R @@ -0,0 +1,95 @@ +#' Storage object +#' +#' @importFrom R6 R6Class +#' @importFrom qs qsave qread +#' @importFrom fs path_norm dir_create path dir_delete file_delete +#' @export +Storage <- R6::R6Class( + "Storage", + public = list( + path = character(0), + initialize = function( + path = "~/.odds" + ){ + self$path <- path_norm(path) + dir_create( + self$path + ) + }, + set = function( + value, + object, + namespace = "global", + preset = "high", + algorithm = "zstd", + compress_level = 4L, + shuffle_control = 15L, + check_hash = TRUE, + nthreads = 1 + ){ + dir_create( + path( + self$path, + namespace + ) + ) + qsave( + x = value, + file = path( + self$path, + namespace, + object, + ext = "qs" + ), + preset = preset, + algorithm = algorithm, + compress_level = compress_level, + shuffle_control = shuffle_control, + check_hash = check_hash, + nthreads = nthreads + ) + }, + get = function( + value, + namespace = "global", + use_alt_rep = FALSE, + strict = FALSE, + nthreads = 1 + ){ + qread( + file = path( + self$path, + namespace, + value, + ext = "qs" + ), + use_alt_rep = use_alt_rep, + strict = strict, + nthreads = nthreads + ) + }, + rm = function( + value, + namespace = "global" + ){ + file_delete( + path( + self$path, + namespace, + value, + ext = "qs" + ) + ) + }, + remove_namespace = function( + namespace + ){ + dir_delete( + path( + self$path, + namespace + ) + ) + } + ) +)