Use more specific query to get logged in players without email

- Reduces the amount of data returned from the DB and the work required to build objects
This commit is contained in:
ljacqu
2017-04-18 22:30:54 +02:00
parent 2f7ebc0ecb
commit 04ca36fe53
10 changed files with 58 additions and 76 deletions
@@ -11,14 +11,15 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.Utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class CacheDataSource implements DataSource {
@@ -240,7 +241,10 @@ public class CacheDataSource implements DataSource {
}
@Override
public List<PlayerAuth> getLoggedPlayers() {
return new ArrayList<>(playerCache.getCache().values());
public List<String> getLoggedPlayersWithEmptyMail() {
return playerCache.getCache().values().stream()
.filter(auth -> Utils.isEmailEmpty(auth.getEmail()))
.map(PlayerAuth::getRealName)
.collect(Collectors.toList());
}
}
@@ -167,11 +167,11 @@ public interface DataSource extends Reloadable {
void purgeLogged();
/**
* Return all players which are logged in.
* Return all players which are logged in and whose email is not set.
*
* @return All logged in players
* @return logged in players with no email
*/
List<PlayerAuth> getLoggedPlayers();
List<String> getLoggedPlayersWithEmptyMail();
/**
* Return the number of registered accounts.
@@ -423,7 +423,7 @@ public class FlatFile implements DataSource {
}
@Override
public List<PlayerAuth> getLoggedPlayers() {
public List<String> getLoggedPlayersWithEmptyMail() {
throw new UnsupportedOperationException("Flat file no longer supported");
}
@@ -927,33 +927,20 @@ public class MySQL implements DataSource {
}
@Override
public List<PlayerAuth> getLoggedPlayers() {
List<PlayerAuth> auths = new ArrayList<>();
String sql = "SELECT * FROM " + tableName + " WHERE " + col.IS_LOGGED + "=1;";
public List<String> getLoggedPlayersWithEmptyMail() {
List<String> players = new ArrayList<>();
String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1"
+ " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);";
try (Connection con = getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) {
PlayerAuth pAuth = buildAuthFromResultSet(rs);
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
try (PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;")) {
int id = rs.getInt(col.ID);
pst.setInt(1, id);
ResultSet rs2 = pst.executeQuery();
if (rs2.next()) {
Blob blob = rs2.getBlob("data");
byte[] bytes = blob.getBytes(1, (int) blob.length());
pAuth.setPassword(new HashedPassword(XfBCrypt.getHashFromBlob(bytes)));
}
rs2.close();
}
}
auths.add(pAuth);
players.add(rs.getString(1));
}
} catch (SQLException ex) {
logSqlException(ex);
}
return auths;
return players;
}
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
@@ -591,18 +591,18 @@ public class SQLite implements DataSource {
}
@Override
public List<PlayerAuth> getLoggedPlayers() {
List<PlayerAuth> auths = new ArrayList<>();
String sql = "SELECT * FROM " + tableName + " WHERE " + col.IS_LOGGED + "=1;";
try (PreparedStatement pst = con.prepareStatement(sql); ResultSet rs = pst.executeQuery()) {
public List<String> getLoggedPlayersWithEmptyMail() {
List<String> players = new ArrayList<>();
String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1"
+ " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);";
try (Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) {
PlayerAuth auth = buildAuthFromResultSet(rs);
auths.add(auth);
players.add(rs.getString(1));
}
} catch (SQLException ex) {
logSqlException(ex);
}
return auths;
return players;
}
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {