Skip to content

Commit

Permalink
Adds HandyDirectory#deleteRecursively
Browse files Browse the repository at this point in the history
Improves DataAssetManager performance when unloading BlobPlugins
Improves LocalizableDataAssetManager performance when unloading BlobPlugins
  • Loading branch information
anjoismysign committed Apr 19, 2024
1 parent a7f2829 commit df0aa72
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.13</version>
<version>1.698.14</version>
<packaging>pom</packaging>

<properties>
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -87,13 +90,6 @@ public void reload(BlobPlugin plugin, IManagerDirector director) {

public void unload(BlobPlugin plugin) {
String pluginName = plugin.getName();
Set<String> assets = this.pluginAssets.get(pluginName);
if (assets == null)
return;
Iterator<String> iterator = assets.iterator();
while (iterator.hasNext()) {
iterator.remove();
}
this.pluginAssets.remove(pluginName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@ public void reload(BlobPlugin plugin, IManagerDirector director) {

public void unload(BlobPlugin plugin) {
String pluginName = plugin.getName();
Set<String> assets = this.assets.get(pluginName);
if (assets == null)
return;
Iterator<String> iterator = assets.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
this.locales.forEach((locale, map) -> map.remove(key));
iterator.remove();
}
this.assets.remove(pluginName);
}

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -56,4 +62,33 @@ public File[] listFiles(@NotNull String extension) {
public Collection<File> 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<IOException> 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);
}
}

0 comments on commit df0aa72

Please sign in to comment.