Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ public IndicesPermission(Group... groups) {
}

static Predicate<String> indexMatcher(List<String> indices) {
Set<String> exactMatch = new HashSet<>();
List<String> nonExactMatch = new ArrayList<>();
for (String indexPattern : indices) {
if (indexPattern.startsWith("/")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing Automatons code throws an exception if a pattern starts with / but doesn't end with /.
So we could simplify all this checking down to a single if/else and still be compatible with existing behaviour.

if (indexPattern.startsWith("/") || indexPattern.contains("*") || indexPattern.contains("?")) {
    nonExactMatch.add(indexPattern);
} else {
    exactMatch.add(indexPattern);
}

I don't care strongly, but shorter and simpler seems nicer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ thanks for noticing that. I misread the if statement in Automatons

if (indexPattern.endsWith("/") || indexPattern.contains("*") || indexPattern.contains("?")) {
nonExactMatch.add(indexPattern);
} else {
exactMatch.add(indexPattern);
}
} else if (indexPattern.contains("*") || indexPattern.contains("?")) {
nonExactMatch.add(indexPattern);
} else {
exactMatch.add(indexPattern);
}
}

if (exactMatch.isEmpty() && nonExactMatch.isEmpty()) {
return s -> false;
} else if (exactMatch.isEmpty()) {
return buildAutomataPredicate(nonExactMatch);
} else if (nonExactMatch.isEmpty()) {
return buildExactMatchPredicate(exactMatch);
} else {
return buildExactMatchPredicate(exactMatch).or(buildAutomataPredicate(nonExactMatch));
}
}

private static Predicate<String> buildExactMatchPredicate(Set<String> indices) {
return indices::contains;
}

private static Predicate<String> buildAutomataPredicate(List<String> indices) {
try {
return Automatons.predicate(indices);
} catch (TooComplexToDeterminizeException e) {
Expand Down