From 62f762cf7b1ceffcc4cb70c5dd735aae73199d2a Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 2 Sep 2017 11:03:28 +0300 Subject: [PATCH] Batch Insert for IPsTable --- .../plan/database/tables/IPsTable.java | 82 +++++++++++++++++-- .../tables/move/BatchOperationTable.java | 13 ++- 2 files changed, 87 insertions(+), 8 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java index 74cc93ec4..5826c56e2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java @@ -18,6 +18,7 @@ public class IPsTable extends UserIDTable { private final String columnIP = "ip"; private final String columnGeolocation = "geolocation"; + private String insertStatement; /** * @param db The database @@ -25,6 +26,13 @@ public class IPsTable extends UserIDTable { */ public IPsTable(SQLDB db, boolean usingMySQL) { super("plan_ips", db, usingMySQL); + insertStatement = "INSERT INTO " + tableName + " (" + + columnUserID + ", " + + columnIP + ", " + + columnGeolocation + + ") VALUES (" + + usersTable.statementSelectID + ", " + + "?, ?)"; } /** @@ -89,13 +97,8 @@ public class IPsTable extends UserIDTable { private void insertIp(UUID uuid, String ip, String geolocation) throws SQLException { PreparedStatement statement = null; try { - statement = prepareStatement("INSERT INTO " + tableName + " (" - + columnUserID + ", " - + columnIP + ", " - + columnGeolocation - + ") VALUES (" - + usersTable.statementSelectID + ", " - + "?, ?)"); + + statement = prepareStatement(insertStatement); statement.setString(1, uuid.toString()); statement.setString(2, ip); statement.setString(3, geolocation); @@ -156,4 +159,69 @@ public class IPsTable extends UserIDTable { close(set, statement); } } + + public Map> getAllIPsAndGeolocations() throws SQLException { + PreparedStatement statement = null; + ResultSet set = null; + try { + String usersIDColumn = usersTable + "." + usersTable.getColumnID(); + String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; + + statement = prepareStatement("SELECT " + + columnGeolocation + ", " + + columnIP + ", " + + usersUUIDColumn + + " FROM " + tableName + + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + ); + statement.setFetchSize(5000); + set = statement.executeQuery(); + Map> map = new HashMap<>(); + while (set.next()) { + UUID uuid = UUID.fromString(set.getString("uuid")); + + Map userMap = map.getOrDefault(uuid, new HashMap<>()); + + String geoLocation = set.getString(columnGeolocation); + String ip = set.getString(columnIP); + + userMap.put(ip, geoLocation); + map.put(uuid, userMap); + } + return map; + } finally { + endTransaction(statement); + close(set, statement); + } + } + + public void insertIPsAndGeolocations(Map> allIPsAndGeolocations) throws SQLException { + if (allIPsAndGeolocations.isEmpty()) { + return; + } + PreparedStatement statement = null; + try { + statement = prepareStatement(insertStatement); + + // Every User + for (UUID uuid : allIPsAndGeolocations.keySet()) { + // Every IP & Geolocation + for (Map.Entry entry : allIPsAndGeolocations.get(uuid).entrySet()) { + String ip = entry.getKey(); + String geoLocation = entry.getValue(); + + statement.setString(1, uuid.toString()); + statement.setString(2, ip); + statement.setString(3, geoLocation); + + 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 4c6e505ab..c3eec7601 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 @@ -12,9 +12,16 @@ import main.java.com.djrapitops.plan.database.tables.Table; import java.sql.SQLException; /** - * //TODO Class Javadoc Comment + * A Fake table used to store a lot of big table operations. + *

+ * To use this table create a new BatchOperationTable with both SQLDB objects. + * {@code SQLDB from; SQLDB to;} + * {@code fromT = new BatchOperationTable(from);} + * {@code toT = new BatchOperationTable(to);} + * {@code fromT.copy(toT);} * * @author Rsl1122 + * @since 4.0.0 */ public class BatchOperationTable extends Table { public BatchOperationTable(SQLDB db, boolean usingMySQL) { @@ -37,4 +44,8 @@ public class BatchOperationTable extends Table { public void copyCommandUse(BatchOperationTable toDB) throws SQLException { toDB.db.getCommandUseTable().insertCommandUsage(db.getCommandUseTable().getAllCommandUsages()); } + + public void copyIPsAndGeolocs(BatchOperationTable toDB) throws SQLException { + toDB.db.getIpsTable().insertIPsAndGeolocations(db.getIpsTable().getAllIPsAndGeolocations()); + } } \ No newline at end of file