1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
65
66
67
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 }