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.rule;
19  
20  import com.cedarsoftware.util.CaseInsensitiveSet;
21  import lombok.Getter;
22  import org.apache.shardingsphere.broadcast.config.BroadcastRuleConfiguration;
23  import org.apache.shardingsphere.broadcast.constant.BroadcastOrder;
24  import org.apache.shardingsphere.broadcast.rule.attribute.BroadcastDataNodeRuleAttribute;
25  import org.apache.shardingsphere.broadcast.rule.attribute.BroadcastTableNamesRuleAttribute;
26  import org.apache.shardingsphere.broadcast.rule.attribute.BroadcastUnregisterStorageUnitRuleAttribute;
27  import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
28  import org.apache.shardingsphere.infra.metadata.database.resource.PhysicalDataSourceAggregator;
29  import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
30  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
31  import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
32  import org.apache.shardingsphere.infra.rule.attribute.datasource.aggregate.AggregatedDataSourceRuleAttribute;
33  import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
34  
35  import javax.sql.DataSource;
36  import java.util.Collection;
37  import java.util.Map;
38  
39  /**
40   * Broadcast rule.
41   */
42  @Getter
43  public final class BroadcastRule implements DatabaseRule {
44      
45      private final BroadcastRuleConfiguration configuration;
46      
47      private final Collection<String> dataSourceNames;
48      
49      private final Collection<String> tables;
50      
51      private final RuleAttributes attributes;
52      
53      public BroadcastRule(final BroadcastRuleConfiguration ruleConfig, final Map<String, DataSource> dataSources, final Collection<ShardingSphereRule> builtRules) {
54          configuration = ruleConfig;
55          Map<String, DataSource> aggregatedDataSources = new RuleMetaData(builtRules).findAttribute(AggregatedDataSourceRuleAttribute.class)
56                  .map(AggregatedDataSourceRuleAttribute::getAggregatedDataSources).orElseGet(() -> PhysicalDataSourceAggregator.getAggregatedDataSources(dataSources, builtRules));
57          dataSourceNames = new CaseInsensitiveSet<>(aggregatedDataSources.keySet());
58          tables = new CaseInsensitiveSet<>(ruleConfig.getTables());
59          attributes = new RuleAttributes(new BroadcastDataNodeRuleAttribute(dataSourceNames, tables),
60                  new BroadcastTableNamesRuleAttribute(tables), new AggregatedDataSourceRuleAttribute(aggregatedDataSources), new BroadcastUnregisterStorageUnitRuleAttribute());
61      }
62      
63      /**
64       * Get broadcast table names.
65       *
66       * @param logicTableNames all logic table names
67       * @return broadcast table names
68       */
69      @HighFrequencyInvocation
70      public Collection<String> getBroadcastTableNames(final Collection<String> logicTableNames) {
71          Collection<String> result = new CaseInsensitiveSet<>();
72          for (String each : logicTableNames) {
73              if (tables.contains(each)) {
74                  result.add(each);
75              }
76          }
77          return result;
78      }
79      
80      @Override
81      public int getOrder() {
82          return BroadcastOrder.ORDER;
83      }
84  }