Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import java.io.IOException;
/**
* Generic implementation of {@link ReducingState} based on a wrapped {@link ValueState}.
*
* @param The type of the key.
* @param The type of the namespace.
* @param The type of the values stored in this {@code ReducingState}.
* @param The type of {@link AbstractStateBackend} that manages this {@code KvState}.
* @param Generic type that extends both the underlying {@code ValueState} and {@code KvState}.
*/
public class GenericReducingState & KvState, ValueStateDescriptor, Backend>>
implements ReducingState, KvState, ReducingStateDescriptor, Backend> {
private final W wrappedState;
private final ReduceFunction reduceFunction;
/**
* Creates a new {@code ReducingState} that wraps the given {@link ValueState}. The
* {@code ValueState} must have a default value of {@code null}.
*
* @param wrappedState The wrapped {@code ValueState}
* @param reduceFunction The {@code ReduceFunction} to use for combining values.
*/
@SuppressWarnings("unchecked")
public GenericReducingState(ValueState wrappedState, ReduceFunction reduceFunction) {
if (!(wrappedState instanceof KvState)) {
throw new IllegalArgumentException("Wrapped state must be a KvState.");
}
this.wrappedState = (W) wrappedState;
this.reduceFunction = reduceFunction;
}
@Override
public void setCurrentKey(K key) {
wrappedState.setCurrentKey(key);
}
@Override
public void setCurrentNamespace(N namespace) {
wrappedState.setCurrentNamespace(namespace);
}
@Override
public KvStateSnapshot, ReducingStateDescriptor, Backend> snapshot(
long checkpointId,
long timestamp) throws Exception {
KvStateSnapshot, ValueStateDescriptor, Backend> wrappedSnapshot = wrappedState.snapshot(
checkpointId,
timestamp);
return new Snapshot<>(wrappedSnapshot, reduceFunction);
}
@Override
public void dispose() {
wrappedState.dispose();
}
@Override
public T get() throws Exception {
return wrappedState.value();
}
@Override
public void add(T value) throws Exception {
T currentValue = wrappedState.value();
if (currentValue == null) {
wrappedState.update(value);
} else {
wrappedState.update(reduceFunction.reduce(currentValue, value));
}
}
@Override
public void clear() {
wrappedState.clear();
}
private static class Snapshot implements KvStateSnapshot, ReducingStateDescriptor, Backend> {
private static final long serialVersionUID = 1L;
private final KvStateSnapshot, ValueStateDescriptor, Backend> wrappedSnapshot;
private final ReduceFunction reduceFunction;
public Snapshot(KvStateSnapshot, ValueStateDescriptor, Backend> wrappedSnapshot,
ReduceFunction reduceFunction) {
this.wrappedSnapshot = wrappedSnapshot;
this.reduceFunction = reduceFunction;
}
@Override
@SuppressWarnings("unchecked")
public KvState, ReducingStateDescriptor, Backend> restoreState(
Backend stateBackend,
TypeSerializer keySerializer,
ClassLoader classLoader) throws Exception {
return new GenericReducingState((ValueState) wrappedSnapshot.restoreState(stateBackend, keySerializer, classLoader), reduceFunction);
}
@Override
public void discardState() throws Exception {
wrappedSnapshot.discardState();
}
@Override
public long getStateSize() throws Exception {
return wrappedSnapshot.getStateSize();
}
@Override
public void close() throws IOException {
wrappedSnapshot.close();
}
}
}