Skip to content

Commit

Permalink
renamed $push_backup() and $backup_dir of BackupQueue to just `$p…
Browse files Browse the repository at this point in the history
…ush` and `$dir`
  • Loading branch information
Stefan Fleck committed Jul 22, 2020
1 parent fcb7fa6 commit 2829ca3
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 169 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
replacement for `base::saveRDS()` that supports creating backups instead of
just overwriting the destination file.
* added `Cache`, an R6 class for managing cache directories
* renamed `$push_backup()` and `$backup_dir` of BackupQueue to just `$push` and
`$dir`


# rotor 0.2.4
Expand Down
34 changes: 17 additions & 17 deletions R/BackupQueue.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ BackupQueue <- R6::R6Class(
public = list(
initialize = function(
file,
backup_dir = dirname(file),
dir = dirname(file),
max_backups = Inf,
compression = FALSE
){
self$set_file(file)
self$set_backup_dir(backup_dir)
self$set_dir(dir)
self$set_compression(compression)
self$set_max_backups(max_backups)

Expand Down Expand Up @@ -107,14 +107,14 @@ BackupQueue <- R6::R6Class(
},


set_backup_dir = function(
set_dir = function(
x
){
assert(
is_scalar_character(x) && dir.exists(x),
"backup dir '", x, "' does not exist."
)
private[[".backup_dir"]] <- x
private[[".dir"]] <- x
self
},

Expand Down Expand Up @@ -148,9 +148,9 @@ BackupQueue <- R6::R6Class(
get(".file", envir = private)
},

#' @field backup_dir `character` scalar. Directory in which to place the backups.
backup_dir = function(){
get(".backup_dir", envir = private)
#' @field dir `character` scalar. Directory in which to place the backups.
dir = function(){
get(".dir", envir = private)
},

#' @field compression (Optional) compression to use `compression` argument of [rotate()].
Expand Down Expand Up @@ -180,7 +180,7 @@ BackupQueue <- R6::R6Class(
backup_files <- get_backups(
file = self$file,
potential_backups =
list_files(self$backup_dir, full.names = self$backup_dir != "."),
list_files(self$dir, full.names = self$dir != "."),
sfx_patterns = c(
"\\d+",
"\\d{4}-\\d{2}-\\d{2}",
Expand Down Expand Up @@ -213,7 +213,7 @@ BackupQueue <- R6::R6Class(

private = list(
.file = NULL,
.backup_dir = NULL,
.dir = NULL,
.compression = NULL,
.max_backups = NULL
)
Expand All @@ -236,10 +236,10 @@ BackupQueueIndex <- R6::R6Class(


#' @description Create a new index-stamped backup (e.g. \file{logfile.1.log})
push_backup = function(){
push = function(){
# generate new filename
name <- file.path(
self$backup_dir,
self$dir,
tools::file_path_sans_ext(basename(self$file))
)
ext <- tools::file_ext(self$file)
Expand Down Expand Up @@ -397,14 +397,14 @@ BackupQueueDateTime <- R6::R6Class(
public = list(
initialize = function(
file,
backup_dir = dirname(file),
dir = dirname(file),
max_backups = Inf,
compression = FALSE,
fmt = "%Y-%m-%d--%H-%M-%S",
cache_backups = FALSE
){
self$set_file(file)
self$set_backup_dir(backup_dir)
self$set_dir(dir)
self$set_compression(compression)
self$set_max_backups(max_backups)
self$set_fmt(fmt)
Expand All @@ -418,7 +418,7 @@ BackupQueueDateTime <- R6::R6Class(
#' fielename (i.e timestamp)?
#' @param now `POSIXct` scalar. Can be used as an override meachanism for
#' the current system time if necessary.
push_backup = function(
push = function(
overwrite = FALSE,
now = Sys.time()
){
Expand All @@ -432,7 +432,7 @@ BackupQueueDateTime <- R6::R6Class(

# generate new filename
name <- file.path(
self$backup_dir,
self$dir,
tools::file_path_sans_ext(basename(self$file))
)

Expand Down Expand Up @@ -664,14 +664,14 @@ BackupQueueDate <- R6::R6Class(

initialize = function(
file,
backup_dir = dirname(file),
dir = dirname(file),
max_backups = Inf,
compression = FALSE,
fmt = "%Y-%m-%d",
cache_backups = FALSE
){
self$set_file(file)
self$set_backup_dir(backup_dir)
self$set_dir(dir)
self$set_compression(compression)
self$set_max_backups(max_backups)
self$set_fmt(fmt)
Expand Down
2 changes: 1 addition & 1 deletion R/Cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Cache <- R6::R6Class(
#' @field max_age
#' - a `Date` scalar: Remove all backups before this date
#' - a `character` scalar representing a Date in ISO format (e.g. `"2019-12-31"`)
#' - a `character` scalar representing an Interval in the form `"<number> <interval>"` (see [?rotate()])
#' - a `character` scalar representing an Interval in the form `"<number> <interval>"` (see [rotate()])
max_age = function(x){
if (missing(x)) return(get(".max_age", envir = private))
self$set_max_age(x)
Expand Down
46 changes: 23 additions & 23 deletions R/list_backups.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
#' [file.info()]
backup_info <- function(
file,
backup_dir = dirname(file)
dir = dirname(file)
){
if (is_pure_BackupQueueIndex(file, backup_dir = backup_dir))
BackupQueueIndex$new(file, backup_dir = backup_dir)$backups
else if (is_pure_BackupQueueDateTime(file, backup_dir = backup_dir))
BackupQueueDateTime$new(file, backup_dir = backup_dir)$backups
if (is_pure_BackupQueueIndex(file, dir = dir))
BackupQueueIndex$new(file, dir = dir)$backups
else if (is_pure_BackupQueueDateTime(file, dir = dir))
BackupQueueDateTime$new(file, dir = dir)$backups
else
BackupQueue$new(file, backup_dir = backup_dir)$backups
BackupQueue$new(file, dir = dir)$backups
}


Expand All @@ -47,9 +47,9 @@ backup_info <- function(
#' @rdname backup_info
list_backups <- function(
file,
backup_dir = dirname(file)
dir = dirname(file)
){
BackupQueue$new(file, backup_dir = backup_dir)$backups$path
BackupQueue$new(file, dir = dir)$backups$path
}


Expand All @@ -60,17 +60,17 @@ list_backups <- function(
#' scalar
n_backups <- function(
file,
backup_dir = dirname(file)
dir = dirname(file)
){
if (!is_pure_BackupQueue(file, backup_dir = backup_dir)){
if (!is_pure_BackupQueue(file, dir = dir)){
warning(
"Found index as well as timestamped backups for '", file, "'. ",
"This is fine, but some rotor functions might not work as expected",
"on such files.",
call. = FALSE
)
}
BackupQueue$new(file, backup_dir = backup_dir)$n_backups
BackupQueue$new(file, dir = dir)$n_backups
}


Expand All @@ -82,25 +82,25 @@ n_backups <- function(
#' @rdname backup_info
newest_backup <- function(
file,
backup_dir = dirname(file)
dir = dirname(file)
){
bq <- BackupQueue$new(file, backup_dir = backup_dir)
bq <- BackupQueue$new(file, dir = dir)
if (!bq$has_backups){
return(character())
}

assert(
is_pure_BackupQueueIndex(file, backup_dir = backup_dir) ||
is_pure_BackupQueueDateTime(file, backup_dir = backup_dir),
is_pure_BackupQueueIndex(file, dir = dir) ||
is_pure_BackupQueueDateTime(file, dir = dir),
"Can only determine newest backup for files that only have either indexed ",
"or timestamped backups, but '", file, "' has both:\n",
paste("~ ", bq$backups$path, collapse = "\n")
)

bq <- BackupQueueDateTime$new(file, backup_dir = backup_dir)
bq <- BackupQueueDateTime$new(file, dir = dir)

if (!bq$has_backups){
bq <- BackupQueueIndex$new(file, backup_dir = backup_dir)
bq <- BackupQueueIndex$new(file, dir = dir)
}

first(bq$backups$path)
Expand All @@ -113,25 +113,25 @@ newest_backup <- function(
#' @rdname backup_info
oldest_backup <- function(
file,
backup_dir = dirname(file)
dir = dirname(file)
){
bq <- BackupQueue$new(file, backup_dir = backup_dir)
bq <- BackupQueue$new(file, dir = dir)
if (!bq$has_backups){
return(character())
}

assert(
is_pure_BackupQueueIndex(file, backup_dir = backup_dir) ||
is_pure_BackupQueueDateTime(file, backup_dir = backup_dir),
is_pure_BackupQueueIndex(file, dir = dir) ||
is_pure_BackupQueueDateTime(file, dir = dir),
"Can only determine newest backup for files that only have either indexed ",
"or timestamped backups, but '", file, "' has both:\n",
paste("~ ", bq$backups$path, collapse = "\n")
)

bq <- BackupQueueDateTime$new(file, backup_dir = backup_dir)
bq <- BackupQueueDateTime$new(file, dir = dir)

if (!bq$has_backups){
bq <- BackupQueueIndex$new(file, backup_dir = backup_dir)
bq <- BackupQueueIndex$new(file, dir = dir)
}

last(bq$backups$path)
Expand Down
26 changes: 13 additions & 13 deletions R/rotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#' `KiB`, `MiB`, `GiB`, `TiB`. In Both cases `1` kilobyte is `1024` bytes, 1
#' `megabyte` is `1024` kilobytes, etc... .
#'
#' @param backup_dir `character` scalar. The directory in which the backups
#' @param dir `character` scalar. The directory in which the backups
#' of `file` are stored (defaults to `dirname(file)`)
#'
#' @param compression Whether or not backups should be compressed
Expand Down Expand Up @@ -174,7 +174,7 @@ rotate <- function(
size = 1,
max_backups = Inf,
compression = FALSE,
backup_dir = dirname(file),
dir = dirname(file),
create_file = TRUE,
dry_run = FALSE,
verbose = dry_run
Expand All @@ -184,7 +184,7 @@ rotate <- function(
size = size,
max_backups = max_backups,
compression = compression,
backup_dir = backup_dir,
dir = dir,
create_file = create_file,
dry_run = dry_run,
verbose = verbose,
Expand All @@ -202,7 +202,7 @@ backup <- function(
size = 0,
max_backups = Inf,
compression = FALSE,
backup_dir = dirname(file),
dir = dirname(file),
dry_run = FALSE,
verbose = dry_run
){
Expand All @@ -211,7 +211,7 @@ backup <- function(
size = size,
max_backups = max_backups,
compression = compression,
backup_dir = backup_dir,
dir = dir,
dry_run = dry_run,
verbose = verbose,
create_file = FALSE,
Expand All @@ -228,7 +228,7 @@ rotate_internal <- function(
max_backups,
compression,
create_file,
backup_dir,
dir,
dry_run,
verbose,
do_rotate
Expand All @@ -240,7 +240,7 @@ rotate_internal <- function(
is_scalar_bool(create_file)
)

assert_pure_BackupQueue(file, backup_dir = backup_dir, warn_only = TRUE)
assert_pure_BackupQueue(file, dir = dir, warn_only = TRUE)

if (dry_run){
DRY_RUN$activate()
Expand All @@ -249,13 +249,13 @@ rotate_internal <- function(

bq <- BackupQueueIndex$new(
file,
backup_dir = backup_dir,
dir = dir,
max_backups = max_backups,
compression = compression
)

if (bq$should_rotate(size = size, verbose = verbose)){
bq$push_backup()
bq$push()
} else {
do_rotate <- FALSE
}
Expand Down Expand Up @@ -286,22 +286,22 @@ rotate_internal <- function(
prune_backups <- function(
file,
max_backups,
backup_dir = dirname(file),
dir = dirname(file),
dry_run = FALSE,
verbose = dry_run
){
assert_pure_BackupQueue(file, backup_dir = backup_dir)
assert_pure_BackupQueue(file, dir = dir)
assert(is_scalar_character(file))

if (dry_run){
DRY_RUN$activate()
on.exit(DRY_RUN$deactivate())
}

bq <- BackupQueueIndex$new(file, backup_dir = backup_dir)
bq <- BackupQueueIndex$new(file, dir = dir)

if (!bq$has_backups)
bq <- BackupQueueDateTime$new(file, backup_dir = backup_dir)
bq <- BackupQueueDateTime$new(file, dir = dir)

bq$prune(max_backups = max_backups)
invisible(file)
Expand Down
Loading

0 comments on commit 2829ca3

Please sign in to comment.