From 54e3d9785343491fea52eb490e75a657155019ad Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 7 Dec 2010 13:31:11 +0200 Subject: [PATCH] + several API improvements - eliminated use of varags with generified types - eliminated primitives as return types --- .../redis/core/BoundHashOperations.java | 2 +- .../redis/core/BoundKeyOperations.java | 2 +- .../redis/core/BoundSetOperations.java | 13 +- .../redis/core/BoundZSetOperations.java | 5 +- .../core/DefaultBoundHashOperations.java | 2 +- .../redis/core/DefaultBoundSetOperations.java | 13 +- .../core/DefaultBoundZSetOperations.java | 5 +- .../keyvalue/redis/core/HashOperations.java | 2 +- .../redis/core/KeyValueOperations.java | 126 ------------------ .../keyvalue/redis/core/RedisOperations.java | 2 +- .../keyvalue/redis/core/RedisTemplate.java | 104 +++++++-------- .../keyvalue/redis/core/SetOperations.java | 13 +- .../keyvalue/redis/core/ValueOperations.java | 3 +- .../keyvalue/redis/core/ZSetOperations.java | 5 +- .../support/collections/CollectionUtils.java | 11 ++ .../support/collections/DefaultRedisSet.java | 45 +++---- .../support/collections/DefaultRedisZSet.java | 19 +-- .../redis/support/collections/RedisSet.java | 13 +- .../redis/support/collections/RedisZSet.java | 5 +- 19 files changed, 129 insertions(+), 261 deletions(-) delete mode 100644 spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/KeyValueOperations.java diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundHashOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundHashOperations.java index 00aea0816..b4aac92f5 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundHashOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundHashOperations.java @@ -36,7 +36,7 @@ public interface BoundHashOperations extends KeyBound { void set(HK key, HV value); - Collection multiGet(Set keys); + Collection multiGet(Collection keys); void multiSet(Map m); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundKeyOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundKeyOperations.java index ec35e56d6..69de46eb9 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundKeyOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundKeyOperations.java @@ -41,7 +41,7 @@ public interface BoundKeyOperations extends KeyBound { Boolean expireAt(Date date); - long getExpire(); + Long getExpire(); void persist(); } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundSetOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundSetOperations.java index 337aa8232..e9274e453 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundSetOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundSetOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.redis.core; +import java.util.Collection; import java.util.Set; /** @@ -27,17 +28,17 @@ public interface BoundSetOperations extends KeyBound { RedisOperations getOperations(); - Set diff(K... keys); + Set diff(Collection keys); - void diffAndStore(K destKey, K... keys); + void diffAndStore(K destKey, Collection keys); - Set intersect(K... keys); + Set intersect(Collection keys); - void intersectAndStore(K destKey, K... keys); + void intersectAndStore(K destKey, Collection keys); - Set union(K... keys); + Set union(Collection keys); - void unionAndStore(K destKey, K... keys); + void unionAndStore(K destKey, Collection keys); Boolean add(V value); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundZSetOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundZSetOperations.java index a5d71a9b2..e3df62abf 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundZSetOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/BoundZSetOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.redis.core; +import java.util.Collection; import java.util.Set; @@ -28,7 +29,7 @@ public interface BoundZSetOperations extends KeyBound { RedisOperations getOperations(); - void intersectAndStore(K destKey, K... keys); + void intersectAndStore(K destKey, Collection keys); Set range(long start, long end); @@ -40,7 +41,7 @@ public interface BoundZSetOperations extends KeyBound { void removeRangeByScore(double min, double max); - void unionAndStore(K destKey, K... keys); + void unionAndStore(K destKey, Collection keys); Boolean add(V value, double score); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundHashOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundHashOperations.java index 3b626cf88..f7c86c197 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundHashOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundHashOperations.java @@ -50,7 +50,7 @@ class DefaultBoundHashOperations extends DefaultKeyBound implement } @Override - public Collection multiGet(Set hashKeys) { + public Collection multiGet(Collection hashKeys) { return ops.multiGet(getKey(), hashKeys); } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundSetOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundSetOperations.java index 48358634f..8e63b9eac 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundSetOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundSetOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.redis.core; +import java.util.Collection; import java.util.Set; /** @@ -45,12 +46,12 @@ class DefaultBoundSetOperations extends DefaultKeyBound implements Boun } @Override - public Set diff(K... keys) { + public Set diff(Collection keys) { return ops.diff(getKey(), keys); } @Override - public void diffAndStore(K destKey, K... keys) { + public void diffAndStore(K destKey, Collection keys) { ops.diffAndStore(getKey(), destKey, keys); } @@ -60,12 +61,12 @@ class DefaultBoundSetOperations extends DefaultKeyBound implements Boun } @Override - public Set intersect(K... keys) { + public Set intersect(Collection keys) { return ops.intersect(getKey(), keys); } @Override - public void intersectAndStore(K destKey, K... keys) { + public void intersectAndStore(K destKey, Collection keys) { ops.intersectAndStore(getKey(), destKey, keys); } @@ -90,12 +91,12 @@ class DefaultBoundSetOperations extends DefaultKeyBound implements Boun } @Override - public Set union(K... keys) { + public Set union(Collection keys) { return ops.union(getKey(), keys); } @Override - public void unionAndStore(K destKey, K... keys) { + public void unionAndStore(K destKey, Collection keys) { ops.unionAndStore(getKey(), destKey, keys); } } \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundZSetOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundZSetOperations.java index a0468dfe8..a71b92fc0 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundZSetOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/DefaultBoundZSetOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.redis.core; +import java.util.Collection; import java.util.Set; /** @@ -49,7 +50,7 @@ class DefaultBoundZSetOperations extends DefaultKeyBound implements Bou } @Override - public void intersectAndStore(K destKey, K... keys) { + public void intersectAndStore(K destKey, Collection keys) { ops.intersectAndStore(getKey(), destKey, keys); } @@ -104,7 +105,7 @@ class DefaultBoundZSetOperations extends DefaultKeyBound implements Bou } @Override - public void unionAndStore(K destKey, K... keys) { + public void unionAndStore(K destKey, Collection keys) { ops.unionAndStore(getKey(), destKey, keys); } } \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/HashOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/HashOperations.java index 7668648a3..ce9d7608c 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/HashOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/HashOperations.java @@ -32,7 +32,7 @@ public interface HashOperations { HV get(H key, Object hashKey); - Collection multiGet(H key, Set hashKeys); + Collection multiGet(H key, Collection hashKeys); Long increment(H key, HK hashKey, long delta); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/KeyValueOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/KeyValueOperations.java deleted file mode 100644 index a4ee00cc7..000000000 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/KeyValueOperations.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2010 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.keyvalue.redis.core; - -import java.util.List; -import java.util.Map; - -/** - * Key value operations with 'friendly' names instead of using command names for methods. - * Additional helper methods for working with keys and values - * - * @author Mark Pollack - * - */ -public interface KeyValueOperations { - - // Set and Set with expiry operations - - void set(String key, String value); - - void set(String key, String value, long expiryInMillis); - - void setAsBytes(String key, byte[] value); - - void setAsBytes(String key, byte[] value, long expiryInMillis); - - void convertAndSet(String key, Object value); - - void convertAndSet(String key, Object value, long expiryInMillis); - - // Get operations - - String get(String key); - - byte[] getAsBytes(String key); - - T getAndConvert(String key, Class requiredType); - - // Get and Set operations - - String getAndSet(String key, String value); - - byte[] getAndSetBytes(String key, byte[] value); - - T getAndSetObject(String key, T value, Class requiredType); - - // Multi-get operations - - List getValues(List keys); - - List getAndConvertValues(List keys, Class requiredType); - - - // Set if non-existent operations - - void setIfKeyNonExistent(String key, String value); - - void setIfKeyNonExistent(String key, byte[] value); - - void convertAndSetIfKeyNonExistent(String key, Object value); - - // Multiple key-value set - - void setMultiple(Map keysAndValues); - - void setMultipleAsBytes(Map keysAndValues); - - void convertAndSetMultiple(Map keysAndValues); - - // Multiple key-value set if non-existent - - void setMultipleIfKeysNonExistent(Map keysAndValues); - - void setMultipleAsBytesIfKeysNonExistent(Map keysAndValues); - - void convertAndSetMultipleIfKeysNonExistent(Map keysAndValues); - - - - // Append - - int append(String key, String value); - - - - // Increment - - int increment(String key); - - int incrementBy(String key, int value); - - // Decrement - - int decrement(String key); - - int decrementBy(String key, int value); - - - // Substring - - String getSubString(String key, int fromIndex, int toIndex); - - boolean containsKey(String key); - - boolean deleteKeys(String... keys); - - - - - - - -} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisOperations.java index 98fa83b8e..dcab5ffcd 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisOperations.java @@ -52,7 +52,7 @@ public interface RedisOperations { void persist(K key); - long getExpire(K key); + Long getExpire(K key); void watch(Collection keys); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java index 3182635a7..aef38cc57 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; +import org.springframework.core.convert.ConversionService; import org.springframework.dao.DataAccessException; import org.springframework.data.keyvalue.redis.connection.DataType; import org.springframework.data.keyvalue.redis.connection.RedisConnection; @@ -73,17 +74,6 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation afterPropertiesSet(); } - public void del(final String redisKey) { - execute(new RedisCallback() { - @Override - public Object doInRedis(RedisConnection connection) { - connection.del(keySerializer.serialize(redisKey)); - return null; - } - }); - } - - public T execute(RedisCallback action) { return execute(action, isExposeConnection()); } @@ -92,7 +82,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation return execute(action, exposeConnection, valueSerializer); } - public T execute(RedisCallback action, boolean exposeConnection, RedisSerializer returnSerializer) { + public T execute(RedisCallback action, boolean exposeConnection, RedisSerializer returnSerializer) { Assert.notNull(action, "Callback object must not be null"); RedisConnectionFactory factory = getConnectionFactory(); @@ -145,7 +135,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation * * @param serializer */ - public void setKeySerializer(RedisSerializer serializer) { + public void setKeySerializer(RedisSerializer serializer) { this.keySerializer = serializer; } @@ -154,7 +144,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation * * @param serializer */ - public void setValueSerializer(RedisSerializer serializer) { + public void setValueSerializer(RedisSerializer serializer) { this.valueSerializer = serializer; } @@ -163,7 +153,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation * * @param hashKeySerializer The hashKeySerializer to set. */ - public void setHashKeySerializer(RedisSerializer hashKeySerializer) { + public void setHashKeySerializer(RedisSerializer hashKeySerializer) { this.hashKeySerializer = hashKeySerializer; } @@ -172,7 +162,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation * * @param hashValueSerializer The hashValueSerializer to set. */ - public void setHashValueSerializer(RedisSerializer hashValueSerializer) { + public void setHashValueSerializer(RedisSerializer hashValueSerializer) { this.hashValueSerializer = hashValueSerializer; } @@ -216,24 +206,16 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } } + @SuppressWarnings("unchecked") private byte[] rawKey(Object key) { return (key != null ? keySerializer.serialize(key) : null); } + @SuppressWarnings("unchecked") private byte[] rawValue(T value) { return (value != null ? valueSerializer.serialize(value) : null); } - private byte[][] rawKeys(K... keys) { - final byte[][] rawKeys = new byte[keys.length][]; - - for (int i = 0; i < keys.length; i++) { - rawKeys[i] = rawKey(keys[i]); - } - - return rawKeys; - } - private byte[][] rawKeys(Collection keys) { final byte[][] rawKeys = new byte[keys.size()][]; @@ -245,10 +227,25 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation return rawKeys; } + private byte[][] rawKeys(K key, Collection keys) { + final byte[][] rawKeys = new byte[keys.size() + 1][]; + + + rawKeys[0] = rawKey(key); + int i = 1; + for (K k : keys) { + rawKeys[i++] = rawKey(k); + } + + return rawKeys; + } + + @SuppressWarnings("unchecked") private byte[] rawHashKey(HK value) { return (value != null ? hashKeySerializer.serialize(value) : null); } + @SuppressWarnings("unchecked") private byte[] rawHashValue(HV value) { return (value != null ? hashValueSerializer.serialize(value) : null); } @@ -303,7 +300,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation return (V) deserialize(value, valueSerializer); } - @SuppressWarnings("unchecked") + @SuppressWarnings( { "unchecked", "unused" }) private HK deserializeHashKey(byte[] value) { return (HK) deserialize(value, hashKeySerializer); } @@ -332,7 +329,6 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation this.key = key; } - @SuppressWarnings("unchecked") @Override public final V doInRedis(RedisConnection connection) { byte[] result = inRedis(rawKey(key), connection); @@ -442,7 +438,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation // @Override - public long getExpire(K key) { + public Long getExpire(K key) { final byte[] rawKey = rawKey(key); return execute(new RedisCallback() { @@ -569,6 +565,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation public V increment(K key, final long delta) { final byte[] rawKey = rawKey(key); // TODO add conversion service in here ? + ConversionService cs; return (V) execute(new RedisCallback() { @Override public Long doInRedis(RedisConnection connection) { @@ -590,7 +587,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public Collection multiGet(Set keys) { + public Collection multiGet(Collection keys) { if (keys.isEmpty()) { return Collections.emptyList(); } @@ -883,16 +880,6 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation // Set operations // - private K[] aggregateKeys(K key, K... keys) { - Object[] aggregate = new Object[keys.length + 1]; - aggregate[0] = key; - for (int i = 0; i < keys.length; i++) { - aggregate[i + 1] = keys[i]; - } - - return (K[]) aggregate; - } - @Override public BoundSetOperations forSet(K key) { return new DefaultBoundSetOperations(key, this); @@ -918,8 +905,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public Set diff(final K key, final K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public Set diff(final K key, final Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); Set rawValues = execute(new RedisCallback>() { @Override public Set doInRedis(RedisConnection connection) { @@ -931,8 +918,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public void diffAndStore(final K key, K destKey, final K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public void diffAndStore(final K key, K destKey, final Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); final byte[] rawDestKey = rawKey(destKey); execute(new RedisCallback() { @Override @@ -949,8 +936,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public Set intersect(K key, K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public Set intersect(K key, Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); Set rawValues = execute(new RedisCallback>() { @Override public Set doInRedis(RedisConnection connection) { @@ -962,8 +949,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public void intersectAndStore(K key, K destKey, K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public void intersectAndStore(K key, K destKey, Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); final byte[] rawDestKey = rawKey(destKey); execute(new RedisCallback() { @Override @@ -1023,8 +1010,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public Set union(K key, K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public Set union(K key, Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); Set rawValues = execute(new RedisCallback>() { @Override public Set doInRedis(RedisConnection connection) { @@ -1036,8 +1023,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public void unionAndStore(K key, K destKey, K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public void unionAndStore(K key, K destKey, Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); final byte[] rawDestKey = rawKey(destKey); execute(new RedisCallback() { @Override @@ -1084,8 +1071,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public void intersectAndStore(K key, K destKey, K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public void intersectAndStore(K key, K destKey, Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); final byte[] rawDestKey = rawKey(destKey); execute(new RedisCallback() { @Override @@ -1229,8 +1216,8 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } @Override - public void unionAndStore(K key, K destKey, K... keys) { - final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys)); + public void unionAndStore(K key, K destKey, Collection keys) { + final byte[][] rawKeys = rawKeys(key, keys); final byte[] rawDestKey = rawKey(destKey); execute(new RedisCallback() { @Override @@ -1306,6 +1293,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } + @SuppressWarnings("unchecked") @Override public Set keys(K key) { final byte[] rawKey = rawKey(key); @@ -1356,8 +1344,9 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } + @SuppressWarnings("unchecked") @Override - public Collection multiGet(K key, Set fields) { + public Collection multiGet(K key, Collection fields) { if (fields.isEmpty()) { return Collections.emptyList(); } @@ -1396,6 +1385,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation }, true); } + @SuppressWarnings("unchecked") @Override public List values(K key) { final byte[] rawKey = rawKey(key); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/SetOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/SetOperations.java index 71346b082..08c5a806c 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/SetOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/SetOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.redis.core; +import java.util.Collection; import java.util.Set; /** @@ -25,19 +26,19 @@ import java.util.Set; */ public interface SetOperations { - Set diff(K key, K... keys); + Set diff(K key, Collection keys); - void diffAndStore(K key, K destKey, K... keys); + void diffAndStore(K key, K destKey, Collection keys); RedisOperations getOperations(); - Set intersect(K key, K... keys); + Set intersect(K key, Collection keys); - void intersectAndStore(K key, K destKey, K... keys); + void intersectAndStore(K key, K destKey, Collection keys); - Set union(K key, K... keys); + Set union(K key, Collection keys); - void unionAndStore(K key, K destKey, K... keys); + void unionAndStore(K key, K destKey, Collection keys); Boolean add(K key, V value); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ValueOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ValueOperations.java index 5c26bfae1..73c18d556 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ValueOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ValueOperations.java @@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.redis.core; import java.util.Collection; import java.util.Map; -import java.util.Set; import java.util.concurrent.TimeUnit; /** @@ -41,7 +40,7 @@ public interface ValueOperations { V getAndSet(K key, V value); - Collection multiGet(Set keys); + Collection multiGet(Collection keys); V increment(K key, long delta); } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ZSetOperations.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ZSetOperations.java index 90afb22c5..fe212f78f 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ZSetOperations.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/ZSetOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.redis.core; +import java.util.Collection; import java.util.Set; /** @@ -25,7 +26,7 @@ import java.util.Set; */ public interface ZSetOperations { - void intersectAndStore(K key, K destKey, K... keys); + void intersectAndStore(K key, K destKey, Collection keys); Set range(K key, long start, long end); @@ -37,7 +38,7 @@ public interface ZSetOperations { void removeRangeByScore(K key, double min, double max); - void unionAndStore(K key, K destKey, K... keys); + void unionAndStore(K key, K destKey, Collection keys); Boolean add(K key, V value, double score); diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/CollectionUtils.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/CollectionUtils.java index cbe210164..99e95a56e 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/CollectionUtils.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/CollectionUtils.java @@ -15,6 +15,7 @@ */ package org.springframework.data.keyvalue.redis.support.collections; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -36,4 +37,14 @@ abstract class CollectionUtils { return (List) Arrays.asList(reverse); } + + static Collection extractKeys(Collection> stores) { + Collection keys = new ArrayList(stores.size()); + + for (RedisStore store : stores) { + keys.add(store.getKey()); + } + + return keys; + } } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisSet.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisSet.java index 074469437..1b0c6f331 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisSet.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisSet.java @@ -15,8 +15,11 @@ */ package org.springframework.data.keyvalue.redis.support.collections; +import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.Set; +import java.util.UUID; import org.springframework.data.keyvalue.redis.core.BoundSetOperations; import org.springframework.data.keyvalue.redis.core.RedisOperations; @@ -64,36 +67,36 @@ public class DefaultRedisSet extends AbstractRedisCollection implements Re } @Override - public Set diff(RedisSet... sets) { - return boundSetOps.diff(extractKeys(sets)); + public Set diff(Collection> sets) { + return boundSetOps.diff(CollectionUtils.extractKeys(sets)); } @Override - public RedisSet diffAndStore(String destKey, RedisSet... sets) { - boundSetOps.diffAndStore(destKey, extractKeys(sets)); - return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey)); + public RedisSet diffAndStore(String destKey, Collection> sets) { + boundSetOps.diffAndStore(destKey, CollectionUtils.extractKeys(sets)); + return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey)); } @Override - public Set intersect(RedisSet... sets) { - return boundSetOps.intersect(extractKeys(sets)); + public Set intersect(Collection> sets) { + return boundSetOps.intersect(CollectionUtils.extractKeys(sets)); } @Override - public RedisSet intersectAndStore(String destKey, RedisSet... sets) { - boundSetOps.intersectAndStore(destKey, extractKeys(sets)); - return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey)); + public RedisSet intersectAndStore(String destKey, Collection> sets) { + boundSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets)); + return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey)); } @Override - public Set union(RedisSet... sets) { - return boundSetOps.union(extractKeys(sets)); + public Set union(Collection> sets) { + return boundSetOps.union(CollectionUtils.extractKeys(sets)); } @Override - public RedisSet unionAndStore(String destKey, RedisSet... sets) { - boundSetOps.unionAndStore(destKey, extractKeys(sets)); - return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey)); + public RedisSet unionAndStore(String destKey, Collection> sets) { + boundSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets)); + return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey)); } @Override @@ -105,7 +108,8 @@ public class DefaultRedisSet extends AbstractRedisCollection implements Re public void clear() { // intersect the set with a non existing one // TODO: find a safer way to clean the set - boundSetOps.intersectAndStore(key, "NON-EXISTING"); + String randomKey = UUID.randomUUID().toString(); + boundSetOps.intersectAndStore(key, Collections.singleton(randomKey)); } @Override @@ -127,13 +131,4 @@ public class DefaultRedisSet extends AbstractRedisCollection implements Re public int size() { return boundSetOps.size().intValue(); } - - private String[] extractKeys(RedisSet... sets) { - String[] keys = new String[sets.length]; - for (int i = 0; i < keys.length; i++) { - keys[i] = sets[i].getKey(); - } - - return keys; - } } \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisZSet.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisZSet.java index 81a809e78..c2cf4f095 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisZSet.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/DefaultRedisZSet.java @@ -15,6 +15,7 @@ */ package org.springframework.data.keyvalue.redis.support.collections; +import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; @@ -90,8 +91,8 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R } @Override - public RedisZSet intersectAndStore(String destKey, RedisZSet... sets) { - boundZSetOps.intersectAndStore(destKey, extractKeys(sets)); + public RedisZSet intersectAndStore(String destKey, Collection> sets) { + boundZSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets)); return new DefaultRedisZSet(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore()); } @@ -123,8 +124,8 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R } @Override - public RedisZSet unionAndStore(String destKey, RedisZSet... sets) { - boundZSetOps.unionAndStore(destKey, extractKeys(sets)); + public RedisZSet unionAndStore(String destKey, Collection> sets) { + boundZSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets)); return new DefaultRedisZSet(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore()); } @@ -198,14 +199,4 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R public Double score(Object o) { return boundZSetOps.score(o); } - - private String[] extractKeys(RedisZSet... sets) { - String[] keys = new String[sets.length]; - keys[0] = key; - for (int i = 0; i < keys.length; i++) { - keys[i] = sets[i].getKey(); - } - - return keys; - } } \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisSet.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisSet.java index 1fdbc544c..21b13bb73 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisSet.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisSet.java @@ -15,6 +15,7 @@ */ package org.springframework.data.keyvalue.redis.support.collections; +import java.util.Collection; import java.util.Set; /** @@ -25,15 +26,15 @@ import java.util.Set; */ public interface RedisSet extends RedisStore, Set { - Set intersect(RedisSet... sets); + Set intersect(Collection> sets); - Set union(RedisSet... sets); + Set union(Collection> sets); - Set diff(RedisSet... sets); + Set diff(Collection> sets); - RedisSet intersectAndStore(String destKey, RedisSet... sets); + RedisSet intersectAndStore(String destKey, Collection> sets); - RedisSet unionAndStore(String destKey, RedisSet... sets); + RedisSet unionAndStore(String destKey, Collection> sets); - RedisSet diffAndStore(String destKey, RedisSet... sets); + RedisSet diffAndStore(String destKey, Collection> sets); } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisZSet.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisZSet.java index 5dbfc9e7e..8a67bd936 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisZSet.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/support/collections/RedisZSet.java @@ -15,6 +15,7 @@ */ package org.springframework.data.keyvalue.redis.support.collections; +import java.util.Collection; import java.util.Comparator; import java.util.NoSuchElementException; import java.util.Set; @@ -28,9 +29,9 @@ import java.util.SortedSet; */ public interface RedisZSet extends RedisStore, Set { - RedisZSet intersectAndStore(String destKey, RedisZSet... sets); + RedisZSet intersectAndStore(String destKey, Collection> sets); - RedisZSet unionAndStore(String destKey, RedisZSet... sets); + RedisZSet unionAndStore(String destKey, Collection> sets); Set range(long start, long end);