Repository: incubator-ignite Updated Branches: refs/heads/ignite-964-1 f3dc93a18 -> 23c7add12
#ignite-964: add methods to nodejs 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/23c7add1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/23c7add1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/23c7add1 Branch: refs/heads/ignite-964-1 Commit: 23c7add12ad2f23767e1256c1bcc05c6d6edea87 Parents: f3dc93a Author: ivasilinets <ivasilin...@gridgain.com> Authored: Fri Jul 3 21:06:47 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Fri Jul 3 21:06:47 2015 +0300 ---------------------------------------------------------------------- .../processors/cache/GridCacheAdapter.java | 2 +- .../handlers/scripting/JSONCacheObject.java | 1 + .../rest/handlers/scripting/NodeJsCache.java | 59 +++++++++- .../rest/handlers/scripting/RestEntry.java | 57 ++++++++++ .../ignite/internal/NodeJsComputeSelfTest.java | 33 ++++++ modules/nodejs/src/test/js/test-compute.js | 114 ++++++++++++++++++- .../http/jetty/GridJettyRestHandler.java | 55 --------- 7 files changed, 262 insertions(+), 59 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/23c7add1/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java index e138520..5c99bb9 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java @@ -2284,7 +2284,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V * @return Put future. */ public IgniteInternalFuture<Boolean> putAsync(K key, V val, - @Nullable CacheEntryPredicate... filter) { + @Nullable CacheEntryPredicate... filter) { final boolean statsEnabled = ctx.config().isStatisticsEnabled(); final long start = statsEnabled ? System.nanoTime() : 0L; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/23c7add1/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java index 9d7129a..00cb28a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java @@ -143,6 +143,7 @@ public class JSONCacheObject implements JSObject { return o; } + @Override public Object call(Object o, Object... objects) { System.out.println("!!!!CALL"); return null; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/23c7add1/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 31abc12..0bb56d7 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 @@ -18,13 +18,16 @@ package org.apache.ignite.internal.processors.rest.handlers.scripting; import org.apache.ignite.*; +import org.apache.ignite.internal.util.typedef.internal.*; + +import java.util.*; /** * Node js cache. */ public class NodeJsCache { /** Ignite cache. */ - private IgniteCache cache; + private IgniteCache<Object, Object> cache; /** * @param cache Ignite cache. @@ -50,11 +53,63 @@ public class NodeJsCache { public Object get(Object key) { Object cacheKey = JSONCacheObject.toSimpleObject(key); - Object res = cache.get(cacheKey); + return cache.get(cacheKey); + } + + /** + * @param key Key + * @return True if cache contains key. + */ + public boolean containsKey(Object key) { + Object cacheKey = JSONCacheObject.toSimpleObject(key); + + return cache.containsKey(cacheKey); + } + + /** + * @param keys Keys + * @return True if cache contains key. + */ + public boolean containsKeys(List keys) { + List cacheKeys = (List)JSONCacheObject.toSimpleObject(keys); + + return cache.containsKeys(new HashSet<>(cacheKeys)); + } + + /** + * @param keys Keys. + * @return Cache entries. + */ + public List<RestEntry> getAll(List keys) { + List cacheKeys = (List)JSONCacheObject.toSimpleObject(keys); + + Map<Object, Object> entries = cache.getAll(new HashSet<>(cacheKeys)); + + List<RestEntry> res = new ArrayList<>(); + + for (Map.Entry<Object, Object> e : entries.entrySet()) + res.add(new RestEntry(e.getKey(), e.getValue())); + return res; } /** + * @param entries Entries. + */ + public void putAll(List entries) { + List cacheKeys = (List)JSONCacheObject.toSimpleObject(entries); + + Map<Object, Object> cacheEntries = U.newHashMap(entries.size()); + + for (Object e : cacheKeys) { + JSONCacheObject e0 = (JSONCacheObject)e; + cacheEntries.put(e0.getField("_key"), e0.getField("_val")); + } + + cache.putAll(cacheEntries); + } + + /** * @return Local size. */ public int localSize() { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/23c7add1/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java new file mode 100644 index 0000000..f12f2f2 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java @@ -0,0 +1,57 @@ +package org.apache.ignite.internal.processors.rest.handlers.scripting; + + +/** + * Rest entry. + */ +public class RestEntry { + /** Key. */ + private Object key; + + /** Value. */ + private Object val; + + /** + * @param key Key. + * @param val Value. + */ + public RestEntry(Object key, Object val) { + if (key instanceof JSONCacheObject) + this.key = ((JSONCacheObject)key).getFields(); + else + this.key = key; + + if (val instanceof JSONCacheObject) + this.val = ((JSONCacheObject)val).getFields(); + else + this.val = val; + } + + /** + * @return Key. + */ + public Object getKey() { + return key; + } + + /** + * @param key Key. + */ + public void setKey(Object key) { + this.key = key; + } + + /** + * @return Value. + */ + public Object getVal() { + return val; + } + + /** + * @param val Value. + */ + public void setVal(Object val) { + this.val = val; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/23c7add1/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 6e4e5a4..67dbfc2 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 @@ -17,6 +17,8 @@ package org.apache.ignite.internal; +import org.apache.ignite.cache.*; +import org.apache.ignite.configuration.*; import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.testframework.*; @@ -50,6 +52,16 @@ public class NodeJsComputeSelfTest extends NodeJsAbstractTest { grid(0).cache("mycache").removeAll(); } + /** {@inheritDoc} */ + @Override protected CacheConfiguration cacheConfiguration() { + CacheConfiguration ccfg = new CacheConfiguration(); + + ccfg.setName(CACHE_NAME); + ccfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); + + return ccfg; + } + /** * @throws Exception If failed. */ @@ -102,6 +114,27 @@ public class NodeJsComputeSelfTest extends NodeJsAbstractTest { /** * @throws Exception If failed. */ + public void testComputeRunScriptContainsKey() throws Exception { + runJsScript("testComputeRunScriptContainsKey"); + } + + /** + * @throws Exception If failed. + */ + public void testComputeRunScriptContainsKeys() throws Exception { + runJsScript("testComputeRunScriptContainsKeys"); + } + + /** + * @throws Exception If failed. + */ + public void testComputeRunScriptPutAllGetAll() throws Exception { + runJsScript("testComputeRunScriptPutAllGetAll"); + } + + /** + * @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/23c7add1/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 02e87f1..eab6c7e 100644 --- a/modules/nodejs/src/test/js/test-compute.js +++ b/modules/nodejs/src/test/js/test-compute.js @@ -42,6 +42,118 @@ testComputeCacheExecute = function() { TestUtils.startIgniteNode(computeCacheExecute); } +testComputeRunScriptContainsKey = function() { + function computeRunScriptContainsKey(error, ignite) { + assert(error == null, "Error on start:" + error); + + var comp = ignite.compute(); + + var f = function(key) { + var cache = ignite.cache("mycache"); + cache.put(key, "[AAAAAAA]"); + + if (!cache.containsKey(key)) + throw "Contains key does not work." + + return key; + } + + function onEnd(err, res) { + assert(err == null); + assert(TestUtils.compareObject(initKey, res), "Incorrect result after script.") + + ignite.cache("mycache").containsKey(initKey, function(err0, res0) { + assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); + assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); + TestUtils.testDone(); + }); + } + + var initKey = {"1" : ["1", "2"]}; + + comp.runScript(f, initKey, onEnd.bind(null)); + } + + TestUtils.startIgniteNode(computeRunScriptContainsKey); +} + +testComputeRunScriptContainsKeys = function() { + function computeRunScriptContainsKey(error, ignite) { + assert(error == null, "Error on start:" + error); + + var comp = ignite.compute(); + + var f = function(keys) { + var cache = ignite.cache("mycache"); + cache.put(keys[0], "[AAAAAAA]"); + cache.put(keys[1], "[BBBBBBB]"); + + if (!cache.containsKeys(keys)) + throw "Contains key does not work." + + return keys; + } + + function onEnd(err, res) { + assert(err == null); + assert(TestUtils.compareObject([initKey0, initKey1], res), "Incorrect result after script.") + + ignite.cache("mycache").containsKey(initKey0, function(err0, res0) { + assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); + assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); + TestUtils.testDone(); + }); + } + + var initKey0 = {"1" : ["1", "2"]}; + var initKey1 = {"2" : "AAA"}; + + comp.runScript(f, [initKey0, initKey1], onEnd.bind(null)); + } + + TestUtils.startIgniteNode(computeRunScriptContainsKey); +} + +testComputeRunScriptPutAllGetAll = function() { + function computeRunScriptContainsKey(error, ignite) { + assert(error == null, "Error on start:" + error); + + var comp = ignite.compute(); + + var f = function(args) { + var cache = ignite.cache("mycache"); + + cache.putAll(args[0]); + + return cache.getAll(args[1]); + } + + 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) + "]"); + + ignite.cache("mycache").containsKey(initKey0, function(err0, res0) { + assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); + assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); + TestUtils.testDone(); + }); + } + + var initKey0 = {"1" : ["1", "2"]}; + var initKey1 = {"2" : "AAA"}; + var initVal0 = {"1" : ["1", "2"]}; + var initVal1 = {"2" : "AAA"}; + var initEntries = [new Entry(initKey0, initVal0), new Entry(initKey1, initVal1)]; + + comp.runScript(f, [initEntries, [initKey0, initKey1]], + onEnd.bind(null)); + } + + TestUtils.startIgniteNode(computeRunScriptContainsKey); +} + function onStart(onPut, error, ignite) { var cache = ignite.cache("mycache"); @@ -181,7 +293,7 @@ function computeCacheExecute(error, ignite) { }; var reduce = function(results) { - return {"1": 1}; + return {"1" : 1}; }; var callback = function(err, res) { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/23c7add1/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 f9c4685..f135e62 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 @@ -805,59 +805,4 @@ public class GridJettyRestHandler extends AbstractHandler { return JSONObject.fromObject(builder.toString()); } - - /** - * Rest entry. - */ - public static class RestEntry { - /** Key. */ - private Object key; - - /** Value. */ - private Object value; - - /** - * @param key Key. - * @param val Value. - */ - public RestEntry(Object key, Object val) { - if (key instanceof JSONCacheObject) - this.key = ((JSONCacheObject)key).getFields(); - else - this.key = key; - - if (val instanceof JSONCacheObject) - this.value = ((JSONCacheObject)val).getFields(); - else - this.value = val; - } - - /** - * @return Key. - */ - public Object getKey() { - return key; - } - - /** - * @param key Key. - */ - public void setKey(Object key) { - this.key = key; - } - - /** - * @return Value. - */ - public Object getValue() { - return value; - } - - /** - * @param val Value. - */ - public void setValue(Object val) { - this.value = val; - } - } }