ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [05/20] incubator-ignite git commit: #ignite-965: codestyle IgniteComputeTaskCommandHandler.
Date Tue, 23 Jun 2015 17:47:31 GMT
#ignite-965: codestyle IgniteComputeTaskCommandHandler.


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

Branch: refs/heads/ignite-965
Commit: 57e9845a56ebba443b83f42c2348f6998e511ba3
Parents: 30ec9f7
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Tue Jun 23 19:40:14 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Tue Jun 23 19:40:14 2015 +0300

----------------------------------------------------------------------
 .../IgniteComputeTaskCommandHandler.java        | 100 ++++++++++---------
 .../scripting/IgniteScriptProcessor.java        |   2 +-
 modules/nodejs/src/main/js/compute.js           |  10 --
 3 files changed, 52 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/57e9845a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
index 33f4893..33cd45b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
@@ -56,29 +56,39 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
     @Override public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest
req) {
         assert req != null;
 
-        assert req instanceof RestComputeTaskRequest : "Invalid type of compute request.";
+        assert req instanceof RestComputeTaskRequest : "Invalid type of compute task request.";
 
         assert SUPPORTED_COMMANDS.contains(req.command());
 
         final RestComputeTaskRequest req0 = (RestComputeTaskRequest) req;
 
-        Object res = ctx.grid().compute().execute(new JsTask(req0.mapFunc(), req0.argument(),
req0.reduceFunc(), ctx), null);
+        Object execRes = ctx.grid().compute().execute(
+            new JsTask(req0.mapFunc(), req0.argument(), req0.reduceFunc(), ctx), null);
 
-        return new GridFinishedFuture<>(new GridRestResponse(res));
+        return new GridFinishedFuture<>(new GridRestResponse(execRes));
     }
 
+    /**
+     * JS Compute Task.
+     */
     private static class JsTask extends ComputeTaskAdapter<String, Object> {
         /** Mapping function. */
         private String mapFunc;
 
+        /** Reduce function. */
         private String reduceFunc;
 
-        /** Grid kernal context. */
+        /** Kernal context. */
         private GridKernalContext ctx;
 
+        /** Map function argument. */
         private String arg;
 
         /**
+         * @param mapFunc Map function.
+         * @param arg Map function argument.
+         * @param reduceFunc Reduce function.
+         * @param ctx Kernal context.
          */
         public JsTask(String mapFunc, String arg, String reduceFunc, GridKernalContext ctx)
{
             this.mapFunc = mapFunc;
@@ -91,61 +101,30 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
         @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode>
nodes, String arg) {
             Map<ComputeJob, ClusterNode> map = new HashMap<>();
 
-            String nodesIds = "[";
-
-            for (ClusterNode node : nodes)
-                nodesIds += "\""  + node.id().toString() + "\"" + ",";
-
-            nodesIds = nodesIds.substring(0, nodesIds.length() - 1) + "]";
-
-            String newMap = new String("function () {\n" +
-                "   var res = [];\n" +
-                "   var resCont = function(f, args, nodeId) {\n" +
-                "       res.push([f.toString(), args, nodeId])\n" +
-                "   }\n" +
-                "   var locF = " + mapFunc + "; \n locF(" +
-                    nodesIds + ", " +
-                "\"" + this.arg + "\"" +
-                ", resCont.bind(null)" + ");\n" +
-                "   return res;\n" +
-                "}");
-
-            List mapRes = (List)ctx.scripting().runJSFunction(newMap);
-
-            for (Object arr : mapRes) {
-                Object[] nodeTask = ((List)arr).toArray();
-
-                final String func = (String)nodeTask[0];
-
-                final List argv = (List) nodeTask[1];
+            List jsMapResult = (List)ctx.scripting().runJSFunction(wrapMapperFunction(nodes));
 
-                String nodeIdStr = (String) nodeTask[2];
+            for (Object jobMapping : jsMapResult) {
+                List task = (List)jobMapping;
 
-                UUID nodeId = UUID.fromString(nodeIdStr);
+                final String func = (String)task.get(0);
+                final List argv = (List)task.get(1);
+                String nodeId = (String)task.get(2);
 
-                ClusterNode node = ctx.grid().cluster().node(nodeId);
+                ClusterNode node = ctx.grid().cluster().node(UUID.fromString(nodeId));
 
                 map.put(new ComputeJobAdapter() {
-                    /** Ignite. */
                     @IgniteInstanceResource
                     private Ignite ignite;
 
                     @Override public Object execute() throws IgniteException {
-                        System.out.println("Compute job on node " + ignite.cluster().localNode().id());
-                        try {
-                            String[] argv1 = new String[argv.size()];
-
-                            for (int i = 0; i < argv1.length; ++i)
-                                argv1[i] = "\"" + argv.get(i).toString() + "\"";
-
-                            return ctx.scripting().runJSFunction(func, argv1);
-                        }
-                        catch (Exception e) {
-                            throw new IgniteException(e);
-                        }
+                        String[] argv1 = new String[argv.size()];
+
+                        for (int i = 0; i < argv1.length; ++i)
+                            argv1[i] = "\"" + argv.get(i).toString() + "\"";
+
+                        return ctx.scripting().runJSFunction(func, argv1);
                     }
                 }, node);
-
             }
 
             return map;
@@ -158,7 +137,30 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
             for (ComputeJobResult res : results)
                 data.add(res.getData());
 
-            return ctx.scripting().runJSFunction(reduceFunc, new String[]{data.toString()});
+            return ctx.scripting().runJSFunction(reduceFunc, data.toString());
+        }
+
+        /**
+         * @param nodes Cluster nodes.
+         * @return Script running map function.
+         */
+        private String wrapMapperFunction(List<ClusterNode> nodes) {
+            List<String> ids = new ArrayList<>();
+
+            for (ClusterNode node : nodes)
+                ids.add("\"" + node.id().toString() + "\"");
+
+            String sep = System.getProperty("line.separator");
+
+            return "function () {" + sep +
+                "       var res = [];" + sep +
+                "       var emitFunc = function(f, args, nodeId) {" + sep +
+                "           res.push([f.toString(), args, nodeId])" + sep +
+                "       }" + sep +
+                "       var f = " + mapFunc + ";" + sep +
+                "       f(" + ids + ", " + "\"" + this.arg + "\"" + ", emitFunc.bind(null)"
+ ");" + sep +
+                "       return res;" + sep +
+                "   }";
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/57e9845a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
index 094373c..1a72920 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
@@ -61,7 +61,7 @@ public class IgniteScriptProcessor extends GridProcessorAdapter {
      * @return Script result.
      * @throws ScriptException If script failed.
      */
-    public Object runJSFunction(String script, String[] args) throws IgniteException {
+    public Object runJSFunction(String script, String... args) throws IgniteException {
         try {
             return jsEngine.eval(callJsFunction(script, args));
         }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/57e9845a/modules/nodejs/src/main/js/compute.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/main/js/compute.js b/modules/nodejs/src/main/js/compute.js
index 7f45f5b..e452847 100644
--- a/modules/nodejs/src/main/js/compute.js
+++ b/modules/nodejs/src/main/js/compute.js
@@ -104,14 +104,4 @@ Compute.prototype.execute = function(map, reduce, arg, callback) {
  * @returns {string} Result
  */
 
-/**
- * @this{Compute}
- * @param {MapFunction} map Map function
- * @param {ReduceFunction} reduce Reduce function
- * @param {string} arg Argument
- * @param {onGet} callback Callback
- */
-Compute.prototype._mapWrapper = function(map) {
-
-}
 exports.Compute = Compute


Mime
View raw message