Introduce base command class

Introduce a base class for all commands dealing with H2 caches. This is
useful to better structure the existing AnalyzeH2Caches command and to
set the ground for more commands, so that they can leverage common
functionalities by extending the same base class.

Feature: Issue 13989
Change-Id: I8254fda4bd85f50bb765c56bfd42ef17d29dba94
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/AnalyzeH2Caches.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/AnalyzeH2Caches.java
index 89dfeef..474ec46 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/AnalyzeH2Caches.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/AnalyzeH2Caches.java
@@ -13,35 +13,22 @@
 // limitations under the License.
 package com.googlesource.gerrit.modules.cache.chroniclemap.command;
 
-import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.config.SitePaths;
-import com.google.gerrit.sshd.SshCommand;
 import com.google.inject.Inject;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
 import java.util.Collections;
-import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
-import org.apache.commons.io.FilenameUtils;
 import org.eclipse.jgit.lib.Config;
-import org.h2.Driver;
 
-public class AnalyzeH2Caches extends SshCommand {
-  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
-
-  private String cacheDirectory;
-  private SitePaths site;
+public class AnalyzeH2Caches extends H2CacheSshCommand {
 
   @Inject
   AnalyzeH2Caches(@GerritServerConfig Config cfg, SitePaths site) {
-    this.cacheDirectory = cfg.getString("cache", null, "directory");
+    this.gerritConfig = cfg;
     this.site = site;
   }
 
@@ -52,39 +39,14 @@
 
     Config config = new Config();
     for (Path h2 : h2Files) {
-      final String url = jdbcUrl(h2);
-      final String baseName =
-          FilenameUtils.removeExtension(FilenameUtils.getBaseName(h2.toString()));
-      try {
+      H2AggregateData stats = getStats(h2);
+      String baseName = baseName(h2);
 
-        try (Connection conn = Driver.load().connect(url, null);
-            Statement s = conn.createStatement();
-            ResultSet r =
-                s.executeQuery(
-                    "SELECT COUNT(*), AVG(OCTET_LENGTH(k)), AVG(OCTET_LENGTH(v)) FROM data")) {
-          if (r.next()) {
-            long size = r.getLong(1);
-            long avgKeySize = r.getLong(2);
-            long avgValueSize = r.getLong(3);
-
-            if (size == 0) {
-              stdout.println(String.format("WARN: Cache %s is empty, skipping.", baseName));
-              continue;
-            }
-
-            config.setLong("cache", baseName, "maxEntries", size);
-            config.setLong("cache", baseName, "avgKeySize", avgKeySize);
-
-            // Account for extra serialization bytes of TimedValue entries.
-            short TIMED_VALUE_WRAPPER_OVERHEAD = Long.BYTES + Integer.BYTES;
-            config.setLong(
-                "cache", baseName, "avgValueSize", avgValueSize + TIMED_VALUE_WRAPPER_OVERHEAD);
-          }
-        }
-      } catch (SQLException e) {
-        stderr.println(String.format("Could not get information from %s", baseName));
-        throw die(e);
+      if (stats.isEmpty()) {
+        stdout.println(String.format("WARN: Cache %s is empty, skipping.", baseName));
+        continue;
       }
+      appendToConfig(config, stats);
     }
     stdout.println();
     stdout.println("****************************");
@@ -97,14 +59,12 @@
   private Set<Path> getH2CacheFiles() throws UnloggedFailure {
 
     try {
-      final Optional<Path> maybeCacheDir = getCacheDir(site, cacheDirectory);
-
-      return maybeCacheDir
+      return getCacheDir()
           .map(
               cacheDir -> {
                 try {
                   return Files.walk(cacheDir)
-                      .filter(path -> path.toString().endsWith("h2.db"))
+                      .filter(path -> path.toString().endsWith(H2_SUFFIX))
                       .collect(Collectors.toSet());
                 } catch (IOException e) {
                   logger.atSevere().withCause(e).log("Could not read H2 files");
@@ -116,26 +76,4 @@
       throw die(e);
     }
   }
-
-  private String jdbcUrl(Path h2FilePath) {
-    final String normalized =
-        FilenameUtils.removeExtension(FilenameUtils.removeExtension(h2FilePath.toString()));
-    return "jdbc:h2:" + normalized + ";AUTO_SERVER=TRUE";
-  }
-
-  private static Optional<Path> getCacheDir(SitePaths site, String name) throws IOException {
-    if (name == null) {
-      return Optional.empty();
-    }
-    Path loc = site.resolve(name);
-    if (!Files.exists(loc)) {
-      throw new IOException(
-          String.format("disk cache is configured but doesn't exist: %s", loc.toAbsolutePath()));
-    }
-    if (!Files.isReadable(loc)) {
-      throw new IOException(String.format("Can't read from disk cache: %s", loc.toAbsolutePath()));
-    }
-    logger.atFine().log("Enabling disk cache %s", loc.toAbsolutePath());
-    return Optional.of(loc);
-  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/H2AggregateData.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/H2AggregateData.java
new file mode 100644
index 0000000..8f22f86
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/H2AggregateData.java
@@ -0,0 +1,40 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.googlesource.gerrit.modules.cache.chroniclemap.command;
+
+import com.google.auto.value.AutoValue;
+
+@AutoValue
+public abstract class H2AggregateData {
+  public abstract String cacheName();
+
+  public abstract long size();
+
+  public abstract long avgKeySize();
+
+  public abstract long avgValueSize();
+
+  public static H2AggregateData create(
+      String cacheName, long size, long avgKeySize, long avgValueSize) {
+    return new AutoValue_H2AggregateData(cacheName, size, avgKeySize, avgValueSize);
+  }
+
+  public static H2AggregateData empty(String cacheName) {
+    return new AutoValue_H2AggregateData(cacheName, 0L, 0L, 0L);
+  }
+
+  public boolean isEmpty() {
+    return size() == 0L;
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/H2CacheSshCommand.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/H2CacheSshCommand.java
new file mode 100644
index 0000000..e971ff1
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/command/H2CacheSshCommand.java
@@ -0,0 +1,97 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.googlesource.gerrit.modules.cache.chroniclemap.command;
+
+import com.google.common.flogger.FluentLogger;
+import com.google.gerrit.server.config.SitePaths;
+import com.google.gerrit.sshd.SshCommand;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Optional;
+import org.apache.commons.io.FilenameUtils;
+import org.eclipse.jgit.lib.Config;
+import org.h2.Driver;
+
+public abstract class H2CacheSshCommand extends SshCommand {
+  protected static final FluentLogger logger = FluentLogger.forEnclosingClass();
+  public static final String H2_SUFFIX = "h2.db";
+
+  protected Config gerritConfig;
+  protected SitePaths site;
+
+  protected static String baseName(Path h2File) {
+    return FilenameUtils.removeExtension(FilenameUtils.getBaseName(h2File.toString()));
+  }
+
+  public static H2AggregateData getStats(Path h2File) throws UnloggedFailure {
+    String url = jdbcUrl(h2File);
+    String baseName = baseName(h2File);
+    try {
+
+      try (Connection conn = Driver.load().connect(url, null);
+          Statement s = conn.createStatement();
+          ResultSet r =
+              s.executeQuery(
+                  "SELECT COUNT(*), AVG(OCTET_LENGTH(k)), AVG(OCTET_LENGTH(v)) FROM data")) {
+        if (r.next()) {
+          long size = r.getLong(1);
+          long avgKeySize = r.getLong(2);
+          long avgValueSize = r.getLong(3);
+
+          // Account for extra serialization bytes of TimedValue entries.
+          short TIMED_VALUE_WRAPPER_OVERHEAD = Long.BYTES + Integer.BYTES;
+          return H2AggregateData.create(
+              baseName, size, avgKeySize, avgValueSize + TIMED_VALUE_WRAPPER_OVERHEAD);
+        }
+        return H2AggregateData.empty(baseName);
+      }
+    } catch (SQLException e) {
+      throw new UnloggedFailure(1, "fatal: " + e.getMessage(), e);
+    }
+  }
+
+  protected static String jdbcUrl(Path h2FilePath) {
+    final String normalized =
+        FilenameUtils.removeExtension(FilenameUtils.removeExtension(h2FilePath.toString()));
+    return "jdbc:h2:" + normalized + ";AUTO_SERVER=TRUE";
+  }
+
+  protected Optional<Path> getCacheDir() throws IOException {
+    String name = gerritConfig.getString("cache", null, "directory");
+    if (name == null) {
+      return Optional.empty();
+    }
+    Path loc = site.resolve(name);
+    if (!Files.exists(loc)) {
+      throw new IOException(
+          String.format("disk cache is configured but doesn't exist: %s", loc.toAbsolutePath()));
+    }
+    if (!Files.isReadable(loc)) {
+      throw new IOException(String.format("Can't read from disk cache: %s", loc.toAbsolutePath()));
+    }
+    logger.atFine().log("Enabling disk cache %s", loc.toAbsolutePath());
+    return Optional.of(loc);
+  }
+
+  protected void appendToConfig(Config config, H2AggregateData stats) {
+    config.setLong("cache", stats.cacheName(), "maxEntries", stats.size());
+    config.setLong("cache", stats.cacheName(), "avgKeySize", stats.avgKeySize());
+    config.setLong("cache", stats.cacheName(), "avgValueSize", stats.avgValueSize());
+  }
+}