Skip to content

Commit

Permalink
This closes #2754
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalperi committed Apr 28, 2017
2 parents ccfff5f + c1ceab7 commit a8a04a2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Paths;
import java.util.Objects;
import java.util.UUID;
import javax.annotation.Nullable;
import org.apache.beam.sdk.io.fs.ResolveOptions;
import org.apache.beam.sdk.io.fs.ResolveOptions.StandardResolveOptions;
import org.apache.beam.sdk.io.fs.ResourceId;
Expand Down Expand Up @@ -92,6 +93,12 @@ public LocalResourceId getCurrentDirectory() {
}
}

@Override
@Nullable public String getFilename() {
Path fileName = getPath().getFileName();
return fileName == null ? null : fileName.toString();
}

private LocalResourceId resolveLocalPath(String other, ResolveOptions resolveOptions) {
return new LocalResourceId(
getPath().resolve(other),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.beam.sdk.io.fs;

import java.io.Serializable;
import javax.annotation.Nullable;
import org.apache.beam.sdk.io.FileSystem;
import org.apache.beam.sdk.io.FileSystems;
import org.apache.beam.sdk.io.fs.ResolveOptions.StandardResolveOptions;
Expand Down Expand Up @@ -100,6 +101,16 @@ public interface ResourceId extends Serializable {
*/
String getScheme();


/**
* Returns the name of the file or directory denoted by this {@code ResourceId}. The file name
* is the farthest element from the root in the directory hierarchy.
*
* @return a string representing the name of file or directory, or null if there are zero
* components.
*/
@Nullable String getFilename();

/**
* Returns the string representation of this {@link ResourceId}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ public void testToString() throws Exception {
assertThat(dirResource.toString(), startsWith(tmpFolder.getRoot().getAbsolutePath()));
}

@Test
public void testGetFilename() throws Exception {
assertEquals(toResourceIdentifier("/").getFilename(), null);
assertEquals(toResourceIdentifier("/root/tmp").getFilename(),
"tmp");
assertEquals(toResourceIdentifier("/root/tmp/").getFilename(),
"tmp");
assertEquals(toResourceIdentifier("/root/tmp/xyz.txt").getFilename(),
"xyz.txt");
}

private LocalResourceId toResourceIdentifier(String str) throws Exception {
boolean isDirectory;
if (SystemUtils.IS_OS_WINDOWS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import javax.annotation.Nullable;
import org.apache.beam.sdk.io.fs.ResolveOptions;
import org.apache.beam.sdk.io.fs.ResolveOptions.StandardResolveOptions;
import org.apache.beam.sdk.io.fs.ResourceId;
Expand Down Expand Up @@ -91,6 +92,16 @@ public String getScheme() {
return "gs";
}

@Override
@Nullable public String getFilename() {
if (gcsPath.getNameCount() <= 1) {
return null;
} else {
GcsPath gcsFilename = gcsPath.getFileName();
return gcsFilename == null ? null : gcsFilename.toString();
}
}

GcsPath getGcsPath() {
return gcsPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ public void testEquals() throws Exception {
toResourceIdentifier("gs://my_bucket/tmp/"));
}

@Test
public void testGetFilename() throws Exception {
assertEquals(toResourceIdentifier("gs://my_bucket/").getFilename(), null);
assertEquals(toResourceIdentifier("gs://my_bucket/abc").getFilename(),
"abc");
assertEquals(toResourceIdentifier("gs://my_bucket/abc/").getFilename(),
"abc");
assertEquals(toResourceIdentifier("gs://my_bucket/abc/xyz.txt").getFilename(),
"xyz.txt");
}

private GcsResourceId toResourceIdentifier(String str) throws Exception {
return GcsResourceId.fromGcsPath(GcsPath.fromUri(str));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public ResourceId getCurrentDirectory() {
public String getScheme() {
throw new UnsupportedOperationException();
}

@Override
public String getFilename() {
throw new UnsupportedOperationException();
}
}

0 comments on commit a8a04a2

Please sign in to comment.