add pipeline/multi-exec/NPE checks
REDIS-100 additionally fix the return value for BoundHashOperations from primitive to obj
This commit is contained in:
@@ -29,7 +29,7 @@ public interface BoundHashOperations<H, HK, HV> extends BoundKeyOperations<H> {
|
||||
|
||||
RedisOperations<H, ?> getOperations();
|
||||
|
||||
boolean hasKey(Object key);
|
||||
Boolean hasKey(Object key);
|
||||
|
||||
Long increment(HK key, long delta);
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultBoundKeyOperations<H>
|
||||
}
|
||||
|
||||
|
||||
public boolean hasKey(Object key) {
|
||||
public Boolean hasKey(Object key) {
|
||||
return ops.hasKey(getKey(), key);
|
||||
}
|
||||
|
||||
|
||||
@@ -142,4 +142,10 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,19 +120,19 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
|
||||
listOps.trim(0, maxSize - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return new DefaultRedisListIterator(content().iterator());
|
||||
List<E> 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<E> extends AbstractRedisCollection<E> implements R
|
||||
Long result = listOps.remove(1, o);
|
||||
return (result != null && result.longValue() > 0);
|
||||
}
|
||||
|
||||
|
||||
public void add(int index, E element) {
|
||||
if (index == 0) {
|
||||
|
||||
@@ -89,30 +89,29 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
|
||||
public Long increment(K key, long delta) {
|
||||
return hashOps.increment(key, delta);
|
||||
}
|
||||
|
||||
|
||||
public RedisOperations<String, ?> 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<java.util.Map.Entry<K, V>> entrySet() {
|
||||
Set<K> keySet = keySet();
|
||||
checkResult(keySet);
|
||||
Collection<V> multiGet = hashOps.multiGet(keySet);
|
||||
|
||||
Iterator<K> keys = keySet.iterator();
|
||||
@@ -125,51 +124,44 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
|
||||
public V get(Object key) {
|
||||
return hashOps.get(key);
|
||||
}
|
||||
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
|
||||
public Set<K> 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<? extends K, ? extends V> 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<V> values() {
|
||||
return hashOps.values();
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
@@ -180,15 +172,12 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
|
||||
}
|
||||
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<K, V> implements RedisMap<K, V> {
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void removeFromRedisStorage(E item) {
|
||||
DefaultRedisSet.this.remove(item);
|
||||
}
|
||||
@@ -71,79 +71,81 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Set<E> diff(RedisSet<?> set) {
|
||||
return boundSetOps.diff(set.getKey());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Set<E> diff(Collection<? extends RedisSet<?>> sets) {
|
||||
return boundSetOps.diff(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public RedisSet<E> diffAndStore(RedisSet<?> set, String destKey) {
|
||||
boundSetOps.diffAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey) {
|
||||
boundSetOps.diffAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Set<E> intersect(RedisSet<?> set) {
|
||||
return boundSetOps.intersect(set.getKey());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Set<E> intersect(Collection<? extends RedisSet<?>> sets) {
|
||||
return boundSetOps.intersect(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey) {
|
||||
boundSetOps.intersectAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey) {
|
||||
boundSetOps.intersectAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Set<E> union(RedisSet<?> set) {
|
||||
return boundSetOps.union(set.getKey());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Set<E> union(Collection<? extends RedisSet<?>> sets) {
|
||||
return boundSetOps.union(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public RedisSet<E> unionAndStore(RedisSet<?> set, String destKey) {
|
||||
boundSetOps.unionAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey) {
|
||||
boundSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisSet<E>(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<E> extends AbstractRedisCollection<E> 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<E> iterator() {
|
||||
return new DefaultRedisSetIterator(boundSetOps.members().iterator());
|
||||
Set<E> 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;
|
||||
}
|
||||
|
||||
@@ -172,11 +172,15 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> 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<E> extends AbstractRedisCollection<E> implements R
|
||||
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return new DefaultRedisSortedSetIterator(boundZSetOps.range(0, -1).iterator());
|
||||
Set<E> 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<E> extends AbstractRedisCollection<E> implements R
|
||||
|
||||
|
||||
public E first() {
|
||||
Iterator<E> iterator = boundZSetOps.range(0, 0).iterator();
|
||||
Set<E> members = boundZSetOps.range(0, 0);
|
||||
checkResult(members);
|
||||
Iterator<E> iterator = members.iterator();
|
||||
|
||||
if (iterator.hasNext())
|
||||
return iterator.next();
|
||||
throw new NoSuchElementException();
|
||||
@@ -219,7 +232,9 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
|
||||
|
||||
|
||||
public E last() {
|
||||
Iterator<E> iterator = boundZSetOps.reverseRange(0, 0).iterator();
|
||||
Set<E> members = boundZSetOps.reverseRange(0, 0);
|
||||
checkResult(members);
|
||||
Iterator<E> iterator = members.iterator();
|
||||
if (iterator.hasNext())
|
||||
return iterator.next();
|
||||
throw new NoSuchElementException();
|
||||
|
||||
Reference in New Issue
Block a user