This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 8ebf205da2e Refactor RuleConfigurationToDistSQLConverter (#30624)
8ebf205da2e is described below
commit 8ebf205da2eff09548adc66d6ee507fee7c9889f
Author: Raigor <[email protected]>
AuthorDate: Sun Mar 24 16:06:43 2024 +0800
Refactor RuleConfigurationToDistSQLConverter (#30624)
---
.../constant/BroadcastDistSQLConstants.java | 34 ------------------
...oadcastRuleConfigurationToDistSQLConverter.java | 18 +++-------
...astRuleConfigurationToDistSQLConverterTest.java | 40 ++++++++++++++++++++++
...EncryptRuleConfigurationToDistSQLConverter.java | 2 +-
.../MaskRuleConfigurationToDistSQLConverter.java | 2 +-
...littingRuleConfigurationToDistSQLConverter.java | 2 +-
.../ShadowRuleConfigurationToDistSQLConverter.java | 2 +-
...hardingRuleConfigurationToDistSQLConverter.java | 33 ++++++++++--------
.../ConvertYamlConfigurationExecutor.java | 1 +
9 files changed, 67 insertions(+), 67 deletions(-)
diff --git
a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/constant/BroadcastDistSQLConstants.java
b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/constant/BroadcastDistSQLConstants.java
deleted file mode 100644
index a622e1d0bf5..00000000000
---
a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/constant/BroadcastDistSQLConstants.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.broadcast.distsql.handler.constant;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-
-/**
- * Broadcast DistSQL constants.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class BroadcastDistSQLConstants {
-
- public static final String COMMA = ", ";
-
- public static final String SEMI = ";";
-
- public static final String CREATE_BROADCAST_RULE = "CREATE BROADCAST TABLE
RULE ";
-}
diff --git
a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastRuleConfigurationToDistSQLConverter.java
b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastRuleConfigurationToDistSQLConverter.java
index 7244dcfb19f..fc35e348fbb 100644
---
a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastRuleConfigurationToDistSQLConverter.java
+++
b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastRuleConfigurationToDistSQLConverter.java
@@ -17,33 +17,23 @@
package org.apache.shardingsphere.broadcast.distsql.handler.provider;
+import com.google.common.base.Joiner;
import
org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
-import
org.apache.shardingsphere.broadcast.distsql.handler.constant.BroadcastDistSQLConstants;
import
org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.RuleConfigurationToDistSQLConverter;
-import java.util.Iterator;
-
/**
* Broadcast rule configuration to DistSQL converter.
*/
public final class BroadcastRuleConfigurationToDistSQLConverter implements
RuleConfigurationToDistSQLConverter<BroadcastRuleConfiguration> {
+ public static final String CREATE_BROADCAST_TABLE_RULE = "CREATE BROADCAST
TABLE RULE %s;";
+
@Override
public String convert(final BroadcastRuleConfiguration ruleConfig) {
if (ruleConfig.getTables().isEmpty()) {
return "";
}
- StringBuilder result = new
StringBuilder(BroadcastDistSQLConstants.CREATE_BROADCAST_RULE);
- Iterator<String> iterator = ruleConfig.getTables().iterator();
- while (iterator.hasNext()) {
- String tableName = iterator.next();
- result.append(tableName);
- if (iterator.hasNext()) {
- result.append(BroadcastDistSQLConstants.COMMA);
- }
- }
-
result.append(BroadcastDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
- return result.toString();
+ return String.format(CREATE_BROADCAST_TABLE_RULE,
Joiner.on(",").join(ruleConfig.getTables()));
}
@Override
diff --git
a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastRuleConfigurationToDistSQLConverterTest.java
b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastRuleConfigurationToDistSQLConverterTest.java
new file mode 100644
index 00000000000..d5bfe7dfed2
--- /dev/null
+++
b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastRuleConfigurationToDistSQLConverterTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.broadcast.distsql.handler.provider;
+
+import
org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class BroadcastRuleConfigurationToDistSQLConverterTest {
+
+ @Test
+ void assertConvert() {
+ BroadcastRuleConfiguration ruleConfig =
mock(BroadcastRuleConfiguration.class);
+ when(ruleConfig.getTables()).thenReturn(Arrays.asList("t_province",
"t_city"));
+ BroadcastRuleConfigurationToDistSQLConverter converter = new
BroadcastRuleConfigurationToDistSQLConverter();
+ String actual = converter.convert(ruleConfig);
+ assertThat(actual, is("CREATE BROADCAST TABLE RULE
t_province,t_city;"));
+ }
+}
diff --git
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/provider/EncryptRuleConfigurationToDistSQLConverter.java
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/provider/EncryptRuleConfigurationToDistSQLConverter.java
index b507d9ec66f..b14d2aab2f6 100644
---
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/provider/EncryptRuleConfigurationToDistSQLConverter.java
+++
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/provider/EncryptRuleConfigurationToDistSQLConverter.java
@@ -50,7 +50,7 @@ public final class EncryptRuleConfigurationToDistSQLConverter
implements RuleCon
result.append(EncryptDistSQLConstants.COMMA).append(System.lineSeparator());
}
}
-
result.append(EncryptDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+ result.append(EncryptDistSQLConstants.SEMI);
return result.toString();
}
diff --git
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/provider/MaskRuleConfigurationToDistSQLConverter.java
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/provider/MaskRuleConfigurationToDistSQLConverter.java
index 8add471ed30..555a9785c8f 100644
---
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/provider/MaskRuleConfigurationToDistSQLConverter.java
+++
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/provider/MaskRuleConfigurationToDistSQLConverter.java
@@ -59,7 +59,7 @@ public final class MaskRuleConfigurationToDistSQLConverter
implements RuleConfig
result.append(",").append(System.lineSeparator());
}
}
-
result.append(";").append(System.lineSeparator()).append(System.lineSeparator());
+ result.append(";");
return result.toString();
}
diff --git
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/provider/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/provider/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
index 65eaf2a7fbb..0b6780e50f1 100644
---
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/provider/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
+++
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/provider/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
@@ -47,7 +47,7 @@ public final class
ReadwriteSplittingRuleConfigurationToDistSQLConverter impleme
result.append(ReadwriteSplittingDistSQLConstants.COMMA);
}
}
-
result.append(ReadwriteSplittingDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+ result.append(ReadwriteSplittingDistSQLConstants.SEMI);
return result.toString();
}
diff --git
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/provider/ShadowRuleConfigurationToDistSQLConverter.java
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/provider/ShadowRuleConfigurationToDistSQLConverter.java
index 977cee4d51b..f436647ebb4 100644
---
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/provider/ShadowRuleConfigurationToDistSQLConverter.java
+++
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/provider/ShadowRuleConfigurationToDistSQLConverter.java
@@ -50,7 +50,7 @@ public final class ShadowRuleConfigurationToDistSQLConverter
implements RuleConf
result.append(ShadowDistSQLConstants.COMMA);
}
}
-
result.append(ShadowDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+ result.append(ShadowDistSQLConstants.SEMI);
return result.toString();
}
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/provider/ShardingRuleConfigurationToDistSQLConverter.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/provider/ShardingRuleConfigurationToDistSQLConverter.java
index 10a8672a7be..b538e50793d 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/provider/ShardingRuleConfigurationToDistSQLConverter.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/provider/ShardingRuleConfigurationToDistSQLConverter.java
@@ -44,30 +44,33 @@ public final class
ShardingRuleConfigurationToDistSQLConverter implements RuleCo
@Override
public String convert(final ShardingRuleConfiguration ruleConfig) {
+ if (ruleConfig.getTables().isEmpty() &&
ruleConfig.getAutoTables().isEmpty()) {
+ return "";
+ }
StringBuilder result = new StringBuilder();
appendShardingTableRules(ruleConfig, result);
- appendShardingBindingTableRules(ruleConfig, result);
- appendDefaultShardingStrategy(ruleConfig, result);
+ if (!ruleConfig.getBindingTableGroups().isEmpty()) {
+
result.append(System.lineSeparator()).append(System.lineSeparator());
+ appendShardingBindingTableRules(ruleConfig, result);
+ }
+ if (null != ruleConfig.getDefaultDatabaseShardingStrategy() || null !=
ruleConfig.getDefaultTableShardingStrategy()) {
+
result.append(System.lineSeparator()).append(System.lineSeparator());
+ appendDefaultShardingStrategy(ruleConfig, result);
+ }
return result.toString();
}
private void appendShardingTableRules(final ShardingRuleConfiguration
ruleConfig, final StringBuilder stringBuilder) {
- if (ruleConfig.getTables().isEmpty() &&
ruleConfig.getAutoTables().isEmpty()) {
- return;
- }
String tableRules = getTableRules(ruleConfig);
String autoTableRules = getAutoTableRules(ruleConfig);
stringBuilder.append(ShardingDistSQLConstants.CREATE_SHARDING_TABLE).append(tableRules);
if (!Strings.isNullOrEmpty(tableRules) &&
!Strings.isNullOrEmpty(autoTableRules)) {
stringBuilder.append(ShardingDistSQLConstants.COMMA);
}
-
stringBuilder.append(autoTableRules).append(ShardingDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+
stringBuilder.append(autoTableRules).append(ShardingDistSQLConstants.SEMI);
}
private void appendShardingBindingTableRules(final
ShardingRuleConfiguration ruleConfig, final StringBuilder stringBuilder) {
- if (ruleConfig.getBindingTableGroups().isEmpty()) {
- return;
- }
stringBuilder.append(ShardingDistSQLConstants.SHARDING_BINDING_TABLE_RULES);
Iterator<ShardingTableReferenceRuleConfiguration> iterator =
ruleConfig.getBindingTableGroups().iterator();
while (iterator.hasNext()) {
@@ -77,20 +80,20 @@ public final class
ShardingRuleConfigurationToDistSQLConverter implements RuleCo
stringBuilder.append(ShardingDistSQLConstants.COMMA);
}
}
-
stringBuilder.append(ShardingDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+ stringBuilder.append(ShardingDistSQLConstants.SEMI);
}
private void appendDefaultShardingStrategy(final ShardingRuleConfiguration
ruleConfig, final StringBuilder result) {
- if (null == ruleConfig.getDefaultDatabaseShardingStrategy() && null ==
ruleConfig.getDefaultTableShardingStrategy()) {
- return;
- }
if (null != ruleConfig.getDefaultDatabaseShardingStrategy()) {
appendStrategy(ruleConfig.getDefaultDatabaseShardingStrategy(),
ShardingDistSQLConstants.DEFAULT_DATABASE_STRATEGY, result,
ruleConfig.getShardingAlgorithms());
-
result.append(ShardingDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+ result.append(ShardingDistSQLConstants.SEMI);
}
if (null != ruleConfig.getDefaultTableShardingStrategy()) {
+ if (null != ruleConfig.getDefaultDatabaseShardingStrategy()) {
+
result.append(System.lineSeparator()).append(System.lineSeparator());
+ }
appendStrategy(ruleConfig.getDefaultTableShardingStrategy(),
ShardingDistSQLConstants.DEFAULT_TABLE_STRATEGY, result,
ruleConfig.getShardingAlgorithms());
-
result.append(ShardingDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+ result.append(ShardingDistSQLConstants.SEMI);
}
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
index 5acbc110f77..539b58d15d1 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
@@ -83,6 +83,7 @@ public final class ConvertYamlConfigurationExecutor
implements DistSQLQueryExecu
appendResourceDistSQL(yamlConfig, result);
for (RuleConfiguration each : swapToRuleConfigs(yamlConfig).values()) {
result.append(TypedSPILoader.getService(RuleConfigurationToDistSQLConverter.class,
each.getClass()).convert(each));
+
result.append(System.lineSeparator()).append(System.lineSeparator());
}
return result.toString();
}