-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrotate_rds.R
186 lines (165 loc) · 3.59 KB
/
rotate_rds.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#' Serialize R objects to disk (with backup)
#'
#' The `rotate_rds*()` functions are wrappers around [base::saveRDS()][base::readRDS()] that
#' create a backup of the destination file (if it exists) instead of just
#' overwriting it.
#'
#' @note The default value for `age` is different for `rotate_rds_date()` (`-1`)
#' than for [rotate_date()] (`1`) to make it a bit safer. This means if you
#' execute `rotate_date()` twice on the same file on a given day it will
#' silently not rotate the file, while `rotate_rds_date()` will throw an
#' error.
#'
#' @param on_change_only `logical` scalaror a `list`. Rotate only if `object`
#' is different from the object saved in `file`. If a `list`, arguments
#' that will be passed on to `data.table::all.equal` (only when both obects
#' are `data.tables`)
#'
#' @inheritParams base::saveRDS
#' @inheritDotParams rotate
#' @inheritParams rotate_date
#' @inheritDotParams rotate_date
#' @inheritDotParams rotate_time
#'
#' @return the path to `file` (invisibly)
#' @export
#'
#' @examples
#' dest <- tempfile()
#' rotate_rds(iris, dest)
#' rotate_rds(iris, dest)
#' rotate_rds(iris, dest)
#'
#' list_backups(dest)
#'
#' # cleanup
#' unlink(list_backups(dest))
#' unlink(dest)
#' @rdname rotate_rds
#' @export
rotate_rds <- function(
object,
file = "",
ascii = FALSE,
version = NULL,
compress = TRUE,
refhook = NULL,
...,
on_change_only = FALSE
){
rotate_rds_internal(
object = object,
file = file,
ascii = ascii,
version = version,
compress = compress,
refhook = refhook,
...,
on_change_only = on_change_only,
fun = rotate
)
}
#' @rdname rotate_rds
#' @export
rotate_rds_date <- function(
object,
file = "",
ascii = FALSE,
version = NULL,
compress = TRUE,
refhook = NULL,
...,
age = -1L,
on_change_only = FALSE
){
rotate_rds_internal(
object = object,
file = file,
ascii = ascii,
version = version,
compress = compress,
refhook = refhook,
...,
age = age,
on_change_only = on_change_only,
fun = rotate_date
)
}
#' @rdname rotate_rds
#' @export
rotate_rds_time <- function(
object,
file = "",
ascii = FALSE,
version = NULL,
compress = TRUE,
refhook = NULL,
...,
age = -1L,
on_change_only = FALSE
){
rotate_rds_internal(
object = object,
file = file,
ascii = ascii,
version = version,
compress = compress,
refhook = refhook,
...,
age = age,
on_change_only = on_change_only,
fun = rotate_time
)
}
rotate_rds_internal <- function(
object,
file,
ascii,
version,
compress,
refhook,
...,
on_change_only,
fun
){
assert(is_scalar_character(file))
assert(is_scalar_bool(on_change_only) || is.list(on_change_only))
if (file.exists(file)){
if (isTRUE(on_change_only) || is.list(on_change_only)){
comp <- readRDS(file)
if (is.list(on_change_only)){
extra_args <- on_change_only
} else {
extra_args <- list()
}
if (objects_are_equal(object, comp, extra_args)){
message(ObjectHasNotChangedMessage("not rotating: object has not changed"))
return(invisible(file))
}
}
fun(file, ...)
}
saveRDS(
object = object,
file = file,
ascii = ascii,
version = version,
compress = compress,
refhook = refhook
)
invisible(file)
}
objects_are_equal <- function(
x,
y,
extra_args = NULL
){
if (identical(x, y)){
return(TRUE)
}
if (inherits(x, "data.table") && inherits(y, "data.table")){
assert_namespace("data.table")
return(isTRUE(do.call(all.equal, c(list(x, y), extra_args))))
}
FALSE
}