Skip to content

Commit f6eaa40

Browse files
mp911dechristophstrobl
authored andcommitted
Polishing.
StreamInfo subtypes now implement Streamable. Switch from stream mapping to for/each style to avoid Java Stream creation. Introduce getRequired(…) methods to improve nullability experience. Original Pull Request: #2276
1 parent 5c4c9e0 commit f6eaa40

File tree

12 files changed

+121
-68
lines changed

12 files changed

+121
-68
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/StreamConverters.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.ArrayList;
2727
import java.util.List;
2828
import java.util.function.BiFunction;
29-
import java.util.stream.Collectors;
3029

3130
import org.springframework.core.convert.converter.Converter;
3231
import org.springframework.data.redis.connection.RedisStreamCommands.XClaimOptions;
@@ -38,6 +37,7 @@
3837
import org.springframework.data.redis.connection.stream.StreamReadOptions;
3938
import org.springframework.data.redis.connection.stream.StreamRecords;
4039
import org.springframework.data.redis.util.ByteUtils;
40+
import org.springframework.lang.Nullable;
4141
import org.springframework.util.NumberUtils;
4242

4343
/**
@@ -69,7 +69,7 @@ class StreamConverters {
6969
return new org.springframework.data.redis.connection.stream.PendingMessage(id, consumer,
7070
Duration.ofMillis(it.getMsSinceLastDelivery()), it.getRedeliveryCount());
7171

72-
}).collect(Collectors.toList());
72+
}).toList();
7373

7474
return new org.springframework.data.redis.connection.stream.PendingMessages(groupName, messages);
7575

@@ -111,18 +111,10 @@ static Converter<StreamMessage<byte[], byte[]>, ByteRecord> byteRecordConverter(
111111
return (it) -> StreamRecords.newRecord().in(it.getStream()).withId(it.getId()).ofBytes(it.getBody());
112112
}
113113

114-
static Converter<List<StreamMessage<byte[], byte[]>>, List<ByteRecord>> byteRecordListConverter() {
115-
return new ListConverter<>(byteRecordConverter());
116-
}
117-
118114
static Converter<StreamMessage<byte[], byte[]>, RecordId> messageToIdConverter() {
119115
return (it) -> RecordId.of(it.getId());
120116
}
121117

122-
static Converter<List<StreamMessage<byte[], byte[]>>, List<RecordId>> messagesToIds() {
123-
return MESSAGEs_TO_IDs;
124-
}
125-
126118
/**
127119
* Convert the raw Lettuce xpending result to {@link PendingMessages}.
128120
*
@@ -157,7 +149,7 @@ static PendingMessagesSummary toPendingMessagesInfo(String groupName, PendingMes
157149
* @param value dont't get me started om this.
158150
* @return preconverted values that Lettuce parsers are able to understand \ö/.
159151
*/
160-
private static Object preConvertNativeValues(Object value) {
152+
private static Object preConvertNativeValues(@Nullable Object value) {
161153

162154
if (value instanceof ByteBuffer || value instanceof byte[]) {
163155

src/main/java/org/springframework/data/redis/connection/stream/ByteBufferRecord.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import java.nio.ByteBuffer;
1919
import java.util.Collections;
20+
import java.util.LinkedHashMap;
2021
import java.util.Map;
21-
import java.util.stream.Collectors;
2222

2323
import org.springframework.data.redis.hash.HashMapper;
2424
import org.springframework.data.redis.serializer.RedisSerializer;
@@ -65,16 +65,17 @@ default <T> MapRecord<T, T, T> deserialize(@Nullable RedisSerializer<T> serializ
6565
* @param valueSerializer can be {@literal null} if the values suite already the target format.
6666
* @return new {@link MapRecord} holding the deserialized values.
6767
*/
68-
@SuppressWarnings({ "unchecked", "rawtypes" })
6968
default <K, HK, HV> MapRecord<K, HK, HV> deserialize(@Nullable RedisSerializer<? extends K> streamSerializer,
7069
@Nullable RedisSerializer<? extends HK> fieldSerializer,
7170
@Nullable RedisSerializer<? extends HV> valueSerializer) {
7271

73-
return mapEntries(it -> Collections.<HK, HV> singletonMap(
74-
fieldSerializer != null ? fieldSerializer.deserialize(ByteUtils.getBytes(it.getKey())) : (HK) it.getKey(),
75-
valueSerializer != null ? valueSerializer.deserialize(ByteUtils.getBytes(it.getValue())) : (HV) it.getValue())
76-
.entrySet().iterator().next()).withStreamKey(
77-
streamSerializer != null ? streamSerializer.deserialize(ByteUtils.getBytes(getStream())) : (K) getStream());
72+
return mapEntries(it -> {
73+
74+
Map<HK, HV> map = Collections.singletonMap(StreamSerialization.deserialize(fieldSerializer, it.getKey()),
75+
StreamSerialization.deserialize(valueSerializer, it.getValue()));
76+
77+
return map.entrySet().iterator().next();
78+
}).withStreamKey(StreamSerialization.deserialize(streamSerializer, getRequiredStream()));
7879
}
7980

8081
/**
@@ -84,7 +85,7 @@ default <K, HK, HV> MapRecord<K, HK, HV> deserialize(@Nullable RedisSerializer<?
8485
* @return new instance of {@link ByteRecord}.
8586
*/
8687
static ByteBufferRecord of(MapRecord<ByteBuffer, ByteBuffer, ByteBuffer> source) {
87-
return StreamRecords.newRecord().in(source.getStream()).withId(source.getId()).ofBuffer(source.getValue());
88+
return StreamRecords.newRecord().in(source.getRequiredStream()).withId(source.getId()).ofBuffer(source.getValue());
8889
}
8990

9091
/**
@@ -97,10 +98,13 @@ static ByteBufferRecord of(MapRecord<ByteBuffer, ByteBuffer, ByteBuffer> source)
9798
default <OV> ObjectRecord<ByteBuffer, OV> toObjectRecord(
9899
HashMapper<? super OV, ? super ByteBuffer, ? super ByteBuffer> mapper) {
99100

100-
Map<byte[], byte[]> targetMap = getValue().entrySet().stream().collect(
101-
Collectors.toMap(entry -> ByteUtils.getBytes(entry.getKey()), entry -> ByteUtils.getBytes(entry.getValue())));
101+
Map<ByteBuffer, ByteBuffer> value = getValue();
102+
Map<byte[], byte[]> targetMap = new LinkedHashMap<>(value.size());
103+
104+
value.forEach((k, v) -> targetMap.put(ByteUtils.getBytes(k), ByteUtils.getBytes(v)));
102105

103106
return Record.<ByteBuffer, OV> of((OV) (mapper).fromHash((Map) targetMap)).withId(getId())
104-
.withStreamKey(getStream());
107+
.withStreamKey(getRequiredStream());
105108
}
109+
106110
}

src/main/java/org/springframework/data/redis/connection/stream/ByteRecord.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package org.springframework.data.redis.connection.stream;
1717

1818
import java.util.Collections;
19+
import java.util.Map;
1920

20-
import org.springframework.data.redis.connection.RedisStreamCommands;
2121
import org.springframework.data.redis.serializer.RedisSerializer;
2222
import org.springframework.lang.Nullable;
2323

@@ -65,11 +65,13 @@ default <K, HK, HV> MapRecord<K, HK, HV> deserialize(@Nullable RedisSerializer<?
6565
@Nullable RedisSerializer<? extends HK> fieldSerializer,
6666
@Nullable RedisSerializer<? extends HV> valueSerializer) {
6767

68-
return mapEntries(it -> Collections
69-
.<HK, HV> singletonMap(fieldSerializer != null ? fieldSerializer.deserialize(it.getKey()) : (HK) it.getKey(),
70-
valueSerializer != null ? valueSerializer.deserialize(it.getValue()) : (HV) it.getValue())
71-
.entrySet().iterator().next())
72-
.withStreamKey(streamSerializer != null ? streamSerializer.deserialize(getStream()) : (K) getStream());
68+
return mapEntries(it -> {
69+
70+
Map<HK, HV> map = Collections.singletonMap(StreamSerialization.deserialize(fieldSerializer, it.getKey()),
71+
StreamSerialization.deserialize(valueSerializer, it.getValue()));
72+
73+
return map.entrySet().iterator().next();
74+
}).withStreamKey(StreamSerialization.deserialize(streamSerializer, getRequiredStream()));
7375
}
7476

7577
/**
@@ -79,6 +81,6 @@ default <K, HK, HV> MapRecord<K, HK, HV> deserialize(@Nullable RedisSerializer<?
7981
* @return new instance of {@link ByteRecord}.
8082
*/
8183
static ByteRecord of(MapRecord<byte[], byte[], byte[]> source) {
82-
return StreamRecords.newRecord().in(source.getStream()).withId(source.getId()).ofBytes(source.getValue());
84+
return StreamRecords.newRecord().in(source.getRequiredStream()).withId(source.getId()).ofBytes(source.getValue());
8385
}
8486
}

src/main/java/org/springframework/data/redis/connection/stream/MapRecord.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ default <HK, HV> MapRecord<S, HK, HV> mapEntries(Function<Entry<K, V>, Entry<HK,
7878
mapped.put(mappedPair.getKey(), mappedPair.getValue());
7979
});
8080

81-
return StreamRecords.newRecord().in(getStream()).withId(getId()).ofMap(mapped);
81+
return StreamRecords.newRecord().in(getRequiredStream()).withId(getId()).ofMap(mapped);
8282
}
8383

8484
/**
@@ -121,7 +121,7 @@ default ByteRecord serialize(@Nullable RedisSerializer<? super S> streamSerializ
121121
StreamSerialization.serialize(valueSerializer, it.getValue())).entrySet().iterator().next());
122122

123123
return StreamRecords.newRecord() //
124-
.in(streamSerializer != null ? streamSerializer.serialize(getStream()) : (byte[]) getStream()) //
124+
.in(StreamSerialization.serialize(streamSerializer, getRequiredStream())) //
125125
.withId(getId()) //
126126
.ofBytes(binaryMap.getValue());
127127
}
@@ -136,6 +136,6 @@ default ByteRecord serialize(@Nullable RedisSerializer<? super S> streamSerializ
136136
*/
137137
@SuppressWarnings({ "unchecked", "rawtypes" })
138138
default <OV> ObjectRecord<S, OV> toObjectRecord(HashMapper<? super OV, ? super K, ? super V> mapper) {
139-
return Record.<S, OV> of((OV) mapper.fromHash((Map) getValue())).withId(getId()).withStreamKey(getStream());
139+
return Record.<S, OV> of((OV) mapper.fromHash((Map) getValue())).withId(getId()).withStreamKey(getRequiredStream());
140140
}
141141
}

src/main/java/org/springframework/data/redis/connection/stream/Record.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ public interface Record<S, V> {
3939
@Nullable
4040
S getStream();
4141

42+
/**
43+
* The id of the stream (aka the {@literal key} in Redis).
44+
*
45+
* @return can be {@literal null}.
46+
* @throws IllegalStateException if the stream is {@literal null}.
47+
* @since 3.0
48+
*/
49+
default S getRequiredStream() {
50+
51+
S stream = getStream();
52+
53+
if (stream == null) {
54+
throw new IllegalStateException("Stream is not available");
55+
}
56+
57+
return stream;
58+
}
59+
4260
/**
4361
* The id of the entry inside the stream.
4462
*

0 commit comments

Comments
 (0)