Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MFile method to create child MFile #1173

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 getChild(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 getChild(String newFilename) {
throw new UnsupportedOperationException("MFileOS7::getChild 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 getChild(String newFilename) {
throw new UnsupportedOperationException("MFileRemote::getChild 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;

/**
* Get child MFile of 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 getChild(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 shouldGetChildMFile() {
final MFileOS mFile = new MFileOS(tempFolder.getRoot() + "/testFile");
final MFileOS newMFile = mFile.getChild("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
14 changes: 14 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 @@ -368,6 +368,20 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
}
}

@Nullable
@Override
public MFileS3 getChild(String newFilename) {
final String existingKey = cdmS3Uri.getKey().orElse("");
final boolean addDelimiter = delimiter != null && !existingKey.endsWith(delimiter) && !existingKey.isEmpty();
final String newKey = addDelimiter ? existingKey + delimiter + newFilename : existingKey + 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 shouldGetChildMFileFromBucket() throws IOException {
final MFileS3 withDelimiter = new MFileS3("cdms3:bucket" + DELIMITER_FRAGMENT);
final MFileS3 newMFileWithDelimiter = withDelimiter.getChild("newKey");
assertThat(newMFileWithDelimiter).isNotNull();
assertThat(newMFileWithDelimiter.getPath()).isEqualTo("cdms3:bucket?newKey" + DELIMITER_FRAGMENT);

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

@Test
public void shouldGetChildMFileFromBucketAndKey() throws IOException {
final MFileS3 withDelimiterWithSlash = new MFileS3("cdms3:bucket?key/" + DELIMITER_FRAGMENT);
final MFileS3 newMFileWithDelimiterWithSlash = withDelimiterWithSlash.getChild("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.getChild("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.getChild("newKey");
assertThat(newMFileWithDelimiterWithoutSlash).isNotNull();
assertThat(newMFileWithDelimiterWithoutSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey" + DELIMITER_FRAGMENT);

final MFileS3 withoutDelimiterWithoutSlash = new MFileS3("cdms3:bucket?key");
final MFileS3 newMFileWithoutDelimiterWithoutSlash = withoutDelimiterWithoutSlash.getChild("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 getChild(String newFilename) {
throw new UnsupportedOperationException("MFileZip::getChild 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 getChild(String newFilename) {
throw new UnsupportedOperationException("GcMFile::getChild not implemented. Filename: " + getName());
}
}