This is an automated email from the ASF dual-hosted git repository. sk0x50 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push: new 832cf80 IGNITE-12986 Fixed the order of elements for mget method. 832cf80 is described below commit 832cf801301f79b7e904b004e33855b105387982 Author: Slava Koptilin <slava.kopti...@gmail.com> AuthorDate: Wed Jun 24 20:36:09 2020 +0300 IGNITE-12986 Fixed the order of elements for mget method. --- .../tcp/redis/RedisProtocolStringSelfTest.java | 75 ++++++++++++++++++++++ .../redis/string/GridRedisMGetCommandHandler.java | 2 +- .../tcp/redis/GridRedisProtocolParser.java | 40 ++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java index e010719..bb95129 100644 --- a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java +++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java @@ -17,7 +17,9 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import org.junit.Assert; @@ -101,6 +103,79 @@ public class RedisProtocolStringSelfTest extends RedisCommonAbstractTest { * @throws Exception If failed. */ @Test + public void testMGetDirectOrder() throws Exception { + testMGetOrder(true); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testMGetReverseOrder() throws Exception { + testMGetOrder(false); + } + + /** + * Tests mget operation. + * + * @param directOrder {@code true} if the order of inserting to a cache should be the same as the order using by mget. + */ + public void testMGetOrder(boolean directOrder) { + int keysCnt = 33; + + List<String> keys = new ArrayList<>(keysCnt); + List<String> values = new ArrayList<>(keysCnt); + + // Fill values. + for (int i = 0; i < keysCnt; ++i) { + keys.add("getKey" + i); + + values.add("getValue" + i); + } + + try (Jedis jedis = pool.getResource()) { + for (int i = 0; i < keysCnt; ++i) + jcache().put(keys.get(i), values.get(i)); + + if (!directOrder) { + Collections.reverse(keys); + + Collections.reverse(values); + } + + List<String> res = jedis.mget(keys.toArray(new String[keysCnt])); + + Assert.assertEquals("The response size is not expected.", keysCnt, res.size()); + + for (int i = 0; i < keysCnt; ++i) + Assert.assertEquals(values.get(i), res.get(i)); + } + } + + + /** + * @throws Exception If failed. + */ + @Test + public void testMGetDuplicates() throws Exception { + try (Jedis jedis = pool.getResource()) { + jcache().put("key-A", "value-A"); + jcache().put("key-B", "value-B"); + + List<String> res = jedis.mget("key-A", "key-B", "key-A"); + + Assert.assertEquals("The size of returned array must be equal to 3.", 3, res.size()); + + Assert.assertEquals("value-A", res.get(0)); + Assert.assertEquals("value-B", res.get(1)); + Assert.assertEquals("value-A", res.get(2)); + } + } + + /** + * @throws Exception If failed. + */ + @Test public void testSet() throws Exception { long EXPIRE_MS = 1000L; int EXPIRE_SEC = 1; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java index 82b507a..e8b37bb 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java @@ -92,6 +92,6 @@ public class GridRedisMGetCommandHandler extends GridRedisRestCommandHandler { /** {@inheritDoc} */ @Override public ByteBuffer makeResponse(final GridRestResponse restRes, List<String> params) { return (restRes.getResponse() == null ? GridRedisProtocolParser.nil() - : GridRedisProtocolParser.toArray((Map<Object, Object>)restRes.getResponse())); + : GridRedisProtocolParser.toOrderedArray((Map<Object, Object>)restRes.getResponse(), params)); } } 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 85fad39..8ce7428 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 @@ -18,7 +18,9 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; import org.apache.ignite.IgniteCheckedException; @@ -289,6 +291,44 @@ public class GridRedisProtocolParser { } /** + * Converts a resultant map response to an array, + * the order of elements in the resulting array is defined by the order of elements in the {@code origin} collection. + * + * @param vals Map. + * @param origin List that defines the order of the resulting array. + * @return Array response. + */ + public static ByteBuffer toOrderedArray(Map<Object, Object> vals, List<?> origin) { + assert vals != null : "The resulting map is null."; + assert origin != null : "The origin list is null."; + + int capacity = 0; + + ArrayList<ByteBuffer> res = new ArrayList<>(); + for (Object o : origin) { + Object val = vals.get(o); + + if (val != null) { + ByteBuffer b = toBulkString(val); + res.add(b); + capacity += b.limit(); + } + } + + byte[] arrSize = String.valueOf(res.size()).getBytes(); + + ByteBuffer buf = ByteBuffer.allocateDirect(capacity + arrSize.length + 1 + CRLF.length); + buf.put(ARRAY); + buf.put(arrSize); + buf.put(CRLF); + res.forEach(o -> buf.put(o)); + + buf.flip(); + + return buf; + } + + /** * Converts a resultant collection response to an array. * * @param vals Array elements.