Skip to content

Commit ac31c1e

Browse files
Cleanup IndexResolver.buildIndices slightly (#99012) (#99148)
No need to check the unmapped array for membership, especially not with itself. The field caps response will not put an index into both unmapped and mapped ever, that woudl be an obvious bug. Also some minor drying up via collection tools we already have in place. This should save a considerable amount of memory for not having to copy the unmapped fields array.
1 parent 4ab45e9 commit ac31c1e

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@
5959
import java.util.function.Supplier;
6060
import java.util.regex.Pattern;
6161

62-
import static java.util.Arrays.asList;
6362
import static java.util.Collections.emptyList;
6463
import static java.util.Collections.emptyMap;
65-
import static java.util.Collections.emptySet;
6664
import static org.elasticsearch.action.ActionListener.wrap;
6765
import static org.elasticsearch.common.Strings.hasText;
6866
import static org.elasticsearch.common.regex.Regex.simpleMatch;
67+
import static org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList;
6968
import static org.elasticsearch.transport.RemoteClusterAware.buildRemoteIndexName;
7069
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
7170
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
@@ -659,15 +658,12 @@ private static List<EsIndex> buildIndices(
659658
}
660659
}
661660

662-
List<String> resolvedIndices = new ArrayList<>(asList(fieldCapsResponse.getIndices()));
663-
int mapSize = CollectionUtils.mapSize(resolvedIndices.size() + resolvedAliases.size());
664-
Map<String, Fields> indices = Maps.newLinkedHashMapWithExpectedSize(mapSize);
661+
final List<String> resolvedIndices = arrayAsArrayList(fieldCapsResponse.getIndices());
662+
Map<String, Fields> indices = Maps.newLinkedHashMapWithExpectedSize(resolvedIndices.size() + resolvedAliases.size());
665663
Pattern pattern = javaRegex != null ? Pattern.compile(javaRegex) : null;
666664

667665
// sort fields in reverse order to build the field hierarchy
668-
Set<Entry<String, Map<String, FieldCapabilities>>> sortedFields = new TreeSet<>(
669-
Collections.reverseOrder(Comparator.comparing(Entry::getKey))
670-
);
666+
Set<Entry<String, Map<String, FieldCapabilities>>> sortedFields = new TreeSet<>(Collections.reverseOrder(Entry.comparingByKey()));
671667
final Map<String, Map<String, FieldCapabilities>> fieldCaps = fieldCapsResponse.get();
672668
sortedFields.addAll(fieldCaps.entrySet());
673669

@@ -682,29 +678,18 @@ private static List<EsIndex> buildIndices(
682678
// apply verification for fields belonging to index aliases
683679
Map<String, InvalidMappedField> invalidFieldsForAliases = getInvalidFieldsForAliases(fieldName, types, aliases);
684680

685-
// filter unmapped
686-
FieldCapabilities unmapped = types.get(UNMAPPED);
687-
Set<String> unmappedIndices = unmapped != null ? new HashSet<>(asList(unmapped.indices())) : emptySet();
688-
689681
// check each type
690682
for (Entry<String, FieldCapabilities> typeEntry : types.entrySet()) {
683+
if (UNMAPPED.equals(typeEntry.getKey())) {
684+
continue;
685+
}
691686
FieldCapabilities typeCap = typeEntry.getValue();
692687
String[] capIndices = typeCap.indices();
693688

694689
// compute the actual indices - if any are specified, take into account the unmapped indices
695-
List<String> concreteIndices = null;
690+
final List<String> concreteIndices;
696691
if (capIndices != null) {
697-
if (unmappedIndices.isEmpty()) {
698-
concreteIndices = new ArrayList<>(asList(capIndices));
699-
} else {
700-
concreteIndices = new ArrayList<>(capIndices.length);
701-
for (String capIndex : capIndices) {
702-
// add only indices that have a mapping
703-
if (unmappedIndices.contains(capIndex) == false) {
704-
concreteIndices.add(capIndex);
705-
}
706-
}
707-
}
692+
concreteIndices = arrayAsArrayList(capIndices);
708693
} else {
709694
concreteIndices = resolvedIndices;
710695
}
@@ -727,11 +712,7 @@ private static List<EsIndex> buildIndices(
727712
// TODO is split still needed?
728713
if (pattern == null || pattern.matcher(splitQualifiedIndex(index).v2()).matches() || isIndexAlias) {
729714
String indexName = isIndexAlias ? index : indexNameProcessor.apply(index);
730-
Fields indexFields = indices.get(indexName);
731-
if (indexFields == null) {
732-
indexFields = new Fields();
733-
indices.put(indexName, indexFields);
734-
}
715+
Fields indexFields = indices.computeIfAbsent(indexName, k -> new Fields());
735716
EsField field = indexFields.flattedMapping.get(fieldName);
736717
boolean createField = false;
737718
if (isIndexAlias == false) {

0 commit comments

Comments
 (0)