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.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   * ShardingSphere database.
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       * Get all schemas.
68       *
69       * @return all schemas
70       */
71      public Collection<ShardingSphereSchema> getAllSchemas() {
72          return schemas.values();
73      }
74      
75      /**
76       * Judge contains schema from database or not.
77       *
78       * @param schemaName schema name
79       * @return contains schema from database or not
80       */
81      public boolean containsSchema(final String schemaName) {
82          return schemas.containsKey(new ShardingSphereIdentifier(schemaName));
83      }
84      
85      /**
86       * Get schema.
87       *
88       * @param schemaName schema name
89       * @return schema
90       */
91      public ShardingSphereSchema getSchema(final String schemaName) {
92          return schemas.get(new ShardingSphereIdentifier(schemaName));
93      }
94      
95      /**
96       * Add schema.
97       *
98       * @param schema schema
99       */
100     public void addSchema(final ShardingSphereSchema schema) {
101         schemas.put(new ShardingSphereIdentifier(schema.getName()), schema);
102     }
103     
104     /**
105      * Drop schema.
106      *
107      * @param schemaName schema name
108      */
109     public void dropSchema(final String schemaName) {
110         schemas.remove(new ShardingSphereIdentifier(schemaName));
111     }
112     
113     /**
114      * Judge whether is completed.
115      *
116      * @return is completed or not
117      */
118     public boolean isComplete() {
119         return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnits().isEmpty();
120     }
121     
122     /**
123      * Judge whether contains data source.
124      *
125      * @return contains data source or not
126      */
127     public boolean containsDataSource() {
128         return !resourceMetaData.getStorageUnits().isEmpty();
129     }
130     
131     /**
132      * Reload rules.
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 }