Skip to content

Commit

Permalink
Refactor DatabaseRuleDropExecutor (#29939)
Browse files Browse the repository at this point in the history
* Refactor DatabaseRuleDropExecutor

* Refactor DatabaseRuleDropExecutor

* Refactor DatabaseRuleDropExecutor

* Refactor DatabaseRuleDropExecutor
  • Loading branch information
terrymanu authored Jan 31, 2024
1 parent c5ba520 commit 40ccd5a
Show file tree
Hide file tree
Showing 22 changed files with 138 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ private boolean containsIgnoreCase(final Collection<String> currentRules, final
}

@Override
public boolean hasAnyOneToBeDropped(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
return isExistRuleConfig(currentRuleConfig) && !getIdenticalData(currentRuleConfig.getTables(), sqlStatement.getTables()).isEmpty();
public boolean hasAnyOneToBeDropped(final DropBroadcastTableRuleStatement sqlStatement) {
return isExistRuleConfig(rule.getConfiguration()) && !getIdenticalData(rule.getConfiguration().getTables(), sqlStatement.getTables()).isEmpty();
}

@Override
public BroadcastRuleConfiguration buildToBeAlteredRuleConfiguration(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
BroadcastRuleConfiguration result = new BroadcastRuleConfiguration(new HashSet<>(currentRuleConfig.getTables()));
public BroadcastRuleConfiguration buildToBeAlteredRuleConfiguration(final DropBroadcastTableRuleStatement sqlStatement) {
BroadcastRuleConfiguration result = new BroadcastRuleConfiguration(new HashSet<>(rule.getConfiguration().getTables()));
result.getTables().removeIf(each -> containsIgnoreCase(sqlStatement.getTables(), each));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,35 @@ private void checkToBeDroppedEncryptTableNames(final DropEncryptRuleStatement sq
}

@Override
public boolean hasAnyOneToBeDropped(final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
return null != currentRuleConfig
&& !getIdenticalData(currentRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getTables()).isEmpty();
public boolean hasAnyOneToBeDropped(final DropEncryptRuleStatement sqlStatement) {
return null != rule && !getIdenticalData(((EncryptRuleConfiguration) rule.getConfiguration())
.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getTables()).isEmpty();
}

@Override
public EncryptRuleConfiguration buildToBeDroppedRuleConfiguration(final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
public EncryptRuleConfiguration buildToBeDroppedRuleConfiguration(final DropEncryptRuleStatement sqlStatement) {
Collection<EncryptTableRuleConfiguration> toBeDroppedTables = new LinkedList<>();
Map<String, AlgorithmConfiguration> toBeDroppedEncryptors = new HashMap<>();
for (String each : sqlStatement.getTables()) {
toBeDroppedTables.add(new EncryptTableRuleConfiguration(each, Collections.emptyList()));
dropRule(currentRuleConfig, each);
dropRule(each);
}
UnusedAlgorithmFinder.findUnusedEncryptor(currentRuleConfig).forEach(each -> toBeDroppedEncryptors.put(each, currentRuleConfig.getEncryptors().get(each)));
UnusedAlgorithmFinder.findUnusedEncryptor((EncryptRuleConfiguration) rule.getConfiguration())
.forEach(each -> toBeDroppedEncryptors.put(each, ((EncryptRuleConfiguration) rule.getConfiguration()).getEncryptors().get(each)));
return new EncryptRuleConfiguration(toBeDroppedTables, toBeDroppedEncryptors);
}

@Override
public boolean updateCurrentRuleConfiguration(final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
sqlStatement.getTables().forEach(each -> dropRule(currentRuleConfig, each));
sqlStatement.getTables().forEach(this::dropRule);
dropUnusedEncryptor(currentRuleConfig);
return currentRuleConfig.isEmpty();
}

private void dropRule(final EncryptRuleConfiguration currentRuleConfig, final String ruleName) {
Optional<EncryptTableRuleConfiguration> encryptTableRuleConfig = currentRuleConfig.getTables().stream().filter(each -> each.getName().equals(ruleName)).findAny();
encryptTableRuleConfig.ifPresent(optional -> currentRuleConfig.getTables().remove(encryptTableRuleConfig.get()));
private void dropRule(final String ruleName) {
Optional<EncryptTableRuleConfiguration> encryptTableRuleConfig = ((EncryptRuleConfiguration) rule.getConfiguration()).getTables().stream()
.filter(each -> each.getName().equals(ruleName)).findAny();
encryptTableRuleConfig.ifPresent(optional -> ((EncryptRuleConfiguration) rule.getConfiguration()).getTables().remove(encryptTableRuleConfig.get()));
}

private void dropUnusedEncryptor(final EncryptRuleConfiguration currentRuleConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ void assertCheckSQLStatementWithoutToBeDroppedRule() {
@Test
void assertUpdateCurrentRuleConfiguration() {
EncryptRuleConfiguration ruleConfig = createCurrentRuleConfiguration();
EncryptRule rule = mock(EncryptRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
assertTrue(executor.updateCurrentRuleConfiguration(createSQLStatement("t_encrypt"), ruleConfig));
assertTrue(ruleConfig.getEncryptors().isEmpty());
}

@Test
void assertUpdateCurrentRuleConfigurationWithInUsedEncryptor() {
EncryptRuleConfiguration ruleConfig = createCurrentRuleConfigurationWithMultipleTableRules();
EncryptRule rule = mock(EncryptRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
assertFalse(executor.updateCurrentRuleConfiguration(createSQLStatement("t_encrypt"), ruleConfig));
assertThat(ruleConfig.getEncryptors().size(), is(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,33 @@ private void checkToBeDroppedMaskTableNames(final DropMaskRuleStatement sqlState
}

@Override
public boolean hasAnyOneToBeDropped(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
return null != currentRuleConfig
&& !getIdenticalData(currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getTables()).isEmpty();
public boolean hasAnyOneToBeDropped(final DropMaskRuleStatement sqlStatement) {
return null != rule
&& !getIdenticalData(rule.getConfiguration().getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getTables()).isEmpty();
}

@Override
public MaskRuleConfiguration buildToBeDroppedRuleConfiguration(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
public MaskRuleConfiguration buildToBeDroppedRuleConfiguration(final DropMaskRuleStatement sqlStatement) {
Collection<MaskTableRuleConfiguration> toBeDroppedTables = new LinkedList<>();
Map<String, AlgorithmConfiguration> toBeDroppedAlgorithms = new LinkedHashMap<>();
for (String each : sqlStatement.getTables()) {
toBeDroppedTables.add(new MaskTableRuleConfiguration(each, Collections.emptyList()));
dropRule(currentRuleConfig, each);
dropRule(each);
}
findUnusedAlgorithms(currentRuleConfig).forEach(each -> toBeDroppedAlgorithms.put(each, currentRuleConfig.getMaskAlgorithms().get(each)));
findUnusedAlgorithms(rule.getConfiguration()).forEach(each -> toBeDroppedAlgorithms.put(each, rule.getConfiguration().getMaskAlgorithms().get(each)));
return new MaskRuleConfiguration(toBeDroppedTables, toBeDroppedAlgorithms);
}

@Override
public boolean updateCurrentRuleConfiguration(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
sqlStatement.getTables().forEach(each -> dropRule(currentRuleConfig, each));
sqlStatement.getTables().forEach(this::dropRule);
dropUnusedAlgorithm(currentRuleConfig);
return currentRuleConfig.isEmpty();
}

private void dropRule(final MaskRuleConfiguration currentRuleConfig, final String ruleName) {
Optional<MaskTableRuleConfiguration> maskTableRuleConfig = currentRuleConfig.getTables().stream().filter(each -> each.getName().equals(ruleName)).findAny();
maskTableRuleConfig.ifPresent(optional -> currentRuleConfig.getTables().remove(maskTableRuleConfig.get()));
private void dropRule(final String ruleName) {
Optional<MaskTableRuleConfiguration> maskTableRuleConfig = rule.getConfiguration().getTables().stream().filter(each -> each.getName().equals(ruleName)).findAny();
maskTableRuleConfig.ifPresent(optional -> rule.getConfiguration().getTables().remove(maskTableRuleConfig.get()));
}

private void dropUnusedAlgorithm(final MaskRuleConfiguration currentRuleConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ void assertCheckSQLStatementWithoutToBeDroppedRule() {
@Test
void assertUpdateCurrentRuleConfiguration() {
MaskRuleConfiguration ruleConfig = createCurrentRuleConfiguration();
MaskRule rule = mock(MaskRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
assertTrue(executor.updateCurrentRuleConfiguration(createSQLStatement(false, "t_mask"), ruleConfig));
assertTrue(ruleConfig.getMaskAlgorithms().isEmpty());
assertTrue(ruleConfig.getTables().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,44 +96,44 @@ private Collection<String> getInUsedResources() {
}

@Override
public ReadwriteSplittingRuleConfiguration buildToBeDroppedRuleConfiguration(final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) {
public ReadwriteSplittingRuleConfiguration buildToBeDroppedRuleConfiguration(final DropReadwriteSplittingRuleStatement sqlStatement) {
Collection<ReadwriteSplittingDataSourceRuleConfiguration> toBeDroppedDataSources = new LinkedList<>();
Map<String, AlgorithmConfiguration> toBeDroppedLoadBalancers = new HashMap<>();
for (String each : sqlStatement.getNames()) {
toBeDroppedDataSources.add(new ReadwriteSplittingDataSourceRuleConfiguration(each, null, null, null));
dropRule(currentRuleConfig, each);
dropRule(each);
}
findUnusedLoadBalancers(currentRuleConfig).forEach(each -> toBeDroppedLoadBalancers.put(each, currentRuleConfig.getLoadBalancers().get(each)));
findUnusedLoadBalancers().forEach(each -> toBeDroppedLoadBalancers.put(each, rule.getConfiguration().getLoadBalancers().get(each)));
return new ReadwriteSplittingRuleConfiguration(toBeDroppedDataSources, toBeDroppedLoadBalancers);
}

@Override
public boolean updateCurrentRuleConfiguration(final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) {
for (String each : sqlStatement.getNames()) {
dropRule(currentRuleConfig, each);
dropRule(each);
}
dropUnusedLoadBalancer(currentRuleConfig);
return currentRuleConfig.isEmpty();
}

private void dropRule(final ReadwriteSplittingRuleConfiguration currentRuleConfig, final String ruleName) {
Optional<ReadwriteSplittingDataSourceRuleConfiguration> dataSourceRuleConfig = currentRuleConfig.getDataSources().stream().filter(each -> ruleName.equals(each.getName())).findAny();
dataSourceRuleConfig.ifPresent(optional -> currentRuleConfig.getDataSources().remove(optional));
private void dropRule(final String ruleName) {
Optional<ReadwriteSplittingDataSourceRuleConfiguration> dataSourceRuleConfig = rule.getConfiguration().getDataSources().stream().filter(each -> ruleName.equals(each.getName())).findAny();
dataSourceRuleConfig.ifPresent(optional -> rule.getConfiguration().getDataSources().remove(optional));
}

private void dropUnusedLoadBalancer(final ReadwriteSplittingRuleConfiguration currentRuleConfig) {
findUnusedLoadBalancers(currentRuleConfig).forEach(each -> currentRuleConfig.getLoadBalancers().remove(each));
findUnusedLoadBalancers().forEach(each -> currentRuleConfig.getLoadBalancers().remove(each));
}

private Collection<String> findUnusedLoadBalancers(final ReadwriteSplittingRuleConfiguration currentRuleConfig) {
Collection<String> inUsedAlgorithms = currentRuleConfig.getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getLoadBalancerName).collect(Collectors.toSet());
return currentRuleConfig.getLoadBalancers().keySet().stream().filter(each -> !inUsedAlgorithms.contains(each)).collect(Collectors.toSet());
private Collection<String> findUnusedLoadBalancers() {
Collection<String> inUsedAlgorithms = rule.getConfiguration().getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getLoadBalancerName).collect(Collectors.toSet());
return rule.getConfiguration().getLoadBalancers().keySet().stream().filter(each -> !inUsedAlgorithms.contains(each)).collect(Collectors.toSet());
}

@Override
public boolean hasAnyOneToBeDropped(final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) {
return null != currentRuleConfig && !getIdenticalData(currentRuleConfig.getDataSources().stream()
.map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getNames()).isEmpty();
public boolean hasAnyOneToBeDropped(final DropReadwriteSplittingRuleStatement sqlStatement) {
return null != rule && !getIdenticalData(
rule.getConfiguration().getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getNames()).isEmpty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,29 @@ void assertCheckSQLStatementWithInUsed() throws RuleDefinitionViolationException
@Test
void assertUpdateCurrentRuleConfiguration() {
ReadwriteSplittingRuleConfiguration ruleConfig = createCurrentRuleConfiguration();
ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
assertTrue(executor.updateCurrentRuleConfiguration(createSQLStatement(), ruleConfig));
assertThat(ruleConfig.getLoadBalancers().size(), is(0));
}

@Test
void assertUpdateCurrentRuleConfigurationWithInUsedLoadBalancer() {
ReadwriteSplittingRuleConfiguration ruleConfig = createMultipleCurrentRuleConfigurations();
ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
assertFalse(executor.updateCurrentRuleConfiguration(createSQLStatement(), ruleConfig));
assertThat(ruleConfig.getLoadBalancers().size(), is(1));
}

@Test
void assertUpdateCurrentRuleConfigurationWithoutLoadBalancerName() {
ReadwriteSplittingRuleConfiguration ruleConfig = createCurrentRuleConfigurationWithoutLoadBalancerName();
ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
assertTrue(executor.updateCurrentRuleConfiguration(createSQLStatement(), ruleConfig));
assertThat(ruleConfig.getLoadBalancers().size(), is(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ private void checkAlgorithm() {
}

@Override
public boolean hasAnyOneToBeDropped(final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
return null != currentRuleConfig && null != currentRuleConfig.getDefaultShadowAlgorithmName();
public boolean hasAnyOneToBeDropped(final DropDefaultShadowAlgorithmStatement sqlStatement) {
return null != rule && null != rule.getConfiguration().getDefaultShadowAlgorithmName();
}

@Override
public ShadowRuleConfiguration buildToBeDroppedRuleConfiguration(final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
public ShadowRuleConfiguration buildToBeDroppedRuleConfiguration(final DropDefaultShadowAlgorithmStatement sqlStatement) {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.setShadowAlgorithms(Collections.singletonMap(currentRuleConfig.getDefaultShadowAlgorithmName(),
currentRuleConfig.getShadowAlgorithms().get(currentRuleConfig.getDefaultShadowAlgorithmName())));
result.setDefaultShadowAlgorithmName(currentRuleConfig.getDefaultShadowAlgorithmName());
result.setShadowAlgorithms(Collections.singletonMap(rule.getConfiguration().getDefaultShadowAlgorithmName(),
rule.getConfiguration().getShadowAlgorithms().get(rule.getConfiguration().getDefaultShadowAlgorithmName())));
result.setDefaultShadowAlgorithmName(rule.getConfiguration().getDefaultShadowAlgorithmName());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ private Collection<String> getAlgorithmInUse() {
}

@Override
public boolean hasAnyOneToBeDropped(final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
return null != currentRuleConfig
&& !getIdenticalData(ShadowRuleStatementSupporter.getAlgorithmNames(currentRuleConfig), sqlStatement.getNames()).isEmpty();
public boolean hasAnyOneToBeDropped(final DropShadowAlgorithmStatement sqlStatement) {
return null != rule
&& !getIdenticalData(ShadowRuleStatementSupporter.getAlgorithmNames(rule.getConfiguration()), sqlStatement.getNames()).isEmpty();
}

@Override
public ShadowRuleConfiguration buildToBeDroppedRuleConfiguration(final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
public ShadowRuleConfiguration buildToBeDroppedRuleConfiguration(final DropShadowAlgorithmStatement sqlStatement) {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
for (String each : sqlStatement.getNames()) {
result.getShadowAlgorithms().put(each, currentRuleConfig.getShadowAlgorithms().get(each));
result.getShadowAlgorithms().put(each, rule.getConfiguration().getShadowAlgorithms().get(each));
}
return result;
}
Expand Down
Loading

0 comments on commit 40ccd5a

Please sign in to comment.