diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-10-15 14:43:50 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-10-15 14:43:50 +0200 |
commit | a42bb2fc386f8e9145f2ada76ff0c5b32af17263 (patch) | |
tree | 427f45233312bc2ff3bb87ac890ac78ad8060ed1 /container-search | |
parent | 2bf905fe31ae8f674655a6228bf93840289a1fd1 (diff) |
Avoid deprecated method
Diffstat (limited to 'container-search')
4 files changed, 86 insertions, 63 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/Index.java b/container-search/src/main/java/com/yahoo/prelude/Index.java index c9781fba76d..ae8aad3956e 100644 --- a/container-search/src/main/java/com/yahoo/prelude/Index.java +++ b/container-search/src/main/java/com/yahoo/prelude/Index.java @@ -4,7 +4,10 @@ package com.yahoo.prelude; import com.yahoo.language.process.StemMode; +import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.Set; @@ -43,6 +46,10 @@ public class Index { public static final Index nullIndex = new Index("(null)"); private String name; + + private List<String> aliases = new ArrayList<>(); + + // The state resulting from adding commands to this (using addCommand) private boolean uriIndex = false; private boolean hostIndex = false; private StemMode stemMode = StemMode.NONE; @@ -60,28 +67,28 @@ public class Index { private boolean numerical = false; private long predicateUpperBound = Long.MAX_VALUE; private long predicateLowerBound = Long.MIN_VALUE; - - /** - * True if this is an <i>exact</i> index - which should match - * tokens containing any characters - */ + /** True if this is an <i>exact</i> index - which should match tokens containing any characters */ private boolean exact = false; - private boolean isNGram = false; private int gramSize=2; - - /** - * The string terminating an exact token in this index, - * or null to use the default (space) - */ + /** The string terminating an exact token in this index, or null to use the default (space) */ private String exactTerminator = null; + /** Commands which are not coverted into a field */ private Set<String> commands = new java.util.HashSet<>(); + /** All the commands added to this, including those converted to fields above */ + private List<String> allCommands = new java.util.ArrayList<>(); + public Index(String name) { this.name = name; } + public void addAlias(String alias) { aliases.add(alias); } + + /** Returns an unmodifiable list of the aliases of this index (not including the index proper name) */ + public List<String> aliases() { return Collections.unmodifiableList(aliases); } + /** * Returns the canonical name of this index, unless it * is the null index, which doesn't have a canonical name @@ -128,6 +135,8 @@ public class Index { /** Adds a type or untyped command string to this */ public Index addCommand(String commandString) { + allCommands.add(commandString); + if ("fullurl".equals(commandString)) { setUriIndex(true); } else if ("urlhost".equals(commandString)) { @@ -151,7 +160,7 @@ public class Index { } else if (commandString.startsWith("exact ")) { setExact(true, commandString.substring(6)); } else if (commandString.startsWith("ngram ")) { - setNGram(true,Integer.parseInt(commandString.substring(6))); + setNGram(true, Integer.parseInt(commandString.substring(6))); } else if (commandString.equals("attribute")) { setAttribute(true); } else if (commandString.equals("default-position")) { @@ -189,7 +198,6 @@ public class Index { } else { predicateUpperBound = Long.MAX_VALUE; } - } /** @@ -315,6 +323,9 @@ public class Index { public long getPredicateLowerBound() { return predicateLowerBound; } + /** Returns all the literal command strings given as arguments to addCommand in this instance */ + public List<String> allCommands() { return allCommands; } + @Override public String toString() { return "index '" + getName() + "'"; 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 4be337f7f32..dbd6c03a157 100644 --- a/container-search/src/main/java/com/yahoo/prelude/IndexModel.java +++ b/container-search/src/main/java/com/yahoo/prelude/IndexModel.java @@ -2,11 +2,13 @@ package com.yahoo.prelude; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.logging.Logger; +import java.util.stream.Collectors; import com.yahoo.log.LogLevel; import com.yahoo.search.config.IndexInfoConfig; @@ -29,6 +31,16 @@ public final class IndexModel { /** * Use IndexModel as a pure wrapper for the parameters given. */ + public IndexModel(Map<String, List<String>> masterClusters, Collection<SearchDefinition> searchDefinitions) { + this.masterClusters = masterClusters; + this.searchDefinitions = searchDefinitions.stream().collect(Collectors.toMap(sd -> sd.getName(), sd -> sd)); + this.unionSearchDefinition = createUnionOf(searchDefinitions); + } + + /** + * Use IndexModel as a pure wrapper for the parameters given. + */ + // TODO: Deprecate on Vespa 7 and remove on Vespa 8 public IndexModel(Map<String, List<String>> masterClusters, Map<String, SearchDefinition> searchDefinitions, SearchDefinition unionSearchDefinition) { @@ -75,6 +87,7 @@ public final class IndexModel { @SuppressWarnings("deprecation") private void setDefinitions(IndexInfoConfig c) { + // TODO: Use createUnionOf to create the union search definition searchDefinitions = new HashMap<>(); unionSearchDefinition = new SearchDefinition(IndexFacts.unionName); @@ -105,31 +118,37 @@ public final class IndexModel { sd.addAlias(aliasString, indexString); try { unionSearchDefinition.addAlias(aliasString, indexString); - } catch (RuntimeException e) { - log.log(LogLevel.WARNING, - "Ignored the alias \"" - + aliasString - + "\" for \"" - + indexString - + "\" in the union of all search definitions," - + " source has to be explicitly set to \"" - + sd.getName() - + "\" for that alias to work.", e); + } catch (IllegalArgumentException e) { + log.log(LogLevel.WARNING, "Ignored the alias '" + aliasString + "' for '" + indexString + + "' in the union of all search definitions, source has to be " + + "explicitly set to '" + sd.getName() + "' for that alias to work.", e); } } } } - public Map<String, List<String>> getMasterClusters() { - return masterClusters; - } + @SuppressWarnings("deprecation") + private SearchDefinition createUnionOf(Collection<SearchDefinition> searchDefinitions) { + SearchDefinition union = new SearchDefinition(IndexFacts.unionName); + + for (SearchDefinition sd : searchDefinitions) { + for (Index index : sd.indices().values()) { + for (String command : index.allCommands()) + union.addCommand(index.getName(), command); + for (String alias : index.aliases()) + union.addAlias(alias, index.getName()); + } - public Map<String, SearchDefinition> getSearchDefinitions() { - return searchDefinitions; + } + union.fillMatchGroups(); + return union; } - public SearchDefinition getUnionSearchDefinition() { - return unionSearchDefinition; - } + public Map<String, List<String>> getMasterClusters() { return masterClusters; } + + public Map<String, SearchDefinition> getSearchDefinitions() { return searchDefinitions; } + + // TODO: Deprecate on Vespa 7 and remove on Vespa 8 + public SearchDefinition getUnionSearchDefinition() { return unionSearchDefinition; } } diff --git a/container-search/src/main/java/com/yahoo/prelude/SearchDefinition.java b/container-search/src/main/java/com/yahoo/prelude/SearchDefinition.java index 47becde7b19..aa76bd5912a 100644 --- a/container-search/src/main/java/com/yahoo/prelude/SearchDefinition.java +++ b/container-search/src/main/java/com/yahoo/prelude/SearchDefinition.java @@ -22,10 +22,7 @@ public class SearchDefinition { /** A map of all indices in this search definition, indexed by name */ private Map<String, Index> indices = new HashMap<>(); - /* - * A map of all indices in this search definition, indexed by lower cased - * name. - */ + /* A map of all indices in this search definition, indexed by lower cased name. */ private Map<String, Index> lowerCase = new HashMap<>(); private String defaultPosition; @@ -49,29 +46,23 @@ public class SearchDefinition { } public void addAlias(String alias, String indexName) { - Index old; - - if ((old = indices.get(alias)) != null) { - if (old.getName().equals(indexName)) { - return; - } else { - throw new IllegalArgumentException("Tried adding the alias \"" - + alias + "\" for the index name \"" + indexName - + "\" when the name \"" + alias - + "\" already maps to \"" + old.getName() + "\"."); - } + Index old = indices.get(alias); + if (old != null) { + if (old.getName().equals(indexName)) return; + throw new IllegalArgumentException("Tried adding the alias '" + alias + "' for the index name '" + + indexName + "' when the name '" + alias + + "' already maps to '" + old.getName() + "'"); } Index index = indices.get(indexName); - if (index == null) { - throw new IllegalArgumentException("Failed adding alias \"" + alias - + "\" for the index name \"" + indexName - + "\" as there is no index with that name available."); - } + if (index == null) + throw new IllegalArgumentException("Failed adding alias '" + alias + "' for the index name '" + indexName + + "' as there is no index with that name available."); indices.put(alias, index); + index.addAlias(alias); + String lca = toLowerCase(alias); - if (lowerCase.get(lca) == null) { + if (lowerCase.get(lca) == null) lowerCase.put(lca, index); - } } public Index getIndex(String name) { @@ -120,6 +111,4 @@ public class SearchDefinition { } } - - } diff --git a/container-search/src/test/java/com/yahoo/prelude/test/IndexFactsTestCase.java b/container-search/src/test/java/com/yahoo/prelude/test/IndexFactsTestCase.java index fa27d4d47f5..cc7173674a7 100644 --- a/container-search/src/test/java/com/yahoo/prelude/test/IndexFactsTestCase.java +++ b/container-search/src/test/java/com/yahoo/prelude/test/IndexFactsTestCase.java @@ -3,12 +3,14 @@ package com.yahoo.prelude.test; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; +import com.google.common.collect.ImmutableList; import com.yahoo.config.subscription.ConfigGetter; import com.yahoo.container.QrSearchersConfig; import com.yahoo.search.config.IndexInfoConfig; @@ -45,7 +47,14 @@ public class IndexFactsTestCase { private IndexFacts createIndexFacts() { ConfigGetter<IndexInfoConfig> getter = new ConfigGetter<>(IndexInfoConfig.class); IndexInfoConfig config = getter.getConfig(INDEXFACTS_TESTING); + return new IndexFacts(new IndexModel(config, createClusters())); + } + + private IndexFacts createIndexFacts(Collection<SearchDefinition> searchDefinitions) { + return new IndexFacts(new IndexModel(createClusters(), searchDefinitions)); + } + private Map<String, List<String>> createClusters() { List<String> clusterOne = new ArrayList<>(); List<String> clusterTwo = new ArrayList<>(); clusterOne.addAll(Arrays.asList("one", "two")); @@ -53,9 +62,7 @@ public class IndexFactsTestCase { Map<String, List<String>> clusters = new HashMap<>(); clusters.put("clusterOne", clusterOne); clusters.put("clusterTwo", clusterTwo); - IndexFacts indexFacts = new IndexFacts(new IndexModel(config, clusters)); - - return indexFacts; + return clusters; } @Test @@ -88,16 +95,13 @@ public class IndexFactsTestCase { sd.addCommand("c", "default-position"); assertTrue(sd.getDefaultPosition().equals("c")); - SearchDefinition sd2 = new SearchDefinition("sd"); + SearchDefinition sd2 = new SearchDefinition("sd2"); sd2.addIndex(new Index("b").addCommand("any")); assertNull(sd2.getDefaultPosition()); sd2.addIndex(a); assertTrue(sd2.getDefaultPosition().equals("a")); - Map<String,SearchDefinition> m = new TreeMap<>(); - m.put(sd.getName(), sd); - IndexFacts indexFacts = createIndexFacts(); - indexFacts.setSearchDefinitions(m,sd2); + IndexFacts indexFacts = createIndexFacts(ImmutableList.of(sd, sd2)); assertTrue(indexFacts.getDefaultPosition(null).equals("a")); assertTrue(indexFacts.getDefaultPosition("sd").equals("c")); } |