Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HashSet based filtering optimization to XContentMapValues #17160

Merged
merged 5 commits into from
Feb 7, 2025
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 @@ -45,9 +45,12 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -216,6 +219,63 @@ public static Map<String, Object> filter(Map<String, ?> map, String[] includes,
* @see #filter(Map, String[], String[]) for details
*/
public static Function<Map<String, ?>, Map<String, Object>> filter(String[] includes, String[] excludes) {

return (map) -> {
if (hasNoDottedKeys(map) && hasNoWildcardsOrDots(includes) && hasNoWildcardsOrDots(excludes)) {
return createSetBasedFilter(includes, excludes).apply(map);
}
return createAutomatonFilter(includes, excludes).apply(map);
};
}

private static boolean hasNoDottedKeys(Map<String, ?> map) {
for (String key : map.keySet()) {
if (key.indexOf('.') != -1) {
return false;
}
}
return true;
}

private static boolean hasNoWildcardsOrDots(String[] fields) {
if (fields == null || fields.length == 0) {
return true;
}

for (String field : fields) {
if (field.indexOf('*') != -1 || field.indexOf('.') != -1) {
return false;
}
}
return true;
}

/**
* Creates a simple HashSet-based filter for exact field name matching
*/
private static Function<Map<String, ?>, Map<String, Object>> createSetBasedFilter(String[] includes, String[] excludes) {

Set<String> includeSet = (includes == null || includes.length == 0) ? null : new HashSet<>(Arrays.asList(includes));
Set<String> excludeSet = (excludes == null || excludes.length == 0)
? Collections.emptySet()
: new HashSet<>(Arrays.asList(excludes));

return (map) -> {
Map<String, Object> filtered = new HashMap<>();
for (Map.Entry<String, ?> entry : map.entrySet()) {
String key = entry.getKey();
if ((includeSet == null || includeSet.contains(key)) && !excludeSet.contains(key)) {
filtered.put(key, entry.getValue());
}
}
msfroh marked this conversation as resolved.
Show resolved Hide resolved
return filtered;
};
}

/**
* Creates an automaton-based filter for complex pattern matching
*/
public static Function<Map<String, ?>, Map<String, Object>> createAutomatonFilter(String[] includes, String[] excludes) {
CharacterRunAutomaton matchAllAutomaton = new CharacterRunAutomaton(Automata.makeAnyString());

CharacterRunAutomaton include;
Expand Down
Loading