Skip to content

Commit 840c7f5

Browse files
christophstroblmp911de
authored andcommitted
Fix LPOS when member not in list.
This commit ensures an empty List is returned in the case the member does not exist in the target list. See #1957. Original pull request: #1962.
1 parent 19eb093 commit 840c7f5

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public List<Long> lPos(byte[] key, byte[] element, @Nullable Integer rank, @Null
7272
return connection.invoke().just(BinaryJedis::lpos, MultiKeyPipelineBase::lpos, key, element, params, count);
7373
}
7474

75-
return connection.invoke().from(BinaryJedis::lpos, MultiKeyPipelineBase::lpos, key, element, params).get(Collections::singletonList);
75+
return connection.invoke().from(BinaryJedis::lpos, MultiKeyPipelineBase::lpos, key, element, params).getOrElse(Collections::singletonList, Collections::emptyList);
7676
}
7777

7878
/*

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceListCommands.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public List<Long> lPos(byte[] key, byte[] element, @Nullable Integer rank, @Null
7171
return connection.invoke().just(RedisListAsyncCommands::lpos, key, element, count, args);
7272
}
7373

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

7777
/*

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

+10
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,16 @@ void lPosCountZero() {
14641464
assertThat((List<Long>) getResults().get(1)).containsExactly(2L, 6L, 7L);
14651465
}
14661466

1467+
@Test // GH-1957
1468+
@EnabledOnCommand("LPOS")
1469+
void lPosNonExisting() {
1470+
1471+
actual.add(connection.rPush("mylist", "a", "b", "c", "1", "2", "3", "c", "c"));
1472+
actual.add(connection.lPos("mylist", "x", null, null));
1473+
1474+
assertThat((List<Long>) getResults().get(1)).isEmpty();
1475+
}
1476+
14671477
// Set operations
14681478

14691479
@Test

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

+10
Original file line numberDiff line numberDiff line change
@@ -2534,4 +2534,14 @@ void lPosCountZero() {
25342534

25352535
assertThat(result).containsExactly(2L, 6L, 7L);
25362536
}
2537+
2538+
@Test // GH-1957
2539+
@EnabledOnCommand("LPOS")
2540+
void lPosNonExisting() {
2541+
2542+
nativeConnection.rpush(KEY_1, "a", "b", "c", "1", "2", "3", "c", "c");
2543+
List<Long> result = clusterConnection.listCommands().lPos(KEY_1_BYTES, "x".getBytes(StandardCharsets.UTF_8), null, null);
2544+
2545+
assertThat(result).isEmpty();
2546+
}
25372547
}

0 commit comments

Comments
 (0)