提交 0aacc937 编写于 作者: N Nikita Koksharov

Feature - Add addIfExists() method to RScoredSortedSet object. #3343

上级 13fd0b91
......@@ -253,6 +253,16 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return get(tryAddAsync(score, object));
}
@Override
public boolean addIfExists(double score, V object) {
return get(addIfExistsAsync(score, object));
}
@Override
public RFuture<Boolean> addIfExistsAsync(double score, V object) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZADD_BOOL, getName(), "XX", BigDecimal.valueOf(score).toPlainString(), encode(object));
}
@Override
public V first() {
return get(firstAsync());
......@@ -321,7 +331,7 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override
public RFuture<Boolean> tryAddAsync(double score, V object) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZADD_NX_BOOL, getName(), "NX", BigDecimal.valueOf(score).toPlainString(), encode(object));
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZADD_BOOL, getName(), "NX", BigDecimal.valueOf(score).toPlainString(), encode(object));
}
@Override
......
......@@ -317,14 +317,25 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
/**
* Adds element to this set only if has not been added before.
* <p>
* Works only with <b>Redis 3.0.2 and higher.</b>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param score - object score
* @param object - object itself
* @return <code>true</code> if element has added and <code>false</code> if not.
* @return <code>true</code> if element added and <code>false</code> if not.
*/
boolean tryAdd(double score, V object);
/**
* Adds element to this set only if it's already exists.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param score - object score
* @param object - object itself
* @return <code>true</code> if element added and <code>false</code> if not.
*/
boolean addIfExists(double score, V object);
/**
* Returns size of this set.
*
......
......@@ -286,6 +286,17 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
*/
RFuture<Boolean> tryAddAsync(double score, V object);
/**
* Adds element to this set only if it's already exists.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param score - object score
* @param object - object itself
* @return <code>true</code> if element added and <code>false</code> if not.
*/
RFuture<Boolean> addIfExistsAsync(double score, V object);
/**
* Removes a single instance of the specified element from this
* sorted set, if it is present.
......
......@@ -308,7 +308,18 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
* @return <code>true</code> if element has added and <code>false</code> if not.
*/
Mono<Boolean> tryAdd(double score, V object);
/**
* Adds element to this set only if it's already exists.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param score - object score
* @param object - object itself
* @return <code>true</code> if element added and <code>false</code> if not.
*/
Mono<Boolean> addIfExists(double score, V object);
/**
* Removes a single instance of the specified element from this
* sorted set, if it is present.
......
......@@ -309,7 +309,18 @@ public interface RScoredSortedSetRx<V> extends RExpirableRx, RSortableRx<Set<V>>
* @return <code>true</code> if element has added and <code>false</code> if not.
*/
Single<Boolean> tryAdd(double score, V object);
/**
* Adds element to this set only if it's already exists.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param score - object score
* @param object - object itself
* @return <code>true</code> if element added and <code>false</code> if not.
*/
Single<Boolean> addIfExists(double score, V object);
/**
* Removes a single instance of the specified element from this
* sorted set, if it is present.
......
......@@ -401,9 +401,19 @@ public class RedissonScoredSortedSetTest extends BaseTest {
new ScoredEntry<String>(0.1, "1"), new ScoredEntry<String>(0.2, "2"), new ScoredEntry<String>(0.3, "3"));
}
@Test
public void testAddIfExists() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
assertThat(set.addIfExists(123.81, "1980")).isFalse();
assertThat(set.getScore("1980")).isNull();
set.add(111, "1980");
assertThat(set.addIfExists(32, "1980")).isFalse();
assertThat(set.getScore("1980")).isEqualTo(32);
}
@Test
public void testTryAdd() {
Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("3.0.2") >= 0);
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
assertThat(set.tryAdd(123.81, "1980")).isTrue();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册