RedissonReference refactoring

pull/872/merge
Nikita 8 years ago
parent 398cb0c777
commit dac88df5e2

@ -114,8 +114,8 @@ public class Redisson implements RedissonClient {
connectionManager = ConfigSupport.createConnectionManager(configCopy); connectionManager = ConfigSupport.createConnectionManager(configCopy);
evictionScheduler = new EvictionScheduler(connectionManager.getCommandExecutor()); evictionScheduler = new EvictionScheduler(connectionManager.getCommandExecutor());
codecProvider = config.getCodecProvider(); codecProvider = configCopy.getCodecProvider();
resolverProvider = config.getResolverProvider(); resolverProvider = configCopy.getResolverProvider();
} }
public EvictionScheduler getEvictionScheduler() { public EvictionScheduler getEvictionScheduler() {

@ -24,6 +24,7 @@ import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.RPromise; import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonObjectFactory;
/** /**
* Base Redisson object * Base Redisson object
@ -162,6 +163,13 @@ public abstract class RedissonObject implements RObject {
} }
protected byte[] encode(Object value) { protected byte[] encode(Object value) {
if (commandExecutor.isRedissonReferenceSupportEnabled()) {
RedissonReference reference = RedissonObjectFactory.toReference(commandExecutor.getConnectionManager().getCfg(), value);
if (reference != null) {
value = reference;
}
}
try { try {
return codec.getValueEncoder().encode(value); return codec.getValueEncoder().encode(value);
} catch (IOException e) { } catch (IOException e) {
@ -170,6 +178,13 @@ public abstract class RedissonObject implements RObject {
} }
protected byte[] encodeMapKey(Object value) { protected byte[] encodeMapKey(Object value) {
if (commandExecutor.isRedissonReferenceSupportEnabled()) {
RedissonReference reference = RedissonObjectFactory.toReference(commandExecutor.getConnectionManager().getCfg(), value);
if (reference != null) {
value = reference;
}
}
try { try {
return codec.getMapKeyEncoder().encode(value); return codec.getMapKeyEncoder().encode(value);
} catch (IOException e) { } catch (IOException e) {
@ -178,6 +193,13 @@ public abstract class RedissonObject implements RObject {
} }
protected byte[] encodeMapValue(Object value) { protected byte[] encodeMapValue(Object value) {
if (commandExecutor.isRedissonReferenceSupportEnabled()) {
RedissonReference reference = RedissonObjectFactory.toReference(commandExecutor.getConnectionManager().getCfg(), value);
if (reference != null) {
value = reference;
}
}
try { try {
return codec.getMapValueEncoder().encode(value); return codec.getMapValueEncoder().encode(value);
} catch (IOException e) { } catch (IOException e) {

@ -26,7 +26,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.redisson.RedisClientResult; import org.redisson.RedisClientResult;
@ -484,10 +483,10 @@ public class CommandAsyncService implements CommandAsyncExecutor {
if (isRedissonReferenceSupportEnabled()) { if (isRedissonReferenceSupportEnabled()) {
try { try {
for (int i = 0; i < params.length; i++) { for (int i = 0; i < params.length; i++) {
RedissonReference reference = redisson != null RedissonReference reference = RedissonObjectFactory.toReference(getConnectionManager().getCfg(), params[i]);
? RedissonObjectFactory.toReference(redisson, params[i]) if (reference != null) {
: RedissonObjectFactory.toReference(redissonReactive, params[i]); params[i] = reference;
params[i] = reference == null ? params[i] : reference; }
} }
} catch (Exception e) { } catch (Exception e) {
connectionManager.getShutdownLatch().release(); connectionManager.getShutdownLatch().release();

@ -114,9 +114,7 @@ public class CommandBatchService extends CommandReactiveService {
} }
if (isRedissonReferenceSupportEnabled()) { if (isRedissonReferenceSupportEnabled()) {
for (int i = 0; i < params.length; i++) { for (int i = 0; i < params.length; i++) {
RedissonReference reference = redisson != null RedissonReference reference = RedissonObjectFactory.toReference(connectionManager.getCfg(), params[i]);
? RedissonObjectFactory.toReference(redisson, params[i])
: RedissonObjectFactory.toReference(redissonReactive, params[i]);
if (reference != null) { if (reference != null) {
params[i] = reference; params[i] = reference;
} }

@ -30,6 +30,7 @@ import org.redisson.client.RedisPubSubListener;
import org.redisson.client.codec.Codec; import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommand;
import org.redisson.command.CommandSyncService; import org.redisson.command.CommandSyncService;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig; import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.misc.InfinitySemaphoreLatch; import org.redisson.misc.InfinitySemaphoreLatch;
import org.redisson.misc.RPromise; import org.redisson.misc.RPromise;
@ -51,6 +52,8 @@ public interface ConnectionManager {
ExecutorService getExecutor(); ExecutorService getExecutor();
URI getLastClusterNode(); URI getLastClusterNode();
Config getCfg();
boolean isClusterMode(); boolean isClusterMode();

@ -148,12 +148,10 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
private final AsyncSemaphore freePubSubLock = new AsyncSemaphore(1); private final AsyncSemaphore freePubSubLock = new AsyncSemaphore(1);
private final boolean sharedEventLoopGroup;
private final boolean sharedExecutor;
private final CommandSyncService commandExecutor; private final CommandSyncService commandExecutor;
private final Config cfg;
{ {
for (int i = 0; i < locks.length; i++) { for (int i = 0; i < locks.length; i++) {
locks[i] = new AsyncSemaphore(1); locks[i] = new AsyncSemaphore(1);
@ -202,11 +200,10 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
} else { } else {
executor = cfg.getExecutor(); executor = cfg.getExecutor();
} }
this.cfg = cfg;
this.codec = cfg.getCodec(); this.codec = cfg.getCodec();
this.shutdownPromise = newPromise(); this.shutdownPromise = newPromise();
this.sharedEventLoopGroup = cfg.getEventLoopGroup() != null;
this.sharedExecutor = cfg.getExecutor() != null;
this.commandExecutor = new CommandSyncService(this); this.commandExecutor = new CommandSyncService(this);
} }
@ -222,6 +219,10 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
return connectionWatcher; return connectionWatcher;
} }
public Config getCfg() {
return cfg;
}
@Override @Override
public MasterSlaveServersConfig getConfig() { public MasterSlaveServersConfig getConfig() {
return config; return config;
@ -784,8 +785,8 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
for (MasterSlaveEntry entry : entries.values()) { for (MasterSlaveEntry entry : entries.values()) {
entry.shutdown(); entry.shutdown();
} }
if (!sharedExecutor) { if (cfg.getExecutor() == null) {
executor.shutdown(); executor.shutdown();
try { try {
executor.awaitTermination(timeout, unit); executor.awaitTermination(timeout, unit);
@ -794,7 +795,7 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
} }
} }
if (!sharedEventLoopGroup) { if (cfg.getEventLoopGroup() == null) {
group.shutdownGracefully(quietPeriod, timeout, unit).syncUninterruptibly(); group.shutdownGracefully(quietPeriod, timeout, unit).syncUninterruptibly();
} }
timer.stop(); timer.stop();

@ -23,7 +23,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.redisson.RedissonReference; import org.redisson.RedissonReference;
import org.redisson.client.codec.Codec;
import org.redisson.api.RLiveObject; import org.redisson.api.RLiveObject;
import org.redisson.api.RLiveObjectService; import org.redisson.api.RLiveObjectService;
import org.redisson.api.RObject; import org.redisson.api.RObject;
@ -32,7 +31,9 @@ import org.redisson.api.RedissonClient;
import org.redisson.api.RedissonReactiveClient; import org.redisson.api.RedissonReactiveClient;
import org.redisson.api.annotation.REntity; import org.redisson.api.annotation.REntity;
import org.redisson.api.annotation.RId; import org.redisson.api.annotation.RId;
import org.redisson.client.codec.Codec;
import org.redisson.codec.CodecProvider; import org.redisson.codec.CodecProvider;
import org.redisson.config.Config;
import org.redisson.liveobject.misc.Introspectior; import org.redisson.liveobject.misc.Introspectior;
import org.redisson.liveobject.resolver.NamingScheme; import org.redisson.liveobject.resolver.NamingScheme;
@ -55,10 +56,10 @@ public class RedissonObjectFactory {
} }
} }
private static final Map<Class, RedissonObjectBuilder> builders; private static final Map<Class<?>, RedissonObjectBuilder> builders;
static { static {
HashMap<Class, RedissonObjectBuilder> b = new HashMap<Class, RedissonObjectBuilder>(); HashMap<Class<?>, RedissonObjectBuilder> b = new HashMap<Class<?>, RedissonObjectBuilder>();
for (Method method : RedissonClient.class.getDeclaredMethods()) { for (Method method : RedissonClient.class.getDeclaredMethods()) {
if (!method.getReturnType().equals(Void.TYPE) if (!method.getReturnType().equals(Void.TYPE)
&& RObject.class.isAssignableFrom(method.getReturnType()) && RObject.class.isAssignableFrom(method.getReturnType())
@ -130,10 +131,6 @@ public class RedissonObjectFactory {
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);
}
public static <T> T fromReference(RedissonReactiveClient redisson, RedissonReference rr, Class<?> expected) throws Exception {
Class<? extends Object> type = rr.getReactiveType(); Class<? extends Object> type = rr.getReactiveType();
CodecProvider codecProvider = redisson.getConfig().getCodecProvider(); CodecProvider codecProvider = redisson.getConfig().getCodecProvider();
/** /**
@ -153,49 +150,29 @@ public class RedissonObjectFactory {
throw new ClassNotFoundException("No RObjectReactive is found to match class type of " + rr.getReactiveTypeName()+ " with codec type of " + rr.getCodecName()); throw new ClassNotFoundException("No RObjectReactive is found to match class type of " + rr.getReactiveTypeName()+ " with codec type of " + rr.getCodecName());
} }
public static RedissonReference toReference(RedissonClient redisson, Object object) { public static RedissonReference toReference(Config config, Object object) {
if (object != null && object.getClass().isAnnotationPresent(REntity.class)) { if (object != null && object.getClass().isAnnotationPresent(REntity.class)) {
throw new IllegalArgumentException("REntity should be attached to Redisson before save"); throw new IllegalArgumentException("REntity should be attached to Redisson before save");
} }
if (object instanceof RObject && !(object instanceof RLiveObject)) { if (object instanceof RObject && !(object instanceof RLiveObject)) {
RObject rObject = ((RObject) object); RObject rObject = ((RObject) object);
redisson.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec()); config.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
return new RedissonReference(object.getClass(), ((RObject) object).getName(), ((RObject) object).getCodec()); return new RedissonReference(object.getClass(), ((RObject) object).getName(), ((RObject) object).getCodec());
} }
try {
if (object instanceof RLiveObject) {
Class<? extends Object> rEntity = object.getClass().getSuperclass();
REntity anno = rEntity.getAnnotation(REntity.class);
NamingScheme ns = anno.namingScheme()
.getDeclaredConstructor(Codec.class)
.newInstance(redisson.getCodecProvider().getCodec(anno, (Class) rEntity));
String name = Introspectior
.getFieldsWithAnnotation(rEntity, RId.class)
.getOnly().getName();
Class<?> type = rEntity.getDeclaredField(name).getType();
return new RedissonReference(rEntity,
ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
}
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return null;
}
public static RedissonReference toReference(RedissonReactiveClient redissonReactive, Object object) {
if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) { if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
RObjectReactive rObject = ((RObjectReactive) object); RObjectReactive rObject = ((RObjectReactive) object);
redissonReactive.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec()); config.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
return new RedissonReference(object.getClass(), ((RObjectReactive) object).getName(), ((RObjectReactive) object).getCodec()); return new RedissonReference(object.getClass(), ((RObjectReactive) object).getName(), ((RObjectReactive) object).getCodec());
} }
try { try {
if (object instanceof RLiveObject) { if (object instanceof RLiveObject) {
Class<? extends Object> rEntity = object.getClass().getSuperclass(); Class<? extends Object> rEntity = object.getClass().getSuperclass();
REntity anno = rEntity.getAnnotation(REntity.class); REntity anno = rEntity.getAnnotation(REntity.class);
NamingScheme ns = anno.namingScheme() NamingScheme ns = anno.namingScheme()
.getDeclaredConstructor(Codec.class) .getDeclaredConstructor(Codec.class)
.newInstance(redissonReactive.getCodecProvider().getCodec(anno, (Class) rEntity)); .newInstance(config.getCodecProvider().getCodec(anno, (Class) rEntity));
String name = Introspectior String name = Introspectior
.getFieldsWithAnnotation(rEntity, RId.class) .getFieldsWithAnnotation(rEntity, RId.class)
.getOnly().getName(); .getOnly().getName();

Loading…
Cancel
Save