Skip to content

Commit

Permalink
Add MFile::resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
tdrwenski committed Apr 26, 2023
1 parent 3524a0e commit ccb0323
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cdm/core/src/main/java/thredds/filesystem/MFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
public File getFile() {
return file;
}

@Override
public MFileOS resolve(String newFilename) {
return new MFileOS(new File(file, newFilename));
}
}
5 changes: 5 additions & 0 deletions cdm/core/src/main/java/thredds/filesystem/MFileOS7.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
}
}

@Override
public MFileOS7 resolve(String newFilename) {
throw new UnsupportedOperationException("MFileOS7::resolve not implemented. Filename: " + getName());
}

public Path getNioPath() {
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ public void writeToStream(OutputStream outputStream) {
public void writeToStream(OutputStream outputStream, long offset, long maxBytes) {
throw new UnsupportedOperationException("Writing MFileRemote not implemented. Filename: " + getName());
}

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

///////////////////////////////
Expand Down
10 changes: 10 additions & 0 deletions cdm/core/src/main/java/thredds/inventory/MFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.Nullable;

/**
* An abstraction for java.io.File / java.nio.file.Path
Expand Down Expand Up @@ -95,4 +96,13 @@ default boolean isReadable() {
* @param maxBytes the maximum number of bytes to copy
*/
void writeToStream(OutputStream outputStream, long offset, long maxBytes) throws IOException;

/**
* Resolve a new MFile in the same location as this MFile
*
* @param newFileName the relative file path of the new MFile
* @return the new MFile or null if the file can't be resolved
*/
@Nullable
MFile resolve(String newFileName);
}
9 changes: 9 additions & 0 deletions cdm/core/src/test/java/thredds/filesystem/TestMFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public void shouldGetInputStream() throws IOException {
assertThat(inputStream.read()).isNotEqualTo(-1);
}
}

@Test
public void shouldResolveNewMFile() {
final MFileOS mFile = new MFileOS(tempFolder.getRoot() + "/testFile");
final MFileOS newMFile = mFile.resolve("newFile");
assertThat(newMFile.getName()).isEqualTo("newFile");
assertThat(newMFile.getParent().getPath()).isEqualTo(mFile.getPath());
assertThat(newMFile.getPath()).isEqualTo(tempFolder.getRoot() + "/testFile/newFile");
}
}

private static File createTemporaryFile(int size) throws IOException {
Expand Down
13 changes: 13 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 @@ -8,6 +8,7 @@
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -367,6 +368,18 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
}
}

@Nullable
@Override
public MFileS3 resolve(String newFilename) {
final String newKey = cdmS3Uri.getKey().map(k -> k + newFilename).orElse(newFilename);

try {
return new MFileS3(cdmS3Uri.resolveNewKey(newKey));
} catch (URISyntaxException e) {
return null;
}
}

public static class Provider implements MFileProvider {

private static String protocol = CdmS3Uri.SCHEME_CDM_S3;
Expand Down
36 changes: 36 additions & 0 deletions cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,42 @@ public void shouldReturnFalseForNonExistentBucket() throws IOException {
assertThat(bucketWithoutDelimiter.exists()).isEqualTo(false);
}

@Test
public void shouldResolveNewMFileFromBucket() throws IOException {
final MFileS3 withDelimiter = new MFileS3("cdms3:bucket" + DELIMITER_FRAGMENT);
final MFileS3 newMFileWithDelimiter = withDelimiter.resolve("newKey");
assertThat(newMFileWithDelimiter).isNotNull();
assertThat(newMFileWithDelimiter.getPath()).isEqualTo("cdms3:bucket?newKey" + DELIMITER_FRAGMENT);

final MFileS3 withoutDelimiter = new MFileS3("cdms3:bucket");
final MFileS3 newMFileWithoutDelimiter = withoutDelimiter.resolve("newKey");
assertThat(newMFileWithoutDelimiter).isNotNull();
assertThat(newMFileWithoutDelimiter.getPath()).isEqualTo("cdms3:bucket?newKey");
}

@Test
public void shouldResolveNewMFileFromBucketAndKey() throws IOException {
final MFileS3 withDelimiterWithSlash = new MFileS3("cdms3:bucket?key/" + DELIMITER_FRAGMENT);
final MFileS3 newMFileWithDelimiterWithSlash = withDelimiterWithSlash.resolve("newKey");
assertThat(newMFileWithDelimiterWithSlash).isNotNull();
assertThat(newMFileWithDelimiterWithSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey" + DELIMITER_FRAGMENT);

final MFileS3 withoutDelimiterWithSlash = new MFileS3("cdms3:bucket?key/");
final MFileS3 newMFileWithoutDelimiterWithSlash = withoutDelimiterWithSlash.resolve("newKey");
assertThat(newMFileWithoutDelimiterWithSlash).isNotNull();
assertThat(newMFileWithoutDelimiterWithSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey");

final MFileS3 withDelimiterWithoutSlash = new MFileS3("cdms3:bucket?key" + DELIMITER_FRAGMENT);
final MFileS3 newMFileWithDelimiterWithoutSlash = withDelimiterWithoutSlash.resolve("newKey");
assertThat(newMFileWithDelimiterWithoutSlash).isNotNull();
assertThat(newMFileWithDelimiterWithoutSlash.getPath()).isEqualTo("cdms3:bucket?keynewKey" + DELIMITER_FRAGMENT);

final MFileS3 withoutDelimiterWithoutSlash = new MFileS3("cdms3:bucket?key");
final MFileS3 newMFileWithoutDelimiterWithoutSlash = withoutDelimiterWithoutSlash.resolve("newKey");
assertThat(newMFileWithoutDelimiterWithoutSlash).isNotNull();
assertThat(newMFileWithoutDelimiterWithoutSlash.getPath()).isEqualTo("cdms3:bucket?keynewKey");
}

@Test
public void shouldGetInputStream() throws IOException {
final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1);
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 @@ -174,6 +174,11 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
"Writing MFileZip with a byte range to stream not implemented. Filename: " + getName());
}

@Override
public MFileZip resolve(String newFilename) {
throw new UnsupportedOperationException("MFileZip::resolve 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 @@ -136,4 +136,9 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
IO.copyRafB(randomAccessFile, offset, maxBytes, outputStream);
}
}

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

0 comments on commit ccb0323

Please sign in to comment.