Skip to content

Commit

Permalink
Refactor ShadowHintDataSourceMappingsFinder (#33550)
Browse files Browse the repository at this point in the history
* Rename ShadowHintDataSourceMappingsFinder

* Refactor ShadowHintDataSourceMappingsFinder

* Refactor ShadowHintDataSourceMappingsFinder
  • Loading branch information
terrymanu authored Nov 4, 2024
1 parent 097a48f commit 30f538e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public Map<String, String> find(final ShadowRule rule) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
* 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;
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;
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<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;
}
Expand Down

0 comments on commit 30f538e

Please sign in to comment.