From 5cca26e5260bfb8129da758c45f9fdf4efc323a4 Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 10 Feb 2016 13:27:46 +0300 Subject: [PATCH] RedissonListReactive refactoring --- src/main/java/org/redisson/RedissonList.java | 4 +- .../java/org/redisson/api/RListReactive.java | 6 +- .../reactive/RedissonListReactive.java | 65 +++++-------------- .../redisson/RedissonListReactiveTest.java | 6 +- 4 files changed, 23 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index c0397117a..734ec97ad 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -56,11 +56,11 @@ public class RedissonList extends RedissonExpirable implements RList { public static final RedisCommand EVAL_BOOLEAN_ARGS2 = new RedisCommand("EVAL", new BooleanReplayConvertor(), 5, ValueType.OBJECTS); - protected RedissonList(CommandAsyncExecutor commandExecutor, String name) { + public RedissonList(CommandAsyncExecutor commandExecutor, String name) { super(commandExecutor, name); } - protected RedissonList(Codec codec, CommandAsyncExecutor commandExecutor, String name) { + public RedissonList(Codec codec, CommandAsyncExecutor commandExecutor, String name) { super(codec, commandExecutor, name); } diff --git a/src/main/java/org/redisson/api/RListReactive.java b/src/main/java/org/redisson/api/RListReactive.java index 45ee12403..6c2536f52 100644 --- a/src/main/java/org/redisson/api/RListReactive.java +++ b/src/main/java/org/redisson/api/RListReactive.java @@ -35,9 +35,9 @@ public interface RListReactive extends RCollectionReactive { Publisher iterator(int startIndex); - Publisher lastIndexOf(Object o); + Publisher lastIndexOf(Object o); - Publisher indexOf(Object o); + Publisher indexOf(Object o); Publisher add(long index, V element); @@ -49,6 +49,6 @@ public interface RListReactive extends RCollectionReactive { Publisher get(long index); - Publisher remove(int index); + Publisher remove(long index); } diff --git a/src/main/java/org/redisson/reactive/RedissonListReactive.java b/src/main/java/org/redisson/reactive/RedissonListReactive.java index 008b5ade8..d33213308 100644 --- a/src/main/java/org/redisson/reactive/RedissonListReactive.java +++ b/src/main/java/org/redisson/reactive/RedissonListReactive.java @@ -30,14 +30,13 @@ import java.util.List; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; +import org.redisson.RedissonList; import org.redisson.api.RListReactive; import org.redisson.client.codec.Codec; import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommand.ValueType; import org.redisson.client.protocol.RedisCommands; -import org.redisson.client.protocol.convertor.BooleanNumberReplayConvertor; import org.redisson.client.protocol.convertor.Convertor; -import org.redisson.client.protocol.convertor.IntegerReplayConvertor; import org.redisson.client.protocol.convertor.LongReplayConvertor; import org.redisson.command.CommandReactiveExecutor; @@ -56,12 +55,16 @@ import reactor.rx.subscription.ReactiveSubscription; */ public class RedissonListReactive extends RedissonExpirableReactive implements RListReactive { + private final RedissonList instance; + public RedissonListReactive(CommandReactiveExecutor commandExecutor, String name) { super(commandExecutor, name); + instance = new RedissonList(commandExecutor, name); } public RedissonListReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name) { super(codec, commandExecutor, name); + instance = new RedissonList(codec, commandExecutor, name); } @Override @@ -151,7 +154,7 @@ public class RedissonListReactive extends RedissonExpirableReactive implement @Override public Publisher remove(Object o) { - return remove(o, 1); + return reactive(instance.removeAsync(o)); } protected Publisher remove(Object o, int count) { @@ -160,17 +163,7 @@ public class RedissonListReactive extends RedissonExpirableReactive implement @Override public Publisher containsAll(Collection c) { - return commandExecutor.evalReadReactive(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES, - "local items = redis.call('lrange', KEYS[1], 0, -1) " + - "for i=1, #items do " + - "for j = 0, table.getn(ARGV), 1 do " + - "if items[i] == ARGV[j] then " + - "table.remove(ARGV, j) " + - "end " + - "end " + - "end " + - "return table.getn(ARGV) == 0 and 1 or 0", - Collections.singletonList(getName()), c.toArray()); + return reactive(instance.containsAllAsync(c)); } @Override @@ -232,40 +225,12 @@ public class RedissonListReactive extends RedissonExpirableReactive implement @Override public Publisher removeAll(Collection c) { - return commandExecutor.evalWriteReactive(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES, - "local v = 0 " + - "for i = 0, table.getn(ARGV), 1 do " - + "if redis.call('lrem', KEYS[1], 0, ARGV[i]) == 1 " - + "then v = 1 end " - +"end " - + "return v ", - Collections.singletonList(getName()), c.toArray()); + return reactive(instance.removeAllAsync(c)); } @Override public Publisher retainAll(Collection c) { - return commandExecutor.evalWriteReactive(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES, - "local changed = 0 " + - "local items = redis.call('lrange', KEYS[1], 0, -1) " - + "local i = 1 " - + "local s = table.getn(items) " - + "while i <= s do " - + "local element = items[i] " - + "local isInAgrs = false " - + "for j = 0, table.getn(ARGV), 1 do " - + "if ARGV[j] == element then " - + "isInAgrs = true " - + "break " - + "end " - + "end " - + "if isInAgrs == false then " - + "redis.call('LREM', KEYS[1], 0, element) " - + "changed = 1 " - + "end " - + "i = i + 1 " - + "end " - + "return changed ", - Collections.singletonList(getName()), c.toArray()); + return reactive(instance.retainAllAsync(c)); } @Override @@ -293,7 +258,7 @@ public class RedissonListReactive extends RedissonExpirableReactive implement } @Override - public Publisher remove(int index) { + public Publisher remove(long index) { if (index == 0) { return commandExecutor.writeReactive(getName(), codec, LPOP, getName()); } @@ -309,7 +274,7 @@ public class RedissonListReactive extends RedissonExpirableReactive implement @Override public Publisher contains(Object o) { - return indexOf(o, new BooleanNumberReplayConvertor(-1L)); + return reactive(instance.containsAsync(o)); } private Publisher indexOf(Object o, Convertor convertor) { @@ -327,13 +292,13 @@ public class RedissonListReactive extends RedissonExpirableReactive implement } @Override - public Publisher indexOf(Object o) { - return indexOf(o, new IntegerReplayConvertor()); + public Publisher indexOf(Object o) { + return indexOf(o, new LongReplayConvertor()); } @Override - public Publisher lastIndexOf(Object o) { - return commandExecutor.evalReadReactive(getName(), codec, new RedisCommand("EVAL", new IntegerReplayConvertor(), 4), + public Publisher lastIndexOf(Object o) { + return commandExecutor.evalReadReactive(getName(), codec, new RedisCommand("EVAL", 4), "local key = KEYS[1] " + "local obj = ARGV[1] " + "local items = redis.call('lrange', key, 0, -1) " + diff --git a/src/test/java/org/redisson/RedissonListReactiveTest.java b/src/test/java/org/redisson/RedissonListReactiveTest.java index f6a45d0d2..755a48083 100644 --- a/src/test/java/org/redisson/RedissonListReactiveTest.java +++ b/src/test/java/org/redisson/RedissonListReactiveTest.java @@ -229,7 +229,7 @@ public class RedissonListReactiveTest extends BaseReactiveTest { sync(list.add(0)); sync(list.add(10)); - int index = sync(list.lastIndexOf(3)); + long index = sync(list.lastIndexOf(3)); Assert.assertEquals(2, index); } @@ -247,7 +247,7 @@ public class RedissonListReactiveTest extends BaseReactiveTest { sync(list.add(0)); sync(list.add(10)); - int index = sync(list.lastIndexOf(3)); + long index = sync(list.lastIndexOf(3)); Assert.assertEquals(5, index); } @@ -265,7 +265,7 @@ public class RedissonListReactiveTest extends BaseReactiveTest { sync(list.add(3)); sync(list.add(10)); - int index = sync(list.lastIndexOf(3)); + long index = sync(list.lastIndexOf(3)); Assert.assertEquals(8, index); }