ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shro...@apache.org
Subject ignite git commit: IGNITE-3066: Implemented NX and XX options for SET command.
Date Wed, 09 Nov 2016 06:36:09 GMT
Repository: ignite
Updated Branches:
  refs/heads/ignite-2788 c827e0428 -> 606ff932c


IGNITE-3066: Implemented NX and XX options for SET command.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/606ff932
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/606ff932
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/606ff932

Branch: refs/heads/ignite-2788
Commit: 606ff932c2276569f6e2011b910a4cbe7c390994
Parents: c827e04
Author: shtykh_roman <rshtykh@yahoo.com>
Authored: Wed Nov 9 15:35:18 2016 +0900
Committer: shtykh_roman <rshtykh@yahoo.com>
Committed: Wed Nov 9 15:35:18 2016 +0900

----------------------------------------------------------------------
 .../tcp/redis/RedisProtocolSelfTest.java        | 11 +++++
 .../string/GridRedisAppendCommandHandler.java   |  2 +-
 .../string/GridRedisSetCommandHandler.java      | 45 ++++++++++++++++++--
 3 files changed, 54 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/606ff932/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java
b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java
index 19111e6..d577efd 100644
--- a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java
+++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java
@@ -205,6 +205,17 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest {
 
             Assert.assertEquals("1", jcache().get("setKey1"));
             Assert.assertEquals("b0", jcache().get("setKey2"));
+
+            // test options.
+            jedis.set("setKey1", "2", "nx");
+            jedis.set("setKey3", "3", "nx");
+            Assert.assertEquals("1", jcache().get("setKey1"));
+            Assert.assertEquals("3", jcache().get("setKey3"));
+
+            jedis.set("setKey1", "2", "xx");
+            jedis.set("setKey4", "4", "xx");
+            Assert.assertEquals("2", jcache().get("setKey1"));
+            Assert.assertNull(jcache().get("setKey4"));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/606ff932/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java
index 78fc8d3..fa70793 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java
@@ -78,7 +78,7 @@ public class GridRedisAppendCommandHandler extends GridRedisThruRestCommandHandl
 
         Object resp = hnd.handle(appendReq).getResponse();
         if (resp != null && !(boolean)resp) {
-            // append on existing key.
+            // append on non-existing key.
             GridRestCacheRequest setReq = new GridRestCacheRequest();
 
             setReq.clientId(msg.clientId());

http://git-wip-us.apache.org/repos/asf/ignite/blob/606ff932/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java
index 1d447f1..bbc3c06 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java
@@ -32,6 +32,8 @@ import org.apache.ignite.internal.processors.rest.request.GridRestRequest;
 import org.apache.ignite.internal.util.typedef.internal.U;
 
 import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_PUT;
+import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_PUT_IF_ABSENT;
+import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_REPLACE;
 import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.SET;
 
 /**
@@ -75,15 +77,52 @@ public class GridRedisSetCommandHandler extends GridRedisThruRestCommandHandler
         restReq.value(msg.aux(VAL_POS));
 
         if (msg.messageSize() >= 4) {
-            // TODO: handle options.
+
+            List<String> params = msg.aux();
+
+            // get rid of SET value.
+            params.remove(0);
+
+            if (isNx(params))
+                restReq.command(CACHE_PUT_IF_ABSENT);
+            else if (isXx(params))
+                restReq.command(CACHE_REPLACE);
+
+            // TODO: handle expiration options.
         }
 
         return restReq;
     }
 
+    /**
+     * @param params Command parameters.
+     * @return True if NX option is available, otherwise false.
+     */
+    private boolean isNx(List<String> params) {
+        if (params.size() >= 3)
+            return params.get(0).equalsIgnoreCase("nx") || params.get(2).equalsIgnoreCase("nx");
+        else
+            return params.get(0).equalsIgnoreCase("nx");
+    }
+
+    /**
+     * @param params Command parameters.
+     * @return True if XX option is available, otherwise false.
+     */
+    private boolean isXx(List<String> params) {
+        if (params.size() >= 3)
+            return params.get(0).equalsIgnoreCase("xx") || params.get(2).equalsIgnoreCase("xx");
+        else
+            return params.get(0).equalsIgnoreCase("xx");
+    }
+
     /** {@inheritDoc} */
     @Override public ByteBuffer makeResponse(final GridRestResponse restRes, List<String>
params) {
-        return (restRes.getResponse() == null ? GridRedisProtocolParser.nil()
-            : GridRedisProtocolParser.OkString());
+        Object resp = restRes.getResponse();
+
+        if (resp == null)
+            return GridRedisProtocolParser.nil();
+
+        return (!(boolean)resp ? GridRedisProtocolParser.nil() : GridRedisProtocolParser.OkString());
     }
 }


Mime
View raw message