/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.core.metadata.datasource;

import io.shardingsphere.core.constant.DatabaseType;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Sharding data source meta data.
 *
 * @author panjuan
 */
public class ShardingDataSourceMetaData {
    
    private final Map<String, DataSourceMetaData> dataSourceMetaDataMap;
    
    public ShardingDataSourceMetaData(final Map<String, DataSource> dataSourceMap, final DatabaseType databaseType) {
        dataSourceMetaDataMap = getDataSourceMetaDataMap(dataSourceMap, databaseType);
    }
    
    private Map<String, DataSourceMetaData> getDataSourceMetaDataMap(final Map<String, DataSource> dataSourceMap, final DatabaseType databaseType) {
        Map<String, DataSourceMetaData> result = new LinkedHashMap<>();
        for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
            result.put(entry.getKey(), DataSourceMetaDataFactory.getDataSourceMetaData(databaseType, entry.getValue()));
        }
        return result;
    }
    
    /**
     * Get all instance data source names.
     *
     * @return instance data source names
     */
    public Collection<String> getAllInstanceDataSourceNames() {
        Collection<String> result = new LinkedList<>();
        for (Entry<String, DataSourceMetaData> entry : dataSourceMetaDataMap.entrySet()) {
            if (!isExisted(entry.getKey(), result)) {
                result.add(entry.getKey());
            }
        }
        return result;
    }
    
    private boolean isExisted(final String dataSourceName, final Collection<String> existedDataSourceNames) {
        for (String each : existedDataSourceNames) {
            if (dataSourceMetaDataMap.get(each).isInSameDatabaseInstance(dataSourceMetaDataMap.get(dataSourceName))) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * Get actual schema name.
     *
     * @param actualDataSourceName actual data source name
     * @return actual schema name
     */
    public String getActualSchemaName(final String actualDataSourceName) {
        return dataSourceMetaDataMap.get(actualDataSourceName).getSchemeName();
    }
}

@@ -662,24 +662,64 @@
 atasource;%0A%0A
+import com.google.common.base.Optional;%0A
 import io.sh
@@ -758,16 +758,65 @@
 aseType;
+%0Aimport io.shardingsphere.core.rule.ShardingRule;
 %0A%0Aimport
@@ -1243,32 +1243,65 @@
 %3E dataSourceMap,
+ final ShardingRule shardingRule,
  final DatabaseT
@@ -1391,16 +1391,30 @@
 urceMap,
+ shardingRule,
  databas
@@ -1541,24 +1541,57 @@
 taSourceMap,
+ final ShardingRule shardingRule,
  final Datab
@@ -1654,22 +1654,37 @@
 taData%3E 
-result
+dataSourceMetaDataMap
  = new L
@@ -1788,22 +1788,37 @@
         
-result
+dataSourceMetaDataMap
 .put(ent
@@ -1914,32 +1914,861 @@
 ()));%0A        %7D%0A
+        return handleMasterSlaveDataSourceNames(shardingRule, dataSourceMetaDataMap);%0A    %7D%0A    %0A    private Map%3CString, DataSourceMetaData%3E handleMasterSlaveDataSourceNames(final ShardingRule shardingRule, final Map%3CString, DataSourceMetaData%3E dataSourceMetaDataMap) %7B%0A        Map%3CString, DataSourceMetaData%3E result = new LinkedHashMap%3C%3E();%0A        if (shardingRule.getMasterSlaveRules().isEmpty()) %7B%0A            return dataSourceMetaDataMap;%0A        %7D%0A        for (Entry%3CString, DataSourceMetaData%3E entry : dataSourceMetaDataMap.entrySet()) %7B%0A            Optional%3CString%3E masterSlaveRuleNameOptional = shardingRule.getMasterSlaveRuleNameOptional(entry.getKey());%0A            if (masterSlaveRuleNameOptional.isPresent()) %7B%0A                result.put(masterSlaveRuleNameOptional.get(), entry.getValue());%0A            %7D%0A        %7D%0A
         return r

add handleMasterSlaveDataSourceNames.