Repository: incubator-ignite Updated Branches: refs/heads/ignite-964-1 a8873e3dd -> 18f21d894
#ignite-964: add getAndPut to node js cache. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/18f21d89 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/18f21d89 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/18f21d89 Branch: refs/heads/ignite-964-1 Commit: 18f21d894e66ae18ee8f076c1f0b2d4d8f6fcd12 Parents: a8873e3 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Sun Jul 5 22:55:50 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Sun Jul 5 22:55:50 2015 +0300 ---------------------------------------------------------------------- .../IgniteScriptingCommandHandler.java | 4 +- .../rest/handlers/scripting/NodeJsCache.java | 19 +++++++ .../ignite/internal/NodeJsComputeSelfTest.java | 7 +++ modules/nodejs/src/test/js/test-compute.js | 53 +++++++++++++++++++- 4 files changed, 79 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18f21d89/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 a792c2a..30b9ef3 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 @@ -167,7 +167,7 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter /** {@inheritDoc} */ @Nullable @Override public Object reduce(List<ComputeJobResult> results) { try { - Object[] data = new Object[results.size()]; + List data = new ArrayList<>(results.size()); for (int i = 0; i < results.size(); ++i) { IgniteException err = results.get(i).getException(); @@ -175,7 +175,7 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter if (err != null) return new GridRestResponse(GridRestResponse.STATUS_FAILED, err.getMessage()); - data[i] = results.get(i).getData(); + data.add(results.get(i).getData()); } return ctx.scripting().invokeFunction(reduceFunc, data, null); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18f21d89/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java index 40473fd..b23e39a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java @@ -113,9 +113,28 @@ public class NodeJsCache { } /** + * @param key Key. + * @param val Value. + * @return Previous value. + */ + public Object getAndPut(Object key, Object val) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + Object cacheVal = JSONCacheObject.toSimpleObject(val); + + return cache.getAndPut(cacheKey, cacheVal); + } + + /** * @return Local size. */ public int localSize() { return cache.localSize(); } + + /** + * @return Size. + */ + public int size() { + return cache.size(); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18f21d89/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java index 67dbfc2..51128e1 100644 --- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java +++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java @@ -135,6 +135,13 @@ public class NodeJsComputeSelfTest extends NodeJsAbstractTest { /** * @throws Exception If failed. */ + public void testComputeMapReduceGetAndPut() throws Exception { + runJsScript("testComputeMapReduceGetAndPut"); + } + + /** + * @throws Exception If failed. + */ public void _testRestartGrid() throws Exception { final AtomicInteger id = new AtomicInteger(2); IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18f21d89/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 eab6c7e..1a8c232 100644 --- a/modules/nodejs/src/test/js/test-compute.js +++ b/modules/nodejs/src/test/js/test-compute.js @@ -115,7 +115,7 @@ testComputeRunScriptContainsKeys = function() { } testComputeRunScriptPutAllGetAll = function() { - function computeRunScriptContainsKey(error, ignite) { + function computeRunScriptPutAllGetAll(error, ignite) { assert(error == null, "Error on start:" + error); var comp = ignite.compute(); @@ -151,7 +151,56 @@ testComputeRunScriptPutAllGetAll = function() { onEnd.bind(null)); } - TestUtils.startIgniteNode(computeRunScriptContainsKey); + TestUtils.startIgniteNode(computeRunScriptPutAllGetAll); +} + +testComputeMapReduceGetAndPut = function() { + function computeMapReduceGetAndPut(error, ignite) { + assert(error == null, "Error on start:" + error); + + var map = function(nodes, arg) { + for (var i = 0; i < nodes.length; i++) { + var f = function (val) { + ignite.cache("mycache").put(val, val); + + return val; + }; + + emit(f, i, nodes[i]); + } + }; + + var reduce = function(results) { + var sum = 0; + + for (var i = 0; i < results.length; ++i) { + if (results.indexOf(i) === -1) { + throw "Do not find " + i; + } + + var prev = ignite.cache("mycache").getAndPut(i, i + 1); + + if (prev !== i) { + throw "Incorrect previous value [key=" + i + ", val=" + prev + "]"; + } + + sum += prev; + } + + return sum; + }; + + var callback = function(err, res) { + assert(err == null, "Get error on compute task [err=" + err + "]"); + assert(res === 1); + + TestUtils.testDone(); + } + + ignite.compute().execute(map, reduce, [], callback); + } + + TestUtils.startIgniteNode(computeMapReduceGetAndPut); } function onStart(onPut, error, ignite) {