Skip to content

Commit

Permalink
Refactor RQLBackendHandler (#29732)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jan 15, 2024
1 parent cab77f7 commit e0f6aea
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -58,8 +55,8 @@ private Collection<LocalDataQueryResultRow> buildColumnData(final MaskTableRuleC
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -48,16 +46,10 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ public Collection<String> getColumnNames() {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

/**
* Show default single table storage unit executor.
Expand All @@ -39,10 +38,8 @@ public Collection<String> getColumnNames() {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit e0f6aea

Please sign in to comment.