From 845c1ba121d6b35203ec8817f6dae6c9f6d24f95 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 14 Apr 2020 14:04:29 +0200 Subject: Shortcut corner cases --- .../search/query/profile/DimensionBinding.java | 47 ++++++++-------------- .../search/query/profile/QueryProfileCompiler.java | 3 -- .../profile/config/test/XmlReadingTestCase.java | 2 - 3 files changed, 16 insertions(+), 36 deletions(-) (limited to 'container-search') diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/DimensionBinding.java b/container-search/src/main/java/com/yahoo/search/query/profile/DimensionBinding.java index 3ab2d2774cf..0cbfdc5dca0 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/DimensionBinding.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/DimensionBinding.java @@ -3,7 +3,6 @@ package com.yahoo.search.query.profile; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -22,7 +21,7 @@ public class DimensionBinding { private DimensionValues values; /** The binding from those dimensions to values, and possibly other values */ - private Map context; // TODO: This is not needed any more + private Map context; public static final DimensionBinding nullBinding = new DimensionBinding(Collections.unmodifiableList(Collections.emptyList()), DimensionValues.empty, null); @@ -34,13 +33,13 @@ public class DimensionBinding { private boolean containsAllNulls; // NOTE: Map must be ordered - public static DimensionBinding createFrom(Map values) { + public static DimensionBinding createFrom(Map values) { return createFrom(new ArrayList<>(values.keySet()), values); } /** Creates a binding from a variant and a context. Any of the arguments may be null. */ // NOTE: Map must be ordered - public static DimensionBinding createFrom(List dimensions, Map context) { + public static DimensionBinding createFrom(List dimensions, Map context) { if (dimensions == null || dimensions.size() == 0) { if (context == null) return nullBinding; if (dimensions == null) return new DimensionBinding(null, DimensionValues.empty, context); // Null, but must preserve context @@ -51,7 +50,7 @@ public class DimensionBinding { /** Creates a binding from a variant and a context. Any of the arguments may be null. */ public static DimensionBinding createFrom(List dimensions, DimensionValues dimensionValues) { - if (dimensionValues==null || dimensionValues == DimensionValues.empty) return nullBinding; + if (dimensionValues == null || dimensionValues == DimensionValues.empty) return nullBinding; // If null, preserve raw material for creating a context later (in createFor) if (dimensions == null) return new DimensionBinding(null, dimensionValues, null); @@ -62,7 +61,6 @@ public class DimensionBinding { /** Returns a binding for a (possibly) new set of variants. Variants may be null, but not bindings */ public DimensionBinding createFor(List newDimensions) { if (newDimensions == null) return this; // Note: Not necessarily null - if no new variants then keep the existing binding - // if (this.context==null && values.length==0) return nullBinding; // No data from which to create a non-null binding if (this.dimensions == newDimensions) return this; // Avoid creating a new object if the dimensions are the same Map context = this.context; @@ -110,10 +108,10 @@ public class DimensionBinding { * Dimensions which are not set in this context get a null value. */ private static DimensionValues extractDimensionValues(List dimensions, Map context) { - String[] dimensionValues=new String[dimensions.size()]; - if (context==null || context.size()==0) return DimensionValues.createFrom(dimensionValues); - for (int i=0; ivalue) as a map */ - private Map asMap() { - Map map = new LinkedHashMap<>(); - for (int i = 0; i < Math.min(dimensions.size(), values.size()); i++) { - if (values.getValues()[i] != null) - map.put(dimensions.get(i), values.getValues()[i]); - } - return map; - } - /** * Returns a combined list of dimensions from two separate lists, * or null if they are incompatible. @@ -155,6 +143,10 @@ public class DimensionBinding { * (or return null if impossible). */ private List combineDimensions(List d1, List d2) { + if (d1.equals(d2)) return d1; + if (d1.isEmpty()) return d2; + if (d2.isEmpty()) return d1; + List combined = new ArrayList<>(); int d1Index = 0, d2Index = 0; while (d1Index < d1.size() && d2Index < d2.size()) { @@ -186,6 +178,8 @@ public class DimensionBinding { * or null if they are incompatible. */ private Map combineValues(Map m1, Map m2) { + if (m1.isEmpty()) return m2; + if (m2.isEmpty()) return m1; Map combinedValues = null; for (Map.Entry m2Entry : m2.entrySet()) { if (m2Entry.getValue() == null) continue; @@ -199,16 +193,7 @@ public class DimensionBinding { return combinedValues == null ? m1 : combinedValues; } - private boolean intersects(List l1, List l2) { - for (String l1Item : l1) - if (l2.contains(l1Item)) - return true; - return false; - } - - /** - * Returns true if this == invalidBinding - */ + /** Returns true if this == invalidBinding */ public boolean isInvalid() { return this == invalidBinding; } @Override @@ -228,7 +213,7 @@ public class DimensionBinding { /** Two bindings are equal if they contain the same dimensions and the same non-null values */ @Override public boolean equals(Object o) { - if (o==this) return true; + if (o == this) return true; if (! (o instanceof DimensionBinding)) return false; DimensionBinding other = (DimensionBinding)o; if ( ! this.dimensions.equals(other.dimensions)) return false; diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileCompiler.java b/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileCompiler.java index c9dc2cdfcb0..f1fc90dee09 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileCompiler.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileCompiler.java @@ -10,7 +10,6 @@ import com.yahoo.search.query.profile.types.QueryProfileType; import java.util.HashSet; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.logging.Logger; @@ -42,10 +41,8 @@ public class QueryProfileCompiler { Set variants = collectVariants(CompoundName.empty, in, DimensionBinding.nullBinding); variants.add(new DimensionBindingForPath(DimensionBinding.nullBinding, CompoundName.empty)); // if this contains no variants log.fine(() -> "Compiling " + in.toString() + " having " + variants.size() + " variants"); - //System.out.println(" Compiling " + in.toString() + " having " + variants.size() + " variants"); for (DimensionBindingForPath variant : variants) { log.finer(() -> " Compiling variant " + variant); - //System.out.println(" Compiling variant " + variant); for (Map.Entry entry : in.visitValues(variant.path(), variant.binding().getContext()).valuesWithSource().entrySet()) { values.put(variant.path().append(entry.getKey()), variant.binding(), entry.getValue()); } diff --git a/container-search/src/test/java/com/yahoo/search/query/profile/config/test/XmlReadingTestCase.java b/container-search/src/test/java/com/yahoo/search/query/profile/config/test/XmlReadingTestCase.java index e9f7ff24d42..6d7d6f38e99 100644 --- a/container-search/src/test/java/com/yahoo/search/query/profile/config/test/XmlReadingTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/query/profile/config/test/XmlReadingTestCase.java @@ -299,8 +299,6 @@ public class XmlReadingTestCase { String queryString="tiled?query=india&queryProfile=myprofile&source.common.intl=tw&source.common.mode=adv"; Query query=new Query(HttpRequest.createTestRequest(queryString, Method.GET), registry.getComponent("myprofile")); - for (Map.Entry e : query.properties().listProperties().entrySet()) - System.out.println(e); assertEquals("news",query.properties().listProperties().get("source.common.provider")); assertEquals("news",query.properties().get("source.common.provider")); } -- cgit v1.2.3