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 88111c7272a Replace `CountDatabaseDiscoveryRuleResultSet` with
`CountDatabaseDiscoveryRuleExecutor` (#23798)
88111c7272a is described below
commit 88111c7272a35dd89627354dc8f257d1c066db6a
Author: Zichao <[email protected]>
AuthorDate: Sun Jan 29 21:34:28 2023 +1300
Replace `CountDatabaseDiscoveryRuleResultSet` with
`CountDatabaseDiscoveryRuleExecutor` (#23798)
---
...ava => CountDatabaseDiscoveryRuleExecutor.java} | 38 +++++++++-------------
...hardingsphere.distsql.handler.query.RQLExecutor | 1 +
...here.distsql.handler.resultset.DistSQLResultSet | 18 ----------
...=> CountDatabaseDiscoveryRuleExecutorTest.java} | 37 +++++++++++++--------
4 files changed, 39 insertions(+), 55 deletions(-)
diff --git
a/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleResultSet.java
b/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleExecutor.java
similarity index 72%
rename from
features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleResultSet.java
rename to
features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleExecutor.java
index 2aa4de5a174..a6e3aacefdc 100644
---
a/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleResultSet.java
+++
b/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleExecutor.java
@@ -19,13 +19,12 @@ package
org.apache.shardingsphere.dbdiscovery.distsql.handler.query;
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.CountDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
-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.sql.parser.sql.common.statement.SQLStatement;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -35,25 +34,30 @@ import java.util.Optional;
import java.util.function.Supplier;
/**
- * Result set for count database discovery rule.
+ * Count database discovery rule executor.
*/
-public final class CountDatabaseDiscoveryRuleResultSet implements
DatabaseDistSQLResultSet {
+public final class CountDatabaseDiscoveryRuleExecutor implements
RQLExecutor<CountDatabaseDiscoveryRuleStatement> {
private static final String DB_DISCOVERY = "db_discovery";
- private Iterator<Entry<String, LinkedList<Object>>> data =
Collections.emptyIterator();
-
@Override
public Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
}
@Override
- public void init(final ShardingSphereDatabase database, final SQLStatement
sqlStatement) {
+ public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final CountDatabaseDiscoveryRuleStatement
sqlStatement) {
Optional<DatabaseDiscoveryRule> rule =
database.getRuleMetaData().findSingleRule(DatabaseDiscoveryRule.class);
- Map<String, LinkedList<Object>> result = new LinkedHashMap<>();
- rule.ifPresent(optional -> addDBDiscoveryData(result,
database.getName(), rule.get()));
- data = result.entrySet().iterator();
+ Map<String, LinkedList<Object>> rowMap = new LinkedHashMap<>();
+ rule.ifPresent(optional -> addDBDiscoveryData(rowMap,
database.getName(), rule.get()));
+ Iterator<Entry<String, LinkedList<Object>>> data =
rowMap.entrySet().iterator();
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ while (data.hasNext()) {
+ Entry<String, LinkedList<Object>> entry = data.next();
+ entry.getValue().addFirst(entry.getKey());
+ result.add(new LocalDataQueryResultRow(entry.getValue()));
+ }
+ return result;
}
private void addDBDiscoveryData(final Map<String, LinkedList<Object>>
rowMap, final String databaseName, final DatabaseDiscoveryRule rule) {
@@ -72,18 +76,6 @@ public final class CountDatabaseDiscoveryRuleResultSet
implements DatabaseDistSQ
return value;
}
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- Entry<String, LinkedList<Object>> entry = data.next();
- entry.getValue().addFirst(entry.getKey());
- return entry.getValue();
- }
-
@Override
public String getType() {
return CountDatabaseDiscoveryRuleStatement.class.getName();
diff --git
a/features/db-discovery/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
b/features/db-discovery/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index 10f226ea36b..8d925b400c6 100644
---
a/features/db-discovery/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
+++
b/features/db-discovery/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -18,3 +18,4 @@
org.apache.shardingsphere.dbdiscovery.distsql.handler.query.ShowDatabaseDiscoveryRuleExecutor
org.apache.shardingsphere.dbdiscovery.distsql.handler.query.ShowDatabaseDiscoveryTypeExecutor
org.apache.shardingsphere.dbdiscovery.distsql.handler.query.ShowDatabaseDiscoveryHeartbeatExecutor
+org.apache.shardingsphere.dbdiscovery.distsql.handler.query.CountDatabaseDiscoveryRuleExecutor
diff --git
a/features/db-discovery/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
b/features/db-discovery/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
deleted file mode 100644
index 6a51091a549..00000000000
---
a/features/db-discovery/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ /dev/null
@@ -1,18 +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.
-#
-
-org.apache.shardingsphere.dbdiscovery.distsql.handler.query.CountDatabaseDiscoveryRuleResultSet
diff --git
a/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleResultSetTest.java
b/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleExecutorTest.java
similarity index 66%
rename from
features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleResultSetTest.java
rename to
features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleExecutorTest.java
index 210c08a5cba..3715d8dea70 100644
---
a/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleResultSetTest.java
+++
b/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/CountDatabaseDiscoveryRuleExecutorTest.java
@@ -19,38 +19,47 @@ package
org.apache.shardingsphere.dbdiscovery.distsql.handler.query;
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.CountDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
+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.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.junit.Test;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
+import java.util.Iterator;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class CountDatabaseDiscoveryRuleResultSetTest {
+public final class CountDatabaseDiscoveryRuleExecutorTest {
@Test
public void assertGetRowData() {
- CountDatabaseDiscoveryRuleResultSet resultSet = new
CountDatabaseDiscoveryRuleResultSet();
- resultSet.init(mockDatabase(),
mock(CountDatabaseDiscoveryRuleStatement.class));
- assertTrue(resultSet.next());
- List<Object> actual = new ArrayList<>(resultSet.getRowData());
- assertThat(actual.size(), is(3));
- assertThat(actual.get(0), is("db_discovery"));
- assertThat(actual.get(1), is("db_1"));
- assertThat(actual.get(2), is(2));
- assertFalse(resultSet.next());
+ RQLExecutor<CountDatabaseDiscoveryRuleStatement> executor = new
CountDatabaseDiscoveryRuleExecutor();
+ Collection<LocalDataQueryResultRow> actual =
executor.getRows(mockDatabase(),
mock(CountDatabaseDiscoveryRuleStatement.class));
+ assertThat(actual.size(), is(1));
+ Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+ LocalDataQueryResultRow row = iterator.next();
+ assertThat(row.getCell(1), is("db_discovery"));
+ assertThat(row.getCell(2), is("db_1"));
+ assertThat(row.getCell(3), is(2));
+ }
+
+ @Test
+ public void assertGetColumnNames() {
+ RQLExecutor<CountDatabaseDiscoveryRuleStatement> executor = new
CountDatabaseDiscoveryRuleExecutor();
+ Collection<String> columns = executor.getColumnNames();
+ assertThat(columns.size(), is(3));
+ Iterator<String> iterator = columns.iterator();
+ assertThat(iterator.next(), is("rule_name"));
+ assertThat(iterator.next(), is("database"));
+ assertThat(iterator.next(), is("count"));
}
private ShardingSphereDatabase mockDatabase() {