Feature - RSet.random method limited by count

pull/1721/head
Nikita Koksharov 6 years ago
parent 6c2cc70914
commit 84350aad69

@ -273,14 +273,14 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
}
@Override
public Long addAll(Map<V, Double> objects) {
public int addAll(Map<V, Double> objects) {
return get(addAllAsync(objects));
}
@Override
public RFuture<Long> addAllAsync(Map<V, Double> objects) {
public RFuture<Integer> addAllAsync(Map<V, Double> objects) {
if (objects.isEmpty()) {
return RedissonPromise.newSucceededFuture(0L);
return RedissonPromise.newSucceededFuture(0);
}
List<Object> params = new ArrayList<Object>(objects.size()*2+1);
params.add(getName());
@ -289,7 +289,7 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
params.add(encode(entry.getKey()));
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZADD, params.toArray());
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZADD_INT, params.toArray());
}
@Override
@ -705,12 +705,12 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
}
@Override
public Long count(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) {
public int count(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) {
return get(countAsync(startScore, startScoreInclusive, endScore, endScoreInclusive));
}
@Override
public RFuture<Long> countAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) {
public RFuture<Integer> countAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) {
String startValue = value(startScore, startScoreInclusive);
String endValue = value(endScore, endScoreInclusive);
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZCOUNT, getName(), startValue, endValue);

@ -190,6 +190,16 @@ public class RedissonSet<V> extends RedissonExpirable implements RSet<V>, ScanIt
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SRANDMEMBER_SINGLE, getName());
}
@Override
public Set<V> random(int count) {
return get(randomAsync(count));
}
@Override
public RFuture<Set<V>> randomAsync(int count) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SRANDMEMBER, getName(), count);
}
@Override
public RFuture<Boolean> removeAsync(Object o) {
return commandExecutor.writeAsync(getName(o), codec, RedisCommands.SREM_SINGLE, getName(o), encode(o));

@ -303,6 +303,16 @@ public class RedissonSetMultimapValues<V> extends RedissonExpirable implements R
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SRANDMEMBER_SINGLE, getName());
}
@Override
public Set<V> random(int count) {
return get(randomAsync(count));
}
@Override
public RFuture<Set<V>> randomAsync(int count) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SRANDMEMBER, getName(), count);
}
@Override
public RFuture<Boolean> removeAsync(Object o) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,

@ -162,7 +162,7 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
*/
Double lastScore();
Long addAll(Map<V, Double> objects);
int addAll(Map<V, Double> objects);
int removeRangeByScore(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive);
@ -370,7 +370,7 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
* @param endScoreInclusive - end score inclusive
* @return count of elements
*/
Long count(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive);
int count(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive);
/**
* Read all values at once.

@ -151,7 +151,7 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
*/
RFuture<Double> lastScoreAsync();
RFuture<Long> addAllAsync(Map<V, Double> objects);
RFuture<Integer> addAllAsync(Map<V, Double> objects);
RFuture<Integer> removeRangeByScoreAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive);
@ -312,7 +312,7 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
* @param endScoreInclusive - end score inclusive
* @return count
*/
RFuture<Long> countAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive);
RFuture<Integer> countAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive);
/**
* Read all values at once.

@ -96,6 +96,14 @@ public interface RSet<V> extends Set<V>, RExpirable, RSetAsync<V>, RSortable<Set
*/
V random();
/**
* Returns random elements from set limited by <code>count</code>
*
* @param count - values amount to return
* @return value
*/
Set<V> random(int count);
/**
* Move a member from this set to the given destination set in.
*

@ -50,6 +50,14 @@ public interface RSetAsync<V> extends RCollectionAsync<V>, RSortableAsync<Set<V>
* @return value
*/
RFuture<V> randomAsync();
/**
* Returns random elements from set limited by <code>count</code>
*
* @param count - values amount to return
* @return value
*/
RFuture<Set<V>> randomAsync(int count);
/**
* Move a member from this set to the given destination set in async mode.

@ -120,7 +120,7 @@ public interface RedisCommands {
RedisCommand<Boolean> ZREM = new RedisCommand<Boolean>("ZREM", new BooleanAmountReplayConvertor());
RedisStrictCommand<Integer> ZCARD_INT = new RedisStrictCommand<Integer>("ZCARD", new IntegerReplayConvertor());
RedisStrictCommand<Long> ZCARD = new RedisStrictCommand<Long>("ZCARD");
RedisStrictCommand<Long> ZCOUNT = new RedisStrictCommand<Long>("ZCOUNT");
RedisStrictCommand<Integer> ZCOUNT = new RedisStrictCommand<Integer>("ZCOUNT", new IntegerReplayConvertor());
RedisStrictCommand<Integer> ZLEXCOUNT = new RedisStrictCommand<Integer>("ZLEXCOUNT", new IntegerReplayConvertor());
RedisCommand<Boolean> ZSCORE_CONTAINS = new RedisCommand<Boolean>("ZSCORE", new BooleanNotNullReplayConvertor());
RedisStrictCommand<Double> ZSCORE = new RedisStrictCommand<Double>("ZSCORE", new DoubleReplayConvertor());
@ -169,7 +169,7 @@ public interface RedisCommands {
RedisCommand<Boolean> SREM_SINGLE = new RedisCommand<Boolean>("SREM", new BooleanAmountReplayConvertor());
RedisCommand<Boolean> SMOVE = new RedisCommand<Boolean>("SMOVE", new BooleanReplayConvertor());
RedisCommand<Set<Object>> SMEMBERS = new RedisCommand<Set<Object>>("SMEMBERS", new ObjectSetReplayDecoder<Object>());
RedisCommand<List<Object>> SRANDMEMBER = new RedisCommand<List<Object>>("SRANDMEMBER", new ObjectListReplayDecoder<Object>());
RedisCommand<Set<Object>> SRANDMEMBER = new RedisCommand<Set<Object>>("SRANDMEMBER", new ObjectSetReplayDecoder<Object>());
RedisCommand<Object> SRANDMEMBER_SINGLE = new RedisCommand<Object>("SRANDMEMBER");
RedisCommand<ListScanResult<Object>> SSCAN = new RedisCommand<ListScanResult<Object>>("SSCAN", new ListMultiDecoder(new LongMultiDecoder(), new ObjectListReplayDecoder(), new ListScanResultReplayDecoder()));
RedisCommand<ListScanResult<Object>> EVAL_SSCAN = new RedisCommand<ListScanResult<Object>>("EVAL", new ListMultiDecoder(new LongMultiDecoder(), new ObjectListReplayDecoder<Object>(), new ListScanResultReplayDecoder()));

@ -347,7 +347,17 @@ public class RedissonSetTest extends BaseTest {
assertThat(set.removeRandom(4)).isEmpty();
}
@Test
public void testRandomLimited() {
RSet<Integer> set = redisson.getSet("simple");
for (int i = 0; i < 10; i++) {
set.add(i);
}
assertThat(set.random(3)).containsAnyElementsOf(set.readAll()).hasSize(3);
}
@Test
public void testRandom() {
RSet<Integer> set = redisson.getSet("simple");

Loading…
Cancel
Save