refactoring

pull/6262/head
Nikita Koksharov 5 months ago
parent a42ad4df9e
commit fcb182a33d

@ -21,7 +21,6 @@ import org.redisson.connection.ServiceManager;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.Objects;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
@ -42,32 +41,7 @@ public class ProxyBuilder {
} }
private static class CacheKey { private static final ConcurrentMap<Tuple<Method, Class<?>>, Method> METHODS_MAPPING = new ConcurrentHashMap<>();
final Method method;
final Class<?> instanceClass;
CacheKey(Method method, Class<?> instanceClass) {
super();
this.method = method;
this.instanceClass = instanceClass;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey = (CacheKey) o;
return method.equals(cacheKey.method) && instanceClass.equals(cacheKey.instanceClass);
}
@Override
public int hashCode() {
return Objects.hash(method, instanceClass);
}
}
private static final ConcurrentMap<CacheKey, Method> METHODS_MAPPING = new ConcurrentHashMap<CacheKey, Method>();
public static <T> T create(Callback commandExecutor, Object instance, Object implementation, Class<T> clazz, ServiceManager serviceManager) { public static <T> T create(Callback commandExecutor, Object instance, Object implementation, Class<T> clazz, ServiceManager serviceManager) {
InvocationHandler handler = new InvocationHandler() { InvocationHandler handler = new InvocationHandler() {
@ -92,7 +66,7 @@ public class ProxyBuilder {
} }
private static Method getMethod(Method method, Object instance, Object implementation) throws NoSuchMethodException { private static Method getMethod(Method method, Object instance, Object implementation) throws NoSuchMethodException {
CacheKey key = new CacheKey(method, instance.getClass()); Tuple<Method, Class<?>> key = new Tuple<>(method, instance.getClass());
Method instanceMethod = METHODS_MAPPING.get(key); Method instanceMethod = METHODS_MAPPING.get(key);
if (instanceMethod == null) { if (instanceMethod == null) {
if (implementation != null) { if (implementation != null) {

@ -33,6 +33,7 @@ import org.redisson.connection.ClientConnectionsEntry;
import org.redisson.connection.ConnectionManager; import org.redisson.connection.ConnectionManager;
import org.redisson.connection.MasterSlaveEntry; import org.redisson.connection.MasterSlaveEntry;
import org.redisson.misc.AsyncSemaphore; import org.redisson.misc.AsyncSemaphore;
import org.redisson.misc.Tuple;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -49,31 +50,6 @@ import java.util.stream.Collectors;
*/ */
public class PublishSubscribeService { public class PublishSubscribeService {
public static class PubSubClientKey {
private final ChannelName channelName;
private final ClientConnectionsEntry entry;
public PubSubClientKey(ChannelName channelName, ClientConnectionsEntry entry) {
this.channelName = channelName;
this.entry = entry;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PubSubClientKey that = (PubSubClientKey) o;
return Objects.equals(channelName, that.channelName) && Objects.equals(entry, that.entry);
}
@Override
public int hashCode() {
return Objects.hash(channelName, entry);
}
}
public static class PubSubKey { public static class PubSubKey {
private final ChannelName channelName; private final ChannelName channelName;
@ -136,7 +112,7 @@ public class PublishSubscribeService {
private final Map<ChannelName, Collection<PubSubConnectionEntry>> name2entry = new ConcurrentHashMap<>(); private final Map<ChannelName, Collection<PubSubConnectionEntry>> name2entry = new ConcurrentHashMap<>();
private final ConcurrentMap<PubSubKey, PubSubConnectionEntry> name2PubSubConnection = new ConcurrentHashMap<>(); private final ConcurrentMap<PubSubKey, PubSubConnectionEntry> name2PubSubConnection = new ConcurrentHashMap<>();
private final ConcurrentMap<MasterSlaveEntry, PubSubEntry> entry2PubSubConnection = new ConcurrentHashMap<>(); private final ConcurrentMap<MasterSlaveEntry, PubSubEntry> entry2PubSubConnection = new ConcurrentHashMap<>();
private final Map<PubSubClientKey, PubSubConnectionEntry> key2connection = new ConcurrentHashMap<>(); private final Map<Tuple<ChannelName, ClientConnectionsEntry>, PubSubConnectionEntry> key2connection = new ConcurrentHashMap<>();
private final SemaphorePubSub semaphorePubSub = new SemaphorePubSub(this); private final SemaphorePubSub semaphorePubSub = new SemaphorePubSub(this);
@ -530,7 +506,7 @@ public class PublishSubscribeService {
PubSubType type, AsyncSemaphore lock, AtomicInteger attempts, RedisPubSubListener<?>... listeners) { PubSubType type, AsyncSemaphore lock, AtomicInteger attempts, RedisPubSubListener<?>... listeners) {
PubSubConnectionEntry connEntry; PubSubConnectionEntry connEntry;
if (clientEntry != null) { if (clientEntry != null) {
connEntry = key2connection.get(new PubSubClientKey(channelName, clientEntry)); connEntry = key2connection.get(new Tuple<>(channelName, clientEntry));
} else { } else {
connEntry = name2PubSubConnection.get(new PubSubKey(channelName, entry)); connEntry = name2PubSubConnection.get(new PubSubKey(channelName, entry));
} }
@ -571,7 +547,7 @@ public class PublishSubscribeService {
PubSubConnectionEntry oldEntry = null; PubSubConnectionEntry oldEntry = null;
if (clientEntry != null) { if (clientEntry != null) {
PubSubClientKey key = new PubSubClientKey(channelName, clientEntry); Tuple<ChannelName, ClientConnectionsEntry> key = new Tuple(channelName, clientEntry);
oldEntry = key2connection.putIfAbsent(key, freeEntry); oldEntry = key2connection.putIfAbsent(key, freeEntry);
if (channelName.isTracking()) { if (channelName.isTracking()) {
clientEntry.getTrackedConnectionsHolder().incUsage(); clientEntry.getTrackedConnectionsHolder().incUsage();
@ -638,7 +614,7 @@ public class PublishSubscribeService {
PubSubConnectionEntry oldEntry = null; PubSubConnectionEntry oldEntry = null;
if (clientEntry != null) { if (clientEntry != null) {
PubSubClientKey key = new PubSubClientKey(channelName, clientEntry); Tuple<ChannelName, ClientConnectionsEntry> key = new Tuple<>(channelName, clientEntry);
oldEntry = key2connection.putIfAbsent(key, entry); oldEntry = key2connection.putIfAbsent(key, entry);
if (channelName.isTracking()) { if (channelName.isTracking()) {
clientEntry.getTrackedConnectionsHolder().incUsage(); clientEntry.getTrackedConnectionsHolder().incUsage();
@ -723,7 +699,7 @@ public class PublishSubscribeService {
name2PubSubConnection.remove(new PubSubKey(channelName, entry.getEntry())); name2PubSubConnection.remove(new PubSubKey(channelName, entry.getEntry()));
ClientConnectionsEntry e = entry.getEntry().getEntry(entry.getConnection().getRedisClient()); ClientConnectionsEntry e = entry.getEntry().getEntry(entry.getConnection().getRedisClient());
PubSubClientKey key = new PubSubClientKey(channelName, e); Tuple<ChannelName, ClientConnectionsEntry> key = new Tuple<>(channelName, e);
key2connection.remove(key); key2connection.remove(key);
if (e.getTrackedConnectionsHolder().decUsage() == 0) { if (e.getTrackedConnectionsHolder().decUsage() == 0) {
e.getTrackedConnectionsHolder().reset(); e.getTrackedConnectionsHolder().reset();

Loading…
Cancel
Save