@@ -855,7 +855,7 @@ public Map<String, List<DataStreamAlias>> findDataStreamAliases(final String[] a
855
855
*
856
856
* @param aliases The aliases to look for. Might contain include or exclude wildcards.
857
857
* @param possibleMatches The data streams or indices that the aliases must point to in order to be returned
858
- * @param getter A function that is used to get the alises for a given data stream or index
858
+ * @param getter A function that is used to get the aliases for a given data stream or index
859
859
* @param setter A function that is used to keep track of the found aliases
860
860
*/
861
861
private void findAliasInfo (final String [] aliases , final String [] possibleMatches , AliasInfoGetter getter , AliasInfoSetter setter ) {
@@ -881,24 +881,30 @@ private void findAliasInfo(final String[] aliases, final String[] possibleMatche
881
881
882
882
boolean matchAllAliases = patterns .length == 0 ;
883
883
884
+ // memoize pattern match against aliases to avoid repeatedly matching when multiple indices share an alias
885
+ HashMap <String , Boolean > seenAliases = new HashMap <>();
886
+ Predicate <String > matcher = alias -> seenAliases .computeIfAbsent (alias , key -> {
887
+ boolean matched = matchAllAliases ;
888
+ for (int i = 0 ; i < patterns .length ; i ++) {
889
+ if (include [i ]) {
890
+ if (matched == false ) {
891
+ String pattern = patterns [i ];
892
+ matched = ALL .equals (pattern ) || Regex .simpleMatch (pattern , key );
893
+ }
894
+ } else if (matched ) {
895
+ matched = Regex .simpleMatch (patterns [i ], key ) == false ;
896
+ }
897
+ }
898
+
899
+ return matched ;
900
+ });
901
+
884
902
for (String index : possibleMatches ) {
885
903
List <AliasInfo > filteredValues = new ArrayList <>();
886
904
887
905
List <? extends AliasInfo > entities = getter .get (index );
888
906
for (AliasInfo aliasInfo : entities ) {
889
- boolean matched = matchAllAliases ;
890
- String alias = aliasInfo .getAlias ();
891
- for (int i = 0 ; i < patterns .length ; i ++) {
892
- if (include [i ]) {
893
- if (matched == false ) {
894
- String pattern = patterns [i ];
895
- matched = ALL .equals (pattern ) || Regex .simpleMatch (pattern , alias );
896
- }
897
- } else if (matched ) {
898
- matched = Regex .simpleMatch (patterns [i ], alias ) == false ;
899
- }
900
- }
901
- if (matched ) {
907
+ if (matcher .test (aliasInfo .getAlias ())) {
902
908
filteredValues .add (aliasInfo );
903
909
}
904
910
}
0 commit comments