+ changed RedisSortedSet to RedisZSet
- removed SortedSet contract as it did not map correctly onto the ZSet semantics
This commit is contained in:
@@ -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<E> extends AbstractRedisCollection<E> implements RedisSortedSet<E> {
|
||||
class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
|
||||
|
||||
private final BoundZSetOperations<String, E> boundZSetOps;
|
||||
|
||||
private double defaultScore = 1;
|
||||
|
||||
private class DefaultRedisSortedSetIterator extends RedisIterator<E> {
|
||||
|
||||
public DefaultRedisSortedSetIterator(Iterator<E> delegate) {
|
||||
@@ -40,31 +40,59 @@ class DefaultRedisSortedSet<E> extends AbstractRedisCollection<E> implements Red
|
||||
|
||||
@Override
|
||||
protected void removeFromRedisStorage(E item) {
|
||||
DefaultRedisSortedSet.this.remove(item);
|
||||
DefaultRedisZSet.this.remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>DefaultRedisZSet</code> instance with a default score of '1'.
|
||||
*
|
||||
* @param key
|
||||
* @param operations
|
||||
*/
|
||||
public DefaultRedisZSet(String key, RedisOperations<String, E> operations) {
|
||||
this(key, operations, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>DefaultRedisSortedSet</code> instance.
|
||||
*
|
||||
* @param key
|
||||
* @param operations
|
||||
* @param defaultScore
|
||||
*/
|
||||
public DefaultRedisSortedSet(String key, RedisOperations<String, E> operations) {
|
||||
public DefaultRedisZSet(String key, RedisOperations<String, E> operations, double defaultScore) {
|
||||
super(key, operations);
|
||||
boundZSetOps = operations.forZSet(key);
|
||||
this.defaultScore = defaultScore;
|
||||
}
|
||||
|
||||
|
||||
public DefaultRedisSortedSet(BoundZSetOperations<String, E> boundOps) {
|
||||
/**
|
||||
* Constructs a new <code>DefaultRedisZSet</code> instance with a default score of '1'.
|
||||
*
|
||||
* @param boundOps
|
||||
*/
|
||||
public DefaultRedisZSet(BoundZSetOperations<String, E> boundOps) {
|
||||
this(boundOps, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>DefaultRedisZSet</code> instance.
|
||||
*
|
||||
* @param boundOps
|
||||
* @param defaultScore
|
||||
*/
|
||||
public DefaultRedisZSet(BoundZSetOperations<String, E> boundOps, double defaultScore) {
|
||||
super(boundOps.getKey(), boundOps.getOperations());
|
||||
this.boundZSetOps = boundOps;
|
||||
this.defaultScore = defaultScore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSortedSet<E> intersectAndStore(String destKey, RedisSortedSet<E>... sets) {
|
||||
public RedisZSet<E> intersectAndStore(String destKey, RedisZSet<E>... sets) {
|
||||
boundZSetOps.intersectAndStore(destKey, extractKeys(sets));
|
||||
return new DefaultRedisSortedSet<E>(boundZSetOps.getOperations().forZSet(destKey));
|
||||
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,32 +100,42 @@ class DefaultRedisSortedSet<E> extends AbstractRedisCollection<E> implements Red
|
||||
return boundZSetOps.range(start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> reverseRange(int start, int end) {
|
||||
return boundZSetOps.reverseRange(start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> rangeByScore(double min, double max) {
|
||||
return boundZSetOps.rangeByScore(min, max);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSortedSet<E> remove(int start, int end) {
|
||||
public RedisZSet<E> remove(int start, int end) {
|
||||
boundZSetOps.removeRange(start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSortedSet<E> removeByScore(double min, double max) {
|
||||
public RedisZSet<E> removeByScore(double min, double max) {
|
||||
boundZSetOps.removeRangeByScore(min, max);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSortedSet<E> unionAndStore(String destKey, RedisSortedSet<E>... sets) {
|
||||
public RedisZSet<E> unionAndStore(String destKey, RedisZSet<E>... sets) {
|
||||
boundZSetOps.unionAndStore(destKey, extractKeys(sets));
|
||||
return new DefaultRedisSortedSet<E>(boundZSetOps.getOperations().forZSet(destKey));
|
||||
return new DefaultRedisZSet<E>(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<E> extends AbstractRedisCollection<E> implements Red
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<? super E> comparator() {
|
||||
return null;
|
||||
public Double getDefaultScore() {
|
||||
return defaultScore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E first() {
|
||||
return boundZSetOps.range(0, 0).iterator().next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SortedSet<E> headSet(E toElement) {
|
||||
throw new UnsupportedOperationException();
|
||||
Iterator<E> 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<E> iterator = boundZSetOps.reverseRange(0, 0).iterator();
|
||||
if (iterator.hasNext())
|
||||
return iterator.next();
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SortedSet<E> subSet(E fromElement, E toElement) {
|
||||
throw new UnsupportedOperationException();
|
||||
public Integer rank(Object o) {
|
||||
return boundZSetOps.rank(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SortedSet<E> tailSet(E fromElement) {
|
||||
throw new UnsupportedOperationException();
|
||||
public Integer reverseRank(Object o) {
|
||||
return boundZSetOps.reverseRank(o);
|
||||
}
|
||||
|
||||
private String[] extractKeys(RedisSortedSet<E>... sets) {
|
||||
@Override
|
||||
public Double score(Object o) {
|
||||
return boundZSetOps.score(o);
|
||||
}
|
||||
|
||||
private String[] extractKeys(RedisZSet<E>... sets) {
|
||||
String[] keys = new String[sets.length + 1];
|
||||
keys[0] = key;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
@@ -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<E> extends RedisStore<String>, SortedSet<E> {
|
||||
|
||||
RedisSortedSet<E> intersectAndStore(String destKey, RedisSortedSet<E>... sets);
|
||||
|
||||
RedisSortedSet<E> unionAndStore(String destKey, RedisSortedSet<E>... sets);
|
||||
|
||||
Set<E> range(int start, int end);
|
||||
|
||||
Set<E> rangeByScore(double min, double max);
|
||||
|
||||
RedisSortedSet<E> remove(int start, int end);
|
||||
|
||||
RedisSortedSet<E> removeByScore(double min, double max);
|
||||
}
|
||||
@@ -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<E> extends RedisStore<String>, Set<E> {
|
||||
|
||||
RedisZSet<E> intersectAndStore(String destKey, RedisZSet<E>... sets);
|
||||
|
||||
RedisZSet<E> unionAndStore(String destKey, RedisZSet<E>... sets);
|
||||
|
||||
Set<E> range(int start, int end);
|
||||
|
||||
Set<E> reverseRange(int start, int end);
|
||||
|
||||
Set<E> rangeByScore(double min, double max);
|
||||
|
||||
RedisZSet<E> remove(int start, int end);
|
||||
|
||||
RedisZSet<E> 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();
|
||||
}
|
||||
Reference in New Issue
Block a user