Merge pull request #6299 from seakider/feat_atomic_compare_set

Feature - Atomic class add greaterThanSet and lessThanSet method
dependabot/maven/io.reactivex.rxjava3-rxjava-3.1.10
Nikita Koksharov 2 months ago committed by GitHub
commit 9c204892bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -170,7 +170,43 @@ public class RedissonAtomicDouble extends RedissonExpirable implements RAtomicDo
public RFuture<Void> setAsync(double newValue) {
return commandExecutor.writeAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.SET, getRawName(), BigDecimal.valueOf(newValue).toPlainString());
}
@Override
public boolean setIfLess(double less, double value) {
return get(setIfLessAsync(less, value));
}
@Override
public RFuture<Boolean> setIfLessAsync(double less, double value) {
return commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local currValue = redis.call('get', KEYS[1]); "
+ "currValue = currValue == false and 0 or tonumber(currValue);"
+ "if currValue < tonumber(ARGV[1]) then "
+ "redis.call('set', KEYS[1], ARGV[2]); "
+ "return 1;"
+ "end; "
+ "return 0;",
Collections.<Object>singletonList(getRawName()), less, value);
}
@Override
public boolean setIfGreater(double greater, double value) {
return get(setIfGreaterAsync(greater, value));
}
@Override
public RFuture<Boolean> setIfGreaterAsync(double greater, double value) {
return commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local currValue = redis.call('get', KEYS[1]); "
+ "currValue = currValue == false and 0 or tonumber(currValue);"
+ "if currValue > tonumber(ARGV[1]) then "
+ "redis.call('set', KEYS[1], ARGV[2]); "
+ "return 1;"
+ "end; "
+ "return 0;",
Collections.<Object>singletonList(getRawName()), greater, value);
}
public String toString() {
return Double.toString(get());
}

@ -168,7 +168,43 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong
public RFuture<Void> setAsync(long newValue) {
return commandExecutor.writeAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.SET, getRawName(), newValue);
}
@Override
public boolean setIfLess(long less, long value) {
return get(setIfLessAsync(less, value));
}
@Override
public RFuture<Boolean> setIfLessAsync(long less, long value) {
return commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local currValue = redis.call('get', KEYS[1]); "
+ "currValue = currValue == false and 0 or tonumber(currValue);"
+ "if currValue < tonumber(ARGV[1]) then "
+ "redis.call('set', KEYS[1], ARGV[2]); "
+ "return 1;"
+ "end; "
+ "return 0;",
Collections.<Object>singletonList(getRawName()), less, value);
}
@Override
public boolean setIfGreater(long greater, long value) {
return get(setIfGreaterAsync(greater, value));
}
@Override
public RFuture<Boolean> setIfGreaterAsync(long greater, long value) {
return commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local currValue = redis.call('get', KEYS[1]); "
+ "currValue = currValue == false and 0 or tonumber(currValue);"
+ "if currValue > tonumber(ARGV[1]) then "
+ "redis.call('set', KEYS[1], ARGV[2]); "
+ "return 1;"
+ "end; "
+ "return 0;",
Collections.<Object>singletonList(getRawName()), greater, value);
}
public String toString() {
return Long.toString(get());
}

@ -106,6 +106,26 @@ public interface RAtomicDouble extends RExpirable, RAtomicDoubleAsync {
* @param newValue the new value
*/
void set(double newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
boolean setIfLess(double less, double value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
boolean setIfGreater(double greater, double value);
/**
* Adds object event listener

@ -107,6 +107,26 @@ public interface RAtomicDoubleAsync extends RExpirableAsync {
* @return void
*/
RFuture<Void> setAsync(double newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
RFuture<Boolean> setIfLessAsync(double less, double value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
RFuture<Boolean> setIfGreaterAsync(double greater, double value);
/**
* Adds object event listener

@ -109,5 +109,25 @@ public interface RAtomicDoubleReactive extends RExpirableReactive {
* @return void
*/
Mono<Void> set(double newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
Mono<Boolean> setIfLess(double less, double value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
Mono<Boolean> setIfGreater(double greater, double value);
}

@ -110,5 +110,25 @@ public interface RAtomicDoubleRx extends RExpirableRx {
* @return void
*/
Completable set(double newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
Single<Boolean> setIfLess(double less, double value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
Single<Boolean> setIfGreater(double greater, double value);
}

@ -106,6 +106,26 @@ public interface RAtomicLong extends RExpirable, RAtomicLongAsync {
* @param newValue the new value
*/
void set(long newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
boolean setIfLess(long less, long value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
boolean setIfGreater(long greater, long value);
/**
* Adds object event listener

@ -107,6 +107,26 @@ public interface RAtomicLongAsync extends RExpirableAsync {
* @return void
*/
RFuture<Void> setAsync(long newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
RFuture<Boolean> setIfLessAsync(long less, long value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
RFuture<Boolean> setIfGreaterAsync(long greater, long value);
/**
* Adds object event listener

@ -109,5 +109,25 @@ public interface RAtomicLongReactive extends RExpirableReactive {
* @return void
*/
Mono<Void> set(long newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
Mono<Boolean> setIfLess(long less, long value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
Mono<Boolean> setIfGreater(long greater, long value);
}

@ -110,5 +110,25 @@ public interface RAtomicLongRx extends RExpirableRx {
* @return void
*/
Completable set(long newValue);
/**
* Atomically sets the given value if current value is less than
* the special value
*
* @param less compare value
* @param value newValue
* @return true when the value update is successful
*/
Single<Boolean> setIfLess(long less, long value);
/**
* Atomically sets the given value if current value is greater than
* the special value
*
* @param greater compare value
* @param value newValue
* @return true when the value update is successful
*/
Single<Boolean> setIfGreater(long greater, long value);
}

@ -8,7 +8,29 @@ import java.math.BigDecimal;
import static org.assertj.core.api.Assertions.assertThat;
public class RedissonAtomicDoubleTest extends RedisDockerTest {
@Test
public void testSetIfLess() {
RAtomicDouble al = redisson.getAtomicDouble("test");
assertThat(al.setIfLess(0, 1)).isFalse();
assertThat(al.get()).isEqualTo(0);
al.set(12);
assertThat(al.setIfLess(13, 1)).isTrue();
assertThat(al.get()).isEqualTo(1);
}
@Test
public void testSetIfGreater() {
RAtomicDouble al = redisson.getAtomicDouble("test");
assertThat(al.setIfLess(0, 1)).isFalse();
assertThat(al.get()).isEqualTo(0);
al.set(12);
assertThat(al.setIfGreater(11, 1)).isTrue();
assertThat(al.get()).isEqualTo(1);
}
@Test
public void testGetAndSet() {
RAtomicDouble al = redisson.getAtomicDouble("test");

@ -5,7 +5,29 @@ import org.junit.jupiter.api.Test;
import org.redisson.api.RAtomicLongReactive;
public class RedissonAtomicLongReactiveTest extends BaseReactiveTest {
@Test
public void testSetIfLess() {
RAtomicLongReactive al = redisson.getAtomicLong("test");
Assertions.assertFalse(sync(al.setIfLess(0, 1)));
Assertions.assertEquals(sync(al.get()).longValue(), 0L);
sync(al.set(12));
Assertions.assertTrue(sync(al.setIfLess(13, 1)));
Assertions.assertEquals(sync(al.get()).longValue(), 1L);
}
@Test
public void testSetIfGreater() {
RAtomicLongReactive al = redisson.getAtomicLong("test");
Assertions.assertFalse(sync(al.setIfGreater(0, 1)));
Assertions.assertEquals(sync(al.get()).longValue(), 0L);
sync(al.set(12));
Assertions.assertTrue(sync(al.setIfGreater(11, 1)));
Assertions.assertEquals(sync(al.get()).longValue(), 1L);
}
@Test
public void testCompareAndSet() {
RAtomicLongReactive al = redisson.getAtomicLong("test");

@ -7,7 +7,29 @@ import org.redisson.api.RAtomicLong;
import static org.assertj.core.api.Assertions.assertThat;
public class RedissonAtomicLongTest extends RedisDockerTest {
@Test
public void testSetIfLess() {
RAtomicLong al = redisson.getAtomicLong("test");
assertThat(al.setIfLess(0, 1)).isFalse();
assertThat(al.get()).isEqualTo(0);
al.set(12);
assertThat(al.setIfLess(13, 1)).isTrue();
assertThat(al.get()).isEqualTo(1);
}
@Test
public void testSetIfGreater() {
RAtomicLong al = redisson.getAtomicLong("test");
assertThat(al.setIfLess(0, 1)).isFalse();
assertThat(al.get()).isEqualTo(0);
al.set(12);
assertThat(al.setIfGreater(11, 1)).isTrue();
assertThat(al.get()).isEqualTo(1);
}
@Test
public void testGetAndSet() {
RAtomicLong al = redisson.getAtomicLong("test");

Loading…
Cancel
Save