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.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
23  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
24  import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
25  import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeAggregator;
26  import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
27  
28  import javax.sql.DataSource;
29  import java.util.Collection;
30  import java.util.LinkedHashMap;
31  import java.util.LinkedList;
32  import java.util.Map;
33  import java.util.Map.Entry;
34  import java.util.stream.Collectors;
35  
36  /**
37   * Resource meta data.
38   */
39  @RequiredArgsConstructor
40  @Getter
41  public final class ResourceMetaData {
42      
43      private final Map<StorageNode, DataSource> dataSources;
44      
45      private final Map<String, StorageUnit> storageUnits;
46      
47      public ResourceMetaData(final Map<String, DataSource> dataSources) {
48          this.dataSources = StorageNodeAggregator.aggregateDataSources(dataSources);
49          Map<String, StorageNode> storageUnitNodeMap = dataSources.keySet().stream()
50                  .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
51          Map<String, DataSourcePoolProperties> dataSourcePoolPropsMap = dataSources.entrySet().stream().collect(
52                  Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
53          storageUnits = new LinkedHashMap<>();
54          for (Entry<String, StorageNode> entry : storageUnitNodeMap.entrySet()) {
55              storageUnits.put(entry.getKey(), new StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName())));
56          }
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          return existedDataSourceNames.stream().anyMatch(each -> storageUnits.get(dataSourceName).getConnectionProperties().isInSameDatabaseInstance(storageUnits.get(each).getConnectionProperties()));
76      }
77      
78      /**
79       * Get not existed resource name.
80       * 
81       * @param resourceNames resource names to be judged
82       * @return not existed resource names
83       */
84      public Collection<String> getNotExistedDataSources(final Collection<String> resourceNames) {
85          return resourceNames.stream().filter(each -> !storageUnits.containsKey(each)).collect(Collectors.toSet());
86      }
87  }