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

org.apache.flink.runtime.state.gemini.subkeyed.GeminiSubKeyedValueStateImpl 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.gemini.subkeyed;

import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.runtime.state.StateAccessException;
import org.apache.flink.runtime.state.StateStorage;
import org.apache.flink.runtime.state.StateTransformationFunction;
import org.apache.flink.runtime.state.gemini.engine.GRegion;
import org.apache.flink.runtime.state.gemini.engine.hashtable.GRegionKMapImpl;
import org.apache.flink.runtime.state.gemini.engine.hashtable.GTableSubKeyedValueImpl;
import org.apache.flink.runtime.state.subkeyed.SubKeyedValueState;
import org.apache.flink.runtime.state.subkeyed.SubKeyedValueStateDescriptor;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
 * An implementation of {@link SubKeyedValueState} based on an {@link StateStorage}
 * The pairs in the state storage are formatted as {(K, N) -> V}, and are
 * partitioned by K.
 *
 * @param  Type of the keys in the state.
 * @param  Type of the namespaces in the state.
 * @param  Type of the values in the state.
 */
public final class GeminiSubKeyedValueStateImpl implements SubKeyedValueState {

	/**
	 * The descriptor of this state.
	 */
	private final SubKeyedValueStateDescriptor descriptor;

	private final GTableSubKeyedValueImpl table;
	/**
	 * Constructor with the state storage to store the values.
	 *
	 * @param descriptor The descriptor of this state.
	 * @param table
	 */
	public GeminiSubKeyedValueStateImpl(
		SubKeyedValueStateDescriptor descriptor,
		GTableSubKeyedValueImpl table
	) {
		this.descriptor = checkNotNull(descriptor);
		this.table = checkNotNull(table);
	}

	@Override
	public SubKeyedValueStateDescriptor getDescriptor() {
		return descriptor;
	}

	//--------------------------------------------------------------------------

	@Override
	public boolean contains(K key, N namespace) {
		if (key == null || namespace == null) {
			return false;
		}

		return getRegion(key).contains(key, namespace);
	}

	@Override
	public V get(K key, N namespace) {
		return getOrDefault(key, namespace, null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public V getOrDefault(K key, N namespace, V defaultValue) {
		if (key == null || namespace == null) {
			return defaultValue;
		}

		return getRegion(key).getOrDefault(key, namespace, defaultValue);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map getAll(K key) {
		if (key == null) {
			return Collections.emptyMap();
		}

		Map result = getRegion(key).get(key);
		return result == null ? Collections.emptyMap() : result;
	}

	@Override
	public void remove(K key, N namespace) {
		if (key == null || namespace == null) {
			return;
		}

		getRegion(key).remove(key, namespace);
	}

	@Override
	public void removeAll(K key) {
		if (key == null) {
			return;
		}

		getRegion(key).remove(key);
	}

	@Override
	public void put(K key, N namespace, V value) {
		checkNotNull(key);
		checkNotNull(namespace);

		getRegion(key).add(key, namespace, value);
	}

	@Override
	public V getAndRemove(K key, N namespace) {
		if (key == null || namespace == null) {
			return null;
		}

		V value = get(key, namespace);
		remove(key, namespace);
		return value;
	}

	@Override
	public Iterator iterator(K key) {
		checkNotNull(key);

		Map ret = getRegion(key).get(key);
		if (ret == null) {
			return Collections.emptyIterator();
		} else {
			Iterator namespaceIter = ret.keySet().iterator();
			return new Iterator() {
				N currentNamespace = null;

				@Override
				public boolean hasNext() {
					return namespaceIter.hasNext();
				}

				@Override
				public N next() {
					currentNamespace = namespaceIter.next();
					return currentNamespace;
				}

				@Override
				public void remove() {
					GeminiSubKeyedValueStateImpl.this.remove(key, currentNamespace);
				}
			};
		}
	}

	@Override
	public StateStorage getStateStorage() {
		throw new UnsupportedOperationException();
	}

	@Override
	public  void transform(K key, N namespace, T value, StateTransformationFunction transformation) {
		V oldValue = get(key, namespace);
		try {
			V newValue = transformation.apply(oldValue, value);
			put(key, namespace, newValue);
		} catch (Exception e) {
			throw new StateAccessException(e);
		}
	}

	@Override
	public Iterable keys(N namespace) {
		Iterator iterator = table.regionIterator();
		Set result = new HashSet<>();
		while (iterator.hasNext()) {
			GRegionKMapImpl cur = (GRegionKMapImpl) iterator.next();
			result.addAll(
				cur.getAll().entrySet().stream()
					.filter(a -> a.getValue().keySet().contains(namespace))
					.map(a -> a.getKey())
					.collect(Collectors.toSet()));
		}
		return result.size() == 0 ? null : result;
	}

	@Override
	public byte[] getSerializedValue(
		byte[] serializedKeyAndNamespace,
		TypeSerializer safeKeySerializer,
		TypeSerializer safeNamespaceSerializer,
		TypeSerializer safeValueSerializer) throws Exception {

		// TODO
		throw new UnsupportedOperationException();
	}

	@SuppressWarnings("unchecked")
	private GRegionKMapImpl getRegion(K key) {
		return table.getRegion(key);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy