You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/11/22 19:29:05 UTC

[GitHub] [beam] reuvenlax commented on a diff in pull request #23491: Add MultimapState API

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


##########
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();
+    }
+
+    @Override
+    public void put(K key, V value) {
+      contents.put(key, value);
+    }
+
+    @Override
+    public void remove(K key) {
+      contents.removeAll(key);
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized ReadableState<Iterable<V>> get(K key) {

Review Comment:
   Are these annotations auto generated?



##########
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 overwritten.
+   *
+   * <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.

Review Comment:
   Is this a point-in-time value or not? e.g. if the user writes
   
   ReadableState<Iterable<V>> values = mm.get(k);
   mm.put(k, newValue);
   
   Will values.read() then contain newValue or not?



##########
runners/core-java/src/main/java/org/apache/beam/runners/core/InMemoryStateInternals.java:
##########
@@ -463,6 +475,124 @@ 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 @UnknownKeyFor @NonNull @Initialized ReadableState<Iterable<V>> get(K key) {
+      return CollectionViewState.of(contents.get(keyCoder.structuralValue(key)));
+    }
+
+    @Override
+    public ReadableState<Iterable<K>> keys() {
+      return CollectionViewState.of(structuralKeysMapping.values());
+    }
+
+    @Override
+    public ReadableState<Iterable<Map.Entry<K, Iterable<V>>>> entries() {
+      return new ReadableState<Iterable<Map.Entry<K, Iterable<V>>>>() {
+        @Override
+        public Iterable<Map.Entry<K, Iterable<V>>> read() {
+          List<Map.Entry<K, Iterable<V>>> result = new ArrayList<>();
+          for (Map.Entry<Object, K> entry : structuralKeysMapping.entrySet()) {
+            result.add(
+                new AbstractMap.SimpleEntry(
+                    entry.getValue(), ImmutableList.copyOf(contents.get(entry.getKey()))));
+          }
+          return ImmutableList.copyOf(result);
+        }
+
+        @Override
+        public ReadableState<Iterable<Map.Entry<K, Iterable<V>>>> readLater() {
+          return this;
+        }
+      };
+    }
+
+    @Override

Review Comment:
   ditto on annotations



##########
runners/core-java/src/main/java/org/apache/beam/runners/core/InMemoryStateInternals.java:
##########
@@ -463,6 +475,124 @@ 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 @UnknownKeyFor @NonNull @Initialized ReadableState<Iterable<V>> get(K key) {
+      return CollectionViewState.of(contents.get(keyCoder.structuralValue(key)));
+    }
+
+    @Override
+    public ReadableState<Iterable<K>> keys() {
+      return CollectionViewState.of(structuralKeysMapping.values());
+    }
+
+    @Override
+    public ReadableState<Iterable<Map.Entry<K, Iterable<V>>>> entries() {
+      return new ReadableState<Iterable<Map.Entry<K, Iterable<V>>>>() {
+        @Override
+        public Iterable<Map.Entry<K, Iterable<V>>> read() {
+          List<Map.Entry<K, Iterable<V>>> result = new ArrayList<>();
+          for (Map.Entry<Object, K> entry : structuralKeysMapping.entrySet()) {
+            result.add(
+                new AbstractMap.SimpleEntry(
+                    entry.getValue(), ImmutableList.copyOf(contents.get(entry.getKey()))));
+          }
+          return ImmutableList.copyOf(result);

Review Comment:
   Collections.unmodifiableList, since it doesn't copy



##########
runners/core-java/src/main/java/org/apache/beam/runners/core/InMemoryStateInternals.java:
##########
@@ -463,6 +475,124 @@ 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 @UnknownKeyFor @NonNull @Initialized ReadableState<Iterable<V>> get(K key) {
+      return CollectionViewState.of(contents.get(keyCoder.structuralValue(key)));
+    }
+
+    @Override
+    public ReadableState<Iterable<K>> keys() {
+      return CollectionViewState.of(structuralKeysMapping.values());
+    }
+
+    @Override
+    public ReadableState<Iterable<Map.Entry<K, Iterable<V>>>> entries() {
+      return new ReadableState<Iterable<Map.Entry<K, Iterable<V>>>>() {
+        @Override
+        public Iterable<Map.Entry<K, Iterable<V>>> read() {
+          List<Map.Entry<K, Iterable<V>>> result = new ArrayList<>();
+          for (Map.Entry<Object, K> entry : structuralKeysMapping.entrySet()) {
+            result.add(
+                new AbstractMap.SimpleEntry(
+                    entry.getValue(), ImmutableList.copyOf(contents.get(entry.getKey()))));
+          }
+          return ImmutableList.copyOf(result);
+        }
+
+        @Override
+        public ReadableState<Iterable<Map.Entry<K, Iterable<V>>>> readLater() {
+          return this;
+        }
+      };
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized ReadableState<
+            @UnknownKeyFor @NonNull @Initialized Boolean>
+        containsKey(K key) {
+      return new ReadableState<Boolean>() {
+        @Override
+        public Boolean read() {
+          return structuralKeysMapping.containsKey(keyCoder.structuralValue(key));
+        }
+
+        @Override
+        public @UnknownKeyFor @NonNull @Initialized ReadableState<Boolean> readLater() {
+          return this;
+        }
+      };
+    }
+
+    @Override

Review Comment:
   annotations



##########
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();
+    }
+
+    @Override
+    public void put(K key, V value) {
+      contents.put(key, value);
+    }
+
+    @Override
+    public void remove(K key) {
+      contents.removeAll(key);
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized ReadableState<Iterable<V>> get(K key) {
+      return contents.containsKey(key)
+          ? CollectionViewState.of(contents.get(key))

Review Comment:
   Doesn't Multimap.get return an empty list if the key is not found? I don't think there's any need for this ternary operator.



##########
runners/core-java/src/main/java/org/apache/beam/runners/core/InMemoryStateInternals.java:
##########
@@ -463,6 +475,124 @@ 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 @UnknownKeyFor @NonNull @Initialized ReadableState<Iterable<V>> get(K key) {
+      return CollectionViewState.of(contents.get(keyCoder.structuralValue(key)));
+    }
+
+    @Override
+    public ReadableState<Iterable<K>> keys() {
+      return CollectionViewState.of(structuralKeysMapping.values());
+    }
+
+    @Override
+    public ReadableState<Iterable<Map.Entry<K, Iterable<V>>>> entries() {
+      return new ReadableState<Iterable<Map.Entry<K, Iterable<V>>>>() {
+        @Override
+        public Iterable<Map.Entry<K, Iterable<V>>> read() {
+          List<Map.Entry<K, Iterable<V>>> result = new ArrayList<>();

Review Comment:
   = Lists.arrayListWithExpectedSize(structuralKeysMapping.size()



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org