You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/06/23 07:00:00 UTC
[shardingsphere] branch master updated: Refactor ShadowAlgorithmQueryResultSet (#18534)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 6f12a807731 Refactor ShadowAlgorithmQueryResultSet (#18534)
6f12a807731 is described below
commit 6f12a80773132f1526d4784502fe4b8b829f7c14
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Jun 23 14:59:54 2022 +0800
Refactor ShadowAlgorithmQueryResultSet (#18534)
* Refactor ShadowAlgorithmQueryResultSet
* Refactor ShadowAlgorithmQueryResultSet
---
.../shardingsphere-shadow-distsql-handler/pom.xml | 2 +-
.../query/ShadowAlgorithmQueryResultSet.java | 25 ++++++++--------------
.../query/ShadowAlgorithmQueryResultSetTest.java | 15 ++++++++++---
3 files changed, 22 insertions(+), 20 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/pom.xml b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/pom.xml
index 1d59cd08bca..0a38653443f 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/pom.xml
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/pom.xml
@@ -30,7 +30,7 @@
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-shadow-api</artifactId>
+ <artifactId>shardingsphere-shadow-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShadowAlgorithmQueryResultSet.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShadowAlgorithmQueryResultSet.java
index 0b5bbdf2fd8..2eaa973376a 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShadowAlgorithmQueryResultSet.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShadowAlgorithmQueryResultSet.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.properties.PropertiesConverter;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.distsql.parser.statement.ShowShadowAlgorithmsStatement;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Arrays;
@@ -35,35 +36,27 @@ import java.util.Optional;
import java.util.Properties;
/**
- * Result set for shadow algorithm.
+ * Query result set for shadow algorithm.
*/
public final class ShadowAlgorithmQueryResultSet implements DistSQLResultSet {
- private static final String SHADOW_ALGORITHM_NAME = "shadow_algorithm_name";
-
- private static final String TYPE = "type";
-
- private static final String PROPS = "props";
-
- private static final String DEFAULT = "is_default";
-
private Iterator<Entry<String, ShardingSphereAlgorithmConfiguration>> data = Collections.emptyIterator();
private String defaultAlgorithm;
@Override
public void init(final ShardingSphereDatabase database, final SQLStatement sqlStatement) {
- Optional<ShadowRuleConfiguration> config = database.getRuleMetaData().getConfigurations()
- .stream().filter(each -> each instanceof ShadowRuleConfiguration).map(each -> (ShadowRuleConfiguration) each).findAny();
- config.ifPresent(optional -> {
- data = optional.getShadowAlgorithms().entrySet().iterator();
- defaultAlgorithm = optional.getDefaultShadowAlgorithmName();
- });
+ Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
+ if (rule.isPresent()) {
+ ShadowRuleConfiguration config = (ShadowRuleConfiguration) rule.get().getConfiguration();
+ data = config.getShadowAlgorithms().entrySet().iterator();
+ defaultAlgorithm = config.getDefaultShadowAlgorithmName();
+ }
}
@Override
public Collection<String> getColumnNames() {
- return Arrays.asList(SHADOW_ALGORITHM_NAME, TYPE, PROPS, DEFAULT);
+ return Arrays.asList("shadow_algorithm_name", "type", "props", "is_default");
}
@Override
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowAlgorithmQueryResultSetTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowAlgorithmQueryResultSetTest.java
index 9d7f1931e97..97d2895f6d4 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowAlgorithmQueryResultSetTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowAlgorithmQueryResultSetTest.java
@@ -25,11 +25,13 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.shadow.distsql.handler.query.ShadowAlgorithmQueryResultSet;
import org.apache.shardingsphere.shadow.distsql.parser.statement.ShowShadowAlgorithmsStatement;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
@@ -42,15 +44,22 @@ public final class ShadowAlgorithmQueryResultSetTest {
@Test
public void assertGetRowData() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(createRuleConfiguration()));
DistSQLResultSet resultSet = new ShadowAlgorithmQueryResultSet();
- resultSet.init(database, mock(ShowShadowAlgorithmsStatement.class));
+ resultSet.init(mockDatabase(), mock(ShowShadowAlgorithmsStatement.class));
List<Object> actual = new ArrayList<>(resultSet.getRowData());
assertThat(actual.size(), is(4));
assertThat(actual.get(0), is("shadowAlgorithmName"));
assertThat(actual.get(1), is("simple_hint"));
assertThat(actual.get(2), is("foo=bar"));
+ assertThat(actual.get(3), is("false"));
+ }
+
+ private ShardingSphereDatabase mockDatabase() {
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ ShadowRule rule = mock(ShadowRule.class);
+ when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
+ when(result.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
+ return result;
}
private RuleConfiguration createRuleConfiguration() {