[tor-commits] [metrics-lib/master] Make unrecognized keys of Torperf results available.

karsten at torproject.org karsten at torproject.org
Tue Apr 26 17:52:21 UTC 2016


commit 45ad8a0d2294242ad4e3f98f29f8f17ec2a588cb
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Fri Apr 15 16:58:06 2016 +0200

    Make unrecognized keys of Torperf results available.
---
 CHANGELOG.md                                       |  2 ++
 src/org/torproject/descriptor/TorperfResult.java   |  6 ++++-
 .../descriptor/impl/TorperfResultImpl.java         | 29 ++++++++++++++++------
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 863bffd..d5b9b9a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,8 @@
    - Include the hostname in directory source entries of consensuses
      and votes.
    - Also accept \r\n as newline in Torperf results files.
+   - Make unrecognized keys of Torperf results available together with
+     the corresponding values, rather than just the whole line.
 
  * Minor changes
    - Include a Torperf results line with more than one unrecognized
diff --git a/src/org/torproject/descriptor/TorperfResult.java b/src/org/torproject/descriptor/TorperfResult.java
index 016a043..0c75cb2 100644
--- a/src/org/torproject/descriptor/TorperfResult.java
+++ b/src/org/torproject/descriptor/TorperfResult.java
@@ -1,4 +1,4 @@
-/* Copyright 2012--2015 The Tor Project
+/* Copyright 2012--2016 The Tor Project
  * See LICENSE for licensing information */
 package org.torproject.descriptor;
 
@@ -7,6 +7,10 @@ import java.util.SortedMap;
 
 public interface TorperfResult extends Descriptor {
 
+  /* Return all unrecognized keys together with their values, or null if
+   * all keys were recognized. */
+  public SortedMap<String, String> getUnrecognizedKeys();
+
   /* Return the configured name of the data source. */
   public String getSource();
 
diff --git a/src/org/torproject/descriptor/impl/TorperfResultImpl.java b/src/org/torproject/descriptor/impl/TorperfResultImpl.java
index b162b18..94e756e 100644
--- a/src/org/torproject/descriptor/impl/TorperfResultImpl.java
+++ b/src/org/torproject/descriptor/impl/TorperfResultImpl.java
@@ -153,6 +153,10 @@ public class TorperfResultImpl extends DescriptorImpl
           throw new DescriptorParseException("Unrecognized key '" + key
               + "' in line '" + line + "'.");
         } else {
+          if (this.unrecognizedKeys == null) {
+            this.unrecognizedKeys = new TreeMap<>();
+          }
+          this.unrecognizedKeys.put(key, value);
           if (this.unrecognizedLines == null) {
             this.unrecognizedLines = new ArrayList<>();
           }
@@ -275,16 +279,19 @@ public class TorperfResultImpl extends DescriptorImpl
       Arrays.asList("10,20,30,40,50,60,70,80,90".split(",")));
   private void parseDataPercentile(String value, String keyValue,
       String line) throws DescriptorParseException {
-    String percentileString = keyValue.substring("DATAPERC".length(),
-        keyValue.indexOf("="));
+    String key = keyValue.substring(0, keyValue.indexOf("="));
+    String percentileString = key.substring("DATAPERC".length());
     if (!this.unparsedPercentiles.contains(percentileString)) {
-      throw new DescriptorParseException("Illegal value in '" + keyValue
-          + "' in line '" + line + "'.");
+      if (this.unrecognizedKeys == null) {
+        this.unrecognizedKeys = new TreeMap<>();
+      }
+      this.unrecognizedKeys.put(key, value);
+    } else {
+      this.unparsedPercentiles.remove(percentileString);
+      int decileIndex = (Integer.parseInt(percentileString) / 10) - 1;
+      long timestamp = this.parseTimestamp(value, keyValue, line);
+      this.dataDeciles[decileIndex] = timestamp;
     }
-    this.unparsedPercentiles.remove(percentileString);
-    int decileIndex = (Integer.parseInt(percentileString) / 10) - 1;
-    long timestamp = this.parseTimestamp(value, keyValue, line);
-    this.dataDeciles[decileIndex] = timestamp;
   }
 
   private void parseLaunch(String value, String keyValue, String line)
@@ -383,6 +390,12 @@ public class TorperfResultImpl extends DescriptorImpl
     }
   }
 
+  private SortedMap<String, String> unrecognizedKeys;
+  public SortedMap<String, String> getUnrecognizedKeys() {
+    return this.unrecognizedKeys == null ? null
+        : new TreeMap<>(this.unrecognizedKeys);
+  }
+
   private String source;
   public String getSource() {
     return this.source;





More information about the tor-commits mailing list