refactoring

pull/4385/head
Nikita Koksharov 3 years ago
parent 58f2595440
commit 6fca7e9a3f

@ -29,6 +29,7 @@ import org.redisson.transaction.operation.UnlinkOperation;
import org.redisson.transaction.operation.bucket.*; import org.redisson.transaction.operation.bucket.*;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage; import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -113,11 +114,7 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
public RFuture<Boolean> isExistsAsync() { public RFuture<Boolean> isExistsAsync() {
checkState(); checkState();
if (state != null) { if (state != null) {
if (state == NULL) { return new CompletableFutureWrapper<>(state != NULL);
return new CompletableFutureWrapper<>((Boolean) null);
} else {
return new CompletableFutureWrapper<>(true);
}
} }
return super.isExistsAsync(); return super.isExistsAsync();
@ -210,11 +207,7 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
operations.add(new BucketCompareAndSetOperation<V>(getRawName(), getLockName(), getCodec(), expect, update, transactionId, currentThreadId)); operations.add(new BucketCompareAndSetOperation<V>(getRawName(), getLockName(), getCodec(), expect, update, transactionId, currentThreadId));
if ((state == NULL && expect == null) if ((state == NULL && expect == null)
|| isEquals(state, expect)) { || isEquals(state, expect)) {
if (update == null) { state = Optional.ofNullable((Object) update).orElse(NULL);
state = NULL;
} else {
state = update;
}
return CompletableFuture.completedFuture(true); return CompletableFuture.completedFuture(true);
} }
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
@ -224,11 +217,7 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
operations.add(new BucketCompareAndSetOperation<V>(getRawName(), getLockName(), getCodec(), expect, update, transactionId, currentThreadId)); operations.add(new BucketCompareAndSetOperation<V>(getRawName(), getLockName(), getCodec(), expect, update, transactionId, currentThreadId));
if ((res == null && expect == null) if ((res == null && expect == null)
|| isEquals(res, expect)) { || isEquals(res, expect)) {
if (update == null) { state = Optional.ofNullable((Object) update).orElse(NULL);
state = NULL;
} else {
state = update;
}
return true; return true;
} }
return false; return false;
@ -251,27 +240,14 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
checkState(); checkState();
return executeLocked(() -> { return executeLocked(() -> {
if (state != null) { if (state != null) {
Object prevValue; Object prevValue = Optional.of(state).filter(s -> s != NULL).orElse(null);
if (state == NULL) {
prevValue = null;
} else {
prevValue = state;
}
operations.add(operation); operations.add(operation);
if (newValue == null) { state = Optional.ofNullable((Object) newValue).orElse(NULL);
state = NULL;
} else {
state = newValue;
}
return CompletableFuture.completedFuture((V) prevValue); return CompletableFuture.completedFuture((V) prevValue);
} }
return getAsync().thenApply(res -> { return getAsync().thenApply(res -> {
if (newValue == null) { state = Optional.ofNullable((Object) newValue).orElse(NULL);
state = NULL;
} else {
state = newValue;
}
operations.add(operation); operations.add(operation);
return res; return res;
}); });
@ -285,12 +261,7 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
long currentThreadId = Thread.currentThread().getId(); long currentThreadId = Thread.currentThread().getId();
return executeLocked(() -> { return executeLocked(() -> {
if (state != null) { if (state != null) {
Object prevValue; Object prevValue = Optional.of(state).filter(s -> s != NULL).orElse(null);
if (state == NULL) {
prevValue = null;
} else {
prevValue = state;
}
operations.add(new BucketGetAndDeleteOperation<V>(getRawName(), getLockName(), getCodec(), transactionId, currentThreadId)); operations.add(new BucketGetAndDeleteOperation<V>(getRawName(), getLockName(), getCodec(), transactionId, currentThreadId));
state = NULL; state = NULL;
return CompletableFuture.completedFuture((V) prevValue); return CompletableFuture.completedFuture((V) prevValue);
@ -314,11 +285,7 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
checkState(); checkState();
return executeLocked(() -> { return executeLocked(() -> {
operations.add(operation); operations.add(operation);
if (newValue == null) { state = Optional.ofNullable((Object) newValue).orElse(NULL);
state = NULL;
} else {
state = newValue;
}
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
}); });
} }
@ -347,11 +314,7 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
if (state != null) { if (state != null) {
operations.add(operation); operations.add(operation);
if (state == NULL) { if (state == NULL) {
if (newValue == null) { state = Optional.ofNullable((Object) newValue).orElse(NULL);
state = NULL;
} else {
state = newValue;
}
return CompletableFuture.completedFuture(true); return CompletableFuture.completedFuture(true);
} else { } else {
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
@ -361,11 +324,7 @@ public class RedissonTransactionalBucket<V> extends RedissonBucket<V> {
return getAsync().thenApply(res -> { return getAsync().thenApply(res -> {
operations.add(operation); operations.add(operation);
if (res == null) { if (res == null) {
if (newValue == null) { state = Optional.ofNullable((Object) newValue).orElse(NULL);
state = NULL;
} else {
state = newValue;
}
return true; return true;
} }
return false; return false;

Loading…
Cancel
Save