From 5a75f4fbb581a83b54fe374b57586576ef748602 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Fri, 1 Sep 2023 16:57:25 +0200 Subject: [PATCH] Cleanup IndexResolver.buildIndices slightly (#99012) 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. --- .../xpack/ql/index/IndexResolver.java | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java index 5b5657e8773ea..a01d3f5af6e33 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java @@ -59,13 +59,12 @@ import java.util.function.Supplier; import java.util.regex.Pattern; -import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; -import static java.util.Collections.emptySet; import static org.elasticsearch.action.ActionListener.wrap; import static org.elasticsearch.common.Strings.hasText; import static org.elasticsearch.common.regex.Regex.simpleMatch; +import static org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList; import static org.elasticsearch.transport.RemoteClusterAware.buildRemoteIndexName; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD; @@ -659,15 +658,12 @@ private static List buildIndices( } } - List resolvedIndices = new ArrayList<>(asList(fieldCapsResponse.getIndices())); - int mapSize = CollectionUtils.mapSize(resolvedIndices.size() + resolvedAliases.size()); - Map indices = Maps.newLinkedHashMapWithExpectedSize(mapSize); + final List resolvedIndices = arrayAsArrayList(fieldCapsResponse.getIndices()); + Map indices = Maps.newLinkedHashMapWithExpectedSize(resolvedIndices.size() + resolvedAliases.size()); Pattern pattern = javaRegex != null ? Pattern.compile(javaRegex) : null; // sort fields in reverse order to build the field hierarchy - Set>> sortedFields = new TreeSet<>( - Collections.reverseOrder(Comparator.comparing(Entry::getKey)) - ); + Set>> sortedFields = new TreeSet<>(Collections.reverseOrder(Entry.comparingByKey())); final Map> fieldCaps = fieldCapsResponse.get(); sortedFields.addAll(fieldCaps.entrySet()); @@ -682,29 +678,18 @@ private static List buildIndices( // apply verification for fields belonging to index aliases Map invalidFieldsForAliases = getInvalidFieldsForAliases(fieldName, types, aliases); - // filter unmapped - FieldCapabilities unmapped = types.get(UNMAPPED); - Set unmappedIndices = unmapped != null ? new HashSet<>(asList(unmapped.indices())) : emptySet(); - // check each type for (Entry typeEntry : types.entrySet()) { + if (UNMAPPED.equals(typeEntry.getKey())) { + continue; + } FieldCapabilities typeCap = typeEntry.getValue(); String[] capIndices = typeCap.indices(); // compute the actual indices - if any are specified, take into account the unmapped indices - List concreteIndices = null; + final List concreteIndices; if (capIndices != null) { - if (unmappedIndices.isEmpty()) { - concreteIndices = new ArrayList<>(asList(capIndices)); - } else { - concreteIndices = new ArrayList<>(capIndices.length); - for (String capIndex : capIndices) { - // add only indices that have a mapping - if (unmappedIndices.contains(capIndex) == false) { - concreteIndices.add(capIndex); - } - } - } + concreteIndices = arrayAsArrayList(capIndices); } else { concreteIndices = resolvedIndices; } @@ -727,11 +712,7 @@ private static List buildIndices( // TODO is split still needed? if (pattern == null || pattern.matcher(splitQualifiedIndex(index).v2()).matches() || isIndexAlias) { String indexName = isIndexAlias ? index : indexNameProcessor.apply(index); - Fields indexFields = indices.get(indexName); - if (indexFields == null) { - indexFields = new Fields(); - indices.put(indexName, indexFields); - } + Fields indexFields = indices.computeIfAbsent(indexName, k -> new Fields()); EsField field = indexFields.flattedMapping.get(fieldName); boolean createField = false; if (isIndexAlias == false) {