Skip to content

Commit

Permalink
Refactor ShadowRule (#33546)
Browse files Browse the repository at this point in the history
* Refactor ShadowRule

* Refactor ShadowRule
  • Loading branch information
terrymanu authored Nov 4, 2024
1 parent a4b30e0 commit 6cdbbf3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public final class ShadowRule implements DatabaseRule {
@Getter
private final ShadowRuleConfiguration configuration;

@Getter
private final Map<String, ShadowAlgorithm> shadowAlgorithms;

private final ShadowAlgorithm defaultShadowAlgorithm;
Expand Down Expand Up @@ -85,6 +84,16 @@ private Map<String, ShadowTableRule> createTableRules(final Map<String, ShadowTa
entry -> new ShadowTableRule(entry.getKey(), entry.getValue().getDataSourceNames(), entry.getValue().getShadowAlgorithmNames(), shadowAlgorithms), (a, b) -> b, LinkedHashMap::new));
}

/**
* Whether contains shadow algorithm.
*
* @param algorithmName algorithm name
* @return contains shadow algorithm or not
*/
public boolean containsShadowAlgorithm(final String algorithmName) {
return shadowAlgorithms.containsKey(algorithmName);
}

/**
* Get default shadow algorithm.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ShadowRuleTest {

Expand Down Expand Up @@ -93,6 +94,11 @@ private Map<String, AlgorithmConfiguration> createShadowAlgorithms() {
return result;
}

@Test
void assertContainsShadowAlgorithm() {
assertTrue(rule.containsShadowAlgorithm("sql-hint-algorithm"));
}

@Test
void assertGetDefaultShadowAlgorithm() {
assertFalse(rule.getDefaultShadowAlgorithm().isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void checkBeforeUpdate(final CreateDefaultShadowAlgorithmStatement sqlSta
}

private void checkAlgorithmExisted() {
boolean isDuplicatedAlgorithmName = null != rule && rule.getShadowAlgorithms().containsKey("default_shadow_algorithm");
boolean isDuplicatedAlgorithmName = null != rule && rule.containsShadowAlgorithm("default_shadow_algorithm");
ShardingSpherePreconditions.checkState(!isDuplicatedAlgorithmName, () -> new InUsedAlgorithmException("Shadow", database.getName(), Collections.singleton("default_shadow_algorithm")));
}

Expand All @@ -72,7 +72,7 @@ private void checkAlgorithmType(final CreateDefaultShadowAlgorithmStatement sqlS
@Override
public ShadowRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateDefaultShadowAlgorithmStatement sqlStatement) {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
if (null == rule || !rule.getShadowAlgorithms().containsKey("default_shadow_algorithm")) {
if (null == rule || !rule.containsShadowAlgorithm("default_shadow_algorithm")) {
result.setShadowAlgorithms(buildAlgorithmMap(sqlStatement));
result.setDefaultShadowAlgorithmName("default_shadow_algorithm");
}
Expand Down

0 comments on commit 6cdbbf3

Please sign in to comment.