diff --git a/src/de/diddiz/LogBlock/ClearLog.java b/src/de/diddiz/LogBlock/ClearLog.java index c28da3b..cdd215b 100644 --- a/src/de/diddiz/LogBlock/ClearLog.java +++ b/src/de/diddiz/LogBlock/ClearLog.java @@ -1,16 +1,22 @@ package de.diddiz.LogBlock; +import java.io.File; import java.sql.Connection; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.text.SimpleDateFormat; import java.util.logging.Level; public class ClearLog implements Runnable { private Connection conn; + private File dumbFolder; - public ClearLog(Connection conn) { + public ClearLog(Connection conn, File dataFolder) { this.conn = conn; + dumbFolder = new File(dataFolder, "dumb"); + dumbFolder.mkdirs(); } @Override @@ -20,16 +26,36 @@ public class ClearLog implements Runnable Statement state = null; try { state = conn.createStatement(); + String time = new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(System.currentTimeMillis() - LogBlock.config.keepLogDays*86400000); + ResultSet rs; for (String table : LogBlock.config.tables.values()) { - int deleted = state.executeUpdate("DELETE FROM `" + table + "` WHERE date < date_sub(now(), INTERVAL " + LogBlock.config.keepLogDays + " DAY)"); - if (deleted > 0) + rs = state.executeQuery("SELECT count(*) FROM `" + table + "` WHERE date < now()"); + rs.next(); + int deleted = rs.getInt(1); + if (deleted > 0) { + if (LogBlock.config.dumpDroppedLog) + state.execute("SELECT * FROM `" + table + "` WHERE date < '" + time + "' INTO OUTFILE '" + dumbFolder.getAbsolutePath().replace("\\", "\\\\") + "\\\\" + table + "-" + time + ".csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'"); + state.execute("DELETE FROM `" + table + "` WHERE date < '" + time + "'"); LogBlock.log.info("[LogBlock] Cleared out table " + table + ". Deleted " + deleted + " entries."); - deleted = state.executeUpdate("DELETE `" + table + "-sign` FROM `" + table + "-sign` LEFT JOIN `" + table + "` ON (`" + table + "-sign`.`id` = `" + table + "`.`id`) WHERE `" + table + "`.`id` IS NULL;"); - if (deleted > 0) + } + rs = state.executeQuery("SELECT COUNT(*) FROM `" + table + "-sign` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL"); + rs.next(); + deleted = rs.getInt(1); + if (deleted > 0) { + if (LogBlock.config.dumpDroppedLog) + state.execute("SELECT * FROM `" + table + "-sign` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL INTO OUTFILE '" + dumbFolder.getAbsolutePath().replace("\\", "\\\\") + "\\\\" + table + "-sign-" + time + ".csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'"); + state.execute("DELETE `" + table + "-sign` FROM `" + table + "-sign` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL;"); LogBlock.log.info("[LogBlock] Cleared out table " + table + "-sign. Deleted " + deleted + " entries."); - deleted = state.executeUpdate("DELETE `" + table + "-chest` FROM `" + table + "-chest` LEFT JOIN `" + table + "` ON (`" + table + "-chest`.`id` = `" + table + "`.`id`) WHERE `" + table + "`.`id` IS NULL;"); - if (deleted > 0) + } + rs = state.executeQuery("SELECT COUNT(*) FROM `" + table + "-chest` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL"); + rs.next(); + deleted = rs.getInt(1); + if (deleted > 0) { + if (LogBlock.config.dumpDroppedLog) + state.execute("SELECT * FROM `" + table + "-chest` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL INTO OUTFILE '" + dumbFolder.getAbsolutePath().replace("\\", "\\\\") + "\\\\" + table + "-chest-" + time + ".csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'"); + state.execute("DELETE `" + table + "-chest` FROM `" + table + "-chest` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL;"); LogBlock.log.info("[LogBlock] Cleared out table " + table + "-chest. Deleted " + deleted + " entries."); + } } } catch (SQLException ex) { LogBlock.log.log(Level.SEVERE, "[LogBlock] SQL exception", ex); diff --git a/src/de/diddiz/LogBlock/Config.java b/src/de/diddiz/LogBlock/Config.java index 33dd4f4..8e2684e 100644 --- a/src/de/diddiz/LogBlock/Config.java +++ b/src/de/diddiz/LogBlock/Config.java @@ -16,6 +16,7 @@ public class Config { String dbUsername; String dbPassword; int keepLogDays; + boolean dumpDroppedLog; int delay; int defaultDist; int defaultTime; @@ -58,6 +59,8 @@ public class Config { config.setProperty("password", "pass"); if (!keys.contains("keepLogDays")) config.setProperty("keepLogDays", -1); + if (!keys.contains("dumpDroppedLog")) + config.setProperty("dumpDroppedLog", true); if (!keys.contains("delay")) config.setProperty("delay", 6); if (!keys.contains("defaultDist")) @@ -99,6 +102,7 @@ public class Config { dbUsername = config.getString("username"); dbPassword = config.getString("password"); keepLogDays = config.getInt("keepLogDays", -1); + dumpDroppedLog = config.getBoolean("dumpDroppedLog", true); delay = config.getInt("delay", 6); defaultDist = config.getInt("defaultDist", 20); defaultTime = LogBlock.parseTimeSpec(config.getString("defaultTime")); diff --git a/src/de/diddiz/LogBlock/LogBlock.java b/src/de/diddiz/LogBlock/LogBlock.java index b48444e..e70bde8 100644 --- a/src/de/diddiz/LogBlock/LogBlock.java +++ b/src/de/diddiz/LogBlock/LogBlock.java @@ -86,7 +86,7 @@ public class LogBlock extends JavaPlugin return; } if (config.keepLogDays >= 0) - new Thread(new ClearLog(getConnection())).start(); + new Thread(new ClearLog(getConnection(), getDataFolder())).start(); LBBlockListener lbBlockListener = new LBBlockListener(); LBPlayerListener lbPlayerListener = new LBPlayerListener(); PluginManager pm = getServer().getPluginManager();