From e7e680fd8e7c288ee1cb9a8139ad5a34725f5ab7 Mon Sep 17 00:00:00 2001 From: GJ Date: Mon, 14 Jan 2013 00:25:31 -0500 Subject: [PATCH] Updating the SQL for the purge command, adding config options for purging old users. --- .../nossr50/commands/mc/McpurgeCommand.java | 55 ++++++------------- .../java/com/gmail/nossr50/config/Config.java | 5 +- .../nossr50/runnables/UserPurgeTask.java | 8 ++- src/main/resources/config.yml | 6 +- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java index 211986c75..2abc2c0bf 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java @@ -24,7 +24,6 @@ public class McpurgeCommand implements CommandExecutor{ private Plugin plugin; private Database database = mcMMO.getPlayerDatabase(); private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); - private String databaseName = Config.getInstance().getMySQLDatabaseName(); public McpurgeCommand(Plugin plugin) { this.plugin = plugin; @@ -37,8 +36,11 @@ public class McpurgeCommand implements CommandExecutor{ } if (Config.getInstance().getUseMySQL()) { - purgePowerlessSQLFaster(); - purgeOldSQL(); + purgePowerlessSQL(); + + if (Config.getInstance().getOldUsersCutoff() != -1) { + purgeOldSQL(); + } } else { //TODO: Make this work for Flatfile data. @@ -48,28 +50,12 @@ public class McpurgeCommand implements CommandExecutor{ return true; } - private void purgePowerlessSQLFaster() { + private void purgePowerlessSQL() { plugin.getLogger().info("Purging powerless users..."); - String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing"; - HashMap> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " = 0 ORDER BY " + query + " DESC "); + HashMap> usernames = database.read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0"); + database.write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT u.id FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0) AS p)"); int purgedUsers = 0; - String userIdString = "("; - - for (int i = 1; i <= userslist.size(); i++) { - int userId = Integer.valueOf(userslist.get(i).get(1)); - - if (i == userslist.size()) { - userIdString = userIdString + userId + ")"; - } - else { - userIdString = userIdString + userId + ","; - } - } - - HashMap> usernames = database.read("SELECT user FROM " + tablePrefix + "users WHERE id IN " + userIdString); - database.write("DELETE FROM " + databaseName + "." + tablePrefix + "users WHERE " + tablePrefix + "users.id IN " + userIdString); - for (int i = 1; i <= usernames.size(); i++) { String playerName = usernames.get(i).get(0); @@ -87,25 +73,20 @@ public class McpurgeCommand implements CommandExecutor{ private void purgeOldSQL() { plugin.getLogger().info("Purging old users..."); long currentTime = System.currentTimeMillis(); - String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing"; - HashMap> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " > 0 ORDER BY " + query + " DESC "); + long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff(); + HashMap> usernames = database.read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")"); + database.write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT id FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")) AS p)"); int purgedUsers = 0; + for (int i = 1; i <= usernames.size(); i++) { + String playerName = usernames.get(i).get(0); - for (int i = 1; i <= userslist.size(); i++) { - int userId = Integer.valueOf(userslist.get(i).get(1)); - HashMap> username = database.read("SELECT user FROM " + tablePrefix + "users WHERE id = '" + userId + "'"); - String playerName = username.get(1).get(0); - - long lastLoginTime = database.getInt("SELECT lastlogin FROM " + tablePrefix + "users WHERE id = '" + userId + "'") * 1000L; - long loginDifference = currentTime - lastLoginTime; - - if (loginDifference > 2630000000L) { - database.write("DELETE FROM " + databaseName + "." + tablePrefix + "users WHERE " + tablePrefix + "users.id IN " + userId); - profileCleanup(playerName); - - purgedUsers++; + if (playerName == null) { + continue; } + + profileCleanup(playerName); + purgedUsers++; } plugin.getLogger().info("Purged " + purgedUsers + " users from the database."); diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index 277fb7f2d..ce1405ad5 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -35,7 +35,10 @@ public class Config extends ConfigLoader { public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); } public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); } public boolean getEventCallbackEnabled() { return config.getBoolean("General.Event_Callback", true); } - public int getPurgeInterval() { return config.getInt("General.Purge_Interval",-1); } + + /* Database Purging */ + public int getPurgeInterval() { return config.getInt("Database_Purging.Purge_Interval", -1); } + public int getOldUsersCutoff() { return config.getInt("Database_Purging.Old_User_Cutoff", 6); } /* mySQL */ public boolean getUseMySQL() { return config.getBoolean("MySQL.Enabled", false); } diff --git a/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java b/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java index fa10751df..9ae8f5b8d 100644 --- a/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java @@ -19,7 +19,6 @@ public class UserPurgeTask implements Runnable { private Plugin plugin; private Database database = mcMMO.getPlayerDatabase(); private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); - private String databaseName = Config.getInstance().getMySQLDatabaseName(); public UserPurgeTask(Plugin plugin) { this.plugin = plugin; @@ -29,7 +28,10 @@ public class UserPurgeTask implements Runnable { public void run() { if (Config.getInstance().getUseMySQL()) { purgePowerlessSQL(); - purgeOldSQL(); + + if (Config.getInstance().getOldUsersCutoff() != -1) { + purgeOldSQL(); + } } else { //TODO: Make this work for Flatfile data. @@ -59,7 +61,7 @@ public class UserPurgeTask implements Runnable { private void purgeOldSQL() { plugin.getLogger().info("Purging old users..."); long currentTime = System.currentTimeMillis(); - long purgeTime = 2630000000L; + long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff(); HashMap> usernames = database.read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")"); database.write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT id FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")) AS p)"); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ddd219cf2..248fbcaaf 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -17,11 +17,15 @@ General: #Allow mcMMO to inform other plugins of damage being dealt Event_Callback: true Power_Level_Cap: 0 + +Database_Purging: #Amount of time (in hours) to wait between database purging #To only run at server start, set to 0 #To never run, set to -1 Purge_Interval: -1 - + #Any user who hasn't connected in this many months will be purged from the database + #To never purge old users, set to -1 + Old_User_Cutoff: 6 # # Settings for using a mySQL database ###