From 229be42d64af385e7e1b41a6fbd5d614743e17ab Mon Sep 17 00:00:00 2001 From: Zrips Date: Fri, 16 Aug 2019 14:22:39 +0300 Subject: [PATCH] Lets save block protection in 100's batches whatever it reaches this limit, instead of doing everything in one go on server shutdown --- src/main/java/com/gamingmesh/jobs/Jobs.java | 7 +- .../gamingmesh/jobs/commands/list/area.java | 15 +++ .../jobs/commands/list/convert.java | 3 +- .../jobs/config/BlockProtectionManager.java | 30 +++++ .../jobs/config/GeneralConfigManager.java | 4 +- .../gamingmesh/jobs/dao/JobsConnection.java | 4 + .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 123 ++++++------------ 7 files changed, 100 insertions(+), 86 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 27102572..ddd04706 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -659,8 +659,8 @@ public class Jobs extends JavaPlugin { dao.getMap().clear(); if (getPlayerManager().getPlayersCache().size() != 0) - consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " + - ((int) (((System.currentTimeMillis() - time) / 1000d) * 100) / 100D)); + consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " + + ((int) (((System.currentTimeMillis() - time) / 1000d) * 100) / 100D)); } public static void reload() throws IOException { @@ -988,7 +988,8 @@ public class Jobs extends JavaPlugin { GUIManager.CloseInventories(); shopManager.CloseInventories(); dao.saveExplore(); - dao.saveBlockProtection(); + + Jobs.getBpManager().saveCache(); FurnaceBrewingHandling.save(); ToggleBarHandling.save(); } catch (Throwable e) { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/area.java b/src/main/java/com/gamingmesh/jobs/commands/list/area.java index 4c5c872a..515b5cb1 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/area.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/area.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map.Entry; +import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -14,6 +15,7 @@ import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.config.RestrictedAreaManager; import com.gamingmesh.jobs.container.CuboidArea; import com.gamingmesh.jobs.container.RestrictedArea; +import com.gamingmesh.jobs.stuff.Debug; public class area implements Cmd { @@ -26,6 +28,19 @@ public class area implements Cmd { } Player player = (Player) sender; + Location center = player.getLocation(); + Debug.D("adding " + Jobs.getBpManager().getSize()); + for (int x = -16; x < 16; x++) { + for (int z = -16; z < 16; z++) { + Location lc = center.clone().add(x, 0, z); + for (int y = 250; y > 1; y--) { + lc.setY(y); + Jobs.getBpManager().add(lc, 100); + } + } + } + Debug.D("added " + Jobs.getBpManager().getSize()); + RestrictedAreaManager ra = Jobs.getRestrictedAreaManager(); if (args.length == 3) { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/convert.java b/src/main/java/com/gamingmesh/jobs/commands/list/convert.java index 0e7e5b4a..ab44a234 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/convert.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/convert.java @@ -52,7 +52,8 @@ public class convert implements Cmd { Jobs.getPlayerManager().clearCache(); Jobs.getJobsDAO().saveExplore(); - Jobs.getJobsDAO().saveBlockProtection(); +// Do we really need to convert Block protection? +// Jobs.getJobsDAO().saveBlockProtection(); } catch (SQLException e) { e.printStackTrace(); Jobs.consoleMsg("&cCan't write data to data base, please send error log to dev's."); diff --git a/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java b/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java index e3b9fc01..70e0d9aa 100644 --- a/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java @@ -3,6 +3,7 @@ package com.gamingmesh.jobs.config; import java.util.HashMap; import java.util.Map.Entry; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; @@ -16,6 +17,7 @@ import com.gamingmesh.jobs.container.DBAction; public class BlockProtectionManager { private HashMap>>> map = new HashMap<>(); + private HashMap> tempCache = new HashMap<>(); public Long timer = 0L; @@ -83,9 +85,37 @@ public class BlockProtectionManager { chunks.put(chunk, Bpm); regions.put(region, chunks); map.put(loc.getWorld(), regions); + addToCache(loc, Bp); return Bp; } + private void addToCache(Location loc, BlockProtection Bp) { + if (!Jobs.getGCManager().useBlockProtection) + return; + String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ(); + HashMap locations = tempCache.get(loc.getWorld()); + if (locations == null) { + locations = new HashMap(); + tempCache.put(loc.getWorld(), locations); + } + + locations.put(v, Bp); + + if (locations.size() > 100) { + Jobs.getJobsDAO().saveBlockProtection(loc.getWorld().getName(), new HashMap(locations)); + locations.clear(); + } + } + + public void saveCache() { + if (!Jobs.getGCManager().useBlockProtection) + return; + for (Entry> one : tempCache.entrySet()) { + Jobs.getJobsDAO().saveBlockProtection(one.getKey().getName(), one.getValue()); + } + tempCache.clear(); + } + public BlockProtection remove(Block block) { return remove(block.getLocation()); } diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index 19f57e73..495cfd10 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -823,9 +823,9 @@ public class GeneralConfigManager { c.addComment("ExploitProtections.General.KeepDataFor", "For how long in days to keep block protection data in data base", "This will clean block data which ones have -1 as cooldown value", - "Data base cleanup will be performed on each server startup", "This cant be more then 30 days"); + "Data base cleanup will be performed on each server startup", "This cant be more then 14 days"); BlockProtectionDays = c.get("ExploitProtections.General.KeepDataFor", 14); - BlockProtectionDays = BlockProtectionDays > 30 ? 30 : BlockProtectionDays; + BlockProtectionDays = BlockProtectionDays > 14 ? 14 : BlockProtectionDays; c.addComment("ExploitProtections.General.GlobalBlockTimer", "All blocks will be protected X sec after player places it on ground."); useGlobalTimer = c.get("ExploitProtections.General.GlobalBlockTimer.use", true); diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java b/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java index 965c7e92..0ddab673 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java @@ -57,4 +57,8 @@ public class JobsConnection { public synchronized DatabaseMetaData getMetaData() throws SQLException { return conn.getMetaData(); } + + public synchronized void setClientInfo(String path, String value) throws SQLException { + conn.setClientInfo(path, value); + } } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 6282b689..8c048175 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -1628,7 +1628,7 @@ public abstract class JobsDAO { * Save block protection information * @param jobBlockProtection - the information getting saved */ - public void saveBlockProtection() { + public void saveBlockProtection(String world, HashMap cache) { JobsConnection conn = getConnection(); if (conn == null) return; @@ -1641,83 +1641,55 @@ public abstract class JobsDAO { update = conn.prepareStatement("UPDATE `" + prefix + "blocks` SET `recorded` = ?, `resets` = ? WHERE `id` = ?;"); delete = conn.prepareStatement("DELETE from `" + getPrefix() + "blocks` WHERE `id` = ?;"); - Jobs.getPluginLogger().info("Saving blocks"); - conn.setAutoCommit(false); - int inserted = 0; - int updated = 0; - int deleted = 0; + Long current = System.currentTimeMillis(); Long mark = System.currentTimeMillis() - (Jobs.getGCManager().BlockProtectionDays * 24L * 60L * 60L * 1000L); - for (Entry>>> worlds : Jobs.getBpManager().getMap().entrySet()) { - for (Entry>> regions : worlds.getValue().entrySet()) { - for (Entry> chunks : regions.getValue().entrySet()) { - for (Entry block : chunks.getValue().entrySet()) { - if (block.getValue() == null) - continue; - switch (block.getValue().getAction()) { - case DELETE: - delete.setInt(1, block.getValue().getId()); - delete.addBatch(); + for (Entry block : cache.entrySet()) { + if (block.getValue() == null) + continue; + switch (block.getValue().getAction()) { + case DELETE: + delete.setInt(1, block.getValue().getId()); + delete.addBatch(); - deleted++; - if (deleted % 10000 == 0) { - delete.executeBatch(); - Jobs.consoleMsg("&6[Jobs] Removed " + deleted + " old block protection entries."); - } - break; - case INSERT: - if (block.getValue().getTime() < current && block.getValue().getTime() != -1) - continue; - insert.setString(1, worlds.getKey().getName()); - insert.setInt(2, block.getValue().getPos().getBlockX()); - insert.setInt(3, block.getValue().getPos().getBlockY()); - insert.setInt(4, block.getValue().getPos().getBlockZ()); - insert.setLong(5, block.getValue().getRecorded()); - insert.setLong(6, block.getValue().getTime()); - insert.addBatch(); + break; + case INSERT: + if (block.getValue().getTime() < current && block.getValue().getTime() != -1) + continue; + insert.setString(1, world); + insert.setInt(2, block.getValue().getPos().getBlockX()); + insert.setInt(3, block.getValue().getPos().getBlockY()); + insert.setInt(4, block.getValue().getPos().getBlockZ()); + insert.setLong(5, block.getValue().getRecorded()); + insert.setLong(6, block.getValue().getTime()); + insert.addBatch(); + block.getValue().setAction(DBAction.NONE); - inserted++; - if (inserted % 10000 == 0) { - insert.executeBatch(); - Jobs.consoleMsg("&6[Jobs] Added " + inserted + " new block protection entries."); - } - break; - case UPDATE: - if (block.getValue().getTime() < current && block.getValue().getTime() != -1) - continue; - update.setLong(1, block.getValue().getRecorded()); - update.setLong(2, block.getValue().getTime()); - update.setInt(3, block.getValue().getId()); - update.addBatch(); + break; + case UPDATE: + if (block.getValue().getTime() < current && block.getValue().getTime() != -1) + continue; + update.setLong(1, block.getValue().getRecorded()); + update.setLong(2, block.getValue().getTime()); + update.setInt(3, block.getValue().getId()); + update.addBatch(); + block.getValue().setAction(DBAction.NONE); - updated++; - if (updated % 10000 == 0) { - update.executeBatch(); - Jobs.consoleMsg("&6[Jobs] Upadated " + updated + " old block protection entries."); - } - break; - case NONE: - if (block.getValue().getTime() < current && block.getValue().getTime() != -1) - continue; - if (block.getValue().getTime() == -1 && block.getValue().getRecorded() > mark) - continue; + break; + case NONE: + if (block.getValue().getTime() < current && block.getValue().getTime() != -1) + continue; + if (block.getValue().getTime() == -1 && block.getValue().getRecorded() > mark) + continue; - delete.setInt(1, block.getValue().getId()); - delete.addBatch(); + delete.setInt(1, block.getValue().getId()); + delete.addBatch(); - deleted++; - if (deleted % 10000 == 0) { - delete.executeBatch(); - Jobs.getPluginLogger().info("[Jobs] Removed " + deleted + " old block protection entries."); - } - break; - default: - continue; - } - } - } + break; + default: + continue; } } @@ -1725,16 +1697,7 @@ public abstract class JobsDAO { update.executeBatch(); delete.executeBatch(); conn.commit(); - conn.setAutoCommit(true); - if (inserted > 0) { - Jobs.consoleMsg("&6[Jobs] Added " + inserted + " new block protection entries."); - } - if (updated > 0) { - Jobs.consoleMsg("&6[Jobs] Updated " + updated + " with new block protection entries."); - } - if (deleted > 0) { - Jobs.consoleMsg("&6[Jobs] Deleted " + deleted + " old block protection entries."); - } + } catch (SQLException e) { e.printStackTrace(); } finally { @@ -2124,4 +2087,4 @@ public abstract class JobsDAO { return map; } -} +} \ No newline at end of file