提交 e73924b0 编写于 作者: T terrymanu

refactor RoutingEngineFactory

上级 03dd94c7
......@@ -99,7 +99,7 @@ public interface MergedResult {
InputStream getInputStream(String columnLabel, String type) throws SQLException;
/**
* Adjust ResultSet is null or not.
* Judge ResultSet is null or not.
*
* @return ResultSet is null or not
* @throws SQLException SQL Exception
......
......@@ -116,7 +116,7 @@ public interface QueryResult {
InputStream getInputStream(String columnLabel, String type) throws SQLException;
/**
* Adjust ResultSet is null or not.
* Judge ResultSet is null or not.
*
* @return ResultSet is null or not
* @throws SQLException SQL Exception
......
......@@ -36,7 +36,7 @@ public final class ShardingConditions {
private final List<ShardingCondition> shardingConditions;
/**
* Adjust sharding conditions is always false or not.
* Judge sharding conditions is always false or not.
*
* @return sharding conditions is always false or not
*/
......
......@@ -125,9 +125,9 @@ public final class LexerEngine {
}
/**
* Adjust current token equals one of input tokens or not.
* Judge current token equals one of input tokens or not.
*
* @param tokenTypes to be adjusted token types
* @param tokenTypes to be judged token types
* @return current token equals one of input tokens or not
*/
public boolean equalAny(final TokenType... tokenTypes) {
......@@ -142,7 +142,7 @@ public final class LexerEngine {
/**
* Skip current token if equals one of input tokens.
*
* @param tokenTypes to be adjusted token types
* @param tokenTypes to be judged token types
* @return skipped current token or not
*/
public boolean skipIfEqual(final TokenType... tokenTypes) {
......@@ -181,7 +181,7 @@ public final class LexerEngine {
/**
* Throw unsupported exception if current token equals one of input tokens.
*
* @param tokenTypes to be adjusted token types
* @param tokenTypes to be judged token types
*/
public void unsupportedIfEqual(final TokenType... tokenTypes) {
if (equalAny(tokenTypes)) {
......@@ -192,7 +192,7 @@ public final class LexerEngine {
/**
* Throw unsupported exception if current token not equals one of input tokens.
*
* @param tokenTypes to be adjusted token types
* @param tokenTypes to be judged token types
*/
public void unsupportedIfNotSkip(final TokenType... tokenTypes) {
if (!skipIfEqual(tokenTypes)) {
......
......@@ -34,9 +34,9 @@ public final class CharType {
public static final byte EOI = 0x1A;
/**
* Adjust is whitespace or not.
* Judge is whitespace or not.
*
* @param ch to be adjusted char
* @param ch to be judged char
* @return is whitespace or not
*/
public static boolean isWhitespace(final char ch) {
......@@ -44,9 +44,9 @@ public final class CharType {
}
/**
* Adjust is end of input or not.
* Judge is end of input or not.
*
* @param ch to be adjusted char
* @param ch to be judged char
* @return is end of input or not
*/
public static boolean isEndOfInput(final char ch) {
......@@ -54,9 +54,9 @@ public final class CharType {
}
/**
* Adjust is alphabet or not.
* Judge is alphabet or not.
*
* @param ch to be adjusted char
* @param ch to be judged char
* @return is alphabet or not
*/
public static boolean isAlphabet(final char ch) {
......@@ -64,9 +64,9 @@ public final class CharType {
}
/**
* Adjust is digital or not.
* Judge is digital or not.
*
* @param ch to be adjusted char
* @param ch to be judged char
* @return is alphabet or not
*/
public static boolean isDigital(final char ch) {
......@@ -74,9 +74,9 @@ public final class CharType {
}
/**
* Adjust is symbol or not.
* Judge is symbol or not.
*
* @param ch to be adjusted char
* @param ch to be judged char
* @return is symbol or not
*/
public static boolean isSymbol(final char ch) {
......
......@@ -198,7 +198,7 @@ public final class SelectStatement extends DQLStatement {
}
/**
* Adjust group by and order by sequence is same or not.
* Judge group by and order by sequence is same or not.
*
* @return group by and order by sequence is same or not
*/
......@@ -251,7 +251,7 @@ public final class SelectStatement extends DQLStatement {
}
/**
* Adjust contains subquery statement or not.
* Judge contains subquery statement or not.
*
* @return contains subquery statement or not
*/
......
......@@ -35,7 +35,7 @@ public final class MasterVisitedManager {
};
/**
* Adjust master data source visited in current thread.
* Judge master data source visited in current thread.
*
* @return master data source visited or not in current thread
*/
......
......@@ -96,7 +96,7 @@ public final class ParsingSQLRouter implements ShardingRouter {
if (generatedKey.isPresent()) {
setGeneratedKeys(result, generatedKey.get());
}
if (isNeedMergeShardingValues(sqlStatement)) {
if (sqlStatement instanceof SelectStatement && isNeedMergeShardingValues((SelectStatement) sqlStatement)) {
checkSubqueryShardingValues(sqlStatement.getConditions(), shardingConditions);
mergeShardingValues(shardingConditions);
}
......@@ -151,16 +151,8 @@ public final class ParsingSQLRouter implements ShardingRouter {
sqlRouteResult.getGeneratedKey().getGeneratedKeys().addAll(generatedKeys);
}
private boolean isNeedMergeShardingValues(final SQLStatement sqlStatement) {
if (!(sqlStatement instanceof SelectStatement) || ((SelectStatement) sqlStatement).getSubqueryConditions().isEmpty()) {
return false;
}
for (String each : sqlStatement.getTables().getTableNames()) {
if (shardingRule.findTableRuleByLogicTable(each).isPresent()) {
return true;
}
}
return false;
private boolean isNeedMergeShardingValues(final SelectStatement selectStatement) {
return !selectStatement.getSubqueryConditions().isEmpty() && !shardingRule.getShardingLogicTableNames(selectStatement.getTables().getTableNames()).isEmpty();
}
private void checkSubqueryShardingValues(final Conditions conditions, final ShardingConditions shardingConditions) {
......
......@@ -29,6 +29,7 @@ import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ResetP
import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.SetParamStatement;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement;
import io.shardingsphere.core.parsing.parser.sql.dml.DMLStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingsphere.core.routing.type.RoutingEngine;
import io.shardingsphere.core.routing.type.broadcast.DatabaseBroadcastRoutingEngine;
......@@ -44,7 +45,6 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.Collection;
import java.util.LinkedList;
/**
* Routing engine factory.
......@@ -66,45 +66,55 @@ public final class RoutingEngineFactory {
public static RoutingEngine newInstance(final ShardingRule shardingRule,
final ShardingDataSourceMetaData shardingDataSourceMetaData, final SQLStatement sqlStatement, final ShardingConditions shardingConditions) {
Collection<String> tableNames = sqlStatement.getTables().getTableNames();
Collection<String> WithOutBroadcastTableNames = removeBroadcastTables(shardingRule, tableNames);
RoutingEngine result;
if (sqlStatement instanceof UseStatement) {
result = new IgnoreRoutingEngine();
} else if (shardingRule.isAllBroadcastTables(tableNames) && !(sqlStatement instanceof SelectStatement)) {
result = new DatabaseBroadcastRoutingEngine(shardingRule);
} else if (sqlStatement instanceof DDLStatement || (sqlStatement instanceof DCLStatement && ((DCLStatement) sqlStatement).isGrantForSingleTable())) {
result = new TableBroadcastRoutingEngine(shardingRule, sqlStatement);
} else if (sqlStatement instanceof ShowDatabasesStatement || ((sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof ShowTableStatusStatement) && tableNames.isEmpty())
|| sqlStatement instanceof SetParamStatement || sqlStatement instanceof ResetParamStatement) {
result = new DatabaseBroadcastRoutingEngine(shardingRule);
} else if (sqlStatement instanceof DCLStatement) {
result = new InstanceBroadcastRoutingEngine(shardingRule, shardingDataSourceMetaData);
} else if (shardingRule.isAllInDefaultDataSource(tableNames)) {
result = new DefaultDatabaseRoutingEngine(shardingRule, tableNames);
} else if (shardingConditions.isAlwaysFalse()) {
result = new UnicastRoutingEngine(shardingRule, tableNames);
} else if (sqlStatement instanceof DALStatement) {
result = new UnicastRoutingEngine(shardingRule, tableNames);
} else if (tableNames.isEmpty() && sqlStatement instanceof SelectStatement || shardingRule.isAllBroadcastTables(tableNames) && sqlStatement instanceof SelectStatement) {
result = new UnicastRoutingEngine(shardingRule, tableNames);
} else if (tableNames.isEmpty()) {
result = new DatabaseBroadcastRoutingEngine(shardingRule);
} else if (1 == WithOutBroadcastTableNames.size() || shardingRule.isAllBindingTables(WithOutBroadcastTableNames)) {
result = new StandardRoutingEngine(shardingRule, WithOutBroadcastTableNames.iterator().next(), shardingConditions);
} else {
// TODO config for cartesian set
result = new ComplexRoutingEngine(shardingRule, tableNames, shardingConditions);
return new IgnoreRoutingEngine();
}
return result;
if (isDatabaseAdministrationCommand(sqlStatement, tableNames) || isDMLBroadcastTable(shardingRule, sqlStatement, tableNames)) {
return new DatabaseBroadcastRoutingEngine(shardingRule);
}
if (sqlStatement instanceof DDLStatement || isDCLForTable(sqlStatement)) {
return new TableBroadcastRoutingEngine(shardingRule, sqlStatement);
}
if (sqlStatement instanceof DCLStatement) {
return new InstanceBroadcastRoutingEngine(shardingRule, shardingDataSourceMetaData);
}
if (shardingRule.isAllInDefaultDataSource(tableNames)) {
return new DefaultDatabaseRoutingEngine(shardingRule, tableNames);
}
if (shardingConditions.isAlwaysFalse()) {
return new UnicastRoutingEngine(shardingRule, tableNames);
}
if (sqlStatement instanceof DALStatement) {
return new UnicastRoutingEngine(shardingRule, tableNames);
}
if (isUnicastDQL(shardingRule, sqlStatement, tableNames)) {
return new UnicastRoutingEngine(shardingRule, tableNames);
}
if (tableNames.isEmpty()) {
return new DatabaseBroadcastRoutingEngine(shardingRule);
}
Collection<String> shardingTableNames = shardingRule.getShardingLogicTableNames(tableNames);
if (1 == shardingTableNames.size() || shardingRule.isAllBindingTables(shardingTableNames)) {
return new StandardRoutingEngine(shardingRule, shardingTableNames.iterator().next(), shardingConditions);
}
// TODO config for cartesian set
return new ComplexRoutingEngine(shardingRule, tableNames, shardingConditions);
}
private static Collection<String> removeBroadcastTables(final ShardingRule shardingRule, final Collection<String> tableNames) {
Collection<String> result = new LinkedList<>();
for (String each : tableNames) {
if (!shardingRule.getBroadcastTables().contains(each)) {
result.add(each);
}
}
return result;
private static boolean isDatabaseAdministrationCommand(final SQLStatement sqlStatement, final Collection<String> tableNames) {
return sqlStatement instanceof ShowDatabasesStatement || ((sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof ShowTableStatusStatement) && tableNames.isEmpty())
|| sqlStatement instanceof SetParamStatement || sqlStatement instanceof ResetParamStatement;
}
private static boolean isDMLBroadcastTable(final ShardingRule shardingRule, final SQLStatement sqlStatement, final Collection<String> tableNames) {
return sqlStatement instanceof DMLStatement && shardingRule.isAllBroadcastTables(tableNames);
}
private static boolean isDCLForTable(final SQLStatement sqlStatement) {
return sqlStatement instanceof DCLStatement && ((DCLStatement) sqlStatement).isGrantForSingleTable();
}
private static boolean isUnicastDQL(final ShardingRule shardingRule, final SQLStatement sqlStatement, final Collection<String> tableNames) {
return sqlStatement instanceof SelectStatement && (tableNames.isEmpty() || shardingRule.isAllBroadcastTables(tableNames));
}
}
......@@ -30,7 +30,7 @@ public class RoutingResult {
private final TableUnits tableUnits = new TableUnits();
/**
* Adjust is route for single database and table only or not.
* Judge is route for single database and table only or not.
*
* @return is route for single database and table only or not
*/
......
......@@ -40,7 +40,7 @@ public final class BindingTableRule {
private final List<TableRule> tableRules;
/**
* Adjust contains this logic table in this rule.
* Judge contains this logic table in this rule.
*
* @param logicTableName logic table name
* @return contains this logic table or not
......
......@@ -172,17 +172,22 @@ public class ShardingRule {
}
/**
* Adjust logic table is belong to broadcast tables.
* Judge logic table is belong to broadcast tables.
*
* @param logicTable logic table name
* @param logicTableName logic table name
* @return logic table is belong to broadcast tables or not
*/
public boolean isBroadcastTable(final String logicTable) {
return broadcastTables.contains(logicTable);
public boolean isBroadcastTable(final String logicTableName) {
for (String each : broadcastTables) {
if (each.equalsIgnoreCase(logicTableName)) {
return true;
}
}
return false;
}
/**
* Adjust logic tables is all belong to broadcast tables.
* Judge logic tables is all belong to broadcast tables.
*
* @param logicTables names of logic tables
* @return logic tables is all belong to broadcast tables or not
......@@ -200,7 +205,7 @@ public class ShardingRule {
}
/**
* Adjust logic tables is all belong to binding tables.
* Judge logic tables is all belong to binding tables.
*
* @param logicTables names of logic tables
* @return logic tables is all belong to binding tables or not
......@@ -219,21 +224,18 @@ public class ShardingRule {
}
/**
* Adjust logic tables is all belong to default data source.
* Judge logic tables is all belong to default data source.
*
* @param logicTables names of logic tables
* @param logicTableNames logic table names
* @return logic tables is all belong to default data source
*/
public boolean isAllInDefaultDataSource(final Collection<String> logicTables) {
for (String each : logicTables) {
if (findTableRuleByLogicTable(each).isPresent()) {
return false;
}
if (isBroadcastTable(each)) {
public boolean isAllInDefaultDataSource(final Collection<String> logicTableNames) {
for (String each : logicTableNames) {
if (findTableRuleByLogicTable(each).isPresent() || isBroadcastTable(each)) {
return false;
}
}
return !logicTables.isEmpty();
return !logicTableNames.isEmpty();
}
private Optional<BindingTableRule> findBindingTableRule(final Collection<String> logicTables) {
......@@ -262,7 +264,7 @@ public class ShardingRule {
}
/**
* Adjust is sharding column or not.
* Judge is sharding column or not.
*
* @param column column object
* @return is sharding column or not
......@@ -361,7 +363,7 @@ public class ShardingRule {
}
/**
* Adjust is logic index or not.
* Judge is logic index or not.
*
* @param logicIndexName logic index name
* @param logicTableName logic table name
......@@ -428,7 +430,7 @@ public class ShardingRule {
}
/**
* Adjust contains table in sharding rule.
* Judge contains table in sharding rule.
*
* @param tableName table name
* @return contains table in sharding rule or not
......@@ -436,4 +438,21 @@ public class ShardingRule {
public boolean contains(final String tableName) {
return findTableRuleByLogicTable(tableName).isPresent() || findBindingTableRule(tableName).isPresent() || isBroadcastTable(tableName);
}
/**
* Get sharding logic table names.
*
* @param logicTableNames logic table names
* @return sharding logic table names
*/
public Collection<String> getShardingLogicTableNames(final Collection<String> logicTableNames) {
Collection<String> result = new LinkedList<>();
for (String each : logicTableNames) {
Optional<TableRule> tableRule = findTableRuleByLogicTable(each);
if (tableRule.isPresent()) {
result.add(each);
}
}
return result;
}
}
......@@ -32,9 +32,9 @@ import java.util.List;
public final class StringUtil {
/**
* Adjust is boolean value or not.
* Judge is boolean value or not.
*
* @param value to be adjusted string value
* @param value to be judged string value
* @return is boolean value or not
*/
public static boolean isBooleanValue(final String value) {
......@@ -42,9 +42,9 @@ public final class StringUtil {
}
/**
* Adjust is int value or not.
* Judge is int value or not.
*
* @param value to be adjusted string value
* @param value to be judged string value
* @return is int value or not
*/
public static boolean isIntValue(final String value) {
......@@ -57,9 +57,9 @@ public final class StringUtil {
}
/**
* Adjust is long value or not.
* Judge is long value or not.
*
* @param value to be adjusted string value
* @param value to be judged string value
* @return is long value or not
*/
public static boolean isLongValue(final String value) {
......
......@@ -27,6 +27,7 @@ import io.shardingsphere.core.exception.ShardingConfigurationException;
import io.shardingsphere.core.keygen.fixture.IncrementKeyGenerator;
import io.shardingsphere.core.parsing.parser.context.condition.Column;
import io.shardingsphere.core.routing.strategy.none.NoneShardingStrategy;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.util.Arrays;
......@@ -324,12 +325,20 @@ public final class ShardingRuleTest {
@Test
public void assertIsAllInDefaultDataSource() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfig = createTableRuleConfig();
shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
ShardingRule actual = new ShardingRule(shardingRuleConfig, createDataSourceNames());
ShardingRule actual = createShardingRule();
assertTrue(actual.isAllInDefaultDataSource(Collections.singletonList("table_0")));
assertFalse(actual.isAllInDefaultDataSource(Collections.singletonList("logic_table")));
}
@Test
public void assertIsNotAllInDefaultDataSourceWithShardingTable() {
ShardingRule actual = createShardingRule();
assertFalse(actual.isAllInDefaultDataSource(Arrays.asList("table_0", "logic_table")));
}
@Test
public void assertIsNotAllInDefaultDataSourceWithBroadcastTable() {
ShardingRule actual = createShardingRule();
assertFalse(actual.isAllInDefaultDataSource(Arrays.asList("table_0", "broadcast_logic_table")));
}
@Test
......@@ -437,6 +446,12 @@ public final class ShardingRuleTest {
assertFalse(createShardingRule().contains("NEW_TABLE"));
}
@Test
public void assertGetShardingLogicTableNames() {
ShardingRule actual = createShardingRule();
assertThat(actual.getShardingLogicTableNames(Arrays.asList("LOGIC_TABLE", "BROADCAST_LOGIC_TABLE")), CoreMatchers.<Collection<String>>is(Collections.singletonList("LOGIC_TABLE")));
}
private ShardingRule createShardingRule() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfig = createTableRuleConfig();
......
......@@ -56,7 +56,7 @@ public interface RegistryCenter extends AutoCloseable {
String getDirectly(String key);
/**
* Adjust data is existed or not.
* Judge data is existed or not.
*
* @param key key of data
* @return data is existed or not
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册