Skip to content

Commit

Permalink
trim and trimAsync methods added to RList. #364
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Jan 21, 2016
1 parent a921cb0 commit 19b3130
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 96 deletions.
12 changes: 11 additions & 1 deletion src/main/java/org/redisson/RedissonList.java
Expand Up @@ -214,7 +214,7 @@ public Future<Boolean> addAllAsync(int index, Collection<? extends V> coll) {
}

@Override
public boolean addAll(final int index, final Collection<? extends V> coll) {
public boolean addAll(int index, Collection<? extends V> coll) {
return get(addAllAsync(index, coll));
}

Expand Down Expand Up @@ -399,6 +399,16 @@ public Future<Integer> lastIndexOfAsync(Object o) {
Collections.<Object>singletonList(getName()), o);
}

@Override
public void trim(int fromIndex, int toIndex) {
get(trimAsync(fromIndex, toIndex));
}

@Override
public Future<Void> trimAsync(int fromIndex, int toIndex) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.LTRIM, getName(), fromIndex, toIndex);
}

@Override
public ListIterator<V> listIterator() {
return listIterator(0);
Expand Down
112 changes: 18 additions & 94 deletions src/main/java/org/redisson/RedissonSubList.java
Expand Up @@ -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;
Expand All @@ -51,7 +50,7 @@
*
* @param <V> the type of elements held in this collection
*/
public class RedissonSubList<V> extends RedissonExpirable implements RList<V> {
public class RedissonSubList<V> extends RedissonList<V> implements RList<V> {

public static final RedisCommand<Boolean> EVAL_BOOLEAN_ARGS2 = new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 5, ValueType.OBJECTS);

Expand All @@ -65,11 +64,6 @@ protected RedissonSubList(Codec codec, CommandAsyncExecutor commandExecutor, Str
this.toIndex.set(toIndex);
}

@Override
public int size() {
return get(sizeAsync());
}

public Future<Integer> sizeAsync() {
if (size != -1) {
return newSucceededFuture(size);
Expand All @@ -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<V> iterator() {
return listIterator();
}

@Override
public Object[] toArray() {
List<V> list = readAll();
return list.toArray();
}

@Override
public List<V> readAll() {
return get(readAllAsync());
}

@Override
public Future<List<V>> readAllAsync() {
return commandExecutor.readAsync(getName(), codec, LRANGE, getName(), fromIndex, toIndex.get()-1);
}

@Override
public <T> T[] toArray(T[] a) {
List<V> list = readAll();
return list.toArray(a);
}

@Override
public boolean add(V e) {
return get(addAsync(e));
}

@Override
public Future<Boolean> addAsync(V e) {
return addAllAsync(toIndex.get() - fromIndex, Collections.singleton(e));
}

@Override
public boolean remove(Object o) {
return get(removeAsync(o));
}

@Override
public Future<Boolean> removeAsync(Object o) {
return removeAllAsync(Collections.singleton(o), 1);
Expand All @@ -162,16 +114,6 @@ public Future<Boolean> containsAllAsync(Collection<?> c) {
Collections.<Object>singletonList(getName()), params.toArray());
}

@Override
public boolean containsAll(Collection<?> c) {
return get(containsAllAsync(c));
}

@Override
public boolean addAll(Collection<? extends V> c) {
return get(addAllAsync(c));
}

@Override
public Future<Boolean> addAllAsync(Collection<? extends V> c) {
if (c.isEmpty()) {
Expand Down Expand Up @@ -217,11 +159,6 @@ public Future<Boolean> addAllAsync(int index, Collection<? extends V> coll) {
Collections.<Object>singletonList(getName()), args.toArray());
}

@Override
public boolean addAll(int index, Collection<? extends V> coll) {
return get(addAllAsync(index, coll));
}

@Override
public Future<Boolean> removeAllAsync(Collection<?> c) {
return removeAllAsync(c, 0);
Expand Down Expand Up @@ -253,16 +190,6 @@ private Future<Boolean> removeAllAsync(Collection<?> c, int count) {
Collections.<Object>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<Boolean> retainAllAsync(Collection<?> c) {
List<Object> params = new ArrayList<Object>();
Expand Down Expand Up @@ -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<Boolean> containsAsync(Object o) {
return indexOfAsync(o, new BooleanNumberReplayConvertor(-1L));
}

private <R> Future<R> indexOfAsync(Object o, Convertor<R> convertor) {
return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<R>("EVAL", convertor, 4),
"local items = redis.call('lrange', KEYS[1], tonumber(ARGV[2]), tonumber(ARGV[3])) " +
Expand All @@ -420,16 +337,6 @@ private <R> Future<R> indexOfAsync(Object o, Convertor<R> convertor) {
Collections.<Object>singletonList(getName()), o, fromIndex, toIndex.get()-1);
}

@Override
public Future<Integer> indexOfAsync(Object o) {
return indexOfAsync(o, new IntegerReplayConvertor());
}

@Override
public int lastIndexOf(Object o) {
return get(lastIndexOfAsync(o));
}

@Override
public Future<Integer> lastIndexOfAsync(Object o) {
return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<Integer>("EVAL", new IntegerReplayConvertor(), 4),
Expand Down Expand Up @@ -564,6 +471,23 @@ public RList<V> subList(int fromIndex, int toIndex) {
return new RedissonSubList<V>(codec, commandExecutor, getName(), fromIndex, toIndex);
}

@Override
public Future<Void> 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<V> it = iterator();
if (! it.hasNext())
Expand Down
Expand Up @@ -125,7 +125,7 @@ public interface RedisCommands {
RedisCommand<Object> LINSERT = new RedisCommand<Object>("LINSERT", 3, ValueType.OBJECTS);
RedisStrictCommand<Integer> LLEN_INT = new RedisStrictCommand<Integer>("LLEN", new IntegerReplayConvertor());
RedisStrictCommand<Long> LLEN = new RedisStrictCommand<Long>("LLEN");
RedisStrictCommand<Boolean> LTRIM = new RedisStrictCommand<Boolean>("LTRIM", new BooleanReplayConvertor());
RedisStrictCommand<Void> LTRIM = new RedisStrictCommand<Void>("LTRIM", new VoidReplayConvertor());

RedisStrictCommand<Boolean> PEXPIRE = new RedisStrictCommand<Boolean>("PEXPIRE", new BooleanReplayConvertor());
RedisStrictCommand<Boolean> PEXPIREAT = new RedisStrictCommand<Boolean>("PEXPIREAT", new BooleanReplayConvertor());
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/redisson/core/RList.java
Expand Up @@ -38,4 +38,14 @@ public interface RList<V> extends List<V>, RExpirable, RListAsync<V>, RandomAcce
*/
List<V> readAll();

/**
* Trim list and remains elements only in specified range
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, inclusive.
*
* @param fromIndex
* @param toIndex
* @return
*/
void trim(int fromIndex, int toIndex);

}
10 changes: 10 additions & 0 deletions src/main/java/org/redisson/core/RListAsync.java
Expand Up @@ -49,4 +49,14 @@ public interface RListAsync<V> extends RCollectionAsync<V>, RandomAccess {
*/
Future<List<V>> readAllAsync();

/**
* Trim list and remains elements only in specified range
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, inclusive.
*
* @param fromIndex
* @param toIndex
* @return
*/
Future<Void> trimAsync(int fromIndex, int toIndex);

}
14 changes: 14 additions & 0 deletions src/test/java/org/redisson/RedissonListTest.java
Expand Up @@ -21,6 +21,20 @@

public class RedissonListTest extends BaseTest {

@Test
public void testTrim() {
RList<String> 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<String> list = redisson.getList("list1");
Expand Down

0 comments on commit 19b3130

Please sign in to comment.