diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java index ed0746b66..c6af8e2f6 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java @@ -127,6 +127,7 @@ public class KillsTable extends UserIDTable { " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnVictimUserID + " WHERE " + columnKillerUserID + "=" + usersTable.statementSelectID); + statement.setFetchSize(10000); statement.setString(1, uuid.toString()); set = statement.executeQuery(); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java index 67aefb520..8ed76bc34 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java @@ -9,9 +9,7 @@ import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; /** * @author Rsl1122 @@ -22,6 +20,7 @@ public class NicknamesTable extends UserIDTable { private final String columnServerID = "server_id"; private final ServerTable serverTable; + private String insertStatement; /** * @param db The database @@ -30,6 +29,14 @@ public class NicknamesTable extends UserIDTable { public NicknamesTable(SQLDB db, boolean usingMySQL) { super("plan_nicknames", db, usingMySQL); serverTable = db.getServerTable(); + insertStatement = "INSERT INTO " + tableName + " (" + + columnUserID + ", " + + columnServerID + ", " + + columnNick + + ") VALUES (" + + usersTable.statementSelectID + ", " + + serverTable.statementSelectServerID + ", " + + "?)"; } /** @@ -142,14 +149,7 @@ public class NicknamesTable extends UserIDTable { PreparedStatement statement = null; try { - statement = prepareStatement("INSERT INTO " + tableName + " (" + - columnUserID + ", " + - columnServerID + ", " + - columnNick + - ") VALUES (" + - usersTable.statementSelectID + ", " + - serverTable.statementSelectServerID + ", " + - "?)"); + statement = prepareStatement(insertStatement); statement.setString(1, uuid.toString()); statement.setString(2, Plan.getServerUUID().toString()); statement.setString(3, displayName); @@ -160,4 +160,73 @@ public class NicknamesTable extends UserIDTable { close(statement); } } + + public Map>> getAllNicknames() throws SQLException { + PreparedStatement statement = null; + ResultSet set = null; + try { + String usersIDColumn = usersTable + "." + usersTable.getColumnID(); + String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; + String serverIDColumn = serverTable + "." + serverTable.getColumnID(); + String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid"; + statement = prepareStatement("SELECT " + + columnNick + ", " + + usersUUIDColumn + ", " + + serverUUIDColumn + + " FROM " + tableName + + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + + " JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID + ); + statement.setFetchSize(5000); + set = statement.executeQuery(); + Map>> map = new HashMap<>(); + while (set.next()) { + UUID serverUUID = UUID.fromString(set.getString("s_uuid")); + UUID uuid = UUID.fromString(set.getString("uuid")); + + Map> serverMap = map.getOrDefault(serverUUID, new HashMap<>()); + List nicknames = serverMap.getOrDefault(uuid, new ArrayList<>()); + + nicknames.add(set.getString(columnNick)); + + serverMap.put(uuid, nicknames); + map.put(serverUUID, serverMap); + } + return map; + } finally { + endTransaction(statement); + close(set, statement); + } + } + + public void insertNicknames(Map>> allNicknames) throws SQLException { + if (allNicknames.isEmpty()) { + return; + } + PreparedStatement statement = null; + try { + statement = prepareStatement(insertStatement); + + // Every Server + for (UUID serverUUID : allNicknames.keySet()) { + // Every User + for (Map.Entry> entry : allNicknames.get(serverUUID).entrySet()) { + UUID uuid = entry.getKey(); + // Every Nickname + List nicknames = entry.getValue(); + for (String nickname : nicknames) { + statement.setString(1, uuid.toString()); + statement.setString(2, serverUUID.toString()); + statement.setString(3, nickname); + statement.addBatch(); + } + } + } + + statement.executeBatch(); + commit(statement.getConnection()); + } finally { + close(statement); + } + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java index c3eec7601..bbdfcca50 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java @@ -10,6 +10,8 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.tables.Table; import java.sql.SQLException; +import java.util.Collection; +import java.util.UUID; /** * A Fake table used to store a lot of big table operations. @@ -19,6 +21,10 @@ import java.sql.SQLException; * {@code fromT = new BatchOperationTable(from);} * {@code toT = new BatchOperationTable(to);} * {@code fromT.copy(toT);} + *

+ * The copy methods assume that the table has been cleared, or that no duplicate data will be entered for a user. + *

+ * clearTable methods can be used to clear the table beforehand. * * @author Rsl1122 * @since 4.0.0 @@ -37,6 +43,10 @@ public class BatchOperationTable extends Table { table.removeAllData(); } + public void clearTable(Collection uuids, Table table) { + // TODO + } + public void copyActions(BatchOperationTable toDB) throws SQLException { toDB.db.getActionsTable().insertActions(db.getActionsTable().getAllActions()); } @@ -48,4 +58,8 @@ public class BatchOperationTable extends Table { public void copyIPsAndGeolocs(BatchOperationTable toDB) throws SQLException { toDB.db.getIpsTable().insertIPsAndGeolocations(db.getIpsTable().getAllIPsAndGeolocations()); } + + public void copyNicknames(BatchOperationTable toDB) throws SQLException { + toDB.db.getNicknamesTable().insertNicknames(db.getNicknamesTable().getAllNicknames()); + } } \ No newline at end of file