#674 PurgeService: Always register if purging, reduce code duplication
- Rename autoPurging to isPurging: we should always register if a purge task is in progress (regardless if autopurge or not) and deny any new requests - Reduce the same logic being coded multiple times by calling through the methods - DataSource: remove purgeBanned in favor of purgeRecords, both do exactly the same thing
This commit is contained in:
@@ -143,14 +143,6 @@ public class CacheDataSource implements DataSource {
|
||||
return source.getRecordsToPurge(until);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeRecords(Set<String> toPurge) {
|
||||
source.purgeRecords(toPurge);
|
||||
for (String name : toPurge) {
|
||||
cachedAuths.invalidate(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAuth(String name) {
|
||||
name = name.toLowerCase();
|
||||
@@ -193,8 +185,8 @@ public class CacheDataSource implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeBanned(final Set<String> banned) {
|
||||
source.purgeBanned(banned);
|
||||
public void purgeRecords(final Set<String> banned) {
|
||||
source.purgeRecords(banned);
|
||||
cachedAuths.invalidateAll(banned);
|
||||
}
|
||||
|
||||
|
||||
@@ -129,13 +129,6 @@ public interface DataSource extends Reloadable {
|
||||
*/
|
||||
void close();
|
||||
|
||||
/**
|
||||
* Purge all given players, i.e. delete all players whose name is in the list.
|
||||
*
|
||||
* @param banned the list of players to delete
|
||||
*/
|
||||
void purgeBanned(Set<String> banned);
|
||||
|
||||
/**
|
||||
* Return the data source type.
|
||||
*
|
||||
|
||||
@@ -441,37 +441,6 @@ public class FlatFile implements DataSource {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeBanned(Set<String> banned) {
|
||||
BufferedReader br = null;
|
||||
BufferedWriter bw = null;
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
try {
|
||||
br = new BufferedReader(new FileReader(source));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] args = line.split(":");
|
||||
try {
|
||||
if (banned.contains(args[0])) {
|
||||
lines.add(line);
|
||||
}
|
||||
} catch (NullPointerException | ArrayIndexOutOfBoundsException ignored) {
|
||||
}
|
||||
}
|
||||
bw = new BufferedWriter(new FileWriter(source));
|
||||
for (String l : lines) {
|
||||
bw.write(l + "\n");
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
ConsoleLogger.showError(ex.getMessage());
|
||||
|
||||
} finally {
|
||||
silentClose(br);
|
||||
silentClose(bw);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.FILE;
|
||||
|
||||
@@ -632,19 +632,6 @@ public class MySQL implements DataSource {
|
||||
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();
|
||||
@@ -752,10 +739,10 @@ public class MySQL implements DataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeBanned(Set<String> banned) {
|
||||
public void purgeRecords(Set<String> toPurge) {
|
||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
for (String name : banned) {
|
||||
for (String name : toPurge) {
|
||||
pst.setString(1, name);
|
||||
pst.executeUpdate();
|
||||
}
|
||||
|
||||
@@ -452,19 +452,6 @@ public class SQLite implements DataSource {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeBanned(Set<String> banned) {
|
||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
for (String name : banned) {
|
||||
pst.setString(1, name);
|
||||
pst.executeUpdate();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.SQLITE;
|
||||
|
||||
Reference in New Issue
Block a user