1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
9 #' Subcollection class represents a folder inside Arvados collection.
10 #' It is essentially a composite of arvadosFiles and other subcollections.
13 #' \preformatted{subcollection = Subcollection$new(name)}
15 #' @section Arguments:
17 #' \item{name}{Name of the subcollection.}
22 #' \item{getName()}{Returns name of the subcollection.}
23 #' \item{getRelativePath()}{Returns subcollection path relative to the root.}
24 #' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the subcollection.}
25 #' \item{remove(name)}{Removes ArvadosFile or Subcollection specified by name from the subcollection.}
26 #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
27 #' \item{getFileListing()}{Returns subcollections file content as character vector.}
28 #' \item{getSizeInBytes()}{Returns subcollections content size in bytes.}
29 #' \item{move(destination)}{Moves subcollection to a new location inside collection.}
30 #' \item{copy(destination)}{Copies subcollection to a new location inside collection.}
33 #' @name Subcollection
36 #' myFolder <- Subcollection$new("myFolder")
37 #' myFile <- ArvadosFile$new("myFile")
39 #' myFolder$add(myFile)
40 #' myFolder$get("myFile")
41 #' myFolder$remove("myFile")
43 #' myFolder$move("newLocation/myFolder")
44 #' myFolder$copy("newLocation/myFolder")
49 Subcollection <- R6::R6Class(
55 initialize = function(name)
60 getName = function() private$name,
62 getRelativePath = function()
64 relativePath <- c(private$name)
65 parent <- private$parent
67 while(!is.null(parent))
69 relativePath <- c(parent$getName(), relativePath)
70 parent <- parent$getParent()
73 relativePath <- relativePath[relativePath != ""]
74 paste0(relativePath, collapse = "/")
77 add = function(content)
79 if("ArvadosFile" %in% class(content) ||
80 "Subcollection" %in% class(content))
82 if(content$getName() == "")
83 stop("Content has invalid name.")
85 childWithSameName <- self$get(content$getName())
87 if(!is.null(childWithSameName))
88 stop(paste("Subcollection already contains ArvadosFile",
89 "or Subcollection with same name."))
91 if(!is.null(private$collection))
93 if(self$getRelativePath() != "")
94 contentPath <- paste0(self$getRelativePath(),
95 "/", content$getFileListing())
97 contentPath <- content$getFileListing()
99 REST <- private$collection$getRESTService()
100 REST$create(contentPath, private$collection$uuid)
101 content$setCollection(private$collection)
104 private$children <- c(private$children, content)
105 content$setParent(self)
107 "Content added successfully."
111 stop(paste0("Expected AravodsFile or Subcollection object, got ",
112 paste0("(", paste0(class(content), collapse = ", "), ")"),
117 remove = function(name)
119 if(is.character(name))
121 child <- self$get(name)
124 stop(paste("Subcollection doesn't contains ArvadosFile",
125 "or Subcollection with specified name."))
127 if(!is.null(private$collection))
129 REST <- private$collection$getRESTService()
130 REST$delete(child$getRelativePath(), private$collection$uuid)
132 child$setCollection(NULL)
135 private$removeChild(name)
136 child$setParent(NULL)
142 stop(paste0("Expected character, got ",
143 paste0("(", paste0(class(name), collapse = ", "), ")"),
148 getFileListing = function(fullPath = TRUE)
150 content <- private$getContentAsCharVector(fullPath)
151 content[order(tolower(content))]
154 getSizeInBytes = function()
156 if(is.null(private$collection))
159 REST <- private$collection$getRESTService()
161 fileSizes <- REST$getResourceSize(paste0(self$getRelativePath(), "/"),
162 private$collection$uuid)
163 return(sum(fileSizes))
166 move = function(destination)
168 if(is.null(private$collection))
169 stop("Subcollection doesn't belong to any collection.")
171 destination <- trimFromEnd(destination, "/")
172 nameAndPath <- splitToPathAndName(destination)
174 newParent <- private$collection$get(nameAndPath$path)
176 if(is.null(newParent))
177 stop("Unable to get destination subcollection.")
179 childWithSameName <- newParent$get(nameAndPath$name)
181 if(!is.null(childWithSameName))
182 stop("Destination already contains content with same name.")
184 REST <- private$collection$getRESTService()
185 REST$move(self$getRelativePath(),
186 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
187 private$collection$uuid)
189 private$dettachFromCurrentParent()
190 private$attachToNewParent(self, newParent)
192 private$parent <- newParent
193 private$name <- nameAndPath$name
198 copy = function(destination)
200 if(is.null(private$collection))
201 stop("Subcollection doesn't belong to any collection.")
203 destination <- trimFromEnd(destination, "/")
204 nameAndPath <- splitToPathAndName(destination)
206 newParent <- private$collection$get(nameAndPath$path)
208 if(is.null(newParent) || !("Subcollection" %in% class(newParent)))
209 stop("Unable to get destination subcollection.")
211 childWithSameName <- newParent$get(nameAndPath$name)
213 if(!is.null(childWithSameName))
214 stop("Destination already contains content with same name.")
216 REST <- private$collection$getRESTService()
217 REST$copy(self$getRelativePath(),
218 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
219 private$collection$uuid)
221 newContent <- self$duplicate(nameAndPath$name)
222 newContent$setCollection(self$getCollection(), setRecursively = TRUE)
223 newContent$setParent(newParent)
224 private$attachToNewParent(newContent, newParent)
229 duplicate = function(newName = NULL)
231 name <- if(!is.null(newName)) newName else private$name
232 root <- Subcollection$new(name)
233 for(child in private$children)
234 root$add(child$duplicate())
241 for(child in private$children)
243 if(child$getName() == name)
250 getFirst = function()
252 if(length(private$children) == 0)
255 private$children[[1]]
258 setCollection = function(collection, setRecursively = TRUE)
260 private$collection = collection
264 for(child in private$children)
265 child$setCollection(collection)
269 getCollection = function() private$collection,
271 getParent = function() private$parent,
273 setParent = function(newParent) private$parent <- newParent
283 removeChild = function(name)
285 numberOfChildren = length(private$children)
286 if(numberOfChildren > 0)
288 for(childIndex in 1:numberOfChildren)
290 if(private$children[[childIndex]]$getName() == name)
292 private$children = private$children[-childIndex]
299 attachToNewParent = function(content, newParent)
301 #Note: We temporary set parents collection to NULL. This will ensure that
302 # add method doesn't post this file on REST.
303 parentsCollection <- newParent$getCollection()
304 newParent$setCollection(NULL, setRecursively = FALSE)
305 newParent$add(content)
306 newParent$setCollection(parentsCollection, setRecursively = FALSE)
309 dettachFromCurrentParent = function()
311 #Note: We temporary set parents collection to NULL. This will ensure that
312 # remove method doesn't remove this subcollection from REST.
313 parent <- private$parent
314 parentsCollection <- parent$getCollection()
315 parent$setCollection(NULL, setRecursively = FALSE)
316 parent$remove(private$name)
317 parent$setCollection(parentsCollection, setRecursively = FALSE)
320 getContentAsCharVector = function(fullPath = TRUE)
326 for(child in private$children)
327 content <- c(content, child$getFileListing())
329 if(private$name != "")
330 content <- unlist(paste0(private$name, "/", content))
334 for(child in private$children)
335 content <- c(content, child$getName())
345 #' print.Subcollection
347 #' Custom print function for Subcollection class
349 #' @param x Instance of Subcollection class
350 #' @param ... Optional arguments.
352 print.Subcollection = function(x, ...)
355 relativePath <- x$getRelativePath()
357 if(!is.null(x$getCollection()))
359 collection <- x$getCollection()$uuid
361 if(!x$getName() == "")
362 relativePath <- paste0("/", relativePath)
365 cat(paste0("Type: ", "\"", "Arvados Subcollection", "\""), sep = "\n")
366 cat(paste0("Name: ", "\"", x$getName(), "\""), sep = "\n")
367 cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n")
368 cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n")