1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
7 #' Collection class provides interface for working with Arvados collections.
10 #' \preformatted{collection = Collection$new(arv, uuid)}
12 #' @section Arguments:
14 #' \item{arv}{Arvados object.}
15 #' \item{uuid}{UUID of a collection.}
20 #' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the collection.}
21 #' \item{create(files)}{Creates one or more ArvadosFiles and adds them to the collection at specified path.}
22 #' \item{remove(fileNames)}{Remove one or more files from the collection.}
23 #' \item{move(content, destination)}{Moves ArvadosFile or Subcollection to another location in the collection.}
24 #' \item{copy(content, destination)}{Copies ArvadosFile or Subcollection to another location in the collection.}
25 #' \item{getFileListing()}{Returns collections file content as character vector.}
26 #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
32 #' arv <- Arvados$new("your Arvados token", "example.arvadosapi.com")
33 #' collection <- Collection$new(arv, "uuid")
35 #' createdFiles <- collection$create(c("main.cpp", lib.dll), "cpp/src/")
37 #' collection$remove("location/to/my/file.cpp")
39 #' collection$move("folder/file.cpp", "file.cpp")
41 #' arvadosFile <- collection$get("location/to/my/file.cpp")
42 #' arvadosSubcollection <- collection$get("location/to/my/directory/")
47 Collection <- R6::R6Class(
55 initialize = function(api, uuid)
57 private$REST <- api$getRESTService()
61 add = function(content, relativePath = "")
63 if(is.null(private$tree))
64 private$generateCollectionTreeStructure()
66 if(relativePath == "" ||
67 relativePath == "." ||
70 subcollection <- private$tree$getTree()
74 relativePath <- trimFromEnd(relativePath, "/")
75 subcollection <- self$get(relativePath)
78 if(is.null(subcollection))
79 stop(paste("Subcollection", relativePath, "doesn't exist."))
81 if("ArvadosFile" %in% class(content) ||
82 "Subcollection" %in% class(content))
84 if(!is.null(content$getCollection()))
85 stop("Content already belongs to a collection.")
87 if(content$getName() == "")
88 stop("Content has invalid name.")
90 subcollection$add(content)
95 stop(paste0("Expected AravodsFile or Subcollection object, got ",
96 paste0("(", paste0(class(content), collapse = ", "), ")"),
101 create = function(files)
103 if(is.null(private$tree))
104 private$generateCollectionTreeStructure()
106 if(is.character(files))
108 sapply(files, function(file)
110 childWithSameName <- self$get(file)
111 if(!is.null(childWithSameName))
112 stop("Destination already contains file with same name.")
114 newTreeBranch <- private$tree$createBranch(file)
115 private$tree$addBranch(private$tree$getTree(), newTreeBranch)
117 private$REST$create(file, self$uuid)
118 newTreeBranch$setCollection(self)
124 stop(paste0("Expected character vector, got ",
125 paste0("(", paste0(class(files), collapse = ", "), ")"),
130 remove = function(paths)
132 if(is.null(private$tree))
133 private$generateCollectionTreeStructure()
135 if(is.character(paths))
137 sapply(paths, function(filePath)
139 filePath <- trimFromEnd(filePath, "/")
140 file <- self$get(filePath)
143 stop(paste("File", filePath, "doesn't exist."))
145 parent <- file$getParent()
148 stop("You can't delete root folder.")
150 parent$remove(file$getName())
157 stop(paste0("Expected character vector, got ",
158 paste0("(", paste0(class(paths), collapse = ", "), ")"),
163 move = function(content, destination)
165 if(is.null(private$tree))
166 private$generateCollectionTreeStructure()
168 content <- trimFromEnd(content, "/")
170 elementToMove <- self$get(content)
172 if(is.null(elementToMove))
173 stop("Content you want to move doesn't exist in the collection.")
175 elementToMove$move(destination)
178 copy = function(content, destination)
180 if(is.null(private$tree))
181 private$generateCollectionTreeStructure()
183 content <- trimFromEnd(content, "/")
185 elementToCopy <- self$get(content)
187 if(is.null(elementToCopy))
188 stop("Content you want to copy doesn't exist in the collection.")
190 elementToCopy$copy(destination)
195 if(!is.null(private$tree))
197 private$tree$getTree()$setCollection(NULL, setRecursively = TRUE)
202 getFileListing = function()
204 if(is.null(private$tree))
205 private$generateCollectionTreeStructure()
207 content <- private$REST$getCollectionContent(self$uuid)
208 content[order(tolower(content))]
211 get = function(relativePath)
213 if(is.null(private$tree))
214 private$generateCollectionTreeStructure()
216 private$tree$getElement(relativePath)
219 getRESTService = function() private$REST,
220 setRESTService = function(newRESTService) private$REST <- newRESTService
229 generateCollectionTreeStructure = function()
231 if(is.null(self$uuid))
232 stop("Collection uuid is not defined.")
234 if(is.null(private$REST))
235 stop("REST service is not defined.")
237 private$fileContent <- private$REST$getCollectionContent(self$uuid)
238 private$tree <- CollectionTree$new(private$fileContent, self)
247 #' Custom print function for Collection class
249 #' @param x Instance of Collection class
250 #' @param ... Optional arguments.
252 print.Collection = function(x, ...)
254 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
255 cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n")