diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2019-10-28 13:32:35 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2019-10-28 13:32:35 +0100 |
commit | f55ece33c8f97ad8c51f58ebdfed15a7d95e89dc (patch) | |
tree | e0345a196aa8cff5d4eb344e6ff794e738229ba8 /container-search/src/main/java/com/yahoo/search/query/profile/compiled | |
parent | a1ea620f6e1aaa13b0ba916377bfa1d2c749b3f7 (diff) |
Retain information about sources when compiling
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/query/profile/compiled')
4 files changed, 62 insertions, 29 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfile.java b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfile.java index ea85a2be242..06a6ac48cb6 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfile.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfile.java @@ -12,7 +12,6 @@ import com.yahoo.search.query.profile.types.QueryProfileType; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -31,7 +30,7 @@ public class CompiledQueryProfile extends AbstractComponent implements Cloneable private final QueryProfileType type; /** The values of this */ - private final DimensionalMap<CompoundName, Object> entries; + private final DimensionalMap<CompoundName, ValueWithSource> entries; /** Keys which have a type in this */ private final DimensionalMap<CompoundName, QueryProfileType> types; @@ -47,7 +46,7 @@ public class CompiledQueryProfile extends AbstractComponent implements Cloneable */ public CompiledQueryProfile(ComponentId id, QueryProfileType type, - DimensionalMap<CompoundName, Object> entries, + DimensionalMap<CompoundName, ValueWithSource> entries, DimensionalMap<CompoundName, QueryProfileType> types, DimensionalMap<CompoundName, Object> references, DimensionalMap<CompoundName, Object> unoverridables, @@ -131,11 +130,14 @@ public class CompiledQueryProfile extends AbstractComponent implements Cloneable */ public Map<String, Object> listValues(CompoundName prefix, Map<String, String> context, Properties substitution) { Map<String, Object> values = new HashMap<>(); - for (Map.Entry<CompoundName, DimensionalValue<Object>> entry : entries.entrySet()) { + for (Map.Entry<CompoundName, DimensionalValue<ValueWithSource>> entry : entries.entrySet()) { if ( entry.getKey().size() <= prefix.size()) continue; if ( ! entry.getKey().hasPrefix(prefix)) continue; - Object value = entry.getValue().get(context); + ValueWithSource valueWithSource = entry.getValue().get(context); + if (valueWithSource == null) continue; + + Object value = valueWithSource.value(); if (value == null) continue; value = substitute(value, context, substitution); @@ -155,7 +157,9 @@ public class CompiledQueryProfile extends AbstractComponent implements Cloneable return get(new CompoundName(name), context, substitution); } public final Object get(CompoundName name, Map<String, String> context, Properties substitution) { - return substitute(entries.get(name, context), context, substitution); + ValueWithSource value = entries.get(name, context); + if (value == null) return null; + return substitute(value.value(), context, substitution); } private Object substitute(Object value, Map<String, String> context, Properties substitution) { diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/DimensionalValue.java b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/DimensionalValue.java index 50d0a2de46f..b5481059ac0 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/DimensionalValue.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/DimensionalValue.java @@ -168,12 +168,14 @@ public class DimensionalValue<VALUE> { return value; } + // TODO: Move this @SuppressWarnings("unchecked") private VALUE substituteIfRelative(VALUE value, DimensionBinding variant, Map<CompoundName, DimensionalValue.Builder<VALUE>> entries) { - if (value instanceof SubstituteString) { - SubstituteString substitute = (SubstituteString)value; + if (value instanceof ValueWithSource && ((ValueWithSource)value).value() instanceof SubstituteString) { + ValueWithSource valueWithSource = (ValueWithSource)value; + SubstituteString substitute = (SubstituteString)valueWithSource.value(); if (substitute.hasRelative()) { List<SubstituteString.Component> resolvedComponents = new ArrayList<>(substitute.components().size()); for (SubstituteString.Component component : substitute.components()) { @@ -184,14 +186,14 @@ public class DimensionalValue<VALUE> { throw new IllegalArgumentException("Could not resolve local substitution '" + relativeComponent.fieldName() + "' in variant " + variant); - String resolved = substituteValues.valueFor(variant).toString(); - resolvedComponents.add(new SubstituteString.StringComponent(resolved)); + ValueWithSource resolved = (ValueWithSource)substituteValues.valueFor(variant); + resolvedComponents.add(new SubstituteString.StringComponent(resolved.value().toString())); } else { resolvedComponents.add(component); } } - return (VALUE)new SubstituteString(resolvedComponents, substitute.stringValue()); + return (VALUE)valueWithSource.withValue(new SubstituteString(resolvedComponents, substitute.stringValue())); } } return value; diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/ValueSource.java b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/ValueSource.java deleted file mode 100644 index 17326efb220..00000000000 --- a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/ValueSource.java +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.search.query.profile.compiled; - -import com.yahoo.search.query.profile.DimensionValues; -import com.yahoo.search.query.profile.QueryProfile; - -/** - * The source of a value in a compiled query profile - * - * @author bratseth - */ -public class ValueSource { - - public ValueSource(QueryProfile owner, DimensionValues variant) { - - } - -} diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/ValueWithSource.java b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/ValueWithSource.java new file mode 100644 index 00000000000..c2ce34c3f47 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/ValueWithSource.java @@ -0,0 +1,45 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.profile.compiled; + +import com.yahoo.search.query.profile.DimensionValues; + +import java.util.Optional; + +/** + * A value in a query profile with information about its source. + * + * @author bratseth + */ +public class ValueWithSource { + + private final Object value; + + /** The id of the query profile having a value */ + private final String ownerId; + + /** The dimension values specifying a variant in that profile, or null if it is not in a variant */ + private final DimensionValues variant; + + public ValueWithSource(Object value, String ownerId, DimensionValues variant) { + this.value = value; + this.ownerId = ownerId; + this.variant = variant; + } + + public Object value() { return value; } + + public String ownerId() { return ownerId; } + + public ValueWithSource withValue(Object value) { + return new ValueWithSource(value, ownerId, variant); + } + + /** Returns the variant having this value, or empty if it's not in a variant */ + public Optional<DimensionValues> variant() { return Optional.ofNullable(variant); } + + @Override + public String toString() { + return value + " from " + ownerId + ( variant != null ? " variant " + variant : ""); + } + +} |