ringles commented on a change in pull request #6700: URL: https://github.com/apache/geode/pull/6700#discussion_r672342255
########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZRangeByScoreExecutor.java ########## @@ -0,0 +1,105 @@ +/* + * 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.geode.redis.internal.executor.sortedset; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_MIN_MAX_NOT_A_FLOAT; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX; +import static org.apache.geode.redis.internal.netty.Coder.bytesToLong; +import static org.apache.geode.redis.internal.netty.Coder.equalsIgnoreCaseBytes; +import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt; +import static org.apache.geode.redis.internal.netty.StringBytesGlossary.bRADISH_LIMIT; +import static org.apache.geode.redis.internal.netty.StringBytesGlossary.bRADISH_WITHSCORES; + +import java.util.List; + +import org.apache.geode.redis.internal.executor.AbstractExecutor; +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class ZRangeByScoreExecutor extends AbstractExecutor { + @Override + public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) { + RedisSortedSetCommands redisSortedSetCommands = context.getSortedSetCommands(); + + List<byte[]> commandElements = command.getProcessedCommand(); + + SortedSetRangeOptions rangeOptions; + boolean withScores = false; + + try { + byte[] minBytes = commandElements.get(2); + byte[] maxBytes = commandElements.get(3); + rangeOptions = new SortedSetRangeOptions(minBytes, maxBytes); + } catch (NumberFormatException ex) { + return RedisResponse.error(ERROR_MIN_MAX_NOT_A_FLOAT); + } + + // Native redis allows multiple "withscores" and "limit ? ?" clauses; the last "limit" + // clause overrides any previous ones + if (commandElements.size() >= 5) { + int currentCommandElement = 4; + + while (currentCommandElement < commandElements.size()) { + try { + if (equalsIgnoreCaseBytes(commandElements.get(currentCommandElement), + bRADISH_WITHSCORES)) { + withScores = true; + currentCommandElement++; + } else { + parseLimitArguments(rangeOptions, commandElements, currentCommandElement); + currentCommandElement += 3; + } + } catch (NumberFormatException nfex) { + return RedisResponse.error(ERROR_NOT_INTEGER); + } catch (IllegalArgumentException iex) { + return RedisResponse.error(ERROR_SYNTAX); + } + } + } + + // If the range is empty (min > max or min == max and both are exclusive), or + // limit specified but count is zero, return early + if ((rangeOptions.hasLimit() && (rangeOptions.getCount() == 0 || rangeOptions.getOffset() < 0)) + || + rangeOptions.getMinDouble() > rangeOptions.getMaxDouble() || + (rangeOptions.getMinDouble() == rangeOptions.getMaxDouble()) + && rangeOptions.isMinExclusive() && rangeOptions.isMaxExclusive()) { + return RedisResponse.emptyArray(); + } + + List<byte[]> result = + redisSortedSetCommands.zrangebyscore(command.getKey(), rangeOptions, withScores); + + return RedisResponse.array(result); + } + + void parseLimitArguments(SortedSetRangeOptions rangeOptions, List<byte[]> commandElements, + int commandIndex) { + int offset; + int count; + if (equalsIgnoreCaseBytes(commandElements.get(commandIndex), bRADISH_LIMIT)) { + offset = narrowLongToInt(bytesToLong(commandElements.get(commandIndex + 1))); + count = narrowLongToInt(bytesToLong(commandElements.get(commandIndex + 2))); Review comment: Tests and fixes added for these. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/netty/StringBytesGlossary.java ########## @@ -238,6 +238,12 @@ @MakeImmutable public static final byte[] bRADISH_DUMP_HEADER = stringToBytes("RADISH"); + @MakeImmutable + public static final byte[] bRADISH_WITHSCORES = stringToBytes("WITHSCORES"); Review comment: Good catch, fixed! ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/netty/StringBytesGlossary.java ########## @@ -238,6 +238,12 @@ @MakeImmutable public static final byte[] bRADISH_DUMP_HEADER = stringToBytes("RADISH"); + @MakeImmutable + public static final byte[] bRADISH_WITHSCORES = stringToBytes("WITHSCORES"); + + @MakeImmutable + public static final byte[] bRADISH_LIMIT = stringToBytes("LIMIT"); Review comment: Also corrected! ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/sortedset/AbstractZRangeByScoreIntegrationTest.java ########## @@ -0,0 +1,269 @@ +/* + * 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.geode.redis.internal.executor.sortedset; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_MIN_MAX_NOT_A_FLOAT; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.Tuple; + +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractZRangeByScoreIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void shouldError_givenWrongNumberOfArguments() { + assertAtLeastNArgs(jedis, Protocol.Command.ZRANGEBYSCORE, 3); + } + + @Test + public void shouldError_givenInvalidMinOrMax() { + assertThatThrownBy(() -> jedis.zrangeByScore("fakeKey", "notANumber", "1")) + .hasMessageContaining(ERROR_MIN_MAX_NOT_A_FLOAT); + assertThatThrownBy(() -> jedis.zrangeByScore("fakeKey", "1", "notANumber")) + .hasMessageContaining(ERROR_MIN_MAX_NOT_A_FLOAT); + assertThatThrownBy(() -> jedis.zrangeByScore("fakeKey", "notANumber", "notANumber")) + .hasMessageContaining(ERROR_MIN_MAX_NOT_A_FLOAT); + assertThatThrownBy(() -> jedis.zrangeByScore("fakeKey", "((", "1")) + .hasMessageContaining(ERROR_MIN_MAX_NOT_A_FLOAT); + assertThatThrownBy(() -> jedis.zrangeByScore("fakeKey", "1", "((")) + .hasMessageContaining(ERROR_MIN_MAX_NOT_A_FLOAT); + assertThatThrownBy(() -> jedis.zrangeByScore("fakeKey", "(a", "(b")) + .hasMessageContaining(ERROR_MIN_MAX_NOT_A_FLOAT); + } + + @Test + public void shouldReturnZero_givenNonExistentKey() { + assertThat(jedis.zrangeByScore("fakeKey", "-inf", "inf")).isEmpty(); + } + + @Test + public void shouldReturnZero_givenMinGreaterThanMax() { + jedis.zadd(KEY, 1, "member"); + + // Count +inf <= score <= -inf + assertThat(jedis.zrangeByScore(KEY, "+inf", "-inf")).isEmpty(); + } + + @Test + public void shouldReturnCount_givenRangeIncludingScore() { + jedis.zadd(KEY, 1, "member"); + + // Count -inf <= score <= +inf + assertThat(jedis.zrangeByScore(KEY, "-inf", "inf")) + .containsExactly("member"); + } + + @Test + public void shouldReturnEmptyArray_givenRangeExcludingScore() { + int score = 1; + jedis.zadd(KEY, score, "member"); + + // Count 2 <= score <= 3 + assertThat(jedis.zrangeByScore(KEY, score + 1, score + 2)).isEmpty(); + } + + @Test + public void shouldReturnRange_givenMinAndMaxEqualToScore() { + int score = 1; + jedis.zadd(KEY, score, "member"); + + // Count 1 <= score <= 1 + assertThat(jedis.zrangeByScore(KEY, score, score)) + .containsExactly("member"); + } + + @Test + public void shouldReturnRange_givenMultipleMembersWithDifferentScores() { + Map<String, Double> map = new HashMap<>(); + + map.put("member1", -10.0); + map.put("member2", 1.0); + map.put("member3", 10.0); + + jedis.zadd(KEY, map); + + // Count -5 <= score <= 15 + assertThat(jedis.zrangeByScore(KEY, "-5", "15")) + .containsExactlyElementsOf(Arrays.asList("member2", "member3")); + } + + @Test + public void shouldReturnRange_givenMultipleMembersWithTheSameScoreAndMinAndMaxEqualToScore() { + Map<String, Double> map = new HashMap<>(); + double score = 1; + map.put("member1", score); + map.put("member2", score); + map.put("member3", score); + + jedis.zadd(KEY, map); + + // Count 1 <= score <= 1 + assertThat(jedis.zrangeByScore(KEY, score, score)) + .containsExactlyInAnyOrderElementsOf(map.keySet()); + } + + @Test + public void shouldReturnRange_givenExclusiveMin() { + Map<String, Double> map = new HashMap<>(); + + map.put("member1", Double.NEGATIVE_INFINITY); + map.put("member2", 1.0); + map.put("member3", Double.POSITIVE_INFINITY); + + jedis.zadd(KEY, map); + + // Count -inf < score <= +inf + assertThat(jedis.zrangeByScore(KEY, "(-inf", "+inf")) + .containsExactlyElementsOf(Arrays.asList("member2", "member3")); Review comment: Got a few of them earlier, all done now! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
