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 98261c57318 Replace `DefaultShardingStrategyResultSet` with
`ShowDefaultShardingStrategyExecutor` (#23841)
98261c57318 is described below
commit 98261c57318569974aae8d98656704bd6809ae0c
Author: Zichao <[email protected]>
AuthorDate: Tue Jan 31 00:53:01 2023 +1300
Replace `DefaultShardingStrategyResultSet` with
`ShowDefaultShardingStrategyExecutor` (#23841)
---
...va => ShowDefaultShardingStrategyExecutor.java} | 38 ++++-----
...hardingsphere.distsql.handler.query.RQLExecutor | 1 +
...here.distsql.handler.resultset.DistSQLResultSet | 1 -
...> ShowDefaultShardingStrategyExecutorTest.java} | 98 +++++++++++++---------
.../framework/helper/PipelineCaseHelper.java | 4 +-
5 files changed, 77 insertions(+), 65 deletions(-)
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/DefaultShardingStrategyResultSet.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowDefaultShardingStrategyExecutor.java
similarity index 77%
rename from
features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/DefaultShardingStrategyResultSet.java
rename to
features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowDefaultShardingStrategyExecutor.java
index 2f53f20ef65..78161f7cc05 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/DefaultShardingStrategyResultSet.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowDefaultShardingStrategyExecutor.java
@@ -17,15 +17,15 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import
org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyType;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowDefaultShardingStrategyStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Arrays;
import java.util.Collection;
@@ -38,16 +38,24 @@ import java.util.Map.Entry;
import java.util.Optional;
/**
- * Result set for show default sharding strategy.
+ * Show default sharding strategy executor.
*/
-public final class DefaultShardingStrategyResultSet implements
DatabaseDistSQLResultSet {
-
- private Iterator<Entry<String, LinkedList<Object>>> data =
Collections.emptyIterator();
+public final class ShowDefaultShardingStrategyExecutor implements
RQLExecutor<ShowDefaultShardingStrategyStatement> {
@Override
- public void init(final ShardingSphereDatabase database, final SQLStatement
sqlStatement) {
+ public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowDefaultShardingStrategyStatement
sqlStatement) {
Optional<ShardingRule> shardingRule =
database.getRuleMetaData().findSingleRule(ShardingRule.class);
- shardingRule.ifPresent(optional -> data =
buildData(optional).entrySet().iterator());
+ if (!shardingRule.isPresent()) {
+ return Collections.emptyList();
+ }
+ Iterator<Entry<String, LinkedList<Object>>> data =
buildData(shardingRule.get()).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 Map<String, LinkedList<Object>> buildData(final ShardingRule rule)
{
@@ -70,7 +78,7 @@ public final class DefaultShardingStrategyResultSet
implements DatabaseDistSQLRe
result.addAll(strategyType.getConfigurationContents(strategyConfig));
AlgorithmConfiguration algorithmConfig =
ruleConfig.getShardingAlgorithms().get(strategyConfig.getShardingAlgorithmName());
result.add(algorithmConfig.getType());
- result.add(algorithmConfig.getProps());
+ result.add(algorithmConfig.getProps().toString());
return result;
}
@@ -79,18 +87,6 @@ public final class DefaultShardingStrategyResultSet
implements DatabaseDistSQLRe
return Arrays.asList("name", "type", "sharding_column",
"sharding_algorithm_name", "sharding_algorithm_type",
"sharding_algorithm_props");
}
- @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 ShowDefaultShardingStrategyStatement.class.getName();
diff --git
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index 7252ed26763..2ecc5b481e2 100644
---
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
+++
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -18,3 +18,4 @@
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingTableRuleExecutor
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingAlgorithmExecutor
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingKeyGeneratorExecutor
+org.apache.shardingsphere.sharding.distsql.handler.query.ShowDefaultShardingStrategyExecutor
diff --git
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index b13404eb985..4b6655ea464 100644
---
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -19,7 +19,6 @@
org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableReferenceR
org.apache.shardingsphere.sharding.distsql.handler.query.BroadcastTableRuleResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.ShardingAuditorsResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableNodesResultSet
-org.apache.shardingsphere.sharding.distsql.handler.query.DefaultShardingStrategyResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.UnusedShardingAlgorithmsResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.UnusedShardingKeyGeneratorResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.UnusedShardingAuditorsResultSet
diff --git
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingDefaultShardingStrategyResultSetTest.java
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java
similarity index 57%
rename from
features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingDefaultShardingStrategyResultSetTest.java
rename to
features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java
index 9658a932d54..863b7ee09ab 100644
---
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingDefaultShardingStrategyResultSetTest.java
+++
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java
@@ -17,8 +17,10 @@
package org.apache.shardingsphere.sharding.distsql.query;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
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.sharding.api.config.ShardingRuleConfiguration;
@@ -26,16 +28,16 @@ import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexSh
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
-import
org.apache.shardingsphere.sharding.distsql.handler.query.DefaultShardingStrategyResultSet;
-import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingAlgorithmsStatement;
+import
org.apache.shardingsphere.sharding.distsql.handler.query.ShowDefaultShardingStrategyExecutor;
+import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowDefaultShardingStrategyStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
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;
@@ -43,7 +45,7 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class ShardingDefaultShardingStrategyResultSetTest {
+public final class ShowDefaultShardingStrategyExecutorTest {
@Test
public void assertGetRowData() {
@@ -51,45 +53,59 @@ public final class
ShardingDefaultShardingStrategyResultSetTest {
ShardingRule rule1 = mock(ShardingRule.class);
when(rule1.getConfiguration()).thenReturn(createRuleConfiguration1());
when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.singleton(rule1)));
- DefaultShardingStrategyResultSet resultSet = new
DefaultShardingStrategyResultSet();
- resultSet.init(database, mock(ShowShardingAlgorithmsStatement.class));
- List<Object> actual = new ArrayList<>(resultSet.getRowData());
- assertThat(actual.size(), is(6));
- assertThat(actual.get(0), is("TABLE"));
- assertThat(actual.get(1), is("NONE"));
- assertThat(actual.get(2), is(""));
- assertThat(actual.get(3), is(""));
- assertThat(actual.get(4), is(""));
- assertThat(actual.get(5), is(""));
- actual = new ArrayList<>(resultSet.getRowData());
- assertThat(actual.size(), is(6));
- assertThat(actual.get(0), is("DATABASE"));
- assertThat(actual.get(1), is("COMPLEX"));
- assertThat(actual.get(2), is("use_id, order_id"));
- assertThat(actual.get(3), is("database_inline"));
- assertThat(actual.get(4), is("INLINE"));
- assertThat(actual.get(5).toString(),
is("{algorithm-expression=ds_${user_id % 2}}"));
+ RQLExecutor<ShowDefaultShardingStrategyStatement> executor = new
ShowDefaultShardingStrategyExecutor();
+ Collection<LocalDataQueryResultRow> actual =
executor.getRows(database, mock(ShowDefaultShardingStrategyStatement.class));
+ assertThat(actual.size(), is(2));
+ Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+ LocalDataQueryResultRow row = iterator.next();
+ assertThat(row.getCell(1), is("TABLE"));
+ assertThat(row.getCell(2), is("NONE"));
+ assertThat(row.getCell(3), is(""));
+ assertThat(row.getCell(4), is(""));
+ assertThat(row.getCell(5), is(""));
+ assertThat(row.getCell(6), is(""));
+ row = iterator.next();
+ assertThat(row.getCell(1), is("DATABASE"));
+ assertThat(row.getCell(2), is("COMPLEX"));
+ assertThat(row.getCell(3), is("use_id, order_id"));
+ assertThat(row.getCell(4), is("database_inline"));
+ assertThat(row.getCell(5), is("INLINE"));
+ assertThat(row.getCell(6), is("{algorithm-expression=ds_${user_id %
2}}"));
ShardingRule rule2 = mock(ShardingRule.class);
when(rule2.getConfiguration()).thenReturn(createRuleConfiguration2());
when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.singleton(rule2)));
- resultSet = new DefaultShardingStrategyResultSet();
- resultSet.init(database, mock(ShowShardingAlgorithmsStatement.class));
- actual = new ArrayList<>(resultSet.getRowData());
- assertThat(actual.size(), is(6));
- assertThat(actual.get(0), is("TABLE"));
- assertThat(actual.get(1), is("STANDARD"));
- assertThat(actual.get(2), is("use_id"));
- assertThat(actual.get(3), is("database_inline"));
- assertThat(actual.get(4), is("INLINE"));
- assertThat(actual.get(5).toString(),
is("{algorithm-expression=ds_${user_id % 2}}"));
- actual = new ArrayList<>(resultSet.getRowData());
- assertThat(actual.size(), is(6));
- assertThat(actual.get(0), is("DATABASE"));
- assertThat(actual.get(1), is("HINT"));
- assertThat(actual.get(2), is(""));
- assertThat(actual.get(3), is("database_inline"));
- assertThat(actual.get(4), is("INLINE"));
- assertThat(actual.get(5).toString(),
is("{algorithm-expression=ds_${user_id % 2}}"));
+ executor = new ShowDefaultShardingStrategyExecutor();
+ actual = executor.getRows(database,
mock(ShowDefaultShardingStrategyStatement.class));
+ assertThat(actual.size(), is(2));
+ iterator = actual.iterator();
+ row = iterator.next();
+ assertThat(row.getCell(1), is("TABLE"));
+ assertThat(row.getCell(2), is("STANDARD"));
+ assertThat(row.getCell(3), is("use_id"));
+ assertThat(row.getCell(4), is("database_inline"));
+ assertThat(row.getCell(5), is("INLINE"));
+ assertThat(row.getCell(6), is("{algorithm-expression=ds_${user_id %
2}}"));
+ row = iterator.next();
+ assertThat(row.getCell(1), is("DATABASE"));
+ assertThat(row.getCell(2), is("HINT"));
+ assertThat(row.getCell(3), is(""));
+ assertThat(row.getCell(4), is("database_inline"));
+ assertThat(row.getCell(5), is("INLINE"));
+ assertThat(row.getCell(6), is("{algorithm-expression=ds_${user_id %
2}}"));
+ }
+
+ @Test
+ public void assertGetColumnNames() {
+ RQLExecutor<ShowDefaultShardingStrategyStatement> executor = new
ShowDefaultShardingStrategyExecutor();
+ Collection<String> columns = executor.getColumnNames();
+ assertThat(columns.size(), is(6));
+ Iterator<String> iterator = columns.iterator();
+ assertThat(iterator.next(), is("name"));
+ assertThat(iterator.next(), is("type"));
+ assertThat(iterator.next(), is("sharding_column"));
+ assertThat(iterator.next(), is("sharding_algorithm_name"));
+ assertThat(iterator.next(), is("sharding_algorithm_type"));
+ assertThat(iterator.next(), is("sharding_algorithm_props"));
}
private RuleConfiguration createRuleConfiguration1() {
diff --git
a/test/e2e/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/framework/helper/PipelineCaseHelper.java
b/test/e2e/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/framework/helper/PipelineCaseHelper.java
index 4a121254f39..a6517644502 100644
---
a/test/e2e/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/framework/helper/PipelineCaseHelper.java
+++
b/test/e2e/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/framework/helper/PipelineCaseHelper.java
@@ -123,8 +123,8 @@ public final class PipelineCaseHelper {
* @param recordCount record count
* @throws SQLException sql exception
*/
- public static void batchInsertOrderRecordsWithGeneralColumns(final
Connection connection, final KeyGenerateAlgorithm keyGenerateAlgorithm, final
String tableName, final int recordCount)
- throws SQLException {
+ public static void batchInsertOrderRecordsWithGeneralColumns(final
Connection connection, final KeyGenerateAlgorithm keyGenerateAlgorithm, final
String tableName,
+ final int
recordCount) throws SQLException {
log.info("init data begin: {}", LocalDateTime.now());
try (PreparedStatement preparedStatement =
connection.prepareStatement(String.format("INSERT INTO %s
(order_id,user_id,status) VALUES (?,?,?)", tableName))) {
for (int i = 0; i < recordCount; i++) {