Repository: ignite Updated Branches: refs/heads/ignite-2788 26d4c631a -> b3e7a2a5f
IGNITE-2788: Basic Redis protocol implementation. Got rid of switch statements. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/b3e7a2a5 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/b3e7a2a5 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/b3e7a2a5 Branch: refs/heads/ignite-2788 Commit: b3e7a2a5faf1799f304c18f85e934e0e85e55fbd Parents: 26d4c63 Author: shtykh_roman <[email protected]> Authored: Fri Apr 15 20:39:07 2016 +0900 Committer: shtykh_roman <[email protected]> Committed: Fri Apr 15 20:39:07 2016 +0900 ---------------------------------------------------------------------- .../processors/redis/RedisProtocolSelfTest.java | 22 +++ .../protocols/tcp/redis/GridRedisCommand.java | 9 +- .../protocols/tcp/redis/GridRedisMessage.java | 9 ++ .../tcp/redis/GridRedisNioListener.java | 15 ++- .../handler/GridRedisStringCommandHandler.java | 134 +++---------------- .../string/GridRedisGetCommandHandler.java | 74 ++++++++++ .../string/GridRedisIncrCommandHandler.java | 104 ++++++++++++++ .../string/GridRedisMGetCommandHandler.java | 86 ++++++++++++ .../string/GridRedisMSetCommandHandler.java | 85 ++++++++++++ .../string/GridRedisSetCommandHandler.java | 83 ++++++++++++ 10 files changed, 495 insertions(+), 126 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 7ae817c..6fe62d5 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 @@ -30,6 +30,7 @@ import org.junit.Assert; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.exceptions.JedisDataException; /** * Tests for Redis protocol. @@ -202,4 +203,25 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest { Assert.assertEquals("2", jcache().get("setKey2")); } } + + /** + * @throws Exception If failed. + */ + public void testIncr() throws Exception { + try (Jedis jedis = pool.getResource()) { + Assert.assertEquals(1, (long)jedis.incr("newKey")); + + jcache().put("incrKey1", 1L); + Assert.assertEquals(2L, (long)jedis.incr("incrKey1")); + + jcache().put("nonInt", "abc"); + try { + jedis.incr("nonInt"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + } + } + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 432cdd0..b0f78bc 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 @@ -38,16 +38,15 @@ public enum GridRedisCommand { /** SET. */ SET("SET"), /** MSET. */ - MSET("MSET"); + MSET("MSET"), + /** INCR. */ + INCR("INCR"); /** String for command. */ private final String cmd; + /** Constructor. */ GridRedisCommand(String cmd) { this.cmd = cmd; } - - public String cmd() { - return cmd; - } } http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java index c50f3f1..20defb0 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java @@ -54,6 +54,15 @@ public class GridRedisMessage implements GridClientMessage { msgParts = new ArrayList<>(msgLen); } + public GridRedisMessage copy(GridRedisMessage msg) { + GridRedisMessage m = new GridRedisMessage(msgParts.size()); + + for (String p : msgParts) + m.append(p); + + return m; + } + public void append(String part) { msgParts.add(part); } http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 c615e16..0ea5138 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,7 +26,11 @@ 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.GridRedisStringCommandHandler; +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.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.util.nio.GridNioFuture; import org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter; import org.apache.ignite.internal.util.nio.GridNioSession; @@ -51,8 +55,15 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter<GridRedis public GridRedisNioListener(IgniteLogger log, GridRestProtocolHandler hnd, GridKernalContext ctx) { this.log = log; + // connection commands. addCommandHandler(new GridRedisConnectionCommandHandler()); - addCommandHandler(new GridRedisStringCommandHandler(hnd)); + + // string commands. + addCommandHandler(new GridRedisGetCommandHandler(hnd)); + addCommandHandler(new GridRedisSetCommandHandler(hnd)); + addCommandHandler(new GridRedisMSetCommandHandler(hnd)); + addCommandHandler(new GridRedisMGetCommandHandler(hnd)); + addCommandHandler(new GridRedisIncrCommandHandler(hnd)); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 24ba40b..00d0b86 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 @@ -18,46 +18,22 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler; import java.nio.ByteBuffer; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; import org.apache.ignite.IgniteCheckedException; 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.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.request.GridRestCacheRequest; 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; -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.GridRestCommand.CACHE_GET_ALL; -import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_PUT; -import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_PUT_ALL; -import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.GET; -import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.MGET; -import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.MSET; -import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.SET; /** * Redis strings command handler. */ -public class GridRedisStringCommandHandler implements GridRedisCommandHandler { - /** Supported commands. */ - private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList( - GET, - MGET, - SET, - MSET - ); - +public abstract class GridRedisStringCommandHandler implements GridRedisCommandHandler { /** REST protocol handler. */ - private GridRestProtocolHandler hnd; + protected GridRestProtocolHandler hnd; /** * Constructor. @@ -69,16 +45,11 @@ public class GridRedisStringCommandHandler implements GridRedisCommandHandler { } /** {@inheritDoc} */ - @Override public Collection<GridRedisCommand> supportedCommands() { - return SUPPORTED_COMMANDS; - } - - /** {@inheritDoc} */ @Override public IgniteInternalFuture<GridRedisMessage> handleAsync(GridRedisMessage msg) { assert msg != null; try { - return hnd.handleAsync(toRestRequest(msg)) + return hnd.handleAsync(asRestRequest(msg)) .chain(new CX1<IgniteInternalFuture<GridRestResponse>, GridRedisMessage>() { @Override public GridRedisMessage applyx(IgniteInternalFuture<GridRestResponse> f) @@ -86,34 +57,9 @@ public class GridRedisStringCommandHandler implements GridRedisCommandHandler { GridRestResponse restRes = f.get(); GridRedisMessage res = msg; - ByteBuffer resp; if (restRes.getSuccessStatus() == GridRestResponse.STATUS_SUCCESS) { - switch (res.command()) { - case GET: - resp = (restRes.getResponse() == null ? GridRedisProtocolParser.nil() - : GridRedisProtocolParser.toBulkString(restRes.getResponse())); - - break; - case MGET: - resp = (restRes.getResponse() == null ? GridRedisProtocolParser.nil() - : GridRedisProtocolParser.toArray((Map<Object, Object>)restRes.getResponse())); - - break; - case SET: - resp = (restRes.getResponse() == null ? GridRedisProtocolParser.nil() - : GridRedisProtocolParser.OkString()); - - break; - case MSET: - resp = GridRedisProtocolParser.OkString(); - - break; - - default: - resp = GridRedisProtocolParser.toGenericError("Unsupported operation!"); - } - res.setResponse(resp); + res.setResponse(makeResponse(restRes)); } else res.setResponse(GridRedisProtocolParser.toGenericError("Operation error!")); @@ -130,69 +76,19 @@ public class GridRedisStringCommandHandler implements GridRedisCommandHandler { } /** + * Converts {@link GridRedisMessage} to {@link GridRestRequest}. + * * @param msg {@link GridRedisMessage} * @return {@link GridRestRequest} + * @throws IgniteCheckedException If fails. */ - private GridRestRequest toRestRequest(GridRedisMessage msg) throws IgniteCheckedException { - assert msg != null; - - GridRestCacheRequest restReq = new GridRestCacheRequest(); - - restReq.clientId(msg.clientId()); - restReq.key(msg.key()); - - switch (msg.command()) { - case SET: - restReq.command(CACHE_PUT); - - if (msg.getMsgParts().size() < 3) - throw new IgniteCheckedException("Invalid request!"); - - restReq.value(msg.getMsgParts().get(2)); - - if (msg.getMsgParts().size() >= 4) { - // handle options. - } - - break; + public abstract GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException; - case MSET: - restReq.command(CACHE_PUT_ALL); - - List<String> els = msg.getMsgParts().subList(1, msg.getMsgParts().size()); - Map<Object, Object> mset = U.newHashMap(els.size() / 2); - Iterator<String> msetIt = els.iterator(); - - while (msetIt.hasNext()) - mset.put(msetIt.next(), msetIt.hasNext() ? msetIt.next() : null); - - restReq.values(mset); - - break; - - case MGET: - restReq.command(CACHE_GET_ALL); - - List<String> keys = msg.getMsgParts().subList(1, msg.getMsgParts().size()); - Map<Object, Object> mget = U.newHashMap(keys.size()); - Iterator<String> mgetIt = keys.iterator(); - - while (mgetIt.hasNext()) - mget.put(mgetIt.next(), null); - - restReq.values(mget); - - break; - - case GET: - restReq.command(CACHE_GET); - - break; - - default: - restReq.command(null); - } - - return restReq; - } + /** + * Prepares a response according to the request. + * + * @param resp REST response. + * @return + */ + public abstract ByteBuffer makeResponse(GridRestResponse resp); } http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 new file mode 100644 index 0000000..d799dc9 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisGetCommandHandler.java @@ -0,0 +1,74 @@ +/* + * 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.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.GET; + +/** + * Redis GET command handler. + */ +public class GridRedisGetCommandHandler extends GridRedisStringCommandHandler { + /** Supported commands. */ + private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList( + GET + ); + + /** {@inheritDoc} */ + public GridRedisGetCommandHandler(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; + + 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) { + return (restRes.getResponse() == null ? GridRedisProtocolParser.nil() + : GridRedisProtocolParser.toBulkString(restRes.getResponse())); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 new file mode 100644 index 0000000..d540da3 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisIncrCommandHandler.java @@ -0,0 +1,104 @@ +/* + * 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/b3e7a2a5/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 new file mode 100644 index 0000000..076a43e --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMGetCommandHandler.java @@ -0,0 +1,86 @@ +/* + * 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 java.util.Iterator; +import java.util.List; +import java.util.Map; +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.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_ALL; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.MGET; + +/** + * Redis MGET command handler. + */ +public class GridRedisMGetCommandHandler extends GridRedisStringCommandHandler { + /** Supported commands. */ + private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList( + MGET + ); + + /** {@inheritDoc} */ + public GridRedisMGetCommandHandler(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; + + GridRestCacheRequest restReq = new GridRestCacheRequest(); + + restReq.clientId(msg.clientId()); + restReq.key(msg.key()); + + restReq.command(CACHE_GET_ALL); + + List<String> keys = msg.getMsgParts().subList(1, msg.getMsgParts().size()); + Map<Object, Object> mget = U.newHashMap(keys.size()); + Iterator<String> mgetIt = keys.iterator(); + + while (mgetIt.hasNext()) + mget.put(mgetIt.next(), null); + + restReq.values(mget); + + return restReq; + } + + /** {@inheritDoc} */ + @Override public ByteBuffer makeResponse(final GridRestResponse restRes) { + return (restRes.getResponse() == null ? GridRedisProtocolParser.nil() + : GridRedisProtocolParser.toArray((Map<Object, Object>)restRes.getResponse())); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 new file mode 100644 index 0000000..b579471 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisMSetCommandHandler.java @@ -0,0 +1,85 @@ +/* + * 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 java.util.Iterator; +import java.util.List; +import java.util.Map; +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.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_PUT_ALL; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.MSET; + +/** + * Redis MSET command handler. + */ +public class GridRedisMSetCommandHandler extends GridRedisStringCommandHandler { + /** Supported commands. */ + private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList( + MSET + ); + + /** {@inheritDoc} */ + public GridRedisMSetCommandHandler(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; + + GridRestCacheRequest restReq = new GridRestCacheRequest(); + + restReq.clientId(msg.clientId()); + restReq.key(msg.key()); + + restReq.command(CACHE_PUT_ALL); + + List<String> els = msg.getMsgParts().subList(1, msg.getMsgParts().size()); + Map<Object, Object> mset = U.newHashMap(els.size() / 2); + Iterator<String> msetIt = els.iterator(); + + while (msetIt.hasNext()) + mset.put(msetIt.next(), msetIt.hasNext() ? msetIt.next() : null); + + restReq.values(mset); + + return restReq; + } + + /** {@inheritDoc} */ + @Override public ByteBuffer makeResponse(final GridRestResponse restRes) { + return GridRedisProtocolParser.OkString(); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/b3e7a2a5/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 new file mode 100644 index 0000000..4f39ee0 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/string/GridRedisSetCommandHandler.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.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_PUT; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.SET; + +/** + * Redis SET command handler. + */ +public class GridRedisSetCommandHandler extends GridRedisStringCommandHandler { + /** Supported commands. */ + private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList( + SET + ); + + /** {@inheritDoc} */ + public GridRedisSetCommandHandler(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; + + GridRestCacheRequest restReq = new GridRestCacheRequest(); + + restReq.clientId(msg.clientId()); + restReq.key(msg.key()); + + restReq.command(CACHE_PUT); + + if (msg.getMsgParts().size() < 3) + throw new IgniteCheckedException("Invalid request!"); + + restReq.value(msg.getMsgParts().get(2)); + + if (msg.getMsgParts().size() >= 4) { + // handle options. + } + + return restReq; + } + + /** {@inheritDoc} */ + @Override public ByteBuffer makeResponse(final GridRestResponse restRes) { + return (restRes.getResponse() == null ? GridRedisProtocolParser.nil() + : GridRedisProtocolParser.OkString()); + } +}
