From e0f6aea7366a61baf310740e9397ae02b3d41280 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 15 Jan 2024 22:25:31 +0800 Subject: [PATCH] Refactor RQLBackendHandler (#29732) --- .../handler/query/ShowEncryptRuleExecutor.java | 6 +++--- .../handler/query/ShowMaskRuleExecutor.java | 11 ++++------- .../query/ShowReadwriteSplittingRuleExecutor.java | 5 ++--- .../handler/query/ShowShadowRuleExecutor.java | 6 +----- .../query/ShowShadowTableRulesExecutor.java | 5 +---- .../query/ShowShardingTableNodesExecutor.java | 14 +++----------- .../handler/type/rql/count/CountRQLExecutor.java | 9 ++++++--- .../ShowDefaultSingleTableStorageUnitExecutor.java | 5 +---- .../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 c44a60b7bf1cd..7cd63d1ca8007 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 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 getRows(final ShardingSphereDatabase database, final ShowEncryptRulesStatement sqlStatement) { Optional rule = database.getRuleMetaData().findSingleRule(EncryptRule.class); - Collection 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 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 93db68aae8d35..7f791b0628efc 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 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 getRows(final ShardingSphereDatabase database, final ShowMaskRulesStatement sqlStatement) { Optional rule = database.getRuleMetaData().findSingleRule(MaskRule.class); - Collection 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 buildData(final MaskRuleConfiguration ruleConfig, final ShowMaskRulesStatement sqlStatement) { @@ -58,8 +55,8 @@ private Collection buildColumnData(final MaskTableRuleC Collection 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 e70799c3fe262..c6c68fde5f3dc 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 getRows(final ShardingSphereDatabase database, final ShowReadwriteSplittingRulesStatement sqlStatement) { Optional rule = database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class); - Collection 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 1cded1108e019..234cf5f61cc4c 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 getRows(final ShardingSphereDatabase database, final ShowShadowRulesStatement sqlStatement) { Optional rule = database.getRuleMetaData().findSingleRule(ShadowRule.class); - Collection 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 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 81a2de728a421..ad53c1f6b587e 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 getRows(final ShardingSphereDatabase database, final ShowShadowTableRulesStatement sqlStatement) { Optional rule = database.getRuleMetaData().findSingleRule(ShadowRule.class); - Iterator> data = Collections.emptyIterator(); - if (rule.isPresent()) { - data = buildData((ShadowRuleConfiguration) rule.get().getConfiguration(), sqlStatement).iterator(); - } + Iterator> data = rule.map(optional -> buildData((ShadowRuleConfiguration) optional.getConfiguration(), sqlStatement).iterator()).orElse(Collections.emptyIterator()); Collection result = new LinkedList<>(); while (data.hasNext()) { Map 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 f7dabec322dd1..aebdb584f1b12 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 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 Collection getRows(final ShardingSphereDatabase if (!shardingRule.isPresent()) { return Collections.emptyList(); } - Collection result = new LinkedList<>(); String tableName = sqlStatement.getTableName(); - if (null == tableName) { - for (Entry 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 2f53c58c9fa80..52663fc3ce285 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 Collection getColumnNames() { @SuppressWarnings({"unchecked", "rawtypes"}) @Override public Collection getRows(final ShardingSphereDatabase database, final CountRuleStatement sqlStatement) { - CountResultRowBuilder rowBuilder = TypedSPILoader.getService(CountResultRowBuilder.class, sqlStatement.getType()); - Optional rule = database.getRuleMetaData().findSingleRule(rowBuilder.getRuleClass()); - return rule.isPresent() ? rowBuilder.generateRows(rule.get(), database.getName()) : Collections.emptyList(); + Optional rowBuilder = TypedSPILoader.findService(CountResultRowBuilder.class, sqlStatement.getType()); + if (!rowBuilder.isPresent()) { + return Collections.emptyList(); + } + Optional 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 3bae7ab3a8290..e2a7c57b22482 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 java.util.Collection; import java.util.Collections; -import java.util.LinkedList; /** * Show default single table storage unit executor. @@ -39,10 +38,8 @@ public Collection getColumnNames() { @Override public Collection getRows(final ShardingSphereDatabase database, final ShowDefaultSingleTableStorageUnitStatement sqlStatement) { - Collection 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 573884f9eb193..ba0bd2e0e3173 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 @@ 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 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 = 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 createQueryHeader(final Collection columnNames) { return columnNames.stream().map(each -> new QueryHeader("", "", each, each, Types.CHAR, "CHAR", 255, 0, false, false, false, false)).collect(Collectors.toList()); }