Skip to content

Commit

Permalink
RList.subList implemented. #60
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Jan 16, 2016
1 parent b37e988 commit b6e7cdd
Show file tree
Hide file tree
Showing 4 changed files with 736 additions and 25 deletions.
26 changes: 15 additions & 11 deletions src/main/java/org/redisson/RedissonList.java
Expand Up @@ -199,9 +199,13 @@ public Future<Boolean> addAllAsync(int index, Collection<? extends V> coll) {
"assert(tonumber(ind) <= size, 'index: ' .. ind .. ' but current size: ' .. size); " +
"local tail = redis.call('lrange', KEYS[1], ind, -1); " +
"redis.call('ltrim', KEYS[1], 0, ind - 1); " +
"redis.call('rpush', KEYS[1], unpack(ARGV));" +
"if #tail > 0 then "
+ "redis.call('rpush', KEYS[1], unpack(tail));"
"for i=1, #ARGV, 5000 do "
+ "redis.call('rpush', KEYS[1], unpack(ARGV, i, math.min(i+4999, #ARGV))); "
+ "end " +
"if #tail > 0 then " +
"for i=1, #tail, 5000 do "
+ "redis.call('rpush', KEYS[1], unpack(tail, i, math.min(i+4999, #tail))); "
+ "end "
+ "end;" +
"return 1;",
Collections.<Object>singletonList(getName()), args.toArray());
Expand Down Expand Up @@ -332,11 +336,13 @@ public V remove(int index) {

Future<V> f = commandExecutor.evalWriteAsync(getName(), codec, EVAL_OBJECT,
"local v = redis.call('lindex', KEYS[1], ARGV[1]); " +
"local tail = redis.call('lrange', KEYS[1], ARGV[1]);" +
"local tail = redis.call('lrange', KEYS[1], ARGV[1] + 1, -1);" +
"redis.call('ltrim', KEYS[1], 0, ARGV[1] - 1);" +
"if #tail > 0 then " +
"redis.call('rpush', KEYS[1], unpack(tail));" +
"end;" +
"for i=1, #tail, 5000 do "
+ "redis.call('rpush', KEYS[1], unpack(tail, i, math.min(i+4999, #tail))); "
+ "end "
+ "end;" +
"return v",
Collections.<Object>singletonList(getName()), index);
return get(f);
Expand Down Expand Up @@ -435,7 +441,7 @@ public void remove() {
if (hasBeenModified) {
throw new IllegalStateException("Element been already deleted");
}
RedissonList.this.remove(currentValueHasRead);
RedissonList.this.remove(currentIndex);
currentIndex--;
hasBeenModified = true;
currentValueHasRead = null;
Expand Down Expand Up @@ -493,9 +499,8 @@ public void add(V e) {
};
}

// TODO use RedissonList with bounds
@Override
public List<V> subList(int fromIndex, int toIndex) {
public RList<V> subList(int fromIndex, int toIndex) {
int size = size();
if (fromIndex < 0 || toIndex > size) {
throw new IndexOutOfBoundsException("fromIndex: " + fromIndex + " toIndex: " + toIndex + " size: " + size);
Expand All @@ -504,8 +509,7 @@ public List<V> subList(int fromIndex, int toIndex) {
throw new IllegalArgumentException("fromIndex: " + fromIndex + " toIndex: " + toIndex);
}

Future<List<V>> f = commandExecutor.readAsync(getName(), codec, LRANGE, getName(), fromIndex, toIndex - 1);
return get(f);
return new RedissonSubList<V>(codec, commandExecutor, getName(), fromIndex, toIndex);
}

public String toString() {
Expand Down

0 comments on commit b6e7cdd

Please sign in to comment.