From 0e39656c8870b1767c8e66ee2347d5974c0f74d0 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Wed, 3 Apr 2019 18:42:58 -0700 Subject: [PATCH] Code cleanup and new settings for Database config --- .../config/hocon/database/ConfigDatabase.java | 15 ++++++++ .../database/ConfigDatabaseFlatFile.java | 20 +++++++++++ .../ConfigSectionDatabaseGeneral.java | 21 +++++++++++ .../hocon/database/ConfigSectionMySQL.java | 6 ++-- .../nossr50/database/DatabaseManager.java | 36 +++++++++---------- .../database/FlatfileDatabaseManager.java | 6 ++-- .../nossr50/database/SQLDatabaseManager.java | 7 +--- .../datatypes/database/PoolIdentifier.java | 4 +++ 8 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabaseFlatFile.java create mode 100644 src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionDatabaseGeneral.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/database/PoolIdentifier.java diff --git a/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabase.java b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabase.java index 10c1678e6..bfc0ed189 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabase.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabase.java @@ -14,6 +14,10 @@ public class ConfigDatabase { comment = "Settings to automatically purge old users to keep database sizes small.") private ConfigSectionCleaning configSectionCleaning = new ConfigSectionCleaning(); + @Setting(value = "FlatFile", comment = "FlatFile is a plain text database used by mcMMO." + + "\nIt is recommended that you use MySQL/MariaDB instead because FlatFile is notoriously slow.") + private ConfigDatabaseFlatFile configDatabaseFlatFile = new ConfigDatabaseFlatFile(); + @Setting(value = "MySQL", comment = "Settings for using MySQL or MariaDB database" + "\nI recommend using MariaDB, its completely compatible with MySQL and runs a lot better" + "\nI also recommend having the MySQL/MariaDB server in the same datacenter or LAN as your Minecraft server" + @@ -21,10 +25,21 @@ public class ConfigDatabase { " but ideally you want low latency to your SQL server anyways!") private ConfigSectionMySQL configSectionMySQL = new ConfigSectionMySQL(); + @Setting(value = "General", comment = "Settings that apply to both databases, both MySQL/MariaDB and FlatFile.") + private ConfigSectionDatabaseGeneral configSectionDatabaseGeneral = new ConfigSectionDatabaseGeneral(); + /* * GETTER BOILERPLATE */ + public ConfigDatabaseFlatFile getConfigDatabaseFlatFile() { + return configDatabaseFlatFile; + } + + public ConfigSectionDatabaseGeneral getConfigSectionDatabaseGeneral() { + return configSectionDatabaseGeneral; + } + public ConfigSectionMySQL getConfigSectionMySQL() { return configSectionMySQL; } diff --git a/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabaseFlatFile.java b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabaseFlatFile.java new file mode 100644 index 000000000..bc915391b --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigDatabaseFlatFile.java @@ -0,0 +1,20 @@ +package com.gmail.nossr50.config.hocon.database; + +import ninja.leaping.configurate.objectmapping.Setting; +import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; + +@ConfigSerializable +public class ConfigDatabaseFlatFile { + + public static final int LEADERBOARD_SCOREBOARD_UPDATE_INTERVAL_MINUTES_DEFAULT = 10; + + @Setting(value = "Scoreboard-Leaderboard-Update-Interval", comment = "How often the scoreboard leaderboards will update." + + "\nThis is an expensive operation, it is highly recommended to avoid doing this often." + + "\nDefault value: "+LEADERBOARD_SCOREBOARD_UPDATE_INTERVAL_MINUTES_DEFAULT) + private int leaderboardUpdateIntervalMinutes = LEADERBOARD_SCOREBOARD_UPDATE_INTERVAL_MINUTES_DEFAULT; + + public int getLeaderboardUpdateIntervalMinutes() { + return leaderboardUpdateIntervalMinutes; + } + +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionDatabaseGeneral.java b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionDatabaseGeneral.java new file mode 100644 index 000000000..658ca88d1 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionDatabaseGeneral.java @@ -0,0 +1,21 @@ +package com.gmail.nossr50.config.hocon.database; + +import ninja.leaping.configurate.objectmapping.Setting; +import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; + +@ConfigSerializable +public class ConfigSectionDatabaseGeneral { + + public static final int SAVE_INTERVAL_MINUTES_DEFAULT = 10; + + @Setting(value = "Save-Interval-Minutes", comment = "How often the database will save." + + "\nSaving the database is an expensive operation although it is done in an ASYNC thread." + + "\nI wouldn't recommend setting this value lower than 10 minutes" + + "\nKeep in mind if you properly shut down your server with a stop command mcMMO saves before your server shuts down." + + "\nDefault value: "+SAVE_INTERVAL_MINUTES_DEFAULT) + private int saveIntervalMinutes = SAVE_INTERVAL_MINUTES_DEFAULT; + + public int getSaveIntervalMinutes() { + return saveIntervalMinutes; + } +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionMySQL.java b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionMySQL.java index d74ef5ede..7179548c1 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionMySQL.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/database/ConfigSectionMySQL.java @@ -1,6 +1,6 @@ package com.gmail.nossr50.config.hocon.database; -import com.gmail.nossr50.database.SQLDatabaseManager; +import com.gmail.nossr50.datatypes.database.PoolIdentifier; import ninja.leaping.configurate.objectmapping.Setting; import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; @@ -53,7 +53,7 @@ public class ConfigSectionMySQL { * HELPER METHODS */ - public int getMaxPoolSize(SQLDatabaseManager.PoolIdentifier poolIdentifier) + public int getMaxPoolSize(PoolIdentifier poolIdentifier) { switch (poolIdentifier) { @@ -68,7 +68,7 @@ public class ConfigSectionMySQL { } } - public int getMaxConnections(SQLDatabaseManager.PoolIdentifier poolIdentifier) + public int getMaxConnections(PoolIdentifier poolIdentifier) { switch (poolIdentifier) { diff --git a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java index 5c5653a63..9a3a0ad50 100644 --- a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java @@ -12,19 +12,19 @@ import java.util.UUID; public interface DatabaseManager { // One month in milliseconds - public final long PURGE_TIME = 2630000000L * mcMMO.getDatabaseCleaningSettings().getOldUserCutoffMonths(); + long PURGE_TIME = 2630000000L * mcMMO.getDatabaseCleaningSettings().getOldUserCutoffMonths(); // During convertUsers, how often to output a status - public final int progressInterval = 200; + int progressInterval = 200; /** * Purge users with 0 power level from the database. */ - public void purgePowerlessUsers(); + void purgePowerlessUsers(); /** * Purge users who haven't logged on in over a certain time frame from the database. */ - public void purgeOldUsers(); + void purgeOldUsers(); /** * Remove a user from the database. @@ -32,7 +32,7 @@ public interface DatabaseManager { * @param playerName The name of the user to remove * @return true if the user was successfully removed, false otherwise */ - public boolean removeUser(String playerName); + boolean removeUser(String playerName); /** * Save a user to the database. @@ -40,7 +40,7 @@ public interface DatabaseManager { * @param profile The profile of the player to save * @return true if successful, false on failure */ - public boolean saveUser(PlayerProfile profile); + boolean saveUser(PlayerProfile profile); /** * Retrieve leaderboard info. @@ -50,7 +50,7 @@ public interface DatabaseManager { * @param statsPerPage The number of stats per page * @return the requested leaderboard information */ - public List readLeaderboard(PrimarySkillType skill, int pageNumber, int statsPerPage); + List readLeaderboard(PrimarySkillType skill, int pageNumber, int statsPerPage); /** * Retrieve rank info into a HashMap from PrimarySkillType to the rank. @@ -61,7 +61,7 @@ public interface DatabaseManager { * @param playerName The name of the user to retrieve the rankings for * @return the requested rank information */ - public Map readRank(String playerName); + Map readRank(String playerName); /** * Add a new user to the database. @@ -69,7 +69,7 @@ public interface DatabaseManager { * @param playerName The name of the player to be added to the database * @param uuid The uuid of the player to be added to the database */ - public void newUser(String playerName, UUID uuid); + void newUser(String playerName, UUID uuid); /** * Load a player from the database. @@ -83,7 +83,7 @@ public interface DatabaseManager { * and createNew is false */ @Deprecated - public PlayerProfile loadPlayerProfile(String playerName, boolean createNew); + PlayerProfile loadPlayerProfile(String playerName, boolean createNew); /** * Load a player from the database. @@ -91,7 +91,7 @@ public interface DatabaseManager { * @param uuid The uuid of the player to load from the database * @return The player's data, or an unloaded PlayerProfile if not found */ - public PlayerProfile loadPlayerProfile(UUID uuid); + PlayerProfile loadPlayerProfile(UUID uuid); /** * Load a player from the database. Attempt to use uuid, fall back on playername @@ -103,14 +103,14 @@ public interface DatabaseManager { * @return The player's data, or an unloaded PlayerProfile if not found * and createNew is false */ - public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean createNew); + PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean createNew); /** * Get all users currently stored in the database. * * @return list of playernames */ - public List getStoredUsers(); + List getStoredUsers(); /** * Convert all users from this database to the provided database using @@ -118,21 +118,21 @@ public interface DatabaseManager { * * @param destination The DatabaseManager to save to */ - public void convertUsers(DatabaseManager destination); + void convertUsers(DatabaseManager destination); - public boolean saveUserUUID(String userName, UUID uuid); + boolean saveUserUUID(String userName, UUID uuid); - public boolean saveUserUUIDs(Map fetchedUUIDs); + boolean saveUserUUIDs(Map fetchedUUIDs); /** * Retrieve the type of database in use. Custom databases should return CUSTOM. * * @return The type of database */ - public DatabaseType getDatabaseType(); + DatabaseType getDatabaseType(); /** * Called when the plugin disables */ - public void onDisable(); + void onDisable(); } diff --git a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java index 21e997ee7..57b0651db 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -19,12 +19,12 @@ public final class FlatfileDatabaseManager implements DatabaseManager { private final HashMap> playerStatHash = new HashMap>(); private final List powerLevels = new ArrayList(); private long lastUpdate = 0; - - private final long UPDATE_WAIT_TIME = 600000L; // 10 minutes + private long updateWaitTime; private final File usersFile; private static final Object fileWritingLock = new Object(); protected FlatfileDatabaseManager() { + updateWaitTime = mcMMO.getConfigManager().getConfigDatabase().getConfigDatabaseFlatFile().getLeaderboardUpdateIntervalMinutes() * (1000 * 60); usersFile = new File(mcMMO.getUsersFilePath()); checkStructure(); updateLeaderboards(); @@ -727,7 +727,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { */ private void updateLeaderboards() { // Only update FFS leaderboards every 10 minutes.. this puts a lot of strain on the server (depending on the size of the database) and should not be done frequently - if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) { + if (System.currentTimeMillis() < lastUpdate + updateWaitTime) { return; } diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index c56230967..297e9d0fc 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -3,6 +3,7 @@ package com.gmail.nossr50.database; import com.gmail.nossr50.datatypes.MobHealthbarType; import com.gmail.nossr50.datatypes.database.DatabaseType; import com.gmail.nossr50.datatypes.database.PlayerStat; +import com.gmail.nossr50.datatypes.database.PoolIdentifier; import com.gmail.nossr50.datatypes.database.UpgradeType; import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.UniqueDataType; @@ -1523,12 +1524,6 @@ public final class SQLDatabaseManager implements DatabaseManager { savePool.close(); } - public enum PoolIdentifier { - MISC, - LOAD, - SAVE - } - public void resetMobHealthSettings() { PreparedStatement statement = null; Connection connection = null; diff --git a/src/main/java/com/gmail/nossr50/datatypes/database/PoolIdentifier.java b/src/main/java/com/gmail/nossr50/datatypes/database/PoolIdentifier.java new file mode 100644 index 000000000..9d7b27377 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/datatypes/database/PoolIdentifier.java @@ -0,0 +1,4 @@ +package com.gmail.nossr50.datatypes.database; + +public enum PoolIdentifier { +}