Skip to content

Commit

Permalink
DATAREDIS-1196 - Add lPos to ReactiveListOperations
Browse files Browse the repository at this point in the history
  • Loading branch information
christophstrobl committed Sep 16, 2020
1 parent 5ea658f commit d8754ad
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.reactivestreams.Publisher;
import org.springframework.data.redis.connection.ReactiveListCommands;
import org.springframework.data.redis.connection.ReactiveListCommands.LPosCommand;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -232,6 +233,30 @@ public Mono<V> index(K key, long index) {
return createMono(connection -> connection.lIndex(rawKey(key), index).map(this::readValue));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveListOperations#indexOf(java.lang.Object, java.lang.Object)
*/
@Override
public Mono<Long> indexOf(K key, V value) {

Assert.notNull(key, "Key must not be null!");

return createMono(connection -> connection.lPos(rawKey(key), rawValue(value)));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveListOperations#lastIndexOf(java.lang.Object, java.lang.Object)
*/
@Override
public Mono<Long> lastIndexOf(K key, V value) {

Assert.notNull(key, "Key must not be null!");

return createMono(connection -> connection.lPos(LPosCommand.lPosOf(rawValue(value)).from(rawKey(key)).rank(-1)));
}

/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveListOperations#leftPop(java.lang.Object)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.time.Duration;
import java.util.Collection;
import java.util.List;

/**
* Redis list specific operations.
Expand Down Expand Up @@ -197,6 +196,30 @@ public interface ReactiveListOperations<K, V> {
*/
Mono<V> index(K key, long index);

/**
* Returns the index of the first occurrence of the specified value in the list at at {@code key}. <br />
* Requires Redis 6.0.6
*
* @param key must not be {@literal null}.
* @param value must not be {@literal null}.
* @return
* @since 2.4
* @see <a href="https://redis.io/commands/lpos">Redis Documentation: LPOS</a>
*/
Mono<Long> indexOf(K key, V value);

/**
* Returns the index of the last occurrence of the specified value in the list at at {@code key}. <br />
* Requires Redis 6.0.6
*
* @param key must not be {@literal null}.
* @param value must not be {@literal null}.
* @return
* @since 2.4
* @see <a href="https://redis.io/commands/lpos">Redis Documentation: LPOS</a>
*/
Mono<Long> lastIndexOf(K key, V value);

/**
* Removes and returns first element in list stored at {@code key}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -34,6 +36,8 @@
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.test.annotation.IfProfileValue;

/**
* Integration tests for {@link DefaultReactiveListOperations}.
Expand All @@ -45,6 +49,8 @@
@SuppressWarnings("unchecked")
public class DefaultReactiveListOperationsIntegrationTests<K, V> {

@Rule public MinimumRedisVersionRule redisVersion = new MinimumRedisVersionRule();

private final ReactiveRedisTemplate<K, V> redisTemplate;
private final ReactiveListOperations<K, V> listOperations;

Expand Down Expand Up @@ -366,6 +372,35 @@ public void index() {
listOperations.index(key, 1).as(StepVerifier::create).expectNext(value2).verifyComplete();
}

@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void indexOf() {

K key = keyFactory.instance();
V v1 = valueFactory.instance();
V v2 = valueFactory.instance();
V v3 = valueFactory.instance();

listOperations.rightPushAll(key, v1, v2, v1, v3).as(StepVerifier::create).expectNext(4L).verifyComplete();

listOperations.indexOf(key, v1).as(StepVerifier::create).expectNext(0L).verifyComplete();
}

@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
@Ignore("https://github.com/lettuce-io/lettuce-core/issues/1410")
public void lastIndexOf() {

K key = keyFactory.instance();
V v1 = valueFactory.instance();
V v2 = valueFactory.instance();
V v3 = valueFactory.instance();

listOperations.rightPushAll(key, v1, v2, v1, v3).as(StepVerifier::create).expectNext(4L).verifyComplete();

listOperations.lastIndexOf(key, v1).as(StepVerifier::create).expectNext(2L).verifyComplete();
}

@Test // DATAREDIS-602
public void leftPop() {

Expand Down

0 comments on commit d8754ad

Please sign in to comment.