From 6a0dc0300f74b325d3ed0c3e56cd82bbe83fdd6a Mon Sep 17 00:00:00 2001 From: Xephi Date: Tue, 19 Aug 2014 06:24:53 +0200 Subject: [PATCH] Add a message to prevent lower performance with File --- src/main/java/fr/xephi/authme/AuthMe.java | 8 ++++++ .../authme/datasource/CacheDataSource.java | 5 ++++ .../xephi/authme/datasource/DataSource.java | 2 ++ .../authme/datasource/FlatFileThread.java | 27 +++++++++++++++++++ .../xephi/authme/datasource/MySQLThread.java | 26 ++++++++++++++++++ .../xephi/authme/datasource/SQLiteThread.java | 23 ++++++++++++++++ 6 files changed, 91 insertions(+) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index c1ba5cdfa..e07e3d4d4 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -204,6 +204,10 @@ public class AuthMe extends JavaPlugin { fileThread.start(); database = fileThread; databaseThread = fileThread; + final int a = database.getAccountsRegistered(); + if (a >= 1000) { + ConsoleLogger.showError("YOUR USING FILE DATABASE WITH " + a + "+ ACCOUNTS, FOR BETTER PERFORMANCES, PLEASE USE MYSQL!!"); + } break; case MYSQL: MySQLThread sqlThread = new MySQLThread(); @@ -216,6 +220,10 @@ public class AuthMe extends JavaPlugin { sqliteThread.start(); database = sqliteThread; databaseThread = sqliteThread; + final int b = database.getAccountsRegistered(); + if (b >= 2000) { + ConsoleLogger.showError("YOU'RE USING SQLITE DATABASE WITH " + b + "+ ACCOUNTS, FOR BETTER PERFORMANCES, PLEASE USE MYSQL!!"); + } break; } diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 8865bb64d..958ea8f71 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -213,4 +213,9 @@ public class CacheDataSource implements DataSource { public void purgeLogged() { source.purgeLogged(); } + + @Override + public int getAccountsRegistered() { + return source.getAccountsRegistered(); + } } diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index cf776cfcb..55a89ca07 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -57,4 +57,6 @@ public interface DataSource { void purgeLogged(); + int getAccountsRegistered(); + } diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFileThread.java b/src/main/java/fr/xephi/authme/datasource/FlatFileThread.java index 19c94b573..28385c124 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFileThread.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFileThread.java @@ -748,4 +748,31 @@ public class FlatFileThread extends Thread implements DataSource { public void purgeLogged() { PlayersLogs.getInstance().clear(); } + + @Override + public int getAccountsRegistered() { + BufferedReader br = null; + int result = 0; + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + result++; + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return result; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return result; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + return result; + } } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQLThread.java b/src/main/java/fr/xephi/authme/datasource/MySQLThread.java index 426812b14..d87b61d8a 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQLThread.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQLThread.java @@ -1157,4 +1157,30 @@ public class MySQLThread extends Thread implements DataSource { return; } + @Override + public int getAccountsRegistered() { + int result = 0; + Connection con = null; + PreparedStatement pst = null; + ResultSet rs = null; + try { + con = makeSureConnectionIsReady(); + pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); + rs = pst.executeQuery(); + if (rs != null && rs.next()) { + result = rs.getInt(1); + } + } catch (SQLException ex) { + ConsoleLogger.showError(ex.getMessage()); + return result; + } catch (TimeoutException ex) { + ConsoleLogger.showError(ex.getMessage()); + return result; + } finally { + close(pst); + close(con); + } + return result; + } + } diff --git a/src/main/java/fr/xephi/authme/datasource/SQLiteThread.java b/src/main/java/fr/xephi/authme/datasource/SQLiteThread.java index ca2114d67..e5d44ac1a 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLiteThread.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLiteThread.java @@ -597,4 +597,27 @@ public class SQLiteThread extends Thread implements DataSource { public void purgeLogged() { PlayersLogs.getInstance().clear(); } + + @Override + public int getAccountsRegistered() { + int result = 0; + PreparedStatement pst = null; + ResultSet rs = null; + try { + pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); + rs = pst.executeQuery(); + if (rs != null && rs.next()) { + result = rs.getInt(1); + } + } catch (SQLException ex) { + ConsoleLogger.showError(ex.getMessage()); + return result; + } catch (TimeoutException ex) { + ConsoleLogger.showError(ex.getMessage()); + return result; + } finally { + close(pst); + } + return result; + } }