Skip to content

Commit

Permalink
Merge 43795b0 into 0dc5419
Browse files Browse the repository at this point in the history
  • Loading branch information
yanickxia committed Dec 12, 2019
2 parents 0dc5419 + 43795b0 commit ee3b87e
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 32 deletions.
Expand Up @@ -19,26 +19,20 @@

import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.api.config.RuleConfiguration;

/**
* Shadow rule configuration.
*
* @author xiayan
*/
@RequiredArgsConstructor
@Getter
public final class ShadowRuleConfiguration implements RuleConfiguration {

private final String column;

private final Comparable value;

public ShadowRuleConfiguration(final String column, final Object value) {
public ShadowRuleConfiguration(final String column) {
Preconditions.checkArgument(null != column, "Column is Required.");
Preconditions.checkArgument(value.getClass().isAssignableFrom(Comparable.class), "Value is need Comparable.");
this.column = column;
this.value = (Comparable) value;
}
}
Expand Up @@ -33,11 +33,8 @@ public class ShadowRule implements BaseRule {

private String column;

private Comparable value;

public ShadowRule(final ShadowRuleConfiguration shadowRuleConfiguration) {
column = shadowRuleConfiguration.getColumn();
value = shadowRuleConfiguration.getValue();
ruleConfiguration = shadowRuleConfiguration;
}

Expand Down
Expand Up @@ -34,7 +34,5 @@ public class YamlShadowRuleConfiguration extends YamlRootRuleConfiguration {

private String column;

private Object value;

private DataSource dataSource;
}
Expand Up @@ -63,7 +63,6 @@ private void assertYamlMasterSlaveConfig(final YamlRootShadowConfiguration actua
assertDataSourceMap(actual);
assertMasterSlaveRule(actual);
assertThat(actual.getShadowRule().getColumn(), is("is_shadow"));
assertThat(actual.getShadowRule().getValue().toString(), is("shadow_db"));
assertShadowDataSourceMap(actual);
}

Expand Down
Expand Up @@ -63,7 +63,6 @@ private void assertYamlShardingConfig(final YamlRootShadowConfiguration actual)
assertDataSourceMap(actual);
assertThat(actual.getShardingRule().getTables().size(), is(4));
assertThat(actual.getShadowRule().getColumn(), is("is_shadow"));
assertTrue((Boolean) actual.getShadowRule().getValue());
assertTUser(actual);
assertTStock(actual);
assertTOrder(actual);
Expand Down
Expand Up @@ -42,7 +42,6 @@ masterSlaveRule:

shadowRule:
column: is_shadow
value: shadow_db
dataSources:
master_ds: !!com.zaxxer.hikari.HikariDataSource
driverClassName: org.h2.Driver
Expand Down
Expand Up @@ -116,7 +116,6 @@ shardingRule:
loadBalanceAlgorithmType: RANDOM
shadowRule:
column: is_shadow
value: true
dataSources:
master_ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: org.h2.Driver
Expand Down
Expand Up @@ -43,13 +43,13 @@
*/
@RequiredArgsConstructor
public class ShadowJudgementEngine {

private final ShadowRule shadowRule;

private final SQLStatementContext sqlStatementContext;

private final List<Object> parameters;

/**
* Judge if the sql should route to shadow table.
*
Expand All @@ -60,8 +60,9 @@ public final boolean isShadowSql() {
if (sqlStatement instanceof InsertStatement) {
LinkedList<ColumnSegment> columnSegments = (LinkedList<ColumnSegment>) ((InsertStatement) sqlStatement).getColumns();
for (int i = 0; i < columnSegments.size(); i++) {
if (columnSegments.get(i).getName().equals(shadowRule.getColumn()) && shadowRule.getValue().equals(parameters.get(i))) {
return true;
if (columnSegments.get(i).getName().equals(shadowRule.getColumn())) {
final Object value = parameters.get(i);
return value instanceof Boolean && (Boolean) value;
}
}
return false;
Expand All @@ -80,19 +81,16 @@ public final boolean isShadowSql() {
}
return false;
}

private boolean judgePredicateSegments(final Collection<PredicateSegment> predicates) {
for (PredicateSegment each : predicates) {
if (each.getColumn().getName().equals(shadowRule.getColumn())) {
Preconditions.checkArgument(each.getRightValue() instanceof PredicateCompareRightValue, "must be ");
Preconditions.checkArgument(each.getRightValue() instanceof PredicateCompareRightValue, "must be PredicateCompareRightValue");
PredicateCompareRightValue rightValue = (PredicateCompareRightValue) each.getRightValue();
Preconditions.checkArgument(rightValue.getExpression() instanceof LiteralExpressionSegment, "must be ");
Preconditions.checkArgument(rightValue.getExpression() instanceof LiteralExpressionSegment, "must be LiteralExpressionSegment");
LiteralExpressionSegment expressionSegment = (LiteralExpressionSegment) rightValue.getExpression();
Preconditions.checkArgument(expressionSegment.getLiterals().getClass().isAssignableFrom(Comparable.class), "must be ");
Comparable comparableValue = (Comparable) expressionSegment.getLiterals();
if (comparableValue.equals(shadowRule.getValue())) {
return true;
}
Preconditions.checkArgument(expressionSegment.getLiterals().getClass() == Boolean.class, "must be boolean type");
return (Boolean) expressionSegment.getLiterals();
}
}
return false;
Expand Down
Expand Up @@ -52,7 +52,7 @@ public final class YamlShadowDataSourceFactory {
@SneakyThrows
public static DataSource createDataSource(final File yamlFile) {
YamlRootShadowConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootShadowConfiguration.class);
ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration(config.getShadowRule().getColumn(), config.getShadowRule().getValue());
ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration(config.getShadowRule().getColumn());
return ShadowDataSourceFactory.createDataSource(createActualDataSource(config), createShadowDataSource(config), shadowRuleConfiguration, config.getProps());
}

Expand All @@ -65,7 +65,7 @@ public static DataSource createDataSource(final File yamlFile) {
@SneakyThrows
public static DataSource createDataSource(final byte[] yamlBytes) {
YamlRootShadowConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootShadowConfiguration.class);
ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration(config.getShadowRule().getColumn(), config.getShadowRule().getValue());
ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration(config.getShadowRule().getColumn());
return ShadowDataSourceFactory.createDataSource(createActualDataSource(config), createShadowDataSource(config), shadowRuleConfiguration, config.getProps());
}

Expand All @@ -78,6 +78,8 @@ private static DataSource createActualDataSource(final YamlRootShadowConfigurati
return ShardingDataSourceFactory.createDataSource(config.getDataSources(), new ShardingRuleConfigurationYamlSwapper().swap(config.getShardingRule()), props);
} else if (config.isMasterSlave()) {
return MasterSlaveDataSourceFactory.createDataSource(config.getDataSources(), new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), props);
} else if (null != config.getDataSource()) {
return config.getDataSource();
} else {
throw new UnsupportedOperationException("unsupported datasource");
}
Expand All @@ -95,6 +97,8 @@ private static DataSource createShadowDataSource(final YamlRootShadowConfigurati
} else if (config.isMasterSlave()) {
return MasterSlaveDataSourceFactory.createDataSource(config.getShadowRule().getDataSources(),
new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), props);
} else if (null != config.getDataSource()) {
return config.getDataSource();
} else {
throw new UnsupportedOperationException("unsupported datasource");
}
Expand Down

0 comments on commit ee3b87e

Please sign in to comment.