From 3b12341d4ccfec278a3096a28351e725ec843277 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 17 Jun 2021 11:54:26 +0800 Subject: [PATCH 1/5] Add a new method for detecting world loaded --- src/main/java/org/maxgamer/quickshop/util/Util.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/maxgamer/quickshop/util/Util.java b/src/main/java/org/maxgamer/quickshop/util/Util.java index e07b0fbe7..040a61b08 100644 --- a/src/main/java/org/maxgamer/quickshop/util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/util/Util.java @@ -872,6 +872,16 @@ public class Util { } } + /** + * Returns true if the world of given location is loaded or not. + * + * @param loc The location containing world + * @return true if the world of given location is loaded or not. + */ + public static boolean isWorldLoaded(Location loc) { + return (getNMSVersion().contains("1_13") && loc.getWorld() != null) || loc.isWorldLoaded(); + } + /** * Returns true if the given location is loaded or not. * @@ -879,7 +889,7 @@ public class Util { * @return true if the given location is loaded or not. */ public static boolean isLoaded(@NotNull Location loc) { - if ((!getNMSVersion().contains("1_13") && !loc.isWorldLoaded()) || loc.getWorld() == null) { + if (!isWorldLoaded(loc)) { return false; } // Calculate the chunks coordinates. These are 1,2,3 for each chunk, NOT From 54ad57fbd8e32507d563e5518ef41a6c6d12a035 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 17 Jun 2021 12:07:39 +0800 Subject: [PATCH 2/5] Refactor Timer and clean up --- .../org/maxgamer/quickshop/QuickShop.java | 2 +- .../quickshop/database/DatabaseManager.java | 4 +- .../maxgamer/quickshop/shop/ShopLoader.java | 16 ++--- .../org/maxgamer/quickshop/util/Timer.java | 58 ++++++++++++++----- 4 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index ef0959a89..267e1bd9e 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -947,7 +947,7 @@ public class QuickShop extends JavaPlugin { calendarWatcher = new CalendarWatcher(this); calendarWatcher.start(); Util.debugLog("Now using display-type: " + DisplayItem.getNowUsing().name()); - getLogger().info("QuickShop Loaded! " + enableTimer.endTimer() + " ms."); + getLogger().info("QuickShop Loaded! " + enableTimer.stopAndGetTimePassed() + " ms."); } private void loadItemMatcher() { diff --git a/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java b/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java index deccc4723..448e83485 100644 --- a/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java +++ b/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java @@ -171,7 +171,7 @@ public class DatabaseManager { } task.run(connection); - long tookTime = timer.endTimer(); + long tookTime = timer.stopAndGetTimePassed(); if (tookTime > 300) { warningSender.sendWarn( "Database performance warning: It took too long time (" @@ -183,7 +183,7 @@ public class DatabaseManager { connection.commit(); connection.setAutoCommit(true); } - long tookTime = ctimer.endTimer(); + long tookTime = ctimer.stopAndGetTimePassed(); if (tookTime > 5500) { warningSender.sendWarn( "Database performance warning: It took too long time (" diff --git a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java index fa0f6b54e..ee726a099 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java @@ -53,7 +53,7 @@ import java.util.logging.Logger; public class ShopLoader { private final List loadTimes = new ArrayList<>(); - private final Map costCache = new HashMap<>(); + private final Map timeCostCache = new HashMap<>(); private final QuickShop plugin; /* This may contains broken shop, must use null check before load it. */ @@ -92,7 +92,7 @@ public class ShopLoader { try (WarpedResultSet warpRS = plugin.getDatabaseHelper().selectAllShops(); ResultSet rs = warpRS.getResultSet()) { this.plugin .getLogger() - .info("Used " + fetchTimer.endTimer() + "ms to fetch all shops from the database."); + .info("Used " + fetchTimer.stopAndGetTimePassed() + "ms to fetch all shops from the database."); while (rs.next()) { Timer singleShopLoadTimer = new Timer(true); ShopDatabaseInfoOrigin origin = new ShopDatabaseInfoOrigin(rs); @@ -115,7 +115,7 @@ public class ShopLoader { shop.setDirty(); } shopsInDatabase.add(shop); - this.costCalc(singleShopLoadTimer); + this.calcTimeCost(singleShopLoadTimer); if (shopNullCheck(shop)) { if (plugin.getConfig().getBoolean("debug.delete-corrupt-shops", false)) { plugin.getLogger().warning("Deleting shop " + shop + " caused by corrupted."); @@ -148,7 +148,7 @@ public class ShopLoader { } singleShopLoaded(singleShopLoadTimer); } - long totalUsedTime = totalLoadTimer.endTimer(); + long totalUsedTime = totalLoadTimer.stopAndGetTimePassed(); long avgPerShop = mean(loadTimes.toArray(new Long[0])); this.plugin .getLogger() @@ -171,7 +171,7 @@ public class ShopLoader { private void singleShopLoaded(@NotNull Timer singleShopLoadTimer) { totalLoaded++; - long singleShopLoadTime = singleShopLoadTimer.endTimer(); + long singleShopLoadTime = singleShopLoadTimer.stopAndGetTimePassed(); loadTimes.add(singleShopLoadTime); Util.debugLog("Loaded shop used time " + singleShopLoadTime + "ms"); // if (singleShopLoadTime > 1500) { @@ -179,9 +179,9 @@ public class ShopLoader { // } } - private double costCalc(@NotNull Timer timer) { - costCache.putIfAbsent(timer, (double) timer.getTimer()); - return timer.getTimer() - costCache.get(timer); + private double calcTimeCost(@NotNull Timer timer) { + timeCostCache.putIfAbsent(timer, (double) timer.getPassedTime()); + return timer.getPassedTime() - timeCostCache.get(timer); } @SuppressWarnings("ConstantConditions") diff --git a/src/main/java/org/maxgamer/quickshop/util/Timer.java b/src/main/java/org/maxgamer/quickshop/util/Timer.java index c6fe93ae0..297a35efd 100644 --- a/src/main/java/org/maxgamer/quickshop/util/Timer.java +++ b/src/main/java/org/maxgamer/quickshop/util/Timer.java @@ -32,21 +32,23 @@ import lombok.ToString; @ToString public class Timer { private long startTime; + private long passedTime; + private boolean isPaused = false; /** - * Create a empty time, use setTimer to start + * Create a empty timer, use setTimer to start */ public Timer() { } /** - * Create a empty time, auto start if autoSet is true + * Create a empty timer, auto start if autoStart is true * - * @param autoSet Auto set the timer + * @param autoStart Auto set the timer */ - public Timer(boolean autoSet) { - if (autoSet) { - startTime = System.currentTimeMillis(); + public Timer(boolean autoStart) { + if (autoStart) { + start(); } } @@ -64,27 +66,47 @@ public class Timer { * * @return time */ - public long endTimer() { - long time = System.currentTimeMillis() - startTime; + public long stopAndGetTimePassed() { + long time = getPassedTime(); startTime = 0; return time; } /** - * Return how long time running after atTimeS. THIS NOT WILL DESTORY AND STOP THE TIMER + * Return how long time running after a specified time. THIS NOT WILL DESTORY AND STOP THE TIMER * - * @param atTime The inited time + * @param atTime The specified time * @return time */ - public long getTimerAt(long atTime) { - return atTime - startTime; + public long getPassedTimeOffsetFrom(long atTime) { + return (atTime - startTime) + passedTime; } /** - * Create a Timer. Time Unit: ms + * Start the timer. Time Unit: ms */ - public void setTimer() { + public void start() { this.startTime = System.currentTimeMillis(); + isPaused = false; + } + + /** + * Pause the timer. Time Unit: ms + */ + public void pause() { + this.passedTime = getPassedTime(); + isPaused = true; + } + + /** + * Resume the timer. Time Unit: ms + */ + public void resume() { + if (isPaused) { + this.startTime = System.currentTimeMillis() - passedTime; + passedTime = 0; + isPaused = false; + } } /** @@ -92,8 +114,12 @@ public class Timer { * * @return time */ - public long getTimer() { - return System.currentTimeMillis() - startTime; + public long getPassedTime() { + if (isPaused) { + return passedTime; + } else { + return System.currentTimeMillis() - startTime; + } } } From 195220e9946910863bbf226a5beab3d40c845142 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 17 Jun 2021 12:10:15 +0800 Subject: [PATCH 3/5] Renaming ShopDatabaseInfoOrigin and fix world unloaded shop loading --- .../maxgamer/quickshop/shop/ShopLoader.java | 49 ++++++++++++------- .../collector/adapter/CollectorAdapter.java | 2 +- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java index ee726a099..46e2e99df 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java @@ -58,7 +58,7 @@ public class ShopLoader { private final QuickShop plugin; /* This may contains broken shop, must use null check before load it. */ private final List shopsInDatabase = new CopyOnWriteArrayList<>(); - private final List originShopsInDatabase = new CopyOnWriteArrayList<>(); + private final List shopRawDatabaseInfoList = new CopyOnWriteArrayList<>(); private int errors; private int loadAfterChunkLoaded = 0; private int loadAfterWorldLoaded = 0; @@ -95,13 +95,19 @@ public class ShopLoader { .info("Used " + fetchTimer.stopAndGetTimePassed() + "ms to fetch all shops from the database."); while (rs.next()) { Timer singleShopLoadTimer = new Timer(true); - ShopDatabaseInfoOrigin origin = new ShopDatabaseInfoOrigin(rs); - originShopsInDatabase.add(origin); + ShopRawDatabaseInfo origin = new ShopRawDatabaseInfo(rs); + shopRawDatabaseInfoList.add(origin); if (worldName != null && !origin.getWorld().equals(worldName)) { singleShopLoaded(singleShopLoadTimer); continue; } ShopDatabaseInfo data = new ShopDatabaseInfo(origin); + //World unloaded and not found + if (data.getWorld() == null) { + ++loadAfterWorldLoaded; + singleShopLoaded(singleShopLoadTimer); + continue; + } Shop shop = new ContainerShop(plugin, data.getLocation(), @@ -124,7 +130,12 @@ public class ShopLoader { Util.debugLog("Trouble database loading debug: " + data); Util.debugLog("Somethings gone wrong, skipping the loading..."); } - loadAfterWorldLoaded++; + singleShopLoaded(singleShopLoadTimer); + continue; + } + //World unloaded but found + if (!Util.isWorldLoaded(shop.getLocation())) { + ++loadAfterWorldLoaded; singleShopLoaded(singleShopLoadTimer); continue; } @@ -294,8 +305,8 @@ public class ShopLoader { String shopStr = shopsPlain[i].trim(); boolean success = false; try { - ShopDatabaseInfoOrigin shopDatabaseInfoOrigin = gson.fromJson(shopStr, ShopDatabaseInfoOrigin.class); - originShopsInDatabase.add(shopDatabaseInfoOrigin); + ShopRawDatabaseInfo shopDatabaseInfoOrigin = gson.fromJson(shopStr, ShopRawDatabaseInfo.class); + shopRawDatabaseInfoList.add(shopDatabaseInfoOrigin); ShopDatabaseInfo data = new ShopDatabaseInfo(shopDatabaseInfoOrigin); Shop shop = new ContainerShop(plugin, @@ -331,12 +342,12 @@ public class ShopLoader { public void removeShopFromShopLoader(Shop shop) { if (this.shopsInDatabase.remove(shop)) { - for (ShopDatabaseInfoOrigin origin : this.originShopsInDatabase) { - if (Objects.equals(shop.getLocation().getWorld().getName(), origin.getWorld())) { - if (shop.getLocation().getBlockX() == origin.getX()) { - if (shop.getLocation().getBlockY() == origin.getY()) { - if (shop.getLocation().getBlockZ() == origin.getZ()) { - this.originShopsInDatabase.remove(origin); + for (ShopRawDatabaseInfo rawDatabaseInfo : this.shopRawDatabaseInfoList) { + if (Objects.equals(shop.getLocation().getWorld().getName(), rawDatabaseInfo.getWorld())) { + if (shop.getLocation().getBlockX() == rawDatabaseInfo.getX()) { + if (shop.getLocation().getBlockY() == rawDatabaseInfo.getY()) { + if (shop.getLocation().getBlockZ() == rawDatabaseInfo.getZ()) { + this.shopRawDatabaseInfoList.remove(rawDatabaseInfo); break; } } @@ -347,13 +358,13 @@ public class ShopLoader { } @NotNull - public List getOriginShopsInDatabase() { - return new ArrayList<>(originShopsInDatabase); + public List getOriginShopsInDatabase() { + return new ArrayList<>(shopRawDatabaseInfoList); } @Getter @Setter - static public class ShopDatabaseInfoOrigin { + static public class ShopRawDatabaseInfo { private String item; private String moderators; @@ -374,7 +385,7 @@ public class ShopLoader { private String extra; - ShopDatabaseInfoOrigin(ResultSet rs) throws SQLException { + ShopRawDatabaseInfo(ResultSet rs) throws SQLException { this.x = rs.getInt("x"); this.y = rs.getInt("y"); this.z = rs.getInt("z"); @@ -391,7 +402,7 @@ public class ShopLoader { } } - ShopDatabaseInfoOrigin(int x, int y, int z, String world, String itemConfig, String owner, double price, int type, boolean unlimited, String extra) { + ShopRawDatabaseInfo(int x, int y, int z, String world, String itemConfig, String owner, double price, int type, boolean unlimited, String extra) { this.x = x; this.y = y; this.z = z; @@ -404,7 +415,7 @@ public class ShopLoader { this.extra = extra; } - ShopDatabaseInfoOrigin() { + ShopRawDatabaseInfo() { } @@ -441,7 +452,7 @@ public class ShopLoader { private AtomicBoolean needUpdate = new AtomicBoolean(false); - ShopDatabaseInfo(ShopDatabaseInfoOrigin origin) { + ShopDatabaseInfo(ShopRawDatabaseInfo origin) { try { this.x = origin.getX(); this.y = origin.getY(); diff --git a/src/main/java/org/maxgamer/quickshop/util/collector/adapter/CollectorAdapter.java b/src/main/java/org/maxgamer/quickshop/util/collector/adapter/CollectorAdapter.java index 94d3e7f7e..53da94fb7 100644 --- a/src/main/java/org/maxgamer/quickshop/util/collector/adapter/CollectorAdapter.java +++ b/src/main/java/org/maxgamer/quickshop/util/collector/adapter/CollectorAdapter.java @@ -216,7 +216,7 @@ public class CollectorAdapter { .getShopLoader() .getOriginShopsInDatabase() .stream() - .map(ShopLoader.ShopDatabaseInfoOrigin::toString) + .map(ShopLoader.ShopRawDatabaseInfo::toString) .toArray()); map.put("memory", plugin.getShopManager().getAllShops().stream().map(Shop::toString).toArray()); return map; From b6937956790aaa70478cf527512428ef3d5eb8ab Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 17 Jun 2021 12:11:04 +0800 Subject: [PATCH 4/5] Inline variable and fix doc --- .../maxgamer/quickshop/shop/ShopLoader.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java index 46e2e99df..32ee8ab8f 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java @@ -60,8 +60,6 @@ public class ShopLoader { private final List shopsInDatabase = new CopyOnWriteArrayList<>(); private final List shopRawDatabaseInfoList = new CopyOnWriteArrayList<>(); private int errors; - private int loadAfterChunkLoaded = 0; - private int loadAfterWorldLoaded = 0; private int totalLoaded = 0; //private final WarningSender warningSender; @@ -80,15 +78,17 @@ public class ShopLoader { } /** - * Load all shops + * Load all shops in the specified world * - * @param worldName The world name + * @param worldName The world name, null if load all shops */ public void loadShops(@Nullable String worldName) { //boolean backupedDatabaseInDeleteProcess = false; Timer totalLoadTimer = new Timer(true); this.plugin.getLogger().info("Loading shops from the database..."); Timer fetchTimer = new Timer(true); + int loadAfterChunkLoaded = 0; + int loadAfterWorldLoaded = 0; try (WarpedResultSet warpRS = plugin.getDatabaseHelper().selectAllShops(); ResultSet rs = warpRS.getResultSet()) { this.plugin .getLogger() @@ -171,9 +171,9 @@ public class ShopLoader { + "ms, Avg " + avgPerShop + "ms per shop)"); - this.plugin.getLogger().info(this.loadAfterChunkLoaded + this.plugin.getLogger().info(loadAfterChunkLoaded + " shops will load after chunk have loaded, " - + this.loadAfterWorldLoaded + + loadAfterWorldLoaded + " shops will load after the world has loaded."); } catch (Exception e) { exceptionHandler(e, null); @@ -198,27 +198,23 @@ public class ShopLoader { @SuppressWarnings("ConstantConditions") private boolean shopNullCheck(@Nullable Shop shop) { if (shop == null) { - Util.debugLog("Shop Object is null"); + Util.debugLog("Shop object is null"); return true; } if (shop.getItem() == null) { - Util.debugLog("Shop ItemStack is null"); + Util.debugLog("Shop itemStack is null"); return true; } if (shop.getItem().getType() == Material.AIR) { - Util.debugLog("Shop ItemStack type can't be AIR"); + Util.debugLog("Shop itemStack type can't be AIR"); return true; } if (shop.getLocation() == null) { - Util.debugLog("Shop Location is null"); - return true; - } - if (shop.getLocation().getWorld() == null) { - Util.debugLog("Shop World is null"); + Util.debugLog("Shop location is null"); return true; } if (shop.getOwner() == null) { - Util.debugLog("Shop Owner is null"); + Util.debugLog("Shop owner is null"); return true; } if (plugin.getServer().getOfflinePlayer(shop.getOwner()).getName() == null) { From e2f7c80bb7ad883656ee1914e9b4ec5630d221e9 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 17 Jun 2021 12:12:06 +0800 Subject: [PATCH 5/5] Commented duplicated loadShop in ContainerShop --- src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index d7d9a9587..3533297da 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -820,7 +820,8 @@ public class ContainerShop implements Shop { return; } this.isLoaded = true; - plugin.getShopManager().loadShop(this.getLocation().getWorld().getName(), this); + //Shop manger done this already + //plugin.getShopManager().loadShop(this.getLocation().getWorld().getName(), this); plugin.getShopManager().getLoadedShops().add(this); plugin.getShopContainerWatcher().scheduleCheck(this);