RedissonList.lastIndexOf tests added

pull/6/head
Nikita 11 years ago
parent c611f5ffca
commit 81eb97970a

@ -10,6 +10,8 @@ import com.lambdaworks.redis.RedisConnection;
public class RedissonList<V> implements List<V> {
private int batchSize = 50;
private RedisConnection<Object, Object> connection;
private String name;
@ -160,6 +162,17 @@ public class RedissonList<V> implements List<V> {
}
}
private int div(int p, int q) {
int div = p / q;
int rem = p - q * div; // equal to p % q
if (rem == 0) {
return div;
}
return div + 1;
}
@Override
public V remove(int index) {
checkIndex(index);
@ -174,8 +187,7 @@ public class RedissonList<V> implements List<V> {
return -1;
}
int batchSize = 50;
int to = Math.max(size()/batchSize, 1);
int to = div(size(), batchSize);
for (int i = 0; i < to; i++) {
List<Object> range = connection.lrange(name, i*batchSize, i*batchSize + batchSize - 1);
int index = range.indexOf(o);
@ -193,12 +205,14 @@ public class RedissonList<V> implements List<V> {
return -1;
}
int to = Math.max(size()/100, 1);
int size = size();
int to = div(size, batchSize);
for (int i = 1; i <= to; i++) {
List<Object> range = connection.lrange(name, -i*100, 100);
int index = range.indexOf(o);
int startIndex = -i*batchSize;
List<Object> range = connection.lrange(name, startIndex, size - (i-1)*batchSize);
int index = range.lastIndexOf(o);
if (index != -1) {
return index + i*100;
return Math.max(size + startIndex, 0) + index;
}
}
@ -283,6 +297,13 @@ public class RedissonList<V> implements List<V> {
@Override
public List<V> subList(int fromIndex, int toIndex) {
int size = size();
if (fromIndex < 0 || toIndex > size) {
throw new IndexOutOfBoundsException("fromIndex: " + fromIndex + " toIndex: " + toIndex + " size: " + size);
}
if (fromIndex > toIndex) {
throw new IllegalArgumentException("fromIndex: " + fromIndex + " toIndex: " + toIndex);
}
return (List<V>) connection.lrange(name, fromIndex, toIndex - 1);
}

@ -11,6 +11,89 @@ import org.junit.Test;
public class RedissonListTest {
@Test
public void testLastIndexOf2() {
Redisson redisson = Redisson.create();
List<Integer> list = redisson.getList("list");
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(0);
list.add(7);
list.add(8);
list.add(0);
list.add(10);
int index = list.lastIndexOf(3);
Assert.assertEquals(2, index);
clear(list);
}
@Test
public void testLastIndexOf1() {
Redisson redisson = Redisson.create();
List<Integer> list = redisson.getList("list");
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(3);
list.add(7);
list.add(8);
list.add(0);
list.add(10);
int index = list.lastIndexOf(3);
Assert.assertEquals(5, index);
clear(list);
}
@Test
public void testLastIndexOf() {
Redisson redisson = Redisson.create();
List<Integer> list = redisson.getList("list");
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(3);
list.add(7);
list.add(8);
list.add(3);
list.add(10);
int index = list.lastIndexOf(3);
Assert.assertEquals(8, index);
clear(list);
}
@Test
public void testLastIndexOfList() {
List<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(3);
list.add(7);
list.add(8);
list.add(3);
list.add(10);
int index = list.lastIndexOf(3);
Assert.assertEquals(8, index);
clear(list);
}
@Test
public void testSubListList() {
List<Integer> list = new LinkedList<Integer>();

Loading…
Cancel
Save