This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 30f538ed828 Refactor ShadowHintDataSourceMappingsFinder (#33550)
30f538ed828 is described below
commit 30f538ed8283c06cb312d5ccec80a3b4d1a05717
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Nov 5 00:28:47 2024 +0800
Refactor ShadowHintDataSourceMappingsFinder (#33550)
* Rename ShadowHintDataSourceMappingsFinder
* Refactor ShadowHintDataSourceMappingsFinder
* Refactor ShadowHintDataSourceMappingsFinder
---
.../shadow/condition/ShadowCondition.java | 4 +++
.../ShadowDataSourceMappingsFinderFactory.java | 2 +-
...ShadowDMLStatementDataSourceMappingsFinder.java | 3 +--
.../ShadowHintDataSourceMappingsFinder.java | 10 +++----
.../ShadowHintDataSourceMappingsFinderTest.java | 31 +++++++++-------------
5 files changed, 21 insertions(+), 29 deletions(-)
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 2806b92dfaa..51e7fd63d16 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 54e2a246d4f..02fce57a2a6 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.ShadowDeleteStatementDa
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 8a4d3c25894..24d2622121c 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 abstract class
AbstractShadowDMLStatementDataSourceMappingsFinder impleme
private boolean isMatchDefaultAlgorithm(final ShadowRule rule) {
Optional<ShadowAlgorithm> defaultAlgorithm =
rule.getDefaultShadowAlgorithm();
if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof
HintShadowAlgorithm<?>) {
- ShadowCondition shadowCondition = new ShadowCondition("",
ShadowOperationType.HINT_MATCH);
- return
HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>)
defaultAlgorithm.get(), shadowCondition, rule, isShadow);
+ return
HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>)
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 a35ca070f78..c483c8451ad 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.condition.ShadowCondition;
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<String, String> 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 b9b88fabbbe..e9bf47493bb 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.config.table.ShadowTableConfiguration;
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 @@ import static org.hamcrest.MatcherAssert.assertThat;
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<String, String> 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;
}