Fixed - RListMultimap.removeAll() method always deletes link to list #5872

pull/5884/head
Nikita Koksharov 9 months ago
parent 27a03c8d31
commit fc80806ad0

@ -197,9 +197,9 @@ public class RedissonListMultimap<K, V> extends RedissonMultimap<K, V> implement
@Override @Override
public RList<V> get(final K key) { public RList<V> get(K key) {
String keyHash = keyHash(key); String keyHash = keyHash(key);
final String setName = getValuesName(keyHash); String setName = getValuesName(keyHash);
return new RedissonList<V>(codec, commandExecutor, setName, null) { return new RedissonList<V>(codec, commandExecutor, setName, null) {
@ -224,19 +224,22 @@ public class RedissonListMultimap<K, V> extends RedissonMultimap<K, V> implement
return new CompletableFutureWrapper<>(false); return new CompletableFutureWrapper<>(false);
} }
List<Object> args = new ArrayList<Object>(c.size() + 1); List<Object> args = new ArrayList<>(c.size() + 1);
args.add(encodeMapKey(key)); args.add(encodeMapKey(key));
encode(args, c); encode(args, c);
return commandExecutor.evalWriteAsync(RedissonListMultimap.this.getRawName(), codec, RedisCommands.EVAL_BOOLEAN, return commandExecutor.evalWriteAsync(RedissonListMultimap.this.getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
"local v = 0 " + "local v = 0 " +
"for i = 2, #ARGV, 1 do " "for i = 2, #ARGV, 1 do "
+ "if redis.call('lrem', KEYS[2], 0, ARGV[i]) == 1 " + "if redis.call('lrem', KEYS[2], 0, ARGV[i]) == 1 then "
+ "then v = 1 end " + "v = 1; "
+ "end "
+"end "
+ "if v == 1 and redis.call('exists', KEYS[2]) == 0 then "
+ "redis.call('hdel', KEYS[1], ARGV[1]); "
+"end " +"end "
+ "redis.call('hdel', KEYS[1], ARGV[1]); "
+ "return v", + "return v",
Arrays.<Object>asList(RedissonListMultimap.this.getRawName(), setName), Arrays.asList(RedissonListMultimap.this.getRawName(), setName),
args.toArray()); args.toArray());
} }

@ -221,17 +221,24 @@ public class RedissonListMultimapTest extends RedisDockerTest {
@Test @Test
public void testRemoveAll() { public void testRemoveAll() {
RListMultimap<SimpleKey, SimpleValue> map = redisson.getListMultimap("test1"); RListMultimap<String, String> map = redisson.getListMultimap("test1");
map.put(new SimpleKey("0"), new SimpleValue("1")); map.put("0", "1");
map.put(new SimpleKey("0"), new SimpleValue("1")); map.put("0", "1");
map.put(new SimpleKey("0"), new SimpleValue("2")); map.put("0", "2");
map.put(new SimpleKey("0"), new SimpleValue("3")); map.put("0", "3");
RList<String> set = map.get("0");
set.removeAll(Arrays.asList("4", "5"));
assertThat(map.size()).isEqualTo(4);
set.removeAll(Arrays.asList("3"));
assertThat(map.size()).isEqualTo(3);
List<SimpleValue> values = map.removeAll(new SimpleKey("0")); List<String> values = map.removeAll("0");
assertThat(values).containsExactly(new SimpleValue("1"), new SimpleValue("1"), new SimpleValue("2"), new SimpleValue("3")); assertThat(values).containsExactly("1", "1", "2");
assertThat(map.size()).isZero(); assertThat(map.size()).isZero();
List<SimpleValue> values2 = map.removeAll(new SimpleKey("0")); List<String> values2 = map.removeAll("0");
assertThat(values2).isEmpty(); assertThat(values2).isEmpty();
} }

Loading…
Cancel
Save