1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.metadata;
19
20 import lombok.Getter;
21 import lombok.SneakyThrows;
22 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
23 import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties;
24 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
25 import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
26 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
27 import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
28 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
29 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
30 import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
31 import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
32 import org.apache.shardingsphere.infra.rule.scope.GlobalRule.GlobalRuleChangedType;
33
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.Optional;
38 import java.util.Properties;
39 import java.util.concurrent.ConcurrentHashMap;
40
41
42
43
44 @Getter
45 public final class ShardingSphereMetaData {
46
47 private final Map<String, ShardingSphereDatabase> databases;
48
49 private final ResourceMetaData globalResourceMetaData;
50
51 private final RuleMetaData globalRuleMetaData;
52
53 private final ConfigurationProperties props;
54
55 private final TemporaryConfigurationProperties temporaryProps;
56
57 public ShardingSphereMetaData() {
58 this(new HashMap<>(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties()));
59 }
60
61 public ShardingSphereMetaData(final Map<String, ShardingSphereDatabase> databases, final ResourceMetaData globalResourceMetaData,
62 final RuleMetaData globalRuleMetaData, final ConfigurationProperties props) {
63 this.databases = new ConcurrentHashMap<>(databases.size(), 1F);
64 databases.forEach((key, value) -> this.databases.put(key.toLowerCase(), value));
65 this.globalResourceMetaData = globalResourceMetaData;
66 this.globalRuleMetaData = globalRuleMetaData;
67 this.props = props;
68 temporaryProps = new TemporaryConfigurationProperties(props.getProps());
69 }
70
71
72
73
74
75
76
77 public boolean containsDatabase(final String databaseName) {
78 return databases.containsKey(databaseName.toLowerCase());
79 }
80
81
82
83
84
85
86
87 public ShardingSphereDatabase getDatabase(final String databaseName) {
88 return databases.get(databaseName.toLowerCase());
89 }
90
91
92
93
94
95
96
97
98 public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) {
99 ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props);
100 databases.put(database.getName().toLowerCase(), database);
101 globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases, GlobalRuleChangedType.DATABASE_CHANGED));
102 }
103
104
105
106
107
108
109 public void dropDatabase(final String databaseName) {
110 cleanResources(databases.remove(databaseName.toLowerCase()));
111 }
112
113 @SneakyThrows(Exception.class)
114 private void cleanResources(final ShardingSphereDatabase database) {
115 globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases, GlobalRuleChangedType.DATABASE_CHANGED));
116 for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
117 if (each instanceof AutoCloseable) {
118 ((AutoCloseable) each).close();
119 }
120 }
121 database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class).forEach(StaticDataSourceRuleAttribute::cleanStorageNodeDataSources);
122 Optional.ofNullable(database.getResourceMetaData())
123 .ifPresent(optional -> optional.getStorageUnits().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
124 }
125 }