Batch Operations for PlayerKills (Session Batch operations complete)

This commit is contained in:
Rsl1122 2017-09-03 14:47:33 +03:00
parent 604bf7e29c
commit a91338dab8
4 changed files with 110 additions and 22 deletions

View File

@ -5,6 +5,7 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.data.PlayerKill;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
@ -26,6 +27,7 @@ public class KillsTable extends UserIDTable {
private final String columnSessionID = "session_id";
private final SessionsTable sessionsTable;
private String insertStatement;
/**
* @param db
@ -34,6 +36,16 @@ public class KillsTable extends UserIDTable {
public KillsTable(SQLDB db, boolean usingMySQL) {
super("plan_kills", db, usingMySQL);
sessionsTable = db.getSessionsTable();
insertStatement = "INSERT INTO " + tableName + " ("
+ columnKillerUserID + ", "
+ columnVictimUserID + ", "
+ columnSessionID + ", "
+ columnDate + ", "
+ columnWeapon
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ usersTable.statementSelectID + ", "
+ "?, ?, ?)";
}
/**
@ -55,7 +67,7 @@ public class KillsTable extends UserIDTable {
}
@Override
public void removeUser(UUID uuid) throws SQLException{
public void removeUser(UUID uuid) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName +
@ -77,16 +89,7 @@ public class KillsTable extends UserIDTable {
}
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnKillerUserID + ", "
+ columnVictimUserID + ", "
+ columnSessionID + ", "
+ columnDate + ", "
+ columnWeapon
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ usersTable.statementSelectID + ", "
+ "?, ?, ?)");
statement = prepareStatement(insertStatement);
for (PlayerKill kill : playerKills) {
UUID victim = kill.getVictim();
long date = kill.getTime();
@ -183,11 +186,91 @@ public class KillsTable extends UserIDTable {
}
}
public void addKillsToSessions(Map<UUID, Map<UUID, List<Session>>> map) {
// TODO
public void addKillsToSessions(Map<UUID, Map<UUID, List<Session>>> map) throws SQLException {
Map<Integer, List<PlayerKill>> playerKillsBySessionID = getAllPlayerKillsBySessionID();
for (UUID serverUUID : map.keySet()) {
for (List<Session> sessions : map.get(serverUUID).values()) {
for (Session session : sessions) {
List<PlayerKill> playerKills = playerKillsBySessionID.get(session.getSessionID());
if (playerKills != null) {
session.setPlayerKills(playerKills);
}
}
}
}
}
public void savePlayerKills(Map<UUID, Map<UUID, List<Session>>> allSessions) {
// TODO
public void savePlayerKills(Map<UUID, Map<UUID, List<Session>>> allSessions) throws SQLException {
if (Verify.isEmpty(allSessions)) {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
String[] gms = GMTimes.getGMKeyArray();
for (UUID serverUUID : allSessions.keySet()) {
for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) {
UUID uuid = entry.getKey();
List<Session> sessions = entry.getValue();
for (Session session : sessions) {
int sessionID = session.getSessionID();
for (PlayerKill kill : session.getPlayerKills()) {
UUID victim = kill.getVictim();
long date = kill.getTime();
String weapon = kill.getWeapon();
statement.setString(1, uuid.toString());
statement.setString(2, victim.toString());
statement.setInt(3, sessionID);
statement.setLong(4, date);
statement.setString(5, weapon);
statement.addBatch();
}
}
}
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
close(statement);
}
}
public Map<Integer, List<PlayerKill>> getAllPlayerKillsBySessionID() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
statement = prepareStatement("SELECT " +
columnSessionID + ", " +
columnDate + ", " +
columnWeapon + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnVictimUserID);
statement.setFetchSize(10000);
set = statement.executeQuery();
Map<Integer, List<PlayerKill>> allPlayerKills = new HashMap<>();
while (set.next()) {
int sessionID = set.getInt(columnSessionID);
List<PlayerKill> playerKills = allPlayerKills.getOrDefault(sessionID, new ArrayList<>());
String uuidS = set.getString("victim_uuid");
UUID victim = UUID.fromString(uuidS);
long date = set.getLong(columnDate);
String weapon = set.getString(columnWeapon);
playerKills.add(new PlayerKill(victim, weapon, date));
allPlayerKills.put(sessionID, playerKills);
}
return allPlayerKills;
} finally {
close(set, statement);
}
}
}

View File

@ -547,7 +547,7 @@ public class SessionsTable extends UserIDTable {
}
}
public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean includeExtraData) throws SQLException {
public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
@ -592,7 +592,7 @@ public class SessionsTable extends UserIDTable {
sessionsByUser.put(uuid, sessions);
map.put(serverUUID, sessionsByUser);
}
if (includeExtraData) {
if (getKillsAndWorldTimes) {
db.getKillsTable().addKillsToSessions(map);
db.getWorldTimesTable().addWorldTimesToSessions(map);
}
@ -603,7 +603,7 @@ public class SessionsTable extends UserIDTable {
}
}
public void insertSessions(Map<UUID, Map<UUID, List<Session>>> allSessions, boolean containsExtraData) throws SQLException {
public void insertSessions(Map<UUID, Map<UUID, List<Session>>> allSessions, boolean saveKillsAndWorldTimes) throws SQLException {
if (Verify.isEmpty(allSessions)) {
return;
}
@ -632,7 +632,7 @@ public class SessionsTable extends UserIDTable {
} finally {
close(statement);
}
if (containsExtraData) {
if (saveKillsAndWorldTimes) {
Map<UUID, Map<UUID, List<Session>>> savedSessions = getAllSessions(false);
matchSessionIDs(allSessions, savedSessions);
db.getKillsTable().savePlayerKills(allSessions);

View File

@ -284,7 +284,6 @@ public class WorldTimesTable extends UserIDTable {
for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) {
UUID uuid = entry.getKey();
List<Session> sessions = entry.getValue();
for (Session session : sessions) {
int sessionID = session.getSessionID();
for (Map.Entry<String, GMTimes> worldTimesEntry : session.getWorldTimes().getWorldTimes().entrySet()) {
@ -293,7 +292,6 @@ public class WorldTimesTable extends UserIDTable {
statement.setString(1, uuid.toString());
statement.setString(2, worldName);
statement.setInt(3, sessionID);
statement.setLong(4, gmTimes.getTime(gms[0]));
statement.setLong(5, gmTimes.getTime(gms[1]));
statement.setLong(6, gmTimes.getTime(gms[2]));

View File

@ -72,7 +72,7 @@ public class BatchOperationTable extends Table {
copyNicknames(toDB);
copyTPS(toDB);
copyWebUsers(toDB);
// TODO WorldTimes, Sessions, PlayerKills
copySessions(toDB);
}
public void copyActions(BatchOperationTable toDB) throws SQLException {
@ -150,4 +150,11 @@ public class BatchOperationTable extends Table {
toTable.insertUsers(fromTable.getUsers());
toTable.updateKicked(fromTable.getAllTimesKicked());
}
public void copySessions(BatchOperationTable toDB) throws SQLException {
if (toDB.equals(this)) {
return;
}
toDB.db.getSessionsTable().insertSessions(db.getSessionsTable().getAllSessions(true), true);
}
}