Skip to content

Commit 99cf4a2

Browse files
denglimingmp911de
authored andcommitted
DATAREDIS-1085 - Support approximate trimming using XTRIM.
Original pull request: #561.
1 parent c72a848 commit 99cf4a2

17 files changed

+179
-15
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,7 +3859,16 @@ public List<StringRecord> xRevRange(String key, org.springframework.data.domain.
38593859
*/
38603860
@Override
38613861
public Long xTrim(String key, long count) {
3862-
return convertAndReturn(delegate.xTrim(serialize(key), count), identityConverter);
3862+
return xTrim(key, count, false);
3863+
}
3864+
3865+
/*
3866+
* (non-Javadoc)
3867+
* @see org.springframework.data.redis.connection.StringRedisConnection#xTrim(java.lang.String, long, boolean)
3868+
*/
3869+
@Override
3870+
public Long xTrim(String key, long count, boolean approximateTrimming) {
3871+
return convertAndReturn(delegate.xTrim(serialize(key), count, approximateTrimming), identityConverter);
38633872
}
38643873

38653874
/*
@@ -4040,7 +4049,16 @@ public List<ByteRecord> xRevRange(byte[] key, org.springframework.data.domain.Ra
40404049
*/
40414050
@Override
40424051
public Long xTrim(byte[] key, long count) {
4043-
return delegate.xTrim(key, count);
4052+
return xTrim(key, count, false);
4053+
}
4054+
4055+
/*
4056+
* (non-Javadoc)
4057+
* @see org.springframework.data.redis.connection.RedisStreamCommands#xTrim(byte[], long, boolean)
4058+
*/
4059+
@Override
4060+
public Long xTrim(byte[] key, long count, boolean approximateTrimming) {
4061+
return delegate.xTrim(key, count, approximateTrimming);
40444062
}
40454063

40464064
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,13 @@ default List<ByteRecord> xRevRange(byte[] key, org.springframework.data.domain.R
609609
@Override
610610
@Deprecated
611611
default Long xTrim(byte[] key, long count) {
612-
return streamCommands().xTrim(key, count);
612+
return xTrim(key, count, false);
613+
}
614+
615+
@Override
616+
@Deprecated
617+
default Long xTrim(byte[] key, long count, boolean approximateTrimming) {
618+
return streamCommands().xTrim(key, count, approximateTrimming);
613619
}
614620

615621
// LIST COMMANDS

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,11 +1376,12 @@ default Flux<ByteBufferRecord> xRevRange(ByteBuffer key, Range<String> range, Li
13761376
class TrimCommand extends KeyCommand {
13771377

13781378
private @Nullable Long count;
1379+
private boolean approximateTrimming;
13791380

1380-
private TrimCommand(ByteBuffer key, @Nullable Long count) {
1381-
1381+
private TrimCommand(ByteBuffer key, @Nullable Long count, boolean approximateTrimming) {
13821382
super(key);
13831383
this.count = count;
1384+
this.approximateTrimming = approximateTrimming;
13841385
}
13851386

13861387
/**
@@ -1393,18 +1394,19 @@ public static TrimCommand stream(ByteBuffer key) {
13931394

13941395
Assert.notNull(key, "Key must not be null!");
13951396

1396-
return new TrimCommand(key, null);
1397+
return new TrimCommand(key, null, false);
13971398
}
13981399

13991400
/**
14001401
* Applies the numeric {@literal count}. Constructs a new command instance with all previously configured
14011402
* properties.
14021403
*
14031404
* @param count
1405+
* @param approximateTrimming
14041406
* @return a new {@link TrimCommand} with {@literal count} applied.
14051407
*/
1406-
public TrimCommand to(long count) {
1407-
return new TrimCommand(getKey(), count);
1408+
public TrimCommand to(long count, boolean approximateTrimming) {
1409+
return new TrimCommand(getKey(), count, approximateTrimming);
14081410
}
14091411

14101412
/**
@@ -1414,6 +1416,10 @@ public TrimCommand to(long count) {
14141416
public Long getCount() {
14151417
return count;
14161418
}
1419+
1420+
public boolean isApproximateTrimming() {
1421+
return approximateTrimming;
1422+
}
14171423
}
14181424

14191425
/**
@@ -1425,10 +1431,23 @@ public Long getCount() {
14251431
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
14261432
*/
14271433
default Mono<Long> xTrim(ByteBuffer key, long count) {
1434+
return xTrim(key, count, false);
1435+
}
1436+
1437+
/**
1438+
* Trims the stream to {@code count} elements.
1439+
*
1440+
* @param key the stream key.
1441+
* @param count length of the stream.
1442+
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
1443+
* @return {@link Mono} emitting the number of removed entries.
1444+
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
1445+
*/
1446+
default Mono<Long> xTrim(ByteBuffer key, long count, boolean approximateTrimming) {
14281447

14291448
Assert.notNull(key, "Key must not be null!");
14301449

1431-
return xTrim(Mono.just(TrimCommand.stream(key).to(count))).next().map(NumericResponse::getOutput);
1450+
return xTrim(Mono.just(TrimCommand.stream(key).to(count, approximateTrimming))).next().map(NumericResponse::getOutput);
14321451
}
14331452

14341453
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,16 @@ default List<ByteRecord> xRevRange(byte[] key, Range<String> range) {
873873
*/
874874
@Nullable
875875
Long xTrim(byte[] key, long count);
876+
877+
/**
878+
* Trims the stream to {@code count} elements.
879+
*
880+
* @param key the stream key.
881+
* @param count length of the stream.
882+
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
883+
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
884+
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
885+
*/
886+
@Nullable
887+
Long xTrim(byte[] key, long count, boolean approximateTrimming);
876888
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,4 +2459,17 @@ default List<StringRecord> xRevRange(String key, org.springframework.data.domain
24592459
*/
24602460
@Nullable
24612461
Long xTrim(String key, long count);
2462+
2463+
/**
2464+
* Trims the stream to {@code count} elements.
2465+
*
2466+
* @param key the stream key.
2467+
* @param count length of the stream.
2468+
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
2469+
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
2470+
* @since 2.2
2471+
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
2472+
*/
2473+
@Nullable
2474+
Long xTrim(String key, long count, boolean approximateTrimming);
24622475
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public Flux<NumericResponse<KeyCommand, Long>> xTrim(Publisher<TrimCommand> comm
420420
Assert.notNull(command.getKey(), "Key must not be null!");
421421
Assert.notNull(command.getCount(), "Count must not be null!");
422422

423-
return cmd.xtrim(command.getKey(), command.getCount()).map(value -> new NumericResponse<>(command, value));
423+
return cmd.xtrim(command.getKey(), command.isApproximateTrimming(), command.getCount()).map(value -> new NumericResponse<>(command, value));
424424
}));
425425
}
426426

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,19 +662,27 @@ public List<ByteRecord> xRevRange(byte[] key, Range<String> range, Limit limit)
662662
*/
663663
@Override
664664
public Long xTrim(byte[] key, long count) {
665+
return xTrim(key, count, false);
666+
}
665667

668+
/*
669+
* (non-Javadoc)
670+
* @see org.springframework.data.redis.connection.RedisStreamCommands#xTrim(byte[], long, boolean)
671+
*/
672+
@Override
673+
public Long xTrim(byte[] key, long count, boolean approximateTrimming) {
666674
Assert.notNull(key, "Key must not be null!");
667675

668676
try {
669677
if (isPipelined()) {
670-
pipeline(connection.newLettuceResult(getAsyncConnection().xtrim(key, count)));
678+
pipeline(connection.newLettuceResult(getAsyncConnection().xtrim(key, approximateTrimming, count)));
671679
return null;
672680
}
673681
if (isQueueing()) {
674-
transaction(connection.newLettuceResult(getAsyncConnection().xtrim(key, count)));
682+
transaction(connection.newLettuceResult(getAsyncConnection().xtrim(key, approximateTrimming, count)));
675683
return null;
676684
}
677-
return getConnection().xtrim(key, count);
685+
return getConnection().xtrim(key, approximateTrimming, count);
678686
} catch (Exception ex) {
679687
throw convertLettuceAccessException(ex);
680688
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,15 @@ default List<MapRecord<K, HK, HV>> reverseRange(Range<String> range) {
208208
*/
209209
@Nullable
210210
Long trim(long count);
211+
212+
/**
213+
* Trims the stream to {@code count} elements.
214+
*
215+
* @param count length of the stream.
216+
* @param approximateTrimming the trimming must be performed in a approximated way in order to maximize performances.
217+
* @return number of removed entries. {@literal null} when used in pipeline / transaction.
218+
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
219+
*/
220+
@Nullable
221+
Long trim(long count, boolean approximateTrimming);
211222
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,17 @@ public List<MapRecord<K, HK, HV>> reverseRange(Range<String> range, Limit limit)
170170
@Nullable
171171
@Override
172172
public Long trim(long count) {
173-
return ops.trim(getKey(), count);
173+
return trim(count, false);
174+
}
175+
176+
/*
177+
* (non-Javadoc)
178+
* @see org.springframework.data.redis.core.BoundStreamOperations#trim(long,boolean)
179+
*/
180+
@Nullable
181+
@Override
182+
public Long trim(long count, boolean approximateTrimming) {
183+
return ops.trim(getKey(), count, approximateTrimming);
174184
}
175185

176186
/*

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,18 @@ public Flux<MapRecord<K, HK, HV>> reverseRange(K key, Range<String> range, Limit
341341
*/
342342
@Override
343343
public Mono<Long> trim(K key, long count) {
344+
return trim(key, count, false);
345+
}
344346

347+
/*
348+
* (non-Javadoc)
349+
* @see org.springframework.data.redis.core.ReactiveStreamOperations#trim(java.lang.Object, long, boolean)
350+
*/
351+
@Override
352+
public Mono<Long> trim(K key, long count, boolean approximateTrimming) {
345353
Assert.notNull(key, "Key must not be null!");
346354

347-
return createMono(connection -> connection.xTrim(rawKey(key), count));
355+
return createMono(connection -> connection.xTrim(rawKey(key), count, approximateTrimming));
348356
}
349357

350358
@Override

0 commit comments

Comments
 (0)