Refactored ServerTable#saveCurrentServerInfo to a transaction

This commit is contained in:
Rsl1122 2019-02-15 09:01:23 +02:00
parent 2e9f6148d6
commit f0f32f3ec4
9 changed files with 91 additions and 82 deletions

View File

@ -19,6 +19,8 @@ package com.djrapitops.plan.system.info.server;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
import com.djrapitops.plan.system.webserver.WebServer;
@ -78,7 +80,7 @@ public class BungeeServerInfo extends ServerInfo {
String accessAddress = webServer.get().getAccessAddress();
if (!accessAddress.equals(server.getWebAddress())) {
server.setWebAddress(accessAddress);
db.save().serverInfoForThisServer(server);
db.executeTransaction(new StoreServerInformationTransaction(server));
}
}
@ -96,9 +98,9 @@ public class BungeeServerInfo extends ServerInfo {
String accessAddress = webServer.get().getAccessAddress();
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.save().serverInfoForThisServer(bungeeCord);
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord));
Optional<Server> bungeeInfo = db.fetch().getBungeeInformation();
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) {
return bungeeInfo.get();
}

View File

@ -0,0 +1,60 @@
package com.djrapitops.plan.db.access.transactions;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.system.info.server.Server;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import static com.djrapitops.plan.db.sql.tables.ServerTable.INSERT_STATEMENT;
import static com.djrapitops.plan.db.sql.tables.ServerTable.UPDATE_STATEMENT;
/**
* Transaction for keeping Plan Server serverrmation up to date in the database.
*
* @author Rsl1122
*/
public class StoreServerInformationTransaction extends Transaction {
private final Server server;
public StoreServerInformationTransaction(Server server) {
this.server = server;
}
@Override
protected void performOperations() {
if (!execute(updateServerInformation())) {
execute(insertServerInformation());
}
}
private Executable updateServerInformation() {
return new ExecStatement(UPDATE_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
String serverUUIDString = server.getUuid().toString();
statement.setString(1, serverUUIDString);
statement.setString(2, server.getName());
statement.setString(3, server.getWebAddress());
statement.setBoolean(4, true);
statement.setInt(5, server.getMaxPlayers());
statement.setString(6, serverUUIDString);
}
};
}
private Executable insertServerInformation() {
return new ExecStatement(INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, server.getUuid().toString());
statement.setString(2, server.getName());
statement.setString(3, server.getWebAddress());
statement.setBoolean(4, true);
statement.setInt(5, server.getMaxPlayers());
}
};
}
}

View File

@ -23,7 +23,6 @@ import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.*;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -56,6 +55,15 @@ public class ServerTable extends Table {
SERVER_UUID, NAME,
WEB_ADDRESS, INSTALLED, MAX_PLAYERS);
public static final String UPDATE_STATEMENT = Update.values(TABLE_NAME,
SERVER_UUID,
NAME,
WEB_ADDRESS,
INSTALLED,
MAX_PLAYERS)
.where(SERVER_UUID + "=?")
.toString();
public static final String STATEMENT_SELECT_SERVER_ID =
"(SELECT " + TABLE_NAME + "." + SERVER_ID + " FROM " + TABLE_NAME +
" WHERE " + TABLE_NAME + "." + SERVER_UUID + "=?" +
@ -79,37 +87,6 @@ public class ServerTable extends Table {
.toString();
}
private void updateServerInfo(Server info) {
String sql = Update.values(tableName,
SERVER_UUID,
NAME,
WEB_ADDRESS,
INSTALLED,
MAX_PLAYERS)
.where(SERVER_ID + "=?")
.toString();
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, info.getUuid().toString());
statement.setString(2, info.getName());
statement.setString(3, info.getWebAddress());
statement.setBoolean(4, true);
statement.setInt(5, info.getMaxPlayers());
statement.setInt(6, info.getId());
}
});
}
public void saveCurrentServerInfo(Server info) {
if (getServerID(info.getUuid()).isPresent()) {
updateServerInfo(info);
} else {
saveNewServerInfo(info);
}
}
/**
* Returns server ID for a matching UUID
*
@ -139,30 +116,6 @@ public class ServerTable extends Table {
});
}
/**
* Inserts new row for a server into the table.
*
* @param info Info to instert (All variables should be present.
* @throws IllegalStateException if one of the Server variables is null
*/
private void saveNewServerInfo(Server info) {
UUID uuid = info.getUuid();
String name = info.getName();
String webAddress = info.getWebAddress();
Verify.nullCheck(uuid, name, webAddress);
execute(new ExecStatement(INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
statement.setString(2, name);
statement.setString(3, webAddress);
statement.setBoolean(4, true);
statement.setInt(5, info.getMaxPlayers());
}
});
}
public Map<UUID, String> getServerNames() {
String sql = Select.from(tableName,
SERVER_UUID, NAME)

View File

@ -21,7 +21,6 @@ import com.djrapitops.plan.data.container.Session;
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.system.info.server.Server;
import java.util.List;
import java.util.Map;
@ -77,9 +76,6 @@ public interface SaveOperations {
@Deprecated
void session(UUID uuid, Session session);
@Deprecated
void serverInfoForThisServer(Server server);
@Deprecated
void setAsUninstalled(UUID serverUUID);

View File

@ -23,7 +23,6 @@ import com.djrapitops.plan.db.access.queries.DataStoreQueries;
import com.djrapitops.plan.db.access.queries.LargeStoreQueries;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.system.database.databases.operation.SaveOperations;
import com.djrapitops.plan.system.info.server.Server;
import java.util.Collection;
import java.util.List;
@ -151,11 +150,6 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
});
}
@Override
public void serverInfoForThisServer(Server server) {
serverTable.saveCurrentServerInfo(server);
}
@Override
public void setAsUninstalled(UUID serverUUID) {
serverTable.setAsUninstalled(serverUUID);

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.system.info.server;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
import com.djrapitops.plan.system.settings.config.PlanConfig;
@ -86,8 +87,8 @@ public class ServerServerInfo extends ServerInfo {
}
private Server updateDbInfo(UUID serverUUID) throws IOException {
Database database = dbSystem.getDatabase();
Optional<Integer> serverID = database.fetch().getServerID(serverUUID);
Database db = dbSystem.getDatabase();
Optional<Integer> serverID = db.fetch().getServerID(serverUUID);
if (!serverID.isPresent()) {
return registerServer(serverUUID);
}
@ -99,7 +100,7 @@ public class ServerServerInfo extends ServerInfo {
int maxPlayers = serverProperties.getMaxPlayers();
Server server = new Server(serverID.get(), serverUUID, name, webAddress, maxPlayers);
database.save().serverInfoForThisServer(server);
db.executeTransaction(new StoreServerInformationTransaction(server));
return server;
}
@ -114,10 +115,10 @@ public class ServerServerInfo extends ServerInfo {
Server server = new Server(-1, serverUUID, name, webAddress, maxPlayers);
Database database = dbSystem.getDatabase();
database.save().serverInfoForThisServer(server);
Database db = dbSystem.getDatabase();
db.executeTransaction(new StoreServerInformationTransaction(server));
Optional<Integer> serverID = database.fetch().getServerID(serverUUID);
Optional<Integer> serverID = db.fetch().getServerID(serverUUID);
int id = serverID.orElseThrow(() -> new IllegalStateException("Failed to Register Server (ID not found)"));
server.setId(id);

View File

@ -36,6 +36,7 @@ import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
import com.djrapitops.plan.db.access.queries.objects.*;
import com.djrapitops.plan.db.access.transactions.BackupCopyTransaction;
import com.djrapitops.plan.db.access.transactions.StoreConfigTransaction;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.access.transactions.commands.RegisterWebUserTransaction;
import com.djrapitops.plan.db.access.transactions.commands.RemoveEverythingTransaction;
@ -46,7 +47,6 @@ import com.djrapitops.plan.db.access.transactions.init.CleanTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateTablesTransaction;
import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import com.djrapitops.plan.db.sql.tables.SessionsTable;
import com.djrapitops.plan.db.sql.tables.TPSTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
@ -143,8 +143,8 @@ public abstract class CommonDBTest {
}.apply();
db.executeTransaction(new CreateTablesTransaction());
db.executeTransaction(new RemoveEverythingTransaction());
ServerTable serverTable = db.getServerTable();
serverTable.saveCurrentServerInfo(new Server(-1, serverUUID, "ServerName", "", 20));
db.executeTransaction(new StoreServerInformationTransaction(new Server(-1, serverUUID, "ServerName", "", 20)));
assertEquals(serverUUID, db.getServerUUIDSupplier().get());
}

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.db.access.transactions.events.PlayerRegisterTransaction;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import com.djrapitops.plan.system.info.server.Server;
@ -65,7 +66,7 @@ public class SQLiteTest extends CommonDBTest {
UUID bungeeUUID = UUID.randomUUID();
Server bungeeCord = new Server(-1, bungeeUUID, "BungeeCord", "Random:1234", 20);
serverTable.saveCurrentServerInfo(bungeeCord);
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord));
commitTest();

View File

@ -19,6 +19,8 @@ package com.djrapitops.plan.system.info.server;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
import com.djrapitops.plan.system.webserver.WebServer;
@ -78,7 +80,7 @@ public class VelocityServerInfo extends ServerInfo {
String accessAddress = webServer.get().getAccessAddress();
if (!accessAddress.equals(server.getWebAddress())) {
server.setWebAddress(accessAddress);
db.save().serverInfoForThisServer(server);
db.executeTransaction(new StoreServerInformationTransaction(server));
}
}
@ -97,9 +99,9 @@ public class VelocityServerInfo extends ServerInfo {
// TODO Rework to allow Velocity as name.
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.save().serverInfoForThisServer(bungeeCord);
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord));
Optional<Server> bungeeInfo = db.fetch().getBungeeInformation();
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) {
return bungeeInfo.get();
}