RedissonList.lastIndexOf optimization

pull/243/head
Nikita 10 years ago
parent 334d7104dd
commit b9dc83a2dc

@ -392,8 +392,14 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
@Override
public Future<Integer> lastIndexOfAsync(Object o) {
return commandExecutor.evalReadAsync(getName(), new RedisCommand<Integer>("EVAL", new IntegerReplayConvertor(), 4),
"local s = redis.call('llen', KEYS[1]);" +
"for i = s, 0, -1 do if ARGV[1] == redis.call('lindex', KEYS[1], i) then return i end end;" +
"local key = KEYS[1] " +
"local obj = ARGV[1] " +
"local items = redis.call('lrange', key, 0, -1) " +
"for i = table.getn(items), 0, -1 do " +
"if items[i] == obj then " +
"return i - 1 " +
"end " +
"end " +
"return -1",
Collections.<Object>singletonList(getName()), o);
}

@ -11,7 +11,9 @@ import java.util.ListIterator;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.core.RBatch;
import org.redisson.core.RList;
import org.redisson.core.RListAsync;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
@ -269,6 +271,18 @@ public class RedissonListTest extends BaseTest {
Assert.assertFalse(iterator.hasPrevious());
}
@Test
public void testLastIndexOfNone() {
List<Integer> list = redisson.getList("list");
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
Assert.assertEquals(-1, list.lastIndexOf(10));
}
@Test
public void testLastIndexOf2() {
List<Integer> list = redisson.getList("list");

Loading…
Cancel
Save