From ea6f8701a2f93b45440d7463e0f48df4f7ee01e3 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 13 Feb 2024 13:34:11 +0100 Subject: Make members final and use Map/List.of() to simplify logic sprinkled around. --- .../main/java/com/yahoo/prelude/IndexFacts.java | 59 +++++++--------------- .../main/java/com/yahoo/prelude/IndexModel.java | 27 +++------- .../prelude/searcher/ValidateSortingSearcher.java | 5 +- .../federation/sourceref/SourceRefResolver.java | 1 - .../yahoo/search/grouping/GroupingValidator.java | 5 +- .../search/pagetemplates/PageTemplateSearcher.java | 6 +-- .../yahoo/search/schema/SchemaInfoConfigurer.java | 8 +-- .../com/yahoo/search/yql/MinimalQueryInserter.java | 2 +- 8 files changed, 39 insertions(+), 74 deletions(-) (limited to 'container-search/src/main/java/com') diff --git a/container-search/src/main/java/com/yahoo/prelude/IndexFacts.java b/container-search/src/main/java/com/yahoo/prelude/IndexFacts.java index 39b9bdadbeb..c0dce3734b2 100644 --- a/container-search/src/main/java/com/yahoo/prelude/IndexFacts.java +++ b/container-search/src/main/java/com/yahoo/prelude/IndexFacts.java @@ -6,7 +6,6 @@ import com.yahoo.search.Query; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -30,23 +29,15 @@ import static com.yahoo.text.Lowercase.toLowerCase; // TODO: Complete migration to SchemaInfo public class IndexFacts { - private Map> clusterByDocument; + private final Map> clusterByDocument; - private static class DocumentTypeListOffset { - public final int offset; - public final SearchDefinition searchDefinition; - - public DocumentTypeListOffset(int offset, SearchDefinition searchDefinition) { - this.offset = offset; - this.searchDefinition = searchDefinition; - } - } + private record DocumentTypeListOffset(int offset, SearchDefinition searchDefinition) { } /** A Map of all known search definitions indexed by name */ - private Map searchDefinitions = new LinkedHashMap<>(); + private final Map searchDefinitions; /** A map of document types contained in each cluster indexed by cluster name */ - private Map> clusters = new LinkedHashMap<>(); + private final Map> clusters; /** * The name of the default search definition, which is the union of all @@ -62,21 +53,18 @@ public class IndexFacts { /** Whether this has (any) NGram indexes. Calculated at freeze time. */ private boolean hasNGramIndices; - public IndexFacts() {} + public IndexFacts() { + searchDefinitions = Map.of(); + clusters = Map.of(); + clusterByDocument = Map.of(); + } public IndexFacts(IndexModel indexModel) { - if (indexModel.getSearchDefinitions() != null) { - this.searchDefinitions = indexModel.getSearchDefinitions(); + this.searchDefinitions = indexModel.getSearchDefinitions(); + if (indexModel.getUnionSearchDefinition() != null) { this.unionSearchDefinition = indexModel.getUnionSearchDefinition(); } - if (indexModel.getMasterClusters() != null) { - setMasterClusters(indexModel.getMasterClusters()); - } - } - - private void setMasterClusters(Map> clusters) { - // TODO: clusters should probably be a separate class - this.clusters = clusters; + this.clusters = indexModel.getMasterClusters(); clusterByDocument = invert(clusters); } @@ -97,18 +85,16 @@ public class IndexFacts { // Assumes that document names are equal to the search definition that contain them. public List clustersHavingSearchDefinition(String searchDefinitionName) { - if (clusterByDocument == null) return List.of(); - List clusters = clusterByDocument.get(searchDefinitionName); return clusters != null ? clusters : List.of(); } - private boolean isInitialized() { - return searchDefinitions.size() > 0; + private boolean notInitialized() { + return searchDefinitions.isEmpty(); } private boolean isIndexFromDocumentTypes(String indexName, List documentTypes) { - if ( ! isInitialized()) return true; + if ( notInitialized()) return true; if (documentTypes.isEmpty()) { return unionSearchDefinition.getIndex(indexName) != null; @@ -127,8 +113,6 @@ public class IndexFacts { } private String getCanonicNameFromDocumentTypes(String indexName, List documentTypes) { - if (!isInitialized()) return indexName; - if (documentTypes.isEmpty()) { Index index = unionSearchDefinition.getIndexByLowerCase(toLowerCase(indexName)); return index == null ? indexName : index.getName(); @@ -150,12 +134,9 @@ public class IndexFacts { } private Index getIndexByCanonicNameFromDocumentTypes(String canonicName, List documentTypes) { - if ( ! isInitialized()) return Index.nullIndex; - if (documentTypes.isEmpty()) { Index index = unionSearchDefinition.getIndex(canonicName); - if (index == null) return Index.nullIndex; - return index; + return (index != null) ? index : Index.nullIndex; } DocumentTypeListOffset sd = chooseSearchDefinition(documentTypes, 0); @@ -170,10 +151,8 @@ public class IndexFacts { } private Collection getIndexes(String documentType) { - if ( ! isInitialized()) return List.of(); SearchDefinition sd = searchDefinitions.get(documentType); - if (sd == null) return List.of(); - return sd.indices().values(); + return (sd != null) ? sd.indices().values() : List.of(); } /** Calls resolveDocumentTypes(query.getModel().getSources(), query.getModel().getRestrict()) */ @@ -279,10 +258,6 @@ public class IndexFacts { return frozen; } - private void ensureNotFrozen() { - if (frozen) throw new IllegalStateException("Tried to modify frozen IndexFacts instance."); - } - public String getDefaultPosition(String sdName) { SearchDefinition sd; if (sdName == null) { diff --git a/container-search/src/main/java/com/yahoo/prelude/IndexModel.java b/container-search/src/main/java/com/yahoo/prelude/IndexModel.java index 3fc426d9650..21dafae7dad 100644 --- a/container-search/src/main/java/com/yahoo/prelude/IndexModel.java +++ b/container-search/src/main/java/com/yahoo/prelude/IndexModel.java @@ -1,16 +1,12 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.prelude; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; -import java.util.stream.Collectors; import com.yahoo.search.config.IndexInfoConfig; import com.yahoo.container.QrSearchersConfig; @@ -31,7 +27,7 @@ public final class IndexModel { /** Create an index model for a single search definition */ public IndexModel(SearchDefinition searchDefinition) { - this(Collections.emptyMap(), Collections.singleton(searchDefinition)); + this(Map.of(), List.of(searchDefinition)); } public IndexModel(Map> masterClusters, Collection searchDefinitions) { @@ -52,23 +48,18 @@ public final class IndexModel { searchDefinitions = toSearchDefinitions(indexInfo); unionSearchDefinition = unionOf(searchDefinitions.values()); } else { - searchDefinitions = null; + searchDefinitions = Map.of(); unionSearchDefinition = null; } this.masterClusters = clusters; } private static Map> toClusters(QrSearchersConfig config) { - if (config == null) return new HashMap<>(); + if (config == null) return Map.of(); Map> clusters = new HashMap<>(); - for (int i = 0; i < config.searchcluster().size(); ++i) { - List docTypes = new ArrayList<>(); - String clusterName = config.searchcluster(i).name(); - for (int j = 0; j < config.searchcluster(i).searchdef().size(); ++j) { - docTypes.add(config.searchcluster(i).searchdef(j)); - } - clusters.put(clusterName, docTypes); + for (var searchCluster : config.searchcluster()) { + clusters.put(searchCluster.name(), searchCluster.searchdef()); } return clusters; } @@ -76,12 +67,10 @@ public final class IndexModel { private static Map toSearchDefinitions(IndexInfoConfig c) { Map searchDefinitions = new HashMap<>(); - for (Iterator i = c.indexinfo().iterator(); i.hasNext();) { - IndexInfoConfig.Indexinfo info = i.next(); + for (IndexInfoConfig.Indexinfo info : c.indexinfo()) { SearchDefinition sd = new SearchDefinition(info.name()); - for (Iterator j = info.command().iterator(); j.hasNext();) { - IndexInfoConfig.Indexinfo.Command command = j.next(); - sd.addCommand(command.indexname(),command.command()); + for (IndexInfoConfig.Indexinfo.Command command : info.command()) { + sd.addCommand(command.indexname(), command.command()); } searchDefinitions.put(info.name(), sd); } diff --git a/container-search/src/main/java/com/yahoo/prelude/searcher/ValidateSortingSearcher.java b/container-search/src/main/java/com/yahoo/prelude/searcher/ValidateSortingSearcher.java index 736dc7d7f39..a25cc30955e 100644 --- a/container-search/src/main/java/com/yahoo/prelude/searcher/ValidateSortingSearcher.java +++ b/container-search/src/main/java/com/yahoo/prelude/searcher/ValidateSortingSearcher.java @@ -37,8 +37,9 @@ public class ValidateSortingSearcher extends Searcher { public ValidateSortingSearcher(QrSearchersConfig qrsConfig, ClusterConfig clusterConfig, AttributesConfig attributesConfig) { initAttributeNames(attributesConfig); - setClusterName(qrsConfig.searchcluster(clusterConfig.clusterId()).name()); - indexingMode = qrsConfig.searchcluster(clusterConfig.clusterId()).indexingmode(); + var searchCluster = qrsConfig.searchcluster(clusterConfig.clusterId()); + setClusterName(searchCluster.name()); + indexingMode = searchCluster.indexingmode(); } public String getClusterName() { diff --git a/container-search/src/main/java/com/yahoo/search/federation/sourceref/SourceRefResolver.java b/container-search/src/main/java/com/yahoo/search/federation/sourceref/SourceRefResolver.java index f463ff67ad9..7345868cae7 100644 --- a/container-search/src/main/java/com/yahoo/search/federation/sourceref/SourceRefResolver.java +++ b/container-search/src/main/java/com/yahoo/search/federation/sourceref/SourceRefResolver.java @@ -5,7 +5,6 @@ import com.yahoo.component.ComponentSpecification; import com.yahoo.prelude.IndexFacts; import com.yahoo.processing.request.Properties; -import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; diff --git a/container-search/src/main/java/com/yahoo/search/grouping/GroupingValidator.java b/container-search/src/main/java/com/yahoo/search/grouping/GroupingValidator.java index bc78579ea77..d61da0ebedf 100644 --- a/container-search/src/main/java/com/yahoo/search/grouping/GroupingValidator.java +++ b/container-search/src/main/java/com/yahoo/search/grouping/GroupingValidator.java @@ -52,9 +52,10 @@ public class GroupingValidator extends Searcher { public GroupingValidator(QrSearchersConfig qrsConfig, ClusterConfig clusterConfig, AttributesConfig attributesConfig) { int clusterId = clusterConfig.clusterId(); - QrSearchersConfig.Searchcluster.Indexingmode.Enum indexingMode = qrsConfig.searchcluster(clusterId).indexingmode(); + var searchCluster = qrsConfig.searchcluster(clusterId); + QrSearchersConfig.Searchcluster.Indexingmode.Enum indexingMode = searchCluster.indexingmode(); enabled = (indexingMode != QrSearchersConfig.Searchcluster.Indexingmode.STREAMING); - clusterName = enabled ? qrsConfig.searchcluster(clusterId).name() : null; + clusterName = enabled ? searchCluster.name() : null; for (AttributesConfig.Attribute attr : attributesConfig.attribute()) { attributes.put(attr.name(), attr); } diff --git a/container-search/src/main/java/com/yahoo/search/pagetemplates/PageTemplateSearcher.java b/container-search/src/main/java/com/yahoo/search/pagetemplates/PageTemplateSearcher.java index 2f327dcd952..e07b290f66e 100644 --- a/container-search/src/main/java/com/yahoo/search/pagetemplates/PageTemplateSearcher.java +++ b/container-search/src/main/java/com/yahoo/search/pagetemplates/PageTemplateSearcher.java @@ -138,7 +138,7 @@ public class PageTemplateSearcher extends Searcher { List pageIds = (List) query.properties().get(pageIdListName); if (pageIds == null) { String pageIdString = query.properties().getString(pageIdName,"").trim(); - if (pageIdString.length() > 0) + if (!pageIdString.isEmpty()) pageIds = Arrays.asList(pageIdString.split(" ")); } @@ -179,7 +179,7 @@ public class PageTemplateSearcher extends Searcher { addErrorIfSameSourceMultipleTimes(pages,pageSources,query); - if (query.getModel().getSources().size() > 0) { + if (!query.getModel().getSources().isEmpty()) { // Add properties if the source list is set explicitly, but do not modify otherwise addParametersForIncludedSources(pageSources, query); return; @@ -208,7 +208,7 @@ public class PageTemplateSearcher extends Searcher { private void addParametersForIncludedSources(Set sources, Query query) { for (Source source : sources) { - if (source.parameters().size() > 0 && query.getModel().getSources().contains(source.getName())) + if (!source.parameters().isEmpty() && query.getModel().getSources().contains(source.getName())) addParameters(source,query); } } diff --git a/container-search/src/main/java/com/yahoo/search/schema/SchemaInfoConfigurer.java b/container-search/src/main/java/com/yahoo/search/schema/SchemaInfoConfigurer.java index 12d5ab4fb53..84cf1744e27 100644 --- a/container-search/src/main/java/com/yahoo/search/schema/SchemaInfoConfigurer.java +++ b/container-search/src/main/java/com/yahoo/search/schema/SchemaInfoConfigurer.java @@ -46,11 +46,11 @@ class SchemaInfoConfigurer { static List toClusters(QrSearchersConfig config) { List clusters = new ArrayList<>(); - for (int i = 0; i < config.searchcluster().size(); ++i) { - String clusterName = config.searchcluster(i).name(); + for (var searchCluster : config.searchcluster()) { + String clusterName = searchCluster.name(); var clusterInfo = new Cluster.Builder(clusterName); - clusterInfo.setStreaming(config.searchcluster(i).indexingmode() == QrSearchersConfig.Searchcluster.Indexingmode.Enum.STREAMING); - for (var schemaDef : config.searchcluster(i).searchdef()) + clusterInfo.setStreaming(searchCluster.indexingmode() == QrSearchersConfig.Searchcluster.Indexingmode.Enum.STREAMING); + for (var schemaDef : searchCluster.searchdef()) clusterInfo.addSchema(schemaDef); clusters.add(clusterInfo.build()); } diff --git a/container-search/src/main/java/com/yahoo/search/yql/MinimalQueryInserter.java b/container-search/src/main/java/com/yahoo/search/yql/MinimalQueryInserter.java index 7ce629df8b1..0116d668d48 100644 --- a/container-search/src/main/java/com/yahoo/search/yql/MinimalQueryInserter.java +++ b/container-search/src/main/java/com/yahoo/search/yql/MinimalQueryInserter.java @@ -121,7 +121,7 @@ public class MinimalQueryInserter extends Searcher { for (VespaGroupingStep step : parser.getGroupingSteps()) GroupingQueryParser.createGroupingRequestIn(query, step.getOperation(), step.continuations()); - if (parser.getYqlSources().size() == 0) { + if (parser.getYqlSources().isEmpty()) { query.getModel().getSources().clear(); } else { query.getModel().getSources().addAll(parser.getYqlSources()); -- cgit v1.2.3