1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-01-04 23:37:49 +01:00

Call some database things in async to do not cause server freeze on startup

Fixes #981
This commit is contained in:
montlikadani 2020-11-05 18:26:27 +01:00
parent 94189783bc
commit f7313be846
3 changed files with 54 additions and 49 deletions

View File

@ -63,6 +63,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger; import java.util.logging.Logger;
public class Jobs extends JavaPlugin { public class Jobs extends JavaPlugin {
@ -465,31 +466,35 @@ public class Jobs extends JavaPlugin {
} }
public static void loadAllPlayersData() { public static void loadAllPlayersData() {
long time = System.currentTimeMillis(); CompletableFuture.supplyAsync(() -> {
// Cloning to avoid issues long time = System.currentTimeMillis();
HashMap<UUID, PlayerInfo> temp = new HashMap<>(getPlayerManager().getPlayersInfoUUIDMap()); // Cloning to avoid issues
HashMap<Integer, List<JobsDAOData>> playersJobs = dao.getAllJobs(); HashMap<UUID, PlayerInfo> temp = new HashMap<>(getPlayerManager().getPlayersInfoUUIDMap());
HashMap<Integer, PlayerPoints> playersPoints = dao.getAllPoints(); HashMap<Integer, List<JobsDAOData>> playersJobs = dao.getAllJobs();
HashMap<Integer, HashMap<String, Log>> playersLogs = dao.getAllLogs(); HashMap<Integer, PlayerPoints> playersPoints = dao.getAllPoints();
HashMap<Integer, ArchivedJobs> playersArchives = dao.getAllArchivedJobs(); HashMap<Integer, HashMap<String, Log>> playersLogs = dao.getAllLogs();
HashMap<Integer, PaymentData> playersLimits = dao.loadPlayerLimits(); HashMap<Integer, ArchivedJobs> playersArchives = dao.getAllArchivedJobs();
for (Iterator<PlayerInfo> it = temp.values().iterator(); it.hasNext();) { HashMap<Integer, PaymentData> playersLimits = dao.loadPlayerLimits();
PlayerInfo one = it.next(); for (Iterator<PlayerInfo> it = temp.values().iterator(); it.hasNext();) {
int id = one.getID(); PlayerInfo one = it.next();
JobsPlayer jPlayer = getPlayerManager().getJobsPlayerOffline( int id = one.getID();
one, JobsPlayer jPlayer = getPlayerManager().getJobsPlayerOffline(
playersJobs.get(id), one,
playersPoints.get(id), playersJobs.get(id),
playersLogs.get(id), playersPoints.get(id),
playersArchives.get(id), playersLogs.get(id),
playersLimits.get(id)); playersArchives.get(id),
if (jPlayer != null) playersLimits.get(id));
getPlayerManager().addPlayerToCache(jPlayer); if (jPlayer != null)
} getPlayerManager().addPlayerToCache(jPlayer);
}
if (!getPlayerManager().getPlayersCache().isEmpty()) return time;
consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " + }).thenAccept(t -> {
((int) (((System.currentTimeMillis() - time) / 1000d) * 100) / 100D)); if (!getPlayerManager().getPlayersCache().isEmpty())
consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " +
((int) (((System.currentTimeMillis() - t) / 1000d) * 100) / 100D));
});
} }
/** /**

View File

@ -73,7 +73,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -105,7 +105,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -134,7 +134,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -163,7 +163,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -193,7 +193,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -224,7 +224,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -253,7 +253,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -285,7 +285,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -312,7 +312,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -342,7 +342,7 @@ public abstract class JobsDAO {
@Override @Override
public String getCollumn() { public String getCollumn() {
return this.name(); return name();
} }
@Override @Override
@ -403,9 +403,9 @@ public abstract class JobsDAO {
private String getQR() { private String getQR() {
switch (dbType) { switch (dbType) {
case MySQL: case MySQL:
return this.mySQL.replace("[tableName]", prefix + this.tableName); return mySQL.replace("[tableName]", prefix + tableName);
case SqLite: case SqLite:
return this.sQlite.replace("[tableName]", this.tableName); return sQlite.replace("[tableName]", tableName);
default: default:
break; break;
} }
@ -415,7 +415,7 @@ public abstract class JobsDAO {
public String getQuery() { public String getQuery() {
String rp = ""; String rp = "";
List<JobsTableInterface> uniques = new ArrayList<>(); List<JobsTableInterface> uniques = new ArrayList<>();
for (JobsTableInterface one : this.getInterface()) { for (JobsTableInterface one : getInterface()) {
if (one.isUnique()) { if (one.isUnique()) {
uniques.add(one); uniques.add(one);
} }
@ -449,7 +449,7 @@ public abstract class JobsDAO {
} }
public JobsTableInterface[] getInterface() { public JobsTableInterface[] getInterface() {
return this.c; return c;
} }
public String getTableName() { public String getTableName() {
@ -536,10 +536,10 @@ public abstract class JobsDAO {
} }
private boolean createDefaultTable(DBTables table) { private boolean createDefaultTable(DBTables table) {
if (this.isTable(table.getTableName())) if (isTable(table.getTableName()))
return true; return true;
try { try {
this.createTable(table.getQuery()); createTable(table.getQuery());
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -550,9 +550,9 @@ public abstract class JobsDAO {
private boolean checkDefaultCollumns() { private boolean checkDefaultCollumns() {
for (DBTables one : DBTables.values()) { for (DBTables one : DBTables.values()) {
for (JobsTableInterface oneT : one.getInterface()) { for (JobsTableInterface oneT : one.getInterface()) {
if (this.isCollumn(one.getTableName(), oneT.getCollumn())) if (isCollumn(one.getTableName(), oneT.getCollumn()))
continue; continue;
this.addCollumn(one.getTableName(), oneT.getCollumn(), oneT.getType()); addCollumn(one.getTableName(), oneT.getCollumn(), oneT.getType());
} }
} }
@ -561,7 +561,7 @@ public abstract class JobsDAO {
public void truncateAllTables() { public void truncateAllTables() {
for (DBTables one : DBTables.values()) { for (DBTables one : DBTables.values()) {
this.truncate(one.getTableName()); truncate(one.getTableName());
} }
} }
@ -1487,7 +1487,7 @@ public abstract class JobsDAO {
} }
public void continueConvertions(List<Convert> list) throws SQLException { public void continueConvertions(List<Convert> list) throws SQLException {
JobsConnection conns = this.getConnection(); JobsConnection conns = getConnection();
if (conns == null) if (conns == null)
return; return;
PreparedStatement insert = null; PreparedStatement insert = null;

View File

@ -28,7 +28,7 @@ public class JobsSQLite extends JobsDAO {
super(plugin, "org.sqlite.JDBC", "jdbc:sqlite:" + new File(file, "jobs.sqlite.db").getPath(), null, null, ""); super(plugin, "org.sqlite.JDBC", "jdbc:sqlite:" + new File(file, "jobs.sqlite.db").getPath(), null, null, "");
if (!file.exists()) if (!file.exists())
file.mkdirs(); file.mkdirs();
this.setDbType(DataBaseType.SqLite); setDbType(DataBaseType.SqLite);
} }
@Override @Override
@ -89,7 +89,7 @@ public class JobsSQLite extends JobsDAO {
public boolean isTable(String table) { public boolean isTable(String table) {
DatabaseMetaData md = null; DatabaseMetaData md = null;
try { try {
md = this.getConnection().getMetaData(); md = getConnection().getMetaData();
ResultSet tables = md.getTables(null, null, table, null); ResultSet tables = md.getTables(null, null, table, null);
if (tables.next()) { if (tables.next()) {
tables.close(); tables.close();
@ -107,7 +107,7 @@ public class JobsSQLite extends JobsDAO {
public boolean isCollumn(String table, String collumn) { public boolean isCollumn(String table, String collumn) {
DatabaseMetaData md = null; DatabaseMetaData md = null;
try { try {
md = this.getConnection().getMetaData(); md = getConnection().getMetaData();
ResultSet tables = md.getColumns(null, null, table, collumn); ResultSet tables = md.getColumns(null, null, table, collumn);
if (tables.next()) { if (tables.next()) {
tables.close(); tables.close();
@ -145,7 +145,7 @@ public class JobsSQLite extends JobsDAO {
Statement statement = null; Statement statement = null;
String query = null; String query = null;
try { try {
if (!this.isTable(table)) { if (!isTable(table)) {
Jobs.consoleMsg("&cTable \"" + table + "\" does not exist."); Jobs.consoleMsg("&cTable \"" + table + "\" does not exist.");
return false; return false;
} }
@ -168,7 +168,7 @@ public class JobsSQLite extends JobsDAO {
Statement statement = null; Statement statement = null;
String query = null; String query = null;
try { try {
if (!this.isTable(table)) { if (!isTable(table)) {
Jobs.consoleMsg("&cTable \"" + table + "\" does not exist."); Jobs.consoleMsg("&cTable \"" + table + "\" does not exist.");
return false; return false;
} }