DATAREDIS-716 - Polishing.

Add OBJECT command support for Jedis Cluster.

Original pull request: #337.
This commit is contained in:
Mark Paluch
2018-05-02 15:51:17 +02:00
parent 70a3e5dbe7
commit 0955a4fea6
4 changed files with 121 additions and 3 deletions

View File

@@ -24,6 +24,7 @@ import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.beans.factory.DisposableBean;
@@ -493,6 +494,22 @@ public class ClusterCommandExecutor implements DisposableBean {
public byte[] getKey() {
return key.getArray();
}
/**
* Apply the {@link Function mapper function} to the value and return the mapped value.
*
* @param mapper must not be {@literal null}.
* @param <U> type of the mapped value.
* @return the mapped value.
* @since 2.1
*/
@Nullable
public <U> U mapValue(Function<? super T, ? extends U> mapper) {
Assert.notNull(mapper, "Mapper function must not be null!");
return mapper.apply(getValue());
}
}
/**

View File

@@ -568,7 +568,13 @@ class JedisClusterKeyCommands implements RedisKeyCommands {
@Nullable
@Override
public ValueEncoding encodingOf(byte[] key) {
throw new UnsupportedOperationException("Jedis does not support OBJECT ENCODING in cluster!");
Assert.notNull(key, "Key must not be null!");
return connection.getClusterCommandExecutor()
.executeCommandOnSingleNode((JedisClusterCommandCallback<byte[]>) client -> client.objectEncoding(key),
connection.clusterGetNodeForKey(key))
.mapValue(JedisConverters::toEncoding);
}
/*
@@ -578,7 +584,13 @@ class JedisClusterKeyCommands implements RedisKeyCommands {
@Nullable
@Override
public Duration idletime(byte[] key) {
throw new UnsupportedOperationException("Jedis does not support OBJECT IDLETIME in cluster!");
Assert.notNull(key, "Key must not be null!");
return connection.getClusterCommandExecutor()
.executeCommandOnSingleNode((JedisClusterCommandCallback<Long>) client -> client.objectIdletime(key),
connection.clusterGetNodeForKey(key))
.mapValue(Converters::secondsToDuration);
}
/*
@@ -588,7 +600,14 @@ class JedisClusterKeyCommands implements RedisKeyCommands {
@Nullable
@Override
public Long refcount(byte[] key) {
throw new UnsupportedOperationException("Jedis does not support OBJECT REFCOUNT in cluster!");
Assert.notNull(key, "Key must not be null!");
return connection.getClusterCommandExecutor()
.executeCommandOnSingleNode((JedisClusterCommandCallback<Long>) client -> client.objectRefcount(key),
connection.clusterGetNodeForKey(key))
.getValue();
}
private DataAccessException convertJedisAccessException(Exception ex) {