diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index f39b68a..c1e8f05 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -170,6 +170,13 @@ public class Config { **/ public static boolean enableWorldGuardIntegration; + /** + *
Sets the time limit for cleaning up the economy log in days
+ * + * If this equals to {@code 0}, the economy log will not be cleaned. + **/ + public static int cleanupEconomyLogDays; + /** * Whether Towny integration should be enabled **/ @@ -478,6 +485,7 @@ public class Config { enableHologramInteraction = plugin.getConfig().getBoolean("enable-hologram-interaction"); enableDebugLog = plugin.getConfig().getBoolean("enable-debug-log"); enableEcomomyLog = plugin.getConfig().getBoolean("enable-economy-log"); + cleanupEconomyLogDays = plugin.getConfig().getInt("cleanup-ecomomy-log-days"); enableWorldGuardIntegration = plugin.getConfig().getBoolean("enable-worldguard-integration"); enableTownyIntegration = plugin.getConfig().getBoolean("enable-towny-integration"); enableAuthMeIntegration = plugin.getConfig().getBoolean("enable-authme-integration"); diff --git a/src/main/java/de/epiceric/shopchest/sql/Database.java b/src/main/java/de/epiceric/shopchest/sql/Database.java index 14302f2..db3ccd7 100644 --- a/src/main/java/de/epiceric/shopchest/sql/Database.java +++ b/src/main/java/de/epiceric/shopchest/sql/Database.java @@ -155,6 +155,11 @@ public abstract class Database { s4.executeUpdate(queryCreateTablePlayerLogout); s4.close(); + // Clean up economy log + if (Config.cleanupEconomyLogDays > 0) { + cleanUpEconomy(false); + } + // Count entries in table "shops" PreparedStatement ps = connection.prepareStatement("SELECT * FROM shops"); ResultSet rs2 = ps.executeQuery(); @@ -424,6 +429,52 @@ public abstract class Database { } } + /** + * Cleans up the economy log to reduce file size + * @param async Whether the call should be executed asynchronously + */ + public void cleanUpEconomy(boolean async) { + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + Statement s = null; + Statement s2 = null; + + Calendar cal = Calendar.getInstance(); + long time = System.currentTimeMillis(); + cal.add(Calendar.DATE, -Config.cleanupEconomyLogDays); + time -= Config.cleanupEconomyLogDays * 86400000L; + String logPurgeLimit = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime()); + String queryCleanUpLog = "DELETE FROM shop_log WHERE timestamp < '" + logPurgeLimit + "'"; + String queryCleanUpPlayers = "DELETE FROM player_logout WHERE time < " + String.valueOf(time); + + try { + s = connection.createStatement(); + s.executeUpdate(queryCleanUpLog); + + s2 = connection.createStatement(); + s2.executeUpdate(queryCleanUpPlayers); + + plugin.getLogger().info("Cleaned up economy log"); + plugin.debug("Cleaned up economy log"); + } catch (final SQLException ex) { + plugin.getLogger().severe("Failed to clean up economy log"); + plugin.debug("Failed to clean up economy log"); + plugin.debug(ex); + } finally { + close(s, null); + close(s2, null); + } + } + }; + + if (async) { + runnable.runTaskAsynchronously(plugin); + } else { + runnable.run(); + } + } + /** * Get the revenue a player got while he was offline * @param player Player whose revenue to get diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 862ced7..cf9b321 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -40,6 +40,11 @@ enable-hologram-interaction: true # Set whether buys and sells should be logged in the database. enable-economy-log: false +# Sets the time limit for cleaning up the economy log in days. +# All log entries older than this will be deleted on server start. +# Set this to 0 in order to disable this feature. +cleanup-ecomomy-log-days: 30 + # Set whether a debug log file should be created. # The file may get large! Please enable this setting when reporting bugs. enable-debug-log: false