commit 1fe217042241376fee66936e0b4140baaf0e513d Author: Karsten Loesing karsten.loesing@gmx.net Date: Fri Mar 21 20:52:04 2014 +0100
Teach UptimeStatus how to load/save itself. --- src/org/torproject/onionoo/UptimeStatus.java | 33 +++++++++++++++++++- .../torproject/onionoo/UptimeStatusUpdater.java | 17 ++-------- 2 files changed, 34 insertions(+), 16 deletions(-)
diff --git a/src/org/torproject/onionoo/UptimeStatus.java b/src/org/torproject/onionoo/UptimeStatus.java index 61b4fe0..1a222b0 100644 --- a/src/org/torproject/onionoo/UptimeStatus.java +++ b/src/org/torproject/onionoo/UptimeStatus.java @@ -95,6 +95,10 @@ class UptimeHistory
class UptimeStatus extends Document {
+ private transient String fingerprint; + + private transient boolean isDirty = false; + private SortedSet<UptimeHistory> history = new TreeSet<UptimeHistory>(); public void setHistory(SortedSet<UptimeHistory> history) { this.history = history; @@ -103,6 +107,19 @@ class UptimeStatus extends Document { return this.history; }
+ public static UptimeStatus loadOrCreate(String fingerprint) { + UptimeStatus uptimeStatus = (fingerprint == null) ? + ApplicationFactory.getDocumentStore().retrieve( + UptimeStatus.class, true) : + ApplicationFactory.getDocumentStore().retrieve( + UptimeStatus.class, true, fingerprint); + if (uptimeStatus == null) { + uptimeStatus = new UptimeStatus(); + } + uptimeStatus.fingerprint = fingerprint; + return uptimeStatus; + } + public void fromDocumentString(String documentString) { Scanner s = new Scanner(documentString); while (s.hasNextLine()) { @@ -138,10 +155,24 @@ class UptimeStatus extends Document { } } this.history.add(interval); + this.isDirty = true; + } + } + + public void storeIfChanged() { + if (this.isDirty) { + this.compressHistory(); + if (fingerprint == null) { + ApplicationFactory.getDocumentStore().store(this); + } else { + ApplicationFactory.getDocumentStore().store(this, + this.fingerprint); + } + this.isDirty = false; } }
- public void compressHistory() { + private void compressHistory() { SortedSet<UptimeHistory> compressedHistory = new TreeSet<UptimeHistory>(); UptimeHistory lastInterval = null; diff --git a/src/org/torproject/onionoo/UptimeStatusUpdater.java b/src/org/torproject/onionoo/UptimeStatusUpdater.java index 4430387..6dc0b94 100644 --- a/src/org/torproject/onionoo/UptimeStatusUpdater.java +++ b/src/org/torproject/onionoo/UptimeStatusUpdater.java @@ -18,11 +18,8 @@ public class UptimeStatusUpdater implements DescriptorListener,
private DescriptorSource descriptorSource;
- private DocumentStore documentStore; - public UptimeStatusUpdater() { this.descriptorSource = ApplicationFactory.getDescriptorSource(); - this.documentStore = ApplicationFactory.getDocumentStore(); this.registerDescriptorListeners(); }
@@ -108,19 +105,9 @@ public class UptimeStatusUpdater implements DescriptorListener,
private void updateStatus(boolean relay, String fingerprint, SortedSet<Long> newUptimeHours) { - UptimeStatus uptimeStatus = fingerprint == null ? - documentStore.retrieve(UptimeStatus.class, true) : - documentStore.retrieve(UptimeStatus.class, true, fingerprint); - if (uptimeStatus == null) { - uptimeStatus = new UptimeStatus(); - } + UptimeStatus uptimeStatus = UptimeStatus.loadOrCreate(fingerprint); uptimeStatus.addToHistory(relay, newUptimeHours); - uptimeStatus.compressHistory(); - if (fingerprint == null) { - this.documentStore.store(uptimeStatus); - } else { - this.documentStore.store(uptimeStatus, fingerprint); - } + uptimeStatus.storeIfChanged(); }
public String getStatsString() {
tor-commits@lists.torproject.org