Code cleanup and new settings for Database config

This commit is contained in:
nossr50 2019-04-03 18:42:58 -07:00
parent 8991c2bf2c
commit 0e39656c88
8 changed files with 85 additions and 30 deletions

View File

@ -14,6 +14,10 @@ public class ConfigDatabase {
comment = "Settings to automatically purge old users to keep database sizes small.") comment = "Settings to automatically purge old users to keep database sizes small.")
private ConfigSectionCleaning configSectionCleaning = new ConfigSectionCleaning(); 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" + @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 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" + "\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!") " but ideally you want low latency to your SQL server anyways!")
private ConfigSectionMySQL configSectionMySQL = new ConfigSectionMySQL(); 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 * GETTER BOILERPLATE
*/ */
public ConfigDatabaseFlatFile getConfigDatabaseFlatFile() {
return configDatabaseFlatFile;
}
public ConfigSectionDatabaseGeneral getConfigSectionDatabaseGeneral() {
return configSectionDatabaseGeneral;
}
public ConfigSectionMySQL getConfigSectionMySQL() { public ConfigSectionMySQL getConfigSectionMySQL() {
return configSectionMySQL; return configSectionMySQL;
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.hocon.database; 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.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
@ -53,7 +53,7 @@ public class ConfigSectionMySQL {
* HELPER METHODS * HELPER METHODS
*/ */
public int getMaxPoolSize(SQLDatabaseManager.PoolIdentifier poolIdentifier) public int getMaxPoolSize(PoolIdentifier poolIdentifier)
{ {
switch (poolIdentifier) switch (poolIdentifier)
{ {
@ -68,7 +68,7 @@ public class ConfigSectionMySQL {
} }
} }
public int getMaxConnections(SQLDatabaseManager.PoolIdentifier poolIdentifier) public int getMaxConnections(PoolIdentifier poolIdentifier)
{ {
switch (poolIdentifier) switch (poolIdentifier)
{ {

View File

@ -12,19 +12,19 @@ import java.util.UUID;
public interface DatabaseManager { public interface DatabaseManager {
// One month in milliseconds // 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 // 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. * 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. * 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. * Remove a user from the database.
@ -32,7 +32,7 @@ public interface DatabaseManager {
* @param playerName The name of the user to remove * @param playerName The name of the user to remove
* @return true if the user was successfully removed, false otherwise * @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. * Save a user to the database.
@ -40,7 +40,7 @@ public interface DatabaseManager {
* @param profile The profile of the player to save * @param profile The profile of the player to save
* @return true if successful, false on failure * @return true if successful, false on failure
*/ */
public boolean saveUser(PlayerProfile profile); boolean saveUser(PlayerProfile profile);
/** /**
* Retrieve leaderboard info. * Retrieve leaderboard info.
@ -50,7 +50,7 @@ public interface DatabaseManager {
* @param statsPerPage The number of stats per page * @param statsPerPage The number of stats per page
* @return the requested leaderboard information * @return the requested leaderboard information
*/ */
public List<PlayerStat> readLeaderboard(PrimarySkillType skill, int pageNumber, int statsPerPage); List<PlayerStat> readLeaderboard(PrimarySkillType skill, int pageNumber, int statsPerPage);
/** /**
* Retrieve rank info into a HashMap from PrimarySkillType to the rank. * 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 * @param playerName The name of the user to retrieve the rankings for
* @return the requested rank information * @return the requested rank information
*/ */
public Map<PrimarySkillType, Integer> readRank(String playerName); Map<PrimarySkillType, Integer> readRank(String playerName);
/** /**
* Add a new user to the database. * 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 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 * @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. * Load a player from the database.
@ -83,7 +83,7 @@ public interface DatabaseManager {
* and createNew is false * and createNew is false
*/ */
@Deprecated @Deprecated
public PlayerProfile loadPlayerProfile(String playerName, boolean createNew); PlayerProfile loadPlayerProfile(String playerName, boolean createNew);
/** /**
* Load a player from the database. * 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 * @param uuid The uuid of the player to load from the database
* @return The player's data, or an unloaded PlayerProfile if not found * @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 * 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 * @return The player's data, or an unloaded PlayerProfile if not found
* and createNew is false * 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. * Get all users currently stored in the database.
* *
* @return list of playernames * @return list of playernames
*/ */
public List<String> getStoredUsers(); List<String> getStoredUsers();
/** /**
* Convert all users from this database to the provided database using * 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 * @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<String, UUID> fetchedUUIDs); boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs);
/** /**
* Retrieve the type of database in use. Custom databases should return CUSTOM. * Retrieve the type of database in use. Custom databases should return CUSTOM.
* *
* @return The type of database * @return The type of database
*/ */
public DatabaseType getDatabaseType(); DatabaseType getDatabaseType();
/** /**
* Called when the plugin disables * Called when the plugin disables
*/ */
public void onDisable(); void onDisable();
} }

View File

@ -19,12 +19,12 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
private final HashMap<PrimarySkillType, List<PlayerStat>> playerStatHash = new HashMap<PrimarySkillType, List<PlayerStat>>(); private final HashMap<PrimarySkillType, List<PlayerStat>> playerStatHash = new HashMap<PrimarySkillType, List<PlayerStat>>();
private final List<PlayerStat> powerLevels = new ArrayList<PlayerStat>(); private final List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
private long lastUpdate = 0; private long lastUpdate = 0;
private long updateWaitTime;
private final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
private final File usersFile; private final File usersFile;
private static final Object fileWritingLock = new Object(); private static final Object fileWritingLock = new Object();
protected FlatfileDatabaseManager() { protected FlatfileDatabaseManager() {
updateWaitTime = mcMMO.getConfigManager().getConfigDatabase().getConfigDatabaseFlatFile().getLeaderboardUpdateIntervalMinutes() * (1000 * 60);
usersFile = new File(mcMMO.getUsersFilePath()); usersFile = new File(mcMMO.getUsersFilePath());
checkStructure(); checkStructure();
updateLeaderboards(); updateLeaderboards();
@ -727,7 +727,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
*/ */
private void updateLeaderboards() { 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 // 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; return;
} }

View File

@ -3,6 +3,7 @@ package com.gmail.nossr50.database;
import com.gmail.nossr50.datatypes.MobHealthbarType; import com.gmail.nossr50.datatypes.MobHealthbarType;
import com.gmail.nossr50.datatypes.database.DatabaseType; import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.database.PlayerStat; 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.database.UpgradeType;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.player.UniqueDataType; import com.gmail.nossr50.datatypes.player.UniqueDataType;
@ -1523,12 +1524,6 @@ public final class SQLDatabaseManager implements DatabaseManager {
savePool.close(); savePool.close();
} }
public enum PoolIdentifier {
MISC,
LOAD,
SAVE
}
public void resetMobHealthSettings() { public void resetMobHealthSettings() {
PreparedStatement statement = null; PreparedStatement statement = null;
Connection connection = null; Connection connection = null;

View File

@ -0,0 +1,4 @@
package com.gmail.nossr50.datatypes.database;
public enum PoolIdentifier {
}