DATAREDIS-674 - Support zAdd with a Set of Tuples using Jedis Cluster connections.
Original pull request: #263.
This commit is contained in:
@@ -19,6 +19,7 @@ import redis.clients.jedis.ScanParams;
|
||||
import redis.clients.jedis.ZParams;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
@@ -33,6 +34,7 @@ import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Clement Ong
|
||||
* @since 2.0
|
||||
*/
|
||||
class JedisClusterZSetCommands implements RedisZSetCommands {
|
||||
@@ -64,8 +66,13 @@ class JedisClusterZSetCommands implements RedisZSetCommands {
|
||||
@Override
|
||||
public Long zAdd(byte[] key, Set<Tuple> tuples) {
|
||||
|
||||
// TODO: need to move the tuple conversion form jedisconnection.
|
||||
throw new UnsupportedOperationException();
|
||||
Map<byte[], Double> args = JedisConverters.zAddArgsConvertor(tuples);
|
||||
|
||||
try {
|
||||
return connection.getCluster().zadd(key, args);
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -28,6 +28,8 @@ import redis.clients.util.SafeEncoder;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -658,4 +660,33 @@ abstract public class JedisConverters extends Converters {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert tuples to map of bytes and double.
|
||||
* Bytes represents the value of the element and double is for the score.
|
||||
* @param tuples
|
||||
* @return
|
||||
*/
|
||||
public static Map<byte[], Double> zAddArgsConvertor(Set<Tuple> tuples) {
|
||||
|
||||
Map<byte[], Double> args = new LinkedHashMap<>(tuples.size(), 1);
|
||||
Set<Double> scores = new HashSet<>(tuples.size(), 1);
|
||||
|
||||
boolean isAtLeastJedis24 = JedisVersionUtil.atLeastJedis24();
|
||||
|
||||
for (Tuple tuple : tuples) {
|
||||
|
||||
if (!isAtLeastJedis24) {
|
||||
if (scores.contains(tuple.getScore())) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Bulk add of multiple elements with the same score is not supported. Add the elements individually.");
|
||||
}
|
||||
scores.add(tuple.getScore());
|
||||
}
|
||||
|
||||
args.put(tuple.getValue(), tuple.getScore());
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@ import redis.clients.jedis.ScanParams;
|
||||
import redis.clients.jedis.ScanResult;
|
||||
import redis.clients.jedis.ZParams;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -79,7 +77,7 @@ class JedisZSetCommands implements RedisZSetCommands {
|
||||
throw new UnsupportedOperationException("zAdd of multiple fields not supported " + "in pipeline or transaction");
|
||||
}
|
||||
|
||||
Map<byte[], Double> args = zAddArgs(tuples);
|
||||
Map<byte[], Double> args = JedisConverters.zAddArgsConvertor(tuples);
|
||||
try {
|
||||
return connection.getJedis().zadd(key, args);
|
||||
} catch (Exception ex) {
|
||||
@@ -808,29 +806,6 @@ class JedisZSetCommands implements RedisZSetCommands {
|
||||
}
|
||||
}
|
||||
|
||||
private Map<byte[], Double> zAddArgs(Set<Tuple> tuples) {
|
||||
|
||||
Map<byte[], Double> args = new LinkedHashMap<>(tuples.size(), 1);
|
||||
Set<Double> scores = new HashSet<>(tuples.size(), 1);
|
||||
|
||||
boolean isAtLeastJedis24 = JedisVersionUtil.atLeastJedis24();
|
||||
|
||||
for (Tuple tuple : tuples) {
|
||||
|
||||
if (!isAtLeastJedis24) {
|
||||
if (scores.contains(tuple.getScore())) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Bulk add of multiple elements with the same score is not supported. Add the elements individually.");
|
||||
}
|
||||
scores.add(tuple.getScore());
|
||||
}
|
||||
|
||||
args.put(tuple.getValue(), tuple.getScore());
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
private boolean isPipelined() {
|
||||
return connection.isPipelined();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user