summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java98
1 files changed, 24 insertions, 74 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java
index f1798948361..9c735a031d7 100644
--- a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java
+++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java
@@ -1,7 +1,6 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query.parser;
-import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.vespa.configdefinition.SpecialtokensConfig;
import com.yahoo.vespa.configdefinition.SpecialtokensConfig.Tokenlist;
import com.yahoo.vespa.configdefinition.SpecialtokensConfig.Tokenlist.Tokens;
@@ -11,55 +10,36 @@ 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;
/**
- * A <i>registry</i> which is responsible for knowing the current
- * set of special tokens. The default registry returns empty token lists
- * for all names. Usage of this registry is multithread safe.
+ * A registry which is responsible for knowing the current
+ * set of special tokens.Usage of this registry is multithread safe.
*
* @author bratseth
*/
public class SpecialTokenRegistry {
- /** The log of this */
- private static final Logger log = Logger.getLogger(SpecialTokenRegistry.class.getName());
-
- private static final SpecialTokens nullSpecialTokens = new SpecialTokens();
-
/**
* The current special token lists, indexed on name.
* These lists are unmodifiable and used directly by clients of this
*/
- private Map<String, SpecialTokens> specialTokenMap = new HashMap<>();
+ private Map<String, SpecialTokens> specialTokenMap;
private boolean frozen = false;
- /**
- * Creates an empty special token registry which
- * does not subscribe to any configuration
- */
- public SpecialTokenRegistry() {}
-
- /**
- * Create a special token registry which subscribes to the specialtokens
- * configuration. Only used for testing.
- */
- public SpecialTokenRegistry(String configId) {
- try {
- build(new ConfigGetter<>(SpecialtokensConfig.class).getConfig(configId));
- } catch (Exception e) {
- log.config("No special tokens are configured (" + e.getMessage() + ")");
- }
+ /** Creates an empty special token registry */
+ public SpecialTokenRegistry() {
+ this(List.of());
}
- /**
- * Create a special token registry from a configuration object. This is the production code path.
- */
+ /** Create a special token registry from a configuration object. */
public SpecialTokenRegistry(SpecialtokensConfig config) {
- if (config != null) {
- build(config);
- }
+ this(specialTokensFrom(config));
+ }
+
+ public SpecialTokenRegistry(List<SpecialTokens> specialTokensList) {
+ specialTokenMap = specialTokensList.stream().collect(Collectors.toMap(t -> t.name(), t -> t));
freeze();
}
@@ -67,35 +47,19 @@ public class SpecialTokenRegistry {
frozen = true;
}
- private void build(SpecialtokensConfig config) {
- List<SpecialTokens> list = new ArrayList<>();
+ private static List<SpecialTokens> specialTokensFrom(SpecialtokensConfig config) {
+ List<SpecialTokens> specialTokensList = new ArrayList<>();
for (Iterator<Tokenlist> i = config.tokenlist().iterator(); i.hasNext();) {
- Tokenlist tokenList = i.next();
- SpecialTokens tokens = new SpecialTokens(tokenList.name());
+ Tokenlist tokenListConfig = i.next();
- for (Iterator<Tokens> j = tokenList.tokens().iterator(); j.hasNext();) {
- Tokens token = j.next();
- tokens.addSpecialToken(token.token(), token.replace());
+ List<SpecialTokens.Token> tokenList = new ArrayList<>();
+ for (Iterator<Tokens> j = tokenListConfig.tokens().iterator(); j.hasNext();) {
+ Tokens tokenConfig = j.next();
+ tokenList.add(new SpecialTokens.Token(tokenConfig.token(), tokenConfig.replace()));
}
- tokens.freeze();
- list.add(tokens);
+ specialTokensList.add(new SpecialTokens(tokenListConfig.name(), tokenList));
}
- addSpecialTokens(list);
- }
-
- /**
- * Adds a SpecialTokens instance to the registry. That is, add the
- * tokens contained for the name of the SpecialTokens instance
- * given.
- *
- * @param specialTokens the SpecialTokens object to add
- */
- public void addSpecialTokens(SpecialTokens specialTokens) {
- ensureNotFrozen();
- List<SpecialTokens> list = new ArrayList<>();
- list.add(specialTokens);
- addSpecialTokens(list);
-
+ return specialTokensList;
}
private void ensureNotFrozen() {
@@ -104,14 +68,6 @@ public class SpecialTokenRegistry {
}
}
- private void addSpecialTokens(List<SpecialTokens> list) {
- HashMap<String,SpecialTokens> tokens = new HashMap<>(specialTokenMap);
- for(SpecialTokens t: list) {
- tokens.put(t.getName(),t);
- }
- specialTokenMap = tokens;
- }
-
/**
* Returns the list of special tokens for a given name.
*
@@ -122,15 +78,9 @@ public class SpecialTokenRegistry {
* has no special tokens
*/
public SpecialTokens getSpecialTokens(String name) {
- if (name == null || name.trim().equals("")) {
+ if (name == null || name.trim().equals(""))
name = "default";
- }
- SpecialTokens specialTokens = specialTokenMap.get(name);
-
- if (specialTokens == null) {
- return nullSpecialTokens;
- }
- return specialTokens;
+ return specialTokenMap.getOrDefault(name, SpecialTokens.empty());
}
}