Feature - RKeys.getKeysWithLimit() method added. #3007

pull/3048/head
Nikita Koksharov 5 years ago
parent facfff5493
commit e7eecb5da5

@ -95,10 +95,10 @@ public class RedissonKeys implements RKeys {
@Override
public Iterable<String> getKeysByPattern(String pattern, int count) {
return getKeysByPattern(RedisCommands.SCAN, pattern, count);
return getKeysByPattern(RedisCommands.SCAN, pattern, 0, count);
}
public <T> Iterable<T> getKeysByPattern(RedisCommand<?> command, String pattern, int count) {
public <T> Iterable<T> getKeysByPattern(RedisCommand<?> command, String pattern, int limit, int count) {
List<Iterable<T>> iterables = new ArrayList<>();
for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
Iterable<T> iterable = new Iterable<T>() {
@ -109,7 +109,17 @@ public class RedissonKeys implements RKeys {
};
iterables.add(iterable);
}
return new CompositeIterable<T>(iterables);
return new CompositeIterable<T>(iterables, limit);
}
@Override
public Iterable<String> getKeysWithLimit(int limit) {
return getKeysWithLimit(null, limit);
}
@Override
public Iterable<String> getKeysWithLimit(String pattern, int limit) {
return getKeysByPattern(RedisCommands.SCAN, pattern, limit, limit);
}
@Override

@ -628,7 +628,7 @@ public class RedissonLiveObjectService implements RLiveObjectService {
}
});
return keys.getKeysByPattern(command, pattern, count);
return keys.getKeysByPattern(command, pattern, 0, count);
} catch (NoSuchFieldException e) {
throw new IllegalStateException(e);
}

@ -25,6 +25,33 @@ import java.util.stream.Stream;
*/
public interface RKeys extends RKeysAsync {
/**
* Get keys using iterator with defined <code>limit</code>.
* Keys are traversed with SCAN operation.
*
* @param limit - limit of keys amount
* @return Iterable object
*/
Iterable<String> getKeysWithLimit(int limit);
/**
* Get keys using iterator with defined <code>limit</code>.
* Keys are traversed with SCAN operation.
* <p>
* Supported glob-style patterns:
* <p>
* h?llo subscribes to hello, hallo and hxllo
* <p>
* h*llo subscribes to hllo and heeeello
* <p>
* h[ae]llo subscribes to hello and hallo, but not hillo
*
* @param limit - limit of keys amount
* @param pattern - match pattern
* @return Iterable object
*/
Iterable<String> getKeysWithLimit(String pattern, int limit);
/**
* Move object to another database
*
@ -183,7 +210,7 @@ public interface RKeys extends RKeysAsync {
* @return Iterable object
*/
Iterable<String> getKeysByPattern(String pattern, int count);
/**
* Get all keys using iterator. Keys traversing with SCAN operation.
* Each SCAN operation loads up to <code>10</code> keys per request.

@ -23,11 +23,17 @@ public class CompositeIterable<T> implements Iterable<T> {
private List<Iterable<T>> iterablesList;
private Iterable<T>[] iterables;
private int limit;
public CompositeIterable(List<Iterable<T>> iterables) {
this.iterablesList = iterables;
}
public CompositeIterable(List<Iterable<T>> iterables, int limit) {
this.iterablesList = iterables;
this.limit = limit;
}
public CompositeIterable(Iterable<T>... iterables) {
this.iterables = iterables;
}
@ -50,6 +56,6 @@ public class CompositeIterable<T> implements Iterable<T> {
}
}
Iterator<Iterator<T>> listIterator = iterators.iterator();
return new CompositeIterator<T>(listIterator);
return new CompositeIterator<T>(listIterator, limit);
}
}

@ -25,9 +25,12 @@ public class CompositeIterator<T> implements Iterator<T> {
private Iterator<Iterator<T>> listIterator;
private Iterator<T> currentIterator;
private int limit;
private int counter;
public CompositeIterator(Iterator<Iterator<T>> iterators) {
public CompositeIterator(Iterator<Iterator<T>> iterators, int limit) {
listIterator = iterators;
this.limit = limit;
}
@Override
@ -37,13 +40,24 @@ public class CompositeIterator<T> implements Iterator<T> {
Iterator<T> iterator = listIterator.next();
currentIterator = iterator;
if (iterator.hasNext()) {
return true;
if (limit == 0) {
return true;
} else {
return limit >= counter + 1;
}
}
}
return false;
}
return currentIterator.hasNext();
if (currentIterator.hasNext()) {
if (limit == 0) {
return true;
} else {
return limit >= counter + 1;
}
}
return false;
}
@Override
@ -52,6 +66,7 @@ public class CompositeIterator<T> implements Iterator<T> {
throw new NoSuchElementException();
}
counter++;
return currentIterator.next();
}

@ -4,7 +4,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@ -25,6 +24,41 @@ import org.redisson.connection.balancer.RandomLoadBalancer;
public class RedissonKeysTest extends BaseTest {
@Test
public void testReadKeys() {
for (int i = 0; i < 10; i++) {
redisson.getBucket("test" + i).set(i);
}
Iterable<String> keys = redisson.getKeys().getKeysWithLimit(3);
assertThat(keys).hasSize(3);
Iterable<String> keys2 = redisson.getKeys().getKeysWithLimit(20);
assertThat(keys2).hasSize(10);
}
@Test
public void testReadKeysPattern() {
for (int i = 0; i < 10; i++) {
redisson.getBucket("test" + i).set(i);
}
for (int i = 0; i < 5; i++) {
redisson.getBucket("red" + i).set(i);
}
Iterable<String> keys = redisson.getKeys().getKeysWithLimit("test*", 3);
assertThat(keys).hasSize(3);
Iterable<String> keys2 = redisson.getKeys().getKeysWithLimit("test*", 20);
assertThat(keys2).hasSize(10);
Iterable<String> keys3 = redisson.getKeys().getKeysWithLimit("red*", 3);
assertThat(keys3).hasSize(3);
Iterable<String> keys4 = redisson.getKeys().getKeysWithLimit("red*", 10);
assertThat(keys4).hasSize(5);
}
@Test
public void testTouch() {
redisson.getSet("test").add("1");

Loading…
Cancel
Save