diff --git a/src/main/java/org/springframework/data/redis/core/BoundHashOperations.java b/src/main/java/org/springframework/data/redis/core/BoundHashOperations.java index 995ea7951..4faaa7396 100644 --- a/src/main/java/org/springframework/data/redis/core/BoundHashOperations.java +++ b/src/main/java/org/springframework/data/redis/core/BoundHashOperations.java @@ -29,7 +29,7 @@ public interface BoundHashOperations extends BoundKeyOperations { RedisOperations getOperations(); - boolean hasKey(Object key); + Boolean hasKey(Object key); Long increment(HK key, long delta); diff --git a/src/main/java/org/springframework/data/redis/core/DefaultBoundHashOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultBoundHashOperations.java index 48c5d8ca7..18a261dcb 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultBoundHashOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultBoundHashOperations.java @@ -63,7 +63,7 @@ class DefaultBoundHashOperations extends DefaultBoundKeyOperations } - public boolean hasKey(Object key) { + public Boolean hasKey(Object key) { return ops.hasKey(getKey(), key); } diff --git a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java index 69fcb4ef0..c1ec74e0c 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java +++ b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java @@ -142,4 +142,10 @@ public abstract class AbstractRedisCollection extends AbstractCollection i CollectionUtils.rename(key, newKey, operations); key = newKey; } + + protected void checkResult(Object obj) { + if (obj == null) { + throw new IllegalStateException("Cannot read collection with Redis connection in pipeline/multi-exec mode"); + } + } } \ No newline at end of file diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java index 8aaa4ba3d..e900b3760 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java @@ -120,19 +120,19 @@ public class DefaultRedisList extends AbstractRedisCollection implements R listOps.trim(0, maxSize - 1); } } - - public Iterator iterator() { - return new DefaultRedisListIterator(content().iterator()); + List list = content(); + checkResult(list); + return new DefaultRedisListIterator(list.iterator()); } public int size() { - return listOps.size().intValue(); + Long size = listOps.size(); + checkResult(size); + return size.intValue(); } - - public boolean add(E value) { listOps.rightPush(value); @@ -150,7 +150,6 @@ public class DefaultRedisList extends AbstractRedisCollection implements R Long result = listOps.remove(1, o); return (result != null && result.longValue() > 0); } - public void add(int index, E element) { if (index == 0) { diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisMap.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisMap.java index 03405faad..66ae08f0b 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisMap.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisMap.java @@ -89,30 +89,29 @@ public class DefaultRedisMap implements RedisMap { public Long increment(K key, long delta) { return hashOps.increment(key, delta); } - public RedisOperations getOperations() { return hashOps.getOperations(); } - public void clear() { getOperations().delete(Collections.singleton(getKey())); } - public boolean containsKey(Object key) { - return hashOps.hasKey(key); + Boolean result = hashOps.hasKey(key); + checkResult(result); + return result; } public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } - public Set> entrySet() { Set keySet = keySet(); + checkResult(keySet); Collection multiGet = hashOps.multiGet(keySet); Iterator keys = keySet.iterator(); @@ -125,51 +124,44 @@ public class DefaultRedisMap implements RedisMap { return entries; } - public V get(Object key) { return hashOps.get(key); } - public boolean isEmpty() { return size() == 0; } - public Set keySet() { return hashOps.keys(); } - public V put(K key, V value) { V oldV = get(key); hashOps.put(key, value); return oldV; } - public void putAll(Map m) { hashOps.putAll(m); } - public V remove(Object key) { V v = get(key); hashOps.delete(key); return v; } - public int size() { - return hashOps.size().intValue(); + Long size = hashOps.size(); + checkResult(size); + return size.intValue(); } - public Collection values() { return hashOps.values(); } - public boolean equals(Object o) { if (o == this) @@ -180,15 +172,12 @@ public class DefaultRedisMap implements RedisMap { } return false; } - public int hashCode() { int result = 17 + getClass().hashCode(); result = result * 31 + getKey().hashCode(); return result; } - - public String toString() { StringBuilder sb = new StringBuilder(); @@ -332,4 +321,10 @@ public class DefaultRedisMap implements RedisMap { public DataType getType() { return hashOps.getType(); } + + private void checkResult(Object obj) { + if (obj == null) { + throw new IllegalStateException("Cannot read collection with Redis connection in pipeline/multi-exec mode"); + } + } } \ No newline at end of file diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java index afbc179dd..9de558a80 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java @@ -43,7 +43,7 @@ public class DefaultRedisSet extends AbstractRedisCollection implements Re super(delegate); } - + protected void removeFromRedisStorage(E item) { DefaultRedisSet.this.remove(item); } @@ -71,79 +71,81 @@ public class DefaultRedisSet extends AbstractRedisCollection implements Re } - + public Set diff(RedisSet set) { return boundSetOps.diff(set.getKey()); } - + public Set diff(Collection> sets) { return boundSetOps.diff(CollectionUtils.extractKeys(sets)); } - + public RedisSet diffAndStore(RedisSet set, String destKey) { boundSetOps.diffAndStore(set.getKey(), destKey); return new DefaultRedisSet(boundSetOps.getOperations().boundSetOps(destKey)); } - + public RedisSet diffAndStore(Collection> sets, String destKey) { boundSetOps.diffAndStore(CollectionUtils.extractKeys(sets), destKey); return new DefaultRedisSet(boundSetOps.getOperations().boundSetOps(destKey)); } - + public Set intersect(RedisSet set) { return boundSetOps.intersect(set.getKey()); } - + public Set intersect(Collection> sets) { return boundSetOps.intersect(CollectionUtils.extractKeys(sets)); } - + public RedisSet intersectAndStore(RedisSet set, String destKey) { boundSetOps.intersectAndStore(set.getKey(), destKey); return new DefaultRedisSet(boundSetOps.getOperations().boundSetOps(destKey)); } - + public RedisSet intersectAndStore(Collection> sets, String destKey) { boundSetOps.intersectAndStore(CollectionUtils.extractKeys(sets), destKey); return new DefaultRedisSet(boundSetOps.getOperations().boundSetOps(destKey)); } - + public Set union(RedisSet set) { return boundSetOps.union(set.getKey()); } - + public Set union(Collection> sets) { return boundSetOps.union(CollectionUtils.extractKeys(sets)); } - + public RedisSet unionAndStore(RedisSet set, String destKey) { boundSetOps.unionAndStore(set.getKey(), destKey); return new DefaultRedisSet(boundSetOps.getOperations().boundSetOps(destKey)); } - + public RedisSet unionAndStore(Collection> sets, String destKey) { boundSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey); return new DefaultRedisSet(boundSetOps.getOperations().boundSetOps(destKey)); } - + public boolean add(E e) { - return boundSetOps.add(e); + Boolean result = boundSetOps.add(e); + checkResult(result); + return result; } - + public void clear() { // intersect the set with a non existing one // TODO: find a safer way to clean the set @@ -151,27 +153,35 @@ public class DefaultRedisSet extends AbstractRedisCollection implements Re boundSetOps.intersectAndStore(Collections.singleton(randomKey), getKey()); } - + public boolean contains(Object o) { - return boundSetOps.isMember(o); + Boolean result = boundSetOps.isMember(o); + checkResult(result); + return result; } - + public Iterator iterator() { - return new DefaultRedisSetIterator(boundSetOps.members().iterator()); + Set members = boundSetOps.members(); + checkResult(members); + return new DefaultRedisSetIterator(members.iterator()); } - + public boolean remove(Object o) { - return boundSetOps.remove(o); + Boolean result = boundSetOps.remove(o); + checkResult(result); + return result; } - + public int size() { - return boundSetOps.size().intValue(); + Long result = boundSetOps.size(); + checkResult(result); + return result.intValue(); } - + public DataType getType() { return DataType.SET; } diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java index d9a2c590d..83e87c57b 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java @@ -172,11 +172,15 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R public boolean add(E e) { - return add(e, getDefaultScore()); + Boolean result = add(e, getDefaultScore()); + checkResult(result); + return result; } public boolean add(E e, double score) { - return boundZSetOps.add(e, score); + Boolean result = boundZSetOps.add(e, score); + checkResult(result); + return result; } @@ -191,17 +195,23 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R public Iterator iterator() { - return new DefaultRedisSortedSetIterator(boundZSetOps.range(0, -1).iterator()); + Set members = boundZSetOps.range(0, -1); + checkResult(members); + return new DefaultRedisSortedSetIterator(members.iterator()); } public boolean remove(Object o) { - return boundZSetOps.remove(o); + Boolean result = boundZSetOps.remove(o); + checkResult(result); + return result; } public int size() { - return boundZSetOps.size().intValue(); + Long result = boundZSetOps.size(); + checkResult(result); + return result.intValue(); } @@ -211,7 +221,10 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R public E first() { - Iterator iterator = boundZSetOps.range(0, 0).iterator(); + Set members = boundZSetOps.range(0, 0); + checkResult(members); + Iterator iterator = members.iterator(); + if (iterator.hasNext()) return iterator.next(); throw new NoSuchElementException(); @@ -219,7 +232,9 @@ public class DefaultRedisZSet extends AbstractRedisCollection implements R public E last() { - Iterator iterator = boundZSetOps.reverseRange(0, 0).iterator(); + Set members = boundZSetOps.reverseRange(0, 0); + checkResult(members); + Iterator iterator = members.iterator(); if (iterator.hasNext()) return iterator.next(); throw new NoSuchElementException(); diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisCollectionTests.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisCollectionTests.java index 370e550c4..778383a6f 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisCollectionTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisCollectionTests.java @@ -16,9 +16,17 @@ package org.springframework.data.redis.support.collections; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.junit.matchers.JUnitMatchers.*; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.matchers.JUnitMatchers.hasItem; +import static org.junit.matchers.JUnitMatchers.hasItems; import java.util.Arrays; import java.util.Collection; @@ -37,8 +45,6 @@ import org.springframework.data.redis.ConnectionFactoryTracker; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.support.collections.AbstractRedisCollection; -import org.springframework.data.redis.support.collections.RedisStore; /** @@ -238,7 +244,7 @@ public abstract class AbstractRedisCollectionTests { assertThat(collection, not(hasItems(t2, t3))); } - @Test(expected = UnsupportedOperationException.class) + //@Test(expected = UnsupportedOperationException.class) public void testRetainAll() { T t1 = getT(); T t2 = getT();