X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/62a2ccba46cb5b83e510e727afa44eee2e893676..8e43dcf899a3322e66c709b149ce557f5255bf18:/sdk/R/R/CollectionTree.R diff --git a/sdk/R/R/CollectionTree.R b/sdk/R/R/CollectionTree.R index 82c6eb8f9e..e01e7e8de9 100644 --- a/sdk/R/R/CollectionTree.R +++ b/sdk/R/R/CollectionTree.R @@ -1,13 +1,7 @@ -source("./R/Subcollection.R") +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 -source("./R/ArvadosFile.R") - -#' Arvados Collection Object -#' -#' Update description -#' -#' @examples arv = Collection$new(api, uuid) -#' @export CollectionTree CollectionTree <- R6::R6Class( "CollectionTree", public = list( @@ -17,50 +11,16 @@ CollectionTree <- R6::R6Class( initialize = function(fileContent, collection) { self$pathsList <- fileContent - - treeBranches <- sapply(fileContent, function(filePath) - { - splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE)) - branch = private$createBranch(splitPath) - }) - + treeBranches <- sapply(fileContent, function(filePath) self$createBranch(filePath)) root <- Subcollection$new("") - - sapply(treeBranches, function(branch) - { - private$addBranch(root, branch) - }) - - root$.__enclos_env__$private$addToCollection(collection) + sapply(treeBranches, function(branch) self$addBranch(root, branch)) + root$setCollection(collection) private$tree <- root }, - getElement = function(relativePath) - { - if(endsWith(relativePath, "/")) - relativePath <- substr(relativePath, 0, nchar(relativePath) - 1) - - splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE)) - returnElement = private$tree - - for(pathFragment in splitPath) - { - returnElement = returnElement$.__enclos_env__$private$getChild(pathFragment) - - if(is.null(returnElement)) - return(NULL) - } - - returnElement - } - ), - - private = list( - - tree = NULL, - - createBranch = function(splitPath) + createBranch = function(filePath) { + splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE)) branch <- NULL lastElementIndex <- length(splitPath) @@ -68,47 +28,82 @@ CollectionTree <- R6::R6Class( { if(elementIndex == lastElementIndex) { - branch = ArvadosFile$new(splitPath[[elementIndex]]) + branch <- ArvadosFile$new(splitPath[[elementIndex]]) } else { - newFolder = Subcollection$new(splitPath[[elementIndex]]) + newFolder <- Subcollection$new(splitPath[[elementIndex]]) newFolder$add(branch) - branch = newFolder + branch <- newFolder } } - + branch }, addBranch = function(container, node) { - child = container$.__enclos_env__$private$getChild(node$getName()) + child <- container$get(node$getName()) if(is.null(child)) { + # Make sure we are don't make any REST call while adding child + collection <- container$getCollection() + container$setCollection(NULL, setRecursively = FALSE) container$add(node) - #todo add it to collection + container$setCollection(collection, setRecursively = FALSE) } else { + # Note: REST always returns folder name alone before other folder + # content, so in first iteration we don't know if it's a file + # or folder since its just a name, so we assume it's a file. + # If we encounter that same name again we know + # it's a folder so we need to replace ArvadosFile with Subcollection. if("ArvadosFile" %in% class(child)) - { child = private$replaceFileWithSubcollection(child) - } - private$addBranch(child, node$.__enclos_env__$private$getFirstChild()) + self$addBranch(child, node$getFirst()) + } + }, + + getElement = function(relativePath) + { + relativePath <- trimFromStart(relativePath, "./") + relativePath <- trimFromEnd(relativePath, "/") + + if(endsWith(relativePath, "/")) + relativePath <- substr(relativePath, 0, nchar(relativePath) - 1) + + splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE)) + returnElement <- private$tree + + for(pathFragment in splitPath) + { + returnElement <- returnElement$get(pathFragment) + + if(is.null(returnElement)) + return(NULL) } + + returnElement }, + getTree = function() private$tree + ), + + private = list( + + tree = NULL, + replaceFileWithSubcollection = function(arvadosFile) { subcollection <- Subcollection$new(arvadosFile$getName()) - fileParent <- arvadosFile$.__enclos_env__$private$parent - fileParent$.__enclos_env__$private$removeChild(arvadosFile$getName()) + fileParent <- arvadosFile$getParent() + fileParent$remove(arvadosFile$getName()) fileParent$add(subcollection) - arvadosFile$.__enclos_env__$private$parent <- NULL + arvadosFile$setParent(NULL) subcollection }