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/18 12:04:21 UTC
ignite git commit: IGNITE-2788: Basic Redis protocol implementation.
Introduced Redis protocol exceptions and added commands.
Repository: ignite
Updated Branches:
refs/heads/ignite-2788 b3e7a2a5f -> a3231a7d9
IGNITE-2788: Basic Redis protocol implementation. Introduced Redis protocol exceptions and added commands.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/a3231a7d
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/a3231a7d
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/a3231a7d
Branch: refs/heads/ignite-2788
Commit: a3231a7d9338746ba2d25c8b6571b944a0f02556
Parents: b3e7a2a
Author: shtykh_roman <rs...@yahoo.com>
Authored: Mon Apr 18 18:57:47 2016 +0900
Committer: shtykh_roman <rs...@yahoo.com>
Committed: Mon Apr 18 18:57:47 2016 +0900
----------------------------------------------------------------------
.../processors/redis/RedisProtocolSelfTest.java | 65 ++++++++-
.../protocols/tcp/redis/GridRedisCommand.java | 12 +-
.../tcp/redis/GridRedisNioListener.java | 14 +-
.../tcp/redis/GridRedisProtocolParser.java | 4 +-
.../handler/GridRedisStringCommandHandler.java | 12 +-
.../exception/GridRedisGenericException.java | 20 +++
.../exception/GridRedisTypeException.java | 20 +++
.../string/GridRedisAppendCommandHandler.java | 106 +++++++++++++++
.../string/GridRedisGetCommandHandler.java | 5 +-
.../string/GridRedisIncrCommandHandler.java | 104 ---------------
.../string/GridRedisIncrDecrCommandHandler.java | 131 +++++++++++++++++++
.../string/GridRedisMGetCommandHandler.java | 5 +-
.../string/GridRedisMSetCommandHandler.java | 5 +-
.../string/GridRedisSetCommandHandler.java | 5 +-
14 files changed, 381 insertions(+), 127 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/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 6fe62d5..aa5b077 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
@@ -17,6 +17,7 @@
package org.apache.ignite.internal.processors.redis;
+import java.math.BigInteger;
import java.util.List;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.configuration.CacheConfiguration;
@@ -174,6 +175,8 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest {
List<String> result = jedis.mget("getKey1", "getKey2", "wrongKey");
Assert.assertTrue(result.contains("getVal1"));
Assert.assertTrue(result.contains("0"));
+
+ // not supported.
// fail("Incompatible! getAll() does not return null values!");
// Assert.assertTrue(result.contains("nil"));
}
@@ -207,12 +210,15 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest {
/**
* @throws Exception If failed.
*/
- public void testIncr() throws Exception {
+ public void testIncrDecr() throws Exception {
try (Jedis jedis = pool.getResource()) {
- Assert.assertEquals(1, (long)jedis.incr("newKey"));
+ Assert.assertEquals(1, (long)jedis.incr("newKeyIncr"));
+ Assert.assertEquals(-1, (long)jedis.decr("newKeyDecr"));
jcache().put("incrKey1", 1L);
Assert.assertEquals(2L, (long)jedis.incr("incrKey1"));
+ jcache().put("decrKey1", 1L);
+ Assert.assertEquals(0L, (long)jedis.decr("decrKey1"));
jcache().put("nonInt", "abc");
try {
@@ -221,7 +227,62 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest {
assert false : "Exception has to be thrown!";
}
catch (JedisDataException e) {
+ assertTrue(e.getMessage().startsWith("ERR"));
+ }
+ try {
+ jedis.decr("nonInt");
+
+ assert false : "Exception has to be thrown!";
}
+ catch (JedisDataException e) {
+ assertTrue(e.getMessage().startsWith("ERR"));
+ }
+
+ jcache().put("outOfRange", new BigInteger("234293482390480948029348230948"));
+ try {
+ jedis.incr("outOfRange");
+
+ assert false : "Exception has to be thrown!";
+ }
+ catch (JedisDataException e) {
+ assertTrue(e.getMessage().startsWith("ERR"));
+ }
+ }
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testIncrDecrBy() throws Exception {
+ try (Jedis jedis = pool.getResource()) {
+ Assert.assertEquals(2, (long)jedis.incrBy("newKeyIncr1", 2));
+ Assert.assertEquals(-2, (long)jedis.decrBy("newKeyDecr1", 2));
+
+ jcache().put("incrKey2", 1L);
+ Assert.assertEquals(3L, (long)jedis.incrBy("incrKey2", 2));
+ jcache().put("decrKey2", 2L);
+ Assert.assertEquals(0L, (long)jedis.decrBy("decrKey2", 2));
+ }
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testIncrByFloat() throws Exception {
+ // not supported.
+// try (Jedis jedis = pool.getResource()) {
+// jcache().put("incrKeyFloat", 10.50);
+// Assert.assertEquals(10.6, jedis.incrByFloat("incrKeyFloat", 0.1), 1e-15);
+// }
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testAppend() throws Exception {
+ try (Jedis jedis = pool.getResource()) {
+ Assert.assertEquals(5, (long)jedis.append("appendKey1", "Hello"));
+ Assert.assertEquals(12, (long)jedis.append("appendKey1", " World!"));
}
}
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/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 b0f78bc..ba5c7ed 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
@@ -40,7 +40,17 @@ public enum GridRedisCommand {
/** MSET. */
MSET("MSET"),
/** INCR. */
- INCR("INCR");
+ INCR("INCR"),
+ /** DECR. */
+ DECR("DECR"),
+ /** INCRBY. */
+ INCRBY("INCRBY"),
+ /** DECRBY. */
+ DECRBY("DECRBY"),
+ /** INCRBYFLOAT. */
+ INCRBYFLOAT("INCRBYFLOAT"),
+ /** APPEND. */
+ APPEND("APPEND");
/** String for command. */
private final String cmd;
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/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 0ea5138..f75ea73 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
@@ -26,8 +26,9 @@ import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.processors.rest.GridRestProtocolHandler;
import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.GridRedisCommandHandler;
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.GridRedisIncrCommandHandler;
+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;
@@ -59,11 +60,12 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter<GridRedis
addCommandHandler(new GridRedisConnectionCommandHandler());
// string commands.
- addCommandHandler(new GridRedisGetCommandHandler(hnd));
- addCommandHandler(new GridRedisSetCommandHandler(hnd));
- addCommandHandler(new GridRedisMSetCommandHandler(hnd));
- addCommandHandler(new GridRedisMGetCommandHandler(hnd));
- addCommandHandler(new GridRedisIncrCommandHandler(hnd));
+ addCommandHandler(new GridRedisGetCommandHandler(ctx, hnd));
+ addCommandHandler(new GridRedisSetCommandHandler(ctx, hnd));
+ addCommandHandler(new GridRedisMSetCommandHandler(ctx, hnd));
+ addCommandHandler(new GridRedisMGetCommandHandler(ctx, hnd));
+ addCommandHandler(new GridRedisIncrDecrCommandHandler(ctx, hnd));
+ addCommandHandler(new GridRedisAppendCommandHandler(ctx, hnd));
}
/**
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java
index 3ded91c..a771c3a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java
@@ -217,8 +217,8 @@ public class GridRedisProtocolParser {
* @param integer Integer result.
* @return RESP integer.
*/
- public static ByteBuffer toInteger(int integer) {
- byte[] b = String.valueOf(integer).getBytes();
+ public static ByteBuffer toInteger(String integer) {
+ byte[] b = integer.getBytes();
ByteBuffer buf = ByteBuffer.allocate(b.length + 3);
buf.put(INTEGER);
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java
index 00d0b86..c07656d 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java
@@ -19,11 +19,13 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler;
import java.nio.ByteBuffer;
import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.IgniteInternalFuture;
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.GridRedisMessage;
import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisProtocolParser;
+import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.exception.GridRedisTypeException;
import org.apache.ignite.internal.processors.rest.request.GridRestRequest;
import org.apache.ignite.internal.util.future.GridFinishedFuture;
import org.apache.ignite.internal.util.typedef.CX1;
@@ -40,7 +42,7 @@ public abstract class GridRedisStringCommandHandler implements GridRedisCommandH
*
* @param hnd REST protocol handler.
*/
- public GridRedisStringCommandHandler(GridRestProtocolHandler hnd) {
+ public GridRedisStringCommandHandler(final GridKernalContext ctx, final GridRestProtocolHandler hnd) {
this.hnd = hnd;
}
@@ -58,9 +60,8 @@ public abstract class GridRedisStringCommandHandler implements GridRedisCommandH
GridRedisMessage res = msg;
- if (restRes.getSuccessStatus() == GridRestResponse.STATUS_SUCCESS) {
+ if (restRes.getSuccessStatus() == GridRestResponse.STATUS_SUCCESS)
res.setResponse(makeResponse(restRes));
- }
else
res.setResponse(GridRedisProtocolParser.toGenericError("Operation error!"));
@@ -69,7 +70,10 @@ public abstract class GridRedisStringCommandHandler implements GridRedisCommandH
});
}
catch (IgniteCheckedException e) {
- msg.setResponse(GridRedisProtocolParser.toGenericError("Operation error!"));
+ if (e instanceof GridRedisTypeException)
+ msg.setResponse(GridRedisProtocolParser.toTypeError(e.getMessage()));
+ else
+ msg.setResponse(GridRedisProtocolParser.toGenericError(e.getMessage()));
return new GridFinishedFuture<>(msg);
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisGenericException.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisGenericException.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisGenericException.java
new file mode 100644
index 0000000..6c7c688
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisGenericException.java
@@ -0,0 +1,20 @@
+package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.exception;
+
+import org.apache.ignite.IgniteCheckedException;
+
+/**
+ * Generic Redis protocol exception.
+ */
+public class GridRedisGenericException extends IgniteCheckedException {
+ /** */
+ private static final long serialVersionUID = 0L;
+
+ /**
+ * Creates a generic exception with given error message.
+ *
+ * @param msg Error message.
+ */
+ public GridRedisGenericException(String msg) {
+ super(msg);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisTypeException.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisTypeException.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisTypeException.java
new file mode 100644
index 0000000..99cb577
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/exception/GridRedisTypeException.java
@@ -0,0 +1,20 @@
+package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.exception;
+
+import org.apache.ignite.IgniteCheckedException;
+
+/**
+ * Exception on operation on the wrong data type.
+ */
+public class GridRedisTypeException extends IgniteCheckedException {
+ /** */
+ private static final long serialVersionUID = 0L;
+
+ /**
+ * Creates a type exception with given error message.
+ *
+ * @param msg Error message.
+ */
+ public GridRedisTypeException(String msg) {
+ super(msg);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/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
new file mode 100644
index 0000000..8ac4c5c
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisAppendCommandHandler.java
@@ -0,0 +1,106 @@
+/*
+ * 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_APPEND;
+import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_GET;
+import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_PUT;
+import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.APPEND;
+
+/**
+ * Redis APPEND command handler.
+ */
+public class GridRedisAppendCommandHandler extends GridRedisStringCommandHandler {
+ /** Supported commands. */
+ private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList(
+ APPEND
+ );
+
+ private static final int VAL_POS = 2;
+
+ /** {@inheritDoc} */
+ public GridRedisAppendCommandHandler(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)
+ throw new GridRedisGenericException("Wrong number of arguments!");
+
+ GridRestCacheRequest appendReq = new GridRestCacheRequest();
+ GridRestCacheRequest getReq = new GridRestCacheRequest();
+
+ String val = msg.getMsgParts().get(VAL_POS);
+
+ appendReq.clientId(msg.clientId());
+ appendReq.key(msg.key());
+ appendReq.value(val);
+ appendReq.command(CACHE_APPEND);
+
+ if ((boolean)hnd.handle(appendReq).getResponse() == false) {
+ // append on on-existing key in REST returns false.
+ GridRestCacheRequest setReq = new GridRestCacheRequest();
+
+ setReq.clientId(msg.clientId());
+ setReq.key(msg.key());
+ setReq.value(val);
+ setReq.command(CACHE_PUT);
+
+ hnd.handle(setReq);
+ }
+
+ getReq.clientId(msg.clientId());
+ getReq.key(msg.key());
+ getReq.command(CACHE_GET);
+
+ return getReq;
+ }
+
+ /** {@inheritDoc} */
+ @Override public ByteBuffer makeResponse(final GridRestResponse restRes) {
+ if (restRes.getResponse() == null)
+ return GridRedisProtocolParser.nil();
+ else {
+ int resLen = ((String)restRes.getResponse()).length();
+ return GridRedisProtocolParser.toInteger(String.valueOf(resLen));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetCommandHandler.java
index d799dc9..d9ce2f7 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetCommandHandler.java
@@ -20,6 +20,7 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.s
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;
@@ -43,8 +44,8 @@ public class GridRedisGetCommandHandler extends GridRedisStringCommandHandler {
);
/** {@inheritDoc} */
- public GridRedisGetCommandHandler(GridRestProtocolHandler hnd) {
- super(hnd);
+ public GridRedisGetCommandHandler(final GridKernalContext ctx, final GridRestProtocolHandler hnd) {
+ super(ctx, hnd);
}
/** {@inheritDoc} */
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrCommandHandler.java
deleted file mode 100644
index d540da3..0000000
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrCommandHandler.java
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * 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.math.BigDecimal;
-import java.nio.ByteBuffer;
-import java.util.Collection;
-import org.apache.ignite.IgniteCheckedException;
-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.DataStructuresRequest;
-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.ATOMIC_INCREMENT;
-import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_GET;
-import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.INCR;
-
-/**
- * Redis INCR command handler.
- */
-public class GridRedisIncrCommandHandler extends GridRedisStringCommandHandler {
- /** Supported commands. */
- private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList(
- INCR
- );
-
- /** {@inheritDoc} */
- public GridRedisIncrCommandHandler(GridRestProtocolHandler hnd) {
- super(hnd);
- }
-
- /** {@inheritDoc} */
- @Override public Collection<GridRedisCommand> supportedCommands() {
- return SUPPORTED_COMMANDS;
- }
-
- /** {@inheritDoc} */
- @Override public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
- assert msg != null;
-
- DataStructuresRequest restReq = new DataStructuresRequest();
-
- GridRestCacheRequest getReq = new GridRestCacheRequest();
- GridRedisMessage getMsg = msg.copy(msg);
-
- getReq.clientId(getMsg.clientId());
- getReq.key(getMsg.key());
- getReq.command(CACHE_GET);
-
- GridRestResponse getResp = hnd.handle(getReq);
-
- if (getResp.getResponse() == null) {
- restReq.initial(0L);
- }
- else {
- if (getResp.getResponse() instanceof Long)
- restReq.initial((Long)getResp.getResponse());
- else
- throw new IgniteCheckedException("Failed to obtain an initial value!");
- }
-
- restReq.clientId(msg.clientId());
- restReq.key(msg.key());
- restReq.delta(1L);
- restReq.command(ATOMIC_INCREMENT);
-
- return restReq;
- }
-
- /** {@inheritDoc} */
- @Override public ByteBuffer makeResponse(final GridRestResponse restRes) {
- if (restRes.getResponse() == null)
- return GridRedisProtocolParser.toGenericError("Failed to increment!");
-
- Long val;
- if (restRes.getResponse() instanceof Long)
- val = (Long)restRes.getResponse();
- else
- return GridRedisProtocolParser.toTypeError("Non-numeric value!");
-
- return GridRedisProtocolParser.toInteger(new BigDecimal(val).intValueExact());
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrDecrCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrDecrCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrDecrCommandHandler.java
new file mode 100644
index 0000000..78e042c
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrDecrCommandHandler.java
@@ -0,0 +1,131 @@
+/*
+ * 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.math.BigDecimal;
+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.DataStructuresRequest;
+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.ATOMIC_DECREMENT;
+import static org.apache.ignite.internal.processors.rest.GridRestCommand.ATOMIC_INCREMENT;
+import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_GET;
+import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.DECR;
+import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.DECRBY;
+import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.INCR;
+import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.INCRBY;
+
+/**
+ * Redis INCR/DECR command handler.
+ */
+public class GridRedisIncrDecrCommandHandler extends GridRedisStringCommandHandler {
+ /** Supported commands. */
+ private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList(
+ INCR,
+ DECR,
+ INCRBY,
+ DECRBY
+ );
+
+ private static final int DELTA_POS = 2;
+
+ /** {@inheritDoc} */
+ public GridRedisIncrDecrCommandHandler(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;
+
+ DataStructuresRequest restReq = new DataStructuresRequest();
+
+ GridRestCacheRequest getReq = new GridRestCacheRequest();
+
+ getReq.clientId(msg.clientId());
+ getReq.key(msg.key());
+ getReq.command(CACHE_GET);
+
+ GridRestResponse getResp = hnd.handle(getReq);
+
+ if (getResp.getResponse() == null) {
+ restReq.initial(0L);
+ }
+ else {
+ if (getResp.getResponse() instanceof Long && (Long)getResp.getResponse() <= Long.MAX_VALUE)
+ restReq.initial((Long)getResp.getResponse());
+ else
+ throw new GridRedisGenericException("An initial value must be numeric and in range!");
+ }
+
+ restReq.clientId(msg.clientId());
+ restReq.key(msg.key());
+ restReq.delta(1L);
+
+ if (msg.getMsgParts().size() > DELTA_POS) {
+ try {
+ restReq.delta(Long.valueOf(msg.getMsgParts().get(DELTA_POS)));
+ }
+ catch (NumberFormatException e) {
+ throw new GridRedisGenericException("An increment value must be numeric and in range!");
+ }
+ }
+
+ switch (msg.command()) {
+ case INCR:
+ case INCRBY:
+ restReq.command(ATOMIC_INCREMENT);
+ break;
+
+ case DECR:
+ case DECRBY:
+ restReq.command(ATOMIC_DECREMENT);
+ break;
+ }
+
+ return restReq;
+ }
+
+ /** {@inheritDoc} */
+ @Override public ByteBuffer makeResponse(final GridRestResponse restRes) {
+ if (restRes.getResponse() == null)
+ return GridRedisProtocolParser.toGenericError("Failed to increment!");
+
+ if (restRes.getResponse() instanceof Long && (Long)restRes.getResponse() <= Long.MAX_VALUE)
+ return GridRedisProtocolParser.toInteger(new BigDecimal((Long)restRes.getResponse()).toString());
+ else
+ return GridRedisProtocolParser.toTypeError("Value is non-numeric or out of range!");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMGetCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMGetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMGetCommandHandler.java
index 076a43e..4a73a48 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMGetCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMGetCommandHandler.java
@@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
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;
@@ -46,8 +47,8 @@ public class GridRedisMGetCommandHandler extends GridRedisStringCommandHandler {
);
/** {@inheritDoc} */
- public GridRedisMGetCommandHandler(GridRestProtocolHandler hnd) {
- super(hnd);
+ public GridRedisMGetCommandHandler(final GridKernalContext ctx, final GridRestProtocolHandler hnd) {
+ super(ctx, hnd);
}
/** {@inheritDoc} */
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMSetCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMSetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMSetCommandHandler.java
index b579471..409be4e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMSetCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMSetCommandHandler.java
@@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
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;
@@ -46,8 +47,8 @@ public class GridRedisMSetCommandHandler extends GridRedisStringCommandHandler {
);
/** {@inheritDoc} */
- public GridRedisMSetCommandHandler(GridRestProtocolHandler hnd) {
- super(hnd);
+ public GridRedisMSetCommandHandler(final GridKernalContext ctx, final GridRestProtocolHandler hnd) {
+ super(ctx, hnd);
}
/** {@inheritDoc} */
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3231a7d/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisSetCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisSetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisSetCommandHandler.java
index 4f39ee0..2737c4d 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisSetCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisSetCommandHandler.java
@@ -20,6 +20,7 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.s
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;
@@ -43,8 +44,8 @@ public class GridRedisSetCommandHandler extends GridRedisStringCommandHandler {
);
/** {@inheritDoc} */
- public GridRedisSetCommandHandler(GridRestProtocolHandler hnd) {
- super(hnd);
+ public GridRedisSetCommandHandler(final GridKernalContext ctx, final GridRestProtocolHandler hnd) {
+ super(ctx, hnd);
}
/** {@inheritDoc} */