refactoring

pull/616/head
Nikita 9 years ago
parent 9618d1afa0
commit 11602d914f

@ -86,10 +86,6 @@ public class RedissonReference {
this.codec = codec != null ? codec.getClass().getName() : null; this.codec = codec != null ? codec.getClass().getName() : null;
} }
public boolean isDefaultCodec() {
return codec == null;
}
/** /**
* @return the type * @return the type
* @throws java.lang.Exception - which could be: * @throws java.lang.Exception - which could be:
@ -152,6 +148,10 @@ public class RedissonReference {
public void setKeyName(String keyName) { public void setKeyName(String keyName) {
this.keyName = keyName; this.keyName = keyName;
} }
public String getCodec() {
return codec;
}
/** /**
* @return the codec * @return the codec

@ -768,73 +768,80 @@ public class CommandAsyncService implements CommandAsyncExecutor {
((RedisClientResult)res).setRedisClient(addr); ((RedisClientResult)res).setRedisClient(addr);
} }
if (isRedissonReferenceSupportEnabled() && (res instanceof List || res instanceof ListScanResult)) { if (isRedissonReferenceSupportEnabled()) {
List r = res instanceof ListScanResult ? ((ListScanResult)res).getValues() : (List) res; handleReference(details.getMainPromise(), res);
for (int i = 0; i < r.size(); i++) { } else {
if (r.get(i) instanceof RedissonReference) {
try {
r.set(i ,(redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) r.get(i))
: RedissonObjectFactory.<R>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.<R>fromReference(redisson, (RedissonReference) se.getValue())
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue())));
} catch (Exception exception) {//skip and carry on to next one.
}
}
}
details.getMainPromise().trySuccess(res); details.getMainPromise().trySuccess(res);
} else if (isRedissonReferenceSupportEnabled() && (res instanceof MapScanResult)) { }
Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult)res).getMap(); } else {
HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null; details.getMainPromise().tryFailure(future.cause());
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : (Set<Map.Entry<ScanObjectEntry, ScanObjectEntry>>) map.entrySet()) { }
if (e.getValue().getObj() instanceof RedissonReference) { AsyncDetails.release(details);
try { }
e.setValue(new ScanObjectEntry(e.getValue().getBuf(), redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getValue().getObj()) private <R, V> void handleReference(RPromise<R> mainPromise, R res) {
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getValue().getObj()))); if (res instanceof List || res instanceof ListScanResult) {
} catch (Exception exception) {//skip and carry on to next one. 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 {
r.set(i ,(redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) r.get(i))
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) r.get(i))));
} catch (Exception exception) {//skip and carry on to next one.
} }
if (e.getKey().getObj() instanceof RedissonReference) { } else if (r.get(i) instanceof ScoredEntry && ((ScoredEntry) r.get(i)).getValue() instanceof RedissonReference) {
if (toAdd == null) { try {
toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>(); ScoredEntry se = ((ScoredEntry) r.get(i));
} r.set(i ,new ScoredEntry(se.getScore(), redisson != null
toAdd.put(e.getKey(), e.getValue()); ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue())
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue())));
} catch (Exception exception) {//skip and carry on to next one.
} }
} }
if (toAdd != null) { }
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : (Set<Map.Entry<ScanObjectEntry, ScanObjectEntry>>) toAdd.entrySet()) { mainPromise.trySuccess(res);
try { } else if (res instanceof MapScanResult) {
map.put(new ScanObjectEntry(e.getValue().getBuf(), (redisson != null Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult)res).getMap();
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getKey().getObj()) HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null;
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), map.remove(e.getKey())); for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : map.entrySet()) {
} catch (Exception exception) {//skip and carry on to next one. 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.
} }
} }
details.getMainPromise().trySuccess(res); if (e.getKey().getObj() instanceof RedissonReference) {
} else if (isRedissonReferenceSupportEnabled() && (res instanceof MapScanResult)) { if (toAdd == null) {
} else if (isRedissonReferenceSupportEnabled() && res instanceof RedissonReference) { toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>();
try { }
details.getMainPromise().trySuccess(redisson != null toAdd.put(e.getKey(), e.getValue());
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) res)
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) res));
} catch (Exception exception) {
details.getMainPromise().trySuccess(res);//fallback
} }
} else { }
details.getMainPromise().trySuccess(res); 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.
}
}
}
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));
} catch (Exception exception) {
mainPromise.trySuccess(res);//fallback
} }
} else { } else {
details.getMainPromise().tryFailure(future.cause()); mainPromise.trySuccess(res);
} }
AsyncDetails.release(details);
} }
} }

@ -118,8 +118,8 @@ public class RedissonObjectFactory {
List<Class<?>> interfaces = Arrays.asList(type.getInterfaces()); List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());
for (Class<?> iType : interfaces) { for (Class<?> iType : interfaces) {
if (builders.containsKey(iType)) {// user cache to speed up things a little. if (builders.containsKey(iType)) {// user cache to speed up things a little.
Method builder = builders.get(iType).get(rr.isDefaultCodec()); Method builder = builders.get(iType).get(isDefaultCodec(rr));
return (T) (rr.isDefaultCodec() return (T) (isDefaultCodec(rr)
? builder.invoke(redisson, rr.getKeyName()) ? builder.invoke(redisson, rr.getKeyName())
: builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType()))); : builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
} }
@ -128,6 +128,11 @@ public class RedissonObjectFactory {
throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodecName()); throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodecName());
} }
public static boolean isDefaultCodec(RedissonReference rr) {
return rr.getCodec() == null;
}
public static <T> T fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws Exception { public static <T> T fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws Exception {
return fromReference(redisson, rr, null); return fromReference(redisson, rr, null);
} }
@ -142,8 +147,8 @@ public class RedissonObjectFactory {
List<Class<?>> interfaces = Arrays.asList(type.getInterfaces()); List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());
for (Class<?> iType : interfaces) { for (Class<?> iType : interfaces) {
if (builders.containsKey(iType)) {// user cache to speed up things a little. if (builders.containsKey(iType)) {// user cache to speed up things a little.
Method builder = builders.get(iType).get(rr.isDefaultCodec()); Method builder = builders.get(iType).get(isDefaultCodec(rr));
return (T) (rr.isDefaultCodec() return (T) (isDefaultCodec(rr)
? builder.invoke(redisson, rr.getKeyName()) ? builder.invoke(redisson, rr.getKeyName())
: builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType()))); : builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
} }

Loading…
Cancel
Save