You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sh...@apache.org on 2016/04/19 06:06:10 UTC

ignite git commit: IGNITE-2788: Basic Redis protocol implementation. Added STRLEN and GETSET commands.

Repository: ignite
Updated Branches:
  refs/heads/ignite-2788 a3231a7d9 -> 998cf4da7


IGNITE-2788: Basic Redis protocol implementation. Added STRLEN and GETSET commands.


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

Branch: refs/heads/ignite-2788
Commit: 998cf4da7ddbbb417d7f3fc6aa9b0c440e09d594
Parents: a3231a7
Author: shtykh_roman <rs...@yahoo.com>
Authored: Tue Apr 19 12:59:38 2016 +0900
Committer: shtykh_roman <rs...@yahoo.com>
Committed: Tue Apr 19 12:59:38 2016 +0900

----------------------------------------------------------------------
 .../processors/redis/RedisProtocolSelfTest.java | 24 ++++++
 .../protocols/tcp/redis/GridRedisCommand.java   |  6 +-
 .../tcp/redis/GridRedisNioListener.java         |  4 +
 .../string/GridRedisAppendCommandHandler.java   |  4 +-
 .../string/GridRedisGetSetCommandHandler.java   | 83 ++++++++++++++++++++
 .../string/GridRedisStrlenCommandHandler.java   | 79 +++++++++++++++++++
 6 files changed, 197 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/998cf4da/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java
index aa5b077..0d27558 100644
--- a/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java
+++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java
@@ -167,6 +167,18 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest {
     /**
      * @throws Exception If failed.
      */
+    public void testGetSet() throws Exception {
+        try (Jedis jedis = pool.getResource()) {
+            jcache().put("getSetKey1", 1);
+
+            Assert.assertEquals("1", jedis.getSet("getSetKey1", "0"));
+            Assert.assertNull(jedis.get("getSetNonExistingKey"));
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
     public void testMGet() throws Exception {
         try (Jedis jedis = pool.getResource()) {
             jcache().put("getKey1", "getVal1");
@@ -285,4 +297,16 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest {
             Assert.assertEquals(12, (long)jedis.append("appendKey1", " World!"));
         }
     }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testStrlen() throws Exception {
+        try (Jedis jedis = pool.getResource()) {
+            Assert.assertEquals(0, (long)jedis.strlen("strlenKeyNonExisting"));
+
+            jcache().put("strlenKey", "abc");
+            Assert.assertEquals(3, (long)jedis.strlen("strlenKey"));
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/998cf4da/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java
index ba5c7ed..8e7e6a8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java
@@ -50,7 +50,11 @@ public enum GridRedisCommand {
     /** INCRBYFLOAT. */
     INCRBYFLOAT("INCRBYFLOAT"),
     /** APPEND. */
-    APPEND("APPEND");
+    APPEND("APPEND"),
+    /** STRLEN. */
+    STRLEN("STRLEN"),
+    /** GETSET. */
+    GETSET("GETSET");
 
     /** String for command. */
     private final String cmd;

http://git-wip-us.apache.org/repos/asf/ignite/blob/998cf4da/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java
index f75ea73..78dcb93 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java
@@ -28,10 +28,12 @@ import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.Gr
 import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.GridRedisConnectionCommandHandler;
 import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisAppendCommandHandler;
 import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisGetCommandHandler;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisGetSetCommandHandler;
 import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisIncrDecrCommandHandler;
 import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisMGetCommandHandler;
 import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisMSetCommandHandler;
 import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisSetCommandHandler;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string.GridRedisStrlenCommandHandler;
 import org.apache.ignite.internal.util.nio.GridNioFuture;
 import org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter;
 import org.apache.ignite.internal.util.nio.GridNioSession;
@@ -66,6 +68,8 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter<GridRedis
         addCommandHandler(new GridRedisMGetCommandHandler(ctx, hnd));
         addCommandHandler(new GridRedisIncrDecrCommandHandler(ctx, hnd));
         addCommandHandler(new GridRedisAppendCommandHandler(ctx, hnd));
+        addCommandHandler(new GridRedisGetSetCommandHandler(ctx, hnd));
+        addCommandHandler(new GridRedisStrlenCommandHandler(ctx, hnd));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/998cf4da/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisAppendCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisAppendCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisAppendCommandHandler.java
index 8ac4c5c..be965f9 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisAppendCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisAppendCommandHandler.java
@@ -62,8 +62,8 @@ public class GridRedisAppendCommandHandler extends GridRedisStringCommandHandler
     @Override public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
         assert msg != null;
 
-        if (msg.getMsgParts().size() <= VAL_POS)
-            throw new GridRedisGenericException("Wrong number of arguments!");
+        if (msg.getMsgParts().size() < VAL_POS + 1)
+            throw new GridRedisGenericException("Wrong syntax!");
 
         GridRestCacheRequest appendReq = new GridRestCacheRequest();
         GridRestCacheRequest getReq = new GridRestCacheRequest();

http://git-wip-us.apache.org/repos/asf/ignite/blob/998cf4da/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetSetCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetSetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetSetCommandHandler.java
new file mode 100644
index 0000000..7ef55fb
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetSetCommandHandler.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string;
+
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.processors.rest.GridRestProtocolHandler;
+import org.apache.ignite.internal.processors.rest.GridRestResponse;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisProtocolParser;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.GridRedisStringCommandHandler;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.exception.GridRedisGenericException;
+import org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest;
+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_GET_AND_PUT;
+import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.GETSET;
+
+/**
+ * Redis GETSET command handler.
+ */
+public class GridRedisGetSetCommandHandler extends GridRedisStringCommandHandler {
+    /** Supported commands. */
+    private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList(
+        GETSET
+    );
+
+    /** Value position in Redis message. */
+    private static final int VAL_POS = 2;
+
+    /** {@inheritDoc} */
+    public GridRedisGetSetCommandHandler(final GridKernalContext ctx, final GridRestProtocolHandler hnd) {
+        super(ctx, hnd);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Collection<GridRedisCommand> supportedCommands() {
+        return SUPPORTED_COMMANDS;
+    }
+
+    /** {@inheritDoc} */
+    @Override public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
+        assert msg != null;
+
+        if (msg.getMsgParts().size() < VAL_POS + 1)
+            throw new GridRedisGenericException("Wrong syntax!");
+
+        GridRestCacheRequest restReq = new GridRestCacheRequest();
+
+        restReq.clientId(msg.clientId());
+        restReq.key(msg.key());
+        restReq.value(msg.getMsgParts().get(VAL_POS));
+
+        restReq.command(CACHE_GET_AND_PUT);
+
+        return restReq;
+    }
+
+    /** {@inheritDoc} */
+    @Override public ByteBuffer makeResponse(final GridRestResponse restRes) {
+        return (restRes.getResponse() == null ? GridRedisProtocolParser.nil()
+            : GridRedisProtocolParser.toBulkString(restRes.getResponse()));
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/998cf4da/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisStrlenCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisStrlenCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisStrlenCommandHandler.java
new file mode 100644
index 0000000..d62ddae
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisStrlenCommandHandler.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.string;
+
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.processors.rest.GridRestProtocolHandler;
+import org.apache.ignite.internal.processors.rest.GridRestResponse;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisProtocolParser;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.GridRedisStringCommandHandler;
+import org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest;
+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_GET;
+import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.STRLEN;
+
+/**
+ * Redis STRLEN command handler.
+ */
+public class GridRedisStrlenCommandHandler extends GridRedisStringCommandHandler {
+    /** Supported commands. */
+    private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList(
+        STRLEN
+    );
+
+    /** {@inheritDoc} */
+    public GridRedisStrlenCommandHandler(final GridKernalContext ctx, final GridRestProtocolHandler hnd) {
+        super(ctx, hnd);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Collection<GridRedisCommand> supportedCommands() {
+        return SUPPORTED_COMMANDS;
+    }
+
+    /** {@inheritDoc} */
+    @Override public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
+        assert msg != null;
+
+        GridRestCacheRequest restReq = new GridRestCacheRequest();
+
+        restReq.clientId(msg.clientId());
+        restReq.key(msg.key());
+
+        restReq.command(CACHE_GET);
+
+        return restReq;
+    }
+
+    /** {@inheritDoc} */
+    @Override public ByteBuffer makeResponse(final GridRestResponse restRes) {
+        if (restRes.getResponse() == null)
+            return GridRedisProtocolParser.toInteger("0");
+        else {
+            int len = String.valueOf(restRes.getResponse()).length();
+            return GridRedisProtocolParser.toInteger(String.valueOf(len));
+        }
+    }
+}