Refactored GeoInfoTable#insertAllGeoInfo to an executable

This commit is contained in:
Rsl1122 2019-01-24 19:21:13 +02:00
parent 2245e9bd00
commit 962faaf859
5 changed files with 67 additions and 48 deletions

View File

@ -65,7 +65,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction {
}
private void copyIPsAndGeolocs() {
db.getGeoInfoTable().insertAllGeoInfo(sourceDB.query(LargeFetchQueries.fetchAllGeoInfoData()));
execute(LargeStoreQueries.storeAllGeoInfoData(sourceDB.query(LargeFetchQueries.fetchAllGeoInfoData())));
}
private void copyNicknames() {

View File

@ -81,7 +81,7 @@ public class LargeFetchQueries {
/**
* Query database for all GeoInfo data.
*
* @return Map: Server UUID - List of GeoInfo
* @return Map: Player UUID - List of GeoInfo
*/
public static Query<Map<UUID, List<GeoInfo>>> fetchAllGeoInfoData() {
String sql = "SELECT " +

View File

@ -16,12 +16,16 @@
*/
package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.db.sql.tables.CommandUseTable;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -36,8 +40,14 @@ public class LargeStoreQueries {
/* Static method class */
}
public static Executable storeAllCommandUsageData(Map<UUID, Map<String, Integer>> allCommandUsages) {
if (allCommandUsages.isEmpty()) {
/**
* Execute a big batch of command use insert statements.
*
* @param ofServers Multi map: Server UUID - (Command name - Usage count)
* @return Executable, use inside a {@link com.djrapitops.plan.db.access.transactions.Transaction}
*/
public static Executable storeAllCommandUsageData(Map<UUID, Map<String, Integer>> ofServers) {
if (ofServers.isEmpty()) {
return Executable.empty();
}
@ -45,9 +55,9 @@ public class LargeStoreQueries {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every Server
for (UUID serverUUID : allCommandUsages.keySet()) {
for (UUID serverUUID : ofServers.keySet()) {
// Every Command
for (Map.Entry<String, Integer> entry : allCommandUsages.get(serverUUID).entrySet()) {
for (Map.Entry<String, Integer> entry : ofServers.get(serverUUID).entrySet()) {
String command = entry.getKey();
int timesUsed = entry.getValue();
@ -60,4 +70,40 @@ public class LargeStoreQueries {
}
};
}
/**
* Execute a big batch of GeoInfo insert statements.
*
* @param ofUsers Map: Player UUID - List of GeoInfo
* @return Executable, use inside a {@link com.djrapitops.plan.db.access.transactions.Transaction}
*/
public static Executable storeAllGeoInfoData(Map<UUID, List<GeoInfo>> ofUsers) {
if (Verify.isEmpty(ofUsers)) {
return Executable.empty();
}
return new ExecBatchStatement(GeoInfoTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every User
for (UUID uuid : ofUsers.keySet()) {
// Every GeoInfo
for (GeoInfo info : ofUsers.get(uuid)) {
String ip = info.getIp();
String ipHash = info.getIpHash();
String geoLocation = info.getGeolocation();
long lastUsed = info.getDate();
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, ipHash);
statement.setString(4, geoLocation);
statement.setLong(5, lastUsed);
statement.addBatch();
}
}
}
};
}
}

View File

@ -27,7 +27,6 @@ import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.utilities.comparators.GeoInfoComparator;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -59,19 +58,18 @@ public class GeoInfoTable extends Table {
public static final String GEOLOCATION = "geolocation";
public static final String LAST_USED = "last_used";
public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " ("
+ USER_UUID + ", "
+ IP + ", "
+ IP_HASH + ", "
+ GEOLOCATION + ", "
+ LAST_USED
+ ") VALUES (?, ?, ?, ?, ?)";
public GeoInfoTable(SQLDB db) {
super(TABLE_NAME, db);
insertStatement = "INSERT INTO " + tableName + " ("
+ USER_UUID + ", "
+ IP + ", "
+ IP_HASH + ", "
+ GEOLOCATION + ", "
+ LAST_USED
+ ") VALUES (?, ?, ?, ?, ?)";
}
private String insertStatement;
public static String createTableSQL(DBType dbType) {
return CreateTableParser.create(TABLE_NAME, dbType)
.column(ID, Sql.INT).primaryKey()
@ -136,7 +134,7 @@ public class GeoInfoTable extends Table {
}
private void insertGeoInfo(UUID uuid, GeoInfo info) {
execute(new ExecStatement(insertStatement) {
execute(new ExecStatement(INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
@ -183,34 +181,4 @@ public class GeoInfoTable extends Table {
return geolocations;
}
public void insertAllGeoInfo(Map<UUID, List<GeoInfo>> allIPsAndGeolocations) {
if (Verify.isEmpty(allIPsAndGeolocations)) {
return;
}
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every User
for (UUID uuid : allIPsAndGeolocations.keySet()) {
// Every GeoInfo
for (GeoInfo info : allIPsAndGeolocations.get(uuid)) {
String ip = info.getIp();
String ipHash = info.getIpHash();
String geoLocation = info.getGeolocation();
long lastUsed = info.getDate();
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, ipHash);
statement.setString(4, geoLocation);
statement.setLong(5, lastUsed);
statement.addBatch();
}
}
}
});
}
}

View File

@ -83,7 +83,12 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
@Override
public void insertAllGeoInfo(Map<UUID, List<GeoInfo>> ofUsers) {
geoInfoTable.insertAllGeoInfo(ofUsers);
db.executeTransaction(new Transaction() {
@Override
protected void performOperations() {
execute(LargeStoreQueries.storeAllGeoInfoData(ofUsers));
}
});
}
@Override