From bacd89756dddc9b907ca29dfa261997efd177137 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 19 Sep 2022 11:02:10 +0300 Subject: [PATCH] refactoring --- .../java/org/redisson/RedissonBucket.java | 27 ++++++++++++ .../java/org/redisson/RedissonJsonBucket.java | 41 +++++++++++++++++++ .../main/java/org/redisson/api/RBucket.java | 31 +++++++++++--- .../java/org/redisson/api/RBucketAsync.java | 25 ++++++++++- .../org/redisson/api/RBucketReactive.java | 27 ++++++++++-- .../main/java/org/redisson/api/RBucketRx.java | 27 ++++++++++-- .../java/org/redisson/api/RJsonBucket.java | 11 +++++ .../org/redisson/api/RJsonBucketAsync.java | 11 +++++ .../java/org/redisson/RedissonBucketTest.java | 10 ++--- .../org/redisson/RedissonJsonBucketTest.java | 4 +- 10 files changed, 194 insertions(+), 20 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonBucket.java b/redisson/src/main/java/org/redisson/RedissonBucket.java index 08994e884..5b351383e 100644 --- a/redisson/src/main/java/org/redisson/RedissonBucket.java +++ b/redisson/src/main/java/org/redisson/RedissonBucket.java @@ -219,6 +219,33 @@ public class RedissonBucket extends RedissonExpirable implements RBucket { return get(trySetAsync(value)); } + @Override + public boolean setIfAbsent(V value) { + return get(setIfAbsentAsync(value)); + } + + @Override + public boolean setIfAbsent(V value, Duration duration) { + return get(setIfAbsentAsync(value, duration)); + } + + @Override + public RFuture setIfAbsentAsync(V value) { + if (value == null) { + return commandExecutor.readAsync(getRawName(), codec, RedisCommands.NOT_EXISTS, getRawName()); + } + + return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SETNX, getRawName(), encode(value)); + } + + @Override + public RFuture setIfAbsentAsync(V value, Duration duration) { + if (value == null) { + throw new IllegalArgumentException("Value can't be null"); + } + return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SET_BOOLEAN, getRawName(), encode(value), "PX", duration.toMillis(), "NX"); + } + @Override public boolean setIfExists(V value) { return get(setIfExistsAsync(value)); diff --git a/redisson/src/main/java/org/redisson/RedissonJsonBucket.java b/redisson/src/main/java/org/redisson/RedissonJsonBucket.java index 2ba5aaa0a..ec44999fc 100644 --- a/redisson/src/main/java/org/redisson/RedissonJsonBucket.java +++ b/redisson/src/main/java/org/redisson/RedissonJsonBucket.java @@ -117,6 +117,33 @@ public class RedissonJsonBucket extends RedissonExpirable implements RJsonBuc Collections.singletonList(getRawName())); } + @Override + public boolean setIfAbsent(V value) { + return get(setIfAbsentAsync(value)); + } + + @Override + public boolean setIfAbsent(V value, Duration duration) { + return get(setIfAbsentAsync(value, duration)); + } + + @Override + public RFuture setIfAbsentAsync(V value) { + return setIfAbsentAsync("$", value); + } + + @Override + public RFuture setIfAbsentAsync(V value, Duration duration) { + return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN, + "local currValue = redis.call('json.set', KEYS[1], '$', ARGV[1], 'NX'); " + + "if currValue ~= false then " + + "redis.call('pexpire', KEYS[1], ARGV[2]); " + + "return 1;" + + "end;" + + "return 0; ", + Collections.singletonList(getRawName()), encode(value), duration.toMillis()); + } + @Override public boolean trySet(V value) { return get(trySetAsync(value)); @@ -127,6 +154,20 @@ public class RedissonJsonBucket extends RedissonExpirable implements RJsonBuc return trySetAsync("$", value); } + @Override + public boolean setIfAbsent(String path, Object value) { + return get(setIfAbsentAsync(path, value)); + } + + @Override + public RFuture setIfAbsentAsync(String path, Object value) { + if (value == null) { + return commandExecutor.readAsync(getRawName(), codec, RedisCommands.NOT_EXISTS, getRawName()); + } + + return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.JSON_SET_BOOLEAN, getRawName(), path, encode(value), "NX"); + } + @Override public boolean trySet(String path, Object value) { return get(trySetAsync(path, value)); diff --git a/redisson/src/main/java/org/redisson/api/RBucket.java b/redisson/src/main/java/org/redisson/api/RBucket.java index 450460858..f39bfe7dd 100644 --- a/redisson/src/main/java/org/redisson/api/RBucket.java +++ b/redisson/src/main/java/org/redisson/api/RBucket.java @@ -50,16 +50,17 @@ public interface RBucket extends RExpirable, RBucketAsync { V getAndDelete(); /** - * Tries to set element atomically into empty holder. - * + * Use {@link #setIfAbsent(Object)} instead + * * @param value - value to set * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated boolean trySet(V value); /** - * Tries to set element atomically into empty holder with defined timeToLive interval. + * Use {@link #setIfAbsent(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -67,10 +68,30 @@ public interface RBucket extends RExpirable, RBucketAsync { * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated boolean trySet(V value, long timeToLive, TimeUnit timeUnit); /** - * Sets value only if it's already exists. + * Sets value only if object holder doesn't exist. + * + * @param value - value to set + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + boolean setIfAbsent(V value); + + /** + * Sets value with defined duration only if object holder doesn't exist. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + boolean setIfAbsent(V value, Duration duration); + + /** + * Sets value only if object holder already exists. * * @param value - value to set * @return {@code true} if successful, or {@code false} if @@ -79,7 +100,7 @@ public interface RBucket extends RExpirable, RBucketAsync { boolean setIfExists(V value); /** - * Sets value only if it's already exists. + * Sets value only if object holder already exists. * * @param value - value to set * @param timeToLive - time to live interval diff --git a/redisson/src/main/java/org/redisson/api/RBucketAsync.java b/redisson/src/main/java/org/redisson/api/RBucketAsync.java index fc2138699..f274f7d28 100644 --- a/redisson/src/main/java/org/redisson/api/RBucketAsync.java +++ b/redisson/src/main/java/org/redisson/api/RBucketAsync.java @@ -50,16 +50,17 @@ public interface RBucketAsync extends RExpirableAsync { RFuture getAndDeleteAsync(); /** - * Tries to set element atomically into empty holder. + * Use {@link #setIfAbsentAsync(Object)} instead * * @param value - value to set * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated RFuture trySetAsync(V value); /** - * Tries to set element atomically into empty holder with defined timeToLive interval. + * Use {@link #setIfAbsentAsync(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -67,8 +68,28 @@ public interface RBucketAsync extends RExpirableAsync { * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated RFuture trySetAsync(V value, long timeToLive, TimeUnit timeUnit); + /** + * Sets value only if object holder doesn't exist. + * + * @param value - value to set + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + RFuture setIfAbsentAsync(V value); + + /** + * Sets value with defined duration only if object holder doesn't exist. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + RFuture setIfAbsentAsync(V value, Duration duration); + /** * Sets value only if it's already exists. * diff --git a/redisson/src/main/java/org/redisson/api/RBucketReactive.java b/redisson/src/main/java/org/redisson/api/RBucketReactive.java index 44904d537..925cb8b08 100644 --- a/redisson/src/main/java/org/redisson/api/RBucketReactive.java +++ b/redisson/src/main/java/org/redisson/api/RBucketReactive.java @@ -37,18 +37,38 @@ public interface RBucketReactive extends RExpirableReactive { * @return object size */ Mono size(); - + + /** + * Sets value only if object holder doesn't exist. + * + * @param value - value to set + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + Mono setIfAbsent(V value); + + /** + * Sets value with defined duration only if object holder doesn't exist. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + Mono setIfAbsent(V value, Duration duration); + /** - * Tries to set element atomically into empty holder. + * Use {@link #setIfAbsent(Object)} instead * * @param value - value to set * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated Mono trySet(V value); /** - * Tries to set element atomically into empty holder with defined timeToLive interval. + * Use {@link #setIfAbsent(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -56,6 +76,7 @@ public interface RBucketReactive extends RExpirableReactive { * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated Mono trySet(V value, long timeToLive, TimeUnit timeUnit); /** diff --git a/redisson/src/main/java/org/redisson/api/RBucketRx.java b/redisson/src/main/java/org/redisson/api/RBucketRx.java index 6825537dd..885da1aca 100644 --- a/redisson/src/main/java/org/redisson/api/RBucketRx.java +++ b/redisson/src/main/java/org/redisson/api/RBucketRx.java @@ -39,18 +39,38 @@ public interface RBucketRx extends RExpirableRx { * @return object size */ Single size(); - + + /** + * Sets value only if object holder doesn't exist. + * + * @param value - value to set + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + Single setIfAbsent(V value); + + /** + * Sets value with defined duration only if object holder doesn't exist. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element was already set + */ + Single setIfAbsent(V value, Duration duration); + /** - * Tries to set element atomically into empty holder. + * Use {@link #setIfAbsent(Object)} instead * * @param value - value to set * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated Single trySet(V value); /** - * Tries to set element atomically into empty holder with defined timeToLive interval. + * Use {@link #setIfAbsent(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -58,6 +78,7 @@ public interface RBucketRx extends RExpirableRx { * @return {@code true} if successful, or {@code false} if * element was already set */ + @Deprecated Single trySet(V value, long timeToLive, TimeUnit timeUnit); /** diff --git a/redisson/src/main/java/org/redisson/api/RJsonBucket.java b/redisson/src/main/java/org/redisson/api/RJsonBucket.java index 1598b74c6..7fa4b8db2 100644 --- a/redisson/src/main/java/org/redisson/api/RJsonBucket.java +++ b/redisson/src/main/java/org/redisson/api/RJsonBucket.java @@ -46,6 +46,17 @@ public interface RJsonBucket extends RBucket, RJsonBucketAsync { * @return {@code true} if successful, or {@code false} if * value was already set */ + boolean setIfAbsent(String path, Object value); + + /** + * Use {@link #setIfAbsent(String, Object)} instead + * + * @param path JSON path + * @param value object + * @return {@code true} if successful, or {@code false} if + * value was already set + */ + @Deprecated boolean trySet(String path, Object value); /** diff --git a/redisson/src/main/java/org/redisson/api/RJsonBucketAsync.java b/redisson/src/main/java/org/redisson/api/RJsonBucketAsync.java index 81d295833..3683f7318 100644 --- a/redisson/src/main/java/org/redisson/api/RJsonBucketAsync.java +++ b/redisson/src/main/java/org/redisson/api/RJsonBucketAsync.java @@ -46,6 +46,17 @@ public interface RJsonBucketAsync extends RBucketAsync { * @return {@code true} if successful, or {@code false} if * value was already set */ + RFuture setIfAbsentAsync(String path, Object value); + + /** + * Use {@link #setIfAbsentAsync(String, Object)} instead + * + * @param path JSON path + * @param value object + * @return {@code true} if successful, or {@code false} if + * value was already set + */ + @Deprecated RFuture trySetAsync(String path, Object value); /** diff --git a/redisson/src/test/java/org/redisson/RedissonBucketTest.java b/redisson/src/test/java/org/redisson/RedissonBucketTest.java index b919b9777..fbdc949ca 100755 --- a/redisson/src/test/java/org/redisson/RedissonBucketTest.java +++ b/redisson/src/test/java/org/redisson/RedissonBucketTest.java @@ -181,7 +181,7 @@ public class RedissonBucketTest extends BaseTest { Assumptions.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("4.0.0") > 0); RBucket al = redisson.getBucket("test"); al.set(1234); - assertThat(al.sizeInMemory()).isEqualTo(54); + assertThat(al.sizeInMemory()).isEqualTo(56); } @Test @@ -298,16 +298,16 @@ public class RedissonBucketTest extends BaseTest { @Test public void testTrySet() { RBucket r1 = redisson.getBucket("testTrySet"); - assertThat(r1.trySet("3")).isTrue(); - assertThat(r1.trySet("4")).isFalse(); + assertThat(r1.setIfAbsent("3")).isTrue(); + assertThat(r1.setIfAbsent("4")).isFalse(); assertThat(r1.get()).isEqualTo("3"); } @Test public void testTrySetTTL() throws InterruptedException { RBucket r1 = redisson.getBucket("testTrySetTTL"); - assertThat(r1.trySet("3", 500, TimeUnit.MILLISECONDS)).isTrue(); - assertThat(r1.trySet("4", 500, TimeUnit.MILLISECONDS)).isFalse(); + assertThat(r1.setIfAbsent("3", Duration.ofMillis(500))).isTrue(); + assertThat(r1.setIfAbsent("4", Duration.ofMillis(500))).isFalse(); assertThat(r1.get()).isEqualTo("3"); Thread.sleep(1000); diff --git a/redisson/src/test/java/org/redisson/RedissonJsonBucketTest.java b/redisson/src/test/java/org/redisson/RedissonJsonBucketTest.java index 400a2a530..5246c691c 100644 --- a/redisson/src/test/java/org/redisson/RedissonJsonBucketTest.java +++ b/redisson/src/test/java/org/redisson/RedissonJsonBucketTest.java @@ -408,8 +408,8 @@ public class RedissonJsonBucketTest extends BaseTest { NestedType nt2 = new NestedType(); nt2.setValue(124); nt2.setValues(Arrays.asList("t4", "t3")); - assertThat(al.trySet("$.type", nt2)).isFalse(); - assertThat(al.trySet("type", nt2)).isFalse(); + assertThat(al.setIfAbsent("$.type", nt2)).isFalse(); + assertThat(al.setIfAbsent("type", nt2)).isFalse(); Integer n2 = al.get(new JacksonCodec<>(Integer.class), "type.value"); assertThat(n2).isEqualTo(123);