mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-24 16:14:26 +08:00
Merge pull request #270 from Fuzzlemann/master
PR for 4.0.0 (Fuzzlemann) (6)
This commit is contained in:
commit
05e06ebb76
16
Plan/pom.xml
16
Plan/pom.xml
@ -31,6 +31,22 @@
|
||||
<version>2.0.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!-- Connection Pool-->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-dbcp2</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
<version>2.4.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<!-- SoftDepended Plugins-->
|
||||
<dependency>
|
||||
<groupId>com.djrapitops</groupId>
|
||||
|
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.data.handling;
|
||||
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import main.java.com.djrapitops.plan.database.Database;
|
||||
import main.java.com.djrapitops.plan.queue.processing.Processor;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Processor for queueing a Database Commit after changes.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class DBCommitProcessor extends Processor<Database> {
|
||||
public DBCommitProcessor(Database object) {
|
||||
super(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
// TODO Prevent Commit during batch operations.
|
||||
try {
|
||||
object.commit();
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.data.listeners;
|
||||
|
||||
import com.djrapitops.plugin.task.AbsRunnable;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.handling.DBCommitProcessor;
|
||||
import main.java.com.djrapitops.plan.database.Database;
|
||||
|
||||
/**
|
||||
* Periodically commits changes to the SQLite Database.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class PeriodicDBCommitTask extends AbsRunnable {
|
||||
|
||||
private Plan plugin;
|
||||
|
||||
public PeriodicDBCommitTask(Plan plugin) {
|
||||
super("PeriodicDBCommitTask");
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Database db = plugin.getDB();
|
||||
if ("mysql".equals(db.getConfigName())) {
|
||||
this.cancel();
|
||||
return;
|
||||
}
|
||||
plugin.addToProcessQueue(new DBCommitProcessor(db));
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package main.java.com.djrapitops.plan.data.listeners;
|
||||
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.handling.player.DeathProcessor;
|
||||
import main.java.com.djrapitops.plan.data.handling.player.KillProcessor;
|
||||
@ -16,8 +15,6 @@ import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.projectiles.ProjectileSource;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Event Listener for EntityDeathEvents.
|
||||
*
|
||||
@ -59,9 +56,6 @@ public class PlanDeathEventListener implements Listener {
|
||||
EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent;
|
||||
Entity killerEntity = entityDamageByEntityEvent.getDamager();
|
||||
|
||||
UUID killerUUID = null;
|
||||
String weapon = null;
|
||||
|
||||
if (killerEntity instanceof Player) {
|
||||
Player killer = (Player) killerEntity;
|
||||
Material itemInHand;
|
||||
@ -75,32 +69,38 @@ public class PlanDeathEventListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
killerUUID = killer.getUniqueId();
|
||||
weapon = normalizeMaterialName(itemInHand);
|
||||
} else if (killerEntity instanceof Wolf) {
|
||||
plugin.addToProcessQueue(new KillProcessor(killer.getUniqueId(), time, dead, normalizeMaterialName(itemInHand)));
|
||||
return;
|
||||
}
|
||||
|
||||
if (killerEntity instanceof Wolf) {
|
||||
Wolf wolf = (Wolf) killerEntity;
|
||||
|
||||
if (!wolf.isTamed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AnimalTamer owner = wolf.getOwner();
|
||||
if (owner instanceof Player) {
|
||||
killerUUID = owner.getUniqueId();
|
||||
weapon = "Wolf";
|
||||
}
|
||||
} else if (killerEntity instanceof Arrow) {
|
||||
Arrow arrow = (Arrow) killerEntity;
|
||||
ProjectileSource source = arrow.getShooter();
|
||||
|
||||
if (source instanceof Player) {
|
||||
Player player = (Player) source;
|
||||
killerUUID = player.getUniqueId();
|
||||
weapon = "Bow";
|
||||
if (!(owner instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.addToProcessQueue(new KillProcessor(owner.getUniqueId(), time, dead, "Wolf"));
|
||||
}
|
||||
|
||||
if (Verify.notNull(killerUUID, weapon)) {
|
||||
plugin.addToProcessQueue(new KillProcessor(killerUUID, time, dead, weapon));
|
||||
if (killerEntity instanceof Arrow) {
|
||||
Arrow arrow = (Arrow) killerEntity;
|
||||
|
||||
ProjectileSource source = arrow.getShooter();
|
||||
|
||||
if (!(source instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = (Player) source;
|
||||
|
||||
plugin.addToProcessQueue(new KillProcessor(player.getUniqueId(), time, dead, "Bow"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@ import com.djrapitops.plugin.utilities.player.Fetch;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.Session;
|
||||
import main.java.com.djrapitops.plan.data.cache.DataCache;
|
||||
import main.java.com.djrapitops.plan.data.handling.DBCommitProcessor;
|
||||
import main.java.com.djrapitops.plan.data.handling.player.*;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -102,8 +101,7 @@ public class PlanPlayerListener implements Listener {
|
||||
plugin.addToProcessQueue(
|
||||
new RegisterProcessor(this, uuid, time, playerName, playersOnline),
|
||||
new IPUpdateProcessor(uuid, ip),
|
||||
new NameProcessor(uuid, playerName, displayName), // TODO NameCache to DataCache
|
||||
new DBCommitProcessor(plugin.getDB())
|
||||
new NameProcessor(uuid, playerName, displayName) // TODO NameCache to DataCache
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,9 @@ package main.java.com.djrapitops.plan.database;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.database.tables.*;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
@ -95,6 +97,8 @@ public abstract class Database {
|
||||
*/
|
||||
protected ServerTable serverTable;
|
||||
|
||||
protected BasicDataSource dataSource;
|
||||
|
||||
/**
|
||||
* Super constructor.
|
||||
*
|
||||
@ -327,9 +331,13 @@ public abstract class Database {
|
||||
return serverTable;
|
||||
}
|
||||
|
||||
public abstract void commit() throws SQLException;
|
||||
|
||||
public ActionsTable getActionsTable() {
|
||||
return actionsTable;
|
||||
}
|
||||
|
||||
public BasicDataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public abstract void commit(Connection connection) throws SQLException;
|
||||
}
|
||||
|
@ -1,15 +1,9 @@
|
||||
package main.java.com.djrapitops.plan.database.databases;
|
||||
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.locale.Locale;
|
||||
import main.java.com.djrapitops.plan.locale.Msg;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@ -25,30 +19,32 @@ public class MySQLDB extends SQLDB {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new connection to the database.
|
||||
*
|
||||
* @return the new Connection.
|
||||
* Setups the {@link BasicDataSource}
|
||||
*/
|
||||
@Override
|
||||
public Connection getNewConnection() {
|
||||
public void setupDataSource() {
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
dataSource = new BasicDataSource();
|
||||
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
|
||||
String url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getString("mysql.port") + "/"
|
||||
+ config.getString("mysql.database")
|
||||
+ "?rewriteBatchedStatements=true";
|
||||
String host = config.getString("Database.MySQL.Host");
|
||||
String port = config.getString("Database.MySQL.Port");
|
||||
String database = config.getString("Database.MySQL.Database");
|
||||
|
||||
return DriverManager.getConnection(url, config.getString("mysql.user"), config.getString("mysql.password"));
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
Log.error(Locale.get(Msg.ENABLE_FAIL_DB).parse(getConfigName(), e.getMessage()));
|
||||
return null;
|
||||
}
|
||||
|
||||
dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + database + "?rewriteBatchedStatements=true");
|
||||
|
||||
String username = config.getString("Database.MySQL.User");
|
||||
String password = config.getString("Database.MySQL.Password");
|
||||
|
||||
dataSource.setUsername(username);
|
||||
dataSource.setPassword(password);
|
||||
dataSource.setMaxTotal(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @return the name of the Database
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
|
@ -1,13 +1,12 @@
|
||||
package main.java.com.djrapitops.plan.database.databases;
|
||||
|
||||
import com.djrapitops.plugin.task.AbsRunnable;
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.database.Database;
|
||||
import main.java.com.djrapitops.plan.database.tables.*;
|
||||
import main.java.com.djrapitops.plan.utilities.Benchmark;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
@ -27,8 +26,6 @@ public abstract class SQLDB extends Database {
|
||||
|
||||
private final boolean usingMySQL;
|
||||
|
||||
private Connection connection;
|
||||
|
||||
/**
|
||||
* @param plugin
|
||||
*/
|
||||
@ -51,31 +48,6 @@ public abstract class SQLDB extends Database {
|
||||
killsTable = new KillsTable(this, usingMySQL);
|
||||
worldTable = new WorldTable(this, usingMySQL);
|
||||
worldTimesTable = new WorldTimesTable(this, usingMySQL);
|
||||
|
||||
startConnectionPingTask();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts repeating Async task that maintains the Database connection.
|
||||
*/
|
||||
public void startConnectionPingTask() {
|
||||
// Maintains Connection.
|
||||
plugin.getRunnableFactory().createNew(new AbsRunnable("DBConnectionPingTask " + getName()) {
|
||||
@Override
|
||||
public void run() {
|
||||
Statement statement = null;
|
||||
try {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
statement = connection.createStatement();
|
||||
statement.execute("/* ping */ SELECT 1");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
connection = getNewConnection();
|
||||
} finally {
|
||||
MiscUtils.close(statement);
|
||||
}
|
||||
}
|
||||
}).runTaskTimerAsynchronously(60L * 20L, 60L * 20L);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,7 +67,9 @@ public abstract class SQLDB extends Database {
|
||||
String benchName = "Init " + getConfigName();
|
||||
Benchmark.start(benchName);
|
||||
try {
|
||||
if (!checkConnection()) {
|
||||
setupDataSource();
|
||||
|
||||
if (!setupDatabase()) {
|
||||
return false;
|
||||
}
|
||||
clean();
|
||||
@ -117,37 +91,29 @@ public abstract class SQLDB extends Database {
|
||||
* @return Is the connection usable?
|
||||
* @throws SQLException
|
||||
*/
|
||||
public boolean checkConnection() throws SQLException {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
connection = getNewConnection();
|
||||
public boolean setupDatabase() throws SQLException {
|
||||
boolean newDatabase = isNewDatabase();
|
||||
|
||||
if (connection == null || connection.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
if (!versionTable.createTable()) {
|
||||
Log.error("Failed to create table: " + versionTable.getTableName());
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean newDatabase = isNewDatabase();
|
||||
if (newDatabase) {
|
||||
Log.info("New Database created.");
|
||||
}
|
||||
|
||||
if (!versionTable.createTable()) {
|
||||
Log.error("Failed to create table: " + versionTable.getTableName());
|
||||
return false;
|
||||
}
|
||||
if (!createTables()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (newDatabase) {
|
||||
Log.info("New Database created.");
|
||||
setVersion(8);
|
||||
}
|
||||
if (newDatabase || getVersion() < 8) {
|
||||
setVersion(8);
|
||||
}
|
||||
|
||||
if (!createTables()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!newDatabase && getVersion() < 8) {
|
||||
setVersion(8);
|
||||
}
|
||||
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("DROP TABLE IF EXISTS plan_locations");
|
||||
}
|
||||
try (Statement statement = getConnection().createStatement()) {
|
||||
statement.execute("DROP TABLE IF EXISTS plan_locations");
|
||||
endTransaction(statement.getConnection());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -207,18 +173,16 @@ public abstract class SQLDB extends Database {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* Setups the {@link BasicDataSource}
|
||||
*/
|
||||
public abstract Connection getNewConnection();
|
||||
public abstract void setupDataSource();
|
||||
|
||||
/**
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
dataSource.close();
|
||||
setStatus("Closed");
|
||||
Log.logDebug("Database"); // Log remaining Debug info if present
|
||||
}
|
||||
@ -238,7 +202,6 @@ public abstract class SQLDB extends Database {
|
||||
@Override
|
||||
public void setVersion(int version) throws SQLException {
|
||||
versionTable.setVersion(version);
|
||||
commit();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -264,29 +227,31 @@ public abstract class SQLDB extends Database {
|
||||
if (uuid == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
Benchmark.start("Remove Account");
|
||||
Log.debug("Database", "Removing Account: " + uuid);
|
||||
checkConnection();
|
||||
try {
|
||||
setupDatabase();
|
||||
} catch (Exception e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean success = true;
|
||||
for (Table t : getAllTablesInRemoveOrder()) {
|
||||
if (!success) {
|
||||
if (!(t instanceof UserIDTable)) {
|
||||
continue;
|
||||
}
|
||||
if (t instanceof UserIDTable) {
|
||||
UserIDTable table = (UserIDTable) t;
|
||||
success = table.removeUser(uuid);
|
||||
|
||||
UserIDTable table = (UserIDTable) t;
|
||||
if (!table.removeUser(uuid)) {
|
||||
throw new IllegalStateException("Removal Failed");
|
||||
}
|
||||
}
|
||||
if (success) {
|
||||
commit();
|
||||
return true;
|
||||
}
|
||||
throw new IllegalStateException("Removal Failed");
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
rollback(); // TODO Test case
|
||||
return false;
|
||||
} finally {
|
||||
Benchmark.stop("Database", "Remove Account");
|
||||
@ -301,7 +266,7 @@ public abstract class SQLDB extends Database {
|
||||
public void clean() {
|
||||
Log.info("Cleaning the database.");
|
||||
try {
|
||||
checkConnection();
|
||||
setupDatabase();
|
||||
tpsTable.clean();
|
||||
Log.info("Clean complete.");
|
||||
} catch (SQLException e) {
|
||||
@ -314,27 +279,21 @@ public abstract class SQLDB extends Database {
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAllData() {
|
||||
boolean success = true;
|
||||
setStatus("Clearing all data");
|
||||
try {
|
||||
for (Table table : getAllTablesInRemoveOrder()) {
|
||||
if (!table.removeAllData()) {
|
||||
success = false;
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (success) {
|
||||
commit();
|
||||
} else {
|
||||
rollback(); // TODO Tests for this case
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
|
||||
return true;
|
||||
} finally {
|
||||
setAvailable();
|
||||
}
|
||||
setAvailable();
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<UserData> getUserDataForUUIDS(Collection<UUID> uuidsCol) throws SQLException {
|
||||
if (uuidsCol == null || uuidsCol.isEmpty()) {
|
||||
@ -344,13 +303,6 @@ public abstract class SQLDB extends Database {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
private void setStatus(String status) {
|
||||
Log.debug("Database", status);
|
||||
}
|
||||
@ -359,14 +311,23 @@ public abstract class SQLDB extends Database {
|
||||
Log.logDebug("Database");
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return getDataSource().getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Commits changes to the .db file when using SQLite Database.
|
||||
* <p>
|
||||
* MySQL has Auto Commit enabled.
|
||||
*/
|
||||
public void commit() throws SQLException {
|
||||
if (!usingMySQL) {
|
||||
getConnection().commit();
|
||||
@Override
|
||||
public void commit(Connection connection) throws SQLException {
|
||||
try {
|
||||
if (!usingMySQL) {
|
||||
connection.commit();
|
||||
}
|
||||
} finally {
|
||||
endTransaction(connection);
|
||||
}
|
||||
}
|
||||
|
||||
@ -375,9 +336,17 @@ public abstract class SQLDB extends Database {
|
||||
* <p>
|
||||
* MySQL has Auto Commit enabled.
|
||||
*/
|
||||
public void rollback() throws SQLException {
|
||||
if (!usingMySQL) {
|
||||
connection.rollback();
|
||||
public void rollback(Connection connection) throws SQLException {
|
||||
try {
|
||||
if (!usingMySQL) {
|
||||
connection.rollback();
|
||||
}
|
||||
} finally {
|
||||
endTransaction(connection);
|
||||
}
|
||||
}
|
||||
|
||||
public void endTransaction(Connection connection) throws SQLException {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
package main.java.com.djrapitops.plan.database.databases;
|
||||
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
@ -33,36 +32,28 @@ public class SQLiteDB extends SQLDB {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new connection to the database.
|
||||
*
|
||||
* @return the new Connection.
|
||||
* Setups the {@link BasicDataSource}
|
||||
*/
|
||||
@Override
|
||||
public Connection getNewConnection() {
|
||||
return getNewConnection(dbName);
|
||||
public void setupDataSource() {
|
||||
dataSource = new BasicDataSource();
|
||||
|
||||
String filePath = new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath();
|
||||
dataSource.setUrl("jdbc:sqlite:" + filePath);
|
||||
|
||||
dataSource.setEnableAutoCommitOnReturn(false);
|
||||
dataSource.setDefaultAutoCommit(false);
|
||||
|
||||
dataSource.setConnectionInitSqls(Collections.singletonList("PRAGMA JOURNAL_MODE=WAL"));
|
||||
dataSource.setMaxTotal(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dbName
|
||||
* @return
|
||||
*/
|
||||
public Connection getNewConnection(String dbName) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath());
|
||||
connection.setAutoCommit(false);
|
||||
return connection;
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @return the name of the Database
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return "SQLite";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package main.java.com.djrapitops.plan.database.sql;
|
||||
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
|
||||
public class Select extends WhereParser {
|
||||
|
||||
public Select(String start) {
|
||||
@ -19,7 +17,6 @@ public class Select extends WhereParser {
|
||||
}
|
||||
|
||||
parser.append(" FROM ").append(table);
|
||||
Log.debug(parser.toString());
|
||||
return parser;
|
||||
}
|
||||
|
||||
|
@ -79,6 +79,7 @@ public class ActionsTable extends UserIDTable {
|
||||
statement.setString(5, action.getAdditionalInfo());
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -109,6 +110,7 @@ public class ActionsTable extends UserIDTable {
|
||||
}
|
||||
return actions;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
@ -94,8 +94,8 @@ public class CommandUseTable extends Table {
|
||||
}
|
||||
return commandUse;
|
||||
} finally {
|
||||
close(set);
|
||||
close(statement);
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
Benchmark.stop("Database", "Get CommandUse");
|
||||
}
|
||||
}
|
||||
@ -129,7 +129,6 @@ public class CommandUseTable extends Table {
|
||||
}
|
||||
|
||||
updateCommands(updateData);
|
||||
commit();
|
||||
Benchmark.stop("Database", "Save Commanduse");
|
||||
db.setAvailable();
|
||||
}
|
||||
@ -142,7 +141,7 @@ public class CommandUseTable extends Table {
|
||||
"WHERE (" + columnCommand + "=?) AND (" +
|
||||
columnServerID + "=" + serverTable.statementSelectServerID + ")";
|
||||
statement = prepareStatement(updateStatement);
|
||||
boolean commitRequired = false;
|
||||
|
||||
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
|
||||
String key = entrySet.getKey();
|
||||
Integer amount = entrySet.getValue();
|
||||
@ -155,13 +154,11 @@ public class CommandUseTable extends Table {
|
||||
statement.setString(2, key);
|
||||
statement.setString(3, Plan.getServerUUID().toString());
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
|
||||
if (commitRequired) {
|
||||
statement.executeBatch();
|
||||
}
|
||||
statement.executeBatch();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -175,7 +172,6 @@ public class CommandUseTable extends Table {
|
||||
+ columnServerID
|
||||
+ ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")";
|
||||
statement = prepareStatement(insertStatement);
|
||||
boolean addedRows = false;
|
||||
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
|
||||
String key = entrySet.getKey();
|
||||
Integer amount = entrySet.getValue();
|
||||
@ -188,13 +184,11 @@ public class CommandUseTable extends Table {
|
||||
statement.setInt(2, amount);
|
||||
statement.setString(3, Plan.getServerUUID().toString());
|
||||
statement.addBatch();
|
||||
addedRows = true;
|
||||
}
|
||||
|
||||
if (addedRows) {
|
||||
statement.executeBatch();
|
||||
}
|
||||
statement.executeBatch();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -211,7 +205,8 @@ public class CommandUseTable extends Table {
|
||||
}
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
close(statement);
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
||||
@ -227,7 +222,8 @@ public class CommandUseTable extends Table {
|
||||
}
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
close(statement);
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ public class IPsTable extends UserIDTable {
|
||||
|
||||
return stringList;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -84,6 +85,7 @@ public class IPsTable extends UserIDTable {
|
||||
if (ips.contains(ip)) {
|
||||
return;
|
||||
}
|
||||
|
||||
insertIp(uuid, ip, geolocation);
|
||||
}
|
||||
|
||||
@ -102,6 +104,7 @@ public class IPsTable extends UserIDTable {
|
||||
statement.setString(3, geolocation);
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -120,6 +123,7 @@ public class IPsTable extends UserIDTable {
|
||||
}
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,11 @@ public class KillsTable extends UserIDTable {
|
||||
Log.toLog(this.getClass().getName(), ex);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
endTransaction(statement);
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -103,6 +108,7 @@ public class KillsTable extends UserIDTable {
|
||||
}
|
||||
statement.executeBatch();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -136,6 +142,7 @@ public class KillsTable extends UserIDTable {
|
||||
session.getPlayerKills().add(new KillData(victim, weapon, date));
|
||||
}
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ public class NicknamesTable extends UserIDTable {
|
||||
}
|
||||
return nicknames;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -127,6 +128,7 @@ public class NicknamesTable extends UserIDTable {
|
||||
}
|
||||
return nicknames;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -152,6 +154,7 @@ public class NicknamesTable extends UserIDTable {
|
||||
statement.setString(3, displayName);
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
@ -53,6 +53,12 @@ public class SecurityTable extends Table {
|
||||
Log.toLog(this.getClass().getName(), ex);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
endTransaction(statement);
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -72,7 +78,8 @@ public class SecurityTable extends Table {
|
||||
statement.setString(2, saltPassHash);
|
||||
statement.setInt(3, permLevel);
|
||||
statement.execute();
|
||||
commit();
|
||||
|
||||
commit(statement.getConnection());
|
||||
} finally {
|
||||
close(statement);
|
||||
}
|
||||
@ -96,8 +103,8 @@ public class SecurityTable extends Table {
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
close(set);
|
||||
close(statement);
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,8 +124,8 @@ public class SecurityTable extends Table {
|
||||
}
|
||||
return list;
|
||||
} finally {
|
||||
close(set);
|
||||
close(statement);
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,6 +86,7 @@ public class ServerTable extends Table {
|
||||
statement.setInt(5, info.getId());
|
||||
statement.executeUpdate();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -116,6 +117,7 @@ public class ServerTable extends Table {
|
||||
statement.setBoolean(4, true);
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -143,6 +145,7 @@ public class ServerTable extends Table {
|
||||
return Optional.empty();
|
||||
}
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -170,6 +173,7 @@ public class ServerTable extends Table {
|
||||
return Optional.empty();
|
||||
}
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -189,6 +193,7 @@ public class ServerTable extends Table {
|
||||
}
|
||||
return names;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -218,6 +223,7 @@ public class ServerTable extends Table {
|
||||
return Optional.empty();
|
||||
}
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -241,6 +247,7 @@ public class ServerTable extends Table {
|
||||
}
|
||||
return servers;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
@ -111,6 +111,7 @@ public class SessionsTable extends UserIDTable {
|
||||
statement.setString(6, Plan.getServerUUID().toString());
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -139,6 +140,7 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
return -1L;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -177,6 +179,7 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
return sessionsByServer;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -241,6 +244,7 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
return 0;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -286,6 +290,7 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
return playtimes;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -326,6 +331,7 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
return 0;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -393,6 +399,7 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
return 0;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
@ -79,8 +79,8 @@ public class TPSTable extends Table {
|
||||
}
|
||||
return data;
|
||||
} finally {
|
||||
close(set);
|
||||
close(statement);
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
Benchmark.stop("Database", "Get TPS");
|
||||
}
|
||||
}
|
||||
@ -99,7 +99,6 @@ public class TPSTable extends Table {
|
||||
}
|
||||
});
|
||||
db.setAvailable();
|
||||
commit();
|
||||
}
|
||||
|
||||
private void saveTPSBatch(List<TPS> batch) throws SQLException {
|
||||
@ -129,7 +128,9 @@ public class TPSTable extends Table {
|
||||
statement.setDouble(7, tps.getChunksLoaded());
|
||||
statement.addBatch();
|
||||
}
|
||||
|
||||
statement.executeBatch();
|
||||
commit(statement.getConnection());
|
||||
} finally {
|
||||
close(statement);
|
||||
}
|
||||
@ -147,6 +148,7 @@ public class TPSTable extends Table {
|
||||
statement.setLong(1, MiscUtils.getTime() - fiveWeeks);
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
@ -65,11 +65,7 @@ public abstract class Table {
|
||||
* @return @throws SQLException
|
||||
*/
|
||||
protected Connection getConnection() throws SQLException {
|
||||
Connection connection = db.getConnection();
|
||||
if (connection == null || connection.isClosed()) {
|
||||
connection = db.getNewConnection();
|
||||
}
|
||||
return connection;
|
||||
return db.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,11 +85,11 @@ public abstract class Table {
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = connection.createStatement();
|
||||
return statement.execute(statementString);
|
||||
boolean b = statement.execute(statementString);
|
||||
commit(statement.getConnection());
|
||||
return b;
|
||||
} finally {
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,7 +187,19 @@ public abstract class Table {
|
||||
*
|
||||
* @throws SQLException If commit fails or there is nothing to commit.
|
||||
*/
|
||||
protected void commit() throws SQLException {
|
||||
db.commit();
|
||||
protected void commit(Connection connection) throws SQLException {
|
||||
db.commit(connection);
|
||||
}
|
||||
|
||||
protected void endTransaction(Connection connection) throws SQLException {
|
||||
db.endTransaction(connection);
|
||||
}
|
||||
|
||||
protected void endTransaction(Statement statement) throws SQLException {
|
||||
if (statement == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
endTransaction(statement.getConnection());
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,11 @@ public abstract class UserIDTable extends Table {
|
||||
Log.toLog(this.getClass().getName(), ex);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
endTransaction(statement);
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -51,6 +56,11 @@ public abstract class UserIDTable extends Table {
|
||||
Log.toLog(this.getClass().getName(), ex);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
endTransaction(statement);
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package main.java.com.djrapitops.plan.database.tables;
|
||||
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import main.java.com.djrapitops.plan.database.databases.SQLDB;
|
||||
import main.java.com.djrapitops.plan.database.sql.*;
|
||||
|
||||
@ -63,6 +64,7 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
return uuids;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -82,6 +84,11 @@ public class UsersTable extends UserIDTable {
|
||||
} catch (SQLException ex) {
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
endTransaction(statement);
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -117,6 +124,7 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -133,6 +141,7 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
return registerDates;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -160,6 +169,7 @@ public class UsersTable extends UserIDTable {
|
||||
statement.setString(3, name);
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -175,6 +185,7 @@ public class UsersTable extends UserIDTable {
|
||||
set = statement.executeQuery();
|
||||
return set.next();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -189,6 +200,7 @@ public class UsersTable extends UserIDTable {
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -207,6 +219,7 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
return 0;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -221,6 +234,7 @@ public class UsersTable extends UserIDTable {
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.execute();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
@ -50,8 +50,8 @@ public class VersionTable extends Table {
|
||||
Log.debug("Database", "DB Schema version: " + version);
|
||||
return version;
|
||||
} finally {
|
||||
close(set);
|
||||
close(statement);
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,9 +65,9 @@ public class VersionTable extends Table {
|
||||
try {
|
||||
statement = prepareStatement("INSERT INTO " + tableName + " (version) VALUES (" + version + ")");
|
||||
statement.executeUpdate();
|
||||
commit(statement.getConnection());
|
||||
} finally {
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -68,6 +68,7 @@ public class WorldTable extends Table {
|
||||
}
|
||||
return worldNames;
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
@ -94,16 +95,14 @@ public class WorldTable extends Table {
|
||||
statement = prepareStatement("INSERT INTO " + tableName + " ("
|
||||
+ columnWorldName
|
||||
+ ") VALUES (?)");
|
||||
boolean commitRequired = false;
|
||||
for (String world : worlds) {
|
||||
statement.setString(1, world);
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
if (commitRequired) {
|
||||
statement.executeBatch();
|
||||
}
|
||||
|
||||
statement.executeBatch();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,7 @@ public class WorldTimesTable extends UserIDTable {
|
||||
|
||||
statement.executeBatch();
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
@ -144,6 +145,7 @@ public class WorldTimesTable extends UserIDTable {
|
||||
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
|
||||
}
|
||||
} finally {
|
||||
endTransaction(statement);
|
||||
close(set, statement);
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,9 @@ public class ProcessingQueue extends Queue<Processor> {
|
||||
* @param processor processing object.
|
||||
*/
|
||||
public void addToQueue(Processor processor) {
|
||||
queue.offer(processor);
|
||||
if (!queue.offer(processor)) {
|
||||
Log.toLog("ProcessingQueue.addToQueue", new IllegalStateException("Processor was not added to Queue"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -312,7 +312,7 @@ public class WebServer {
|
||||
|
||||
Plan plan = Plan.getInstance();
|
||||
|
||||
if (!checkKey(plan, key)) {
|
||||
if (!checkKey(key)) {
|
||||
String error = "Server Key not given or invalid";
|
||||
return PageCacheHandler.loadPage(error, () -> {
|
||||
ForbiddenResponse forbidden = new ForbiddenResponse();
|
||||
@ -336,8 +336,8 @@ public class WebServer {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkKey(Plan plan, String key) {
|
||||
UUID uuid = plan.getServerInfoManager().getServerUUID();
|
||||
private boolean checkKey(String key) {
|
||||
UUID uuid = Plan.getServerUUID();
|
||||
UUID keyUUID;
|
||||
try {
|
||||
keyUUID = UUID.fromString(key);
|
||||
@ -544,7 +544,7 @@ public class WebServer {
|
||||
return usingHttps ? "https" : "http";
|
||||
}
|
||||
|
||||
public boolean usingHttps() {
|
||||
public boolean isUsingHTTPS() {
|
||||
return usingHttps;
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import main.java.com.djrapitops.plan.data.UserData;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
* @deprecated Will be removed once it's sure that it's unnecessary
|
||||
*/
|
||||
@Deprecated // TODO Remove once sure that this is unnecessary.
|
||||
public class NewPlayerCreator {
|
||||
|
@ -198,7 +198,7 @@ public class DumpUtils {
|
||||
* @param plan The Plan instance
|
||||
*/
|
||||
private static void addConfigurationDetails(DumpLog log, Plan plan) {
|
||||
boolean usingHTTPS = plan.getUiServer().usingHttps();
|
||||
boolean usingHTTPS = plan.getUiServer().isUsingHTTPS();
|
||||
boolean analysisExport = Settings.ANALYSIS_EXPORT.isTrue();
|
||||
boolean usingAlternativeServerIP = Settings.SHOW_ALTERNATIVE_IP.isTrue();
|
||||
|
||||
|
@ -44,11 +44,7 @@ public class QueueTest {
|
||||
public void setUp() throws Exception {
|
||||
TestInit t = TestInit.init();
|
||||
Plan plan = t.getPlanMock();
|
||||
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
|
||||
@Override
|
||||
public void startConnectionPingTask() {
|
||||
}
|
||||
};
|
||||
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
|
||||
db.init();
|
||||
when(plan.getDB()).thenReturn(db);
|
||||
dataCache = new DataCache(plan) {
|
||||
|
@ -29,14 +29,12 @@ public class WorldTimesTest {
|
||||
public void setUp() throws Exception {
|
||||
test = new WorldTimes(worldOne, gms[0]);
|
||||
time = test.getGMTimes(worldOne).getLastStateChange();
|
||||
System.out.println(test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWorldChange() {
|
||||
long changeTime = time + 1000L;
|
||||
test.updateState(worldTwo, gms[0], changeTime);
|
||||
System.out.println(test);
|
||||
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
|
||||
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
|
||||
}
|
||||
@ -45,7 +43,6 @@ public class WorldTimesTest {
|
||||
public void testGMChange() {
|
||||
long changeTime = time + 1000L;
|
||||
test.updateState(worldOne, gms[0], changeTime);
|
||||
System.out.println(test);
|
||||
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
|
||||
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
|
||||
}
|
||||
@ -55,11 +52,9 @@ public class WorldTimesTest {
|
||||
long changeTime = time + 1000L;
|
||||
long changeTime2 = changeTime + 1000L;
|
||||
test.updateState(worldTwo, gms[2], changeTime);
|
||||
System.out.println(test);
|
||||
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
|
||||
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
|
||||
test.updateState(worldOne, gms[1], changeTime2);
|
||||
System.out.println(test);
|
||||
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
|
||||
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
|
||||
assertEquals(1000L, test.getGMTimes(worldTwo).getTime(gms[2]));
|
||||
@ -98,7 +93,6 @@ public class WorldTimesTest {
|
||||
|
||||
long time1 = test.getWorldPlaytime(worldOne);
|
||||
long time2 = test.getWorldPlaytime(worldTwo);
|
||||
System.out.println(test);
|
||||
|
||||
// Tests World time calculation.
|
||||
assertEquals(amount * 50, time1 + time2);
|
||||
@ -156,13 +150,10 @@ public class WorldTimesTest {
|
||||
|
||||
// No change should occur.
|
||||
test.updateState(worldOne, "ADVENTURE", time + 5000L);
|
||||
System.out.println(test);
|
||||
assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE"));
|
||||
assertEquals(1000L, worldTwoGMTimes.getTime("CREATIVE"));
|
||||
test.updateState(worldTwo, "CREATIVE", time + 5000L);
|
||||
System.out.println(test);
|
||||
test.updateState(worldOne, "ADVENTURE", time + 6000L);
|
||||
System.out.println(test);
|
||||
assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE"));
|
||||
assertEquals(2000L, worldTwoGMTimes.getTime("CREATIVE"));
|
||||
|
||||
|
@ -3,7 +3,6 @@ package test.java.main.java.com.djrapitops.plan.database;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.TPS;
|
||||
import main.java.com.djrapitops.plan.data.WebUser;
|
||||
import main.java.com.djrapitops.plan.database.Database;
|
||||
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
|
||||
@ -34,21 +33,18 @@ import static org.junit.Assert.assertTrue;
|
||||
public class DatabaseCommitTest {
|
||||
|
||||
private Plan plan;
|
||||
private Database db;
|
||||
private SQLiteDB db;
|
||||
private int rows;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
TestInit t = TestInit.init();
|
||||
plan = t.getPlanMock();
|
||||
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
|
||||
@Override
|
||||
public void startConnectionPingTask() {
|
||||
|
||||
}
|
||||
};
|
||||
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
|
||||
File f = new File(plan.getDataFolder(), "Errors.txt");
|
||||
rows = FileUtil.lines(f).size();
|
||||
|
||||
db.init();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,9 +68,10 @@ public class DatabaseCommitTest {
|
||||
@Test
|
||||
public void testNoExceptionWhenCommitEmpty() throws SQLException {
|
||||
db.init();
|
||||
db.commit();
|
||||
db.commit();
|
||||
db.commit();
|
||||
|
||||
db.commit(db.getConnection());
|
||||
db.commit(db.getConnection());
|
||||
db.commit(db.getConnection());
|
||||
}
|
||||
|
||||
@Ignore("//TODO")
|
||||
|
@ -61,14 +61,11 @@ public class DatabaseTest {
|
||||
public void setUp() throws Exception {
|
||||
TestInit t = TestInit.init();
|
||||
plan = t.getPlanMock();
|
||||
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
|
||||
@Override
|
||||
public void startConnectionPingTask() {
|
||||
|
||||
}
|
||||
};
|
||||
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
|
||||
File f = new File(plan.getDataFolder(), "Errors.txt");
|
||||
rows = FileUtil.lines(f).size();
|
||||
|
||||
db.init();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,12 +119,7 @@ public class DatabaseTest {
|
||||
*/
|
||||
@Test
|
||||
public void testMysqlGetConfigName() {
|
||||
assertEquals("mysql", new MySQLDB(plan) {
|
||||
@Override
|
||||
public void startConnectionPingTask() {
|
||||
|
||||
}
|
||||
}.getConfigName());
|
||||
assertEquals("mysql", new MySQLDB(plan).getConfigName());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -135,12 +127,7 @@ public class DatabaseTest {
|
||||
*/
|
||||
@Test
|
||||
public void testMysqlGetName() {
|
||||
assertEquals("MySQL", new MySQLDB(plan) {
|
||||
@Override
|
||||
public void startConnectionPingTask() {
|
||||
|
||||
}
|
||||
}.getName());
|
||||
assertEquals("MySQL", new MySQLDB(plan).getName());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -150,8 +137,8 @@ public class DatabaseTest {
|
||||
@Test // TODO Rewrite
|
||||
public void testRemoveAll() throws SQLException {
|
||||
db.init();
|
||||
// UserData data = MockUtils.mockUser();
|
||||
// db.saveUserData(data);
|
||||
//UserData data = MockUtils.mockUser();
|
||||
//db.saveUserData(data);
|
||||
HashMap<String, Integer> c = new HashMap<>();
|
||||
c.put("/plan", 1);
|
||||
c.put("/tp", 4);
|
||||
@ -195,11 +182,13 @@ public class DatabaseTest {
|
||||
*/
|
||||
@Test // TODO Rewrite
|
||||
public void testRemove() throws SQLException {
|
||||
/*
|
||||
db.init();
|
||||
// UserData data = MockUtils.mockUser();
|
||||
// db.saveUserData(data);
|
||||
// assertTrue(db.removeAccount(data.getUuid().toString()));
|
||||
// assertTrue("Contains the user", !db.wasSeenBefore(data.getUuid()));
|
||||
UserData data = MockUtils.mockUser();
|
||||
db.saveUserData(data);
|
||||
assertTrue(db.removeAccount(data.getUuid().toString()));
|
||||
assertTrue("Contains the user", !db.wasSeenBefore(data.getUuid()));
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,7 +213,6 @@ public class DatabaseTest {
|
||||
public void testTPSSaving() throws SQLException {
|
||||
db.init();
|
||||
TPSTable tpsTable = db.getTpsTable();
|
||||
List<TPS> expected = new ArrayList<>();
|
||||
Random r = new Random();
|
||||
|
||||
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
|
||||
@ -235,12 +223,17 @@ public class DatabaseTest {
|
||||
final int entityCount = 6123;
|
||||
final int chunksLoaded = 2134;
|
||||
|
||||
List<TPS> expected = new ArrayList<>();
|
||||
|
||||
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded));
|
||||
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded));
|
||||
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded));
|
||||
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded));
|
||||
|
||||
List<TPS> tpsDataOld = tpsTable.getTPSData();
|
||||
tpsTable.saveTPSData(expected);
|
||||
|
||||
expected.addAll(0, tpsDataOld);
|
||||
assertEquals(expected, tpsTable.getTPSData());
|
||||
}
|
||||
}
|
||||
|
48
Plan/test/test/java/utils/DBTestSuite.java
Normal file
48
Plan/test/test/java/utils/DBTestSuite.java
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package test.java.utils;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import test.java.main.java.com.djrapitops.plan.database.DatabaseCommitTest;
|
||||
import test.java.main.java.com.djrapitops.plan.database.DatabaseTest;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({DatabaseCommitTest.class, DatabaseTest.class})
|
||||
public class DBTestSuite {
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
clean(true);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() throws IOException {
|
||||
clean(false);
|
||||
}
|
||||
|
||||
private static void clean(boolean dbOnly) throws IOException {
|
||||
File testFolder = TestInit.getTestFolder();
|
||||
|
||||
if (!testFolder.exists() || !testFolder.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (File f : testFolder.listFiles()) {
|
||||
if (dbOnly && !f.getName().contains(".db")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,6 @@ import org.powermock.api.mockito.PowerMockito;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.powermock.api.mockito.PowerMockito.when;
|
||||
@ -63,17 +62,11 @@ public class TestInit {
|
||||
|
||||
public static TestInit init() throws Exception {
|
||||
TestInit t = new TestInit();
|
||||
t.setUp(true);
|
||||
t.setUp();
|
||||
return t;
|
||||
}
|
||||
|
||||
public static TestInit init(boolean clearOnStart) throws Exception {
|
||||
TestInit t = new TestInit();
|
||||
t.setUp(clearOnStart);
|
||||
return t;
|
||||
}
|
||||
|
||||
private void setUp(boolean clearOnStart) throws Exception {
|
||||
private void setUp() throws Exception {
|
||||
planMock = PowerMockito.mock(Plan.class);
|
||||
StaticHolder.setInstance(Plan.class, planMock);
|
||||
StaticHolder.setInstance(planMock.getClass(), planMock);
|
||||
@ -82,9 +75,6 @@ public class TestInit {
|
||||
when(planMock.getConfig()).thenReturn(config);
|
||||
|
||||
File testFolder = getTestFolder();
|
||||
if (clearOnStart) {
|
||||
clean(testFolder);
|
||||
}
|
||||
when(planMock.getDataFolder()).thenReturn(testFolder);
|
||||
|
||||
// Html Files
|
||||
@ -119,7 +109,7 @@ public class TestInit {
|
||||
}
|
||||
|
||||
private RunnableFactory<Plan> mockRunnableFactory() {
|
||||
RunnableFactory<Plan> runnableFactory = new RunnableFactory<Plan>(planMock) {
|
||||
return new RunnableFactory<Plan>(planMock) {
|
||||
@Override
|
||||
public IRunnable createNew(String name, final AbsRunnable runnable) {
|
||||
return new IRunnable() {
|
||||
@ -170,27 +160,14 @@ public class TestInit {
|
||||
};
|
||||
}
|
||||
};
|
||||
return runnableFactory;
|
||||
}
|
||||
|
||||
private static File getTestFolder() {
|
||||
static File getTestFolder() {
|
||||
File testFolder = new File("temporaryTestFolder");
|
||||
testFolder.mkdir();
|
||||
return testFolder;
|
||||
}
|
||||
|
||||
public static void clean() throws IOException {
|
||||
clean(getTestFolder());
|
||||
}
|
||||
|
||||
public static void clean(File testFolder) throws IOException {
|
||||
if (testFolder.exists() && testFolder.isDirectory()) {
|
||||
for (File f : testFolder.listFiles()) {
|
||||
Files.deleteIfExists(f.toPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Server mockServer() {
|
||||
Server mockServer = PowerMockito.mock(Server.class);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user