From df0aa7283cab702f3d3f4c1e0123da5cca0965a6 Mon Sep 17 00:00:00 2001 From: lbenav8095 Date: Fri, 19 Apr 2024 14:46:24 -0600 Subject: [PATCH] Adds HandyDirectory#deleteRecursively Improves DataAssetManager performance when unloading BlobPlugins Improves LocalizableDataAssetManager performance when unloading BlobPlugins --- pom.xml | 2 +- .../bloblib/managers/DataAssetManager.java | 12 +++---- .../managers/LocalizableDataAssetManager.java | 9 ----- .../bloblib/utilities/HandyDirectory.java | 35 +++++++++++++++++++ 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 96646b7..eac467b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 us.mytheria BlobLib - 1.698.13 + 1.698.14 pom diff --git a/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java b/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java index 47e8039..ecc0f6b 100644 --- a/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java @@ -10,7 +10,10 @@ import us.mytheria.bloblib.entities.DataAssetType; import java.io.File; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.Predicate; @@ -87,13 +90,6 @@ public void reload(BlobPlugin plugin, IManagerDirector director) { public void unload(BlobPlugin plugin) { String pluginName = plugin.getName(); - Set assets = this.pluginAssets.get(pluginName); - if (assets == null) - return; - Iterator iterator = assets.iterator(); - while (iterator.hasNext()) { - iterator.remove(); - } this.pluginAssets.remove(pluginName); } diff --git a/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java b/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java index 886b721..b58db30 100644 --- a/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java @@ -89,15 +89,6 @@ public void reload(BlobPlugin plugin, IManagerDirector director) { public void unload(BlobPlugin plugin) { String pluginName = plugin.getName(); - Set assets = this.assets.get(pluginName); - if (assets == null) - return; - Iterator iterator = assets.iterator(); - while (iterator.hasNext()) { - String key = iterator.next(); - this.locales.forEach((locale, map) -> map.remove(key)); - iterator.remove(); - } this.assets.remove(pluginName); } diff --git a/src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java b/src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java index cd0d9d1..c9c310f 100644 --- a/src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java +++ b/src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java @@ -2,10 +2,16 @@ import org.apache.commons.io.FileUtils; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; +import java.util.Comparator; import java.util.Objects; +import java.util.function.Consumer; public record HandyDirectory(@NotNull File directory) { @@ -56,4 +62,33 @@ public File[] listFiles(@NotNull String extension) { public Collection listRecursively(@NotNull String... extensions) { return FileUtils.listFiles(directory, extensions, true); } + + /** + * Deletes the directory and all its contents. + * + * @param ifError The consumer to accept the exception if an error occurs. + * @return {@code true} if the directory was deleted successfully, {@code false} otherwise. + */ + public boolean deleteRecursively(@Nullable Consumer ifError) { + Path directory = this.directory.toPath(); + try (var walk = Files.walk(directory)) { + walk.sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + return true; + } catch (IOException exception) { + if (ifError != null) + ifError.accept(exception); + return false; + } + } + + /** + * Deletes the directory and all its contents. + * + * @return {@code true} if the directory was deleted successfully, {@code false} otherwise. + */ + public boolean deleteRecursively() { + return deleteRecursively(null); + } }