jdeppe-pivotal commented on a change in pull request #6700: URL: https://github.com/apache/geode/pull/6700#discussion_r671319519
########## 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()); Review comment: I think this should also be using `containsExactly` since the order of the returned values should be lexicographically sorted. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java ########## @@ -307,6 +308,57 @@ long zcount(SortedSetRangeOptions rangeOptions) { return getRange(min, max, withScores, false); } + + List<byte[]> zrangebyscore(SortedSetRangeOptions rangeOptions, boolean withScores) { + List<byte[]> result = new ArrayList<>(); + AbstractOrderedSetEntry minEntry = + new DummyOrderedSetEntry(rangeOptions.getMinDouble(), rangeOptions.isMinExclusive(), true); + long minIndex = scoreSet.indexOf(minEntry); + if (minIndex >= scoreSet.size()) { + return Collections.emptyList(); + } + + AbstractOrderedSetEntry maxEntry = + new DummyOrderedSetEntry(rangeOptions.getMaxDouble(), rangeOptions.isMaxExclusive(), false); + long maxIndex = scoreSet.indexOf(maxEntry); + if (minIndex == maxIndex) { + return Collections.emptyList(); + } + + // Okay, if we make it this far there's a potential range of things to return. + int offset = 0; + int count = Integer.MAX_VALUE; + Iterator<AbstractOrderedSetEntry> entryIterator = + scoreSet.getIndexRange((int) minIndex, (int) maxIndex, false); + if (rangeOptions.hasLimit()) { + count = rangeOptions.getCount(); + offset = rangeOptions.getOffset(); + } + int skip = 0; + int returnedCount = 0; + + while (entryIterator.hasNext() && returnedCount < count) { + AbstractOrderedSetEntry entry = entryIterator.next(); + if (skip < offset) { + skip++; + continue; + } Review comment: You could avoid use of `skip` here and just count down on `offset`: ``` if (offset > 0) { offset--; continue; } ``` ########## 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 { Review comment: There are a lot of boundary cases tested, but please would you also add a test for exclusivity. -- 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]
