Skip to content

Commit 19eb093

Browse files
christophstroblmp911de
authored andcommitted
Support LPOS command via Jedis.
This commit introduces support for the LPOS command when using the Jedis Redis driver. Closes #1957. Original pull request: #1962.
1 parent 7e7197b commit 19eb093

File tree

4 files changed

+98
-15
lines changed

4 files changed

+98
-15
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterListCommands.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.springframework.data.redis.connection.jedis;
1717

18+
import redis.clients.jedis.params.LPosParams;
19+
1820
import java.util.Arrays;
1921
import java.util.Collections;
2022
import java.util.List;
2123

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

71-
throw new InvalidDataAccessApiUsageException("LPOS is not supported by jedis.");
72+
LPosParams params = new LPosParams();
73+
if (rank != null) {
74+
params.rank(rank);
75+
}
76+
77+
try {
78+
79+
if (count != null) {
80+
return connection.getCluster().lpos(key, element, params, count);
81+
}
82+
83+
Long value = connection.getCluster().lpos(key, element, params);
84+
return value != null ? Collections.singletonList(value) : Collections.emptyList();
85+
} catch (Exception ex) {
86+
throw convertJedisAccessException(ex);
87+
}
7288
}
7389

7490
/*

src/main/java/org/springframework/data/redis/connection/jedis/JedisListCommands.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import redis.clients.jedis.MultiKeyPipelineBase;
2020
import redis.clients.jedis.Protocol;
2121

22+
import java.util.Collections;
2223
import java.util.List;
2324

2425
import org.springframework.dao.InvalidDataAccessApiUsageException;
2526
import org.springframework.data.redis.connection.RedisListCommands;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
29+
import redis.clients.jedis.params.LPosParams;
2830

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

64-
throw new InvalidDataAccessApiUsageException("LPOS is not supported by jedis.");
66+
LPosParams params = new LPosParams();
67+
if(rank != null) {
68+
params.rank(rank);
69+
}
70+
71+
if(count != null) {
72+
return connection.invoke().just(BinaryJedis::lpos, MultiKeyPipelineBase::lpos, key, element, params, count);
73+
}
74+
75+
return connection.invoke().from(BinaryJedis::lpos, MultiKeyPipelineBase::lpos, key, element, params).get(Collections::singletonList);
6576
}
6677

6778
/*

src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -1404,9 +1404,8 @@ void testLPushMultiple() {
14041404
verifyResults(Arrays.asList(2L, Arrays.asList("baz", "bar")));
14051405
}
14061406

1407-
@Test // DATAREDIS-1196
1407+
@Test // DATAREDIS-1196, GH-1957
14081408
@EnabledOnCommand("LPOS")
1409-
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
14101409
void lPos() {
14111410

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

1418-
@Test // DATAREDIS-1196
1417+
@Test // DATAREDIS-1196, GH-1957
14191418
@EnabledOnCommand("LPOS")
1420-
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
14211419
void lPosRank() {
14221420

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

1429-
@Test // DATAREDIS-1196
1427+
@Test // DATAREDIS-1196, GH-1957
14301428
@EnabledOnCommand("LPOS")
1431-
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
14321429
void lPosNegativeRank() {
14331430

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

1440-
@Test // DATAREDIS-1196
1437+
@Test // DATAREDIS-1196, GH-1957
14411438
@EnabledOnCommand("LPOS")
1442-
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
14431439
void lPosCount() {
14441440

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

1451-
@Test // DATAREDIS-1196
1447+
@Test // DATAREDIS-1196, GH-1957
14521448
@EnabledOnCommand("LPOS")
1453-
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
14541449
void lPosRankCount() {
14551450

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

1462-
@Test // DATAREDIS-1196
1457+
@Test // DATAREDIS-1196, GH-1957
14631458
@EnabledOnCommand("LPOS")
1464-
@EnabledOnRedisDriver({ RedisDriver.LETTUCE })
14651459
void lPosCountZero() {
14661460

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

src/test/java/org/springframework/data/redis/connection/jedis/JedisClusterConnectionTests.java

+62
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
import static org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs.*;
2626
import static org.springframework.data.redis.core.ScanOptions.*;
2727

28+
import org.springframework.data.redis.test.condition.EnabledOnCommand;
2829
import redis.clients.jedis.HostAndPort;
2930
import redis.clients.jedis.Jedis;
3031
import redis.clients.jedis.JedisCluster;
3132
import redis.clients.jedis.JedisPool;
3233

3334
import java.io.IOException;
3435
import java.nio.charset.Charset;
36+
import java.nio.charset.StandardCharsets;
3537
import java.time.Duration;
3638
import java.util.*;
3739
import java.util.concurrent.TimeUnit;
@@ -2472,4 +2474,64 @@ void evelShaShouldRunScript() {
24722474
Long result = clusterConnection.scriptingCommands().evalSha(digest, ReturnType.VALUE, 1, keyAndArgs);
24732475
assertThat(result).isEqualTo(1L);
24742476
}
2477+
2478+
@Test // GH-1957
2479+
@EnabledOnCommand("LPOS")
2480+
void lPos() {
2481+
2482+
nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
2483+
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), null, null);
2484+
2485+
assertThat(result).containsOnly(2L);
2486+
}
2487+
2488+
@Test // GH-1957
2489+
@EnabledOnCommand("LPOS")
2490+
void lPosRank() {
2491+
2492+
nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
2493+
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), 2, null);
2494+
2495+
assertThat(result).containsExactly(6L);
2496+
}
2497+
2498+
@Test // GH-1957
2499+
@EnabledOnCommand("LPOS")
2500+
void lPosNegativeRank() {
2501+
2502+
nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
2503+
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), -1, null);
2504+
2505+
assertThat(result).containsExactly(7L);
2506+
}
2507+
2508+
@Test // GH-1957
2509+
@EnabledOnCommand("LPOS")
2510+
void lPosCount() {
2511+
2512+
nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
2513+
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), null, 2);
2514+
2515+
assertThat(result).containsExactly(2L, 6L);
2516+
}
2517+
2518+
@Test // GH-1957
2519+
@EnabledOnCommand("LPOS")
2520+
void lPosRankCount() {
2521+
2522+
nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
2523+
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), -1, 2);
2524+
2525+
assertThat(result).containsExactly(7L, 6L);
2526+
}
2527+
2528+
@Test // GH-1957
2529+
@EnabledOnCommand("LPOS")
2530+
void lPosCountZero() {
2531+
2532+
nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
2533+
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "c".getBytes(StandardCharsets.UTF_8), null, 0);
2534+
2535+
assertThat(result).containsExactly(2L, 6L, 7L);
2536+
}
24752537
}

0 commit comments

Comments
 (0)