Implement accounts* warm-cache command

Scan all accounts from ReviewDb and load them by all criteria
to populate the in-memory cache.

Change-Id: I37d6fddf4ef55031f5232d760bad60b635c1da4b
diff --git a/admin/warm-cache-1.0.groovy b/admin/warm-cache-1.0.groovy
index 26b480c..5b1d75b 100644
--- a/admin/warm-cache-1.0.groovy
+++ b/admin/warm-cache-1.0.groovy
@@ -15,6 +15,9 @@
 import com.google.gerrit.sshd.*
 import com.google.gerrit.extensions.annotations.*
 import com.google.gerrit.server.project.*
+import com.google.gerrit.server.account.*
+import com.google.gerrit.reviewdb.client.AccountGroup
+import com.google.gerrit.reviewdb.server.ReviewDb
 import com.google.inject.*
 import org.kohsuke.args4j.*
 
@@ -98,5 +101,39 @@
   }
 }
 
-commands = [ WarmProjectsCache, WarmGroupsCache ]
+@Export("accounts")
+class WarmAccountsCache extends BaseSshCommand {
+
+  @Inject
+  AccountCache cache
+
+  @Inject
+  AccountByEmailCache cacheByEmail
+
+  @Inject
+  Provider<ReviewDb> db
+
+  public void run() {
+    println "Loading accounts ..."
+    def start = System.currentTimeMillis()
+    def allAccounts = db.get().accounts().all()
+    def loaded = 0
+
+    for (account in allAccounts) {
+      cache.get(account.accountId)
+      if (account.preferredEmail != null) {
+        cacheByEmail.get(account.preferredEmail)
+      }
+      loaded++
+      if (loaded%1000==0) {
+        println "$loaded accounts"
+      }
+    }
+
+    def elapsed = (System.currentTimeMillis()-start)/1000
+    println "$loaded accounts loaded in $elapsed secs"
+  }
+}
+
+commands = [ WarmProjectsCache, WarmGroupsCache, WarmAccountsCache ]