From d9c9ccce1d553b4318e1d8d6f7a32a68bab57965 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Mon, 4 Nov 2024 00:45:28 +0800 Subject: [PATCH] Refactor ShadowDataSourceMappingsFinderFactory --- .../shadow/route/ShadowSQLRouter.java | 32 ++++++++-- .../route/engine/ShadowRouteEngine.java | 63 ------------------- 2 files changed, 28 insertions(+), 67 deletions(-) delete mode 100644 features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java index 854721b29ca8f..24577ab0709e4 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java @@ -22,24 +22,48 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.route.DecorateSQLRouter; 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.infra.session.query.QueryContext; import org.apache.shardingsphere.shadow.constant.ShadowOrder; +import org.apache.shardingsphere.shadow.route.engine.finder.ShadowDataSourceMappingsFinder; import org.apache.shardingsphere.shadow.route.engine.finder.ShadowDataSourceMappingsFinderFactory; -import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngine; import org.apache.shardingsphere.shadow.rule.ShadowRule; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; +import java.util.Optional; + /** * Shadow SQL router. */ @HighFrequencyInvocation public final class ShadowSQLRouter implements DecorateSQLRouter { - private final ShadowRouteEngine routeEngine = new ShadowRouteEngine(); - @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, final ShadowRule rule, final ConfigurationProperties props) { - routeEngine.route(routeContext, rule, ShadowDataSourceMappingsFinderFactory.newInstance(queryContext)); + route(routeContext, rule, ShadowDataSourceMappingsFinderFactory.newInstance(queryContext)); + } + + private void route(final RouteContext routeContext, final ShadowRule rule, final ShadowDataSourceMappingsFinder finder) { + Collection toBeRemovedRouteUnit = new LinkedList<>(); + Collection toBeAddedRouteUnit = new LinkedList<>(); + Map shadowDataSourceMappings = finder.find(rule); + for (RouteUnit each : routeContext.getRouteUnits()) { + String logicName = each.getDataSourceMapper().getLogicName(); + String actualName = each.getDataSourceMapper().getActualName(); + Optional productionDataSourceName = rule.findProductionDataSourceName(actualName); + if (productionDataSourceName.isPresent()) { + String shadowDataSourceName = shadowDataSourceMappings.get(productionDataSourceName.get()); + toBeRemovedRouteUnit.add(each); + String dataSourceName = null == shadowDataSourceName ? productionDataSourceName.get() : shadowDataSourceName; + toBeAddedRouteUnit.add(new RouteUnit(new RouteMapper(logicName, dataSourceName), each.getTableMappers())); + } + } + routeContext.getRouteUnits().removeAll(toBeRemovedRouteUnit); + routeContext.getRouteUnits().addAll(toBeAddedRouteUnit); } @Override diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java deleted file mode 100644 index 1dae3f8a86725..0000000000000 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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.route.engine; - -import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; -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.route.engine.finder.ShadowDataSourceMappingsFinder; -import org.apache.shardingsphere.shadow.rule.ShadowRule; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.Map; -import java.util.Optional; - -/** - * Shadow route engine. - */ -@HighFrequencyInvocation -public final class ShadowRouteEngine { - - /** - * Route. - * - * @param routeContext route context - * @param rule shadow rule - * @param finder finder - */ - public void route(final RouteContext routeContext, final ShadowRule rule, final ShadowDataSourceMappingsFinder finder) { - Collection toBeRemovedRouteUnit = new LinkedList<>(); - Collection toBeAddedRouteUnit = new LinkedList<>(); - Map shadowDataSourceMappings = finder.find(rule); - for (RouteUnit each : routeContext.getRouteUnits()) { - String logicName = each.getDataSourceMapper().getLogicName(); - String actualName = each.getDataSourceMapper().getActualName(); - Optional productionDataSourceName = rule.findProductionDataSourceName(actualName); - if (productionDataSourceName.isPresent()) { - String shadowDataSourceName = shadowDataSourceMappings.get(productionDataSourceName.get()); - toBeRemovedRouteUnit.add(each); - String dataSourceName = null == shadowDataSourceName ? productionDataSourceName.get() : shadowDataSourceName; - toBeAddedRouteUnit.add(new RouteUnit(new RouteMapper(logicName, dataSourceName), each.getTableMappers())); - } - } - routeContext.getRouteUnits().removeAll(toBeRemovedRouteUnit); - routeContext.getRouteUnits().addAll(toBeAddedRouteUnit); - } -}