From d0d645d09ad0175dbec585be94643228325800c6 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 25 Jan 2019 11:38:24 +0200 Subject: [PATCH] Refactored UserInfoTable#insertUserInfo to an executable --- .../transactions/BackupCopyTransaction.java | 2 +- .../db/sql/queries/LargeStoreQueries.java | 32 ++++++++++++++ .../plan/db/sql/tables/UserInfoTable.java | 43 ++++--------------- .../databases/sql/operation/SQLSaveOps.java | 7 ++- 4 files changed, 47 insertions(+), 37 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java index 999331846..ecc234f07 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java @@ -94,7 +94,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction { } private void copyUserInfo() { - db.getUserInfoTable().insertUserInfo(sourceDB.query(LargeFetchQueries.fetchPerServerUserInformation())); + copy(LargeStoreQueries::storePerServerUserInformation, LargeFetchQueries.fetchPerServerUserInformation()); } private void copyWorlds() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java index 704c79acd..50f865e90 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java @@ -19,6 +19,7 @@ package com.djrapitops.plan.db.sql.queries; import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.container.GeoInfo; import com.djrapitops.plan.data.container.TPS; +import com.djrapitops.plan.data.container.UserInfo; import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.db.access.ExecBatchStatement; import com.djrapitops.plan.db.access.Executable; @@ -242,4 +243,35 @@ public class LargeStoreQueries { } }; } + + /** + * Execute a big batch of Per server UserInfo insert statements. + * + * @param ofServers Map: Server UUID - List of user information + * @return Executable, use inside a {@link com.djrapitops.plan.db.access.transactions.Transaction} + */ + public static Executable storePerServerUserInformation(Map> ofServers) { + if (Verify.isEmpty(ofServers)) { + return Executable.empty(); + } + + return new ExecBatchStatement(UserInfoTable.INSERT_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + // Every Server + for (Map.Entry> entry : ofServers.entrySet()) { + UUID serverUUID = entry.getKey(); + // Every User + for (UserInfo user : entry.getValue()) { + statement.setString(1, user.getUuid().toString()); + statement.setLong(2, user.getRegistered()); + statement.setString(3, serverUUID.toString()); + statement.setBoolean(4, user.isBanned()); + statement.setBoolean(5, user.isOperator()); + statement.addBatch(); + } + } + } + }; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UserInfoTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UserInfoTable.java index 63db3e6f1..8dc62a2d1 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UserInfoTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UserInfoTable.java @@ -29,7 +29,6 @@ import com.djrapitops.plan.db.sql.parsing.Select; import com.djrapitops.plan.db.sql.parsing.Sql; import com.djrapitops.plan.db.sql.parsing.Update; import com.djrapitops.plan.system.info.server.Server; -import com.djrapitops.plugin.utilities.Verify; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -58,20 +57,19 @@ public class UserInfoTable extends Table { public static final String OP = "opped"; public static final String BANNED = "banned"; - private final String insertStatement; + public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " (" + + USER_UUID + ", " + + REGISTERED + ", " + + SERVER_UUID + ", " + + BANNED + ", " + + OP + + ") VALUES (?, ?, ?, ?, ?)"; private final UsersTable usersTable; public UserInfoTable(SQLDB db) { super(TABLE_NAME, db); usersTable = db.getUsersTable(); - insertStatement = "INSERT INTO " + tableName + " (" + - USER_UUID + ", " + - REGISTERED + ", " + - SERVER_UUID + ", " + - BANNED + ", " + - OP + - ") VALUES (?, ?, ?, ?, ?)"; } public static String createTableSQL(DBType dbType) { @@ -86,7 +84,7 @@ public class UserInfoTable extends Table { } public void registerUserInfo(UUID uuid, long registered) { - execute(new ExecStatement(insertStatement) { + execute(new ExecStatement(INSERT_STATEMENT) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, uuid.toString()); @@ -240,31 +238,6 @@ public class UserInfoTable extends Table { return getServerUserInfo(getServerUUID()); } - public void insertUserInfo(Map> allUserInfos) { - if (Verify.isEmpty(allUserInfos)) { - return; - } - - executeBatch(new ExecStatement(insertStatement) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - // Every Server - for (Map.Entry> entry : allUserInfos.entrySet()) { - UUID serverUUID = entry.getKey(); - // Every User - for (UserInfo user : entry.getValue()) { - statement.setString(1, user.getUuid().toString()); - statement.setLong(2, user.getRegistered()); - statement.setString(3, serverUUID.toString()); - statement.setBoolean(4, user.isBanned()); - statement.setBoolean(5, user.isOperator()); - statement.addBatch(); - } - } - } - }); - } - public int getServerUserCount(UUID serverUUID) { String sql = "SELECT " + " COUNT(" + REGISTERED + ") as c" + diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java index 2ae50025f..8aef12a09 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java @@ -78,7 +78,12 @@ public class SQLSaveOps extends SQLOps implements SaveOperations { @Override public void insertUserInfo(Map> ofServers) { - userInfoTable.insertUserInfo(ofServers); + db.executeTransaction(new Transaction() { + @Override + protected void performOperations() { + execute(LargeStoreQueries.storePerServerUserInformation(ofServers)); + } + }); } @Override