1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.metadata.database;
19
20 import lombok.AccessLevel;
21 import lombok.Getter;
22 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
23 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
24 import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
25 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
26 import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
27 import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
28 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
29 import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
30
31 import javax.sql.DataSource;
32 import java.util.Collection;
33 import java.util.LinkedHashMap;
34 import java.util.LinkedList;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.stream.Collectors;
39
40
41
42
43 @Getter
44 public final class ShardingSphereDatabase {
45
46 private final String name;
47
48 private final DatabaseType protocolType;
49
50 private final ResourceMetaData resourceMetaData;
51
52 private final RuleMetaData ruleMetaData;
53
54 @Getter(AccessLevel.NONE)
55 private final Map<ShardingSphereIdentifier, ShardingSphereSchema> schemas;
56
57 public ShardingSphereDatabase(final String name, final DatabaseType protocolType, final ResourceMetaData resourceMetaData,
58 final RuleMetaData ruleMetaData, final Collection<ShardingSphereSchema> schemas) {
59 this.name = name;
60 this.protocolType = protocolType;
61 this.resourceMetaData = resourceMetaData;
62 this.ruleMetaData = ruleMetaData;
63 this.schemas = new ConcurrentHashMap<>(schemas.stream().collect(Collectors.toMap(each -> new ShardingSphereIdentifier(each.getName()), each -> each)));
64 }
65
66
67
68
69
70
71 public Collection<ShardingSphereSchema> getAllSchemas() {
72 return schemas.values();
73 }
74
75
76
77
78
79
80
81 public boolean containsSchema(final String schemaName) {
82 return schemas.containsKey(new ShardingSphereIdentifier(schemaName));
83 }
84
85
86
87
88
89
90
91 public ShardingSphereSchema getSchema(final String schemaName) {
92 return schemas.get(new ShardingSphereIdentifier(schemaName));
93 }
94
95
96
97
98
99
100 public void addSchema(final ShardingSphereSchema schema) {
101 schemas.put(new ShardingSphereIdentifier(schema.getName()), schema);
102 }
103
104
105
106
107
108
109 public void dropSchema(final String schemaName) {
110 schemas.remove(new ShardingSphereIdentifier(schemaName));
111 }
112
113
114
115
116
117
118 public boolean isComplete() {
119 return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnits().isEmpty();
120 }
121
122
123
124
125
126
127 public boolean containsDataSource() {
128 return !resourceMetaData.getStorageUnits().isEmpty();
129 }
130
131
132
133
134 public synchronized void reloadRules() {
135 Collection<ShardingSphereRule> toBeReloadedRules = ruleMetaData.getRules().stream()
136 .filter(each -> each.getAttributes().findAttribute(MutableDataNodeRuleAttribute.class).isPresent()).collect(Collectors.toList());
137 RuleConfiguration ruleConfig = toBeReloadedRules.stream().map(ShardingSphereRule::getConfiguration).findFirst().orElse(null);
138 Collection<ShardingSphereRule> rules = new LinkedList<>(ruleMetaData.getRules());
139 toBeReloadedRules.stream().findFirst().ifPresent(optional -> {
140 rules.removeAll(toBeReloadedRules);
141 Map<String, DataSource> dataSources = resourceMetaData.getStorageUnits().entrySet().stream()
142 .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
143 rules.add(optional.getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).reloadRule(ruleConfig, name, dataSources, rules));
144 });
145 ruleMetaData.getRules().clear();
146 ruleMetaData.getRules().addAll(rules);
147 }
148 }