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

StreamEntry support Binary #3803

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,38 @@ public String toString() {
}
};

public static final Builder<List<StreamEntryBinary>> STREAM_ENTRY_BINARY = new Builder<List<StreamEntryBinary>>() {
@Override
@SuppressWarnings("unchecked")
public List<StreamEntryBinary> build(Object data) {
List<StreamEntryBinary> result = new ArrayList<>();
if (data == null) {
return result;
}
List<Object> objectList = (List<Object>) data;

for (Object obj : objectList) {
List<Object> entry = (List<Object>) obj;
String entryIdString = SafeEncoder.encode((byte[]) entry.get(0));
StreamEntryID entryID = new StreamEntryID(entryIdString);
List<byte[]> hash = (List<byte[]>) entry.get(1);

Iterator<byte[]> hashIterator = hash.iterator();
Map<byte[], byte[]> map = new HashMap<>(hash.size() / 2, 1f);
while (hashIterator.hasNext()) {
map.put(hashIterator.next(), hashIterator.next());
}
result.add(new StreamEntryBinary(entryID, map));
}
return result;
}

@Override
public String toString() {
return "StreamEntryBinary";
}
};

public static final Builder<List<StreamEntry>> STREAM_ENTRY_LIST = new Builder<List<StreamEntry>>() {
@Override
@SuppressWarnings("unchecked")
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/redis/clients/jedis/JedisBinaryStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package redis.clients.jedis;

import redis.clients.jedis.resps.StreamEntryBinary;

import java.util.List;

public class JedisBinaryStream extends Jedis {
public JedisBinaryStream(String host) {
super(host);
}

public List<StreamEntryBinary> xrangeBinary(byte[] key, byte[] start, byte[] end, int count) {
List<Object> rawResponse = super.xrange(key, start, end, count);
return BuilderFactory.STREAM_ENTRY_BINARY.build(rawResponse);
}

public List<StreamEntryBinary> xrangeBinary(byte[] key, byte[] start, byte[] end) {
List<Object> rawResponse = super.xrange(key, start, end);
return BuilderFactory.STREAM_ENTRY_BINARY.build(rawResponse);
}
}
43 changes: 43 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,43 @@
package redis.clients.jedis.resps;

import redis.clients.jedis.StreamEntryID;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

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();
}
}
12 changes: 12 additions & 0 deletions src/test/java/redis/clients/jedis/JedisBinaryStreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package redis.clients.jedis;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import redis.clients.jedis.commands.jedis.JedisCommandsTestBase;

@RunWith(Parameterized.class)
public class JedisBinaryStreamTest extends JedisCommandsTestBase {
public JedisBinaryStreamTest(RedisProtocol protocol) {
super(protocol);
}
}
Loading