Skip to content

Commit

Permalink
Reduces storage of Island objects in the cache #2360 (#2369)
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed May 19, 2024
1 parent 290158e commit 99717f5
Showing 1 changed file with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class IslandCache {
* UUID, value is a set of islands
*/
@NonNull
private final Map<@NonNull UUID, Set<Island>> islandsByUUID;
private final Map<@NonNull UUID, Set<String>> islandsByUUID;

@NonNull
private final Map<@NonNull World, @NonNull IslandGrid> grids;
Expand Down Expand Up @@ -69,15 +70,17 @@ public void updateIsland(@NonNull Island newIsland) {
for (UUID oldMember : oldMembers) {
if (!newMembers.contains(oldMember)) {
// Member has been removed - remove island
islandsByUUID.computeIfAbsent(oldMember, k -> new HashSet<>()).remove(oldIsland);
islandsByUUID.computeIfAbsent(oldMember, k -> new HashSet<>()).remove(oldIsland.getUniqueId());
}
}
}
// Update the members with the new island object
for (UUID newMember : newMembers) {
Set<Island> set = islandsByUUID.computeIfAbsent(newMember, k -> new HashSet<>());
set.remove(oldIsland);
set.add(newIsland);
Set<String> set = islandsByUUID.computeIfAbsent(newMember, k -> new HashSet<>());
if (oldIsland != null) {
set.remove(oldIsland.getUniqueId());
}
set.add(newIsland.getUniqueId());
islandsByUUID.put(newMember, set);
}

Expand All @@ -101,7 +104,7 @@ public boolean addIsland(@NonNull Island island) {
islandsById.put(island.getUniqueId(), island);
// Only add islands to this map if they are owned
if (island.isOwned()) {
islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>()).add(island);
islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>()).add(island.getUniqueId());
island.getMemberSet().forEach(member -> addPlayer(member, island));
}
return true;
Expand All @@ -117,7 +120,7 @@ public boolean addIsland(@NonNull Island island) {
* associated per world.
*/
public void addPlayer(@NonNull UUID uuid, @NonNull Island island) {
islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).add(island);
islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).add(island.getUniqueId());
}

/**
Expand Down Expand Up @@ -150,8 +153,8 @@ public void deleteIslandFromCache(@NonNull Island island) {
}

private void removeFromIslandsByUUID(Island island) {
for (Set<Island> set : islandsByUUID.values()) {
set.removeIf(island::equals);
for (Set<String> set : islandsByUUID.values()) {
set.removeIf(island.getUniqueId()::equals);
}
}

Expand Down Expand Up @@ -203,7 +206,8 @@ public List<Island> getIslands(@NonNull World world, @NonNull UUID uuid) {
if (w == null) {
return new ArrayList<>();
}
return islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).stream().filter(island -> w.equals(island.getWorld()))
return islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).stream().map(islandsById::get)
.filter(Objects::nonNull).filter(island -> w.equals(island.getWorld()))
.sorted(Comparator.comparingLong(Island::getCreatedDate))
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -287,7 +291,8 @@ public boolean hasIsland(@NonNull World world, @NonNull UUID uuid) {
if (!islandsByUUID.containsKey(uuid)) {
return false;
}
return this.islandsByUUID.get(uuid).stream().filter(i -> world.equals(i.getWorld()))
return this.islandsByUUID.get(uuid).stream().map(islandsById::get).filter(Objects::nonNull)
.filter(i -> world.equals(i.getWorld()))
.anyMatch(i -> uuid.equals(i.getOwner()));
}

Expand All @@ -297,32 +302,38 @@ public boolean hasIsland(@NonNull World world, @NonNull UUID uuid) {
*
* @param world world
* @param uuid player's UUID
* @return list of islands player had or empty if none
* @return set of islands player had or empty if none
*/
public Set<Island> removePlayer(@NonNull World world, @NonNull UUID uuid) {
World w = Util.getWorld(world);
Set<Island> islandSet = islandsByUUID.get(uuid);
if (w == null || islandSet == null) {
return Collections.emptySet(); // Return empty list if no islands map exists for the world
World resolvedWorld = Util.getWorld(world);
Set<String> playerIslandIds = islandsByUUID.get(uuid);
Set<Island> removedIslands = new HashSet<>();

if (resolvedWorld == null || playerIslandIds == null) {
return Collections.emptySet(); // Return empty set if no islands map exists for the world
}
// Go through all the islands associated with this player in this world and
// remove the player from them.
Iterator<Island> it = islandSet.iterator();
while (it.hasNext()) {
Island island = it.next();
if (w.equals(island.getWorld())) {

// Iterate over the player's island IDs and process each associated island
Iterator<String> iterator = playerIslandIds.iterator();
while (iterator.hasNext()) {
Island island = this.getIslandById(iterator.next());
if (island != null && resolvedWorld.equals(island.getWorld())) {
removedIslands.add(island);

if (uuid.equals(island.getOwner())) {
// Player is the owner, so clear the whole island and clear the ownership
island.getMembers().clear();
island.setOwner(null);
} else {
island.removeMember(uuid);
}
// Remove this island from this set of islands associated to this player
it.remove();

// Remove this island from the set of islands associated with this player
iterator.remove();
}
}
return islandSet;

return removedIslands;
}

/**
Expand All @@ -332,9 +343,9 @@ public Set<Island> removePlayer(@NonNull World world, @NonNull UUID uuid) {
* @param uuid uuid of member to remove
*/
public void removePlayer(@NonNull Island island, @NonNull UUID uuid) {
Set<Island> islandSet = islandsByUUID.get(uuid);
Set<String> islandSet = islandsByUUID.get(uuid);
if (islandSet != null) {
islandSet.remove(island);
islandSet.remove(island.getUniqueId());
}
island.removeMember(uuid);
island.removePrimary(uuid);
Expand Down Expand Up @@ -368,7 +379,7 @@ public long size(World world) {
public void setOwner(@NonNull Island island, @Nullable UUID newOwnerUUID) {
island.setOwner(newOwnerUUID);
if (newOwnerUUID != null) {
islandsByUUID.computeIfAbsent(newOwnerUUID, k -> new HashSet<>()).add(island);
islandsByUUID.computeIfAbsent(newOwnerUUID, k -> new HashSet<>()).add(island.getUniqueId());
}
island.setRank(newOwnerUUID, RanksManager.OWNER_RANK);
islandsById.put(island.getUniqueId(), island);
Expand Down

0 comments on commit 99717f5

Please sign in to comment.