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;
}
public boolean isDefaultCodec() {
return codec == null;
}
/**
* @return the type
* @throws java.lang.Exception - which could be:
@ -152,6 +148,10 @@ public class RedissonReference {
public void setKeyName(String keyName) {
this.keyName = keyName;
}
public String getCodec() {
return codec;
}
/**
* @return the codec

@ -768,73 +768,80 @@ public class CommandAsyncService implements CommandAsyncExecutor {
((RedisClientResult)res).setRedisClient(addr);
}
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 {
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.
}
}
}
if (isRedissonReferenceSupportEnabled()) {
handleReference(details.getMainPromise(), res);
} else {
details.getMainPromise().trySuccess(res);
} else if (isRedissonReferenceSupportEnabled() && (res instanceof MapScanResult)) {
Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult)res).getMap();
HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null;
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : (Set<Map.Entry<ScanObjectEntry, ScanObjectEntry>>) map.entrySet()) {
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.
}
}
} else {
details.getMainPromise().tryFailure(future.cause());
}
AsyncDetails.release(details);
}
private <R, V> void handleReference(RPromise<R> mainPromise, R res) {
if (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 {
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) {
if (toAdd == null) {
toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>();
}
toAdd.put(e.getKey(), e.getValue());
} 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.
}
}
if (toAdd != null) {
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : (Set<Map.Entry<ScanObjectEntry, ScanObjectEntry>>) 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 MapScanResult) {
Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult)res).getMap();
HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null;
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : map.entrySet()) {
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);
} else if (isRedissonReferenceSupportEnabled() && (res instanceof MapScanResult)) {
} else if (isRedissonReferenceSupportEnabled() && res instanceof RedissonReference) {
try {
details.getMainPromise().trySuccess(redisson != null
? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) res)
: RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) res));
} catch (Exception exception) {
details.getMainPromise().trySuccess(res);//fallback
if (e.getKey().getObj() instanceof RedissonReference) {
if (toAdd == null) {
toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>();
}
toAdd.put(e.getKey(), e.getValue());
}
} 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 {
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());
for (Class<?> iType : interfaces) {
if (builders.containsKey(iType)) {// user cache to speed up things a little.
Method builder = builders.get(iType).get(rr.isDefaultCodec());
return (T) (rr.isDefaultCodec()
Method builder = builders.get(iType).get(isDefaultCodec(rr));
return (T) (isDefaultCodec(rr)
? builder.invoke(redisson, rr.getKeyName())
: 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());
}
public static boolean isDefaultCodec(RedissonReference rr) {
return rr.getCodec() == null;
}
public static <T> T fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws Exception {
return fromReference(redisson, rr, null);
}
@ -142,8 +147,8 @@ public class RedissonObjectFactory {
List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());
for (Class<?> iType : interfaces) {
if (builders.containsKey(iType)) {// user cache to speed up things a little.
Method builder = builders.get(iType).get(rr.isDefaultCodec());
return (T) (rr.isDefaultCodec()
Method builder = builders.get(iType).get(isDefaultCodec(rr));
return (T) (isDefaultCodec(rr)
? builder.invoke(redisson, rr.getKeyName())
: builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
}

Loading…
Cancel
Save