Refactored BatchOperationTable into BackupCopyTransaction

This commit is contained in:
Rsl1122 2019-01-24 15:54:39 +02:00
parent 5e8cbf1a88
commit 2be3b2cf34
4 changed files with 109 additions and 168 deletions

View File

@ -0,0 +1,105 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access.transactions;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.tables.UsersTable;
/**
* Transaction that performs a clear + copy operation to duplicate a source database in the current one.
*
* @author Rsl1122
*/
public class BackupCopyTransaction extends RemoveEverythingTransaction {
private final Database sourceDB;
public BackupCopyTransaction(Database sourceDB) {
this.sourceDB = sourceDB;
}
@Override
protected boolean shouldBeExecuted() {
return !sourceDB.equals(db) && sourceDB.isOpen();
}
@Override
protected void execute() {
super.execute();
copyServers();
copyUsers();
copyWorlds();
copyTPS();
copyWebUsers();
copyCommandUse();
copyIPsAndGeolocs();
copyNicknames();
copySessions();
copyUserInfo();
copyPings();
}
private void copyPings() {
db.getPingTable().insertAllPings(sourceDB.query(LargeFetchQueries.fetchAllPingData()));
}
private void copyCommandUse() {
db.getCommandUseTable().insertCommandUsage(sourceDB.query(LargeFetchQueries.fetchAllCommandUsageData()));
}
private void copyIPsAndGeolocs() {
db.getGeoInfoTable().insertAllGeoInfo(sourceDB.query(LargeFetchQueries.fetchAllGeoInfoData()));
}
private void copyNicknames() {
db.getNicknamesTable().insertNicknames(sourceDB.query(LargeFetchQueries.fetchAllNicknameData()));
}
private void copyWebUsers() {
db.getSecurityTable().addUsers(sourceDB.query(LargeFetchQueries.fetchAllPlanWebUsers()));
}
private void copyServers() {
db.getServerTable().insertAllServers(sourceDB.query(LargeFetchQueries.fetchPlanServerInformation()).values());
}
private void copyTPS() {
db.getTpsTable().insertAllTPS(sourceDB.query(LargeFetchQueries.fetchAllTPSData()));
}
private void copyUserInfo() {
db.getUserInfoTable().insertUserInfo(sourceDB.query(LargeFetchQueries.fetchPerServerUserInformation()));
}
private void copyWorlds() {
db.getWorldTable().saveWorlds(sourceDB.query(LargeFetchQueries.fetchAllWorldNames()));
}
private void copyUsers() {
UsersTable fromTable = db.getUsersTable();
UsersTable toTable = db.getUsersTable();
toTable.insertUsers(sourceDB.query(LargeFetchQueries.fetchAllCommonUserInformation()));
toTable.updateKicked(fromTable.getAllTimesKicked());
}
private void copySessions() {
db.getSessionsTable().insertSessions(sourceDB.query(LargeFetchQueries.fetchAllSessionsWithKillAndWorldData()), true);
}
}

View File

@ -35,7 +35,7 @@ import java.sql.Savepoint;
*/
public abstract class Transaction {
private SQLDB db;
SQLDB db;
private Connection connection;
private Savepoint savepoint;

View File

@ -1,157 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.sql.tables.move;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.tables.Table;
import com.djrapitops.plan.db.sql.tables.UsersTable;
/**
* A Fake table used to store a lot of big table operations.
* <p>
* 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);}
* <p>
* The copy methods assume that the table has been cleared, or that no duplicate data will be entered for a user.
* <p>
* Server and User tables should be copied first.
*
* @author Rsl1122
*/
public class BatchOperationTable extends Table {
/**
* Constructor.
* <p>
* Call to access copy functionality.
*
* @param database Database to copy things from
* @throws IllegalStateException if database.init has not been called.
* @throws ClassCastException if database is not SQLDB.
*/
public BatchOperationTable(SQLDB database) {
super("", database);
if (!db.isOpen()) {
throw new IllegalStateException("Given Database had not been initialized.");
}
}
public void copyEverything(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.executeTransaction(new RemoveEverythingTransaction());
copyServers(toDB);
copyUsers(toDB);
copyWorlds(toDB);
copyTPS(toDB);
copyWebUsers(toDB);
copyCommandUse(toDB);
copyIPsAndGeolocs(toDB);
copyNicknames(toDB);
copySessions(toDB);
copyUserInfo(toDB);
copyPings(toDB);
}
public void copyPings(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getPingTable().insertAllPings(db.query(LargeFetchQueries.fetchAllPingData()));
}
public void copyCommandUse(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getCommandUseTable().insertCommandUsage(db.query(LargeFetchQueries.fetchAllCommandUsageData()));
}
public void copyIPsAndGeolocs(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getGeoInfoTable().insertAllGeoInfo(db.query(LargeFetchQueries.fetchAllGeoInfoData()));
}
public void copyNicknames(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getNicknamesTable().insertNicknames(db.query(LargeFetchQueries.fetchAllNicknameData()));
}
public void copyWebUsers(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getSecurityTable().addUsers(db.query(LargeFetchQueries.fetchAllPlanWebUsers()));
}
public void copyServers(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getServerTable().insertAllServers(db.query(LargeFetchQueries.fetchPlanServerInformation()).values());
}
public void copyTPS(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getTpsTable().insertAllTPS(db.query(LargeFetchQueries.fetchAllTPSData()));
}
public void copyUserInfo(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getUserInfoTable().insertUserInfo(db.query(LargeFetchQueries.fetchPerServerUserInformation()));
}
public void copyWorlds(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getWorldTable().saveWorlds(db.query(LargeFetchQueries.fetchAllWorldNames()));
}
public void copyUsers(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
UsersTable fromTable = db.getUsersTable();
UsersTable toTable = toDB.db.getUsersTable();
toTable.insertUsers(db.query(LargeFetchQueries.fetchAllCommonUserInformation()));
toTable.updateKicked(fromTable.getAllTimesKicked());
}
public void copySessions(BatchOperationTable toDB) {
if (toDB.equals(this)) {
return;
}
toDB.db.getSessionsTable().insertSessions(db.query(LargeFetchQueries.fetchAllSessionsWithKillAndWorldData()), true);
}
}

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.system.database.databases.sql.operation;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.sql.tables.move.BatchOperationTable;
import com.djrapitops.plan.db.access.transactions.BackupCopyTransaction;
import com.djrapitops.plan.system.database.databases.operation.BackupOperations;
public class SQLBackupOps extends SQLOps implements BackupOperations {
@ -29,18 +29,11 @@ public class SQLBackupOps extends SQLOps implements BackupOperations {
@Override
public void backup(Database toDatabase) {
if (toDatabase instanceof SQLDB) {
BatchOperationTable toDB = new BatchOperationTable((SQLDB) toDatabase);
BatchOperationTable fromDB = new BatchOperationTable(db);
fromDB.copyEverything(toDB);
} else {
throw new IllegalArgumentException("Database was not a SQL database - backup not implemented.");
}
toDatabase.executeTransaction(new BackupCopyTransaction(db));
}
@Override
public void restore(Database fromDatabase) {
fromDatabase.backup().backup(db);
db.executeTransaction(new BackupCopyTransaction(fromDatabase));
}
}