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.resource;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.database.connector.core.jdbcurl.judger.DatabaseInstanceJudgeEngine;
23  import org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionProperties;
24  import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
25  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
26  import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
27  import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeAggregator;
28  import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
29  
30  import javax.sql.DataSource;
31  import java.util.Collection;
32  import java.util.LinkedHashMap;
33  import java.util.LinkedList;
34  import java.util.Map;
35  import java.util.Map.Entry;
36  import java.util.stream.Collectors;
37  
38  /**
39   * Resource meta data.
40   */
41  @RequiredArgsConstructor
42  @Getter
43  public final class ResourceMetaData {
44      
45      private final Map<StorageNode, DataSource> dataSources;
46      
47      private final Map<String, StorageUnit> storageUnits;
48      
49      public ResourceMetaData(final Map<String, DataSource> dataSources) {
50          this.dataSources = StorageNodeAggregator.aggregateDataSources(dataSources);
51          Map<String, StorageNode> storageUnitNodeMap = dataSources.keySet().stream()
52                  .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
53          Map<String, DataSourcePoolProperties> dataSourcePoolPropsMap = dataSources.entrySet().stream().collect(
54                  Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
55          storageUnits = storageUnitNodeMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> new StorageUnit(
56                  entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName())), (oldValue, currentValue) -> currentValue, LinkedHashMap::new));
57      }
58      
59      /**
60       * Get all instance data source names.
61       *
62       * @return instance data source names
63       */
64      public Collection<String> getAllInstanceDataSourceNames() {
65          Collection<String> result = new LinkedList<>();
66          for (String each : storageUnits.keySet()) {
67              if (!isExisted(each, result)) {
68                  result.add(each);
69              }
70          }
71          return result;
72      }
73      
74      private boolean isExisted(final String dataSourceName, final Collection<String> existedDataSourceNames) {
75          DatabaseInstanceJudgeEngine judgeEngine = new DatabaseInstanceJudgeEngine(storageUnits.get(dataSourceName).getStorageType());
76          ConnectionProperties connectionProps = storageUnits.get(dataSourceName).getConnectionProperties();
77          return existedDataSourceNames.stream().anyMatch(each -> judgeEngine.isInSameDatabaseInstance(connectionProps, storageUnits.get(each).getConnectionProperties()));
78      }
79      
80      /**
81       * Get not existed resource name.
82       *
83       * @param resourceNames resource names to be judged
84       * @return not existed resource names
85       */
86      public Collection<String> getNotExistedDataSources(final Collection<String> resourceNames) {
87          return resourceNames.stream().filter(each -> !storageUnits.containsKey(each)).collect(Collectors.toSet());
88      }
89      
90      /**
91       * Get data source map.
92       *
93       * @return data source map
94       */
95      public Map<String, DataSource> getDataSourceMap() {
96          return storageUnits.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
97      }
98  }