<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.shardingsphere.infra.datanode;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.DataNodeRoutedRule;
import org.apache.shardingsphere.infra.rule.DataSourceRoutedRule;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Data nodes.
 */
@RequiredArgsConstructor
public final class DataNodes {
    
    private final Collection<ShardingSphereRule> rules;
    
    /**
     * Get data nodes.
     * 
     * @param tableName table name
     * @return data nodes
     */
    public Collection<DataNode> getDataNodes(final String tableName) {
        Optional<DataNodeRoutedRule> dataNodeRoutedRule = rules.stream().filter(each -> each instanceof DataNodeRoutedRule).findFirst().map(rule -> (DataNodeRoutedRule) rule);
        if (!dataNodeRoutedRule.isPresent()) {
            return Collections.emptyList();
        }
        Collection<DataNode> result = new LinkedList<>(dataNodeRoutedRule.get().getAllDataNodes().get(tableName));
        for (ShardingSphereRule each : rules) {
            if (each instanceof DataSourceRoutedRule) {
                for (Entry<String, Collection<String>> entry : ((DataSourceRoutedRule) each).getDataSourceMapper().entrySet()) {
                    Collection<DataNode> dataNodes = find(result, entry.getKey());
                    result.removeAll(dataNodes);
                    result.addAll(regenerate(dataNodes, entry.getValue()));
                }
            }
        }
        return result;
    }
    
    private Collection<DataNode> find(final Collection<DataNode> dataNodes, final String logicDataSource) {
        return dataNodes.stream().filter(each -> each.getDataSourceName().equals(logicDataSource)).collect(Collectors.toList());
    }
    
    private Collection<DataNode> regenerate(final Collection<DataNode> dataNodes, final Collection<String> actualDataSources) {
        Collection<DataNode> result = new LinkedHashSet<>();
        for (DataNode each : dataNodes) {
            result.addAll(regenerate(actualDataSources, each.getTableName()));
        }
        return result;
    }
    
    private Collection<DataNode> regenerate(final Collection<String> dataSources, final String table) {
        Collection<DataNode> result = new LinkedHashSet<>();
        for (String each : dataSources) {
            result.add(new DataNode(each, table));
        }
        return result;
    }
    <commit_msg>Refactor DataNodes<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.shardingsphere.infra.datanode;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.DataNodeRoutedRule;
import org.apache.shardingsphere.infra.rule.DataSourceRoutedRule;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Data nodes.
 */
@RequiredArgsConstructor
public final class DataNodes {
    
    private final Collection<ShardingSphereRule> rules;
    
    /**
     * Get data nodes.
     * 
     * @param tableName table name
     * @return data nodes
     */
    public Collection<DataNode> getDataNodes(final String tableName) {
        Optional<DataNodeRoutedRule> dataNodeRoutedRule = rules.stream().filter(each -> each instanceof DataNodeRoutedRule).findFirst().map(rule -> (DataNodeRoutedRule) rule);
        if (!dataNodeRoutedRule.isPresent()) {
            return Collections.emptyList();
        }
        Collection<DataNode> result = new LinkedList<>(dataNodeRoutedRule.get().getAllDataNodes().get(tableName));
        for (ShardingSphereRule each : rules) {
            if (each instanceof DataSourceRoutedRule) {
                for (Entry<String, Collection<String>> entry : ((DataSourceRoutedRule) each).getDataSourceMapper().entrySet()) {
                    Collection<DataNode> dataNodes = find(result, entry.getKey());
                    result.removeAll(dataNodes);
                    result.addAll(regenerate(dataNodes, entry.getValue()));
                }
            }
        }
        return result;
    }
    
    private Collection<DataNode> find(final Collection<DataNode> dataNodes, final String logicDataSource) {
        return dataNodes.stream().filter(each -> each.getDataSourceName().equals(logicDataSource)).collect(Collectors.toList());
    }
    
    private Collection<DataNode> regenerate(final Collection<DataNode> dataNodes, final Collection<String> actualDataSources) {
        Collection<DataNode> result = new LinkedHashSet<>();
        for (DataNode each : dataNodes) {
            result.addAll(regenerate(actualDataSources, each.getTableName()));
        }
        return result;
    }
    
    private Collection<DataNode> regenerate(final Collection<String> dataSources, final String table) {
        Collection<DataNode> result = new LinkedHashSet<>(dataSources.size(), 1);
        for (String each : dataSources) {
            result.add(new DataNode(each, table));
        }
        return result;
    }
    
{'fraction_non_alphanumeric': 0.09514342516330587, 'fraction_numerical': 0.0012780460096563477, 'mean_word_length': 3.3582920792079207, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3442966', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1825, 'n_words': 518}