Skip to content

Support LPOS command via Jedis. #1962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.0-SNAPSHOT</version>
<version>2.5.0-GH-1957-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/
package org.springframework.data.redis.connection.jedis;

import redis.clients.jedis.params.LPosParams;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.ClusterSlotHashUtil;
import org.springframework.data.redis.connection.RedisListCommands;
import org.springframework.data.redis.connection.jedis.JedisClusterConnection.JedisMultiKeyClusterCommandCallback;
Expand Down Expand Up @@ -68,7 +69,22 @@ public List<Long> lPos(byte[] key, byte[] element, @Nullable Integer rank, @Null
Assert.notNull(key, "Key must not be null!");
Assert.notNull(element, "Element must not be null!");

throw new InvalidDataAccessApiUsageException("LPOS is not supported by jedis.");
LPosParams params = new LPosParams();
if (rank != null) {
params.rank(rank);
}

try {

if (count != null) {
return connection.getCluster().lpos(key, element, params, count);
}

Long value = connection.getCluster().lpos(key, element, params);
return value != null ? Collections.singletonList(value) : Collections.emptyList();
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import redis.clients.jedis.MultiKeyPipelineBase;
import redis.clients.jedis.Protocol;

import java.util.Collections;
import java.util.List;

import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.RedisListCommands;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import redis.clients.jedis.params.LPosParams;

/**
* @author Christoph Strobl
Expand Down Expand Up @@ -61,7 +63,16 @@ public List<Long> lPos(byte[] key, byte[] element, @Nullable Integer rank, @Null
Assert.notNull(key, "Key must not be null!");
Assert.notNull(element, "Element must not be null!");

throw new InvalidDataAccessApiUsageException("LPOS is not supported by jedis.");
LPosParams params = new LPosParams();
if(rank != null) {
params.rank(rank);
}

if(count != null) {
return connection.invoke().just(BinaryJedis::lpos, MultiKeyPipelineBase::lpos, key, element, params, count);
}

return connection.invoke().from(BinaryJedis::lpos, MultiKeyPipelineBase::lpos, key, element, params).getOrElse(Collections::singletonList, Collections::emptyList);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public List<Long> lPos(byte[] key, byte[] element, @Nullable Integer rank, @Null
return connection.invoke().just(RedisListAsyncCommands::lpos, key, element, count, args);
}

return connection.invoke().from(RedisListAsyncCommands::lpos, key, element, args).get(Collections::singletonList);
return connection.invoke().from(RedisListAsyncCommands::lpos, key, element, args).getOrElse(Collections::singletonList, Collections::emptyList);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1404,9 +1404,8 @@ void testLPushMultiple() {
verifyResults(Arrays.asList(2L, Arrays.asList("baz", "bar")));
}

@Test // DATAREDIS-1196
@Test // DATAREDIS-1196, GH-1957
@EnabledOnCommand("LPOS")
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
void lPos() {

actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
Expand All @@ -1415,9 +1414,8 @@ void lPos() {
assertThat((List<Long>) getResults().get(1)).containsOnly(2L);
}

@Test // DATAREDIS-1196
@Test // DATAREDIS-1196, GH-1957
@EnabledOnCommand("LPOS")
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
void lPosRank() {

actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
Expand All @@ -1426,9 +1424,8 @@ void lPosRank() {
assertThat((List<Long>) getResults().get(1)).containsExactly(6L);
}

@Test // DATAREDIS-1196
@Test // DATAREDIS-1196, GH-1957
@EnabledOnCommand("LPOS")
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
void lPosNegativeRank() {

actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
Expand All @@ -1437,9 +1434,8 @@ void lPosNegativeRank() {
assertThat((List<Long>) getResults().get(1)).containsExactly(7L);
}

@Test // DATAREDIS-1196
@Test // DATAREDIS-1196, GH-1957
@EnabledOnCommand("LPOS")
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
void lPosCount() {

actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
Expand All @@ -1448,9 +1444,8 @@ void lPosCount() {
assertThat((List<Long>) getResults().get(1)).containsExactly(2L, 6L);
}

@Test // DATAREDIS-1196
@Test // DATAREDIS-1196, GH-1957
@EnabledOnCommand("LPOS")
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
void lPosRankCount() {

actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
Expand All @@ -1459,9 +1454,8 @@ void lPosRankCount() {
assertThat((List<Long>) getResults().get(1)).containsExactly(7L, 6L);
}

@Test // DATAREDIS-1196
@Test // DATAREDIS-1196, GH-1957
@EnabledOnCommand("LPOS")
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
void lPosCountZero() {

actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
Expand All @@ -1470,6 +1464,16 @@ void lPosCountZero() {
assertThat((List<Long>) getResults().get(1)).containsExactly(2L, 6L, 7L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPosNonExisting() {

actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
actual.add(connection.lPos("mylist", "x", null, null));

assertThat((List<Long>) getResults().get(1)).isEmpty();
}

// Set operations

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import static org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs.*;
import static org.springframework.data.redis.core.ScanOptions.*;

import org.springframework.data.redis.test.condition.EnabledOnCommand;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -2472,4 +2474,74 @@ void evelShaShouldRunScript() {
Long result = clusterConnection.scriptingCommands().evalSha(digest, ReturnType.VALUE, 1, keyAndArgs);
assertThat(result).isEqualTo(1L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPos() {

nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), null, null);

assertThat(result).containsOnly(2L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPosRank() {

nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), 2, null);

assertThat(result).containsExactly(6L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPosNegativeRank() {

nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), -1, null);

assertThat(result).containsExactly(7L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPosCount() {

nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), null, 2);

assertThat(result).containsExactly(2L, 6L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPosRankCount() {

nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), -1, 2);

assertThat(result).containsExactly(7L, 6L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPosCountZero() {

nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), null, 0);

assertThat(result).containsExactly(2L, 6L, 7L);
}

@Test // GH-1957
@EnabledOnCommand("LPOS")
void lPosNonExisting() {

nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "x".getBytes(StandardCharsets.UTF_8), null, null);

assertThat(result).isEmpty();
}
}