diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-07-02 18:00:39 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-07-02 18:00:39 +0200 |
commit | 8934b4c48616352468124fd22227f4668a0f3706 (patch) | |
tree | a5e744264543d35a4e25d066db66467df8d103d8 /container-search | |
parent | 47d3107c262de1a0ad69110d81759d30951636b2 (diff) |
Add test, faster size, and order primary/secondary as expected.
Diffstat (limited to 'container-search')
3 files changed, 65 insertions, 2 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/ChainedMap.java b/container-search/src/main/java/com/yahoo/search/query/profile/ChainedMap.java index 7da5441129c..3ec9a3c41b8 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/ChainedMap.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/ChainedMap.java @@ -23,7 +23,17 @@ class ChainedMap<K, V> implements Map<K, V> { @Override public int size() { - return keySet().size(); + return (primary.size() >= secondary.size()) + ? countUnique(primary, secondary) + : countUnique(secondary, primary); + } + + private int countUnique(Map<K, V> large, Map<K,V> small) { + int size = large.size(); + for (K key : small.keySet()) { + if ( ! large.containsKey(key)) size++; + } + return size; } @Override diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java b/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java index da1eb5f7ecc..224f75e7034 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java @@ -300,7 +300,7 @@ public class QueryProfileProperties extends Properties { if (zoneInfo == ZoneInfo.defaultInfo()) return context; if (context == null || context.isEmpty()) return zoneContext; if (context == zoneContext) return context; - return new ChainedMap(zoneContext, context); + return new ChainedMap(context, zoneContext); } private boolean reachableTypesAreComplete(CompoundName prefix, CompiledQueryProfile profile, StringBuilder firstMissingName, Map<String,String> context) { diff --git a/container-search/src/test/java/com/yahoo/search/query/profile/ChainedMapTestCase.java b/container-search/src/test/java/com/yahoo/search/query/profile/ChainedMapTestCase.java new file mode 100644 index 00000000000..87459c22d47 --- /dev/null +++ b/container-search/src/test/java/com/yahoo/search/query/profile/ChainedMapTestCase.java @@ -0,0 +1,53 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.profile; + +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author baldersheim + */ +public class ChainedMapTestCase { + @Test + public void testIsEmpty() { + assertTrue(new ChainedMap<String, String>(Map.of(), Map.of()).isEmpty()); + assertFalse(new ChainedMap<>(Map.of("k", "v"), Map.of()).isEmpty()); + assertFalse(new ChainedMap<>(Map.of(), Map.of("k", "v")).isEmpty()); + assertFalse(new ChainedMap<>(Map.of("k", "v"), Map.of("k", "v")).isEmpty()); + } + @Test + public void testSize() { + assertEquals(0, new ChainedMap<String, String>(Map.of(), Map.of()).size()); + assertEquals(1, new ChainedMap<>(Map.of("k", "v"), Map.of()).size()); + assertEquals(1, new ChainedMap<>(Map.of(), Map.of("k", "v")).size()); + assertEquals(1, new ChainedMap<>(Map.of("k", "v"), Map.of("k", "v")).size()); + assertEquals(2, new ChainedMap<>(Map.of("k", "v"), Map.of("K", "v")).size()); + assertEquals(2, new ChainedMap<>(Map.of("k", "v"), Map.of("K", "v", "k", "v")).size()); + } + @Test + public void testGetUsesBoth() { + Map<String, String> a = Map.of("a", "a_1"); + Map<String, String> b = Map.of("b", "b_1"); + Map<String, String> ab = Map.of("a", "a_2", "b", "b_2"); + Map<String, String> a_b = new ChainedMap<>(a, b); + assertEquals("a_1", a_b.get("a")); + assertEquals("b_1", a_b.get("b")); + + Map<String, String> b_a = new ChainedMap<>(a, b); + assertEquals("a_1", b_a.get("a")); + assertEquals("b_1", b_a.get("b")); + + Map<String, String> a_ab = new ChainedMap<>(a, ab); + assertEquals("a_1", a_ab.get("a")); + assertEquals("b_2", a_ab.get("b")); + + Map<String, String> ab_a = new ChainedMap<>(ab, a); + assertEquals("a_2", ab_a.get("a")); + assertEquals("b_2", ab_a.get("b")); + } +} |