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 70c02a33286 Refactor `RulesUsedStorageUnitResultSet` (#23766)
70c02a33286 is described below
commit 70c02a332868c971e34a326995bb8f9a100d5f35
Author: Zichao <[email protected]>
AuthorDate: Sat Jan 28 20:09:11 2023 +1300
Refactor `RulesUsedStorageUnitResultSet` (#23766)
* Replace `RulesUsedStorageUnitResultSet` by
`ShowRulesUsedStorageUnitExecutor`
* Replace `RulesUsedStorageUnitResultSet` by
`ShowRulesUsedStorageUnitExecutor`
---
....java => ShowRulesUsedStorageUnitExecutor.java} | 71 +++++++------
...hardingsphere.distsql.handler.query.RQLExecutor | 1 +
...here.distsql.handler.resultset.DistSQLResultSet | 1 -
...a => ShowRulesUsedStorageUnitExecutorTest.java} | 115 ++++++++++-----------
4 files changed, 93 insertions(+), 95 deletions(-)
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/RulesUsedStorageUnitResultSet.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java
similarity index 70%
rename from
proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/RulesUsedStorageUnitResultSet.java
rename to
proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java
index 6a2d1f62046..9cbdf63bfe4 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/RulesUsedStorageUnitResultSet.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java
@@ -19,11 +19,14 @@ package
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule;
import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRulesUsedStorageUnitStatement;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
-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.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.rule.MaskRule;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
@@ -33,21 +36,18 @@ import
org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
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;
import java.util.Collections;
-import java.util.Iterator;
import java.util.LinkedList;
-import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
- * Result set for show rules used storage unit.
+ * Show rules used storage unit executor.
*/
-public final class RulesUsedStorageUnitResultSet implements
DatabaseDistSQLResultSet {
+public final class ShowRulesUsedStorageUnitExecutor implements
RQLExecutor<ShowRulesUsedStorageUnitStatement> {
private static final String SHARDING = "sharding";
@@ -59,29 +59,29 @@ public final class RulesUsedStorageUnitResultSet implements
DatabaseDistSQLResul
private static final String SHADOW = "shadow";
- private Iterator<Collection<Object>> data;
+ private static final String MASK = "mask";
@Override
- public void init(final ShardingSphereDatabase database, final SQLStatement
sqlStatement) {
- List<Collection<Object>> data = new LinkedList<>();
- ShowRulesUsedStorageUnitStatement statement =
(ShowRulesUsedStorageUnitStatement) sqlStatement;
- String resourceName = statement.getStorageUnitName().orElse(null);
+ public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowRulesUsedStorageUnitStatement
sqlStatement) {
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ String resourceName = sqlStatement.getStorageUnitName().orElse(null);
if
(database.getResourceMetaData().getDataSources().containsKey(resourceName)) {
- data.addAll(getShardingData(database));
- data.addAll(getReadwriteSplittingData(database, resourceName));
- data.addAll(getDatabaseDiscoveryData(database, resourceName));
- data.addAll(getEncryptData(database));
- data.addAll(getShadowData(database, resourceName));
+ result.addAll(getShardingData(database));
+ result.addAll(getReadwriteSplittingData(database, resourceName));
+ result.addAll(getDatabaseDiscoveryData(database, resourceName));
+ result.addAll(getEncryptData(database));
+ result.addAll(getShadowData(database, resourceName));
+ result.addAll(getMaskData(database));
}
- this.data = data.iterator();
+ return result;
}
- private Collection<Collection<Object>> getShardingData(final
ShardingSphereDatabase database) {
+ private Collection<LocalDataQueryResultRow> getShardingData(final
ShardingSphereDatabase database) {
Optional<ShardingRule> rule =
database.getRuleMetaData().findSingleRule(ShardingRule.class);
if (!rule.isPresent()) {
return Collections.emptyList();
}
- Collection<Collection<Object>> result = new LinkedList<>();
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
ShardingRuleConfiguration config = (ShardingRuleConfiguration)
rule.get().getConfiguration();
for (ShardingAutoTableRuleConfiguration each : config.getAutoTables())
{
result.add(buildRow(SHARDING, each.getLogicTable()));
@@ -92,12 +92,12 @@ public final class RulesUsedStorageUnitResultSet implements
DatabaseDistSQLResul
return result;
}
- private Collection<Collection<Object>> getReadwriteSplittingData(final
ShardingSphereDatabase database, final String resourceName) {
+ private Collection<LocalDataQueryResultRow>
getReadwriteSplittingData(final ShardingSphereDatabase database, final String
resourceName) {
Optional<ReadwriteSplittingRule> rule =
database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class);
if (!rule.isPresent()) {
return Collections.emptyList();
}
- Collection<Collection<Object>> result = new LinkedList<>();
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
ReadwriteSplittingRuleConfiguration config =
(ReadwriteSplittingRuleConfiguration) rule.get().getConfiguration();
for (ReadwriteSplittingDataSourceRuleConfiguration each :
config.getDataSources()) {
if (null != each.getStaticStrategy()) {
@@ -112,7 +112,7 @@ public final class RulesUsedStorageUnitResultSet implements
DatabaseDistSQLResul
return result;
}
- private Collection<Collection<Object>> getDatabaseDiscoveryData(final
ShardingSphereDatabase database, final String resourceName) {
+ private Collection<LocalDataQueryResultRow> getDatabaseDiscoveryData(final
ShardingSphereDatabase database, final String resourceName) {
Optional<DatabaseDiscoveryRule> rule =
database.getRuleMetaData().findSingleRule(DatabaseDiscoveryRule.class);
if (!rule.isPresent()) {
return Collections.emptyList();
@@ -121,7 +121,7 @@ public final class RulesUsedStorageUnitResultSet implements
DatabaseDistSQLResul
return config.getDataSources().stream().filter(each ->
each.getDataSourceNames().contains(resourceName)).map(each ->
buildRow(DB_DISCOVERY, each.getGroupName())).collect(Collectors.toList());
}
- private Collection<Collection<Object>> getEncryptData(final
ShardingSphereDatabase database) {
+ private Collection<LocalDataQueryResultRow> getEncryptData(final
ShardingSphereDatabase database) {
Optional<EncryptRule> rule =
database.getRuleMetaData().findSingleRule(EncryptRule.class);
if (!rule.isPresent()) {
return Collections.emptyList();
@@ -130,7 +130,7 @@ public final class RulesUsedStorageUnitResultSet implements
DatabaseDistSQLResul
return config.getTables().stream().map(each -> buildRow(ENCRYPT,
each.getName())).collect(Collectors.toList());
}
- private Collection<Collection<Object>> getShadowData(final
ShardingSphereDatabase database, final String resourceName) {
+ private Collection<LocalDataQueryResultRow> getShadowData(final
ShardingSphereDatabase database, final String resourceName) {
Optional<ShadowRule> rule =
database.getRuleMetaData().findSingleRule(ShadowRule.class);
if (!rule.isPresent()) {
return Collections.emptyList();
@@ -141,23 +141,22 @@ public final class RulesUsedStorageUnitResultSet
implements DatabaseDistSQLResul
.map(each -> buildRow(SHADOW,
each.getName())).collect(Collectors.toList());
}
- private Collection<Object> buildRow(final String type, final String name) {
- return Arrays.asList(type, name);
- }
-
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("type", "name");
+ private Collection<LocalDataQueryResultRow> getMaskData(final
ShardingSphereDatabase database) {
+ Optional<MaskRule> rule =
database.getRuleMetaData().findSingleRule(MaskRule.class);
+ if (!rule.isPresent()) {
+ return Collections.emptyList();
+ }
+ MaskRuleConfiguration config = (MaskRuleConfiguration)
rule.get().getConfiguration();
+ return config.getTables().stream().map(each -> buildRow(MASK,
each.getName())).collect(Collectors.toList());
}
- @Override
- public boolean next() {
- return data.hasNext();
+ private LocalDataQueryResultRow buildRow(final String type, final String
name) {
+ return new LocalDataQueryResultRow(type, name);
}
@Override
- public Collection<Object> getRowData() {
- return data.next();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("type", "name");
}
@Override
diff --git
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index 802f374e65f..ca72cbbef45 100644
---
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
+++
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -17,4 +17,5 @@
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.storage.unit.ShowStorageUnitExecutor
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowSingleTableExecutor
+org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowRulesUsedStorageUnitExecutor
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowDefaultSingleTableStorageUnitExecutor
diff --git
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index 62b2edaa2ee..ad990a02dff 100644
---
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -16,6 +16,5 @@
#
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.LogicalTableResultSet
-org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.RulesUsedStorageUnitResultSet
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.CountSingleTableResultSet
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowMigrationRuleResultSet
diff --git
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RulesUsedStorageUnitResultSetTest.java
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/ShowRulesUsedStorageUnitExecutorTest.java
similarity index 70%
rename from
proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RulesUsedStorageUnitResultSetTest.java
rename to
proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/ShowRulesUsedStorageUnitExecutorTest.java
index fcc77a6a2dc..f14cb179cb3 100644
---
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RulesUsedStorageUnitResultSetTest.java
+++
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/ShowRulesUsedStorageUnitExecutorTest.java
@@ -20,16 +20,19 @@ package
org.apache.shardingsphere.proxy.backend.handler.distsql.rql;
import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRulesUsedStorageUnitStatement;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
-import
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
-import org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet;
+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.resource.ShardingSphereResourceMetaData;
import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.RulesUsedStorageUnitResultSet;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import
org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.rule.MaskRule;
+import
org.apache.shardingsphere.proxy.backend.handler.distsql.rql.rule.ShowRulesUsedStorageUnitExecutor;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.strategy.StaticReadwriteSplittingStrategyConfiguration;
@@ -51,32 +54,53 @@ import java.util.Iterator;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class RulesUsedStorageUnitResultSetTest {
+public final class ShowRulesUsedStorageUnitExecutorTest {
@Test
public void assertGetRowData() {
- DatabaseDistSQLResultSet resultSet = new
RulesUsedStorageUnitResultSet();
+ RQLExecutor<ShowRulesUsedStorageUnitStatement> executor = new
ShowRulesUsedStorageUnitExecutor();
ShowRulesUsedStorageUnitStatement sqlStatement =
mock(ShowRulesUsedStorageUnitStatement.class);
when(sqlStatement.getStorageUnitName()).thenReturn(Optional.of("foo_ds"));
- resultSet.init(mockDatabase(), sqlStatement);
- assertShardingTableData(resultSet);
- assertReadwriteSplittingData(resultSet);
- assertDatabaseDiscoveryData(resultSet);
- assertEncryptData(resultSet);
- assertShadowData(resultSet);
- assertFalse(resultSet.next());
+ Collection<LocalDataQueryResultRow> rowData =
executor.getRows(mockDatabase(), sqlStatement);
+ assertThat(rowData.size(), is(8));
+ Iterator<LocalDataQueryResultRow> actual = rowData.iterator();
+ LocalDataQueryResultRow row = actual.next();
+ assertThat(row.getCell(1), is("sharding"));
+ assertThat(row.getCell(2), is("sharding_auto_table"));
+ row = actual.next();
+ assertThat(row.getCell(1), is("sharding"));
+ assertThat(row.getCell(2), is("sharding_table"));
+ row = actual.next();
+ assertThat(row.getCell(1), is("readwrite_splitting"));
+ assertThat(row.getCell(2), is("readwrite_splitting_source"));
+ row = actual.next();
+ assertThat(row.getCell(1), is("readwrite_splitting"));
+ assertThat(row.getCell(2), is("readwrite_splitting_source"));
+ row = actual.next();
+ assertThat(row.getCell(1), is("db_discovery"));
+ assertThat(row.getCell(2), is("db_discovery_group_name"));
+ row = actual.next();
+ assertThat(row.getCell(1), is("encrypt"));
+ assertThat(row.getCell(2), is("encrypt_table"));
+ row = actual.next();
+ assertThat(row.getCell(1), is("shadow"));
+ assertThat(row.getCell(2), is("shadow_source"));
+ row = actual.next();
+ assertThat(row.getCell(1), is("mask"));
+ assertThat(row.getCell(2), is("mask_table"));
+ assertFalse(actual.hasNext());
}
private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(
- Arrays.asList(mockShardingRule(),
mockReadwriteSplittingRule(), mockDatabaseDiscoveryRule(), mockEncryptRule(),
mockShadowRule()));
+ Arrays.asList(mockShardingRule(),
mockReadwriteSplittingRule(), mockDatabaseDiscoveryRule(), mockEncryptRule(),
mockShadowRule(), mockMaskRule()));
when(result.getRuleMetaData()).thenReturn(ruleMetaData);
ShardingSphereResourceMetaData resourceMetaData = new
ShardingSphereResourceMetaData("sharding_db",
Collections.singletonMap("foo_ds", new MockedDataSource()));
when(result.getResourceMetaData()).thenReturn(resourceMetaData);
@@ -125,57 +149,22 @@ public final class RulesUsedStorageUnitResultSetTest {
return result;
}
- private void assertShardingTableData(final DistSQLResultSet resultSet) {
- Iterator<Object> actual = getActualRowData(resultSet);
- assertThat(actual.next(), is("sharding"));
- assertThat(actual.next(), is("sharding_auto_table"));
- actual = getActualRowData(resultSet);
- assertThat(actual.next(), is("sharding"));
- assertThat(actual.next(), is("sharding_table"));
- }
-
- private void assertReadwriteSplittingData(final DistSQLResultSet
resultSet) {
- Iterator<Object> actual = getActualRowData(resultSet);
- assertThat(actual.next(), is("readwrite_splitting"));
- assertThat(actual.next(), is("readwrite_splitting_source"));
- actual = getActualRowData(resultSet);
- assertThat(actual.next(), is("readwrite_splitting"));
- assertThat(actual.next(), is("readwrite_splitting_source"));
- }
-
- private void assertDatabaseDiscoveryData(final DistSQLResultSet resultSet)
{
- Iterator<Object> actual = getActualRowData(resultSet);
- assertThat(actual.next(), is("db_discovery"));
- assertThat(actual.next(), is("db_discovery_group_name"));
- }
-
- private void assertEncryptData(final DistSQLResultSet resultSet) {
- Iterator<Object> actual = getActualRowData(resultSet);
- assertThat(actual.next(), is("encrypt"));
- assertThat(actual.next(), is("encrypt_table"));
- }
-
- private void assertShadowData(final DistSQLResultSet resultSet) {
- Iterator<Object> actual = getActualRowData(resultSet);
- assertThat(actual.next(), is("shadow"));
- assertThat(actual.next(), is("shadow_source"));
- }
-
- private Iterator<Object> getActualRowData(final DistSQLResultSet
resultSet) {
- assertTrue(resultSet.next());
- Collection<Object> actual = resultSet.getRowData();
- assertThat(actual.size(), is(2));
- return actual.iterator();
+ private MaskRule mockMaskRule() {
+ MaskRule result = mock(MaskRule.class);
+ MaskRuleConfiguration config = mock(MaskRuleConfiguration.class);
+ when(config.getTables()).thenReturn(Collections.singleton(new
MaskTableRuleConfiguration("mask_table", Collections.emptyList())));
+ when(result.getConfiguration()).thenReturn(config);
+ return result;
}
@Test
public void assertGetEmptyRowData() {
ShardingSphereDatabase database = mockEmptyDatabase();
- DatabaseDistSQLResultSet resultSet = new
RulesUsedStorageUnitResultSet();
+ RQLExecutor<ShowRulesUsedStorageUnitStatement> executor = new
ShowRulesUsedStorageUnitExecutor();
ShowRulesUsedStorageUnitStatement sqlStatement =
mock(ShowRulesUsedStorageUnitStatement.class);
when(sqlStatement.getStorageUnitName()).thenReturn(Optional.of("empty_ds"));
- resultSet.init(database, sqlStatement);
- assertFalse(resultSet.next());
+ Collection<LocalDataQueryResultRow> rowData =
executor.getRows(database, sqlStatement);
+ assertTrue(rowData.isEmpty());
}
private ShardingSphereDatabase mockEmptyDatabase() {
@@ -185,4 +174,14 @@ public final class RulesUsedStorageUnitResultSetTest {
when(result.getResourceMetaData()).thenReturn(resourceMetaData);
return result;
}
+
+ @Test
+ public void assertGetColumnNames() {
+ RQLExecutor<ShowRulesUsedStorageUnitStatement> executor = new
ShowRulesUsedStorageUnitExecutor();
+ Collection<String> columns = executor.getColumnNames();
+ assertThat(columns.size(), is(2));
+ Iterator<String> iterator = columns.iterator();
+ assertThat(iterator.next(), is("type"));
+ assertThat(iterator.next(), is("name"));
+ }
}