Refactored IPTable

This commit is contained in:
Rsl1122 2017-10-04 17:36:23 +03:00
parent e2ad414b88
commit d872fcf395

View File

@ -3,11 +3,13 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException; import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
import main.java.com.djrapitops.plan.database.processing.QueryAllStatement;
import main.java.com.djrapitops.plan.database.processing.QueryStatement;
import main.java.com.djrapitops.plan.database.sql.Select; import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -62,25 +64,25 @@ public class IPsTable extends UserIDTable {
} }
private List<String> getStringList(UUID uuid, String column) throws SQLException { private List<String> getStringList(UUID uuid, String column) throws SQLException {
PreparedStatement statement = null; String sql = Select.from(tableName, column)
ResultSet set = null; .where(columnUserID + "=" + usersTable.statementSelectID)
try (Connection connection = getConnection()) { .toString();
List<String> stringList = new ArrayList<>();
statement = connection.prepareStatement(Select.from(tableName, column) return query(new QueryStatement<List<String>>(sql, 100) {
.where(columnUserID + "=" + usersTable.statementSelectID) @Override
.toString()); public void prepare(PreparedStatement statement) throws SQLException {
statement.setFetchSize(50); statement.setString(1, uuid.toString());
statement.setString(1, uuid.toString());
set = statement.executeQuery();
while (set.next()) {
stringList.add(set.getString(column));
} }
return stringList; @Override
} finally { public List<String> processQuery(ResultSet set) throws SQLException {
close(set, statement); List<String> stringList = new ArrayList<>();
} while (set.next()) {
stringList.add(set.getString(column));
}
return stringList;
}
});
} }
public void saveIP(UUID uuid, String ip, String geolocation) throws SQLException { public void saveIP(UUID uuid, String ip, String geolocation) throws SQLException {
@ -92,130 +94,115 @@ public class IPsTable extends UserIDTable {
} }
private void insertIp(UUID uuid, String ip, String geolocation) throws SQLException { private void insertIp(UUID uuid, String ip, String geolocation) throws SQLException {
PreparedStatement statement = null; execute(new ExecStatement(insertStatement) {
try (Connection connection = getConnection()) { @Override
public void prepare(PreparedStatement statement) throws SQLException {
statement = connection.prepareStatement(insertStatement); statement.setString(1, uuid.toString());
statement.setString(1, uuid.toString()); statement.setString(2, ip);
statement.setString(2, ip); statement.setString(3, geolocation);
statement.setString(3, geolocation); }
statement.execute(); });
commit(connection);
} finally {
close(statement);
}
} }
public Optional<String> getGeolocation(String ip) throws SQLException { public Optional<String> getGeolocation(String ip) throws SQLException {
PreparedStatement statement = null; String sql = Select.from(tableName, columnGeolocation)
ResultSet set = null; .where(columnIP + "=?")
try (Connection connection = getConnection()) { .toString();
statement = connection.prepareStatement(Select.from(tableName, columnGeolocation)
.where(columnIP + "=?") return query(new QueryStatement<Optional<String>>(sql) {
.toString()); @Override
statement.setString(1, ip); public void prepare(PreparedStatement statement) throws SQLException {
set = statement.executeQuery(); statement.setString(1, ip);
if (set.next()) {
return Optional.of(set.getString(columnGeolocation));
} }
return Optional.empty();
} finally { @Override
close(set, statement); public Optional<String> processQuery(ResultSet set) throws SQLException {
} if (set.next()) {
return Optional.of(set.getString(columnGeolocation));
}
return Optional.empty();
}
});
} }
public Map<UUID, List<String>> getAllGeolocations() throws SQLException { public Map<UUID, List<String>> getAllGeolocations() throws SQLException {
PreparedStatement statement = null; String usersIDColumn = usersTable + "." + usersTable.getColumnID();
ResultSet set = null; String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
try (Connection connection = getConnection()) { String sql = "SELECT " +
Map<UUID, List<String>> geoLocations = new HashMap<>(); columnGeolocation + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); return query(new QueryAllStatement<Map<UUID, List<String>>>(sql, 50000) {
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; @Override
public Map<UUID, List<String>> processQuery(ResultSet set) throws SQLException {
Map<UUID, List<String>> geoLocations = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
statement = connection.prepareStatement("SELECT " + List<String> userGeoLocs = geoLocations.getOrDefault(uuid, new ArrayList<>());
columnGeolocation + ", " + userGeoLocs.add(set.getString(columnGeolocation));
usersUUIDColumn + geoLocations.put(uuid, userGeoLocs);
" FROM " + tableName + }
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID return geoLocations;
);
statement.setFetchSize(50000);
set = statement.executeQuery();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
List<String> userGeoLocs = geoLocations.getOrDefault(uuid, new ArrayList<>());
userGeoLocs.add(set.getString(columnGeolocation));
geoLocations.put(uuid, userGeoLocs);
} }
return geoLocations; });
} finally {
close(set, statement);
}
} }
public Map<UUID, Map<String, String>> getAllIPsAndGeolocations() throws SQLException { public Map<UUID, Map<String, String>> getAllIPsAndGeolocations() throws SQLException {
PreparedStatement statement = null; String usersIDColumn = usersTable + "." + usersTable.getColumnID();
ResultSet set = null; String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
try (Connection connection = getConnection()){ String sql = "SELECT " +
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); columnGeolocation + ", " +
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; columnIP + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
statement = connection.prepareStatement("SELECT " + return query(new QueryAllStatement<Map<UUID, Map<String, String>>>(sql, 50000) {
columnGeolocation + ", " + @Override
columnIP + ", " + public Map<UUID, Map<String, String>> processQuery(ResultSet set) throws SQLException {
usersUUIDColumn + Map<UUID, Map<String, String>> map = new HashMap<>();
" FROM " + tableName + while (set.next()) {
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID UUID uuid = UUID.fromString(set.getString("uuid"));
);
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<>()); Map<String, String> userMap = map.getOrDefault(uuid, new HashMap<>());
String geoLocation = set.getString(columnGeolocation); String geoLocation = set.getString(columnGeolocation);
String ip = set.getString(columnIP); String ip = set.getString(columnIP);
userMap.put(ip, geoLocation); userMap.put(ip, geoLocation);
map.put(uuid, userMap); map.put(uuid, userMap);
}
return map;
} }
return map; });
} finally {
close(set, statement);
}
} }
public void insertIPsAndGeolocations(Map<UUID, Map<String, String>> allIPsAndGeolocations) throws SQLException { public void insertIPsAndGeolocations(Map<UUID, Map<String, String>> allIPsAndGeolocations) throws SQLException {
if (Verify.isEmpty(allIPsAndGeolocations)) { if (Verify.isEmpty(allIPsAndGeolocations)) {
return; return;
} }
PreparedStatement statement = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
// Every User executeBatch(new ExecStatement(insertStatement) {
for (UUID uuid : allIPsAndGeolocations.keySet()) { @Override
// Every IP & Geolocation public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<String, String> entry : allIPsAndGeolocations.get(uuid).entrySet()) { // Every User
String ip = entry.getKey(); for (UUID uuid : allIPsAndGeolocations.keySet()) {
String geoLocation = entry.getValue(); // 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(1, uuid.toString());
statement.setString(2, ip); statement.setString(2, ip);
statement.setString(3, geoLocation); statement.setString(3, geoLocation);
statement.addBatch(); statement.addBatch();
}
} }
} }
});
statement.executeBatch();
commit(connection);
} finally {
close(statement);
}
} }
} }