diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index 48457fbe9..c47f25e78 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -1,6 +1,7 @@ package org.redisson; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -56,8 +57,7 @@ public class RedissonList implements List { @Override public boolean add(V e) { - connection.rpush(name, e); - return true; + return addAll(Collections.singleton(e)); } @Override @@ -83,11 +83,26 @@ public class RedissonList implements List { } @Override - public boolean addAll(int index, Collection c) { - for (V v : c) { - add(index++, v); + public boolean addAll(int index, Collection coll) { + checkPosition(index); + while (true) { + if (index < size()) { + RedisConnection c = redisson.connect(); + + c.watch(name); + List tail = c.lrange(name, index, size()); + + c.multi(); + c.ltrim(name, 0, index - 1); + c.rpush(name, coll.toArray()); + c.rpush(name, tail.toArray()); + if (c.exec().size() == 3) { + return true; + } + } else { + return addAll(coll); + } } - return true; } @Override @@ -157,19 +172,7 @@ public class RedissonList implements List { @Override public void add(int index, V element) { - checkPosition(index); - if (index < size()) { - RedisConnection c = redisson.connect(); - c.watch(name); - List tail = c.lrange(name, index, size()); - c.multi(); - c.ltrim(name, 0, index - 1); - c.rpush(name, element); - c.rpush(name, tail.toArray()); - c.exec(); - } else { - add(element); - } + addAll(index, Collections.singleton(element)); } private int div(int p, int q) {