Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ZADD GT/LT options #1455

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
dengliming marked this conversation as resolved.
Show resolved Hide resolved
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")));
}
}