Skip to content

Commit

Permalink
Dry up index name expression resolving a little (#88244)
Browse files Browse the repository at this point in the history
Just two simple spots I found while researching something else.
  • Loading branch information
original-brownbear authored Jul 4, 2022
1 parent 453f12c commit 40de25b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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?
Expand All @@ -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);
}
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -825,20 +825,7 @@ public Map<String, Set<String>> resolveSearchRouting(ClusterState state, @Nullab
}
} else {
// Non-routing alias
if (norouting.contains(concreteIndex) == false) {
norouting.add(concreteIndex);
if (paramRouting != null) {
Set<String> 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);
}
}
}
Expand All @@ -850,38 +837,12 @@ public Map<String, Set<String>> 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<String> 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<String> 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);
}

}
Expand All @@ -891,6 +852,26 @@ public Map<String, Set<String>> resolveSearchRouting(ClusterState state, @Nullab
return routings;
}

@Nullable
private static Map<String, Set<String>> collectRoutings(
@Nullable Map<String, Set<String>> routings,
@Nullable Set<String> paramRouting,
Set<String> 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
*/
Expand Down

0 comments on commit 40de25b

Please sign in to comment.