
com.hazelcast.jet.impl.processor.GroupP Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
*
* 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.hazelcast.jet.impl.processor;
import com.hazelcast.function.FunctionEx;
import com.hazelcast.jet.Traverser;
import com.hazelcast.jet.aggregate.AggregateOperation;
import com.hazelcast.jet.aggregate.AggregateOperation1;
import com.hazelcast.jet.core.AbstractProcessor;
import com.hazelcast.jet.core.Processor;
import com.hazelcast.jet.impl.memory.AccumulationLimitExceededException;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import java.util.function.Function;
import static com.hazelcast.internal.util.Preconditions.checkTrue;
import static java.util.Collections.singletonList;
/**
* Batch processor that groups items by key and computes the supplied
* aggregate operation on each group. The items may originate from one or
* more inbound edges. The supplied aggregate operation must have as many
* accumulation functions as there are inbound edges.
*/
public class GroupP extends AbstractProcessor {
protected final Map keyToAcc = new HashMap<>();
private final List> groupKeyFns;
private final AggregateOperation aggrOp;
private final BiFunction super K, ? super R, OUT> mapToOutputFn;
private long maxEntries;
private Traverser resultTraverser;
public GroupP(
@Nonnull List> groupKeyFns,
@Nonnull AggregateOperation aggrOp,
@Nonnull BiFunction super K, ? super R, OUT> mapToOutputFn
) {
checkTrue(groupKeyFns.size() == aggrOp.arity(), groupKeyFns.size() + " key functions " +
"provided for " + aggrOp.arity() + "-arity aggregate operation");
this.groupKeyFns = groupKeyFns;
this.aggrOp = aggrOp;
this.mapToOutputFn = mapToOutputFn;
}
public GroupP(
@Nonnull FunctionEx super T, ? extends K> groupKeyFn,
@Nonnull AggregateOperation1 super T, A, R> aggrOp,
@Nonnull BiFunction super K, ? super R, OUT> mapToOutputFn
) {
this(singletonList(groupKeyFn), aggrOp, mapToOutputFn);
}
@Override
protected void init(@Nonnull Processor.Context context) throws Exception {
maxEntries = context.maxProcessorAccumulatedRecords();
}
@Override
@SuppressWarnings("unchecked")
protected boolean tryProcess(int ordinal, @Nonnull Object item) {
Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy