3131 * Utility class for rewriting cross-project index expressions.
3232 * Provides methods that can rewrite qualified and unqualified index expressions to canonical CCS.
3333 */
34- public class CrossProjectIndexExpressionsRewriter {
34+ public class IndexExpressionsRewriter {
3535 public static TransportVersion NO_MATCHING_PROJECT_EXCEPTION_VERSION = TransportVersion .fromName ("no_matching_project_exception" );
3636
37- private static final Logger logger = LogManager .getLogger (CrossProjectIndexExpressionsRewriter .class );
37+ private static final Logger logger = LogManager .getLogger (IndexExpressionsRewriter .class );
3838 private static final String ORIGIN_PROJECT_KEY = "_origin" ;
3939 private static final String [] MATCH_ALL = new String [] { Metadata .ALL };
4040 private static final String EXCLUSION = "-" ;
@@ -74,7 +74,7 @@ public static Map<String, List<String>> rewriteIndexExpressions(
7474 }
7575 canonicalExpressionsMap .put (
7676 indexExpression ,
77- rewriteIndexExpression (indexExpression , originProjectAlias , allProjectAliases ). all ( )
77+ resultAsList ( rewriteIndexExpression (indexExpression , originProjectAlias , allProjectAliases ))
7878 );
7979 }
8080 return canonicalExpressionsMap ;
@@ -91,7 +91,7 @@ public static Map<String, List<String>> rewriteIndexExpressions(
9191 * @throws IllegalArgumentException if exclusions, date math or selectors are present in the index expressions
9292 * @throws NoMatchingProjectException if a qualified resource cannot be resolved because a project is missing
9393 */
94- public static LocalWithRemoteExpressions rewriteIndexExpression (
94+ public static IndexRewriteResult rewriteIndexExpression (
9595 String indexExpression ,
9696 @ Nullable String originProjectAlias ,
9797 Set <String > allProjectAliases
@@ -102,7 +102,7 @@ public static LocalWithRemoteExpressions rewriteIndexExpression(
102102 maybeThrowOnUnsupportedResource (indexExpression );
103103
104104 final boolean isQualified = RemoteClusterAware .isRemoteIndexName (indexExpression );
105- final LocalWithRemoteExpressions rewrittenExpression ;
105+ final IndexRewriteResult rewrittenExpression ;
106106 if (isQualified ) {
107107 rewrittenExpression = rewriteQualifiedExpression (indexExpression , originProjectAlias , allProjectAliases );
108108 logger .debug ("Rewrote qualified expression [{}] to [{}]" , indexExpression , rewrittenExpression );
@@ -124,7 +124,7 @@ private static Set<String> getAllProjectAliases(@Nullable ProjectRoutingInfo ori
124124 return Collections .unmodifiableSet (allProjectAliases );
125125 }
126126
127- private static LocalWithRemoteExpressions rewriteUnqualifiedExpression (
127+ private static IndexRewriteResult rewriteUnqualifiedExpression (
128128 String indexExpression ,
129129 @ Nullable String originAlias ,
130130 Set <String > allProjectAliases
@@ -139,10 +139,10 @@ private static LocalWithRemoteExpressions rewriteUnqualifiedExpression(
139139 rewrittenExpressions .add (RemoteClusterAware .buildRemoteIndexName (targetProjectAlias , indexExpression ));
140140 }
141141 }
142- return new LocalWithRemoteExpressions (localExpression , rewrittenExpressions );
142+ return new IndexRewriteResult (localExpression , rewrittenExpressions );
143143 }
144144
145- private static LocalWithRemoteExpressions rewriteQualifiedExpression (
145+ private static IndexRewriteResult rewriteQualifiedExpression (
146146 String resource ,
147147 @ Nullable String originProjectAlias ,
148148 Set <String > allProjectAliases
@@ -161,7 +161,7 @@ private static LocalWithRemoteExpressions rewriteQualifiedExpression(
161161
162162 if (originProjectAlias != null && ORIGIN_PROJECT_KEY .equals (requestedProjectAlias )) {
163163 // handling case where we have a qualified expression like: _origin:indexName
164- return new LocalWithRemoteExpressions (indexExpression );
164+ return new IndexRewriteResult (indexExpression );
165165 }
166166
167167 if (originProjectAlias == null && ORIGIN_PROJECT_KEY .equals (requestedProjectAlias )) {
@@ -189,7 +189,7 @@ private static LocalWithRemoteExpressions rewriteQualifiedExpression(
189189 }
190190 }
191191
192- return new LocalWithRemoteExpressions (localExpression , resourcesMatchingLinkedProjectAliases );
192+ return new IndexRewriteResult (localExpression , resourcesMatchingLinkedProjectAliases );
193193 } catch (NoSuchRemoteClusterException ex ) {
194194 logger .debug (ex .getMessage (), ex );
195195 throw new NoMatchingProjectException (requestedProjectAlias );
@@ -208,4 +208,23 @@ private static void maybeThrowOnUnsupportedResource(String resource) {
208208 throw new IllegalArgumentException ("Selectors are not currently supported but was found in the expression [" + resource + "]" );
209209 }
210210 }
211+
212+ private static List <String > resultAsList (IndexRewriteResult result ) {
213+ if (result .localExpression == null ) {
214+ return result .remoteExpressions ;
215+ }
216+ List <String > all = new ArrayList <>();
217+ all .add (result .localExpression );
218+ all .addAll (result .remoteExpressions );
219+ return List .copyOf (all );
220+ }
221+
222+ /**
223+ * A container for a local expression and a list of remote expressions.
224+ */
225+ public record IndexRewriteResult (@ Nullable String localExpression , List <String > remoteExpressions ) {
226+ public IndexRewriteResult (String localExpression ) {
227+ this (localExpression , List .of ());
228+ }
229+ }
211230}
0 commit comments