WorldsTable table structure optimization

- Replaced server_id with server_uuid
This commit is contained in:
Rsl1122 2018-12-13 11:42:14 +02:00
parent b4089cff28
commit a44335709d
5 changed files with 96 additions and 13 deletions

View File

@ -202,7 +202,8 @@ public abstract class SQLDB extends Database {
new NicknameLastSeenPatch(this),
new VersionTableRemovalPatch(this),
new DiskUsagePatch(this),
new WorldTimesOptimizationPatch(this)
new WorldTimesOptimizationPatch(this),
new WorldsOptimizationPatch(this)
};
try {

View File

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

View File

@ -40,9 +40,9 @@ public class WorldsServerIDPatch extends Patch {
@Override
public boolean hasBeenApplied() {
String tableName = WorldTable.TABLE_NAME;
String columnName = WorldTable.Col.SERVER_ID.get();
String columnName = "server_id";
// WorldOptimizationPatch makes this patch incompatible with newer patch versions.
// WorldsOptimizationPatch makes this patch incompatible with newer patch versions.
return hasColumn(tableName, "server_uuid")
|| hasColumn(tableName, columnName)
&& allValuesHaveServerID(tableName, columnName);
@ -82,7 +82,7 @@ public class WorldsServerIDPatch extends Patch {
}
updateWorldTimesTableWorldIDs();
db.executeUnsafe("DELETE FROM " + WorldTable.TABLE_NAME + " WHERE " + WorldTable.Col.SERVER_ID + "=0");
db.executeUnsafe("DELETE FROM " + WorldTable.TABLE_NAME + " WHERE server_id=0");
}
private Set<String> getWorldNamesOld(UUID serverUUID) {
@ -162,7 +162,7 @@ public class WorldsServerIDPatch extends Patch {
List<WorldObj> objects = new ArrayList<>();
while (set.next()) {
int worldID = set.getInt(WorldTable.Col.ID.get());
int serverID = set.getInt(WorldTable.Col.SERVER_ID.get());
int serverID = set.getInt("server_id");
String worldName = set.getString(WorldTable.Col.NAME.get());
objects.add(new WorldObj(worldID, serverID, worldName));
}

View File

@ -35,6 +35,11 @@ import java.util.*;
* Table class representing database table plan_worlds.
* <p>
* Used for storing id references to world names.
* <p>
* Patches related to this table:
* {@link com.djrapitops.plan.system.database.databases.sql.patches.Version10Patch}
* {@link com.djrapitops.plan.system.database.databases.sql.patches.WorldsServerIDPatch}
* {@link com.djrapitops.plan.system.database.databases.sql.patches.WorldsOptimizationPatch}
*
* @author Rsl1122
* @since 3.6.0 / Database version 7
@ -50,7 +55,7 @@ public class WorldTable extends Table {
serverTable = db.getServerTable();
statementSelectID = "(SELECT " + Col.ID + " FROM " + tableName +
" WHERE (" + Col.NAME + "=?)" +
" AND (" + Col.SERVER_ID + "=" + serverTable.statementSelectServerID + ")" +
" AND (" + Col.SERVER_UUID + "=?)" +
" LIMIT 1)";
}
@ -59,9 +64,8 @@ public class WorldTable extends Table {
createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(supportsMySQLQueries, Col.ID)
.column(Col.NAME, Sql.varchar(100)).notNull()
.column(Col.SERVER_ID, Sql.INT).notNull()
.column(Col.SERVER_UUID, Sql.varchar(36)).notNull()
.primaryKey(supportsMySQLQueries, Col.ID)
.foreignKey(Col.SERVER_ID, ServerTable.TABLE_NAME, ServerTable.Col.SERVER_ID)
.toString()
);
}
@ -93,7 +97,7 @@ public class WorldTable extends Table {
public List<String> getWorlds(UUID serverUUID) {
String sql = "SELECT * FROM " + tableName +
" WHERE " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
" WHERE " + Col.SERVER_UUID + "=?";
return query(new QueryStatement<List<String>>(sql) {
@ -137,8 +141,8 @@ public class WorldTable extends Table {
String sql = "INSERT INTO " + tableName + " ("
+ Col.NAME + ", "
+ Col.SERVER_ID
+ ") VALUES (?, " + serverTable.statementSelectServerID + ")";
+ Col.SERVER_UUID
+ ") VALUES (?, ?)";
executeBatch(new ExecStatement(sql) {
@Override
@ -154,7 +158,7 @@ public class WorldTable extends Table {
public Set<String> getWorldNames(UUID serverUUID) {
String sql = "SELECT DISTINCT " + Col.NAME + " FROM " + tableName +
" WHERE " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
" WHERE " + Col.SERVER_UUID + "=?";
return query(new QueryStatement<Set<String>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -174,6 +178,7 @@ public class WorldTable extends Table {
public enum Col implements Column {
ID("id"),
@Deprecated
SERVER_ID("server_id"),
SERVER_UUID("server_uuid"),
NAME("world_name");

View File

@ -41,7 +41,13 @@ import java.util.stream.Collectors;
* <p>
* Table Name: plan_world_times
* <p>
* For contained columns {@see Col}
* For contained columns {@link Col}
* <p>
* Patches related to this table:
* {@link com.djrapitops.plan.system.database.databases.sql.patches.Version10Patch}
* {@link com.djrapitops.plan.system.database.databases.sql.patches.WorldTimesSeverIDPatch}
* {@link com.djrapitops.plan.system.database.databases.sql.patches.WorldsServerIDPatch}
* {@link com.djrapitops.plan.system.database.databases.sql.patches.WorldTimesOptimizationPatch}
*
* @author Rsl1122
*/