Skip to content

Commit 030ffba

Browse files
author
Stefan Fleck
committed
implement BackupQueue$prune_identical()
1 parent 3984997 commit 030ffba

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `$backup_dir` -> `dir()`
1212
- `$backups` -> `$files`
1313
- `$file` -> `$origin`
14+
* `BackupQueue$prune_identical()` removes identical backups for a BackupQueue
1415

1516

1617
# rotor 0.2.4

R/BackupQueue.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,36 @@ BackupQueueIndex <- R6::R6Class(
310310
},
311311

312312

313+
prune_identical = function(
314+
){
315+
dd <- self$files
316+
dd$md5 <- tools::md5sum(self$files$path)
317+
318+
dd <- dd[nrow(dd):1L, ]
319+
sel <- duplicated(dd$md5)
320+
321+
remove <- dd[sel, ]
322+
keep <- dd[!sel, ]
323+
324+
unlink(remove$path)
325+
326+
keep$path_new <- paste(
327+
file.path(dirname(keep$path), keep$name),
328+
pad_left(nrow(keep):1, pad = "0"),
329+
keep$ext,
330+
sep = "."
331+
)
332+
keep$path_new <- gsub("\\.$", "", keep$path_new)
333+
334+
# path_new will always have identical or lower indices than the old path.
335+
# if we sort be sfx we can prevent race conditions in file.rename
336+
# (i.e. where files would be overwriten because)
337+
keep <- keep[order(keep$sfx), ]
338+
file.rename(keep$path, keep$path_new)
339+
self
340+
},
341+
342+
313343
#' @description Should a file of `size` be rotated? See `size` argument of [`rotate()`]
314344
#' @return `TRUE` or `FALSE`
315345
should_rotate = function(size, verbose = FALSE){

tests/testthat/test_BackupQueue.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,39 @@ test_that("BackupQueueIndex: $should_rotate(verbose = TRUE) displays helpful mes
488488

489489

490490

491+
492+
test_that("BackupQueueIndex: $prune_identical works", {
493+
tf <- file.path(td, "test")
494+
495+
saveRDS(iris, tf)
496+
iris_md5 <- tools::md5sum(tf)
497+
bq <- BackupQueueIndex$new(tf)
498+
on.exit({
499+
bq$prune(0)
500+
unlink(tf)
501+
})
502+
backup(tf)
503+
backup(tf)
504+
rotate(tf)
505+
506+
saveRDS(cars, tf)
507+
cars_md5 <- tools::md5sum(tf)
508+
backup(tf)
509+
saveRDS(cars, tf)
510+
rotate(tf)
511+
512+
saveRDS(iris, tf)
513+
514+
bq$prune_identical()
515+
516+
expect_identical(
517+
unname(tools::md5sum(bq$files$path)),
518+
unname(c(cars_md5, iris_md5))
519+
)
520+
})
521+
522+
523+
491524
# BackupQueueDateTime -----------------------------------------------------
492525
context("BackupQueueDateTime")
493526

0 commit comments

Comments
 (0)