Skip to content

Commit c16e13a

Browse files
committed
DATAREDIS-1085 - Polishing.
Add author and since tags. Introduce separate approximate() method instead of changing count(…) to retain backwards compatibility. Original pull request: #561.
1 parent 99cf4a2 commit c16e13a

File tree

8 files changed

+56
-25
lines changed

8 files changed

+56
-25
lines changed

src/main/java/org/springframework/data/redis/connection/ReactiveStreamCommands.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @author Mark Paluch
5858
* @author Christoph Strobl
5959
* @author Tugdual Grall
60+
* @author Dengliming
6061
* @since 2.2
6162
*/
6263
public interface ReactiveStreamCommands {
@@ -1402,10 +1403,30 @@ public static TrimCommand stream(ByteBuffer key) {
14021403
* properties.
14031404
*
14041405
* @param count
1405-
* @param approximateTrimming
14061406
* @return a new {@link TrimCommand} with {@literal count} applied.
14071407
*/
1408-
public TrimCommand to(long count, boolean approximateTrimming) {
1408+
public TrimCommand to(long count) {
1409+
return new TrimCommand(getKey(), count, approximateTrimming);
1410+
}
1411+
1412+
/**
1413+
* Applies approximate trimming. Constructs a new command instance with all previously configured properties.
1414+
*
1415+
* @return a new {@link TrimCommand} with {@literal approximateTrimming} applied.
1416+
* @since 2.4
1417+
*/
1418+
public TrimCommand approximate() {
1419+
return approximate(true);
1420+
}
1421+
1422+
/**
1423+
* Applies {@code approximateTrimming}. Constructs a new command instance with all previously configured properties.
1424+
*
1425+
* @param approximateTrimming
1426+
* @return a new {@link TrimCommand} with {@literal approximateTrimming} applied.
1427+
* @since 2.4
1428+
*/
1429+
public TrimCommand approximate(boolean approximateTrimming) {
14091430
return new TrimCommand(getKey(), count, approximateTrimming);
14101431
}
14111432

@@ -1441,13 +1462,15 @@ default Mono<Long> xTrim(ByteBuffer key, long count) {
14411462
* @param count length of the stream.
14421463
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
14431464
* @return {@link Mono} emitting the number of removed entries.
1465+
* @since 2.4
14441466
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
14451467
*/
14461468
default Mono<Long> xTrim(ByteBuffer key, long count, boolean approximateTrimming) {
14471469

14481470
Assert.notNull(key, "Key must not be null!");
14491471

1450-
return xTrim(Mono.just(TrimCommand.stream(key).to(count, approximateTrimming))).next().map(NumericResponse::getOutput);
1472+
return xTrim(Mono.just(TrimCommand.stream(key).to(count).approximate(approximateTrimming))).next()
1473+
.map(NumericResponse::getOutput);
14511474
}
14521475

14531476
/**

src/main/java/org/springframework/data/redis/connection/RedisStreamCommands.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Mark Paluch
4141
* @author Christoph Strobl
4242
* @author Tugdual Grall
43+
* @author Dengliming
4344
* @see <a href="https://redis.io/topics/streams-intro">Redis Documentation - Streams</a>
4445
* @since 2.2
4546
*/
@@ -136,7 +137,7 @@ public static XAddOptions none() {
136137

137138
/**
138139
* Limit the size of the stream to the given maximum number of elements.
139-
*
140+
*
140141
* @return new instance of {@link XAddOptions}.
141142
*/
142143
public static XAddOptions maxlen(long maxlen) {
@@ -145,7 +146,7 @@ public static XAddOptions maxlen(long maxlen) {
145146

146147
/**
147148
* Limit the size of the stream to the given maximum number of elements.
148-
*
149+
*
149150
* @return can be {@literal null}.
150151
*/
151152
@Nullable
@@ -513,7 +514,7 @@ default Boolean xGroupDelConsumer(byte[] key, String groupName, String consumerN
513514

514515
/**
515516
* Obtain general information about the stream stored at the specified {@literal key}.
516-
*
517+
*
517518
* @param key the {@literal key} the stream is stored at.
518519
* @return {@literal null} when used in pipeline / transaction.
519520
* @since 2.3
@@ -881,6 +882,7 @@ default List<ByteRecord> xRevRange(byte[] key, Range<String> range) {
881882
* @param count length of the stream.
882883
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
883884
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
885+
* @since 2.4
884886
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
885887
*/
886888
@Nullable

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author Mark Paluch
6363
* @author Ninad Divadkar
6464
* @author Tugdual Grall
65+
* @author Dengliming
6566
* @see RedisCallback
6667
* @see RedisSerializer
6768
* @see StringRedisTemplate
@@ -2467,7 +2468,7 @@ default List<StringRecord> xRevRange(String key, org.springframework.data.domain
24672468
* @param count length of the stream.
24682469
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
24692470
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
2470-
* @since 2.2
2471+
* @since 2.4
24712472
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
24722473
*/
24732474
@Nullable

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
*
5555
* @author Mark Paluch
5656
* @author Tugdual Grall
57+
* @author Dengliming
5758
* @since 2.2
5859
*/
5960
class LettuceReactiveStreamCommands implements ReactiveStreamCommands {

src/main/java/org/springframework/data/redis/core/BoundStreamOperations.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@
1919
import java.util.Map;
2020

2121
import org.springframework.data.domain.Range;
22+
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
2223
import org.springframework.data.redis.connection.stream.Consumer;
2324
import org.springframework.data.redis.connection.stream.MapRecord;
2425
import org.springframework.data.redis.connection.stream.ReadOffset;
2526
import org.springframework.data.redis.connection.stream.RecordId;
2627
import org.springframework.data.redis.connection.stream.StreamReadOptions;
27-
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
2828
import org.springframework.lang.Nullable;
2929

3030
/**
3131
* Redis stream specific operations bound to a certain key.
3232
*
3333
* @author Mark Paluch
3434
* @author Christoph Strobl
35+
* @author Dengliming
3536
* @since 2.2
3637
*/
3738
public interface BoundStreamOperations<K, HK, HV> {
@@ -215,6 +216,7 @@ default List<MapRecord<K, HK, HV>> reverseRange(Range<String> range) {
215216
* @param count length of the stream.
216217
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
217218
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
219+
* @since 2.4
218220
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
219221
*/
220222
@Nullable

src/main/java/org/springframework/data/redis/core/ReactiveStreamOperations.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Mark Paluch
3939
* @author Christoph Strobl
40+
* @author Dengliming
4041
* @since 2.2
4142
*/
4243
public interface ReactiveStreamOperations<K, HK, HV> extends HashMapperProvider<HK, HV> {
@@ -551,6 +552,7 @@ default <V> Flux<ObjectRecord<K, V>> reverseRange(Class<V> targetType, K key, Ra
551552
* @param count length of the stream.
552553
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
553554
* @return number of removed entries.
555+
* @since 2.4
554556
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
555557
*/
556558
Mono<Long> trim(K key, long count, boolean approximateTrimming);

src/main/java/org/springframework/data/redis/core/StreamOperations.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Mark Paluch
3838
* @author Christoph Strobl
39+
* @author Dengliming
3940
* @since 2.2
4041
*/
4142
public interface StreamOperations<K, HK, HV> extends HashMapperProvider<HK, HV> {
@@ -532,6 +533,7 @@ default <V> List<ObjectRecord<K, V>> reverseRange(Class<V> targetType, K key, Ra
532533
* @param count length of the stream.
533534
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
534535
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
536+
* @since 2.4
535537
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
536538
*/
537539
@Nullable

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Set;
3131
import java.util.concurrent.TimeUnit;
3232

33-
import org.assertj.core.api.Assertions;
3433
import org.junit.Before;
3534
import org.junit.Ignore;
3635
import org.junit.Test;
@@ -122,7 +121,6 @@ public void setUp() {
122121

123122

124123
MockitoAnnotations.initMocks(this);
125-
// this.nativeConnection = mock(RedisConnection.class, withSettings().verboseLogging());
126124
this.nativeConnection = mock(RedisConnection.class);
127125
this.connection = new DefaultStringRedisConnection(nativeConnection);
128126
bytesMap.put(fooBytes, barBytes);
@@ -2052,7 +2050,7 @@ public void xAckShouldDelegateAndConvertCorrectly() {
20522050
doReturn(1L).when(nativeConnection).xAck(any(byte[].class), any(String.class), eq(RecordId.of("1-1")));
20532051

20542052
actual.add(connection.xAck("key", "group", RecordId.of("1-1")));
2055-
Assertions.assertThat(getResults()).containsExactly(1L);
2053+
assertThat(getResults()).containsExactly(1L);
20562054
}
20572055

20582056
@Test // DATAREDIS-864, DATAREDIS-1122
@@ -2062,7 +2060,7 @@ public void xAddShouldAppendRecordCorrectly() {
20622060
actual.add(connection
20632061
.xAdd(StreamRecords.newRecord().in("stream-1").ofStrings(Collections.singletonMap("field", "value"))));
20642062

2065-
Assertions.assertThat(getResults()).containsExactly(RecordId.of("1-1"));
2063+
assertThat(getResults()).containsExactly(RecordId.of("1-1"));
20662064
}
20672065

20682066
@Test // DATAREDIS-864
@@ -2071,7 +2069,7 @@ public void xDelShouldDelegateAndConvertCorrectly() {
20712069
doReturn(1L).when(nativeConnection).xDel(any(byte[].class), eq(RecordId.of("1-1")));
20722070

20732071
actual.add(connection.xDel("key", RecordId.of("1-1")));
2074-
Assertions.assertThat(getResults()).containsExactly(1L);
2072+
assertThat(getResults()).containsExactly(1L);
20752073
}
20762074

20772075
@Test // DATAREDIS-864
@@ -2080,7 +2078,7 @@ public void xGroupCreateShouldDelegateAndConvertCorrectly() {
20802078
doReturn("OK").when(nativeConnection).xGroupCreate(any(), any(), any());
20812079

20822080
actual.add(connection.xGroupCreate("key", ReadOffset.latest(), "consumer-group"));
2083-
Assertions.assertThat(getResults()).containsExactly("OK");
2081+
assertThat(getResults()).containsExactly("OK");
20842082
}
20852083

20862084
@Test // DATAREDIS-864
@@ -2092,7 +2090,7 @@ public void xGroupDelConsumerShouldDelegateAndConvertCorrectly() {
20922090
doReturn(Boolean.TRUE).when(nativeConnection).xGroupDelConsumer(eq(fooBytes), eq(consumer));
20932091

20942092
actual.add(connection.xGroupDelConsumer(foo, consumer));
2095-
Assertions.assertThat(getResults()).containsExactly(Boolean.TRUE);
2093+
assertThat(getResults()).containsExactly(Boolean.TRUE);
20962094
}
20972095

20982096
@Test // DATAREDIS-864
@@ -2101,7 +2099,7 @@ public void xGroupDestroyShouldDelegateAndConvertCorrectly() {
21012099
doReturn(Boolean.TRUE).when(nativeConnection).xGroupDestroy(any(), any());
21022100

21032101
actual.add(connection.xGroupDestroy("key", "comsumer-group"));
2104-
Assertions.assertThat(getResults()).containsExactly(Boolean.TRUE);
2102+
assertThat(getResults()).containsExactly(Boolean.TRUE);
21052103
}
21062104

21072105
@Test // DATAREDIS-864
@@ -2110,7 +2108,7 @@ public void xLenShouldDelegateAndConvertCorrectly() {
21102108
doReturn(1L).when(nativeConnection).xLen(any());
21112109

21122110
actual.add(connection.xLen("key"));
2113-
Assertions.assertThat(getResults()).containsExactly(1L);
2111+
assertThat(getResults()).containsExactly(1L);
21142112
}
21152113

21162114
@Test // DATAREDIS-864
@@ -2121,7 +2119,7 @@ public void xRangeShouldDelegateAndConvertCorrectly() {
21212119

21222120
actual.add(connection.xRange("stream-1", org.springframework.data.domain.Range.unbounded(), Limit.unlimited()));
21232121

2124-
Assertions.assertThat(getResults()).containsExactly(
2122+
assertThat(getResults()).containsExactly(
21252123
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
21262124
}
21272125

@@ -2133,7 +2131,7 @@ public void xReadShouldDelegateAndConvertCorrectly() {
21332131
.when(nativeConnection).xRead(any(), any());
21342132
actual.add(connection.xReadAsString(StreamReadOptions.empty(), StreamOffset.create("stream-1", ReadOffset.latest())));
21352133

2136-
Assertions.assertThat(getResults()).containsExactly(
2134+
assertThat(getResults()).containsExactly(
21372135
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
21382136
}
21392137

@@ -2144,7 +2142,7 @@ public void xReadGroupShouldDelegateAndConvertCorrectly() {
21442142
.when(nativeConnection).xReadGroup(any(), any(), any());
21452143
actual.add(connection.xReadGroupAsString(Consumer.from("groupe", "one"), StreamReadOptions.empty(), StreamOffset.create("stream-1", ReadOffset.latest())));
21462144

2147-
Assertions.assertThat(getResults()).containsExactly(
2145+
assertThat(getResults()).containsExactly(
21482146
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
21492147
}
21502148

@@ -2156,26 +2154,26 @@ public void xRevRangeShouldDelegateAndConvertCorrectly() {
21562154

21572155
actual.add(connection.xRevRange("stream-1", org.springframework.data.domain.Range.unbounded(), Limit.unlimited()));
21582156

2159-
Assertions.assertThat(getResults()).containsExactly(
2157+
assertThat(getResults()).containsExactly(
21602158
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
21612159
}
21622160

21632161
@Test // DATAREDIS-864
21642162
public void xTrimShouldDelegateAndConvertCorrectly() {
21652163

2166-
doReturn(1L).when(nativeConnection).xTrim(any(), anyLong());
2164+
doReturn(1L).when(nativeConnection).xTrim(any(), anyLong(), eq(false));
21672165

21682166
actual.add(connection.xTrim("key", 2L));
2169-
Assertions.assertThat(getResults()).containsExactly(1L);
2167+
assertThat(getResults()).containsExactly(1L);
21702168
}
21712169

2172-
@Test
2170+
@Test // DATAREDIS-1085
21732171
public void xTrimApproximateShouldDelegateAndConvertCorrectly() {
21742172

21752173
doReturn(1L).when(nativeConnection).xTrim(any(), anyLong(), anyBoolean());
21762174

21772175
actual.add(connection.xTrim("key", 2L, true));
2178-
Assertions.assertThat(getResults()).containsExactly(1L);
2176+
assertThat(getResults()).containsExactly(1L);
21792177
}
21802178

21812179
protected List<Object> getResults() {

0 commit comments

Comments
 (0)