From 08d65f36622ccf8626286b7cfeb745dcc6f2c298 Mon Sep 17 00:00:00 2001 From: cnr Date: Fri, 23 Dec 2016 20:27:54 -0700 Subject: [PATCH] Load communities with 4 queries instead of 3n+1 --- .../storage/CommunityRepository.java | 186 ++++++------------ 1 file changed, 65 insertions(+), 121 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 6b814c0c9..c8087bf32 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -29,9 +29,9 @@ public class CommunityRepository extends MinecraftRepository private static final String GET_ALL_COMMUNITIES = "SELECT * FROM communities WHERE region=?;"; private static final String GET_COMMUNITY_BY_ID = "SELECT * FROM communities WHERE id=?;"; private static final String GET_COMMUNITY_BY_NAME = "SELECT * FROM communities WHERE name=? AND region=?;"; - private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId WHERE communityId=?;"; - private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId WHERE communityId=?;"; - private static final String GET_COMMUNITY_SETTINGS = "SELECT settingId, settingValue FROM communitySettings WHERE communityId=?;"; + private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.communityId, cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId;"; + private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.communityId, cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId;"; + private static final String GET_COMMUNITY_SETTINGS = "SELECT communityId, settingId, settingValue FROM communitySettings;"; private static final String REMOVE_FROM_COMMUNITY = "DELETE FROM communityMembers WHERE accountId=? AND communityId=?;"; private static final String UPDATE_COMMUNITY_ROLE = "UPDATE communityMembers SET communityRole=? WHERE accountId=? AND communityId=?;"; @@ -57,77 +57,7 @@ public class CommunityRepository extends MinecraftRepository _repo = statusRepo; _us = us; } - - public void loadCommunity(int communityId, final Map communityMap) - { - try (Connection connection = getConnection()) - { - executeQuery(connection, GET_COMMUNITY_BY_ID, resultSet -> - { - if (resultSet.next()) - { - final int id = resultSet.getInt("id"); - final String cName = resultSet.getString("name"); - final Community community = new Community(id, cName); - executeQuery(connection, GET_COMMUNITY_MEMBERS, memberSet -> - { - while (memberSet.next()) - { - final int accountId = memberSet.getInt("accountId"); - final String name = memberSet.getString("name"); - final UUID uuid = UUID.fromString(memberSet.getString("uuid")); - final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); - boolean readingChat = memberSet.getBoolean("readingChat"); - CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name.toLowerCase()); - if (status != null) - { - info.update(lastLogin, true, status.getServer()); - } - info.ReadingChat = readingChat; - community.getMembers().put(info.UUID, info); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> - { - while (requestSet.next()) - { - final int accountId = requestSet.getInt("accountId"); - final UUID uuid = UUID.fromString(requestSet.getString("uuid")); - final String name = requestSet.getString("name"); - - community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_SETTINGS, settingSet -> - { - while (settingSet.next()) - { - final int settingId = settingSet.getInt("settingId"); - final String value = settingSet.getString("settingValue"); - - CommunitySetting setting = CommunitySetting.getSetting(settingId); - if (setting != null) - { - setting.parseValueInto(value, community); - } - } - }, new ColumnInt("communityId", community.getId())); - - communityMap.put(community.getId(), community); - } - }, new ColumnInt("id", communityId)); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - public void loadCommunities(final Map communityMap) { try (Connection connection = getConnection()) @@ -140,54 +70,6 @@ public class CommunityRepository extends MinecraftRepository final int id = resultSet.getInt("id"); final String cName = resultSet.getString("name"); final Community community = new Community(id, cName); - executeQuery(connection, GET_COMMUNITY_MEMBERS, memberSet -> - { - while (memberSet.next()) - { - final int accountId = memberSet.getInt("accountId"); - final String name = memberSet.getString("name"); - final UUID uuid = UUID.fromString(memberSet.getString("uuid")); - final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); - boolean readingChat = memberSet.getBoolean("readingChat"); - - CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name.toLowerCase()); - if (status != null) - { - info.update(lastLogin, true, status.getServer()); - } - info.ReadingChat = readingChat; - community.getMembers().put(info.UUID, info); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> - { - while (requestSet.next()) - { - final int accountId = requestSet.getInt("accountId"); - final UUID uuid = UUID.fromString(requestSet.getString("uuid")); - final String name = requestSet.getString("name"); - - community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_SETTINGS, settingSet -> - { - while (settingSet.next()) - { - final int settingId = settingSet.getInt("settingId"); - final String value = settingSet.getString("settingValue"); - - CommunitySetting setting = CommunitySetting.getSetting(settingId); - if (setting != null) - { - setting.parseValueInto(value, community); - } - } - }, new ColumnInt("communityId", community.getId())); resultant.put(community.getId(), community); } @@ -195,6 +77,68 @@ public class CommunityRepository extends MinecraftRepository communityMap.clear(); communityMap.putAll(resultant); }, new ColumnVarChar("region", 5, _us ? "US" : "EU")); + + executeQuery(connection, GET_COMMUNITY_MEMBERS, memberSet -> + { + while (memberSet.next()) + { + final int communityId = memberSet.getInt("communityId"); + final int accountId = memberSet.getInt("accountId"); + final String name = memberSet.getString("name"); + final UUID uuid = UUID.fromString(memberSet.getString("uuid")); + final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); + final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); + boolean readingChat = memberSet.getBoolean("readingChat"); + + CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); + PlayerStatus status = _repo.getElement(name.toLowerCase()); + if (status != null) + { + info.update(lastLogin, true, status.getServer()); + } + info.ReadingChat = readingChat; + + Community community = communityMap.get(communityId); + if (community != null) + { + community.getMembers().put(info.UUID, info); + } + } + }); + + executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> + { + while (requestSet.next()) + { + final int communityId = requestSet.getInt("communityId"); + final int accountId = requestSet.getInt("accountId"); + final UUID uuid = UUID.fromString(requestSet.getString("uuid")); + final String name = requestSet.getString("name"); + + Community community = communityMap.get(communityId); + if (community != null) + { + community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); + } + } + }); + + executeQuery(connection, GET_COMMUNITY_SETTINGS, settingSet -> + { + while (settingSet.next()) + { + final int communityId = settingSet.getInt("communityId"); + final int settingId = settingSet.getInt("settingId"); + final String value = settingSet.getString("settingValue"); + + Community community = communityMap.get(communityId); + CommunitySetting setting = CommunitySetting.getSetting(settingId); + if (community != null && setting != null) + { + setting.parseValueInto(value, community); + } + } + }); } catch (SQLException e) {