org.apache.flink.runtime.state.memory.AbstractMemState 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.memory;
import org.apache.flink.api.common.state.ListState;
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.AbstractHeapState;
import org.apache.flink.runtime.state.KvStateSnapshot;
import org.apache.flink.runtime.util.DataOutputSerializer;
import java.util.HashMap;
import java.util.Map;
/**
* Base class for partitioned {@link ListState} implementations that are backed by a regular
* heap hash map. The concrete implementations define how the state is checkpointed.
*
* @param The type of the key.
* @param The type of the namespace.
* @param The type of the values in the state.
* @param The type of State
* @param The type of StateDescriptor for the State S
*/
public abstract class AbstractMemState>
extends AbstractHeapState {
public AbstractMemState(TypeSerializer keySerializer,
TypeSerializer namespaceSerializer,
TypeSerializer stateSerializer,
SD stateDesc) {
super(keySerializer, namespaceSerializer, stateSerializer, stateDesc);
}
public AbstractMemState(TypeSerializer keySerializer,
TypeSerializer namespaceSerializer,
TypeSerializer stateSerializer,
SD stateDesc,
HashMap> state) {
super(keySerializer, namespaceSerializer, stateSerializer, stateDesc, state);
}
public abstract KvStateSnapshot createHeapSnapshot(byte[] bytes);
@Override
public KvStateSnapshot snapshot(long checkpointId, long timestamp) throws Exception {
DataOutputSerializer out = new DataOutputSerializer(Math.max(size() * 16, 16));
out.writeInt(state.size());
for (Map.Entry> namespaceState: state.entrySet()) {
N namespace = namespaceState.getKey();
namespaceSerializer.serialize(namespace, out);
out.writeInt(namespaceState.getValue().size());
for (Map.Entry entry: namespaceState.getValue().entrySet()) {
keySerializer.serialize(entry.getKey(), out);
stateSerializer.serialize(entry.getValue(), out);
}
}
byte[] bytes = out.getCopyOfBuffer();
return createHeapSnapshot(bytes);
}
}