Refactored GeoInfoTable#getAllGeoInfo to a query

This commit is contained in:
Rsl1122 2019-01-20 11:28:02 +02:00
parent 6bab6009a1
commit cec8b7ac6c
5 changed files with 43 additions and 41 deletions

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement; import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryStatement; import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.queries.batch.LargeFetchQueries;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable; import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import java.net.InetAddress; import java.net.InetAddress;
@ -69,7 +70,7 @@ public class IPAnonPatch extends Patch {
@Override @Override
protected void applyPatch() { protected void applyPatch() {
Map<UUID, List<GeoInfo>> allGeoInfo = db.getGeoInfoTable().getAllGeoInfo(); Map<UUID, List<GeoInfo>> allGeoInfo = db.query(LargeFetchQueries.fetchAllGeoInfoData());
anonymizeIPs(allGeoInfo); anonymizeIPs(allGeoInfo);
groupHashedIPs(); groupHashedIPs();
} }

View File

@ -16,16 +16,16 @@
*/ */
package com.djrapitops.plan.db.sql.queries.batch; package com.djrapitops.plan.db.sql.queries.batch;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryAllStatement; import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.sql.tables.CommandUseTable; import com.djrapitops.plan.db.sql.tables.CommandUseTable;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import com.djrapitops.plan.db.sql.tables.ServerTable; import com.djrapitops.plan.db.sql.tables.ServerTable;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.UUID;
/** /**
* Static method class for queries that use large amount of memory. * Static method class for queries that use large amount of memory.
@ -73,4 +73,35 @@ public class LargeFetchQueries {
}; };
} }
public static Query<Map<UUID, List<GeoInfo>>> fetchAllGeoInfoData() {
String sql = "SELECT " +
GeoInfoTable.Col.IP + ", " +
GeoInfoTable.Col.GEOLOCATION + ", " +
GeoInfoTable.Col.LAST_USED + ", " +
GeoInfoTable.Col.IP_HASH + ", " +
GeoInfoTable.Col.UUID +
" FROM " + GeoInfoTable.TABLE_NAME;
return new QueryAllStatement<Map<UUID, List<GeoInfo>>>(sql, 50000) {
@Override
public Map<UUID, List<GeoInfo>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<GeoInfo>> geoLocations = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString(GeoInfoTable.Col.UUID.get()));
List<GeoInfo> userGeoInfo = geoLocations.getOrDefault(uuid, new ArrayList<>());
String ip = set.getString(GeoInfoTable.Col.IP.get());
String geolocation = set.getString(GeoInfoTable.Col.GEOLOCATION.get());
String ipHash = set.getString(GeoInfoTable.Col.IP_HASH.get());
long lastUsed = set.getLong(GeoInfoTable.Col.LAST_USED.get());
userGeoInfo.add(new GeoInfo(ip, geolocation, lastUsed, ipHash));
geoLocations.put(uuid, userGeoInfo);
}
return geoLocations;
}
};
}
} }

View File

@ -20,13 +20,13 @@ import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.container.GeoInfo; import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement; 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.access.QueryStatement;
import com.djrapitops.plan.db.patches.*; import com.djrapitops.plan.db.patches.*;
import com.djrapitops.plan.db.sql.parsing.Column; import com.djrapitops.plan.db.sql.parsing.Column;
import com.djrapitops.plan.db.sql.parsing.Select; import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql; import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.db.sql.parsing.TableSqlParser; import com.djrapitops.plan.db.sql.parsing.TableSqlParser;
import com.djrapitops.plan.db.sql.queries.batch.LargeFetchQueries;
import com.djrapitops.plan.utilities.comparators.GeoInfoComparator; import com.djrapitops.plan.utilities.comparators.GeoInfoComparator;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
@ -166,41 +166,10 @@ public class GeoInfoTable extends UserUUIDTable {
}); });
} }
public Map<UUID, List<GeoInfo>> getAllGeoInfo() {
String sql = "SELECT " +
Col.IP + ", " +
Col.GEOLOCATION + ", " +
Col.LAST_USED + ", " +
Col.IP_HASH + ", " +
Col.UUID +
" FROM " + tableName;
return query(new QueryAllStatement<Map<UUID, List<GeoInfo>>>(sql, 50000) {
@Override
public Map<UUID, List<GeoInfo>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<GeoInfo>> geoLocations = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
List<GeoInfo> userGeoInfo = geoLocations.getOrDefault(uuid, new ArrayList<>());
String ip = set.getString(Col.IP.get());
String geolocation = set.getString(Col.GEOLOCATION.get());
String ipHash = set.getString(Col.IP_HASH.get());
long lastUsed = set.getLong(Col.LAST_USED.get());
userGeoInfo.add(new GeoInfo(ip, geolocation, lastUsed, ipHash));
geoLocations.put(uuid, userGeoInfo);
}
return geoLocations;
}
});
}
public List<String> getNetworkGeolocations() { public List<String> getNetworkGeolocations() {
List<String> geolocations = new ArrayList<>(); List<String> geolocations = new ArrayList<>();
Map<UUID, List<GeoInfo>> geoInfo = getAllGeoInfo(); Map<UUID, List<GeoInfo>> geoInfo = db.query(LargeFetchQueries.fetchAllGeoInfoData());
for (List<GeoInfo> userGeoInfos : geoInfo.values()) { for (List<GeoInfo> userGeoInfos : geoInfo.values()) {
if (userGeoInfos.isEmpty()) { if (userGeoInfos.isEmpty()) {
continue; continue;

View File

@ -115,7 +115,7 @@ public class BatchOperationTable extends Table {
if (toDB.equals(this)) { if (toDB.equals(this)) {
return; return;
} }
toDB.db.getGeoInfoTable().insertAllGeoInfo(db.getGeoInfoTable().getAllGeoInfo()); toDB.db.getGeoInfoTable().insertAllGeoInfo(db.query(LargeFetchQueries.fetchAllGeoInfoData()));
} }
public void copyNicknames(BatchOperationTable toDB) { public void copyNicknames(BatchOperationTable toDB) {

View File

@ -27,6 +27,7 @@ import com.djrapitops.plan.data.store.objects.DateObj;
import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.sql.queries.batch.LargeFetchQueries;
import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.databases.operation.FetchOperations; import com.djrapitops.plan.system.database.databases.operation.FetchOperations;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
@ -123,7 +124,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
List<UserInfo> serverUserInfo = userInfoTable.getServerUserInfo(serverUUID); List<UserInfo> serverUserInfo = userInfoTable.getServerUserInfo(serverUUID);
Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked(); Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked();
Map<UUID, List<GeoInfo>> geoInfo = geoInfoTable.getAllGeoInfo(); Map<UUID, List<GeoInfo>> geoInfo = db.query(LargeFetchQueries.fetchAllGeoInfoData());
Map<UUID, List<Ping>> allPings = pingTable.getAllPings(); Map<UUID, List<Ping>> allPings = pingTable.getAllPings();
Map<UUID, List<Nickname>> allNicknames = nicknamesTable.getAllNicknamesUnmapped(); Map<UUID, List<Nickname>> allNicknames = nicknamesTable.getAllNicknamesUnmapped();
@ -188,7 +189,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
Map<UUID, UserInfo> users = usersTable.getUsers(); Map<UUID, UserInfo> users = usersTable.getUsers();
Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked(); Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked();
Map<UUID, List<GeoInfo>> geoInfo = geoInfoTable.getAllGeoInfo(); Map<UUID, List<GeoInfo>> geoInfo = db.query(LargeFetchQueries.fetchAllGeoInfoData());
Map<UUID, List<Ping>> allPings = pingTable.getAllPings(); Map<UUID, List<Ping>> allPings = pingTable.getAllPings();
Map<UUID, List<Nickname>> allNicknames = nicknamesTable.getAllNicknamesUnmapped(); Map<UUID, List<Nickname>> allNicknames = nicknamesTable.getAllNicknamesUnmapped();
@ -428,7 +429,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override @Override
public Map<UUID, List<GeoInfo>> getAllGeoInfo() { public Map<UUID, List<GeoInfo>> getAllGeoInfo() {
return geoInfoTable.getAllGeoInfo(); return db.query(LargeFetchQueries.fetchAllGeoInfoData());
} }
@Override @Override