Implemented feature to get size of ArvadosFile or Subcollection
authorFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 12 Dec 2017 15:01:08 +0000 (16:01 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 12 Dec 2017 15:01:08 +0000 (16:01 +0100)
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/ArvadosSDK_0.1.0_R_x86_64-pc-linux-gnu.tar.gz [new file with mode: 0644]
sdk/R/.RData
sdk/R/R/ArvadosFile.R
sdk/R/R/Collection.R
sdk/R/R/HttpParser.R
sdk/R/R/Subcollection.R
sdk/R/README

diff --git a/sdk/ArvadosSDK_0.1.0_R_x86_64-pc-linux-gnu.tar.gz b/sdk/ArvadosSDK_0.1.0_R_x86_64-pc-linux-gnu.tar.gz
new file mode 100644 (file)
index 0000000..aa802dd
Binary files /dev/null and b/sdk/ArvadosSDK_0.1.0_R_x86_64-pc-linux-gnu.tar.gz differ
index b08e7751163f390ab6a4f6f0dcf90db8334fcf84..88c2ce75b9b08998c18c1640036216231fb8c56b 100644 (file)
Binary files a/sdk/R/.RData and b/sdk/R/.RData differ
index f7c45dcc206c6a2d7d01fd33b23722f22010e0d8..ce3a6fff5973f60256c5256ac59daf7881ad5f10 100644 (file)
@@ -9,9 +9,10 @@ ArvadosFile <- R6::R6Class(
 
     public = list(
 
-        initialize = function(name, relativePath, api, collection)
+        initialize = function(name, relativePath, size, api, collection)
         {
             private$name         <- name
+            private$size         <- size
             private$relativePath <- relativePath
             private$api          <- api
             private$collection   <- collection
@@ -23,6 +24,8 @@ ArvadosFile <- R6::R6Class(
 
         getRelativePath = function() private$relativePath,
 
+        getSizeInBytes = function() private$size,
+
         read = function(offset = 0, length = 0)
         {
             if(offset < 0 || length < 0)
@@ -50,6 +53,7 @@ ArvadosFile <- R6::R6Class(
 
         name         = NULL,
         relativePath = NULL,
+        size         = NULL,
         parent       = NULL,
         api          = NULL,
         collection   = NULL,
index fed222fdda9c994a4914f4ebc594dc0a1ae8362f..14371a69a93088b98d360c6e6b485258a40616d9 100644 (file)
@@ -67,13 +67,17 @@ Collection <- R6::R6Class(
             private$fileTree <- private$generateTree(private$fileItems)
         },
 
-        printFileContent = function(pretty = TRUE)
+        printFileContent = function()
         {
-            if(pretty)
-                private$fileTree$printContent(0)
-            else
-                print(private$fileItems)
+            private$fileTree$printContent(0)
+        },
 
+        getFileContent = function()
+        {
+            sapply(private$fileItems, function(file)
+            {
+                file$name
+            })
         },
 
         get = function(relativePath)
@@ -111,8 +115,8 @@ Collection <- R6::R6Class(
     
     private = list(
 
-        api       = NULL,
         fileItems = NULL,
+        api       = NULL,
         fileTree  = NULL,
 
         createSubcollectionTree = function(treeNode)
@@ -132,23 +136,12 @@ Collection <- R6::R6Class(
             else
             {
                 if(treeNode$type == "file")
-                    return(ArvadosFile$new(treeNode$name, treeNode$relativePath, private$api, self))
-                else if(treeNode$type == "folder" || treeNode$type == "root")
+                    return(ArvadosFile$new(treeNode$name, treeNode$relativePath, treeNode$size, private$api, self))
+                else 
                     return(Subcollection$new(treeNode$name, treeNode$relativePath, NULL))
             }
         },
 
-        createSubcollectionFromNode = function(treeNode, children)
-        {
-            subcollection = NULL
-            if(treeNode$type == "file")
-                subcollection = ArvadosFile$new(treeNode$name, treeNode$relativePath)
-            else if(treeNode$type == "folder" || treeNode$type == "root")
-                subcollection = Subcollection$new(treeNode$name, treeNode$relativePath, children)
-            
-            subcollection
-        },
-
         getCollectionContent = function()
         {
             #TODO(Fudo): Use proper URL here.
@@ -170,14 +163,12 @@ Collection <- R6::R6Class(
         {
             treeBranches <- sapply(collectionContent, function(filePath)
             {
-                splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
+                splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE))
 
-                pathEndsWithSlash <- substr(filePath, nchar(filePath), nchar(filePath)) == "/"
-                
-                branch = private$createBranch(splitPath, pathEndsWithSlash)      
+                branch = private$createBranch(splitPath, filePath$fileSize)      
             })
 
-            root <- TreeNode$new("./", "root")
+            root <- TreeNode$new("./", "root", NULL)
             root$relativePath = ""
 
             sapply(treeBranches, function(branch)
@@ -188,31 +179,27 @@ Collection <- R6::R6Class(
             root
         },
 
-        createBranch = function(splitPath, pathEndsWithSlash)
+        createBranch = function(splitPath, fileSize)
         {
             branch <- NULL
             lastElementIndex <- length(splitPath)
-            
-            lastElementInPathType = "file"
-            if(pathEndsWithSlash)
-                lastElementInPathType = "folder"
 
             for(elementIndex in lastElementIndex:1)
             {
                 if(elementIndex == lastElementIndex)
                 {
-                    branch = TreeNode$new(splitPath[[elementIndex]], lastElementInPathType)
+                    branch = TreeNode$new(splitPath[[elementIndex]], "file", fileSize)
                 }
                 else
                 {
-                    newFolder = TreeNode$new(splitPath[[elementIndex]], "folder")
+                    newFolder = TreeNode$new(splitPath[[elementIndex]], "folder", NULL)
                     newFolder$addChild(branch)
                     branch = newFolder
                 }
 
                 branch$relativePath <- paste(unlist(splitPath[1:elementIndex]), collapse = "/")
             }
-
+            
             branch
         },
 
@@ -226,6 +213,7 @@ Collection <- R6::R6Class(
             }
             else
             {
+                child$type = "folder"
                 private$addNode(child, node$getFirstChild())
             }
         },
@@ -266,19 +254,18 @@ TreeNode <- R6::R6Class(
 
     public = list(
 
-        name = NULL,
+        name         = NULL,
         relativePath = NULL,
-        children = NULL,
-        parent = NULL,
-        type = NULL,
+        size         = NULL,
+        children     = NULL,
+        parent       = NULL,
+        type         = NULL,
 
-        initialize = function(name, type)
+        initialize = function(name, type, size)
         {
-            if(type == "folder")
-                name <- paste0(name, "/")
-
             self$name <- name
             self$type <- type
+            self$size <- size
             self$children <- list()
         },
 
@@ -324,7 +311,10 @@ TreeNode <- R6::R6Class(
         printContent = function(depth)
         {
             indentation <- paste(rep("....", depth), collapse = "")
-            print(paste0(indentation, self$name))
+            if(self$type == "folder")
+                print(paste0(indentation, self$name, "/"))
+            else
+                print(paste0(indentation, self$size))
             
             for(child in self$children)
                 child$printContent(depth + 1)
index d54207ea6f4a4c6fa8ec18904084be7cdc235b20..9129bbc66b5e25771d501eb489fcc4353b3e2475 100644 (file)
@@ -18,17 +18,20 @@ HttpParser <- R6::R6Class(
         parseWebDAVResponse = function(response, uri)
         {
             text <- rawToChar(response$content)
-            print(text)
             doc <- XML::xmlParse(text, asText=TRUE)
 
             # calculate relative paths
             base <- paste(paste("/", strsplit(uri, "/")[[1]][-1:-3], sep="", collapse=""), "/", sep="")
-            result <- unlist(
-                XML::xpathApply(doc, "//D:response/D:href", function(node) {
-                    sub(base, "", URLdecode(XML::xmlValue(node)), fixed=TRUE)
-                })
-            )
-            result <- result[result != ""]
+            result <- XML::xpathApply(doc, "//D:response", function(node) {
+                result = list()
+                children = xmlChildren(node)
+
+                result$name = sub(base, "", URLdecode(XML::xmlValue(children$href)), fixed=TRUE)
+                sizeXMLNode = xmlChildren(xmlChildren(children$propstat)$prop)$getcontentlength
+                result$fileSize = as.numeric(xmlValue(sizeXMLNode))
+
+                result
+            })
 
             result[-1]
         }
index 4053546fcb2b5171c2cd505872ba16986bb169d4..1d8c8a77d9a473c0a772e42fc05f812d42b84f2f 100644 (file)
@@ -20,6 +20,15 @@ Subcollection <- R6::R6Class(
 
         getRelativePath = function() private$relativePath,
 
+        getSizeInBytes = function()
+        {
+            overallSize = 0
+            for(child in private$children)
+                overallSize = overallSize + child$getSizeInBytes()
+
+            overallSize
+        },
+
         setParent = function(parent) private$parent <- parent
     ),
 
index 6f4e31ebb6597af2659ec678af72a6d191c716e1..344deab78d09d11e18c46a391246e993c6760a8a 100644 (file)
@@ -55,13 +55,15 @@ collection <- Collection$new(arv, "uuid")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Print content of the collection
+#Print content of the collection (directory/folder tree structure)
 
 collection$printFileContent()
 
-#of if you just want a list of relative paths:
+--------------------------------------------------------------------------------------------------------------------------------
+
+#Get file/folder content as character vector
 
-collection$printFileContent(pretty = FALSE)
+collection$getFileContent()
 
 --------------------------------------------------------------------------------------------------------------------------------
 
@@ -77,6 +79,13 @@ arvadosSubcollection <- collection.get("location/to/my/directory/")
 
 #Read whole file or just a portion of it.
 
-arvadosFile$read(offset = 1024, length = 512)
+fileContent <- arvadosFile$read(offset = 1024, length = 512)
+
+--------------------------------------------------------------------------------------------------------------------------------
+
+#Get ArvadosFile or Subcollection size
+
+size <- arvadosFile$getSizeInBytes()
+size <- arvadosSubcollection$getSizeInBytes()
 
 --------------------------------------------------------------------------------------------------------------------------------