diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/condition/ShadowCondition.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/condition/ShadowCondition.java index 2806b92dfaaf5..51e7fd63d16e4 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/condition/ShadowCondition.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/condition/ShadowCondition.java @@ -34,6 +34,10 @@ public final class ShadowCondition { private final ShadowColumnCondition columnCondition; + public ShadowCondition() { + this("", ShadowOperationType.HINT_MATCH, null); + } + public ShadowCondition(final String tableName, final ShadowOperationType operationType) { this(tableName, operationType, null); } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/ShadowDataSourceMappingsFinderFactory.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/ShadowDataSourceMappingsFinderFactory.java index 54e2a246d4f47..02fce57a2a6ca 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/ShadowDataSourceMappingsFinderFactory.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/ShadowDataSourceMappingsFinderFactory.java @@ -28,7 +28,7 @@ import org.apache.shardingsphere.shadow.route.finder.dml.ShadowInsertStatementDataSourceMappingsFinder; import org.apache.shardingsphere.shadow.route.finder.dml.ShadowSelectStatementDataSourceMappingsFinder; import org.apache.shardingsphere.shadow.route.finder.dml.ShadowUpdateStatementDataSourceMappingsFinder; -import org.apache.shardingsphere.shadow.route.finder.other.ShadowHintDataSourceMappingsFinder; +import org.apache.shardingsphere.shadow.route.finder.hint.ShadowHintDataSourceMappingsFinder; /** * Shadow data source mappings finder factory. diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/dml/AbstractShadowDMLStatementDataSourceMappingsFinder.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/dml/AbstractShadowDMLStatementDataSourceMappingsFinder.java index 8a4d3c2589477..24d2622121cb9 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/dml/AbstractShadowDMLStatementDataSourceMappingsFinder.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/dml/AbstractShadowDMLStatementDataSourceMappingsFinder.java @@ -83,8 +83,7 @@ public Map find(final ShadowRule rule) { private boolean isMatchDefaultAlgorithm(final ShadowRule rule) { Optional defaultAlgorithm = rule.getDefaultShadowAlgorithm(); if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof HintShadowAlgorithm) { - ShadowCondition shadowCondition = new ShadowCondition("", ShadowOperationType.HINT_MATCH); - return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm>) defaultAlgorithm.get(), shadowCondition, rule, isShadow); + return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm>) defaultAlgorithm.get(), new ShadowCondition(), rule, isShadow); } return false; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/other/ShadowHintDataSourceMappingsFinder.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/hint/ShadowHintDataSourceMappingsFinder.java similarity index 80% rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/other/ShadowHintDataSourceMappingsFinder.java rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/hint/ShadowHintDataSourceMappingsFinder.java index a35ca070f78e0..c483c8451adf6 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/other/ShadowHintDataSourceMappingsFinder.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/finder/hint/ShadowHintDataSourceMappingsFinder.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.finder.other; +package org.apache.shardingsphere.shadow.route.finder.hint; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.hint.HintValueContext; @@ -23,7 +23,6 @@ import org.apache.shardingsphere.shadow.route.determiner.HintShadowAlgorithmDeterminer; import org.apache.shardingsphere.shadow.route.finder.ShadowDataSourceMappingsFinder; import org.apache.shardingsphere.shadow.rule.ShadowRule; -import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import java.util.Collections; import java.util.Map; @@ -38,12 +37,9 @@ public final class ShadowHintDataSourceMappingsFinder implements ShadowDataSourc @Override public Map find(final ShadowRule rule) { - return hintValueContext.isShadow() && isMatchAnyHintShadowAlgorithms(rule, new ShadowCondition("", ShadowOperationType.HINT_MATCH)) + ShadowCondition shadowCondition = new ShadowCondition(); + return rule.getAllHintShadowAlgorithms().stream().anyMatch(each -> HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, hintValueContext.isShadow())) ? rule.getAllShadowDataSourceMappings() : Collections.emptyMap(); } - - private boolean isMatchAnyHintShadowAlgorithms(final ShadowRule rule, final ShadowCondition shadowCondition) { - return rule.getAllHintShadowAlgorithms().stream().anyMatch(each -> HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, hintValueContext.isShadow())); - } } diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/finder/other/ShadowHintDataSourceMappingsFinderTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/finder/hint/ShadowHintDataSourceMappingsFinderTest.java similarity index 67% rename from features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/finder/other/ShadowHintDataSourceMappingsFinderTest.java rename to features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/finder/hint/ShadowHintDataSourceMappingsFinderTest.java index b9b88fabbbe87..e9bf47493bbfb 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/finder/other/ShadowHintDataSourceMappingsFinderTest.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/finder/hint/ShadowHintDataSourceMappingsFinderTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.finder.other; +package org.apache.shardingsphere.shadow.route.finder.hint; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; import org.apache.shardingsphere.infra.hint.HintValueContext; @@ -25,7 +25,6 @@ import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -36,29 +35,23 @@ class ShadowHintDataSourceMappingsFinderTest { - private ShadowHintDataSourceMappingsFinder finder; - - @BeforeEach - void init() { - finder = new ShadowHintDataSourceMappingsFinder(createHintValueContext()); - } - - private HintValueContext createHintValueContext() { - HintValueContext result = new HintValueContext(); - result.setShadow(true); - return result; + @Test + void assertFindWithShadowHint() { + HintValueContext hintValueContext = new HintValueContext(); + hintValueContext.setShadow(true); + assertThat(new ShadowHintDataSourceMappingsFinder(hintValueContext).find(new ShadowRule(createRuleConfiguration())), is(Collections.singletonMap("prod_ds", "shadow_ds"))); } @Test - void assertRoute() { - Map shadowDataSourceMappings = finder.find(new ShadowRule(createShadowRuleConfiguration())); - assertThat(shadowDataSourceMappings, is(Collections.singletonMap("ds", "ds_shadow"))); + void assertFindWithNotShadowHint() { + HintValueContext hintValueContext = new HintValueContext(); + assertThat(new ShadowHintDataSourceMappingsFinder(hintValueContext).find(new ShadowRule(createRuleConfiguration())), is(Collections.emptyMap())); } - private ShadowRuleConfiguration createShadowRuleConfiguration() { + private ShadowRuleConfiguration createRuleConfiguration() { ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - result.setDataSources(Collections.singleton(new ShadowDataSourceConfiguration("shadow-data-source", "ds", "ds_shadow"))); - result.setTables(Collections.singletonMap("t_order", new ShadowTableConfiguration(Collections.singleton("shadow-data-source"), Collections.singleton("sql-hint-algorithm")))); + result.setDataSources(Collections.singleton(new ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds"))); + result.setTables(Collections.singletonMap("foo_tbl", new ShadowTableConfiguration(Collections.singleton("foo_ds"), Collections.singleton("sql-hint-algorithm")))); result.setShadowAlgorithms(createShadowAlgorithms()); return result; }