All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.state.PriorityQueueStateMetaInfoSnapshot Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * 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.typeutils.TypeSerializer;
import org.apache.flink.api.common.typeutils.TypeSerializerConfigSnapshot;
import org.apache.flink.api.common.typeutils.TypeSerializerSerializationUtil;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.core.memory.DataInputView;
import org.apache.flink.core.memory.DataOutputView;


import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Generalized snapshot for meta information about one state in a state backend
 * (e.g. {@link PriorityQueueStateMetaInfoSnapshot}).
 */
public class PriorityQueueStateMetaInfoSnapshot {

	/** The name of the state. */
	private final String name;

	private final TypeSerializer elementSerializer;

	public PriorityQueueStateMetaInfoSnapshot(
		@Nonnull String name,
		@Nonnull TypeSerializer serializer) {
		this.name = name;
		this.elementSerializer = serializer;
	}

	public String getName() {
		return name;
	}

	@Nullable
	public TypeSerializer getSerializer() {
		return elementSerializer;
	}

	public void write(@Nonnull DataOutputView outputView) throws IOException {
		CurrentWriterImpl.INSTANCE.writeStateMetaInfoSnapshot(this, outputView);
	}

	public static PriorityQueueStateMetaInfoSnapshot read(@Nonnull DataInputView inputView, @Nonnull ClassLoader userCodeClassLoader) throws IOException {
		return CurrentReaderImpl.INSTANCE.readStateMetaInfoSnapshot(inputView, userCodeClassLoader);
	}

	/**
	 * Implementation of writer for {@link RegisteredPriorityQueueStateBackendMetaInfo}.
	 */
	public static class CurrentWriterImpl {

		private static final CurrentWriterImpl INSTANCE = new CurrentWriterImpl();

		public void writeStateMetaInfoSnapshot(
			@Nonnull PriorityQueueStateMetaInfoSnapshot snapshot,
			@Nonnull DataOutputView outputView) throws IOException {
			outputView.writeUTF(snapshot.name);
			List, TypeSerializerConfigSnapshot>> serializersWithConfig = new ArrayList<>(1);
			serializersWithConfig.add(new Tuple2<>(snapshot.elementSerializer, snapshot.elementSerializer.snapshotConfiguration()));
			TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience(outputView, serializersWithConfig);
		}
	}

	/**
	 * Implementation for the current version and generic for all state types.
	 */
	public static class CurrentReaderImpl {

		private static final CurrentReaderImpl INSTANCE = new CurrentReaderImpl();

		@Nonnull
		public PriorityQueueStateMetaInfoSnapshot readStateMetaInfoSnapshot(
			@Nonnull DataInputView inputView,
			@Nonnull ClassLoader userCodeClassLoader) throws IOException {

			final String stateName = inputView.readUTF();

			final List, TypeSerializerConfigSnapshot>> serializersWithConfig =
				TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(inputView, userCodeClassLoader);
			if (serializersWithConfig.size() >= 1) {
				return new PriorityQueueStateMetaInfoSnapshot(stateName, serializersWithConfig.get(0).f0);
			} else {
				throw new IOException("unexpected snapshot for PriorityQueueStateMetaInfoSnapshot");
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy