org.apache.flink.runtime.state.keyed.KeyedMapStateImpl 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.keyed;
import org.apache.flink.api.common.typeutils.SerializationException;
import org.apache.flink.api.common.typeutils.base.StringSerializer;
import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
import org.apache.flink.runtime.state.AbstractInternalStateBackend;
import org.apache.flink.runtime.state.StateStorage;
import org.apache.flink.util.Preconditions;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* An implementation of {@link KeyedMapState} backed by a state storage.
*
* @param Type of the keys in the state.
* @param Type of the map keys in the state.
* @param Type of the map values in the state.
*/
public final class KeyedMapStateImpl
extends AbstractKeyedMapStateImpl>
implements KeyedMapState {
/**
* The descriptor of current state.
*/
private KeyedMapStateDescriptor stateDescriptor;
/**
* Constructor with the state storage to store mappings.
*
* @param internalStateBackend The state backend who creates the current state.
* @param descriptor The descriptor of current state.
* @param stateStorage The state storage where mappings are stored.
*/
public KeyedMapStateImpl(
AbstractInternalStateBackend internalStateBackend,
KeyedMapStateDescriptor descriptor,
StateStorage stateStorage) {
super(internalStateBackend, stateStorage);
this.stateDescriptor = Preconditions.checkNotNull(descriptor);
this.keySerializer = descriptor.getKeySerializer();
this.mapKeySerializer = descriptor.getMapKeySerializer();
this.mapValueSerializer = descriptor.getMapValueSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
StringSerializer.INSTANCE.serialize(descriptor.getName(), new DataOutputViewStreamWrapper(out));
stateNameByte = out.toByteArray();
} catch (IOException e) {
throw new SerializationException(e);
}
this.stateNameForSerialize = stateStorage.supportMultiColumnFamilies() ? null : stateNameByte;
this.serializedStateNameLength = stateNameForSerialize == null ? 0 : stateNameForSerialize.length;
}
@Override
public KeyedMapStateDescriptor getDescriptor() {
return stateDescriptor;
}
@Override
Map createMap() {
return new HashMap<>();
}
}