diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index 59b0edfb6..8a691f482 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -25,7 +25,9 @@ import java.util.NoSuchElementException; import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommands; +import org.redisson.client.protocol.convertor.BooleanNumberReplayConvertor; import org.redisson.client.protocol.convertor.BooleanReplayConvertor; +import org.redisson.client.protocol.convertor.Convertor; import org.redisson.client.protocol.convertor.IntegerReplayConvertor; import static org.redisson.client.protocol.RedisCommands.*; @@ -65,7 +67,7 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> { @Override public boolean contains(Object o) { - return indexOf(o) != -1; + return connectionManager.get(containsAsync(o)); } @Override @@ -80,11 +82,11 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> { } private List<V> readAll() { - return connectionManager.get(readAllAsync()); + return (List<V>) connectionManager.get(readAllAsync()); } @Override - public Future<List<V>> readAllAsync() { + public Future<Collection<V>> readAllAsync() { return connectionManager.readAsync(getName(), LRANGE, getName(), 0, -1); } @@ -210,15 +212,11 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> { @Override public Future<Boolean> removeAllAsync(Collection<?> c) { - if (c.isEmpty()) { - return connectionManager.getGroup().next().newSucceededFuture(false); - } - return connectionManager.evalWriteAsync(getName(), new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 4), - "local v = true " + + "local v = false " + "for i = 0, table.getn(ARGV), 1 do " - + "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 0 " - + "then v = false end " + + "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 1 " + + "then v = true end " +"end " + "return v ", Collections.<Object>singletonList(getName()), c.toArray()); @@ -352,7 +350,20 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> { @Override public int indexOf(Object o) { - return connectionManager.get(indexOfAsync(o)); + return connectionManager.get(indexOfAsync(o, new IntegerReplayConvertor())); + } + + @Override + public Future<Boolean> containsAsync(Object o) { + return indexOfAsync(o, new BooleanNumberReplayConvertor()); + } + + private <R> Future<R> indexOfAsync(Object o, Convertor<R> convertor) { + return connectionManager.evalReadAsync(getName(), new RedisCommand<R>("EVAL", convertor, 4), + "local s = redis.call('llen', KEYS[1]);" + + "for i = 0, s, 1 do if ARGV[1] == redis.call('lindex', KEYS[1], i) then return i end end;" + + "return -1", + Collections.<Object>singletonList(getName()), o); } @Override diff --git a/src/test/java/org/redisson/RedissonListTest.java b/src/test/java/org/redisson/RedissonListTest.java index ce161811a..9b3c9081d 100644 --- a/src/test/java/org/redisson/RedissonListTest.java +++ b/src/test/java/org/redisson/RedissonListTest.java @@ -470,15 +470,16 @@ public class RedissonListTest extends BaseTest { list.add(4); list.add(5); - list.removeAll(Arrays.asList(3, 2, 10, 6)); + Assert.assertFalse(list.removeAll(Collections.emptyList())); + Assert.assertTrue(list.removeAll(Arrays.asList(3, 2, 10, 6))); Assert.assertThat(list, Matchers.contains(1, 4, 5)); - list.removeAll(Arrays.asList(4)); + Assert.assertTrue(list.removeAll(Arrays.asList(4))); Assert.assertThat(list, Matchers.contains(1, 5)); - list.removeAll(Arrays.asList(1, 5, 1, 5)); + Assert.assertTrue(list.removeAll(Arrays.asList(1, 5, 1, 5))); Assert.assertTrue(list.isEmpty()); }