Merge branch 'master' of github.com:redisson/redisson

pull/1071/head
Nikita 7 years ago
commit ad2b010574

@ -71,6 +71,9 @@ import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import java.util.AbstractMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
/**
*
@ -317,9 +320,9 @@ public class CommandAsyncService implements CommandAsyncExecutor {
}
public <V> RedisException convertException(RFuture<V> future) {
return future.cause() instanceof RedisException ?
(RedisException) future.cause() :
new RedisException("Unexpected exception while processing command", future.cause());
return future.cause() instanceof RedisException
? (RedisException) future.cause()
: new RedisException("Unexpected exception while processing command", future.cause());
}
private NodeSource getNodeSource(String key) {
@ -355,7 +358,6 @@ public class CommandAsyncService implements CommandAsyncExecutor {
return mainPromise;
}
@Override
public <T, R> RFuture<R> writeAsync(Integer slot, Codec codec, RedisCommand<T> command, Object... params) {
RPromise<R> mainPromise = connectionManager.newPromise();
@ -404,7 +406,6 @@ public class CommandAsyncService implements CommandAsyncExecutor {
return evalAsync(new NodeSource(slot), false, codec, evalCommandType, script, keys, params);
}
@Override
public <T, R> RFuture<R> evalWriteAllAsync(RedisCommand<T> command, SlotCallback<T, R> callback, String script, List<Object> keys, Object... params) {
return evalAllAsync(false, command, callback, script, keys, params);
@ -481,7 +482,6 @@ public class CommandAsyncService implements CommandAsyncExecutor {
return;
}
final AsyncDetails<V, R> details = AsyncDetails.acquire();
if (isRedissonReferenceSupportEnabled()) {
try {
@ -869,24 +869,55 @@ public class CommandAsyncService implements CommandAsyncExecutor {
List<Object> r = (List<Object>) res;
for (int i = 0; i < r.size(); i++) {
if (r.get(i) instanceof RedissonReference) {
try {
r.set(i, redisson != null
? RedissonObjectFactory.fromReference(redisson, (RedissonReference) r.get(i))
: RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) r.get(i)));
} catch (Exception exception) {//skip and carry on to next one.
}
r.set(i, fromReference(r.get(i)));
} else if (r.get(i) instanceof ScoredEntry && ((ScoredEntry) r.get(i)).getValue() instanceof RedissonReference) {
try {
ScoredEntry<?> se = ((ScoredEntry<?>) r.get(i));
se = new ScoredEntry(se.getScore(), redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue())
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
se = new ScoredEntry(se.getScore(), fromReference(se.getValue()));
r.set(i, se);
} catch (Exception exception) {//skip and carry on to next one.
}
}
}
mainPromise.trySuccess(res);
} else if (res instanceof Set) {
Set r = (Set) res;
LinkedHashSet converted = new LinkedHashSet();
for (Object o : r) {
if (o instanceof RedissonReference) {
converted.add(fromReference(o));
} else if (o instanceof ScoredEntry && ((ScoredEntry) o).getValue() instanceof RedissonReference) {
ScoredEntry<?> se = ((ScoredEntry<?>) o);
se = new ScoredEntry(se.getScore(), fromReference(se.getValue()));
converted.add(se);
} else if (o instanceof Map.Entry) {
Map.Entry old = (Map.Entry) o;
Object key = old.getKey();
if (key instanceof RedissonReference) {
key = fromReference(key);
}
Object value = old.getValue();
if (value instanceof RedissonReference) {
value = fromReference(value);
}
converted.add(new AbstractMap.SimpleEntry(key, value));
} else {
converted.add(o);
}
}
mainPromise.trySuccess((R) converted);
} else if (res instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) res;
LinkedHashMap<Object, Object> converted = new LinkedHashMap<Object, Object>();
for (Map.Entry<Object, Object> e : map.entrySet()) {
Object value = e.getValue();
if (e.getValue() instanceof RedissonReference) {
value = fromReference(e.getValue());
}
Object key = e.getKey();
if (e.getKey() instanceof RedissonReference) {
key = fromReference(e.getKey());
}
converted.put(key, value);
}
mainPromise.trySuccess((R) converted);
} else if (res instanceof ListScanResult) {
List<ScanObjectEntry> r = ((ListScanResult) res).getValues();
for (int i = 0; i < r.size(); i++) {
@ -896,60 +927,42 @@ public class CommandAsyncService implements CommandAsyncExecutor {
}
ScanObjectEntry e = r.get(i);
if (e.getObj() instanceof RedissonReference) {
try {
r.set(i , new ScanObjectEntry(e.getBuf(), redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getObj())
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getObj())));
} catch (Exception exception) {//skip and carry on to next one.
}
r.set(i, new ScanObjectEntry(e.getBuf(), fromReference(e.getObj())));
} else if (e.getObj() instanceof ScoredEntry && ((ScoredEntry<?>) e.getObj()).getValue() instanceof RedissonReference) {
try {
ScoredEntry<?> se = ((ScoredEntry<?>) e.getObj());
se = new ScoredEntry(se.getScore(), redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue())
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
se = new ScoredEntry(se.getScore(), fromReference(se.getValue()));
r.set(i, new ScanObjectEntry(e.getBuf(), se));
} catch (Exception exception) {//skip and carry on to next one.
}
}
}
mainPromise.trySuccess(res);
} else if (res instanceof MapScanResult) {
MapScanResult scanResult = (MapScanResult) res;
Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult) res).getMap();
HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null;
LinkedHashMap<ScanObjectEntry, ScanObjectEntry> converted = new LinkedHashMap<ScanObjectEntry, ScanObjectEntry>();
boolean hasConversion = false;
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : map.entrySet()) {
ScanObjectEntry value = e.getValue();
if (e.getValue().getObj() instanceof RedissonReference) {
try {
e.setValue(new ScanObjectEntry(e.getValue().getBuf(), redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getValue().getObj())
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getValue().getObj())));
} catch (Exception exception) {//skip and carry on to next one.
}
value = new ScanObjectEntry(e.getValue().getBuf(), fromReference(e.getValue().getObj()));
hasConversion = true;
}
ScanObjectEntry key = e.getKey();
if (e.getKey().getObj() instanceof RedissonReference) {
if (toAdd == null) {
toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>();
}
toAdd.put(e.getKey(), e.getValue());
}
}
if (toAdd != null) {
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : toAdd.entrySet()) {
try {
map.put(new ScanObjectEntry(e.getValue().getBuf(), (redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getKey().getObj())
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), map.remove(e.getKey()));
} catch (Exception exception) {//skip and carry on to next one.
key = new ScanObjectEntry(e.getKey().getBuf(), fromReference(e.getKey().getObj()));
hasConversion = true;
}
converted.put(key, value);
}
if (hasConversion) {
MapScanResult<ScanObjectEntry, ScanObjectEntry> newScanResult = new MapScanResult<ScanObjectEntry, ScanObjectEntry>(scanResult.getPos(), converted);
newScanResult.setRedisClient(scanResult.getRedisClient());
mainPromise.trySuccess((R) newScanResult);
} else {
mainPromise.trySuccess((R) res);
}
mainPromise.trySuccess(res);
} else if (res instanceof RedissonReference) {
try {
mainPromise.trySuccess(redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) res)
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) res));
mainPromise.trySuccess(this.<R>fromReference(res));
} catch (Exception exception) {
mainPromise.trySuccess(res);//fallback
}
@ -958,4 +971,13 @@ public class CommandAsyncService implements CommandAsyncExecutor {
}
}
private <R> R fromReference(Object res) {
try {
return redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) res)
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) res);
} catch (Exception exception) {
return (R) res;
}
}
}

@ -5,16 +5,27 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import org.junit.Test;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.api.RBatch;
import org.redisson.api.RBatchReactive;
import org.redisson.api.RBucket;
import org.redisson.api.RBucketAsync;
import org.redisson.api.RBucketReactive;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RGeo;
import org.redisson.api.RList;
import org.redisson.api.RListMultimap;
import org.redisson.api.RLiveObject;
import org.redisson.api.RLiveObjectService;
import org.redisson.api.RLocalCachedMap;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RPriorityQueue;
import org.redisson.api.RQueue;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RSet;
import org.redisson.api.RSetCache;
import org.redisson.api.RSetMultimap;
import org.redisson.client.protocol.ScoredEntry;
/**
@ -116,6 +127,96 @@ public class RedissonReferenceTest extends BaseTest {
assertEquals(b2.get(), entryRange.iterator().next().getValue().get());
}
@Test
public void testReadAll() throws InterruptedException {
RSetCache<RBucket<String>> b1 = redisson.getSetCache("set");
RBucket<String> b2 = redisson.getBucket("bucket");
b1.add(b2, 1, TimeUnit.MINUTES);
b2.set("test1");
assertEquals(b2.get(), b1.readAll().iterator().next().get());
assertEquals(2, redisson.getKeys().count());
RMapCache<String, RSetCache<RBucket<String>>> b3 = redisson.getMapCache("map");
b3.put("1", b1);
assertEquals(b2.get(), b3.readAllMap().get("1").iterator().next().get());
assertEquals(b2.get(), b3.readAllEntrySet().iterator().next().getValue().iterator().next().get());
assertEquals(b2.get(), b3.readAllValues().iterator().next().iterator().next().get());
RMapCache<RBucket<String>, RSetCache<RBucket<String>>> b4 = redisson.getMapCache("map1");
b4.put(b2, b1);
assertEquals(b2.get(), b4.readAllKeySet().iterator().next().get());
RPriorityQueue<RBucket<String>> q1 = redisson.getPriorityQueue("q1");
q1.add(b2);
assertEquals(b2.get(), q1.readAll().get(0).get());
RQueue<RBucket<String>> q2 = redisson.getQueue("q2");
q2.add(b2);
assertEquals(b2.get(), q2.readAll().get(0).get());
RDelayedQueue<RBucket<String>> q3 = redisson.getDelayedQueue(q2);
q3.offer(b2, 10, TimeUnit.MINUTES);
assertEquals(b2.get(), q3.readAll().get(0).get());
RList<RBucket<String>> l1 = redisson.getList("l1");
l1.add(b2);
assertEquals(b2.get(), l1.readAll().get(0).get());
RList<RBucket<String>> sl1 = l1.subList(0, 0);
assertEquals(b2.get(), sl1.readAll().get(0).get());
RLocalCachedMap<String, RBucket<String>> m1 = redisson.getLocalCachedMap("m1", LocalCachedMapOptions.defaults());
m1.put("1", b2);
assertEquals(b2.get(), m1.readAllMap().get("1").get());
assertEquals(b2.get(), m1.readAllEntrySet().iterator().next().getValue().get());
assertEquals(b2.get(), m1.readAllValues().iterator().next().get());
m1 = redisson.getLocalCachedMap("m1", LocalCachedMapOptions.defaults());
assertEquals(b2.get(), m1.readAllMap().get("1").get());
assertEquals(b2.get(), m1.readAllEntrySet().iterator().next().getValue().get());
assertEquals(b2.get(), m1.readAllValues().iterator().next().get());
RLocalCachedMap<RBucket<String>, RBucket<String>> m2 = redisson.getLocalCachedMap("m2", LocalCachedMapOptions.defaults());
m2.put(b2, b2);
assertEquals(b2.get(), m2.readAllKeySet().iterator().next().get());
m2 = redisson.getLocalCachedMap("m2", LocalCachedMapOptions.defaults());
assertEquals(b2.get(), m2.readAllKeySet().iterator().next().get());
RMap<String, RSetCache<RBucket<String>>> m3 = redisson.getMap("m3");
m3.put("1", b1);
assertEquals(b2.get(), m3.readAllMap().get("1").iterator().next().get());
assertEquals(b2.get(), m3.readAllEntrySet().iterator().next().getValue().iterator().next().get());
assertEquals(b2.get(), m3.readAllValues().iterator().next().iterator().next().get());
RMap<RBucket<String>, RSetCache<RBucket<String>>> m4 = redisson.getMap("m4");
m4.put(b2, b1);
assertEquals(b2.get(), m4.readAllKeySet().iterator().next().get());
//multimap
RGeo<RBucket<String>> g1 = redisson.getGeo("g1");
g1.add(13.361389, 38.115556, b2);
assertEquals(b2.get(), g1.readAll().iterator().next().get());
RScoredSortedSet<RBucket<String>> s1 = redisson.getScoredSortedSet("s1");
s1.add(0.0, b2);
assertEquals(b2.get(), s1.readAll().iterator().next().get());
RListMultimap<String, RBucket<String>> mm1 = redisson.getListMultimap("mm1");
mm1.put("1", b2);
assertEquals(b2.get(), mm1.get("1").readAll().get(0).get());
RListMultimap<RBucket<String>, RBucket<String>> mm2 = redisson.getListMultimap("mm2");
mm2.put(b2, b2);
assertEquals(b2.get(), mm2.get(b2).readAll().get(0).get());
RSetMultimap<String, RBucket<String>> mm3 = redisson.getSetMultimap("mm3");
mm3.put("1", b2);
assertEquals(b2.get(), mm3.get("1").readAll().iterator().next().get());
RSetMultimap<RBucket<String>, RBucket<String>> mm4 = redisson.getSetMultimap("mm4");
mm4.put(b2, b2);
assertEquals(b2.get(), mm4.get(b2).readAll().iterator().next().get());
}
@Test
public void testWithMap() {
RMap<RBucket<RMap>, RBucket<RMap>> map = redisson.getMap("set");

Loading…
Cancel
Save