1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-01-02 14:29:07 +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.sql.SQLException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
public class Jobs extends JavaPlugin {
@ -465,31 +466,35 @@ public class Jobs extends JavaPlugin {
}
public static void loadAllPlayersData() {
long time = System.currentTimeMillis();
// Cloning to avoid issues
HashMap<UUID, PlayerInfo> temp = new HashMap<>(getPlayerManager().getPlayersInfoUUIDMap());
HashMap<Integer, List<JobsDAOData>> playersJobs = dao.getAllJobs();
HashMap<Integer, PlayerPoints> playersPoints = dao.getAllPoints();
HashMap<Integer, HashMap<String, Log>> playersLogs = dao.getAllLogs();
HashMap<Integer, ArchivedJobs> playersArchives = dao.getAllArchivedJobs();
HashMap<Integer, PaymentData> playersLimits = dao.loadPlayerLimits();
for (Iterator<PlayerInfo> it = temp.values().iterator(); it.hasNext();) {
PlayerInfo one = it.next();
int id = one.getID();
JobsPlayer jPlayer = getPlayerManager().getJobsPlayerOffline(
one,
playersJobs.get(id),
playersPoints.get(id),
playersLogs.get(id),
playersArchives.get(id),
playersLimits.get(id));
if (jPlayer != null)
getPlayerManager().addPlayerToCache(jPlayer);
}
CompletableFuture.supplyAsync(() -> {
long time = System.currentTimeMillis();
// Cloning to avoid issues
HashMap<UUID, PlayerInfo> temp = new HashMap<>(getPlayerManager().getPlayersInfoUUIDMap());
HashMap<Integer, List<JobsDAOData>> playersJobs = dao.getAllJobs();
HashMap<Integer, PlayerPoints> playersPoints = dao.getAllPoints();
HashMap<Integer, HashMap<String, Log>> playersLogs = dao.getAllLogs();
HashMap<Integer, ArchivedJobs> playersArchives = dao.getAllArchivedJobs();
HashMap<Integer, PaymentData> playersLimits = dao.loadPlayerLimits();
for (Iterator<PlayerInfo> it = temp.values().iterator(); it.hasNext();) {
PlayerInfo one = it.next();
int id = one.getID();
JobsPlayer jPlayer = getPlayerManager().getJobsPlayerOffline(
one,
playersJobs.get(id),
playersPoints.get(id),
playersLogs.get(id),
playersArchives.get(id),
playersLimits.get(id));
if (jPlayer != null)
getPlayerManager().addPlayerToCache(jPlayer);
}
if (!getPlayerManager().getPlayersCache().isEmpty())
consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " +
((int) (((System.currentTimeMillis() - time) / 1000d) * 100) / 100D));
return time;
}).thenAccept(t -> {
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
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -105,7 +105,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -134,7 +134,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -163,7 +163,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -193,7 +193,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -224,7 +224,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -253,7 +253,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -285,7 +285,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -312,7 +312,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -342,7 +342,7 @@ public abstract class JobsDAO {
@Override
public String getCollumn() {
return this.name();
return name();
}
@Override
@ -403,9 +403,9 @@ public abstract class JobsDAO {
private String getQR() {
switch (dbType) {
case MySQL:
return this.mySQL.replace("[tableName]", prefix + this.tableName);
return mySQL.replace("[tableName]", prefix + tableName);
case SqLite:
return this.sQlite.replace("[tableName]", this.tableName);
return sQlite.replace("[tableName]", tableName);
default:
break;
}
@ -415,7 +415,7 @@ public abstract class JobsDAO {
public String getQuery() {
String rp = "";
List<JobsTableInterface> uniques = new ArrayList<>();
for (JobsTableInterface one : this.getInterface()) {
for (JobsTableInterface one : getInterface()) {
if (one.isUnique()) {
uniques.add(one);
}
@ -449,7 +449,7 @@ public abstract class JobsDAO {
}
public JobsTableInterface[] getInterface() {
return this.c;
return c;
}
public String getTableName() {
@ -536,10 +536,10 @@ public abstract class JobsDAO {
}
private boolean createDefaultTable(DBTables table) {
if (this.isTable(table.getTableName()))
if (isTable(table.getTableName()))
return true;
try {
this.createTable(table.getQuery());
createTable(table.getQuery());
return true;
} catch (SQLException e) {
e.printStackTrace();
@ -550,9 +550,9 @@ public abstract class JobsDAO {
private boolean checkDefaultCollumns() {
for (DBTables one : DBTables.values()) {
for (JobsTableInterface oneT : one.getInterface()) {
if (this.isCollumn(one.getTableName(), oneT.getCollumn()))
if (isCollumn(one.getTableName(), oneT.getCollumn()))
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() {
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 {
JobsConnection conns = this.getConnection();
JobsConnection conns = getConnection();
if (conns == null)
return;
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, "");
if (!file.exists())
file.mkdirs();
this.setDbType(DataBaseType.SqLite);
setDbType(DataBaseType.SqLite);
}
@Override
@ -89,7 +89,7 @@ public class JobsSQLite extends JobsDAO {
public boolean isTable(String table) {
DatabaseMetaData md = null;
try {
md = this.getConnection().getMetaData();
md = getConnection().getMetaData();
ResultSet tables = md.getTables(null, null, table, null);
if (tables.next()) {
tables.close();
@ -107,7 +107,7 @@ public class JobsSQLite extends JobsDAO {
public boolean isCollumn(String table, String collumn) {
DatabaseMetaData md = null;
try {
md = this.getConnection().getMetaData();
md = getConnection().getMetaData();
ResultSet tables = md.getColumns(null, null, table, collumn);
if (tables.next()) {
tables.close();
@ -145,7 +145,7 @@ public class JobsSQLite extends JobsDAO {
Statement statement = null;
String query = null;
try {
if (!this.isTable(table)) {
if (!isTable(table)) {
Jobs.consoleMsg("&cTable \"" + table + "\" does not exist.");
return false;
}
@ -168,7 +168,7 @@ public class JobsSQLite extends JobsDAO {
Statement statement = null;
String query = null;
try {
if (!this.isTable(table)) {
if (!isTable(table)) {
Jobs.consoleMsg("&cTable \"" + table + "\" does not exist.");
return false;
}