DATAREDIS-1196 - Add support for LPOS command.

Original pull request: #563.
This commit is contained in:
Christoph Strobl
2020-09-15 09:47:59 +02:00
committed by Mark Paluch
parent 1a025fea46
commit 76cbe5124d
22 changed files with 718 additions and 11 deletions

View File

@@ -1479,6 +1479,72 @@ public abstract class AbstractConnectionIntegrationTests {
verifyResults(Arrays.asList(new Object[] { 2l, Arrays.asList(new String[] { "baz", "bar" }) }));
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
@WithRedisDriver({ RedisDriver.LETTUCE })
public void lPos() {
actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
actual.add(connection.lPos("mylist", "c"));
assertThat((Long) getResults().get(1)).isEqualTo(2);
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
@WithRedisDriver({ RedisDriver.LETTUCE })
public void lPosRank() {
actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
actual.add(connection.lPos("mylist", "c", 2, null));
assertThat((List<Long>) getResults().get(1)).containsExactly(6L);
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
@WithRedisDriver({ RedisDriver.LETTUCE })
public void lPosNegativeRank() {
actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
actual.add(connection.lPos("mylist", "c", -1, null));
assertThat((List<Long>) getResults().get(1)).containsExactly(7L);
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
@WithRedisDriver({ RedisDriver.LETTUCE })
public void lPosCount() {
actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
actual.add(connection.lPos("mylist", "c", null, 2));
assertThat((List<Long>) getResults().get(1)).containsExactly(2L, 6L);
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
@WithRedisDriver({ RedisDriver.LETTUCE })
public void lPosRankCount() {
actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
actual.add(connection.lPos("mylist", "c", -1, 2));
assertThat((List<Long>) getResults().get(1)).containsExactly(7L, 6L);
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
@WithRedisDriver({ RedisDriver.LETTUCE })
public void lPosCountZero() {
actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
actual.add(connection.lPos("mylist", "c", null, 0));
assertThat((List<Long>) getResults().get(1)).containsExactly(2L, 6L, 7L);
}
// Set operations
@Test

View File

@@ -23,19 +23,23 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.ReactiveListCommands.LPosCommand;
import org.springframework.data.redis.connection.ReactiveListCommands.PopResult;
import org.springframework.data.redis.connection.ReactiveListCommands.PushCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnection.CommandResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.RangeCommand;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.test.annotation.IfProfileValue;
/**
* @author Christoph Strobl
@@ -44,6 +48,8 @@ import org.springframework.data.redis.connection.RedisListCommands.Position;
*/
public class LettuceReactiveListCommandTests extends LettuceReactiveCommandsTestsBase {
@Rule public MinimumRedisVersionRule redisVersion = new MinimumRedisVersionRule();
@Test // DATAREDIS-525
public void rPushShouldAppendValuesCorrectly() {
@@ -304,4 +310,88 @@ public class LettuceReactiveListCommandTests extends LettuceReactiveCommandsTest
assertThat(nativeCommands.llen(KEY_2)).isEqualTo(2L);
assertThat(nativeCommands.lindex(KEY_2, 0)).isEqualTo(VALUE_3);
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lPos() {
nativeCommands.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
connection.listCommands().lPos(KEY_1_BBUFFER, ByteBuffer.wrap("c".getBytes(StandardCharsets.UTF_8))) //
.as(StepVerifier::create) //
.expectNext(2L) //
.verifyComplete();
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lPosRank() {
nativeCommands.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
connection.listCommands()
.lPos(LPosCommand.lPosOf(ByteBuffer.wrap("c".getBytes(StandardCharsets.UTF_8))).from(KEY_1_BBUFFER).rank(2)) //
.as(StepVerifier::create) //
.expectNext(6L) //
.verifyComplete();
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lPosNegativeRank() {
nativeCommands.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
connection.listCommands()
.lPos(LPosCommand.lPosOf(ByteBuffer.wrap("c".getBytes(StandardCharsets.UTF_8))).from(KEY_1_BBUFFER).rank(-1)) //
.as(StepVerifier::create) //
.expectNext(7L) //
.verifyComplete();
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lPosCount() {
nativeCommands.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
connection.listCommands()
.lPos(LPosCommand.lPosOf(ByteBuffer.wrap("c".getBytes(StandardCharsets.UTF_8))).from(KEY_1_BBUFFER).count(2)) //
.as(StepVerifier::create) //
.expectNext(2L) //
.expectNext(6L) //
.verifyComplete();
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lPosRankCount() {
nativeCommands.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
connection.listCommands()
.lPos(LPosCommand.lPosOf(ByteBuffer.wrap("c".getBytes(StandardCharsets.UTF_8))).from(KEY_1_BBUFFER)
.from(KEY_1_BBUFFER).rank(-1).count(2)) //
.as(StepVerifier::create) //
.expectNext(7L) //
.expectNext(6L) //
.verifyComplete();
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lPosCountZero() {
nativeCommands.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
connection.listCommands()
.lPos(LPosCommand.lPosOf(ByteBuffer.wrap("c".getBytes(StandardCharsets.UTF_8))).from(KEY_1_BBUFFER).count(0)) //
.as(StepVerifier::create) //
.expectNext(2L) //
.expectNext(6L) //
.expectNext(7L) //
.verifyComplete();
}
}

View File

@@ -27,15 +27,18 @@ import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.RedisTestProfileValueSource;
import org.springframework.data.redis.StringObjectFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.test.annotation.IfProfileValue;
/**
* Integration test of {@link DefaultListOperations}
@@ -49,6 +52,8 @@ import org.springframework.data.redis.StringObjectFactory;
@RunWith(Parameterized.class)
public class DefaultListOperationsTests<K, V> {
@Rule public MinimumRedisVersionRule redisVersion = new MinimumRedisVersionRule();
private RedisTemplate<K, V> redisTemplate;
private ObjectFactory<K> keyFactory;
@@ -284,6 +289,8 @@ public class DefaultListOperationsTests<K, V> {
@SuppressWarnings("unchecked")
public void testLeftPushAllCollection() {
assumeTrue(redisTemplate.getConnectionFactory() instanceof LettuceConnectionFactory);
K key = keyFactory.instance();
V v1 = valueFactory.instance();
@@ -311,4 +318,35 @@ public class DefaultListOperationsTests<K, V> {
assertThatIllegalArgumentException()
.isThrownBy(() -> listOps.leftPushAll(keyFactory.instance(), (Collection<V>) null));
}
@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();
assertThat(listOps.rightPush(key, v1)).isEqualTo(Long.valueOf(1));
assertThat(listOps.rightPush(key, v2)).isEqualTo(Long.valueOf(2));
assertThat(listOps.rightPush(key, v1, v3)).isEqualTo(Long.valueOf(3));
assertThat(listOps.indexOf(key, v1)).isEqualTo(0);
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lastIndexOf() {
K key = keyFactory.instance();
V v1 = valueFactory.instance();
V v2 = valueFactory.instance();
V v3 = valueFactory.instance();
assertThat(listOps.rightPush(key, v1)).isEqualTo(Long.valueOf(1));
assertThat(listOps.rightPush(key, v2)).isEqualTo(Long.valueOf(2));
assertThat(listOps.rightPush(key, v1)).isEqualTo(Long.valueOf(3));
assertThat(listOps.rightPush(key, v3)).isEqualTo(Long.valueOf(4));
assertThat(listOps.lastIndexOf(key, v1)).isEqualTo(2);
}
}

View File

@@ -24,6 +24,7 @@ import java.util.Collection;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -34,6 +35,8 @@ import org.springframework.data.redis.ObjectFactory;
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}.
@@ -45,6 +48,8 @@ import org.springframework.data.redis.serializer.RedisSerializer;
@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;
@@ -366,6 +371,34 @@ public class DefaultReactiveListOperationsIntegrationTests<K, V> {
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+")
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() {

View File

@@ -24,11 +24,15 @@ import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.assertj.core.api.Assumptions;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.test.annotation.IfProfileValue;
/**
* Integration test for RedisList
@@ -38,6 +42,8 @@ import org.springframework.data.redis.core.RedisTemplate;
*/
public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionTests<T> {
@Rule public MinimumRedisVersionRule redisVersion = new MinimumRedisVersionRule();
protected RedisList<T> list;
/**
@@ -54,6 +60,7 @@ public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionT
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
super.setUp();
list = (RedisList<T>) collection;
}
@@ -157,8 +164,12 @@ public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionT
assertThat(list.addAll(1, asList)).isTrue();
}
@Test(expected = UnsupportedOperationException.class)
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void testIndexOfObject() {
Assumptions.assumeThat(template.getConnectionFactory()).isInstanceOf(LettuceConnectionFactory.class);
T t1 = getT();
T t2 = getT();
@@ -168,7 +179,7 @@ public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionT
assertThat(list.indexOf(t2)).isEqualTo(-1);
list.add(t2);
assertThat(list.indexOf(t1)).isEqualTo(1);
assertThat(list.indexOf(t2)).isEqualTo(1);
}
@Test
@@ -519,4 +530,22 @@ public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionT
public void testTakeLast() {
testPollLast();
}
@Test // DATAREDIS-1196
@IfProfileValue(name = "redisVersion", value = "6.0.6+")
public void lastIndexOf() {
Assumptions.assumeThat(template.getConnectionFactory()).isInstanceOf(LettuceConnectionFactory.class);
T t1 = getT();
T t2 = getT();
T t3 = getT();
list.add(t1);
list.add(t2);
list.add(t1);
list.add(t3);
assertThat(list.lastIndexOf(t1)).isEqualTo(2);
}
}