Further caching improvements

This commit is contained in:
William committed 2023-03-21 20:20:08 +00:00
1 parent a959aa9519
commit 8923b44c92
12 files changed
+212 -46

No files matched your search

@@ -99,7 +99,7 @@ public class BlueMapHook extends MapHook {
}
@Override
public CompletableFuture<Void> clearHomes(@NotNull User user) {
public void clearHomes(@NotNull User user) {
BlueMapAPI.getInstance().ifPresent((BlueMapAPI blueMapAPI) -> blueMapAPI.getWorlds()
.forEach(blueMapWorld -> blueMapWorld.getMaps()
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
@@ -108,7 +108,7 @@ public class BlueMapHook extends MapHook {
return markerSet;
}))));
return CompletableFuture.completedFuture(null);
CompletableFuture.completedFuture(null);
}
@Override
@@ -143,7 +143,7 @@ public class BlueMapHook extends MapHook {
}
@Override
public CompletableFuture<Void> clearWarps() {
public void clearWarps() {
BlueMapAPI.getInstance().ifPresent((BlueMapAPI blueMapAPI) -> blueMapAPI.getWorlds()
.forEach(blueMapWorld -> blueMapWorld.getMaps()
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
@@ -152,7 +152,7 @@ public class BlueMapHook extends MapHook {
return markerSet;
}))));
return CompletableFuture.completedFuture(null);
CompletableFuture.completedFuture(null);
}
/**
@@ -67,9 +67,8 @@ public abstract class MapHook extends PluginHook {
* Clears homes owned by a player from the map
*
* @param user the player whose homes to clear
* @return a {@link CompletableFuture} that completes when the homes have been cleared
*/
public abstract CompletableFuture<Void> clearHomes(@NotNull User user);
public abstract void clearHomes(@NotNull User user);
/**
* Update a warp, adding it to the map if it exists, or updating it on the map if it doesn't
@@ -88,10 +87,8 @@ public abstract class MapHook extends PluginHook {
/**
* Clears all warps from the map
*
* @return a {@link CompletableFuture} that completes when the warps have been cleared
*/
public abstract CompletableFuture<Void> clearWarps();
public abstract void clearWarps();
/**
* Returns if the position is valid to be set on this server
@@ -40,20 +40,24 @@ public class EventListener {
if (plugin.getSettings().isCrossServer()) {
this.handleInboundTeleport(onlineUser);
// Update caches
if (plugin.getOnlineUsers().size() == 1) {
plugin.getManager().homes().updatePublicHomeCache();
plugin.getManager().warps().updateWarpCache();
}
// Update the player list
plugin.runLater(() -> {
if (plugin.getOnlineUsers().size() == 1) {
Message.builder()
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.type(Message.Type.REQUEST_PLAYER_LIST)
.build().send(plugin.getMessenger(), onlineUser);
}
// Send a player list update to other servers
sendPlayerListUpdates(onlineUser);
plugin.getOnlineUsers().stream()
.filter(user -> !user.equals(onlineUser))
.findAny()
.ifPresent(this::sendPlayerListUpdates);
// Request updated player lists from other servers
plugin.getGlobalPlayerList().clear();
Message.builder()
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.type(Message.Type.REQUEST_PLAYER_LIST)
.build().send(plugin.getMessenger(), onlineUser);
}, plugin.getSettings().getBrokerType() == Broker.Type.PLUGIN_MESSAGE ? 40L : 0L);
}
@@ -2,6 +2,8 @@ package net.william278.huskhomes.manager;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.PositionMeta;
@@ -12,17 +14,19 @@ import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class HomesManager {
private final HuskHomes plugin;
private final List<Home> publicHomes;
private final Map<String, List<Home>> userHomes;
private final ConcurrentLinkedQueue<Home> publicHomes;
private final ConcurrentHashMap<String, ConcurrentLinkedQueue<Home>> userHomes;
protected HomesManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.publicHomes = plugin.getDatabase().getPublicHomes();
this.userHomes = new HashMap<>();
this.publicHomes = new ConcurrentLinkedQueue<>(plugin.getDatabase().getPublicHomes());
this.userHomes = new ConcurrentHashMap<>();
plugin.runAsync(() -> plugin.getOnlineUsers()
.forEach(this::cacheUserHomes));
}
@@ -38,7 +42,56 @@ public class HomesManager {
}
public void cacheUserHomes(@NotNull User user) {
userHomes.put(user.getUuid().toString(), plugin.getDatabase().getHomes(user));
userHomes.put(user.getUsername(), new ConcurrentLinkedQueue<>(plugin.getDatabase().getHomes(user)));
}
public void cacheHome(@NotNull Home home, boolean propagate) {
userHomes.computeIfPresent(home.getOwner().getUsername(), (k, v) -> {
v.remove(home);
v.add(home);
return v;
});
if (publicHomes.remove(home) && !home.isPublic()) {
plugin.getMapHook().ifPresent(hook -> hook.removeHome(home));
}
if (home.isPublic()) {
publicHomes.add(home);
plugin.getMapHook().ifPresent(hook -> hook.updateHome(home));
}
if (propagate) {
propagateCacheUpdate(home.getUuid());
}
}
public void unCacheHome(@NotNull UUID homeId, boolean propagate) {
userHomes.values().forEach(homes -> homes.removeIf(home -> home.getUuid().equals(homeId)));
publicHomes.removeIf(home -> {
if (home.getUuid().equals(homeId)) {
plugin.getMapHook().ifPresent(hook -> hook.removeHome(home));
return true;
}
return false;
});
if (propagate) {
this.propagateCacheUpdate(homeId);
}
}
private void propagateCacheUpdate(@NotNull UUID homeId) {
if (plugin.getSettings().isCrossServer()) {
plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder()
.type(Message.Type.UPDATE_HOME)
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.payload(Payload.withString(homeId.toString()))
.build().send(plugin.getMessenger(), user));
}
}
public void updatePublicHomeCache() {
plugin.getDatabase().getPublicHomes().forEach(home -> cacheHome(home, false));
}
public void removeUserHomes(@NotNull User user) {
@@ -99,6 +152,7 @@ public class HomesManager {
})
.orElse(new Home(position, new PositionMeta(name, ""), owner));
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void createHome(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) throws ValidationException {
@@ -116,10 +170,19 @@ public class HomesManager {
public void deleteHome(@NotNull Home home) {
plugin.getDatabase().deleteHome(home.getUuid());
this.unCacheHome(home.getUuid(), true);
}
public int deleteAllHomes(@NotNull User owner) {
return plugin.getDatabase().deleteAllHomes(owner);
final int deleted = plugin.getDatabase().deleteAllHomes(owner);
userHomes.computeIfPresent(owner.getUsername(), (k, v) -> {
v.clear();
return v;
});
publicHomes.removeIf(h -> h.getOwner().getUuid().equals(owner.getUuid()));
plugin.getMapHook().ifPresent(hook -> hook.clearHomes(owner));
plugin.getManager().propagateCacheUpdate();
return deleted;
}
public void setHomePosition(@NotNull User owner, @NotNull String name, @NotNull Position position) throws ValidationException {
@@ -134,6 +197,7 @@ public class HomesManager {
public void setHomePosition(@NotNull Home home, @NotNull Position position) throws ValidationException {
home.update(position);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void setHomeName(@NotNull User owner, @NotNull String name, @NotNull String newName) throws ValidationException {
@@ -152,6 +216,7 @@ public class HomesManager {
home.getMeta().setName(newName);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void setHomeDescription(@NotNull User owner, @NotNull String name, @NotNull String description) throws ValidationException {
@@ -170,6 +235,7 @@ public class HomesManager {
home.getMeta().setDescription(description);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void setHomePrivacy(@NotNull User owner, @NotNull String name, boolean isPublic) throws ValidationException {
@@ -199,6 +265,7 @@ public class HomesManager {
home.setPublic(isPublic);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
}
@@ -1,15 +1,18 @@
package net.william278.huskhomes.manager;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.network.Message;
import org.jetbrains.annotations.NotNull;
public class Manager {
private final HuskHomes plugin;
private final HomesManager homes;
private final WarpsManager warps;
private final RequestsManager requests;
public Manager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.homes = new HomesManager(plugin);
this.warps = new WarpsManager(plugin);
this.requests = new RequestsManager(plugin);
@@ -29,4 +32,13 @@ public class Manager {
public RequestsManager requests() {
return requests;
}
// Update caches on all servers
protected void propagateCacheUpdate() {
plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder()
.type(Message.Type.UPDATE_CACHES)
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.build().send(plugin.getMessenger(), user));
}
}
@@ -1,6 +1,9 @@
package net.william278.huskhomes.manager;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.MapHook;
import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.position.Warp;
@@ -9,14 +12,54 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
public class WarpsManager {
private final HuskHomes plugin;
private final List<Warp> warps;
private final ConcurrentLinkedQueue<Warp> warps;
protected WarpsManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.warps = plugin.getDatabase().getWarps();
this.warps = new ConcurrentLinkedQueue<>(plugin.getDatabase().getWarps());
}
public void cacheWarp(@NotNull Warp warp, boolean propagate) {
warps.remove(warp);
warps.add(warp);
plugin.getMapHook().ifPresent(hook -> hook.updateWarp(warp));
if (propagate) {
this.propagateCacheUpdate(warp.getUuid());
}
}
public void unCacheWarp(@NotNull UUID warpId, boolean propagate) {
warps.removeIf(warp -> {
if (warp.getUuid().equals(warpId)) {
plugin.getMapHook().ifPresent(hook -> hook.removeWarp(warp));
return true;
}
return false;
});
if (propagate) {
this.propagateCacheUpdate(warpId);
}
}
private void propagateCacheUpdate(@NotNull UUID warpId) {
if (plugin.getSettings().isCrossServer()) {
plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder()
.type(Message.Type.UPDATE_WARP)
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.payload(Payload.withString(warpId.toString()))
.build().send(plugin.getMessenger(), user));
}
}
public void updateWarpCache() {
plugin.getDatabase().getWarps().forEach(warp -> cacheWarp(warp, false));
}
/**
@@ -50,6 +93,7 @@ public class WarpsManager {
})
.orElse(new Warp(position, new PositionMeta(name, "")));
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
public void createWarp(@NotNull String name, @NotNull Position position) throws ValidationException {
@@ -67,10 +111,15 @@ public class WarpsManager {
public void deleteWarp(@NotNull Warp warp) {
plugin.getDatabase().deleteWarp(warp.getUuid());
this.unCacheWarp(warp.getUuid(), true);
}
public int deleteAllWarps() {
return plugin.getDatabase().deleteAllWarps();
final int deleted = plugin.getDatabase().deleteAllWarps();
warps.clear();
plugin.getMapHook().ifPresent(MapHook::clearWarps);
plugin.getManager().propagateCacheUpdate();
return deleted;
}
public void relocateWarp(@NotNull String name, @NotNull Position position) throws ValidationException {
@@ -85,6 +134,7 @@ public class WarpsManager {
public void relocateWarp(@NotNull Warp warp, @NotNull Position position) {
warp.update(position);
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
public void renameWarp(@NotNull String name, @NotNull String newName) throws ValidationException {
@@ -103,6 +153,7 @@ public class WarpsManager {
warp.getMeta().setName(newName);
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
public void setWarpDescription(@NotNull String name, @NotNull String description) throws ValidationException {
@@ -121,6 +172,7 @@ public class WarpsManager {
warp.getMeta().setDescription(description);
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
}
@@ -1,11 +1,16 @@
package net.william278.huskhomes.network;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.UUID;
public abstract class Broker {
protected final HuskHomes plugin;
@@ -63,6 +68,30 @@ public abstract class Broker {
case PLAYER_LIST -> message.getPayload()
.getStringList()
.ifPresent(players -> plugin.setPlayerList(message.getSourceServer(), players));
case UPDATE_HOME -> message.getPayload().getString()
.map(UUID::fromString)
.ifPresent(homeId -> {
final Optional<Home> optionalHome = plugin.getDatabase().getHome(homeId);
if (optionalHome.isPresent()) {
plugin.getManager().homes().cacheHome(optionalHome.get(), false);
} else {
plugin.getManager().homes().unCacheHome(homeId, false);
}
});
case UPDATE_WARP -> message.getPayload().getString()
.map(UUID::fromString)
.ifPresent(warpId -> {
final Optional<Warp> optionalWarp = plugin.getDatabase().getWarp(warpId);
if (optionalWarp.isPresent()) {
plugin.getManager().warps().cacheWarp(optionalWarp.get(), false);
} else {
plugin.getManager().warps().unCacheWarp(warpId, false);
}
});
case UPDATE_CACHES -> {
plugin.getManager().homes().updatePublicHomeCache();
plugin.getManager().warps().updateWarpCache();
}
}
}
@@ -143,6 +143,9 @@ public class Message {
TELEPORT_REQUEST_RESPONSE,
REQUEST_PLAYER_LIST,
PLAYER_LIST,
UPDATE_HOME,
UPDATE_WARP,
UPDATE_CACHES,
}
public enum Scope {
@@ -33,12 +33,6 @@ public abstract class SavedPosition extends Position implements Comparable<Saved
this.uuid = UUID.randomUUID();
}
// Compare based on names for alphabetical sorting
@Override
public int compareTo(@NotNull SavedPosition o) {
return this.getMeta().getName().compareTo(o.getMeta().getName());
}
/**
* Metadata about this position (name, description)
*/
@@ -66,4 +60,19 @@ public abstract class SavedPosition extends Position implements Comparable<Saved
public UUID getUuid() {
return uuid;
}
// Compare based on names for alphabetical sorting
@Override
public int compareTo(@NotNull SavedPosition o) {
return this.getMeta().getName().compareTo(o.getMeta().getName());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof SavedPosition) {
return this.getUuid().equals(((SavedPosition) obj).getUuid());
}
return false;
}
}
@@ -16,21 +16,16 @@ public class TeleportRequest {
@Expose
@SerializedName("requester_name")
private String requesterName;
@Expose
@SerializedName("requester_position")
private Position requesterPosition;
@Expose
@SerializedName("expiry_time")
private long expiryTime;
@Expose
private Type type;
@Expose
private Status status;
@Expose
private String recipientName;
@@ -63,7 +63,7 @@ public class TimedTeleport extends Teleport {
// Run the warmup
final AtomicInteger warmupTaskId = new AtomicInteger();
final Runnable delayRunnable = (() -> {
final Runnable countdownRunnable = (() -> {
// Display countdown action bar message
if (timeLeft > 0) {
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP)
@@ -82,7 +82,7 @@ public class TimedTeleport extends Teleport {
plugin.cancelTask(warmupTaskId.get());
}
});
warmupTaskId.set(plugin.runAsyncRepeating(delayRunnable, 20));
warmupTaskId.set(plugin.runAsyncRepeating(countdownRunnable, 20L));
});
}