From 8d459220d30735f51c737ea4a24e3f7624056171 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 15 Aug 2017 11:55:06 +0200 Subject: [PATCH] Vacuum SQLite database on server stop Closes #132 --- .../java/de/epiceric/shopchest/ShopChest.java | 4 +++ .../de/epiceric/shopchest/sql/Database.java | 10 +++--- .../de/epiceric/shopchest/sql/SQLite.java | 35 +++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 0f65aa2..9742785 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -209,6 +209,10 @@ public class ShopChest extends JavaPlugin { debug("Removed shop (#" + shop.getID() + ")"); } + if (database instanceof SQLite) { + ((SQLite) database).vacuum(false); + } + database.disconnect(); } diff --git a/src/main/java/de/epiceric/shopchest/sql/Database.java b/src/main/java/de/epiceric/shopchest/sql/Database.java index 6b48d92..2d8dd25 100644 --- a/src/main/java/de/epiceric/shopchest/sql/Database.java +++ b/src/main/java/de/epiceric/shopchest/sql/Database.java @@ -537,14 +537,14 @@ public abstract class Database { } /** - * Closes a {@link PreparedStatement} and a {@link ResultSet} - * @param ps {@link PreparedStatement} to close + * Closes a {@link Statement} and a {@link ResultSet} + * @param s {@link Statement} to close * @param rs {@link ResultSet} to close */ - private void close(PreparedStatement ps, ResultSet rs) { + void close(Statement s, ResultSet rs) { try { - if (ps != null) - ps.close(); + if (s != null) + s.close(); if (rs != null) rs.close(); } catch (SQLException ex) { diff --git a/src/main/java/de/epiceric/shopchest/sql/SQLite.java b/src/main/java/de/epiceric/shopchest/sql/SQLite.java index 52be243..2874c13 100644 --- a/src/main/java/de/epiceric/shopchest/sql/SQLite.java +++ b/src/main/java/de/epiceric/shopchest/sql/SQLite.java @@ -1,12 +1,15 @@ package de.epiceric.shopchest.sql; import de.epiceric.shopchest.ShopChest; +import org.bukkit.scheduler.BukkitRunnable; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.SQLException; +import java.sql.Statement; public class SQLite extends Database { @@ -46,4 +49,36 @@ public class SQLite extends Database { return null; } + + /** + * Vacuums the database to reduce file size + * @param async Whether the call should be executed asynchronously + */ + public void vacuum(boolean async) { + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + Statement s = null; + + try { + s = connection.createStatement(); + s.executeUpdate("VACUUM"); + + plugin.debug("Vacuumed SQLite database"); + } catch (final SQLException ex) { + plugin.getLogger().severe("Failed to access database"); + plugin.debug("Failed to vacuum database"); + plugin.debug(ex); + } finally { + close(s, null); + } + } + }; + + if (async) { + runnable.runTaskAsynchronously(plugin); + } else { + runnable.run(); + } + } }