Skip to content

Add binary stream support for XREAD and XREADGROUP (#3566) #4152

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

Merged
merged 17 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
282 changes: 188 additions & 94 deletions src/main/java/redis/clients/jedis/BuilderFactory.java

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -2892,6 +2892,30 @@ public final CommandObject<List<Object>> xread(XReadParams xReadParams, Map.Entr
return new CommandObject<>(args, BuilderFactory.RAW_OBJECT_LIST);
}

public final CommandObject<List<Map.Entry<byte[], List<StreamEntryBinary>>>> xreadBinary(
XReadParams xReadParams, Map.Entry<byte[], byte[]>... streams) {
CommandArguments args = commandArguments(XREAD).addParams(xReadParams).add(STREAMS);
for (Map.Entry<byte[], byte[]> entry : streams) {
args.key(entry.getKey());
}
for (Map.Entry<byte[], byte[]> entry : streams) {
args.add(entry.getValue());
}
return new CommandObject<>(args, BuilderFactory.STREAM_READ_BINARY_RESPONSE);
}

public final CommandObject<Map<byte[], List<StreamEntryBinary>>> xreadBinaryAsMap(
XReadParams xReadParams, Map.Entry<byte[], byte[]>... streams) {
CommandArguments args = commandArguments(XREAD).addParams(xReadParams).add(STREAMS);
for (Map.Entry<byte[], byte[]> entry : streams) {
args.key(entry.getKey());
}
for (Map.Entry<byte[], byte[]> entry : streams) {
args.add(entry.getValue());
}
return new CommandObject<>(args, BuilderFactory.STREAM_READ_BINARY_MAP_RESPONSE);
}

public final CommandObject<List<Object>> xreadGroup(byte[] groupName, byte[] consumer,
XReadGroupParams xReadGroupParams, Map.Entry<byte[], byte[]>... streams) {
CommandArguments args = commandArguments(XREADGROUP)
Expand All @@ -2905,6 +2929,36 @@ public final CommandObject<List<Object>> xreadGroup(byte[] groupName, byte[] con
}
return new CommandObject<>(args, BuilderFactory.RAW_OBJECT_LIST);
}

public final CommandObject<List<Map.Entry<byte[], List<StreamEntryBinary>>>> xreadGroupBinary(
byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams,
Map.Entry<byte[], byte[]>... streams) {
CommandArguments args = commandArguments(XREADGROUP)
.add(GROUP).add(groupName).add(consumer)
.addParams(xReadGroupParams).add(STREAMS);
for (Map.Entry<byte[], byte[]> entry : streams) {
args.key(entry.getKey());
}
for (Map.Entry<byte[], byte[]> entry : streams) {
args.add(entry.getValue());
}
return new CommandObject<>(args, BuilderFactory.STREAM_READ_BINARY_RESPONSE);
}

public final CommandObject<Map<byte[], List<StreamEntryBinary>>> xreadGroupBinaryAsMap(
byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams,
Map.Entry<byte[], byte[]>... streams) {
CommandArguments args = commandArguments(XREADGROUP)
.add(GROUP).add(groupName).add(consumer)
.addParams(xReadGroupParams).add(STREAMS);
for (Map.Entry<byte[], byte[]> entry : streams) {
args.key(entry.getKey());
}
for (Map.Entry<byte[], byte[]> entry : streams) {
args.add(entry.getValue());
}
return new CommandObject<>(args, BuilderFactory.STREAM_READ_BINARY_MAP_RESPONSE);
}
// Stream commands

// Scripting commands
Expand Down
34 changes: 31 additions & 3 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ public long hsetex(byte[] key, HSetExParams params, byte[] field, byte[] value)
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hsetex(key, params, field, value));
}

@Override
public long hsetex(byte[] key, HSetExParams params, Map<byte[], byte[]> hash){
checkIsInMultiOrPipeline();
Expand Down Expand Up @@ -1200,13 +1200,13 @@ public List<byte[]> hgetex(byte[] key, HGetExParams params, byte[]... fields){
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hgetex(key, params, fields));
}

@Override
public List<byte[]> hgetdel(byte[] key, byte[]... fields){
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hgetdel(key, fields));
}

/**
* Set the specified hash field to the specified value if the field not exists. <b>Time
* complexity:</b> O(1)
Expand Down Expand Up @@ -4783,6 +4783,34 @@ public List<Object> xreadGroup(byte[] groupName, byte[] consumer,
return connection.executeCommand(commandObjects.xreadGroup(groupName, consumer, xReadGroupParams, streams));
}

@Override
public List<Map.Entry<byte[], List<StreamEntryBinary>>> xreadBinary(XReadParams xReadParams,
Entry<byte[], byte[]>... streams) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.xreadBinary(xReadParams, streams));
}

@Override
public Map<byte[], List<StreamEntryBinary>> xreadBinaryAsMap(XReadParams xReadParams,
Entry<byte[], byte[]>... streams) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.xreadBinaryAsMap(xReadParams, streams));
}

@Override
public List<Map.Entry<byte[], List<StreamEntryBinary>>> xreadGroupBinary(byte[] groupName, byte[] consumer,
XReadGroupParams xReadGroupParams, Entry<byte[], byte[]>... streams) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.xreadGroupBinary(groupName, consumer, xReadGroupParams, streams));
}

@Override
public Map<byte[], List<StreamEntryBinary>> xreadGroupBinaryAsMap(byte[] groupName, byte[] consumer,
XReadGroupParams xReadGroupParams, Entry<byte[], byte[]>... streams) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.xreadGroupBinaryAsMap(groupName, consumer, xReadGroupParams, streams));
}

@Override
public byte[] xadd(final byte[] key, final XAddParams params, final Map<byte[], byte[]> hash) {
checkIsInMultiOrPipeline();
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ public byte[] hget(byte[] key, byte[] field) {
public List<byte[]> hgetex(byte[] key, HGetExParams params, byte[]... fields) {
return executeCommand(commandObjects.hgetex(key, params, fields));
}

@Override
public List<byte[]> hgetdel(byte[] key, byte[]... fields) {
return executeCommand(commandObjects.hgetdel(key, fields));
Expand Down Expand Up @@ -3460,6 +3460,30 @@ public List<Object> xread(XReadParams xReadParams, Map.Entry<byte[], byte[]>...
public List<Object> xreadGroup(byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams, Map.Entry<byte[], byte[]>... streams) {
return executeCommand(commandObjects.xreadGroup(groupName, consumer, xReadGroupParams, streams));
}

@Override
public List<Map.Entry<byte[], List<StreamEntryBinary>>> xreadBinary(XReadParams xReadParams,
Map.Entry<byte[], byte[]>... streams) {
return executeCommand(commandObjects.xreadBinary(xReadParams, streams));
}

@Override
public Map<byte[], List<StreamEntryBinary>> xreadBinaryAsMap(XReadParams xReadParams,
Map.Entry<byte[], byte[]>... streams) {
return executeCommand(commandObjects.xreadBinaryAsMap(xReadParams, streams));
}

@Override
public List<Map.Entry<byte[], List<StreamEntryBinary>>> xreadGroupBinary(byte[] groupName, byte[] consumer,
XReadGroupParams xReadGroupParams, Map.Entry<byte[], byte[]>... streams) {
return executeCommand(commandObjects.xreadGroupBinary(groupName, consumer, xReadGroupParams, streams));
}

@Override
public Map<byte[], List<StreamEntryBinary>> xreadGroupBinaryAsMap(byte[] groupName, byte[] consumer,
XReadGroupParams xReadGroupParams, Map.Entry<byte[], byte[]>... streams) {
return executeCommand(commandObjects.xreadGroupBinaryAsMap(groupName, consumer, xReadGroupParams, streams));
}
// Stream commands

// Scripting commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import redis.clients.jedis.params.*;
import redis.clients.jedis.resps.StreamEntryBinary;

public interface StreamBinaryCommands {

Expand Down Expand Up @@ -79,4 +80,15 @@ List<Object> xautoclaimJustId(byte[] key, byte[] groupName, byte[] consumerName,
List<Object> xreadGroup(byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams,
Map.Entry<byte[], byte[]>... streams);

List<Map.Entry<byte[], List<StreamEntryBinary>>> xreadBinary(XReadParams xReadParams,
Map.Entry<byte[], byte[]>... streams);

Map<byte[], List<StreamEntryBinary>> xreadBinaryAsMap(XReadParams xReadParams,
Map.Entry<byte[], byte[]>... streams);

List<Map.Entry<byte[], List<StreamEntryBinary>>> xreadGroupBinary(byte[] groupName, byte[] consumer,
XReadGroupParams xReadGroupParams, Map.Entry<byte[], byte[]>... streams);

Map<byte[], List<StreamEntryBinary>> xreadGroupBinaryAsMap(byte[] groupName, byte[] consumer,
XReadGroupParams xReadGroupParams, Map.Entry<byte[], byte[]>... streams);
}
42 changes: 42 additions & 0 deletions src/main/java/redis/clients/jedis/resps/StreamEntryBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package redis.clients.jedis.resps;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import redis.clients.jedis.StreamEntryID;

public class StreamEntryBinary implements Serializable {

private static final long serialVersionUID = 1L;

private StreamEntryID id;
private Map<byte[], byte[]> fields;

public StreamEntryBinary(StreamEntryID id, Map<byte[], byte[]> fields) {
this.id = id;
this.fields = fields;
}

public StreamEntryID getID() {
return id;
}

public Map<byte[], byte[]> getFields() {
return fields;
}

@Override
public String toString() {
return id + " " + fields;
}

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.writeUnshared(this.id);
out.writeUnshared(this.fields);
}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
this.id = (StreamEntryID) in.readUnshared();
this.fields = (Map<byte[], byte[]>) in.readUnshared();
}
}
Loading