#ignite-965: emit is a global function.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/e8377811 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/e8377811 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/e8377811 Branch: refs/heads/ignite-965 Commit: e8377811f02969cf905a61f772cc0727669516ec Parents: 53e5be7 Author: ivasilinets <[email protected]> Authored: Wed Jun 24 15:29:32 2015 +0300 Committer: ivasilinets <[email protected]> Committed: Wed Jun 24 15:29:32 2015 +0300 ---------------------------------------------------------------------- .../handlers/scripting/IgniteJsEmitResult.java | 57 ++++++++++++++++++++ .../IgniteScriptingCommandHandler.java | 35 ++++++++---- .../scripting/IgniteScriptProcessor.java | 14 +++++ modules/nodejs/src/test/js/test-compute.js | 2 +- 4 files changed, 96 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e8377811/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteJsEmitResult.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteJsEmitResult.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteJsEmitResult.java new file mode 100644 index 0000000..ecd6cba --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteJsEmitResult.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.ignite.internal.processors.rest.handlers.scripting; + +import org.apache.ignite.internal.util.typedef.*; + +import java.util.*; + +/** + * Emit result binding. + */ +public class IgniteJsEmitResult { + /** Thread local emit result.*/ + private ThreadLocal<List<T3<Object, Object, Object>>> emitResPerCall = new ThreadLocal<>(); + + /** + * @param f JS function. + * @param args Function arguments. + * @param node Node. + */ + public void add(Object f, Object args, Object node) { + List<T3<Object, Object, Object>> res = emitResPerCall.get(); + + if (res == null) + res = new ArrayList(); + + res.add(new T3(f, args, node)); + + emitResPerCall.set(res); + } + + /** + * @return Emit result. + */ + public List<T3<Object, Object, Object>> getEmitResult() { + List<T3<Object, Object, Object>> res = emitResPerCall.get(); + + emitResPerCall.set(null); + + return res; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e8377811/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java index 54dffe2..edf9826 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java @@ -26,6 +26,7 @@ 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.*; import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.lang.*; import org.apache.ignite.resources.*; @@ -44,6 +45,9 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter EXECUTE_MAP_REDUCE_SCRIPT, RUN_SCRIPT); + /** Emit result. */ + private IgniteJsEmitResult emitRes; + /** * @param ctx Context. */ @@ -53,17 +57,20 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter try { IgniteScriptProcessor script = ctx.scripting(); - String emitFunction = "function emit(result, f, args, nodeId) {result.push([f.toString(), args, nodeId])}"; + String emitFunction = "function emit(f, args, nodeId) {" + + "__emitResult.add(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;" + + " f(ids, args); " + " }"; script.addEngineFunction(emitFunction); script.addEngineFunction(computeFunction); + + emitRes = new IgniteJsEmitResult(); + + script.addBinding("__emitResult", emitRes); } catch (IgniteCheckedException e) { ctx.log().error(e.getMessage()); @@ -101,7 +108,8 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter final RestMapReduceScriptRequest req0 = (RestMapReduceScriptRequest) req; GridRestResponse res = ctx.grid().compute().execute( - new JsTask(req0.mapFunction(), req0.argument(), req0.reduceFunction(), ctx), null); + new JsTask(req0.mapFunction(), req0.argument(), req0.reduceFunction(), ctx, emitRes), + null); return new GridFinishedFuture<>(res); } @@ -129,17 +137,21 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter /** Map function argument. */ private String arg; + /** Emit results. */ + private IgniteJsEmitResult emitRes; + /** * @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) { + public JsTask(String mapFunc, String arg, String reduceFunc, GridKernalContext ctx, IgniteJsEmitResult emitRes) { this.mapFunc = mapFunc; this.reduceFunc = reduceFunc; this.arg = arg; this.ctx = ctx; + this.emitRes = emitRes; } /** {@inheritDoc} */ @@ -147,14 +159,15 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter try { Map<ComputeJob, ClusterNode> map = new HashMap<>(); - List jsMapRes = (List)ctx.scripting().invokeFunctionByName("__compute", + ctx.scripting().invokeFunctionByName("__compute", mapFunc, nodes.toArray(new ClusterNode[nodes.size()]), this.arg); - for (Object jobMapping : jsMapRes) { - List task = (List)jobMapping; + List<T3<Object, Object, Object>> jsMapRes = emitRes.getEmitResult(); + + for (T3<Object, Object, Object> task : jsMapRes) { - map.put(new JsCallFunctionJob((String)task.get(0), task.get(1)), - (ClusterNode)task.get(2)); + map.put(new JsCallFunctionJob((String)task.get1(), task.get2()), + (ClusterNode)task.get3()); } return map; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e8377811/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 9843bc3..5073427 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 @@ -84,6 +84,20 @@ public class IgniteScriptProcessor extends GridProcessorAdapter { } /** + * Add binding. + * + * @param name Binding name. + * @param o Object to bind. + */ + public void addBinding(String name, Object o) { + Bindings b = jsEngine.getBindings(ENGINE_SCOPE); + + b.put(name, o); + + jsEngine.setBindings(b, ENGINE_SCOPE); + } + + /** * @param src Script src. * @param args Arguments. * @return Result of the function. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e8377811/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 0fe7842..6aad0c6 100644 --- a/modules/nodejs/src/test/js/test-compute.js +++ b/modules/nodejs/src/test/js/test-compute.js @@ -57,7 +57,7 @@ function computeRunScript(ignite, error) { } function computeExecute(error, ignite) { - var map = function(nodes, arg, emit) { + var map = function(nodes, arg) { var words = arg.split(" "); for (var i = 0; i < words.length; i++) {
