Repository: phoenix Updated Branches: refs/heads/4.x-HBase-0.98 922966218 -> e81a4cc8f
PHOENIX-2119 Do not copy underlying HBase configuration properties when connection properties are supplied Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/e81a4cc8 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/e81a4cc8 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/e81a4cc8 Branch: refs/heads/4.x-HBase-0.98 Commit: e81a4cc8f00264fafe86dc78836cf1eb2008a33e Parents: 9229662 Author: Samarth <samarth.j...@salesforce.com> Authored: Fri Jan 15 15:09:28 2016 -0800 Committer: Samarth <samarth.j...@salesforce.com> Committed: Fri Jan 15 15:09:28 2016 -0800 ---------------------------------------------------------------------- .../org/apache/phoenix/util/ReadOnlyProps.java | 40 +++++++--- .../phoenix/jdbc/ReadOnlyPropertiesTest.java | 84 ++++++++++++++++++++ 2 files changed, 111 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/e81a4cc8/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java b/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java index a6fb7a5..d6950a2 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java @@ -18,7 +18,6 @@ package org.apache.phoenix.util; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -27,11 +26,14 @@ import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.annotation.Nonnull; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Objects; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.Maps; /** @@ -39,13 +41,14 @@ import com.google.common.collect.Maps; * Read-only properties that avoids unnecessary synchronization in * java.util.Properties. * - * - * @since 1.2.2 */ public class ReadOnlyProps implements Iterable<Entry<String, String>> { private static final Logger logger = LoggerFactory.getLogger(ReadOnlyProps.class); public static final ReadOnlyProps EMPTY_PROPS = new ReadOnlyProps(); + @Nonnull private final Map<String, String> props; + @Nonnull + private final Map<String, String> overrideProps; public ReadOnlyProps(ReadOnlyProps defaultProps, Iterator<Entry<String, String>> iterator) { Map<String, String> map = new HashMap<String,String>(defaultProps.asMap()); @@ -54,6 +57,7 @@ public class ReadOnlyProps implements Iterable<Entry<String, String>> { map.put(entry.getKey(), entry.getValue()); } this.props = ImmutableMap.copyOf(map); + this.overrideProps = ImmutableMap.of(); } public ReadOnlyProps(Iterator<Entry<String, String>> iterator) { @@ -61,22 +65,31 @@ public class ReadOnlyProps implements Iterable<Entry<String, String>> { } private ReadOnlyProps() { - this.props = Collections.emptyMap(); + this.props = ImmutableMap.of(); + this.overrideProps = ImmutableMap.of(); } public ReadOnlyProps(Map<String, String> props) { this.props = ImmutableMap.copyOf(props); + this.overrideProps = ImmutableMap.of(); } - private ReadOnlyProps(ReadOnlyProps defaultProps, Properties overrides) { - Map<String,String> combinedProps = Maps.newHashMapWithExpectedSize(defaultProps.props.size() + overrides.size()); - combinedProps.putAll(defaultProps.props); - for (Entry<Object, Object> entry : overrides.entrySet()) { - String key = entry.getKey().toString(); - String value = entry.getValue().toString(); - combinedProps.put(key, value); + private ReadOnlyProps(ReadOnlyProps defaultProps, Properties overridesArg) { + this.props = defaultProps.props; + if (overridesArg == null || overridesArg.isEmpty()) { + this.overrideProps = defaultProps.overrideProps; + } else { + Map<String, String> combinedOverrides = + Maps.newHashMapWithExpectedSize(defaultProps.overrideProps.size() + + overridesArg.size()); + if (!defaultProps.overrideProps.isEmpty()) { + combinedOverrides.putAll(defaultProps.overrideProps); + } + for (Entry<Object, Object> entry : overridesArg.entrySet()) { + combinedOverrides.put(entry.getKey().toString(), entry.getValue().toString()); + } + this.overrideProps = ImmutableMap.copyOf(combinedOverrides); } - this.props = ImmutableMap.copyOf(combinedProps); } private static Pattern varPat = Pattern.compile("\\$\\{[^\\}\\$\u0020]+\\}"); @@ -122,7 +135,8 @@ public class ReadOnlyProps implements Iterable<Entry<String, String>> { * or null if no such property exists. */ public String getRaw(String name) { - return props.get(name); + String overridenValue = overrideProps.get(name); + return overridenValue == null ? props.get(name) : overridenValue; } public String getRaw(String name, String defaultValue) { http://git-wip-us.apache.org/repos/asf/phoenix/blob/e81a4cc8/phoenix-core/src/test/java/org/apache/phoenix/jdbc/ReadOnlyPropertiesTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/jdbc/ReadOnlyPropertiesTest.java b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/ReadOnlyPropertiesTest.java new file mode 100644 index 0000000..cea9e6d --- /dev/null +++ b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/ReadOnlyPropertiesTest.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.phoenix.jdbc; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.Map; +import java.util.Properties; + +import org.apache.phoenix.util.ReadOnlyProps; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; + +public class ReadOnlyPropertiesTest { + + private static final String PROPERTY_NAME_1 = "property1"; + private static final String DEFAULT_VALUE_1 = "default1"; + private static final String OVERRIDEN_VALUE_1 = "override1"; + private static final String OVERRIDEN_VALUE_2 = "override2"; + + private static final String PROPERTY_NAME_2 = "property2"; + private static final String DEFAULT_VALUE_2 = "default2"; + + private static final Map<String, String> EMPTY_OVERRIDE_MAP = Collections.emptyMap(); + private static final Map<String, String> DEFAULT_PROPS_MAP = ImmutableMap.of(PROPERTY_NAME_1, DEFAULT_VALUE_1, PROPERTY_NAME_2, DEFAULT_VALUE_2); + private static final Map<String, String> OVERRIDE_MAP = ImmutableMap.of(PROPERTY_NAME_1, OVERRIDEN_VALUE_1); + + @Test + public void testDefaultProperties() { + ReadOnlyProps defaultProps = new ReadOnlyProps(DEFAULT_PROPS_MAP); + ReadOnlyProps readOnlyProps = new ReadOnlyProps(defaultProps, EMPTY_OVERRIDE_MAP.entrySet().iterator()); + assertEquals(DEFAULT_VALUE_1, readOnlyProps.get(PROPERTY_NAME_1)); + assertEquals(DEFAULT_VALUE_2, readOnlyProps.get(PROPERTY_NAME_2)); + } + + @Test + public void testOverrideProperties() { + ReadOnlyProps defaultProps = new ReadOnlyProps(DEFAULT_PROPS_MAP); + ReadOnlyProps readOnlyProps = new ReadOnlyProps(defaultProps, OVERRIDE_MAP.entrySet().iterator()); + assertEquals(OVERRIDEN_VALUE_1, readOnlyProps.get(PROPERTY_NAME_1)); + assertEquals(DEFAULT_VALUE_2, readOnlyProps.get(PROPERTY_NAME_2)); + } + + @Test + public void testAddAllOverrideProperties() { + ReadOnlyProps defaultProps = new ReadOnlyProps(DEFAULT_PROPS_MAP); + Properties overrideProps = new Properties(); + overrideProps.setProperty(PROPERTY_NAME_1, OVERRIDEN_VALUE_1); + ReadOnlyProps newProps = defaultProps.addAll(overrideProps); + assertEquals(OVERRIDEN_VALUE_1, newProps.get(PROPERTY_NAME_1)); + assertEquals(DEFAULT_VALUE_2, newProps.get(PROPERTY_NAME_2)); + } + + @Test + public void testOverridingNonDefaultProperties() { + ReadOnlyProps defaultProps = new ReadOnlyProps(DEFAULT_PROPS_MAP); + Properties props = new Properties(); + props.setProperty(PROPERTY_NAME_1, OVERRIDEN_VALUE_1); + ReadOnlyProps nonDefaultProps = defaultProps.addAll(props); + + Properties overrideProps = new Properties(); + overrideProps.setProperty(PROPERTY_NAME_1, OVERRIDEN_VALUE_2); + ReadOnlyProps newProps = nonDefaultProps.addAll(overrideProps); + assertEquals(OVERRIDEN_VALUE_2, newProps.get(PROPERTY_NAME_1)); + assertEquals(DEFAULT_VALUE_2, newProps.get(PROPERTY_NAME_2)); + } +}