lukecwik commented on code in PR #23491:
URL: https://github.com/apache/beam/pull/23491#discussion_r1004017417


##########
runners/flink/flink_runner.gradle:
##########
@@ -272,7 +272,9 @@ def createValidatesRunnerTask(Map m) {
       excludeCategories 'org.apache.beam.sdk.testing.UsesCommittedMetrics'
       excludeCategories 'org.apache.beam.sdk.testing.UsesSystemMetrics'
       excludeCategories 'org.apache.beam.sdk.testing.UsesStrictTimerOrdering'
+      excludeCategories 'org.apache.beam.sdk.testing.UsesMultimapState'
       excludeCategories 'org.apache.beam.sdk.testing.UsesLoopingTimer'
+      excludeCategories 'org.apache.beam.sdk.testing.UsesMultimapState'

Review Comment:
   ```suggestion
   ```



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/state/MultimapState.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.beam.sdk.state;
+
+import java.util.Map;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+
+/**
+ * A {@link ReadableState} cell mapping keys to bags of values.
+ *
+ * <p>Implementations of this form of state are expected to implement multimap 
operations
+ * efficiently as supported by some associated backing key-value store.
+ *
+ * @param <K> the type of keys maintained by this multimap
+ * @param <V> the type of mapped values
+ */
+@Experimental(Kind.STATE)
+public interface MultimapState<K, V> extends State {
+
+  /**
+   * Associates the specified value with the specified key in this multimap.
+   *
+   * <p>Changes will not be reflected in the results returned by previous 
calls to {@link
+   * ReadableState#read} on the results any of the reading methods({@link 
#get}, {@link #keys},
+   * {@link #entries}).
+   */
+  void put(K key, V value);
+
+  /**
+   * A deferred lookup, using null values if the item is not found.

Review Comment:
   Should this return an empty iterable instead of null?



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/state/MultimapState.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.beam.sdk.state;
+
+import java.util.Map;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+
+/**
+ * A {@link ReadableState} cell mapping keys to bags of values.
+ *
+ * <p>Implementations of this form of state are expected to implement multimap 
operations
+ * efficiently as supported by some associated backing key-value store.
+ *
+ * @param <K> the type of keys maintained by this multimap
+ * @param <V> the type of mapped values
+ */
+@Experimental(Kind.STATE)
+public interface MultimapState<K, V> extends State {
+
+  /**
+   * Associates the specified value with the specified key in this multimap.
+   *
+   * <p>Changes will not be reflected in the results returned by previous 
calls to {@link
+   * ReadableState#read} on the results any of the reading methods({@link 
#get}, {@link #keys},
+   * {@link #entries}).
+   */
+  void put(K key, V value);
+
+  /**
+   * A deferred lookup, using null values if the item is not found.
+   *
+   * <p>A user is encouraged to call {@code get} for all relevant keys and 
call {@code readLater()}
+   * on the results.
+   *
+   * <p>When {@code read} is called, a particular state implementation is 
encouraged to perform all
+   * pending reads in a single batch.
+   */
+  ReadableState<Iterable<V>> get(K key);
+
+  /**
+   * Removes all values associated with the key from this multimap if exists.
+   *
+   * <p>Changes will not be reflected in the results returned by previous 
calls to {@link
+   * ReadableState#read} on the results any of the reading methods({@link 
#get}, {@link #keys},
+   * {@link #entries}).
+   */
+  void remove(K key);
+
+  /** Returns an {@link Iterable} over the keys contained in this multimap. */
+  ReadableState<Iterable<K>> keys();
+
+  /** Returns an {@link Iterable} over all key-values pairs contained in this 
multimap. */

Review Comment:
   ```suggestion
     /** Returns an {@link Iterable} over all key-value pairs contained in this 
multimap. */
   ```



##########
runners/core-java/src/main/java/org/apache/beam/runners/core/InMemoryStateInternals.java:
##########
@@ -463,6 +475,114 @@ public InMemoryBag<T> copy() {
     }
   }
 
+  /** An {@link InMemoryState} implementation of {@link MultimapState}. */
+  public static final class InMemoryMultimap<K, V>
+      implements MultimapState<K, V>, InMemoryState<InMemoryMultimap<K, V>> {
+    private final Coder<K> keyCoder;
+    private final Coder<V> valueCoder;
+    private Multimap<K, V> contents = ArrayListMultimap.create();

Review Comment:
   Using a map that stores keys only works if the key coder is consistent with 
equals otherwise the keys that you need to use are the `Coder#structuralValue`. 
This is a bug in the InMemoryMap implementation as well.
   
   You can test this by using `byte[]` keys that have the same contents but are 
different instances.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to