Skip to content

Commit 4135f26

Browse files
Polishing stream commands changes
Signed-off-by: viktoriya.kutsarova <[email protected]>
1 parent da57163 commit 4135f26

12 files changed

+465
-198
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public RedisClusterNode lookup(String nodeId) {
186186
}
187187

188188
/**
189-
* Get the {@link RedisClusterNode} matching matching either {@link RedisClusterNode#getHost() host} and
189+
* Get the {@link RedisClusterNode} matching either {@link RedisClusterNode#getHost() host} and
190190
* {@link RedisClusterNode#getPort() port} or {@link RedisClusterNode#getId() nodeId}
191191
*
192192
* @param node must not be {@literal null}

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

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
import org.springframework.data.redis.connection.ReactiveRedisConnection.CommandResponse;
3434
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyCommand;
3535
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
36+
import org.springframework.data.redis.connection.RedisStreamCommands.MaxLenTrimStrategy;
37+
import org.springframework.data.redis.connection.RedisStreamCommands.TrimOperator;
38+
import org.springframework.data.redis.connection.RedisStreamCommands.TrimOptions;
39+
import org.springframework.data.redis.connection.RedisStreamCommands.TrimStrategy;
3640
import org.springframework.data.redis.connection.RedisStreamCommands.XAddOptions;
3741
import org.springframework.data.redis.connection.RedisStreamCommands.XClaimOptions;
3842
import org.springframework.data.redis.connection.RedisStreamCommands.XDelOptions;
@@ -639,7 +643,7 @@ public static DeleteExCommand stream(ByteBuffer key) {
639643

640644
Assert.notNull(key, "Key must not be null");
641645

642-
return new DeleteExCommand(key, Collections.emptyList(), XDelOptions.defaultOptions());
646+
return new DeleteExCommand(key, Collections.emptyList(), XDelOptions.defaults());
643647
}
644648

645649
/**
@@ -726,7 +730,7 @@ public static AcknowledgeDeleteCommand stream(ByteBuffer key) {
726730

727731
Assert.notNull(key, "Key must not be null");
728732

729-
return new AcknowledgeDeleteCommand(key, null, Collections.emptyList(), XDelOptions.defaultOptions());
733+
return new AcknowledgeDeleteCommand(key, null, Collections.emptyList(), XDelOptions.defaults());
730734
}
731735

732736
/**
@@ -1895,31 +1899,55 @@ private TrimCommand(@Nullable ByteBuffer key, XTrimOptions options) {
18951899
*
18961900
* @param key must not be {@literal null}.
18971901
* @return a new {@link TrimCommand} for {@link ByteBuffer key}.
1902+
* @since 4.0
1903+
* @deprecated since 4.0, prefer {@link #stream(ByteBuffer, XTrimOptions)} instead.
18981904
*/
1905+
@Deprecated(since = "4.0", forRemoval = false)
18991906
public static TrimCommand stream(ByteBuffer key) {
19001907

19011908
Assert.notNull(key, "Key must not be null");
19021909

1903-
return new TrimCommand(key, XTrimOptions.none());
1910+
return new TrimCommand(key, XTrimOptions.trim(TrimOptions.maxLen(0)));
1911+
}
1912+
1913+
/**
1914+
* Creates a new {@link TrimCommand} given a {@link ByteBuffer key} and {@link XTrimOptions}.
1915+
*
1916+
* @param key must not be {@literal null}.
1917+
* @param options must not be {@literal null}.
1918+
* @return a new {@link TrimCommand} for {@link ByteBuffer key}.
1919+
* @since 4.0
1920+
*/
1921+
public static TrimCommand stream(ByteBuffer key, XTrimOptions options) {
1922+
return new TrimCommand(key, options);
19041923
}
19051924

19061925
/**
1907-
* Applies the numeric {@literal limit}. Constructs a new command instance with all previously configured
1926+
* Applies the numeric {@literal threshold}. Constructs a new command instance with all previously configured
19081927
* properties.
19091928
*
1910-
* @param limit
1911-
* @return a new {@link TrimCommand} with {@literal limit} applied.
1929+
* @param threshold
1930+
* @return a new {@link TrimCommand} with {@literal threshold} applied.
1931+
* @deprecated since 4.0: specify a concrete trim strategy (MAXLEN or MINID) via {@link XTrimOptions}
1932+
* and {@link TrimOptions} instead of using this method. Prefer
1933+
* {@code options(XTrimOptions.trim(TrimOptions.maxLen(threshold)))} or construct with
1934+
* {@code stream(key, XTrimOptions.trim(TrimOptions.maxLen(threshold)))}.
19121935
*/
1913-
public TrimCommand to(long limit) {
1914-
return new TrimCommand(getKey(), options.limit(limit));
1936+
@Deprecated(since = "4.0", forRemoval = false)
1937+
public TrimCommand to(long threshold) {
1938+
return new TrimCommand(getKey(), XTrimOptions.trim(TrimOptions.maxLen(threshold)));
19151939
}
19161940

19171941
/**
19181942
* Applies approximate trimming. Constructs a new command instance with all previously configured properties.
19191943
*
19201944
* @return a new {@link TrimCommand} with {@literal approximateTrimming} applied.
19211945
* @since 2.4
1946+
* @deprecated since 4.0: do not toggle the trim operator in isolation. Specify a concrete trim
1947+
* strategy (MAXLEN or MINID) and operator via {@link XTrimOptions} and {@link TrimOptions}, e.g.
1948+
* {@code options(XTrimOptions.trim(TrimOptions.maxLen(n).approximate()))}.
19221949
*/
1950+
@Deprecated(since = "4.0", forRemoval = false)
19231951
public TrimCommand approximate() {
19241952
return approximate(true);
19251953
}
@@ -1930,37 +1958,55 @@ public TrimCommand approximate() {
19301958
* @param approximateTrimming
19311959
* @return a new {@link TrimCommand} with {@literal approximateTrimming} applied.
19321960
* @since 2.4
1961+
* @deprecated since 4.0: do not toggle the trim operator in isolation. Specify a concrete trim
1962+
* strategy (MAXLEN or MINID) and operator via {@link XTrimOptions} and {@link TrimOptions}, e.g.
1963+
* {@code options(XTrimOptions.trim(TrimOptions.maxLen(n).approximate()))} or
1964+
* {@code options(XTrimOptions.trim(TrimOptions.minId(id).exact()))}.
19331965
*/
1966+
@Deprecated(since = "4.0", forRemoval = false)
19341967
public TrimCommand approximate(boolean approximateTrimming) {
1935-
return new TrimCommand(getKey(), options.approximateTrimming(approximateTrimming));
1968+
if (approximateTrimming) {
1969+
return new TrimCommand(getKey(), XTrimOptions.trim(options.getTrimOptions().approximate()));
1970+
}
1971+
return new TrimCommand(getKey(), XTrimOptions.trim(options.getTrimOptions().exact()));
19361972
}
19371973

19381974
/**
19391975
* Apply the given {@link XTrimOptions} to configure the {@literal XTRIM} command.
19401976
* <p>
19411977
* This method allows setting all XTRIM options at once, including trimming strategies
1942-
* ({@literal MAXLEN}, {@literal MINID}), stream creation behavior ({@literal NOMKSTREAM}),
1943-
* and other parameters. Constructs a new command instance with all previously configured
1944-
* properties except the options, which are replaced by the provided {@link XTrimOptions}.
1978+
* ({@literal MAXLEN}, {@literal MINID}) and other parameters. Constructs a new command instance with all
1979+
* previously configured properties except the options, which are replaced by the provided {@link XTrimOptions}.
19451980
*
19461981
* @param options the {@link XTrimOptions} to apply. Must not be {@literal null}.
19471982
* @return a new {@link TrimCommand} with the specified options applied.
19481983
* @since 4.0
19491984
*/
1950-
public TrimCommand withOptions(XTrimOptions options) {
1985+
public TrimCommand options(XTrimOptions options) {
19511986
return new TrimCommand(getKey(), options);
19521987
}
19531988

19541989
/**
1990+
* Returns the MAXLEN threshold if the active trim strategy is {@literal MAXLEN}; otherwise {@literal null}.
1991+
*
19551992
* @return can be {@literal null}.
1993+
* @deprecated since 4.0: Inspect {@link #getOptions()} -> {@link XTrimOptions#getTrimOptions()} ->
1994+
* {@link TrimOptions#getTrimStrategy()} and obtain the threshold from the concrete strategy instead. For example:
1995+
* {@code if (strategy instanceof MaxLenTrimStrategy m) { m.threshold(); }} or
1996+
* {@code if (strategy instanceof MinIdTrimStrategy i) { i.threshold(); }}.
19561997
*/
1998+
@Deprecated(since = "4.0", forRemoval = false)
19571999
public @Nullable Long getCount() {
1958-
return options.getLimit();
2000+
TrimStrategy strategy = options.getTrimOptions().getTrimStrategy();
2001+
if (strategy instanceof MaxLenTrimStrategy maxLen) {
2002+
return maxLen.threshold();
2003+
}
2004+
return null;
19592005
}
19602006

19612007

19622008
public boolean isApproximateTrimming() {
1963-
return options.isApproximateTrimming();
2009+
return options.getTrimOptions().getTrimOperator() == TrimOperator.APPROXIMATE;
19642010
}
19652011

19662012
public XTrimOptions getOptions() {
@@ -2002,7 +2048,7 @@ default Mono<Long> xTrim(ByteBuffer key, XTrimOptions options) {
20022048

20032049
Assert.notNull(key, "Key must not be null");
20042050

2005-
return xTrim(Mono.just(TrimCommand.stream(key).withOptions(options))).next()
2051+
return xTrim(Mono.just(TrimCommand.stream(key).options(options))).next()
20062052
.map(NumericResponse::getOutput);
20072053
}
20082054

0 commit comments

Comments
 (0)