Mysql speed improved.

Data file will now periodically back up to prevent data loss.
To be saved data is now cached before executed all at once as to prevent the possibility of data loss.
This commit is contained in:
Brianna O'Keefe 2018-11-01 00:04:27 -04:00
parent feaa4b697c
commit 35b3423119
4 changed files with 77 additions and 35 deletions

View File

@ -433,12 +433,13 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
}
private void saveToFile() {
this.storage.closeConnection();
checkStorage();
//ToDO: If the defaults are set correctly this could do the initial config save.
// Save spawner settings
FileConfiguration spawnerConfig = spawnerFile.getConfig();
spawnerConfig.set("Entities", null);
@ -504,7 +505,7 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
this.spawnerFile.saveConfig();
storage.clearFile();
// Save game data
for (Spawner spawner : spawnerManager.getSpawners()) {
if (spawner.getFirstStack() == null
@ -522,22 +523,23 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
StorageItem placedBy = spawner.getPlacedBy() != null ? new StorageItem("placedby", spawner.getPlacedBy().getUniqueId().toString()) : null;
storage.saveItem("spawners", location, stacks, new StorageItem("spawns", spawner.getSpawnCount()), placedBy);
storage.prepareSaveItem("spawners", location, stacks, new StorageItem("spawns", spawner.getSpawnCount()), placedBy);
}
for (BoostData boostData : boostManager.getBoosts()) {
storage.saveItem("boosts", new StorageItem("endtime", String.valueOf(boostData.getEndTime())),
storage.prepareSaveItem("boosts", new StorageItem("endtime", String.valueOf(boostData.getEndTime())),
new StorageItem("boosttype", boostData.getBoostType().name()),
new StorageItem("data", boostData.getData()),
new StorageItem("amount", boostData.getAmtBoosted()));
}
for (PlayerData playerData : playerActionManager.getRegisteredPlayers()) {
storage.saveItem("players", new StorageItem("uuid", playerData.getPlayer().getUniqueId().toString()),
storage.prepareSaveItem("players", new StorageItem("uuid", playerData.getPlayer().getUniqueId().toString()),
new StorageItem("entitykills", playerData.getEntityKills()));
}
storage.doSave();
}
private <T extends Enum<T>> String[] getStrings(List<T> mats) {

View File

@ -22,9 +22,9 @@ public abstract class Storage {
public abstract List<StorageRow> getRowsByGroup(String group);
public abstract void clearFile();
public abstract void prepareSaveItem(String group, StorageItem... items);
public abstract void saveItem(String group, StorageItem... items);
public abstract void doSave();
public abstract void closeConnection();

View File

@ -9,6 +9,7 @@ import com.songoda.epicspawners.utils.MySQLDatabase;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -17,6 +18,7 @@ import java.util.Map;
public class StorageMysql extends Storage {
private MySQLDatabase database;
private static List<String> toSave = new ArrayList<>();
public StorageMysql(EpicSpawnersPlugin instance) {
super(instance);
@ -62,19 +64,7 @@ public class StorageMysql extends Storage {
}
@Override
public void clearFile() {
try {
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "spawners`");
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "boosts`");
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "players`");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void saveItem(String group, StorageItem... items) {
try {
public void prepareSaveItem(String group, StorageItem... items) {
StringBuilder sql = new StringBuilder(String.format("INSERT INTO `" + instance.getConfig().getString("Database.Prefix") + "%s`", group));
sql.append(" (");
@ -97,7 +87,27 @@ public class StorageMysql extends Storage {
sql.append(");");
database.getConnection().createStatement().execute(sql.toString());
toSave.add(sql.toString());
}
@Override
public void doSave() {
try {
// Clear database
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "spawners`");
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "boosts`");
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "players`");
Statement stmt = database.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
for (String line : toSave) {
stmt.addBatch(line);
}
stmt.executeBatch();
toSave.clear();
} catch (SQLException e) {
e.printStackTrace();
}

View File

@ -4,16 +4,19 @@ import com.songoda.epicspawners.EpicSpawnersPlugin;
import com.songoda.epicspawners.storage.Storage;
import com.songoda.epicspawners.storage.StorageItem;
import com.songoda.epicspawners.storage.StorageRow;
import com.songoda.epicspawners.utils.Debugger;
import org.apache.commons.io.FileUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class StorageYaml extends Storage {
private static final Map<String, Object> toSave = new HashMap<>();
public StorageYaml(EpicSpawnersPlugin instance) {
super(instance);
}
@ -44,23 +47,50 @@ public class StorageYaml extends Storage {
}
private String convertToInLineList(String path) {
String converted = "";
StringBuilder converted = new StringBuilder();
for (String key : dataFile.getConfig().getConfigurationSection(path).getKeys(false)) {
converted += key + ":" + dataFile.getConfig().getInt(path + "." + key) + ";";
converted.append(key).append(":").append(dataFile.getConfig().getInt(path + "." + key)).append(";");
}
return converted;
return converted.toString();
}
@Override
public void clearFile() {
dataFile.getConfig().set("data", null);
public void prepareSaveItem(String group, StorageItem... items) {
for (StorageItem item : items) {
if (item == null || item.asObject() == null) continue;
toSave.put("data." + group + "." + items[0].asString() + "." + item.getKey(), item.asObject());
}
}
@Override
public void saveItem(String group, StorageItem... items) {
for (int i = 0; i < items.length; i++) {
if (items[i] == null || items[i].asObject() == null) continue;
dataFile.getConfig().set("data." + group + "." + items[0].asString() + "." + items[i].getKey(), items[i].asObject());
public void doSave() {
try {
dataFile.getConfig().set("data", null); // Clear file
File data = new File(instance.getDataFolder() + "/data.yml");
File dataClone = new File(instance.getDataFolder() + "/data-backup-" + System.currentTimeMillis() + ".yml");
try {
FileUtils.copyFile(data, dataClone);
} catch (IOException e) {
Debugger.runReport(e);
}
Deque<File> backups = new ArrayDeque<>();
for (File file : Objects.requireNonNull(instance.getDataFolder().listFiles())) {
if (file.getName().toLowerCase().contains("data-backup")) {
backups.add(file);
}
}
if (backups.size() > 5) {
backups.getFirst().delete();
}
for (Map.Entry<String, Object> entry : toSave.entrySet()) {
dataFile.getConfig().set(entry.getKey(), entry.getValue());
}
toSave.clear();
} catch (NullPointerException e) {
Debugger.runReport(e);
}
}