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


##########
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:
   Done



##########
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:
   Done



##########
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:
   Done



##########
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:
   Done



##########
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:
   I have removed the 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);
+        }
+
+        @Override
+        public ReadableState<Iterable<Map.Entry<K, Iterable<V>>>> readLater() {
+          return this;
+        }
+      };
+    }
+
+    @Override

Review Comment:
   Done



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