Avoid loading all SSH keys when adding a new one

Load only the last one (with the highest seq number) as this is the only
thing we need to determine the next seq number.

Change-Id: Ia4e8a03d489984b8e5c1b5e887a863a0b7d4a9a9
This commit is contained in:
Saša Živkov
2014-05-02 13:07:14 +02:00
parent 703e2a69c6
commit c7b339cf88
2 changed files with 9 additions and 5 deletions

View File

@@ -29,4 +29,7 @@ public interface AccountSshKeyAccess extends
@Query("WHERE id.accountId = ?")
ResultSet<AccountSshKey> byAccount(Account.Id id) throws OrmException;
@Query("WHERE id.accountId = ? ORDER BY id.seq DESC LIMIT 1")
ResultSet<AccountSshKey> byAccountLast(Account.Id id) throws OrmException;
}

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.account;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
import com.google.gerrit.common.errors.InvalidSshKeyException;
import com.google.gerrit.extensions.restapi.AuthException;
@@ -31,6 +32,7 @@ import com.google.gerrit.server.account.AddSshKey.Input;
import com.google.gerrit.server.account.GetSshKeys.SshKeyInfo;
import com.google.gerrit.server.ssh.SshKeyCache;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -74,11 +76,10 @@ public class AddSshKey implements RestModifyView<AccountResource, Input> {
throw new BadRequestException("SSH public key missing");
}
int max = 0;
for (AccountSshKey k : dbProvider.get().accountSshKeys()
.byAccount(user.getAccountId())) {
max = Math.max(max, k.getKey().get());
}
ResultSet<AccountSshKey> byAccountLast =
dbProvider.get().accountSshKeys().byAccountLast(user.getAccountId());
AccountSshKey last = Iterables.getOnlyElement(byAccountLast, null);
int max = last == null ? 0 : last.getKey().get();
final RawInput rawKey = input.raw;
String sshPublicKey = new ByteSource() {