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