This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 86139abb2fb Refactor ShadowRouteEngine (#20342)
86139abb2fb is described below
commit 86139abb2fbe4a608353b55f0586444f8d34c8cc
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 21 23:58:03 2022 +0800
Refactor ShadowRouteEngine (#20342)
---
.../shadow/route/engine/ShadowRouteEngine.java | 43 +++++++++++-----------
.../dml/AbstractShadowDMLStatementRouteEngine.java | 4 +-
.../impl/ShadowNonDMLStatementRoutingEngine.java | 2 +-
3 files changed, 24 insertions(+), 25 deletions(-)
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 927b3b94c6d..ca196d0714f 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
@@ -33,37 +33,36 @@ import java.util.Optional;
public interface ShadowRouteEngine {
/**
- * Shadow route decorate.
+ * Route.
*
- * @param routeContext route context
- * @param shadowRule shadow rule
+ * @param routeContext route context
+ * @param shadowRule shadow rule
+ */
+ void route(RouteContext routeContext, ShadowRule shadowRule);
+
+ /**
+ * Decorate route context.
+ *
+ * @param routeContext route context to be decorated
+ * @param shadowRule shadow rule
* @param shadowDataSourceMappings shadow data source mappings
*/
- default void shadowRouteDecorate(final RouteContext routeContext, final
ShadowRule shadowRule, final Map<String, String> shadowDataSourceMappings) {
- Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
- Collection<RouteUnit> toBeRemoved = new LinkedList<>();
- Collection<RouteUnit> toBeAdded = new LinkedList<>();
- for (RouteUnit each : routeUnits) {
+ default void decorateRouteContext(final RouteContext routeContext, final
ShadowRule shadowRule, final Map<String, String> shadowDataSourceMappings) {
+ Collection<RouteUnit> toBeRemovedRouteUnit = new LinkedList<>();
+ Collection<RouteUnit> toBeAddedRouteUnit = new LinkedList<>();
+ for (RouteUnit each : routeContext.getRouteUnits()) {
String logicName = each.getDataSourceMapper().getLogicName();
- String shadowLogicName =
each.getDataSourceMapper().getActualName();
- Optional<String> sourceDataSourceName =
shadowRule.getSourceDataSourceName(shadowLogicName);
+ String actualName = each.getDataSourceMapper().getActualName();
+ Optional<String> sourceDataSourceName =
shadowRule.getSourceDataSourceName(actualName);
if (sourceDataSourceName.isPresent()) {
String shadowDataSourceName =
shadowDataSourceMappings.get(sourceDataSourceName.get());
- toBeRemoved.add(each);
- toBeAdded.add(null == shadowDataSourceName
+ toBeRemovedRouteUnit.add(each);
+ toBeAddedRouteUnit.add(null == shadowDataSourceName
? new RouteUnit(new RouteMapper(logicName,
sourceDataSourceName.get()), each.getTableMappers())
: new RouteUnit(new RouteMapper(logicName,
shadowDataSourceName), each.getTableMappers()));
}
}
- routeUnits.removeAll(toBeRemoved);
- routeUnits.addAll(toBeAdded);
+ routeContext.getRouteUnits().removeAll(toBeRemovedRouteUnit);
+ routeContext.getRouteUnits().addAll(toBeAddedRouteUnit);
}
-
- /**
- * Route.
- *
- * @param routeContext route context
- * @param shadowRule shadow rule
- */
- 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 32f001446fe..41b8ffe22d1 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
@@ -41,14 +41,14 @@ import java.util.Optional;
/**
* Abstract shadow DML statement route engine.
*/
+@Getter
public abstract class AbstractShadowDMLStatementRouteEngine implements
ShadowRouteEngine {
- @Getter
private final Map<String, String> tableAliasNameMappings = new
LinkedHashMap<>();
@Override
public void route(final RouteContext routeContext, final ShadowRule
shadowRule) {
- shadowRouteDecorate(routeContext, shadowRule,
findShadowDataSourceMappings(shadowRule));
+ decorateRouteContext(routeContext, shadowRule,
findShadowDataSourceMappings(shadowRule));
}
private Map<String, String> findShadowDataSourceMappings(final ShadowRule
shadowRule) {
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 82dbf193544..e6ddc1a7a74 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
@@ -45,7 +45,7 @@ public final class ShadowNonDMLStatementRoutingEngine
implements ShadowRouteEngi
@Override
public void route(final RouteContext routeContext, final ShadowRule
shadowRule) {
- shadowRouteDecorate(routeContext, shadowRule,
findShadowDataSourceMappings(shadowRule));
+ decorateRouteContext(routeContext, shadowRule,
findShadowDataSourceMappings(shadowRule));
}
private Map<String, String> findShadowDataSourceMappings(final ShadowRule
shadowRule) {