Skip to content

DATAREDIS-693 - Add support for UNLINK. #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAREDIS-693-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ public Long del(byte[]... keys) {
return convertAndReturn(delegate.del(keys), identityConverter);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#unlink(byte[][])
*/
@Override
public Long unlink(byte[]... keys) {
return convertAndReturn(delegate.unlink(keys), identityConverter);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisTxCommands#discard()
Expand Down Expand Up @@ -1785,6 +1794,15 @@ public Long del(String... keys) {
return del(serializeMulti(keys));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#unlink(java.lang.String[])
*/
@Override
public Long unlink(String... keys) {
return unlink(serializeMulti(keys));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#echo(java.lang.String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ default Long del(byte[]... keys) {
return keyCommands().del(keys);
}

/** @deprecated in favor of {@link RedisConnection#keyCommands()}. */
@Override
@Deprecated
default Long unlink(byte[]... keys) {
return keyCommands().unlink(keys);
}

/** @deprecated in favor of {@link RedisConnection#keyCommands()}. */
@Override
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,60 @@ default Mono<Long> mDel(List<ByteBuffer> keys) {
*/
Flux<NumericResponse<List<ByteBuffer>, Long>> mDel(Publisher<List<ByteBuffer>> keys);

/**
* Unlink the {@code key} from the keyspace. Unlike with {@link #del(ByteBuffer)} the actual memory reclaiming here
* happens asynchronously.
*
* @param key must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
default Mono<Long> unlink(ByteBuffer key) {

Assert.notNull(key, "Keys must not be null!");

return unlink(Mono.just(key).map(KeyCommand::new)).next().map(NumericResponse::getOutput);
}

/**
* Unlink the {@code key} from the keyspace. Unlike with {@link #del(ByteBuffer)} the actual memory reclaiming here
* happens asynchronously.
*
* @param keys must not be {@literal null}.
* @return {@link Flux} of {@link NumericResponse} holding the {@literal key} removed along with the unlink result.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
Flux<NumericResponse<KeyCommand, Long>> unlink(Publisher<KeyCommand> keys);

/**
* Unlink the {@code keys} from the keyspace. Unlike with {@link #mDel(List)} the actual memory reclaiming here
* happens asynchronously.
*
* @param keys must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
default Mono<Long> mUnlink(List<ByteBuffer> keys) {

Assert.notNull(keys, "Keys must not be null!");

return mUnlink(Mono.just(keys)).next().map(NumericResponse::getOutput);
}

/**
* Unlink the {@code keys} from the keyspace. Unlike with {@link #mDel(Publisher)} the actual memory reclaiming here
* happens asynchronously.
*
* @param keys must not be {@literal null}.
* @return {@link Flux} of {@link NumericResponse} holding the {@literal key} removed along with the deletion result.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
Flux<NumericResponse<List<ByteBuffer>, Long>> mUnlink(Publisher<List<ByteBuffer>> keys);

/**
* {@code EXPIRE}/{@code PEXPIRE} command parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ default Boolean exists(byte[] key) {
@Nullable
Long del(byte[]... keys);

/**
* Unlink the {@code keys} from the keyspace. Unlike with {@link #del(byte[]...)} the actual memory reclaiming here
* happens asynchronously.
*
* @param keys must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
@Nullable
Long unlink(byte[]... keys);

/**
* Determine the type stored at {@code key}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ interface StringTuple extends Tuple {
*/
Long del(String... keys);

/**
* Unlink the {@code keys} from the keyspace. Unlike with {@link #del(String...)} the actual memory reclaiming here
* happens asynchronously.
*
* @param keys must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
@Nullable
Long unlink(String... keys);

/**
* Determine the type stored at {@code key}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ public Long del(byte[]... keys) {
.resultsAsList().size();
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#unlink(byte[][])
*/
@Nullable
@Override
public Long unlink(byte[]... keys) {

Assert.notNull(keys, "Keys must not be null!");

return connection.<Long> execute("UNLINK", Arrays.asList(keys), Collections.emptyList()).stream()
.mapToLong(val -> val).sum();
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#type(byte[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ public Long del(byte[]... keys) {
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#unlink(byte[][])
*/
@Nullable
@Override
public Long unlink(byte[]... keys) {

Assert.notNull(keys, "Keys must not be null!");

return Long.class.cast(connection.execute("UNLINK", keys));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#type(byte[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ public Long del(byte[]... keys) {
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#unlink(byte[][])
*/
@Override
public Long unlink(byte[]... keys) {

Assert.notNull(keys, "Keys must not be null!");

try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().unlink(keys)));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceResult(getAsyncConnection().unlink(keys)));
return null;
}
return getConnection().unlink(keys);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#type(byte[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.reactivestreams.Publisher;
import org.springframework.data.redis.connection.DataType;
Expand Down Expand Up @@ -186,11 +185,41 @@ public Flux<NumericResponse<List<ByteBuffer>, Long>> mDel(Publisher<List<ByteBuf

Assert.notEmpty(keys, "Keys must not be null!");

return cmd.del(keys.stream().collect(Collectors.toList()).toArray(new ByteBuffer[keys.size()]))
return cmd.del(keys.toArray(new ByteBuffer[keys.size()]))
.map((value) -> new NumericResponse<>(keys, value));
}));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveRedisConnection.ReactiveKeyCommands#unlink(org.reactivestreams.Publisher)
*/
@Override
public Flux<NumericResponse<KeyCommand, Long>> unlink(Publisher<KeyCommand> commands) {

return connection.execute(cmd -> Flux.from(commands).concatMap((command) -> {

Assert.notNull(command.getKey(), "Key must not be null!");

return cmd.unlink(command.getKey()).map((value) -> new NumericResponse<>(command, value));
}));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveRedisConnection.ReactiveKeyCommands#mUnlink(org.reactivestreams.Publisher)
*/
@Override
public Flux<NumericResponse<List<ByteBuffer>, Long>> mUnlink(Publisher<List<ByteBuffer>> keysCollection) {

return connection.execute(cmd -> Flux.from(keysCollection).concatMap((keys) -> {

Assert.notEmpty(keys, "Keys must not be null!");

return cmd.unlink(keys.toArray(new ByteBuffer[keys.size()])).map((value) -> new NumericResponse<>(keys, value));
}));
}

/* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveKeyCommands#expire(org.reactivestreams.Publisher)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ public interface ReactiveRedisOperations<K, V> {
*/
Mono<Long> delete(Publisher<K> keys);

/**
* Unlink the {@code key} from the keyspace. Unlike with {@link #delete(Object[])} the actual memory reclaiming here
* happens asynchronously.
*
* @param key must not be {@literal null}.
* @return The number of keys that were removed. {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
Mono<Long> unlink(K... key);

/**
* Unlink the {@code keys} from the keyspace. Unlike with {@link #delete(Publisher)} the actual memory reclaiming here
* happens asynchronously.
*
* @param keys must not be {@literal null}.
* @return The number of keys that were removed. {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
Mono<Long> unlink(Publisher<K> keys);

/**
* Set time to live for given {@code key}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,40 @@ public Mono<Long> delete(Publisher<K> keys) {
.map(CommandResponse::getOutput));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveRedisOperations#unlink(java.lang.Object[])
*/
@Override
@SafeVarargs
public final Mono<Long> unlink(K... keys) {

Assert.notNull(keys, "Keys must not be null!");
Assert.notEmpty(keys, "Keys must not be empty!");
Assert.noNullElements(keys, "Keys must not contain null elements!");

if (keys.length == 1) {
return createMono(connection -> connection.keyCommands().unlink(rawKey(keys[0])));
}

Mono<List<ByteBuffer>> listOfKeys = Flux.fromArray(keys).map(this::rawKey).collectList();
return createMono(connection -> listOfKeys.flatMap(rawKeys -> connection.keyCommands().mUnlink(rawKeys)));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveRedisOperations#unlink(org.reactivestreams.Publisher)
*/
@Override
public Mono<Long> unlink(Publisher<K> keys) {

Assert.notNull(keys, "Keys must not be null!");

return createMono(connection -> connection.keyCommands() //
.unlink(Flux.from(keys).map(this::rawKey).map(KeyCommand::new)) //
.map(CommandResponse::getOutput));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveRedisOperations#expire(java.lang.Object, java.time.Duration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSer
@Nullable
Long delete(Collection<K> keys);

/**
* Unlink the {@code key} from the keyspace. Unlike with {@link #delete(Object)} the actual memory reclaiming here
* happens asynchronously.
*
* @param key must not be {@literal null}.
* @return The number of keys that were removed. {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
@Nullable
Boolean unlink(K key);

/**
* Unlink the {@code keys} from the keyspace. Unlike with {@link #delete(Collection)} the actual memory reclaiming
* here happens asynchronously.
*
* @param keys must not be {@literal null}.
* @return The number of keys that were removed. {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/unlink">Redis Documentation: UNLINK</a>
* @since 2.1
*/
@Nullable
Long unlink(Collection<K> keys);

/**
* Determine the type stored at {@code key}.
*
Expand Down
Loading