Batch Insert for IPsTable

This commit is contained in:
Rsl1122 2017-09-02 11:03:28 +03:00
parent c1481d7935
commit 62f762cf7b
2 changed files with 87 additions and 8 deletions

View File

@ -18,6 +18,7 @@ public class IPsTable extends UserIDTable {
private final String columnIP = "ip";
private final String columnGeolocation = "geolocation";
private String insertStatement;
/**
* @param db The database
@ -25,6 +26,13 @@ public class IPsTable extends UserIDTable {
*/
public IPsTable(SQLDB db, boolean usingMySQL) {
super("plan_ips", db, usingMySQL);
insertStatement = "INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnIP + ", "
+ columnGeolocation
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ "?, ?)";
}
/**
@ -89,13 +97,8 @@ public class IPsTable extends UserIDTable {
private void insertIp(UUID uuid, String ip, String geolocation) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnIP + ", "
+ columnGeolocation
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ "?, ?)");
statement = prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, geolocation);
@ -156,4 +159,69 @@ public class IPsTable extends UserIDTable {
close(set, statement);
}
}
public Map<UUID, Map<String, String>> getAllIPsAndGeolocations() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
statement = prepareStatement("SELECT " +
columnGeolocation + ", " +
columnIP + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID
);
statement.setFetchSize(5000);
set = statement.executeQuery();
Map<UUID, Map<String, String>> map = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
Map<String, String> userMap = map.getOrDefault(uuid, new HashMap<>());
String geoLocation = set.getString(columnGeolocation);
String ip = set.getString(columnIP);
userMap.put(ip, geoLocation);
map.put(uuid, userMap);
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
}
}
public void insertIPsAndGeolocations(Map<UUID, Map<String, String>> allIPsAndGeolocations) throws SQLException {
if (allIPsAndGeolocations.isEmpty()) {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
// Every User
for (UUID uuid : allIPsAndGeolocations.keySet()) {
// Every IP & Geolocation
for (Map.Entry<String, String> entry : allIPsAndGeolocations.get(uuid).entrySet()) {
String ip = entry.getKey();
String geoLocation = entry.getValue();
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, geoLocation);
statement.addBatch();
}
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
close(statement);
}
}
}

View File

@ -12,9 +12,16 @@ import main.java.com.djrapitops.plan.database.tables.Table;
import java.sql.SQLException;
/**
* //TODO Class Javadoc Comment
* A Fake table used to store a lot of big table operations.
* <p>
* To use this table create a new BatchOperationTable with both SQLDB objects.
* {@code SQLDB from; SQLDB to;}
* {@code fromT = new BatchOperationTable(from);}
* {@code toT = new BatchOperationTable(to);}
* {@code fromT.copy(toT);}
*
* @author Rsl1122
* @since 4.0.0
*/
public class BatchOperationTable extends Table {
public BatchOperationTable(SQLDB db, boolean usingMySQL) {
@ -37,4 +44,8 @@ public class BatchOperationTable extends Table {
public void copyCommandUse(BatchOperationTable toDB) throws SQLException {
toDB.db.getCommandUseTable().insertCommandUsage(db.getCommandUseTable().getAllCommandUsages());
}
public void copyIPsAndGeolocs(BatchOperationTable toDB) throws SQLException {
toDB.db.getIpsTable().insertIPsAndGeolocations(db.getIpsTable().getAllIPsAndGeolocations());
}
}