<commit_before>/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * 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 io.datakernel.stream.processor;

import com.google.common.base.Function;
import io.datakernel.async.CompletionCallback;
import io.datakernel.eventloop.Eventloop;
import io.datakernel.stream.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Represent {@link StreamTransformer} which receives data and saves it in collection, when it
 * receive end of stream it sorts it and streams to destination.
 *
 * @param <K> type of keys
 * @param <T> type of objects
 */
public final class StreamSorter<K, T> implements StreamTransformer<T, T>, StreamSorterMBean {
	protected long jmxItems;

	private InputConsumer inputConsumer;

	private final class InputConsumer extends AbstractStreamConsumer<T> implements StreamDataReceiver<T> {
		private final StreamMergeSorterStorage<T> storage;
		private final Comparator<T> itemComparator;
		private final StreamMerger<K, T> merger;
		private List<Integer> listOfPartitions;
		private final int itemsInMemorySize;
		private ArrayList<T> list;
		private StreamForwarder<T> forwarder;

		private boolean writing;

		protected InputConsumer(Eventloop eventloop, int itemsInMemorySize, Comparator<T> itemComparator, StreamMergeSorterStorage<T> storage, StreamMerger<K, T> merger) {
			super(eventloop);
			this.itemsInMemorySize = itemsInMemorySize;
			this.itemComparator = itemComparator;
			this.storage = storage;
			this.merger = merger;
			this.list = new ArrayList<>(this.itemsInMemorySize + (this.itemsInMemorySize >> 4));
			this.listOfPartitions = new ArrayList<>();
			this.forwarder = new StreamForwarder<>(eventloop);
		}

		@Override
		protected void onError(Exception e) {
			StreamProducers.<T>closingWithError(eventloop, e).streamTo(merger.newInput());
			merger.getOutput().streamTo(forwarder.getInput());
		}

		@Override
		public StreamDataReceiver<T> getDataReceiver() {
			return this;
		}

		@Override
		public void onData(T item) {
			assert jmxItems != ++jmxItems;<commit_msg>Remove excessive memory allocation from StreamSorter<commit_after>/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * 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 io.datakernel.stream.processor;

import com.google.common.base.Function;
import io.datakernel.async.CompletionCallback;
import io.datakernel.eventloop.Eventloop;
import io.datakernel.stream.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Represent {@link StreamTransformer} which receives data and saves it in collection, when it
 * receive end of stream it sorts it and streams to destination.
 *
 * @param <K> type of keys
 * @param <T> type of objects
 */
public final class StreamSorter<K, T> implements StreamTransformer<T, T>, StreamSorterMBean {
	protected long jmxItems;

	private InputConsumer inputConsumer;

	private final class InputConsumer extends AbstractStreamConsumer<T> implements StreamDataReceiver<T> {
		private final StreamMergeSorterStorage<T> storage;
		private final Comparator<T> itemComparator;
		private final StreamMerger<K, T> merger;
		private List<Integer> listOfPartitions;
		private final int itemsInMemorySize;
		private ArrayList<T> list;
		private StreamForwarder<T> forwarder;

		private boolean writing;

		protected InputConsumer(Eventloop eventloop, int itemsInMemorySize, Comparator<T> itemComparator, StreamMergeSorterStorage<T> storage, StreamMerger<K, T> merger) {
			super(eventloop);
			this.itemsInMemorySize = itemsInMemorySize;
			this.itemComparator = itemComparator;
			this.storage = storage;
			this.merger = merger;
			this.list = new ArrayList<>();
			this.listOfPartitions = new ArrayList<>();
			this.forwarder = new StreamForwarder<>(eventloop);
		}

		@Override
		protected void onError(Exception e) {
			StreamProducers.<T>closingWithError(eventloop, e).streamTo(merger.newInput());
			merger.getOutput().streamTo(forwarder.getInput());
		}

		@Override
		public StreamDataReceiver<T> getDataReceiver() {
			return this;
		}

		@Override
		public void onData(T item) {
			assert jmxItems != ++jmxItems;
{'fraction_non_alphanumeric': 0.09074377159483542, 'fraction_numerical': 0.0030914711765775596, 'mean_word_length': 4.96529284164859, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 45, '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': '5713636', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1526, 'n_words': 498}