Skip to content

Commit

Permalink
Redisson.findKeys method added. #194
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Jul 23, 2015
1 parent 0d3cdf9 commit a8782be
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/redisson/CommandExecutor.java
Expand Up @@ -69,7 +69,7 @@ public interface CommandExecutor {


<T, R> R write(String key, RedisCommand<T> command, Object ... params); <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); <T> Future<Boolean> writeAllAsync(RedisCommand<T> command, Object ... params);


Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/redisson/CommandExecutorService.java
Expand Up @@ -65,13 +65,13 @@ public ConnectionManager getConnectionManager() {
return connectionManager; return connectionManager;
} }


public <T> Future<Queue<Object>> readAllAsync(RedisCommand<T> command, Object ... params) { public <T, R> Future<Queue<R>> readAllAsync(RedisCommand<T> command, Object ... params) {
final Promise<Queue<Object>> mainPromise = connectionManager.newPromise(); final Promise<Queue<R>> mainPromise = connectionManager.newPromise();
Promise<Object> promise = new DefaultPromise<Object>() { Promise<R> promise = new DefaultPromise<R>() {
Queue<Object> results = new ConcurrentLinkedQueue<Object>(); Queue<R> results = new ConcurrentLinkedQueue<R>();
AtomicInteger counter = new AtomicInteger(connectionManager.getEntries().keySet().size()); AtomicInteger counter = new AtomicInteger(connectionManager.getEntries().keySet().size());
@Override @Override
public Promise<Object> setSuccess(Object result) { public Promise<R> setSuccess(R result) {
if (result instanceof Collection) { if (result instanceof Collection) {
results.addAll((Collection)result); results.addAll((Collection)result);
} else { } else {
Expand Down
41 changes: 37 additions & 4 deletions src/main/java/org/redisson/Redisson.java
Expand Up @@ -16,8 +16,8 @@
package org.redisson; package org.redisson;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;


Expand All @@ -43,6 +43,8 @@
import org.redisson.core.RSortedSet; import org.redisson.core.RSortedSet;
import org.redisson.core.RTopic; import org.redisson.core.RTopic;


import io.netty.util.concurrent.Future;

/** /**
* Main infrastructure class allows to get access * Main infrastructure class allows to get access
* to all Redisson objects on top of Redis server. * to all Redisson objects on top of Redis server.
Expand Down Expand Up @@ -115,7 +117,8 @@ public <V> RBucket<V> getBucket(String name) {
*/ */
@Override @Override
public <V> List<RBucket<V>> getBuckets(String pattern) { 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()); List<RBucket<V>> buckets = new ArrayList<RBucket<V>>(keys.size());
for (Object key : keys) { for (Object key : keys) {
if(key != null) { if(key != null) {
Expand Down Expand Up @@ -301,14 +304,44 @@ public Config getConfig() {
return config; 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 * Delete multiple objects by name
* *
* @param keys - object names * @param keys - object names
* @return * @return
*/ */
public long delete(String ... keys) { 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(); AtomicLong results = new AtomicLong();
@Override @Override
public void onSlotResult(Long result) { public void onSlotResult(Long result) {
Expand All @@ -319,7 +352,7 @@ public void onSlotResult(Long result) {
public Long onFinish() { public Long onFinish() {
return results.get(); return results.get();
} }
}, (Object[])keys)); }, (Object[])keys);
} }


public void flushdb() { public void flushdb() {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/redisson/RedissonBucketTest.java
@@ -1,5 +1,7 @@
package org.redisson; package org.redisson;


import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.redisson.core.RBucket; import org.redisson.core.RBucket;
Expand All @@ -8,9 +10,24 @@
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Queue;


public class RedissonBucketTest extends BaseTest { 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 @Test
public void testMassDelete() { public void testMassDelete() {
RBucket<String> bucket = redisson.getBucket("test"); RBucket<String> bucket = redisson.getBucket("test");
Expand Down

0 comments on commit a8782be

Please sign in to comment.