Refactored Database system.

This commit is contained in:
Rsl1122 2017-12-07 18:29:22 +02:00
parent a598ad75fc
commit 71c61cb469
19 changed files with 232 additions and 107 deletions

View File

@ -34,20 +34,18 @@ import com.djrapitops.plugin.settings.ColorScheme;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.ITask;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.API;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.command.PlanCommand;
import main.java.com.djrapitops.plan.data.plugin.HookHandler;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.MySQLDB;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.settings.locale.Locale;
import main.java.com.djrapitops.plan.settings.locale.Msg;
import main.java.com.djrapitops.plan.settings.theme.Theme;
import main.java.com.djrapitops.plan.systems.DatabaseSystem;
import main.java.com.djrapitops.plan.systems.Systems;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.systems.cache.GeolocationCache;
import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager;
@ -70,9 +68,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
/**
@ -91,12 +87,11 @@ public class Plan extends BukkitPlugin implements IPlan {
private Config config;
private Theme theme;
private Systems systems;
private ProcessingQueue processingQueue;
private HookHandler hookHandler; // Manages 3rd party data sources
private Database db;
private Set<Database> databases;
private WebServer webServer;
private BukkitInformationManager infoManager;
@ -191,10 +186,8 @@ public class Plan extends BukkitPlugin implements IPlan {
serverVariableHolder = new ServerVariableHolder(getServer());
Benchmark.stop("Enable", "Reading server variables");
Benchmark.start("Init Database");
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString());
initDatabase();
Benchmark.stop("Enable", "Init Database");
systems = new Systems();
systems.init();
Benchmark.start("WebServer Initialization");
webServer = new WebServer(this);
@ -382,34 +375,6 @@ public class Plan extends BukkitPlugin implements IPlan {
Benchmark.stop("Enable", "Register Listeners");
}
/**
* Initializes the database according to settings in the config.
* <p>
* If database connection can not be established plugin is disabled.
*/
private void initDatabase() throws DatabaseInitException {
databases = new HashSet<>();
databases.add(new MySQLDB(this));
databases.add(new SQLiteDB(this));
String dbType = Settings.DB_TYPE.toString().toLowerCase().trim();
for (Database database : databases) {
String databaseType = database.getConfigName().toLowerCase().trim();
if (Verify.equalsIgnoreCase(dbType, databaseType)) {
this.db = database;
break;
}
}
if (db == null) {
throw new DatabaseInitException(Locale.get(Msg.ENABLE_FAIL_WRONG_DB).toString() + " " + dbType);
}
db.init();
Log.info(Locale.get(Msg.ENABLE_DB_INFO).parse(db.getConfigName()));
}
/**
* Used to access Cache.
*
@ -424,8 +389,9 @@ public class Plan extends BukkitPlugin implements IPlan {
*
* @return the Current Database
*/
@Deprecated
public Database getDB() {
return db;
return DatabaseSystem.getInstance().getActiveDatabase();
}
/**
@ -446,17 +412,6 @@ public class Plan extends BukkitPlugin implements IPlan {
return hookHandler;
}
/**
* Used to get all possible database objects.
* <p>
* #init() might need to be called in order for the object to function.
*
* @return Set containing the SqLite and MySQL objects.
*/
public Set<Database> getDatabases() {
return databases;
}
/**
* Used to get the ID of the BootAnalysisTask, so that it can be disabled.
*
@ -581,4 +536,8 @@ public class Plan extends BukkitPlugin implements IPlan {
public Theme getTheme() {
return theme;
}
public Systems getSystems() {
return systems;
}
}

View File

@ -194,7 +194,7 @@ public class PlanBungee extends BungeePlugin implements IPlan {
}
private void initDatabase() throws DatabaseInitException {
db = new MySQLDB(this);
db = new MySQLDB();
db.init();
}

View File

@ -54,7 +54,7 @@ public class ManageBackupCommand extends SubCommand {
return true;
}
final Database database = ManageUtils.getDB(plugin, dbName);
final Database database = ManageUtils.getDB(dbName);
// If DB is null return
if (!Condition.isTrue(Verify.notNull(database), Locale.get(Msg.MANAGE_FAIL_FAULTY_DB).toString(), sender)) {

View File

@ -71,7 +71,7 @@ public class ManageClearCommand extends SubCommand {
}
try {
Database database = ManageUtils.getDB(plugin, dbName);
Database database = ManageUtils.getDB(dbName);
runClearTask(sender, database);
} catch (DatabaseInitException e) {
sender.sendMessage(Locale.get(Msg.MANAGE_FAIL_FAULTY_DB).toString());

View File

@ -66,7 +66,7 @@ public class ManageHotswapCommand extends SubCommand {
}
try {
final Database database = ManageUtils.getDB(plugin, dbName);
final Database database = ManageUtils.getDB(dbName);
// If DB is null return
if (!Condition.isTrue(Verify.notNull(database), Locale.get(Msg.MANAGE_FAIL_FAULTY_DB).toString(), sender)) {

View File

@ -71,8 +71,8 @@ public class ManageMoveCommand extends SubCommand {
}
try {
final Database fromDatabase = ManageUtils.getDB(plugin, fromDB);
final Database toDatabase = ManageUtils.getDB(plugin, toDB);
final Database fromDatabase = ManageUtils.getDB(fromDB);
final Database toDatabase = ManageUtils.getDB(toDB);
runMoveTask(fromDatabase, toDatabase, sender);
} catch (Exception e) {

View File

@ -61,7 +61,7 @@ public class ManageRestoreCommand extends SubCommand {
}
try {
final Database database = ManageUtils.getDB(plugin, db);
final Database database = ManageUtils.getDB(db);
runRestoreTask(args, sender, database);
} catch (Exception e) {
@ -87,7 +87,7 @@ public class ManageRestoreCommand extends SubCommand {
backupDBName = backupDBName.substring(0, backupDBName.length() - 3);
}
SQLiteDB backupDB = new SQLiteDB(plugin, backupDBName);
SQLiteDB backupDB = new SQLiteDB(backupDBName);
backupDB.init();
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_START).parse());

View File

@ -1,6 +1,5 @@
package main.java.com.djrapitops.plan.database;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.data.PlayerProfile;
import main.java.com.djrapitops.plan.data.ServerProfile;
@ -23,7 +22,6 @@ import java.util.UUID;
*/
public abstract class Database {
protected final IPlan plugin;
protected UsersTable usersTable;
protected UserInfoTable userInfoTable;
protected ActionsTable actionsTable;
@ -43,11 +41,8 @@ public abstract class Database {
/**
* Super constructor.
*
* @param plugin current instance of Plan.
*/
public Database(IPlan plugin) {
this.plugin = plugin;
public Database() {
}
/**

View File

@ -1,7 +1,6 @@
package main.java.com.djrapitops.plan.database.databases;
import com.djrapitops.plugin.api.utility.log.Log;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.settings.Settings;
import org.apache.commons.dbcp2.BasicDataSource;
@ -16,15 +15,6 @@ public class MySQLDB extends SQLDB {
private BasicDataSource dataSource;
/**
* Class Constructor.
*
* @param plugin Current instance of Plan
*/
public MySQLDB(IPlan plugin) {
super(plugin);
}
/**
* Setups the {@link BasicDataSource}
*/

View File

@ -6,7 +6,6 @@ import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.ITask;
import com.djrapitops.plugin.task.RunnableFactory;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.data.PlayerProfile;
import main.java.com.djrapitops.plan.data.ServerProfile;
@ -33,8 +32,7 @@ public abstract class SQLDB extends Database {
private boolean open = false;
private ITask dbCleanTask;
public SQLDB(IPlan plugin) {
super(plugin);
public SQLDB() {
usingMySQL = getName().equals("MySQL");
versionTable = new VersionTable(this, usingMySQL);

View File

@ -4,7 +4,6 @@ import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.ITask;
import com.djrapitops.plugin.task.RunnableFactory;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.apache.commons.dbcp2.BasicDataSource;
@ -26,15 +25,12 @@ public class SQLiteDB extends SQLDB {
/**
* Class Constructor.
*
* @param plugin Current instance of Plan
*/
public SQLiteDB(Plan plugin) {
this(plugin, "database");
public SQLiteDB() {
this("database");
}
public SQLiteDB(Plan plugin, String dbName) {
super(plugin);
public SQLiteDB(String dbName) {
this.dbName = dbName;
}
@ -59,7 +55,7 @@ public class SQLiteDB extends SQLDB {
return null; // Should never happen.
}
String dbFilePath = new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath();
String dbFilePath = new File(MiscUtils.getIPlan().getDataFolder(), dbName + ".db").getAbsolutePath();
Connection connection;
try {

View File

@ -0,0 +1,104 @@
/*
* 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.systems;
import com.djrapitops.plugin.api.Benchmark;
import com.djrapitops.plugin.api.Check;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.MySQLDB;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.settings.locale.Locale;
import main.java.com.djrapitops.plan.settings.locale.Msg;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class DatabaseSystem implements SubSystem {
private Database db;
private Set<Database> databases;
@Override
public void init() throws PlanEnableException {
try {
Benchmark.start("Init Database");
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString());
initDatabase();
Log.info(Locale.get(Msg.ENABLE_DB_INFO).parse(db.getConfigName()));
Benchmark.stop("Systems", "Init Database");
} catch (DatabaseInitException e) {
throw new PlanEnableException(db.getConfigName() + "-Database failed to initialize", e);
}
}
private void initDatabase() throws DatabaseInitException {
if (Check.isBungeeAvailable()) {
db = new MySQLDB();
} else {
// Enables database on Bukkit
// Bukkit supports SQLite.
initPlanDatabase();
}
db.init();
}
private void initPlanDatabase() throws DatabaseInitException {
databases = new HashSet<>();
databases.add(new MySQLDB());
databases.add(new SQLiteDB());
String dbType = Settings.DB_TYPE.toString().toLowerCase().trim();
db = getActiveDatabase(dbType);
}
public Set<Database> getDatabases() {
return databases;
}
public void setDatabases(Set<Database> databases) {
this.databases = databases;
}
@Override
public void close() {
try {
if (db != null) {
db.close();
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
public static DatabaseSystem getInstance() {
return Systems.getInstance().databaseSystem;
}
public Database getActiveDatabase() {
return db;
}
public static Database getActiveDatabase(String dbName) throws DatabaseInitException {
for (Database database : DatabaseSystem.getInstance().getDatabases()) {
String dbConfigName = database.getConfigName();
if (Verify.equalsIgnoreCase(dbName, dbConfigName)) {
database.init();
return database;
}
}
throw new DatabaseInitException(Locale.get(Msg.ENABLE_FAIL_WRONG_DB) + " " + dbName);
}
}

View File

@ -0,0 +1,25 @@
/*
* 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.systems;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class FileSystem implements SubSystem {
@Override
public void init() throws PlanEnableException {
}
@Override
public void close() {
}
}

View File

@ -0,0 +1,20 @@
/*
* 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.systems;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public interface SubSystem {
void init() throws PlanEnableException;
void close();
}

View File

@ -0,0 +1,46 @@
/*
* 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.systems;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
/**
* Layer for reducing
*
* @author Rsl1122
*/
public class Systems implements SubSystem {
DatabaseSystem databaseSystem;
public Systems() {
this.databaseSystem = new DatabaseSystem();
}
private SubSystem[] getSubSystems() {
return new SubSystem[]{
databaseSystem
};
}
@Override
public void init() throws PlanEnableException {
for (SubSystem subSystem : getSubSystems()) {
subSystem.init();
}
}
@Override
public void close() {
for (SubSystem subSystem : getSubSystems()) {
subSystem.close();
}
}
public static Systems getInstance() {
return Plan.getInstance().getSystems();
}
}

View File

@ -98,7 +98,7 @@ public class DebugPageResponse extends ErrorResponse {
String javaVMVersion = properties.getProperty("java.vm.version");
List<String> javaVMFlags = runtimeMxBean.getInputArguments();
content.append("**Operating System:** ").append(osName).append(" (").append(osArch)
content.append("**Operating SubSystem:** ").append(osName).append(" (").append(osArch)
.append(") version ").append(osVersion).append("<br>");
content.append("**Java Version:** ").append(javaVersion).append(", ").append(javaVendor).append("<br>");

View File

@ -1,12 +1,12 @@
package main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.database.tables.move.BatchOperationTable;
import main.java.com.djrapitops.plan.systems.DatabaseSystem;
import java.sql.SQLException;
import java.util.*;
@ -33,7 +33,7 @@ public class ManageUtils {
Plan plugin = Plan.getInstance();
String timeStamp = new Date().toString().substring(4, 10).replace(" ", "-");
String fileName = dbName + "-backup-" + timeStamp;
SQLiteDB backupDB = new SQLiteDB(plugin, fileName);
SQLiteDB backupDB = new SQLiteDB(fileName);
Collection<UUID> uuids = ManageUtils.getUUIDS(copyFromDB);
if (uuids.isEmpty()) {
return;
@ -74,16 +74,8 @@ public class ManageUtils {
fromDB.copyEverything(toDB);
}
public static Database getDB(Plan plugin, String dbName) throws DatabaseInitException {
Database database = null;
for (Database sqldb : plugin.getDatabases()) {
String dbConfigName = sqldb.getConfigName();
if (Verify.equalsIgnoreCase(dbName, dbConfigName)) {
database = sqldb;
database.init();
break;
}
}
return database;
@Deprecated
public static Database getDB(String dbName) throws DatabaseInitException {
return DatabaseSystem.getActiveDatabase(dbName);
}
}

View File

@ -69,7 +69,7 @@ public class DatabaseTest {
TestInit t = TestInit.init();
plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
db = new SQLiteDB("debug" + MiscUtils.getTime());
db.init();
when(plan.getDB()).thenReturn(db);
@ -132,12 +132,12 @@ public class DatabaseTest {
@Test
public void testMySQLGetConfigName() {
assertEquals("mysql", new MySQLDB(plan).getConfigName());
assertEquals("mysql", new MySQLDB().getConfigName());
}
@Test
public void testMySQLGetName() {
assertEquals("MySQL", new MySQLDB(plan).getName());
assertEquals("MySQL", new MySQLDB().getName());
}
@Test(timeout = 3000)
@ -776,7 +776,7 @@ public class DatabaseTest {
@Test
public void testBackupAndRestore() throws SQLException, DatabaseInitException {
SQLiteDB backup = new SQLiteDB(plan, "debug-backup" + MiscUtils.getTime());
SQLiteDB backup = new SQLiteDB("debug-backup" + MiscUtils.getTime());
backup.init();
saveAllData(db);

View File

@ -163,7 +163,7 @@ public class MiscUtilsTest {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
db = new SQLiteDB("debug" + MiscUtils.getTime());
db.init();
db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, TestInit.getServerUUID(), "ServerName", "", 20));