DATAREDIS-1085 - Polishing.

Add author and since tags. Introduce separate approximate() method instead of changing count(…) to retain backwards compatibility.

Original pull request: #561.
This commit is contained in:
Mark Paluch
2020-09-16 09:47:19 +02:00
parent 99cf4a2238
commit c16e13ae75
8 changed files with 56 additions and 25 deletions

View File

@@ -57,6 +57,7 @@ import org.springframework.util.StringUtils;
* @author Mark Paluch
* @author Christoph Strobl
* @author Tugdual Grall
* @author Dengliming
* @since 2.2
*/
public interface ReactiveStreamCommands {
@@ -1402,10 +1403,30 @@ public interface ReactiveStreamCommands {
* properties.
*
* @param count
* @param approximateTrimming
* @return a new {@link TrimCommand} with {@literal count} applied.
*/
public TrimCommand to(long count, boolean approximateTrimming) {
public TrimCommand to(long count) {
return new TrimCommand(getKey(), count, approximateTrimming);
}
/**
* Applies approximate trimming. Constructs a new command instance with all previously configured properties.
*
* @return a new {@link TrimCommand} with {@literal approximateTrimming} applied.
* @since 2.4
*/
public TrimCommand approximate() {
return approximate(true);
}
/**
* Applies {@code approximateTrimming}. Constructs a new command instance with all previously configured properties.
*
* @param approximateTrimming
* @return a new {@link TrimCommand} with {@literal approximateTrimming} applied.
* @since 2.4
*/
public TrimCommand approximate(boolean approximateTrimming) {
return new TrimCommand(getKey(), count, approximateTrimming);
}
@@ -1441,13 +1462,15 @@ public interface ReactiveStreamCommands {
* @param count length of the stream.
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
* @return {@link Mono} emitting the number of removed entries.
* @since 2.4
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
default Mono<Long> xTrim(ByteBuffer key, long count, boolean approximateTrimming) {
Assert.notNull(key, "Key must not be null!");
return xTrim(Mono.just(TrimCommand.stream(key).to(count, approximateTrimming))).next().map(NumericResponse::getOutput);
return xTrim(Mono.just(TrimCommand.stream(key).to(count).approximate(approximateTrimming))).next()
.map(NumericResponse::getOutput);
}
/**

View File

@@ -40,6 +40,7 @@ import org.springframework.util.StringUtils;
* @author Mark Paluch
* @author Christoph Strobl
* @author Tugdual Grall
* @author Dengliming
* @see <a href="https://redis.io/topics/streams-intro">Redis Documentation - Streams</a>
* @since 2.2
*/
@@ -136,7 +137,7 @@ public interface RedisStreamCommands {
/**
* Limit the size of the stream to the given maximum number of elements.
*
*
* @return new instance of {@link XAddOptions}.
*/
public static XAddOptions maxlen(long maxlen) {
@@ -145,7 +146,7 @@ public interface RedisStreamCommands {
/**
* Limit the size of the stream to the given maximum number of elements.
*
*
* @return can be {@literal null}.
*/
@Nullable
@@ -513,7 +514,7 @@ public interface RedisStreamCommands {
/**
* Obtain general information about the stream stored at the specified {@literal key}.
*
*
* @param key the {@literal key} the stream is stored at.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.3
@@ -881,6 +882,7 @@ public interface RedisStreamCommands {
* @param count length of the stream.
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable

View File

@@ -62,6 +62,7 @@ import org.springframework.util.CollectionUtils;
* @author Mark Paluch
* @author Ninad Divadkar
* @author Tugdual Grall
* @author Dengliming
* @see RedisCallback
* @see RedisSerializer
* @see StringRedisTemplate
@@ -2467,7 +2468,7 @@ public interface StringRedisConnection extends RedisConnection {
* @param count length of the stream.
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
* @since 2.2
* @since 2.4
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable

View File

@@ -54,6 +54,7 @@ import org.springframework.util.Assert;
*
* @author Mark Paluch
* @author Tugdual Grall
* @author Dengliming
* @since 2.2
*/
class LettuceReactiveStreamCommands implements ReactiveStreamCommands {

View File

@@ -19,12 +19,12 @@ import java.util.List;
import java.util.Map;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
import org.springframework.data.redis.connection.stream.Consumer;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
import org.springframework.lang.Nullable;
/**
@@ -32,6 +32,7 @@ import org.springframework.lang.Nullable;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Dengliming
* @since 2.2
*/
public interface BoundStreamOperations<K, HK, HV> {
@@ -215,6 +216,7 @@ public interface BoundStreamOperations<K, HK, HV> {
* @param count length of the stream.
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable

View File

@@ -37,6 +37,7 @@ import org.springframework.util.Assert;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Dengliming
* @since 2.2
*/
public interface ReactiveStreamOperations<K, HK, HV> extends HashMapperProvider<HK, HV> {
@@ -551,6 +552,7 @@ public interface ReactiveStreamOperations<K, HK, HV> extends HashMapperProvider<
* @param count length of the stream.
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
* @return number of removed entries.
* @since 2.4
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
Mono<Long> trim(K key, long count, boolean approximateTrimming);

View File

@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Dengliming
* @since 2.2
*/
public interface StreamOperations<K, HK, HV> extends HashMapperProvider<HK, HV> {
@@ -532,6 +533,7 @@ public interface StreamOperations<K, HK, HV> extends HashMapperProvider<HK, HV>
* @param count length of the stream.
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable

View File

@@ -30,7 +30,6 @@ import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@@ -122,7 +121,6 @@ public class DefaultStringRedisConnectionTests {
MockitoAnnotations.initMocks(this);
// this.nativeConnection = mock(RedisConnection.class, withSettings().verboseLogging());
this.nativeConnection = mock(RedisConnection.class);
this.connection = new DefaultStringRedisConnection(nativeConnection);
bytesMap.put(fooBytes, barBytes);
@@ -2052,7 +2050,7 @@ public class DefaultStringRedisConnectionTests {
doReturn(1L).when(nativeConnection).xAck(any(byte[].class), any(String.class), eq(RecordId.of("1-1")));
actual.add(connection.xAck("key", "group", RecordId.of("1-1")));
Assertions.assertThat(getResults()).containsExactly(1L);
assertThat(getResults()).containsExactly(1L);
}
@Test // DATAREDIS-864, DATAREDIS-1122
@@ -2062,7 +2060,7 @@ public class DefaultStringRedisConnectionTests {
actual.add(connection
.xAdd(StreamRecords.newRecord().in("stream-1").ofStrings(Collections.singletonMap("field", "value"))));
Assertions.assertThat(getResults()).containsExactly(RecordId.of("1-1"));
assertThat(getResults()).containsExactly(RecordId.of("1-1"));
}
@Test // DATAREDIS-864
@@ -2071,7 +2069,7 @@ public class DefaultStringRedisConnectionTests {
doReturn(1L).when(nativeConnection).xDel(any(byte[].class), eq(RecordId.of("1-1")));
actual.add(connection.xDel("key", RecordId.of("1-1")));
Assertions.assertThat(getResults()).containsExactly(1L);
assertThat(getResults()).containsExactly(1L);
}
@Test // DATAREDIS-864
@@ -2080,7 +2078,7 @@ public class DefaultStringRedisConnectionTests {
doReturn("OK").when(nativeConnection).xGroupCreate(any(), any(), any());
actual.add(connection.xGroupCreate("key", ReadOffset.latest(), "consumer-group"));
Assertions.assertThat(getResults()).containsExactly("OK");
assertThat(getResults()).containsExactly("OK");
}
@Test // DATAREDIS-864
@@ -2092,7 +2090,7 @@ public class DefaultStringRedisConnectionTests {
doReturn(Boolean.TRUE).when(nativeConnection).xGroupDelConsumer(eq(fooBytes), eq(consumer));
actual.add(connection.xGroupDelConsumer(foo, consumer));
Assertions.assertThat(getResults()).containsExactly(Boolean.TRUE);
assertThat(getResults()).containsExactly(Boolean.TRUE);
}
@Test // DATAREDIS-864
@@ -2101,7 +2099,7 @@ public class DefaultStringRedisConnectionTests {
doReturn(Boolean.TRUE).when(nativeConnection).xGroupDestroy(any(), any());
actual.add(connection.xGroupDestroy("key", "comsumer-group"));
Assertions.assertThat(getResults()).containsExactly(Boolean.TRUE);
assertThat(getResults()).containsExactly(Boolean.TRUE);
}
@Test // DATAREDIS-864
@@ -2110,7 +2108,7 @@ public class DefaultStringRedisConnectionTests {
doReturn(1L).when(nativeConnection).xLen(any());
actual.add(connection.xLen("key"));
Assertions.assertThat(getResults()).containsExactly(1L);
assertThat(getResults()).containsExactly(1L);
}
@Test // DATAREDIS-864
@@ -2121,7 +2119,7 @@ public class DefaultStringRedisConnectionTests {
actual.add(connection.xRange("stream-1", org.springframework.data.domain.Range.unbounded(), Limit.unlimited()));
Assertions.assertThat(getResults()).containsExactly(
assertThat(getResults()).containsExactly(
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
}
@@ -2133,7 +2131,7 @@ public class DefaultStringRedisConnectionTests {
.when(nativeConnection).xRead(any(), any());
actual.add(connection.xReadAsString(StreamReadOptions.empty(), StreamOffset.create("stream-1", ReadOffset.latest())));
Assertions.assertThat(getResults()).containsExactly(
assertThat(getResults()).containsExactly(
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
}
@@ -2144,7 +2142,7 @@ public class DefaultStringRedisConnectionTests {
.when(nativeConnection).xReadGroup(any(), any(), any());
actual.add(connection.xReadGroupAsString(Consumer.from("groupe", "one"), StreamReadOptions.empty(), StreamOffset.create("stream-1", ReadOffset.latest())));
Assertions.assertThat(getResults()).containsExactly(
assertThat(getResults()).containsExactly(
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
}
@@ -2156,26 +2154,26 @@ public class DefaultStringRedisConnectionTests {
actual.add(connection.xRevRange("stream-1", org.springframework.data.domain.Range.unbounded(), Limit.unlimited()));
Assertions.assertThat(getResults()).containsExactly(
assertThat(getResults()).containsExactly(
Collections.singletonList(StreamRecords.newRecord().in(bar2).withId("stream-1").ofStrings(stringMap)));
}
@Test // DATAREDIS-864
public void xTrimShouldDelegateAndConvertCorrectly() {
doReturn(1L).when(nativeConnection).xTrim(any(), anyLong());
doReturn(1L).when(nativeConnection).xTrim(any(), anyLong(), eq(false));
actual.add(connection.xTrim("key", 2L));
Assertions.assertThat(getResults()).containsExactly(1L);
assertThat(getResults()).containsExactly(1L);
}
@Test
@Test // DATAREDIS-1085
public void xTrimApproximateShouldDelegateAndConvertCorrectly() {
doReturn(1L).when(nativeConnection).xTrim(any(), anyLong(), anyBoolean());
actual.add(connection.xTrim("key", 2L, true));
Assertions.assertThat(getResults()).containsExactly(1L);
assertThat(getResults()).containsExactly(1L);
}
protected List<Object> getResults() {