Refactored TPSTable#insertAllTPS to an executable

This commit is contained in:
Rsl1122 2019-01-24 19:43:20 +02:00
parent 9d550caa14
commit 62645b97d3
4 changed files with 53 additions and 48 deletions

View File

@ -90,7 +90,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction {
} }
private void copyTPS() { private void copyTPS() {
db.getTpsTable().insertAllTPS(sourceDB.query(LargeFetchQueries.fetchAllTPSData())); copy(LargeStoreQueries::storeAllTPSData, LargeFetchQueries.fetchAllTPSData());
} }
private void copyUserInfo() { private void copyUserInfo() {

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.data.container.GeoInfo; 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.data.store.objects.Nickname;
import com.djrapitops.plan.db.access.ExecBatchStatement; import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.Executable; import com.djrapitops.plan.db.access.Executable;
@ -187,4 +188,34 @@ public class LargeStoreQueries {
} }
}; };
} }
public static Executable storeAllTPSData(Map<UUID, List<TPS>> 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<UUID, List<TPS>> entry : ofServers.entrySet()) {
UUID serverUUID = entry.getKey();
// Every TPS Data point
List<TPS> 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();
}
}
}
};
}
} }

View File

@ -29,7 +29,6 @@ import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries; import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.utilities.Verify;
import org.apache.commons.text.TextStringBuilder; import org.apache.commons.text.TextStringBuilder;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -58,26 +57,26 @@ public class TPSTable extends Table {
public static final String CHUNKS = "chunks_loaded"; public static final String CHUNKS = "chunks_loaded";
public static final String FREE_DISK = "free_disk_space"; 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) { public TPSTable(SQLDB db) {
super(TABLE_NAME, db); super(TABLE_NAME, db);
serverTable = db.getServerTable(); 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 final ServerTable serverTable;
private String insertStatement;
public static String createTableSQL(DBType dbType) { public static String createTableSQL(DBType dbType) {
return CreateTableParser.create(TABLE_NAME, dbType) return CreateTableParser.create(TABLE_NAME, dbType)
@ -159,7 +158,7 @@ public class TPSTable extends Table {
} }
public void insertTPS(TPS tps) { public void insertTPS(TPS tps) {
execute(new ExecStatement(insertStatement) { execute(new ExecStatement(INSERT_STATEMENT) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, getServerUUID().toString()); statement.setString(1, getServerUUID().toString());
@ -263,36 +262,6 @@ public class TPSTable extends Table {
}); });
} }
public void insertAllTPS(Map<UUID, List<TPS>> allTPS) {
if (Verify.isEmpty(allTPS)) {
return;
}
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every Server
for (Map.Entry<UUID, List<TPS>> entry : allTPS.entrySet()) {
UUID serverUUID = entry.getKey();
// Every TPS Data point
List<TPS> 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<Integer, List<TPS>> getPlayersOnlineForServers(Collection<Server> servers) { public Map<Integer, List<TPS>> getPlayersOnlineForServers(Collection<Server> servers) {
if (servers.isEmpty()) { if (servers.isEmpty()) {
return new HashMap<>(); return new HashMap<>();

View File

@ -43,7 +43,12 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
@Override @Override
public void insertTPS(Map<UUID, List<TPS>> ofServers) { public void insertTPS(Map<UUID, List<TPS>> ofServers) {
tpsTable.insertAllTPS(ofServers); db.executeTransaction(new Transaction() {
@Override
protected void performOperations() {
execute(LargeStoreQueries.storeAllTPSData(ofServers));
}
});
} }
@Override @Override