From 672bbb1c67236fc3cc94b74e683a92431e81c1af Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <eelco.dolstra@logicblox.com>
Date: Thu, 11 Jun 2015 14:09:50 +0200
Subject: [PATCH] hydra-update-gc-roots: Get Builds and BuildOutputs in the
 same query

This greatly reduces the number of roundtrips to the database.
---
 src/script/hydra-update-gc-roots | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/script/hydra-update-gc-roots b/src/script/hydra-update-gc-roots
index f1bc3f51..7b6fb806 100755
--- a/src/script/hydra-update-gc-roots
+++ b/src/script/hydra-update-gc-roots
@@ -22,7 +22,10 @@ sub addRoot {
 }
 
 
-my @columns = ( "id", "project", "jobset", "job", "system", "finished", "drvpath", "timestamp", "buildstatus" );
+my @columns =
+    ( "id", "project", "jobset", "job", "system", "finished", "drvpath", "timestamp", "buildstatus"
+    , { "outpaths" => \ "(select string_agg(path, ' ') from BuildOutputs where build = me.id)" }
+    );
 
 my %seenBuilds;
 
@@ -35,11 +38,11 @@ sub keepBuild {
         $build->system, "; ",
         strftime("%Y-%m-%d %H:%M:%S", localtime($build->timestamp)), ")\n";
     if ($build->finished && ($build->buildstatus == 0 || $build->buildstatus == 6)) {
-        foreach my $out ($build->buildoutputs->all) {
-            if (isValidPath($out->path)) {
-                addRoot $out->path;
+        foreach my $path (split / /, $build->get_column('outpaths')) {
+            if (isValidPath($path)) {
+                addRoot $path;
             } else {
-                print STDERR "    warning: output ", $out->path, " has disappeared\n" if $build->finished;
+                print STDERR "    warning: output ", $path, " has disappeared\n" if $build->finished;
             }
         }
     }