From 082ee703a735419fb3eddec0f11822222feaa069 Mon Sep 17 00:00:00 2001 From: Rui Gu Date: Mon, 12 Sep 2016 16:41:31 +0100 Subject: [PATCH 1/2] Fixed iterator not been converted --- .../redisson/command/CommandAsyncService.java | 44 ++++++++++++++++--- .../org/redisson/RedissonReferenceTest.java | 29 ++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/redisson/src/main/java/org/redisson/command/CommandAsyncService.java b/redisson/src/main/java/org/redisson/command/CommandAsyncService.java index b2645da50..c69a3a11b 100644 --- a/redisson/src/main/java/org/redisson/command/CommandAsyncService.java +++ b/redisson/src/main/java/org/redisson/command/CommandAsyncService.java @@ -59,13 +59,14 @@ import io.netty.util.Timeout; import io.netty.util.TimerTask; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.FutureListener; -import io.netty.util.concurrent.Promise; -import org.redisson.Redisson; -import org.redisson.RedissonReactive; +import java.util.HashMap; +import java.util.Map; import org.redisson.RedissonReference; -import org.redisson.api.RObject; import org.redisson.api.RedissonClient; import org.redisson.api.RedissonReactiveClient; +import org.redisson.client.protocol.decoder.ListScanResult; +import org.redisson.client.protocol.decoder.MapScanResult; +import org.redisson.client.protocol.decoder.ScanObjectEntry; import org.redisson.misc.RedissonObjectFactory; /** @@ -752,8 +753,8 @@ public class CommandAsyncService implements CommandAsyncExecutor { ((RedisClientResult)res).setRedisClient(addr); } - if (isRedissonReferenceSupportEnabled() && res instanceof List) { - List r = (List) res; + if (isRedissonReferenceSupportEnabled() && (res instanceof List || res instanceof ListScanResult)) { + List r = res instanceof ListScanResult ? ((ListScanResult)res).getValues() : (List) res; for (int i = 0; i < r.size(); i++) { if (r.get(i) instanceof RedissonReference) { try { @@ -765,6 +766,37 @@ public class CommandAsyncService implements CommandAsyncExecutor { } } details.getMainPromise().trySuccess(res); + } else if (isRedissonReferenceSupportEnabled() && (res instanceof MapScanResult)) { + Map map = ((MapScanResult)res).getMap(); + HashMap toAdd = null; + for (Map.Entry e : (Set>) map.entrySet()) { + if (e.getValue().getObj() instanceof RedissonReference) { + try { + e.setValue(new ScanObjectEntry(e.getValue().getBuf(), redisson != null + ? RedissonObjectFactory.fromReference(redisson, (RedissonReference) e.getValue().getObj()) + : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) e.getValue().getObj()))); + } catch (Exception exception) {//skip and carry on to next one. + } + } + if (e.getKey().getObj() instanceof RedissonReference) { + if (toAdd == null) { + toAdd = new HashMap(); + } + toAdd.put(e.getKey(), e.getValue()); + } + } + if (toAdd != null) { + for (Map.Entry e : (Set>) toAdd.entrySet()) { + try { + map.remove(e.getKey()); + map.put(new ScanObjectEntry(e.getValue().getBuf(), (redisson != null + ? RedissonObjectFactory.fromReference(redisson, (RedissonReference) e.getKey().getObj()) + : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), e.getValue()); + } catch (Exception exception) {//skip and carry on to next one. + } + } + } + details.getMainPromise().trySuccess(res); } else if (isRedissonReferenceSupportEnabled() && res instanceof RedissonReference) { try { details.getMainPromise().trySuccess(redisson != null diff --git a/redisson/src/test/java/org/redisson/RedissonReferenceTest.java b/redisson/src/test/java/org/redisson/RedissonReferenceTest.java index 622db2ab1..09d4f57c8 100644 --- a/redisson/src/test/java/org/redisson/RedissonReferenceTest.java +++ b/redisson/src/test/java/org/redisson/RedissonReferenceTest.java @@ -10,6 +10,8 @@ import org.redisson.api.RBucket; import org.redisson.api.RBucketAsync; import org.redisson.api.RBucketReactive; import org.redisson.api.RLiveObject; +import org.redisson.api.RMap; +import org.redisson.api.RSet; /** * @@ -84,4 +86,31 @@ public class RedissonReferenceTest extends BaseTest { assertEquals("b1", result.get(2).getName()); } + @Test + public void testWithList() { + RSet> b1 = redisson.getSet("set"); + RBucket b2 = redisson.getBucket("bucket"); + + b1.add(b2); + b2.set("test1"); + assertEquals(b2.get(), b1.iterator().next().get()); + assertEquals(2, redisson.getKeys().count()); + } + + @Test + public void testWithMap() { + RMap, RBucket> map = redisson.getMap("set"); + RBucket b1 = redisson.getBucket("bucket1"); + RBucket b2 = redisson.getBucket("bucket2"); + + map.put(b1, b2); + assertEquals(b2.get(), map.values().iterator().next().get()); + assertEquals(b1.get(), map.keySet().iterator().next().get()); + assertNotEquals(3, redisson.getKeys().count()); + assertEquals(1, redisson.getKeys().count()); + b1.set(map); + b2.set(map); + assertNotEquals(1, redisson.getKeys().count()); + assertEquals(3, redisson.getKeys().count()); + } } From 87f5be54f0c26067ef4efb56f51ab5c4edbc99b0 Mon Sep 17 00:00:00 2001 From: Rui Gu Date: Mon, 12 Sep 2016 17:48:23 +0100 Subject: [PATCH 2/2] fixed Redisson Reference nested in ScoreEntry --- .../redisson/command/CommandAsyncService.java | 13 +++++++++++-- .../org/redisson/RedissonReferenceTest.java | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/redisson/src/main/java/org/redisson/command/CommandAsyncService.java b/redisson/src/main/java/org/redisson/command/CommandAsyncService.java index 8fb5ee9d3..46276a37c 100644 --- a/redisson/src/main/java/org/redisson/command/CommandAsyncService.java +++ b/redisson/src/main/java/org/redisson/command/CommandAsyncService.java @@ -65,6 +65,7 @@ import java.util.Map; import org.redisson.RedissonReference; import org.redisson.api.RedissonClient; import org.redisson.api.RedissonReactiveClient; +import org.redisson.client.protocol.ScoredEntry; import org.redisson.client.protocol.decoder.ListScanResult; import org.redisson.client.protocol.decoder.MapScanResult; import org.redisson.client.protocol.decoder.ScanObjectEntry; @@ -777,6 +778,14 @@ public class CommandAsyncService implements CommandAsyncExecutor { : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) r.get(i)))); } catch (Exception exception) {//skip and carry on to next one. } + } else if (r.get(i) instanceof ScoredEntry && ((ScoredEntry) r.get(i)).getValue() instanceof RedissonReference) { + try { + ScoredEntry se = ((ScoredEntry) r.get(i)); + r.set(i ,new ScoredEntry(se.getScore(), redisson != null + ? RedissonObjectFactory.fromReference(redisson, (RedissonReference) se.getValue()) + : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) se.getValue()))); + } catch (Exception exception) {//skip and carry on to next one. + } } } details.getMainPromise().trySuccess(res); @@ -802,15 +811,15 @@ public class CommandAsyncService implements CommandAsyncExecutor { if (toAdd != null) { for (Map.Entry e : (Set>) toAdd.entrySet()) { try { - map.remove(e.getKey()); map.put(new ScanObjectEntry(e.getValue().getBuf(), (redisson != null ? RedissonObjectFactory.fromReference(redisson, (RedissonReference) e.getKey().getObj()) - : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), e.getValue()); + : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), map.remove(e.getKey())); } catch (Exception exception) {//skip and carry on to next one. } } } details.getMainPromise().trySuccess(res); + } else if (isRedissonReferenceSupportEnabled() && (res instanceof MapScanResult)) { } else if (isRedissonReferenceSupportEnabled() && res instanceof RedissonReference) { try { details.getMainPromise().trySuccess(redisson != null diff --git a/redisson/src/test/java/org/redisson/RedissonReferenceTest.java b/redisson/src/test/java/org/redisson/RedissonReferenceTest.java index 09d4f57c8..b486f63b5 100644 --- a/redisson/src/test/java/org/redisson/RedissonReferenceTest.java +++ b/redisson/src/test/java/org/redisson/RedissonReferenceTest.java @@ -1,5 +1,6 @@ package org.redisson; +import java.util.Collection; import java.util.List; import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; @@ -11,7 +12,9 @@ import org.redisson.api.RBucketAsync; import org.redisson.api.RBucketReactive; import org.redisson.api.RLiveObject; import org.redisson.api.RMap; +import org.redisson.api.RScoredSortedSet; import org.redisson.api.RSet; +import org.redisson.client.protocol.ScoredEntry; /** * @@ -86,7 +89,7 @@ public class RedissonReferenceTest extends BaseTest { assertEquals("b1", result.get(2).getName()); } - @Test + @Test public void testWithList() { RSet> b1 = redisson.getSet("set"); RBucket b2 = redisson.getBucket("bucket"); @@ -97,7 +100,19 @@ public class RedissonReferenceTest extends BaseTest { assertEquals(2, redisson.getKeys().count()); } - @Test + @Test + public void testWithZSet() { + RScoredSortedSet> b1 = redisson.getScoredSortedSet("set"); + RBucket b2 = redisson.getBucket("bucket"); + b1.add(0.0, b2); + b2.set("test1"); + assertEquals(b2.get(), b1.iterator().next().get()); + assertEquals(2, redisson.getKeys().count()); + Collection>> entryRange = b1.entryRange(0, 1); + assertEquals(b2.get(), entryRange.iterator().next().getValue().get()); + } + + @Test public void testWithMap() { RMap, RBucket> map = redisson.getMap("set"); RBucket b1 = redisson.getBucket("bucket1");