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


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/state/MultimapState.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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. Keys are 
considered equivalent if
+ * their structural values are equivalent, see
+ * {@link org.apache.beam.sdk.coders.Coder#structuralValue} for additional 
details.
+ *
+ * <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. 
Existing values
+   * associated with the same key will not be removed.
+   *
+   * <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, returns an empty iterable 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.

Review Comment:
   ```suggestion
      * Removes all values associated with the key from this multimap. This is 
a no-op if the key is not contained within the multimap.
   ```



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/state/MultimapState.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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. Keys are 
considered equivalent if
+ * their structural values are equivalent, see
+ * {@link org.apache.beam.sdk.coders.Coder#structuralValue} for additional 
details.
+ *
+ * <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. 
Existing values
+   * associated with the same key will not be removed.
+   *
+   * <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, returns an empty iterable 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},

Review Comment:
   ```suggestion
      * ReadableState#read} on the results of any of the reading methods({@link 
#get}, {@link #keys},
   ```



##########
runners/core-java/src/main/java/org/apache/beam/runners/core/InMemoryStateInternals.java:
##########
@@ -463,6 +476,121 @@ 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<Object, V> contents = ArrayListMultimap.create();
+    private Map<Object, K> structuralKeysMapping = Maps.newHashMap();
+
+    public InMemoryMultimap(Coder<K> keyCoder, Coder<V> valueCoder) {
+      this.keyCoder = keyCoder;
+      this.valueCoder = valueCoder;
+    }
+
+    @Override
+    public void clear() {
+      contents = ArrayListMultimap.create();
+      structuralKeysMapping = Maps.newHashMap();
+    }
+
+    @Override
+    public void put(K key, V value) {
+      Object structuralKey = keyCoder.structuralValue(key);
+      structuralKeysMapping.put(structuralKey, key);
+      contents.put(structuralKey, value);
+    }
+
+    @Override
+    public void remove(K key) {
+      Object structuralKey = keyCoder.structuralValue(key);
+      structuralKeysMapping.remove(structuralKey);
+      contents.removeAll(structuralKey);
+    }
+
+    @Override
+    public ReadableState<Iterable<V>> get(K key) {
+      return 
CollectionViewState.of(contents.get(keyCoder.structuralValue(key)));

Review Comment:
   This returns a stale view in this order of operations:
   ```
   mm.put("A", 1);
   ReadableState<Iterable<V>> iter = mm.get("A");
   mm.clear();
   iter.read(); <-- will contain "1"
   ```
   
   I believe there are other combinations where you won't get what you want. 
Please ensure that ParDoTest covers:
   * `get` before and after remove/put/clear.
   * `containsKey` before and after remove/put/clear
   * `entries` before and after remove/put/clear
   * `isEmpty` before and after remove/put/clear
   * `keys` fore and after remove/put/clear
   
   Currently I see your covering entries before and after put/remove.
   
   



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateSpecs.java:
##########
@@ -247,6 +247,44 @@ public static <T> StateSpec<OrderedListState<T>> 
orderedList(Coder<T> elemCoder)
   public static StateSpec<OrderedListState<Row>> rowOrderedList(Schema 
valueSchema) {
     return new OrderedListStateSpec<>(RowCoder.of(valueSchema));
   }
+
+  /**
+   * Create a {@link StateSpec} for a {@link MultimapState}, optimized for key 
lookups and writes.
+   *
+   * <p>This method attempts to infer the key and value coders automatically.
+   *
+   * <p>If the key and value types have schemas registered, then the schemas 
will be used to encode
+   * the elements.
+   *
+   * @see #multimap(Coder, Coder)
+   */
+  public static <K, V> StateSpec<MultimapState<K, V>> multimap() {
+    return new MultimapStateSpec<>(null, null);
+  }
+
+  /**
+   * Create a {@link StateSpec} for a {@link MultimapState}, optimized for key 
lookups and writes.

Review Comment:
   ```suggestion
      * Create a {@link StateSpec} for a {@link MultimapState}, optimized for 
key lookups, key puts, and clear.
   ```



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateSpecs.java:
##########
@@ -247,6 +247,44 @@ public static <T> StateSpec<OrderedListState<T>> 
orderedList(Coder<T> elemCoder)
   public static StateSpec<OrderedListState<Row>> rowOrderedList(Schema 
valueSchema) {
     return new OrderedListStateSpec<>(RowCoder.of(valueSchema));
   }
+
+  /**
+   * Create a {@link StateSpec} for a {@link MultimapState}, optimized for key 
lookups and writes.

Review Comment:
   ```suggestion
      * Create a {@link StateSpec} for a {@link MultimapState}, optimized for 
key lookups, key puts, and clear.
   ```



##########
runners/core-java/src/main/java/org/apache/beam/runners/core/InMemoryStateInternals.java:
##########
@@ -463,6 +476,116 @@ 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();
+
+    public InMemoryMultimap(Coder<K> keyCoder, Coder<V> valueCoder) {
+      this.keyCoder = keyCoder;
+      this.valueCoder = valueCoder;
+    }
+
+    @Override
+    public void clear() {
+      contents = ArrayListMultimap.create();

Review Comment:
   +1



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/testing/UsesMultimapState.java:
##########
@@ -0,0 +1,24 @@
+/*
+ * 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.testing;
+
+import org.apache.beam.sdk.annotations.Internal;
+
+/** Category tag for validation tests which utilize {@link UsesMultimapState}. 
*/

Review Comment:
   ```suggestion
   import org.apache.beam.sdk.state.MultimapState;
   
   /** Category tag for validation tests which utilize {@link MultimapState}. */
   ```



##########
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:
   I missed that we are returning `Entry<K, Iterable<V>>` which is why I 
suggested the swap to `key-value` over `key-values`.
   
   Note that other map APIs use `Entry<K, V>`, see:
   
https://guava.dev/releases/23.0/api/docs/com/google/common/collect/ArrayListMultimap.html#entries--
   
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiValuedMap.html#entries--
   
   It would likely make sense to match what existing common libraries do on 
this front so we are idiomatic for Java users.



-- 
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