HADOOP-17686. Avoid potential NPE by using Path#getParentPath API in hadoop-huaweicloud (#2990)
Signed-off-by: Takanobu Asanuma <tasanuma@apache.org>
This commit is contained in:
parent
b944084b32
commit
c80f07422f
@ -55,6 +55,7 @@
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@ -872,8 +873,12 @@ private static void getDirectories(final String key, final String sourceKey,
|
||||
directories.add(p.toString());
|
||||
}
|
||||
while (p.compareTo(sourcePath) > 0) {
|
||||
p = p.getParent();
|
||||
if (p.isRoot() || p.compareTo(sourcePath) == 0) {
|
||||
Optional<Path> parent = p.getOptionalParentPath();
|
||||
if (!parent.isPresent()) {
|
||||
break;
|
||||
}
|
||||
p = parent.get();
|
||||
if (p.compareTo(sourcePath) == 0) {
|
||||
break;
|
||||
}
|
||||
directories.add(p.toString());
|
||||
|
@ -24,6 +24,7 @@
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.avro.reflect.Stringable;
|
||||
@ -420,10 +421,27 @@ public String getName() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parent of a path or null if at root.
|
||||
* Returns the parent of a path or null if at root. Better alternative is
|
||||
* {@link #getOptionalParentPath()} to handle nullable value for root path.
|
||||
*
|
||||
* @return the parent of a path or null if at root
|
||||
*/
|
||||
public Path getParent() {
|
||||
return getParentUtil();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parent of a path as {@link Optional} or
|
||||
* {@link Optional#empty()} i.e an empty Optional if at root.
|
||||
*
|
||||
* @return Parent of path wrappen in {@link Optional}.
|
||||
* {@link Optional#empty()} i.e an empty Optional if at root.
|
||||
*/
|
||||
public Optional<Path> getOptionalParentPath() {
|
||||
return Optional.ofNullable(getParentUtil());
|
||||
}
|
||||
|
||||
private Path getParentUtil() {
|
||||
String path = uri.getPath();
|
||||
int lastSlash = path.lastIndexOf('/');
|
||||
int start = startPositionWithoutWindowsDrive(path);
|
||||
|
Loading…
Reference in New Issue
Block a user