Skip to content

Commit

Permalink
Support listing directories with provided filter (implements #75)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelmay committed Jul 24, 2022
1 parent f9892ed commit 2328941
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/src/main/java/de/m3y/hadoop/hdfs/hfsa/core/FsImageData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import de.m3y.hadoop.hdfs.hfsa.util.FsUtil;
Expand Down Expand Up @@ -142,6 +143,17 @@ public boolean hasINode(String path) throws IOException {
* @throws IOException on error, e.g. FileNotFoundException if path does not exist.
*/
public List<String> getChildDirectories(String path) throws IOException {
return getChildDirectories(path,childName -> true );
}
/**
* Gets the child directory absolute paths for given path.
*
* @param path the parent directory path.
* @param filter filters the child directory inode (use e.g. inode.getName().toStringUtf8() to get name)
* @return the list of child directory paths.
* @throws IOException on error, e.g. FileNotFoundException if path does not exist.
*/
public List<String> getChildDirectories(String path, Predicate<FsImageProto.INodeSection.INode> filter) throws IOException {
final long parentNodeId = lookupInodeId(path);
long[] children = dirMap.get(parentNodeId);
if (children.length == 0) {
Expand All @@ -154,7 +166,9 @@ public List<String> getChildDirectories(String path) throws IOException {
for (long cid : children) {
final FsImageProto.INodeSection.INode inode = inodes.getInode(cid);
if (FsUtil.isDirectory(inode)) {
childPaths.add(pathWithTrailingSlash + inode.getName().toStringUtf8());
if(filter.test(inode)) {
childPaths.add(pathWithTrailingSlash + inode.getName().toStringUtf8());
}
}
}
return childPaths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ public void testGetChildDirectories() throws IOException {
childPaths = fsImageData.getChildDirectories("/datalake/");
expectedChildPaths = new String[]{"/datalake/asset1", "/datalake/asset2", "/datalake/asset3"};
assertThat(childPaths).containsExactlyInAnyOrder(expectedChildPaths);

// Using name filter
childPaths = fsImageData.getChildDirectories("/datalake/", inode -> inode.getName().toStringUtf8().contains("2") );
expectedChildPaths = new String[]{"/datalake/asset2"};
assertThat(childPaths).containsExactlyInAnyOrder(expectedChildPaths);

childPaths = fsImageData.getChildDirectories("/", inode -> inode.getName().toStringUtf8().startsWith("test") );
expectedChildPaths = new String[]{"/test1", "/test2", "/test3"};
assertThat(childPaths).containsExactlyInAnyOrder(expectedChildPaths);
}

@Test
Expand Down

0 comments on commit 2328941

Please sign in to comment.