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  import java.util.stream.Collectors;
33  
34  /**
35   * Value of sharding route cache.
36   */
37  @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
38  public final class ShardingRouteCacheValue {
39      
40      private final boolean cacheable;
41      
42      private final RouteContext cachedRouteContext;
43      
44      public ShardingRouteCacheValue(final RouteContext routeContext) {
45          this(null != routeContext, routeContext);
46      }
47      
48      /**
49       * Get cached route context.
50       *
51       * @return optional cached route context
52       */
53      public Optional<RouteContext> getCachedRouteContext() {
54          return cacheable ? Optional.of(deepCopyRouteContext()) : Optional.empty();
55      }
56      
57      private RouteContext deepCopyRouteContext() {
58          RouteContext result = new RouteContext();
59          result.getOriginalDataNodes().addAll(deepCopyOriginalDataNodes());
60          result.getRouteUnits().addAll(deepCopyRouteUnits());
61          result.getRouteStageContexts().putAll(deepCopyRouteStageContext());
62          return result;
63      }
64      
65      private Collection<Collection<DataNode>> deepCopyOriginalDataNodes() {
66          Collection<Collection<DataNode>> result = new ArrayList<>(cachedRouteContext.getOriginalDataNodes().size());
67          for (Collection<DataNode> eachDataNodes : cachedRouteContext.getOriginalDataNodes()) {
68              result.add(eachDataNodes.stream().map(each -> new DataNode(each.getDataSourceName(), each.getSchemaName(), each.getTableName())).collect(Collectors.toList()));
69          }
70          return result;
71      }
72      
73      private Collection<RouteUnit> deepCopyRouteUnits() {
74          Collection<RouteUnit> result = new ArrayList<>(cachedRouteContext.getRouteUnits().size());
75          for (RouteUnit each : cachedRouteContext.getRouteUnits()) {
76              result.add(new RouteUnit(each.getDataSourceMapper(), new ArrayList<>(each.getTableMappers())));
77          }
78          return result;
79      }
80      
81      private Map<Class<? extends ShardingSphereRule>, ? extends RouteStageContext> deepCopyRouteStageContext() {
82          // TODO Implements deep copy for route stage contexts
83          return cachedRouteContext.getRouteStageContexts();
84      }
85  }