<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.index.revision;

import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Sets.newHashSet;

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 java.util.UUID;

import com.b2international.index.Writer;
import com.b2international.index.query.Expression;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.google.common.collect.ImmutableMap;

/**
 * @since 4.7
 */
public class DefaultRevisionWriter implements RevisionWriter {

	private final String branchPath;
	private final long commitTimestamp;
	private final Writer index;
	private final RevisionSearcher searcher;
	
	private final Map<Class<? extends Revision>, Collection<Long>> revisionUpdates = newHashMap();

	public DefaultRevisionWriter(String branchPath, long commitTimestamp, Writer index, RevisionSearcher searcher) {
		this.branchPath = branchPath;
		this.commitTimestamp = commitTimestamp;
		this.index = index;
		this.searcher = searcher;
	}

	@Override
	public void put(long storageKey, Revision object) throws IOException {
		if (revisionUpdates.containsKey(object.getClass())) {
			revisionUpdates.get(object.getClass()).add(storageKey);
		} else {
			revisionUpdates.put(object.getClass(), newHashSet(storageKey));
		}
		object.setBranchPath(branchPath);
		object.setCommitTimestamp(commitTimestamp);
		object.setStorageKey(storageKey);
		index.put(generateRevisionId(), object);
	}

	@Override
	public void putAll(Map<Long, Revision> revisionsByStorageKey) throws IOException {
		throw new UnsupportedOperationException("TODO implement me");
	}

	@Override
	public void remove(Class<? extends Revision> type, long storageKey) throws IOException {
		remove(type, Collections.singleton(storageKey));
	}
	
	@Override
	public void remove(Class<? extends Revision> type, Collection<Long> storageKeys) throws IOException {
		removeAll(ImmutableMap.<Class<? extends Revision>, Collection<Long>>of(type, storageKeys));
	}

	@Override
	public void removeAll(Map<Class<? extends Revision>, Collection<Long>> storageKeysByType) throws IOException {
		final Map<Class<? extends Revision>, Query<? extends Revision>> queriesByType = newHashMap();
		for (Class<? extends Revision> type : storageKeysByType.keySet()) {
			final Collection<Long> storageKeysToUpdate = storageKeysByType.get(type);
			if (!storageKeysToUpdate.isEmpty()) {
				final Expression where = Expressions.matchAnyLong(Revision.STORAGE_KEY, storageKeysToUpdate);
				queriesByType.put(type, Query.builder(type).selectAll().where(where).limit(Integer.MAX_VALUE).build());
			}
		}
		for (Entry<Class<? extends Revision>, Query<? extends Revision>> entry : queriesByType.entrySet()) {
			final Iterable<? extends Revision> revisionsToUpdate = searcher.search(entry.getValue());
			final Map<String, Object> revisionUpdates = newHashMap();
			for (Revision rev : revisionsToUpdate) {
				final Set<ReplacedIn> set = newHashSet();
				final Collection<ReplacedIn> prevReplacedIns = rev.getReplacedIns();
				set.addAll(prevReplacedIns);
				set.add(new ReplacedIn(branchPath, commitTimestamp));
				rev.setReplacedIns(set);<commit_msg>SO-1957: implement putAll on DefaultRevisionWriter<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.index.revision;

import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Sets.newHashSet;

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 java.util.UUID;

import com.b2international.index.Writer;
import com.b2international.index.query.Expression;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.google.common.collect.ImmutableMap;

/**
 * @since 4.7
 */
public class DefaultRevisionWriter implements RevisionWriter {

	private final String branchPath;
	private final long commitTimestamp;
	private final Writer index;
	private final RevisionSearcher searcher;
	
	private final Map<Class<? extends Revision>, Collection<Long>> revisionUpdates = newHashMap();

	public DefaultRevisionWriter(String branchPath, long commitTimestamp, Writer index, RevisionSearcher searcher) {
		this.branchPath = branchPath;
		this.commitTimestamp = commitTimestamp;
		this.index = index;
		this.searcher = searcher;
	}

	@Override
	public void put(long storageKey, Revision object) throws IOException {
		if (revisionUpdates.containsKey(object.getClass())) {
			revisionUpdates.get(object.getClass()).add(storageKey);
		} else {
			revisionUpdates.put(object.getClass(), newHashSet(storageKey));
		}
		object.setBranchPath(branchPath);
		object.setCommitTimestamp(commitTimestamp);
		object.setStorageKey(storageKey);
		index.put(generateRevisionId(), object);
	}

	@Override
	public void putAll(Map<Long, Revision> revisionsByStorageKey) throws IOException {
		for (Entry<Long, Revision> doc : revisionsByStorageKey.entrySet()) {
			put(doc.getKey(), doc.getValue());
		}
	}

	@Override
	public void remove(Class<? extends Revision> type, long storageKey) throws IOException {
		remove(type, Collections.singleton(storageKey));
	}
	
	@Override
	public void remove(Class<? extends Revision> type, Collection<Long> storageKeys) throws IOException {
		removeAll(ImmutableMap.<Class<? extends Revision>, Collection<Long>>of(type, storageKeys));
	}

	@Override
	public void removeAll(Map<Class<? extends Revision>, Collection<Long>> storageKeysByType) throws IOException {
		final Map<Class<? extends Revision>, Query<? extends Revision>> queriesByType = newHashMap();
		for (Class<? extends Revision> type : storageKeysByType.keySet()) {
			final Collection<Long> storageKeysToUpdate = storageKeysByType.get(type);
			if (!storageKeysToUpdate.isEmpty()) {
				final Expression where = Expressions.matchAnyLong(Revision.STORAGE_KEY, storageKeysToUpdate);
				queriesByType.put(type, Query.builder(type).selectAll().where(where).limit(Integer.MAX_VALUE).build());
			}
		}
		for (Entry<Class<? extends Revision>, Query<? extends Revision>> entry : queriesByType.entrySet()) {
			final Iterable<? extends Revision> revisionsToUpdate = searcher.search(entry.getValue());
			final Map<String, Object> revisionUpdates = newHashMap();
			for (Revision rev : revisionsToUpdate) {
				final Set<ReplacedIn> set = newHashSet();
				final Collection<ReplacedIn> prevReplacedIns = rev.getReplacedIns();
				set.addAll(prevReplacedIns);
				set.add(new ReplacedIn(branchPath, commitTimestamp));
				rev.setReplacedIns(set);
{'fraction_non_alphanumeric': 0.1058705469141997, 'fraction_numerical': 0.00577019568489714, 'mean_word_length': 5.487388120423108, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, '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': '1469974', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2250, 'n_words': 611}