improve DatabaseCalls,

This commit is contained in:
DNx5
2015-09-15 14:38:55 +07:00
parent 91d6ead466
commit 3620b712b7
7 changed files with 109 additions and 333 deletions
@@ -11,21 +11,24 @@ import java.util.concurrent.ConcurrentHashMap;
public class CacheDataSource implements DataSource {
private DataSource source;
public AuthMe plugin;
private final DataSource source;
private ConcurrentHashMap<String, PlayerAuth> cache = new ConcurrentHashMap<>();
public CacheDataSource(AuthMe plugin, DataSource source) {
this.plugin = plugin;
this.source = source;
public CacheDataSource(AuthMe plugin, DataSource src) {
this.source = src;
/*
* We need to load all players in cache ... It will took more time to
* load the server, but it will be much easier to check for an
* isAuthAvailable !
*/
for (PlayerAuth auth : source.getAllAuths()) {
cache.put(auth.getNickname().toLowerCase(), auth);
}
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable(){
@Override
public void run() {
for (PlayerAuth auth : source.getAllAuths()) {
cache.put(auth.getNickname().toLowerCase(), auth);
}
}
});
}
@Override
@@ -6,7 +6,7 @@ import java.util.List;
public interface DataSource {
public enum DataSourceType {
enum DataSourceType {
MYSQL,
FILE,
SQLITE,
@@ -1,67 +1,42 @@
package fr.xephi.authme.datasource;
import fr.xephi.authme.cache.auth.PlayerAuth;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import fr.xephi.authme.cache.auth.PlayerAuth;
import java.util.concurrent.*;
public class DatabaseCalls implements DataSource {
private DataSource database;
private final ExecutorService exec;
public DatabaseCalls(DataSource database) {
this.database = database;
this.exec = Executors.newCachedThreadPool();
}
@Override
public synchronized boolean isAuthAvailable(final String user) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.isAuthAvailable(user);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized PlayerAuth getAuth(final String user) {
ExecutorService executor = Executors.newSingleThreadExecutor();
PlayerAuth result;
try {
result = executor.submit(new Callable<PlayerAuth>() {
return exec.submit(new Callable<PlayerAuth>() {
public PlayerAuth call() throws Exception {
return database.getAuth(user);
}
}).get();
} catch (InterruptedException e1) {
return null;
} catch (ExecutionException e1) {
return null;
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return null;
}
@@ -69,332 +44,183 @@ public class DatabaseCalls implements DataSource {
@Override
public synchronized boolean saveAuth(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.saveAuth(auth);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized boolean updateSession(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateSession(auth);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized boolean updatePassword(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updatePassword(auth);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized int purgeDatabase(final long until) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Integer result;
try {
result = executor.submit(new Callable<Integer>() {
return exec.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return database.purgeDatabase(until);
}
}).get();
} catch (InterruptedException e1) {
return 0;
} catch (ExecutionException e1) {
return 0;
} finally {
executor.shutdown();
}
try {
return result.intValue();
} catch (Exception e) {
return (0);
return -1;
}
}
@Override
public synchronized List<String> autoPurgeDatabase(final long until) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.autoPurgeDatabase(until);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized boolean removeAuth(final String user) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.removeAuth(user);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized boolean updateQuitLoc(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateQuitLoc(auth);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized int getIps(final String ip) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Integer result;
try {
result = executor.submit(new Callable<Integer>() {
return exec.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return database.getIps(ip);
}
}).get();
} catch (InterruptedException e1) {
return 0;
} catch (ExecutionException e1) {
return 0;
} finally {
executor.shutdown();
}
try {
return result.intValue();
} catch (Exception e) {
return (0);
return -1;
}
}
@Override
public synchronized List<String> getAllAuthsByName(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByName(auth);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized List<String> getAllAuthsByIp(final String ip) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByIp(ip);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized List<String> getAllAuthsByEmail(final String email) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByEmail(email);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized boolean updateEmail(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateEmail(auth);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized boolean updateSalt(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateSalt(auth);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized void close() {
database.close();
try {
exec.shutdown();
exec.awaitTermination(10, TimeUnit.SECONDS);
database.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
@@ -405,8 +231,6 @@ public class DatabaseCalls implements DataSource {
@Override
public synchronized void purgeBanned(final List<String> banned) {
new Thread(new Runnable() {
@Override
public synchronized void run() {
database.purgeBanned(banned);
}
@@ -420,139 +244,90 @@ public class DatabaseCalls implements DataSource {
@Override
public synchronized boolean isLogged(final String user) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.isLogged(user);
}
}).get();
} catch (InterruptedException e1) {
return false;
} catch (ExecutionException e1) {
return false;
} finally {
executor.shutdown();
}
try {
return result.booleanValue();
} catch (Exception e) {
return (false);
return false;
}
}
@Override
public synchronized void setLogged(final String user) {
new Thread(new Runnable() {
@Override
exec.execute(new Runnable() {
public synchronized void run() {
database.setLogged(user);
}
}).start();
});
}
@Override
public synchronized void setUnlogged(final String user) {
new Thread(new Runnable() {
@Override
exec.execute(new Runnable() {
public synchronized void run() {
database.setUnlogged(user);
}
}).start();
});
}
@Override
public synchronized void purgeLogged() {
new Thread(new Runnable() {
@Override
exec.execute(new Runnable() {
public synchronized void run() {
database.purgeLogged();
}
}).start();
});
}
@Override
public synchronized int getAccountsRegistered() {
ExecutorService executor = Executors.newSingleThreadExecutor();
Integer result;
try {
result = executor.submit(new Callable<Integer>() {
return exec.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return database.getAccountsRegistered();
}
}).get();
} catch (InterruptedException e1) {
return 0;
} catch (ExecutionException e1) {
return 0;
} finally {
executor.shutdown();
}
try {
return result.intValue();
} catch (Exception e) {
return (0);
return -1;
}
}
@Override
public synchronized void updateName(final String oldone,
final String newone) {
new Thread(new Runnable() {
@Override
public synchronized void updateName(final String oldone, final String newone) {
exec.execute(new Runnable() {
public synchronized void run() {
database.updateName(oldone, newone);
}
}).start();
});
}
@Override
public synchronized List<PlayerAuth> getAllAuths() {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<PlayerAuth> result;
try {
result = executor.submit(new Callable<List<PlayerAuth>>() {
return exec.submit(new Callable<List<PlayerAuth>>() {
public List<PlayerAuth> call() throws Exception {
return database.getAllAuths();
}
}).get();
} catch (InterruptedException e1) {
return (new ArrayList<PlayerAuth>());
} catch (ExecutionException e1) {
return (new ArrayList<PlayerAuth>());
} finally {
executor.shutdown();
} catch (Exception e) {
return new ArrayList<>();
}
return result;
}
@Override
public List<PlayerAuth> getLoggedPlayers() {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<PlayerAuth> result;
try {
result = executor.submit(new Callable<List<PlayerAuth>>() {
return exec.submit(new Callable<List<PlayerAuth>>() {
public List<PlayerAuth> call() throws Exception {
return database.getLoggedPlayers();
}
}).get();
} catch (InterruptedException e1) {
return (new ArrayList<PlayerAuth>());
} catch (ExecutionException e1) {
return (new ArrayList<PlayerAuth>());
} finally {
executor.shutdown();
} catch (Exception e) {
return new ArrayList<>();
}
return result;
}
}
@@ -235,14 +235,13 @@ public class MySQL implements DataSource {
}
}
@SuppressWarnings("resource")
@Override
public synchronized PlayerAuth getAuth(String user) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
PlayerAuth pAuth = null;
int id = -1;
int id;
try {
con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);");
@@ -1019,7 +1018,7 @@ public class MySQL implements DataSource {
@Override
public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<PlayerAuth>();
List<PlayerAuth> auths = new ArrayList<>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
@@ -1028,7 +1027,7 @@ public class MySQL implements DataSource {
pst = con.prepareStatement("SELECT * FROM " + tableName + ";");
rs = pst.executeQuery();
while (rs.next()) {
PlayerAuth pAuth = null;
PlayerAuth pAuth;
int id = rs.getInt(columnID);
if (rs.getString(columnIp).isEmpty() && rs.getString(columnIp) != null) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "192.168.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
@@ -1052,11 +1051,9 @@ public class MySQL implements DataSource {
byte[] bytes = blob.getBytes(1, (int) blob.length());
pAuth.setHash(new String(bytes));
}
if (rsid != null)
rsid.close();
rsid.close();
}
if (pAuth != null)
auths.add(pAuth);
auths.add(pAuth);
}
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());