Class Location

java.lang.Object
io.trino.filesystem.Location

public final class Location extends Object
Location of a file or directory in a blob or hierarchical file system. The location uses the URI like format scheme://[userInfo@]host[:port][/path], but does not follow the format rules of a URI or URL which support escapes and other special characters.

Alternatively, a location can be specified as /path for usage with legacy HDFS installations, or as file:/path for local file systems as returned by File.toURI().

The API of this class is very limited, so blob storage locations can be used as well. Specifically, methods are provided to get the name of a file location, get the parent of a location, append a path to a location, and parse a location. This allows for the operations needed for analysing data in an object store where you need to create subdirectories, and get peers of a file. Specifically, walking up a path is discouraged as some blob locations have invalid inner path parts.

  • Method Details

    • of

      public static Location of(String location)
    • scheme

      public Optional<String> scheme()
      Returns the scheme of the location, if present. If the scheme is present, the value will not be an empty string. Legacy HDFS paths do not have a scheme.
    • userInfo

      public Optional<String> userInfo()
      Returns the user info of the location, if present. The user info will be present if the location authority contains an at sign, but the value may be an empty string.
    • host

      public Optional<String> host()
      Returns the host of the location, if present. If the host is present, the value will not be an empty string.
    • port

      public OptionalInt port()
    • path

      public String path()
      Returns the path of the location. The path will not start with a slash, and might be empty.
    • fileName

      public String fileName()
      Returns the file name of the location. The location must be a valid file location. The file name is all characters after the last slash in the path.
      Throws:
      IllegalStateException - if the location is not a valid file location
    • sibling

      public Location sibling(String name)
      Returns a new location with the same parent directory as the current location, but with the filename corresponding to the specified name. The location must be a valid file location.
    • parentDirectory

      public Location parentDirectory()
      Creates a new location with all characters removed after the last slash in the path. This should only be used once, as recursive calls for blob paths may lead to incorrect results.
      Throws:
      IllegalStateException - if the location is not a valid file location
    • appendPath

      public Location appendPath(String newPathElement)
      Creates a new location by appending the given path element to the current path. A slash will be added between the current path and the new path element if needed.
      Throws:
      IllegalArgumentException - if the new path element is empty or starts with a slash
    • appendSuffix

      public Location appendSuffix(String suffix)
      Creates a new location by appending the given suffix to the current path. Typical usage for this method is to append a file extension to a file name, but it may be used to append anything, including a slash.

      Use appendPath(String) instead of this method to append a path element.

    • verifyValidFileLocation

      public void verifyValidFileLocation()
      Verifies the location is valid for a file reference. Specifically, the path must not be empty and must not end with a slash.
      Throws:
      IllegalStateException - if the location is not a valid file location
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Return the original location string.
      Overrides:
      toString in class Object