You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@geode.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2017/10/23 17:44:00 UTC

[jira] [Commented] (GEODE-3884) Replace consumer with direct invocation in the examples

    [ https://issues.apache.org/jira/browse/GEODE-3884?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16215518#comment-16215518 ] 

ASF GitHub Bot commented on GEODE-3884:
---------------------------------------

upthewaterspout closed pull request #26: GEODE-3884: Replace consumer with direct invocation in the examples
URL: https://github.com/apache/geode-examples/pull/26
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeData.java b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeData.java
index 234458f..c623bf3 100644
--- a/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeData.java
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeData.java
@@ -17,11 +17,10 @@
 import java.io.Serializable;
 
 public class EmployeeData implements Serializable {
-  private static final long serialVersionUID = 1L;
-
-  private EmployeeKey nameAndNumber;
-  private int salary;
-  private int hoursPerWeek;
+  private static final long serialVersionUID = 2095541179L;
+  private final EmployeeKey nameAndNumber;
+  private final int salary;
+  private final int hoursPerWeek;
 
   public EmployeeData(EmployeeKey nameAndNumber, int salary, int hoursPerWeek) {
     this.nameAndNumber = nameAndNumber;
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeKey.java b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeKey.java
index e6b475d..f80396d 100644
--- a/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeKey.java
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeKey.java
@@ -17,10 +17,14 @@
 import java.io.Serializable;
 
 public class EmployeeKey implements Serializable {
-  private static final long serialVersionUID = 1L;
+  private static final long serialVersionUID = 160372860L;
+  private final String name;
+  private final int emplNumber;
 
-  private String name;
-  private int emplNumber;
+  public EmployeeKey(String name, int emplNumber) {
+    this.name = name;
+    this.emplNumber = emplNumber;
+  }
 
   public String getName() {
     return name;
@@ -30,12 +34,6 @@ public int getEmplNumber() {
     return emplNumber;
   }
 
-  public EmployeeKey(String name, int emplNumber) {
-    super();
-    this.name = name;
-    this.emplNumber = emplNumber;
-  }
-
   @Override
   public int hashCode() {
     int result = name.hashCode();
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Example.java b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Example.java
index d60894e..c40517e 100644
--- a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Example.java
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Example.java
@@ -15,47 +15,55 @@
 package org.apache.geode.examples.partitioned;
 
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Random;
-import java.util.function.Consumer;
+import java.util.Set;
 
 import org.apache.geode.cache.Region;
 import org.apache.geode.cache.client.ClientCache;
 import org.apache.geode.cache.client.ClientCacheFactory;
 import org.apache.geode.cache.client.ClientRegionShortcut;
 
-public class Example implements Consumer<Region<EmployeeKey, EmployeeData>> {
+public class Example {
+  private final Region<EmployeeKey, EmployeeData> region;
+
+  public Example(Region<EmployeeKey, EmployeeData> region) {
+    this.region = region;
+  }
+
   public static void main(String[] args) {
     // connect to the locator using default port 10334
     ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334)
         .set("log-level", "WARN").create();
 
     // create a local region that matches the server region
-    Region<EmployeeKey, EmployeeData> region = cache
-        .<EmployeeKey, EmployeeData>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
-        .create("example-region");
+    Region<EmployeeKey, EmployeeData> region =
+        cache.<EmployeeKey, EmployeeData>createClientRegionFactory(ClientRegionShortcut.PROXY)
+            .create("example-region");
+
+    Example example = new Example(region);
+    example.insertValues(new String[] {"Alex Able", "Bertie Bell", "Chris Call", "Dale Driver",
+        "Frankie Forth", "Jamie Jive", "Morgan Minnow", "Pat Pearson", "Ricky Reliable",
+        "Taylor Tack", "Zelda Zankowski"});
+    example.printValues(example.getValues());
 
-    new Example().accept(region);
     cache.close();
   }
 
-  @Override
-  public void accept(Region<EmployeeKey, EmployeeData> region) {
-    // insert values into the region
+  Set<EmployeeKey> getValues() {
+    return new HashSet<>(region.keySetOnServer());
+  }
+
+  void insertValues(String[] names) {
     Random r = new Random();
-    String[] names =
-        "Alex Able,Bertie Bell,Kris Call,Dale Driver,Frankie Forth,Jamie Jive,Morgan Minnow,Pat Puts,Ricky Reliable,Taylor Tack"
-            .split(",");
     Arrays.stream(names).forEach(name -> {
-      EmployeeKey key = new EmployeeKey(name, r.nextInt());
-      EmployeeData val = new EmployeeData(key, r.nextInt(), 40);
+      EmployeeKey key = new EmployeeKey(name, 1 + r.nextInt(1000000));
+      EmployeeData val = new EmployeeData(key, 50000 + r.nextInt(100000), 40);
       region.put(key, val);
     });
+  }
 
-    // count the values in the region
-    int inserted = region.keySetOnServer().size();
-    System.out.println(String.format("Counted %d keys in region %s", inserted, region.getName()));
-
-    // fetch the values in the region
-    region.keySetOnServer().forEach(key -> System.out.println(region.get(key)));
+  void printValues(Set<EmployeeKey> values) {
+    values.forEach(key -> System.out.println(key.getName() + " -> " + region.get(key)));
   }
 }
diff --git a/partitioned/src/test/java/org/apache/geode/examples/partitioned/ExampleTest.java b/partitioned/src/test/java/org/apache/geode/examples/partitioned/ExampleTest.java
deleted file mode 100644
index 757e664..0000000
--- a/partitioned/src/test/java/org/apache/geode/examples/partitioned/ExampleTest.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.geode.examples.partitioned;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import org.apache.geode.cache.Region;
-import org.geode.examples.util.Mocks;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.contrib.java.lang.system.SystemOutRule;
-
-public class ExampleTest {
-
-  @Rule
-  public SystemOutRule systemOutRule = new SystemOutRule().enableLog();
-
-  @Test
-  public void testExample() throws Exception {
-    Region<EmployeeKey, EmployeeData> region = Mocks.region("example-region");
-    new Example().accept(region);
-
-    assertThat(systemOutRule.getLog()).contains("Counted 10 keys in region");
-    assertThat(systemOutRule.getLog()).contains("Jamie Jive");
-  }
-}
diff --git a/persistence/src/main/java/org/apache/geode/examples/persistence/Example.java b/persistence/src/main/java/org/apache/geode/examples/persistence/Example.java
index a44cf60..e425847 100644
--- a/persistence/src/main/java/org/apache/geode/examples/persistence/Example.java
+++ b/persistence/src/main/java/org/apache/geode/examples/persistence/Example.java
@@ -14,18 +14,14 @@
  */
 package org.apache.geode.examples.persistence;
 
-import java.util.function.Consumer;
-
-import org.apache.geode.cache.DiskStore;
 import org.apache.geode.cache.Region;
 import org.apache.geode.cache.client.ClientCache;
 import org.apache.geode.cache.client.ClientCacheFactory;
 import org.apache.geode.cache.client.ClientRegionShortcut;
 
-public class Example implements Consumer<Region<String, Integer>> {
-  static final String KEY = "counter";
-
-  int counter;
+public class Example {
+  private static final String KEY = "counter";
+  private final Region<String, Integer> region;
 
   public static void main(String[] args) {
     // connect to the locator using default port 10334
@@ -37,32 +33,27 @@ public static void main(String[] args) {
         cache.<String, Integer>createClientRegionFactory(ClientRegionShortcut.PROXY)
             .create("example-region");
 
-    new Example().accept(region);
+    Example example = new Example(region);
+    final int previous = example.getCounter();
+    example.increment();
+    final int current = example.getCounter();
+    System.out.println(previous + " -> " + current);
+
     cache.close();
   }
 
-  public Example() {
-    counter = -1;
+  public Example(Region<String, Integer> region) {
+    this.region = region;
+    if (!region.containsKeyOnServer(KEY)) {
+      region.put(KEY, 0);
+    }
   }
 
   public int getCounter() {
-    return counter;
+    return region.get(KEY);
   }
 
-  @Override
-  public void accept(Region<String, Integer> region) {
-    if (region.containsKeyOnServer(KEY)) {
-      counter = region.get(KEY);
-      System.out.println("Retrieved counter of " + getCounter());
-    } else {
-      counter = 0;
-      System.out.println("Initialized counter to " + getCounter());
-    }
-
-    ++counter;
-    region.put(KEY, counter);
-
-    counter = region.get(KEY);
-    System.out.println("Incremented counter to " + getCounter());
+  public void increment() {
+    region.put(KEY, region.get(KEY) + 1);
   }
 }
diff --git a/persistence/src/test/java/org/apache/geode/examples/persistence/ExampleTest.java b/persistence/src/test/java/org/apache/geode/examples/persistence/ExampleTest.java
deleted file mode 100644
index d240f44..0000000
--- a/persistence/src/test/java/org/apache/geode/examples/persistence/ExampleTest.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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.geode.examples.persistence;
-
-import static org.junit.Assert.assertTrue;
-
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-
-import org.apache.geode.cache.Region;
-import org.apache.geode.cache.execute.Execution;
-
-import org.geode.examples.util.Mocks;
-import org.junit.Test;
-
-public class ExampleTest {
-
-  @Test
-  public void testExample() throws Exception {
-    Example example = new Example();
-    final int previous = example.getCounter();
-
-    Region<String, Integer> region = Mocks.region("example-region");
-
-    example.accept(region);
-    assertTrue(previous < example.getCounter());
-  }
-}
diff --git a/putall/src/main/java/org/apache/geode/examples/putall/Example.java b/putall/src/main/java/org/apache/geode/examples/putall/Example.java
index 21b45c2..eacf599 100644
--- a/putall/src/main/java/org/apache/geode/examples/putall/Example.java
+++ b/putall/src/main/java/org/apache/geode/examples/putall/Example.java
@@ -15,8 +15,9 @@
 package org.apache.geode.examples.putall;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
-import java.util.function.Consumer;
+import java.util.Set;
 import java.util.stream.IntStream;
 
 import org.apache.geode.cache.Region;
@@ -24,7 +25,13 @@
 import org.apache.geode.cache.client.ClientCacheFactory;
 import org.apache.geode.cache.client.ClientRegionShortcut;
 
-public class Example implements Consumer<Region<Integer, String>> {
+public class Example {
+  private final Region<Integer, String> region;
+
+  public Example(Region<Integer, String> region) {
+    this.region = region;
+  }
+
   public static void main(String[] args) {
     // connect to the locator using default port 10334
     ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334)
@@ -32,21 +39,27 @@ public static void main(String[] args) {
 
     // create a local region that matches the server region
     Region<Integer, String> region =
-        cache.<Integer, String>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+        cache.<Integer, String>createClientRegionFactory(ClientRegionShortcut.PROXY)
             .create("example-region");
 
-    new Example().accept(region);
+    Example example = new Example(region);
+    example.insertValues(10);
+    example.printValues(example.getValues());
+
     cache.close();
   }
 
-  @Override
-  public void accept(Region<Integer, String> region) {
+  Set<Integer> getValues() {
+    return new HashSet<>(region.keySetOnServer());
+  }
+
+  void insertValues(int upperLimit) {
     Map values = new HashMap<Integer, String>();
-    IntStream.rangeClosed(1, 10).forEach(i -> values.put(i, "value" + i));
-    System.out
-        .println(String.format("Size of region %s before: %d", region.getName(), region.size()));
+    IntStream.rangeClosed(1, upperLimit).forEach(i -> values.put(i, "value" + i));
     region.putAll(values);
-    System.out
-        .println(String.format("Size of region %s after: %d", region.getName(), region.size()));
+  }
+
+  void printValues(Set<Integer> values) {
+    values.forEach(key -> System.out.println(String.format("%d:%s", key, region.get(key))));
   }
 }
diff --git a/putall/src/test/java/org/apache/geode/examples/putall/ExampleTest.java b/putall/src/test/java/org/apache/geode/examples/putall/ExampleTest.java
deleted file mode 100644
index 359f55c..0000000
--- a/putall/src/test/java/org/apache/geode/examples/putall/ExampleTest.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.geode.examples.putall;
-
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.when;
-
-import org.apache.geode.cache.Region;
-import org.geode.examples.util.Mocks;
-import org.junit.Test;
-
-public class ExampleTest {
-
-  @Test
-  public void testExample() throws Exception {
-    Region<Integer, String> region = Mocks.region("example-region");
-    when(region.size()).then(inv -> {
-      return 10;
-    });
-    new Example().accept(region);
-
-    assertEquals(10, region.size());
-  }
-}
diff --git a/replicated/src/main/java/org/apache/geode/examples/replicated/Example.java b/replicated/src/main/java/org/apache/geode/examples/replicated/Example.java
index 53e8e0e..9fca5ea 100644
--- a/replicated/src/main/java/org/apache/geode/examples/replicated/Example.java
+++ b/replicated/src/main/java/org/apache/geode/examples/replicated/Example.java
@@ -14,7 +14,8 @@
  */
 package org.apache.geode.examples.replicated;
 
-import java.util.function.Consumer;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.stream.IntStream;
 
 import org.apache.geode.cache.Region;
@@ -22,7 +23,13 @@
 import org.apache.geode.cache.client.ClientCacheFactory;
 import org.apache.geode.cache.client.ClientRegionShortcut;
 
-public class Example implements Consumer<Region<Integer, String>> {
+public class Example {
+  private final Region<Integer, String> region;
+
+  public Example(Region<Integer, String> region) {
+    this.region = region;
+  }
+
   public static void main(String[] args) {
     // connect to the locator using default port 10334
     ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334)
@@ -30,27 +37,25 @@ public static void main(String[] args) {
 
     // create a local region that matches the server region
     Region<Integer, String> region =
-        cache.<Integer, String>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+        cache.<Integer, String>createClientRegionFactory(ClientRegionShortcut.PROXY)
             .create("example-region");
 
-    new Example().accept(region);
+    Example example = new Example(region);
+    example.insertValues(10);
+    example.printValues(example.getValues());
+
     cache.close();
   }
 
-  @Override
-  public void accept(Region<Integer, String> region) {
-    // insert values into the region
-    int count = 10;
-    IntStream.range(0, count).forEach(i -> region.put(i, "value" + i));
-    System.out
-        .println(String.format("Inserted %d entries into region %s", count, region.getName()));
-
-    // count the values in the region
-    int inserted = region.keySetOnServer().size();
-    System.out.println(String.format("Counted %d keys in region %s", inserted, region.getName()));
-
-    // fetch the values in the region
-    region.keySetOnServer()
-        .forEach(key -> System.out.println(String.format("%d:%s", key, region.get(key))));
+  Set<Integer> getValues() {
+    return new HashSet<>(region.keySetOnServer());
+  }
+
+  void insertValues(int upperLimit) {
+    IntStream.rangeClosed(1, upperLimit).forEach(i -> region.put(i, "value" + i));
+  }
+
+  void printValues(Set<Integer> values) {
+    values.forEach(key -> System.out.println(String.format("%d:%s", key, region.get(key))));
   }
 }
diff --git a/replicated/src/test/java/org/apache/geode/examples/replicated/ExampleTest.java b/replicated/src/test/java/org/apache/geode/examples/replicated/ExampleTest.java
deleted file mode 100644
index 3bab1f0..0000000
--- a/replicated/src/test/java/org/apache/geode/examples/replicated/ExampleTest.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.geode.examples.replicated;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import org.apache.geode.cache.Region;
-import org.geode.examples.util.Mocks;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.contrib.java.lang.system.SystemOutRule;
-
-public class ExampleTest {
-
-  @Rule
-  public SystemOutRule systemOutRule = new SystemOutRule().enableLog();
-
-  @Test
-  public void testExample() throws Exception {
-    Region<Integer, String> region = Mocks.region("example-region");
-    new Example().accept(region);
-
-    assertThat(systemOutRule.getLog()).contains("Inserted 10 entries into region");
-    assertThat(systemOutRule.getLog()).contains("value9");
-  }
-}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Replace consumer with direct invocation in the examples
> -------------------------------------------------------
>
>                 Key: GEODE-3884
>                 URL: https://issues.apache.org/jira/browse/GEODE-3884
>             Project: Geode
>          Issue Type: Improvement
>          Components: examples
>            Reporter: Michael Dodge
>            Assignee: Michael Dodge
>
> Replace the use of the consumer interface with direct invocation of the relevant methods in the partitioned region, persistent region, multiple put, and replicated region examples.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)