Load communities with 4 queries instead of 3n+1

This commit is contained in:
cnr 2016-12-23 20:27:54 -07:00
parent 35a9eabf74
commit 08d65f3662

View File

@ -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_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_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_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_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.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_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 settingId, settingValue FROM communitySettings WHERE communityId=?;"; 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 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=?;"; private static final String UPDATE_COMMUNITY_ROLE = "UPDATE communityMembers SET communityRole=? WHERE accountId=? AND communityId=?;";
@ -58,76 +58,6 @@ public class CommunityRepository extends MinecraftRepository
_us = us; _us = us;
} }
public void loadCommunity(int communityId, final Map<Integer, Community> 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<Integer, Community> communityMap) public void loadCommunities(final Map<Integer, Community> communityMap)
{ {
try (Connection connection = getConnection()) try (Connection connection = getConnection())
@ -140,54 +70,6 @@ public class CommunityRepository extends MinecraftRepository
final int id = resultSet.getInt("id"); final int id = resultSet.getInt("id");
final String cName = resultSet.getString("name"); final String cName = resultSet.getString("name");
final Community community = new Community(id, cName); 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); resultant.put(community.getId(), community);
} }
@ -195,6 +77,68 @@ public class CommunityRepository extends MinecraftRepository
communityMap.clear(); communityMap.clear();
communityMap.putAll(resultant); communityMap.putAll(resultant);
}, new ColumnVarChar("region", 5, _us ? "US" : "EU")); }, 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) catch (SQLException e)
{ {