diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index 16ceb2238e0..c0397117adc 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -214,7 +214,7 @@ public Future addAllAsync(int index, Collection coll) { } @Override - public boolean addAll(final int index, final Collection coll) { + public boolean addAll(int index, Collection coll) { return get(addAllAsync(index, coll)); } @@ -399,6 +399,16 @@ public Future lastIndexOfAsync(Object o) { Collections.singletonList(getName()), o); } + @Override + public void trim(int fromIndex, int toIndex) { + get(trimAsync(fromIndex, toIndex)); + } + + @Override + public Future trimAsync(int fromIndex, int toIndex) { + return commandExecutor.writeAsync(getName(), codec, RedisCommands.LTRIM, getName(), fromIndex, toIndex); + } + @Override public ListIterator listIterator() { return listIterator(0); diff --git a/src/main/java/org/redisson/RedissonSubList.java b/src/main/java/org/redisson/RedissonSubList.java index c6d7ad421e6..3306d83c30c 100644 --- a/src/main/java/org/redisson/RedissonSubList.java +++ b/src/main/java/org/redisson/RedissonSubList.java @@ -35,7 +35,6 @@ import org.redisson.client.protocol.RedisCommand.ValueType; import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisStrictCommand; -import org.redisson.client.protocol.convertor.BooleanNumberReplayConvertor; import org.redisson.client.protocol.convertor.BooleanReplayConvertor; import org.redisson.client.protocol.convertor.Convertor; import org.redisson.client.protocol.convertor.IntegerReplayConvertor; @@ -51,7 +50,7 @@ * * @param the type of elements held in this collection */ -public class RedissonSubList extends RedissonExpirable implements RList { +public class RedissonSubList extends RedissonList implements RList { public static final RedisCommand EVAL_BOOLEAN_ARGS2 = new RedisCommand("EVAL", new BooleanReplayConvertor(), 5, ValueType.OBJECTS); @@ -65,11 +64,6 @@ protected RedissonSubList(Codec codec, CommandAsyncExecutor commandExecutor, Str this.toIndex.set(toIndex); } - @Override - public int size() { - return get(sizeAsync()); - } - public Future sizeAsync() { if (size != -1) { return newSucceededFuture(size); @@ -84,58 +78,16 @@ public Integer convert(Object obj) { }), getName()); } - @Override - public boolean isEmpty() { - return size() == 0; - } - - @Override - public boolean contains(Object o) { - return get(containsAsync(o)); - } - - @Override - public Iterator iterator() { - return listIterator(); - } - - @Override - public Object[] toArray() { - List list = readAll(); - return list.toArray(); - } - - @Override - public List readAll() { - return get(readAllAsync()); - } - @Override public Future> readAllAsync() { return commandExecutor.readAsync(getName(), codec, LRANGE, getName(), fromIndex, toIndex.get()-1); } - @Override - public T[] toArray(T[] a) { - List list = readAll(); - return list.toArray(a); - } - - @Override - public boolean add(V e) { - return get(addAsync(e)); - } - @Override public Future addAsync(V e) { return addAllAsync(toIndex.get() - fromIndex, Collections.singleton(e)); } - @Override - public boolean remove(Object o) { - return get(removeAsync(o)); - } - @Override public Future removeAsync(Object o) { return removeAllAsync(Collections.singleton(o), 1); @@ -162,16 +114,6 @@ public Future containsAllAsync(Collection c) { Collections.singletonList(getName()), params.toArray()); } - @Override - public boolean containsAll(Collection c) { - return get(containsAllAsync(c)); - } - - @Override - public boolean addAll(Collection c) { - return get(addAllAsync(c)); - } - @Override public Future addAllAsync(Collection c) { if (c.isEmpty()) { @@ -217,11 +159,6 @@ public Future addAllAsync(int index, Collection coll) { Collections.singletonList(getName()), args.toArray()); } - @Override - public boolean addAll(int index, Collection coll) { - return get(addAllAsync(index, coll)); - } - @Override public Future removeAllAsync(Collection c) { return removeAllAsync(c, 0); @@ -253,16 +190,6 @@ private Future removeAllAsync(Collection c, int count) { Collections.singletonList(getName()), params.toArray()); } - @Override - public boolean removeAll(Collection c) { - return get(removeAllAsync(c)); - } - - @Override - public boolean retainAll(Collection c) { - return get(retainAllAsync(c)); - } - @Override public Future retainAllAsync(Collection c) { List params = new ArrayList(); @@ -398,16 +325,6 @@ private V removeInner(int index) { return get(f); } - @Override - public int indexOf(Object o) { - return get(indexOfAsync(o)); - } - - @Override - public Future containsAsync(Object o) { - return indexOfAsync(o, new BooleanNumberReplayConvertor(-1L)); - } - private Future indexOfAsync(Object o, Convertor convertor) { return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand("EVAL", convertor, 4), "local items = redis.call('lrange', KEYS[1], tonumber(ARGV[2]), tonumber(ARGV[3])) " + @@ -420,16 +337,6 @@ private Future indexOfAsync(Object o, Convertor convertor) { Collections.singletonList(getName()), o, fromIndex, toIndex.get()-1); } - @Override - public Future indexOfAsync(Object o) { - return indexOfAsync(o, new IntegerReplayConvertor()); - } - - @Override - public int lastIndexOf(Object o) { - return get(lastIndexOfAsync(o)); - } - @Override public Future lastIndexOfAsync(Object o) { return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand("EVAL", new IntegerReplayConvertor(), 4), @@ -564,6 +471,23 @@ public RList subList(int fromIndex, int toIndex) { return new RedissonSubList(codec, commandExecutor, getName(), fromIndex, toIndex); } + @Override + public Future trimAsync(int fromIndex, int toIndex) { + if (fromIndex < this.fromIndex || toIndex >= this.toIndex.get()) { + throw new IndexOutOfBoundsException("fromIndex: " + fromIndex + " toIndex: " + toIndex); + } + if (fromIndex > toIndex) { + throw new IllegalArgumentException("fromIndex: " + fromIndex + " toIndex: " + toIndex); + } + + return super.trimAsync(fromIndex, toIndex); + } + + @Override + public void trim(int fromIndex, int toIndex) { + get(trimAsync(fromIndex, toIndex)); + } + public String toString() { Iterator it = iterator(); if (! it.hasNext()) diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index e56afa4a9c5..d1bfa7182a6 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -125,7 +125,7 @@ public interface RedisCommands { RedisCommand LINSERT = new RedisCommand("LINSERT", 3, ValueType.OBJECTS); RedisStrictCommand LLEN_INT = new RedisStrictCommand("LLEN", new IntegerReplayConvertor()); RedisStrictCommand LLEN = new RedisStrictCommand("LLEN"); - RedisStrictCommand LTRIM = new RedisStrictCommand("LTRIM", new BooleanReplayConvertor()); + RedisStrictCommand LTRIM = new RedisStrictCommand("LTRIM", new VoidReplayConvertor()); RedisStrictCommand PEXPIRE = new RedisStrictCommand("PEXPIRE", new BooleanReplayConvertor()); RedisStrictCommand PEXPIREAT = new RedisStrictCommand("PEXPIREAT", new BooleanReplayConvertor()); diff --git a/src/main/java/org/redisson/core/RList.java b/src/main/java/org/redisson/core/RList.java index 3a9b2b1b89c..efe8da1695f 100644 --- a/src/main/java/org/redisson/core/RList.java +++ b/src/main/java/org/redisson/core/RList.java @@ -38,4 +38,14 @@ public interface RList extends List, RExpirable, RListAsync, RandomAcce */ List readAll(); + /** + * Trim list and remains elements only in specified range + * fromIndex, inclusive, and toIndex, inclusive. + * + * @param fromIndex + * @param toIndex + * @return + */ + void trim(int fromIndex, int toIndex); + } diff --git a/src/main/java/org/redisson/core/RListAsync.java b/src/main/java/org/redisson/core/RListAsync.java index 155d89d8175..fec490fae8e 100644 --- a/src/main/java/org/redisson/core/RListAsync.java +++ b/src/main/java/org/redisson/core/RListAsync.java @@ -49,4 +49,14 @@ public interface RListAsync extends RCollectionAsync, RandomAccess { */ Future> readAllAsync(); + /** + * Trim list and remains elements only in specified range + * fromIndex, inclusive, and toIndex, inclusive. + * + * @param fromIndex + * @param toIndex + * @return + */ + Future trimAsync(int fromIndex, int toIndex); + } diff --git a/src/test/java/org/redisson/RedissonListTest.java b/src/test/java/org/redisson/RedissonListTest.java index 4fabbf3d8ff..9341d939ba6 100644 --- a/src/test/java/org/redisson/RedissonListTest.java +++ b/src/test/java/org/redisson/RedissonListTest.java @@ -21,6 +21,20 @@ public class RedissonListTest extends BaseTest { + @Test + public void testTrim() { + RList list = redisson.getList("list1"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + list.add("5"); + list.add("6"); + + list.trim(0, 3); + assertThat(list).containsExactly("1", "2", "3", "4"); + } + @Test public void testAddAllBigList() { RList list = redisson.getList("list1");