RedissonList refactoring

pull/6/head
Nikita 11 years ago
parent b85a9a1e30
commit 6dfa671c15

@ -1,6 +1,7 @@
package org.redisson; package org.redisson;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
@ -56,8 +57,7 @@ public class RedissonList<V> implements List<V> {
@Override @Override
public boolean add(V e) { public boolean add(V e) {
connection.rpush(name, e); return addAll(Collections.singleton(e));
return true;
} }
@Override @Override
@ -83,12 +83,27 @@ public class RedissonList<V> implements List<V> {
} }
@Override @Override
public boolean addAll(int index, Collection<? extends V> c) { public boolean addAll(int index, Collection<? extends V> coll) {
for (V v : c) { checkPosition(index);
add(index++, v); while (true) {
} if (index < size()) {
RedisConnection<Object, Object> c = redisson.connect();
c.watch(name);
List<Object> 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; return true;
} }
} else {
return addAll(coll);
}
}
}
@Override @Override
public boolean removeAll(Collection<?> c) { public boolean removeAll(Collection<?> c) {
@ -157,19 +172,7 @@ public class RedissonList<V> implements List<V> {
@Override @Override
public void add(int index, V element) { public void add(int index, V element) {
checkPosition(index); addAll(index, Collections.singleton(element));
if (index < size()) {
RedisConnection<Object, Object> c = redisson.connect();
c.watch(name);
List<Object> 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);
}
} }
private int div(int p, int q) { private int div(int p, int q) {

Loading…
Cancel
Save