This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 34cef1ecd6d Refactor 
ReadwriteSplittingRuleConfigurationToDistSQLConverter (#33464)
34cef1ecd6d is described below

commit 34cef1ecd6dcbf08ff7f3a09864d22e06797cdea
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Oct 30 12:14:50 2024 +0800

    Refactor ReadwriteSplittingRuleConfigurationToDistSQLConverter (#33464)
---
 ...askRuleConfigurationToDistSQLConverterTest.java | 13 ++----
 ...littingRuleConfigurationToDistSQLConverter.java | 53 +++++++---------------
 ...ingRuleConfigurationToDistSQLConverterTest.java | 41 ++++++++++++-----
 ...dowRuleConfigurationToDistSQLConverterTest.java | 21 +++++----
 ...gleRuleConfigurationToDistSQLConverterTest.java | 20 ++++----
 5 files changed, 75 insertions(+), 73 deletions(-)

diff --git 
a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java
 
b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java
index e0842b8c55b..bc25715f27e 100644
--- 
a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java
+++ 
b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java
@@ -31,8 +31,6 @@ import java.util.Properties;
 
 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 MaskRuleConfigurationToDistSQLConverterTest {
     
@@ -41,21 +39,20 @@ class MaskRuleConfigurationToDistSQLConverterTest {
     
     @Test
     void assertConvertWithEmptyTables() {
-        MaskRuleConfiguration maskRuleConfig = 
mock(MaskRuleConfiguration.class);
-        when(maskRuleConfig.getTables()).thenReturn(Collections.emptyList());
-        assertThat(converter.convert(maskRuleConfig), is(""));
+        MaskRuleConfiguration ruleConfig = new 
MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap());
+        assertThat(converter.convert(ruleConfig), is(""));
     }
     
     @Test
     void assertConvert() {
-        MaskRuleConfiguration maskRuleConfig = getMaskRuleConfiguration();
-        assertThat(converter.convert(maskRuleConfig), is("CREATE MASK RULE"
+        MaskRuleConfiguration ruleConfig = createRuleConfiguration();
+        assertThat(converter.convert(ruleConfig), is("CREATE MASK RULE"
                 + " foo_tbl (" + System.lineSeparator() + "COLUMNS(" + 
System.lineSeparator() + "(NAME=foo_col_1, TYPE(NAME='md5'))," + 
System.lineSeparator() + "(NAME=foo_col_2, TYPE(NAME='md5'))"
                 + System.lineSeparator() + "))," + System.lineSeparator()
                 + " bar_tbl (" + System.lineSeparator() + "COLUMNS(" + 
System.lineSeparator() + "(NAME=bar_col, TYPE(NAME='md5'))" + 
System.lineSeparator() + "));"));
     }
     
-    private MaskRuleConfiguration getMaskRuleConfiguration() {
+    private MaskRuleConfiguration createRuleConfiguration() {
         MaskTableRuleConfiguration fooTableRuleConfig = new 
MaskTableRuleConfiguration("foo_tbl",
                 Arrays.asList(new MaskColumnRuleConfiguration("foo_col_1", 
"md5_algo"), new MaskColumnRuleConfiguration("foo_col_2", "md5_algo")));
         MaskTableRuleConfiguration barTableRuleConfig = new 
MaskTableRuleConfiguration("bar_tbl", Collections.singleton(new 
MaskColumnRuleConfiguration("bar_col", "md5_algo")));
diff --git 
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
 
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
index 87ee2203122..9865b5e4813 100644
--- 
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
+++ 
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverter.java
@@ -26,8 +26,8 @@ import 
org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRul
 import 
org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
 
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * Readwrite-splitting rule configuration to DistSQL converter.
@@ -36,49 +36,30 @@ public final class 
ReadwriteSplittingRuleConfigurationToDistSQLConverter impleme
     
     @Override
     public String convert(final ReadwriteSplittingRuleConfiguration 
ruleConfig) {
-        if (ruleConfig.getDataSourceGroups().isEmpty()) {
-            return "";
-        }
-        StringBuilder result = new 
StringBuilder(ReadwriteSplittingConvertDistSQLConstants.CREATE_READWRITE_SPLITTING_RULE);
-        Iterator<ReadwriteSplittingDataSourceGroupRuleConfiguration> iterator 
= ruleConfig.getDataSourceGroups().iterator();
-        while (iterator.hasNext()) {
-            appendReadWriteSplittingRule(ruleConfig.getLoadBalancers(), 
iterator.next(), result);
-            if (iterator.hasNext()) {
-                result.append(DistSQLConstants.COMMA);
-            }
-        }
-        result.append(DistSQLConstants.SEMI);
-        return result.toString();
+        return ruleConfig.getDataSourceGroups().isEmpty()
+                ? ""
+                : 
ReadwriteSplittingConvertDistSQLConstants.CREATE_READWRITE_SPLITTING_RULE + 
convertReadWriteSplittingRules(ruleConfig) + DistSQLConstants.SEMI;
     }
     
-    private void appendReadWriteSplittingRule(final Map<String, 
AlgorithmConfiguration> loadBalancers,
-                                              final 
ReadwriteSplittingDataSourceGroupRuleConfiguration dataSourceGroupRuleConfig, 
final StringBuilder stringBuilder) {
-        String readDataSourceNames = 
getReadDataSourceNames(dataSourceGroupRuleConfig.getReadDataSourceNames());
+    private String convertReadWriteSplittingRules(final 
ReadwriteSplittingRuleConfiguration ruleConfig) {
+        return ruleConfig.getDataSourceGroups().stream().map(each -> 
convertReadWriteSplittingRule(each, 
ruleConfig.getLoadBalancers())).collect(Collectors.joining(DistSQLConstants.COMMA));
+    }
+    
+    private String convertReadWriteSplittingRule(final 
ReadwriteSplittingDataSourceGroupRuleConfiguration dataSourceGroupRuleConfig, 
final Map<String, AlgorithmConfiguration> loadBalancers) {
+        String readDataSourceNames = 
convertReadDataSourceNames(dataSourceGroupRuleConfig.getReadDataSourceNames());
         String transactionalReadQueryStrategy = 
dataSourceGroupRuleConfig.getTransactionalReadQueryStrategy().name();
-        String loadBalancerType = 
getLoadBalancerType(loadBalancers.get(dataSourceGroupRuleConfig.getLoadBalancerName()));
-        
stringBuilder.append(String.format(ReadwriteSplittingConvertDistSQLConstants.READWRITE_SPLITTING_RULE,
-                dataSourceGroupRuleConfig.getName(), 
dataSourceGroupRuleConfig.getWriteDataSourceName(), readDataSourceNames, 
transactionalReadQueryStrategy, loadBalancerType));
+        String loadBalancerType = 
convertLoadBalancerType(loadBalancers.get(dataSourceGroupRuleConfig.getLoadBalancerName()));
+        return 
String.format(ReadwriteSplittingConvertDistSQLConstants.READWRITE_SPLITTING_RULE,
+                dataSourceGroupRuleConfig.getName(), 
dataSourceGroupRuleConfig.getWriteDataSourceName(), readDataSourceNames, 
transactionalReadQueryStrategy, loadBalancerType);
     }
     
-    private String getReadDataSourceNames(final Collection<String> 
readDataSourceNames) {
-        StringBuilder result = new StringBuilder();
-        Iterator<String> iterator = readDataSourceNames.iterator();
-        while (iterator.hasNext()) {
-            
result.append(String.format(ReadwriteSplittingConvertDistSQLConstants.READ_DATA_SOURCE,
 iterator.next()));
-            if (iterator.hasNext()) {
-                result.append(DistSQLConstants.COMMA);
-            }
-        }
-        return result.toString();
+    private String convertReadDataSourceNames(final Collection<String> 
readDataSourceNames) {
+        return readDataSourceNames.stream().map(each -> 
String.format(ReadwriteSplittingConvertDistSQLConstants.READ_DATA_SOURCE, 
each)).collect(Collectors.joining(DistSQLConstants.COMMA));
     }
     
-    private String getLoadBalancerType(final AlgorithmConfiguration 
algorithmConfig) {
-        StringBuilder result = new StringBuilder();
+    private String convertLoadBalancerType(final AlgorithmConfiguration 
algorithmConfig) {
         String loadBalancerType = 
AlgorithmDistSQLConverter.getAlgorithmType(algorithmConfig);
-        if (!Strings.isNullOrEmpty(loadBalancerType)) {
-            
result.append(DistSQLConstants.COMMA).append(System.lineSeparator()).append(loadBalancerType);
-        }
-        return result.toString();
+        return Strings.isNullOrEmpty(loadBalancerType) ? "" : 
DistSQLConstants.COMMA + System.lineSeparator() + loadBalancerType;
     }
     
     @Override
diff --git 
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverterTest.java
 
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverterTest.java
index 6bbdd0cac94..682cfe61d48 100644
--- 
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverterTest.java
+++ 
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/converter/ReadwriteSplittingRuleConfigurationToDistSQLConverterTest.java
@@ -30,8 +30,6 @@ import java.util.Collections;
 
 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 ReadwriteSplittingRuleConfigurationToDistSQLConverterTest {
     
@@ -41,20 +39,41 @@ class 
ReadwriteSplittingRuleConfigurationToDistSQLConverterTest {
     
     @Test
     void assertConvertWithEmptyDataSources() {
-        ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfig = 
mock(ReadwriteSplittingRuleConfiguration.class);
-        
when(readwriteSplittingRuleConfig.getDataSourceGroups()).thenReturn(Collections.emptyList());
+        ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfig = new 
ReadwriteSplittingRuleConfiguration(Collections.emptyList(), 
Collections.emptyMap());
         assertThat(converter.convert(readwriteSplittingRuleConfig), is(""));
     }
     
     @Test
     void assertConvert() {
-        ReadwriteSplittingDataSourceGroupRuleConfiguration 
dataSourceGroupConfig =
-                new 
ReadwriteSplittingDataSourceGroupRuleConfiguration("readwrite_ds", 
"ds_primary", Arrays.asList("ds_slave_0", "ds_slave_1"), "test");
-        ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfig = new 
ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceGroupConfig),
+        ReadwriteSplittingRuleConfiguration ruleConfig = 
createRuleConfiguration();
+        assertThat(converter.convert(ruleConfig),
+                is("CREATE READWRITE_SPLITTING RULE foo_ds ("
+                        + System.lineSeparator()
+                        + "WRITE_STORAGE_UNIT=ds_primary,"
+                        + System.lineSeparator()
+                        + "READ_STORAGE_UNITS(ds_slave_0,ds_slave_1),"
+                        + System.lineSeparator()
+                        + "TRANSACTIONAL_READ_QUERY_STRATEGY='DYNAMIC',"
+                        + System.lineSeparator()
+                        + "TYPE(NAME='random', 
PROPERTIES('read_weight'='2:1'))"
+                        + System.lineSeparator()
+                        + "), bar_ds ("
+                        + System.lineSeparator()
+                        + "WRITE_STORAGE_UNIT=ds_primary,"
+                        + System.lineSeparator()
+                        + "READ_STORAGE_UNITS(ds_slave_0,ds_slave_1),"
+                        + System.lineSeparator()
+                        + "TRANSACTIONAL_READ_QUERY_STRATEGY='DYNAMIC'"
+                        + System.lineSeparator()
+                        + ");"));
+    }
+    
+    private static ReadwriteSplittingRuleConfiguration 
createRuleConfiguration() {
+        ReadwriteSplittingDataSourceGroupRuleConfiguration 
dataSourceGroupConfig0 = new ReadwriteSplittingDataSourceGroupRuleConfiguration(
+                "foo_ds", "ds_primary", Arrays.asList("ds_slave_0", 
"ds_slave_1"), "test");
+        ReadwriteSplittingDataSourceGroupRuleConfiguration 
dataSourceGroupConfig1 = new ReadwriteSplittingDataSourceGroupRuleConfiguration(
+                "bar_ds", "ds_primary", Arrays.asList("ds_slave_0", 
"ds_slave_1"), "not_existed");
+        return new 
ReadwriteSplittingRuleConfiguration(Arrays.asList(dataSourceGroupConfig0, 
dataSourceGroupConfig1),
                 Collections.singletonMap("test", new 
AlgorithmConfiguration("random", PropertiesBuilder.build(new 
PropertiesBuilder.Property("read_weight", "2:1")))));
-        assertThat(converter.convert(readwriteSplittingRuleConfig),
-                is("CREATE READWRITE_SPLITTING RULE readwrite_ds (" + 
System.lineSeparator() + "WRITE_STORAGE_UNIT=ds_primary," + 
System.lineSeparator() + "READ_STORAGE_UNITS(ds_slave_0,ds_slave_1),"
-                        + System.lineSeparator() + 
"TRANSACTIONAL_READ_QUERY_STRATEGY='DYNAMIC'," + System.lineSeparator() + 
"TYPE(NAME='random', PROPERTIES('read_weight'='2:1'))"
-                        + System.lineSeparator() + ");"));
     }
 }
diff --git 
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverterTest.java
 
b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverterTest.java
index 4b7a8f1729c..1e2c08c1a2b 100644
--- 
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverterTest.java
+++ 
b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverterTest.java
@@ -38,19 +38,24 @@ class ShadowRuleConfigurationToDistSQLConverterTest {
     
     @Test
     void assertConvertWithoutDataSources() {
-        ShadowRuleConfiguration shadowRuleConfig = new 
ShadowRuleConfiguration();
-        assertThat(converter.convert(shadowRuleConfig), is(""));
+        ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration();
+        assertThat(converter.convert(ruleConfig), is(""));
     }
     
     @Test
     void assertConvert() {
-        ShadowRuleConfiguration shadowRuleConfig = new 
ShadowRuleConfiguration();
-        shadowRuleConfig.getDataSources().add(new 
ShadowDataSourceConfiguration("shadow_rule", "source", "shadow"));
-        
shadowRuleConfig.getShadowAlgorithms().put("user_id_select_match_algorithm", 
new AlgorithmConfiguration("REGEX_MATCH", new Properties()));
-        shadowRuleConfig.getTables().put("t_order", new 
ShadowTableConfiguration(Collections.singleton("shadow_rule"), 
Collections.singleton("user_id_select_match_algorithm")));
-        shadowRuleConfig.getTables().put("t_order_item", new 
ShadowTableConfiguration(Collections.singleton("shadow_rule"), 
Collections.singleton("user_id_select_match_algorithm")));
-        assertThat(converter.convert(shadowRuleConfig),
+        ShadowRuleConfiguration ruleConfig = createRuleConfiguration();
+        assertThat(converter.convert(ruleConfig),
                 is("CREATE SHADOW RULE shadow_rule(" + System.lineSeparator() 
+ "SOURCE=source," + System.lineSeparator() + "SHADOW=shadow," + 
System.lineSeparator()
                         + "t_order(TYPE(NAME='regex_match'))," + 
System.lineSeparator() + "t_order_item(TYPE(NAME='regex_match'))" + 
System.lineSeparator() + ");"));
     }
+    
+    private static ShadowRuleConfiguration createRuleConfiguration() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.getDataSources().add(new 
ShadowDataSourceConfiguration("shadow_rule", "source", "shadow"));
+        result.getShadowAlgorithms().put("user_id_select_match_algorithm", new 
AlgorithmConfiguration("REGEX_MATCH", new Properties()));
+        result.getTables().put("t_order", new 
ShadowTableConfiguration(Collections.singleton("shadow_rule"), 
Collections.singleton("user_id_select_match_algorithm")));
+        result.getTables().put("t_order_item", new 
ShadowTableConfiguration(Collections.singleton("shadow_rule"), 
Collections.singleton("user_id_select_match_algorithm")));
+        return result;
+    }
 }
diff --git 
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/converter/SingleRuleConfigurationToDistSQLConverterTest.java
 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/converter/SingleRuleConfigurationToDistSQLConverterTest.java
index e1c79f1fbd9..300e3feb635 100644
--- 
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/converter/SingleRuleConfigurationToDistSQLConverterTest.java
+++ 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/converter/SingleRuleConfigurationToDistSQLConverterTest.java
@@ -34,21 +34,21 @@ class SingleRuleConfigurationToDistSQLConverterTest {
     private final RuleConfigurationToDistSQLConverter<SingleRuleConfiguration> 
converter = 
TypedSPILoader.getService(RuleConfigurationToDistSQLConverter.class, 
SingleRuleConfiguration.class);
     
     @Test
-    void assertConvert() {
-        SingleRuleConfiguration singleRuleConfig = new 
SingleRuleConfiguration(Arrays.asList("foo_tbl", "bar_tbl"), "foo_ds");
-        assertThat(converter.convert(singleRuleConfig),
-                is("LOAD SINGLE TABLE foo_tbl,bar_tbl;" + 
System.lineSeparator() + System.lineSeparator() + "SET DEFAULT SINGLE TABLE 
STORAGE UNIT = foo_ds;"));
+    void assertConvertWithEmptyTablesAndDefaultDatasource() {
+        SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
+        assertThat(converter.convert(ruleConfig), is(""));
     }
     
     @Test
-    void assertConvertWithDefaultDatasourceOnly() {
-        SingleRuleConfiguration singleRuleConfig = new 
SingleRuleConfiguration(Collections.emptyList(), "foo_ds");
-        assertThat(converter.convert(singleRuleConfig), is("SET DEFAULT SINGLE 
TABLE STORAGE UNIT = foo_ds;"));
+    void assertConvert() {
+        SingleRuleConfiguration ruleConfig = new 
SingleRuleConfiguration(Arrays.asList("foo_tbl", "bar_tbl"), "foo_ds");
+        assertThat(converter.convert(ruleConfig),
+                is("LOAD SINGLE TABLE foo_tbl,bar_tbl;" + 
System.lineSeparator() + System.lineSeparator() + "SET DEFAULT SINGLE TABLE 
STORAGE UNIT = foo_ds;"));
     }
     
     @Test
-    void assertConvertWithEmptyTablesAndDefaultDatasource() {
-        SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
-        assertThat(converter.convert(ruleConfig), is(""));
+    void assertConvertWithDefaultDatasourceOnly() {
+        SingleRuleConfiguration ruleConfig = new 
SingleRuleConfiguration(Collections.emptyList(), "foo_ds");
+        assertThat(converter.convert(ruleConfig), is("SET DEFAULT SINGLE TABLE 
STORAGE UNIT = foo_ds;"));
     }
 }

Reply via email to