<commit_before>/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.sal.dom.broker.util

import org.opendaylight.yangtools.yang.data.api.CompositeNode
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
import static com.google.common.base.Preconditions.*;
import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
import java.util.ArrayList

import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
import org.opendaylight.yangtools.yang.data.api.Node
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
import java.util.List
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode
import java.util.Collections
import java.util.HashSet
import org.opendaylight.yangtools.yang.common.QName
import static extension org.opendaylight.controller.sal.dom.broker.util.YangDataUtils.*;

class YangDataOperations {

    static def CompositeNode merge(DataSchemaNode schema, CompositeNode stored, CompositeNode modified, boolean config) {
        if (stored === null) {
            return modified;
        }

        if (schema instanceof ListSchemaNode || schema instanceof ContainerSchemaNode) {
            return mergeContainer(schema as DataNodeContainer, stored, modified, config);
        }
        throw new IllegalArgumentException("Supplied node is not data node container.");
    }

    private def static checkConfigurational(DataSchemaNode node, boolean config) {
        if (config) {
            checkArgument(node.configuration, "Supplied composite node is not configurational.");
        }
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(LeafSchemaNode node, List<Node<?>> original,
        List<Node<?>> modified, boolean configurational) {
        checkArgument(original.size === 1);
        checkArgument(modified.size === 1);
        
        return modified;
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(LeafListSchemaNode node,
        List<Node<?>> original, List<Node<?>> modified, boolean configurational) {
        return modified;
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(ContainerSchemaNode node,
        List<Node<?>> original, List<Node<?>> modified, boolean configurational) {
        checkArgument(original.size === 1);
        checkArgument(modified.size === 1);
        return Collections.singletonList(
            merge(node, original.get(0) as CompositeNode, modified.get(0) as CompositeNode, configurational));
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(ListSchemaNode node, List<Node<?>> original,
        List<Node<?>> modified, boolean configurational) {
        
        if(node.keyDefinition === null || node.keyDefinition.empty) {
            return modified;
        } 
        val originalMap = (original as List).toIndexMap(node.keyDefinition);
        val modifiedMap = (modified as List).toIndexMap(node.keyDefinition);
        
        val List<Node<?>> mergedNodes = new ArrayList(original.size + modified.size);
        for(entry : modifiedMap.entrySet) {
            val originalEntry = originalMap.get(entry.key);
            if(originalEntry != null) {
                originalMap.remove(entry.key);
                mergedNodes.add(merge(node,originalEntry,entry.value,configurational));
                
            }
        }
        mergedNodes.addAll(originalMap.values);
        return mergedNodes;
    }

    static private def CompositeNode mergeContainer(DataNodeContainer schema, CompositeNode stored,
        CompositeNode modified, boolean config) {
        if (stored == null) {
            return modified;
        }
        checkNotNull(stored)
        checkNotNull(modified)
        checkArgument(stored.nodeType == modified.nodeType);

        val mergedChildNodes = new ArrayList<Node<?>>(stored.children.size + modified.children.size);
        
        val toProcess = new HashSet<QName>(stored.keySet);
        toProcess.addAll(modified.keySet);
        
        for (qname : toProcess) {
            val schemaChild = schema.getDataChildByName(qname);
            val storedChildren = stored.get(qname);
            val modifiedChildren = modified.get(qname);
<commit_msg>Fixed bug when new childs were ommited during data store merge.<commit_after>/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.sal.dom.broker.util

import org.opendaylight.yangtools.yang.data.api.CompositeNode
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
import static com.google.common.base.Preconditions.*;
import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
import java.util.ArrayList

import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
import org.opendaylight.yangtools.yang.data.api.Node
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
import java.util.List
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode
import java.util.Collections
import java.util.HashSet
import org.opendaylight.yangtools.yang.common.QName
import static extension org.opendaylight.controller.sal.dom.broker.util.YangDataUtils.*;

class YangDataOperations {

    static def CompositeNode merge(DataSchemaNode schema, CompositeNode stored, CompositeNode modified, boolean config) {
        if (stored === null) {
            return modified;
        }

        if (schema instanceof ListSchemaNode || schema instanceof ContainerSchemaNode) {
            return mergeContainer(schema as DataNodeContainer, stored, modified, config);
        }
        throw new IllegalArgumentException("Supplied node is not data node container.");
    }

    private def static checkConfigurational(DataSchemaNode node, boolean config) {
        if (config) {
            checkArgument(node.configuration, "Supplied composite node is not configurational.");
        }
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(LeafSchemaNode node, List<Node<?>> original,
        List<Node<?>> modified, boolean configurational) {
        checkArgument(original.size === 1);
        checkArgument(modified.size === 1);
        
        return modified;
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(LeafListSchemaNode node,
        List<Node<?>> original, List<Node<?>> modified, boolean configurational) {
        return modified;
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(ContainerSchemaNode node,
        List<Node<?>> original, List<Node<?>> modified, boolean configurational) {
        checkArgument(original.size === 1);
        checkArgument(modified.size === 1);
        return Collections.singletonList(
            merge(node, original.get(0) as CompositeNode, modified.get(0) as CompositeNode, configurational));
    }

    private static dispatch def Iterable<? extends Node<?>> mergeMultiple(ListSchemaNode node, List<Node<?>> original,
        List<Node<?>> modified, boolean configurational) {
        
        if(node.keyDefinition === null || node.keyDefinition.empty) {
            return modified;
        }
        val originalMap = (original as List).toIndexMap(node.keyDefinition);
        val modifiedMap = (modified as List).toIndexMap(node.keyDefinition);
        
        val List<Node<?>> mergedNodes = new ArrayList(original.size + modified.size);
        for(entry : modifiedMap.entrySet) {
            val originalEntry = originalMap.get(entry.key);
            if(originalEntry != null) {
                originalMap.remove(entry.key);
                mergedNodes.add(merge(node,originalEntry,entry.value,configurational));
            } else {
                mergedNodes.add(entry.value);
            }
        }
        mergedNodes.addAll(originalMap.values);
        return mergedNodes;
    }

    static private def CompositeNode mergeContainer(DataNodeContainer schema, CompositeNode stored,
        CompositeNode modified, boolean config) {
        if (stored == null) {
            return modified;
        }
        checkNotNull(stored)
        checkNotNull(modified)
        checkArgument(stored.nodeType == modified.nodeType);

        val mergedChildNodes = new ArrayList<Node<?>>(stored.children.size + modified.children.size);
        
        val toProcess = new HashSet<QName>(stored.keySet);
        toProcess.addAll(modified.keySet);
        
        for (qname : toProcess) {
            val schemaChild = schema.getDataChildByName(qname);
            val storedChildren = stored.get(qname);
            val modifiedChildren = modified.get(qname);

{'fraction_non_alphanumeric': 0.09750927397986221, 'fraction_numerical': 0.00296767355590885, 'mean_word_length': 3.2504504504504506, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '1897376', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2473, 'n_words': 594}