X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/3a6a89199e2478454905731ccef0868d5fdd6f8f..649d52a70fd75e255437deb1798fa9f09697b78c:/sdk/R/R/Collection.R diff --git a/sdk/R/R/Collection.R b/sdk/R/R/Collection.R index 203aaff8cb..833b833c80 100644 --- a/sdk/R/R/Collection.R +++ b/sdk/R/R/Collection.R @@ -1,26 +1,68 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 + source("./R/Subcollection.R") source("./R/ArvadosFile.R") source("./R/RESTService.R") source("./R/util.R") -#' Arvados Collection Object +#' Collection +#' +#' Collection class provides interface for working with Arvados collections. +#' +#' @section Usage: +#' \preformatted{collection = Collection$new(arv, uuid)} +#' +#' @section Arguments: +#' \describe{ +#' \item{arv}{Arvados object.} +#' \item{uuid}{UUID of a collection.} +#' } +#' +#' @section Methods: +#' \describe{ +#' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the collection.} +#' \item{create(fileNames, relativePath = "")}{Creates one or more ArvadosFiles and adds them to the collection at specified path.} +#' \item{remove(fileNames)}{Remove one or more files from the collection.} +#' \item{move(content, newLocation)}{Moves ArvadosFile or Subcollection to another location in the collection.} +#' \item{getFileListing()}{Returns collections file content as character vector.} +#' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.} +#' } +#' +#' @name Collection +#' @examples +#' \dontrun{ +#' arv <- Arvados$new("your Arvados token", "example.arvadosapi.com") +#' collection <- Collection$new(arv, "uuid") +#' +#' newFile <- ArvadosFile$new("myFile") +#' collection$add(newFile, "myFolder") +#' +#' createdFiles <- collection$create(c("main.cpp", lib.dll), "cpp/src/") +#' +#' collection$remove("location/to/my/file.cpp") #' -#' Update description +#' collection$move("folder/file.cpp", "file.cpp") #' -#' @examples arv = Collection$new(api, uuid) -#' @export Collection +#' arvadosFile <- collection$get("location/to/my/file.cpp") +#' arvadosSubcollection <- collection$get("location/to/my/directory/") +#' } +NULL + +#' @export Collection <- R6::R6Class( "Collection", public = list( - api = NULL, - uuid = NULL, + uuid = NULL, + # api = NULL, - initialize = function(api, uuid) + initialize = function(api, uuid) { - self$api <- api + # self$api <- api private$REST <- api$getRESTService() self$uuid <- uuid @@ -31,6 +73,9 @@ Collection <- R6::R6Class( add = function(content, relativePath = "") { + if(is.null(private$tree)) + private$genereateCollectionTreeStructure() + if(relativePath == "" || relativePath == "." || relativePath == "./") @@ -49,8 +94,10 @@ Collection <- R6::R6Class( if("ArvadosFile" %in% class(content) || "Subcollection" %in% class(content)) { - subcollection$add(content) + if(content$getName() == "") + stop("Content has invalid name.") + subcollection$add(content) content } else @@ -63,6 +110,9 @@ Collection <- R6::R6Class( create = function(fileNames, relativePath = "") { + if(is.null(private$tree)) + private$genereateCollectionTreeStructure() + if(relativePath == "" || relativePath == "." || relativePath == "./") @@ -71,7 +121,7 @@ Collection <- R6::R6Class( } else { - relativePath <- trimFromEnd(relativePath, "/") + relativePath <- trimFromEnd(relativePath, "/") subcollection <- self$get(relativePath) } @@ -98,7 +148,7 @@ Collection <- R6::R6Class( else return(arvadosFiles) } - else + else { stop(paste0("Expected character vector, got ", paste0("(", paste0(class(fileNames), collapse = ", "), ")"), @@ -108,6 +158,9 @@ Collection <- R6::R6Class( remove = function(paths) { + if(is.null(private$tree)) + private$genereateCollectionTreeStructure() + if(is.character(paths)) { sapply(paths, function(filePath) @@ -119,10 +172,16 @@ Collection <- R6::R6Class( stop(paste("File", filePath, "doesn't exist.")) parent <- file$getParent() + + if(is.null(parent)) + stop("You can't delete root folder.") + parent$remove(file$getName()) }) + + "Content removed" } - else + else { stop(paste0("Expected character vector, got ", paste0("(", paste0(class(paths), collapse = ", "), ")"), @@ -132,23 +191,57 @@ Collection <- R6::R6Class( move = function(content, newLocation) { + if(is.null(private$tree)) + private$genereateCollectionTreeStructure() + content <- trimFromEnd(content, "/") elementToMove <- self$get(content) if(is.null(elementToMove)) - stop("Element you want to move doesn't exist in the collection.") + stop("Content you want to move doesn't exist in the collection.") elementToMove$move(newLocation) }, - getFileListing = function() private$REST$getCollectionContent(self$uuid), + getFileListing = function() + { + if(is.null(private$tree)) + private$genereateCollectionTreeStructure() + + content <- private$REST$getCollectionContent(self$uuid) + content[order(tolower(content))] + }, get = function(relativePath) { + if(is.null(private$tree)) + private$genereateCollectionTreeStructure() + private$tree$getElement(relativePath) }, + toJSON = function() + { + fields <- sapply(private$classFields, function(field) + { + self[[field]] + }, USE.NAMES = TRUE) + + jsonlite::toJSON(list("collection" = + Filter(Negate(is.null), fields)), auto_unbox = TRUE) + }, + + isEmpty = function() { + fields <- sapply(private$classFields, + function(field) self[[field]]) + + if(any(sapply(fields, function(field) !is.null(field) && field != ""))) + FALSE + else + TRUE + }, + getRESTService = function() private$REST, setRESTService = function(newRESTService) private$REST <- newRESTService ), @@ -158,17 +251,33 @@ Collection <- R6::R6Class( REST = NULL, tree = NULL, fileContent = NULL, + classFields = NULL, - generateTree = function(content) + genereateCollectionTreeStructure = function() { - treeBranches <- sapply(collectionContent, function(filePath) - { - splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE)) + if(is.null(self$uuid)) + stop("Collection uuid is not defined.") + + if(is.null(private$REST)) + stop("REST service is not defined.") - branch = private$createBranch(splitPath, filePath$fileSize) - }) + private$fileContent <- private$REST$getCollectionContent(self$uuid) + private$tree <- CollectionTree$new(private$fileContent, self) } ), cloneable = FALSE ) + +#' print.Collection +#' +#' Custom print function for Collection class +#' +#' @param x Instance of Collection class +#' @param ... Optional arguments. +#' @export +print.Collection = function(x, ...) +{ + cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n") + cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n") +}