Refactored collection tree into separate file.
authorFuad Muhic <fmuhic@capeannenterprises.com>
Wed, 13 Dec 2017 17:07:54 +0000 (18:07 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Wed, 13 Dec 2017 17:07:54 +0000 (18:07 +0100)
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/R/R/ArvadosFile.R
sdk/R/R/Collection.R
sdk/R/R/FileTree.R [new file with mode: 0644]

index f13bf571ff7b97436b1551bfc091debf530b4640..da8692d74a1aa0993e45f2f436f1d3bf07da1d35 100644 (file)
@@ -37,7 +37,6 @@ ArvadosFile <- R6::R6Class(
                 range = paste0(range, offset + length - 1)
             
             fileURL = paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid, "/", private$relativePath);
-            print(fileURL)
             headers <- list(Authorization = paste("OAuth2", private$api$getToken()), 
                             Range = range)
 
@@ -77,7 +76,8 @@ ArvadosFile <- R6::R6Class(
 
             private$size <- fileInfo[[1]]$fileSize
             private$collection$update(self, "File size changed")
-            #parsed_response <- httr::content(serverResponse, "text")
+
+            parsed_response <- httr::content(serverResponse, "text")
         }
     ),
 
index cecf657e8bb9a58694caac8d072932b2a8b55503..c29f8f005535dc0fefaef2009afbf9cbe84bf8f3 100644 (file)
@@ -1,5 +1,6 @@
 source("./R/Subcollection.R")
 source("./R/ArvadosFile.R")
+source("./R/FileTree.R")
 
 #' Arvados Collection Object
 #'
@@ -63,12 +64,12 @@ Collection <- R6::R6Class(
 
             private$fileItems <- private$getCollectionContent()
 
-            private$fileTree <- private$generateTree(private$fileItems)
+            private$fileTree <- FileTree$new(private$fileItems)
         },
 
         printFileContent = function()
         {
-            private$fileTree$printContent(0)
+            private$fileTree$printContent(private$fileTree$getRoot(), 0)
         },
 
         getFileContent = function()
@@ -81,7 +82,7 @@ Collection <- R6::R6Class(
 
         get = function(relativePath)
         {
-            treeNode <- private$traverseInOrder(private$fileTree, function(node)
+            treeNode <- private$fileTree$traverseInOrder(private$fileTree$getRoot(), function(node)
             {
                 if(node$relativePath == relativePath)
                     return(node)
@@ -130,9 +131,7 @@ Collection <- R6::R6Class(
 
         handleFileSizeChange = function(filePath, newSize)
         {
-            print(paste(filePath, newSize))
-
-            node <- private$getNode(filePath)
+            node <- private$fileTree$getNode(filePath)
             node$size <- newSize
         },
 
@@ -172,189 +171,9 @@ Collection <- R6::R6Class(
 
             parsedResponse <- HttpParser$new()$parseWebDAVResponse(response, uri)
             parsedResponse[-1]
-        },
-
-        #Todo(Fudo): Move tree creation to another file.
-        generateTree = function(collectionContent)
-        {
-            treeBranches <- sapply(collectionContent, function(filePath)
-            {
-                splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE))
-
-                branch = private$createBranch(splitPath, filePath$fileSize)      
-            })
-
-            root <- TreeNode$new("./", "root", NULL)
-            root$relativePath = ""
-
-            sapply(treeBranches, function(branch)
-            {
-                private$addNode(root, branch)
-            })
-
-            root
-        },
-
-        createBranch = function(splitPath, fileSize)
-        {
-            branch <- NULL
-            lastElementIndex <- length(splitPath)
-
-            for(elementIndex in lastElementIndex:1)
-            {
-                if(elementIndex == lastElementIndex)
-                {
-                    branch = TreeNode$new(splitPath[[elementIndex]], "file", fileSize)
-                }
-                else
-                {
-                    newFolder = TreeNode$new(splitPath[[elementIndex]], "folder", NULL)
-                    newFolder$addChild(branch)
-                    branch = newFolder
-                }
-
-                branch$relativePath <- paste(unlist(splitPath[1:elementIndex]), collapse = "/")
-            }
-            
-            branch
-        },
-
-        addNode = function(container, node)
-        {
-            child = container$getChild(node$name)
-
-            if(is.null(child))
-            {
-                container$addChild(node)
-            }
-            else
-            {
-                child$type = "folder"
-                private$addNode(child, node$getFirstChild())
-            }
-        },
-
-        traverseInOrder = function(node, predicate)
-        {
-            if(node$hasChildren())
-            {
-                result <- predicate(node)
-
-                if(!is.null(result))
-                    return(result)               
-
-                for(child in node$children)
-                {
-                    result <- private$traverseInOrder(child, predicate)
-
-                    if(!is.null(result))
-                        return(result)
-                }
-
-                return(NULL)
-            }
-            else
-            {
-                return(predicate(node))
-            }
-        },
-
-        getNode = function(relativePathToNode)
-        {
-            treeBranches <- sapply(relativePathToNode, function(filePath)
-            {
-                splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
-                
-                node = private$fileTree
-                for(pathFragment in splitPath)
-                {
-                    child = node$getChild(pathFragment)
-                    if(is.null(child))
-                        stop("Subcollection/ArvadosFile you are looking for doesn't exist.")
-                    node = child
-                }
-
-                node
-            })
         }
 
     ),
 
     cloneable = FALSE
 )
-
-TreeNode <- R6::R6Class(
-
-    "TreeNode",
-
-    public = list(
-
-        name         = NULL,
-        relativePath = NULL,
-        size         = NULL,
-        children     = NULL,
-        parent       = NULL,
-        type         = NULL,
-
-        initialize = function(name, type, size)
-        {
-            self$name <- name
-            self$type <- type
-            self$size <- size
-            self$children <- list()
-        },
-
-        addChild = function(node)
-        {
-            self$children <- c(self$children, node)
-            node$setParent(self)
-            self
-        },
-
-        setParent = function(parent)
-        {
-            self$parent = parent
-        },
-
-        getChild = function(childName)
-        {
-            for(child in self$children)
-            {
-                if(childName == child$name)
-                    return(child)
-            }
-
-            return(NULL)
-        },
-
-        hasChildren = function()
-        {
-            if(length(self$children) != 0)
-                return(TRUE)
-            else
-                return(FALSE)
-        },
-
-        getFirstChild = function()
-        {
-            if(!self$hasChildren())
-                return(NULL)
-            else
-                return(self$children[[1]])
-        },
-
-        printContent = function(depth)
-        {
-            indentation <- paste(rep("....", depth), collapse = "")
-            if(self$type == "folder")
-                print(paste0(indentation, self$name, "/"))
-            else
-                print(paste0(indentation, self$name))
-            
-            for(child in self$children)
-                child$printContent(depth + 1)
-        }
-    ),
-
-    cloneable = FALSE
-)
diff --git a/sdk/R/R/FileTree.R b/sdk/R/R/FileTree.R
new file mode 100644 (file)
index 0000000..ee54bd9
--- /dev/null
@@ -0,0 +1,192 @@
+FileTree <- R6::R6Class(
+    "FileTree",
+    public = list(
+        initialize = function(collectionContent)
+        {
+            treeBranches <- sapply(collectionContent, function(filePath)
+            {
+                splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE))
+
+                branch = private$createBranch(splitPath, filePath$fileSize)      
+            })
+
+            root <- TreeNode$new("./", "root", NULL)
+            root$relativePath = ""
+
+            sapply(treeBranches, function(branch)
+            {
+                private$addBranch(root, branch)
+            })
+
+            private$tree <- root
+        },
+
+        getRoot = function() private$tree,
+
+        printContent = function(node, depth)
+        {
+            indentation <- paste(rep("....", depth), collapse = "")
+            if(node$type == "folder")
+                print(paste0(indentation, node$name, "/"))
+            else
+                print(paste0(indentation, node$name))
+            
+            for(child in node$children)
+                self$printContent(child, depth + 1)
+        },
+
+        traverseInOrder = function(node, predicate)
+        {
+            if(node$hasChildren())
+            {
+                result <- predicate(node)
+
+                if(!is.null(result))
+                    return(result)               
+
+                for(child in node$children)
+                {
+                    result <- self$traverseInOrder(child, predicate)
+
+                    if(!is.null(result))
+                        return(result)
+                }
+
+                return(NULL)
+            }
+            else
+            {
+                return(predicate(node))
+            }
+        },
+
+        getNode = function(relativePathToNode)
+        {
+            treeBranches <- sapply(relativePathToNode, function(filePath)
+            {
+                splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
+                
+                node <- private$tree
+                for(pathFragment in splitPath)
+                {
+                    child = node$getChild(pathFragment)
+                    if(is.null(child))
+                        stop("Subcollection/ArvadosFile you are looking for doesn't exist.")
+                    node = child
+                }
+
+                node
+            })
+        }
+    ),
+
+    private = list(
+        tree = NULL,
+
+        createBranch = function(splitPath, fileSize)
+        {
+            branch <- NULL
+            lastElementIndex <- length(splitPath)
+
+            for(elementIndex in lastElementIndex:1)
+            {
+                if(elementIndex == lastElementIndex)
+                {
+                    branch = TreeNode$new(splitPath[[elementIndex]], "file", fileSize)
+                }
+                else
+                {
+                    newFolder = TreeNode$new(splitPath[[elementIndex]], "folder", NULL)
+                    newFolder$addChild(branch)
+                    branch = newFolder
+                }
+
+                branch$relativePath <- paste(unlist(splitPath[1:elementIndex]), collapse = "/")
+            }
+            
+            branch
+        },
+
+        addBranch = function(container, node)
+        {
+            child = container$getChild(node$name)
+
+            if(is.null(child))
+            {
+                container$addChild(node)
+            }
+            else
+            {
+                child$type = "folder"
+                private$addBranch(child, node$getFirstChild())
+            }
+        }
+    ),
+
+    cloneable = FALSE
+)
+
+TreeNode <- R6::R6Class(
+
+    "TreeNode",
+
+    public = list(
+
+        name         = NULL,
+        relativePath = NULL,
+        size         = NULL,
+        children     = NULL,
+        parent       = NULL,
+        type         = NULL,
+
+        initialize = function(name, type, size)
+        {
+            self$name <- name
+            self$type <- type
+            self$size <- size
+            self$children <- list()
+        },
+
+        addChild = function(node)
+        {
+            self$children <- c(self$children, node)
+            node$setParent(self)
+            self
+        },
+
+        setParent = function(parent)
+        {
+            self$parent = parent
+        },
+
+        getChild = function(childName)
+        {
+            for(child in self$children)
+            {
+                if(childName == child$name)
+                    return(child)
+            }
+
+            return(NULL)
+        },
+
+        hasChildren = function()
+        {
+            if(length(self$children) != 0)
+                return(TRUE)
+            else
+                return(FALSE)
+        },
+
+        getFirstChild = function()
+        {
+            if(!self$hasChildren())
+                return(NULL)
+            else
+                return(self$children[[1]])
+        }
+
+    ),
+
+    cloneable = FALSE
+)