Skip to content

Commit

Permalink
Remove MaskDistSQLConstants (#30611)
Browse files Browse the repository at this point in the history
* Remove MaskDistSQLConstants

* Refactor MaskImportRuleConfigurationProvider
  • Loading branch information
terrymanu authored Mar 22, 2024
1 parent 95e3baf commit a08e83b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 61 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.stream.Collectors;

Expand All @@ -53,11 +52,6 @@ public void check(final ShardingSphereDatabase database, final RuleConfiguration
checkMaskAlgorithmsExisted(maskRuleConfig, database.getName());
}

@Override
public DatabaseRule build(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig, final InstanceContext instanceContext) {
return new MaskRule((MaskRuleConfiguration) ruleConfig);
}

private void checkTables(final MaskRuleConfiguration currentRuleConfig, final String databaseName) {
Collection<String> tableNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList());
Collection<String> duplicatedTables = tableNames.stream().collect(Collectors.groupingBy(each -> each, Collectors.counting())).entrySet().stream()
Expand All @@ -70,14 +64,17 @@ private void checkMaskAlgorithms(final MaskRuleConfiguration currentRuleConfig)
}

private void checkMaskAlgorithmsExisted(final MaskRuleConfiguration currentRuleConfig, final String databaseName) {
Collection<MaskColumnRuleConfiguration> columns = new LinkedList<>();
currentRuleConfig.getTables().forEach(each -> columns.addAll(each.getColumns()));
Collection<String> notExistedAlgorithms = columns.stream().map(MaskColumnRuleConfiguration::getMaskAlgorithm).collect(Collectors.toList());
Collection<String> maskAlgorithms = currentRuleConfig.getMaskAlgorithms().keySet();
notExistedAlgorithms.removeIf(maskAlgorithms::contains);
Collection<String> notExistedAlgorithms = currentRuleConfig.getTables().stream()
.flatMap(each -> each.getColumns().stream()).map(MaskColumnRuleConfiguration::getMaskAlgorithm).collect(Collectors.toList());
notExistedAlgorithms.removeIf(currentRuleConfig.getMaskAlgorithms().keySet()::contains);
ShardingSpherePreconditions.checkState(notExistedAlgorithms.isEmpty(), () -> new MissingRequiredAlgorithmException(databaseName, notExistedAlgorithms));
}

@Override
public DatabaseRule build(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig, final InstanceContext instanceContext) {
return new MaskRule((MaskRuleConfiguration) ruleConfig);
}

@Override
public Class<? extends RuleConfiguration> getType() {
return MaskRuleConfiguration.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.distsql.handler.constant.MaskDistSQLConstants;

import java.util.Collection;
import java.util.Iterator;
Expand All @@ -34,21 +33,33 @@
*/
public final class MaskRuleConfigurationToDistSQLConverter implements RuleConfigurationToDistSQLConverter<MaskRuleConfiguration> {

private static final String CREATE_MASK = "CREATE MASK RULE";

private static final String MASK = " %s ("
+ System.lineSeparator()
+ "COLUMNS("
+ System.lineSeparator()
+ "%s"
+ System.lineSeparator()
+ "),";

private static final String MASK_COLUMN = "(NAME=%s, %s)";

@Override
public String convert(final MaskRuleConfiguration ruleConfig) {
if (ruleConfig.getTables().isEmpty()) {
return "";
}
StringBuilder result = new StringBuilder(MaskDistSQLConstants.CREATE_MASK);
StringBuilder result = new StringBuilder(CREATE_MASK);
Iterator<MaskTableRuleConfiguration> iterator = ruleConfig.getTables().iterator();
while (iterator.hasNext()) {
MaskTableRuleConfiguration tableRuleConfig = iterator.next();
result.append(String.format(MaskDistSQLConstants.MASK, tableRuleConfig.getName(), getMaskColumns(tableRuleConfig.getColumns(), ruleConfig.getMaskAlgorithms())));
result.append(String.format(MASK, tableRuleConfig.getName(), getMaskColumns(tableRuleConfig.getColumns(), ruleConfig.getMaskAlgorithms())));
if (iterator.hasNext()) {
result.append(MaskDistSQLConstants.COMMA).append(System.lineSeparator());
result.append(",").append(System.lineSeparator());
}
}
result.append(MaskDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
result.append(";").append(System.lineSeparator()).append(System.lineSeparator());
return result.toString();
}

Expand All @@ -57,7 +68,7 @@ private String getMaskColumns(final Collection<MaskColumnRuleConfiguration> colu
Iterator<MaskColumnRuleConfiguration> iterator = columnRuleConfig.iterator();
if (iterator.hasNext()) {
MaskColumnRuleConfiguration column = iterator.next();
result.append(String.format(MaskDistSQLConstants.MASK_COLUMN, column.getLogicColumn(), getMaskAlgorithms(column, maskAlgorithms)));
result.append(String.format(MASK_COLUMN, column.getLogicColumn(), getMaskAlgorithms(column, maskAlgorithms)));
}
return result.toString();
}
Expand Down

0 comments on commit a08e83b

Please sign in to comment.