@@ -413,18 +413,21 @@ protected boolean isFileSliceAfterPendingCompaction(FileSlice fileSlice) {
413413 * base-files.
414414 *
415415 * @param fileSlice File Slice
416+ * @param includeEmptyFileSlice include empty file-slice
416417 */
417- protected FileSlice filterBaseFileAfterPendingCompaction (FileSlice fileSlice ) {
418+ protected Stream < FileSlice > filterBaseFileAfterPendingCompaction (FileSlice fileSlice , boolean includeEmptyFileSlice ) {
418419 if (isFileSliceAfterPendingCompaction (fileSlice )) {
419420 LOG .debug ("File Slice (" + fileSlice + ") is in pending compaction" );
420421 // Base file is filtered out of the file-slice as the corresponding compaction
421422 // instant not completed yet.
422- FileSlice transformed =
423- new FileSlice (fileSlice .getPartitionPath (), fileSlice .getBaseInstantTime (), fileSlice .getFileId ());
423+ FileSlice transformed = new FileSlice (fileSlice .getPartitionPath (), fileSlice .getBaseInstantTime (), fileSlice .getFileId ());
424424 fileSlice .getLogFiles ().forEach (transformed ::addLogFile );
425- return transformed ;
425+ if (transformed .isEmpty () && !includeEmptyFileSlice ) {
426+ return Stream .of ();
427+ }
428+ return Stream .of (transformed );
426429 }
427- return fileSlice ;
430+ return Stream . of ( fileSlice ) ;
428431 }
429432
430433 protected HoodieFileGroup addBootstrapBaseFileIfPresent (HoodieFileGroup fileGroup ) {
@@ -606,9 +609,9 @@ public final Stream<FileSlice> getLatestFileSlices(String partitionStr) {
606609 String partitionPath = formatPartitionKey (partitionStr );
607610 ensurePartitionLoadedCorrectly (partitionPath );
608611 return fetchLatestFileSlices (partitionPath )
609- .filter (slice -> !isFileGroupReplaced (slice .getFileGroupId ()))
610- . map ( this :: filterBaseFileAfterPendingCompaction )
611- .map (this ::addBootstrapBaseFileIfPresent );
612+ .filter (slice -> !isFileGroupReplaced (slice .getFileGroupId ()))
613+ . flatMap ( slice -> this . filterBaseFileAfterPendingCompaction ( slice , true ) )
614+ .map (this ::addBootstrapBaseFileIfPresent );
612615 } finally {
613616 readLock .unlock ();
614617 }
@@ -627,7 +630,10 @@ public final Option<FileSlice> getLatestFileSlice(String partitionStr, String fi
627630 return Option .empty ();
628631 } else {
629632 Option <FileSlice > fs = fetchLatestFileSlice (partitionPath , fileId );
630- return fs .map (this ::filterBaseFileAfterPendingCompaction ).map (this ::addBootstrapBaseFileIfPresent );
633+ if (!fs .isPresent ()) {
634+ return Option .empty ();
635+ }
636+ return Option .ofNullable (filterBaseFileAfterPendingCompaction (fs .get (), true ).map (this ::addBootstrapBaseFileIfPresent ).findFirst ().orElse (null ));
631637 }
632638 } finally {
633639 readLock .unlock ();
@@ -665,13 +671,21 @@ public final Stream<FileSlice> getLatestFileSlicesBeforeOrOn(String partitionStr
665671 readLock .lock ();
666672 String partitionPath = formatPartitionKey (partitionStr );
667673 ensurePartitionLoadedCorrectly (partitionPath );
668- Stream <FileSlice > fileSliceStream = fetchLatestFileSlicesBeforeOrOn (partitionPath , maxCommitTime )
669- .filter (slice -> !isFileGroupReplacedBeforeOrOn (slice .getFileGroupId (), maxCommitTime ));
674+ Stream <Stream <FileSlice >> allFileSliceStream = fetchAllStoredFileGroups (partitionPath )
675+ .filter (slice -> !isFileGroupReplacedBeforeOrOn (slice .getFileGroupId (), maxCommitTime ))
676+ .map (fg -> fg .getAllFileSlicesBeforeOn (maxCommitTime ));
670677 if (includeFileSlicesInPendingCompaction ) {
671- return fileSliceStream .map (this ::filterBaseFileAfterPendingCompaction ).map (this ::addBootstrapBaseFileIfPresent );
678+ return allFileSliceStream .map (sliceStream -> sliceStream .flatMap (slice -> this .filterBaseFileAfterPendingCompaction (slice , false )))
679+ .map (sliceStream -> Option .fromJavaOptional (sliceStream .findFirst ())).filter (Option ::isPresent ).map (Option ::get )
680+ .map (this ::addBootstrapBaseFileIfPresent );
672681 } else {
673- return fileSliceStream .filter (fs -> !isPendingCompactionScheduledForFileId (fs .getFileGroupId ()))
674- .map (this ::addBootstrapBaseFileIfPresent );
682+ return allFileSliceStream
683+ .map (sliceStream ->
684+ Option .fromJavaOptional (sliceStream
685+ .filter (slice -> !isPendingCompactionScheduledForFileId (slice .getFileGroupId ()))
686+ .filter (slice -> !slice .isEmpty ())
687+ .findFirst ()))
688+ .filter (Option ::isPresent ).map (Option ::get ).map (this ::addBootstrapBaseFileIfPresent );
675689 }
676690 } finally {
677691 readLock .unlock ();
@@ -893,7 +907,6 @@ protected abstract Option<Pair<String, CompactionOperation>> getPendingCompactio
893907 */
894908 abstract Stream <BootstrapBaseFileMapping > fetchBootstrapBaseFiles ();
895909
896-
897910 /**
898911 * Checks if partition is pre-loaded and available in store.
899912 *
@@ -967,7 +980,7 @@ Stream<FileSlice> fetchLatestFileSliceInRange(List<String> commitsToReturn) {
967980 */
968981 Stream <FileSlice > fetchAllFileSlices (String partitionPath ) {
969982 return fetchAllStoredFileGroups (partitionPath ).map (this ::addBootstrapBaseFileIfPresent )
970- .map (HoodieFileGroup ::getAllFileSlices ). flatMap ( sliceList -> sliceList );
983+ .flatMap (HoodieFileGroup ::getAllFileSlices );
971984 }
972985
973986 /**
@@ -1003,8 +1016,7 @@ private Stream<HoodieBaseFile> fetchLatestBaseFiles() {
10031016 * @param partitionPath partition-path
10041017 */
10051018 Stream <HoodieBaseFile > fetchAllBaseFiles (String partitionPath ) {
1006- return fetchAllStoredFileGroups (partitionPath ).map (HoodieFileGroup ::getAllBaseFiles )
1007- .flatMap (baseFileList -> baseFileList );
1019+ return fetchAllStoredFileGroups (partitionPath ).flatMap (HoodieFileGroup ::getAllBaseFiles );
10081020 }
10091021
10101022 /**
@@ -1023,18 +1035,6 @@ Stream<FileSlice> fetchLatestFileSlices(String partitionPath) {
10231035 .map (Option ::get );
10241036 }
10251037
1026- /**
1027- * Default implementation for fetching latest file-slices for a partition path as of instant.
1028- *
1029- * @param partitionPath Partition Path
1030- * @param maxCommitTime Instant Time
1031- */
1032- Stream <FileSlice > fetchLatestFileSlicesBeforeOrOn (String partitionPath , String maxCommitTime ) {
1033- return fetchAllStoredFileGroups (partitionPath )
1034- .map (fileGroup -> fileGroup .getLatestFileSliceBeforeOrOn (maxCommitTime )).filter (Option ::isPresent )
1035- .map (Option ::get );
1036- }
1037-
10381038 /**
10391039 * Helper to merge last 2 file-slices. These 2 file-slices do not have compaction done yet.
10401040 *
0 commit comments