diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/IndexFacts.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/prelude/IndexFacts.java | 98 |
1 files changed, 36 insertions, 62 deletions
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 92ce6abb319..88a37ea5a02 100644 --- a/container-search/src/main/java/com/yahoo/prelude/IndexFacts.java +++ b/container-search/src/main/java/com/yahoo/prelude/IndexFacts.java @@ -6,11 +6,11 @@ import com.yahoo.search.Query; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.TreeSet; import static com.yahoo.text.Lowercase.toLowerCase; @@ -32,16 +32,6 @@ public class IndexFacts { private Map<String, List<String>> 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; - } - } - /** A Map of all known search definitions indexed by name */ private Map<String, SearchDefinition> searchDefinitions = new LinkedHashMap<>(); @@ -110,34 +100,32 @@ public class IndexFacts { private boolean isIndexFromDocumentTypes(String indexName, List<String> documentTypes) { if ( ! isInitialized()) return true; - if (documentTypes.isEmpty()) { - return unionSearchDefinition.getIndex(indexName) != null; - } + if (documentTypes.isEmpty()) return unionSearchDefinition.getIndex(indexName) != null; - DocumentTypeListOffset sd = chooseSearchDefinition(documentTypes, 0); - while (sd != null) { - Index index = sd.searchDefinition.getIndex(indexName); - if (index != null) { - return true; + for (String docName : documentTypes) { + SearchDefinition sd = searchDefinitions.get(docName); + if (sd != null) { + Index index = sd.getIndex(indexName); + if (index != null) return true; } - sd = chooseSearchDefinition(documentTypes, sd.offset); } - return false; } private String getCanonicNameFromDocumentTypes(String indexName, List<String> documentTypes) { if (!isInitialized()) return indexName; + String lowerCased = toLowerCase(indexName); if (documentTypes.isEmpty()) { - Index index = unionSearchDefinition.getIndexByLowerCase(toLowerCase(indexName)); + Index index = unionSearchDefinition.getIndexByLowerCase(lowerCased); return index == null ? indexName : index.getName(); } - DocumentTypeListOffset sd = chooseSearchDefinition(documentTypes, 0); - while (sd != null) { - Index index = sd.searchDefinition.getIndexByLowerCase(toLowerCase(indexName)); - if (index != null) return index.getName(); - sd = chooseSearchDefinition(documentTypes, sd.offset); + for (String docName : documentTypes) { + SearchDefinition sd = searchDefinitions.get(docName); + if (sd != null) { + Index index = sd.getIndexByLowerCase(lowerCased); + if (index != null) return index.getName(); + } } return indexName; } @@ -158,13 +146,12 @@ public class IndexFacts { return index; } - DocumentTypeListOffset sd = chooseSearchDefinition(documentTypes, 0); - - while (sd != null) { - Index index = sd.searchDefinition.getIndex(canonicName); - - if (index != null) return index; - sd = chooseSearchDefinition(documentTypes, sd.offset); + for (String docName : documentTypes) { + SearchDefinition sd = searchDefinitions.get(docName); + if (sd != null) { + Index index = sd.getIndex(canonicName); + if (index != null) return index; + } } return Index.nullIndex; } @@ -187,7 +174,7 @@ public class IndexFacts { * Given a search list which is a mixture of document types and cluster * names, and a restrict list which is a list of document types, return a * set of all valid document types for this combination. Most use-cases for - * fetching index settings will involve calling this method with the the + * fetching index settings will involve calling this method with the * incoming query's {@link com.yahoo.search.query.Model#getSources()} and * {@link com.yahoo.search.query.Model#getRestrict()} as input parameters * before calling any other method of this class. @@ -196,20 +183,20 @@ public class IndexFacts { * @param restrict the restrict list for a query * @return a (possibly empty) set of valid document types */ - private Set<String> resolveDocumentTypes(Collection<String> sources, Collection<String> restrict, + private Set<String> resolveDocumentTypes(Collection<String> sources, Set<String> restrict, Set<String> candidateDocumentTypes) { sources = emptyCollectionIfNull(sources); - restrict = emptyCollectionIfNull(restrict); + restrict = emptySetIfNull(restrict); if (sources.isEmpty()) { if ( ! restrict.isEmpty()) { - return new TreeSet<>(restrict); + return Set.copyOf(restrict); } else { return candidateDocumentTypes; } } - Set<String> toSearch = new TreeSet<>(); + Set<String> toSearch = new HashSet<>(); for (String source : sources) { // source: a document type or a cluster containing them List<String> clusterDocTypes = clusters.get(source); if (clusterDocTypes == null) { // source was a document type @@ -235,21 +222,8 @@ public class IndexFacts { private Collection<String> emptyCollectionIfNull(Collection<String> collection) { return collection == null ? List.of() : collection; } - - /** - * Chooses the correct search definition, default if in doubt. - * - * @return the search definition to use - */ - private DocumentTypeListOffset chooseSearchDefinition(List<String> documentTypes, int index) { - while (index < documentTypes.size()) { - String docName = documentTypes.get(index++); - SearchDefinition sd = searchDefinitions.get(docName); - if (sd != null) { - return new DocumentTypeListOffset(index, sd); - } - } - return null; + private Set<String> emptySetIfNull(Set<String> collection) { + return collection == null ? Set.of() : collection; } /** @@ -279,10 +253,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) { @@ -300,12 +270,16 @@ public class IndexFacts { return new Session(query); } - public Session newSession(Collection<String> sources, Collection<String> restrict) { + public Session newSession() { + return new Session(Set.of(), Set.of()); + } + + public Session newSession(Collection<String> sources, Set<String> restrict) { return new Session(sources, restrict); } public Session newSession(Collection<String> sources, - Collection<String> restrict, + Set<String> restrict, Set<String> candidateDocumentTypes) { return new Session(sources, restrict, candidateDocumentTypes); } @@ -323,12 +297,12 @@ public class IndexFacts { documentTypes = List.copyOf(resolveDocumentTypes(query)); } - private Session(Collection<String> sources, Collection<String> restrict) { + private Session(Collection<String> sources, Set<String> restrict) { // Assumption: Search definition name equals document name. documentTypes = List.copyOf(resolveDocumentTypes(sources, restrict, searchDefinitions.keySet())); } - private Session(Collection<String> sources, Collection<String> restrict, Set<String> candidateDocumentTypes) { + private Session(Collection<String> sources, Set<String> restrict, Set<String> candidateDocumentTypes) { documentTypes = List.copyOf(resolveDocumentTypes(sources, restrict, candidateDocumentTypes)); } |