+ implement RedisSortedSet

+ move up more methods to Redis abstract collection
This commit is contained in:
Costin Leau
2010-11-09 16:12:13 +02:00
parent 6e98ecb1e9
commit 52061da2af
6 changed files with 76 additions and 112 deletions

View File

@@ -23,7 +23,8 @@ import java.util.Collection;
*
* @author Costin Leau
*/
public interface RedisCommands extends RedisTxCommands, RedisStringCommands, RedisListCommands, RedisSetCommands {
public interface RedisCommands extends RedisTxCommands, RedisStringCommands, RedisListCommands, RedisSetCommands,
RedisZSetCommands {
Boolean exists(String key);

View File

@@ -40,15 +40,42 @@ public abstract class AbstractRedisCollection extends AbstractCollection<String>
return key;
}
@Override
public boolean addAll(Collection<? extends String> c) {
boolean modified = false;
for (String string : c) {
modified |= add(string);
}
return modified;
}
public abstract boolean add(String e);
public abstract void clear();
public abstract boolean removeAll(Collection<?> c);
@Override
public boolean containsAll(Collection<?> c) {
boolean contains = true;
for (Object object : c) {
contains &= contains(object);
}
return contains;
}
public abstract boolean remove(Object o);
@Override
public boolean removeAll(Collection<?> c) {
boolean modified = false;
for (Object object : c) {
modified |= remove(object);
}
return modified;
}
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
}

View File

@@ -89,16 +89,6 @@ public class DefaultRedisList extends AbstractRedisCollection implements RedisLi
return (result != null && result.intValue() > 0);
}
@Override
public boolean removeAll(Collection<?> c) {
boolean modified = false;
for (Object object : c) {
modified |= remove(object);
}
return modified;
}
@Override
public void add(int index, String element) {
if (index == 0) {

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.datastore.redis.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
@@ -82,16 +81,6 @@ public class DefaultRedisSet extends AbstractRedisCollection implements RedisSet
return commands.sAdd(key, e);
}
@Override
public boolean addAll(Collection<? extends String> c) {
boolean modified = false;
for (String string : c) {
modified |= add(string);
}
return modified;
}
@Override
public void clear() {
// intersect the set with a non existing one
@@ -104,15 +93,6 @@ public class DefaultRedisSet extends AbstractRedisCollection implements RedisSet
return commands.sIsMember(key, o.toString());
}
@Override
public boolean containsAll(Collection<?> c) {
boolean contains = true;
for (Object object : c) {
contains &= contains(object);
}
return contains;
}
@Override
public Iterator<String> iterator() {
return new DefaultRedisSetIterator(commands.sMembers(key).iterator());
@@ -123,24 +103,16 @@ public class DefaultRedisSet extends AbstractRedisCollection implements RedisSet
return commands.sRem(key, o.toString());
}
@Override
public boolean removeAll(Collection<?> c) {
boolean modified = false;
for (Object object : c) {
modified |= remove(object);
}
return modified;
}
@Override
public int size() {
return commands.sCard(key);
}
private String[] extractKeys(RedisSet... sets) {
String[] keys = new String[sets.length];
String[] keys = new String[sets.length + 1];
keys[0] = key;
for (int i = 0; i < keys.length; i++) {
keys[i] = sets[i].getKey();
keys[i + 1] = sets[i].getKey();
}
return keys;

View File

@@ -15,10 +15,9 @@
*/
package org.springframework.datastore.redis.util;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import org.springframework.datastore.redis.connection.RedisCommands;
@@ -47,102 +46,67 @@ class DefaultRedisSortedSet extends AbstractRedisCollection implements RedisSort
}
@Override
public RedisSortedSet intersectAndStore(String destKey, RedisSet... sets) {
return null;
public RedisSortedSet intersectAndStore(String destKey, RedisSortedSet... sets) {
commands.zInterStore(destKey, extractKeys(sets));
return new DefaultRedisSortedSet(destKey, commands);
}
@Override
public List<String> range(int start, int end) {
return null;
public Set<String> range(int start, int end) {
return commands.zRange(key, start, end);
}
@Override
public List<String> rangeByScore(int start, int end) {
return null;
public Set<String> rangeByScore(double min, double max) {
return commands.zRangeByScore(key, min, max);
}
@Override
public RedisSortedSet trim(int start, int end) {
return null;
commands.zRemRange(key, start, end);
return this;
}
@Override
public RedisSortedSet trimByScore(int start, int end) {
return null;
public RedisSortedSet trimByScore(double min, double max) {
commands.zRemRangeByScore(key, min, max);
return this;
}
@Override
public RedisSortedSet unionAndStore(String destKey, RedisSet... sets) {
return null;
}
@Override
public String getKey() {
return null;
public RedisSortedSet unionAndStore(String destKey, RedisSortedSet... sets) {
commands.zUnionStore(destKey, extractKeys(sets));
return new DefaultRedisSortedSet(destKey, commands);
}
@Override
public boolean add(String e) {
return false;
}
@Override
public boolean addAll(Collection<? extends String> c) {
return false;
return commands.zAdd(key, 0, e);
}
@Override
public void clear() {
commands.zRemRange(key, 0, -1);
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean isEmpty() {
return false;
return (commands.zRank(key, o.toString()) != null);
}
@Override
public Iterator<String> iterator() {
return null;
return new DefaultRedisSortedSetIterator(commands.zRange(key, 0, -1).iterator());
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
return commands.zRem(key, o.toString());
}
@Override
public int size() {
return 0;
}
@Override
public Object[] toArray() {
return null;
}
@Override
public <T> T[] toArray(T[] a) {
return null;
return commands.zCard(key);
}
@Override
@@ -152,26 +116,36 @@ class DefaultRedisSortedSet extends AbstractRedisCollection implements RedisSort
@Override
public String first() {
return null;
return commands.zRange(key, 0, 0).iterator().next();
}
@Override
public SortedSet<String> headSet(String toElement) {
return null;
throw new UnsupportedOperationException();
}
@Override
public String last() {
return null;
return commands.zRevRange(key, 0, 0).iterator().next();
}
@Override
public SortedSet<String> subSet(String fromElement, String toElement) {
return null;
throw new UnsupportedOperationException();
}
@Override
public SortedSet<String> tailSet(String fromElement) {
return null;
throw new UnsupportedOperationException();
}
private String[] extractKeys(RedisSortedSet... sets) {
String[] keys = new String[sets.length + 1];
keys[0] = key;
for (int i = 0; i < keys.length; i++) {
keys[i + 1] = sets[i].getKey();
}
return keys;
}
}

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.datastore.redis.util;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
/**
@@ -26,15 +26,15 @@ import java.util.SortedSet;
*/
public interface RedisSortedSet extends RedisCollection, SortedSet<String> {
RedisSortedSet intersectAndStore(String destKey, RedisSet... sets);
RedisSortedSet intersectAndStore(String destKey, RedisSortedSet... sets);
RedisSortedSet unionAndStore(String destKey, RedisSet... sets);
RedisSortedSet unionAndStore(String destKey, RedisSortedSet... sets);
List<String> range(int start, int end);
Set<String> range(int start, int end);
List<String> rangeByScore(int start, int end);
Set<String> rangeByScore(double min, double max);
RedisSortedSet trim(int start, int end);
RedisSortedSet trimByScore(int start, int end);
RedisSortedSet trimByScore(double min, double max);
}