geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmil...@apache.org
Subject geode-examples git commit: GEODE-2231 Non-working partition regions example
Date Thu, 12 Jan 2017 22:01:24 GMT
Repository: geode-examples
Updated Branches:
  refs/heads/feature/GEODE-2231 8ec519a55 -> 9b5b0e9b2


GEODE-2231 Non-working partition regions example


Project: http://git-wip-us.apache.org/repos/asf/geode-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode-examples/commit/9b5b0e9b
Tree: http://git-wip-us.apache.org/repos/asf/geode-examples/tree/9b5b0e9b
Diff: http://git-wip-us.apache.org/repos/asf/geode-examples/diff/9b5b0e9b

Branch: refs/heads/feature/GEODE-2231
Commit: 9b5b0e9b20b1d2c6037614f54ded556b78929472
Parents: 8ec519a
Author: Karen Miller <kmiller@pivotal.io>
Authored: Thu Jan 12 13:59:35 2017 -0800
Committer: Karen Miller <kmiller@pivotal.io>
Committed: Thu Jan 12 13:59:35 2017 -0800

----------------------------------------------------------------------
 partitioned/scripts/startAll.sh                 |  2 +-
 .../examples/partitioned/BadEmployeeKey.java    | 68 ++++++++++++++++++++
 .../geode/examples/partitioned/BaseClient.java  |  4 +-
 .../geode/examples/partitioned/Consumer.java    | 18 +++++-
 .../examples/partitioned/EmployeeData.java      | 57 ++++++++++++++++
 .../geode/examples/partitioned/EmployeeKey.java | 64 ++++++++++++++++++
 .../geode/examples/partitioned/Producer.java    | 21 ++++--
 7 files changed, 225 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode-examples/blob/9b5b0e9b/partitioned/scripts/startAll.sh
----------------------------------------------------------------------
diff --git a/partitioned/scripts/startAll.sh b/partitioned/scripts/startAll.sh
index ff2bc03..1bf63ca 100755
--- a/partitioned/scripts/startAll.sh
+++ b/partitioned/scripts/startAll.sh
@@ -37,7 +37,7 @@ do
 done
 
 # create a region using GFSH
-gfsh -e "connect --locator=localhost[${GEODE_LOCATOR_PORT}]" -e "create region --name=myRegion
--type=PARTITION"
+gfsh -e "connect --locator=localhost[${GEODE_LOCATOR_PORT}]" -e "create region --name=EmployeeRegion
--type=PARTITION"
 
 gfsh -e "connect --locator=localhost[${GEODE_LOCATOR_PORT}]" -e "list members"
 

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/9b5b0e9b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java
----------------------------------------------------------------------
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java
b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java
new file mode 100644
index 0000000..3671a9e
--- /dev/null
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java
@@ -0,0 +1,68 @@
+/*
+ * 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 java.util.logging.Logger;
+import org.apache.geode.cache.client.ClientCache;
+
+public class BadEmployeeKey {
+
+  static final Logger logger = Logger.getAnonymousLogger();
+  private String name;
+  private int emplNumber;
+
+  public BadEmployeeKey() {
+  }
+
+  public BadEmployeeKey(String n, int en) {
+    this.name = n;
+    this.emplNumber = en;
+  }
+
+  public String getName() {
+    return(name);
+  }
+
+  public int getEmplNumber() {
+    return(emplNumber);
+  }
+
+  public boolean equals(EmployeeKey key) {
+    if ( this.name.equals(key.getName()) && 
+        this.emplNumber==key.getEmplNumber() ) {
+      return true;
+    }
+    return false;
+  }
+
+  /*
+   *  This hashCode is what make this class a very poor implementation.
+   *  It always returns the value 1, so that every key gets placed in
+   *  the same bucket, and partitioning is useless.
+   *  
+   *  Forgetting to define, or implementing an erroneous hashCode can result
+   *  in rotten distribution of region entries across buckets.
+   */
+  public int hashCode() {
+    return(1);
+  }
+
+  public String toString() {
+    return("Name: " + this.name + " Employee Number: " + this.emplNumber);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/9b5b0e9b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
----------------------------------------------------------------------
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
index 7eac0e0..3b01882 100644
--- a/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
@@ -36,8 +36,8 @@ public abstract class BaseClient {
   private Region region;
   private final String locatorHost = System.getProperty("GEODE_LOCATOR_HOST", "localhost");
   private final int locatorPort = Integer.getInteger("GEODE_LOCATOR_PORT", 10334);
-  protected static final String REGION_NAME = "myRegion";
-  static final int NUM_ENTRIES = 50;
+  protected static final String REGION_NAME = "EmployeeRegion";
+  static final int NUM_ENTRIES = 3;
 
   public BaseClient() {
     this.clientCache = getClientCache();

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/9b5b0e9b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
----------------------------------------------------------------------
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
index acb6bd4..91c6af9 100644
--- a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
@@ -16,12 +16,15 @@
  */
 package org.apache.geode.examples.partitioned;
 
+import java.util.*;
 import org.apache.geode.cache.client.ClientCache;
 
 public class Consumer extends BaseClient {
 
   public static void main(String[] args) {
-    new Consumer().countEntriesOnServer();
+    Consumer c = new Consumer();
+    int numEntries = c.countEntriesOnServer();
+    c.printRegionContents();
   }
 
   public Consumer() {
@@ -32,9 +35,20 @@ public class Consumer extends BaseClient {
   }
 
   public int countEntriesOnServer() {
-    int size = getRegion().keySetOnServer().size();
+    int size = this.getRegion().keySetOnServer().size();
     logger.info(String.format("Done. %d entries available on the server(s).", size));
     return size;
   }
 
+  public void printRegionContents() {
+    Set<EmployeeKey> setOfKeys = this.getRegion().keySetOnServer();
+    /* for each key in setOfKeys
+     *   print the entry
+     */
+    logger.info("Region contents:");
+    for (EmployeeKey key : setOfKeys) {
+      logger.info(getRegion().getEntry(key).toString());
+    }
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/9b5b0e9b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeData.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..76979e4
--- /dev/null
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeData.java
@@ -0,0 +1,57 @@
+/*
+ * 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 java.util.logging.Logger;
+
+public class EmployeeData {
+
+  static final Logger logger = Logger.getAnonymousLogger();
+  private EmployeeKey nameAndNumber;
+  private int salary;
+  private int hoursPerWeek;
+
+  public EmployeeData() {
+    this.nameAndNumber = new EmployeeKey("no name", 0);
+    this.salary = 0;
+    this.hoursPerWeek = 0;
+  }
+
+  public EmployeeData(EmployeeKey k, int s, int hrs) {
+    this.nameAndNumber = k;
+    this.salary = s;
+    this.hoursPerWeek = hrs;
+  }
+
+  public EmployeeKey getEmployeeKey() {
+    return(nameAndNumber);
+  }
+
+  public int getSalary() {
+    return(salary);
+  }
+
+  public int getHoursPerWeek() {
+    return(hoursPerWeek);
+  }
+
+  public String toString() {
+    return(nameAndNumber.toString() + " salary=" + this.salary +
+     " hoursPerWeek=" + this.hoursPerWeek);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/9b5b0e9b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeKey.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..1a6e06c
--- /dev/null
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/EmployeeKey.java
@@ -0,0 +1,64 @@
+/*
+ * 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 java.util.logging.Logger;
+import org.apache.geode.cache.client.ClientCache;
+
+public class EmployeeKey {
+
+  static final Logger logger = Logger.getAnonymousLogger();
+  private String name;
+  private int emplNumber;
+
+  public EmployeeKey() {
+  }
+
+  public EmployeeKey(String n, int en) {
+    this.name = n;
+    this.emplNumber = en;
+  }
+
+  public String getName() {
+    return(name);
+  }
+
+  public int getEmplNumber() {
+    return(emplNumber);
+  }
+
+  public boolean equals(EmployeeKey key) {
+    if ( this.name.equals(key.getName()) && 
+        this.emplNumber==key.getEmplNumber() ) {
+      return true;
+    }
+    return false;
+  }
+
+  public int hashCode() {
+    int nameHashValue = this.name.hashCode();
+    int numHashValue = this.emplNumber;
+    logger.info("Key: name hash=" + nameHashValue + " num hash=" +
+      numHashValue);
+    return(nameHashValue + numHashValue);
+  }
+
+  public String toString() {
+    return("Name: " + this.name + " Employee Number: " + this.emplNumber);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/9b5b0e9b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
----------------------------------------------------------------------
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
index 2aeb8ff..e909b54 100644
--- a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
@@ -32,9 +32,22 @@ public class Producer extends BaseClient {
   }
 
   public void populateRegion() {
-    for (int i=0; i < NUM_ENTRIES; i++) {
-      getRegion().put(i, "value" + i);
-    }
-    logger.info("Done. Inserted " + NUM_ENTRIES + " entries.");
+
+    EmployeeKey k1 = new EmployeeKey("Alex Able", 160);
+    EmployeeData d1 = new EmployeeData(k1, 70000, 40);
+    logger.info("About to try put on k1,d1 ");
+    getRegion().put(k1, d1);
+
+    EmployeeKey k2 = new EmployeeKey("Bertie Bell", 170);
+    EmployeeData d2 = new EmployeeData(k2, 72000, 40);
+    logger.info("About to try put on k2,d2 ");
+    getRegion().put(k2, d2);
+
+    EmployeeKey k3 = new EmployeeKey("Chris Call", 180);
+    EmployeeData d3 = new EmployeeData(k3, 68000, 40);
+    logger.info("About to try put on k3,d3 ");
+    getRegion().put(k3, d3);
+
+    logger.info("Inserted 3 entries.");
   }
 }


Mime
View raw message