Skip to content

Add NOMKSTREAM option to XADD command #2118

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* @author Christoph Strobl
* @author Tugdual Grall
* @author Dengliming
* @author Mark John Moreno
* @since 2.2
*/
public interface ReactiveStreamCommands {
Expand Down Expand Up @@ -199,12 +200,14 @@ class AddStreamRecord extends KeyCommand {

private final ByteBufferRecord record;
private final @Nullable Long maxlen;
private final boolean nomkstream;

private AddStreamRecord(ByteBufferRecord record, @Nullable Long maxlen) {
private AddStreamRecord(ByteBufferRecord record, @Nullable Long maxlen, boolean nomkstream) {

super(record.getStream());
this.record = record;
this.maxlen = maxlen;
this.nomkstream = nomkstream;
}

/**
Expand All @@ -217,7 +220,7 @@ public static AddStreamRecord of(ByteBufferRecord record) {

Assert.notNull(record, "Record must not be null!");

return new AddStreamRecord(record, null);
return new AddStreamRecord(record, null, false);
}

/**
Expand All @@ -230,7 +233,7 @@ public static AddStreamRecord body(Map<ByteBuffer, ByteBuffer> body) {

Assert.notNull(body, "Body must not be null!");

return new AddStreamRecord(StreamRecords.rawBuffer(body), null);
return new AddStreamRecord(StreamRecords.rawBuffer(body), null, false);
}

/**
Expand All @@ -240,7 +243,7 @@ public static AddStreamRecord body(Map<ByteBuffer, ByteBuffer> body) {
* @return a new {@link ReactiveGeoCommands.GeoAddCommand} with {@literal key} applied.
*/
public AddStreamRecord to(ByteBuffer key) {
return new AddStreamRecord(record.withStreamKey(key), maxlen);
return new AddStreamRecord(record.withStreamKey(key), maxlen, false);
}

/**
Expand All @@ -249,7 +252,28 @@ public AddStreamRecord to(ByteBuffer key) {
* @return new instance of {@link AddStreamRecord}.
*/
public AddStreamRecord maxlen(long maxlen) {
return new AddStreamRecord(record, maxlen);
return new AddStreamRecord(record, maxlen, false);
}

/**
* Disable creation of stream if it does not already exist.
*
* @return new instance of {@link AddStreamRecord}.
* @since 2.6
*/
public AddStreamRecord makeNoStream() {
return new AddStreamRecord(record, maxlen, true);
}

/**
* Disable creation of stream if it does not already exist.
*
* @param makeNoStream {@code true} to not create a stream if it does not already exist.
* @return new instance of {@link AddStreamRecord}.
* @since 2.6
*/
public AddStreamRecord makeNoStream(boolean makeNoStream) {
return new AddStreamRecord(record, maxlen, makeNoStream);
}

/**
Expand Down Expand Up @@ -281,6 +305,14 @@ public Long getMaxlen() {
public boolean hasMaxlen() {
return maxlen != null && maxlen > 0;
}

/**
* @return {@literal true} if {@literal NOMKSTREAM} is set.
* @since 2.6
*/
public boolean isNoMkStream() {
return nomkstream;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @author Christoph Strobl
* @author Tugdual Grall
* @author Dengliming
* @author Mark John Moreno
* @see <a href="https://redis.io/topics/streams-intro">Redis Documentation - Streams</a>
* @since 2.2
*/
Expand Down Expand Up @@ -116,16 +117,19 @@ default RecordId xAdd(MapRecord<byte[], byte[], byte[]> record) {
* Additional options applicable for {@literal XADD} command.
*
* @author Christoph Strobl
* @author Mark John Moreno
* @since 2.3
*/
class XAddOptions {

private static final XAddOptions NONE = new XAddOptions(null);
private static final XAddOptions NONE = new XAddOptions(null, false);

private final @Nullable Long maxlen;
private final boolean nomkstream;

private XAddOptions(@Nullable Long maxlen) {
private XAddOptions(@Nullable Long maxlen, boolean nomkstream) {
this.maxlen = maxlen;
this.nomkstream = nomkstream;
}

/**
Expand All @@ -141,7 +145,28 @@ public static XAddOptions none() {
* @return new instance of {@link XAddOptions}.
*/
public static XAddOptions maxlen(long maxlen) {
return new XAddOptions(maxlen);
return new XAddOptions(maxlen, false);
}

/**
* Disable creation of stream if it does not already exist.
*
* @return new instance of {@link XAddOptions}.
* @since 2.6
*/
public static XAddOptions makeNoStream() {
return new XAddOptions(null, true);
}

/**
* Disable creation of stream if it does not already exist.
*
* @param makeNoStream {@code true} to not create a stream if it does not already exist.
* @return new instance of {@link XAddOptions}.
* @since 2.6
*/
public static XAddOptions makeNoStream(boolean makeNoStream) {
return new XAddOptions(null, makeNoStream);
}

/**
Expand All @@ -161,6 +186,14 @@ public boolean hasMaxlen() {
return maxlen != null && maxlen > 0;
}

/**
* @return {@literal true} if {@literal NOMKSTREAM} is set.
* @since 2.6
*/
public boolean isNoMkStream() {
return nomkstream;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -169,14 +202,16 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}

XAddOptions that = (XAddOptions) o;
if (this.nomkstream != that.nomkstream) return false;
return ObjectUtils.nullSafeEquals(this.maxlen, that.maxlen);
}

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.maxlen);
int result = ObjectUtils.nullSafeHashCode(this.maxlen);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.nomkstream);
return result;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* @author Mark Paluch
* @author Tugdual Grall
* @author Dengliming
* @author Mark John Moreno
* @since 2.2
*/
class LettuceReactiveStreamCommands implements ReactiveStreamCommands {
Expand Down Expand Up @@ -110,6 +111,7 @@ public Flux<CommandResponse<AddStreamRecord, RecordId>> xAdd(Publisher<AddStream
if (command.hasMaxlen()) {
args.maxlen(command.getMaxlen());
}
args.nomkstream(command.isNoMkStream());

return cmd.xadd(command.getKey(), args, command.getBody())
.map(value -> new CommandResponse<>(command, RecordId.of(value)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @author Tugdual Grall
* @author Dejan Jankov
* @author Dengliming
* @author Mark John Moreno
* @since 2.2
*/
class LettuceStreamCommands implements RedisStreamCommands {
Expand Down Expand Up @@ -90,6 +91,7 @@ public RecordId xAdd(MapRecord<byte[], byte[], byte[]> record, XAddOptions optio
if (options.hasMaxlen()) {
args.maxlen(options.getMaxlen());
}
args.nomkstream(options.isNoMkStream());

return connection.invoke().from(RedisStreamAsyncCommands::xadd, record.getStream(), args, record.getValue())
.get(RecordId::of);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ void executeShouldPassThruCustomCommands() {

verify(asyncCommandsMock).dispatch(eq(command.getType()), eq(command.getOutput()), any(CommandArgs.class));
}

@Test // GH-2047
void xaddShouldHonorNoMkStream() {

MapRecord<byte[], byte[], byte[]> record = MapRecord.create("key".getBytes(), Collections.emptyMap());

connection.streamCommands().xAdd(record, XAddOptions.makeNoStream());
ArgumentCaptor<XAddArgs> args = ArgumentCaptor.forClass(XAddArgs.class);
if (connection.isPipelined()) {
verify(asyncCommandsMock, times(1)).xadd(any(), args.capture(), anyMap());
} else {
verify(syncCommandsMock, times(1)).xadd(any(), args.capture(), anyMap());
}

assertThat(args.getValue()).extracting("nomkstream").isEqualTo(true);
}

}

public static class LettucePipelineConnectionUnitTests extends BasicUnitTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,22 @@ void xaddShouldHonorMaxlen() {

assertThat(args.getValue()).extracting("maxlen").isEqualTo(100L);
}

@Test // GH-2047
void xaddShouldHonorNoMkStream() {

LettuceReactiveRedisConnection connection = new LettuceReactiveRedisConnection(connectionProvider);

ArgumentCaptor<XAddArgs> args = ArgumentCaptor.forClass(XAddArgs.class);
when(reactiveCommands.xadd(any(ByteBuffer.class), args.capture(), anyMap())).thenReturn(Mono.just("1-1"));

MapRecord<ByteBuffer, ByteBuffer, ByteBuffer> record = MapRecord.create(ByteBuffer.wrap("key".getBytes()),
Collections.emptyMap());

connection.streamCommands().xAdd(Mono.just(AddStreamRecord.of(ByteBufferRecord.of(record)).makeNoStream()))
.subscribe();

assertThat(args.getValue()).extracting("nomkstream").isEqualTo(true);
}

}