mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-27 09:00:28 +08:00
Refactored SQLDB#clean into a transaction
This commit is contained in:
parent
0f79263424
commit
4ee31de3ed
@ -22,8 +22,8 @@ import com.djrapitops.plan.data.store.containers.NetworkContainer;
|
||||
import com.djrapitops.plan.db.access.ExecStatement;
|
||||
import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.access.transactions.CleanTransaction;
|
||||
import com.djrapitops.plan.db.access.transactions.CreateTablesTransaction;
|
||||
import com.djrapitops.plan.db.access.transactions.RemovePlayerTransaction;
|
||||
import com.djrapitops.plan.db.access.transactions.Transaction;
|
||||
import com.djrapitops.plan.db.patches.*;
|
||||
import com.djrapitops.plan.db.sql.tables.*;
|
||||
@ -32,7 +32,6 @@ import com.djrapitops.plan.db.tasks.PatchTask;
|
||||
import com.djrapitops.plan.system.database.databases.operation.*;
|
||||
import com.djrapitops.plan.system.database.databases.sql.operation.*;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.locale.lang.PluginLang;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.settings.paths.PluginSettings;
|
||||
import com.djrapitops.plan.system.settings.paths.TimeSettings;
|
||||
@ -49,13 +48,10 @@ import com.djrapitops.plugin.utilities.Verify;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Class containing main logic for different data related save and load functionality.
|
||||
@ -167,7 +163,9 @@ public abstract class SQLDB extends AbstractDatabase {
|
||||
public void run() {
|
||||
try {
|
||||
if (isOpen()) {
|
||||
clean();
|
||||
executeTransaction(new CleanTransaction(serverUUIDSupplier.get(),
|
||||
config.get(TimeSettings.KEEP_INACTIVE_PLAYERS), logger, locale)
|
||||
);
|
||||
}
|
||||
} catch (DBOpException e) {
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
@ -262,26 +260,6 @@ public abstract class SQLDB extends AbstractDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
private void clean() {
|
||||
tpsTable.clean();
|
||||
pingTable.clean();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long keepActiveAfter = now - config.get(TimeSettings.KEEP_INACTIVE_PLAYERS);
|
||||
|
||||
List<UUID> inactivePlayers = sessionsTable.getLastSeenForAllPlayers().entrySet().stream()
|
||||
.filter(entry -> entry.getValue() < keepActiveAfter)
|
||||
.map(Map.Entry::getKey)
|
||||
.collect(Collectors.toList());
|
||||
for (UUID uuid : inactivePlayers) {
|
||||
executeTransaction(new RemovePlayerTransaction(uuid));
|
||||
}
|
||||
int removed = inactivePlayers.size();
|
||||
if (removed > 0) {
|
||||
logger.info(locale.getString(PluginLang.DB_NOTIFY_CLEAN, removed));
|
||||
}
|
||||
}
|
||||
|
||||
public abstract Connection getConnection() throws SQLException;
|
||||
|
||||
@Deprecated
|
||||
|
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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.data.store.objects.DateObj;
|
||||
import com.djrapitops.plan.db.access.ExecStatement;
|
||||
import com.djrapitops.plan.db.access.Executable;
|
||||
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
|
||||
import com.djrapitops.plan.db.sql.tables.PingTable;
|
||||
import com.djrapitops.plan.db.sql.tables.TPSTable;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.locale.lang.PluginLang;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Transaction for cleaning up old data from the database.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class CleanTransaction extends Transaction {
|
||||
|
||||
private final UUID serverUUID;
|
||||
|
||||
private final long keepInactiveForMs;
|
||||
|
||||
private final PluginLogger logger;
|
||||
private final Locale locale;
|
||||
|
||||
public CleanTransaction(
|
||||
UUID serverUUID,
|
||||
long keepInactiveForMs,
|
||||
PluginLogger logger,
|
||||
Locale locale
|
||||
) {
|
||||
this.serverUUID = serverUUID;
|
||||
this.keepInactiveForMs = keepInactiveForMs;
|
||||
this.logger = logger;
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
Optional<Integer> allTimePeak = db.query(OptionalFetchQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(DateObj::getValue);
|
||||
|
||||
execute(cleanTPSTable(allTimePeak.orElse(-1)));
|
||||
execute(cleanPingTable());
|
||||
|
||||
int removed = cleanOldPlayers();
|
||||
if (removed > 0) {
|
||||
logger.info(locale.getString(PluginLang.DB_NOTIFY_CLEAN, removed));
|
||||
}
|
||||
}
|
||||
|
||||
private int cleanOldPlayers() {
|
||||
long now = System.currentTimeMillis();
|
||||
long keepActiveAfter = now - keepInactiveForMs;
|
||||
|
||||
List<UUID> inactivePlayers = db.getSessionsTable().getLastSeenForAllPlayers().entrySet().stream()
|
||||
.filter(entry -> entry.getValue() < keepActiveAfter)
|
||||
.map(Map.Entry::getKey)
|
||||
.collect(Collectors.toList());
|
||||
for (UUID uuid : inactivePlayers) {
|
||||
// TODO Figure out a better way to schedule transactions from within transactions
|
||||
// or figure out a way to execute the transaction within this transaction.
|
||||
db.executeTransaction(new RemovePlayerTransaction(uuid));
|
||||
}
|
||||
return inactivePlayers.size();
|
||||
}
|
||||
|
||||
private Executable cleanTPSTable(int allTimePlayerPeak) {
|
||||
String sql = "DELETE FROM " + TPSTable.TABLE_NAME +
|
||||
" WHERE (" + TPSTable.DATE + "<?)" +
|
||||
" AND (" + TPSTable.PLAYERS_ONLINE + "!=?)";
|
||||
|
||||
return new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
// More than 3 Months ago.
|
||||
long threeMonths = TimeAmount.MONTH.toMillis(3L);
|
||||
statement.setLong(1, System.currentTimeMillis() - threeMonths);
|
||||
statement.setInt(2, allTimePlayerPeak);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Executable cleanPingTable() {
|
||||
String sql = "DELETE FROM " + PingTable.TABLE_NAME +
|
||||
" WHERE (" + PingTable.DATE + "<?)" +
|
||||
" OR (" + PingTable.MIN_PING + "<0)";
|
||||
|
||||
return new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
long twoWeeks = TimeAmount.WEEK.toMillis(2L);
|
||||
statement.setLong(1, System.currentTimeMillis() - twoWeeks);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -24,7 +24,6 @@ import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.patches.PingOptimizationPatch;
|
||||
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
|
||||
import com.djrapitops.plan.db.sql.parsing.Sql;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -78,20 +77,6 @@ public class PingTable extends Table {
|
||||
.toString();
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
String sql = "DELETE FROM " + tableName +
|
||||
" WHERE (" + DATE + "<?)" +
|
||||
" OR (" + MIN_PING + "<0)";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
long twoWeeks = TimeAmount.WEEK.toMillis(2L);
|
||||
statement.setLong(1, System.currentTimeMillis() - twoWeeks);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void insertPing(UUID uuid, Ping ping) {
|
||||
execute(new ExecStatement(INSERT_STATEMENT) {
|
||||
@Override
|
||||
|
@ -18,7 +18,6 @@ package com.djrapitops.plan.db.sql.tables;
|
||||
|
||||
import com.djrapitops.plan.data.container.TPS;
|
||||
import com.djrapitops.plan.data.container.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.data.store.objects.DateObj;
|
||||
import com.djrapitops.plan.db.DBType;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.ExecStatement;
|
||||
@ -128,32 +127,6 @@ public class TPSTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the TPS Table of old data.
|
||||
*/
|
||||
public void clean() {
|
||||
Optional<DateObj<Integer>> allTimePeak = db.query(OptionalFetchQueries.fetchAllTimePeakPlayerCount(getServerUUID()));
|
||||
int p = -1;
|
||||
if (allTimePeak.isPresent()) {
|
||||
p = allTimePeak.get().getValue();
|
||||
}
|
||||
final int pValue = p;
|
||||
|
||||
String sql = "DELETE FROM " + tableName +
|
||||
" WHERE (" + DATE + "<?)" +
|
||||
" AND (" + PLAYERS_ONLINE + " != ?)";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
// More than 3 Months ago.
|
||||
long threeMonths = TimeAmount.MONTH.toMillis(3L);
|
||||
statement.setLong(1, System.currentTimeMillis() - threeMonths);
|
||||
statement.setInt(2, pValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void insertTPS(TPS tps) {
|
||||
execute(new ExecStatement(INSERT_STATEMENT) {
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user