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 8b676c05aec Refactor ShadowRuleConfigurationToDistSQLConverter (#33463)
8b676c05aec is described below

commit 8b676c05aecbe01f0f2b78d2d614c3a74d3344ee
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Oct 30 10:57:13 2024 +0800

    Refactor ShadowRuleConfigurationToDistSQLConverter (#33463)
---
 .../converter/ShadowConvertDistSQLConstants.java   |  2 +-
 .../ShadowRuleConfigurationToDistSQLConverter.java | 65 ++++++++--------------
 ...dowRuleConfigurationToDistSQLConverterTest.java |  5 +-
 3 files changed, 24 insertions(+), 48 deletions(-)

diff --git 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowConvertDistSQLConstants.java
 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowConvertDistSQLConstants.java
index 77802362ed7..e355737a773 100644
--- 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowConvertDistSQLConstants.java
+++ 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowConvertDistSQLConstants.java
@@ -28,7 +28,7 @@ public final class ShadowConvertDistSQLConstants {
     
     public static final String CREATE_SHADOW_RULE = "CREATE SHADOW RULE";
     
-    public static final String SHADOW_RULE = " %s("
+    public static final String SHADOW_DATA_SOURCE = " %s("
             + System.lineSeparator()
             + "SOURCE=%s,"
             + System.lineSeparator()
diff --git 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverter.java
 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverter.java
index 018fa5e809c..744084b0acf 100644
--- 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverter.java
+++ 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleConfigurationToDistSQLConverter.java
@@ -26,9 +26,8 @@ import 
org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfig
 import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration;
 
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.Map;
-import java.util.Map.Entry;
+import java.util.stream.Collectors;
 
 /**
  * Shadow rule configuration to DistSQL converter.
@@ -37,51 +36,31 @@ public final class 
ShadowRuleConfigurationToDistSQLConverter implements RuleConf
     
     @Override
     public String convert(final ShadowRuleConfiguration ruleConfig) {
-        if (ruleConfig.getDataSources().isEmpty()) {
-            return "";
-        }
-        StringBuilder result = new 
StringBuilder(ShadowConvertDistSQLConstants.CREATE_SHADOW_RULE);
-        Iterator<ShadowDataSourceConfiguration> iterator = 
ruleConfig.getDataSources().iterator();
-        while (iterator.hasNext()) {
-            ShadowDataSourceConfiguration dataSourceConfig = iterator.next();
-            String shadowRuleName = dataSourceConfig.getName();
-            String shadowTables = getShadowTables(shadowRuleName, 
ruleConfig.getTables(), ruleConfig.getShadowAlgorithms());
-            result.append(
-                    String.format(ShadowConvertDistSQLConstants.SHADOW_RULE, 
shadowRuleName, dataSourceConfig.getProductionDataSourceName(), 
dataSourceConfig.getShadowDataSourceName(), shadowTables));
-            if (iterator.hasNext()) {
-                result.append(DistSQLConstants.COMMA);
-            }
-        }
-        result.append(DistSQLConstants.SEMI);
-        return result.toString();
+        return ruleConfig.getDataSources().isEmpty() ? "" : 
ShadowConvertDistSQLConstants.CREATE_SHADOW_RULE + 
convertShadowDataSources(ruleConfig) + DistSQLConstants.SEMI;
     }
     
-    private String getShadowTables(final String shadowRuleName, final 
Map<String, ShadowTableConfiguration> ruleConfig, final Map<String, 
AlgorithmConfiguration> algorithmConfigs) {
-        StringBuilder result = new StringBuilder();
-        Iterator<Entry<String, ShadowTableConfiguration>> iterator = 
ruleConfig.entrySet().iterator();
-        while (iterator.hasNext()) {
-            Entry<String, ShadowTableConfiguration> shadowTableConfig = 
iterator.next();
-            if 
(shadowTableConfig.getValue().getDataSourceNames().contains(shadowRuleName)) {
-                String shadowTableTypes = 
getShadowTableTypes(shadowTableConfig.getValue().getShadowAlgorithmNames(), 
algorithmConfigs);
-                
result.append(String.format(ShadowConvertDistSQLConstants.SHADOW_TABLE, 
shadowTableConfig.getKey(), shadowTableTypes));
-            }
-            if (iterator.hasNext()) {
-                
result.append(DistSQLConstants.COMMA).append(System.lineSeparator());
-            }
-        }
-        return result.toString();
+    private String convertShadowDataSources(final ShadowRuleConfiguration 
ruleConfig) {
+        return ruleConfig.getDataSources().stream().map(each -> 
convertShadowDataSource(each, 
ruleConfig)).collect(Collectors.joining(DistSQLConstants.COMMA));
     }
     
-    private String getShadowTableTypes(final Collection<String> 
shadowAlgorithmNames, final Map<String, AlgorithmConfiguration> 
algorithmConfigs) {
-        StringBuilder result = new StringBuilder();
-        Iterator<String> iterator = shadowAlgorithmNames.iterator();
-        while (iterator.hasNext()) {
-            
result.append(AlgorithmDistSQLConverter.getAlgorithmType(algorithmConfigs.get(iterator.next())));
-            if (iterator.hasNext()) {
-                result.append(DistSQLConstants.COMMA).append(' ');
-            }
-        }
-        return result.toString();
+    private String convertShadowDataSource(final ShadowDataSourceConfiguration 
dataSourceConfig, final ShadowRuleConfiguration ruleConfig) {
+        String shadowTables = convertShadowTables(dataSourceConfig.getName(), 
ruleConfig.getTables(), ruleConfig.getShadowAlgorithms());
+        return String.format(ShadowConvertDistSQLConstants.SHADOW_DATA_SOURCE,
+                dataSourceConfig.getName(), 
dataSourceConfig.getProductionDataSourceName(), 
dataSourceConfig.getShadowDataSourceName(), shadowTables);
+    }
+    
+    private String convertShadowTables(final String shadowRuleName, final 
Map<String, ShadowTableConfiguration> tableConfigs, final Map<String, 
AlgorithmConfiguration> algorithmConfigs) {
+        return tableConfigs.entrySet().stream().filter(entry -> 
entry.getValue().getDataSourceNames().contains(shadowRuleName))
+                .map(entry -> convertShadowTable(entry.getKey(), 
entry.getValue(), 
algorithmConfigs)).collect(Collectors.joining(DistSQLConstants.COMMA + 
System.lineSeparator()));
+    }
+    
+    private String convertShadowTable(final String shadowTableName, final 
ShadowTableConfiguration shadowTableConfig, final Map<String, 
AlgorithmConfiguration> algorithmConfigs) {
+        String shadowTableTypes = 
convertShadowTableTypes(shadowTableConfig.getShadowAlgorithmNames(), 
algorithmConfigs);
+        return String.format(ShadowConvertDistSQLConstants.SHADOW_TABLE, 
shadowTableName, shadowTableTypes);
+    }
+    
+    private String convertShadowTableTypes(final Collection<String> 
shadowAlgorithmNames, final Map<String, AlgorithmConfiguration> 
algorithmConfigs) {
+        return shadowAlgorithmNames.stream().map(each -> 
AlgorithmDistSQLConverter.getAlgorithmType(algorithmConfigs.get(each))).collect(Collectors.joining(DistSQLConstants.COMMA
 + ' '));
     }
     
     @Override
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 a5e129ab05a..4b7a8f1729c 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
@@ -30,8 +30,6 @@ import java.util.Properties;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 class ShadowRuleConfigurationToDistSQLConverterTest {
     
@@ -40,8 +38,7 @@ class ShadowRuleConfigurationToDistSQLConverterTest {
     
     @Test
     void assertConvertWithoutDataSources() {
-        ShadowRuleConfiguration shadowRuleConfig = 
mock(ShadowRuleConfiguration.class);
-        
when(shadowRuleConfig.getDataSources()).thenReturn(Collections.emptyList());
+        ShadowRuleConfiguration shadowRuleConfig = new 
ShadowRuleConfiguration();
         assertThat(converter.convert(shadowRuleConfig), is(""));
     }
     

Reply via email to