Skip to content

Commit

Permalink
Update options to use builder
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jun 14, 2024
1 parent a5c39ff commit 57ab990
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,45 @@
import glide.api.commands.StreamBaseCommands;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;

/**
* Optional arguments for {@link StreamBaseCommands#xgroupCreate(String, String, String,
* StreamGroupOptions)}
*
* @see <a href="https://valkey.io/commands/xgroup-create/">valkey.io</a>
*/
@Builder
public final class StreamGroupOptions {

// Redis API String argument for makeStream
public static final String MAKE_STREAM_REDIS_API = "MKSTREAM";
public static final String ENTRIES_READ_REDIS_API = "ENTRIESREAD";

/** If the stream doesn't exist, creates a new stream with a length of <code>0</code>. */
boolean makeStream;
// Redis API String argument for entriesRead
public static final String ENTRIES_READ_REDIS_API = "ENTRIESREAD";

/**
* An arbitrary ID (that isn't the first ID, last ID, or the zero <code>"0-0"</code>. Use it to
* find out how many entries are between the arbitrary ID (excluding it) and the stream's last
* entry.
*
* @since Redis 7.0.0
* If <code>true</code> and the stream doesn't exist, creates a new stream with a length of <code>
* 0</code>.
*/
String entriesRead;
@Builder.Default private boolean mkStream = false;

/**
* Options for {@link StreamBaseCommands#xgroupCreate(String, String, String, StreamGroupOptions)}
*
* @param makeStream If the stream doesn't exist, creates a new stream with a length of <code>0
* </code>.
*/
public StreamGroupOptions(Boolean makeStream) {
this.makeStream = makeStream;
}
public static class StreamGroupOptionsBuilder {

/**
* Options for {@link StreamBaseCommands#xgroupCreate(String, String, String, StreamGroupOptions)}
*
* @param entriesRead An arbitrary ID that isn't the first ID, last ID, or the zero <code>"0-0"
* </code>. Use it to find out how many entries are between the arbitrary ID (excluding it)
* and the stream's last entry.
* @since ENTRIESREAD was added in Redis 7.0.0.
*/
public StreamGroupOptions(String entriesRead) {
this.makeStream = false;
this.entriesRead = entriesRead;
/** If the stream doesn't exist, this creates a new stream with a length of <code>0</code>. */
public StreamGroupOptionsBuilder makeStream() {
return mkStream(true);
}
}

/**
* Options for {@link StreamBaseCommands#xgroupCreate(String, String, String, StreamGroupOptions)}
* An arbitrary ID (that isn't the first ID, last ID, or the zero <code>"0-0"</code>. Use it to
* find out how many entries are between the arbitrary ID (excluding it) and the stream's last
* entry.
*
* @param makeStream If the stream doesn't exist, creates a new stream with a length of <code>0
* </code>.
* @param entriesRead An arbitrary ID that isn't the first ID, last ID, or the zero <code>"0-0"
* </code>. Use it to find out how many entries are between the arbitrary ID (excluding it)
* and the stream's last entry.
* @since ENTRIESREAD was added in Redis 7.0.0.
* @since Redis 7.0.0
*/
public StreamGroupOptions(Boolean makeStream, String entriesRead) {
this.makeStream = makeStream;
this.entriesRead = entriesRead;
}
private String entriesRead;

/**
* Converts options and the key-to-id input for {@link StreamBaseCommands#xgroupCreate(String,
Expand All @@ -75,7 +53,7 @@ public StreamGroupOptions(Boolean makeStream, String entriesRead) {
public String[] toArgs() {
List<String> optionArgs = new ArrayList<>();

if (this.makeStream) {
if (this.mkStream) {
optionArgs.add(MAKE_STREAM_REDIS_API);
}

Expand Down
3 changes: 2 additions & 1 deletion java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4308,7 +4308,8 @@ public void xgroupCreate_withOptions() {
String groupName = "testGroupName";
String id = "testId";
String testEntry = "testEntry";
StreamGroupOptions options = new StreamGroupOptions(true, testEntry);
StreamGroupOptions options =
StreamGroupOptions.builder().makeStream().entriesRead(testEntry).build();
String[] arguments =
new String[] {key, groupName, id, MAKE_STREAM_REDIS_API, ENTRIES_READ_REDIS_API, testEntry};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,11 @@ InfScoreBound.NEGATIVE_INFINITY, new ScoreBoundary(3, false), new Limit(1, 2)),
transaction.xgroupCreate("key", "group", "id");
results.add(Pair.of(XGroupCreate, buildArgs("key", "group", "id")));

transaction.xgroupCreate("key", "group", "id", new StreamGroupOptions(true, "entry"));
transaction.xgroupCreate(
"key",
"group",
"id",
StreamGroupOptions.builder().makeStream().entriesRead("entry").build());
results.add(
Pair.of(
XGroupCreate,
Expand Down
7 changes: 4 additions & 3 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3341,7 +3341,8 @@ public void xgroupCreate_xgroupDestroy(BaseClient client) {

// Stream with option to create creates stream & Group
assertEquals(
OK, client.xgroupCreate(key, groupName, streamId, new StreamGroupOptions(true)).get());
OK, client.xgroupCreate(key, groupName, streamId, StreamGroupOptions.builder().makeStream()
.build()).get());

// ...and again results in BUSYGROUP error, because group names must be unique
executionException =
Expand All @@ -3357,7 +3358,7 @@ public void xgroupCreate_xgroupDestroy(BaseClient client) {
assertEquals(false, client.xgroupDestroy(key, groupName).get());

// ENTRIESREAD option was added in redis 7.0.0
StreamGroupOptions entriesReadOption = new StreamGroupOptions("10");
StreamGroupOptions entriesReadOption = StreamGroupOptions.builder().entriesRead("10").build();
if (REDIS_VERSION.isGreaterThanOrEqualTo("7.0.0")) {
assertEquals(OK, client.xgroupCreate(key, groupName, streamId, entriesReadOption).get());
} else {
Expand All @@ -3375,7 +3376,7 @@ public void xgroupCreate_xgroupDestroy(BaseClient client) {
ExecutionException.class,
() ->
client
.xgroupCreate(stringKey, groupName, streamId, new StreamGroupOptions(true))
.xgroupCreate(stringKey, groupName, streamId, StreamGroupOptions.builder().makeStream().build())
.get());
assertInstanceOf(RequestException.class, executionException.getCause());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ private static Object[] streamCommands(BaseTransaction<?> transaction) {
.xrevrange(streamKey1, IdBound.of("0-1"), IdBound.of("0-1"), 1L)
.xtrim(streamKey1, new MinId(true, "0-2"))
.xgroupCreate(streamKey1, groupName1, "0-0")
.xgroupCreate(streamKey1, groupName2, "0-0", new StreamGroupOptions(true))
.xgroupCreate(streamKey1, groupName2, "0-0", StreamGroupOptions.builder().makeStream().build())
.xgroupDestroy(streamKey1, groupName1)
.xgroupDestroy(streamKey1, groupName2)
.xdel(streamKey1, new String[] {"0-3", "0-5"});
Expand Down

0 comments on commit 57ab990

Please sign in to comment.