Skip to content

Commit

Permalink
Add MFile::relativize method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tara Drwenski committed Sep 30, 2024
1 parent eefcea5 commit 82b1fb3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cdm/core/src/main/java/thredds/filesystem/MFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,12 @@ public File getFile() {
public MFileOS getChild(String newFilename) {
return new MFileOS(new File(file, newFilename));
}

@Override
public String relativize(MFile other) {
if (other instanceof MFileOS) {
return file.toPath().relativize(((MFileOS) other).getFile().toPath()).toString();
}
throw new IllegalArgumentException("Cannot relativize " + other + " against " + this);
}
}
8 changes: 8 additions & 0 deletions cdm/core/src/main/java/thredds/filesystem/MFileOS7.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,12 @@ public MFileOS7 getChild(String newFilename) {
public Path getNioPath() {
return path;
}

@Override
public String relativize(MFile other) {
if (other instanceof MFileOS7) {
return path.relativize(((MFileOS7) other).path).toString();
}
throw new IllegalArgumentException("Cannot relativize " + other + " against " + this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
public MFileRemote getChild(String newFilename) {
throw new UnsupportedOperationException("MFileRemote::getChild not implemented. Filename: " + getName());
}

@Override
public String relativize(MFile other) {
throw new UnsupportedOperationException("MFileRemote::relativize not implemented. Filename: " + getName());
}
}

///////////////////////////////
Expand Down
8 changes: 8 additions & 0 deletions cdm/core/src/main/java/thredds/inventory/MFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,12 @@ default boolean isReadable() {
*/
@Nullable
MFile getChild(String newFileName);

/**
* Construct a relative path between this MFile and a given MFile.
*
* @param other the MFile to relativize against this MFile's path
* @return the resulting relative path as a String, or an empty path if both paths are equal
*/
String relativize(MFile other);
}
26 changes: 26 additions & 0 deletions cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -382,6 +383,31 @@ public MFileS3 getChild(String newFilename) {
}
}

/**
* Construct a relative key path (as if it were a file path) between this MFile and a given MFile.
* They must have same delimiter and bucket, otherwise the path returned is empty.
*
* @param other the MFile to relativize against this MFile's path
* @return the resulting relative path as a String, or an empty path if both paths are equal
*/
@Override
public String relativize(MFile other) {
if (!(other instanceof MFileS3)) {
throw new IllegalArgumentException("Cannot relativize " + other + " against " + this);
}
final MFileS3 otherS3 = (MFileS3) other;

if (getDelimiter() != null && getDelimiter().equals("/") && getDelimiter().equals(otherS3.getDelimiter())
&& cdmS3Uri.getBucket().equals(otherS3.cdmS3Uri.getBucket())) {
final String key = getKey();
final String otherKey = otherS3.getKey();
return key == null || otherKey == null ? ""
: Paths.get("/" + key).relativize(Paths.get("/" + otherKey)).toString();
}

return "";
}

public static class Provider implements MFileProvider {

private static String protocol = CdmS3Uri.SCHEME_CDM_S3;
Expand Down
5 changes: 5 additions & 0 deletions cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public MFileZip getChild(String newFilename) {
throw new UnsupportedOperationException("MFileZip::getChild not implemented. Filename: " + getName());
}

@Override
public String relativize(MFile other) {
throw new UnsupportedOperationException("MFileZip::relativize not implemented. Filename: " + getName());
}

public Path getRootPath() {
return rootPath;
}
Expand Down
5 changes: 5 additions & 0 deletions grib/src/main/java/ucar/nc2/grib/collection/GcMFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
public GcMFile getChild(String newFilename) {
throw new UnsupportedOperationException("GcMFile::getChild not implemented. Filename: " + getName());
}

@Override
public String relativize(MFile other) {
throw new UnsupportedOperationException("GcMFile::relativize not implemented. Filename: " + getName());
}
}

0 comments on commit 82b1fb3

Please sign in to comment.