[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [onionoo/release] Return results in deterministic order.



commit 8dfabbe36d5f90e2df14b504461298b1585fd5ae
Author: iwakeh <iwakeh@xxxxxxxxxxxxxx>
Date:   Wed Dec 20 16:16:03 2017 +0000

    Return results in deterministic order.
    
    Part of task-25002.
---
 CHANGELOG.md                                                   |  5 +++++
 .../java/org/torproject/onionoo/server/RequestHandler.java     | 10 ++++------
 .../torproject/onionoo/server/SummaryDocumentComparator.java   |  6 +++++-
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d0fed40..fa33c82 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,11 @@
  * Medium changes
    - Stop omitting "n" in summary docs for "Unnamed" relays/bridges.
 
+ * Minor changes
+   - Make responses deterministic by always sorting results by
+     fingerprint, either if no specific order was requested or to
+     break ties after ordering results as requested.
+
 
 # Changes in version 5.0-1.10.1 - 2018-02-07
 
diff --git a/src/main/java/org/torproject/onionoo/server/RequestHandler.java b/src/main/java/org/torproject/onionoo/server/RequestHandler.java
index 55938c5..63b8fe9 100644
--- a/src/main/java/org/torproject/onionoo/server/RequestHandler.java
+++ b/src/main/java/org/torproject/onionoo/server/RequestHandler.java
@@ -603,12 +603,10 @@ public class RequestHandler {
         uniqueBridges.add(bridge);
       }
     }
-    if (this.order != null) {
-      Comparator<SummaryDocument> comparator
-          = new SummaryDocumentComparator(this.order);
-      Collections.sort(uniqueRelays, comparator);
-      Collections.sort(uniqueBridges, comparator);
-    }
+    Comparator<SummaryDocument> comparator
+        = new SummaryDocumentComparator(this.order);
+    Collections.sort(uniqueRelays, comparator);
+    Collections.sort(uniqueBridges, comparator);
     this.orderedRelays.addAll(uniqueRelays);
     this.orderedBridges.addAll(uniqueBridges);
   }
diff --git a/src/main/java/org/torproject/onionoo/server/SummaryDocumentComparator.java b/src/main/java/org/torproject/onionoo/server/SummaryDocumentComparator.java
index 0e6729b..86d4d32 100644
--- a/src/main/java/org/torproject/onionoo/server/SummaryDocumentComparator.java
+++ b/src/main/java/org/torproject/onionoo/server/SummaryDocumentComparator.java
@@ -13,7 +13,8 @@ public class SummaryDocumentComparator implements Comparator<SummaryDocument> {
 
   /** Comparator is initialized with the order parameters. */
   public SummaryDocumentComparator(String ... orderParameters) {
-    this.orderParameters = orderParameters;
+    this.orderParameters
+        = null == orderParameters ? new String[]{} : orderParameters;
   }
 
   @Override
@@ -45,6 +46,9 @@ public class SummaryDocumentComparator implements Comparator<SummaryDocument> {
         break;
       }
     }
+    if (0 == result) {
+      result = o1.getFingerprint().compareTo(o2.getFingerprint());
+    }
     return result;
   }
 }



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits