strongduanmu commented on a change in pull request #14864:
URL: https://github.com/apache/shardingsphere/pull/14864#discussion_r820376619
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
##########
@@ -216,6 +221,104 @@ private BindingTableRule createBindingTableRule(final
String bindingTableGroup)
return result;
}
+ private void checkSameBindingTables(final Collection<String>
bindingTableGroups, final Map<String, BindingTableRule> bindingTableRules) {
+ for (String each : bindingTableGroups) {
+ List<String> bindingTableList =
Splitter.on(",").trimResults().splitToList(each.toLowerCase());
+ TableRule sampleTableRule = null;
+ for (String bindingTable : bindingTableList) {
+ TableRule tableRule =
bindingTableRules.get(bindingTable).getTableRules().get(bindingTable);
+ if (null == sampleTableRule) {
+ sampleTableRule = tableRule;
+ } else {
+
checkSameActualDatasourceNamesAndActualTableIndex(sampleTableRule, tableRule,
each);
+ }
+ }
+ }
+ }
+
+ private void checkSameActualDatasourceNamesAndActualTableIndex(final
TableRule sampleTableRule, final TableRule tableRule, final String
bindingTableGroup) {
+ if
(!CollectionUtils.isEqualCollection(sampleTableRule.getActualDatasourceNames(),
tableRule.getActualDatasourceNames())) {
Review comment:
@cheese8 Can we remove this util class? Replace it with
`sampleTableRule.getActualDatasourceNames().equals(
tableRule.getActualDatasourceNames()))`
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
##########
@@ -216,6 +221,104 @@ private BindingTableRule createBindingTableRule(final
String bindingTableGroup)
return result;
}
+ private void checkSameBindingTables(final Collection<String>
bindingTableGroups, final Map<String, BindingTableRule> bindingTableRules) {
+ for (String each : bindingTableGroups) {
+ List<String> bindingTableList =
Splitter.on(",").trimResults().splitToList(each.toLowerCase());
+ TableRule sampleTableRule = null;
+ for (String bindingTable : bindingTableList) {
+ TableRule tableRule =
bindingTableRules.get(bindingTable).getTableRules().get(bindingTable);
+ if (null == sampleTableRule) {
+ sampleTableRule = tableRule;
+ } else {
+
checkSameActualDatasourceNamesAndActualTableIndex(sampleTableRule, tableRule,
each);
+ }
+ }
+ }
+ }
+
+ private void checkSameActualDatasourceNamesAndActualTableIndex(final
TableRule sampleTableRule, final TableRule tableRule, final String
bindingTableGroup) {
+ if
(!CollectionUtils.isEqualCollection(sampleTableRule.getActualDatasourceNames(),
tableRule.getActualDatasourceNames())) {
+ throw new ShardingSphereConfigurationException("The %s on
bindingTableGroup `%s` are inconsistent", "actualDatasourceNames",
bindingTableGroup);
+ }
+ checkSameAlgorithmOnDatabase(sampleTableRule, tableRule,
sampleTableRule.getActualDatasourceNames().stream().findFirst().get(),
bindingTableGroup);
+ for (String each : sampleTableRule.getActualDatasourceNames()) {
+ Collection<String> sampleActualTableNames =
sampleTableRule.getActualTableNames(each).stream().map(optional ->
substring(optional)[1])
+ .filter(optional ->
!optional.isEmpty()).collect(Collectors.toList());
+ Collection<String> actualTableNames =
tableRule.getActualTableNames(each).stream().map(optional ->
substring(optional)[1])
+ .filter(optional ->
!optional.isEmpty()).collect(Collectors.toList());
+ if (!CollectionUtils.isEqualCollection(sampleActualTableNames,
actualTableNames)) {
+ throw new ShardingSphereConfigurationException("The %s on
bindingTableGroup `%s` are inconsistent", "actualTableNames",
bindingTableGroup);
+ }
+ checkSameAlgorithmOnTable(sampleTableRule,
sampleTableRule.getActualTableNames(each).stream().findFirst().get(), tableRule,
+
tableRule.getActualTableNames(each).stream().findFirst().get(),
bindingTableGroup);
+ }
+ }
+
+ private void checkSameAlgorithmOnDatabase(final TableRule sampleTableRule,
final TableRule tableRule, final String dataSourceName,
+ final String bindingTableGroup) {
+ Collection<String[]> algorithmExpressions = new ArrayList<>();
+ String logicName = substring(dataSourceName)[0];
+ algorithmExpressions.add(new String[]
{getAlgorithmExpression(sampleTableRule, true), logicName,
getShardingColumn(sampleTableRule.getDatabaseShardingStrategyConfig())});
+ algorithmExpressions.add(new String[]
{getAlgorithmExpression(tableRule, true), logicName,
getShardingColumn(tableRule.getDatabaseShardingStrategyConfig())});
+ checkSameAlgorithmExpression(algorithmExpressions,
"databaseShardingStrategyConfig", bindingTableGroup);
+ }
+
+ private String getAlgorithmExpression(final TableRule tableRule, final
boolean databaseOrTable) {
+ if (null == tableRule.getDatabaseShardingStrategyConfig() || null ==
tableRule.getTableShardingStrategyConfig()) {
Review comment:
Is this logic wrong when check table sharding?
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
##########
@@ -216,6 +221,104 @@ private BindingTableRule createBindingTableRule(final
String bindingTableGroup)
return result;
}
+ private void checkSameBindingTables(final Collection<String>
bindingTableGroups, final Map<String, BindingTableRule> bindingTableRules) {
+ for (String each : bindingTableGroups) {
+ List<String> bindingTableList =
Splitter.on(",").trimResults().splitToList(each.toLowerCase());
+ TableRule sampleTableRule = null;
+ for (String bindingTable : bindingTableList) {
+ TableRule tableRule =
bindingTableRules.get(bindingTable).getTableRules().get(bindingTable);
+ if (null == sampleTableRule) {
+ sampleTableRule = tableRule;
+ } else {
+
checkSameActualDatasourceNamesAndActualTableIndex(sampleTableRule, tableRule,
each);
+ }
+ }
+ }
+ }
+
+ private void checkSameActualDatasourceNamesAndActualTableIndex(final
TableRule sampleTableRule, final TableRule tableRule, final String
bindingTableGroup) {
+ if
(!CollectionUtils.isEqualCollection(sampleTableRule.getActualDatasourceNames(),
tableRule.getActualDatasourceNames())) {
+ throw new ShardingSphereConfigurationException("The %s on
bindingTableGroup `%s` are inconsistent", "actualDatasourceNames",
bindingTableGroup);
+ }
+ checkSameAlgorithmOnDatabase(sampleTableRule, tableRule,
sampleTableRule.getActualDatasourceNames().stream().findFirst().get(),
bindingTableGroup);
+ for (String each : sampleTableRule.getActualDatasourceNames()) {
+ Collection<String> sampleActualTableNames =
sampleTableRule.getActualTableNames(each).stream().map(optional ->
substring(optional)[1])
+ .filter(optional ->
!optional.isEmpty()).collect(Collectors.toList());
+ Collection<String> actualTableNames =
tableRule.getActualTableNames(each).stream().map(optional ->
substring(optional)[1])
+ .filter(optional ->
!optional.isEmpty()).collect(Collectors.toList());
+ if (!CollectionUtils.isEqualCollection(sampleActualTableNames,
actualTableNames)) {
+ throw new ShardingSphereConfigurationException("The %s on
bindingTableGroup `%s` are inconsistent", "actualTableNames",
bindingTableGroup);
+ }
+ checkSameAlgorithmOnTable(sampleTableRule,
sampleTableRule.getActualTableNames(each).stream().findFirst().get(), tableRule,
+
tableRule.getActualTableNames(each).stream().findFirst().get(),
bindingTableGroup);
+ }
+ }
+
+ private void checkSameAlgorithmOnDatabase(final TableRule sampleTableRule,
final TableRule tableRule, final String dataSourceName,
+ final String bindingTableGroup) {
+ Collection<String[]> algorithmExpressions = new ArrayList<>();
+ String logicName = substring(dataSourceName)[0];
+ algorithmExpressions.add(new String[]
{getAlgorithmExpression(sampleTableRule, true), logicName,
getShardingColumn(sampleTableRule.getDatabaseShardingStrategyConfig())});
+ algorithmExpressions.add(new String[]
{getAlgorithmExpression(tableRule, true), logicName,
getShardingColumn(tableRule.getDatabaseShardingStrategyConfig())});
+ checkSameAlgorithmExpression(algorithmExpressions,
"databaseShardingStrategyConfig", bindingTableGroup);
+ }
+
+ private String getAlgorithmExpression(final TableRule tableRule, final
boolean databaseOrTable) {
+ if (null == tableRule.getDatabaseShardingStrategyConfig() || null ==
tableRule.getTableShardingStrategyConfig()) {
+ return "";
+ }
+ String shardingAlgorithmName = databaseOrTable ?
tableRule.getDatabaseShardingStrategyConfig().getShardingAlgorithmName()
+ :
tableRule.getTableShardingStrategyConfig().getShardingAlgorithmName();
+ ShardingAlgorithm shardingAlgorithm =
shardingAlgorithms.get(shardingAlgorithmName);
+ return null == shardingAlgorithm ? "" :
checkWithDefaultValue(shardingAlgorithm.getProps().getProperty("algorithm-expression"),
"");
+ }
+
+ private String checkWithDefaultValue(final String str, final String
defaultValue) {
Review comment:
@cheese8 Can we remove this method with `StringUtils.defaultString()`?
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
##########
@@ -216,6 +221,104 @@ private BindingTableRule createBindingTableRule(final
String bindingTableGroup)
return result;
}
+ private void checkSameBindingTables(final Collection<String>
bindingTableGroups, final Map<String, BindingTableRule> bindingTableRules) {
+ for (String each : bindingTableGroups) {
+ List<String> bindingTableList =
Splitter.on(",").trimResults().splitToList(each.toLowerCase());
+ TableRule sampleTableRule = null;
+ for (String bindingTable : bindingTableList) {
+ TableRule tableRule =
bindingTableRules.get(bindingTable).getTableRules().get(bindingTable);
+ if (null == sampleTableRule) {
+ sampleTableRule = tableRule;
+ } else {
+
checkSameActualDatasourceNamesAndActualTableIndex(sampleTableRule, tableRule,
each);
+ }
+ }
+ }
+ }
+
+ private void checkSameActualDatasourceNamesAndActualTableIndex(final
TableRule sampleTableRule, final TableRule tableRule, final String
bindingTableGroup) {
+ if
(!CollectionUtils.isEqualCollection(sampleTableRule.getActualDatasourceNames(),
tableRule.getActualDatasourceNames())) {
+ throw new ShardingSphereConfigurationException("The %s on
bindingTableGroup `%s` are inconsistent", "actualDatasourceNames",
bindingTableGroup);
+ }
+ checkSameAlgorithmOnDatabase(sampleTableRule, tableRule,
sampleTableRule.getActualDatasourceNames().stream().findFirst().get(),
bindingTableGroup);
+ for (String each : sampleTableRule.getActualDatasourceNames()) {
+ Collection<String> sampleActualTableNames =
sampleTableRule.getActualTableNames(each).stream().map(optional ->
substring(optional)[1])
+ .filter(optional ->
!optional.isEmpty()).collect(Collectors.toList());
+ Collection<String> actualTableNames =
tableRule.getActualTableNames(each).stream().map(optional ->
substring(optional)[1])
+ .filter(optional ->
!optional.isEmpty()).collect(Collectors.toList());
+ if (!CollectionUtils.isEqualCollection(sampleActualTableNames,
actualTableNames)) {
+ throw new ShardingSphereConfigurationException("The %s on
bindingTableGroup `%s` are inconsistent", "actualTableNames",
bindingTableGroup);
+ }
+ checkSameAlgorithmOnTable(sampleTableRule,
sampleTableRule.getActualTableNames(each).stream().findFirst().get(), tableRule,
+
tableRule.getActualTableNames(each).stream().findFirst().get(),
bindingTableGroup);
+ }
+ }
+
+ private void checkSameAlgorithmOnDatabase(final TableRule sampleTableRule,
final TableRule tableRule, final String dataSourceName,
+ final String bindingTableGroup) {
+ Collection<String[]> algorithmExpressions = new ArrayList<>();
Review comment:
@cheese8 LinkedList may be better.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]