Myasuka commented on a change in pull request #17525:
URL: https://github.com/apache/flink/pull/17525#discussion_r742673540



##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/state/StateBackendTestBase.java
##########
@@ -5084,6 +5084,58 @@ public void testMapStateGetKeys() throws Exception {
         }
     }
 
+    @Test
+    public void testMapStateGetKeysAndNamespaces() throws Exception {
+        final int elementsNum = 1000;
+        String fieldName = "get-keys-test";
+        CheckpointableKeyedStateBackend<Integer> backend =
+                createKeyedBackend(IntSerializer.INSTANCE);
+        try {
+            final String ns1 = "ns1";
+            MapState<String, Integer> keyedState1 =
+                    backend.getPartitionedState(
+                            ns1,
+                            StringSerializer.INSTANCE,
+                            new MapStateDescriptor<>(
+                                    fieldName, StringSerializer.INSTANCE, 
IntSerializer.INSTANCE));
+
+            final String ns2 = "ns2";
+            MapState<String, Integer> keyedState2 =
+                    backend.getPartitionedState(
+                            ns2,
+                            StringSerializer.INSTANCE,
+                            new MapStateDescriptor<>(
+                                    fieldName, StringSerializer.INSTANCE, 
IntSerializer.INSTANCE));
+
+            for (int key = 0; key < elementsNum; key++) {
+                backend.setCurrentKey(key);
+                keyedState1.put("he", key * 2);
+                keyedState1.put("ho", key * 2);
+                keyedState2.put("he", key * 2);
+                keyedState2.put("ho", key * 2);
+            }
+
+            try (Stream<Tuple2<Integer, String>> stream = 
backend.getKeysAndNamespaces(fieldName)) {
+                final Map<String, Set<Integer>> keysByNamespace = new 
HashMap<>();
+                stream.forEach(
+                        entry -> {
+                            assertThat("Unexpected namespace", entry.f1, 
isOneOf(ns1, ns2));

Review comment:
       As I suggested before, you could leverage `createInternalState` API to 
reduce code duplication.
   
   I think code below looks better:
   ~~~ java
   InternalMapState<Integer, String, String, Integer> internalState = 
backend.createInternalState(
           StringSerializer.INSTANCE,
           new MapStateDescriptor<>(
                   fieldName, StringSerializer.INSTANCE, 
IntSerializer.INSTANCE));
   String[] namespaces = new String[] {"ns1", "ns2"};
   
   for (int key = 0; key < elementsNum; key++) {
       backend.setCurrentKey(key);
       for (String ns : namespaces) {
           internalState.setCurrentNamespace(ns);
           internalState.put("hello", key);
           internalState.put("world", key);
       }
   }
   
   try ...........
    assertThat("Unexpected namespace", entry.f1, isOneOf(namespaces)
   ~~~
   
   




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