1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.sqlfederation.rule;
19
20 import lombok.Getter;
21 import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
22 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
23 import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
24 import org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContext;
25 import org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContextFactory;
26 import org.apache.shardingsphere.sqlfederation.compiler.exception.InvalidExecutionPlanCacheConfigException;
27 import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
28 import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
29 import org.apache.shardingsphere.sqlfederation.constant.SQLFederationOrder;
30
31 import java.util.Collection;
32 import java.util.concurrent.atomic.AtomicReference;
33
34
35
36
37 @Getter
38 public final class SQLFederationRule implements GlobalRule {
39
40 private final SQLFederationRuleConfiguration configuration;
41
42 private final AtomicReference<CompilerContext> compilerContext;
43
44 public SQLFederationRule(final SQLFederationRuleConfiguration ruleConfig, final Collection<ShardingSphereDatabase> databases) {
45 configuration = ruleConfig;
46 compilerContext = new AtomicReference<>(CompilerContextFactory.create(databases));
47 checkExecutionPlanCacheConfiguration(ruleConfig.getExecutionPlanCache());
48 }
49
50 private void checkExecutionPlanCacheConfiguration(final SQLFederationCacheOption executionPlanCache) {
51 ShardingSpherePreconditions.checkState(executionPlanCache.getInitialCapacity() > 0,
52 () -> new InvalidExecutionPlanCacheConfigException("initialCapacity", executionPlanCache.getInitialCapacity()));
53 ShardingSpherePreconditions.checkState(executionPlanCache.getMaximumSize() > 0, () -> new InvalidExecutionPlanCacheConfigException("maximumSize", executionPlanCache.getMaximumSize()));
54 }
55
56
57
58
59
60
61 public CompilerContext getCompilerContext() {
62 return compilerContext.get();
63 }
64
65 @Override
66 public void refresh(final Collection<ShardingSphereDatabase> databases, final GlobalRuleChangedType changedType) {
67 compilerContext.set(CompilerContextFactory.create(databases));
68 }
69
70 @Override
71 public int getOrder() {
72 return SQLFederationOrder.ORDER;
73 }
74 }