Refactored BatchOperationTable#copyWorlds

This commit is contained in:
Rsl1122 2019-01-20 18:42:57 +02:00
parent a110759719
commit fd85207ff1
5 changed files with 82 additions and 66 deletions

View File

@ -464,6 +464,13 @@ public class LargeFetchQueries {
};
}
/**
* Query database for common user information.
* <p>
* This is the base for any user information.
*
* @return Map: Player UUID - UserInfo
*/
public static Query<Map<UUID, UserInfo>> fetchAllCommonUserInformation() {
String sql = Select.all(UsersTable.TABLE_NAME).toString();
@ -482,4 +489,27 @@ public class LargeFetchQueries {
}
};
}
/**
* Query database for world names.
*
* @return Map: Server UUID - List of world names
*/
public static Query<Map<UUID, Collection<String>>> fetchAllWorldNames() {
String sql = "SELECT * FROM " + WorldTable.TABLE_NAME;
return new QueryAllStatement<Map<UUID, Collection<String>>>(sql, 1000) {
@Override
public Map<UUID, Collection<String>> processResults(ResultSet set) throws SQLException {
Map<UUID, Collection<String>> worldMap = new HashMap<>();
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString(WorldTable.Col.SERVER_UUID.get()));
Collection<String> worlds = worldMap.getOrDefault(serverUUID, new HashSet<>());
worlds.add(set.getString(WorldTable.Col.NAME.get()));
worldMap.put(serverUUID, worlds);
}
return worldMap;
}
};
}
}

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.patches.Version10Patch;
import com.djrapitops.plan.db.patches.WorldsOptimizationPatch;
@ -72,27 +71,6 @@ public class WorldTable extends Table {
);
}
/**
* Used to get the available world names.
*
* @return List of all world names in the database.
*/
public List<String> getAllWorlds() {
String sql = "SELECT * FROM " + tableName;
return query(new QueryAllStatement<List<String>>(sql) {
@Override
public List<String> processResults(ResultSet set) throws SQLException {
List<String> worldNames = new ArrayList<>();
while (set.next()) {
String worldName = set.getString(Col.NAME.get());
worldNames.add(worldName);
}
return worldNames;
}
});
}
public List<String> getWorlds() {
return getWorlds(getServerUUID());
}
@ -120,6 +98,27 @@ public class WorldTable extends Table {
});
}
public void saveWorlds(Map<UUID, Collection<String>> worldMap) {
String sql = "INSERT INTO " + tableName + " ("
+ Col.NAME + ", "
+ Col.SERVER_UUID
+ ") VALUES (?, ?)";
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<UUID, Collection<String>> entry : worldMap.entrySet()) {
UUID serverUUID = entry.getKey();
for (String world : entry.getValue()) {
statement.setString(1, world);
statement.setString(2, serverUUID.toString());
statement.addBatch();
}
}
}
});
}
public void saveWorlds(Collection<String> worlds) {
saveWorlds(worlds, getServerUUID());
}

View File

@ -149,7 +149,7 @@ public class BatchOperationTable extends Table {
if (toDB.equals(this)) {
return;
}
toDB.db.getWorldTable().saveWorlds(db.getWorldTable().getAllWorlds());
toDB.db.getWorldTable().saveWorlds(db.query(LargeFetchQueries.fetchAllWorldNames()));
}
public void copyUsers(BatchOperationTable toDB) {

View File

@ -72,8 +72,7 @@ public class Server implements Comparable<Server> {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Server that = (Server) o;
return id == that.id &&
Objects.equals(uuid, that.uuid) &&
return Objects.equals(uuid, that.uuid) &&
Objects.equals(name, that.name) &&
Objects.equals(webAddress, that.webAddress);
}

View File

@ -28,6 +28,7 @@ import com.djrapitops.plan.data.store.keys.*;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.queries.batch.LargeFetchQueries;
import com.djrapitops.plan.db.sql.tables.*;
@ -72,7 +73,6 @@ public abstract class CommonDBTest {
@ClassRule
public static ComponentMocker component = new PluginComponentMocker(temporaryFolder);
public static UUID serverUUID;
public static DBSystem dbSystem;
@ -329,7 +329,7 @@ public abstract class CommonDBTest {
commitTest();
List<String> saved = worldTable.getAllWorlds();
List<String> saved = worldTable.getWorlds(serverUUID);
assertEquals(new HashSet<>(worlds), new HashSet<>(saved));
}
@ -550,33 +550,27 @@ public abstract class CommonDBTest {
@Test
public void testRemovalEverything() throws NoSuchAlgorithmException {
UserInfoTable userInfoTable = db.getUserInfoTable();
UsersTable usersTable = db.getUsersTable();
SessionsTable sessionsTable = db.getSessionsTable();
NicknamesTable nicknamesTable = db.getNicknamesTable();
GeoInfoTable geoInfoTable = db.getGeoInfoTable();
TPSTable tpsTable = db.getTpsTable();
SecurityTable securityTable = db.getSecurityTable();
saveAllData(db);
db.remove().everything();
assertFalse(usersTable.isRegistered(playerUUID));
assertFalse(usersTable.isRegistered(TestConstants.PLAYER_TWO_UUID));
assertFalse(userInfoTable.isRegistered(playerUUID));
assertTrue(nicknamesTable.getNicknames(playerUUID).isEmpty());
assertTrue(geoInfoTable.getGeoInfo(playerUUID).isEmpty());
assertTrue(sessionsTable.getSessions(playerUUID).isEmpty());
assertTrue(db.getCommandUseTable().getCommandUse().isEmpty());
assertTrue(db.getWorldTable().getAllWorlds().isEmpty());
assertTrue(tpsTable.getTPSData().isEmpty());
assertTrue(db.query(LargeFetchQueries.fetchPlanServerInformation()).isEmpty());
assertTrue(db.query(LargeFetchQueries.fetchAllPingData()).isEmpty());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllCommonUserInformation());
assertQueryIsEmpty(db, LargeFetchQueries.fetchPerServerUserInformation());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllNicknameData());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllGeoInfoData());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllSessionsWithoutKillOrWorldData());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllCommandUsageData());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllWorldNames());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllTPSData());
assertQueryIsEmpty(db, LargeFetchQueries.fetchPlanServerInformation());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllPingData());
assertTrue(db.query(LargeFetchQueries.fetchAllPlanWebUsers()).isEmpty());
}
private <T extends Map> void assertQueryIsEmpty(Database database, Query<T> query) {
assertTrue(database.query(query).isEmpty());
}
private void saveAllData(SQLDB database) throws NoSuchAlgorithmException {
UserInfoTable userInfoTable = database.getUserInfoTable();
UsersTable usersTable = database.getUsersTable();
@ -718,26 +712,20 @@ public abstract class CommonDBTest {
db.backup().backup(backup);
UserInfoTable userInfoTable = backup.getUserInfoTable();
UsersTable usersTable = backup.getUsersTable();
SessionsTable sessionsTable = backup.getSessionsTable();
NicknamesTable nicknamesTable = backup.getNicknamesTable();
GeoInfoTable ipsTable = backup.getGeoInfoTable();
TPSTable tpsTable = backup.getTpsTable();
SecurityTable securityTable = backup.getSecurityTable();
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllCommonUserInformation());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchPerServerUserInformation());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllNicknameData());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllGeoInfoData());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllSessionsWithKillAndWorldData());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllCommandUsageData());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllWorldNames());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllTPSData());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchPlanServerInformation());
assertQueryResultIsEqual(db, backup, LargeFetchQueries.fetchAllPlanWebUsers());
}
assertTrue(usersTable.isRegistered(playerUUID));
assertTrue(usersTable.isRegistered(TestConstants.PLAYER_TWO_UUID));
assertTrue(userInfoTable.isRegistered(playerUUID));
assertFalse(nicknamesTable.getNicknames(playerUUID).isEmpty());
assertFalse(ipsTable.getGeoInfo(playerUUID).isEmpty());
assertFalse(sessionsTable.getSessions(playerUUID).isEmpty());
assertFalse(backup.getCommandUseTable().getCommandUse().isEmpty());
assertFalse(backup.getWorldTable().getAllWorlds().isEmpty());
assertFalse(tpsTable.getTPSData().isEmpty());
assertFalse(backup.query(LargeFetchQueries.fetchPlanServerInformation()).isEmpty());
assertFalse(backup.query(LargeFetchQueries.fetchAllPlanWebUsers()).isEmpty());
private <T> void assertQueryResultIsEqual(Database one, Database two, Query<T> query) {
assertEquals(one.query(query), two.query(query));
}
@Test