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.broadcast.route;
19  
20  import org.apache.shardingsphere.broadcast.constant.BroadcastOrder;
21  import org.apache.shardingsphere.broadcast.route.engine.BroadcastRouteEngineFactory;
22  import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
23  import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
24  import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
25  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
26  import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
27  import org.apache.shardingsphere.infra.route.context.RouteContext;
28  import org.apache.shardingsphere.infra.route.lifecycle.EntranceSQLRouter;
29  import org.apache.shardingsphere.infra.session.query.QueryContext;
30  
31  import java.util.Collection;
32  
33  /**
34   * Broadcast SQL router.
35   */
36  @HighFrequencyInvocation
37  public final class BroadcastSQLRouter implements EntranceSQLRouter<BroadcastRule> {
38      
39      @Override
40      public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database,
41                                             final BroadcastRule rule, final Collection<String> tableNames, final ConfigurationProperties props) {
42          Collection<String> broadcastTableNames = rule.getBroadcastTableNames(tableNames);
43          if (broadcastTableNames.isEmpty()) {
44              return new RouteContext();
45          }
46          return BroadcastRouteEngineFactory.newInstance(queryContext, broadcastTableNames).route(rule);
47      }
48      
49      @Override
50      public Type getType() {
51          return Type.DATA_NODE;
52      }
53      
54      @Override
55      public int getOrder() {
56          return BroadcastOrder.ORDER;
57      }
58      
59      @Override
60      public Class<BroadcastRule> getTypeClass() {
61          return BroadcastRule.class;
62      }
63  }