From 2417dace2d9a9d4a6dfd92e068a3f81bb01e4dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 20 May 2020 15:12:09 +0200 Subject: [PATCH] Add UUID caching --- .../com/plotsquared/bukkit/BukkitMain.java | 60 ++++++++++++++++++- .../bukkit/uuid/SQLiteUUIDService.java | 21 +++++++ .../com/plotsquared/core/PlotSquared.java | 17 ------ 3 files changed, 80 insertions(+), 18 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 90a717fd5..7effa5b4a 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -136,10 +136,17 @@ import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Queue; +import java.util.Set; import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import static com.plotsquared.core.util.PremiumVerification.getDownloadID; import static com.plotsquared.core.util.PremiumVerification.getResourceID; @@ -243,6 +250,8 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain final OfflinePlayerUUIDService offlinePlayerUUIDService = new OfflinePlayerUUIDService(); impromptuPipeline.registerService(offlinePlayerUUIDService); backgroundPipeline.registerService(offlinePlayerUUIDService); + + final SQLiteUUIDService sqLiteUUIDService = new SQLiteUUIDService(); if (!Settings.UUID.OFFLINE) { // If running Paper we'll also try to use their profiles if (PaperLib.isPaper()) { @@ -250,7 +259,6 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain impromptuPipeline.registerService(paperUUIDService); backgroundPipeline.registerService(paperUUIDService); } - final SQLiteUUIDService sqLiteUUIDService = new SQLiteUUIDService(); impromptuPipeline.registerService(sqLiteUUIDService); backgroundPipeline.registerService(sqLiteUUIDService); impromptuPipeline.registerConsumer(sqLiteUUIDService); @@ -259,9 +267,15 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain impromptuPipeline.registerService(impromptuMojangService); final SquirrelIdUUIDService backgroundMojangService = new SquirrelIdUUIDService(Settings.UUID.BACKGROUND_LIMIT); backgroundPipeline.registerService(backgroundMojangService); + } else { + impromptuPipeline.registerService(sqLiteUUIDService); + backgroundPipeline.registerService(sqLiteUUIDService); + impromptuPipeline.registerConsumer(sqLiteUUIDService); + backgroundPipeline.registerConsumer(sqLiteUUIDService); } impromptuPipeline.storeImmediately("*", DBFunc.EVERYONE); + this.startUuidCatching(sqLiteUUIDService, cacheUUIDService); if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { new Placeholders().register(); @@ -384,6 +398,50 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain } } + private void startUuidCatching(@NotNull final SQLiteUUIDService sqLiteUUIDService, + @NotNull final CacheUUIDService cacheUUIDService) { + // Load all uuids into a big chunky boi queue + final Queue uuidQueue = new LinkedBlockingQueue<>(); + PlotSquared.get().forEachPlotRaw(plot -> { + final Set uuids = new HashSet<>(); + uuids.add(plot.getOwnerAbs()); + uuids.addAll(plot.getMembers()); + uuids.addAll(plot.getTrusted()); + uuids.addAll(plot.getDenied()); + for (final UUID uuid : uuids) { + if (!uuidQueue.contains(uuid)) { + uuidQueue.add(uuid); + } + } + }); + PlotSquared.log(Captions.PREFIX.getTranslated() + "(UUID) " + uuidQueue.size() + " UUIDs will be cached."); + + Executors.newSingleThreadScheduledExecutor().schedule(() -> { + // Begin by reading all the SQLite cache at once + cacheUUIDService.accept(sqLiteUUIDService.getAll()); + // Now fetch names for all known UUIDs + final int totalSize = uuidQueue.size(); + int read = 0; + PlotSquared.log(Captions.PREFIX.getTranslated() + "(UUID) PlotSquared will fetch UUIDs in groups of " + + Settings.UUID.BACKGROUND_LIMIT); + final List uuidList = new ArrayList<>(Settings.UUID.BACKGROUND_LIMIT); + while (!uuidQueue.isEmpty()) { + for (int i = 0; i < Settings.UUID.BACKGROUND_LIMIT && !uuidQueue.isEmpty(); i++) { + uuidList.add(uuidQueue.poll()); + read++; + } + try { + PlotSquared.get().getBackgroundUUIDPipeline().getNames(uuidList).get(); + } catch (final InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + final double percentage = ((double) read / (double) totalSize) * 100.0D; + PlotSquared.log(Captions.PREFIX.getTranslated() + String.format("(UUID) PlotSquared has cached %.1f%% of UUIDs", percentage)); + } + PlotSquared.log(Captions.PREFIX.getTranslated() + "(UUID) PlotSquared has cached all UUIDs"); + }, 10, TimeUnit.SECONDS); + } + @Override public void onDisable() { PlotSquared.get().disable(); Bukkit.getScheduler().cancelTasks(this); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java index 6ad2f01ce..dc251bec4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java @@ -37,6 +37,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.util.UUID; import java.util.function.Consumer; @@ -121,4 +122,24 @@ public class SQLiteUUIDService implements UUIDService, Consumer getAll() { + final List mappings = new LinkedList<>(); + try (final PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM `usercache`")) { + try (final ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { + mappings.add(new UUIDMapping(UUID.fromString(resultSet.getString("uuid")), resultSet.getString("username"))); + } + } + } catch (final Exception e) { + e.printStackTrace(); + } + return mappings; + } + + } diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 4d632ff2f..6c3b78ba3 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -424,23 +424,6 @@ public class PlotSquared { } } - private void startUuidCatching() { - TaskManager.runTaskLater(() -> { - debug("Starting UUID caching"); - /*UUIDHandler.startCaching(() -> { - forEachPlotRaw(plot -> { - if (plot.hasOwner() && plot.temp != -1) { - if (UUIDHandler.getName(plot.getOwnerAbs()) == null) { - UUIDHandler.implementation.unknown.add(plot.getOwnerAbs()); - } - } - }); - startExpiryTasks(); - });*/ - // TODO: Re-implement - }, 20); - } - private void startExpiryTasks() { if (Settings.Enabled_Components.PLOT_EXPIRY) { ExpireManager.IMP = new ExpireManager();