ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ivasilin...@apache.org
Subject [2/8] incubator-ignite git commit: #ignite-964: add contains keys method for nodejs cache.
Date Wed, 01 Jul 2015 13:09:10 GMT
#ignite-964: add contains keys method for 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/1ba51fcd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/1ba51fcd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/1ba51fcd

Branch: refs/heads/ignite-964
Commit: 1ba51fcdcc24876dbb734fe7f8c3e8ce791889bc
Parents: 7bed77b
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Wed Jul 1 15:00:00 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Wed Jul 1 15:00:00 2015 +0300

----------------------------------------------------------------------
 .../processors/rest/GridRestCommand.java        |  3 +
 .../processors/rest/GridRestProcessor.java      |  2 +
 .../handlers/cache/GridCacheCommandHandler.java | 76 +++++++++++++++-----
 modules/nodejs/src/main/js/cache.js             | 12 ++++
 .../ignite/internal/NodeJsCacheApiSelfTest.java | 14 ++++
 modules/nodejs/src/test/js/test-cache-api.js    | 57 +++++++++++++++
 .../http/jetty/GridJettyRestHandler.java        |  3 +-
 7 files changed, 147 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1ba51fcd/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java
index 48d5bcf..a6563c9 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java
@@ -36,6 +36,9 @@ public enum GridRestCommand {
     /** Contains cached value. */
     CACHE_CONTAINS_KEY("containskey"),
 
+    /** Contains cached values. */
+    CACHE_CONTAINS_KEYS("containskeys"),
+
     /** Get several cached values. */
     CACHE_GET_ALL("getall"),
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1ba51fcd/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
index 220f2d1..f688d3a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
@@ -388,6 +388,7 @@ public class GridRestProcessor extends GridProcessorAdapter {
 
         if (interceptor != null && res.getResponse() != null) {
             switch (req.command()) {
+                case CACHE_CONTAINS_KEYS:
                 case CACHE_CONTAINS_KEY:
                 case CACHE_GET:
                 case CACHE_GET_ALL:
@@ -533,6 +534,7 @@ public class GridRestProcessor extends GridProcessorAdapter {
         switch (req.command()) {
             case CACHE_GET:
             case CACHE_CONTAINS_KEY:
+            case CACHE_CONTAINS_KEYS:
             case CACHE_GET_ALL:
                 perm = SecurityPermission.CACHE_READ;
                 name = ((GridRestCacheRequest)req).cacheName();

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1ba51fcd/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
index a4dc466..6a966ee 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
@@ -53,6 +53,7 @@ import static org.apache.ignite.transactions.TransactionIsolation.*;
 public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter {
     /** Supported commands. */
     private static final Collection<GridRestCommand> SUPPORTED_COMMANDS = U.sealList(
+        CACHE_CONTAINS_KEYS,
         CACHE_CONTAINS_KEY,
         CACHE_GET,
         CACHE_GET_ALL,
@@ -139,9 +140,16 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
             IgniteInternalFuture<GridRestResponse> fut;
 
             switch (cmd) {
+                case CACHE_CONTAINS_KEYS: {
+                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName,
skipStore, key,
+                        new ContainsKeysCommand(getKeys(req0)));
+
+                    break;
+                }
+
                 case CACHE_CONTAINS_KEY: {
                     fut = executeCommand(req.destinationId(), req.clientId(), cacheName,
skipStore, key,
-                        new ContainsCommand(key));
+                        new ContainsKeyCommand(key));
 
                     break;
                 }
@@ -154,23 +162,8 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
                 }
 
                 case CACHE_GET_ALL: {
-                    Set<Object> keys = req0.values().keySet();
-
-                    if (F.isEmpty(keys))
-                        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("keys"));
-
-                    // HashSet wrapping for correct serialization
-                    HashSet<Object> keys0 = new HashSet<>();
-
-                    for (Object getKey : keys) {
-                        if (getKey == null)
-                            throw new IgniteCheckedException("Failing getAll operation (null
keys are not allowed).");
-
-                        keys0.add(getKey);
-                    }
-
                     fut = executeCommand(req.destinationId(), req.clientId(), cacheName,
skipStore, key,
-                        new GetAllCommand(keys0));
+                        new GetAllCommand(getKeys(req0)));
 
                     break;
                 }
@@ -306,6 +299,30 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
     }
 
     /**
+     * @param req Request.
+     * @return Request keys.
+     * @throws IgniteCheckedException If incorrect keys are presented.
+     */
+    private Set<Object> getKeys(GridRestCacheRequest req) throws IgniteCheckedException
{
+        Set<Object> keys = req.values().keySet();
+
+        if (F.isEmpty(keys))
+            throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("keys"));
+
+        // HashSet wrapping for correct serialization
+        HashSet<Object> keys0 = new HashSet<>();
+
+        for (Object getKey : keys) {
+            if (getKey == null)
+                throw new IgniteCheckedException("Failing operation (null keys are not allowed).");
+
+            keys0.add(getKey);
+        }
+
+        return keys0;
+    }
+
+    /**
      * Executes command on flagged cache projection. Checks {@code destId} to find
      * if command could be performed locally or routed to a remote node.
      *
@@ -711,7 +728,7 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
     }
 
     /** */
-    private static class ContainsCommand extends CacheProjectionCommand {
+    private static class ContainsKeyCommand extends CacheProjectionCommand {
         /** */
         private static final long serialVersionUID = 0L;
 
@@ -721,7 +738,7 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
         /**
          * @param key Key.
          */
-        ContainsCommand(Object key) {
+        ContainsKeyCommand(Object key) {
             this.key = key;
         }
 
@@ -732,6 +749,27 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
     }
 
     /** */
+    private static class ContainsKeysCommand extends CacheProjectionCommand {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** */
+        private final Collection<Object> keys;
+
+        /**
+         * @param keys Keys.
+         */
+        ContainsKeysCommand(Collection<Object> keys) {
+            this.keys = keys;
+        }
+
+        /** {@inheritDoc} */
+        @Override public IgniteInternalFuture<?> applyx(IgniteInternalCache<Object,
Object> c, GridKernalContext ctx) {
+            return c.containsKeysAsync(keys);
+        }
+    }
+
+    /** */
     private static class GetCommand extends CacheProjectionCommand {
         /** */
         private static final long serialVersionUID = 0L;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1ba51fcd/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 2d91fcb..6152d57 100644
--- a/modules/nodejs/src/main/js/cache.js
+++ b/modules/nodejs/src/main/js/cache.js
@@ -140,6 +140,18 @@ Cache.prototype.containsKey = function(key, callback) {
 }
 
 /**
+ * Determines if the cache contains an entry for the specified key.
+ *
+ * @this {Cache}
+ * @param {Object[]} keys Keys
+ * @param {Cache~onGetAll} callback Called on finish with boolean result
+ */
+Cache.prototype.containsKeys = function(keys, callback) {
+    this._server.runCommand(this._createCommand("containskeys").
+        setPostData(JSON.stringify({"keys" : keys})), callback);
+}
+
+/**
  * Execute sql query
  *
  * @param {SqlQuery|SqlFieldsQuery} qry Query

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1ba51fcd/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java
b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java
index 8fadcc4..f992a7c 100644
--- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java
+++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java
@@ -112,4 +112,18 @@ public class NodeJsCacheApiSelfTest extends NodeJsAbstractTest {
     public void testPutContains() throws Exception {
         runJsScript("testPutContains");
     }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutContainsAll() throws Exception {
+        runJsScript("testPutContainsAll");
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testNotContainsAll() throws Exception {
+        runJsScript("testNotContainsAll");
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1ba51fcd/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 afcefa3..5a2e9f1 100644
--- a/modules/nodejs/src/test/js/test-cache-api.js
+++ b/modules/nodejs/src/test/js/test-cache-api.js
@@ -34,6 +34,29 @@ testContains = function() {
     startTest("mycache", {trace: [notContainsKey], entry: ["key" , "6"]});
 }
 
+testPutContainsAll = function() {
+    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));
+
+    startTest("mycache", {trace: [putAll, containsKeys], entry: entries});
+}
+
+testNotContainsAll = function() {
+    entries = [];
+
+    entries.push(new Entry("key1", "val1"));
+    entries.push(new Entry("key2", "val2"));
+
+    startTest("mycache", {trace: [notContainsKeys], entry: entries});
+}
+
 testRemove = function() {
     startTest("mycache", {trace: [put, getExist, remove, getNonExist], entry: ["key" , "6"]});
 }
@@ -136,6 +159,40 @@ function notContainsKey(cache, entry, next) {
     }
 }
 
+function containsKeys(cache, entries, next) {
+    var keys = []
+
+    for (var entry of entries) {
+        keys.push(entry.key());
+    }
+
+    cache.containsKeys(keys, onContainsKeys);
+
+    function onContainsKeys(err, val) {
+        assert(err === null, "Error on contains key [err=" + err + "]");
+        assert(val === true, "Incorrect result [expected=" + true + ", val=" + val + "]");
+
+        TestUtils.testDone();
+    }
+}
+
+function notContainsKeys(cache, entries, next) {
+    var keys = []
+
+    for (var entry of entries) {
+        keys.push(entry.key());
+    }
+
+    cache.containsKeys(keys, onContainsKeys);
+
+    function onContainsKeys(err, val) {
+        assert(err === null, "Error on contains key [err=" + err + "]");
+        assert(val === false, "Incorrect result [expected=" + false + ", val=" + val + "]");
+
+        TestUtils.testDone();
+    }
+}
+
 function getExist(cache, entry, next) {
     var key = Object.keys(entry)[0];
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1ba51fcd/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 c6fbbea..d864713 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
@@ -363,6 +363,7 @@ public class GridJettyRestHandler extends AbstractHandler {
             }
 
             case CACHE_CONTAINS_KEY:
+            case CACHE_CONTAINS_KEYS:
             case CACHE_GET:
             case CACHE_GET_ALL:
             case CACHE_PUT:
@@ -400,7 +401,7 @@ public class GridJettyRestHandler extends AbstractHandler {
 
                         restReq0.values(map);
                     }
-                    else if (cmd == CACHE_GET_ALL || cmd == CACHE_REMOVE_ALL) {
+                    else if (cmd == CACHE_GET_ALL || cmd == CACHE_REMOVE_ALL || cmd == CACHE_CONTAINS_KEYS)
{
                         JSONCacheObject cacheObj = new JSONCacheObject(o);
 
                         List keys = (List)cacheObj.getField("keys");


Mime
View raw message