Skip to content

Commit

Permalink
DATAREDIS-993 - Fix StreamOperations MapRecord HashValue serialization.
Browse files Browse the repository at this point in the history
We now use the appropriate serializer to serialize hash values.

Original pull request: #455.
  • Loading branch information
Romain Beghi authored and mp911de committed Jun 18, 2019
1 parent 17e3488 commit 548d8e7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @param <V> the value type of the backing map.
* @author Christoph Strobl
* @author Mark Paluch
* @author Romain Beghi
* @since 2.2
*/
public interface MapRecord<S, K, V> extends Record<S, Map<K, V>>, Iterable<Map.Entry<K, V>> {
Expand Down Expand Up @@ -125,7 +126,7 @@ default ByteRecord serialize(@Nullable RedisSerializer<? super S> streamSerializ

MapRecord<S, byte[], byte[]> binaryMap = mapEntries(
it -> Collections.singletonMap(StreamSerialization.serialize(fieldSerializer, it.getKey()),
StreamSerialization.serialize(fieldSerializer, it.getValue())).entrySet().iterator().next());
StreamSerialization.serialize(valueSerializer, it.getValue())).entrySet().iterator().next());

return StreamRecords.newRecord() //
.in(streamSerializer != null ? streamSerializer.serialize(getStream()) : (byte[]) getStream()) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,37 @@
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

/**
* @author Christoph Strobl
* @author Romain Beghi
*/
public class StreamRecordsUnitTests {

static final String STRING_STREAM_KEY = "stream-key";
static final RecordId RECORD_ID = RecordId.of("1-0");
static final String STRING_MAP_KEY = "string-key";
static final String STRING_VAL = "string-val";
static final DummyObject OBJECT_VAL = new DummyObject();

static final Jackson2JsonRedisSerializer<DummyObject> JSON_REDIS_SERIALIZER = new Jackson2JsonRedisSerializer<>(DummyObject.class);

static final byte[] SERIALIZED_STRING_VAL = RedisSerializer.string().serialize(STRING_VAL);
static final byte[] SERIALIZED_STRING_MAP_KEY = RedisSerializer.string().serialize(STRING_MAP_KEY);
static final byte[] SERIALIZED_STRING_STREAM_KEY = RedisSerializer.string().serialize(STRING_STREAM_KEY);
static final byte[] SERIALIZED_OBJECT_VAL = JSON_REDIS_SERIALIZER.serialize(OBJECT_VAL);

private static class DummyObject implements Serializable {
private final Integer dummyId = 1;

public Integer getDummyId() {
return this.dummyId;
}
}

@Test // DATAREDIS-864
public void objectRecordToMapRecordViaHashMapper() {
Expand Down Expand Up @@ -71,7 +87,7 @@ public void mapRecordToObjectRecordViaHashMapper() {
}

@Test // DATAREDIS-864
public void serializeMapRecord() {
public void serializeMapRecordStringAsHashValue() {

MapRecord<String, String, String> source = Record.of(Collections.singletonMap(STRING_MAP_KEY, STRING_VAL))
.withId(RECORD_ID).withStreamKey(STRING_STREAM_KEY);
Expand All @@ -84,6 +100,20 @@ public void serializeMapRecord() {
assertThat(target.getValue().values().iterator().next()).isEqualTo(SERIALIZED_STRING_VAL);
}

@Test // DATAREDIS-993
public void serializeMapRecordObjectAsHashValue() {

MapRecord<String, String, DummyObject> source = Record.of(Collections.singletonMap(STRING_MAP_KEY, OBJECT_VAL))
.withId(RECORD_ID).withStreamKey(STRING_STREAM_KEY);

ByteRecord target = source.serialize(RedisSerializer.string(), RedisSerializer.string(), JSON_REDIS_SERIALIZER);

assertThat(target.getId()).isEqualTo(RECORD_ID);
assertThat(target.getStream()).isEqualTo(SERIALIZED_STRING_STREAM_KEY);
assertThat(target.getValue().keySet().iterator().next()).isEqualTo(SERIALIZED_STRING_MAP_KEY);
assertThat(target.getValue().values().iterator().next()).isEqualTo(SERIALIZED_OBJECT_VAL);
}

@Test // DATAREDIS-864
public void deserializeByteMapRecord() {

Expand Down

0 comments on commit 548d8e7

Please sign in to comment.