/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ZParams;
import java.util.Set;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.ClusterSlotHashUtil;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanCursor;
import org.springframework.data.redis.core.ScanIteration;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.util.Assert;
/**
* @author Christoph Strobl
* @since 2.0
*/
class JedisClusterZSetCommands implements RedisZSetCommands {
private final JedisClusterConnection connection;
public JedisClusterZSetCommands(JedisClusterConnection connection) {
this.connection = connection;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zAdd(byte[], double, byte[])
*/
@Override
public Boolean zAdd(byte[] key, double score, byte[] value) {
try {
return JedisConverters.toBoolean(connection.getCluster().zadd(key, score, value));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zAdd(byte[], java.util.Set)
*/
@Override
public Long zAdd(byte[] key, Set<Tuple> tuples) {
// TODO: need to move the tuple conversion form jedisconnection.
throw new UnsupportedOperationException();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRem(byte[], byte[][])
*/
@Override
public Long zRem(byte[] key, byte[]... values) {
try {
return connection.getCluster().zrem(key, values);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zIncrBy(byte[], double, byte[])
*/
@Override
public Double zIncrBy(byte[] key, double increment, byte[] value) {
try {
return connection.getCluster().zincrby(key, increment, value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRank(byte[], byte[])
*/
@Override
public Long zRank(byte[] key, byte[] value) {
try {
return connection.getCluster().zrank(key, value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRank(byte[], byte[])
*/
@Override
public Long zRevRank(byte[] key, byte[] value) {
try {
return connection.getCluster().zrevrank(key, value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRange(byte[], long, long)
*/
@Override
public Set<byte[]> zRange(byte[] key, long begin, long end) {
try {
return connection.getCluster().zrange(key, begin, end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScoreWithScores(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<Tuple> zRangeByScoreWithScores(byte[] key, Range range, Limit limit) {
Assert.notNull(range, "Range cannot be null for ZRANGEBYSCOREWITHSCORES.");
byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES);
try {
if (limit != null) {
return JedisConverters.toTupleSet(
connection.getCluster().zrangeByScoreWithScores(key, min, max, limit.getOffset(), limit.getCount()));
}
return JedisConverters.toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByScore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<byte[]> zRevRangeByScore(byte[] key, Range range, Limit limit) {
Assert.notNull(range, "Range cannot be null for ZREVRANGEBYSCORE.");
byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES);
try {
if (limit != null) {
return connection.getCluster().zrevrangeByScore(key, max, min, limit.getOffset(), limit.getCount());
}
return connection.getCluster().zrevrangeByScore(key, max, min);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByScoreWithScores(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<Tuple> zRevRangeByScoreWithScores(byte[] key, Range range, Limit limit) {
Assert.notNull(range, "Range cannot be null for ZREVRANGEBYSCOREWITHSCORES.");
byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES);
try {
if (limit != null) {
return JedisConverters.toTupleSet(
connection.getCluster().zrevrangeByScoreWithScores(key, max, min, limit.getOffset(), limit.getCount()));
}
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long zCount(byte[] key, Range range) {
Assert.notNull(range, "Range cannot be null for ZCOUNT.");
byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES);
try {
return connection.getCluster().zcount(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRemRangeByScore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long zRemRangeByScore(byte[] key, Range range) {
Assert.notNull(range, "Range cannot be null for ZREMRANGEBYSCORE.");
byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES);
try {
return connection.getCluster().zremrangeByScore(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<byte[]> zRangeByScore(byte[] key, Range range, Limit limit) {
Assert.notNull(range, "Range cannot be null for ZRANGEBYSCORE.");
byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES);
try {
if (limit != null) {
return connection.getCluster().zrangeByScore(key, min, max, limit.getOffset(), limit.getCount());
}
return connection.getCluster().zrangeByScore(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByLex(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<byte[]> zRangeByLex(byte[] key, Range range, Limit limit) {
Assert.notNull(range, "Range cannot be null for ZRANGEBYLEX.");
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.toBytes("-"));
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.toBytes("+"));
try {
if (limit != null) {
return connection.getCluster().zrangeByLex(key, min, max, limit.getOffset(), limit.getCount());
}
return connection.getCluster().zrangeByLex(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeWithScores(byte[], long, long)
*/
@Override
public Set<Tuple> zRangeWithScores(byte[] key, long begin, long end) {
try {
return JedisConverters.toTupleSet(connection.getCluster().zrangeWithScores(key, begin, end));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScore(byte[], double, double)
*/
@Override
public Set<byte[]> zRangeByScore(byte[] key, double min, double max) {
try {
return connection.getCluster().zrangeByScore(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScoreWithScores(byte[], double, double)
*/
@Override
public Set<Tuple> zRangeByScoreWithScores(byte[] key, double min, double max) {
try {
return JedisConverters.toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScore(byte[], double, double, long, long)
*/
@Override
public Set<byte[]> zRangeByScore(byte[] key, double min, double max, long offset, long count) {
if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Count/Offset cannot exceed Integer.MAX_VALUE!");
}
try {
return connection.getCluster().zrangeByScore(key, min, max, Long.valueOf(offset).intValue(),
Long.valueOf(count).intValue());
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScoreWithScores(byte[], double, double, long, long)
*/
@Override
public Set<Tuple> zRangeByScoreWithScores(byte[] key, double min, double max, long offset, long count) {
if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Count/Offset cannot exceed Integer.MAX_VALUE!");
}
try {
return JedisConverters.toTupleSet(connection.getCluster().zrangeByScoreWithScores(key, min, max,
Long.valueOf(offset).intValue(), Long.valueOf(count).intValue()));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRange(byte[], long, long)
*/
@Override
public Set<byte[]> zRevRange(byte[] key, long begin, long end) {
try {
return connection.getCluster().zrevrange(key, begin, end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeWithScores(byte[], long, long)
*/
@Override
public Set<Tuple> zRevRangeWithScores(byte[] key, long begin, long end) {
try {
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeWithScores(key, begin, end));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByScore(byte[], double, double)
*/
@Override
public Set<byte[]> zRevRangeByScore(byte[] key, double min, double max) {
try {
return connection.getCluster().zrevrangeByScore(key, max, min);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByScoreWithScores(byte[], double, double)
*/
@Override
public Set<Tuple> zRevRangeByScoreWithScores(byte[] key, double min, double max) {
try {
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByScore(byte[], double, double, long, long)
*/
@Override
public Set<byte[]> zRevRangeByScore(byte[] key, double min, double max, long offset, long count) {
if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Count/Offset cannot exceed Integer.MAX_VALUE!");
}
try {
return connection.getCluster().zrevrangeByScore(key, max, min, Long.valueOf(offset).intValue(),
Long.valueOf(count).intValue());
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByScoreWithScores(byte[], double, double, long, long)
*/
@Override
public Set<Tuple> zRevRangeByScoreWithScores(byte[] key, double min, double max, long offset, long count) {
if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Count/Offset cannot exceed Integer.MAX_VALUE!");
}
try {
return JedisConverters.toTupleSet(connection.getCluster().zrevrangeByScoreWithScores(key, max, min,
Long.valueOf(offset).intValue(), Long.valueOf(count).intValue()));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zCount(byte[], double, double)
*/
@Override
public Long zCount(byte[] key, double min, double max) {
try {
return connection.getCluster().zcount(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zCard(byte[])
*/
@Override
public Long zCard(byte[] key) {
try {
return connection.getCluster().zcard(key);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zScore(byte[], byte[])
*/
@Override
public Double zScore(byte[] key, byte[] value) {
try {
return connection.getCluster().zscore(key, value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRemRange(byte[], long, long)
*/
@Override
public Long zRemRange(byte[] key, long begin, long end) {
try {
return connection.getCluster().zremrangeByRank(key, begin, end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRemRangeByScore(byte[], double, double)
*/
@Override
public Long zRemRangeByScore(byte[] key, double min, double max) {
try {
return connection.getCluster().zremrangeByScore(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], byte[][])
*/
@Override
public Long zUnionStore(byte[] destKey, byte[]... sets) {
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
if (ClusterSlotHashUtil.isSameSlotForAllKeys(allKeys)) {
try {
return connection.getCluster().zunionstore(destKey, sets);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
throw new InvalidDataAccessApiUsageException("ZUNIONSTORE can only be executed when all keys map to the same slot");
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zUnionStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
if (ClusterSlotHashUtil.isSameSlotForAllKeys(allKeys)) {
ZParams zparams = new ZParams().weights(weights).aggregate(ZParams.Aggregate.valueOf(aggregate.name()));
try {
return connection.getCluster().zunionstore(destKey, zparams, sets);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
throw new InvalidDataAccessApiUsageException("ZUNIONSTORE can only be executed when all keys map to the same slot");
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], byte[][])
*/
@Override
public Long zInterStore(byte[] destKey, byte[]... sets) {
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
if (ClusterSlotHashUtil.isSameSlotForAllKeys(allKeys)) {
try {
return connection.getCluster().zinterstore(destKey, sets);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
throw new InvalidDataAccessApiUsageException("ZINTERSTORE can only be executed when all keys map to the same slot");
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zInterStore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, int[], byte[][])
*/
@Override
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
byte[][] allKeys = ByteUtils.mergeArrays(destKey, sets);
if (ClusterSlotHashUtil.isSameSlotForAllKeys(allKeys)) {
ZParams zparams = new ZParams().weights(weights).aggregate(ZParams.Aggregate.valueOf(aggregate.name()));
try {
return connection.getCluster().zinterstore(destKey, zparams, sets);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
throw new IllegalArgumentException("ZINTERSTORE can only be executed when all keys map to the same slot");
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zScan(byte[], org.springframework.data.redis.core.ScanOptions)
*/
@Override
public Cursor<Tuple> zScan(final byte[] key, final ScanOptions options) {
return new ScanCursor<Tuple>(options) {
@Override
protected ScanIteration<Tuple> doScan(long cursorId, ScanOptions options) {
ScanParams params = JedisConverters.toScanParams(options);
redis.clients.jedis.ScanResult<redis.clients.jedis.Tuple> result = connection.getCluster().zscan(key,
JedisConverters.toBytes(cursorId), params);
return new ScanIteration<>(Long.valueOf(result.getStringCursor()),
JedisConverters.tuplesToTuples().convert(result.getResult()));
}
}.open();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScore(byte[], java.lang.String, java.lang.String)
*/
@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {
try {
return connection.getCluster().zrangeByScore(key, JedisConverters.toBytes(min), JedisConverters.toBytes(max));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeByScore(byte[], java.lang.String, java.lang.String, long, long)
*/
@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {
if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Count/Offset cannot exceed Integer.MAX_VALUE!");
}
try {
return connection.getCluster().zrangeByScore(key, JedisConverters.toBytes(min), JedisConverters.toBytes(max),
Long.valueOf(offset).intValue(), Long.valueOf(count).intValue());
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
private DataAccessException convertJedisAccessException(Exception ex) {
return connection.convertJedisAccessException(ex);
}
}