Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7689818CB9 for ; Sun, 5 Jul 2015 20:32:06 +0000 (UTC) Received: (qmail 8119 invoked by uid 500); 5 Jul 2015 20:32:06 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 8088 invoked by uid 500); 5 Jul 2015 20:32:06 -0000 Mailing-List: contact commits-help@ignite.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.incubator.apache.org Delivered-To: mailing list commits@ignite.incubator.apache.org Received: (qmail 8079 invoked by uid 99); 5 Jul 2015 20:32:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 05 Jul 2015 20:32:06 +0000 X-ASF-Spam-Status: No, hits=-2000.6 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 05 Jul 2015 20:29:55 +0000 Received: (qmail 7455 invoked by uid 99); 5 Jul 2015 20:31:41 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 05 Jul 2015 20:31:41 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id DAD83DFF71; Sun, 5 Jul 2015 20:31:40 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ivasilinets@apache.org To: commits@ignite.incubator.apache.org Date: Sun, 05 Jul 2015 20:31:40 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] incubator-ignite git commit: #ignite-964: add getAndPutIfAbsent to node js cache. X-Virus-Checked: Checked by ClamAV on apache.org Repository: incubator-ignite Updated Branches: refs/heads/ignite-964-1 18f21d894 -> be7cda252 #ignite-964: add getAndPutIfAbsent 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/bd4cb3e6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/bd4cb3e6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/bd4cb3e6 Branch: refs/heads/ignite-964-1 Commit: bd4cb3e64de06b1b95d0ca6dfaf96c7014fc6ef8 Parents: 18f21d8 Author: ivasilinets Authored: Sun Jul 5 23:30:07 2015 +0300 Committer: ivasilinets Committed: Sun Jul 5 23:30:07 2015 +0300 ---------------------------------------------------------------------- .../IgniteScriptingCommandHandler.java | 1 - .../rest/handlers/scripting/NodeJsCache.java | 43 +++++++++++- .../processors/scripting/JSONCacheObject.java | 3 + modules/nodejs/src/main/js/cache.js | 22 +----- .../ignite/internal/NodeJsComputeSelfTest.java | 7 ++ modules/nodejs/src/test/js/test-cache-api.js | 10 +-- modules/nodejs/src/test/js/test-compute.js | 71 +++++++++++++++++++- .../http/jetty/GridJettyRestHandler.java | 4 +- 8 files changed, 130 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/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 30b9ef3..24dc58c 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 @@ -23,7 +23,6 @@ import org.apache.ignite.compute.*; 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.handlers.scripting.*; import org.apache.ignite.internal.processors.rest.request.*; import org.apache.ignite.internal.processors.scripting.*; import org.apache.ignite.internal.util.future.*; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/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 b23e39a..641d107 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 @@ -121,7 +121,48 @@ public class NodeJsCache { Object cacheKey = JSONCacheObject.toSimpleObject(key); Object cacheVal = JSONCacheObject.toSimpleObject(val); - return cache.getAndPut(cacheKey, cacheVal); + return RestJSONCacheObject.convertToRestObject(cache.getAndPut(cacheKey, cacheVal)); + } + + /** + * @param key Key. + * @param val Value. + * @return Previous value. + */ + public Object getAndPutIfAbsent(Object key, Object val) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + Object cacheVal = JSONCacheObject.toSimpleObject(val); + + return RestJSONCacheObject.convertToRestObject(cache.getAndPutIfAbsent(cacheKey, cacheVal)); + } + + /** + * @param key Key. + * @return Previous value. + */ + public Object getAndRemove(Object key) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + + return RestJSONCacheObject.convertToRestObject(cache.getAndRemove(cacheKey)); + } + + /** + * @param key Key. + * @param val Value. + * @return Previous value. + */ + public Object putIfAbsent(Object key, Object val) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + Object cacheVal = JSONCacheObject.toSimpleObject(val); + + return cache.putIfAbsent(cacheKey, cacheVal); + } + + /** + * @return Cache name. + */ + public String getName() { + return cache.getName(); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java index 5d3b424..8116fa0 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java @@ -61,6 +61,9 @@ public class JSONCacheObject extends HashMap { * @return Converted object. */ public static Object toSimpleObject(Object o) { + if (o == null) + return null; + if (o instanceof Map) { Map o1 = (Map)o; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/modules/nodejs/src/main/js/cache.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/cache.js b/modules/nodejs/src/main/js/cache.js index 2fea1f6..020c71d 100644 --- a/modules/nodejs/src/main/js/cache.js +++ b/modules/nodejs/src/main/js/cache.js @@ -355,25 +355,9 @@ Cache.prototype._createQueryCommand = function(name, qry) { * @param key Key * @param val Value */ -function Entry(key, val) { - this._key = key; - this._val = val; -} - -/** - * @this{Entry} - * @returns Key - */ -Entry.prototype.key = function() { - return this._key; -} - -/** - * @this{Entry} - * @returns Value - */ -Entry.prototype.val = function() { - return this._val; +function Entry(key0, val0) { + this.key = key0; + this.value = val0; } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/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 51128e1..c870ab6 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 @@ -142,6 +142,13 @@ public class NodeJsComputeSelfTest extends NodeJsAbstractTest { /** * @throws Exception If failed. */ + public void testComputeMapReduceGetAndRemoveObject() throws Exception { + runJsScript("testComputeMapReduceGetAndRemoveObject"); + } + + /** + * @throws Exception If failed. + */ public void _testRestartGrid() throws Exception { final AtomicInteger id = new AtomicInteger(2); IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable() { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/modules/nodejs/src/test/js/test-cache-api.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/js/test-cache-api.js b/modules/nodejs/src/test/js/test-cache-api.js index 947a3f6..dcf465a 100644 --- a/modules/nodejs/src/test/js/test-cache-api.js +++ b/modules/nodejs/src/test/js/test-cache-api.js @@ -350,7 +350,7 @@ function containsKeys(cache, entries, next) { var keys = [] for (var entry of entries) { - keys.push(entry.key()); + keys.push(entry.key); } cache.containsKeys(keys, onContainsKeys); @@ -367,7 +367,7 @@ function notContainsKeys(cache, entries, next) { var keys = [] for (var entry of entries) { - keys.push(entry.key()); + keys.push(entry.key); } cache.containsKeys(keys, onContainsKeys); @@ -413,7 +413,7 @@ function getAll(cache, entries, next) { var keys = [] for (var entry of entries) { - keys.push(entry.key()); + keys.push(entry.key); } cache.getAll(keys, onGetAll.bind(null, keys)); @@ -432,7 +432,7 @@ function getAll(cache, entries, next) { var foundVal = null; for (var j = 0; j < values.length; ++j) { - if (TestUtils.compareObject(key, values[j].key())) { + if (TestUtils.compareObject(key, values[j].key)) { foundVal = values[j]; } } @@ -440,7 +440,7 @@ function getAll(cache, entries, next) { var foundExp = null; for (var j = 0; j < expected.length; ++j) { - if (TestUtils.compareObject(key, expected[j].key())) { + if (TestUtils.compareObject(key, expected[j].key)) { foundExp = expected[j]; } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/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 1a8c232..ec80c17 100644 --- a/modules/nodejs/src/test/js/test-compute.js +++ b/modules/nodejs/src/test/js/test-compute.js @@ -131,8 +131,8 @@ testComputeRunScriptPutAllGetAll = function() { function onEnd(err, res) { assert(err == null); - assert(TestUtils.compareObject(initEntries[0].key(), res[0].key), "Incorrect result after script " + - "[InitEntries=" + JSON.stringify(initEntries[0].key()) + ", val=" + JSON.stringify(res[0].key) + "]"); + assert(TestUtils.compareObject(initEntries[0].key, res[0].key), "Incorrect result after script " + + "[InitEntries=" + JSON.stringify(initEntries[0].key) + ", val=" + JSON.stringify(res[0].key) + "]"); ignite.cache("mycache").containsKey(initKey0, function(err0, res0) { assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); @@ -161,7 +161,11 @@ testComputeMapReduceGetAndPut = function() { var map = function(nodes, arg) { for (var i = 0; i < nodes.length; i++) { var f = function (val) { - ignite.cache("mycache").put(val, val); + var prev = ignite.cache("mycache").getAndPutIfAbsent(val, val); + + if (prev !== null) { + throw "Get and put if absent does not work."; + } return val; }; @@ -203,6 +207,67 @@ testComputeMapReduceGetAndPut = function() { TestUtils.startIgniteNode(computeMapReduceGetAndPut); } +testComputeMapReduceGetAndRemoveObject = function() { + function computeMapReduceGetAndRemove(error, ignite) { + assert(error == null, "Error on start:" + error); + + var map = function(nodes, entries) { + for (var i = 0; i < entries.length; i++) { + var f = function (entry) { + var cache = ignite.cache("mycache"); + print("ENTRY =" + entry); + + print("ENTRY Key=" + entry.key); + + if (cache.putIfAbsent(entry.key, entry.value) !== true) { + throw "Incorrect put if absent result." + } + + if (cache.putIfAbsent(entry.key, "1") !== false) { + throw "Incorrect put if absent result." + } + + return cache.getAndRemove(entry.key); + }; + + emit(f, entries[i], nodes[i % nodes.length]); + } + }; + + var reduce = function(results) { + var sum = 0; + + for (var i = 0; i < results.length; ++i) { + sum += results[i].age; + } + + return sum; + }; + + var callback = function(err, res) { + assert(err == null, "Get error on compute task [err=" + err + "]"); + assert(res === 25, "Incorrect reduce result."); + + TestUtils.testDone(); + } + + + entries = []; + + var key1 = {"name" : "Ann"}; + var key2 = {"name" : "Paul"}; + var val1 = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = {"age" : 13, "books" : ["1", "Book"]}; + + entries.push(new Entry(key1, val1)); + entries.push(new Entry(key2, val2)); + + ignite.compute().execute(map, reduce, entries, callback); + } + + TestUtils.startIgniteNode(computeMapReduceGetAndRemove); +} + function onStart(onPut, error, ignite) { var cache = ignite.cache("mycache"); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd4cb3e6/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java ---------------------------------------------------------------------- diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java index dd57c93..7ad2a15 100644 --- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java +++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java @@ -411,8 +411,8 @@ public class GridJettyRestHandler extends AbstractHandler { for (Object entry : entries) { JSONCacheObject cacheEntry = new JSONCacheObject((JSONObject) entry); - Object key = cacheEntry.getField("_key"); - Object val = cacheEntry.getField("_val"); + Object key = cacheEntry.getField("key"); + Object val = cacheEntry.getField("value"); map.put(key, val); }