Skip to content

Commit

Permalink
save database asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursiveG committed Nov 22, 2016
1 parent b290b79 commit 6fe2b47
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,27 @@ public DatabaseManager(File db_file, File old_db_file) {
}

public void save() {
final Map<UUID, DatabaseRecord> clonedMap = new HashMap<>();
final File clonedFile = new File(dbFile.toURI());
for (Map.Entry<UUID, DatabaseRecord> 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<UUID, DatabaseRecord> 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);
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
3 changes: 2 additions & 1 deletion src/main/java/cat/nyaa/playtimetracker/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static void debug(String msg) {
public void onDisable() {
Bukkit.getScheduler().cancelTasks(this);
updater.updateAllOnlinePlayers();
database.synchronizeSave();
}

@Override
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6fe2b47

Please sign in to comment.