Add UUID caching

This commit is contained in:
Alexander Söderberg 2020-05-20 15:12:09 +02:00
parent f82a111518
commit 2417dace2d
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
3 changed files with 80 additions and 18 deletions

View File

@ -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<UUID> uuidQueue = new LinkedBlockingQueue<>();
PlotSquared.get().forEachPlotRaw(plot -> {
final Set<UUID> 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<UUID> 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);

View File

@ -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<List<UUIDMapping
}
}
/**
* Read the entire cache at once
*
* @return All read mappings
*/
@NotNull public List<UUIDMapping> getAll() {
final List<UUIDMapping> 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;
}
}

View File

@ -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();