@@ -672,7 +672,7 @@ private static Predicate<SnapshotInfo> buildAfterPredicate(
672
672
}
673
673
}
674
674
675
- private static Predicate < SnapshotInfo > filterBySLMPolicies (String [] slmPolicies ) {
675
+ private static SnapshotPredicate filterBySLMPolicies (String [] slmPolicies ) {
676
676
final List <String > includePatterns = new ArrayList <>();
677
677
final List <String > excludePatterns = new ArrayList <>();
678
678
boolean seenWildcard = false ;
@@ -692,25 +692,47 @@ private static Predicate<SnapshotInfo> filterBySLMPolicies(String[] slmPolicies)
692
692
final String [] includes = includePatterns .toArray (Strings .EMPTY_ARRAY );
693
693
final String [] excludes = excludePatterns .toArray (Strings .EMPTY_ARRAY );
694
694
final boolean matchWithoutPolicy = matchNoPolicy ;
695
- return snapshotInfo -> {
696
- final Map <String , Object > metadata = snapshotInfo .userMetadata ();
697
- final String policy ;
698
- if (metadata == null ) {
699
- policy = null ;
700
- } else {
701
- final Object policyFound = metadata .get (SnapshotsService .POLICY_ID_METADATA_FIELD );
702
- policy = policyFound instanceof String ? (String ) policyFound : null ;
703
- }
704
- if (policy == null ) {
705
- return matchWithoutPolicy ;
695
+ return new SnapshotPredicate () {
696
+ @ Override
697
+ public boolean matchesPreflight (SnapshotId snapshotId , RepositoryData repositoryData ) {
698
+ final RepositoryData .SnapshotDetails details = repositoryData .getSnapshotDetails (snapshotId );
699
+ final String policy ;
700
+ if (details == null || (details .getSlmPolicy () == null )) {
701
+ // no SLM policy recorded
702
+ return true ;
703
+ } else {
704
+ final String policyFound = details .getSlmPolicy ();
705
+ // empty string means that snapshot was not created by an SLM policy
706
+ policy = policyFound .isEmpty () ? null : policyFound ;
707
+ }
708
+ return matchPolicy (includes , excludes , matchWithoutPolicy , policy );
706
709
}
707
- if (Regex .simpleMatch (includes , policy ) == false ) {
708
- return false ;
710
+
711
+ @ Override
712
+ public boolean matches (SnapshotInfo snapshotInfo ) {
713
+ final Map <String , Object > metadata = snapshotInfo .userMetadata ();
714
+ final String policy ;
715
+ if (metadata == null ) {
716
+ policy = null ;
717
+ } else {
718
+ final Object policyFound = metadata .get (SnapshotsService .POLICY_ID_METADATA_FIELD );
719
+ policy = policyFound instanceof String ? (String ) policyFound : null ;
720
+ }
721
+ return matchPolicy (includes , excludes , matchWithoutPolicy , policy );
709
722
}
710
- return excludes .length == 0 || Regex .simpleMatch (excludes , policy ) == false ;
711
723
};
712
724
}
713
725
726
+ private static boolean matchPolicy (String [] includes , String [] excludes , boolean matchWithoutPolicy , @ Nullable String policy ) {
727
+ if (policy == null ) {
728
+ return matchWithoutPolicy ;
729
+ }
730
+ if (Regex .simpleMatch (includes , policy ) == false ) {
731
+ return false ;
732
+ }
733
+ return excludes .length == 0 || Regex .simpleMatch (excludes , policy ) == false ;
734
+ }
735
+
714
736
private static Predicate <SnapshotInfo > filterByLongOffset (ToLongFunction <SnapshotInfo > extractor , long after , SortOrder order ) {
715
737
return order == SortOrder .ASC ? info -> after <= extractor .applyAsLong (info ) : info -> after >= extractor .applyAsLong (info );
716
738
}
@@ -765,19 +787,23 @@ private static final class SnapshotPredicates {
765
787
Predicate <SnapshotInfo > snapshotPredicate = null ;
766
788
final String [] slmPolicies = request .policies ();
767
789
final String fromSortValue = request .fromSortValue ();
790
+ BiPredicate <SnapshotId , RepositoryData > preflightPredicate = null ;
768
791
if (slmPolicies .length > 0 ) {
769
- snapshotPredicate = filterBySLMPolicies (slmPolicies );
792
+ final SnapshotPredicate predicate = filterBySLMPolicies (slmPolicies );
793
+ snapshotPredicate = predicate ::matches ;
794
+ preflightPredicate = predicate ::matchesPreflight ;
770
795
}
771
796
final GetSnapshotsRequest .SortBy sortBy = request .sort ();
772
797
final SortOrder order = request .order ();
773
798
if (fromSortValue == null ) {
774
- preflightPredicate = null ;
799
+ this . preflightPredicate = preflightPredicate ;
775
800
} else {
776
801
final Predicate <SnapshotInfo > fromSortValuePredicate ;
802
+ final BiPredicate <SnapshotId , RepositoryData > preflightPred ;
777
803
switch (sortBy ) {
778
804
case START_TIME :
779
805
final long after = Long .parseLong (fromSortValue );
780
- preflightPredicate = order == SortOrder .ASC ? (snapshotId , repositoryData ) -> {
806
+ preflightPred = order == SortOrder .ASC ? (snapshotId , repositoryData ) -> {
781
807
final long startTime = getStartTime (snapshotId , repositoryData );
782
808
return startTime == -1 || after <= startTime ;
783
809
} : (snapshotId , repositoryData ) -> {
@@ -787,14 +813,14 @@ private static final class SnapshotPredicates {
787
813
fromSortValuePredicate = filterByLongOffset (SnapshotInfo ::startTime , after , order );
788
814
break ;
789
815
case NAME :
790
- preflightPredicate = order == SortOrder .ASC
816
+ preflightPred = order == SortOrder .ASC
791
817
? (snapshotId , repositoryData ) -> fromSortValue .compareTo (snapshotId .getName ()) <= 0
792
818
: (snapshotId , repositoryData ) -> fromSortValue .compareTo (snapshotId .getName ()) >= 0 ;
793
819
fromSortValuePredicate = null ;
794
820
break ;
795
821
case DURATION :
796
822
final long afterDuration = Long .parseLong (fromSortValue );
797
- preflightPredicate = order == SortOrder .ASC ? (snapshotId , repositoryData ) -> {
823
+ preflightPred = order == SortOrder .ASC ? (snapshotId , repositoryData ) -> {
798
824
final long duration = getDuration (snapshotId , repositoryData );
799
825
return duration == -1 || afterDuration <= duration ;
800
826
} : (snapshotId , repositoryData ) -> {
@@ -805,22 +831,22 @@ private static final class SnapshotPredicates {
805
831
break ;
806
832
case INDICES :
807
833
final int afterIndexCount = Integer .parseInt (fromSortValue );
808
- preflightPredicate = order == SortOrder .ASC
834
+ preflightPred = order == SortOrder .ASC
809
835
? (snapshotId , repositoryData ) -> afterIndexCount <= indexCount (snapshotId , repositoryData )
810
836
: (snapshotId , repositoryData ) -> afterIndexCount >= indexCount (snapshotId , repositoryData );
811
837
fromSortValuePredicate = null ;
812
838
break ;
813
839
case REPOSITORY :
814
840
// already handled in #maybeFilterRepositories
815
- preflightPredicate = null ;
841
+ preflightPred = null ;
816
842
fromSortValuePredicate = null ;
817
843
break ;
818
844
case SHARDS :
819
- preflightPredicate = null ;
845
+ preflightPred = null ;
820
846
fromSortValuePredicate = filterByLongOffset (SnapshotInfo ::totalShards , Integer .parseInt (fromSortValue ), order );
821
847
break ;
822
848
case FAILED_SHARDS :
823
- preflightPredicate = null ;
849
+ preflightPred = null ;
824
850
fromSortValuePredicate = filterByLongOffset (SnapshotInfo ::failedShards , Integer .parseInt (fromSortValue ), order );
825
851
break ;
826
852
default :
@@ -832,6 +858,15 @@ private static final class SnapshotPredicates {
832
858
} else if (fromSortValuePredicate != null ) {
833
859
snapshotPredicate = fromSortValuePredicate .and (snapshotPredicate );
834
860
}
861
+ if (preflightPredicate == null ) {
862
+ this .preflightPredicate = preflightPred ;
863
+ } else {
864
+ if (preflightPred != null ) {
865
+ this .preflightPredicate = preflightPredicate .and (preflightPred );
866
+ } else {
867
+ this .preflightPredicate = preflightPredicate ;
868
+ }
869
+ }
835
870
}
836
871
this .snapshotPredicate = snapshotPredicate ;
837
872
}
@@ -848,6 +883,19 @@ public BiPredicate<SnapshotId, RepositoryData> preflightPredicate() {
848
883
849
884
}
850
885
886
+ private interface SnapshotPredicate {
887
+
888
+ /**
889
+ * Checks if a snapshot matches the predicate by testing its {@link SnapshotId} for a given {@link RepositoryData}.
890
+ */
891
+ boolean matchesPreflight (SnapshotId snapshotId , RepositoryData repositoryData );
892
+
893
+ /**
894
+ * Checks if a snapshot matches the predicate by testing its {@link SnapshotInfo}.
895
+ */
896
+ boolean matches (SnapshotInfo snapshotInfo );
897
+ }
898
+
851
899
private static final class SnapshotsInRepo {
852
900
853
901
private final List <SnapshotInfo > snapshotInfos ;
0 commit comments