RKeys.keysIterable by pattern added. #203

pull/243/head
Nikita 10 years ago
parent c7e2497bf8
commit 577edd2470

@ -25,6 +25,20 @@ public class RedissonKeys implements RKeys {
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
} }
@Override
public Iterable<String> keysIterable(final String pattern) {
List<Iterable<String>> iterables = new ArrayList<Iterable<String>>();
for (final Integer slot : commandExecutor.getConnectionManager().getEntries().keySet()) {
Iterable<String> iterable = new Iterable<String>() {
@Override
public Iterator<String> iterator() {
return createKeysIterator(slot, pattern);
}
};
iterables.add(iterable);
}
return new CompositeIterable<String>(iterables);
}
@Override @Override
public Iterable<String> keysIterable() { public Iterable<String> keysIterable() {
@ -33,7 +47,7 @@ public class RedissonKeys implements RKeys {
Iterable<String> iterable = new Iterable<String>() { Iterable<String> iterable = new Iterable<String>() {
@Override @Override
public Iterator<String> iterator() { public Iterator<String> iterator() {
return createKeysIterator(slot); return createKeysIterator(slot, null);
} }
}; };
iterables.add(iterable); iterables.add(iterable);
@ -41,11 +55,14 @@ public class RedissonKeys implements RKeys {
return new CompositeIterable<String>(iterables); return new CompositeIterable<String>(iterables);
} }
private ListScanResult<String> scanIterator(int slot, long startPos) { private ListScanResult<String> scanIterator(int slot, long startPos, String pattern) {
return commandExecutor.read(slot, StringCodec.INSTANCE, RedisCommands.SCAN, startPos); if (pattern == null) {
return commandExecutor.read(slot, StringCodec.INSTANCE, RedisCommands.SCAN, startPos);
}
return commandExecutor.read(slot, StringCodec.INSTANCE, RedisCommands.SCAN, startPos, "MATCH", pattern);
} }
private Iterator<String> createKeysIterator(final int slot) { private Iterator<String> createKeysIterator(final int slot, final String pattern) {
return new Iterator<String>() { return new Iterator<String>() {
private Iterator<String> iter; private Iterator<String> iter;
@ -57,11 +74,11 @@ public class RedissonKeys implements RKeys {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
if (iter == null) { if (iter == null) {
ListScanResult<String> res = scanIterator(slot, 0); ListScanResult<String> res = scanIterator(slot, 0, pattern);
iter = res.getValues().iterator(); iter = res.getValues().iterator();
iterPos = res.getPos(); iterPos = res.getPos();
} else if (!iter.hasNext() && iterPos != 0) { } else if (!iter.hasNext() && iterPos != 0) {
ListScanResult<String> res = scanIterator(slot, iterPos); ListScanResult<String> res = scanIterator(slot, iterPos, pattern);
iter = res.getValues().iterator(); iter = res.getValues().iterator();
iterPos = res.getPos(); iterPos = res.getPos();
} }

@ -4,8 +4,30 @@ import java.util.Collection;
public interface RKeys extends RKeysAsync { public interface RKeys extends RKeysAsync {
/**
* Get keys by pattern using iterator. Keys traversing with SCAN operation
*
* Supported glob-style patterns:
* h?llo subscribes to hello, hallo and hxllo
* h*llo subscribes to hllo and heeeello
* h[ae]llo subscribes to hello and hallo, but not hillo
*
* @return
*/
Iterable<String> keysIterable(String pattern);
/**
* Get keys using iterator. Keys traversing with SCAN operation
*
* @return
*/
Iterable<String> keysIterable(); Iterable<String> keysIterable();
/**
* Get random key
*
* @return
*/
String randomKey(); String randomKey();
/** /**

@ -6,6 +6,11 @@ import io.netty.util.concurrent.Future;
public interface RKeysAsync { public interface RKeysAsync {
/**
* Get random key in async mode
*
* @return
*/
Future<String> randomKeyAsync(); Future<String> randomKeyAsync();
/** /**

Loading…
Cancel
Save