Refactored NicknamesTable#getAllNicknamesUnmapped to a query

This commit is contained in:
Rsl1122 2019-01-20 11:44:54 +02:00
parent cd51c9a8de
commit 7c45564fa9
3 changed files with 32 additions and 33 deletions

View File

@ -186,4 +186,33 @@ public class LargeFetchQueries {
};
}
/**
* Query database for all nickname data.
*
* @return Map: Player UUID - List of nicknames.
*/
public static Query<Map<UUID, List<Nickname>>> fetchAllNicknameDataByPlayerUUIDs() {
String sql = "SELECT " +
NicknamesTable.Col.NICKNAME + ", " +
NicknamesTable.Col.LAST_USED + ", " +
NicknamesTable.Col.UUID + ", " +
NicknamesTable.Col.SERVER_UUID +
" FROM " + NicknamesTable.TABLE_NAME;
return new QueryAllStatement<Map<UUID, List<Nickname>>>(sql, 5000) {
@Override
public Map<UUID, List<Nickname>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<Nickname>> map = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString(NicknamesTable.Col.UUID.get()));
UUID serverUUID = UUID.fromString(set.getString(NicknamesTable.Col.SERVER_UUID.get()));
List<Nickname> nicknames = map.computeIfAbsent(uuid, x -> new ArrayList<>());
nicknames.add(new Nickname(
set.getString(NicknamesTable.Col.NICKNAME.get()), set.getLong(NicknamesTable.Col.LAST_USED.get()), serverUUID
));
}
return map;
}
};
}
}

View File

@ -157,36 +157,6 @@ public class NicknamesTable extends UserUUIDTable {
});
}
/**
* Get nicknames of all users but doesn't map them by Server.
*
* @return a {@code Map<UUID, List<Nickname>} with all nicknames of all users
*
*/
public Map<UUID, List<Nickname>> getAllNicknamesUnmapped() {
String sql = "SELECT " +
Col.NICKNAME + ", " +
Col.LAST_USED + ", " +
Col.UUID + ", " +
Col.SERVER_UUID +
" FROM " + tableName;
return query(new QueryAllStatement<Map<UUID, List<Nickname>>>(sql, 5000) {
@Override
public Map<UUID, List<Nickname>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<Nickname>> map = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get()));
List<Nickname> nicknames = map.computeIfAbsent(uuid, x -> new ArrayList<>());
nicknames.add(new Nickname(
set.getString(Col.NICKNAME.get()), set.getLong(Col.LAST_USED.get()), serverUUID
));
}
return map;
}
});
}
public void saveUserName(UUID uuid, Nickname name) {
List<Nickname> saved = getNicknameInformation(uuid);
if (saved.contains(name)) {

View File

@ -124,9 +124,9 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
List<UserInfo> serverUserInfo = userInfoTable.getServerUserInfo(serverUUID);
Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked();
Map<UUID, List<GeoInfo>> geoInfo = db.query(LargeFetchQueries.fetchAllGeoInfoData());
Map<UUID, List<GeoInfo>> geoInfo = db.query(LargeFetchQueries.fetchAllGeoInfoData()); // TODO Optimize
Map<UUID, List<Ping>> allPings = pingTable.getAllPings();
Map<UUID, List<Nickname>> allNicknames = nicknamesTable.getAllNicknamesUnmapped();
Map<UUID, List<Nickname>> allNicknames = db.query(LargeFetchQueries.fetchAllNicknameDataByPlayerUUIDs()); // TODO Optimize
Map<UUID, List<Session>> sessions = sessionsTable.getSessionInfoOfServer(serverUUID);
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
@ -191,7 +191,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked();
Map<UUID, List<GeoInfo>> geoInfo = db.query(LargeFetchQueries.fetchAllGeoInfoData());
Map<UUID, List<Ping>> allPings = pingTable.getAllPings();
Map<UUID, List<Nickname>> allNicknames = nicknamesTable.getAllNicknamesUnmapped();
Map<UUID, List<Nickname>> allNicknames = db.query(LargeFetchQueries.fetchAllNicknameDataByPlayerUUIDs());
Map<UUID, Map<UUID, List<Session>>> sessions = sessionsTable.getAllSessions(false);
Map<UUID, List<UserInfo>> allUserInfo = userInfoTable.getAllUserInfo();