Redisson.findKeys method added. #194

pull/243/head
Nikita 10 years ago
parent 0d3cdf971c
commit a8782be2be

@ -69,7 +69,7 @@ public interface CommandExecutor {
<T, R> R write(String key, RedisCommand<T> command, Object ... params);
<T> Future<Queue<Object>> readAllAsync(RedisCommand<T> command, Object ... params);
<T, R> Future<Queue<R>> readAllAsync(RedisCommand<T> command, Object ... params);
<T> Future<Boolean> writeAllAsync(RedisCommand<T> command, Object ... params);

@ -65,13 +65,13 @@ public class CommandExecutorService implements CommandExecutor {
return connectionManager;
}
public <T> Future<Queue<Object>> readAllAsync(RedisCommand<T> command, Object ... params) {
final Promise<Queue<Object>> mainPromise = connectionManager.newPromise();
Promise<Object> promise = new DefaultPromise<Object>() {
Queue<Object> results = new ConcurrentLinkedQueue<Object>();
public <T, R> Future<Queue<R>> readAllAsync(RedisCommand<T> command, Object ... params) {
final Promise<Queue<R>> mainPromise = connectionManager.newPromise();
Promise<R> promise = new DefaultPromise<R>() {
Queue<R> results = new ConcurrentLinkedQueue<R>();
AtomicInteger counter = new AtomicInteger(connectionManager.getEntries().keySet().size());
@Override
public Promise<Object> setSuccess(Object result) {
public Promise<R> setSuccess(R result) {
if (result instanceof Collection) {
results.addAll((Collection)result);
} else {

@ -16,8 +16,8 @@
package org.redisson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
@ -43,6 +43,8 @@ import org.redisson.core.RSet;
import org.redisson.core.RSortedSet;
import org.redisson.core.RTopic;
import io.netty.util.concurrent.Future;
/**
* Main infrastructure class allows to get access
* to all Redisson objects on top of Redis server.
@ -115,7 +117,8 @@ public class Redisson implements RedissonClient {
*/
@Override
public <V> List<RBucket<V>> getBuckets(String pattern) {
Collection<Object> keys = commandExecutor.get(commandExecutor.readAllAsync(RedisCommands.KEYS, pattern));
Future<Queue<String>> r = commandExecutor.readAllAsync(RedisCommands.KEYS, pattern);
Queue<String> keys = commandExecutor.<Queue<String>>get(r);
List<RBucket<V>> buckets = new ArrayList<RBucket<V>>(keys.size());
for (Object key : keys) {
if(key != null) {
@ -301,6 +304,26 @@ public class Redisson implements RedissonClient {
return config;
}
/**
* Find keys by key search pattern
*
* @param pattern
* @return
*/
public Queue<String> findKeys(String pattern) {
return commandExecutor.get(findKeysAsync(pattern));
}
/**
* Find keys by key search pattern in async mode
*
* @param pattern
* @return
*/
public Future<Queue<String>> findKeysAsync(String pattern) {
return commandExecutor.readAllAsync(RedisCommands.KEYS, pattern);
}
/**
* Delete multiple objects by name
*
@ -308,7 +331,17 @@ public class Redisson implements RedissonClient {
* @return
*/
public long delete(String ... keys) {
return commandExecutor.get(commandExecutor.writeAllAsync(RedisCommands.DEL, new SlotCallback<Long, Long>() {
return commandExecutor.get(deleteAsync(keys));
}
/**
* Delete multiple objects by name in async mode
*
* @param keys - object names
* @return
*/
public Future<Long> deleteAsync(String ... keys) {
return commandExecutor.writeAllAsync(RedisCommands.DEL, new SlotCallback<Long, Long>() {
AtomicLong results = new AtomicLong();
@Override
public void onSlotResult(Long result) {
@ -319,7 +352,7 @@ public class Redisson implements RedissonClient {
public Long onFinish() {
return results.get();
}
}, (Object[])keys));
}, (Object[])keys);
}
public void flushdb() {

@ -1,5 +1,7 @@
package org.redisson;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.core.RBucket;
@ -8,9 +10,24 @@ import org.redisson.core.RMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
public class RedissonBucketTest extends BaseTest {
@Test
public void testFindKeys() {
RBucket<String> bucket = redisson.getBucket("test1");
bucket.set("someValue");
RMap<String, String> map = redisson.getMap("test2");
map.fastPut("1", "2");
Queue<String> keys = redisson.findKeys("test?");
MatcherAssert.assertThat(keys, Matchers.contains("test1", "test2"));
Queue<String> keys2 = redisson.findKeys("test");
MatcherAssert.assertThat(keys2, Matchers.empty());
}
@Test
public void testMassDelete() {
RBucket<String> bucket = redisson.getBucket("test");

Loading…
Cancel
Save