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

Support NOMKSTREAM option in xadd command #1504

Closed
wants to merge 1 commit 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
30 changes: 30 additions & 0 deletions src/main/java/io/lettuce/core/XAddArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* {@link XAddArgs} is a mutable object and instances should be used only once to avoid shared mutable state.
*
* @author Mark Paluch
* @author dengliming
* @since 5.1
*/
public class XAddArgs {
Expand All @@ -36,6 +37,8 @@ public class XAddArgs {

private boolean approximateTrimming;

private boolean noMkStream;

/**
* Builder entry points for {@link XAddArgs}.
*/
Expand Down Expand Up @@ -108,6 +111,29 @@ public XAddArgs approximateTrimming(boolean approximateTrimming) {
return this;
}

/**
* Not to create new stream by default.
*
* @return {@code this}
* @since 6.1
*/
public XAddArgs noMkStream() {
return noMkStream(true);
}

/**
* Not to create new stream by default.
*
* @param noMkStream {@code true} to apply not to create new stream by default.
* @return {@code this}
* @since 6.1
*/
public XAddArgs noMkStream(boolean noMkStream) {

this.noMkStream = noMkStream;
return this;
}

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

if (maxlen != null) {
Expand All @@ -121,6 +147,10 @@ public <K, V> void build(CommandArgs<K, V> args) {
args.add(maxlen);
}

if (noMkStream) {
args.add(CommandKeyword.NOMKSTREAM);
}

if (id != null) {
args.add(id);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/lettuce/core/protocol/CommandKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @author Will Glozer
* @author Mark Paluch
* @author Zhang Jessey
* @author dengliming
*/
public enum CommandKeyword implements ProtocolKeyword {

Expand All @@ -34,7 +35,7 @@ public enum CommandKeyword implements ProtocolKeyword {

IDLETIME, JUSTID, KILL, KEYSLOT, LEFT, LEN, LIMIT, LIST, LOAD, MATCH,

MAX, MAXLEN, MEET, MIN, MOVED, NO, NOACK, NODE, NODES, NOSAVE, NOT, NUMSUB, NUMPAT, OFF, ON, ONE, OR, PAUSE,
MAX, MAXLEN, MEET, MIN, MOVED, NO, NOACK, NODE, NODES, NOMKSTREAM, NOSAVE, NOT, NUMSUB, NUMPAT, OFF, ON, ONE, OR, PAUSE,

REFCOUNT, REMOVE, RELOAD, REPLACE, REPLICATE, RESET,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* Integration tests for {@link io.lettuce.core.api.sync.RedisStreamCommands}.
*
* @author Mark Paluch
* @author dengliming
*/
@ExtendWith(LettuceExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -91,6 +92,23 @@ void xaddMaxLenEfficientTrimming() {
assertThat(id).isNotNull();
}

@Test
void xaddMaxLenEfficientTrimmingNoMkStream() {

Map<String, String> body = Collections.singletonMap("foo", "bar");
String id = redis.xadd(key, XAddArgs.Builder.maxlen(5).approximateTrimming().noMkStream(), body);
assertThat(id).isNull();
assertThat(redis.exists(key)).isEqualTo(0L);

id = redis.xadd(key, XAddArgs.Builder.maxlen(5).approximateTrimming(), body);
assertThat(id).isNotNull();
assertThat(redis.exists(key)).isEqualTo(1L);

List<StreamMessage<String, String>> messages = redis.xrange(key, Range.unbounded());
assertThat(messages.size()).isEqualTo(1);
assertThat(messages.get(0).getBody()).isEqualTo(body);
}

@Test
void xdel() {

Expand Down Expand Up @@ -523,4 +541,5 @@ void xgroupSetid() {

assertThat(redis.xgroupSetid(StreamOffset.latest(key), "group")).isEqualTo("OK");
}

}