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 a97c63d5f..999331846 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 @@ -90,7 +90,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction { } private void copyTPS() { - db.getTpsTable().insertAllTPS(sourceDB.query(LargeFetchQueries.fetchAllTPSData())); + copy(LargeStoreQueries::storeAllTPSData, LargeFetchQueries.fetchAllTPSData()); } private void copyUserInfo() { 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 a35ec4d0e..a7ddf7391 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 @@ -18,6 +18,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.store.objects.Nickname; import com.djrapitops.plan.db.access.ExecBatchStatement; import com.djrapitops.plan.db.access.Executable; @@ -187,4 +188,34 @@ public class LargeStoreQueries { } }; } + + public static Executable storeAllTPSData(Map> ofServers) { + if (Verify.isEmpty(ofServers)) { + return Executable.empty(); + } + + return new ExecBatchStatement(TPSTable.INSERT_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + // Every Server + for (Map.Entry> entry : ofServers.entrySet()) { + UUID serverUUID = entry.getKey(); + // Every TPS Data point + List tpsList = entry.getValue(); + for (TPS tps : tpsList) { + statement.setString(1, serverUUID.toString()); + statement.setLong(2, tps.getDate()); + statement.setDouble(3, tps.getTicksPerSecond()); + statement.setInt(4, tps.getPlayers()); + statement.setDouble(5, tps.getCPUUsage()); + statement.setLong(6, tps.getUsedMemory()); + statement.setDouble(7, tps.getEntityCount()); + statement.setDouble(8, tps.getChunksLoaded()); + statement.setLong(9, tps.getFreeDiskSpace()); + statement.addBatch(); + } + } + } + }; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java index b35922a01..e0e7ab24c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java @@ -29,7 +29,6 @@ import com.djrapitops.plan.db.sql.parsing.Sql; import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries; import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plugin.api.TimeAmount; -import com.djrapitops.plugin.utilities.Verify; import org.apache.commons.text.TextStringBuilder; import java.sql.PreparedStatement; @@ -58,26 +57,26 @@ public class TPSTable extends Table { public static final String CHUNKS = "chunks_loaded"; public static final String FREE_DISK = "free_disk_space"; + public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " (" + + SERVER_ID + ", " + + DATE + ", " + + TPS + ", " + + PLAYERS_ONLINE + ", " + + CPU_USAGE + ", " + + RAM_USAGE + ", " + + ENTITIES + ", " + + CHUNKS + ", " + + FREE_DISK + + ") VALUES (" + + ServerTable.STATEMENT_SELECT_SERVER_ID + ", " + + "?, ?, ?, ?, ?, ?, ?, ?)"; + public TPSTable(SQLDB db) { super(TABLE_NAME, db); serverTable = db.getServerTable(); - insertStatement = "INSERT INTO " + tableName + " (" - + SERVER_ID + ", " - + DATE + ", " - + TPS + ", " - + PLAYERS_ONLINE + ", " - + CPU_USAGE + ", " - + RAM_USAGE + ", " - + ENTITIES + ", " - + CHUNKS + ", " - + FREE_DISK - + ") VALUES (" - + serverTable.statementSelectServerID + ", " - + "?, ?, ?, ?, ?, ?, ?, ?)"; } private final ServerTable serverTable; - private String insertStatement; public static String createTableSQL(DBType dbType) { return CreateTableParser.create(TABLE_NAME, dbType) @@ -159,7 +158,7 @@ public class TPSTable extends Table { } public void insertTPS(TPS tps) { - execute(new ExecStatement(insertStatement) { + execute(new ExecStatement(INSERT_STATEMENT) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, getServerUUID().toString()); @@ -263,36 +262,6 @@ public class TPSTable extends Table { }); } - public void insertAllTPS(Map> allTPS) { - if (Verify.isEmpty(allTPS)) { - return; - } - - executeBatch(new ExecStatement(insertStatement) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - // Every Server - for (Map.Entry> entry : allTPS.entrySet()) { - UUID serverUUID = entry.getKey(); - // Every TPS Data point - List tpsList = entry.getValue(); - for (TPS tps : tpsList) { - statement.setString(1, serverUUID.toString()); - statement.setLong(2, tps.getDate()); - statement.setDouble(3, tps.getTicksPerSecond()); - statement.setInt(4, tps.getPlayers()); - statement.setDouble(5, tps.getCPUUsage()); - statement.setLong(6, tps.getUsedMemory()); - statement.setDouble(7, tps.getEntityCount()); - statement.setDouble(8, tps.getChunksLoaded()); - statement.setLong(9, tps.getFreeDiskSpace()); - statement.addBatch(); - } - } - } - }); - } - public Map> getPlayersOnlineForServers(Collection servers) { if (servers.isEmpty()) { return new HashMap<>(); 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 1e89a0f87..2ae50025f 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 @@ -43,7 +43,12 @@ public class SQLSaveOps extends SQLOps implements SaveOperations { @Override public void insertTPS(Map> ofServers) { - tpsTable.insertAllTPS(ofServers); + db.executeTransaction(new Transaction() { + @Override + protected void performOperations() { + execute(LargeStoreQueries.storeAllTPSData(ofServers)); + } + }); } @Override