org.apache.flink.runtime.state.heap.HeapAggregatingState Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.flink.runtime.state.heap;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.state.State;
import org.apache.flink.api.common.state.StateDescriptor;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.runtime.state.StateTransformationFunction;
import org.apache.flink.runtime.state.internal.InternalAggregatingState;
import org.apache.flink.util.Preconditions;
import java.io.IOException;
/**
* Heap-backed partitioned {@link AggregatingState} that is
* snapshotted into files.
*
* @param The type of the key.
* @param The type of the namespace.
* @param The type of the value added to the state.
* @param The type of the value stored in the state (the accumulator type).
* @param The type of the value returned from the state.
*/
class HeapAggregatingState
extends AbstractHeapMergingState
implements InternalAggregatingState {
private final AggregateTransformation aggregateTransformation;
/**
* Creates a new key/value state for the given hash map of key/value pairs.
*
* @param stateTable The state table for which this state is associated to.
* @param keySerializer The serializer for the keys.
* @param valueSerializer The serializer for the state.
* @param namespaceSerializer The serializer for the namespace.
* @param defaultValue The default value for the state.
* @param aggregateFunction The aggregating function used for aggregating state.
*/
private HeapAggregatingState(
StateTable stateTable,
TypeSerializer keySerializer,
TypeSerializer valueSerializer,
TypeSerializer namespaceSerializer,
ACC defaultValue,
AggregateFunction aggregateFunction) {
super(stateTable, keySerializer, valueSerializer, namespaceSerializer, defaultValue);
this.aggregateTransformation = new AggregateTransformation<>(aggregateFunction);
}
@Override
public TypeSerializer getKeySerializer() {
return keySerializer;
}
@Override
public TypeSerializer getNamespaceSerializer() {
return namespaceSerializer;
}
@Override
public TypeSerializer getValueSerializer() {
return valueSerializer;
}
// ------------------------------------------------------------------------
// state access
// ------------------------------------------------------------------------
@Override
public OUT get() {
ACC accumulator = getInternal();
return accumulator != null ? aggregateTransformation.aggFunction.getResult(accumulator) : null;
}
@Override
public void add(IN value) throws IOException {
final N namespace = currentNamespace;
if (value == null) {
clear();
return;
}
try {
stateTable.transform(namespace, value, aggregateTransformation);
} catch (Exception e) {
throw new IOException("Exception while applying AggregateFunction in aggregating state", e);
}
}
// ------------------------------------------------------------------------
// state merging
// ------------------------------------------------------------------------
@Override
protected ACC mergeState(ACC a, ACC b) {
return aggregateTransformation.aggFunction.merge(a, b);
}
static final class AggregateTransformation implements StateTransformationFunction {
private final AggregateFunction aggFunction;
AggregateTransformation(AggregateFunction aggFunction) {
this.aggFunction = Preconditions.checkNotNull(aggFunction);
}
@Override
public ACC apply(ACC accumulator, IN value) {
if (accumulator == null) {
accumulator = aggFunction.createAccumulator();
}
return aggFunction.add(value, accumulator);
}
}
@SuppressWarnings("unchecked")
static IS create(
StateDescriptor stateDesc,
StateTable stateTable,
TypeSerializer keySerializer) {
return (IS) new HeapAggregatingState<>(
stateTable,
keySerializer,
stateTable.getStateSerializer(),
stateTable.getNamespaceSerializer(),
stateDesc.getDefaultValue(),
((AggregatingStateDescriptor) stateDesc).getAggregateFunction());
}
}