RandomKey and Keys iteration fixed

pull/243/head
Nikita 10 years ago
parent c8b7d6f56d
commit 0142095c82

@ -17,7 +17,6 @@ package org.redisson;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
@ -82,8 +81,8 @@ public interface CommandExecutor {
<V> V get(Future<V> future);
<T, R> Future<R> readRandomAsync(RedisCommand<T> command, Codec codec, Object ... params);
<T, R> Future<R> readRandomAsync(RedisCommand<T> command, Object ... params);
<T, R> R read(Integer slot, Codec codec, RedisCommand<T> command, Object ... params);
<T, R> R write(Integer slot, Codec codec, RedisCommand<T> command, Object ... params);
}

@ -103,15 +103,41 @@ public class CommandExecutorService implements CommandExecutor {
return mainPromise;
}
public <T, R> Future<R> readRandomAsync(RedisCommand<T> command, Codec codec, Object ... params) {
public <T, R> Future<R> readRandomAsync(final RedisCommand<T> command, final Object ... params) {
final Promise<R> mainPromise = connectionManager.newPromise();
List<Integer> slots = new ArrayList<Integer>(connectionManager.getEntries().keySet());
final List<Integer> slots = new ArrayList<Integer>(connectionManager.getEntries().keySet());
Collections.shuffle(slots);
Integer slot = slots.get(0);
async(true, slot, null, codec, command, params, mainPromise, 0);
retryReadRandomAsync(command, mainPromise, slots, params);
return mainPromise;
}
private <R, T> void retryReadRandomAsync(final RedisCommand<T> command, final Promise<R> mainPromise,
final List<Integer> slots, final Object... params) {
final Promise<R> attemptPromise = connectionManager.newPromise();
attemptPromise.addListener(new FutureListener<R>() {
@Override
public void operationComplete(Future<R> future) throws Exception {
if (future.isSuccess()) {
if (future.getNow() == null) {
if (slots.isEmpty()) {
mainPromise.setSuccess(null);
} else {
retryReadRandomAsync(command, mainPromise, slots, params);
}
} else {
mainPromise.setSuccess(future.getNow());
}
} else {
mainPromise.setFailure(future.cause());
}
}
});
Integer slot = slots.remove(0);
async(true, slot, null, connectionManager.getCodec(), command, params, attemptPromise, 0);
}
public <T> Future<Void> writeAllAsync(RedisCommand<T> command, Object ... params) {
return writeAllAsync(command, null, params);
}
@ -177,14 +203,14 @@ public class CommandExecutorService implements CommandExecutor {
return mainPromise;
}
public <T, R> R read(Integer slot, Codec codec, RedisCommand<T> command, Object ... params) {
Future<R> res = readAsync(slot, codec, command, params);
public <T, R> R write(Integer slot, Codec codec, RedisCommand<T> command, Object ... params) {
Future<R> res = writeAsync(slot, codec, command, params);
return get(res);
}
public <T, R> Future<R> readAsync(Integer slot, Codec codec, RedisCommand<T> command, Object ... params) {
public <T, R> Future<R> writeAsync(Integer slot, Codec codec, RedisCommand<T> command, Object ... params) {
Promise<R> mainPromise = connectionManager.newPromise();
async(true, slot, null, codec, command, params, mainPromise, 0);
async(false, slot, null, codec, command, params, mainPromise, 0);
return mainPromise;
}

@ -72,9 +72,9 @@ public class RedissonKeys implements RKeys {
private ListScanResult<String> scanIterator(int slot, long startPos, String pattern) {
if (pattern == null) {
return commandExecutor.read(slot, StringCodec.INSTANCE, RedisCommands.SCAN, startPos);
return commandExecutor.write(slot, StringCodec.INSTANCE, RedisCommands.SCAN, startPos);
}
return commandExecutor.read(slot, StringCodec.INSTANCE, RedisCommands.SCAN, startPos, "MATCH", pattern);
return commandExecutor.write(slot, StringCodec.INSTANCE, RedisCommands.SCAN, startPos, "MATCH", pattern);
}
private Iterator<String> createKeysIterator(final int slot, final String pattern) {
@ -132,7 +132,7 @@ public class RedissonKeys implements RKeys {
@Override
public Future<String> randomKeyAsync() {
return commandExecutor.readRandomAsync(RedisCommands.RANDOM_KEY, StringCodec.INSTANCE);
return commandExecutor.readRandomAsync(RedisCommands.RANDOM_KEY);
}
/**

@ -43,8 +43,8 @@ import org.redisson.client.protocol.pubsub.PubSubStatusDecoder;
public interface RedisCommands {
RedisCommand<ListScanResult<String>> SCAN = new RedisCommand<ListScanResult<String>>("SCAN", new NestedMultiDecoder(new ObjectListReplayDecoder<String>(), new ListScanResultReplayDecoder()), ValueType.MAP);
RedisStrictCommand<String> RANDOM_KEY = new RedisStrictCommand<String>("RANDOMKEY");
RedisCommand<ListScanResult<String>> SCAN = new RedisCommand<ListScanResult<String>>("SCAN", new NestedMultiDecoder(new ObjectListReplayDecoder<String>(), new ListScanResultReplayDecoder()), ValueType.OBJECT);
RedisStrictCommand<String> RANDOM_KEY = new RedisStrictCommand<String>("RANDOMKEY", new StringDataDecoder());
RedisStrictCommand<String> PING = new RedisStrictCommand<String>("PING");
RedisStrictCommand<Void> UNWATCH = new RedisStrictCommand<Void>("UNWATCH", new VoidReplayConvertor());
@ -58,7 +58,7 @@ public interface RedisCommands {
RedisCommand<Boolean> SADD_SINGLE = new RedisCommand<Boolean>("SADD", new BooleanReplayConvertor(), 2);
RedisCommand<Boolean> SREM_SINGLE = new RedisCommand<Boolean>("SREM", new BooleanReplayConvertor(), 2);
RedisCommand<List<Object>> SMEMBERS = new RedisCommand<List<Object>>("SMEMBERS", new ObjectListReplayDecoder<Object>());
RedisCommand<ListScanResult<Object>> SSCAN = new RedisCommand<ListScanResult<Object>>("SSCAN", new NestedMultiDecoder(new ObjectListReplayDecoder<Object>(), new ListScanResultReplayDecoder()), ValueType.MAP);
RedisCommand<ListScanResult<Object>> SSCAN = new RedisCommand<ListScanResult<Object>>("SSCAN", new NestedMultiDecoder(new ObjectListReplayDecoder<Object>(), new ListScanResultReplayDecoder()), ValueType.OBJECT);
RedisCommand<Boolean> SISMEMBER = new RedisCommand<Boolean>("SISMEMBER", new BooleanReplayConvertor(), 2);
RedisStrictCommand<Integer> SCARD = new RedisStrictCommand<Integer>("SCARD", new IntegerReplayConvertor());
@ -152,6 +152,7 @@ public interface RedisCommands {
RedisCommand<Object> PUNSUBSCRIBE = new RedisCommand<Object>("PUNSUBSCRIBE", new PubSubStatusDecoder());
RedisStrictCommand<String> CLUSTER_NODES = new RedisStrictCommand<String>("CLUSTER", "NODES", new StringDataDecoder());
RedisStrictCommand<String> CLUSTER_INFO = new RedisStrictCommand<String>("CLUSTER", "INFO", new StringDataDecoder());
RedisStrictCommand<List<String>> SENTINEL_GET_MASTER_ADDR_BY_NAME = new RedisStrictCommand<List<String>>("SENTINEL", "GET-MASTER-ADDR-BY-NAME", new StringListReplayDecoder());
RedisStrictCommand<List<Map<String, String>>> SENTINEL_SLAVES = new RedisStrictCommand<List<Map<String, String>>>("SENTINEL", "SLAVES", new StringMapReplayDecoder());

@ -25,41 +25,6 @@ public class RedissonBucketTest extends BaseTest {
Assert.assertNull(bucket.get());
}
@Test
public void testDeleteByPattern() {
RBucket<String> bucket = redisson.getBucket("test1");
bucket.set("someValue");
RMap<String, String> map = redisson.getMap("test2");
map.fastPut("1", "2");
Assert.assertEquals(2, redisson.deleteByPattern("test?"));
}
@Test
public void testFindKeys() {
RBucket<String> bucket = redisson.getBucket("test1");
bucket.set("someValue");
RMap<String, String> map = redisson.getMap("test2");
map.fastPut("1", "2");
Collection<String> keys = redisson.findKeysByPattern("test?");
MatcherAssert.assertThat(keys, Matchers.containsInAnyOrder("test1", "test2"));
Collection<String> keys2 = redisson.findKeysByPattern("test");
MatcherAssert.assertThat(keys2, Matchers.empty());
}
@Test
public void testMassDelete() {
RBucket<String> bucket = redisson.getBucket("test");
bucket.set("someValue");
RMap<String, String> map = redisson.getMap("map2");
map.fastPut("1", "2");
Assert.assertEquals(2, redisson.delete("test", "map2"));
Assert.assertEquals(0, redisson.delete("test", "map2"));
}
@Test
public void testRenamenx() {
RBucket<String> bucket = redisson.getBucket("test");

Loading…
Cancel
Save