From 3912dbfabc9837a06061a52a607a0cb72bd9d146 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Mon, 14 Jul 2014 18:23:29 -0500 Subject: [PATCH] Only save the prisoners if they were changed, add that flag. --- README.md | 2 +- .../java/com/graywolf336/jail/JailIO.java | 48 +++++++++++-------- .../com/graywolf336/jail/beans/Prisoner.java | 40 ++++++++++++++-- 3 files changed, 65 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index a2e5546..c20df24 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Beta 2 Changes * Added `/jail time` for easy access to adding/subtracting time - [Bukkit Dev Ticket #432](http://dev.bukkit.org/bukkit-plugins/jail/tickets/432/) * Added `/togglejaildebug` for easily toggling the debugging state, enable if you have a problem and want to send me information * Added some caching of online prisoners and where they're located at, this improves performance on servers with 500+ prisoners jailed +* Only updating prisoners in the database if they were changed, this should help improve saving speed Beta 1 Changes === @@ -44,7 +45,6 @@ Changes ToDo === -* Only save prisoners that have been edited, aka a flag on the prisoners which is "have they changed" and change that when a function is called that changes the prisoner so we don't save prisoners that haven't changed * When calculating the reducing of time, make it async and all the calls it does be scheduled * Jail set * Jail vote diff --git a/src/main/java/com/graywolf336/jail/JailIO.java b/src/main/java/com/graywolf336/jail/JailIO.java index b5d183d..22f40fb 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -470,6 +470,7 @@ public class JailIO { p.setArmor(new String(ar.getBytes(1, (int)ar.length()))); p.setPreviousPosition(set.getString("previousLocation")); p.setPreviousGameMode(set.getString("previousGameMode")); + p.setChanged(false);//Since we just loaded the prisoner, we really don't need to save them. if(cellname == null || cellname.isEmpty()) { j.addPrisoner(p); @@ -608,6 +609,7 @@ public class JailIO { p.setPreviousGameMode(flat.getString(cellNode + "prisoner.previousGameMode")); p.setInventory(flat.getString(cellNode + "prisoner.inventory", "")); p.setArmor(flat.getString(cellNode + "prisoner.armor", "")); + p.setChanged(false);//Since we just loaded the prisoner, we really don't need to save them. c.setPrisoner(p); } @@ -633,6 +635,7 @@ public class JailIO { pris.setPreviousGameMode(flat.getString(pNode + "previousGameMode")); pris.setInventory(flat.getString(pNode + "inventory", "")); pris.setArmor(flat.getString(pNode + "armor", "")); + pris.setChanged(false);//Since we just loaded the prisoner, we really don't need to save them. j.addPrisoner(pris); } } @@ -711,7 +714,7 @@ public class JailIO { if(con == null) this.prepareStorage(false); for(Cell c : j.getCells()) { - if(c.hasPrisoner()) { + if(c.hasPrisoner() && c.getPrisoner().wasChanged()) { Prisoner p = c.getPrisoner(); PreparedStatement pPS = con.prepareStatement("REPLACE INTO `" + prefix + "prisoners` (`uuid`, `name`, `jail`, `cell`, `muted`, `time`," + "`offlinePending`, `toBeTransferred`, `jailer`, `reason`, `inventory`, `armor`, `previousLocation`, `previousGameMode`)" @@ -733,6 +736,8 @@ public class JailIO { pPS.executeUpdate(); pPS.close(); + + p.setChanged(false);//Since we just saved the prisoner } } } catch (SQLException e) { @@ -745,25 +750,28 @@ public class JailIO { if(con == null) this.prepareStorage(false); for(Prisoner p : j.getPrisonersNotInCells().values()) { - PreparedStatement pPS = con.prepareStatement("REPLACE INTO `" + prefix + "prisoners` (`uuid`, `name`, `jail`, `cell`, `muted`, `time`," - + "`offlinePending`, `toBeTransferred`, `jailer`, `reason`, `inventory`, `armor`, `previousLocation`, `previousGameMode`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); - pPS.setString(1, p.getUUID().toString()); - pPS.setString(2, p.getLastKnownName()); - pPS.setString(3, j.getName()); - pPS.setString(4, ""); - pPS.setBoolean(5, p.isMuted()); - pPS.setFloat(6, p.getRemainingTime()); - pPS.setBoolean(7, p.isOfflinePending()); - pPS.setBoolean(8, p.isToBeTransferred()); - pPS.setString(9, p.getJailer()); - pPS.setString(10, p.getReason()); - pPS.setBytes(11, p.getInventory().getBytes()); - pPS.setBytes(12, p.getArmor().getBytes()); - pPS.setString(13, p.getPreviousLocationString()); - pPS.setString(14, p.getPreviousGameMode().toString()); - - pPS.executeUpdate(); - pPS.close(); + if(p.wasChanged()) { + PreparedStatement pPS = con.prepareStatement("REPLACE INTO `" + prefix + "prisoners` (`uuid`, `name`, `jail`, `cell`, `muted`, `time`," + + "`offlinePending`, `toBeTransferred`, `jailer`, `reason`, `inventory`, `armor`, `previousLocation`, `previousGameMode`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); + pPS.setString(1, p.getUUID().toString()); + pPS.setString(2, p.getLastKnownName()); + pPS.setString(3, j.getName()); + pPS.setString(4, ""); + pPS.setBoolean(5, p.isMuted()); + pPS.setFloat(6, p.getRemainingTime()); + pPS.setBoolean(7, p.isOfflinePending()); + pPS.setBoolean(8, p.isToBeTransferred()); + pPS.setString(9, p.getJailer()); + pPS.setString(10, p.getReason()); + pPS.setBytes(11, p.getInventory().getBytes()); + pPS.setBytes(12, p.getArmor().getBytes()); + pPS.setString(13, p.getPreviousLocationString()); + pPS.setString(14, p.getPreviousGameMode().toString()); + + pPS.executeUpdate(); + pPS.close(); + p.setChanged(false);//Since we just saved the prisoner + } } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/com/graywolf336/jail/beans/Prisoner.java b/src/main/java/com/graywolf336/jail/beans/Prisoner.java index a635735..e6e2f64 100644 --- a/src/main/java/com/graywolf336/jail/beans/Prisoner.java +++ b/src/main/java/com/graywolf336/jail/beans/Prisoner.java @@ -12,11 +12,11 @@ import org.bukkit.Location; * * @author graywolf336 * @since 2.x.x - * @version 3.0.1 + * @version 3.0.2 */ public class Prisoner { private String uuid, name, jailer, reason, inventory, armor; - private boolean muted, offlinePending, teleporting, toBeTransferred; + private boolean muted, offlinePending, teleporting, toBeTransferred, changed; private long time, afk; private Location previousPosition; private GameMode previousGameMode; @@ -46,6 +46,7 @@ public class Prisoner { this.inventory = ""; this.armor = ""; this.afk = 0; + this.changed = false; } /** Returns the UUID of the prisoner. */ @@ -61,6 +62,7 @@ public class Prisoner { /** Sets the name of this prisoner. */ public String setLastKnownName(String username) { this.name = username; + this.changed = true; return this.name; } @@ -69,9 +71,16 @@ public class Prisoner { return this.reason; } - /** Sets the reason this player was jailed for. */ - public void setReason(String reason) { + /** + * Sets the reason this player was jailed for. + * + * @param reason the player was jailed. + * @return the reason the player was jailed, what we have stored about them. + */ + public String setReason(String reason) { this.reason = reason; + this.changed = true; + return this.reason; } /** Gets the person who jailed this prisoner. */ @@ -82,6 +91,7 @@ public class Prisoner { /** Sets the person who jailed this prisoner. */ public void setJailer(String jailer) { this.jailer = jailer; + this.changed = true; } /** Gets whether the prisoner is muted or not. */ @@ -92,6 +102,7 @@ public class Prisoner { /** Sets whether the prisoner is muted or not. */ public void setMuted(boolean muted) { this.muted = muted; + this.changed = true; } /** Gets the remaining time the prisoner has. */ @@ -116,6 +127,7 @@ public class Prisoner { */ public void setRemainingTime(long time) { this.time = time; + this.changed = true; } /** @@ -126,6 +138,7 @@ public class Prisoner { */ public long addTime(long time) { this.time += time; + this.changed = true; return this.time; } @@ -137,6 +150,7 @@ public class Prisoner { */ public long subtractTime(long time) { this.time -= time; + this.changed = true; return this.time; } @@ -148,6 +162,7 @@ public class Prisoner { /** Sets whether the player is offline or not. */ public void setOfflinePending(boolean offline) { this.offlinePending = offline; + this.changed = true; } /** Gets whether the player is being teleported or not. */ @@ -168,6 +183,7 @@ public class Prisoner { /** Sets whether the prisoner is going to be transferred or not, mainly for teleporting on join purposes. */ public void setToBeTransferred(boolean transferred) { this.toBeTransferred = transferred; + this.changed = true; } /** Gets the previous location of this player, can be null. */ @@ -197,6 +213,7 @@ public class Prisoner { if(location == null) return; if(location.isEmpty()) return; + this.changed = true; String[] s = location.split(","); this.previousPosition = new Location(Bukkit.getWorld(s[0]), Double.valueOf(s[1]), @@ -214,6 +231,7 @@ public class Prisoner { /** Sets the previous gamemode of this player. */ public void setPreviousGameMode(GameMode previous) { this.previousGameMode = previous; + this.changed = true; } /** Sets the previous gamemode of this player based upon the provided string. */ @@ -221,6 +239,7 @@ public class Prisoner { if(previous == null) return; else if(previous.isEmpty()) return; else this.previousGameMode = GameMode.valueOf(previous); + this.changed = true; } /** Gets the inventory string for this player, it is encoded in Base64 string. */ @@ -231,6 +250,7 @@ public class Prisoner { /** Sets the inventory Base64 string. */ public void setInventory(String inventory) { this.inventory = inventory; + this.changed = true; } /** Gets the armor content, encoded in Base64 string. */ @@ -241,6 +261,7 @@ public class Prisoner { /** Sets the armor inventory Base64 string. */ public void setArmor(String armor) { this.armor = armor; + this.changed = true; } /** Gets the time, in milliseconds, this prisoner has been afk. */ @@ -252,4 +273,15 @@ public class Prisoner { public void setAFKTime(long time) { this.afk = time; } + + /** Checks if the prisoner was changed or not. */ + public boolean wasChanged() { + return this.changed; + } + + /** Sets whether the prisoner was changed or not. */ + public boolean setChanged(boolean change) { + this.changed = change; + return this.changed; + } }