DATAREDIS-993 - Fix StreamOperations MapRecord HashValue serialization.

We now use the appropriate serializer to serialize hash values.

Original pull request: #455.
This commit is contained in:
Romain Beghi
2019-06-13 16:37:40 -04:00
committed by Mark Paluch
parent 17e3488fb1
commit 548d8e7545
2 changed files with 33 additions and 2 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.util.Assert;
* @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>> {
@@ -125,7 +126,7 @@ public interface MapRecord<S, K, V> extends Record<S, Map<K, V>>, Iterable<Map.E
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()) //

View File

@@ -28,10 +28,14 @@ import org.springframework.data.redis.connection.stream.Record;
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 {
@@ -39,10 +43,22 @@ public class StreamRecordsUnitTests {
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() {
@@ -71,7 +87,7 @@ public class StreamRecordsUnitTests {
}
@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);
@@ -84,6 +100,20 @@ public class StreamRecordsUnitTests {
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() {