You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ab...@apache.org on 2017/09/05 22:44:37 UTC

[geode-examples] 01/02: GEODE-3428: Add example of putting multiple values all at once.

This is an automated email from the ASF dual-hosted git repository.

abaker pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-examples.git

commit b7495ce162ae9a00e3dd816c23298fafdeb15449
Author: Sarge <md...@pivotal.io>
AuthorDate: Fri Aug 11 11:50:53 2017 -0700

    GEODE-3428: Add example of putting multiple values all at once.
---
 .gitignore                                         |  1 +
 README.md                                          |  1 +
 putall/README.md                                   | 49 ++++++++++++++++++++
 {replicated => putall}/scripts/start.gfsh          |  5 +--
 .../scripts/start.gfsh => putall/scripts/stop.gfsh | 13 +-----
 .../org/apache/geode/examples/putall/Example.java  | 52 ++++++++++++++++++++++
 .../apache/geode/examples/putall/ExampleTest.java  | 36 +++++++++++++++
 replicated/scripts/start.gfsh                      |  2 -
 settings.gradle                                    |  1 +
 9 files changed, 143 insertions(+), 17 deletions(-)

diff --git a/.gitignore b/.gitignore
index bc00815..fa3d887 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,7 @@ build/
 .idea/
 build-eclipse/
 /tags
+out/
 
 locator
 server1
diff --git a/README.md b/README.md
index 1894591..507da4d 100644
--- a/README.md
+++ b/README.md
@@ -75,6 +75,7 @@ tutorial.
 
 *  [Replicated Region](replicated/README.md)
 *  [Partitioned Region](partitioned/README.md)
+*  [Put Multiple Values at Once](putall/README.md)
 *  Persistence
 *  OQL (Querying)
 
diff --git a/putall/README.md b/putall/README.md
new file mode 100644
index 0000000..1b67c41
--- /dev/null
+++ b/putall/README.md
@@ -0,0 +1,49 @@
+<!--
+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.
+-->
+
+# Geode putAll region example
+
+This is a simple example that demonstrates putting a map of values into a region, checking the size, and retrieving the values.
+
+This example assumes you have installed Java and Geode.
+
+## Steps
+
+1. From the `geode-examples/putall` directory, build the example and
+   run unit tests
+
+        $ ../gradlew build
+
+2. Next start the locator and two servers
+
+        $ gfsh run --file=scripts/start.gfsh
+
+3. Run the example to create entries in the region
+
+        $ ../gradlew run
+
+4. Kill one of the servers
+
+        $ gfsh -e "connect --locator=127.0.0.1[10334]" -e "stop server --name=server"
+
+5. Run a gfsh query, and notice that all the entries are still available due to replication
+
+        $ gfsh -e "connect --locator=127.0.0.1[10334]" -e "query --query='select e.key from /example-region.entries e'"
+
+6. Shut down the system:
+
+        $ gfsh run --file=scripts/stop.gfsh
diff --git a/replicated/scripts/start.gfsh b/putall/scripts/start.gfsh
old mode 100644
new mode 100755
similarity index 86%
copy from replicated/scripts/start.gfsh
copy to putall/scripts/start.gfsh
index 88eb2a5..093b822
--- a/replicated/scripts/start.gfsh
+++ b/putall/scripts/start.gfsh
@@ -16,10 +16,7 @@
 #
 start locator --name=locator --bind-address=127.0.0.1
 
-start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
-start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
-
-#connect
+start server --name=server --locators=127.0.0.1[10334] --server-port=0
 
 create region --name=example-region --type=REPLICATE
 
diff --git a/replicated/scripts/start.gfsh b/putall/scripts/stop.gfsh
old mode 100644
new mode 100755
similarity index 71%
copy from replicated/scripts/start.gfsh
copy to putall/scripts/stop.gfsh
index 88eb2a5..7672d87
--- a/replicated/scripts/start.gfsh
+++ b/putall/scripts/stop.gfsh
@@ -14,14 +14,5 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-start locator --name=locator --bind-address=127.0.0.1
-
-start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
-start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
-
-#connect
-
-create region --name=example-region --type=REPLICATE
-
-list members
-describe region --name=example-region
+connect --locator=127.0.0.1[10334]
+shutdown --include-locators=true
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
new file mode 100644
index 0000000..21b45c2
--- /dev/null
+++ b/putall/src/main/java/org/apache/geode/examples/putall/Example.java
@@ -0,0 +1,52 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.stream.IntStream;
+
+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<Integer, String>> {
+  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<Integer, String> region =
+        cache.<Integer, String>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+            .create("example-region");
+
+    new Example().accept(region);
+    cache.close();
+  }
+
+  @Override
+  public void accept(Region<Integer, String> region) {
+    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()));
+    region.putAll(values);
+    System.out
+        .println(String.format("Size of region %s after: %d", region.getName(), region.size()));
+  }
+}
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
new file mode 100644
index 0000000..359f55c
--- /dev/null
+++ b/putall/src/test/java/org/apache/geode/examples/putall/ExampleTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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/scripts/start.gfsh b/replicated/scripts/start.gfsh
index 88eb2a5..090f3b0 100644
--- a/replicated/scripts/start.gfsh
+++ b/replicated/scripts/start.gfsh
@@ -19,8 +19,6 @@ start locator --name=locator --bind-address=127.0.0.1
 start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
 start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
 
-#connect
-
 create region --name=example-region --type=REPLICATE
 
 list members
diff --git a/settings.gradle b/settings.gradle
index 3604a11..7b579bb 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -20,3 +20,4 @@ include 'replicated'
 include 'partitioned'
 include 'lucene'
 include 'loader'
+include 'putall'

-- 
To stop receiving notification emails like this one, please contact
"commits@geode.apache.org" <co...@geode.apache.org>.