You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/02/21 09:52:48 UTC

[GitHub] aljoscha commented on a change in pull request #7496: [FLINK-11323] Upgrade KryoSerializer snapshot to implement new TypeSerializerSnapshot interface

aljoscha commented on a change in pull request #7496: [FLINK-11323] Upgrade KryoSerializer snapshot to implement new TypeSerializerSnapshot interface
URL: https://github.com/apache/flink/pull/7496#discussion_r258032514
 
 

 ##########
 File path: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/kryo/OptionalMap.java
 ##########
 @@ -0,0 +1,248 @@
+/*
+ * 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.api.java.typeutils.runtime.kryo;
+
+import org.apache.flink.annotation.VisibleForTesting;
+
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * An OptionalMap is an order preserving map (like {@link LinkedHashMap}) where keys have a unique string name, but are
+ * optionally present, and the values are optional.
+ */
+final class OptionalMap<K, V> {
+
+
+	// --------------------------------------------------------------------------------------------------------
+	// Factory
+	// --------------------------------------------------------------------------------------------------------
+
+	/**
+	 * Creates an {@code OptionalMap} from the provided map.
+	 *
+	 * <p>This method is the equivalent of {@link Optional#of(Object)} but for maps. To support more than one {@code NULL}
+	 * key, an optional map requires a unique string name to be associated with each key (provided by keyNameGetter)
+	 *
+	 * @param sourceMap     a source map to wrap as an optional map.
+	 * @param keyNameGetter function that assigns a unique name to the keys of the source map.
+	 * @param <K>           key type
+	 * @param <V>           value type
+	 * @return an {@code OptionalMap} with optional named keys, and optional values.
+	 */
+	static <K, V> OptionalMap<K, V> optionalMapOf(LinkedHashMap<K, V> sourceMap, Function<K, String> keyNameGetter) {
+
+		LinkedHashMap<String, KeyValue<K, V>> underlyingMap = new LinkedHashMap<>(sourceMap.size());
+
+		sourceMap.forEach((k, v) -> {
+			String keyName = keyNameGetter.apply(k);
+			underlyingMap.put(keyName, new KeyValue<>(k, v));
+		});
+
+		return new OptionalMap<>(underlyingMap);
+	}
+
+	/**
+	 * Tries to merges the keys and the values of @right into @left.
+	 */
+	static <K, V> MergeResult<K, V> mergeRightIntoLeft(OptionalMap<K, V> left, OptionalMap<K, V> right) {
+		OptionalMap<K, V> merged = new OptionalMap<>(left);
+		merged.putAll(right);
+
+		return new MergeResult<>(merged, isLeftPrefixOfRight(left, right));
+	}
+
+	// --------------------------------------------------------------------------------------------------------
+	// Constructor
+	// --------------------------------------------------------------------------------------------------------
+
+	private final LinkedHashMap<String, KeyValue<K, V>> underlyingMap;
+
+	OptionalMap() {
+		this(new LinkedHashMap<>());
+	}
+
+	@SuppressWarnings("CopyConstructorMissesField")
+	OptionalMap(OptionalMap<K, V> optionalMap) {
+		this(new LinkedHashMap<>(optionalMap.underlyingMap));
+	}
+
+	private OptionalMap(LinkedHashMap<String, KeyValue<K, V>> underlyingMap) {
+		this.underlyingMap = checkNotNull(underlyingMap);
+	}
+
+	// --------------------------------------------------------------------------------------------------------
+	// API
+	// --------------------------------------------------------------------------------------------------------
+
+	int size() {
+		return underlyingMap.size();
+	}
+
+	void put(String keyName, @Nullable K key, @Nullable V value) {
+		checkNotNull(keyName);
+
+		underlyingMap.compute(keyName, (unused, kv) ->
+			(kv == null) ? new KeyValue<>(key, value) : kv.merge(key, value));
+	}
+
+	void putAll(OptionalMap<K, V> right) {
+		for (Entry<String, KeyValue<K, V>> entry : right.underlyingMap.entrySet()) {
+			KeyValue<K, V> kv = entry.getValue();
+			this.put(entry.getKey(), kv.key, kv.value);
+		}
+	}
+
+	/**
+	 * returns the key names of any keys or values that are absent.
+	 */
+	Set<String> absentKeysOrValues() {
+		return underlyingMap.entrySet()
+			.stream()
+			.filter(OptionalMap::keyOrValueIsAbsent)
+			.map(Entry::getKey)
+			.collect(Collectors.toSet());
+	}
+
+	/**
+	 * assuming all the entries of this map are present (keys and values) this method would return
+	 * a map with these key and values, striped from their Optional wrappers.
+	 * NOTE: please note that if any of the key or values are absent this method would throw an {@link IllegalStateException}.
+	 */
+	LinkedHashMap<K, V> unwrapOptionals() {
+		final LinkedHashMap<K, V> unwrapped = new LinkedHashMap<>(underlyingMap.size());
+
+		for (Entry<String, KeyValue<K, V>> entry : underlyingMap.entrySet()) {
+			String namedKey = entry.getKey();
+			KeyValue<K, V> kv = entry.getValue();
+			if (kv.key == null) {
+				throw new IllegalStateException("Missing key '" + namedKey + "'");
+			}
+			if (kv.value == null) {
+				throw new IllegalStateException("Missing value for the key '" + namedKey + "'");
+			}
+			unwrapped.put(kv.key, kv.value);
+		}
+
+		return unwrapped;
+	}
+
+	/**
+	 * returns the key names added to this map.
 
 Review comment:
   Nitpick: lowercase `returns` -> `Returns`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services