|
31 | 31 | import org.apache.hadoop.fs.Path; |
32 | 32 |
|
33 | 33 | import java.io.IOException; |
34 | | -import java.util.ArrayList; |
35 | 34 | import java.util.Arrays; |
36 | 35 | import java.util.Collections; |
37 | | -import java.util.LinkedList; |
38 | 36 | import java.util.List; |
39 | 37 | import java.util.Map; |
| 38 | +import java.util.concurrent.CopyOnWriteArrayList; |
40 | 39 | import java.util.stream.Collectors; |
41 | 40 |
|
42 | 41 | public class FileSystemBackedTableMetadata implements HoodieTableMetadata { |
@@ -64,45 +63,41 @@ public FileStatus[] getAllFilesInPartition(Path partitionPath) throws IOExceptio |
64 | 63 |
|
65 | 64 | @Override |
66 | 65 | public List<String> getAllPartitionPaths() throws IOException { |
| 66 | + FileSystem fs = new Path(datasetBasePath).getFileSystem(hadoopConf.get()); |
67 | 67 | if (assumeDatePartitioning) { |
68 | | - FileSystem fs = new Path(datasetBasePath).getFileSystem(hadoopConf.get()); |
69 | 68 | return FSUtils.getAllPartitionFoldersThreeLevelsDown(fs, datasetBasePath); |
70 | 69 | } |
71 | 70 |
|
72 | | - List<Path> pathsToList = new LinkedList<>(); |
| 71 | + List<Path> pathsToList = new CopyOnWriteArrayList<>(); |
73 | 72 | pathsToList.add(new Path(datasetBasePath)); |
74 | | - List<String> partitionPaths = new ArrayList<>(); |
| 73 | + List<String> partitionPaths = new CopyOnWriteArrayList<>(); |
75 | 74 |
|
76 | 75 | while (!pathsToList.isEmpty()) { |
77 | 76 | // TODO: Get the parallelism from HoodieWriteConfig |
78 | 77 | int listingParallelism = Math.min(DEFAULT_LISTING_PARALLELISM, pathsToList.size()); |
79 | 78 |
|
80 | 79 | // List all directories in parallel |
81 | | - List<Pair<Path, FileStatus[]>> dirToFileListing = engineContext.map(pathsToList, path -> { |
| 80 | + List<FileStatus[]> dirToFileListing = engineContext.map(pathsToList, path -> { |
82 | 81 | FileSystem fileSystem = path.getFileSystem(hadoopConf.get()); |
83 | | - return Pair.of(path, fileSystem.listStatus(path)); |
| 82 | + return fileSystem.listStatus(path); |
84 | 83 | }, listingParallelism); |
85 | 84 | pathsToList.clear(); |
86 | 85 |
|
87 | | - // If the listing reveals a directory, add it to queue. If the listing reveals a hoodie partition, add it to |
88 | | - // the results. |
89 | | - dirToFileListing.forEach(p -> { |
90 | | - Option<FileStatus> partitionMetaFile = Option.fromJavaOptional(Arrays.stream(p.getRight()).parallel() |
91 | | - .filter(fs -> fs.getPath().getName().equals(HoodiePartitionMetadata.HOODIE_PARTITION_METAFILE)) |
92 | | - .findFirst()); |
93 | | - |
94 | | - if (partitionMetaFile.isPresent()) { |
95 | | - // Is a partition. |
96 | | - String partitionName = FSUtils.getRelativePartitionPath(new Path(datasetBasePath), p.getLeft()); |
97 | | - partitionPaths.add(partitionName); |
98 | | - } else { |
99 | | - // Add sub-dirs to the queue |
100 | | - pathsToList.addAll(Arrays.stream(p.getRight()) |
101 | | - .filter(fs -> fs.isDirectory() && !fs.getPath().getName().equals(HoodieTableMetaClient.METAFOLDER_NAME)) |
102 | | - .map(fs -> fs.getPath()) |
103 | | - .collect(Collectors.toList())); |
104 | | - } |
105 | | - }); |
| 86 | + // if current dictionary contains PartitionMetadata, add it to result |
| 87 | + // if current dictionary does not contain PartitionMetadata, add it to queue |
| 88 | + dirToFileListing.stream().flatMap(Arrays::stream).parallel() |
| 89 | + .forEach(fileStatus -> { |
| 90 | + if (fileStatus.isDirectory()) { |
| 91 | + if (HoodiePartitionMetadata.hasPartitionMetadata(fs, fileStatus.getPath())) { |
| 92 | + partitionPaths.add(FSUtils.getRelativePartitionPath(new Path(datasetBasePath), fileStatus.getPath())); |
| 93 | + } else if (!fileStatus.getPath().getName().equals(HoodieTableMetaClient.METAFOLDER_NAME)) { |
| 94 | + pathsToList.add(fileStatus.getPath()); |
| 95 | + } |
| 96 | + } else if (fileStatus.getPath().getName().equals(HoodiePartitionMetadata.HOODIE_PARTITION_METAFILE)) { |
| 97 | + String partitionName = FSUtils.getRelativePartitionPath(new Path(datasetBasePath), fileStatus.getPath().getParent()); |
| 98 | + partitionPaths.add(partitionName); |
| 99 | + } |
| 100 | + }); |
106 | 101 | } |
107 | 102 | return partitionPaths; |
108 | 103 | } |
|
0 commit comments