github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/hadoopfs/src/main/java/io/lakefs/utils/ObjectLocation.java (about)

     1  package io.lakefs.utils;
     2  
     3  import io.lakefs.Constants;
     4  
     5  import org.apache.hadoop.fs.Path;
     6  
     7  import javax.annotation.Nonnull;
     8  import java.net.URI;
     9  
    10  public class ObjectLocation {
    11      private String scheme;
    12      private String repository;
    13      private String ref;
    14      private String path;
    15  
    16      /**
    17       * Returns Location with repository, ref and path used by lakeFS based on filesystem path.
    18       *
    19       * @param workingDirectory used if path is local.
    20       * @param path to extract information from.
    21       * @return lakeFS Location with repository, ref and path
    22       */
    23      @Nonnull
    24      static public ObjectLocation pathToObjectLocation(Path workingDirectory, Path path) {
    25          if (!path.isAbsolute()) {
    26              if (workingDirectory == null) {
    27                  throw new IllegalArgumentException(String.format("cannot expand local path %s with null workingDirectory", path));
    28              }
    29              path = new Path(workingDirectory, path);
    30          }
    31  
    32          URI uri = path.toUri();
    33          ObjectLocation loc = new ObjectLocation();
    34          loc.setScheme(uri.getScheme());
    35          loc.setRepository(uri.getHost());
    36          // extract ref and rest of the path after removing the '/' prefix
    37          String s = StringUtils.trimLeadingSlash(uri.getPath());
    38          int i = s.indexOf(Constants.SEPARATOR);
    39          if (i == -1) {
    40              loc.setRef(s);
    41              loc.setPath("");
    42          } else {
    43              loc.setRef(s.substring(0, i));
    44              loc.setPath(s.substring(i + 1));
    45          }
    46          return loc;
    47      }
    48  
    49      /**
    50       * Returns Location with repository, ref and path used by lakeFS based on filesystem path.
    51       *
    52       * @param path to extract information from.
    53       * @return lakeFS Location with repository, ref and path
    54       */
    55      @Nonnull
    56      static public ObjectLocation pathToObjectLocation(Path path) {
    57          return pathToObjectLocation(null, path);
    58      }
    59  
    60      public static String formatPath(String scheme, String repository, String ref, String path) {
    61          String ret = formatPath(scheme, repository, ref);
    62          if (path != null) ret = ret + "/" + path;
    63          return ret;
    64      }
    65  
    66  
    67      public static String formatPath(String scheme, String repository, String ref) {
    68          return String.format("%s://%s/%s", scheme, repository, ref);
    69      }
    70  
    71      public String getScheme() {
    72          return scheme;
    73      }
    74  
    75      public void setScheme(String scheme) {
    76          this.scheme = scheme;
    77      }
    78  
    79      public ObjectLocation() {
    80      }
    81  
    82      public ObjectLocation(String scheme, String repository, String ref) {
    83          this.scheme = scheme;
    84          this.repository = repository;
    85          this.ref = ref;
    86      }
    87  
    88      public ObjectLocation(String scheme, String repository, String ref, String path) {
    89          this.scheme = scheme;
    90          this.repository = repository;
    91          this.ref = ref;
    92          this.path = path;
    93      }
    94  
    95      public ObjectLocation clone() {
    96          return new ObjectLocation(scheme, repository, ref, path);
    97      }
    98  
    99      public ObjectLocation getParent() {
   100          if (this.path == null) {
   101              return null;
   102          }
   103          Path parentPath = new Path(this.path).getParent();
   104          if (parentPath == null) {
   105              return null;
   106          }
   107          return new ObjectLocation(scheme, repository, ref, parentPath.toString());
   108      }
   109  
   110      public String getRepository() {
   111          return repository;
   112      }
   113  
   114      public void setRepository(String repository) {
   115          this.repository = repository;
   116      }
   117  
   118      public String getRef() {
   119          return ref;
   120      }
   121  
   122      public void setRef(String ref) {
   123          this.ref = ref;
   124      }
   125  
   126      public String getPath() {
   127          return path;
   128      }
   129  
   130      public void setPath(String path) {
   131          this.path = path;
   132      }
   133  
   134      public boolean isValidPath() {
   135          return !this.repository.isEmpty() &&
   136                  !this.ref.isEmpty() &&
   137                  !this.path.isEmpty();
   138      }
   139  
   140      @Override
   141      public boolean equals(Object obj) {
   142          if (obj == this) {
   143              return true;
   144          }
   145          if (!(obj instanceof ObjectLocation)) {
   146              return false;
   147          }
   148  
   149          ObjectLocation objLoc = (ObjectLocation) obj;
   150          return this.repository.equals(objLoc.getRepository()) &&
   151                  this.ref.equals(objLoc.getRef()) && this.path.equals(objLoc.getPath());
   152      }
   153  
   154      /**
   155       * Checks if an ObjectLocation is on the same branch.
   156       *
   157       * @param otherObjLoc the objectLocation to compare
   158       * @return true if the object location is on same branch, false otherwise
   159       */
   160      public boolean onSameBranch(ObjectLocation otherObjLoc) {
   161          return this.scheme.equals(otherObjLoc.getScheme()) &&
   162                  this.repository.equals(otherObjLoc.getRepository()) &&
   163                  this.ref.equals(otherObjLoc.getRef());
   164      }
   165  
   166      @Override
   167      public String toString() {
   168          return formatPath(scheme, repository, ref, path);
   169      }
   170  
   171      public String toRefString() {
   172          return formatPath(scheme, repository, ref);
   173      }
   174  
   175      public ObjectLocation toDirectory() {
   176          return new ObjectLocation(scheme, repository, ref, StringUtils.addLeadingSlash(path));
   177      }
   178  
   179      public Path toFSPath() {
   180          return new Path(this.toString());
   181      }
   182  }