Refactored some Container fetch operations into queries:

- FetchOperations#getNetworkContainer refactored into a query
- FetchOperations#getServerContainer refactored into a query
- Deprecated SQLDB#get[TableName] methods
- Deprecated SQLDB#getAllTables & SQLDB#getAllTablesRemoveOrder
- Deprecated All Operations interfaces

Everything deprecated in this commit will be removed.
This commit is contained in:
Rsl1122 2019-01-25 19:55:38 +02:00
parent afa0715416
commit 6f39b19470
8 changed files with 180 additions and 82 deletions

View File

@ -249,6 +249,7 @@ public abstract class SQLDB extends AbstractDatabase {
* <p>
* Updates table columns to latest schema.
*/
@Deprecated
void createTables() throws DBInitException {
executeTransaction(new CreateTablesTransaction());
}
@ -258,6 +259,7 @@ public abstract class SQLDB extends AbstractDatabase {
*
* @return Table array.
*/
@Deprecated
public Table[] getAllTables() {
return new Table[]{
serverTable, usersTable, userInfoTable, geoInfoTable,
@ -272,6 +274,7 @@ public abstract class SQLDB extends AbstractDatabase {
*
* @return Tables in the order the data should be removed in.
*/
@Deprecated
public Table[] getAllTablesInRemoveOrder() {
return new Table[]{
settingsTable, geoInfoTable, nicknamesTable, killsTable,
@ -409,88 +412,108 @@ public abstract class SQLDB extends AbstractDatabase {
transaction.executeTransaction(this);
}
@Deprecated
public UsersTable getUsersTable() {
return usersTable;
}
@Deprecated
public SessionsTable getSessionsTable() {
return sessionsTable;
}
@Deprecated
public KillsTable getKillsTable() {
return killsTable;
}
@Deprecated
public GeoInfoTable getGeoInfoTable() {
return geoInfoTable;
}
@Deprecated
public NicknamesTable getNicknamesTable() {
return nicknamesTable;
}
@Deprecated
public CommandUseTable getCommandUseTable() {
return commandUseTable;
}
@Deprecated
public TPSTable getTpsTable() {
return tpsTable;
}
@Deprecated
public SecurityTable getSecurityTable() {
return securityTable;
}
@Deprecated
public WorldTable getWorldTable() {
return worldTable;
}
@Deprecated
public WorldTimesTable getWorldTimesTable() {
return worldTimesTable;
}
@Deprecated
public ServerTable getServerTable() {
return serverTable;
}
@Deprecated
public UserInfoTable getUserInfoTable() {
return userInfoTable;
}
@Deprecated
public PingTable getPingTable() {
return pingTable;
}
@Deprecated
public SettingsTable getSettingsTable() {
return settingsTable;
}
@Override
@Deprecated
public CheckOperations check() {
return checkOps;
}
@Override
@Deprecated
public FetchOperations fetch() {
return fetchOps;
}
@Override
@Deprecated
public RemoveOperations remove() {
return removeOps;
}
@Override
@Deprecated
public SearchOperations search() {
return searchOps;
}
@Override
@Deprecated
public CountOperations count() {
return countOps;
}
@Override
@Deprecated
public SaveOperations save() {
return saveOps;
}

View File

@ -0,0 +1,135 @@
/*
* 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;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.container.TPS;
import com.djrapitops.plan.data.store.containers.NetworkContainer;
import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.data.store.keys.NetworkKeys;
import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.data.store.mutators.PlayersMutator;
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.data.store.objects.DateObj;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.info.server.Server;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* Static method class for queries that return some kind of {@link com.djrapitops.plan.data.store.containers.DataContainer}.
*
* @author Rsl1122
*/
public class ContainerFetchQueries {
private ContainerFetchQueries() {
/* Static method class */
}
public static Query<NetworkContainer> fetchNetworkContainer() {
return db -> {
ServerContainer bungeeContainer = db.query(getBungeeServerContainer());
NetworkContainer networkContainer = db.getNetworkContainerFactory().forBungeeContainer(bungeeContainer);
networkContainer.putCachingSupplier(NetworkKeys.BUKKIT_SERVERS, () ->
db.query(LargeFetchQueries.fetchPlanServerInformation()).values()
.stream().filter(Server::isNotProxy).collect(Collectors.toSet())
);
return networkContainer;
};
}
private static Query<ServerContainer> getBungeeServerContainer() {
return db -> {
Optional<Server> proxyInformation = db.query(OptionalFetchQueries.proxyServerInformation());
if (!proxyInformation.isPresent()) {
return new ServerContainer();
}
UUID serverUUID = proxyInformation.get().getUuid();
ServerContainer container = db.query(getServerContainer(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
container.putSupplier(ServerKeys.PLAYER_KILLS, null);
container.putSupplier(ServerKeys.PLAYER_KILL_COUNT, null);
return container;
};
}
public static Query<ServerContainer> getServerContainer(UUID serverUUID) {
return db -> {
ServerContainer container = new ServerContainer();
Optional<Server> serverInfo = db.getServerTable().getServerInfo(serverUUID);
if (!serverInfo.isPresent()) {
return container;
}
container.putRawData(ServerKeys.SERVER_UUID, serverUUID);
container.putRawData(ServerKeys.NAME, serverInfo.get().getName());
container.putCachingSupplier(ServerKeys.PLAYERS, () -> db.fetch().getPlayerContainers(serverUUID));
container.putSupplier(ServerKeys.PLAYER_COUNT, () -> container.getUnsafe(ServerKeys.PLAYERS).size());
container.putCachingSupplier(ServerKeys.TPS, () -> db.getTpsTable().getTPSData(serverUUID));
container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings());
container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () -> {
Optional<TPS> allTimePeak = db.getTpsTable().getAllTimePeak(serverUUID);
if (allTimePeak.isPresent()) {
TPS peak = allTimePeak.get();
return new DateObj<>(peak.getDate(), peak.getPlayers());
}
return null;
});
container.putCachingSupplier(ServerKeys.RECENT_PEAK_PLAYERS, () -> {
long twoDaysAgo = System.currentTimeMillis() - (TimeUnit.DAYS.toMillis(2L));
Optional<TPS> lastPeak = db.getTpsTable().getPeakPlayerCount(serverUUID, twoDaysAgo);
if (lastPeak.isPresent()) {
TPS peak = lastPeak.get();
return new DateObj<>(peak.getDate(), peak.getPlayers());
}
return null;
});
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.getCommandUseTable().getCommandUse(serverUUID));
container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.getWorldTimesTable().getWorldTimesOfServer(serverUUID));
// Calculating getters
container.putCachingSupplier(ServerKeys.OPERATORS, () -> PlayersMutator.forContainer(container).operators());
container.putCachingSupplier(ServerKeys.SESSIONS, () -> {
List<Session> sessions = PlayersMutator.forContainer(container).getSessions();
if (serverUUID.equals(serverInfo.get().getUuid())) {
sessions.addAll(SessionCache.getActiveSessions().values());
}
return sessions;
});
container.putCachingSupplier(ServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList());
container.putCachingSupplier(ServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(ServerKeys.PLAYER_KILLS).size());
container.putCachingSupplier(ServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount());
container.putCachingSupplier(ServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount());
return container;
};
}
}

View File

@ -18,15 +18,20 @@ package com.djrapitops.plan.system.database.databases.operation;
import java.util.UUID;
@Deprecated
public interface CheckOperations {
@Deprecated
boolean isPlayerRegistered(UUID player);
@Deprecated
boolean isPlayerRegistered(UUID player, UUID server);
@Deprecated
boolean doesWebUserExists(String username);
@Deprecated
boolean isPlayerRegisteredOnThisServer(UUID player);
@Deprecated
boolean isServerInDatabase(UUID serverUUID);
}

View File

@ -18,8 +18,9 @@ package com.djrapitops.plan.system.database.databases.operation;
import java.util.UUID;
@Deprecated
public interface CountOperations {
@Deprecated
int getServerPlayerCount(UUID server);
}

View File

@ -63,6 +63,8 @@ public interface FetchOperations {
@Deprecated
ServerContainer getServerContainer(UUID serverUUID);
List<PlayerContainer> getPlayerContainers(UUID serverUUID);
/**
* Used to get PlayerContainers of all players on the network, some limitations apply to DataContainer keys.
* <p>

View File

@ -16,7 +16,8 @@
*/
package com.djrapitops.plan.system.database.databases.operation;
@Deprecated
public interface RemoveOperations {
@Deprecated
void webUser(String name);
}

View File

@ -18,8 +18,9 @@ package com.djrapitops.plan.system.database.databases.operation;
import java.util.List;
@Deprecated
public interface SearchOperations {
@Deprecated
List<String> matchingPlayers(String search);
}

View File

@ -19,23 +19,22 @@ package com.djrapitops.plan.system.database.databases.sql.operation;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.data.container.*;
import com.djrapitops.plan.data.store.containers.*;
import com.djrapitops.plan.data.store.keys.*;
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.PlayersMutator;
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.data.store.objects.DateObj;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.sql.queries.ContainerFetchQueries;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.databases.operation.FetchOperations;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.settings.config.Config;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class SQLFetchOps extends SQLOps implements FetchOperations {
@ -46,85 +45,16 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public NetworkContainer getNetworkContainer() {
NetworkContainer networkContainer = db.getNetworkContainerFactory().forBungeeContainer(getBungeeServerContainer());
networkContainer.putCachingSupplier(NetworkKeys.BUKKIT_SERVERS, () ->
db.query(LargeFetchQueries.fetchPlanServerInformation()).values()
.stream().filter(Server::isNotProxy).collect(Collectors.toSet())
);
return networkContainer;
}
private ServerContainer getBungeeServerContainer() {
Optional<Server> proxyInformation = db.query(OptionalFetchQueries.proxyServerInformation());
if (!proxyInformation.isPresent()) {
return new ServerContainer();
}
ServerContainer container = getServerContainer(proxyInformation.get().getUuid());
container.putCachingSupplier(ServerKeys.PLAYERS, this::getAllPlayerContainers);
container.putCachingSupplier(ServerKeys.TPS, tpsTable::getNetworkOnlineData);
container.putSupplier(ServerKeys.WORLD_TIMES, null); // Additional Session information not supported
container.putSupplier(ServerKeys.PLAYER_KILLS, null);
container.putSupplier(ServerKeys.PLAYER_KILL_COUNT, null);
return container;
return db.query(ContainerFetchQueries.fetchNetworkContainer());
}
@Override
public ServerContainer getServerContainer(UUID serverUUID) {
ServerContainer container = new ServerContainer();
Optional<Server> serverInfo = serverTable.getServerInfo(serverUUID);
if (!serverInfo.isPresent()) {
return container;
}
container.putRawData(ServerKeys.SERVER_UUID, serverUUID);
container.putRawData(ServerKeys.NAME, serverInfo.get().getName());
container.putCachingSupplier(ServerKeys.PLAYERS, () -> getPlayerContainers(serverUUID));
container.putSupplier(ServerKeys.PLAYER_COUNT, () -> container.getUnsafe(ServerKeys.PLAYERS).size());
container.putCachingSupplier(ServerKeys.TPS, () -> tpsTable.getTPSData(serverUUID));
container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings());
container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () -> {
Optional<TPS> allTimePeak = tpsTable.getAllTimePeak(serverUUID);
if (allTimePeak.isPresent()) {
TPS peak = allTimePeak.get();
return new DateObj<>(peak.getDate(), peak.getPlayers());
}
return null;
});
container.putCachingSupplier(ServerKeys.RECENT_PEAK_PLAYERS, () -> {
long twoDaysAgo = System.currentTimeMillis() - (TimeUnit.DAYS.toMillis(2L));
Optional<TPS> lastPeak = tpsTable.getPeakPlayerCount(serverUUID, twoDaysAgo);
if (lastPeak.isPresent()) {
TPS peak = lastPeak.get();
return new DateObj<>(peak.getDate(), peak.getPlayers());
}
return null;
});
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> commandUseTable.getCommandUse(serverUUID));
container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> worldTimesTable.getWorldTimesOfServer(serverUUID));
// Calculating getters
container.putCachingSupplier(ServerKeys.OPERATORS, () -> PlayersMutator.forContainer(container).operators());
container.putCachingSupplier(ServerKeys.SESSIONS, () -> {
List<Session> sessions = PlayersMutator.forContainer(container).getSessions();
if (serverUUID.equals(serverInfo.get().getUuid())) {
sessions.addAll(SessionCache.getActiveSessions().values());
}
return sessions;
});
container.putCachingSupplier(ServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList());
container.putCachingSupplier(ServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(ServerKeys.PLAYER_KILLS).size());
container.putCachingSupplier(ServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount());
container.putCachingSupplier(ServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount());
return container;
return db.query(ContainerFetchQueries.getServerContainer(serverUUID));
}
private List<PlayerContainer> getPlayerContainers(UUID serverUUID) {
@Override
public List<PlayerContainer> getPlayerContainers(UUID serverUUID) {
List<PlayerContainer> containers = new ArrayList<>();
List<UserInfo> serverUserInfo = userInfoTable.getServerUserInfo(serverUUID); // TODO Optimize and sort out