Skip to content

Commit

Permalink
Add exclusive ranges to X[REV]RANGE and XPENDING #1585 #1586
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Feb 3, 2021
1 parent fdea21a commit 638adff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2567,20 +2567,20 @@ public Command<K, V, Long> xtrim(K key, boolean approximateTrimming, long count)

private static String getLowerValue(Range<String> range) {

if (range.getLower().equals(Boundary.unbounded())) {
return "-";
}
Boundary<String> boundary = range.getLower();

return range.getLower().getValue();
return boundary.equals(Boundary.unbounded()) ? "-" : getRange(boundary);
}

private static String getUpperValue(Range<String> range) {

if (range.getUpper().equals(Boundary.unbounded())) {
return "+";
}
Boundary<String> boundary = range.getUpper();

return boundary.equals(Boundary.unbounded()) ? "+" : getRange(boundary);
}

return range.getUpper().getValue();
private static String getRange(Boundary<String> boundary) {
return !boundary.isIncluding() ? "(" + boundary.getValue() : boundary.getValue();
}

public Command<K, V, List<StreamMessage<K, V>>> xread(XReadArgs xReadArgs, StreamOffset<K>[] streams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ void xrange() {
assertThat(range.get(0).getBody()).isEqualTo(expectedBody);
}

@Test
@EnabledOnCommand("XAUTOCLAIM") // Redis 6.2
void xrangeRanges() {

String id1 = redis.xadd(key, Collections.singletonMap("key", "value"));
String id2 = redis.xadd(key, Collections.singletonMap("key", "value"));
String id3 = redis.xadd(key, Collections.singletonMap("key", "value"));

assertThat(redis.xrange(key, Range.unbounded())).hasSize(3);
assertThat(redis.xrange(key, Range.from(Range.Boundary.including(id1), Range.Boundary.excluding(id3)))).hasSize(2);
assertThat(redis.xrange(key, Range.from(Range.Boundary.excluding(id1), Range.Boundary.excluding(id3)))).hasSize(1);
assertThat(redis.xrange(key, Range.from(Range.Boundary.excluding(id1), Range.Boundary.including(id3)))).hasSize(2);
}

@Test
void xrevrange() {

Expand Down Expand Up @@ -410,6 +424,23 @@ void xpending() {
assertThat(message.getRedeliveryCount()).isEqualTo(1);
}

@Test
@EnabledOnCommand("XAUTOCLAIM") // Redis 6.2
void xpendingRanges() {

redis.xgroupCreate(StreamOffset.latest(key), "group", XGroupCreateArgs.Builder.mkstream());
String id1 = redis.xadd(key, Collections.singletonMap("key", "value"));
String id2 = redis.xadd(key, Collections.singletonMap("key", "value"));

redis.xreadgroup(Consumer.from("group", "consumer1"), StreamOffset.lastConsumed(key));

assertThat(redis.xpending(key, "group", Range.unbounded(), Limit.from(10))).hasSize(2);
assertThat(redis.xpending(key, "group", Range.from(Range.Boundary.including(id1), Range.Boundary.excluding(id2)),
Limit.from(10))).hasSize(1);
assertThat(redis.xpending(key, "group", Range.from(Range.Boundary.including(id1), Range.Boundary.including(id2)),
Limit.from(10))).hasSize(2);
}

@Test
void xpendingWithoutMessages() {

Expand Down

0 comments on commit 638adff

Please sign in to comment.