Store skill totals rather than calculating them on the fly for faster leaderboard queries.

This commit is contained in:
t00thpick1 2018-11-14 20:31:57 -05:00
parent 203d3749ee
commit 495c4a33e5
2 changed files with 51 additions and 4 deletions

View File

@ -20,7 +20,7 @@ import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
public final class SQLDatabaseManager implements DatabaseManager {
private static final String ALL_QUERY_VERSION = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy";
private static final String ALL_QUERY_VERSION = "total";
private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
private final Map<UUID, Integer> cachedUserIDs = new HashMap<UUID, Integer>();
@ -225,7 +225,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ " taming = ?, mining = ?, repair = ?, woodcutting = ?"
+ ", unarmed = ?, herbalism = ?, excavation = ?"
+ ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
+ ", fishing = ?, alchemy = ? WHERE user_id = ?");
+ ", fishing = ?, alchemy = ?, total = ? WHERE user_id = ?");
statement.setInt(1, profile.getSkillLevel(SkillType.TAMING));
statement.setInt(2, profile.getSkillLevel(SkillType.MINING));
statement.setInt(3, profile.getSkillLevel(SkillType.REPAIR));
@ -239,7 +239,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setInt(11, profile.getSkillLevel(SkillType.ACROBATICS));
statement.setInt(12, profile.getSkillLevel(SkillType.FISHING));
statement.setInt(13, profile.getSkillLevel(SkillType.ALCHEMY));
statement.setInt(14, id);
int total = 0;
for (SkillType skillType : SkillType.NON_CHILD_SKILLS)
total += profile.getSkillLevel(skillType);
statement.setInt(14, total);
statement.setInt(15, id);
success &= (statement.executeUpdate() != 0);
statement.close();
if (!success) {
@ -838,6 +842,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`fishing` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`alchemy` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`total` int(10) unsigned NOT NULL DEFAULT '0',"
+ "PRIMARY KEY (`user_id`)) "
+ "DEFAULT CHARSET=latin1;");
tryClose(createStatement);
@ -980,6 +985,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
checkNameUniqueness(statement);
return;
case ADD_SKILL_TOTAL:
checkUpgradeSkillTotal(connection);
break;
default:
break;
@ -1317,6 +1326,43 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
}
private void checkUpgradeSkillTotal(final Connection connection) throws SQLException {
ResultSet resultSet = null;
Statement statement = null;
try {
connection.setAutoCommit(false);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM `" + tablePrefix + "skills` LIMIT 1");
ResultSetMetaData rsmeta = resultSet.getMetaData();
boolean column_exists = false;
for (int i = 1; i <= rsmeta.getColumnCount(); i++) {
if (rsmeta.getColumnName(i).equalsIgnoreCase("total")) {
column_exists = true;
break;
}
}
if (!column_exists) {
mcMMO.p.getLogger().info("Adding skill total column to skills table...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD COLUMN `total` int NOT NULL DEFAULT '0'");
statement.executeUpdate("UPDATE `" + tablePrefix + "skills` SET `total` = (taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy)");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_total` (`total`) USING BTREE");
connection.commit();
}
}
catch (SQLException ex) {
printErrors(ex);
}
finally {
connection.setAutoCommit(true);
tryClose(resultSet);
tryClose(statement);
}
}
private void checkUpgradeDropSpout(final Statement statement) {
ResultSet resultSet = null;

View File

@ -11,5 +11,6 @@ public enum UpgradeType {
ADD_UUIDS,
ADD_UUIDS_PARTY,
ADD_SCOREBOARD_TIPS,
DROP_NAME_UNIQUENESS;
DROP_NAME_UNIQUENESS,
ADD_SKILL_TOTAL,
}