Skip to content

Commit

Permalink
Add support for ZADD GT/LT options #1451
Browse files Browse the repository at this point in the history
Original pull request: #1455.
  • Loading branch information
dengliming authored and mp911de committed Oct 26, 2020
1 parent d032662 commit 5810f96
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/java/io/lettuce/core/ZAddArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* {@link ZAddArgs} is a mutable object and instances should be used only once to avoid shared mutable state.
*
* @author Mark Paluch
* @author dengliming
*/
public class ZAddArgs implements CompositeArgument {

Expand All @@ -33,6 +34,10 @@ public class ZAddArgs implements CompositeArgument {

private boolean ch = false;

private boolean lt = false;

private boolean gt = false;

/**
* Builder entry points for {@link ScanArgs}.
*/
Expand Down Expand Up @@ -74,6 +79,27 @@ public static ZAddArgs ch() {
return new ZAddArgs().ch();
}

/**
* Creates new {@link ZAddArgs} and enabling {@literal GT}.
*
* @return new {@link ZAddArgs} with {@literal GT} enabled.
* @see ZAddArgs#gt()
* @since 6.1
*/
public static ZAddArgs gt() {
return new ZAddArgs().gt();
}

/**
* Creates new {@link ZAddArgs} and enabling {@literal LT}.
*
* @return new {@link ZAddArgs} with {@literal LT} enabled.
* @see ZAddArgs#lt()
* @since 6.1
*/
public static ZAddArgs lt() {
return new ZAddArgs().lt();
}
}

/**
Expand Down Expand Up @@ -109,6 +135,32 @@ public ZAddArgs ch() {
return this;
}

/**
* Only update existing elements if the new score is greater than the current score. This flag doesn't prevent adding new elements.
*
* @return {@code this} {@link ZAddArgs}.
* @since 6.1
*/
public ZAddArgs gt() {

this.gt = true;
this.lt = false;
return this;
}

/**
* Only update existing elements if the new score is less than the current score. This flag doesn't prevent adding new elements.
*
* @return {@code this} {@link ZAddArgs}.
* @since 6.1
*/
public ZAddArgs lt() {

this.lt = true;
this.gt = false;
return this;
}

public <K, V> void build(CommandArgs<K, V> args) {

if (nx) {
Expand All @@ -119,6 +171,13 @@ public <K, V> void build(CommandArgs<K, V> args) {
args.add("XX");
}

if (gt) {
args.add("GT");
}
if (lt) {
args.add("LT");
}

if (ch) {
args.add("CH");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,36 @@ void setup100KeyValues(Set<String> expect) {
expect.add(value + i);
}
}

@Test
void zaddgt() {
assertThat(redis.zadd(key, 1.0, "a")).isEqualTo(1);
// new score less than the current score
assertThat(redis.zadd(key, ZAddArgs.Builder.gt(), 0.0, "a")).isEqualTo(0);
assertThat(redis.zrangeWithScores(key, 0, -1)).isEqualTo(svlist(sv(1.0, "a")));

// new score greater than the current score
assertThat(redis.zadd(key, ZAddArgs.Builder.gt(), 2.0, "a")).isEqualTo(0);
assertThat(redis.zrangeWithScores(key, 0, -1)).isEqualTo(svlist(sv(2.0, "a")));

// add new element
assertThat(redis.zadd(key, ZAddArgs.Builder.gt(), 0.0, "b")).isEqualTo(1);
assertThat(redis.zrangeWithScores(key, 0, -1)).isEqualTo(svlist(sv(0.0, "b"), sv(2.0, "a")));
}

@Test
void zaddlt() {
assertThat(redis.zadd(key, 2.0, "a")).isEqualTo(1);
// new score greater than the current score
assertThat(redis.zadd(key, ZAddArgs.Builder.lt(), 3.0, "a")).isEqualTo(0);
assertThat(redis.zrangeWithScores(key, 0, -1)).isEqualTo(svlist(sv(2.0, "a")));

// new score less than the current score
assertThat(redis.zadd(key, ZAddArgs.Builder.lt(), 1.0, "a")).isEqualTo(0);
assertThat(redis.zrangeWithScores(key, 0, -1)).isEqualTo(svlist(sv(1.0, "a")));

// add new element
assertThat(redis.zadd(key, ZAddArgs.Builder.lt(), 0.0, "b")).isEqualTo(1);
assertThat(redis.zrangeWithScores(key, 0, -1)).isEqualTo(svlist(sv(0.0, "b"), sv(1.0, "a")));
}
}

0 comments on commit 5810f96

Please sign in to comment.