RList.removeAll fixed

pull/243/head
Nikita 10 years ago
parent ad5385aa98
commit fbf8aa82d5

@ -25,7 +25,9 @@ import java.util.NoSuchElementException;
import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands; 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.BooleanReplayConvertor;
import org.redisson.client.protocol.convertor.Convertor;
import org.redisson.client.protocol.convertor.IntegerReplayConvertor; import org.redisson.client.protocol.convertor.IntegerReplayConvertor;
import static org.redisson.client.protocol.RedisCommands.*; import static org.redisson.client.protocol.RedisCommands.*;
@ -65,7 +67,7 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
@Override @Override
public boolean contains(Object o) { public boolean contains(Object o) {
return indexOf(o) != -1; return connectionManager.get(containsAsync(o));
} }
@Override @Override
@ -80,11 +82,11 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
} }
private List<V> readAll() { private List<V> readAll() {
return connectionManager.get(readAllAsync()); return (List<V>) connectionManager.get(readAllAsync());
} }
@Override @Override
public Future<List<V>> readAllAsync() { public Future<Collection<V>> readAllAsync() {
return connectionManager.readAsync(getName(), LRANGE, getName(), 0, -1); return connectionManager.readAsync(getName(), LRANGE, getName(), 0, -1);
} }
@ -210,15 +212,11 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
@Override @Override
public Future<Boolean> removeAllAsync(Collection<?> c) { 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), 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 " "for i = 0, table.getn(ARGV), 1 do "
+ "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 0 " + "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 1 "
+ "then v = false end " + "then v = true end "
+"end " +"end "
+ "return v ", + "return v ",
Collections.<Object>singletonList(getName()), c.toArray()); Collections.<Object>singletonList(getName()), c.toArray());
@ -352,7 +350,20 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
@Override @Override
public int indexOf(Object o) { 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 @Override

@ -470,15 +470,16 @@ public class RedissonListTest extends BaseTest {
list.add(4); list.add(4);
list.add(5); 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)); 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)); 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()); Assert.assertTrue(list.isEmpty());
} }

Loading…
Cancel
Save