Ping table structure optimization

- Replaced user_id with uuid
- Replaced server_id with server_uuid

- UserUUIDTable replacing UserIDTable for remove user method
This commit is contained in:
Rsl1122 2018-12-13 18:54:23 +02:00
parent 53adc8e7ec
commit 35d6d0bbb4
9 changed files with 185 additions and 78 deletions

View File

@ -204,7 +204,8 @@ public abstract class SQLDB extends Database {
new DiskUsagePatch(this),
new WorldsOptimizationPatch(this),
new WorldTimesOptimizationPatch(this),
new SessionsOptimizationPatch(this)
new SessionsOptimizationPatch(this),
new PingOptimizationPatch(this)
};
try {

View File

@ -0,0 +1,80 @@
/*
* 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.system.database.databases.sql.patches;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.PingTable;
import com.djrapitops.plan.system.database.databases.sql.tables.PingTable.Col;
public class PingOptimizationPatch extends Patch {
private String tempTableName;
private String tableName;
public PingOptimizationPatch(SQLDB db) {
super(db);
tableName = PingTable.TABLE_NAME;
tempTableName = "temp_ping";
}
@Override
public boolean hasBeenApplied() {
return hasColumn(tableName, Col.UUID.get())
&& hasColumn(tableName, Col.SERVER_UUID.get())
&& !hasColumn(tableName, "user_id")
&& !hasColumn(tableName, "server_id")
&& !hasTable(tempTableName); // If this table exists the patch has failed to finish.
}
@Override
public void apply() {
try {
tempOldTable();
db.getPingTable().createTable();
db.execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " +
Col.SERVER_UUID + ", " +
Col.ID + ", " +
Col.MIN_PING + ", " +
Col.MAX_PING + ", " +
Col.AVG_PING + ", " +
Col.DATE +
") SELECT " +
"(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " +
"(SELECT plan_servers.uuid FROM plan_servers WHERE plan_servers.id = " + tempTableName + ".server_id LIMIT 1), " +
Col.ID + ", " +
Col.MIN_PING + ", " +
Col.MAX_PING + ", " +
Col.AVG_PING + ", " +
Col.DATE +
" FROM " + tempTableName
);
dropTable(tempTableName);
} catch (Exception e) {
throw new DBOpException(PingOptimizationPatch.class.getSimpleName() + " failed.", e);
}
}
private void tempOldTable() {
if (!hasTable(tempTableName)) {
renameTable(tableName, tempTableName);
}
}
}

View File

@ -32,40 +32,42 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class PingTable extends UserIDTable {
/**
* Table that represents plan_ping in the database.
* <p>
* Patches related to this table:
* {@link com.djrapitops.plan.system.database.databases.sql.patches.PingOptimizationPatch}
*
* @author Rsl1122
*/
public class PingTable extends UserUUIDTable {
public static final String TABLE_NAME = "plan_ping";
private final String insertStatement;
private final ServerTable serverTable;
public PingTable(SQLDB db) {
super(TABLE_NAME, db);
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " (" +
Col.USER_ID + ", " +
Col.SERVER_ID + ", " +
Col.UUID + ", " +
Col.SERVER_UUID + ", " +
Col.DATE + ", " +
Col.MIN_PING + ", " +
Col.MAX_PING + ", " +
Col.AVG_PING +
") VALUES (" +
usersTable.statementSelectID + ", " +
serverTable.statementSelectServerID + ", ?, ?, ?, ?)";
") VALUES (?, ?, ?, ?, ?, ?)";
}
@Override
public void createTable() throws DBInitException {
createTable(TableSqlParser.createTable(TABLE_NAME)
.primaryKeyIDColumn(supportsMySQLQueries, Col.ID)
.column(Col.USER_ID, Sql.INT).notNull()
.column(Col.SERVER_ID, Sql.INT).notNull()
.column(Col.UUID, Sql.varchar(36)).notNull()
.column(Col.SERVER_UUID, Sql.varchar(36)).notNull()
.column(Col.DATE, Sql.LONG).notNull()
.column(Col.MAX_PING, Sql.INT).notNull()
.column(Col.MIN_PING, Sql.INT).notNull()
.column(Col.AVG_PING, Sql.DOUBLE).notNull()
.primaryKey(supportsMySQLQueries, Col.ID)
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
.foreignKey(Col.SERVER_ID, ServerTable.TABLE_NAME, ServerTable.Col.SERVER_ID)
.toString());
}
@ -98,9 +100,8 @@ public class PingTable extends UserIDTable {
}
public List<Ping> getPing(UUID uuid) {
Map<Integer, UUID> serverUUIDs = serverTable.getServerUUIDsByID();
String sql = "SELECT * FROM " + tableName +
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID;
" WHERE " + Col.UUID + "=?";
return query(new QueryStatement<List<Ping>>(sql, 10000) {
@Override
@ -115,7 +116,7 @@ public class PingTable extends UserIDTable {
while (set.next()) {
pings.add(new Ping(
set.getLong(Col.DATE.get()),
serverUUIDs.get(set.getInt(Col.SERVER_ID.get())),
UUID.fromString(set.getString(Col.SERVER_UUID.get())),
set.getInt(Col.MIN_PING.get()),
set.getInt(Col.MAX_PING.get()),
set.getDouble(Col.AVG_PING.get())
@ -129,28 +130,22 @@ public class PingTable extends UserIDTable {
}
public Map<UUID, List<Ping>> getAllPings() {
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
String sql = "SELECT " +
Col.DATE + ", " +
Col.MAX_PING + ", " +
Col.MIN_PING + ", " +
Col.AVG_PING + ", " +
usersUUIDColumn + ", " +
serverUUIDColumn +
" FROM " + tableName +
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + UserInfoTable.Col.USER_ID +
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + UserInfoTable.Col.SERVER_ID;
Col.UUID + ", " +
Col.SERVER_UUID +
" FROM " + tableName;
return query(new QueryAllStatement<Map<UUID, List<Ping>>>(sql, 100000) {
@Override
public Map<UUID, List<Ping>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<Ping>> userPings = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get()));
long date = set.getLong(Col.DATE.get());
double avgPing = set.getDouble(Col.AVG_PING.get());
int minPing = set.getInt(Col.MIN_PING.get());
@ -198,8 +193,12 @@ public class PingTable extends UserIDTable {
public enum Col implements Column {
ID("id"),
@Deprecated
USER_ID(UserIDTable.Col.USER_ID.get()),
UUID(UserUUIDTable.Col.UUID.get()),
@Deprecated
SERVER_ID("server_id"),
SERVER_UUID("server_uuid"),
DATE("date"),
MAX_PING("max_ping"),
AVG_PING("avg_ping"),

View File

@ -205,6 +205,7 @@ public class ServerTable extends Table {
});
}
@Deprecated
public Map<Integer, UUID> getServerUUIDsByID() {
String sql = Select.from(tableName,
Col.SERVER_ID, Col.SERVER_UUID)

View File

@ -46,7 +46,7 @@ import java.util.stream.Collectors;
*
* @author Rsl1122
*/
public class SessionsTable extends UserIDTable {
public class SessionsTable extends UserUUIDTable {
public static final String TABLE_NAME = "plan_sessions";
@ -597,22 +597,10 @@ public class SessionsTable extends UserIDTable {
});
}
@Override
public void removeUser(UUID uuid) {
String sql = "DELETE FROM " + tableName + " WHERE (" + Col.UUID + "=?)";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
});
}
public enum Col implements Column {
@Deprecated
USER_ID(UserIDTable.Col.USER_ID.get()),
UUID("uuid"),
UUID(UserUUIDTable.Col.UUID.get()),
ID("id"),
@Deprecated
SERVER_ID("server_id"),

View File

@ -30,6 +30,7 @@ import java.util.UUID;
* @author Rsl1122
* @since 3.7.0
*/
@Deprecated
public abstract class UserIDTable extends Table {
public enum Col implements Column {

View File

@ -0,0 +1,68 @@
/*
* 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.system.database.databases.sql.tables;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
/**
* Represents a Table that uses UUIDs to get their data.
*
* @author Rsl1122
*/
public abstract class UserUUIDTable extends Table {
public UserUUIDTable(String name, SQLDB db) {
super(name, db);
}
public void removeUser(UUID uuid) {
String sql = "DELETE FROM " + tableName + " WHERE (" + Col.UUID + "=?)";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
});
}
public enum Col implements Column {
UUID("uuid");
private final String column;
Col(String column) {
this.column = column;
}
@Override
public String get() {
return toString();
}
@Override
public String toString() {
return column;
}
}
}

View File

@ -42,7 +42,7 @@ import java.util.*;
*
* @author Rsl1122
*/
public class UsersTable extends UserIDTable {
public class UsersTable extends UserUUIDTable {
public UsersTable(SQLDB db) {
super("plan_users", db);
@ -88,23 +88,6 @@ public class UsersTable extends UserIDTable {
});
}
/**
* Remove a user from Users Table.
*
* @param uuid the UUID of the user that should be removed.
*/
@Override
public void removeUser(UUID uuid) {
String sql = "DELETE FROM " + tableName + " WHERE (" + Col.UUID + "=?)";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
});
}
/**
* Get UUID of a player.
*
@ -397,6 +380,7 @@ public class UsersTable extends UserIDTable {
});
}
@Deprecated
public Map<Integer, UUID> getUUIDsByID() {
String sql = Select.from(tableName, Col.ID, Col.UUID).toString();

View File

@ -51,10 +51,9 @@ import java.util.stream.Collectors;
*
* @author Rsl1122
*/
public class WorldTimesTable extends UserIDTable {
public class WorldTimesTable extends UserUUIDTable {
public static final String TABLE_NAME = "plan_world_times";
private final ServerTable serverTable;
private final WorldTable worldTable;
private final SessionsTable sessionsTable;
private String insertStatement;
@ -63,7 +62,6 @@ public class WorldTimesTable extends UserIDTable {
super(TABLE_NAME, db);
worldTable = db.getWorldTable();
sessionsTable = db.getSessionsTable();
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " (" +
Col.UUID + ", " +
Col.WORLD_ID + ", " +
@ -370,40 +368,27 @@ public class WorldTimesTable extends UserIDTable {
});
}
@Override
public void removeUser(UUID uuid) {
String sql = "DELETE FROM " + tableName + " WHERE (" + Col.UUID + "=?)";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
});
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof WorldTimesTable)) return false;
if (!super.equals(o)) return false;
WorldTimesTable that = (WorldTimesTable) o;
return Objects.equals(serverTable, that.serverTable) &&
Objects.equals(worldTable, that.worldTable) &&
return Objects.equals(worldTable, that.worldTable) &&
Objects.equals(sessionsTable, that.sessionsTable) &&
Objects.equals(insertStatement, that.insertStatement);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), serverTable, worldTable, sessionsTable, insertStatement);
return Objects.hash(super.hashCode(), worldTable, sessionsTable, insertStatement);
}
public enum Col implements Column {
ID("id"),
@Deprecated
USER_ID(UserIDTable.Col.USER_ID.get()),
UUID("uuid"),
UUID(UserUUIDTable.Col.UUID.get()),
@Deprecated
SERVER_ID("server_id"),
SERVER_UUID("server_uuid"),