Updating the SQL for the purge command, adding config options for

purging old users.
This commit is contained in:
GJ 2013-01-14 00:25:31 -05:00
parent 7b334601c6
commit e7e680fd8e
4 changed files with 32 additions and 42 deletions

View File

@ -24,7 +24,6 @@ public class McpurgeCommand implements CommandExecutor{
private Plugin plugin; private Plugin plugin;
private Database database = mcMMO.getPlayerDatabase(); private Database database = mcMMO.getPlayerDatabase();
private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
private String databaseName = Config.getInstance().getMySQLDatabaseName();
public McpurgeCommand(Plugin plugin) { public McpurgeCommand(Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -37,9 +36,12 @@ public class McpurgeCommand implements CommandExecutor{
} }
if (Config.getInstance().getUseMySQL()) { if (Config.getInstance().getUseMySQL()) {
purgePowerlessSQLFaster(); purgePowerlessSQL();
if (Config.getInstance().getOldUsersCutoff() != -1) {
purgeOldSQL(); purgeOldSQL();
} }
}
else { else {
//TODO: Make this work for Flatfile data. //TODO: Make this work for Flatfile data.
} }
@ -48,28 +50,12 @@ public class McpurgeCommand implements CommandExecutor{
return true; return true;
} }
private void purgePowerlessSQLFaster() { private void purgePowerlessSQL() {
plugin.getLogger().info("Purging powerless users..."); plugin.getLogger().info("Purging powerless users...");
String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing"; HashMap<Integer, ArrayList<String>> 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");
HashMap<Integer, ArrayList<String>> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " = 0 ORDER BY " + query + " DESC "); 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; 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<Integer, ArrayList<String>> 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++) { for (int i = 1; i <= usernames.size(); i++) {
String playerName = usernames.get(i).get(0); String playerName = usernames.get(i).get(0);
@ -87,25 +73,20 @@ public class McpurgeCommand implements CommandExecutor{
private void purgeOldSQL() { private void purgeOldSQL() {
plugin.getLogger().info("Purging old users..."); plugin.getLogger().info("Purging old users...");
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing"; long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff();
HashMap<Integer, ArrayList<String>> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " > 0 ORDER BY " + query + " DESC "); HashMap<Integer, ArrayList<String>> 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; 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++) { if (playerName == null) {
int userId = Integer.valueOf(userslist.get(i).get(1)); continue;
HashMap<Integer, ArrayList<String>> 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++;
} }
profileCleanup(playerName);
purgedUsers++;
} }
plugin.getLogger().info("Purged " + purgedUsers + " users from the database."); plugin.getLogger().info("Purged " + purgedUsers + " users from the database.");

View File

@ -35,7 +35,10 @@ public class Config extends ConfigLoader {
public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); } public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); }
public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); } public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); }
public boolean getEventCallbackEnabled() { return config.getBoolean("General.Event_Callback", 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 */ /* mySQL */
public boolean getUseMySQL() { return config.getBoolean("MySQL.Enabled", false); } public boolean getUseMySQL() { return config.getBoolean("MySQL.Enabled", false); }

View File

@ -19,7 +19,6 @@ public class UserPurgeTask implements Runnable {
private Plugin plugin; private Plugin plugin;
private Database database = mcMMO.getPlayerDatabase(); private Database database = mcMMO.getPlayerDatabase();
private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
private String databaseName = Config.getInstance().getMySQLDatabaseName();
public UserPurgeTask(Plugin plugin) { public UserPurgeTask(Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -29,8 +28,11 @@ public class UserPurgeTask implements Runnable {
public void run() { public void run() {
if (Config.getInstance().getUseMySQL()) { if (Config.getInstance().getUseMySQL()) {
purgePowerlessSQL(); purgePowerlessSQL();
if (Config.getInstance().getOldUsersCutoff() != -1) {
purgeOldSQL(); purgeOldSQL();
} }
}
else { else {
//TODO: Make this work for Flatfile data. //TODO: Make this work for Flatfile data.
} }
@ -59,7 +61,7 @@ public class UserPurgeTask implements Runnable {
private void purgeOldSQL() { private void purgeOldSQL() {
plugin.getLogger().info("Purging old users..."); plugin.getLogger().info("Purging old users...");
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
long purgeTime = 2630000000L; long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff();
HashMap<Integer, ArrayList<String>> usernames = database.read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")"); HashMap<Integer, ArrayList<String>> 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)"); 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)");

View File

@ -17,11 +17,15 @@ General:
#Allow mcMMO to inform other plugins of damage being dealt #Allow mcMMO to inform other plugins of damage being dealt
Event_Callback: true Event_Callback: true
Power_Level_Cap: 0 Power_Level_Cap: 0
Database_Purging:
#Amount of time (in hours) to wait between database purging #Amount of time (in hours) to wait between database purging
#To only run at server start, set to 0 #To only run at server start, set to 0
#To never run, set to -1 #To never run, set to -1
Purge_Interval: -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 # Settings for using a mySQL database
### ###