Permissions can now pre-cache all known players, and API users can get all cached players

This commit is contained in:
Marc Baloup 2024-03-01 20:17:30 +01:00
parent d7705d8904
commit ca7a51af2c
5 changed files with 59 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import java.util.UUID;
/** /**
* Represents a dummy player in the permission system, that have no specific data, only inheriting from the default * Represents a dummy player in the permission system, that have no specific data, only inheriting from the default
* groups. * groups.
* * <p>
* The current implementation provides a player named {@code default.0} with an uuid of * The current implementation provides a player named {@code default.0} with an uuid of
* {@code fffdef17-ffff-b0ff-ffff-ffffffffffff}. * {@code fffdef17-ffff-b0ff-ffff-ffffffffffff}.
* Trying to set a permission data for this player will log a warning. * Trying to set a permission data for this player will log a warning.

View File

@ -18,7 +18,7 @@ public final class PermGroup extends PermEntity {
super(name, EntityType.Group); super(name, EntityType.Group);
} }
@Override @Override
protected CachedGroup getBackendEntity() { /* package */ CachedGroup getBackendEntity() {
return Permissions.backendReader.getCachedGroup(name); return Permissions.backendReader.getCachedGroup(name);
} }

View File

@ -17,7 +17,7 @@ public sealed class PermPlayer extends PermEntity permits DefaultPlayer {
playerId = id; playerId = id;
} }
@Override @Override
protected CachedPlayer getBackendEntity() { /* package */ CachedPlayer getBackendEntity() {
return Permissions.backendReader.getCachedPlayer(playerId); return Permissions.backendReader.getCachedPlayer(playerId);
} }

View File

@ -104,9 +104,21 @@ public class Permissions {
return new PermPlayer(playerId); return new PermPlayer(playerId);
} }
/**
* Gets the permission object of all players stored in cache.
* @return the permission player object.
* @throws IllegalStateException if the permission system was not initialized properly.
*/
public static List<PermPlayer> getCachedPlayers() {
checkInitialized();
return backendReader.getAllCachedPlayers().stream()
.map(cp -> getPlayer(cp.playerId))
.toList();
}
/** /**
* Gets a dummy permission player object, that have no specific data, only inheriting from the default groups. * Gets a dummy permission player object, that have no specific data, only inheriting from the default groups.
* * <p>
* The current implementation provides a player named {@code default.0} with an uuid of * The current implementation provides a player named {@code default.0} with an uuid of
* {@code fffdef17-ffff-b0ff-ffff-ffffffffffff}. * {@code fffdef17-ffff-b0ff-ffff-ffffffffffff}.
* Trying to set a permission data for this player will log a warning. * Trying to set a permission data for this player will log a warning.
@ -137,6 +149,16 @@ public class Permissions {
t.start(); t.start();
} }
/**
* Asks the permission system to preventively and asynchronously cache the data of all players.
* This method can be called before doing any operation involving most or all the permission data.
* @throws IllegalStateException if the permission system was not initialized properly.
*/
public static void precachePlayers() {
checkInitialized();
backendReader.precacheAllPlayers();
}
/** /**
* Gets the permission group object. * Gets the permission group object.
* @param name the name of the group. * @param name the name of the group.

View File

@ -63,6 +63,31 @@ import fr.pandacube.lib.util.log.Log;
} }
} }
/* package */ synchronized List<CachedPlayer> getAllCachedPlayers() {
return new ArrayList<>(usersCache.asMap().values());
}
/* package */ synchronized void precacheAllPlayers() {
try {
DB.getAll(SQLPermissions.class, SQLPermissions.type.eq(EntityType.User.getCode()))
.stream()
.collect(Collectors.groupingBy(el -> el.get(SQLPermissions.name),
Collectors.toCollection(() -> new SQLElementList<SQLPermissions>())
)
)
.forEach((idStr, pData) -> {
try {
UUID pId = UUID.fromString(idStr);
usersCache.put(pId, initPlayer(pId, pData));
} catch (Exception e) {
Log.severe("Error caching player permission data (name=\"" + idStr + "\")", e);
}
});
} catch (DBException e) {
throw new RuntimeException(e);
}
}
private CachedPlayer initPlayer(UUID playerId) throws DBException { private CachedPlayer initPlayer(UUID playerId) throws DBException {
if (playerId.equals(DEFAULT_PLAYER.playerId)) if (playerId.equals(DEFAULT_PLAYER.playerId))
return DEFAULT_PLAYER; return DEFAULT_PLAYER;
@ -72,6 +97,11 @@ import fr.pandacube.lib.util.log.Log;
.and(SQLPermissions.name.like(playerId.toString())) .and(SQLPermissions.name.like(playerId.toString()))
); );
return initPlayer(playerId, playerData);
}
private CachedPlayer initPlayer(UUID playerId, SQLElementList<SQLPermissions> playerData) {
Map<String, List<SQLPermissions>> playerRawData = playerData.stream() Map<String, List<SQLPermissions>> playerRawData = playerData.stream()
.collect( .collect(
Collectors.groupingBy(e -> e.get(SQLPermissions.key), Collectors.groupingBy(e -> e.get(SQLPermissions.key),