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

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

There is a newer version: 1.13.6
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.state.ListState;
import org.apache.flink.core.fs.FSDataOutputStream;
import org.apache.flink.core.memory.DataOutputView;
import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
import org.apache.flink.util.Preconditions;

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

/**
 * Implementation of operator list state.
 *
 * @param  the type of an operator state partition.
 */
public final class PartitionableListState implements ListState {

	/**
	 * Meta information of the state, including state name, assignment mode, and typeSerializer
	 */
	private RegisteredOperatorStateBackendMetaInfo stateMetaInfo;

	/**
	 * The internal list the holds the elements of the state
	 */
	private final ArrayList internalList;

	/**
	 * A typeSerializer that allows to perform deep copies of internalList
	 */
	private final ArrayListSerializer internalListCopySerializer;

	PartitionableListState(RegisteredOperatorStateBackendMetaInfo stateMetaInfo) {
		this(stateMetaInfo, new ArrayList());
	}

	private PartitionableListState(
			RegisteredOperatorStateBackendMetaInfo stateMetaInfo,
			ArrayList internalList) {

		this.stateMetaInfo = Preconditions.checkNotNull(stateMetaInfo);
		this.internalList = Preconditions.checkNotNull(internalList);
		this.internalListCopySerializer = new ArrayListSerializer<>(stateMetaInfo.getPartitionStateSerializer());
	}

	private PartitionableListState(PartitionableListState toCopy) {

		this(toCopy.stateMetaInfo.deepCopy(), toCopy.internalListCopySerializer.copy(toCopy.internalList));
	}

	public void setStateMetaInfo(RegisteredOperatorStateBackendMetaInfo stateMetaInfo) {
		this.stateMetaInfo = stateMetaInfo;
	}

	public RegisteredOperatorStateBackendMetaInfo getStateMetaInfo() {
		return stateMetaInfo;
	}

	public PartitionableListState deepCopy() {
		return new PartitionableListState<>(this);
	}

	@Override
	public void clear() {
		internalList.clear();
	}

	@Override
	public Iterable get() {
		return internalList;
	}

	@Override
	public void add(S value) {
		Preconditions.checkNotNull(value, "You cannot add null to a ListState.");
		internalList.add(value);
	}

	@Override
	public String toString() {
		return "PartitionableListState{" +
				"stateMetaInfo=" + stateMetaInfo +
				", internalList=" + internalList +
				'}';
	}

	public long[] write(FSDataOutputStream out) throws IOException {

		long[] partitionOffsets = new long[internalList.size()];

		DataOutputView dov = new DataOutputViewStreamWrapper(out);

		for (int i = 0; i < internalList.size(); ++i) {
			S element = internalList.get(i);
			partitionOffsets[i] = out.getPos();
			getStateMetaInfo().getPartitionStateSerializer().serialize(element, dov);
		}

		return partitionOffsets;
	}

	@Override
	public void update(List values) {
		internalList.clear();

		addAll(values);
	}

	@Override
	public void addAll(List values) {
		if (values != null && !values.isEmpty()) {
			internalList.addAll(values);
		}
	}
}