From 6fe2b4781dc131ef5ff8f37a59fda1cc61128718 Mon Sep 17 00:00:00 2001 From: Recursive G Date: Mon, 21 Nov 2016 23:39:46 -0600 Subject: [PATCH] save database asynchronously --- .../nyaa/playtimetracker/DatabaseManager.java | 22 +++++++++++++++++-- .../nyaa/playtimetracker/DatabaseRecord.java | 19 +++++++++++++++- .../java/cat/nyaa/playtimetracker/Main.java | 3 ++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java b/src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java index b2f4172..e035e6a 100644 --- a/src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java +++ b/src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java @@ -66,9 +66,27 @@ public DatabaseManager(File db_file, File old_db_file) { } public void save() { + final Map clonedMap = new HashMap<>(); + final File clonedFile = new File(dbFile.toURI()); + for (Map.Entry entry : recordMap.entrySet()) { + clonedMap.put(entry.getKey(), entry.getValue().clone()); + } + new Thread(new Runnable() { + @Override + public void run() { + synchronizeSave(clonedFile, clonedMap); + } + }).start(); + } + + public void synchronizeSave() { + synchronizeSave(dbFile, recordMap); + } + + private static synchronized void synchronizeSave(final File dbFile, final Map records) { YamlConfiguration cfg = new YamlConfiguration(); - for (UUID id : recordMap.keySet()) { - recordMap.get(id).serialize(cfg.createSection(id.toString())); + for (UUID id : records.keySet()) { + records.get(id).serialize(cfg.createSection(id.toString())); } try { cfg.save(dbFile); diff --git a/src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java b/src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java index a030276..a731325 100644 --- a/src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java +++ b/src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java @@ -10,7 +10,7 @@ import java.util.Set; import java.util.UUID; -public class DatabaseRecord { +public class DatabaseRecord implements Cloneable { public UUID uuid; public ZonedDateTime lastSeen; // timestamp millisecond public long dailyTime; // millisecond @@ -103,4 +103,21 @@ public String toString() { lastSeen.toString(), dailyTime, weeklyTime, monthlyTime, totalTime, completedDailyMissions, completedWeeklyMissions, completedMonthlyMissions, completedLifetimeMissions); } + + @Override + public DatabaseRecord clone() { + try { + DatabaseRecord r = (DatabaseRecord) super.clone(); + r.completedLifetimeMissions = new HashSet<>(completedLifetimeMissions); + r.completedDailyMissions = new HashSet<>(completedDailyMissions); + r.completedMonthlyMissions = new HashSet<>(completedMonthlyMissions); + r.completedWeeklyMissions = new HashSet<>(completedWeeklyMissions); + r.lastSeen = ZonedDateTime.from(lastSeen); + return r; + } catch (CloneNotSupportedException ex) { + ex.printStackTrace(); + Main.log("Failed to clone: " + toString()); + } + return new DatabaseRecord(); + } } diff --git a/src/main/java/cat/nyaa/playtimetracker/Main.java b/src/main/java/cat/nyaa/playtimetracker/Main.java index 03f7d1d..ca594da 100644 --- a/src/main/java/cat/nyaa/playtimetracker/Main.java +++ b/src/main/java/cat/nyaa/playtimetracker/Main.java @@ -53,6 +53,7 @@ public static void debug(String msg) { public void onDisable() { Bukkit.getScheduler().cancelTasks(this); updater.updateAllOnlinePlayers(); + database.synchronizeSave(); } @Override @@ -166,7 +167,7 @@ private void notifyAcquire(Player p) { @Override public void run() { // Auto-save timer - log("Auto-save timer executing..."); + debug("Auto-save timer executing..."); updater.updateAllOnlinePlayers(); for (Player p : Bukkit.getOnlinePlayers()) { notifyAcquire(p);