DATAREDIS-1085 - Support approximate trimming using XTRIM.

Original pull request: #561.
This commit is contained in:
dengliming
2020-09-11 23:54:11 +08:00
committed by Mark Paluch
parent c72a84885c
commit 99cf4a2238
17 changed files with 179 additions and 15 deletions

View File

@@ -3859,7 +3859,16 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
*/
@Override
public Long xTrim(String key, long count) {
return convertAndReturn(delegate.xTrim(serialize(key), count), identityConverter);
return xTrim(key, count, false);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#xTrim(java.lang.String, long, boolean)
*/
@Override
public Long xTrim(String key, long count, boolean approximateTrimming) {
return convertAndReturn(delegate.xTrim(serialize(key), count, approximateTrimming), identityConverter);
}
/*
@@ -4040,7 +4049,16 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
*/
@Override
public Long xTrim(byte[] key, long count) {
return delegate.xTrim(key, count);
return xTrim(key, count, false);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisStreamCommands#xTrim(byte[], long, boolean)
*/
@Override
public Long xTrim(byte[] key, long count, boolean approximateTrimming) {
return delegate.xTrim(key, count, approximateTrimming);
}
/**

View File

@@ -609,7 +609,13 @@ public interface DefaultedRedisConnection extends RedisConnection {
@Override
@Deprecated
default Long xTrim(byte[] key, long count) {
return streamCommands().xTrim(key, count);
return xTrim(key, count, false);
}
@Override
@Deprecated
default Long xTrim(byte[] key, long count, boolean approximateTrimming) {
return streamCommands().xTrim(key, count, approximateTrimming);
}
// LIST COMMANDS

View File

@@ -1376,11 +1376,12 @@ public interface ReactiveStreamCommands {
class TrimCommand extends KeyCommand {
private @Nullable Long count;
private boolean approximateTrimming;
private TrimCommand(ByteBuffer key, @Nullable Long count) {
private TrimCommand(ByteBuffer key, @Nullable Long count, boolean approximateTrimming) {
super(key);
this.count = count;
this.approximateTrimming = approximateTrimming;
}
/**
@@ -1393,7 +1394,7 @@ public interface ReactiveStreamCommands {
Assert.notNull(key, "Key must not be null!");
return new TrimCommand(key, null);
return new TrimCommand(key, null, false);
}
/**
@@ -1401,10 +1402,11 @@ public interface ReactiveStreamCommands {
* properties.
*
* @param count
* @param approximateTrimming
* @return a new {@link TrimCommand} with {@literal count} applied.
*/
public TrimCommand to(long count) {
return new TrimCommand(getKey(), count);
public TrimCommand to(long count, boolean approximateTrimming) {
return new TrimCommand(getKey(), count, approximateTrimming);
}
/**
@@ -1414,6 +1416,10 @@ public interface ReactiveStreamCommands {
public Long getCount() {
return count;
}
public boolean isApproximateTrimming() {
return approximateTrimming;
}
}
/**
@@ -1425,10 +1431,23 @@ public interface ReactiveStreamCommands {
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
default Mono<Long> xTrim(ByteBuffer key, long count) {
return xTrim(key, count, false);
}
/**
* Trims the stream to {@code count} elements.
*
* @param key the stream key.
* @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.
* @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))).next().map(NumericResponse::getOutput);
return xTrim(Mono.just(TrimCommand.stream(key).to(count, approximateTrimming))).next().map(NumericResponse::getOutput);
}
/**

View File

@@ -873,4 +873,16 @@ public interface RedisStreamCommands {
*/
@Nullable
Long xTrim(byte[] key, long count);
/**
* Trims the stream to {@code count} elements.
*
* @param key the stream key.
* @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.
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable
Long xTrim(byte[] key, long count, boolean approximateTrimming);
}

View File

@@ -2459,4 +2459,17 @@ public interface StringRedisConnection extends RedisConnection {
*/
@Nullable
Long xTrim(String key, long count);
/**
* Trims the stream to {@code count} elements.
*
* @param key the stream key.
* @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
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable
Long xTrim(String key, long count, boolean approximateTrimming);
}

View File

@@ -420,7 +420,7 @@ class LettuceReactiveStreamCommands implements ReactiveStreamCommands {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getCount(), "Count must not be null!");
return cmd.xtrim(command.getKey(), command.getCount()).map(value -> new NumericResponse<>(command, value));
return cmd.xtrim(command.getKey(), command.isApproximateTrimming(), command.getCount()).map(value -> new NumericResponse<>(command, value));
}));
}

View File

@@ -662,19 +662,27 @@ class LettuceStreamCommands implements RedisStreamCommands {
*/
@Override
public Long xTrim(byte[] key, long count) {
return xTrim(key, count, false);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisStreamCommands#xTrim(byte[], long, boolean)
*/
@Override
public Long xTrim(byte[] key, long count, boolean approximateTrimming) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().xtrim(key, count)));
pipeline(connection.newLettuceResult(getAsyncConnection().xtrim(key, approximateTrimming, count)));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceResult(getAsyncConnection().xtrim(key, count)));
transaction(connection.newLettuceResult(getAsyncConnection().xtrim(key, approximateTrimming, count)));
return null;
}
return getConnection().xtrim(key, count);
return getConnection().xtrim(key, approximateTrimming, count);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}

View File

@@ -208,4 +208,15 @@ public interface BoundStreamOperations<K, HK, HV> {
*/
@Nullable
Long trim(long count);
/**
* Trims the stream to {@code count} elements.
*
* @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.
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable
Long trim(long count, boolean approximateTrimming);
}

View File

@@ -170,7 +170,17 @@ class DefaultBoundStreamOperations<K, HK, HV> extends DefaultBoundKeyOperations<
@Nullable
@Override
public Long trim(long count) {
return ops.trim(getKey(), count);
return trim(count, false);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.BoundStreamOperations#trim(long,boolean)
*/
@Nullable
@Override
public Long trim(long count, boolean approximateTrimming) {
return ops.trim(getKey(), count, approximateTrimming);
}
/*

View File

@@ -341,10 +341,18 @@ class DefaultReactiveStreamOperations<K, HK, HV> implements ReactiveStreamOperat
*/
@Override
public Mono<Long> trim(K key, long count) {
return trim(key, count, false);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveStreamOperations#trim(java.lang.Object, long, boolean)
*/
@Override
public Mono<Long> trim(K key, long count, boolean approximateTrimming) {
Assert.notNull(key, "Key must not be null!");
return createMono(connection -> connection.xTrim(rawKey(key), count));
return createMono(connection -> connection.xTrim(rawKey(key), count, approximateTrimming));
}
@Override

View File

@@ -336,6 +336,12 @@ class DefaultStreamOperations<K, HK, HV> extends AbstractOperations<K, Object> i
return execute(connection -> connection.xTrim(rawKey, count), true);
}
@Override
public Long trim(K key, long count, boolean approximateTrimming) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.xTrim(rawKey, count, approximateTrimming), true);
}
@Override
public <V> HashMapper<V, HK, HV> getHashMapper(Class<V> targetType) {
return objectMapper.getHashMapper(targetType);

View File

@@ -544,6 +544,17 @@ public interface ReactiveStreamOperations<K, HK, HV> extends HashMapperProvider<
*/
Mono<Long> trim(K key, long count);
/**
* Trims the stream to {@code count} elements.
*
* @param key the stream key.
* @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.
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
Mono<Long> trim(K key, long count, boolean approximateTrimming);
/**
* Get the {@link HashMapper} for a specific type.
*

View File

@@ -525,6 +525,18 @@ public interface StreamOperations<K, HK, HV> extends HashMapperProvider<HK, HV>
@Nullable
Long trim(K key, long count);
/**
* Trims the stream to {@code count} elements.
*
* @param key the stream key.
* @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.
* @see <a href="https://redis.io/commands/xtrim">Redis Documentation: XTRIM</a>
*/
@Nullable
Long trim(K key, long count, boolean approximateTrimming);
/**
* Get the {@link HashMapper} for a specific type.
*