Refactored SessionsTable

This commit is contained in:
Rsl1122 2017-10-04 19:10:00 +03:00
parent 4f75d23865
commit da21aa4efd

View File

@ -5,11 +5,13 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException; import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.Session;
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.Select; import main.java.com.djrapitops.plan.database.sql.Select;
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;
@ -95,22 +97,17 @@ public class SessionsTable extends UserIDTable {
* @throws SQLException DB Error * @throws SQLException DB Error
*/ */
private void saveSessionInformation(UUID uuid, Session session) throws SQLException { private void saveSessionInformation(UUID uuid, Session session) throws SQLException {
PreparedStatement statement = null; execute(new ExecStatement(insertStatement) {
try (Connection connection = getConnection()){ @Override
statement = connection.prepareStatement(insertStatement); public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
statement.setLong(2, session.getSessionStart()); statement.setLong(2, session.getSessionStart());
statement.setLong(3, session.getSessionEnd()); statement.setLong(3, session.getSessionEnd());
statement.setInt(4, session.getDeaths()); statement.setInt(4, session.getDeaths());
statement.setInt(5, session.getMobKills()); statement.setInt(5, session.getMobKills());
statement.setString(6, Plan.getServerUUID().toString()); statement.setString(6, Plan.getServerUUID().toString());
statement.execute();
commit(connection);
} finally {
close(statement);
} }
});
} }
/** /**
@ -121,24 +118,27 @@ public class SessionsTable extends UserIDTable {
* @return ID of the inserted session or -1 if session has not been inserted. * @return ID of the inserted session or -1 if session has not been inserted.
*/ */
private int getSessionID(UUID uuid, Session session) throws SQLException { private int getSessionID(UUID uuid, Session session) throws SQLException {
PreparedStatement statement = null; String sql = "SELECT " + columnID + " FROM " + tableName +
ResultSet set = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT " + columnID + " FROM " + tableName +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID + " WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnSessionStart + "=?" + " AND " + columnSessionStart + "=?" +
" AND " + columnSessionEnd + "=?"); " AND " + columnSessionEnd + "=?";
return query(new QueryStatement<Integer>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
statement.setLong(2, session.getSessionStart()); statement.setLong(2, session.getSessionStart());
statement.setLong(3, session.getSessionEnd()); statement.setLong(3, session.getSessionEnd());
set = statement.executeQuery(); }
@Override
public Integer processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return set.getInt(columnID); return set.getInt(columnID);
} }
return -1; return -1;
} finally {
close(set, statement);
} }
});
} }
/** /**
@ -153,15 +153,19 @@ public class SessionsTable extends UserIDTable {
*/ */
private Map<String, List<Session>> getSessionInformation(UUID uuid) throws SQLException { private Map<String, List<Session>> getSessionInformation(UUID uuid) throws SQLException {
Map<Integer, String> serverNames = serverTable.getServerNames(); Map<Integer, String> serverNames = serverTable.getServerNames();
Map<String, List<Session>> sessionsByServer = new HashMap<>(); String sql = Select.from(tableName, "*")
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID) .where(columnUserID + "=" + usersTable.statementSelectID)
.toString()); .toString();
return query(new QueryStatement<Map<String, List<Session>>>(sql, 10000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
set = statement.executeQuery(); }
@Override
public Map<String, List<Session>> processResults(ResultSet set) throws SQLException {
Map<String, List<Session>> sessionsByServer = new HashMap<>();
while (set.next()) { while (set.next()) {
int id = set.getInt(columnID); int id = set.getInt(columnID);
long start = set.getLong(columnSessionStart); long start = set.getLong(columnSessionStart);
@ -179,9 +183,8 @@ public class SessionsTable extends UserIDTable {
sessionsByServer.put(serverName, sessions); sessionsByServer.put(serverName, sessions);
} }
return sessionsByServer; return sessionsByServer;
} finally {
close(set, statement);
} }
});
} }
public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException { public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException {
@ -238,26 +241,29 @@ public class SessionsTable extends UserIDTable {
* @throws SQLException DB Error * @throws SQLException DB Error
*/ */
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException { public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null; String sql = "SELECT" +
ResultSet set = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" + " (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
" FROM " + tableName + " FROM " + tableName +
" WHERE " + columnSessionStart + ">?" + " WHERE " + columnSessionStart + ">?" +
" AND " + columnUserID + "=" + usersTable.statementSelectID + " AND " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnServerID + "=" + serverTable.statementSelectServerID); " AND " + columnServerID + "=" + serverTable.statementSelectServerID;
return query(new QueryStatement<Long>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, afterDate); statement.setLong(1, afterDate);
statement.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
statement.setString(3, serverUUID.toString()); statement.setString(3, serverUUID.toString());
set = statement.executeQuery(); }
@Override
public Long processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return set.getLong("playtime"); return set.getLong("playtime");
} }
return 0; return 0L;
} finally {
close(set, statement);
} }
});
} }
/** /**
@ -281,29 +287,31 @@ public class SessionsTable extends UserIDTable {
*/ */
public Map<String, Long> getPlaytimeByServer(UUID uuid, long afterDate) throws SQLException { public Map<String, Long> getPlaytimeByServer(UUID uuid, long afterDate) throws SQLException {
Map<Integer, String> serverNames = serverTable.getServerNames(); Map<Integer, String> serverNames = serverTable.getServerNames();
Map<String, Long> playtimes = new HashMap<>(); String sql = "SELECT " +
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT " +
"(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime, " + "(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime, " +
columnServerID + columnServerID +
" FROM " + tableName + " FROM " + tableName +
" WHERE " + columnSessionStart + ">?" + " WHERE " + columnSessionStart + ">?" +
" AND " + columnUserID + "=" + usersTable.statementSelectID + " AND " + columnUserID + "=" + usersTable.statementSelectID +
" GROUP BY " + columnServerID); " GROUP BY " + columnServerID;
return query(new QueryStatement<Map<String, Long>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, afterDate); statement.setLong(1, afterDate);
statement.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
set = statement.executeQuery(); }
@Override
public Map<String, Long> processResults(ResultSet set) throws SQLException {
Map<String, Long> playtimes = new HashMap<>();
while (set.next()) { while (set.next()) {
String serverName = serverNames.get(set.getInt(columnServerID)); String serverName = serverNames.get(set.getInt(columnServerID));
long playtime = set.getLong("playtime"); long playtime = set.getLong("playtime");
playtimes.put(serverName, playtime); playtimes.put(serverName, playtime);
} }
return playtimes; return playtimes;
} finally {
close(set, statement);
} }
});
} }
/** /**
@ -347,25 +355,27 @@ public class SessionsTable extends UserIDTable {
* @throws SQLException DB Error * @throws SQLException DB Error
*/ */
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException { public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null; String sql = "SELECT" +
ResultSet set = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" + " (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
" FROM " + tableName + " FROM " + tableName +
" WHERE " + columnSessionStart + ">?" + " WHERE " + columnSessionStart + ">?" +
" AND " + columnServerID + "=" + serverTable.statementSelectServerID); " AND " + columnServerID + "=" + serverTable.statementSelectServerID;
statement.setFetchSize(1000);
return query(new QueryStatement<Long>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, afterDate); statement.setLong(1, afterDate);
statement.setString(2, serverUUID.toString()); statement.setString(2, serverUUID.toString());
set = statement.executeQuery(); }
@Override
public Long processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return set.getLong("playtime"); return set.getLong("playtime");
} }
return 0; return 0L;
} finally {
close(set, statement);
} }
});
} }
/** /**
@ -413,26 +423,29 @@ public class SessionsTable extends UserIDTable {
* @throws SQLException DB Error * @throws SQLException DB Error
*/ */
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException { public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null; String sql = "SELECT" +
ResultSet set = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" COUNT(*) as logintimes" + " COUNT(*) as logintimes" +
" FROM " + tableName + " FROM " + tableName +
" WHERE (" + columnSessionStart + " >= ?)" + " WHERE (" + columnSessionStart + " >= ?)" +
" AND " + columnUserID + "=" + usersTable.statementSelectID + " AND " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnServerID + "=" + serverTable.statementSelectServerID); " AND " + columnServerID + "=" + serverTable.statementSelectServerID;
return query(new QueryStatement<Integer>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, afterDate); statement.setLong(1, afterDate);
statement.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
statement.setString(3, serverUUID.toString()); statement.setString(3, serverUUID.toString());
set = statement.executeQuery(); }
@Override
public Integer processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return set.getInt("logintimes"); return set.getInt("logintimes");
} }
return 0; return 0;
} finally {
close(set, statement);
} }
});
} }
public String getColumnID() { public String getColumnID() {
@ -449,14 +462,10 @@ public class SessionsTable extends UserIDTable {
return new HashMap<>(); return new HashMap<>();
} }
int serverID = id.get(); int serverID = id.get();
Map<UUID, List<Session>> sessionsByUser = new HashMap<>();
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String sql = "SELECT " +
statement = connection.prepareStatement("SELECT " +
columnSessionStart + ", " + columnSessionStart + ", " +
columnSessionEnd + ", " + columnSessionEnd + ", " +
columnDeaths + ", " + columnDeaths + ", " +
@ -464,11 +473,17 @@ public class SessionsTable extends UserIDTable {
usersUUIDColumn + usersUUIDColumn +
" FROM " + tableName + " FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID " WHERE " + columnServerID + "=" + serverTable.statementSelectServerID;
);
statement.setFetchSize(5000); return query(new QueryStatement<Map<UUID, List<Session>>>(sql, 5000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString()); statement.setString(1, serverUUID.toString());
set = statement.executeQuery(); }
@Override
public Map<UUID, List<Session>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<Session>> sessionsByUser = new HashMap<>();
while (set.next()) { while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid")); UUID uuid = UUID.fromString(set.getString("uuid"));
long start = set.getLong(columnSessionStart); long start = set.getLong(columnSessionStart);
@ -481,47 +496,47 @@ public class SessionsTable extends UserIDTable {
sessionsByUser.put(uuid, sessions); sessionsByUser.put(uuid, sessions);
} }
return sessionsByUser; return sessionsByUser;
} finally {
close(set, statement);
} }
});
} }
// TODO Write tests for this method // TODO Write tests for this method
public long getLastSeen(UUID uuid) throws SQLException { public long getLastSeen(UUID uuid) throws SQLException {
PreparedStatement statement = null; String sql = "SELECT" +
ResultSet set = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" MAX(" + columnSessionEnd + ") as last_seen" + " MAX(" + columnSessionEnd + ") as last_seen" +
" FROM " + tableName + " FROM " + tableName +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID); " WHERE " + columnUserID + "=" + usersTable.statementSelectID;
return query(new QueryStatement<Long>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
set = statement.executeQuery(); }
@Override
public Long processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return set.getLong("last_seen"); return set.getLong("last_seen");
} }
return 0; return 0L;
} finally {
close(set, statement);
} }
});
} }
// TODO Write tests for this method // TODO Write tests for this method
public Map<UUID, Long> getLastSeenForAllPlayers() throws SQLException { public Map<UUID, Long> getLastSeenForAllPlayers() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String sql = "SELECT" +
statement = connection.prepareStatement("SELECT" +
" MAX(" + columnSessionEnd + ") as last_seen, " + " MAX(" + columnSessionEnd + ") as last_seen, " +
usersUUIDColumn + usersUUIDColumn +
" FROM " + tableName + " FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" GROUP BY uuid"); " GROUP BY uuid";
statement.setFetchSize(20000);
set = statement.executeQuery(); return query(new QueryAllStatement<Map<UUID, Long>>(sql, 20000) {
@Override
public Map<UUID, Long> processResults(ResultSet set) throws SQLException {
Map<UUID, Long> lastSeenMap = new HashMap<>(); Map<UUID, Long> lastSeenMap = new HashMap<>();
while (set.next()) { while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid")); UUID uuid = UUID.fromString(set.getString("uuid"));
@ -529,21 +544,16 @@ public class SessionsTable extends UserIDTable {
lastSeenMap.put(uuid, lastSeen); lastSeenMap.put(uuid, lastSeen);
} }
return lastSeenMap; return lastSeenMap;
} finally {
close(set, statement);
} }
});
} }
public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException { public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID(); String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid"; String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
String sql = "SELECT " +
statement = connection.prepareStatement("SELECT " +
tableName + "." + columnID + ", " + tableName + "." + columnID + ", " +
columnSessionStart + ", " + columnSessionStart + ", " +
columnSessionEnd + ", " + columnSessionEnd + ", " +
@ -553,11 +563,11 @@ public class SessionsTable extends UserIDTable {
serverUUIDColumn + serverUUIDColumn +
" FROM " + tableName + " FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID " JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
);
statement.setFetchSize(20000);
set = statement.executeQuery();
return query(new QueryAllStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) {
@Override
public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException {
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>(); Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
while (set.next()) { while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString("s_uuid")); UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
@ -584,18 +594,18 @@ public class SessionsTable extends UserIDTable {
db.getWorldTimesTable().addWorldTimesToSessions(map); db.getWorldTimesTable().addWorldTimesToSessions(map);
} }
return map; return map;
} finally {
close(set, statement);
} }
});
} }
public void insertSessions(Map<UUID, Map<UUID, List<Session>>> allSessions, boolean saveKillsAndWorldTimes) throws SQLException { public void insertSessions(Map<UUID, Map<UUID, List<Session>>> allSessions, boolean saveKillsAndWorldTimes) throws SQLException {
if (Verify.isEmpty(allSessions)) { if (Verify.isEmpty(allSessions)) {
return; return;
} }
PreparedStatement statement = null;
try (Connection connection = getConnection()){ executeBatch(new ExecStatement(insertStatement) {
statement = connection.prepareStatement(insertStatement); @Override
public void prepare(PreparedStatement statement) throws SQLException {
for (UUID serverUUID : allSessions.keySet()) { for (UUID serverUUID : allSessions.keySet()) {
for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) { for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) {
UUID uuid = entry.getKey(); UUID uuid = entry.getKey();
@ -612,12 +622,8 @@ public class SessionsTable extends UserIDTable {
} }
} }
} }
statement.executeBatch();
commit(connection);
} finally {
close(statement);
} }
});
if (saveKillsAndWorldTimes) { if (saveKillsAndWorldTimes) {
Map<UUID, Map<UUID, List<Session>>> savedSessions = getAllSessions(false); Map<UUID, Map<UUID, List<Session>>> savedSessions = getAllSessions(false);
matchSessionIDs(allSessions, savedSessions); matchSessionIDs(allSessions, savedSessions);