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.sharding.cache.route.cache;
19  
20  import lombok.AccessLevel;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.infra.datanode.DataNode;
23  import org.apache.shardingsphere.infra.route.context.RouteContext;
24  import org.apache.shardingsphere.infra.route.context.RouteStageContext;
25  import org.apache.shardingsphere.infra.route.context.RouteUnit;
26  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Map;
31  import java.util.Optional;
32  
33  /**
34   * Value of sharding route cache.
35   */
36  @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
37  public final class ShardingRouteCacheValue {
38      
39      private final boolean cacheable;
40      
41      private final RouteContext cachedRouteContext;
42      
43      public ShardingRouteCacheValue(final RouteContext routeContext) {
44          this(null != routeContext, routeContext);
45      }
46      
47      /**
48       * Get cached route context.
49       *
50       * @return optional cached route context
51       */
52      public Optional<RouteContext> getCachedRouteContext() {
53          return cacheable ? Optional.of(deepCopyRouteContext()) : Optional.empty();
54      }
55      
56      private RouteContext deepCopyRouteContext() {
57          RouteContext result = new RouteContext();
58          result.getOriginalDataNodes().addAll(deepCopyOriginalDataNodes());
59          result.getRouteUnits().addAll(deepCopyRouteUnits());
60          result.getRouteStageContexts().putAll(deepCopyRouteStageContext());
61          return result;
62      }
63      
64      private Collection<Collection<DataNode>> deepCopyOriginalDataNodes() {
65          Collection<Collection<DataNode>> result = new ArrayList<>(cachedRouteContext.getOriginalDataNodes().size());
66          for (Collection<DataNode> eachDataNodes : cachedRouteContext.getOriginalDataNodes()) {
67              Collection<DataNode> eachResult = new ArrayList<>(eachDataNodes.size());
68              // TODO This could be simplified if all fields of DataNode were immutable
69              for (DataNode each : eachDataNodes) {
70                  DataNode copiedDataNode = new DataNode(each.getDataSourceName(), each.getTableName());
71                  copiedDataNode.setSchemaName(each.getSchemaName());
72                  eachResult.add(copiedDataNode);
73              }
74              result.add(eachResult);
75          }
76          return result;
77      }
78      
79      private Collection<RouteUnit> deepCopyRouteUnits() {
80          Collection<RouteUnit> result = new ArrayList<>(cachedRouteContext.getRouteUnits().size());
81          for (RouteUnit each : cachedRouteContext.getRouteUnits()) {
82              result.add(new RouteUnit(each.getDataSourceMapper(), new ArrayList<>(each.getTableMappers())));
83          }
84          return result;
85      }
86      
87      private Map<Class<? extends ShardingSphereRule>, ? extends RouteStageContext> deepCopyRouteStageContext() {
88          // TODO Implements deep copy for route stage contexts
89          return cachedRouteContext.getRouteStageContexts();
90      }
91  }