org.springframework.data.redis.support.collections.DefaultRedisZSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-redis Show documentation
Show all versions of spring-data-redis Show documentation
Spring Data module for Redis
/*
* Copyright 2011-2023 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
*
* https://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.redis.support.collections;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.Limit;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.ConvertingCursor;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
/**
* Default implementation for {@link RedisZSet}. Note that the collection support works only with normal,
* non-pipeline/multi-exec connections as it requires a reply to be sent right away.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
* @author Andrey Shlykov
*/
public class DefaultRedisZSet extends AbstractRedisCollection implements RedisZSet {
private final BoundZSetOperations boundZSetOps;
private final double defaultScore;
private class DefaultRedisSortedSetIterator extends RedisIterator {
public DefaultRedisSortedSetIterator(Iterator delegate) {
super(delegate);
}
@Override
protected void removeFromRedisStorage(E item) {
DefaultRedisZSet.this.remove(item);
}
}
/**
* Constructs a new {@link DefaultRedisZSet} instance with a default score of {@literal 1}.
*
* @param key Redis key of this set.
* @param operations {@link RedisOperations} for the value type of this set.
*/
public DefaultRedisZSet(String key, RedisOperations operations) {
this(key, operations, 1);
}
/**
* Constructs a new {@link DefaultRedisZSet} instance.
*
* @param key Redis key of this set.
* @param operations {@link RedisOperations} for the value type of this set.
* @param defaultScore
*/
public DefaultRedisZSet(String key, RedisOperations operations, double defaultScore) {
super(key, operations);
boundZSetOps = operations.boundZSetOps(key);
this.defaultScore = defaultScore;
}
/**
* Constructs a new {@link DefaultRedisZSet} instance with a default score of '1'.
*
* @param boundOps {@link BoundZSetOperations} for the value type of this set.
*/
public DefaultRedisZSet(BoundZSetOperations boundOps) {
this(boundOps, 1);
}
/**
* Constructs a new {@link DefaultRedisZSet} instance.
*
* @param boundOps {@link BoundZSetOperations} for the value type of this set.
* @param defaultScore
*/
public DefaultRedisZSet(BoundZSetOperations boundOps, double defaultScore) {
super(boundOps.getKey(), boundOps.getOperations());
this.boundZSetOps = boundOps;
this.defaultScore = defaultScore;
}
@Override
public Set diff(RedisZSet set) {
return boundZSetOps.difference(set.getKey());
}
@Override
public Set diff(Collection> sets) {
return boundZSetOps.difference(CollectionUtils.extractKeys(sets));
}
@Override
public Set> diffWithScores(RedisZSet set) {
return boundZSetOps.differenceWithScores(set.getKey());
}
@Override
public Set> diffWithScores(Collection> sets) {
return boundZSetOps.differenceWithScores(CollectionUtils.extractKeys(sets));
}
@Override
public RedisZSet diffAndStore(RedisZSet set, String destKey) {
boundZSetOps.differenceAndStore(set.getKey(), destKey);
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override
public RedisZSet diffAndStore(Collection> sets, String destKey) {
boundZSetOps.differenceAndStore(CollectionUtils.extractKeys(sets), destKey);
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override
public Set intersect(RedisZSet set) {
return boundZSetOps.intersect(set.getKey());
}
@Override
public Set intersect(Collection> sets) {
return boundZSetOps.intersect(CollectionUtils.extractKeys(sets));
}
@Override
public Set> intersectWithScores(RedisZSet set) {
return boundZSetOps.intersectWithScores(set.getKey());
}
@Override
public Set> intersectWithScores(Collection> sets) {
return boundZSetOps.intersectWithScores(CollectionUtils.extractKeys(sets));
}
@Override
public RedisZSet intersectAndStore(RedisZSet set, String destKey) {
boundZSetOps.intersectAndStore(set.getKey(), destKey);
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override
public RedisZSet intersectAndStore(Collection> sets, String destKey) {
boundZSetOps.intersectAndStore(CollectionUtils.extractKeys(sets), destKey);
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override
public Set union(RedisZSet set) {
return boundZSetOps.union(set.getKey());
}
@Override
public Set union(Collection> sets) {
return boundZSetOps.union(CollectionUtils.extractKeys(sets));
}
@Override
public Set> unionWithScores(RedisZSet set) {
return boundZSetOps.unionWithScores(set.getKey());
}
@Override
public Set> unionWithScores(Collection> sets) {
return boundZSetOps.unionWithScores(CollectionUtils.extractKeys(sets));
}
@Override
public RedisZSet unionAndStore(RedisZSet set, String destKey) {
boundZSetOps.unionAndStore(set.getKey(), destKey);
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override
public RedisZSet unionAndStore(Collection> sets, String destKey) {
boundZSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey);
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override
public E randomValue() {
return boundZSetOps.randomMember();
}
@Override
public Set range(long start, long end) {
return boundZSetOps.range(start, end);
}
@Override
public Set reverseRange(long start, long end) {
return boundZSetOps.reverseRange(start, end);
}
@Override
public Set rangeByLex(Range range, Limit limit) {
return boundZSetOps.rangeByLex(range, limit);
}
@Override
public Set reverseRangeByLex(Range range, Limit limit) {
return boundZSetOps.reverseRangeByLex(range, limit);
}
@Override
public Set rangeByScore(double min, double max) {
return boundZSetOps.rangeByScore(min, max);
}
@Override
public Set reverseRangeByScore(double min, double max) {
return boundZSetOps.reverseRangeByScore(min, max);
}
@Override
public Set> rangeByScoreWithScores(double min, double max) {
return boundZSetOps.rangeByScoreWithScores(min, max);
}
@Override
public Set> rangeWithScores(long start, long end) {
return boundZSetOps.rangeWithScores(start, end);
}
@Override
public Set> reverseRangeByScoreWithScores(double min, double max) {
return boundZSetOps.reverseRangeByScoreWithScores(min, max);
}
@Override
public Set> reverseRangeWithScores(long start, long end) {
return boundZSetOps.reverseRangeWithScores(start, end);
}
@Override
public RedisZSet rangeAndStoreByLex(String dstKey, Range range, Limit limit) {
boundZSetOps.rangeAndStoreByLex(dstKey, range, limit);
return new DefaultRedisZSet<>(getOperations().boundZSetOps(dstKey));
}
@Override
public RedisZSet reverseRangeAndStoreByLex(String dstKey, Range range, Limit limit) {
boundZSetOps.reverseRangeAndStoreByLex(dstKey, range, limit);
return new DefaultRedisZSet<>(getOperations().boundZSetOps(dstKey));
}
@Override
public RedisZSet rangeAndStoreByScore(String dstKey, Range range, Limit limit) {
boundZSetOps.rangeAndStoreByScore(dstKey, range, limit);
return new DefaultRedisZSet<>(getOperations().boundZSetOps(dstKey));
}
@Override
public RedisZSet reverseRangeAndStoreByScore(String dstKey, Range range, Limit limit) {
boundZSetOps.reverseRangeAndStoreByScore(dstKey, range, limit);
return new DefaultRedisZSet<>(getOperations().boundZSetOps(dstKey));
}
@Override
public RedisZSet remove(long start, long end) {
boundZSetOps.removeRange(start, end);
return this;
}
@Override
public RedisZSet removeByLex(Range range) {
boundZSetOps.removeRangeByLex(range);
return this;
}
@Override
public RedisZSet removeByScore(double min, double max) {
boundZSetOps.removeRangeByScore(min, max);
return this;
}
@Override
public boolean add(E e) {
Boolean result = add(e, getDefaultScore());
checkResult(result);
return result;
}
@Override
public boolean add(E e, double score) {
Boolean result = boundZSetOps.add(e, score);
checkResult(result);
return result;
}
@Override
public boolean addIfAbsent(E e, double score) {
Boolean result = boundZSetOps.addIfAbsent(e, score);
checkResult(result);
return result;
}
@Override
public void clear() {
boundZSetOps.removeRange(0, -1);
}
@Override
public boolean contains(Object o) {
return (boundZSetOps.rank(o) != null);
}
@Override
public Iterator iterator() {
Set members = boundZSetOps.range(0, -1);
checkResult(members);
return new DefaultRedisSortedSetIterator(members.iterator());
}
@Override
public boolean remove(Object o) {
Long result = boundZSetOps.remove(o);
checkResult(result);
return result == 1;
}
@Override
public int size() {
Long result = boundZSetOps.size();
checkResult(result);
return result.intValue();
}
@Override
public Double getDefaultScore() {
return defaultScore;
}
@Override
public E first() {
Set members = boundZSetOps.range(0, 0);
checkResult(members);
Iterator iterator = members.iterator();
if (iterator.hasNext()) {
return iterator.next();
}
throw new NoSuchElementException();
}
@Override
public E popFirst() {
TypedTuple tuple = boundZSetOps.popMin();
if (tuple != null) {
return tuple.getValue();
}
throw new NoSuchElementException();
}
@Override
public E popFirst(long timeout, TimeUnit unit) {
TypedTuple tuple = boundZSetOps.popMin(timeout, unit);
if (tuple != null) {
return tuple.getValue();
}
throw new NoSuchElementException();
}
@Override
public E last() {
Set members = boundZSetOps.reverseRange(0, 0);
checkResult(members);
Iterator iterator = members.iterator();
if (iterator.hasNext()) {
return iterator.next();
}
throw new NoSuchElementException();
}
@Override
public E popLast() {
TypedTuple tuple = boundZSetOps.popMax();
if (tuple != null) {
return tuple.getValue();
}
throw new NoSuchElementException();
}
@Override
public E popLast(long timeout, TimeUnit unit) {
TypedTuple tuple = boundZSetOps.popMax(timeout, unit);
if (tuple != null) {
return tuple.getValue();
}
throw new NoSuchElementException();
}
@Override
public Long rank(Object o) {
return boundZSetOps.rank(o);
}
@Override
public Long reverseRank(Object o) {
return boundZSetOps.reverseRank(o);
}
@Override
public Long lexCount(Range range) {
return boundZSetOps.lexCount(range);
}
@Override
public Double score(Object o) {
return boundZSetOps.score(o);
}
@Override
public DataType getType() {
return DataType.ZSET;
}
@Override
public Cursor scan() {
return new ConvertingCursor<>(scan(ScanOptions.NONE), TypedTuple::getValue);
}
/**
* @since 1.4
* @param options
* @return
*/
public Cursor> scan(ScanOptions options) {
return boundZSetOps.scan(options);
}
}