View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.shadow.route.engine;
19  
20  import org.apache.shardingsphere.infra.route.context.RouteContext;
21  import org.apache.shardingsphere.infra.route.context.RouteMapper;
22  import org.apache.shardingsphere.infra.route.context.RouteUnit;
23  import org.apache.shardingsphere.shadow.rule.ShadowRule;
24  
25  import java.util.Collection;
26  import java.util.LinkedList;
27  import java.util.Map;
28  import java.util.Optional;
29  
30  /**
31   * Shadow route engine.
32   */
33  public interface ShadowRouteEngine {
34      
35      /**
36       * Route.
37       *
38       * @param routeContext route context
39       * @param rule shadow rule
40       */
41      void route(RouteContext routeContext, ShadowRule rule);
42      
43      /**
44       * Decorate route context.
45       *
46       * @param routeContext route context to be decorated
47       * @param rule shadow rule
48       * @param shadowDataSourceMappings shadow data source mappings
49       */
50      default void decorateRouteContext(final RouteContext routeContext, final ShadowRule rule, final Map<String, String> shadowDataSourceMappings) {
51          Collection<RouteUnit> toBeRemovedRouteUnit = new LinkedList<>();
52          Collection<RouteUnit> toBeAddedRouteUnit = new LinkedList<>();
53          for (RouteUnit each : routeContext.getRouteUnits()) {
54              String logicName = each.getDataSourceMapper().getLogicName();
55              String actualName = each.getDataSourceMapper().getActualName();
56              Optional<String> sourceDataSourceName = rule.getSourceDataSourceName(actualName);
57              if (sourceDataSourceName.isPresent()) {
58                  String shadowDataSourceName = shadowDataSourceMappings.get(sourceDataSourceName.get());
59                  toBeRemovedRouteUnit.add(each);
60                  toBeAddedRouteUnit.add(null == shadowDataSourceName
61                          ? new RouteUnit(new RouteMapper(logicName, sourceDataSourceName.get()), each.getTableMappers())
62                          : new RouteUnit(new RouteMapper(logicName, shadowDataSourceName), each.getTableMappers()));
63              }
64          }
65          routeContext.getRouteUnits().removeAll(toBeRemovedRouteUnit);
66          routeContext.getRouteUnits().addAll(toBeAddedRouteUnit);
67      }
68  }