io.datakernel.stream.processor.AbstractStreamReducer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of async-streams Show documentation
Show all versions of async-streams Show documentation
Composable asynchronous/reactive streams with powerful data processing capabilities.
The newest version!
/*
* 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.eventloop.Eventloop;
import io.datakernel.stream.AbstractStreamConsumer;
import io.datakernel.stream.AbstractStreamTransformer_M_1;
import io.datakernel.stream.StreamConsumer;
import io.datakernel.stream.StreamDataReceiver;
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.PriorityQueue;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Perform aggregative functions on the elements from input streams. Searches key of item
* with key function, selects elements with some key, reductions it and streams result sorted by key.
* Elements from stream to input must be sorted by keys. It is {@link AbstractStreamTransformer_M_1}
* because it represents few consumers and one producer.
*
* @param type of key of element
* @param type of output data
* @param type of accumulator
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class AbstractStreamReducer extends AbstractStreamTransformer_M_1 implements AbstractStreamReducerMBean {
public static final int BUFFER_SIZE = 1024;
private final int bufferSize;
private InternalConsumer> lastInput;
private K key = null;
private A accumulator;
private final PriorityQueue priorityQueue;
private int streamsAwaiting;
private int jmxInputItems;
private int jmxOnFirst;
private int jmxOnNext;
private int jmxOnComplete;
/**
* Creates a new instance of AbstractStreamReducer
*
* @param eventloop eventloop in which runs reducer
* @param keyComparator comparator for compare keys
* @param bufferSize maximal size of items which can be stored before reducing
*/
public AbstractStreamReducer(Eventloop eventloop, final Comparator keyComparator, int bufferSize) {
super(eventloop);
checkArgument(bufferSize >= 0, "bufferSize must be positive value, got %s", bufferSize);
this.bufferSize = bufferSize;
this.priorityQueue = new PriorityQueue<>(1, new Comparator() {
@Override
public int compare(InternalConsumer o1, InternalConsumer o2) {
int compare = ((Comparator) keyComparator).compare(o1.headKey, o2.headKey);
if (compare != 0)
return compare;
return o1.index - o2.index;
}
});
}
/**
* Creates a new instance of AbstractStreamReducer with default buffer size - 1024
*
* @param eventloop eventloop in which runs reducer
* @param keyComparator comparator for compare keys
*/
public AbstractStreamReducer(Eventloop eventloop, Comparator keyComparator) {
this(eventloop, keyComparator, BUFFER_SIZE);
}
/**
* This method is called if consumer was changed, checks if it has consumers, if not sets status
* end of stream.
*/
@Override
protected void onProducerStarted() {
if (inputs.isEmpty()) {
sendEndOfStream();
}
}
@Override
@SuppressWarnings("AssertWithSideEffects")
protected void doProduce() {
while (status == READY && streamsAwaiting == 0) {
InternalConsumer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy