X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/2957b917aaaefc1485e1c5293d413f0931b3030a..342c15f625fda8746e7d74510a298286b3a52196:/sdk/R/R/Subcollection.R diff --git a/sdk/R/R/Subcollection.R b/sdk/R/R/Subcollection.R index b3b01f89c1..17a9ef3ee3 100644 --- a/sdk/R/R/Subcollection.R +++ b/sdk/R/R/Subcollection.R @@ -1,10 +1,14 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 + source("./R/util.R") #' Subcollection -#' +#' #' Subcollection class represents a folder inside Arvados collection. #' It is essentially a composite of arvadosFiles and other subcollections. -#' +#' #' @section Usage: #' \preformatted{subcollection = Subcollection$new(name)} #' @@ -12,7 +16,7 @@ source("./R/util.R") #' \describe{ #' \item{name}{Name of the subcollection.} #' } -#' +#' #' @section Methods: #' \describe{ #' \item{getName()}{Returns name of the subcollection.} @@ -22,7 +26,8 @@ source("./R/util.R") #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.} #' \item{getFileListing()}{Returns subcollections file content as character vector.} #' \item{getSizeInBytes()}{Returns subcollections content size in bytes.} -#' \item{move(newLocation)}{Moves subcollection to a new location inside collection.} +#' \item{move(destination)}{Moves subcollection to a new location inside collection.} +#' \item{copy(destination)}{Copies subcollection to a new location inside collection.} #' } #' #' @name Subcollection @@ -36,6 +41,7 @@ source("./R/util.R") #' myFolder$remove("myFile") #' #' myFolder$move("newLocation/myFolder") +#' myFolder$copy("newLocation/myFolder") #' } NULL @@ -52,7 +58,7 @@ Subcollection <- R6::R6Class( }, getName = function() private$name, - + getRelativePath = function() { relativePath <- c(private$name) @@ -73,6 +79,9 @@ Subcollection <- R6::R6Class( if("ArvadosFile" %in% class(content) || "Subcollection" %in% class(content)) { + if(!is.null(content$getCollection())) + stop("Content already belongs to a collection.") + if(content$getName() == "") stop("Content has invalid name.") @@ -83,7 +92,7 @@ Subcollection <- R6::R6Class( "or Subcollection with same name.")) if(!is.null(private$collection)) - { + { if(self$getRelativePath() != "") contentPath <- paste0(self$getRelativePath(), "/", content$getFileListing()) @@ -157,20 +166,18 @@ Subcollection <- R6::R6Class( return(sum(fileSizes)) }, - move = function(newLocation) + move = function(destination) { if(is.null(private$collection)) - stop("Subcollection doesn't belong to any collection") + stop("Subcollection doesn't belong to any collection.") - newLocation <- trimFromEnd(newLocation, "/") - nameAndPath <- splitToPathAndName(newLocation) + destination <- trimFromEnd(destination, "/") + nameAndPath <- splitToPathAndName(destination) newParent <- private$collection$get(nameAndPath$path) if(is.null(newParent)) - { - stop("Unable to get destination subcollection") - } + stop("Unable to get destination subcollection.") childWithSameName <- newParent$get(nameAndPath$name) @@ -183,11 +190,53 @@ Subcollection <- R6::R6Class( private$collection$uuid) private$dettachFromCurrentParent() - private$attachToNewParent(newParent) + private$attachToNewParent(self, newParent) + private$parent <- newParent private$name <- nameAndPath$name - "Content moved successfully." + self + }, + + copy = function(destination) + { + if(is.null(private$collection)) + stop("Subcollection doesn't belong to any collection.") + + destination <- trimFromEnd(destination, "/") + nameAndPath <- splitToPathAndName(destination) + + newParent <- private$collection$get(nameAndPath$path) + + if(is.null(newParent) || !("Subcollection" %in% class(newParent))) + stop("Unable to get destination subcollection.") + + childWithSameName <- newParent$get(nameAndPath$name) + + if(!is.null(childWithSameName)) + stop("Destination already contains content with same name.") + + REST <- private$collection$getRESTService() + REST$copy(self$getRelativePath(), + paste0(newParent$getRelativePath(), "/", nameAndPath$name), + private$collection$uuid) + + newContent <- self$duplicate(nameAndPath$name) + newContent$setCollection(self$getCollection(), setRecursively = TRUE) + newContent$setParent(newParent) + private$attachToNewParent(newContent, newParent) + + newContent + }, + + duplicate = function(newName = NULL) + { + name <- if(!is.null(newName)) newName else private$name + root <- Subcollection$new(name) + for(child in private$children) + root$add(child$duplicate()) + + root }, get = function(name) @@ -250,30 +299,29 @@ Subcollection <- R6::R6Class( } }, - attachToNewParent = function(newParent) + attachToNewParent = function(content, newParent) { - #Note: We temporary set parents collection to NULL. This will ensure that - # add method doesn't post file on REST. + # We temporary set parents collection to NULL. This will ensure that + # add method doesn't post this subcollection to REST. + # We also need to set content's collection to NULL because + # add method throws exception if we try to add content that already + # belongs to a collection. parentsCollection <- newParent$getCollection() + content$setCollection(NULL, setRecursively = FALSE) newParent$setCollection(NULL, setRecursively = FALSE) - - newParent$add(self) - + newParent$add(content) + content$setCollection(parentsCollection, setRecursively = FALSE) newParent$setCollection(parentsCollection, setRecursively = FALSE) - - private$parent <- newParent }, dettachFromCurrentParent = function() { - #Note: We temporary set parents collection to NULL. This will ensure that - # remove method doesn't remove this subcollection from REST. + # We temporary set parents collection to NULL. This will ensure that + # remove method doesn't remove this subcollection from REST. parent <- private$parent parentsCollection <- parent$getCollection() parent$setCollection(NULL, setRecursively = FALSE) - parent$remove(private$name) - parent$setCollection(parentsCollection, setRecursively = FALSE) }, @@ -298,7 +346,7 @@ Subcollection <- R6::R6Class( content } ), - + cloneable = FALSE ) @@ -308,7 +356,7 @@ Subcollection <- R6::R6Class( #' #' @param x Instance of Subcollection class #' @param ... Optional arguments. -#' @export +#' @export print.Subcollection = function(x, ...) { collection <- NULL