Refactored SQLFetchOps#getPlayerContainer to a query

This commit is contained in:
Rsl1122 2019-01-25 20:27:44 +02:00
parent 6023b9e88c
commit d4b5731e2e
4 changed files with 151 additions and 80 deletions

View File

@ -17,9 +17,11 @@
package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.store.containers.NetworkContainer;
import com.djrapitops.plan.data.store.containers.PlayerContainer;
import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.sql.queries.containers.NetworkContainerQuery;
import com.djrapitops.plan.db.sql.queries.containers.PlayerContainerQuery;
import com.djrapitops.plan.db.sql.queries.containers.ServerContainerQuery;
import java.util.UUID;
@ -39,7 +41,7 @@ public class ContainerFetchQueries {
* Used to get a NetworkContainer, some limitations apply to values returned by DataContainer keys.
*
* @return a new NetworkContainer.
* @see com.djrapitops.plan.db.sql.queries.containers.NetworkContainerQuery
* @see NetworkContainerQuery
*/
public static Query<NetworkContainer> fetchNetworkContainer() {
return new NetworkContainerQuery();
@ -47,14 +49,26 @@ public class ContainerFetchQueries {
/**
* Used to get a ServerContainer, some limitations apply to values returned by DataContainer keys.
* <p>
*
* @param serverUUID UUID of the Server.
* @return a new ServerContainer.
* @see com.djrapitops.plan.db.sql.queries.containers.ServerContainerQuery
* @see ServerContainerQuery
*/
public static Query<ServerContainer> getServerContainer(UUID serverUUID) {
public static Query<ServerContainer> fetchServerContainer(UUID serverUUID) {
return new ServerContainerQuery(serverUUID);
}
/**
* Used to get a PlayerContainer of a specific player.
* <p>
* Blocking methods are not called until DataContainer getter methods are called.
*
* @param playerUUID UUID of the player.
* @return a new PlayerContainer.
* @see PlayerContainerQuery
*/
public static Query<PlayerContainer> fetchPlayerContainer(UUID playerUUID) {
return new PlayerContainerQuery(playerUUID);
}
}

View File

@ -55,7 +55,7 @@ public class NetworkContainerQuery implements Query<NetworkContainer> {
}
UUID serverUUID = proxyInformation.get().getUuid();
ServerContainer container = db.query(ContainerFetchQueries.getServerContainer(serverUUID));
ServerContainer container = db.query(ContainerFetchQueries.fetchServerContainer(serverUUID));
container.putCachingSupplier(ServerKeys.PLAYERS, db.fetch()::getAllPlayerContainers);
container.putCachingSupplier(ServerKeys.TPS, db.getTpsTable()::getNetworkOnlineData);
container.putSupplier(ServerKeys.WORLD_TIMES, null); // Additional Session information not supported

View File

@ -0,0 +1,130 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.sql.queries.containers;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.container.UserInfo;
import com.djrapitops.plan.data.store.containers.DataContainer;
import com.djrapitops.plan.data.store.containers.PerServerContainer;
import com.djrapitops.plan.data.store.containers.PlayerContainer;
import com.djrapitops.plan.data.store.keys.PerServerKeys;
import com.djrapitops.plan.data.store.keys.PlayerKeys;
import com.djrapitops.plan.data.store.keys.SessionKeys;
import com.djrapitops.plan.data.store.mutators.PerServerMutator;
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Used to get a PlayerContainer of a specific player.
* <p>
* Blocking methods are not called until DataContainer getter methods are called.
*
* @author Rsl1122
*/
public class PlayerContainerQuery implements Query<PlayerContainer> {
private final UUID uuid;
public PlayerContainerQuery(UUID uuid) {
this.uuid = uuid;
}
@Override
public PlayerContainer executeQuery(SQLDB db) {
PlayerContainer container = new PlayerContainer();
container.putRawData(PlayerKeys.UUID, uuid);
container.putAll(db.getUsersTable().getUserInformation(uuid));
container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.getGeoInfoTable().getGeoInfo(uuid));
container.putCachingSupplier(PlayerKeys.PING, () -> db.getPingTable().getPing(uuid));
container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.getNicknamesTable().getNicknameInformation(uuid));
container.putCachingSupplier(PlayerKeys.PER_SERVER, () -> getPerServerData(db));
container.putSupplier(PlayerKeys.BANNED, () -> new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).isBanned());
container.putSupplier(PlayerKeys.OPERATOR, () -> new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).isOperator());
container.putCachingSupplier(PlayerKeys.SESSIONS, () -> {
List<Session> sessions = new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).flatMapSessions();
container.getValue(PlayerKeys.ACTIVE_SESSION).ifPresent(sessions::add);
return sessions;
}
);
container.putCachingSupplier(PlayerKeys.WORLD_TIMES, () ->
{
WorldTimes worldTimes = new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).flatMapWorldTimes();
container.getValue(PlayerKeys.ACTIVE_SESSION).ifPresent(session -> worldTimes.add(
session.getValue(SessionKeys.WORLD_TIMES).orElse(new WorldTimes(new HashMap<>())))
);
return worldTimes;
});
container.putSupplier(PlayerKeys.LAST_SEEN, () -> SessionsMutator.forContainer(container).toLastSeen());
container.putSupplier(PlayerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList());
container.putSupplier(PlayerKeys.PLAYER_DEATHS, () -> SessionsMutator.forContainer(container).toPlayerDeathList());
container.putSupplier(PlayerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(PlayerKeys.PLAYER_KILLS).size());
container.putSupplier(PlayerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount());
container.putSupplier(PlayerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount());
return container;
}
private PerServerContainer getPerServerData(SQLDB db) {
PerServerContainer perServerContainer = new PerServerContainer();
Map<UUID, UserInfo> allUserInfo = db.getUserInfoTable().getAllUserInfo(uuid);
for (Map.Entry<UUID, UserInfo> entry : allUserInfo.entrySet()) {
UUID serverUUID = entry.getKey();
UserInfo info = entry.getValue();
DataContainer container = perServerContainer.getOrDefault(serverUUID, new DataContainer());
container.putRawData(PlayerKeys.REGISTERED, info.getRegistered());
container.putRawData(PlayerKeys.BANNED, info.isBanned());
container.putRawData(PlayerKeys.OPERATOR, info.isOperator());
perServerContainer.put(serverUUID, container);
}
Map<UUID, List<Session>> sessions = db.getSessionsTable().getSessions(uuid);
for (Map.Entry<UUID, List<Session>> entry : sessions.entrySet()) {
UUID serverUUID = entry.getKey();
List<Session> serverSessions = entry.getValue();
DataContainer container = perServerContainer.getOrDefault(serverUUID, new DataContainer());
container.putRawData(PerServerKeys.SESSIONS, serverSessions);
container.putSupplier(PerServerKeys.LAST_SEEN, () -> SessionsMutator.forContainer(container).toLastSeen());
container.putSupplier(PerServerKeys.WORLD_TIMES, () -> SessionsMutator.forContainer(container).toTotalWorldTimes());
container.putSupplier(PerServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList());
container.putSupplier(PerServerKeys.PLAYER_DEATHS, () -> SessionsMutator.forContainer(container).toPlayerDeathList());
container.putSupplier(PerServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(PerServerKeys.PLAYER_KILLS).size());
container.putSupplier(PerServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount());
container.putSupplier(PerServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount());
perServerContainer.put(serverUUID, container);
}
return perServerContainer;
}
}

View File

@ -50,7 +50,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public ServerContainer getServerContainer(UUID serverUUID) {
return db.query(ContainerFetchQueries.getServerContainer(serverUUID));
return db.query(ContainerFetchQueries.fetchServerContainer(serverUUID));
}
@Override
@ -235,80 +235,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public PlayerContainer getPlayerContainer(UUID uuid) {
PlayerContainer container = new PlayerContainer();
container.putRawData(PlayerKeys.UUID, uuid);
container.putAll(usersTable.getUserInformation(uuid));
container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> geoInfoTable.getGeoInfo(uuid));
container.putCachingSupplier(PlayerKeys.PING, () -> pingTable.getPing(uuid));
container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> nicknamesTable.getNicknameInformation(uuid));
container.putCachingSupplier(PlayerKeys.PER_SERVER, () -> getPerServerData(uuid));
container.putSupplier(PlayerKeys.BANNED, () -> new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).isBanned());
container.putSupplier(PlayerKeys.OPERATOR, () -> new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).isOperator());
container.putCachingSupplier(PlayerKeys.SESSIONS, () -> {
List<Session> sessions = new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).flatMapSessions();
container.getValue(PlayerKeys.ACTIVE_SESSION).ifPresent(sessions::add);
return sessions;
}
);
container.putCachingSupplier(PlayerKeys.WORLD_TIMES, () ->
{
WorldTimes worldTimes = new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).flatMapWorldTimes();
container.getValue(PlayerKeys.ACTIVE_SESSION).ifPresent(session -> worldTimes.add(
session.getValue(SessionKeys.WORLD_TIMES).orElse(new WorldTimes(new HashMap<>())))
);
return worldTimes;
});
container.putSupplier(PlayerKeys.LAST_SEEN, () -> SessionsMutator.forContainer(container).toLastSeen());
container.putSupplier(PlayerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList());
container.putSupplier(PlayerKeys.PLAYER_DEATHS, () -> SessionsMutator.forContainer(container).toPlayerDeathList());
container.putSupplier(PlayerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(PlayerKeys.PLAYER_KILLS).size());
container.putSupplier(PlayerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount());
container.putSupplier(PlayerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount());
return container;
}
private PerServerContainer getPerServerData(UUID uuid) {
PerServerContainer perServerContainer = new PerServerContainer();
Map<UUID, UserInfo> allUserInfo = userInfoTable.getAllUserInfo(uuid);
for (Map.Entry<UUID, UserInfo> entry : allUserInfo.entrySet()) {
UUID serverUUID = entry.getKey();
UserInfo info = entry.getValue();
DataContainer container = perServerContainer.getOrDefault(serverUUID, new DataContainer());
container.putRawData(PlayerKeys.REGISTERED, info.getRegistered());
container.putRawData(PlayerKeys.BANNED, info.isBanned());
container.putRawData(PlayerKeys.OPERATOR, info.isOperator());
perServerContainer.put(serverUUID, container);
}
Map<UUID, List<Session>> sessions = sessionsTable.getSessions(uuid);
for (Map.Entry<UUID, List<Session>> entry : sessions.entrySet()) {
UUID serverUUID = entry.getKey();
List<Session> serverSessions = entry.getValue();
DataContainer container = perServerContainer.getOrDefault(serverUUID, new DataContainer());
container.putRawData(PerServerKeys.SESSIONS, serverSessions);
container.putSupplier(PerServerKeys.LAST_SEEN, () -> SessionsMutator.forContainer(container).toLastSeen());
container.putSupplier(PerServerKeys.WORLD_TIMES, () -> SessionsMutator.forContainer(container).toTotalWorldTimes());
container.putSupplier(PerServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList());
container.putSupplier(PerServerKeys.PLAYER_DEATHS, () -> SessionsMutator.forContainer(container).toPlayerDeathList());
container.putSupplier(PerServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(PerServerKeys.PLAYER_KILLS).size());
container.putSupplier(PerServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount());
container.putSupplier(PerServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount());
perServerContainer.put(serverUUID, container);
}
return perServerContainer;
return db.query(ContainerFetchQueries.fetchPlayerContainer(uuid));
}
@Override