ignite-commits mailing list archives

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

Branch: refs/heads/ignite-964
Commit: 943d2dbce9b01572f434582fef4cf6efd058f0d7
Parents: 1ba51fc
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Wed Jul 1 15:17:46 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Wed Jul 1 15:17:46 2015 +0300

----------------------------------------------------------------------
 .../processors/rest/GridRestCommand.java        |  3 +
 .../processors/rest/GridRestProcessor.java      |  1 +
 .../handlers/cache/GridCacheCommandHandler.java | 40 ++++++++++++
 modules/nodejs/src/main/js/cache.js             | 15 ++++-
 .../ignite/internal/NodeJsCacheApiSelfTest.java |  7 ++
 modules/nodejs/src/test/js/test-cache-api.js    | 69 +++++++++-----------
 .../http/jetty/GridJettyRestHandler.java        |  3 +-
 7 files changed, 98 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/943d2dbc/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 a6563c9..2d9a159 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
@@ -42,6 +42,9 @@ public enum GridRestCommand {
     /** Get several cached values. */
     CACHE_GET_ALL("getall"),
 
+    /** Store value in cache and return previous value. */
+    CACHE_GET_AND_PUT("getandput"),
+
     /** Store value in cache. */
     CACHE_PUT("put"),
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/943d2dbc/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 f688d3a..ee9bfce 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
@@ -548,6 +548,7 @@ public class GridRestProcessor extends GridProcessorAdapter {
             case CACHE_CAS:
             case CACHE_APPEND:
             case CACHE_PREPEND:
+            case CACHE_GET_AND_PUT:
                 perm = SecurityPermission.CACHE_PUT;
                 name = ((GridRestCacheRequest)req).cacheName();
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/943d2dbc/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 6a966ee..5d7f24a 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
@@ -56,6 +56,7 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
         CACHE_CONTAINS_KEYS,
         CACHE_CONTAINS_KEY,
         CACHE_GET,
+        CACHE_GET_AND_PUT,
         CACHE_GET_ALL,
         CACHE_PUT,
         CACHE_ADD,
@@ -73,6 +74,7 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
     private static final EnumSet<GridRestCommand> KEY_REQUIRED_REQUESTS = EnumSet.of(
         CACHE_CONTAINS_KEY,
         CACHE_GET,
+        CACHE_GET_AND_PUT,
         CACHE_PUT,
         CACHE_ADD,
         CACHE_REMOVE,
@@ -161,6 +163,18 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
                     break;
                 }
 
+                case CACHE_GET_AND_PUT: {
+                    final Object val = req0.value();
+
+                    if (val == null)
+                        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("val"));
+
+                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName,
skipStore, key,
+                        new GetAndPutCommand(key, val));
+
+                    break;
+                }
+
                 case CACHE_GET_ALL: {
                     fut = executeCommand(req.destinationId(), req.clientId(), cacheName,
skipStore, key,
                         new GetAllCommand(getKeys(req0)));
@@ -791,6 +805,32 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
     }
 
     /** */
+    private static class GetAndPutCommand extends CacheProjectionCommand {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** Key. */
+        private final Object key;
+
+        /** Value.*/
+        private final Object val;
+
+        /**
+         * @param key Key.
+         * @param val Value.
+         */
+        GetAndPutCommand(Object key, Object val) {
+            this.key = key;
+            this.val = val;
+        }
+
+        /** {@inheritDoc} */
+        @Override public IgniteInternalFuture<?> applyx(IgniteInternalCache<Object,
Object> c, GridKernalContext ctx) {
+            return c.getAndPutAsync(key, val);
+        }
+    }
+
+    /** */
     private static class GetAllCommand extends CacheProjectionCommand {
         /** */
         private static final long serialVersionUID = 0L;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/943d2dbc/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 6152d57..4ac07f8 100644
--- a/modules/nodejs/src/main/js/cache.js
+++ b/modules/nodejs/src/main/js/cache.js
@@ -140,7 +140,7 @@ Cache.prototype.containsKey = function(key, callback) {
 }
 
 /**
- * Determines if the cache contains an entry for the specified key.
+ * Determines if the cache contains all keys.
  *
  * @this {Cache}
  * @param {Object[]} keys Keys
@@ -152,6 +152,19 @@ Cache.prototype.containsKeys = function(keys, callback) {
 }
 
 /**
+ * Put cache value
+ *
+ * @this {Cache}
+ * @param {string} key Key
+ * @param {string} value Value
+ * @param {onGet} callback Called on finish
+ */
+Cache.prototype.getAndPut = function(key, val, callback) {
+    this._server.runCommand(this._createCommand("getandput").
+        setPostData(JSON.stringify({"key" : key, "val" : val})), callback);
+}
+
+/**
  * Execute sql query
  *
  * @param {SqlQuery|SqlFieldsQuery} qry Query

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/943d2dbc/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 f992a7c..d79debc 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
@@ -126,4 +126,11 @@ public class NodeJsCacheApiSelfTest extends NodeJsAbstractTest {
     public void testNotContainsAll() throws Exception {
         runJsScript("testNotContainsAll");
     }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testGetAndPut() throws Exception {
+        runJsScript("testGetAndPut");
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/943d2dbc/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 5a2e9f1..904a65f 100644
--- a/modules/nodejs/src/test/js/test-cache-api.js
+++ b/modules/nodejs/src/test/js/test-cache-api.js
@@ -35,26 +35,11 @@ testContains = function() {
 }
 
 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});
+    startTest("mycache", {trace: [putAll, containsKeys], entry: objectEntries()});
 }
 
 testNotContainsAll = function() {
-    entries = [];
-
-    entries.push(new Entry("key1", "val1"));
-    entries.push(new Entry("key2", "val2"));
-
-    startTest("mycache", {trace: [notContainsKeys], entry: entries});
+    startTest("mycache", {trace: [notContainsKeys], entry: stringEntries()});
 }
 
 testRemove = function() {
@@ -66,29 +51,41 @@ testRemoveNoKey = function() {
 }
 
 testPutAllGetAll = function() {
-    entries = [];
+    startTest("mycache", {trace: [putAll, getAll], entry: stringEntries()});
+}
 
-    entries.push(new Entry("key1", "val1"));
-    entries.push(new Entry("key2", "val2"));
+testPutAllObjectGetAll = function() {
+    startTest("mycache", {trace: [putAll, getAll], entry: objectEntries()});
+}
 
-    startTest("mycache", {trace: [putAll, getAll], entry: entries});
+testRemoveAllObjectGetAll = function() {
+    startTest("mycache", {trace: [putAll, getAll, removeAll, getNone], entry: objectEntries()});
 }
 
-testPutAllObjectGetAll = function() {
-    entries = [];
+testRemoveAll = function() {
+    startTest("mycache", {trace: [putAll, getAll, removeAll, getNone], entry: stringEntries()});
+}
 
-    var key1 = {"name" : "Ann"};
-    var key2 = {"name" : "Paul"};
-    var val1 = {"age" : 12, "books" : ["1", "Book"]};
-    var val2 = {"age" : 13, "books" : ["1", "Book"]};
+testIncorrectCacheName = function() {
+    startTest("mycache1", {trace: [incorrectPut], entry: ["key", "6"]});
+}
 
-    entries.push(new Entry(key1, val1));
-    entries.push(new Entry(key2, val2));
+testGetAndPut = function() {
+    function onGetAndPut(err, res) {
+        assert(err === null, "Get error on get and put [err=" + err + "]");
+        assert(res === "6", "Incorrect result for getAndPut [expected=6, val" + res + "]");
 
-    startTest("mycache", {trace: [putAll, getAll], entry: entries});
+        TestUtils.testDone();
+    }
+
+    function getAndPut(cache, entry) {
+        cache.getAndPut("key", "7", onGetAndPut);
+    }
+
+    startTest("mycache", {trace: [put, getAndPut], entry: ["key", "6"]});
 }
 
-testRemoveAllObjectGetAll = function() {
+function objectEntries() {
     entries = [];
 
     var key1 = {"name" : "Ann"};
@@ -99,20 +96,16 @@ testRemoveAllObjectGetAll = function() {
     entries.push(new Entry(key1, val1));
     entries.push(new Entry(key2, val2));
 
-    startTest("mycache", {trace: [putAll, getAll, removeAll, getNone], entry: entries});
+    return entries;
 }
 
-testRemoveAll = function() {
+function stringEntries() {
     entries = [];
 
     entries.push(new Entry("key1", "val1"));
     entries.push(new Entry("key2", "val2"));
 
-    startTest("mycache", {trace: [putAll, getAll, removeAll, getNone], entry: entries});
-}
-
-testIncorrectCacheName = function() {
-    startTest("mycache1", {trace: [incorrectPut], entry: ["key", "6"]});
+    return entries;
 }
 
 function startTest(cacheName, testDescription) {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/943d2dbc/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 d864713..029129b 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
@@ -366,6 +366,7 @@ public class GridJettyRestHandler extends AbstractHandler {
             case CACHE_CONTAINS_KEYS:
             case CACHE_GET:
             case CACHE_GET_ALL:
+            case CACHE_GET_AND_PUT:
             case CACHE_PUT:
             case CACHE_PUT_ALL:
             case CACHE_REMOVE:
@@ -414,7 +415,7 @@ public class GridJettyRestHandler extends AbstractHandler {
                         restReq0.values(map);
                     }
                     else if (cmd == CACHE_GET || cmd == CACHE_PUT || cmd == CACHE_REMOVE
||
-                        cmd == CACHE_CONTAINS_KEY) {
+                        cmd == CACHE_CONTAINS_KEY || cmd == CACHE_GET_AND_PUT) {
                         JSONCacheObject cacheObj = new JSONCacheObject(o);
 
                         restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName);


Mime
View raw message