Skip to content

Commit

Permalink
Support NOMKSTREAM option in xadd command
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming committed Nov 8, 2020
1 parent 4395040 commit 0e0d9c1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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");
}

}

0 comments on commit 0e0d9c1

Please sign in to comment.