forked from fppt/jedis-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 8 additions & 51 deletions
59
src/main/java/com/github/fppt/jedismock/operations/sortedsets/ZRevRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,29 @@ | ||
package com.github.fppt.jedismock.operations.sortedsets; | ||
|
||
import com.github.fppt.jedismock.datastructures.RMHMap; | ||
import com.github.fppt.jedismock.datastructures.Slice; | ||
import com.github.fppt.jedismock.operations.AbstractRedisOperation; | ||
import com.github.fppt.jedismock.operations.RedisCommand; | ||
import com.github.fppt.jedismock.server.Response; | ||
import com.github.fppt.jedismock.storage.RedisBase; | ||
|
||
import java.util.Comparator; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static com.github.fppt.jedismock.Utils.convertToInteger; | ||
|
||
@RedisCommand("zrevrange") | ||
class ZRevRange extends AbstractRedisOperation { | ||
|
||
private static final String WITH_SCORES = "WITHSCORES"; | ||
private static final Comparator<Map.Entry<Slice, Double>> zRevRangeComparator = (o1, o2) -> { | ||
int byScoreComparison = o2.getValue().compareTo(o1.getValue()); | ||
return byScoreComparison != 0 ? byScoreComparison : o2.getKey().compareTo(o1.getKey()); | ||
}; | ||
private static final String IS_REV = "REV"; | ||
private final ZRange zRange; | ||
|
||
|
||
ZRevRange(RedisBase base, List<Slice> params) { | ||
super(base, params); | ||
List<Slice> updatedParams = new ArrayList<>(params); | ||
updatedParams.add(Slice.create(IS_REV)); | ||
this.zRange = new ZRange(base, updatedParams); | ||
} | ||
|
||
@Override | ||
protected Slice response() { | ||
Slice key = params().get(0); | ||
final RMHMap mapDBObj = getHMapFromBaseOrCreateEmpty(key); | ||
final Map<Slice, Double> map = mapDBObj.getStoredData(); | ||
|
||
int start = convertToInteger(params().get(1).toString()); | ||
int end = convertToInteger(params().get(2).toString()); | ||
|
||
if (start < 0) { | ||
start = map.size() + start; | ||
if (start < 0) { | ||
start = 0; | ||
} | ||
} | ||
|
||
if (end < 0) { | ||
end = map.size() + end; | ||
if (end < 0) { | ||
end = -1; | ||
} | ||
} | ||
|
||
if (end >= map.size()) { | ||
end = map.size() - 1; | ||
} | ||
|
||
final boolean withScores = params().size() == 4 && WITH_SCORES.equalsIgnoreCase(params().get(3).toString()); | ||
|
||
List<Slice> values = map.entrySet().stream() | ||
.sorted(zRevRangeComparator) | ||
.skip(start) | ||
.limit(end - start + 1) | ||
.flatMap(e -> withScores | ||
? Stream.of(e.getKey(), Slice.create(e.getValue().toString())) | ||
: Stream.of(e.getKey())) | ||
.map(Response::bulkString) | ||
.collect(Collectors.toList()); | ||
|
||
return Response.array(values); | ||
return zRange.response(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/com/github/fppt/jedismock/comparisontests/sortedsets/TestZRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.github.fppt.jedismock.comparisontests.sortedsets; | ||
|
||
import com.github.fppt.jedismock.comparisontests.ComparisonBase; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import redis.clients.jedis.Jedis; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@ExtendWith(ComparisonBase.class) | ||
public class TestZRange { | ||
|
||
private static final String ZSET_KEY = "myzset"; | ||
|
||
@BeforeEach | ||
public void setUp(Jedis jedis) { | ||
jedis.flushDB(); | ||
jedis.zadd(ZSET_KEY, 2, "aaaa"); | ||
jedis.zadd(ZSET_KEY, 3, "bbbb"); | ||
jedis.zadd(ZSET_KEY, 1, "cccc"); | ||
jedis.zadd(ZSET_KEY, 3, "bcbb"); | ||
jedis.zadd(ZSET_KEY, 3, "babb"); | ||
assertEquals(5L, jedis.zcount(ZSET_KEY, Integer.MIN_VALUE, Integer.MAX_VALUE)); | ||
} | ||
|
||
@TestTemplate | ||
public void whenUsingZrange_EnsureItReturnsEverythingInRightOrderWithPlusMinusMaxInteger(Jedis jedis) { | ||
assertEquals(Arrays.asList("cccc", "aaaa", "babb", "bbbb", "bcbb"), new ArrayList<>(jedis.zrange(ZSET_KEY, Integer.MIN_VALUE, Integer.MAX_VALUE))); | ||
} | ||
|
||
@TestTemplate | ||
public void whenUsingZrange_EnsureItReturnsListInRightOrderWithPositiveRange(Jedis jedis) { | ||
assertEquals(Arrays.asList("aaaa", "babb", "bbbb"), new ArrayList<>(jedis.zrange(ZSET_KEY, 1, 3))); | ||
} | ||
|
||
@TestTemplate | ||
public void whenUsingZrange_EnsureItReturnsListInRightOrderWithNegativeRange(Jedis jedis) { | ||
assertEquals(Arrays.asList("babb", "bbbb", "bcbb"), new ArrayList<>(jedis.zrange(ZSET_KEY, -3, -1))); | ||
} | ||
|
||
@TestTemplate | ||
public void whenUsingZrange_EnsureItReturnsListInRightOrderWithNegativeStartAndPositiveEndRange(Jedis jedis) { | ||
assertEquals(Arrays.asList("cccc", "aaaa", "babb"), new ArrayList<>(jedis.zrange(ZSET_KEY, -5, 2))); | ||
} | ||
|
||
@TestTemplate | ||
public void whenUsingZrange_EnsureItReturnsListInRightOrderWithPositiveStartAndNegativeEndRange(Jedis jedis) { | ||
assertEquals(Arrays.asList("aaaa", "babb", "bbbb", "bcbb"), new ArrayList<>(jedis.zrange(ZSET_KEY, 1, -1))); | ||
} | ||
} |