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);
evictionScheduler = new EvictionScheduler(connectionManager.getCommandExecutor());
codecProvider = config.getCodecProvider();
resolverProvider = config.getResolverProvider();
codecProvider = configCopy.getCodecProvider();
resolverProvider = configCopy.getResolverProvider();
}
public EvictionScheduler getEvictionScheduler() {

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

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

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

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

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

@ -23,7 +23,6 @@ import java.util.List;
import java.util.Map;
import org.redisson.RedissonReference;
import org.redisson.client.codec.Codec;
import org.redisson.api.RLiveObject;
import org.redisson.api.RLiveObjectService;
import org.redisson.api.RObject;
@ -32,7 +31,9 @@ import org.redisson.api.RedissonClient;
import org.redisson.api.RedissonReactiveClient;
import org.redisson.api.annotation.REntity;
import org.redisson.api.annotation.RId;
import org.redisson.client.codec.Codec;
import org.redisson.codec.CodecProvider;
import org.redisson.config.Config;
import org.redisson.liveobject.misc.Introspectior;
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 {
HashMap<Class, RedissonObjectBuilder> b = new HashMap<Class, RedissonObjectBuilder>();
HashMap<Class<?>, RedissonObjectBuilder> b = new HashMap<Class<?>, RedissonObjectBuilder>();
for (Method method : RedissonClient.class.getDeclaredMethods()) {
if (!method.getReturnType().equals(Void.TYPE)
&& RObject.class.isAssignableFrom(method.getReturnType())
@ -130,10 +131,6 @@ public class RedissonObjectFactory {
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();
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());
}
public static RedissonReference toReference(RedissonClient redisson, Object object) {
public static RedissonReference toReference(Config config, Object object) {
if (object != null && object.getClass().isAnnotationPresent(REntity.class)) {
throw new IllegalArgumentException("REntity should be attached to Redisson before save");
}
if (object instanceof RObject && !(object instanceof RLiveObject)) {
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());
}
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)) {
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());
}
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(redissonReactive.getCodecProvider().getCodec(anno, (Class) rEntity));
.newInstance(config.getCodecProvider().getCodec(anno, (Class) rEntity));
String name = Introspectior
.getFieldsWithAnnotation(rEntity, RId.class)
.getOnly().getName();

Loading…
Cancel
Save