From 40de25b12e8078690f53f059c56e5dd78ca7cc5e Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 4 Jul 2022 13:23:27 +0200 Subject: [PATCH] Dry up index name expression resolving a little (#88244) Just two simple spots I found while researching something else. --- .../metadata/IndexAbstractionResolver.java | 62 ++++++----------- .../metadata/IndexNameExpressionResolver.java | 67 +++++++------------ 2 files changed, 44 insertions(+), 85 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolver.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolver.java index 6b560213bfb85..e71576549c806 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolver.java @@ -179,20 +179,7 @@ public static boolean isIndexVisible( return false; } if (indexAbstraction.isSystem()) { - final SystemIndexAccessLevel level = resolver.getSystemIndexAccessLevel(); - switch (level) { - case ALL: - return true; - case NONE: - return false; - case RESTRICTED: - return resolver.getSystemIndexAccessPredicate().test(indexAbstraction.getName()); - case BACKWARDS_COMPATIBLE_ONLY: - return resolver.getNetNewSystemIndexPredicate().test(indexAbstraction.getName()); - default: - assert false : "unexpected system index access level [" + level + "]"; - throw new IllegalStateException("unexpected system index access level [" + level + "]"); - } + return isSystemIndexVisible(resolver, indexAbstraction); } else { return isVisible; } @@ -210,20 +197,7 @@ public static boolean isIndexVisible( if (indexAbstraction.isSystem()) { // check if it is net new if (resolver.getNetNewSystemIndexPredicate().test(indexAbstraction.getName())) { - final SystemIndexAccessLevel level = resolver.getSystemIndexAccessLevel(); - switch (level) { - case ALL: - return true; - case NONE: - return false; - case RESTRICTED: - return resolver.getSystemIndexAccessPredicate().test(indexAbstraction.getName()); - case BACKWARDS_COMPATIBLE_ONLY: - return resolver.getNetNewSystemIndexPredicate().test(indexAbstraction.getName()); - default: - assert false : "unexpected system index access level [" + level + "]"; - throw new IllegalStateException("unexpected system index access level [" + level + "]"); - } + return isSystemIndexVisible(resolver, indexAbstraction); } // does the system index back a system data stream? @@ -232,20 +206,7 @@ public static boolean isIndexVisible( assert false : "system index is part of a data stream that is not a system data stream"; throw new IllegalStateException("system index is part of a data stream that is not a system data stream"); } - final SystemIndexAccessLevel level = resolver.getSystemIndexAccessLevel(); - switch (level) { - case ALL: - return true; - case NONE: - return false; - case RESTRICTED: - return resolver.getSystemIndexAccessPredicate().test(indexAbstraction.getName()); - case BACKWARDS_COMPATIBLE_ONLY: - return resolver.getNetNewSystemIndexPredicate().test(indexAbstraction.getName()); - default: - assert false : "unexpected system index access level [" + level + "]"; - throw new IllegalStateException("unexpected system index access level [" + level + "]"); - } + return isSystemIndexVisible(resolver, indexAbstraction); } } @@ -259,6 +220,23 @@ public static boolean isIndexVisible( return false; } + private static boolean isSystemIndexVisible(IndexNameExpressionResolver resolver, IndexAbstraction indexAbstraction) { + final SystemIndexAccessLevel level = resolver.getSystemIndexAccessLevel(); + switch (level) { + case ALL: + return true; + case NONE: + return false; + case RESTRICTED: + return resolver.getSystemIndexAccessPredicate().test(indexAbstraction.getName()); + case BACKWARDS_COMPATIBLE_ONLY: + return resolver.getNetNewSystemIndexPredicate().test(indexAbstraction.getName()); + default: + assert false : "unexpected system index access level [" + level + "]"; + throw new IllegalStateException("unexpected system index access level [" + level + "]"); + } + } + private static boolean isVisibleDueToImplicitHidden(String expression, String index) { return index.startsWith(".") && expression.startsWith(".") && Regex.isSimpleMatchPattern(expression); } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java index 56de7c89d11bf..674fd91ce00c6 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -764,7 +764,7 @@ public String[] indexAliases( if (aliases == null) { return null; } - return aliases.toArray(new String[aliases.size()]); + return aliases.toArray(Strings.EMPTY_ARRAY); } } @@ -825,20 +825,7 @@ public Map> resolveSearchRouting(ClusterState state, @Nullab } } else { // Non-routing alias - if (norouting.contains(concreteIndex) == false) { - norouting.add(concreteIndex); - if (paramRouting != null) { - Set r = new HashSet<>(paramRouting); - if (routings == null) { - routings = new HashMap<>(); - } - routings.put(concreteIndex, r); - } else { - if (routings != null) { - routings.remove(concreteIndex); - } - } - } + routings = collectRoutings(routings, paramRouting, norouting, concreteIndex); } } } @@ -850,38 +837,12 @@ public Map> resolveSearchRouting(ClusterState state, @Nullab if (dataStream.getIndices() != null) { for (Index index : dataStream.getIndices()) { String concreteIndex = index.getName(); - if (norouting.contains(concreteIndex) == false) { - norouting.add(concreteIndex); - if (paramRouting != null) { - Set r = new HashSet<>(paramRouting); - if (routings == null) { - routings = new HashMap<>(); - } - routings.put(concreteIndex, r); - } else { - if (routings != null) { - routings.remove(concreteIndex); - } - } - } + routings = collectRoutings(routings, paramRouting, norouting, concreteIndex); } } } else { // Index - if (norouting.contains(expression) == false) { - norouting.add(expression); - if (paramRouting != null) { - Set r = new HashSet<>(paramRouting); - if (routings == null) { - routings = new HashMap<>(); - } - routings.put(expression, r); - } else { - if (routings != null) { - routings.remove(expression); - } - } - } + routings = collectRoutings(routings, paramRouting, norouting, expression); } } @@ -891,6 +852,26 @@ public Map> resolveSearchRouting(ClusterState state, @Nullab return routings; } + @Nullable + private static Map> collectRoutings( + @Nullable Map> routings, + @Nullable Set paramRouting, + Set noRouting, + String concreteIndex + ) { + if (noRouting.add(concreteIndex)) { + if (paramRouting != null) { + if (routings == null) { + routings = new HashMap<>(); + } + routings.put(concreteIndex, new HashSet<>(paramRouting)); + } else if (routings != null) { + routings.remove(concreteIndex); + } + } + return routings; + } + /** * Sets the same routing for all indices */