Refactored WorldTimesTable (DB Table refactor complete)

This commit is contained in:
Rsl1122 2017-10-06 17:40:27 +03:00
parent 5433116cdb
commit a268a092d7

View File

@ -7,10 +7,12 @@ import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.data.time.WorldTimes;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
import main.java.com.djrapitops.plan.database.processing.QueryAllStatement;
import main.java.com.djrapitops.plan.database.processing.QueryStatement;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -86,76 +88,73 @@ public class WorldTimesTable extends UserIDTable {
Set<String> worldNames = worldTimesMap.keySet();
db.getWorldTable().saveWorlds(worldNames);
PreparedStatement statement = null;
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<String, GMTimes> entry : worldTimesMap.entrySet()) {
String worldName = entry.getKey();
GMTimes gmTimes = entry.getValue();
statement.setString(1, uuid.toString());
statement.setString(2, worldName);
statement.setInt(3, sessionID);
for (Map.Entry<String, GMTimes> entry : worldTimesMap.entrySet()) {
String worldName = entry.getKey();
GMTimes gmTimes = entry.getValue();
statement.setString(1, uuid.toString());
statement.setString(2, worldName);
statement.setInt(3, sessionID);
String[] gms = GMTimes.getGMKeyArray();
statement.setLong(4, gmTimes.getTime(gms[0]));
statement.setLong(5, gmTimes.getTime(gms[1]));
statement.setLong(6, gmTimes.getTime(gms[2]));
statement.setLong(7, gmTimes.getTime(gms[3]));
statement.addBatch();
String[] gms = GMTimes.getGMKeyArray();
statement.setLong(4, gmTimes.getTime(gms[0]));
statement.setLong(5, gmTimes.getTime(gms[1]));
statement.setLong(6, gmTimes.getTime(gms[2]));
statement.setLong(7, gmTimes.getTime(gms[3]));
statement.addBatch();
}
}
statement.executeBatch();
commit(connection);
} finally {
close(statement);
}
});
}
public void addWorldTimesToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = connection.prepareStatement("SELECT " +
columnSessionID + ", " +
columnSurvival + ", " +
columnCreative + ", " +
columnAdventure + ", " +
columnSpectator + ", " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID
);
statement.setFetchSize(2000);
statement.setString(1, uuid.toString());
set = statement.executeQuery();
String[] gms = GMTimes.getGMKeyArray();
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
String sql = "SELECT " +
columnSessionID + ", " +
columnSurvival + ", " +
columnCreative + ", " +
columnAdventure + ", " +
columnSpectator + ", " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID;
while (set.next()) {
int sessionID = set.getInt(columnSessionID);
Session session = sessions.get(sessionID);
if (session == null) {
continue;
}
String worldName = set.getString("world_name");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong(columnSurvival));
gmMap.put(gms[1], set.getLong(columnCreative));
gmMap.put(gms[2], set.getLong(columnAdventure));
gmMap.put(gms[3], set.getLong(columnSpectator));
GMTimes gmTimes = new GMTimes(gmMap);
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
query(new QueryStatement<Object>(sql, 2000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
} finally {
close(set, statement);
}
@Override
public Object processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray();
while (set.next()) {
int sessionID = set.getInt(columnSessionID);
Session session = sessions.get(sessionID);
if (session == null) {
continue;
}
String worldName = set.getString("world_name");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong(columnSurvival));
gmMap.put(gms[1], set.getLong(columnCreative));
gmMap.put(gms[2], set.getLong(columnAdventure));
gmMap.put(gms[3], set.getLong(columnSpectator));
GMTimes gmTimes = new GMTimes(gmMap);
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
}
return null;
}
});
}
public WorldTimes getWorldTimesOfServer() throws SQLException {
@ -163,87 +162,90 @@ public class WorldTimesTable extends UserIDTable {
}
public WorldTimes getWorldTimesOfServer(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
String sessionIDColumn = sessionsTable + "." + sessionsTable.getColumnID();
String sessionServerIDColumn = sessionsTable + ".server_id";
statement = connection.prepareStatement("SELECT " +
"SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " +
"SUM(" + columnSpectator + ") as spectator, " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" JOIN " + sessionsTable + " on " + sessionIDColumn + "=" + columnSessionID +
" WHERE " + sessionServerIDColumn + "=" + db.getServerTable().statementSelectServerID +
" GROUP BY " + columnWorldId
);
statement.setFetchSize(1000);
statement.setString(1, serverUUID.toString());
set = statement.executeQuery();
String[] gms = GMTimes.getGMKeyArray();
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
String sessionIDColumn = sessionsTable + "." + sessionsTable.getColumnID();
String sessionServerIDColumn = sessionsTable + ".server_id";
String sql = "SELECT " +
"SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " +
"SUM(" + columnSpectator + ") as spectator, " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" JOIN " + sessionsTable + " on " + sessionIDColumn + "=" + columnSessionID +
" WHERE " + sessionServerIDColumn + "=" + db.getServerTable().statementSelectServerID +
" GROUP BY " + columnWorldId;
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world_name");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong("survival"));
gmMap.put(gms[1], set.getLong("creative"));
gmMap.put(gms[2], set.getLong("adventure"));
gmMap.put(gms[3], set.getLong("spectator"));
GMTimes gmTimes = new GMTimes(gmMap);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
return query(new QueryStatement<WorldTimes>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
return worldTimes;
} finally {
close(set, statement);
}
@Override
public WorldTimes processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray();
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world_name");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong("survival"));
gmMap.put(gms[1], set.getLong("creative"));
gmMap.put(gms[2], set.getLong("adventure"));
gmMap.put(gms[3], set.getLong("spectator"));
GMTimes gmTimes = new GMTimes(gmMap);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
}
return worldTimes;
}
});
}
public WorldTimes getWorldTimesOfUser(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = connection.prepareStatement("SELECT " +
"SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " +
"SUM(" + columnSpectator + ") as spectator, " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" GROUP BY " + columnWorldId
);
statement.setString(1, uuid.toString());
set = statement.executeQuery();
String[] gms = GMTimes.getGMKeyArray();
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
String sql = "SELECT " +
"SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " +
"SUM(" + columnSpectator + ") as spectator, " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" GROUP BY " + columnWorldId;
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world_name");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong("survival"));
gmMap.put(gms[1], set.getLong("creative"));
gmMap.put(gms[2], set.getLong("adventure"));
gmMap.put(gms[3], set.getLong("spectator"));
GMTimes gmTimes = new GMTimes(gmMap);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
return query(new QueryStatement<WorldTimes>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
return worldTimes;
} finally {
close(set, statement);
}
@Override
public WorldTimes processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray();
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world_name");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong("survival"));
gmMap.put(gms[1], set.getLong("creative"));
gmMap.put(gms[2], set.getLong("adventure"));
gmMap.put(gms[3], set.getLong("spectator"));
GMTimes gmTimes = new GMTimes(gmMap);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
}
return worldTimes;
}
});
}
public void addWorldTimesToSessions(Map<UUID, Map<UUID, List<Session>>> map) throws SQLException {
@ -277,78 +279,76 @@ public class WorldTimesTable extends UserIDTable {
.collect(Collectors.toList());
db.getWorldTable().saveWorlds(worldNames);
PreparedStatement statement = null;
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
String[] gms = GMTimes.getGMKeyArray();
for (Map<UUID, List<Session>> serverSessions : allSessions.values()) {
for (Map.Entry<UUID, List<Session>> entry : serverSessions.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()) {
String worldName = worldTimesEntry.getKey();
GMTimes gmTimes = worldTimesEntry.getValue();
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]));
statement.setLong(7, gmTimes.getTime(gms[3]));
statement.addBatch();
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
String[] gms = GMTimes.getGMKeyArray();
// Every Server
for (Map<UUID, List<Session>> serverSessions : allSessions.values()) {
// Every User
for (Map.Entry<UUID, List<Session>> entry : serverSessions.entrySet()) {
UUID uuid = entry.getKey();
List<Session> sessions = entry.getValue();
// Every Session
for (Session session : sessions) {
int sessionID = session.getSessionID();
// Every WorldTimes
for (Map.Entry<String, GMTimes> worldTimesEntry : session.getWorldTimes().getWorldTimes().entrySet()) {
String worldName = worldTimesEntry.getKey();
GMTimes gmTimes = worldTimesEntry.getValue();
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]));
statement.setLong(7, gmTimes.getTime(gms[3]));
statement.addBatch();
}
}
}
}
}
statement.executeBatch();
commit(connection);
} finally {
close(statement);
}
});
}
public Map<Integer, WorldTimes> getAllWorldTimesBySessionID() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = connection.prepareStatement("SELECT " +
columnSessionID + ", " +
columnSurvival + ", " +
columnCreative + ", " +
columnAdventure + ", " +
columnSpectator + ", " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId
);
statement.setFetchSize(50000);
set = statement.executeQuery();
String[] gms = GMTimes.getGMKeyArray();
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
String sql = "SELECT " +
columnSessionID + ", " +
columnSurvival + ", " +
columnCreative + ", " +
columnAdventure + ", " +
columnSpectator + ", " +
worldNameColumn +
" FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId;
Map<Integer, WorldTimes> worldTimes = new HashMap<>();
while (set.next()) {
int sessionID = set.getInt(columnSessionID);
return query(new QueryAllStatement<Map<Integer, WorldTimes>>(sql, 50000) {
@Override
public Map<Integer, WorldTimes> processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray();
String worldName = set.getString("world_name");
Map<Integer, WorldTimes> worldTimes = new HashMap<>();
while (set.next()) {
int sessionID = set.getInt(columnSessionID);
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong(columnSurvival));
gmMap.put(gms[1], set.getLong(columnCreative));
gmMap.put(gms[2], set.getLong(columnAdventure));
gmMap.put(gms[3], set.getLong(columnSpectator));
GMTimes gmTimes = new GMTimes(gmMap);
String worldName = set.getString("world_name");
WorldTimes worldTOfSession = worldTimes.getOrDefault(sessionID, new WorldTimes(new HashMap<>()));
worldTOfSession.setGMTimesForWorld(worldName, gmTimes);
worldTimes.put(sessionID, worldTOfSession);
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong(columnSurvival));
gmMap.put(gms[1], set.getLong(columnCreative));
gmMap.put(gms[2], set.getLong(columnAdventure));
gmMap.put(gms[3], set.getLong(columnSpectator));
GMTimes gmTimes = new GMTimes(gmMap);
WorldTimes worldTOfSession = worldTimes.getOrDefault(sessionID, new WorldTimes(new HashMap<>()));
worldTOfSession.setGMTimesForWorld(worldName, gmTimes);
worldTimes.put(sessionID, worldTOfSession);
}
return worldTimes;
}
return worldTimes;
} finally {
close(set, statement);
}
});
}
}