RedissonSortedSet refactoring

pull/520/head
Nikita 9 years ago
parent 1f8a2e09c3
commit 601befa6ac

@ -32,7 +32,6 @@ import java.util.SortedSet;
import org.redisson.client.RedisConnection; import org.redisson.client.RedisConnection;
import org.redisson.client.codec.Codec; import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec; import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandExecutor; import org.redisson.command.CommandExecutor;
@ -191,6 +190,7 @@ public class RedissonSortedSet<V> extends RedissonObject implements RSortedSet<V
return new Iterator<V>() { return new Iterator<V>() {
private int currentIndex = ind - 1; private int currentIndex = ind - 1;
private V currentElement;
private boolean removeExecuted; private boolean removeExecuted;
@Override @Override
@ -206,7 +206,8 @@ public class RedissonSortedSet<V> extends RedissonObject implements RSortedSet<V
} }
currentIndex++; currentIndex++;
removeExecuted = false; removeExecuted = false;
return RedissonSortedSet.this.get(currentIndex); currentElement = RedissonSortedSet.this.get(currentIndex);
return currentElement;
} }
@Override @Override
@ -214,66 +215,15 @@ public class RedissonSortedSet<V> extends RedissonObject implements RSortedSet<V
if (removeExecuted) { if (removeExecuted) {
throw new IllegalStateException("Element been already deleted"); throw new IllegalStateException("Element been already deleted");
} }
RedissonSortedSet.this.remove(currentIndex); RedissonSortedSet.this.remove(currentElement);
currentIndex--; currentIndex--;
removeExecuted = true; removeExecuted = true;
} }
}; };
// Double startScore;
// RedisConnection<Object, V> connection = connectionManager.connectionReadOp();
// try {
// startScore = getScoreAtIndex(0, connection);
// } finally {
// connectionManager.releaseRead(connection);
// }
// if (startScore == null) {
// return new Iterator<V>() {
// @Override
// public boolean hasNext() {
// return false;
// }
//
// @Override
// public V next() {
// throw new NoSuchElementException();
// }
//
// @Override
// public void remove() {
// }
// };
// }
//
// return iterator(startScore, Double.MAX_VALUE);
}
private void remove(final int index) {
commandExecutor.write(getName(), codec, new SyncOperation<V>() {
@Override
public V execute(Codec codec, RedisConnection conn) {
if (index == 0) {
return conn.sync(codec, RedisCommands.LPOP, getName());
}
while (true) {
conn.sync(RedisCommands.WATCH, getName());
conn.sync(RedisCommands.MULTI);
conn.sync(RedisCommands.EVAL_VOID,
"local len = redis.call('llen', KEYS[1]);"
+ "local tail = redis.call('lrange', KEYS[1], tonumber(ARGV[1]) + 1, len);"
+ "redis.call('ltrim', KEYS[1], 0, tonumber(ARGV[1]) - 1);"
+ "if #tail > 0 then "
+ "redis.call('rpush', KEYS[1], unpack(tail)); "
+ "end;", 1, getName(), index);
if (((List<Object>)conn.sync(codec, RedisCommands.EXEC)).size() == 1) {
return null;
}
}
}
});
} }
private V get(final int index) { private V get(int index) {
return commandExecutor.read(getName(), codec, RedisCommands.LINDEX, getName(), index); return commandExecutor.read(getName(), codec, RedisCommands.LINDEX, getName(), index);
} }

Loading…
Cancel
Save