|
|
@ -15,17 +15,14 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
package org.redisson.reactive;
|
|
|
|
package org.redisson.reactive;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.reactivestreams.Publisher;
|
|
|
|
import org.reactivestreams.Publisher;
|
|
|
|
|
|
|
|
import org.redisson.RedissonAtomicLong;
|
|
|
|
|
|
|
|
import org.redisson.api.RAtomicLongAsync;
|
|
|
|
import org.redisson.api.RAtomicLongReactive;
|
|
|
|
import org.redisson.api.RAtomicLongReactive;
|
|
|
|
import org.redisson.client.codec.LongCodec;
|
|
|
|
import org.redisson.api.RFuture;
|
|
|
|
import org.redisson.client.codec.StringCodec;
|
|
|
|
|
|
|
|
import org.redisson.client.protocol.RedisCommands;
|
|
|
|
|
|
|
|
import org.redisson.client.protocol.RedisStrictCommand;
|
|
|
|
|
|
|
|
import org.redisson.client.protocol.convertor.SingleConvertor;
|
|
|
|
|
|
|
|
import org.redisson.command.CommandReactiveExecutor;
|
|
|
|
import org.redisson.command.CommandReactiveExecutor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import reactor.fn.Supplier;
|
|
|
|
import reactor.rx.Streams;
|
|
|
|
import reactor.rx.Streams;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -36,29 +33,41 @@ import reactor.rx.Streams;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class RedissonAtomicLongReactive extends RedissonExpirableReactive implements RAtomicLongReactive {
|
|
|
|
public class RedissonAtomicLongReactive extends RedissonExpirableReactive implements RAtomicLongReactive {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final RAtomicLongAsync instance;
|
|
|
|
|
|
|
|
|
|
|
|
public RedissonAtomicLongReactive(CommandReactiveExecutor commandExecutor, String name) {
|
|
|
|
public RedissonAtomicLongReactive(CommandReactiveExecutor commandExecutor, String name) {
|
|
|
|
super(commandExecutor, name);
|
|
|
|
super(commandExecutor, name);
|
|
|
|
|
|
|
|
instance = new RedissonAtomicLong(commandExecutor, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Publisher<Long> addAndGet(long delta) {
|
|
|
|
public Publisher<Long> addAndGet(final long delta) {
|
|
|
|
return commandExecutor.writeReactive(getName(), StringCodec.INSTANCE, RedisCommands.INCRBY, getName(), delta);
|
|
|
|
return reactive(new Supplier<RFuture<Long>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public RFuture<Long> get() {
|
|
|
|
|
|
|
|
return instance.addAndGetAsync(delta);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Publisher<Boolean> compareAndSet(long expect, long update) {
|
|
|
|
public Publisher<Boolean> compareAndSet(final long expect, final long update) {
|
|
|
|
return commandExecutor.evalWriteReactive(getName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
|
|
|
|
return reactive(new Supplier<RFuture<Boolean>>() {
|
|
|
|
"if redis.call('get', KEYS[1]) == ARGV[1] then "
|
|
|
|
@Override
|
|
|
|
+ "redis.call('set', KEYS[1], ARGV[2]); "
|
|
|
|
public RFuture<Boolean> get() {
|
|
|
|
+ "return 1 "
|
|
|
|
return instance.compareAndSetAsync(expect, update);
|
|
|
|
+ "else "
|
|
|
|
}
|
|
|
|
+ "return 0 end",
|
|
|
|
});
|
|
|
|
Collections.<Object>singletonList(getName()), expect, update);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Publisher<Long> decrementAndGet() {
|
|
|
|
public Publisher<Long> decrementAndGet() {
|
|
|
|
return commandExecutor.writeReactive(getName(), StringCodec.INSTANCE, RedisCommands.DECR, getName());
|
|
|
|
return reactive(new Supplier<RFuture<Long>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public RFuture<Long> get() {
|
|
|
|
|
|
|
|
return instance.decrementAndGetAsync();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -68,23 +77,33 @@ public class RedissonAtomicLongReactive extends RedissonExpirableReactive implem
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Publisher<Long> getAndAdd(final long delta) {
|
|
|
|
public Publisher<Long> getAndAdd(final long delta) {
|
|
|
|
return commandExecutor.writeReactive(getName(), StringCodec.INSTANCE, new RedisStrictCommand<Long>("INCRBY", new SingleConvertor<Long>() {
|
|
|
|
return reactive(new Supplier<RFuture<Long>>() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Long convert(Object obj) {
|
|
|
|
public RFuture<Long> get() {
|
|
|
|
return ((Long) obj) - delta;
|
|
|
|
return instance.getAndAddAsync(delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}), getName(), delta);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Publisher<Long> getAndSet(long newValue) {
|
|
|
|
public Publisher<Long> getAndSet(final long newValue) {
|
|
|
|
return commandExecutor.writeReactive(getName(), LongCodec.INSTANCE, RedisCommands.GETSET, getName(), newValue);
|
|
|
|
return reactive(new Supplier<RFuture<Long>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public RFuture<Long> get() {
|
|
|
|
|
|
|
|
return instance.getAndSetAsync(newValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Publisher<Long> incrementAndGet() {
|
|
|
|
public Publisher<Long> incrementAndGet() {
|
|
|
|
return commandExecutor.writeReactive(getName(), StringCodec.INSTANCE, RedisCommands.INCR, getName());
|
|
|
|
return reactive(new Supplier<RFuture<Long>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public RFuture<Long> get() {
|
|
|
|
|
|
|
|
return instance.incrementAndGetAsync();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -98,8 +117,13 @@ public class RedissonAtomicLongReactive extends RedissonExpirableReactive implem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Publisher<Void> set(long newValue) {
|
|
|
|
public Publisher<Void> set(final long newValue) {
|
|
|
|
return commandExecutor.writeReactive(getName(), StringCodec.INSTANCE, RedisCommands.SET, getName(), newValue);
|
|
|
|
return reactive(new Supplier<RFuture<Void>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public RFuture<Void> get() {
|
|
|
|
|
|
|
|
return instance.setAsync(newValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
public String toString() {
|
|
|
|