Skip to content

Commit

Permalink
Refine RedisList.
Browse files Browse the repository at this point in the history
Add missing Javadoc. Add trim overload accepting long to avoid potential integer downcasting. Add factory methods to RedisList.

See: #2039
Original Pull Request: #2107
  • Loading branch information
mp911de authored and christophstrobl committed Jul 1, 2021
1 parent 54ad66b commit eefbd0d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public DefaultRedisList(String key, RedisOperations<String, E> operations) {
this(operations.boundListOps(key));
}

/**
* Constructs a new {@link DefaultRedisList} instance.
*
* @param key Redis key of this list.
* @param operations {@link RedisOperations} for the value type of this list.
* @param maxSize
* @since 2.6
*/
public DefaultRedisList(String key, RedisOperations<String, E> operations, int maxSize) {
this(operations.boundListOps(key), maxSize);
}

/**
* Constructs a new, uncapped {@link DefaultRedisList} instance.
*
Expand Down Expand Up @@ -194,6 +206,16 @@ public RedisList<E> trim(int start, int end) {
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisList#trim(long, long)
*/
@Override
public RedisList<E> trim(long start, long end) {
listOps.trim(start, end);
return this;
}

/*
* (non-Javadoc)
* @see java.util.AbstractCollection#iterator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void afterPropertiesSet() {
private RedisStore createStore(DataType dt) {
switch (dt) {
case LIST:
return new DefaultRedisList(key, template);
return RedisList.create(key, template);

case SET:
return new DefaultRedisSet(key, template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -37,6 +39,50 @@
*/
public interface RedisList<E> extends RedisCollection<E>, List<E>, BlockingDeque<E> {

/**
* Constructs a new, uncapped {@link RedisList} instance.
*
* @param key Redis key of this list.
* @param operations {@link RedisOperations} for the value type of this list.
* @since 2.6
*/
static <E> RedisList<E> create(String key, RedisOperations<String, E> operations) {
return new DefaultRedisList<>(key, operations);
}

/**
* Constructs a new {@link RedisList} instance.
*
* @param key Redis key of this list.
* @param operations {@link RedisOperations} for the value type of this list.
* @param maxSize
* @since 2.6
*/
static <E> RedisList<E> create(String key, RedisOperations<String, E> operations, int maxSize) {
return new DefaultRedisList<>(key, operations, maxSize);
}

/**
* Constructs a new, uncapped {@link DefaultRedisList} instance.
*
* @param boundOps {@link BoundListOperations} for the value type of this list.
* @since 2.6
*/
static <E> RedisList<E> create(BoundListOperations<String, E> boundOps) {
return new DefaultRedisList<>(boundOps);
}

/**
* Constructs a new {@link DefaultRedisList} instance.
*
* @param boundOps {@link BoundListOperations} for the value type of this list.
* @param maxSize
* @since 2.6
*/
static <E> RedisList<E> create(BoundListOperations<String, E> boundOps, int maxSize) {
return new DefaultRedisList<>(boundOps, maxSize);
}

/**
* Atomically returns and removes the first element of the list stored at the bound key, and pushes the element at the
* first/last element (head/tail depending on the {@link Direction destinationPosition} argument) of the list stored
Expand Down Expand Up @@ -155,7 +201,32 @@ default E moveLastTo(RedisList<E> destination, Direction destinationPosition, Du
@Nullable
E moveLastTo(RedisList<E> destination, Direction destinationPosition, long timeout, TimeUnit unit);

List<E> range(long begin, long end);
/**
* Get elements between {@code start} and {@code end} from list at the bound key.
*
* @param start
* @param end
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="https://redis.io/commands/lrange">Redis Documentation: LRANGE</a>
*/
List<E> range(long start, long end);

RedisList<E> trim(int begin, int end);
/**
* Trim list at the bound key to elements between {@code start} and {@code end}.
*
* @param start
* @param end
* @see <a href="https://redis.io/commands/ltrim">Redis Documentation: LTRIM</a>
*/
RedisList<E> trim(int start, int end);

/**
* Trim list at the bound key to elements between {@code start} and {@code end}.
*
* @param start
* @param end
* @since 2.6
* @see <a href="https://redis.io/commands/ltrim">Redis Documentation: LTRIM</a>
*/
RedisList<E> trim(long start, long end);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisMap;
import org.springframework.data.redis.support.collections.DefaultRedisSet;
import org.springframework.data.redis.support.collections.RedisList;
Expand All @@ -47,7 +46,7 @@ public static Collection<Object[]> testParams() {
StringRedisTemplate templateJS = new StringRedisTemplate(jedisConnFactory);
DefaultRedisMap mapJS = new DefaultRedisMap("bound:key:map", templateJS);
DefaultRedisSet setJS = new DefaultRedisSet("bound:key:set", templateJS);
RedisList list = new DefaultRedisList("bound:key:list", templateJS);
RedisList list = RedisList.create("bound:key:list", templateJS);

// Lettuce
LettuceConnectionFactory lettuceConnFactory = LettuceConnectionFactoryExtension
Expand All @@ -56,7 +55,7 @@ public static Collection<Object[]> testParams() {
StringRedisTemplate templateLT = new StringRedisTemplate(lettuceConnFactory);
DefaultRedisMap mapLT = new DefaultRedisMap("bound:key:mapLT", templateLT);
DefaultRedisSet setLT = new DefaultRedisSet("bound:key:setLT", templateLT);
RedisList listLT = new DefaultRedisList("bound:key:listLT", templateLT);
RedisList listLT = RedisList.create("bound:key:listLT", templateLT);

StringObjectFactory sof = new StringObjectFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,10 @@ void testTrim() {
list.add(t1);
list.add(t2);
assertThat(list).hasSize(2);
assertThat(list.trim(0, 0)).hasSize(1);
assertThat(list.trim(0L, 0L)).hasSize(1);
assertThat(list).hasSize(1);
assertThat(list.get(0)).isEqualTo(t1);
assertThat(list).hasSize(1);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public RedisListIntegrationTests(ObjectFactory<Object> factory, RedisTemplate<Ob
}

RedisStore copyStore(RedisStore store) {
return new DefaultRedisList<>(store.getKey(), store.getOperations());
return RedisList.create(store.getKey(), store.getOperations());
}

AbstractRedisCollection<Object> createCollection() {
Expand Down

0 comments on commit eefbd0d

Please sign in to comment.