# IGNITE-451 rework scalar examples to be as java counterpart.

Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/d2cc690b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/d2cc690b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/d2cc690b

Branch: refs/heads/ignite-421
Commit: d2cc690b2b057deea9fe0be604c7e59ca301e5ec
Parents: 8792734
Author: AKuznetsov <akuznet...@gridgain.com>
Authored: Mon Mar 16 16:00:31 2015 +0700
Committer: AKuznetsov <akuznet...@gridgain.com>
Committed: Mon Mar 16 16:00:31 2015 +0700

----------------------------------------------------------------------
 .../examples/ScalarCacheAffinityExample.scala   | 115 +++++++++++++++++
 .../examples/ScalarCacheAffinityExample1.scala  | 123 -------------------
 .../examples/ScalarCacheAffinityExample2.scala  | 120 ------------------
 .../ScalarCacheAffinitySimpleExample.scala      |  88 -------------
 .../tests/examples/ScalarExamplesSelfTest.scala |  12 +-
 5 files changed, 116 insertions(+), 342 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d2cc690b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample.scala
----------------------------------------------------------------------
diff --git 
a/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample.scala
 
b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample.scala
new file mode 100644
index 0000000..4263492
--- /dev/null
+++ 
b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample.scala
@@ -0,0 +1,115 @@
+/*
+ * 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.ignite.scalar.examples
+
+import org.apache.ignite.IgniteCache
+import org.apache.ignite.scalar.scalar
+import org.apache.ignite.scalar.scalar._
+
+import scala.collection.JavaConversions._
+
+/**
+ * This example demonstrates the simplest code that populates the distributed 
cache
+ * and co-locates simple closure execution with each key. The goal of this 
particular
+ * example is to provide the simplest code example of this logic.
+ *
+ * Note also that for affinity routing is enabled for all caches.
+ *
+ * Remote nodes should always be started with configuration file which includes
+ * cache: `'ignite.sh examples/config/example-cache.xml'`. Local node can
+ * be started with or without cache.
+ */
+object ScalarCacheAffinityExample extends App {
+    /** Configuration file name. */
+    private val CONFIG = "examples/config/example-compute.xml"
+
+    /** Name of cache. */
+    private val NAME = ScalarCacheAffinityExample.getClass.getSimpleName
+
+    /** Number of keys. */
+    private val KEY_CNT = 20
+
+    /** Type alias. */
+    type Cache = IgniteCache[Int, String]
+
+    /*
+     * Note that in case of `LOCAL` configuration,
+     * since there is no distribution, values may come back as `nulls`.
+     */
+    scalar(CONFIG) {
+        val cache = createCache$[Int, String](NAME)
+
+        try {
+            populate (cache)
+    
+            visitUsingAffinityRun(cache)
+    
+            visitUsingMapKeysToNodes(cache)
+        }
+        finally {
+            cache.close()
+        }
+    }
+
+    /**
+     * Visits every in-memory data ignite entry on the remote node it resides 
by co-locating visiting
+     * closure with the cache key.
+     *
+     * @param c Cache to use.
+     */
+    private def visitUsingAffinityRun(c: IgniteCache[Int, String]) {
+        (0 until KEY_CNT).foreach (i =>
+            ignite$.compute ().affinityRun (NAME, i,
+                () => println ("Co-located using affinityRun [key= " + i + ", 
value=" + c.localPeek (i) + ']') )
+        )
+    }
+
+    /**
+     * Collocates jobs with keys they need to work.
+     *
+     * @param c Cache to use.
+     */
+    private def visitUsingMapKeysToNodes(c: IgniteCache[Int, String]) {
+        val keys = (0 until KEY_CNT).toSeq
+
+        // Map all keys to nodes.
+        val mappings = ignite$.cluster().mapKeysToNodes(NAME, keys)
+
+        mappings.foreach(mapping => {
+            val node = mapping._1
+            val mappedKeys = mapping._2
+
+            if (node != null) {
+                ignite$.cluster().forNode(node) *< (() => {
+                    // Check cache without loading the value.
+                    mappedKeys.foreach(key => println("Co-located using 
mapKeysToNodes [key= " + key +
+                        ", value=" + c.localPeek(key) + ']'))
+                }, null)
+            }
+        })
+    }
+
+    /**
+     * Populates given cache.
+     *
+     * @param c Cache to populate.
+     */
+    private def populate(c: Cache) {
+        (0 until KEY_CNT).foreach(i => c += (i -> i.toString))
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d2cc690b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample1.scala
----------------------------------------------------------------------
diff --git 
a/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample1.scala
 
b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample1.scala
deleted file mode 100644
index c99a284..0000000
--- 
a/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample1.scala
+++ /dev/null
@@ -1,123 +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.ignite.scalar.examples
-
-import org.apache.ignite._
-import org.apache.ignite.cache.affinity.CacheAffinityKeyMapped
-import org.apache.ignite.lang.IgniteCallable
-import org.apache.ignite.scalar.scalar
-import org.apache.ignite.scalar.scalar._
-import org.jetbrains.annotations.Nullable
-
-/**
- * Example of how to collocate computations and data in Ignite using
- * `CacheAffinityKeyMapped` annotation as opposed to direct API calls. This
- * example will first populate cache on some node where cache is available, 
and then
- * will send jobs to the nodes where keys reside and print out values for those
- * keys.
- *
- * Remote nodes should always be started with configuration file which includes
- * cache: `'ignite.sh examples/config/example-cache.xml'`. Local node can
- * be started with or without cache.
- */
-object ScalarCacheAffinityExample1 {
-    /** Configuration file name. */
-    private val CONFIG = "examples/config/example-compute.xml"
-
-    /** Name of cache. */
-    private val NAME = ScalarCacheAffinityExample1.getClass.getSimpleName
-
-    /**
-     * Example entry point. No arguments required.
-     *
-     * Note that in case of `LOCAL` configuration,
-     * since there is no distribution, values may come back as `nulls`.
-     */
-    def main(args: Array[String]) {
-        scalar(CONFIG) {
-            val cache = createCache$[String, String](NAME)
-
-            try {
-                val keys = ('A' to 'Z').map(_.toString).toSeq
-
-                populateCache(ignite$, keys)
-
-                var results = Map.empty[String, String]
-
-                keys.foreach(key => {
-                    val res = ignite$.call$(
-                        new IgniteCallable[String] {
-                            @CacheAffinityKeyMapped
-                            def affinityKey(): String = key
-
-                            def cacheName(): String = NAME
-
-                            @Nullable def call: String = {
-                                println(">>> Executing affinity job for key: " 
+ key)
-
-                                val cache = cache$[String, String](NAME)
-
-                                if (!cache.isDefined) {
-                                    println(">>> Cache not found [nodeId=" + 
ignite$.cluster().localNode.id +
-                                        ", cacheName=" + NAME + ']')
-
-                                    "Error"
-                                }
-                                else
-                                    cache.get.localPeek(key)
-                            }
-                        },
-                        null
-                    )
-
-                    results += (key -> res.head)
-                })
-
-                results.foreach(e => println(">>> Affinity job result for key 
'" + e._1 + "': " + e._2))
-            }
-            finally {
-                cache.close()
-            }
-        }
-    }
-
-    /**
-     * Populates cache with given keys. This method accounts for the case when
-     * cache is not started on local node. In that case a job which populates
-     * the cache will be sent to the node where cache is started.
-     *
-     * @param ignite Ignite.
-     * @param keys Keys to populate.
-     */
-    private def populateCache(ignite: Ignite, keys: Seq[String]) {
-        var prj = ignite.cluster().forCacheNodes(NAME)
-
-        // Give preference to local node.
-        if (prj.nodes().contains(ignite.cluster().localNode))
-            prj = ignite.cluster().forLocal()
-
-        // Populate cache on some node (possibly this node) which has cache 
with given name started.
-        prj.run$(() => {
-            println(">>> Storing keys in cache: " + keys)
-
-            val c = cache$[String, String](NAME).get
-
-            keys.foreach(key => c += (key -> key.toLowerCase))
-        }, null)
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d2cc690b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample2.scala
----------------------------------------------------------------------
diff --git 
a/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample2.scala
 
b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample2.scala
deleted file mode 100644
index a683884..0000000
--- 
a/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinityExample2.scala
+++ /dev/null
@@ -1,120 +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.ignite.scalar.examples
-
-import org.apache.ignite.Ignite
-import org.apache.ignite.scalar.scalar
-import org.apache.ignite.scalar.scalar._
-
-import scala.collection.JavaConversions._
-import scala.util.control.Breaks._
-
-/**
- * Note that affinity routing is enabled for all caches.
- *
- * Remote nodes should always be started with configuration file which includes
- * cache: `'ignite.sh examples/config/example-cache.xml'`. Local node can
- * be started with or without cache.
- */
-object ScalarCacheAffinityExample2 {
-    /** Configuration file name. */
-    private val CONFIG = "examples/config/example-compute.xml"
-
-    /** Name of cache. */
-    private val NAME = ScalarCacheAffinityExample2.getClass.getSimpleName
-
-    /**
-     * Example entry point. No arguments required.
-     *
-     * Note that in case of `LOCAL` configuration,
-     * since there is no distribution, values may come back as `nulls`.
-     */
-    def main(args: Array[String]) {
-        scalar(CONFIG) {
-            val cache = createCache$[String, String](NAME)
-
-            try {
-                var keys = Seq.empty[String]
-
-                ('A' to 'Z').foreach(keys :+= _.toString)
-
-                populateCache(ignite$, keys)
-
-                // Map all keys to nodes.
-                val mappings = ignite$.cluster().mapKeysToNodes(NAME, keys)
-
-                mappings.foreach(mapping => {
-                    val node = mapping._1
-                    val mappedKeys = mapping._2
-
-                    if (node != null) {
-                        ignite$.cluster().forNode(node) *<(() => {
-                            breakable {
-                                println(">>> Executing affinity job for keys: 
" + mappedKeys)
-
-                                // Get cache.
-                                val cache = cache$[String, String](NAME)
-
-                                // If cache is not defined at this point then 
it means that
-                                // job was not routed by affinity.
-                                if (!cache.isDefined)
-                                    println(">>> Cache not found [nodeId=" + 
ignite$.cluster().localNode().id() +
-                                        ", cacheName=" + NAME + ']').^^
-
-                                // Check cache without loading the value.
-                                mappedKeys.foreach(key => println(">>> Peeked 
at: " + cache.get.localPeek(key)))
-                            }
-                        }, null)
-                    }
-                })
-            }
-            finally {
-                cache.close()
-            }
-        }
-    }
-
-    /**
-     * Populates cache with given keys. This method accounts for the case when
-     * cache is not started on local node. In that case a job which populates
-     * the cache will be sent to the node where cache is started.
-     *
-     * @param ignite Ignite.
-     * @param keys Keys to populate.
-     */
-    private def populateCache(ignite: Ignite, keys: Seq[String]) {
-        var prj = ignite.cluster().forCacheNodes(NAME)
-
-        // Give preference to local node.
-        if (prj.nodes().contains(ignite.cluster().localNode))
-            prj = ignite.cluster().forLocal()
-
-        // Populate cache on some node (possibly this node)
-        // which has cache with given name started.
-        prj.run$(
-            () => {
-                println(">>> Storing keys in cache: " + keys)
-
-                val c = cache$[String, String](NAME).get
-
-                keys.foreach(key => c += (key -> key.toLowerCase))
-            },
-            null
-        )
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d2cc690b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinitySimpleExample.scala
----------------------------------------------------------------------
diff --git 
a/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinitySimpleExample.scala
 
b/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinitySimpleExample.scala
deleted file mode 100644
index ce6dd72..0000000
--- 
a/examples/src/main/scala/org/apache/ignite/scalar/examples/ScalarCacheAffinitySimpleExample.scala
+++ /dev/null
@@ -1,88 +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.ignite.scalar.examples
-
-import org.apache.ignite.IgniteCache
-import org.apache.ignite.scalar.scalar
-import org.apache.ignite.scalar.scalar._
-
-/**
- * This example demonstrates the simplest code that populates the distributed 
cache
- * and co-locates simple closure execution with each key. The goal of this 
particular
- * example is to provide the simplest code example of this logic.
- *
- * Note that other examples in this package provide more detailed examples
- * of affinity co-location.
- *
- * Note also that for affinity routing is enabled for all caches.
- *
- * Remote nodes should always be started with configuration file which includes
- * cache: `'ignite.sh examples/config/example-cache.xml'`. Local node can
- * be started with or without cache.
- */
-object ScalarCacheAffinitySimpleExample extends App {
-    /** Configuration file name. */
-    private val CONFIG = "examples/config/example-compute.xml"
-
-    /** Name of cache. */
-    private val NAME = ScalarCacheAffinitySimpleExample.getClass.getSimpleName
-
-    /** Number of keys. */
-    private val KEY_CNT = 20
-
-    /** Type alias. */
-    type Cache = IgniteCache[Int, String]
-
-    /*
-     * Note that in case of `LOCAL` configuration,
-     * since there is no distribution, values may come back as `nulls`.
-     */
-    scalar(CONFIG) {
-        val cache = createCache$[Int, String](NAME)
-
-        try {
-            populate(cache)
-            visit(cache)
-        }
-        finally {
-            cache.close()
-        }
-    }
-
-    /**
-     * Visits every in-memory data ignite entry on the remote node it resides 
by co-locating visiting
-     * closure with the cache key.
-     *
-     * @param c Cache to use.
-     */
-    private def visit(c: Cache) {
-        (0 until KEY_CNT).foreach(i =>
-            ignite$.compute().affinityRun(NAME, i,
-                () => println("Co-located [key= " + i + ", value=" + 
c.localPeek(i) + ']'))
-        )
-    }
-
-    /**
-     * Populates given cache.
-     *
-     * @param c Cache to populate.
-     */
-    private def populate(c: Cache) {
-        (0 until KEY_CNT).foreach(i => c += (i -> i.toString))
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d2cc690b/examples/src/test/scala/org/apache/ignite/scalar/tests/examples/ScalarExamplesSelfTest.scala
----------------------------------------------------------------------
diff --git 
a/examples/src/test/scala/org/apache/ignite/scalar/tests/examples/ScalarExamplesSelfTest.scala
 
b/examples/src/test/scala/org/apache/ignite/scalar/tests/examples/ScalarExamplesSelfTest.scala
index 97d0cf5..52d5ce1 100644
--- 
a/examples/src/test/scala/org/apache/ignite/scalar/tests/examples/ScalarExamplesSelfTest.scala
+++ 
b/examples/src/test/scala/org/apache/ignite/scalar/tests/examples/ScalarExamplesSelfTest.scala
@@ -30,18 +30,8 @@ class ScalarExamplesSelfTest extends 
GridAbstractExamplesTest with JUnitSuiteLik
     private def EMPTY_ARGS = Array.empty[String]
 
     /** */
-    def testScalarCacheAffinityExample1() {
-        ScalarCacheAffinityExample1.main(EMPTY_ARGS)
-    }
-
-    /** */
-    def testScalarCacheAffinityExample2() {
-        ScalarCacheAffinityExample2.main(EMPTY_ARGS)
-    }
-
-    /** */
     def testScalarCacheAffinitySimpleExample() {
-        ScalarCacheAffinitySimpleExample.main(EMPTY_ARGS)
+        ScalarCacheAffinityExample.main(EMPTY_ARGS)
     }
 
     /** */

Reply via email to