This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 94256ca Recognize property synonyms in DataSourceProperties (#14802)
94256ca is described below
commit 94256ca5780bdf1d20bff1ca982238b91358dbfa
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jan 16 11:41:39 2022 +0800
Recognize property synonyms in DataSourceProperties (#14802)
---
.../pool/creator/DataSourcePoolCreator.java | 7 ----
.../impl/HikariDataSourcePoolMetaData.java | 3 ++
.../datasource/props/DataSourceProperties.java | 46 +++++++++++++++-------
.../YamlDataSourceConfigurationSwapper.java | 2 +-
.../datasource/props/DataSourcePropertiesTest.java | 20 ----------
.../rql/resource/DataSourceQueryResultSet.java | 24 +++--------
6 files changed, 42 insertions(+), 60 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/DataSourcePoolCreator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/DataSourcePoolCreator.java
index 0435631..2c23a00 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/DataSourcePoolCreator.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/DataSourcePoolCreator.java
@@ -55,7 +55,6 @@ public final class DataSourcePoolCreator {
public static DataSource create(final DataSourceProperties
dataSourceProps) {
DataSource result =
createDataSource(dataSourceProps.getDataSourceClassName());
DataSourcePoolMetaData poolMetaData =
DataSourcePoolMetaDataFactory.newInstance(dataSourceProps.getDataSourceClassName());
- addPropertySynonym(dataSourceProps, poolMetaData);
DataSourceReflection dataSourceReflection = new
DataSourceReflection(result);
setDefaultFields(dataSourceReflection, poolMetaData);
setConfiguredFields(dataSourceProps, dataSourceReflection,
poolMetaData);
@@ -68,12 +67,6 @@ public final class DataSourcePoolCreator {
return (DataSource)
Class.forName(dataSourceClassName).getConstructor().newInstance();
}
- private static void addPropertySynonym(final DataSourceProperties
dataSourceProps, final DataSourcePoolMetaData poolMetaData) {
- for (Entry<String, String> entry :
poolMetaData.getPropertySynonyms().entrySet()) {
- dataSourceProps.addPropertySynonym(entry.getKey(),
entry.getValue());
- }
- }
-
private static void setDefaultFields(final DataSourceReflection
dataSourceReflection, final DataSourcePoolMetaData poolMetaData) {
for (Entry<String, Object> entry :
poolMetaData.getDefaultProperties().entrySet()) {
dataSourceReflection.setField(entry.getKey(), entry.getValue());
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/metadata/impl/HikariDataSourcePoolMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/metadata/impl/HikariDataSourcePoolMetaData.java
index bfeda33..ba9bd19 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/metadata/impl/HikariDataSourcePoolMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/metadata/impl/HikariDataSourcePoolMetaData.java
@@ -57,6 +57,9 @@ public final class HikariDataSourcePoolMetaData implements
DataSourcePoolMetaDat
}
private void buildPropertySynonyms() {
+ propertySynonyms.put("connectionTimeoutMilliseconds",
"connectionTimeout");
+ propertySynonyms.put("idleTimeoutMilliseconds", "idleTimeout");
+ propertySynonyms.put("maxLifetimeMilliseconds", "maxLifetime");
propertySynonyms.put("maxPoolSize", "maximumPoolSize");
propertySynonyms.put("minPoolSize", "minimumIdle");
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourceProperties.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourceProperties.java
index ad16a66..3c1eef6 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourceProperties.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourceProperties.java
@@ -18,11 +18,12 @@
package org.apache.shardingsphere.infra.config.datasource.props;
import com.google.common.base.Objects;
+import lombok.AccessLevel;
import lombok.Getter;
-import
org.apache.shardingsphere.infra.config.datasource.pool.metadata.DataSourcePoolMetaData;
import
org.apache.shardingsphere.infra.config.datasource.pool.metadata.DataSourcePoolMetaDataFactory;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
@@ -37,7 +38,8 @@ public final class DataSourceProperties {
private final String dataSourceClassName;
- private final DataSourcePoolMetaData poolMetaData;
+ @Getter(AccessLevel.NONE)
+ private final Map<String, String> propertySynonyms;
private final Map<String, Object> props;
@@ -45,23 +47,39 @@ public final class DataSourceProperties {
public DataSourceProperties(final String dataSourceClassName, final
Map<String, Object> props) {
this.dataSourceClassName = dataSourceClassName;
- this.props = props;
- poolMetaData =
DataSourcePoolMetaDataFactory.newInstance(dataSourceClassName);
+ propertySynonyms =
DataSourcePoolMetaDataFactory.newInstance(dataSourceClassName).getPropertySynonyms();
+ this.props = getLocalProperties(props);
+ }
+
+ private Map<String, Object> getLocalProperties(final Map<String, Object>
props) {
+ Map<String, Object> result = new LinkedHashMap<>(props);
+ for (Entry<String, String> entry : propertySynonyms.entrySet()) {
+ String standardPropertyName = entry.getKey();
+ String synonymsPropertyName = entry.getValue();
+ if (props.containsKey(standardPropertyName)) {
+ result.put(synonymsPropertyName,
props.get(standardPropertyName));
+ result.remove(standardPropertyName);
+ }
+ }
+ return result;
}
/**
- * Add property synonym to shared configuration.
- *
- * @param originalName original key for data source configuration property
- * @param synonym property synonym for configuration
+ * Get standard properties.
+ *
+ * @return standard properties
*/
- public void addPropertySynonym(final String originalName, final String
synonym) {
- if (props.containsKey(originalName)) {
- props.put(synonym, props.get(originalName));
- }
- if (props.containsKey(synonym)) {
- props.put(originalName, props.get(synonym));
+ public Map<String, Object> getStandardProperties() {
+ Map<String, Object> result = new LinkedHashMap<>(props);
+ for (Entry<String, String> entry : propertySynonyms.entrySet()) {
+ String standardPropertyName = entry.getKey();
+ String synonymsPropertyName = entry.getValue();
+ if (props.containsKey(synonymsPropertyName)) {
+ result.put(standardPropertyName,
props.get(synonymsPropertyName));
+ result.remove(synonymsPropertyName);
+ }
}
+ return result;
}
/**
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
index f4a31d8..90af492 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
@@ -89,7 +89,7 @@ public final class YamlDataSourceConfigurationSwapper {
* @return data source map
*/
public Map<String, Object> swapToMap(final DataSourceProperties
dataSourceProps) {
- Map<String, Object> result = new HashMap<>(dataSourceProps.getProps());
+ Map<String, Object> result = new
HashMap<>(dataSourceProps.getStandardProperties());
if (!dataSourceProps.getCustomPoolProps().isEmpty()) {
result.put(DataSourceProperties.CUSTOM_POOL_PROPS_KEY,
dataSourceProps.getCustomPoolProps());
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourcePropertiesTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourcePropertiesTest.java
index e0b9ba9..c36d05f 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourcePropertiesTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/props/DataSourcePropertiesTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.infra.config.datasource.props;
-import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.test.mock.MockedDataSource;
import org.junit.Rule;
@@ -46,25 +45,6 @@ public final class DataSourcePropertiesTest {
public ExpectedException thrown = ExpectedException.none();
@Test
- public void assertAddSynonym() {
- HikariDataSource actualDataSource = new HikariDataSource();
-
actualDataSource.setDriverClassName(MockedDataSource.class.getCanonicalName());
- actualDataSource.setJdbcUrl("jdbc:mock://127.0.0.1/foo_ds");
- actualDataSource.setUsername("root");
- actualDataSource.setPassword("root");
- DataSourceProperties actual =
DataSourcePropertiesCreator.create(actualDataSource);
- actual.addPropertySynonym("url", "jdbcUrl");
- actual.addPropertySynonym("user", "username");
- assertThat(actual.getDataSourceClassName(),
is(HikariDataSource.class.getName()));
- assertThat(actual.getProps().get("driverClassName").toString(),
is(MockedDataSource.class.getCanonicalName()));
- assertThat(actual.getProps().get("jdbcUrl").toString(),
is("jdbc:mock://127.0.0.1/foo_ds"));
- assertThat(actual.getProps().get("username").toString(), is("root"));
- assertThat(actual.getProps().get("password").toString(), is("root"));
- assertThat(actual.getProps().get("jdbcUrl"),
is(actual.getProps().get("url")));
- assertThat(actual.getProps().get("username"),
is(actual.getProps().get("user")));
- }
-
- @Test
public void assertEquals() {
assertThat(new DataSourceProperties(MockedDataSource.class.getName(),
createUserProperties("root")),
is(new DataSourceProperties(MockedDataSource.class.getName(),
createUserProperties("root"))));
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/resource/DataSourceQueryResultSet.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/resource/DataSourceQueryResultSet.java
index c4c64c7..366354e 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/resource/DataSourceQueryResultSet.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/resource/DataSourceQueryResultSet.java
@@ -77,30 +77,18 @@ public final class DataSourceQueryResultSet implements
DistSQLResultSet {
private Map<String, Object> getAttributeMap(final DataSourceProperties
dataSourceProps) {
Map<String, Object> result = new LinkedHashMap<>(7, 1);
- result.put("connectionTimeoutMilliseconds",
getProperty(dataSourceProps, "connectionTimeoutMilliseconds",
"connectionTimeout"));
- result.put("idleTimeoutMilliseconds", getProperty(dataSourceProps,
"idleTimeoutMilliseconds", "idleTimeout"));
- result.put("maxLifetimeMilliseconds", getProperty(dataSourceProps,
"maxLifetimeMilliseconds", "maxLifetime"));
- result.put("maxPoolSize", getProperty(dataSourceProps, "maxPoolSize",
"maximumPoolSize"));
- result.put("minPoolSize", getProperty(dataSourceProps, "minPoolSize",
"minimumIdle"));
- result.put("readOnly", getProperty(dataSourceProps, "readOnly"));
+ result.put("connectionTimeoutMilliseconds",
dataSourceProps.getStandardProperties().get("connectionTimeoutMilliseconds"));
+ result.put("idleTimeoutMilliseconds",
dataSourceProps.getStandardProperties().get("idleTimeoutMilliseconds"));
+ result.put("maxLifetimeMilliseconds",
dataSourceProps.getStandardProperties().get("maxLifetimeMilliseconds"));
+ result.put("maxPoolSize",
dataSourceProps.getStandardProperties().get("maxPoolSize"));
+ result.put("minPoolSize",
dataSourceProps.getStandardProperties().get("minPoolSize"));
+ result.put("readOnly",
dataSourceProps.getStandardProperties().get("readOnly"));
if (!dataSourceProps.getCustomPoolProps().isEmpty()) {
result.put(DataSourceProperties.CUSTOM_POOL_PROPS_KEY,
dataSourceProps.getCustomPoolProps());
}
return result;
}
- private Object getProperty(final DataSourceProperties dataSourceProps,
final String key, final String... synonym) {
- if (dataSourceProps.getProps().containsKey(key)) {
- return dataSourceProps.getProps().get(key);
- }
- for (String each : synonym) {
- if (dataSourceProps.getProps().containsKey(each)) {
- return dataSourceProps.getProps().get(each);
- }
- }
- return null;
- }
-
@Override
public String getType() {
return ShowResourcesStatement.class.getCanonicalName();