/*
 * 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 com.google.common.base.Optional;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.core.rule.ShardingRule;

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, String> dataSourceURLs, final ShardingRule shardingRule, final DatabaseType databaseType) {
        dataSourceMetaDataMap = getDataSourceMetaDataMap(dataSourceURLs, shardingRule, databaseType);
    }
    
    private Map<String, DataSourceMetaData> getDataSourceMetaDataMap(final Map<String, String> dataSourceURLs, final ShardingRule shardingRule, final DatabaseType databaseType) {
        Map<String, DataSourceMetaData> dataSourceMetaDataMap = new LinkedHashMap<>(dataSourceURLs.size(), 1);
        for (Entry<String, String> entry : dataSourceURLs.entrySet()) {
            dataSourceMetaDataMap.put(entry.getKey(), DataSourceMetaDataFactory.newInstance(databaseType, entry.getValue()));
        }
        return handleMasterSlaveDataSources(shardingRule, dataSourceMetaDataMap);
    }
    
    private Map<String, DataSourceMetaData> handleMasterSlaveDataSources(final ShardingRule shardingRule, final Map<String, DataSourceMetaData> dataSourceMetaDataMap) {
        Map<String, DataSourceMetaData> result = new LinkedHashMap<>();
        if (shardingRule.getMasterSlaveRules().isEmpty()) {
            return dataSourceMetaDataMap;
        }
        for (Entry<String, DataSourceMetaData> entry : dataSourceMetaDataMap.entrySet()) {
            Optional<MasterSlaveRule> masterSlaveRule = shardingRule.findMasterSlaveRule(entry.getKey());
            // TODO original DataSourceMetaData do not remove?
            if (masterSlaveRule.isPresent() && masterSlaveRule.get().getMasterDataSourceName().equals(entry.getKey())) {
                result.put(masterSlaveRule.get().getName(), 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();
    }
}

@@ -1672,37 +1672,22 @@
 taData%3E 
-dataSourceMetaDataMap
+result
  = new L
@@ -1812,37 +1812,22 @@
         
-dataSourceMetaDataMap
+result
 .put(ent
@@ -1929,23 +1929,16 @@
         
-return 
 handleMa
@@ -1976,86 +1976,67 @@
 le, 
-dataSourceMetaDataMap);%0A    %7D%0A    %0A    private Map%3CString, DataSourceMetaData%3E
+result);%0A        return result;%0A    %7D%0A    %0A    private void
  han
@@ -2169,183 +2169,152 @@
     
-Map%3CString, DataSourceMetaData%3E result = new LinkedHashMap%3C%3E();%0A        if (shardingRule.getMasterSlaveRules().isEmpty()) %7B%0A            return dataSourceMetaDataMap;%0A        %7D
+if (shardingRule.getMasterSlaveRules().isEmpty()) %7B%0A            return;%0A        %7D%0A        Collection%3CString%3E toRemovedKeys = new LinkedList%3C%3E();
 %0A   
@@ -2413,16 +2413,22 @@
         
+final 
 Optional
@@ -2517,71 +2517,8 @@
 ));%0A
-            // TODO original DataSourceMetaData do not remove?%0A
     
@@ -2654,117 +2654,610 @@
     
-result.put(masterSlaveRule.get().getName(), entry.getValue());%0A            %7D%0A        %7D%0A        return result;
+toRemovedKeys.add(masterSlaveRule.get().getMasterDataSourceName());%0A                toRemovedKeys.addAll(masterSlaveRule.get().getSlaveDataSourceNames());%0A                dataSourceMetaDataMap.put(masterSlaveRule.get().getName(), entry.getValue());%0A            %7D%0A        %7D%0A        removeDataSourceMetaData(dataSourceMetaDataMap, toRemovedKeys);%0A    %7D%0A    %0A    private void removeDataSourceMetaData(final Map%3CString, DataSourceMetaData%3E dataSourceMetaDataMap, final Collection%3CString%3E toRemovedKeys) %7B%0A        for (String each : toRemovedKeys) %7B%0A            dataSourceMetaDataMap.remove(each);%0A        %7D
 %0A   

add removeDataSourceMetaData