From aa6cb0e3670f43ae74f9dfc3c9b6cf1485663bc8 Mon Sep 17 00:00:00 2001 From: zontreck Date: Thu, 25 Apr 2024 02:15:31 -0700 Subject: [PATCH] Add some more sanity checks to cut down on database query spam --- gradle.properties | 2 +- .../memory/world/BlockRestoreQueue.java | 16 ++++++++++++++++ .../memory/world/BlockRestoreRunner.java | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 5cdc71b..64c253f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=1201.13.042524.0151 +mod_version=1201.13.042524.0215 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java index 525da6c..ffe91a9 100644 --- a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java @@ -25,6 +25,11 @@ public abstract class BlockRestoreQueue private final BlockRestoreRunner RUNNER; private ScheduledFuture RUNNING_TASK; + /** + * When in database mode, this flag will be checked by the Restore Runner so a database call is not made unnecessarily. + */ + private boolean hasBlocks = true; + public BlockRestoreQueue() { RUNNER = new BlockRestoreRunner(this); @@ -74,6 +79,7 @@ public abstract class BlockRestoreQueue { databaseUpdate(block); notifyDirtyQueue(true); + hasBlocks=true; return; } BLOCK_QUEUE.add(block); @@ -178,6 +184,16 @@ public abstract class BlockRestoreQueue */ public abstract boolean sorted(); + /** + * Whether the queue has blocks or not + * @return + */ + public boolean hasBlocks() + { + if(usesDatabase()) return hasBlocks; + else return getQueuedBlocks() != 0; + } + /** * Clears the entire queue, discarding the saved blocks permanently. */ diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreRunner.java b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreRunner.java index 23bc9dd..05ade1c 100644 --- a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreRunner.java +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreRunner.java @@ -25,6 +25,9 @@ public class BlockRestoreRunner implements Runnable public void run() { if(queue.getQueuedBlocks() == 0 && !queue.usesDatabase()) return; // We'll be queued back up later + if(!queue.hasBlocks()) + return; + PrimitiveBlock prim = queue.getNextBlock(); if(prim == null)return; // No more blocks.