ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject incubator-ignite git commit: #ignite-965: small refactoring.
Date Wed, 24 Jun 2015 00:03:54 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-965 b51ca63aa -> bd65dcf07


#ignite-965: small refactoring.


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

Branch: refs/heads/ignite-965
Commit: bd65dcf07b6b394eaf877e719bcdb6efd9cfb500
Parents: b51ca63
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Wed Jun 24 03:02:15 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Wed Jun 24 03:02:15 2015 +0300

----------------------------------------------------------------------
 .../processors/rest/GridRestProcessor.java      | 18 ++---
 .../compute/IgniteComputeCommandHandler.java    |  4 +-
 .../IgniteComputeTaskCommandHandler.java        | 58 ++++++++--------
 .../scripting/IgniteScriptProcessor.java        | 69 +++++++++++++-------
 modules/nodejs/src/test/js/test-compute.js      |  3 +-
 5 files changed, 87 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd65dcf0/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
index c156101..594e592 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
@@ -247,15 +247,6 @@ public class GridRestProcessor extends GridProcessorAdapter {
     /** {@inheritDoc} */
     @Override public void start() throws IgniteCheckedException {
         if (isRestEnabled()) {
-            // Register handlers.
-            addHandler(new GridCacheCommandHandler(ctx));
-            addHandler(new GridTaskCommandHandler(ctx));
-            addHandler(new GridTopologyCommandHandler(ctx));
-            addHandler(new GridVersionCommandHandler(ctx));
-            addHandler(new DataStructuresCommandHandler(ctx));
-            addHandler(new IgniteComputeCommandHandler(ctx));
-            addHandler(new IgniteComputeTaskCommandHandler(ctx));
-
             // Start protocols.
             startTcpProtocol();
             startHttpProtocol();
@@ -291,6 +282,15 @@ public class GridRestProcessor extends GridProcessorAdapter {
 
             if (log.isDebugEnabled())
                 log.debug("REST processor started.");
+
+            // Register handlers.
+            addHandler(new GridCacheCommandHandler(ctx));
+            addHandler(new GridTaskCommandHandler(ctx));
+            addHandler(new GridTopologyCommandHandler(ctx));
+            addHandler(new GridVersionCommandHandler(ctx));
+            addHandler(new DataStructuresCommandHandler(ctx));
+            addHandler(new IgniteComputeCommandHandler(ctx));
+            addHandler(new IgniteComputeTaskCommandHandler(ctx));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd65dcf0/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java
index f36e024..26246f5 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java
@@ -69,7 +69,7 @@ public class IgniteComputeCommandHandler extends GridRestCommandHandlerAdapter
{
                     private Ignite ignite;
 
                     @Override public void run() {
-                        ((IgniteKernal) ignite).context().scripting().runJSFunction(req0.function());
+                        ((IgniteKernal) ignite).context().scripting().invokeFunction(req0.function());
                     }
                 });
 
@@ -81,7 +81,7 @@ public class IgniteComputeCommandHandler extends GridRestCommandHandlerAdapter
{
                     private Ignite ignite;
 
                     @Override public Object call() {
-                        return ((IgniteKernal) ignite).context().scripting().runJSFunction(req0.function());
+                        return ((IgniteKernal) ignite).context().scripting().invokeFunction(req0.function());
                     }
                 });
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd65dcf0/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 4f15081..cbf113c 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
@@ -24,6 +24,7 @@ import org.apache.ignite.internal.*;
 import org.apache.ignite.internal.processors.rest.*;
 import org.apache.ignite.internal.processors.rest.handlers.*;
 import org.apache.ignite.internal.processors.rest.request.*;
+import org.apache.ignite.internal.processors.scripting.*;
 import org.apache.ignite.internal.util.future.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
 import org.apache.ignite.resources.*;
@@ -45,6 +46,20 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
      */
     public IgniteComputeTaskCommandHandler(GridKernalContext ctx) {
         super(ctx);
+
+        IgniteScriptProcessor script = ctx.scripting();
+
+        String emitFunction = "function emit(result, f, args, nodeId) {result.push([f.toString(),
args, nodeId])}";
+
+        String computeFunction = "function __compute(mapFuncSource, ids, args) {"  +
+            "       var res = [];" +
+            "       var f = __createJSFunction(mapFuncSource);" +
+            "       f(ids, args, emit.bind(null, res)); "  +
+            "       return res;" +
+            "   }";
+
+        script.addEngineFunction(emitFunction);
+        script.addEngineFunction(computeFunction);
     }
 
     /** {@inheritDoc} */
@@ -104,7 +119,13 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
         @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode>
nodes, String arg) {
             Map<ComputeJob, ClusterNode> map = new HashMap<>();
 
-            List jsMapRes = (List)ctx.scripting().runJSFunction(wrapMapperFunction(nodes));
+            String[] ids = new String[nodes.size()];
+
+            for (int i = 0; i < ids.length; ++i)
+                ids[i] = nodes.get(i).id().toString();
+
+            List jsMapRes = (List)ctx.scripting().invokeFunctionByName("__compute",
+                mapFunc, ids, this.arg);
 
             for (Object jobMapping : jsMapRes) {
                 List task = (List)jobMapping;
@@ -123,9 +144,9 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
                         String[] argv1 = new String[argv.size()];
 
                         for (int i = 0; i < argv1.length; ++i)
-                            argv1[i] = "\"" + argv.get(i).toString() + "\"";
+                            argv1[i] = argv.get(i).toString();
 
-                        return ctx.scripting().runJSFunction(func, argv1);
+                        return ((IgniteKernal)ignite).context().scripting().invokeFunction(func,
argv1);
                     }
                 }, node);
             }
@@ -135,35 +156,12 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
 
         /** {@inheritDoc} */
         @Nullable @Override public Object reduce(List<ComputeJobResult> results) {
-            List<Object> data = new ArrayList<>();
-
-            for (ComputeJobResult res : results)
-                data.add(res.getData());
+            String[] data = new String[results.size()];
 
-            return ctx.scripting().runJSFunction(reduceFunc, data.toString());
-        }
+            for (int i = 0; i < results.size(); ++i)
+                data[i] = results.get(i).getData().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 +
-                "   }";
+            return ctx.scripting().invokeFunction(reduceFunc, (Object)data);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd65dcf0/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 1a72920..9c8a94e 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
@@ -53,45 +53,68 @@ public class IgniteScriptProcessor extends GridProcessorAdapter {
         bind.put("ignite", new IgniteJS());
 
         jsEngine.setBindings(bind, ENGINE_SCOPE);
+
+        String createJSFunction = "function __createJSFunction(mapFunc) {" +
+                "return eval('(function() { return ' + mapFunc.trim() + '})()'); }";
+
+        String internalCall  = "function __internalCall(funcSource) { " +
+                "var func = __createJSFunction(funcSource); " +
+                "var arg = Array.prototype.slice.call(arguments, 0);" +
+                "arg.shift();" +
+                "return func.apply(null, arg);" +
+                "}";
+
+        addEngineFunction(createJSFunction);
+        addEngineFunction(internalCall);
     }
 
     /**
-     * @param script Script.
-     * @param args Arguments.
-     * @return Script result.
-     * @throws ScriptException If script failed.
+     * Add function to scope.
+     *
+     * @param script Function script.
      */
-    public Object runJSFunction(String script, String... args) throws IgniteException {
+    public void addEngineFunction(String script) {
         try {
-            return jsEngine.eval(callJsFunction(script, args));
+            jsEngine.eval(script);
         }
         catch (ScriptException e) {
-            throw new IgniteException("Cannot evaluate javascript function + " + script,
e);
+            throw new IgniteException("Script Engine does not work.", e);
         }
     }
 
     /**
-     * @param script Script.
-     * @return Script result.
-     * @throws ScriptException If script failed.
+     * @param source Script source.
+     * @param args Arguments.
+     * @return Result of the function.
      */
-    public Object runJSFunction(String script) throws IgniteException {
-        return runJSFunction(script, new String[]{""});
+    public Object invokeFunction(String source, Object... args) {
+        Object[] newArgs = new Object[args.length + 1];
+
+        newArgs[0] = source;
+
+        System.arraycopy(args, 0, newArgs, 1, args.length);
+
+        return invokeFunctionByName("__internalCall", newArgs);
     }
 
     /**
-     * @param script JS function script.
-     * @param args Arguments.
-     * @return Script that calls function.
+     * Invoke function.
+     *
+     * @param nameFunc Function name.
+     * @param args Function arguments.
+     * @return Result of the function.
      */
-    private String callJsFunction(String script, String[] args) {
-        String callFuncScript = "(" + script + ")(";
-
-        for (int i = 0; i < args.length; ++i)
-            callFuncScript += args[i] + (i < args.length - 1 ? "," : "");
+    public Object invokeFunctionByName(String nameFunc, Object... args) {
+        Invocable invocable = (Invocable) jsEngine;
 
-        callFuncScript += ");";
-
-        return callFuncScript;
+        try {
+            return invocable.invokeFunction(nameFunc, args);
+        }
+        catch (ScriptException e) {
+            throw new IgniteException("Script Engine does not work.", e);
+        }
+        catch (NoSuchMethodException e) {
+            throw new IgniteException("Script Engine does not work.", e);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd65dcf0/modules/nodejs/src/test/js/test-compute.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/js/test-compute.js b/modules/nodejs/src/test/js/test-compute.js
index 2267662..8c1a7fe 100644
--- a/modules/nodejs/src/test/js/test-compute.js
+++ b/modules/nodejs/src/test/js/test-compute.js
@@ -97,8 +97,9 @@ function computeExecute(error, ignite) {
   var reduce = function(results) {
     var sum = 0;
 
-    for (var i = 0; i < results.length; ++i)
+    for (var i = 0; i < results.length; ++i) {
      sum += parseInt(results[i], 10);
+    }
 
     return sum;
   };


Mime
View raw message