Class PathUtils


  • @ThreadSafe
    public final class PathUtils
    extends java.lang.Object
    Utilities related to both Alluxio paths like AlluxioURI and local file paths.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String cleanPath​(java.lang.String path)
      Checks and normalizes the given path.
      static java.lang.String concatPath​(java.lang.Object base, java.lang.Object path)
      Joins two path elements, separated by AlluxioURI.SEPARATOR.
      static java.lang.String concatPath​(java.lang.Object base, java.lang.Object... paths)
      Joins each element in paths in order, separated by AlluxioURI.SEPARATOR.
      static java.lang.String concatUfsPath​(java.lang.String base, java.lang.String path)
      Join two path elements for ufs, separated by AlluxioURI.SEPARATOR.
      static AlluxioURI findLowestCommonAncestor​(java.util.Collection<AlluxioURI> paths)  
      static java.lang.String[] getCleanedPathComponents​(java.lang.String path)
      Get the components of a path that has already been cleaned.
      static java.lang.String getParent​(java.lang.String path)
      Gets the parent of the file at a path.
      static java.lang.String getParentCleaned​(java.lang.String cleanedPath)
      The same as getParent(java.lang.String) except does not clean the path before getting the parent.
      static java.lang.String[] getPathComponents​(java.lang.String path)
      Gets the path components of the given path.
      static java.lang.String getPermanentFileName​(java.lang.String path)  
      static java.lang.String getPersistentTmpPath​(AlluxioConfiguration ufsConfiguration, java.lang.String path)
      Get temp path for async persistence job.
      static boolean hasPrefix​(java.lang.String path, java.lang.String prefix)
      Checks whether the given path contains the given prefix.
      static boolean isRoot​(java.lang.String path)
      Checks if the given path is the root.
      static boolean isTemporaryFileName​(java.lang.String path)
      Determines whether the given path is a temporary file name generated by Alluxio.
      static java.lang.String normalizePath​(java.lang.String path, java.lang.String separator)
      Adds a trailing separator if it does not exist in path.
      static java.lang.String subtractPaths​(java.lang.String path, java.lang.String prefix)
      Removes the prefix from the path, yielding a relative path from the second path to the first.
      static java.lang.String temporaryFileName​(long nonce, java.lang.String path)
      Generates a deterministic temporary file name for the a path and a file id and a nonce.
      static java.lang.String uniqPath()
      Creates a unique path based off the caller.
      static void validatePath​(java.lang.String path)
      Checks if the given path is properly formed.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • cleanPath

        public static java.lang.String cleanPath​(java.lang.String path)
                                          throws InvalidPathException
        Checks and normalizes the given path.
        Parameters:
        path - The path to clean up
        Returns:
        a normalized version of the path, with single separators between path components and dot components resolved
        Throws:
        InvalidPathException - if the path is invalid
      • concatPath

        public static java.lang.String concatPath​(java.lang.Object base,
                                                  java.lang.Object path)
        Joins two path elements, separated by AlluxioURI.SEPARATOR.

        Note that empty element in base or paths is ignored.

        Parameters:
        base - base path
        path - path element to concatenate
        Returns:
        joined path
      • concatPath

        public static java.lang.String concatPath​(java.lang.Object base,
                                                  java.lang.Object... paths)
                                           throws java.lang.IllegalArgumentException
        Joins each element in paths in order, separated by AlluxioURI.SEPARATOR.

        For example,

         
         concatPath("/myroot/", "dir", 1L, "filename").equals("/myroot/dir/1/filename");
         concatPath("alluxio://myroot", "dir", "filename").equals("alluxio://myroot/dir/filename");
         concatPath("myroot/", "/dir/", "filename").equals("myroot/dir/filename");
         concatPath("/", "dir", "filename").equals("/dir/filename");
         
         
        Note that empty element in base or paths is ignored.
        Parameters:
        base - base path
        paths - paths to concatenate
        Returns:
        joined path
        Throws:
        java.lang.IllegalArgumentException - if base or paths is null
      • findLowestCommonAncestor

        public static AlluxioURI findLowestCommonAncestor​(java.util.Collection<AlluxioURI> paths)
        Parameters:
        paths - the set of paths
        Returns:
        the lowest common ancestor, or null if paths is null or empty
      • getParent

        public static java.lang.String getParent​(java.lang.String path)
                                          throws InvalidPathException
        Gets the parent of the file at a path.
        Parameters:
        path - The path
        Returns:
        the parent path of the file; this is "/" if the given path is the root
        Throws:
        InvalidPathException - if the path is invalid
      • getParentCleaned

        public static java.lang.String getParentCleaned​(java.lang.String cleanedPath)
                                                 throws InvalidPathException
        The same as getParent(java.lang.String) except does not clean the path before getting the parent.
        Parameters:
        cleanedPath - the path that has been cleaned
        Returns:
        the parent path of the file; this is "/" if the given path is the root
        Throws:
        InvalidPathException - if the path is invalid
      • concatUfsPath

        public static java.lang.String concatUfsPath​(java.lang.String base,
                                                     java.lang.String path)
        Join two path elements for ufs, separated by AlluxioURI.SEPARATOR. For example,
         
         concatUfsPath("s3://myroot/", "filename").equals("s3://myroot/filename");
         concatUfsPath("s3://", "filename").equals("s3://filename");
         
         
        Parameters:
        base - base path
        path - path element to concatenate
        Returns:
        joined path
      • getPersistentTmpPath

        public static java.lang.String getPersistentTmpPath​(AlluxioConfiguration ufsConfiguration,
                                                            java.lang.String path)
        Get temp path for async persistence job.
        Parameters:
        ufsConfiguration - the ufs configuration
        path - ufs path
        Returns:
        ufs temp path with UUID
      • getPathComponents

        public static java.lang.String[] getPathComponents​(java.lang.String path)
                                                    throws InvalidPathException
        Gets the path components of the given path. The first component will be an empty string. "/a/b/c" => {"", "a", "b", "c"} "/" => {""}
        Parameters:
        path - The path to split
        Returns:
        the path split into components
        Throws:
        InvalidPathException - if the path is invalid
      • getCleanedPathComponents

        public static java.lang.String[] getCleanedPathComponents​(java.lang.String path)
                                                           throws InvalidPathException
        Get the components of a path that has already been cleaned.
        Parameters:
        path - the path
        Returns:
        the components
        Throws:
        InvalidPathException
      • subtractPaths

        public static java.lang.String subtractPaths​(java.lang.String path,
                                                     java.lang.String prefix)
                                              throws InvalidPathException
        Removes the prefix from the path, yielding a relative path from the second path to the first. If the paths are the same, this method returns the empty string.
        Parameters:
        path - the full path
        prefix - the prefix to remove
        Returns:
        the path with the prefix removed
        Throws:
        InvalidPathException - if either of the arguments are not valid paths
      • hasPrefix

        public static boolean hasPrefix​(java.lang.String path,
                                        java.lang.String prefix)
                                 throws InvalidPathException
        Checks whether the given path contains the given prefix. The comparison happens at a component granularity; for example, hasPrefix(/dir/file, /dir) should evaluate to true, while hasPrefix(/dir/file, /d) should evaluate to false.
        Parameters:
        path - a path
        prefix - a prefix
        Returns:
        whether the given path has the given prefix
        Throws:
        InvalidPathException - when the path or prefix is invalid
      • isRoot

        public static boolean isRoot​(java.lang.String path)
                              throws InvalidPathException
        Checks if the given path is the root.
        Parameters:
        path - The path to check
        Returns:
        true if the path is the root
        Throws:
        InvalidPathException - if the path is invalid
      • validatePath

        public static void validatePath​(java.lang.String path)
                                 throws InvalidPathException
        Checks if the given path is properly formed.
        Parameters:
        path - The path to check
        Throws:
        InvalidPathException - If the path is not properly formed
      • temporaryFileName

        public static java.lang.String temporaryFileName​(long nonce,
                                                         java.lang.String path)
        Generates a deterministic temporary file name for the a path and a file id and a nonce.
        Parameters:
        nonce - a nonce token
        path - a file path
        Returns:
        a deterministic temporary file name
      • getPermanentFileName

        public static java.lang.String getPermanentFileName​(java.lang.String path)
        Parameters:
        path - the path of the file, possibly temporary
        Returns:
        the permanent path of the file if it was temporary, or the original path if it was not
      • isTemporaryFileName

        public static boolean isTemporaryFileName​(java.lang.String path)
        Determines whether the given path is a temporary file name generated by Alluxio.
        Parameters:
        path - the path to check
        Returns:
        whether the given path is a temporary file name generated by Alluxio
      • uniqPath

        public static java.lang.String uniqPath()
        Creates a unique path based off the caller.
        Returns:
        unique path based off the caller
      • normalizePath

        public static java.lang.String normalizePath​(java.lang.String path,
                                                     java.lang.String separator)
        Adds a trailing separator if it does not exist in path.
        Parameters:
        path - the file name
        separator - trailing separator to add
        Returns:
        updated path with trailing separator