diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/DefaultRedisSortedSet.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/DefaultRedisZSet.java similarity index 52% rename from spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/DefaultRedisSortedSet.java rename to spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/DefaultRedisZSet.java index c975d82f4..868529fce 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/DefaultRedisSortedSet.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/DefaultRedisZSet.java @@ -15,23 +15,23 @@ */ package org.springframework.data.keyvalue.redis.util; -import java.util.Comparator; import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.Set; -import java.util.SortedSet; import org.springframework.data.keyvalue.redis.core.BoundZSetOperations; import org.springframework.data.keyvalue.redis.core.RedisOperations; /** - * Default implementation for {@link RedisSortedSet}. + * Default implementation for {@link RedisZSet}. * * @author Costin Leau */ -class DefaultRedisSortedSet extends AbstractRedisCollection implements RedisSortedSet { +class DefaultRedisZSet extends AbstractRedisCollection implements RedisZSet { private final BoundZSetOperations boundZSetOps; - + private double defaultScore = 1; + private class DefaultRedisSortedSetIterator extends RedisIterator { public DefaultRedisSortedSetIterator(Iterator delegate) { @@ -40,31 +40,59 @@ class DefaultRedisSortedSet extends AbstractRedisCollection implements Red @Override protected void removeFromRedisStorage(E item) { - DefaultRedisSortedSet.this.remove(item); + DefaultRedisZSet.this.remove(item); } } + /** + * Constructs a new DefaultRedisZSet instance with a default score of '1'. + * + * @param key + * @param operations + */ + public DefaultRedisZSet(String key, RedisOperations operations) { + this(key, operations, 1); + } + /** * Constructs a new DefaultRedisSortedSet instance. * * @param key * @param operations + * @param defaultScore */ - public DefaultRedisSortedSet(String key, RedisOperations operations) { + public DefaultRedisZSet(String key, RedisOperations operations, double defaultScore) { super(key, operations); boundZSetOps = operations.forZSet(key); + this.defaultScore = defaultScore; } - public DefaultRedisSortedSet(BoundZSetOperations boundOps) { + /** + * Constructs a new DefaultRedisZSet instance with a default score of '1'. + * + * @param boundOps + */ + public DefaultRedisZSet(BoundZSetOperations boundOps) { + this(boundOps, 1); + } + + /** + * Constructs a new DefaultRedisZSet instance. + * + * @param boundOps + * @param defaultScore + */ + public DefaultRedisZSet(BoundZSetOperations boundOps, double defaultScore) { super(boundOps.getKey(), boundOps.getOperations()); this.boundZSetOps = boundOps; + this.defaultScore = defaultScore; } @Override - public RedisSortedSet intersectAndStore(String destKey, RedisSortedSet... sets) { + public RedisZSet intersectAndStore(String destKey, RedisZSet... sets) { boundZSetOps.intersectAndStore(destKey, extractKeys(sets)); - return new DefaultRedisSortedSet(boundZSetOps.getOperations().forZSet(destKey)); + return new DefaultRedisZSet(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore()); } @Override @@ -72,32 +100,42 @@ class DefaultRedisSortedSet extends AbstractRedisCollection implements Red return boundZSetOps.range(start, end); } + @Override + public Set reverseRange(int start, int end) { + return boundZSetOps.reverseRange(start, end); + } + @Override public Set rangeByScore(double min, double max) { return boundZSetOps.rangeByScore(min, max); } @Override - public RedisSortedSet remove(int start, int end) { + public RedisZSet remove(int start, int end) { boundZSetOps.removeRange(start, end); return this; } @Override - public RedisSortedSet removeByScore(double min, double max) { + public RedisZSet removeByScore(double min, double max) { boundZSetOps.removeRangeByScore(min, max); return this; } @Override - public RedisSortedSet unionAndStore(String destKey, RedisSortedSet... sets) { + public RedisZSet unionAndStore(String destKey, RedisZSet... sets) { boundZSetOps.unionAndStore(destKey, extractKeys(sets)); - return new DefaultRedisSortedSet(boundZSetOps.getOperations().forZSet(destKey)); + return new DefaultRedisZSet(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore()); } @Override public boolean add(E e) { - return boundZSetOps.add(e, 0); + return add(e, getDefaultScore()); + } + + @Override + public boolean add(E e, double score) { + return boundZSetOps.add(e, score); } @Override @@ -126,36 +164,42 @@ class DefaultRedisSortedSet extends AbstractRedisCollection implements Red } @Override - public Comparator comparator() { - return null; + public Double getDefaultScore() { + return defaultScore; } @Override public E first() { - return boundZSetOps.range(0, 0).iterator().next(); - } - - @Override - public SortedSet headSet(E toElement) { - throw new UnsupportedOperationException(); + Iterator iterator = boundZSetOps.range(0, 0).iterator(); + if (iterator.hasNext()) + return iterator.next(); + throw new NoSuchElementException(); } @Override public E last() { - return boundZSetOps.reverseRange(0, 0).iterator().next(); + Iterator iterator = boundZSetOps.reverseRange(0, 0).iterator(); + if (iterator.hasNext()) + return iterator.next(); + throw new NoSuchElementException(); } @Override - public SortedSet subSet(E fromElement, E toElement) { - throw new UnsupportedOperationException(); + public Integer rank(Object o) { + return boundZSetOps.rank(o); } @Override - public SortedSet tailSet(E fromElement) { - throw new UnsupportedOperationException(); + public Integer reverseRank(Object o) { + return boundZSetOps.reverseRank(o); } - private String[] extractKeys(RedisSortedSet... sets) { + @Override + public Double score(Object o) { + return boundZSetOps.score(o); + } + + private String[] extractKeys(RedisZSet... sets) { String[] keys = new String[sets.length + 1]; keys[0] = key; for (int i = 0; i < keys.length; i++) { diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/RedisSortedSet.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/RedisSortedSet.java deleted file mode 100644 index 7875ceec4..000000000 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/RedisSortedSet.java +++ /dev/null @@ -1,40 +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.util; - -import java.util.Set; -import java.util.SortedSet; - -/** - * Redis extension for the {@link SortedSet} contract. Supports {@link SortedSet} specific - * operations backed by Redis operations. - * - * @author Costin Leau - */ -public interface RedisSortedSet extends RedisStore, SortedSet { - - RedisSortedSet intersectAndStore(String destKey, RedisSortedSet... sets); - - RedisSortedSet unionAndStore(String destKey, RedisSortedSet... sets); - - Set range(int start, int end); - - Set rangeByScore(double min, double max); - - RedisSortedSet remove(int start, int end); - - RedisSortedSet removeByScore(double min, double max); -} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/RedisZSet.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/RedisZSet.java new file mode 100644 index 000000000..ea18abd5b --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/util/RedisZSet.java @@ -0,0 +1,114 @@ +/* + * 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.util; + +import java.util.Comparator; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.SortedSet; + +/** + * Redis ZSet contract. Acts as a {@link SortedSet} based on the given priorities. Since using a {@link Comparator} + * does not apply, a ZSet implements the {@link SortedSet} methods where applicable. + * + * @author Costin Leau + */ +public interface RedisZSet extends RedisStore, Set { + + RedisZSet intersectAndStore(String destKey, RedisZSet... sets); + + RedisZSet unionAndStore(String destKey, RedisZSet... sets); + + Set range(int start, int end); + + Set reverseRange(int start, int end); + + Set rangeByScore(double min, double max); + + RedisZSet remove(int start, int end); + + RedisZSet removeByScore(double min, double max); + + /** + * Adds an element to the set with the given score, or updates the score if + * the element exists. + * + * @param e element to add + * @param score element score + * @return true if a new element was added, false otherwise (only the score has been updated) + */ + boolean add(E e, double score); + + /** + * Adds an element to the set with a default score. Equivalent to + * {@code add(e, getDefaultScore())}. + * + * + * The score value is implementation specific. + * + * {@inheritDoc} + */ + boolean add(E e); + + /** + * Returns the score of the given element. Returns null if the element is not contained by the set. + * + * @param o object + * @return the score associated with the given object + */ + Double score(Object o); + + /** + * Returns the rank (position) of the given element in the set, in ascending order. + * Returns null if the element is not contained by the set. + * + * @param o object + * @return rank of the given object + */ + Integer rank(Object o); + + /** + * Returns the rank (position) of the given element in the set, in descending order. + * Returns null if the element is not contained by the set. + * + * @param o object + * @return reverse rank of the given object + */ + Integer reverseRank(Object o); + + /** + * Returns the default score used by this set. + * + * @return the default score used by the implementation. + */ + Double getDefaultScore(); + + /** + * Returns the first (lowest) element currently in this sorted set. + * + * @return the first (lowest) element currently in this sorted set. + * @throws NoSuchElementException sorted set is empty. + */ + E first(); + + /** + * Returns the last (highest) element currently in this sorted set. + * + * @return the last (highest) element currently in this sorted set. + * @throws NoSuchElementException sorted set is empty. + */ + E last(); +} \ No newline at end of file