-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDirectoryQueue.R
104 lines (81 loc) · 2.07 KB
/
DirectoryQueue.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
# DirectoryQueue --------------------------------------------------------------
#' An R6 class for managing persistent file-based queues (abstract base class)
#'
#' Abstract class from which all other classes in \pkg{rotor} inherit their
#' basic fields and methods.
#'
#' @template r6_api
#' @export
DirectoryQueue <- R6::R6Class(
"DirectoryQueue",
cloneable = FALSE,
public = list(
initialize = function(
...
){
NotImplementedError()
},
push = function(
x,
...
){
NotImplementedError()
},
prune = function(
x,
...
){
NotImplementedError()
},
# ... setters -------------------------------------------------------------
set_dir = function(
x,
create = TRUE
){
assert(is_scalar_character(x))
assert(is_scalar_bool(create))
x <- path_tidy(x)
if (!file_exists(x)){
if (create){
message("creating directory '", x, "'")
dir.create(x, recursive = TRUE)
} else {
stop(DirDoesNotExistError(dir = x))
}
}
assert(is_dir(x), PathIsNotADirError(dir = x))
private[[".dir"]] <- x
self
}
),
# ... getters -------------------------------------------------------------
active = list(
#' @field dir a `character` scalar. path of the directory in which to store
#' the cache files
dir = function(dir){
if (missing(dir))
return(get(".dir", envir = private))
self$set_dir(dir, create = FALSE)
},
n = function(){
nrow(self$files)
},
files = function(){
files <- list.files(self$dir, full.names = TRUE, all.files = TRUE, no.. = TRUE,)
if (!length(files)){
return(data.frame())
}
finfo <- file.info(files)
res <- cbind(
data.frame(path = rownames(finfo), stringsAsFactors = FALSE),
data.frame(key = basename(rownames(finfo)), stringsAsFactors = FALSE),
finfo
)
row.names(res) <- NULL
res[order(res$mtime), ]
}
),
private = list(
.dir = NULL
)
)