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 e0f6aea7366 Refactor RQLBackendHandler (#29732)
e0f6aea7366 is described below
commit e0f6aea7366a61baf310740e9397ae02b3d41280
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jan 15 22:25:31 2024 +0800
Refactor RQLBackendHandler (#29732)
---
.../distsql/handler/query/ShowEncryptRuleExecutor.java | 6 +++---
.../mask/distsql/handler/query/ShowMaskRuleExecutor.java | 11 ++++-------
.../handler/query/ShowReadwriteSplittingRuleExecutor.java | 5 ++---
.../distsql/handler/query/ShowShadowRuleExecutor.java | 6 +-----
.../handler/query/ShowShadowTableRulesExecutor.java | 5 +----
.../handler/query/ShowShardingTableNodesExecutor.java | 14 +++-----------
.../distsql/handler/type/rql/count/CountRQLExecutor.java | 9 ++++++---
.../query/ShowDefaultSingleTableStorageUnitExecutor.java | 5 +----
.../backend/handler/distsql/rql/RQLBackendHandler.java | 14 ++------------
9 files changed, 23 insertions(+), 52 deletions(-)
diff --git
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
index c44a60b7bf1..7cd63d1ca80 100644
---
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
+++
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
@@ -32,6 +32,7 @@ import
org.apache.shardingsphere.infra.props.PropertiesConverter;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
@@ -45,14 +46,13 @@ public final class ShowEncryptRuleExecutor implements
RQLExecutor<ShowEncryptRul
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowEncryptRulesStatement sqlStatement) {
Optional<EncryptRule> rule =
database.getRuleMetaData().findSingleRule(EncryptRule.class);
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
if (rule.isPresent()) {
EncryptRuleConfiguration ruleConfig =
rule.get().getConfiguration() instanceof CompatibleEncryptRuleConfiguration
? ((CompatibleEncryptRuleConfiguration)
rule.get().getConfiguration()).convertToEncryptRuleConfiguration()
: (EncryptRuleConfiguration) rule.get().getConfiguration();
- result = buildData(ruleConfig, sqlStatement);
+ return buildData(ruleConfig, sqlStatement);
}
- return result;
+ return Collections.emptyList();
}
private Collection<LocalDataQueryResultRow> buildData(final
EncryptRuleConfiguration ruleConfig, final ShowEncryptRulesStatement
sqlStatement) {
diff --git
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java
index 93db68aae8d..7f791b0628e 100644
---
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java
+++
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.mask.rule.MaskRule;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
@@ -42,11 +43,7 @@ public final class ShowMaskRuleExecutor implements
RQLExecutor<ShowMaskRulesStat
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowMaskRulesStatement sqlStatement) {
Optional<MaskRule> rule =
database.getRuleMetaData().findSingleRule(MaskRule.class);
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- if (rule.isPresent()) {
- result = buildData((MaskRuleConfiguration)
rule.get().getConfiguration(), sqlStatement);
- }
- return result;
+ return rule.isPresent() ? buildData((MaskRuleConfiguration)
rule.get().getConfiguration(), sqlStatement) : Collections.emptyList();
}
private Collection<LocalDataQueryResultRow> buildData(final
MaskRuleConfiguration ruleConfig, final ShowMaskRulesStatement sqlStatement) {
@@ -58,8 +55,8 @@ public final class ShowMaskRuleExecutor implements
RQLExecutor<ShowMaskRulesStat
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
tableRuleConfig.getColumns().forEach(each -> {
AlgorithmConfiguration maskAlgorithmConfig =
algorithmMap.get(each.getMaskAlgorithm());
- result.add(new
LocalDataQueryResultRow(Arrays.asList(tableRuleConfig.getName(),
each.getLogicColumn(),
- maskAlgorithmConfig.getType(),
PropertiesConverter.convert(maskAlgorithmConfig.getProps()))));
+ result.add(new LocalDataQueryResultRow(
+ Arrays.asList(tableRuleConfig.getName(),
each.getLogicColumn(), maskAlgorithmConfig.getType(),
PropertiesConverter.convert(maskAlgorithmConfig.getProps()))));
});
return result;
}
diff --git
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java
index e70799c3fe2..c6c68fde5f3 100644
---
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java
+++
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java
@@ -47,12 +47,11 @@ public final class ShowReadwriteSplittingRuleExecutor
implements RQLExecutor<Sho
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowReadwriteSplittingRulesStatement
sqlStatement) {
Optional<ReadwriteSplittingRule> rule =
database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class);
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
if (rule.isPresent()) {
buildExportableMap(rule.get());
- result = buildData(rule.get(), sqlStatement);
+ return buildData(rule.get(), sqlStatement);
}
- return result;
+ return Collections.emptyList();
}
@SuppressWarnings("unchecked")
diff --git
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
index 1cded1108e0..234cf5f61cc 100644
---
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
+++
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
@@ -45,11 +45,7 @@ public final class ShowShadowRuleExecutor implements
RQLExecutor<ShowShadowRules
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowShadowRulesStatement sqlStatement) {
Optional<ShadowRule> rule =
database.getRuleMetaData().findSingleRule(ShadowRule.class);
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- if (rule.isPresent()) {
- result = buildData((ShadowRuleConfiguration)
rule.get().getConfiguration(), sqlStatement);
- }
- return result;
+ return rule.isPresent() ? buildData((ShadowRuleConfiguration)
rule.get().getConfiguration(), sqlStatement) : Collections.emptyList();
}
private Collection<LocalDataQueryResultRow> buildData(final
ShadowRuleConfiguration ruleConfig, final ShowShadowRulesStatement
sqlStatement) {
diff --git
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
index 81a2de728a4..ad53c1f6b58 100644
---
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
+++
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
@@ -47,10 +47,7 @@ public final class ShowShadowTableRulesExecutor implements
RQLExecutor<ShowShado
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowShadowTableRulesStatement
sqlStatement) {
Optional<ShadowRule> rule =
database.getRuleMetaData().findSingleRule(ShadowRule.class);
- Iterator<Map<String, String>> data = Collections.emptyIterator();
- if (rule.isPresent()) {
- data = buildData((ShadowRuleConfiguration)
rule.get().getConfiguration(), sqlStatement).iterator();
- }
+ Iterator<Map<String, String>> data = rule.map(optional ->
buildData((ShadowRuleConfiguration) optional.getConfiguration(),
sqlStatement).iterator()).orElse(Collections.emptyIterator());
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
while (data.hasNext()) {
Map<String, String> row = data.next();
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java
index f7dabec322d..aebdb584f1b 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java
@@ -28,8 +28,6 @@ import org.apache.shardingsphere.sharding.rule.TableRule;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -48,16 +46,10 @@ public final class ShowShardingTableNodesExecutor
implements RQLExecutor<ShowSha
if (!shardingRule.isPresent()) {
return Collections.emptyList();
}
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
String tableName = sqlStatement.getTableName();
- if (null == tableName) {
- for (Entry<String, TableRule> entry :
shardingRule.get().getTableRules().entrySet()) {
- result.add(new LocalDataQueryResultRow(entry.getKey(),
getTableNodes(entry.getValue())));
- }
- } else {
- result.add(new LocalDataQueryResultRow(tableName,
getTableNodes(shardingRule.get().getTableRule(tableName))));
- }
- return result;
+ return null == tableName
+ ?
shardingRule.get().getTableRules().entrySet().stream().map(entry -> new
LocalDataQueryResultRow(entry.getKey(),
getTableNodes(entry.getValue()))).collect(Collectors.toList())
+ : Collections.singleton(new LocalDataQueryResultRow(tableName,
getTableNodes(shardingRule.get().getTableRule(tableName))));
}
private String getTableNodes(final TableRule tableRule) {
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rql/count/CountRQLExecutor.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rql/count/CountRQLExecutor.java
index 2f53c58c9fa..52663fc3ce2 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rql/count/CountRQLExecutor.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rql/count/CountRQLExecutor.java
@@ -42,9 +42,12 @@ public final class CountRQLExecutor implements
RQLExecutor<CountRuleStatement> {
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final CountRuleStatement sqlStatement) {
- CountResultRowBuilder rowBuilder =
TypedSPILoader.getService(CountResultRowBuilder.class, sqlStatement.getType());
- Optional<ShardingSphereRule> rule =
database.getRuleMetaData().findSingleRule(rowBuilder.getRuleClass());
- return rule.isPresent() ? rowBuilder.generateRows(rule.get(),
database.getName()) : Collections.emptyList();
+ Optional<CountResultRowBuilder> rowBuilder =
TypedSPILoader.findService(CountResultRowBuilder.class, sqlStatement.getType());
+ if (!rowBuilder.isPresent()) {
+ return Collections.emptyList();
+ }
+ Optional<ShardingSphereRule> rule =
database.getRuleMetaData().findSingleRule(rowBuilder.get().getRuleClass());
+ return rule.isPresent() ? rowBuilder.get().generateRows(rule.get(),
database.getName()) : Collections.emptyList();
}
@Override
diff --git
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
index 3bae7ab3a82..e2a7c57b224 100644
---
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
+++
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.single.rule.SingleRule;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
/**
* Show default single table storage unit executor.
@@ -39,10 +38,8 @@ public final class ShowDefaultSingleTableStorageUnitExecutor
implements RQLExecu
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final
ShowDefaultSingleTableStorageUnitStatement sqlStatement) {
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
SingleRule rule =
database.getRuleMetaData().getSingleRule(SingleRule.class);
- result.add(new
LocalDataQueryResultRow(rule.getConfiguration().getDefaultDataSource().orElse("RANDOM")));
- return result;
+ return Collections.singleton(new
LocalDataQueryResultRow(rule.getConfiguration().getDefaultDataSource().orElse("RANDOM")));
}
@Override
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RQLBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RQLBackendHandler.java
index 573884f9eb1..ba0bd2e0e31 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RQLBackendHandler.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RQLBackendHandler.java
@@ -19,14 +19,11 @@ package
org.apache.shardingsphere.proxy.backend.handler.distsql.rql;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.NoDatabaseSelectedException;
-import
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.statement.rql.RQLStatement;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedResult;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
-import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler;
@@ -67,24 +64,17 @@ public final class RQLBackendHandler<T extends
RQLStatement> implements DistSQLB
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public ResponseHeader execute() throws SQLException {
- String databaseName = getDatabaseName(connectionSession, sqlStatement);
- checkDatabaseName(databaseName);
RQLExecutor executor = TypedSPILoader.getService(RQLExecutor.class,
sqlStatement.getClass());
queryHeaders = createQueryHeader(executor.getColumnNames());
- mergedResult =
createMergedResult(executor.getRows(ProxyContext.getInstance().getDatabase(databaseName),
sqlStatement));
+ mergedResult =
createMergedResult(executor.getRows(ProxyContext.getInstance().getDatabase(getDatabaseName()),
sqlStatement));
return new QueryResponseHeader(queryHeaders);
}
- private String getDatabaseName(final ConnectionSession connectionSession,
final T sqlStatement) {
+ private String getDatabaseName() {
Optional<DatabaseSegment> databaseSegment = sqlStatement instanceof
FromDatabaseAvailable ? ((FromDatabaseAvailable) sqlStatement).getDatabase() :
Optional.empty();
return databaseSegment.isPresent() ?
databaseSegment.get().getIdentifier().getValue() :
connectionSession.getDatabaseName();
}
- private void checkDatabaseName(final String databaseName) {
- ShardingSpherePreconditions.checkNotNull(databaseName,
NoDatabaseSelectedException::new);
-
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName),
() -> new UnknownDatabaseException(databaseName));
- }
-
private List<QueryHeader> createQueryHeader(final Collection<String>
columnNames) {
return columnNames.stream().map(each -> new QueryHeader("", "", each,
each, Types.CHAR, "CHAR", 255, 0, false, false, false,
false)).collect(Collectors.toList());
}