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

jianglongtao 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 b486c360187 Replace `ShadowRuleResultSet` with 
`ShowShadowRuleExecutor` (#23812)
b486c360187 is described below

commit b486c36018730a10e0684ee3836f7d29b254bbe3
Author: Zichao <[email protected]>
AuthorDate: Mon Jan 30 06:25:37 2023 +1300

    Replace `ShadowRuleResultSet` with `ShowShadowRuleExecutor` (#23812)
---
 ...eResultSet.java => ShowShadowRuleExecutor.java} | 43 ++++++++++------------
 ...ardingsphere.distsql.handler.query.RQLExecutor} |  6 +--
 ...here.distsql.handler.resultset.DistSQLResultSet |  1 -
 ...etTest.java => ShowShadowRuleExecutorTest.java} | 40 +++++++++++++-------
 4 files changed, 47 insertions(+), 43 deletions(-)

diff --git 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShadowRuleResultSet.java
 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
similarity index 78%
rename from 
features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShadowRuleResultSet.java
rename to 
features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
index cd1844144fd..f6a1e4e9b74 100644
--- 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShadowRuleResultSet.java
+++ 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
@@ -17,14 +17,14 @@
 
 package org.apache.shardingsphere.shadow.distsql.handler.query;
 
-import 
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
 import 
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
 import 
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
 import 
org.apache.shardingsphere.shadow.distsql.parser.statement.ShowShadowRulesStatement;
 import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -32,14 +32,15 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
- * Query result set for show shadow rules.
+ * Show shadow rule executor.
  */
-public final class ShadowRuleResultSet implements DatabaseDistSQLResultSet {
+public final class ShowShadowRuleExecutor implements 
RQLExecutor<ShowShadowRulesStatement> {
     
     private static final String RULE_NAME = "rule_name";
     
@@ -49,19 +50,27 @@ public final class ShadowRuleResultSet implements 
DatabaseDistSQLResultSet {
     
     private static final String SHADOW_TABLE = "shadow_table";
     
-    private Iterator<Map<String, String>> data = Collections.emptyIterator();
-    
     @Override
-    public void init(final ShardingSphereDatabase database, final SQLStatement 
sqlStatement) {
+    public Collection<LocalDataQueryResultRow> getRows(final 
ShardingSphereDatabase database, final ShowShadowRulesStatement sqlStatement) {
         Optional<ShadowRule> rule = 
database.getRuleMetaData().findSingleRule(ShadowRule.class);
-        rule.ifPresent(optional -> 
buildDataSourceIterator((ShadowRuleConfiguration) optional.getConfiguration(), 
(ShowShadowRulesStatement) sqlStatement));
+        rule.ifPresent(optional -> 
buildDataSourceIterator((ShadowRuleConfiguration) optional.getConfiguration(), 
sqlStatement));
+        Iterator<Map<String, String>> data = Collections.emptyIterator();
+        if (rule.isPresent()) {
+            data = buildDataSourceIterator((ShadowRuleConfiguration) 
rule.get().getConfiguration(), sqlStatement);
+        }
+        Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+        while (data.hasNext()) {
+            Map<String, String> row = data.next();
+            result.add(new LocalDataQueryResultRow(row.get(RULE_NAME), 
row.get(SOURCE_NAME), row.get(SHADOW_NAME), row.getOrDefault(SHADOW_TABLE, 
"")));
+        }
+        return result;
     }
     
-    private void buildDataSourceIterator(final ShadowRuleConfiguration 
ruleConfig, final ShowShadowRulesStatement sqlStatement) {
+    private Iterator<Map<String, String>> buildDataSourceIterator(final 
ShadowRuleConfiguration ruleConfig, final ShowShadowRulesStatement 
sqlStatement) {
         Map<String, Map<String, ShadowTableConfiguration>> dataSourceTableMap 
= convertToDataSourceTableMap(ruleConfig.getTables());
         Collection<ShadowDataSourceConfiguration> specifiedConfigs = 
!isSpecified(sqlStatement) ? ruleConfig.getDataSources()
                 : ruleConfig.getDataSources().stream().filter(each -> 
each.getName().equalsIgnoreCase(sqlStatement.getRuleName())).collect(Collectors.toList());
-        data = specifiedConfigs.stream().map(each -> buildDataItem(each, 
dataSourceTableMap)).collect(Collectors.toList()).iterator();
+        return specifiedConfigs.stream().map(each -> buildDataItem(each, 
dataSourceTableMap)).collect(Collectors.toList()).iterator();
     }
     
     private Map<String, Map<String, ShadowTableConfiguration>> 
convertToDataSourceTableMap(final Map<String, ShadowTableConfiguration> tables) 
{
@@ -99,20 +108,6 @@ public final class ShadowRuleResultSet implements 
DatabaseDistSQLResultSet {
         return Arrays.asList(RULE_NAME, SOURCE_NAME, SHADOW_NAME, 
SHADOW_TABLE);
     }
     
-    @Override
-    public boolean next() {
-        return data.hasNext();
-    }
-    
-    @Override
-    public Collection<Object> getRowData() {
-        return buildRowData(data.next());
-    }
-    
-    private Collection<Object> buildRowData(final Map<String, String> data) {
-        return Arrays.asList(data.get(RULE_NAME), data.get(SOURCE_NAME), 
data.get(SHADOW_NAME), data.getOrDefault(SHADOW_TABLE, ""));
-    }
-    
     @Override
     public String getType() {
         return ShowShadowRulesStatement.class.getName();
diff --git 
a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
 
b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
similarity index 66%
copy from 
features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
copy to 
features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index 986c1aba6f1..d3afebb18bf 100644
--- 
a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ 
b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -15,8 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.shadow.distsql.handler.query.ShadowRuleResultSet
-org.apache.shardingsphere.shadow.distsql.handler.query.ShadowAlgorithmResultSet
-org.apache.shardingsphere.shadow.distsql.handler.query.DefaultShadowAlgorithmResultSet
-org.apache.shardingsphere.shadow.distsql.handler.query.ShadowTableRuleResultSet
-org.apache.shardingsphere.shadow.distsql.handler.query.CountShadowRuleResultSet
+org.apache.shardingsphere.shadow.distsql.handler.query.ShowShadowRuleExecutor
diff --git 
a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
 
b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index 986c1aba6f1..daddc164532 100644
--- 
a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ 
b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -15,7 +15,6 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.shadow.distsql.handler.query.ShadowRuleResultSet
 org.apache.shardingsphere.shadow.distsql.handler.query.ShadowAlgorithmResultSet
 
org.apache.shardingsphere.shadow.distsql.handler.query.DefaultShadowAlgorithmResultSet
 org.apache.shardingsphere.shadow.distsql.handler.query.ShadowTableRuleResultSet
diff --git 
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowRuleResultSetTest.java
 
b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
similarity index 67%
rename from 
features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowRuleResultSetTest.java
rename to 
features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
index 107f1f74bdf..ea4ac998114 100644
--- 
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowRuleResultSetTest.java
+++ 
b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
@@ -17,21 +17,22 @@
 
 package org.apache.shardingsphere.shadow.distsql.query;
 
-import 
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import 
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
 import 
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
 import 
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
-import 
org.apache.shardingsphere.shadow.distsql.handler.query.ShadowRuleResultSet;
+import 
org.apache.shardingsphere.shadow.distsql.handler.query.ShowShadowRuleExecutor;
 import 
org.apache.shardingsphere.shadow.distsql.parser.statement.ShowShadowRulesStatement;
 import org.apache.shardingsphere.shadow.rule.ShadowRule;
 import org.junit.Test;
 
-import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
-import java.util.List;
+import java.util.Iterator;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -39,18 +40,31 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-public final class ShadowRuleResultSetTest {
+public final class ShowShadowRuleExecutorTest {
     
     @Test
     public void assertGetRowData() {
-        DatabaseDistSQLResultSet resultSet = new ShadowRuleResultSet();
-        resultSet.init(mockDatabase(), mock(ShowShadowRulesStatement.class));
-        List<Object> actual = new ArrayList<>(resultSet.getRowData());
-        assertThat(actual.size(), is(4));
-        assertThat(actual.get(0), is("shadow_rule"));
-        assertThat(actual.get(1), is("source"));
-        assertThat(actual.get(2), is("shadow"));
-        assertThat(actual.get(3), is("t_order,t_order_1"));
+        RQLExecutor<ShowShadowRulesStatement> executor = new 
ShowShadowRuleExecutor();
+        Collection<LocalDataQueryResultRow> actual = 
executor.getRows(mockDatabase(), mock(ShowShadowRulesStatement.class));
+        assertThat(actual.size(), is(1));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("shadow_rule"));
+        assertThat(row.getCell(2), is("source"));
+        assertThat(row.getCell(3), is("shadow"));
+        assertThat(row.getCell(4), is("t_order,t_order_1"));
+    }
+    
+    @Test
+    public void assertGetColumnNames() {
+        RQLExecutor<ShowShadowRulesStatement> executor = new 
ShowShadowRuleExecutor();
+        Collection<String> columns = executor.getColumnNames();
+        assertThat(columns.size(), is(4));
+        Iterator<String> iterator = columns.iterator();
+        assertThat(iterator.next(), is("rule_name"));
+        assertThat(iterator.next(), is("source_name"));
+        assertThat(iterator.next(), is("shadow_name"));
+        assertThat(iterator.next(), is("shadow_table"));
     }
     
     private ShardingSphereDatabase mockDatabase() {

Reply via email to