<commit_before>/*
 * Copyright 2011-2016 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.
 */
package com.b2international.snowowl.datastore.index;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.b2international.index.revision.Revision;
import com.b2international.index.revision.RevisionWriter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

/**
 * @since 5.0
 */
public final class ImmutableIndexCommitChangeSet implements IndexCommitChangeSet {

	private final Map<String, Object> rawMappings;
	private final Multimap<Class<?>, String> rawDeletions;
	private final Map<Long, Revision> revisionMappings;
	private final Multimap<Class<? extends Revision>, Long> revisionDeletions;
	private final Collection<String> newComponents;
	private final Collection<String> changedComponents;
	private final Collection<String> deletedComponents;

	private ImmutableIndexCommitChangeSet(final Map<String, Object> rawMappings, 
			final Multimap<Class<?>, String> rawDeletions, 
			final Map<Long, Revision> revisionMappings,
			final Multimap<Class<? extends Revision>, Long> revisionDeletions,
			final Collection<String> newComponents,
			final Collection<String> changedComponents,
			final Collection<String> deletedComponents) {
		this.rawMappings = rawMappings;
		this.rawDeletions = rawDeletions;
		this.revisionMappings = revisionMappings;
		this.revisionDeletions = revisionDeletions;
		this.newComponents = newComponents;
		this.changedComponents = changedComponents;
		this.deletedComponents = deletedComponents;
	}
	
	@Override
	public boolean isEmpty() {
		return rawMappings.isEmpty() 
				&& rawDeletions.isEmpty()
				&& revisionMappings.isEmpty()
				&& revisionDeletions.isEmpty();
	}
	
	@Override
	public Collection<String> getNewComponents() {
		return newComponents;
	}
	
	@Override
	public Collection<String> getChangedComponents() {
		return changedComponents;
	}
	
	@Override
	public Collection<String> getDeletedComponents() {
		return deletedComponents;
	}
	
	@Override
	public Map<String, Object> getRawMappings() {
		return rawMappings;
	}
	
	@Override
	public Multimap<Class<?>, String> getRawDeletions() {
		return rawDeletions;
	}
	
	@Override
	public Map<Long, Revision> getRevisionMappings() {
		return revisionMappings;
	}
	
	@Override
	public Multimap<Class<? extends Revision>, Long> getRevisionDeletions() {
		return revisionDeletions;
	}
	
	@Override
	public IndexCommitChangeSet merge(IndexCommitChangeSet indexCommitChangeSet) {
		return builder().from(this).from(indexCommitChangeSet).build();
	}
	
	/**
	 * Apply this {@link ImmutableIndexCommitChangeSet} on the given {@link RevisionWriter index transaction}.
	 * 
	 * @param index
	 * @throws IOException
	 */
	@Override
	public void apply(RevisionWriter index) throws IOException {
		for (final Class<?> type : rawDeletions.keySet()) {
			final Map<Class<?>, Set<String>> map = Collections.<Class<?>, Set<String>> singletonMap(type,
					Sets.newHashSet(rawDeletions.get(type)));
			index.writer().removeAll(map);
		}
		
		for (Entry<String, Object> doc : rawMappings.entrySet()) {
			index.writer().put(doc.getKey(), doc.getValue());
		}

		final Multimap<Class<? extends Revision>, Long> copiedRevision = ImmutableMultimap.copyOf(revisionDeletions);
		for (Class<? extends Revision> type : copiedRevision.keySet()) {
			index.remove(type, copiedRevision.get(type));
		}

		for (Entry<Long, Revision> doc : revisionMappings.entrySet()) {
			if (!revisionDeletions.containsValue(doc.getKey())) {
				index.put(doc.getKey(), doc.getValue());
			}<commit_msg>SO-2058 Check if deletions contains value before adding it to the index<commit_after>/*
 * Copyright 2011-2016 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.
 */
package com.b2international.snowowl.datastore.index;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.b2international.index.revision.Revision;
import com.b2international.index.revision.RevisionWriter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

/**
 * @since 5.0
 */
public final class ImmutableIndexCommitChangeSet implements IndexCommitChangeSet {

	private final Map<String, Object> rawMappings;
	private final Multimap<Class<?>, String> rawDeletions;
	private final Map<Long, Revision> revisionMappings;
	private final Multimap<Class<? extends Revision>, Long> revisionDeletions;
	private final Collection<String> newComponents;
	private final Collection<String> changedComponents;
	private final Collection<String> deletedComponents;

	private ImmutableIndexCommitChangeSet(final Map<String, Object> rawMappings, 
			final Multimap<Class<?>, String> rawDeletions, 
			final Map<Long, Revision> revisionMappings,
			final Multimap<Class<? extends Revision>, Long> revisionDeletions,
			final Collection<String> newComponents,
			final Collection<String> changedComponents,
			final Collection<String> deletedComponents) {
		this.rawMappings = rawMappings;
		this.rawDeletions = rawDeletions;
		this.revisionMappings = revisionMappings;
		this.revisionDeletions = revisionDeletions;
		this.newComponents = newComponents;
		this.changedComponents = changedComponents;
		this.deletedComponents = deletedComponents;
	}
	
	@Override
	public boolean isEmpty() {
		return rawMappings.isEmpty() 
				&& rawDeletions.isEmpty()
				&& revisionMappings.isEmpty()
				&& revisionDeletions.isEmpty();
	}
	
	@Override
	public Collection<String> getNewComponents() {
		return newComponents;
	}
	
	@Override
	public Collection<String> getChangedComponents() {
		return changedComponents;
	}
	
	@Override
	public Collection<String> getDeletedComponents() {
		return deletedComponents;
	}
	
	@Override
	public Map<String, Object> getRawMappings() {
		return rawMappings;
	}
	
	@Override
	public Multimap<Class<?>, String> getRawDeletions() {
		return rawDeletions;
	}
	
	@Override
	public Map<Long, Revision> getRevisionMappings() {
		return revisionMappings;
	}
	
	@Override
	public Multimap<Class<? extends Revision>, Long> getRevisionDeletions() {
		return revisionDeletions;
	}
	
	@Override
	public IndexCommitChangeSet merge(IndexCommitChangeSet indexCommitChangeSet) {
		return builder().from(this).from(indexCommitChangeSet).build();
	}
	
	/**
	 * Apply this {@link ImmutableIndexCommitChangeSet} on the given {@link RevisionWriter index transaction}.
	 * 
	 * @param index
	 * @throws IOException
	 */
	@Override
	public void apply(RevisionWriter index) throws IOException {
		for (final Class<?> type : rawDeletions.keySet()) {
			final Map<Class<?>, Set<String>> map = Collections.<Class<?>, Set<String>> singletonMap(type,
					Sets.newHashSet(rawDeletions.get(type)));
			index.writer().removeAll(map);
		}
		
		for (Entry<String, Object> doc : rawMappings.entrySet()) {
			if (!rawDeletions.containsValue(doc.getKey())) {
				index.writer().put(doc.getKey(), doc.getValue());
			}
		}

		final Multimap<Class<? extends Revision>, Long> copiedRevision = ImmutableMultimap.copyOf(revisionDeletions);
		for (Class<? extends Revision> type : copiedRevision.keySet()) {
			index.remove(type, copiedRevision.get(type));
		}

		for (Entry<Long, Revision> doc : revisionMappings.entrySet()) {
			if (!revisionDeletions.containsValue(doc.getKey())) {
				index.put(doc.getKey(), doc.getValue());
			}
{'fraction_non_alphanumeric': 0.10599595778127105, 'fraction_numerical': 0.004715921850437907, 'mean_word_length': 5.142758620689655, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 81, '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': '7493919', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2578, 'n_words': 671}