This is an automated email from the ASF dual-hosted git repository.
panjuan 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 4713e81 Add default shadow algorithm (#13231)
4713e81 is described below
commit 4713e81a406662784c25cfbd3d4f8ce44f0a8b1e
Author: gin <[email protected]>
AuthorDate: Sun Oct 24 12:30:35 2021 +0800
Add default shadow algorithm (#13231)
* Add default shadow algorithm name in shadow rule configuration.
* Add default shadow algorithm name in yaml shadow rule configuration.
* Add match default shadow algorithm in shadow route engine.
* Fix shadow ut.
---
.../shadow/api/config/ShadowRuleConfiguration.java | 2 +
.../shadow/api/shadow/ShadowOperationType.java | 4 +-
.../AlgorithmProvidedShadowRuleConfiguration.java | 2 +
.../shadow/note/SimpleSQLNoteShadowAlgorithm.java | 2 +-
.../AbstractShadowRuleConfigurationChecker.java | 36 ++++++++--
...ithmProvidedShadowRuleConfigurationChecker.java | 6 +-
.../checker/ShadowRuleConfigurationChecker.java | 6 +-
.../shadow/route/ShadowSQLRouter.java | 2 +-
.../shadow/route/engine/ShadowRouteEngine.java | 30 ++++++++-
.../dml/AbstractShadowDMLStatementRouteEngine.java | 74 +++++++++++----------
.../impl/ShadowNonDMLStatementRoutingEngine.java | 26 +-------
.../shardingsphere/shadow/rule/ShadowRule.java | 32 +++++++--
.../yaml/config/YamlShadowRuleConfiguration.java | 2 +
...eAlgorithmProviderConfigurationYamlSwapper.java | 2 +
.../ShadowRuleConfigurationYamlSwapper.java | 2 +
.../note/SimpleSQLNoteShadowAlgorithmTest.java | 2 +-
.../ShadowDeleteStatementRoutingEngineTest.java | 6 +-
.../ShadowInsertStatementRoutingEngineTest.java | 6 +-
.../ShadowSelectStatementRoutingEngineTest.java | 6 +-
.../ShadowUpdateStatementRoutingEngineTest.java | 6 +-
.../YmlShadowDefaultSpringBootStarterTest.java | 77 ++++++++++++++++++++++
.../test/resources/application-shadow-default.yml | 33 ++++++++++
.../parser/ShadowRuleBeanDefinitionParser.java | 11 +++-
.../namespace/tag/ShadowRuleBeanDefinitionTag.java | 4 +-
.../main/resources/META-INF/namespace/shadow.xsd | 9 ++-
.../ShadowDefaultAlgorithmSpringNamespaceTest.java | 71 ++++++++++++++++++++
...hadow-default-algorithm-application-context.xml | 38 +++++++++++
27 files changed, 393 insertions(+), 104 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
index 3843e1f..987e7a0 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
@@ -37,6 +37,8 @@ public final class ShadowRuleConfiguration implements
SchemaRuleConfiguration, D
private boolean enable;
+ private String defaultShadowAlgorithmName;
+
private Map<String, ShadowDataSourceConfiguration> dataSources = new
LinkedHashMap<>();
private Map<String, ShadowTableConfiguration> tables = new
LinkedHashMap<>();
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/ShadowOperationType.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/ShadowOperationType.java
index d0ecc75..c4ffeac 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/ShadowOperationType.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/ShadowOperationType.java
@@ -45,9 +45,9 @@ public enum ShadowOperationType {
SELECT,
/**
- * non-DML statement shadow operation.
+ * The shadow operation is SQL note match.
*/
- NON_DML;
+ NOTE_MATCH;
/**
* Contains operation type.
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/config/AlgorithmProvidedShadowRuleConfiguration.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/config/AlgorithmProvidedShadowRuleConfiguration.java
index db3fb0b..6bea1d2 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/config/AlgorithmProvidedShadowRuleConfiguration.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/config/AlgorithmProvidedShadowRuleConfiguration.java
@@ -37,6 +37,8 @@ public final class AlgorithmProvidedShadowRuleConfiguration
implements SchemaRul
private boolean enable;
+ private String defaultShadowAlgorithmName;
+
private Map<String, ShadowDataSourceConfiguration> dataSources = new
LinkedHashMap<>();
private Map<String, ShadowTableConfiguration> tables = new
LinkedHashMap<>();
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithm.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithm.java
index 32ba08b..e11382d 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithm.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithm.java
@@ -50,7 +50,7 @@ public final class SimpleSQLNoteShadowAlgorithm implements
NoteShadowAlgorithm<S
@Override
public boolean isShadow(final Collection<String> shadowTableNames, final
PreciseNoteShadowValue<String> noteShadowValue) {
- if (ShadowOperationType.NON_DML !=
noteShadowValue.getShadowOperationType() &&
!shadowTableNames.contains(noteShadowValue.getLogicTableName())) {
+ if (ShadowOperationType.NOTE_MATCH !=
noteShadowValue.getShadowOperationType() &&
!shadowTableNames.contains(noteShadowValue.getLogicTableName())) {
return false;
}
Optional<Map<String, String>> noteOptional =
NoteShadowAlgorithmUtil.parseSimpleSQLNote(noteShadowValue.getSqlNoteValue());
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java
index e1f934c..6f918f6 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java
@@ -23,6 +23,7 @@ import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmC
import org.apache.shardingsphere.infra.config.checker.RuleConfigurationChecker;
import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import java.util.Collection;
@@ -43,9 +44,11 @@ public abstract class
AbstractShadowRuleConfigurationChecker<T extends RuleConfi
protected abstract void checkShadowRuleConfiguration(T config);
- protected void sizeCheck(final Map<String, ShadowDataSourceConfiguration>
dataSources, final Map<String, ShadowTableConfiguration> shadowTables) {
+ protected void sizeCheck(final Map<String, ShadowDataSourceConfiguration>
dataSources, final Map<String, ShadowTableConfiguration> shadowTables, final
String defaultShadowAlgorithmName) {
Preconditions.checkState(!dataSources.isEmpty(), "No available shadow
data sources mappings in shadow configuration.");
- Preconditions.checkState(!shadowTables.isEmpty(), "No available shadow
tables in shadow configuration.");
+ if (null == defaultShadowAlgorithmName) {
+ Preconditions.checkState(!shadowTables.isEmpty(), "No available
shadow tables in shadow configuration.");
+ }
}
protected void shadowAlgorithmsSizeCheck(final Map<String,
ShadowAlgorithm> shadowAlgorithms) {
@@ -72,8 +75,33 @@ public abstract class
AbstractShadowRuleConfigurationChecker<T extends RuleConfi
});
}
- protected void shadowTableAlgorithmsAutoReferences(final Map<String,
ShadowTableConfiguration> shadowTables, final Set<String> shadowAlgorithmNames)
{
- shadowTables.forEach((key, value) ->
value.getShadowAlgorithmNames().removeIf(each ->
!shadowAlgorithmNames.contains(each)));
+ protected void defaultShadowAlgorithmConfigurationCheck(final String
defaultShadowAlgorithmName, final Map<String,
ShardingSphereAlgorithmConfiguration> shadowAlgorithmConfigurations) {
+ if (null != defaultShadowAlgorithmName) {
+ ShardingSphereAlgorithmConfiguration
shardingSphereAlgorithmConfiguration =
shadowAlgorithmConfigurations.get(defaultShadowAlgorithmName);
+ boolean state = null != shardingSphereAlgorithmConfiguration &&
"SIMPLE_NOTE".equals(shardingSphereAlgorithmConfiguration.getType());
+ Preconditions.checkState(state, "Default shadow algorithm class
should be implement NoteShadowAlgorithm.");
+ }
+ }
+
+ protected void defaultShadowAlgorithmCheck(final String
defaultShadowAlgorithmName, final Map<String, ShadowAlgorithm>
shadowAlgorithms) {
+ if (null != defaultShadowAlgorithmName) {
+ boolean isNoteShadowAlgorithmState =
shadowAlgorithms.get(defaultShadowAlgorithmName) instanceof NoteShadowAlgorithm;
+ Preconditions.checkState(isNoteShadowAlgorithmState, "Default
shadow algorithm class should be implement NoteShadowAlgorithm.");
+ }
+ }
+
+ protected void shadowTableAlgorithmsAutoReferences(final Map<String,
ShadowTableConfiguration> shadowTables, final Set<String> shadowAlgorithmNames,
final String defaultShadowAlgorithmName) {
+ for (Map.Entry<String, ShadowTableConfiguration> entry :
shadowTables.entrySet()) {
+ Collection<String> names =
entry.getValue().getShadowAlgorithmNames();
+ names.forEach(each -> {
+ if (!shadowAlgorithmNames.contains(each)) {
+ names.remove(each);
+ }
+ });
+ if (null != defaultShadowAlgorithmName && names.isEmpty()) {
+ names.add(defaultShadowAlgorithmName);
+ }
+ }
}
protected void shadowTableAlgorithmsReferencesCheck(final Map<String,
ShadowTableConfiguration> shadowTables) {
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java
index 2dff600..1806a7b 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java
@@ -36,11 +36,13 @@ public final class
AlgorithmProvidedShadowRuleConfigurationChecker extends Abstr
Map<String, ShadowDataSourceConfiguration> dataSources =
config.getDataSources();
Map<String, ShadowTableConfiguration> shadowTables =
config.getTables();
Map<String, ShadowAlgorithm> shadowAlgorithms =
config.getShadowAlgorithms();
- sizeCheck(dataSources, shadowTables);
+ String defaultShadowAlgorithmName =
config.getDefaultShadowAlgorithmName();
+ sizeCheck(dataSources, shadowTables, defaultShadowAlgorithmName);
shadowAlgorithmsSizeCheck(shadowAlgorithms);
shadowTableDataSourcesAutoReferences(shadowTables, dataSources);
shadowTableDataSourcesReferencesCheck(shadowTables, dataSources);
- shadowTableAlgorithmsAutoReferences(shadowTables,
shadowAlgorithms.keySet());
+ defaultShadowAlgorithmCheck(defaultShadowAlgorithmName,
shadowAlgorithms);
+ shadowTableAlgorithmsAutoReferences(shadowTables,
shadowAlgorithms.keySet(), defaultShadowAlgorithmName);
shadowTableAlgorithmsReferencesCheck(shadowTables);
}
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
index d335ddd..5b904e6 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
@@ -36,11 +36,13 @@ public final class ShadowRuleConfigurationChecker extends
AbstractShadowRuleConf
Map<String, ShadowDataSourceConfiguration> dataSources =
config.getDataSources();
Map<String, ShadowTableConfiguration> shadowTables =
config.getTables();
Map<String, ShardingSphereAlgorithmConfiguration>
shadowAlgorithmConfigurations = config.getShadowAlgorithms();
- sizeCheck(dataSources, shadowTables);
+ String defaultShadowAlgorithmName =
config.getDefaultShadowAlgorithmName();
+ sizeCheck(dataSources, shadowTables, defaultShadowAlgorithmName);
shadowAlgorithmConfigurationsSizeCheck(shadowAlgorithmConfigurations);
shadowTableDataSourcesAutoReferences(shadowTables, dataSources);
shadowTableDataSourcesReferencesCheck(shadowTables, dataSources);
- shadowTableAlgorithmsAutoReferences(shadowTables,
shadowAlgorithmConfigurations.keySet());
+
defaultShadowAlgorithmConfigurationCheck(defaultShadowAlgorithmName,
shadowAlgorithmConfigurations);
+ shadowTableAlgorithmsAutoReferences(shadowTables,
shadowAlgorithmConfigurations.keySet(), defaultShadowAlgorithmName);
shadowTableAlgorithmsReferencesCheck(shadowTables);
}
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
index f7b8370..2d1238f 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
@@ -46,7 +46,7 @@ public final class ShadowSQLRouter implements
SQLRouter<ShadowRule> {
}
private void doShadowDecorate(final RouteContext routeContext, final
LogicSQL logicSQL, final ShadowRule rule, final ConfigurationProperties props) {
- ShadowRouteEngineFactory.newInstance(logicSQL).route(routeContext,
rule, props);
+ ShadowRouteEngineFactory.newInstance(logicSQL).route(routeContext,
rule);
}
@Override
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
index 53bcba4..ccccafd 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
@@ -17,21 +17,45 @@
package org.apache.shardingsphere.shadow.route.engine;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+
/**
* Shadow route engine.
*/
public interface ShadowRouteEngine {
/**
+ * Shadow route decorate.
+ *
+ * @param routeContext route context
+ * @param shadowDataSourceMappings shadow data source mappings
+ */
+ default void shadowRouteDecorate(final RouteContext routeContext, final
Map<String, String> shadowDataSourceMappings) {
+ Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
+ Collection<RouteUnit> toBeAdded = new LinkedList<>();
+ for (RouteUnit each : routeUnits) {
+ RouteMapper dataSourceMapper = each.getDataSourceMapper();
+ String shadowActualName =
shadowDataSourceMappings.get(dataSourceMapper.getActualName());
+ if (null != shadowActualName) {
+ toBeAdded.add(new RouteUnit(new
RouteMapper(dataSourceMapper.getLogicName(), shadowActualName),
each.getTableMappers()));
+ }
+ }
+ routeUnits.clear();
+ routeUnits.addAll(toBeAdded);
+ }
+
+ /**
* Route.
*
* @param routeContext route context
* @param shadowRule shadow rule
- * @param props configuration properties
*/
- void route(RouteContext routeContext, ShadowRule shadowRule,
ConfigurationProperties props);
+ void route(RouteContext routeContext, ShadowRule shadowRule);
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
index 4c8a5b1..2613aaa 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
@@ -18,11 +18,7 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
import lombok.Getter;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
import
org.apache.shardingsphere.shadow.api.shadow.column.ColumnShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
@@ -31,12 +27,12 @@ import
org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngine;
import
org.apache.shardingsphere.shadow.route.engine.determiner.ShadowDeterminerFactory;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
-import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
@@ -49,25 +45,55 @@ public abstract class AbstractShadowDMLStatementRouteEngine
implements ShadowRou
private final Map<String, String> tableAliasNameMappings = new
LinkedHashMap<>();
@Override
- public void route(final RouteContext routeContext, final ShadowRule
shadowRule, final ConfigurationProperties props) {
- findShadowDataSourceMappings(shadowRule,
props).ifPresent(shadowDataSourceMappings ->
shadowDMLStatementRouteDecorate(routeContext, shadowDataSourceMappings));
+ public void route(final RouteContext routeContext, final ShadowRule
shadowRule) {
+
findShadowDataSourceMappings(shadowRule).ifPresent(shadowDataSourceMappings ->
shadowRouteDecorate(routeContext, shadowDataSourceMappings));
}
- private Optional<Map<String, String>> findShadowDataSourceMappings(final
ShadowRule shadowRule, final ConfigurationProperties props) {
+ private Optional<Map<String, String>> findShadowDataSourceMappings(final
ShadowRule shadowRule) {
Collection<String> relatedShadowTables =
getRelatedShadowTables(getAllTables(), shadowRule);
- boolean sqlCommentParseEnabled =
Boolean.parseBoolean(String.valueOf(props.getProps().get(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey())));
+ if (relatedShadowTables.isEmpty() &&
isMatchDefaultShadowAlgorithm(shadowRule)) {
+ return Optional.of(shadowRule.getAllShadowDataSourceMappings());
+ }
ShadowOperationType shadowOperationType = getShadowOperationType();
for (String each : relatedShadowTables) {
- if (isShadowTable(each, shadowRule, shadowOperationType,
sqlCommentParseEnabled)) {
+ if (isShadowTable(each, shadowRule, shadowOperationType)) {
return shadowRule.getRelatedShadowDataSourceMappings(each);
}
}
return Optional.empty();
}
- private boolean isShadowTable(final String tableName, final ShadowRule
shadowRule, final ShadowOperationType shadowOperationType, final boolean
sqlCommentParseEnabled) {
+ private Collection<String> getRelatedShadowTables(final
Collection<SimpleTableSegment> simpleTableSegments, final ShadowRule
shadowRule) {
+ Collection<String> tableNames = new LinkedHashSet<>();
+ for (SimpleTableSegment each : simpleTableSegments) {
+ String tableName = each.getTableName().getIdentifier().getValue();
+ String alias = each.getAlias().isPresent() ? each.getAlias().get()
: tableName;
+ tableNames.add(tableName);
+ tableAliasNameMappings.put(alias, tableName);
+ }
+ return shadowRule.getRelatedShadowTables(tableNames);
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean isMatchDefaultShadowAlgorithm(final ShadowRule shadowRule)
{
+ Optional<Collection<String>> sqlNotes = parseSqlNotes();
+ if (!sqlNotes.isPresent()) {
+ return false;
+ }
+ Optional<ShadowAlgorithm> defaultShadowAlgorithm =
shadowRule.getDefaultShadowAlgorithm();
+ if (defaultShadowAlgorithm.isPresent()) {
+ ShadowAlgorithm shadowAlgorithm = defaultShadowAlgorithm.get();
+ if (shadowAlgorithm instanceof NoteShadowAlgorithm<?>) {
+ ShadowDetermineCondition shadowDetermineCondition = new
ShadowDetermineCondition("", ShadowOperationType.NOTE_MATCH);
+ return
isMatchNoteShadowAlgorithm((NoteShadowAlgorithm<Comparable<?>>)
shadowAlgorithm, shadowDetermineCondition.initSqlNotes(sqlNotes.get()),
shadowRule);
+ }
+ }
+ return false;
+ }
+
+ private boolean isShadowTable(final String tableName, final ShadowRule
shadowRule, final ShadowOperationType shadowOperationType) {
ShadowDetermineCondition shadowCondition = new
ShadowDetermineCondition(tableName, shadowOperationType);
- if (sqlCommentParseEnabled && isShadowSqlNote(tableName, shadowRule,
shadowCondition)) {
+ if (isShadowSqlNote(tableName, shadowRule, shadowCondition)) {
return true;
}
return isShadowColumn(tableName, shadowRule, shadowCondition);
@@ -110,17 +136,6 @@ public abstract class
AbstractShadowDMLStatementRouteEngine implements ShadowRou
return
ShadowDeterminerFactory.newInstance(columnShadowAlgorithm).isShadow(shadowCondition,
shadowRule);
}
- private Collection<String> getRelatedShadowTables(final
Collection<SimpleTableSegment> simpleTableSegments, final ShadowRule
shadowRule) {
- Collection<String> tableNames = new LinkedHashSet<>();
- for (SimpleTableSegment each : simpleTableSegments) {
- String tableName = each.getTableName().getIdentifier().getValue();
- String alias = each.getAlias().isPresent() ? each.getAlias().get()
: tableName;
- tableNames.add(tableName);
- tableAliasNameMappings.put(alias, tableName);
- }
- return shadowRule.getRelatedShadowTables(tableNames);
- }
-
/**
* Parse shadow column conditions.
*
@@ -157,17 +172,4 @@ public abstract class
AbstractShadowDMLStatementRouteEngine implements ShadowRou
protected String getSingleTableName() {
return tableAliasNameMappings.entrySet().iterator().next().getValue();
}
-
- private void shadowDMLStatementRouteDecorate(final RouteContext
routeContext, final Map<String, String> shadowDataSourceMappings) {
- Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
- Collection<RouteUnit> toBeAdded = new LinkedList<>();
- for (RouteUnit each : routeUnits) {
- RouteMapper routeMapper = each.getDataSourceMapper();
- if (null !=
shadowDataSourceMappings.get(routeMapper.getActualName())) {
- toBeAdded.add(new RouteUnit(new
RouteMapper(routeMapper.getLogicName(),
shadowDataSourceMappings.get(routeMapper.getActualName())),
each.getTableMappers()));
- }
- }
- routeUnits.clear();
- routeUnits.addAll(toBeAdded);
- }
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java
index 1b76fa5..c24b636 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java
@@ -19,11 +19,7 @@ package org.apache.shardingsphere.shadow.route.engine.impl;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
@@ -48,10 +44,8 @@ public final class ShadowNonDMLStatementRoutingEngine
implements ShadowRouteEngi
private final SQLStatementContext<?> sqlStatementContext;
@Override
- public void route(final RouteContext routeContext, final ShadowRule
shadowRule, final ConfigurationProperties props) {
- if
(Boolean.parseBoolean(String.valueOf(props.getProps().get(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey()))))
{
- findShadowDataSourceMappings(shadowRule).ifPresent(stringStringMap
-> shadowNonDMLStatementRouteDecorate(routeContext, stringStringMap));
- }
+ public void route(final RouteContext routeContext, final ShadowRule
shadowRule) {
+ findShadowDataSourceMappings(shadowRule).ifPresent(stringStringMap ->
shadowRouteDecorate(routeContext, stringStringMap));
}
private Optional<Map<String, String>> findShadowDataSourceMappings(final
ShadowRule shadowRule) {
@@ -76,7 +70,7 @@ public final class ShadowNonDMLStatementRoutingEngine
implements ShadowRouteEngi
}
private ShadowDetermineCondition createShadowDetermineCondition(final
Collection<String> sqlNotes) {
- ShadowDetermineCondition result = new ShadowDetermineCondition("",
ShadowOperationType.NON_DML);
+ ShadowDetermineCondition result = new ShadowDetermineCondition("",
ShadowOperationType.NOTE_MATCH);
return result.initSqlNotes(sqlNotes);
}
@@ -92,18 +86,4 @@ public final class ShadowNonDMLStatementRoutingEngine
implements ShadowRouteEngi
private boolean isMatchNoteShadowAlgorithm(final
NoteShadowAlgorithm<Comparable<?>> noteShadowAlgorithm, final
ShadowDetermineCondition shadowCondition, final ShadowRule shadowRule) {
return
ShadowDeterminerFactory.newInstance(noteShadowAlgorithm).isShadow(shadowCondition,
shadowRule);
}
-
- private void shadowNonDMLStatementRouteDecorate(final RouteContext
routeContext, final Map<String, String> shadowDataSourceMappings) {
- Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
- Collection<RouteUnit> toBeAdded = new LinkedList<>();
- for (RouteUnit each : routeUnits) {
- RouteMapper dataSourceMapper = each.getDataSourceMapper();
- String shadowActualName =
shadowDataSourceMappings.get(dataSourceMapper.getActualName());
- if (null != shadowActualName) {
- toBeAdded.add(new RouteUnit(new
RouteMapper(dataSourceMapper.getLogicName(), shadowActualName),
each.getTableMappers()));
- }
- }
- routeUnits.clear();
- routeUnits.addAll(toBeAdded);
- }
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
index ebba2f3..87eadb2 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
@@ -54,6 +54,8 @@ public final class ShadowRule implements SchemaRule,
DataSourceContainedRule {
private final boolean enable;
+ private ShadowAlgorithm defaultShadowAlgorithm;
+
private final Map<String, ShadowDataSourceRule> shadowDataSourceMappings =
new LinkedHashMap<>();
private final Map<String, ShadowAlgorithm> shadowAlgorithms = new
LinkedHashMap<>();
@@ -65,6 +67,7 @@ public final class ShadowRule implements SchemaRule,
DataSourceContainedRule {
if (enable) {
initShadowDataSourceMappings(shadowRuleConfig.getDataSources());
initShadowAlgorithmConfigurations(shadowRuleConfig.getShadowAlgorithms());
+
initDefaultShadowAlgorithm(shadowRuleConfig.getDefaultShadowAlgorithmName());
initShadowTableRules(shadowRuleConfig.getTables());
}
}
@@ -74,24 +77,38 @@ public final class ShadowRule implements SchemaRule,
DataSourceContainedRule {
if (enable) {
initShadowDataSourceMappings(shadowRuleConfig.getDataSources());
initShadowAlgorithms(shadowRuleConfig.getShadowAlgorithms());
+
initDefaultShadowAlgorithm(shadowRuleConfig.getDefaultShadowAlgorithmName());
initShadowTableRules(shadowRuleConfig.getTables());
}
}
- private void initShadowAlgorithmConfigurations(final Map<String,
ShardingSphereAlgorithmConfiguration> shadowAlgorithmConfigurations) {
- shadowAlgorithmConfigurations.forEach((key, value) ->
shadowAlgorithms.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value,
ShadowAlgorithm.class)));
+ private void initShadowDataSourceMappings(final Map<String,
ShadowDataSourceConfiguration> dataSources) {
+ dataSources.forEach((key, value) -> shadowDataSourceMappings.put(key,
new ShadowDataSourceRule(value.getSourceDataSourceName(),
value.getShadowDataSourceName())));
}
- private void initShadowTableRules(final Map<String,
ShadowTableConfiguration> tables) {
- tables.forEach((key, value) -> shadowTableRules.put(key, new
ShadowTableRule(key, value.getDataSourceNames(),
value.getShadowAlgorithmNames(), shadowAlgorithms)));
+ private void initShadowAlgorithmConfigurations(final Map<String,
ShardingSphereAlgorithmConfiguration> shadowAlgorithmConfigurations) {
+ shadowAlgorithmConfigurations.forEach((key, value) ->
shadowAlgorithms.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value,
ShadowAlgorithm.class)));
}
private void initShadowAlgorithms(final Map<String, ShadowAlgorithm>
shadowAlgorithms) {
this.shadowAlgorithms.putAll(shadowAlgorithms);
}
- private void initShadowDataSourceMappings(final Map<String,
ShadowDataSourceConfiguration> dataSources) {
- dataSources.forEach((key, value) -> shadowDataSourceMappings.put(key,
new ShadowDataSourceRule(value.getSourceDataSourceName(),
value.getShadowDataSourceName())));
+ private void initDefaultShadowAlgorithm(final String
defaultShadowAlgorithmName) {
+ defaultShadowAlgorithm =
shadowAlgorithms.get(defaultShadowAlgorithmName);
+ }
+
+ private void initShadowTableRules(final Map<String,
ShadowTableConfiguration> tables) {
+ tables.forEach((key, value) -> shadowTableRules.put(key, new
ShadowTableRule(key, value.getDataSourceNames(),
value.getShadowAlgorithmNames(), shadowAlgorithms)));
+ }
+
+ /**
+ * Get default shadow algorithm.
+ *
+ * @return shadow algorithm
+ */
+ public Optional<ShadowAlgorithm> getDefaultShadowAlgorithm() {
+ return null == defaultShadowAlgorithm ? Optional.empty() :
Optional.of(defaultShadowAlgorithm);
}
/**
@@ -122,6 +139,9 @@ public final class ShadowRule implements SchemaRule,
DataSourceContainedRule {
public Optional<Collection<NoteShadowAlgorithm<Comparable<?>>>>
getAllNoteShadowAlgorithms() {
Collection<NoteShadowAlgorithm<Comparable<?>>> result =
shadowAlgorithms.values().stream().filter(each -> each instanceof
NoteShadowAlgorithm<?>)
.map(each -> (NoteShadowAlgorithm<Comparable<?>>)
each).collect(Collectors.toCollection(LinkedList::new));
+ if (defaultShadowAlgorithm instanceof NoteShadowAlgorithm<?>) {
+ result.add((NoteShadowAlgorithm<Comparable<?>>)
defaultShadowAlgorithm);
+ }
return result.isEmpty() ? Optional.empty() : Optional.of(result);
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/config/YamlShadowRuleConfiguration.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/config/YamlShadowRuleConfiguration.java
index 6f58320..25d49ba 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/config/YamlShadowRuleConfiguration.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/config/YamlShadowRuleConfiguration.java
@@ -37,6 +37,8 @@ public final class YamlShadowRuleConfiguration implements
YamlRuleConfiguration
private boolean enable;
+ private String defaultShadowAlgorithmName;
+
private Map<String, YamlShadowDataSourceConfiguration> dataSources = new
LinkedHashMap<>();
private Map<String, YamlShadowTableConfiguration> tables = new
LinkedHashMap<>();
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleAlgorithmProviderConfigurationYamlSwapper.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleAlgorithmProviderConfigurationYamlSwapper.java
index ca2bb54..0ca7d5b 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleAlgorithmProviderConfigurationYamlSwapper.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -38,6 +38,7 @@ public final class
ShadowRuleAlgorithmProviderConfigurationYamlSwapper implement
public YamlShadowRuleConfiguration swapToYamlConfiguration(final
AlgorithmProvidedShadowRuleConfiguration dataConfiguration) {
YamlShadowRuleConfiguration result = new YamlShadowRuleConfiguration();
result.setEnable(dataConfiguration.isEnable());
+
result.setDefaultShadowAlgorithmName(dataConfiguration.getDefaultShadowAlgorithmName());
parseDataSources(dataConfiguration, result);
parseShadowTables(dataConfiguration, result);
parseShadowAlgorithms(dataConfiguration, result);
@@ -61,6 +62,7 @@ public final class
ShadowRuleAlgorithmProviderConfigurationYamlSwapper implement
public AlgorithmProvidedShadowRuleConfiguration swapToObject(final
YamlShadowRuleConfiguration yamlConfiguration) {
AlgorithmProvidedShadowRuleConfiguration result = new
AlgorithmProvidedShadowRuleConfiguration();
result.setEnable(yamlConfiguration.isEnable());
+
result.setDefaultShadowAlgorithmName(yamlConfiguration.getDefaultShadowAlgorithmName());
parseYamlDataSources(yamlConfiguration, result);
parseYamlShadowTables(yamlConfiguration, result);
return result;
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java
index 86d2a34..24ea6de 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java
@@ -40,6 +40,7 @@ public final class ShadowRuleConfigurationYamlSwapper
implements YamlRuleConfigu
public YamlShadowRuleConfiguration swapToYamlConfiguration(final
ShadowRuleConfiguration dataConfiguration) {
YamlShadowRuleConfiguration result = new YamlShadowRuleConfiguration();
result.setEnable(dataConfiguration.isEnable());
+
result.setDefaultShadowAlgorithmName(dataConfiguration.getDefaultShadowAlgorithmName());
parseDataSources(dataConfiguration, result);
parseShadowTables(dataConfiguration, result);
parseShadowAlgorithms(dataConfiguration, result);
@@ -62,6 +63,7 @@ public final class ShadowRuleConfigurationYamlSwapper
implements YamlRuleConfigu
public ShadowRuleConfiguration swapToObject(final
YamlShadowRuleConfiguration yamlConfiguration) {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.setEnable(yamlConfiguration.isEnable());
+
result.setDefaultShadowAlgorithmName(yamlConfiguration.getDefaultShadowAlgorithmName());
parseYamlDataSources(yamlConfiguration, result);
parseYamlShadowTables(yamlConfiguration, result);
parseYamlShadowAlgorithms(yamlConfiguration, result);
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithmTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithmTest.java
index 156547f..bf5d922 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithmTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/note/SimpleSQLNoteShadowAlgorithmTest.java
@@ -67,7 +67,7 @@ public final class SimpleSQLNoteShadowAlgorithmTest {
}
private PreciseNoteShadowValue<String> createNoteShadowValue(final String
sqlNote) {
- return new PreciseNoteShadowValue<>("t_user",
ShadowOperationType.NON_DML, sqlNote);
+ return new PreciseNoteShadowValue<>("t_user",
ShadowOperationType.NOTE_MATCH, sqlNote);
}
private Collection<String> createShadowTableNames() {
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
index f73fe3c..a1d0195 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
@@ -18,8 +18,6 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
import
org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementContext;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
@@ -90,9 +88,7 @@ public final class ShadowDeleteStatementRoutingEngineTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds", "ds_shadow"), new
LinkedList<>()));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- Properties props = new Properties();
-
props.setProperty(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey(),
String.valueOf(Boolean.TRUE));
- shadowDeleteStatementRoutingEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()), new
ConfigurationProperties(props));
+ shadowDeleteStatementRoutingEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
Optional<Collection<ShadowColumnCondition>> shadowColumnConditions =
shadowDeleteStatementRoutingEngine.parseShadowColumnConditions();
assertThat(shadowColumnConditions.isPresent(), is(true));
Collection<ShadowColumnCondition> shadowColumns =
shadowColumnConditions.get();
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngineTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngineTest.java
index 97db196..83d626d 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngineTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngineTest.java
@@ -19,8 +19,6 @@ package org.apache.shardingsphere.shadow.route.engine.dml;
import
org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
@@ -95,9 +93,7 @@ public final class ShadowInsertStatementRoutingEngineTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds", "ds_shadow"), new
LinkedList<>()));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- Properties props = new Properties();
-
props.setProperty(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey(),
String.valueOf(Boolean.TRUE));
- shadowRouteEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()), new
ConfigurationProperties(props));
+ shadowRouteEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
Optional<Collection<ShadowColumnCondition>> shadowColumnConditions =
shadowRouteEngine.parseShadowColumnConditions();
assertThat(shadowColumnConditions.isPresent(), is(true));
Collection<ShadowColumnCondition> shadowColumns =
shadowColumnConditions.get();
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngineTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngineTest.java
index d3d81fe..6cb5659 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngineTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngineTest.java
@@ -18,8 +18,6 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
@@ -90,9 +88,7 @@ public final class ShadowSelectStatementRoutingEngineTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds", "ds_shadow"), new
LinkedList<>()));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- Properties props = new Properties();
-
props.setProperty(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey(),
String.valueOf(Boolean.TRUE));
- shadowRouteEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()), new
ConfigurationProperties(props));
+ shadowRouteEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
Optional<Collection<ShadowColumnCondition>> shadowColumnConditions =
shadowRouteEngine.parseShadowColumnConditions();
assertThat(shadowColumnConditions.isPresent(), is(true));
Collection<ShadowColumnCondition> shadowColumns =
shadowColumnConditions.get();
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
index 06d969e..ef0a234 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
@@ -18,8 +18,6 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
import
org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
@@ -91,9 +89,7 @@ public final class ShadowUpdateStatementRoutingEngineTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds", "ds_shadow"), new
LinkedList<>()));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- Properties props = new Properties();
-
props.setProperty(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey(),
String.valueOf(Boolean.TRUE));
- shadowRouteEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()), new
ConfigurationProperties(props));
+ shadowRouteEngine.route(routeContext, new
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
Optional<Collection<ShadowColumnCondition>> shadowColumnConditions =
shadowRouteEngine.parseShadowColumnConditions();
assertTrue(shadowColumnConditions.isPresent());
Collection<ShadowColumnCondition> shadowColumns =
shadowColumnConditions.get();
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/test/java/org/apache/shardingsphere/shadow/spring/boot/YmlShadowDefaultSpringBootStarterTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/test/java/org/apache/shardingsphere/shadow/spring/boot/YmlShadowDefaultSpringBootStarterTest.java
new file mode 100644
index 0000000..d3a31bd
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/test/java/org/apache/shardingsphere/shadow/spring/boot/YmlShadowDefaultSpringBootStarterTest.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.shadow.spring.boot;
+
+import
org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
+import
org.apache.shardingsphere.shadow.algorithm.shadow.note.SimpleSQLNoteShadowAlgorithm;
+import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.annotation.Resource;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(classes = YmlShadowDefaultSpringBootStarterTest.class)
+@SpringBootApplication
+@ActiveProfiles("shadow-default")
+public class YmlShadowDefaultSpringBootStarterTest {
+
+ @Resource
+ private AlgorithmProvidedShadowRuleConfiguration shadowRuleConfiguration;
+
+ @Test
+ public void assertShadowRuleConfiguration() {
+ assertThat(shadowRuleConfiguration.isEnable(), is(true));
+ assertShadowDataSources(shadowRuleConfiguration.getDataSources());
+ assertShadowTables(shadowRuleConfiguration.getTables());
+
assertDefaultShadowAlgorithm(shadowRuleConfiguration.getDefaultShadowAlgorithmName());
+ assertShadowAlgorithms(shadowRuleConfiguration.getShadowAlgorithms());
+ }
+
+ private void assertDefaultShadowAlgorithm(final String
defaultShadowAlgorithmName) {
+ assertThat("simple-note-algorithm".equals(defaultShadowAlgorithmName),
is(true));
+ }
+
+ private void assertShadowAlgorithms(final Map<String, ShadowAlgorithm>
shadowAlgorithms) {
+ ShadowAlgorithm simpleNoteAlgorithm =
shadowAlgorithms.get("simple-note-algorithm");
+ assertThat(simpleNoteAlgorithm instanceof
SimpleSQLNoteShadowAlgorithm, is(true));
+ assertThat(simpleNoteAlgorithm.getType(), is("SIMPLE_NOTE"));
+ assertThat(simpleNoteAlgorithm.getProps().get("shadow"), is(true));
+ assertThat(simpleNoteAlgorithm.getProps().get("foo"), is("bar"));
+ }
+
+ private void assertShadowTables(final Map<String,
ShadowTableConfiguration> shadowTables) {
+ assertThat(shadowTables.size(), is(0));
+ }
+
+ private void assertShadowDataSources(final Map<String,
ShadowDataSourceConfiguration> dataSources) {
+ assertThat(dataSources.size(), is(1));
+
assertThat(dataSources.get("shadow-data-source").getSourceDataSourceName(),
is("ds"));
+
assertThat(dataSources.get("shadow-data-source").getShadowDataSourceName(),
is("ds-shadow"));
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/test/resources/application-shadow-default.yml
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/test/resources/application-shadow-default.yml
new file mode 100644
index 0000000..60c14fd
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/test/resources/application-shadow-default.yml
@@ -0,0 +1,33 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+spring:
+ shardingsphere:
+ rules:
+ shadow:
+ enable: true
+ data-sources:
+ shadow-data-source:
+ source-data-source-name: ds
+ shadow-data-source-name: ds-shadow
+ default-shadow-algorithm-name: simple-note-algorithm
+ shadow-algorithms:
+ simple-note-algorithm:
+ type: SIMPLE_NOTE
+ props:
+ shadow: true
+ foo: bar
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/parser/ShadowRuleBeanDefinitionParser.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/parser/ShadowRuleBeanDefinitionParser.java
index 78c5032..3454407 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/parser/ShadowRuleBeanDefinitionParser.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/parser/ShadowRuleBeanDefinitionParser.java
@@ -57,6 +57,7 @@ public final class ShadowRuleBeanDefinitionParser extends
AbstractBeanDefinition
factory.addPropertyValue("enable",
parseShadowEnableConfiguration(element));
factory.addPropertyValue("dataSources",
parseDataSourcesConfiguration(element));
factory.addPropertyValue("tables",
parseShadowTablesConfiguration(element));
+ factory.addPropertyValue("defaultShadowAlgorithmName",
parseDefaultShadowAlgorithmName(element));
factory.addPropertyValue("shadowAlgorithms",
ShardingSphereAlgorithmBeanRegistry.getAlgorithmBeanReferences(parserContext,
ShadowAlgorithmFactoryBean.class));
return factory.getBeanDefinition();
}
@@ -69,11 +70,19 @@ public final class ShadowRuleBeanDefinitionParser extends
AbstractBeanDefinition
List<Element> tableRuleElements =
DomUtils.getChildElementsByTagName(element,
ShadowRuleBeanDefinitionTag.SHADOW_TABLE_TAG);
Map<String, BeanDefinition> result = new
ManagedMap<>(tableRuleElements.size());
for (Element each : tableRuleElements) {
-
result.put(each.getAttribute(ShadowRuleBeanDefinitionTag.SHADOW_TABLE_NAME_ATTRIBUTE),
parseShadowTableConfiguration(each));
+
result.put(each.getAttribute(ShadowRuleBeanDefinitionTag.SHADOW_NAME_ATTRIBUTE),
parseShadowTableConfiguration(each));
}
return result;
}
+ private String parseDefaultShadowAlgorithmName(final Element element) {
+ Element defaultShadowAlgorithmElement =
DomUtils.getChildElementByTagName(element,
ShadowRuleBeanDefinitionTag.SHADOW_DEFAULT_SHADOW_ALGORITHM_NAME);
+ if (null == defaultShadowAlgorithmElement) {
+ return null;
+ }
+ return
defaultShadowAlgorithmElement.getAttribute(ShadowRuleBeanDefinitionTag.SHADOW_NAME_ATTRIBUTE);
+ }
+
private BeanDefinition parseShadowTableConfiguration(final Element
element) {
BeanDefinitionBuilder factory =
BeanDefinitionBuilder.rootBeanDefinition(ShadowTableConfiguration.class);
factory.addConstructorArgValue(parseTableDataSourcesAttribute(element));
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/tag/ShadowRuleBeanDefinitionTag.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/tag/ShadowRuleBeanDefinitionTag.java
index 22c9ee5..b8c0fca 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/tag/ShadowRuleBeanDefinitionTag.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/java/org/apache/shardingsphere/shadow/spring/namespace/tag/ShadowRuleBeanDefinitionTag.java
@@ -34,13 +34,15 @@ public final class ShadowRuleBeanDefinitionTag {
public static final String DATA_SOURCE_ID_ATTRIBUTE = "id";
+ public static final String SHADOW_NAME_ATTRIBUTE = "name";
+
public static final String SOURCE_DATA_SOURCE_NAME_ATTRIBUTE =
"source-data-source-name";
public static final String SHADOW_DATA_SOURCE_NAME_ATTRIBUTE =
"shadow-data-source-name";
public static final String SHADOW_TABLE_TAG = "shadow-table";
- public static final String SHADOW_TABLE_NAME_ATTRIBUTE = "name";
+ public static final String SHADOW_DEFAULT_SHADOW_ALGORITHM_NAME =
"default-shadow-algorithm-name";
public static final String SHADOW_TABLE_DATA_SOURCE_REFS_ATTRIBUTE =
"data-sources";
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/resources/META-INF/namespace/shadow.xsd
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/resources/META-INF/namespace/shadow.xsd
index 1b68788..ce633cd 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/resources/META-INF/namespace/shadow.xsd
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/main/resources/META-INF/namespace/shadow.xsd
@@ -27,8 +27,9 @@
<xsd:element name="rule">
<xsd:complexType>
<xsd:sequence>
- <xsd:element ref="data-source" minOccurs="0"
maxOccurs="unbounded" />
+ <xsd:element ref="data-source" maxOccurs="unbounded" />
<xsd:element ref="shadow-table" minOccurs="0"
maxOccurs="unbounded" />
+ <xsd:element ref="default-shadow-algorithm-name" minOccurs="0"
maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required"/>
<xsd:attribute name="enable" type="xsd:boolean"/>
@@ -59,6 +60,12 @@
</xsd:complexType>
</xsd:element>
+ <xsd:element name="default-shadow-algorithm-name">
+ <xsd:complexType>
+ <xsd:attribute name="name" type="xsd:string" use="required"/>
+ </xsd:complexType>
+ </xsd:element>
+
<xsd:element name="shadow-algorithm">
<xsd:complexType>
<xsd:all>
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/test/java/org/apache/shardingsphere/shadow/spring/namespace/ShadowDefaultAlgorithmSpringNamespaceTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/test/java/org/apache/shardingsphere/shadow/spring/namespace/ShadowDefaultAlgorithmSpringNamespaceTest.java
new file mode 100644
index 0000000..288078d
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/test/java/org/apache/shardingsphere/shadow/spring/namespace/ShadowDefaultAlgorithmSpringNamespaceTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.shadow.spring.namespace;
+
+import
org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
+import
org.apache.shardingsphere.shadow.algorithm.shadow.note.SimpleSQLNoteShadowAlgorithm;
+import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import org.junit.Test;
+import org.springframework.test.context.ContextConfiguration;
+import
org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+import javax.annotation.Resource;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+@ContextConfiguration(locations =
"classpath:META-INF/spring/shadow-default-algorithm-application-context.xml")
+public final class ShadowDefaultAlgorithmSpringNamespaceTest extends
AbstractJUnit4SpringContextTests {
+
+ @Resource
+ private AlgorithmProvidedShadowRuleConfiguration shadowRule;
+
+ @Test
+ public void assertDataSource() {
+ assertThat(shadowRule.isEnable(), is(true));
+ assertShadowDataSources(shadowRule.getDataSources());
+ assertShadowTables(shadowRule.getTables());
+
assertDefaultShadowAlgorithm(shadowRule.getDefaultShadowAlgorithmName());
+ assertShadowAlgorithms(shadowRule.getShadowAlgorithms());
+ }
+
+ private void assertDefaultShadowAlgorithm(final String
defaultShadowAlgorithmName) {
+ assertThat("simple-note-algorithm".equals(defaultShadowAlgorithmName),
is(true));
+ }
+
+ private void assertShadowAlgorithms(final Map<String, ShadowAlgorithm>
shadowAlgorithms) {
+ ShadowAlgorithm simpleNoteAlgorithm =
shadowAlgorithms.get("simple-note-algorithm");
+ assertThat(simpleNoteAlgorithm instanceof
SimpleSQLNoteShadowAlgorithm, is(true));
+ assertThat(simpleNoteAlgorithm.getType(), is("SIMPLE_NOTE"));
+ assertThat(simpleNoteAlgorithm.getProps().get("shadow"), is("true"));
+ assertThat(simpleNoteAlgorithm.getProps().get("foo"), is("bar"));
+ }
+
+ private void assertShadowTables(final Map<String,
ShadowTableConfiguration> shadowTables) {
+ assertThat(shadowTables.size(), is(0));
+ }
+
+ private void assertShadowDataSources(final Map<String,
ShadowDataSourceConfiguration> dataSources) {
+ assertThat(dataSources.size(), is(1));
+
assertThat(dataSources.get("shadow-data-source").getSourceDataSourceName(),
is("ds"));
+
assertThat(dataSources.get("shadow-data-source").getShadowDataSourceName(),
is("ds-shadow"));
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/test/resources/META-INF/spring/shadow-default-algorithm-application-context.xml
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/test/resources/META-INF/spring/shadow-default-algorithm-application-context.xml
new file mode 100644
index 0000000..8b194b3
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-namespace/src/test/resources/META-INF/spring/shadow-default-algorithm-application-context.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xmlns:shadow="http://shardingsphere.apache.org/schema/shardingsphere/shadow"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+
http://www.springframework.org/schema/beans/spring-beans.xsd
+
http://shardingsphere.apache.org/schema/shardingsphere/shadow
+
http://shardingsphere.apache.org/schema/shardingsphere/shadow/shadow.xsd
+ ">
+ <shadow:shadow-algorithm id="simple-note-algorithm" type="SIMPLE_NOTE">
+ <props>
+ <prop key="shadow">true</prop>
+ <prop key="foo">bar</prop>
+ </props>
+ </shadow:shadow-algorithm>
+
+ <shadow:rule id="shadowRule" enable="true">
+ <shadow:data-source id="shadow-data-source"
source-data-source-name="ds" shadow-data-source-name="ds-shadow"/>
+ <shadow:default-shadow-algorithm-name name="simple-note-algorithm"/>
+ </shadow:rule>
+</beans>