Skip to content

Commit

Permalink
Merge ShadowRuleConfigurationChecker and ShadowImportRuleConfiguratio…
Browse files Browse the repository at this point in the history
…nProvider
  • Loading branch information
terrymanu committed Mar 24, 2024
1 parent 4a98a28 commit 9bb5a57
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 92 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.rule.DuplicateRuleException;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
Expand All @@ -29,13 +31,16 @@
import org.apache.shardingsphere.shadow.exception.metadata.MissingRequiredShadowAlgorithmException;
import org.apache.shardingsphere.shadow.exception.metadata.MissingRequiredShadowConfigurationException;
import org.apache.shardingsphere.shadow.exception.metadata.ShadowDataSourceMappingNotFoundException;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Shadow rule configuration checker.
Expand All @@ -44,6 +49,8 @@ public final class ShadowRuleConfigurationChecker implements RuleConfigurationCh

@Override
public void check(final String databaseName, final ShadowRuleConfiguration ruleConfig, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> builtRules) {
checkShadowAlgorithms(ruleConfig);
checkTablesNotDuplicated(databaseName, ruleConfig);
Map<String, ShadowDataSourceConfiguration> dataSources = initShadowDataSources(ruleConfig.getDataSources());
checkDataSources(dataSources, dataSourceMap, databaseName);
Map<String, ShadowTableConfiguration> shadowTables = ruleConfig.getTables();
Expand All @@ -56,6 +63,17 @@ public void check(final String databaseName, final ShadowRuleConfiguration ruleC
checkShadowTableAlgorithmsReferences(shadowTables, databaseName);
}

private void checkShadowAlgorithms(final ShadowRuleConfiguration ruleConfig) {
ruleConfig.getShadowAlgorithms().values().forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getType(), each.getProps()));
}

private void checkTablesNotDuplicated(final String databaseName, final ShadowRuleConfiguration ruleConfig) {
Collection<String> tableNames = ruleConfig.getTables().keySet();
Collection<String> duplicatedTables = tableNames.stream().collect(Collectors.groupingBy(each -> each, Collectors.counting())).entrySet().stream()
.filter(each -> each.getValue() > 1).map(Entry::getKey).collect(Collectors.toSet());
ShardingSpherePreconditions.checkState(duplicatedTables.isEmpty(), () -> new DuplicateRuleException("SHADOW", databaseName, duplicatedTables));
}

private void checkDataSources(final Map<String, ShadowDataSourceConfiguration> shadowDataSources, final Map<String, DataSource> dataSourceMap, final String databaseName) {
Set<String> dataSourceName = dataSourceMap.keySet();
for (Entry<String, ShadowDataSourceConfiguration> entry : shadowDataSources.entrySet()) {
Expand Down Expand Up @@ -113,6 +131,20 @@ private Map<String, ShadowDataSourceConfiguration> initShadowDataSources(final C
return result;
}

@Override
public Collection<String> getRequiredDataSourceNames(final ShadowRuleConfiguration ruleConfig) {
Collection<String> result = new LinkedHashSet<>();
ruleConfig.getDataSources().forEach(each -> {
if (null != each.getShadowDataSourceName()) {
result.add(each.getShadowDataSourceName());
}
if (null != each.getProductionDataSourceName()) {
result.add(each.getProductionDataSourceName());
}
});
return result;
}

@Override
public int getOrder() {
return ShadowOrder.ORDER;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private ShadowRuleConfiguration createShadowRuleConfiguration() {
}

private AlgorithmConfiguration createAlgorithmConfiguration() {
return new AlgorithmConfiguration("user-id-insert-match-algorithm",
return new AlgorithmConfiguration("REGEX_MATCH",
PropertiesBuilder.build(new Property("column", "shadow"), new Property("operation", "insert"), new Property("regex", "[1]")));
}
}

0 comments on commit 9bb5a57

Please sign in to comment.