Repository: incubator-ignite Updated Branches: refs/heads/ignite-964-1 be7cda252 -> d63febad3
#ignite-964: add all methods 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/d63febad Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/d63febad Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/d63febad Branch: refs/heads/ignite-964-1 Commit: d63febad3e834f6a449cf76524b0738d7e750a05 Parents: be7cda2 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Mon Jul 6 00:09:47 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Mon Jul 6 00:09:47 2015 +0300 ---------------------------------------------------------------------- .../IgniteScriptingCommandHandler.java | 6 +- .../rest/handlers/scripting/NodeJsCache.java | 78 +++++++++++++++++ .../ignite/internal/NodeJsComputeSelfTest.java | 7 ++ modules/nodejs/src/test/js/test-compute.js | 88 ++++++++++++++++++++ 4 files changed, 178 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d63febad/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 24dc58c..2ccdcbc 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 @@ -63,11 +63,15 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter script.addEngineFunction(emitFunction); + String entryFunction = "Entry = function(key, val) {" + + "this.key = key; this.value = val}"; + + script.addEngineFunction(entryFunction); + emitRes = new IgniteJsEmitResult(); script.addBinding("__emitResult", emitRes); - script.addBinding("ignite", new NodeJSIgnite(ctx.grid())); } catch (IgniteCheckedException e) { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d63febad/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 95f575c..5c21e28 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 @@ -97,6 +97,15 @@ public class NodeJsCache { } /** + * @param keys Keys. + */ + public void removeAll(List keys) { + List cacheKeys = (List)JSONCacheObject.toSimpleObject(keys); + + cache.removeAll(new HashSet<>(cacheKeys)); + } + + /** * @param entries Entries. */ public void putAll(List entries) { @@ -129,6 +138,20 @@ public class NodeJsCache { * @param val Value. * @return Previous value. */ + public Object getAndReplace(Object key, Object val) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + Object cacheVal = JSONCacheObject.toSimpleObject(val); + + Object o = RestJSONCacheObject.convertToRestObject(cache.getAndReplace(cacheKey, cacheVal)); + + return o; + } + + /** + * @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); @@ -148,6 +171,61 @@ public class NodeJsCache { /** * @param key Key. + * @return If operation success. + */ + public boolean remove(Object key) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + + return cache.remove(cacheKey); + } + + /** + * @param key Key. + * @param val Value. + * @return If operation success. + */ + public boolean removeValue(Object key, Object val) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + Object cacheVal = JSONCacheObject.toSimpleObject(val); + + return cache.remove(cacheKey, cacheVal); + } + + /** + * @param key Key. + * @param val Value. + * @return If operation success. + */ + public boolean replace(Object key, Object val) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + Object cacheVal = JSONCacheObject.toSimpleObject(val); + + return cache.replace(cacheKey, cacheVal); + } + + /** + * @param key Key. + * @param val Value. + * @param oldVal Old value. + * @return If operation success. + */ + public boolean replaceValue(Object key, Object val, Object oldVal) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + Object cacheVal = JSONCacheObject.toSimpleObject(val); + Object oldCacheVal = JSONCacheObject.toSimpleObject(oldVal); + + return cache.replace(cacheKey, oldCacheVal, cacheVal); + } + + /** + * Removes all from cache. + */ + public void removeAllFromCache() { + cache.removeAll(); + } + + /** + * @param key Key. * @param val Value. * @return Previous value. */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d63febad/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 c870ab6..48b2855 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 testComputeRunScriptRemoveOperations() throws Exception { + runJsScript("testComputeRunScriptRemoveOperations"); + } + + /** + * @throws Exception If failed. + */ public void testComputeMapReduceGetAndPut() throws Exception { runJsScript("testComputeMapReduceGetAndPut"); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d63febad/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 ec80c17..b99a7ad 100644 --- a/modules/nodejs/src/test/js/test-compute.js +++ b/modules/nodejs/src/test/js/test-compute.js @@ -154,6 +154,94 @@ testComputeRunScriptPutAllGetAll = function() { TestUtils.startIgniteNode(computeRunScriptPutAllGetAll); } +testComputeRunScriptRemoveOperations = function() { + computeRunScriptRemoveOperations = function(error, ignite) { + assert(error === null, "Error on put:" + error); + + var comp = ignite.compute(); + + var f = function (args) { + var cache = ignite.cache("mycache"); + + if (cache.remove("key1") === true) { + throw "Incorrect remove from empty map"; + } + + var key0 = {"keyName" : "keyVal"}; + var key1 = {"keyName" : "keyVal1"}; + var val0 = {"valName" : 1}; + var val1 = {"valName" : 2}; + + var entries = [new Entry(key0, val0), new Entry(key1, val1)]; + var keys = [key0, key1]; + + cache.put(key0, val0); + + if (cache.removeValue(key0, val1) === true) { + throw "Incorrect removeValue from empty map [key=" + JSON.stringify(key0) + "]"; + } + + if (cache.remove(key0) === false) { + throw "Incorrect remove from empty map [key=" + JSON.stringify(key0) + "]"; + } + + cache.put(key0, val0); + + if (cache.replaceValue(key0, val0, val1) === true) { + throw "Incorrect replaceValue result [key=" + JSON.stringify(key0) + "]"; + } + + var prevVal = cache.getAndReplace(key0, val1); + + if (prevVal.valName !== val0.valName) { + throw "Incorrect getAndReplace result [key=" + JSON.stringify(key0) + + ", prevVal=" + prevVal.valName + + ", expected=" + val0.valName + "]"; + } + + prevVal = cache.get(key0); + + if (prevVal.valName !== val1.valName) { + throw "Incorrect getAndReplace result [key=" + JSON.stringify(key0) + "]"; + } + + cache.removeAllFromCache(); + + if (cache.get(key0) !== null) { + throw "Incorrect removeAll result"; + } + + cache.putAll(entries); + + if (cache.replace(key1, val0) !== true) { + throw "Incorrect replace result"; + } + + prevVal = cache.get(key1); + + if (prevVal.valName !== val0.valName) { + throw "Incorrect replace [key=" + JSON.stringify(key1) + "]"; + } + + cache.removeAll(keys); + + if (cache.size() !== 0) { + throw "Incorrect removeAll result."; + } + } + + function onEnd(err, res) { + assert(err == null); + + TestUtils.testDone(); + } + + comp.runScript(f, [], onEnd.bind(null)); + } + + TestUtils.startIgniteNode(computeRunScriptRemoveOperations); +} + testComputeMapReduceGetAndPut = function() { function computeMapReduceGetAndPut(error, ignite) { assert(error == null, "Error on start:" + error);