Skip to content

Commit

Permalink
Refactor usage of LocalDataQueryResultRow (apache#30113)
Browse files Browse the repository at this point in the history
* Refactor usage of LocalDataQueryResultRow

* Refactor usage of LocalDataQueryResultRow
  • Loading branch information
terrymanu authored Feb 13, 2024
1 parent f42a960 commit 7b3a525
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

/**
Expand All @@ -59,8 +61,8 @@ private Collection<LocalDataQueryResultRow> buildColumnData(final EncryptTableRu
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
for (EncryptColumnRuleConfiguration each : tableRuleConfig.getColumns()) {
AlgorithmConfiguration encryptorAlgorithmConfig = encryptors.get(each.getCipher().getEncryptorName());
AlgorithmConfiguration assistedQueryEncryptorAlgorithmConfig = each.getAssistedQuery().isPresent() ? encryptors.get(each.getAssistedQuery().get().getEncryptorName()) : null;
AlgorithmConfiguration likeQueryEncryptorAlgorithmConfig = each.getLikeQuery().isPresent() ? encryptors.get(each.getLikeQuery().get().getEncryptorName()) : null;
Optional<AlgorithmConfiguration> assistedQueryEncryptorAlgorithmConfig = each.getAssistedQuery().map(optional -> encryptors.get(optional.getEncryptorName()));
Optional<AlgorithmConfiguration> likeQueryEncryptorAlgorithmConfig = each.getLikeQuery().map(optional -> encryptors.get(optional.getEncryptorName()));
result.add(new LocalDataQueryResultRow(
tableRuleConfig.getName(),
each.getName(),
Expand All @@ -69,10 +71,10 @@ private Collection<LocalDataQueryResultRow> buildColumnData(final EncryptTableRu
each.getLikeQuery().map(EncryptColumnItemRuleConfiguration::getName).orElse(""),
encryptorAlgorithmConfig.getType(),
encryptorAlgorithmConfig.getProps(),
null == assistedQueryEncryptorAlgorithmConfig ? "" : assistedQueryEncryptorAlgorithmConfig.getType(),
null == assistedQueryEncryptorAlgorithmConfig ? "" : assistedQueryEncryptorAlgorithmConfig.getProps(),
null == likeQueryEncryptorAlgorithmConfig ? "" : likeQueryEncryptorAlgorithmConfig.getType(),
null == likeQueryEncryptorAlgorithmConfig ? "" : likeQueryEncryptorAlgorithmConfig.getProps()));
assistedQueryEncryptorAlgorithmConfig.map(AlgorithmConfiguration::getType).orElse(""),
assistedQueryEncryptorAlgorithmConfig.map(AlgorithmConfiguration::getProps).orElse(new Properties()),
likeQueryEncryptorAlgorithmConfig.map(AlgorithmConfiguration::getType).orElse(""),
likeQueryEncryptorAlgorithmConfig.map(AlgorithmConfiguration::getProps).orElse(new Properties())));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private LocalDataQueryResultRow buildDataItem(final String defaultType, final Sh
Iterator<String> iterator = strategyType.getConfigurationContents(strategyConfig).iterator();
String shardingColumn = iterator.next();
String algorithmName = iterator.next();
return new LocalDataQueryResultRow(defaultType, strategyType.toString(), shardingColumn, algorithmName, algorithmConfig.getType(), algorithmConfig.getProps());
return new LocalDataQueryResultRow(defaultType, strategyType, shardingColumn, algorithmName, algorithmConfig.getType(), algorithmConfig.getProps());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ private LocalDataQueryResultRow buildTableRowData(final ShardingRuleConfiguratio
Optional<ShardingStrategyConfiguration> databaseShardingStrategyConfig = getDatabaseShardingStrategy(ruleConfig, shardingTableRuleConfig);
Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(ruleConfig, shardingTableRuleConfig.getTableShardingStrategy());
return new LocalDataQueryResultRow(shardingTableRuleConfig.getLogicTable(), shardingTableRuleConfig.getActualDataNodes(), "",
databaseShardingStrategyConfig.map(this::getStrategyType).orElse(""), databaseShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
databaseShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)).orElse(""),
databaseShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)).orElse(new Properties()),
tableShardingStrategyConfig.map(this::getStrategyType).orElse(""), tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
tableShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)).orElse(""),
tableShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)).orElse(new Properties()),
databaseShardingStrategyConfig.map(this::getStrategyType), databaseShardingStrategyConfig.map(this::getShardingColumn),
databaseShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)),
databaseShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)),
tableShardingStrategyConfig.map(this::getStrategyType).orElse(""), tableShardingStrategyConfig.map(this::getShardingColumn),
tableShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)),
tableShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)),
getKeyGenerateColumn(ruleConfig, shardingTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(ruleConfig, shardingTableRuleConfig.getKeyGenerateStrategy()),
getKeyGeneratorProps(ruleConfig, shardingTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(ruleConfig, shardingTableRuleConfig.getAuditStrategy()),
getAllowHintDisable(ruleConfig, shardingTableRuleConfig.getAuditStrategy()));
Expand All @@ -97,9 +97,9 @@ private LocalDataQueryResultRow buildTableRowData(final ShardingRuleConfiguratio
private LocalDataQueryResultRow buildAutoTableRowData(final ShardingRuleConfiguration ruleConfig, final ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(ruleConfig, shardingAutoTableRuleConfig.getShardingStrategy());
return new LocalDataQueryResultRow(shardingAutoTableRuleConfig.getLogicTable(), "", shardingAutoTableRuleConfig.getActualDataSources(), "", "", "", "",
tableShardingStrategyConfig.map(this::getStrategyType).orElse(""), tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
tableShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)).orElse(""),
tableShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)).orElse(new Properties()),
tableShardingStrategyConfig.map(this::getStrategyType), tableShardingStrategyConfig.map(this::getShardingColumn),
tableShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)),
tableShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)),
getKeyGenerateColumn(ruleConfig, shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(ruleConfig, shardingAutoTableRuleConfig.getKeyGenerateStrategy()),
getKeyGeneratorProps(ruleConfig, shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(ruleConfig, shardingAutoTableRuleConfig.getAuditStrategy()),
getAllowHintDisable(ruleConfig, shardingAutoTableRuleConfig.getAuditStrategy()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.util.json.JsonUtils;
import org.apache.shardingsphere.mode.manager.ContextManager;

import javax.sql.DataSource;
Expand Down Expand Up @@ -76,7 +75,7 @@ public Collection<LocalDataQueryResultRow> getRows(final ShowStorageUnitsStateme
getStandardProperty(poolProps, "maxPoolSize"),
getStandardProperty(poolProps, "minPoolSize"),
getStandardProperty(poolProps, "readOnly"),
customProps.isEmpty() ? "" : JsonUtils.toJsonString(customProps)));
customProps));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.base.Preconditions;
import org.apache.shardingsphere.infra.util.json.JsonUtils;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -46,7 +47,10 @@ private Object convert(final Object data) {
if (data instanceof Optional) {
return ((Optional<?>) data).isPresent() ? convert(((Optional<?>) data).get()) : "";
}
if (data instanceof Boolean || data instanceof Integer || data instanceof Long) {
if (data instanceof String) {
return data;
}
if (data instanceof Boolean || data instanceof Integer || data instanceof Long || data instanceof LocalDateTime) {
return data.toString();
}
if (data instanceof Enum) {
Expand All @@ -55,7 +59,10 @@ private Object convert(final Object data) {
if (data instanceof Properties) {
return ((Properties) data).isEmpty() ? "" : JsonUtils.toJsonString(convert((Properties) data));
}
return data;
if (data instanceof Map) {
return ((Map<?, ?>) data).isEmpty() ? "" : JsonUtils.toJsonString(data);
}
return JsonUtils.toJsonString(data);
}

private Map<Object, Object> convert(final Properties props) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void assertGetCalendarValue() {
LocalDataQueryResultRow row = new LocalDataQueryResultRow(new Date(0L));
LocalDataMergedResult actual = new LocalDataMergedResult(Collections.singletonList(row));
assertTrue(actual.next());
assertThat(actual.getCalendarValue(1, Object.class, Calendar.getInstance()), is(new Date(0L)));
assertThat(actual.getCalendarValue(1, Object.class, Calendar.getInstance()), is("0"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand All @@ -47,7 +46,7 @@ public Collection<String> getColumnNames(final ShowStreamingListStatement sqlSta
public Collection<LocalDataQueryResultRow> getRows(final ShowStreamingListStatement sqlStatement, final ContextManager contextManager) {
return pipelineJobManager.getJobInfos(new PipelineContextKey(InstanceType.PROXY)).stream().map(each -> new LocalDataQueryResultRow(each.getJobMetaData().getJobId(),
each.getDatabaseName(), each.getTableName(), each.getJobMetaData().getJobItemCount(), each.getJobMetaData().isActive(),
each.getJobMetaData().getCreateTime(), Optional.ofNullable(each.getJobMetaData().getStopTime()).orElse(""))).collect(Collectors.toList());
each.getJobMetaData().getCreateTime(), each.getJobMetaData().getStopTime())).collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -53,13 +54,17 @@ private LocalDataQueryResultRow getRow(final TransmissionJobItemInfo jobItemInfo
if (null == jobItemProgress) {
return new LocalDataQueryResultRow(jobItemInfo.getShardingItem(), "", "", "", "", "", "", "", jobItemInfo.getErrorMessage());
}
String incrementalIdleSeconds = "";
return new LocalDataQueryResultRow(jobItemInfo.getShardingItem(), jobItemProgress.getDataSourceName(), jobItemInfo.getTableNames(), jobItemProgress.getStatus(), jobItemProgress.isActive(),
jobItemProgress.getProcessedRecordsCount(), jobItemInfo.getInventoryFinishedPercentage(), getIncrementalIdleSeconds(jobItemProgress, jobItemInfo, currentTimeMillis),
jobItemInfo.getErrorMessage());
}

private Optional<Long> getIncrementalIdleSeconds(final TransmissionJobItemProgress jobItemProgress, final TransmissionJobItemInfo jobItemInfo, final long currentTimeMillis) {
if (jobItemProgress.getIncremental().getIncrementalLatestActiveTimeMillis() > 0) {
long latestActiveTimeMillis = Math.max(jobItemInfo.getStartTimeMillis(), jobItemProgress.getIncremental().getIncrementalLatestActiveTimeMillis());
incrementalIdleSeconds = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - latestActiveTimeMillis));
return Optional.of(TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - latestActiveTimeMillis));
}
return new LocalDataQueryResultRow(jobItemInfo.getShardingItem(), jobItemProgress.getDataSourceName(), jobItemInfo.getTableNames(), jobItemProgress.getStatus(),
jobItemProgress.isActive(), jobItemProgress.getProcessedRecordsCount(), jobItemInfo.getInventoryFinishedPercentage(), incrementalIdleSeconds, jobItemInfo.getErrorMessage());
return Optional.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Collection<String> getColumnNames(final ShowSQLTranslatorRuleStatement sq
@Override
public Collection<LocalDataQueryResultRow> getRows(final ShowSQLTranslatorRuleStatement sqlStatement, final ContextManager contextManager) {
SQLTranslatorRuleConfiguration ruleConfig = rule.getConfiguration();
return Collections.singleton(new LocalDataQueryResultRow(ruleConfig.getType(), ruleConfig.getProps(), String.valueOf(ruleConfig.isUseOriginalSQLWhenTranslatingFailed())));
return Collections.singleton(new LocalDataQueryResultRow(ruleConfig.getType(), ruleConfig.getProps(), ruleConfig.isUseOriginalSQLWhenTranslatingFailed()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public Collection<LocalDataQueryResultRow> getRows(final ShowTrafficRulesStateme
}

private LocalDataQueryResultRow buildRow(final TrafficStrategyConfiguration strategy, final AlgorithmConfiguration trafficAlgorithm, final AlgorithmConfiguration loadBalancer) {
return new LocalDataQueryResultRow(strategy.getName(), String.join(",", strategy.getLabels()), null != trafficAlgorithm ? trafficAlgorithm.getType() : "",
null == trafficAlgorithm ? null : trafficAlgorithm.getProps(), null == loadBalancer ? "" : loadBalancer.getType(), null == loadBalancer ? "" : loadBalancer.getProps());
return new LocalDataQueryResultRow(strategy.getName(), String.join(",", strategy.getLabels()), null == trafficAlgorithm ? null : trafficAlgorithm.getType(),
null == trafficAlgorithm ? null : trafficAlgorithm.getProps(), null == loadBalancer ? null : loadBalancer.getType(), null == loadBalancer ? null : loadBalancer.getProps());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Collection<LocalDataQueryResultRow> getRows(final ShowComputeNodeInfoStat
String modeType = contextManager.getInstanceContext().getModeConfiguration().getType();
return Collections.singletonList(new LocalDataQueryResultRow(instanceMetaData.getId(), instanceMetaData.getIp(),
instanceMetaData instanceof ProxyInstanceMetaData ? ((ProxyInstanceMetaData) instanceMetaData).getPort() : -1,
instance.getState().getCurrentState().name(), modeType, instance.getWorkerId(), String.join(",", instance.getLabels()),
instance.getState().getCurrentState(), modeType, instance.getWorkerId(), String.join(",", instance.getLabels()),
instanceMetaData.getVersion()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public Collection<String> getColumnNames(final ShowComputeNodeModeStatement sqlS
public Collection<LocalDataQueryResultRow> getRows(final ShowComputeNodeModeStatement sqlStatement, final ContextManager contextManager) {
PersistRepositoryConfiguration repositoryConfig = contextManager.getInstanceContext().getModeConfiguration().getRepository();
String modeType = contextManager.getInstanceContext().getModeConfiguration().getType();
String repositoryType = null == repositoryConfig ? "" : repositoryConfig.getType();
Properties props = null == repositoryConfig ? new Properties() : repositoryConfig.getProps();
String repositoryType = null == repositoryConfig ? null : repositoryConfig.getType();
Properties props = null == repositoryConfig ? null : repositoryConfig.getProps();
return Collections.singleton(new LocalDataQueryResultRow(modeType, repositoryType, props));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public Collection<LocalDataQueryResultRow> getRows(final ShowComputeNodesStateme
private LocalDataQueryResultRow buildRow(final ComputeNodeInstance instance, final String modeType) {
String labels = String.join(",", instance.getLabels());
InstanceMetaData instanceMetaData = instance.getMetaData();
return new LocalDataQueryResultRow(instanceMetaData.getId(), instanceMetaData.getType().name(), instanceMetaData.getIp(),
return new LocalDataQueryResultRow(instanceMetaData.getId(), instanceMetaData.getType(), instanceMetaData.getIp(),
instanceMetaData instanceof ProxyInstanceMetaData ? ((ProxyInstanceMetaData) instanceMetaData).getPort() : -1,
instance.getState().getCurrentState().name(), modeType, instance.getWorkerId(), labels, instanceMetaData.getVersion());
instance.getState().getCurrentState(), modeType, instance.getWorkerId(), labels, instanceMetaData.getVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.util.json.JsonUtils;
import org.apache.shardingsphere.mode.manager.ContextManager;

import java.util.Arrays;
Expand Down Expand Up @@ -66,11 +65,11 @@ private Collection<LocalDataQueryResultRow> buildTableRows(final String database
}

private LocalDataQueryResultRow buildColumnRow(final String databaseName, final String tableName, final ShardingSphereColumn column) {
return new LocalDataQueryResultRow(databaseName, tableName, "COLUMN", column.getName(), JsonUtils.toJsonString(column));
return new LocalDataQueryResultRow(databaseName, tableName, "COLUMN", column.getName(), column);
}

private LocalDataQueryResultRow buildIndexRow(final String databaseName, final String tableName, final ShardingSphereIndex index) {
return new LocalDataQueryResultRow(databaseName, tableName, "INDEX", index.getName(), JsonUtils.toJsonString(index));
return new LocalDataQueryResultRow(databaseName, tableName, "INDEX", index.getName(), index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public final class ShowVersionExecutor implements DatabaseAdminQueryExecutor {

@Override
public void execute(final ConnectionSession connectionSession) {
mergedResult = new LocalDataMergedResult(Collections.singleton(new LocalDataQueryResultRow(DatabaseProtocolServerInfo.getProtocolVersion(connectionSession.getDatabaseName(),
TypedSPILoader.getService(DatabaseType.class, "MySQL")))));
mergedResult = new LocalDataMergedResult(Collections.singleton(new LocalDataQueryResultRow(
DatabaseProtocolServerInfo.getProtocolVersion(connectionSession.getDatabaseName(), TypedSPILoader.getService(DatabaseType.class, "MySQL")))));
}

@Override
Expand Down

0 comments on commit 7b3a525

Please sign in to comment.