From cd51c9a8de1ff1b71f443eae1deb6e6d6f96a21b Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 20 Jan 2019 11:41:17 +0200 Subject: [PATCH] Refactored NicknamesTable#getAllNicknames to a query --- .../sql/queries/batch/LargeFetchQueries.java | 41 ++++++++++++++++++- .../plan/db/sql/tables/KillsTable.java | 2 +- .../plan/db/sql/tables/NicknamesTable.java | 4 +- .../sql/tables/move/BatchOperationTable.java | 2 +- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/batch/LargeFetchQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/batch/LargeFetchQueries.java index b8bb56d5a..2c2daa605 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/batch/LargeFetchQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/batch/LargeFetchQueries.java @@ -18,6 +18,7 @@ package com.djrapitops.plan.db.sql.queries.batch; import com.djrapitops.plan.data.container.GeoInfo; import com.djrapitops.plan.data.container.PlayerKill; +import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.QueryAllStatement; import com.djrapitops.plan.db.sql.tables.*; @@ -113,7 +114,7 @@ public class LargeFetchQueries { * * @return Map: Session ID - List of PlayerKills */ - public static Query>> fetchAllPlayerKillsBySessionID() { + public static Query>> fetchAllPlayerKillDataBySessionID() { String usersUUIDColumn = UsersTable.TABLE_NAME + "." + UsersTable.Col.UUID; String usersNameColumn = UsersTable.TABLE_NAME + "." + UsersTable.Col.USER_NAME + " as victim_name"; String sql = "SELECT " + @@ -147,4 +148,42 @@ public class LargeFetchQueries { }; } + /** + * Query database for all nickname data. + * + * @return Multimap: Server UUID - (Player UUID - List of nicknames) + */ + public static Query>>> fetchAllNicknameData() { + String sql = "SELECT " + + NicknamesTable.Col.NICKNAME + ", " + + NicknamesTable.Col.LAST_USED + ", " + + NicknamesTable.Col.UUID + ", " + + NicknamesTable.Col.SERVER_UUID + + " FROM " + NicknamesTable.TABLE_NAME; + + return new QueryAllStatement>>>(sql, 5000) { + @Override + public Map>> processResults(ResultSet set) throws SQLException { + Map>> map = new HashMap<>(); + while (set.next()) { + UUID serverUUID = UUID.fromString(set.getString(NicknamesTable.Col.SERVER_UUID.get())); + UUID uuid = UUID.fromString(set.getString(NicknamesTable.Col.UUID.get())); + + Map> serverMap = map.getOrDefault(serverUUID, new HashMap<>()); + List nicknames = serverMap.getOrDefault(uuid, new ArrayList<>()); + + nicknames.add(new Nickname( + set.getString(NicknamesTable.Col.NICKNAME.get()), + set.getLong(NicknamesTable.Col.LAST_USED.get()), + serverUUID + )); + + serverMap.put(uuid, nicknames); + map.put(serverUUID, serverMap); + } + return map; + } + }; + } + } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/KillsTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/KillsTable.java index f0028c332..dfcdc5848 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/KillsTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/KillsTable.java @@ -211,7 +211,7 @@ public class KillsTable extends UserUUIDTable { } public void addKillsToSessions(Map>> map) { - Map> playerKillsBySessionID = db.query(LargeFetchQueries.fetchAllPlayerKillsBySessionID()); + Map> playerKillsBySessionID = db.query(LargeFetchQueries.fetchAllPlayerKillDataBySessionID()); for (UUID serverUUID : map.keySet()) { for (List sessions : map.get(serverUUID).values()) { for (Session session : sessions) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java index 7ea0430be..46034d221 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java @@ -158,9 +158,7 @@ public class NicknamesTable extends UserUUIDTable { } /** - * Get nicknames of all users but doesn't map them by Server - * - * See {@link NicknamesTable#getAllNicknames} + * Get nicknames of all users but doesn't map them by Server. * * @return a {@code Map} with all nicknames of all users * diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/move/BatchOperationTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/move/BatchOperationTable.java index d57721740..de84055a0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/move/BatchOperationTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/move/BatchOperationTable.java @@ -122,7 +122,7 @@ public class BatchOperationTable extends Table { if (toDB.equals(this)) { return; } - toDB.db.getNicknamesTable().insertNicknames(db.getNicknamesTable().getAllNicknames()); + toDB.db.getNicknamesTable().insertNicknames(db.query(LargeFetchQueries.fetchAllNicknameData())); } public void copyWebUsers(BatchOperationTable toDB) {