MemorySessionStore#m_persistentSubscriptions: Map Layout changed

If one clientid subscribes a lot of topics the old implementation
struggles with the for loops
This commit is contained in:
Andreas Mager
2017-01-23 18:37:43 +01:00
parent 716c9ac0a0
commit 6f7011c9f2
@@ -37,9 +37,10 @@ import java.util.concurrent.BlockingQueue;
* @author andrea * @author andrea
*/ */
public class MemorySessionStore implements ISessionsStore { public class MemorySessionStore implements ISessionsStore {
private static final Logger LOG = LoggerFactory.getLogger(MemorySessionStore.class); private static final Logger LOG = LoggerFactory.getLogger(MemorySessionStore.class);
private Map<String, Set<Subscription>> m_persistentSubscriptions = new HashMap<>(); private Map<String, HashMap<String, Subscription>> m_persistentSubscriptions = new HashMap<>();
private Map<String, MapDBPersistentStore.PersistentSession> m_persistentSessions = new HashMap<>(); private Map<String, MapDBPersistentStore.PersistentSession> m_persistentSessions = new HashMap<>();
@@ -65,19 +66,8 @@ public class MemorySessionStore implements ISessionsStore {
if (!m_persistentSubscriptions.containsKey(clientID)) { if (!m_persistentSubscriptions.containsKey(clientID)) {
return; return;
} }
Set<Subscription> clientSubscriptions = m_persistentSubscriptions.get(clientID); HashMap<String, Subscription> clientSubscriptions = m_persistentSubscriptions.get(clientID);
//search for the subscription to remove clientSubscriptions.remove(topic);
Subscription toBeRemoved = null;
for (Subscription sub : clientSubscriptions) {
if (sub.getTopicFilter().equals(topic)) {
toBeRemoved = sub;
break;
}
}
if (toBeRemoved != null) {
clientSubscriptions.remove(toBeRemoved);
}
} }
@Override @Override
@@ -89,13 +79,10 @@ public class MemorySessionStore implements ISessionsStore {
public void addNewSubscription(Subscription newSubscription) { public void addNewSubscription(Subscription newSubscription) {
final String clientID = newSubscription.getClientId(); final String clientID = newSubscription.getClientId();
if (!m_persistentSubscriptions.containsKey(clientID)) { if (!m_persistentSubscriptions.containsKey(clientID)) {
m_persistentSubscriptions.put(clientID, new HashSet<Subscription>()); m_persistentSubscriptions.put(clientID, new HashMap<String, Subscription>());
} }
Set<Subscription> subs = m_persistentSubscriptions.get(clientID); m_persistentSubscriptions.get(clientID).put(newSubscription.getTopicFilter(), newSubscription);
subs.remove(newSubscription); //same topic and clientID
subs.add(newSubscription);
m_persistentSubscriptions.put(clientID, subs);
} }
@Override @Override
@@ -116,7 +103,7 @@ public class MemorySessionStore implements ISessionsStore {
throw new IllegalArgumentException("Can't create a session with the ID of an already existing" + clientID); throw new IllegalArgumentException("Can't create a session with the ID of an already existing" + clientID);
} }
LOG.debug("clientID {} is a newcome, creating it's empty subscriptions set", clientID); LOG.debug("clientID {} is a newcome, creating it's empty subscriptions set", clientID);
m_persistentSubscriptions.put(clientID, new HashSet<Subscription>()); m_persistentSubscriptions.put(clientID, new HashMap<String, Subscription>());
m_persistentSessions.put(clientID, new MapDBPersistentStore.PersistentSession(cleanSession)); m_persistentSessions.put(clientID, new MapDBPersistentStore.PersistentSession(cleanSession));
return new ClientSession(clientID, m_messagesStore, this, cleanSession); return new ClientSession(clientID, m_messagesStore, this, cleanSession);
} }
@@ -133,7 +120,7 @@ public class MemorySessionStore implements ISessionsStore {
@Override @Override
public Collection<ClientSession> getAllSessions() { public Collection<ClientSession> getAllSessions() {
Collection<ClientSession> result = new ArrayList<ClientSession>(); Collection<ClientSession> result = new ArrayList<>();
for (Map.Entry<String, PersistentSession> entry : m_persistentSessions.entrySet()) { for (Map.Entry<String, PersistentSession> entry : m_persistentSessions.entrySet()) {
result.add(new ClientSession(entry.getKey(), m_messagesStore, this, entry.getValue().cleanSession)); result.add(new ClientSession(entry.getKey(), m_messagesStore, this, entry.getValue().cleanSession));
} }
@@ -148,8 +135,8 @@ public class MemorySessionStore implements ISessionsStore {
@Override @Override
public List<ClientTopicCouple> listAllSubscriptions() { public List<ClientTopicCouple> listAllSubscriptions() {
List<ClientTopicCouple> allSubscriptions = new ArrayList<>(); List<ClientTopicCouple> allSubscriptions = new ArrayList<>();
for (Map.Entry<String, Set<Subscription>> entry : m_persistentSubscriptions.entrySet()) { for (Map.Entry<String, HashMap<String, Subscription>> entry : m_persistentSubscriptions.entrySet()) {
for (Subscription sub : entry.getValue()) { for (Subscription sub : entry.getValue().values()) {
allSubscriptions.add(sub.asClientTopicCouple()); allSubscriptions.add(sub.asClientTopicCouple());
} }
} }
@@ -158,23 +145,18 @@ public class MemorySessionStore implements ISessionsStore {
@Override @Override
public Subscription getSubscription(ClientTopicCouple couple) { public Subscription getSubscription(ClientTopicCouple couple) {
Set<Subscription> subscriptions = m_persistentSubscriptions.get(couple.clientID); HashMap<String, Subscription> subscriptions = m_persistentSubscriptions.get(couple.clientID);
if (subscriptions == null || subscriptions.isEmpty()) { if (subscriptions == null || subscriptions.isEmpty()) {
return null; return null;
} }
for (Subscription sub : subscriptions) { return subscriptions.get(couple.topicFilter);
if (sub.getTopicFilter().equals(couple.topicFilter)) {
return sub;
}
}
return null;
} }
@Override @Override
public List<Subscription> getSubscriptions() { public List<Subscription> getSubscriptions() {
List<Subscription> subscriptions = new ArrayList<>(); List<Subscription> subscriptions = new ArrayList<>();
for (Map.Entry<String, Set<Subscription>> entry : m_persistentSubscriptions.entrySet()) { for (Map.Entry<String, HashMap<String, Subscription>> entry : m_persistentSubscriptions.entrySet()) {
subscriptions.addAll(entry.getValue()); subscriptions.addAll(entry.getValue().values());
} }
return subscriptions; return subscriptions;
} }