You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by do...@apache.org on 2020/08/13 12:13:43 UTC
[shardingsphere] branch master updated: Refactor
CreateShardingRuleStatementContext (#6828)
This is an automated email from the ASF dual-hosted git repository.
dongzonglei 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 4a01656 Refactor CreateShardingRuleStatementContext (#6828)
4a01656 is described below
commit 4a016561baaed926424a90d5940cc1fab56bc1d0
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Thu Aug 13 20:13:23 2020 +0800
Refactor CreateShardingRuleStatementContext (#6828)
---
...reateShardingRuleStatementContextConverter.java | 44 ++++++++------
...eShardingRuleStatementContextConverterTest.java | 29 +++++-----
.../jdbc/execute/RDLExecuteEngineTest.java | 18 +++---
.../CreateShardingRuleStatementContext.java | 45 ++-------------
.../util/ShardingAlgorithmPropertiesUtil.java | 67 ++++++++++++++++++++++
5 files changed, 120 insertions(+), 83 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
index 09cf9fe..d6772ff 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
@@ -21,11 +21,14 @@ import com.google.common.base.Joiner;
import org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.rdl.parser.binder.context.CreateShardingRuleStatementContext;
import org.apache.shardingsphere.rdl.parser.binder.generator.SQLStatementContextConverter;
+import org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlStandardShardingStrategyConfiguration;
+import java.util.Properties;
+
/**
* Create sharding rule statement context converter.
*/
@@ -34,33 +37,36 @@ public final class CreateShardingRuleStatementContextConverter implements SQLSta
@Override
public YamlShardingRuleConfiguration convert(final CreateShardingRuleStatementContext context) {
YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
- addYamlShardingSphereAlgorithmConfiguration(context, result);
- addYamlShardingAutoTableRuleConfiguration(context, result);
+ for (TableRuleSegment each : context.getSqlStatement().getTables()) {
+ result.getShardingAlgorithms().put(getAlgorithmName(each.getLogicTable(), each.getAlgorithmType()),
+ createAlgorithmConfiguration(each, context.getAlgorithmProperties(each)));
+ result.getAutoTables().put(each.getLogicTable(), createAutoTableRuleConfiguration(each));
+ }
return result;
}
- private void addYamlShardingSphereAlgorithmConfiguration(final CreateShardingRuleStatementContext context, final YamlShardingRuleConfiguration ruleConfiguration) {
- YamlShardingSphereAlgorithmConfiguration algorithmConfiguration = new YamlShardingSphereAlgorithmConfiguration();
- algorithmConfiguration.setType(context.getAlgorithmType());
- algorithmConfiguration.setProps(context.getAlgorithmProperties());
- ruleConfiguration.getShardingAlgorithms().put(getAlgorithmName(context.getLogicTable(), context.getAlgorithmType()), algorithmConfiguration);
+ private YamlShardingSphereAlgorithmConfiguration createAlgorithmConfiguration(final TableRuleSegment segment, final Properties properties) {
+ YamlShardingSphereAlgorithmConfiguration result = new YamlShardingSphereAlgorithmConfiguration();
+ result.setType(segment.getAlgorithmType());
+ result.setProps(properties);
+ return result;
}
- private void addYamlShardingAutoTableRuleConfiguration(final CreateShardingRuleStatementContext context, final YamlShardingRuleConfiguration ruleConfiguration) {
- YamlShardingAutoTableRuleConfiguration tableRuleConfiguration = new YamlShardingAutoTableRuleConfiguration();
- tableRuleConfiguration.setLogicTable(context.getLogicTable());
- tableRuleConfiguration.setActualDataSources(Joiner.on(",").join(context.getDataSources()));
- tableRuleConfiguration.setShardingStrategy(createYamlShardingStrategyConfiguration(context));
- ruleConfiguration.getAutoTables().put(context.getLogicTable(), tableRuleConfiguration);
+ private YamlShardingAutoTableRuleConfiguration createAutoTableRuleConfiguration(final TableRuleSegment segment) {
+ YamlShardingAutoTableRuleConfiguration result = new YamlShardingAutoTableRuleConfiguration();
+ result.setLogicTable(segment.getLogicTable());
+ result.setActualDataSources(Joiner.on(",").join(segment.getDataSources()));
+ result.setShardingStrategy(createStrategyConfiguration(segment));
+ return result;
}
- private YamlShardingStrategyConfiguration createYamlShardingStrategyConfiguration(final CreateShardingRuleStatementContext context) {
- YamlShardingStrategyConfiguration strategy = new YamlShardingStrategyConfiguration();
+ private YamlShardingStrategyConfiguration createStrategyConfiguration(final TableRuleSegment segment) {
+ YamlShardingStrategyConfiguration result = new YamlShardingStrategyConfiguration();
YamlStandardShardingStrategyConfiguration standard = new YamlStandardShardingStrategyConfiguration();
- standard.setShardingColumn(context.getShardingColumn());
- standard.setShardingAlgorithmName(getAlgorithmName(context.getLogicTable(), context.getAlgorithmType()));
- strategy.setStandard(standard);
- return strategy;
+ standard.setShardingColumn(segment.getShardingColumn());
+ standard.setShardingAlgorithmName(getAlgorithmName(segment.getLogicTable(), segment.getAlgorithmType()));
+ result.setStandard(standard);
+ return result;
}
private String getAlgorithmName(final String tableName, final String algorithmType) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverterTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverterTest.java
index ad6f9ad..b55fff3 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverterTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverterTest.java
@@ -18,33 +18,34 @@
package org.apache.shardingsphere.sharding.convert;
import org.apache.shardingsphere.rdl.parser.binder.context.CreateShardingRuleStatementContext;
+import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement;
+import org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
-import java.util.Properties;
+import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class CreateShardingRuleStatementContextConverterTest {
+ private TableRuleSegment segment;
+
private CreateShardingRuleStatementContext context;
@Before
public void setUp() {
- context = mock(CreateShardingRuleStatementContext.class);
- when(context.getLogicTable()).thenReturn("t_order");
- when(context.getDataSources()).thenReturn(Arrays.asList("ds0", "ds1"));
- when(context.getShardingColumn()).thenReturn("order_id");
- when(context.getAlgorithmType()).thenReturn("MOD");
- Properties properties = new Properties();
- properties.setProperty("sharding.count", "2");
- when(context.getAlgorithmProperties()).thenReturn(properties);
+ segment = new TableRuleSegment();
+ segment.setLogicTable("t_order");
+ segment.setDataSources(Arrays.asList("ds0", "ds1"));
+ segment.setShardingColumn("order_id");
+ segment.setAlgorithmType("MOD");
+ segment.setProperties(Collections.singleton("2"));
+ context = new CreateShardingRuleStatementContext(new CreateShardingRuleStatement(Collections.singleton(segment)));
}
@Test
@@ -52,9 +53,9 @@ public class CreateShardingRuleStatementContextConverterTest {
YamlShardingRuleConfiguration rule = new CreateShardingRuleStatementContextConverter().convert(context);
assertTrue(rule.getTables().isEmpty());
assertThat(rule.getAutoTables().size(), is(1));
- assertThat(rule.getAutoTables().get(context.getLogicTable()).getActualDataSources(), is("ds0,ds1"));
- assertThat(rule.getAutoTables().get(context.getLogicTable()).getShardingStrategy().getStandard().getShardingColumn(), is("order_id"));
- assertThat(rule.getAutoTables().get(context.getLogicTable()).getShardingStrategy().getStandard().getShardingAlgorithmName(), is("t_order_MOD"));
+ assertThat(rule.getAutoTables().get(segment.getLogicTable()).getActualDataSources(), is("ds0,ds1"));
+ assertThat(rule.getAutoTables().get(segment.getLogicTable()).getShardingStrategy().getStandard().getShardingColumn(), is("order_id"));
+ assertThat(rule.getAutoTables().get(segment.getLogicTable()).getShardingStrategy().getStandard().getShardingAlgorithmName(), is("t_order_MOD"));
assertTrue(rule.getShardingAlgorithms().containsKey("t_order_MOD"));
assertThat(rule.getShardingAlgorithms().get("t_order_MOD").getType(), is("MOD"));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
index 36eada7..8b56b0f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
@@ -31,6 +31,7 @@ import org.apache.shardingsphere.rdl.parser.binder.context.CreateDataSourcesStat
import org.apache.shardingsphere.rdl.parser.binder.context.CreateShardingRuleStatementContext;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateDataSourcesStatement;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement;
+import org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -39,12 +40,10 @@ import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
-import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class RDLExecuteEngineTest {
@@ -63,14 +62,13 @@ public final class RDLExecuteEngineTest {
}
private void createRuleContext() {
- ruleContext = mock(CreateShardingRuleStatementContext.class);
- when(ruleContext.getLogicTable()).thenReturn("t_order");
- when(ruleContext.getDataSources()).thenReturn(Arrays.asList("ds0", "ds1"));
- when(ruleContext.getShardingColumn()).thenReturn("order_id");
- when(ruleContext.getAlgorithmType()).thenReturn("MOD");
- Properties properties = new Properties();
- properties.setProperty("sharding.count", "2");
- when(ruleContext.getAlgorithmProperties()).thenReturn(properties);
+ TableRuleSegment segment = new TableRuleSegment();
+ segment.setLogicTable("t_order");
+ segment.setDataSources(Arrays.asList("ds0", "ds1"));
+ segment.setShardingColumn("order_id");
+ segment.setAlgorithmType("MOD");
+ segment.setProperties(Collections.singleton("2"));
+ ruleContext = new CreateShardingRuleStatementContext(new CreateShardingRuleStatement(Collections.singleton(segment)));
}
@Test
diff --git a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateShardingRuleStatementContext.java b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateShardingRuleStatementContext.java
index 1bcf514..fb20015 100644
--- a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateShardingRuleStatementContext.java
+++ b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateShardingRuleStatementContext.java
@@ -17,11 +17,11 @@
package org.apache.shardingsphere.rdl.parser.binder.context;
+import org.apache.shardingsphere.rdl.parser.binder.util.ShardingAlgorithmPropertiesUtil;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement;
+import org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment;
import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
-import java.util.Collection;
-import java.util.LinkedList;
import java.util.Properties;
/**
@@ -34,47 +34,12 @@ public final class CreateShardingRuleStatementContext extends CommonSQLStatement
}
/**
- * Get logic table.
- *
- * @return logic table
- */
- public String getLogicTable() {
- return "";
- }
-
- /**
- * Get data sources.
- *
- * @return data sources
- */
- public Collection<String> getDataSources() {
- return new LinkedList<>();
- }
-
- /**
- * Get sharding column.
- *
- * @return sharding column
- */
- public String getShardingColumn() {
- return "";
- }
-
- /**
- * Get sharding algorithm type.
- *
- * @return sharding algorithm type
- */
- public String getAlgorithmType() {
- return "";
- }
-
- /**
* Get algorithm properties.
*
+ * @param segment segment
* @return algorithm properties
*/
- public Properties getAlgorithmProperties() {
- return new Properties();
+ public Properties getAlgorithmProperties(final TableRuleSegment segment) {
+ return ShardingAlgorithmPropertiesUtil.getProperties(segment.getAlgorithmType(), segment.getProperties());
}
}
diff --git a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/util/ShardingAlgorithmPropertiesUtil.java b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/util/ShardingAlgorithmPropertiesUtil.java
new file mode 100644
index 0000000..44ff5fc
--- /dev/null
+++ b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/util/ShardingAlgorithmPropertiesUtil.java
@@ -0,0 +1,67 @@
+/*
+ * 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.shardingsphere.rdl.parser.binder.util;
+
+import com.google.common.base.Preconditions;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Sharding algorithm properties util.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingAlgorithmPropertiesUtil {
+
+ public static final Map<String, Collection<String>> TYPE_AND_PROPERTIES;
+
+ static {
+ TYPE_AND_PROPERTIES = new LinkedHashMap<>();
+ TYPE_AND_PROPERTIES.put("MOD", Collections.singleton("sharding.count"));
+ TYPE_AND_PROPERTIES.put("HASH_MOD", Collections.singleton("sharding.count"));
+ TYPE_AND_PROPERTIES.put("VOLUME_RANGE", Arrays.asList("range.lower", "range.upper", "sharding.volume"));
+ TYPE_AND_PROPERTIES.put("BOUNDARY_RANGE", Collections.singleton("sharding.ranges"));
+ }
+
+ /**
+ * Get properties.
+ *
+ * @param shardingAlgorithmType sharding algorithm type
+ * @param properties properties
+ * @return properties
+ */
+ public static Properties getProperties(final String shardingAlgorithmType, final Collection<String> properties) {
+ Preconditions.checkArgument(TYPE_AND_PROPERTIES.containsKey(shardingAlgorithmType), "Bad sharding algorithm type: %s.", shardingAlgorithmType);
+ Preconditions.checkArgument(TYPE_AND_PROPERTIES.get(shardingAlgorithmType).size() == properties.size(),
+ "%s needs %d properties, but %s properties are given.", shardingAlgorithmType, TYPE_AND_PROPERTIES.get(shardingAlgorithmType).size(), properties.size());
+ Properties result = new Properties();
+ Iterator<String> keys = TYPE_AND_PROPERTIES.get(shardingAlgorithmType).iterator();
+ Iterator<String> values = properties.iterator();
+ while (keys.hasNext()) {
+ result.setProperty(keys.next(), values.next());
+ }
+ return result;
+ }
+}