未验证 提交 4a016561 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Refactor CreateShardingRuleStatementContext (#6828)

上级 5ad256e4
......@@ -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) {
......
......@@ -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"));
}
......
......@@ -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
......
......@@ -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;
/**
......@@ -33,48 +33,13 @@ public final class CreateShardingRuleStatementContext extends CommonSQLStatement
super(sqlStatement);
}
/**
* 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());
}
}
/*
* 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;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册