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

[tor-commits] [metrics-web/master] Make Database AutoCloseable and use try-with-resource.



commit bad6542772eacfd57b7030df417f845f2aa74583
Author: iwakeh <iwakeh@xxxxxxxxxxxxxx>
Date:   Wed Dec 20 13:06:49 2017 +0000

    Make Database AutoCloseable and use try-with-resource.
---
 .../metrics/stats/ipv6servers/Database.java        |   6 +-
 .../torproject/metrics/stats/ipv6servers/Main.java | 109 ++++++++++-----------
 2 files changed, 54 insertions(+), 61 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/stats/ipv6servers/Database.java b/src/main/java/org/torproject/metrics/stats/ipv6servers/Database.java
index c334263..401bdb7 100644
--- a/src/main/java/org/torproject/metrics/stats/ipv6servers/Database.java
+++ b/src/main/java/org/torproject/metrics/stats/ipv6servers/Database.java
@@ -20,7 +20,7 @@ import java.util.TimeZone;
 
 /** Database wrapper to connect to the database, insert data, run the stored
  * procedure for aggregating data, and query aggregated data as output. */
-class Database {
+class Database implements AutoCloseable {
 
   /** Database connection string. */
   private String jdbcString;
@@ -210,8 +210,8 @@ class Database {
     return statistics;
   }
 
-  /** Disconnect from the database. */
-  void disconnect() throws SQLException {
+  /** Release database connection. */
+  public void close() throws SQLException {
     this.connection.close();
   }
 }
diff --git a/src/main/java/org/torproject/metrics/stats/ipv6servers/Main.java b/src/main/java/org/torproject/metrics/stats/ipv6servers/Main.java
index 81433c0..508d73e 100644
--- a/src/main/java/org/torproject/metrics/stats/ipv6servers/Main.java
+++ b/src/main/java/org/torproject/metrics/stats/ipv6servers/Main.java
@@ -35,72 +35,65 @@ public class Main {
     File historyFile = new File(Configuration.history);
     reader.setHistoryFile(historyFile);
     Parser parser = new Parser();
-    Database database = new Database(Configuration.database);
-    try {
-      for (Descriptor descriptor : reader.readDescriptors(
-          new File(Configuration.descriptors
-              + "recent/relay-descriptors/consensuses"),
-          new File(Configuration.descriptors
-              + "recent/relay-descriptors/server-descriptors"),
-          new File(Configuration.descriptors
-              + "recent/bridge-descriptors/statuses"),
-          new File(Configuration.descriptors
-              + "recent/bridge-descriptors/server-descriptors"),
-          new File(Configuration.descriptors
-              + "archive/relay-descriptors/consensuses"),
-          new File(Configuration.descriptors
-              + "archive/relay-descriptors/server-descriptors"),
-          new File(Configuration.descriptors
-              + "archive/bridge-descriptors/statuses"),
-          new File(Configuration.descriptors
-              + "archive/bridge-descriptors/server-descriptors"))) {
-        if (descriptor instanceof ServerDescriptor) {
-          database.insertServerDescriptor(parser.parseServerDescriptor(
-              (ServerDescriptor) descriptor));
-        } else if (descriptor instanceof RelayNetworkStatusConsensus) {
-          database.insertStatus(parser.parseRelayNetworkStatusConsensus(
-              (RelayNetworkStatusConsensus) descriptor));
-        } else if (descriptor instanceof BridgeNetworkStatus) {
-          database.insertStatus(parser.parseBridgeNetworkStatus(
-              (BridgeNetworkStatus) descriptor));
-        } else {
-          log.debug("Skipping unknown descriptor of type {}.",
-              descriptor.getClass());
+    try (Database database = new Database(Configuration.database)) {
+      try {
+        for (Descriptor descriptor : reader.readDescriptors(
+            new File(Configuration.descriptors
+                + "recent/relay-descriptors/consensuses"),
+            new File(Configuration.descriptors
+                + "recent/relay-descriptors/server-descriptors"),
+            new File(Configuration.descriptors
+                + "recent/bridge-descriptors/statuses"),
+            new File(Configuration.descriptors
+                + "recent/bridge-descriptors/server-descriptors"),
+            new File(Configuration.descriptors
+                + "archive/relay-descriptors/consensuses"),
+            new File(Configuration.descriptors
+                + "archive/relay-descriptors/server-descriptors"),
+            new File(Configuration.descriptors
+                + "archive/bridge-descriptors/statuses"),
+            new File(Configuration.descriptors
+                + "archive/bridge-descriptors/server-descriptors"))) {
+          if (descriptor instanceof ServerDescriptor) {
+            database.insertServerDescriptor(parser.parseServerDescriptor(
+                (ServerDescriptor) descriptor));
+          } else if (descriptor instanceof RelayNetworkStatusConsensus) {
+            database.insertStatus(parser.parseRelayNetworkStatusConsensus(
+                (RelayNetworkStatusConsensus) descriptor));
+          } else if (descriptor instanceof BridgeNetworkStatus) {
+            database.insertStatus(parser.parseBridgeNetworkStatus(
+                (BridgeNetworkStatus) descriptor));
+          } else {
+            log.debug("Skipping unknown descriptor of type {}.",
+                descriptor.getClass());
+          }
         }
-      }
 
-      log.info("Aggregating database entries.");
-      database.aggregate();
+        log.info("Aggregating database entries.");
+        database.aggregate();
 
-      log.info("Committing all updated parts in the database.");
-      database.commit();
-    } catch (SQLException sqle) {
-      log.error("Cannot recover from SQL exception while inserting or "
-          + "aggregating data. Rolling back and exiting.", sqle);
-      database.rollback();
-      database.disconnect();
-      return;
-    }
-    reader.saveHistoryFile(historyFile);
+        log.info("Committing all updated parts in the database.");
+        database.commit();
+      } catch (SQLException sqle) {
+        log.error("Cannot recover from SQL exception while inserting or "
+            + "aggregating data. Rolling back and exiting.", sqle);
+        database.rollback();
+        return;
+      }
+      reader.saveHistoryFile(historyFile);
 
-    log.info("Querying aggregated statistics from the database.");
-    Iterable<OutputLine> output;
-    try {
-      output = database.queryServersIpv6();
+      log.info("Querying aggregated statistics from the database.");
+      Iterable<OutputLine> output = database.queryServersIpv6();
+      log.info("Writing aggregated statistics to {}.", Configuration.output);
+      if (null != output) {
+        new Writer().write(Paths.get(Configuration.output), output);
+      }
+
+      log.info("Terminating ipv6servers module.");
     } catch (SQLException sqle) {
       log.error("Cannot recover from SQL exception while querying. Not writing "
           + "output file.", sqle);
-      return;
-    } finally {
-      database.disconnect();
-    }
-
-    log.info("Writing aggregated statistics to {}.", Configuration.output);
-    if (null != output) {
-      new Writer().write(Paths.get(Configuration.output), output);
     }
-
-    log.info("Terminating ipv6servers module.");
   }
 }
 



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