Initial async saving on auto save and force save.

This commit is contained in:
drtshock 2015-08-06 17:41:37 -05:00
parent f76fc190fd
commit 1a94e89558
9 changed files with 68 additions and 18 deletions

View File

@ -80,5 +80,7 @@ public abstract class Board {
public abstract boolean forceSave(); public abstract boolean forceSave();
public abstract boolean forceSave(boolean sync);
public abstract boolean load(); public abstract boolean load();
} }

View File

@ -31,6 +31,8 @@ public abstract class FPlayers {
public abstract void forceSave(); public abstract void forceSave();
public abstract void forceSave(boolean sync);
public abstract FPlayer getByOfflinePlayer(OfflinePlayer player); public abstract FPlayer getByOfflinePlayer(OfflinePlayer player);
public abstract FPlayer getById(String string); public abstract FPlayer getById(String string);

View File

@ -37,6 +37,8 @@ public abstract class Factions {
public abstract void forceSave(); public abstract void forceSave();
public abstract void forceSave(boolean sync);
public static Factions getInstance() { public static Factions getInstance() {
return instance; return instance;
} }

View File

@ -166,7 +166,7 @@ public class P extends MPlugin {
@Override @Override
public void postAutoSave() { public void postAutoSave() {
Board.getInstance().forceSave(); //Board.getInstance().forceSave(); Not sure why this was there as it's called after the board is already saved.
Conf.save(); Conf.save();
} }

View File

@ -28,9 +28,9 @@ public class CmdSaveAll extends FCommand {
@Override @Override
public void perform() { public void perform() {
FPlayers.getInstance().forceSave(); FPlayers.getInstance().forceSave(false);
Factions.getInstance().forceSave(); Factions.getInstance().forceSave(false);
Board.getInstance().forceSave(); Board.getInstance().forceSave(false);
Conf.save(); Conf.save();
msg(TL.COMMAND_SAVEALL_SUCCESS); msg(TL.COMMAND_SAVEALL_SUCCESS);
} }

View File

@ -21,9 +21,9 @@ public class SaveTask implements Runnable {
} }
running = true; running = true;
p.preAutoSave(); p.preAutoSave();
Factions.getInstance().forceSave(); Factions.getInstance().forceSave(false);
FPlayers.getInstance().forceSave(); FPlayers.getInstance().forceSave(false);
Board.getInstance().forceSave(); Board.getInstance().forceSave(false);
p.postAutoSave(); p.postAutoSave();
running = false; running = false;
} }

View File

@ -6,6 +6,7 @@ import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.P; import com.massivecraft.factions.P;
import com.massivecraft.factions.zcore.persist.MemoryBoard; import com.massivecraft.factions.zcore.persist.MemoryBoard;
import com.massivecraft.factions.zcore.util.DiscUtil; import com.massivecraft.factions.zcore.util.DiscUtil;
import org.bukkit.Bukkit;
import java.io.File; import java.io.File;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -63,8 +64,11 @@ public class JSONBoard extends MemoryBoard {
} }
public boolean forceSave() { public boolean forceSave() {
//Factions.log("Saving board to disk"); return forceSave(true);
}
public boolean forceSave(boolean sync) {
if (sync) {
try { try {
DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat())); DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat()));
} catch (Exception e) { } catch (Exception e) {
@ -72,6 +76,19 @@ public class JSONBoard extends MemoryBoard {
P.p.log("Failed to save the board to disk."); P.p.log("Failed to save the board to disk.");
return false; return false;
} }
} else {
Bukkit.getScheduler().runTaskAsynchronously(P.p, new Runnable() {
@Override
public void run() {
try {
DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat()));
} catch (Exception e) {
e.printStackTrace();
P.p.log("Failed to save the board to disk.");
}
}
});
}
return true; return true;
} }

View File

@ -51,13 +51,27 @@ public class JSONFPlayers extends MemoryFPlayers {
} }
public void forceSave() { public void forceSave() {
Map<String, JSONFPlayer> entitiesThatShouldBeSaved = new HashMap<String, JSONFPlayer>(); forceSave(true);
}
public void forceSave(boolean sync) {
final Map<String, JSONFPlayer> entitiesThatShouldBeSaved = new HashMap<String, JSONFPlayer>();
for (FPlayer entity : this.fPlayers.values()) { for (FPlayer entity : this.fPlayers.values()) {
if (((MemoryFPlayer) entity).shouldBeSaved()) { if (((MemoryFPlayer) entity).shouldBeSaved()) {
entitiesThatShouldBeSaved.put(entity.getId(), (JSONFPlayer) entity); entitiesThatShouldBeSaved.put(entity.getId(), (JSONFPlayer) entity);
} }
} }
this.saveCore(this.file, entitiesThatShouldBeSaved);
if (sync) {
saveCore(file, entitiesThatShouldBeSaved);
} else {
Bukkit.getScheduler().runTaskAsynchronously(P.p, new Runnable() {
@Override
public void run() {
saveCore(file, entitiesThatShouldBeSaved);
}
});
}
} }
private boolean saveCore(File target, Map<String, JSONFPlayer> data) { private boolean saveCore(File target, Map<String, JSONFPlayer> data) {

View File

@ -53,12 +53,25 @@ public class JSONFactions extends MemoryFactions {
} }
public void forceSave() { public void forceSave() {
Map<String, JSONFaction> entitiesThatShouldBeSaved = new HashMap<String, JSONFaction>(); forceSave(true);
}
public void forceSave(boolean sync) {
final Map<String, JSONFaction> entitiesThatShouldBeSaved = new HashMap<String, JSONFaction>();
for (Faction entity : this.factions.values()) { for (Faction entity : this.factions.values()) {
entitiesThatShouldBeSaved.put(entity.getId(), (JSONFaction) entity); entitiesThatShouldBeSaved.put(entity.getId(), (JSONFaction) entity);
} }
this.saveCore(this.file, entitiesThatShouldBeSaved); if (sync) {
saveCore(file, entitiesThatShouldBeSaved);
} else {
Bukkit.getScheduler().runTaskAsynchronously(P.p, new Runnable() {
@Override
public void run() {
saveCore(file, entitiesThatShouldBeSaved);
}
});
}
} }
private boolean saveCore(File target, Map<String, JSONFaction> entities) { private boolean saveCore(File target, Map<String, JSONFaction> entities) {