[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [onionoo/master] Refactor storing histories in UptimeStatus.
commit fecc396fbc7857c7a59d7f6ad944411241b261eb
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Mon Mar 24 08:45:34 2014 +0100
Refactor storing histories in UptimeStatus.
---
.../torproject/onionoo/UptimeDocumentWriter.java | 21 +++----
src/org/torproject/onionoo/UptimeStatus.java | 60 +++++++++++++-------
test/org/torproject/onionoo/UptimeStatusTest.java | 41 +++++++------
.../onionoo/UptimeStatusUpdaterTest.java | 24 ++++----
4 files changed, 83 insertions(+), 63 deletions(-)
diff --git a/src/org/torproject/onionoo/UptimeDocumentWriter.java b/src/org/torproject/onionoo/UptimeDocumentWriter.java
index 6b44b24..60ca311 100644
--- a/src/org/torproject/onionoo/UptimeDocumentWriter.java
+++ b/src/org/torproject/onionoo/UptimeDocumentWriter.java
@@ -44,27 +44,18 @@ public class UptimeDocumentWriter implements FingerprintListener,
}
public void writeDocuments() {
- SortedSet<UptimeHistory>
- knownRelayStatuses = new TreeSet<UptimeHistory>(),
- knownBridgeStatuses = new TreeSet<UptimeHistory>();
UptimeStatus uptimeStatus = this.documentStore.retrieve(
UptimeStatus.class, true);
if (uptimeStatus == null) {
return;
}
- SortedSet<UptimeHistory> knownStatuses = uptimeStatus.getHistory();
- for (UptimeHistory status : knownStatuses) {
- if (status.isRelay()) {
- knownRelayStatuses.add(status);
- } else {
- knownBridgeStatuses.add(status);
- }
- }
for (String fingerprint : this.newRelayFingerprints) {
- this.updateDocument(true, fingerprint, knownRelayStatuses);
+ this.updateDocument(true, fingerprint,
+ uptimeStatus.getRelayHistory());
}
for (String fingerprint : this.newBridgeFingerprints) {
- this.updateDocument(false, fingerprint, knownBridgeStatuses);
+ this.updateDocument(false, fingerprint,
+ uptimeStatus.getBridgeHistory());
}
Logger.printStatusTime("Wrote uptime document files");
}
@@ -76,7 +67,9 @@ public class UptimeDocumentWriter implements FingerprintListener,
UptimeStatus uptimeStatus = this.documentStore.retrieve(
UptimeStatus.class, true, fingerprint);
if (uptimeStatus != null) {
- SortedSet<UptimeHistory> history = uptimeStatus.getHistory();
+ SortedSet<UptimeHistory> history = relay
+ ? uptimeStatus.getRelayHistory()
+ : uptimeStatus.getBridgeHistory();
UptimeDocument uptimeDocument = new UptimeDocument();
uptimeDocument.setDocumentString(this.formatHistoryString(relay,
fingerprint, history, knownStatuses));
diff --git a/src/org/torproject/onionoo/UptimeStatus.java b/src/org/torproject/onionoo/UptimeStatus.java
index 02b83ee..4cf71c6 100644
--- a/src/org/torproject/onionoo/UptimeStatus.java
+++ b/src/org/torproject/onionoo/UptimeStatus.java
@@ -99,12 +99,22 @@ class UptimeStatus extends Document {
private transient boolean isDirty = false;
- private SortedSet<UptimeHistory> history = new TreeSet<UptimeHistory>();
- public void setHistory(SortedSet<UptimeHistory> history) {
- this.history = history;
+ private SortedSet<UptimeHistory> relayHistory =
+ new TreeSet<UptimeHistory>();
+ public void setRelayHistory(SortedSet<UptimeHistory> relayHistory) {
+ this.relayHistory = relayHistory;
}
- public SortedSet<UptimeHistory> getHistory() {
- return this.history;
+ public SortedSet<UptimeHistory> getRelayHistory() {
+ return this.relayHistory;
+ }
+
+ private SortedSet<UptimeHistory> bridgeHistory =
+ new TreeSet<UptimeHistory>();
+ public void setBridgeHistory(SortedSet<UptimeHistory> bridgeHistory) {
+ this.bridgeHistory = bridgeHistory;
+ }
+ public SortedSet<UptimeHistory> getBridgeHistory() {
+ return this.bridgeHistory;
}
public static UptimeStatus loadOrCreate(String fingerprint) {
@@ -126,7 +136,11 @@ class UptimeStatus extends Document {
String line = s.nextLine();
UptimeHistory parsedLine = UptimeHistory.fromString(line);
if (parsedLine != null) {
- this.history.add(parsedLine);
+ if (parsedLine.isRelay()) {
+ this.relayHistory.add(parsedLine);
+ } else {
+ this.bridgeHistory.add(parsedLine);
+ }
} else {
System.err.println("Could not parse uptime history line '"
+ line + "'. Skipping.");
@@ -137,31 +151,34 @@ class UptimeStatus extends Document {
public void addToHistory(boolean relay, SortedSet<Long> newIntervals) {
for (long startMillis : newIntervals) {
+ SortedSet<UptimeHistory> history = relay ? this.relayHistory
+ : this.bridgeHistory;
UptimeHistory interval = new UptimeHistory(relay, startMillis, 1);
- if (!this.history.headSet(interval).isEmpty()) {
- UptimeHistory prev = this.history.headSet(interval).last();
+ if (!history.headSet(interval).isEmpty()) {
+ UptimeHistory prev = history.headSet(interval).last();
if (prev.isRelay() == interval.isRelay() &&
prev.getStartMillis() + DateTimeHelper.ONE_HOUR
* prev.getUptimeHours() > interval.getStartMillis()) {
continue;
}
}
- if (!this.history.tailSet(interval).isEmpty()) {
- UptimeHistory next = this.history.tailSet(interval).first();
+ if (!history.tailSet(interval).isEmpty()) {
+ UptimeHistory next = history.tailSet(interval).first();
if (next.isRelay() == interval.isRelay() &&
next.getStartMillis() < interval.getStartMillis()
+ DateTimeHelper.ONE_HOUR) {
continue;
}
}
- this.history.add(interval);
+ history.add(interval);
this.isDirty = true;
}
}
public void storeIfChanged() {
if (this.isDirty) {
- this.compressHistory();
+ this.compressHistory(this.relayHistory);
+ this.compressHistory(this.bridgeHistory);
if (fingerprint == null) {
ApplicationFactory.getDocumentStore().store(this);
} else {
@@ -172,11 +189,12 @@ class UptimeStatus extends Document {
}
}
- private void compressHistory() {
- SortedSet<UptimeHistory> compressedHistory =
- new TreeSet<UptimeHistory>();
+ private void compressHistory(SortedSet<UptimeHistory> history) {
+ SortedSet<UptimeHistory> uncompressedHistory =
+ new TreeSet<UptimeHistory>(history);
+ history.clear();
UptimeHistory lastInterval = null;
- for (UptimeHistory interval : history) {
+ for (UptimeHistory interval : uncompressedHistory) {
if (lastInterval != null &&
lastInterval.getStartMillis() + DateTimeHelper.ONE_HOUR
* lastInterval.getUptimeHours() == interval.getStartMillis() &&
@@ -184,20 +202,22 @@ class UptimeStatus extends Document {
lastInterval.addUptime(interval);
} else {
if (lastInterval != null) {
- compressedHistory.add(lastInterval);
+ history.add(lastInterval);
}
lastInterval = interval;
}
}
if (lastInterval != null) {
- compressedHistory.add(lastInterval);
+ history.add(lastInterval);
}
- this.history = compressedHistory;
}
public String toDocumentString() {
StringBuilder sb = new StringBuilder();
- for (UptimeHistory interval : this.history) {
+ for (UptimeHistory interval : this.relayHistory) {
+ sb.append(interval.toString() + "\n");
+ }
+ for (UptimeHistory interval : this.bridgeHistory) {
sb.append(interval.toString() + "\n");
}
return sb.toString();
diff --git a/test/org/torproject/onionoo/UptimeStatusTest.java b/test/org/torproject/onionoo/UptimeStatusTest.java
index fab48e6..884ccc5 100644
--- a/test/org/torproject/onionoo/UptimeStatusTest.java
+++ b/test/org/torproject/onionoo/UptimeStatusTest.java
@@ -43,8 +43,9 @@ public class UptimeStatusTest {
new Long[] { DateTimeHelper.parse("2013-12-20 00:00:00") })));
uptimeStatus.storeIfChanged();
assertEquals("History must contain single entry.", 1,
- uptimeStatus.getHistory().size());
- UptimeHistory newUptimeHistory = uptimeStatus.getHistory().first();
+ uptimeStatus.getRelayHistory().size());
+ UptimeHistory newUptimeHistory =
+ uptimeStatus.getRelayHistory().first();
assertEquals("History not for relay.", true,
newUptimeHistory.isRelay());
assertEquals("History start millis not same as provided.",
@@ -66,8 +67,9 @@ public class UptimeStatusTest {
DateTimeHelper.parse("2013-12-20 01:00:00") })));
uptimeStatus.storeIfChanged();
assertEquals("History must contain single entry.", 1,
- uptimeStatus.getHistory().size());
- UptimeHistory newUptimeHistory = uptimeStatus.getHistory().first();
+ uptimeStatus.getRelayHistory().size());
+ UptimeHistory newUptimeHistory =
+ uptimeStatus.getRelayHistory().first();
assertEquals("History not for relay.", true,
newUptimeHistory.isRelay());
assertEquals("History start millis not same as provided.",
@@ -100,11 +102,12 @@ public class UptimeStatusTest {
new Long[] { DateTimeHelper.parse("2013-09-09 02:00:00"),
DateTimeHelper.parse("2013-12-20 00:00:00") })));
assertEquals("Uncompressed history must contain five entries.", 5,
- uptimeStatus.getHistory().size());
+ uptimeStatus.getRelayHistory().size());
uptimeStatus.storeIfChanged();
assertEquals("Compressed history must contain one entry.", 1,
- uptimeStatus.getHistory().size());
- UptimeHistory newUptimeHistory = uptimeStatus.getHistory().first();
+ uptimeStatus.getRelayHistory().size());
+ UptimeHistory newUptimeHistory =
+ uptimeStatus.getRelayHistory().first();
assertEquals("History not for relay.", true,
newUptimeHistory.isRelay());
assertEquals("History start millis not as expected.",
@@ -148,8 +151,9 @@ public class UptimeStatusTest {
DateTimeHelper.parse("2013-07-22 17:00:00")})));
uptimeStatus.storeIfChanged();
assertEquals("Compressed history must still contain three entries.",
- 3, uptimeStatus.getHistory().size());
- UptimeHistory newUptimeHistory = uptimeStatus.getHistory().first();
+ 3, uptimeStatus.getRelayHistory().size());
+ UptimeHistory newUptimeHistory =
+ uptimeStatus.getRelayHistory().first();
assertEquals("History not for relay.", true,
newUptimeHistory.isRelay());
assertEquals("History start millis not as expected.",
@@ -169,8 +173,9 @@ public class UptimeStatusTest {
DateTimeHelper.parse("2013-09-09 02:00:00")})));
uptimeStatus.storeIfChanged();
assertEquals("Compressed history must now contain two entries.",
- 2, uptimeStatus.getHistory().size());
- UptimeHistory newUptimeHistory = uptimeStatus.getHistory().first();
+ 2, uptimeStatus.getRelayHistory().size());
+ UptimeHistory newUptimeHistory =
+ uptimeStatus.getRelayHistory().first();
assertEquals("History not for relay.", true,
newUptimeHistory.isRelay());
assertEquals("History start millis not as expected.",
@@ -202,9 +207,10 @@ public class UptimeStatusTest {
new Long[] { DateTimeHelper.parse("2013-07-22 16:00:00"),
DateTimeHelper.parse("2014-03-21 20:00:00")})));
uptimeStatus.storeIfChanged();
- assertEquals("Compressed history must still contain two entries.",
- 2, uptimeStatus.getHistory().size());
- UptimeHistory newUptimeHistory = uptimeStatus.getHistory().first();
+ assertEquals("Compressed relay history must still contain one entry.",
+ 1, uptimeStatus.getRelayHistory().size());
+ UptimeHistory newUptimeHistory =
+ uptimeStatus.getRelayHistory().first();
assertEquals("History not for relay.", true,
newUptimeHistory.isRelay());
assertEquals("History start millis not as expected.",
@@ -223,9 +229,10 @@ public class UptimeStatusTest {
new Long[] { DateTimeHelper.parse("2013-07-22 16:00:00"),
DateTimeHelper.parse("2014-03-21 20:00:00")})));
uptimeStatus.storeIfChanged();
- assertEquals("Compressed history must still contain two entries.",
- 2, uptimeStatus.getHistory().size());
- UptimeHistory newUptimeHistory = uptimeStatus.getHistory().last();
+ assertEquals("Compressed bridge history must still contain one "
+ + "entry.", 1, uptimeStatus.getBridgeHistory().size());
+ UptimeHistory newUptimeHistory =
+ uptimeStatus.getBridgeHistory().last();
assertEquals("History not for bridge.", false,
newUptimeHistory.isRelay());
assertEquals("History start millis not as expected.",
diff --git a/test/org/torproject/onionoo/UptimeStatusUpdaterTest.java b/test/org/torproject/onionoo/UptimeStatusUpdaterTest.java
index 4ca2245..a34292b 100644
--- a/test/org/torproject/onionoo/UptimeStatusUpdaterTest.java
+++ b/test/org/torproject/onionoo/UptimeStatusUpdaterTest.java
@@ -64,9 +64,9 @@ public class UptimeStatusUpdaterTest {
null }) {
UptimeStatus status = this.documentStore.getDocument(
UptimeStatus.class, fingerprint);
- UptimeHistory history = status.getHistory().first();
+ UptimeHistory history = status.getRelayHistory().first();
assertEquals("History must contain one entry.", 1,
- status.getHistory().size());
+ status.getRelayHistory().size());
assertEquals("History not for relay.", true, history.isRelay());
assertEquals("History start millis not as expected.",
VALID_AFTER_SAMPLE, history.getStartMillis());
@@ -99,9 +99,9 @@ public class UptimeStatusUpdaterTest {
2, this.documentStore.getPerformedStoreOperations());
UptimeStatus status = this.documentStore.getDocument(
UptimeStatus.class, ALL_RELAYS_AND_BRIDGES_FINGERPRINT);
- assertEquals("History must contain two entries.", 2,
- status.getHistory().size());
- UptimeHistory history = status.getHistory().first();
+ assertEquals("Relay history must contain one entry", 1,
+ status.getRelayHistory().size());
+ UptimeHistory history = status.getRelayHistory().first();
assertEquals("History not for relay.", true, history.isRelay());
assertEquals("History start millis not as expected.",
DateTimeHelper.parse("2013-07-22 17:00:00"),
@@ -139,9 +139,9 @@ public class UptimeStatusUpdaterTest {
null }) {
UptimeStatus status = this.documentStore.getDocument(
UptimeStatus.class, fingerprint);
- UptimeHistory history = status.getHistory().first();
- assertEquals("History must contain one entry.", 1,
- status.getHistory().size());
+ UptimeHistory history = status.getBridgeHistory().first();
+ assertEquals("Bridge history must contain one entry.", 1,
+ status.getBridgeHistory().size());
assertEquals("History not for bridge.", false, history.isRelay());
assertEquals("History start millis not as expected.",
DateTimeHelper.parse("2014-03-21 20:00:00"),
@@ -162,10 +162,10 @@ public class UptimeStatusUpdaterTest {
2, this.documentStore.getPerformedStoreOperations());
UptimeStatus status = this.documentStore.getDocument(
UptimeStatus.class, ALL_RELAYS_AND_BRIDGES_FINGERPRINT);
- assertEquals("History must contain two entries.", 2,
- status.getHistory().size());
- UptimeHistory history = status.getHistory().last();
- assertEquals("History not for relay.", false, history.isRelay());
+ assertEquals("Bridge history must contain one entry.", 1,
+ status.getBridgeHistory().size());
+ UptimeHistory history = status.getBridgeHistory().last();
+ assertEquals("History not for bridge.", false, history.isRelay());
assertEquals("History start millis not as expected.",
DateTimeHelper.parse("2013-07-22 17:00:00"),
history.getStartMillis());
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits