major refactor of the purging process

This commit is contained in:
Gnat008
2016-06-16 12:28:42 -04:00
parent 3a102c324e
commit af1520802d
10 changed files with 499 additions and 89 deletions
@@ -148,6 +148,18 @@ public class CacheDataSource implements DataSource {
return cleared;
}
@Override
public Set<String> getRecordsToPurge(long until) {
return source.getRecordsToPurge(until);
}
@Override
public void purgeRecords(Set<String> toPurge) {
for (String name : toPurge) {
cachedAuths.invalidate(name);
}
}
@Override
public boolean removeAuth(String name) {
name = name.toLowerCase();
@@ -71,13 +71,28 @@ public interface DataSource extends Reloadable {
/**
* Purge all records in the database whose last login was longer ago than
* the given time.
* the given time if they do not have the bypass permission.
*
* @param until The minimum last login
* @return The account names that have been removed
*/
Set<String> autoPurgeDatabase(long until);
/**
* Get all records in the database whose last login was before the given time.
*
* @param until The minimum last login
* @return The account names selected to purge
*/
Set<String> getRecordsToPurge(long until);
/**
* Purge the given players from the database.
*
* @param toPurge The players to purge
*/
void purgeRecords(Set<String> toPurge);
/**
* Remove a user record from the database.
*
@@ -8,6 +8,7 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.Settings;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
@@ -262,6 +263,65 @@ public class FlatFile implements DataSource {
return cleared;
}
@Override
public Set<String> getRecordsToPurge(long until) {
BufferedReader br = null;
Set<String> list = new HashSet<>();
try {
br = new BufferedReader(new FileReader(source));
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
if (args.length >= 4) {
if (Long.parseLong(args[3]) >= until) {
list.add(args[0]);
continue;
}
}
}
} catch (IOException ex) {
ConsoleLogger.showError(ex.getMessage());
return list;
} finally {
silentClose(br);
}
return list;
}
@Override
public void purgeRecords(Set<String> toPurge) {
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<String> lines = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(source));
bw = new BufferedWriter(new FileWriter(source));
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
if (args.length >= 4) {
if (toPurge.contains(args[0])) {
lines.add(line);
continue;
}
}
}
for (String l : lines) {
bw.write(l + "\n");
}
} catch (IOException ex) {
ConsoleLogger.showError(ex.getMessage());
return;
} finally {
silentClose(br);
silentClose(bw);
}
}
@Override
public synchronized boolean removeAuth(String user) {
if (!isAuthAvailable(user)) {
@@ -635,6 +635,39 @@ public class MySQL implements DataSource {
return list;
}
@Override
public Set<String> getRecordsToPurge(long until) {
Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
try (Connection con = getConnection();
PreparedStatement selectPst = con.prepareStatement(select)) {
selectPst.setLong(1, until);
try (ResultSet rs = selectPst.executeQuery()) {
while (rs.next()) {
list.add(rs.getString(col.NAME));
}
}
} catch (SQLException ex) {
logSqlException(ex);
}
return list;
}
@Override
public void purgeRecords(Set<String> toPurge) {
String delete = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (Connection con = getConnection(); PreparedStatement deletePst = con.prepareStatement(delete)) {
for (String name : toPurge) {
deletePst.setString(1, name);
deletePst.executeUpdate();
}
} catch (SQLException ex) {
logSqlException(ex);
}
}
@Override
public boolean removeAuth(String user) {
user = user.toLowerCase();
@@ -314,6 +314,38 @@ public class SQLite implements DataSource {
return list;
}
@Override
public Set<String> getRecordsToPurge(long until) {
Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
try (PreparedStatement selectPst = con.prepareStatement(select)) {
selectPst.setLong(1, until);
try (ResultSet rs = selectPst.executeQuery()) {
while (rs.next()) {
list.add(rs.getString(col.NAME));
}
}
} catch (SQLException ex) {
logSqlException(ex);
}
return list;
}
@Override
public void purgeRecords(Set<String> toPurge) {
String delete = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
for (String name : toPurge) {
try (PreparedStatement deletePst = con.prepareStatement(delete)) {
deletePst.setString(1, name);
deletePst.executeUpdate();
} catch (SQLException ex) {
logSqlException(ex);
}
}
}
@Override
public boolean removeAuth(String user) {
PreparedStatement pst = null;