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;
try (Connection connection = getConnection()) {
List<String> stringList = new ArrayList<>();
statement = connection.prepareStatement(Select.from(tableName, column)
.where(columnUserID + "=" + usersTable.statementSelectID) .where(columnUserID + "=" + usersTable.statementSelectID)
.toString()); .toString();
statement.setFetchSize(50);
return query(new QueryStatement<List<String>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
set = statement.executeQuery(); }
@Override
public List<String> processQuery(ResultSet set) throws SQLException {
List<String> stringList = new ArrayList<>();
while (set.next()) { while (set.next()) {
stringList.add(set.getString(column)); stringList.add(set.getString(column));
} }
return stringList; return stringList;
} finally {
close(set, statement);
} }
});
} }
public void saveIP(UUID uuid, String ip, String geolocation) throws SQLException { public void saveIP(UUID uuid, String ip, String geolocation) throws SQLException {
@ -92,56 +94,50 @@ 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;
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnGeolocation)
.where(columnIP + "=?") .where(columnIP + "=?")
.toString()); .toString();
return query(new QueryStatement<Optional<String>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, ip); statement.setString(1, ip);
set = statement.executeQuery(); }
@Override
public Optional<String> processQuery(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return Optional.of(set.getString(columnGeolocation)); return Optional.of(set.getString(columnGeolocation));
} }
return Optional.empty(); return Optional.empty();
} finally {
close(set, statement);
} }
});
} }
public Map<UUID, List<String>> getAllGeolocations() throws SQLException { public Map<UUID, List<String>> getAllGeolocations() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
Map<UUID, List<String>> geoLocations = new HashMap<>();
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String sql = "SELECT " +
statement = connection.prepareStatement("SELECT " +
columnGeolocation + ", " + columnGeolocation + ", " +
usersUUIDColumn + usersUUIDColumn +
" FROM " + tableName + " FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
);
statement.setFetchSize(50000); return query(new QueryAllStatement<Map<UUID, List<String>>>(sql, 50000) {
set = statement.executeQuery(); @Override
public Map<UUID, List<String>> processQuery(ResultSet set) throws SQLException {
Map<UUID, List<String>> geoLocations = new HashMap<>();
while (set.next()) { while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid")); UUID uuid = UUID.fromString(set.getString("uuid"));
@ -150,27 +146,23 @@ public class IPsTable extends UserIDTable {
geoLocations.put(uuid, userGeoLocs); geoLocations.put(uuid, userGeoLocs);
} }
return geoLocations; 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;
ResultSet set = null;
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String sql = "SELECT " +
statement = connection.prepareStatement("SELECT " +
columnGeolocation + ", " + columnGeolocation + ", " +
columnIP + ", " + columnIP + ", " +
usersUUIDColumn + usersUUIDColumn +
" FROM " + tableName + " FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
);
statement.setFetchSize(5000); return query(new QueryAllStatement<Map<UUID, Map<String, String>>>(sql, 50000) {
set = statement.executeQuery(); @Override
public Map<UUID, Map<String, String>> processQuery(ResultSet set) throws SQLException {
Map<UUID, Map<String, String>> map = new HashMap<>(); Map<UUID, Map<String, String>> map = new HashMap<>();
while (set.next()) { while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid")); UUID uuid = UUID.fromString(set.getString("uuid"));
@ -184,19 +176,18 @@ public class IPsTable extends UserIDTable {
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);
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every User // Every User
for (UUID uuid : allIPsAndGeolocations.keySet()) { for (UUID uuid : allIPsAndGeolocations.keySet()) {
// Every IP & Geolocation // Every IP & Geolocation
@ -211,11 +202,7 @@ public class IPsTable extends UserIDTable {
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); });
commit(connection);
} finally {
close(statement);
}
} }
} }